From c30d31e6cd168d0655f2445c2d8f222d954e61a6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 18 Apr 2024 16:23:25 -0500 Subject: [PATCH 1/2] test(parser): Show emoji bug --- crates/toml_edit/tests/testsuite/invalid.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/toml_edit/tests/testsuite/invalid.rs b/crates/toml_edit/tests/testsuite/invalid.rs index 5e7acca0..a156792e 100644 --- a/crates/toml_edit/tests/testsuite/invalid.rs +++ b/crates/toml_edit/tests/testsuite/invalid.rs @@ -209,3 +209,12 @@ dotted key `k1` attempted to extend non-table type (integer) let err = toml_input.parse::().unwrap_err(); snapbox::assert_eq(expected_err, err.to_string()); } + +#[test] +#[should_panic] +fn emoji_error_span() { + let input = "😀"; + let err = input.parse::().unwrap_err(); + dbg!(err.span()); + let _ = &input[err.span().unwrap()]; +} From 1a6ed6e0669e98f7c3875223d81fe17d833912f2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 18 Apr 2024 16:38:51 -0500 Subject: [PATCH 2/2] fix(parser): Return correct span for multi-byte characters This is a step towards addressing rust-lang/cargo#13772 --- crates/toml_edit/src/error.rs | 18 +++++++++++------- .../invalid/key/special-character.stderr | 2 +- .../fixtures/invalid/table/no-close-2.stderr | 2 +- crates/toml_edit/tests/testsuite/invalid.rs | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/toml_edit/src/error.rs b/crates/toml_edit/src/error.rs index d9630fca..6a278024 100644 --- a/crates/toml_edit/src/error.rs +++ b/crates/toml_edit/src/error.rs @@ -21,19 +21,23 @@ impl TomlError { ) -> Self { use winnow::stream::Stream; + let message = error.inner().to_string(); + let raw = raw.finish(); + let raw = String::from_utf8(raw.to_owned()).expect("original document was utf8"); + let offset = error.offset(); - let span = if offset == raw.len() { - offset..offset + let mut indices = raw[offset..].char_indices(); + indices.next(); + let len = if let Some((index, _)) = indices.next() { + index } else { - offset..(offset + 1) + raw.len() }; - - let message = error.inner().to_string(); - let raw = raw.finish(); + let span = offset..(offset + len); Self { message, - raw: Some(String::from_utf8(raw.to_owned()).expect("original document was utf8")), + raw: Some(raw), keys: Vec::new(), span: Some(span), } diff --git a/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr b/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr index 7ada2f21..10592cfb 100644 --- a/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr +++ b/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr @@ -1,5 +1,5 @@ TOML parse error at line 1, column 1 | 1 | μ = "greek small letter mu" - | ^ + | ^^ invalid key diff --git a/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr b/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr index a7adf382..26fc7875 100644 --- a/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr +++ b/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr @@ -1,6 +1,6 @@ TOML parse error at line 1, column 25 | 1 | [closing-bracket.missingö - | ^ + | ^^ invalid table header expected `.`, `]` diff --git a/crates/toml_edit/tests/testsuite/invalid.rs b/crates/toml_edit/tests/testsuite/invalid.rs index a156792e..79724163 100644 --- a/crates/toml_edit/tests/testsuite/invalid.rs +++ b/crates/toml_edit/tests/testsuite/invalid.rs @@ -211,10 +211,10 @@ dotted key `k1` attempted to extend non-table type (integer) } #[test] -#[should_panic] fn emoji_error_span() { let input = "😀"; let err = input.parse::().unwrap_err(); dbg!(err.span()); - let _ = &input[err.span().unwrap()]; + let actual = &input[err.span().unwrap()]; + assert_eq!(actual, input); }