From d5423f623e831ef24aadc8cb469f760ad4c8bfad Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 31 Jan 2024 09:26:30 -0600 Subject: [PATCH 1/2] test(serde): Show bad span --- crates/toml/tests/testsuite/serde.rs | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/toml/tests/testsuite/serde.rs b/crates/toml/tests/testsuite/serde.rs index d9d7a6db..ab1f98a7 100644 --- a/crates/toml/tests/testsuite/serde.rs +++ b/crates/toml/tests/testsuite/serde.rs @@ -1346,3 +1346,32 @@ fn serialize_array_with_enum_of_optional_struct_field() { let raw = toml::to_string(&input).unwrap(); snapbox::assert_eq(expected, raw); } + +#[test] +fn span_for_sequence_as_map() { + #[allow(dead_code)] + #[derive(Deserialize)] + struct Manifest { + package: Package, + bench: Vec, + } + + #[derive(Deserialize)] + struct Package {} + + #[derive(Deserialize)] + struct Bench {} + + let raw = r#" +[package] +name = "foo" +version = "0.1.0" +edition = "2021" +[[bench.foo]] +"#; + let err = match toml::from_str::(raw) { + Ok(_) => panic!("should fail"), + Err(err) => err, + }; + assert_eq!(err.span(), Some(0..0)); +} From 7e239714d05121fbd9d111224f7a11a7d9d99134 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 31 Jan 2024 10:26:03 -0600 Subject: [PATCH 2/2] fix(serde): Improve spans for empty tables Fixes #669 --- crates/toml/tests/testsuite/serde.rs | 2 +- crates/toml_edit/src/de/table.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/toml/tests/testsuite/serde.rs b/crates/toml/tests/testsuite/serde.rs index ab1f98a7..d7f8bc40 100644 --- a/crates/toml/tests/testsuite/serde.rs +++ b/crates/toml/tests/testsuite/serde.rs @@ -1373,5 +1373,5 @@ edition = "2021" Ok(_) => panic!("should fail"), Err(err) => err, }; - assert_eq!(err.span(), Some(0..0)); + assert_eq!(err.span(), Some(61..66)); } diff --git a/crates/toml_edit/src/de/table.rs b/crates/toml_edit/src/de/table.rs index 0b6183e0..33aa3970 100644 --- a/crates/toml_edit/src/de/table.rs +++ b/crates/toml_edit/src/de/table.rs @@ -118,7 +118,7 @@ impl crate::InlineTable { pub(crate) struct TableMapAccess { iter: indexmap::map::IntoIter, span: Option>, - value: Option<(crate::InternalString, crate::Item)>, + value: Option<(crate::Key, crate::Item)>, } impl TableMapAccess { @@ -149,7 +149,7 @@ impl<'de> serde::de::MapAccess<'de> for TableMapAccess { } e }); - self.value = Some((v.key.into(), v.value)); + self.value = Some((v.key, v.value)); ret } None => Ok(None), @@ -162,13 +162,13 @@ impl<'de> serde::de::MapAccess<'de> for TableMapAccess { { match self.value.take() { Some((k, v)) => { - let span = v.span(); + let span = v.span().or_else(|| k.span()); seed.deserialize(crate::de::ValueDeserializer::new(v)) .map_err(|mut e: Self::Error| { if e.span().is_none() { e.set_span(span); } - e.add_key(k.as_str().to_owned()); + e.add_key(k.get().to_owned()); e }) }