Skip to content

Commit 87461d8

Browse files
committed
fix(toml): Track dotted key for spans
As this is just `toml`, this is for display purposes only and this can help with visualizing issues, see rust-lang/cargo#16600
1 parent 796370e commit 87461d8

27 files changed

Lines changed: 65 additions & 66 deletions

crates/toml/src/de/parser/key.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ fn more_key(input: &Input<'_>) -> bool {
8686

8787
struct State {
8888
current_key: Option<toml_parser::parser::Event>,
89+
start: usize,
8990
}
9091

9192
impl State {
9293
fn new(key_event: &toml_parser::parser::Event) -> Self {
9394
Self {
9495
current_key: Some(*key_event),
96+
start: key_event.span().start(),
9597
}
9698
}
9799

@@ -108,8 +110,9 @@ impl State {
108110
return;
109111
};
110112

111-
let key_span = key.span();
112-
let key_span = key_span.start()..key_span.end();
113+
let key_start = self.start;
114+
let key_end = key.span().end();
115+
let key_span = key_start..key_end;
113116

114117
let raw = source.get(key).unwrap();
115118
let mut decoded = alloc::borrow::Cow::Borrowed("");

crates/toml/tests/serde/spanned.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,8 @@ fn test_spanned_nested() {
381381
}
382382

383383
fn good(s: &str, foo: &Foo) {
384-
for (k, v) in foo.foo.iter() {
385-
assert_eq!(&s[k.span().start..k.span().end], k.as_ref());
386-
for (n_k, n_v) in v.iter() {
387-
assert_eq!(&s[n_k.span().start..n_k.span().end], n_k.as_ref());
384+
for v in foo.foo.values() {
385+
for n_v in v.values() {
388386
assert_eq!(
389387
&s[(n_v.span().start + 1)..(n_v.span().end - 1)],
390388
n_v.as_ref()
@@ -408,7 +406,7 @@ fn test_spanned_nested() {
408406
Foo {
409407
foo: {
410408
Spanned {
411-
span: 14..15,
409+
span: 10..15,
412410
value: "a",
413411
}: {
414412
Spanned {
@@ -434,7 +432,7 @@ Foo {
434432
},
435433
},
436434
Spanned {
437-
span: 78..81,
435+
span: 74..81,
438436
value: "bar",
439437
}: {
440438
Spanned {
@@ -690,7 +688,7 @@ Map(
690688
[
691689
(
692690
Spanned {
693-
span: 6..9,
691+
span: 2..9,
694692
value: "bar",
695693
},
696694
Spanned {
@@ -708,7 +706,7 @@ Map(
708706
[
709707
(
710708
Spanned {
711-
span: 17..20,
709+
span: 11..20,
712710
value: "bob",
713711
},
714712
Spanned {
@@ -726,7 +724,7 @@ Map(
726724
[
727725
(
728726
Spanned {
729-
span: 29..32,
727+
span: 25..32,
730728
value: "two",
731729
},
732730
Spanned {
@@ -766,19 +764,19 @@ Map(
766764
assert_data_eq!(&INPUT[foo.0.span()], str!["foo"]);
767765
assert_data_eq!(&INPUT[foo.1.span()], str!["foo"]);
768766
let bar = foo.1.get_ref().get("bar").unwrap();
769-
assert_data_eq!(&INPUT[bar.0.span()], str!["bar"]);
767+
assert_data_eq!(&INPUT[bar.0.span()], str!["foo.bar"]);
770768
assert_data_eq!(&INPUT[bar.1.span()], str!["[foo.bar]"]);
771769
let alice = bar.1.get_ref().get("alice").unwrap();
772770
assert_data_eq!(&INPUT[alice.0.span()], str!["alice"]);
773771
assert_data_eq!(&INPUT[alice.1.span()], str!["alice"]);
774772
let bob = alice.1.get_ref().get("bob").unwrap();
775-
assert_data_eq!(&INPUT[bob.0.span()], str!["bob"]);
773+
assert_data_eq!(&INPUT[bob.0.span()], str!["alice.bob"]);
776774
assert_data_eq!(&INPUT[bob.1.span()], str![[r#"{ one.two = "qux" }"#]]);
777775
let one = bob.1.get_ref().get("one").unwrap();
778776
assert_data_eq!(&INPUT[one.0.span()], str!["one"]);
779777
assert_data_eq!(&INPUT[one.1.span()], str!["one"]);
780778
let two = one.1.get_ref().get("two").unwrap();
781-
assert_data_eq!(&INPUT[two.0.span()], str!["two"]);
779+
assert_data_eq!(&INPUT[two.0.span()], str!["one.two"]);
782780
assert_data_eq!(&INPUT[two.1.span()], str![[r#""qux""#]]);
783781
}
784782

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 3, column 5
1+
TOML parse error at line 3, column 1
22
|
33
3 | arr.val1=1
4-
| ^^^^
4+
| ^^^^^^^^
55
duplicate key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 9, column 10
1+
TOML parse error at line 9, column 4
22
|
33
9 | [fruit.variety]
4-
| ^^^^^^^
4+
| ^^^^^^^^^^^^^
55
duplicate key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 2, column 3
1+
TOML parse error at line 2, column 1
22
|
33
2 | a.b = 2
4-
| ^
4+
| ^^^
55
duplicate key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 5, column 4
1+
TOML parse error at line 5, column 2
22
|
33
5 | [_.s]
4-
| ^
4+
| ^^^
55
duplicate key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 4, column 4
1+
TOML parse error at line 4, column 2
22
|
33
4 | [_.s]
4-
| ^
4+
| ^^^
55
duplicate key

crates/toml/tests/snapshots/invalid/ext/table/duplicate-key-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
TOML parse error at line 3, column 4
1+
TOML parse error at line 3, column 2
22
|
33
3 | [a.b]
4-
| ^
4+
| ^^^
55
duplicate key
66

77
---
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 1, column 36
1+
TOML parse error at line 1, column 29
22
|
33
1 | table1 = { table2.dupe = 1, table2.dupe = 2 }
4-
| ^^^^
4+
| ^^^^^^^^^^^
55
duplicate key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TOML parse error at line 1, column 24
1+
TOML parse error at line 1, column 22
22
|
33
1 | tbl = { a.b = "a_b", a.b.c = "a_b_c" }
4-
| ^
4+
| ^^^
55
cannot extend value of type string with a dotted key

0 commit comments

Comments
 (0)