From 5805040d2830a96af48f8c219b3c1586eaf24bef Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Wed, 14 Feb 2024 14:40:55 -0500 Subject: [PATCH] Search span using *unnormalized* ident (#6073) Fixes 6069 Calling `item.ident.as_str()` returns an NFC normalized ident, which might not be what's written in the source code. To avoid panics when calling `snippet_provider.span_after` use the ident from the source. --- src/items.rs | 6 +++--- tests/rustfmt/main.rs | 2 +- tests/target/issue_6069.rs | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 tests/target/issue_6069.rs diff --git a/src/items.rs b/src/items.rs index 8669bdda32b..e7ff5ff818b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1166,9 +1166,9 @@ pub(crate) fn format_trait( // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds. if !bounds.is_empty() { - let ident_hi = context - .snippet_provider - .span_after(item.span, item.ident.as_str()); + // Retrieve *unnormalized* ident (See #6069) + let source_ident = context.snippet(item.ident.span); + let ident_hi = context.snippet_provider.span_after(item.span, source_ident); let bound_hi = bounds.last().unwrap().span().hi(); let snippet = context.snippet(mk_sp(ident_hi, bound_hi)); if contains_comment(snippet) { diff --git a/tests/rustfmt/main.rs b/tests/rustfmt/main.rs index 87b55ca1d1d..11fb4786e82 100644 --- a/tests/rustfmt/main.rs +++ b/tests/rustfmt/main.rs @@ -176,7 +176,7 @@ fn rustfmt_emits_error_on_line_overflow_true() { #[test] #[allow(non_snake_case)] fn dont_emit_ICE() { - let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs"]; + let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs", "tests/target/issue_6069.rs"]; for file in files { let args = [file]; diff --git a/tests/target/issue_6069.rs b/tests/target/issue_6069.rs new file mode 100644 index 00000000000..d866ce7c646 --- /dev/null +++ b/tests/target/issue_6069.rs @@ -0,0 +1,3 @@ +// `Foó` as written here ends with ASCII 6F `'o'` followed by `'\u{0301}'` COMBINING ACUTE ACCENT. +// The compiler normalizes that combination to NFC form, `'\u{00F3}'` LATIN SMALL LETTER O WITH ACUTE. +trait Foó: Bar {}