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.
This commit is contained in:
Jules Bertholet 2024-02-14 14:40:55 -05:00 committed by GitHub
parent 8486837d55
commit 5805040d28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 4 deletions

View File

@ -1166,9 +1166,9 @@ pub(crate) fn format_trait(
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds. // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
if !bounds.is_empty() { if !bounds.is_empty() {
let ident_hi = context // Retrieve *unnormalized* ident (See #6069)
.snippet_provider let source_ident = context.snippet(item.ident.span);
.span_after(item.span, item.ident.as_str()); let ident_hi = context.snippet_provider.span_after(item.span, source_ident);
let bound_hi = bounds.last().unwrap().span().hi(); let bound_hi = bounds.last().unwrap().span().hi();
let snippet = context.snippet(mk_sp(ident_hi, bound_hi)); let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(snippet) { if contains_comment(snippet) {

View File

@ -176,7 +176,7 @@ fn rustfmt_emits_error_on_line_overflow_true() {
#[test] #[test]
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn dont_emit_ICE() { 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 { for file in files {
let args = [file]; let args = [file];

View File

@ -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 {}