1
Fork 0

Rollup merge of #127886 - estebank:as-rename-suggestion, r=compiler-errors

Accurate `use` rename suggestion span

When suggesting to rename an import with `as`, use a smaller span to render the suggestion with a better format:

```
error[E0252]: the name `baz` is defined multiple times
  --> $DIR/issue-25396.rs:4:5
   |
LL | use foo::baz;
   |     -------- previous import of the module `baz` here
LL | use bar::baz;
   |     ^^^^^^^^ `baz` reimported here
   |
   = note: `baz` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
   |
LL | use bar::baz as other_baz;
   |              ++++++++++++
```
This commit is contained in:
Matthias Krüger 2024-07-18 08:09:01 +02:00 committed by GitHub
commit b52883decf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 47 additions and 45 deletions

View file

@ -371,6 +371,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
};
let mut suggestion = None;
let mut span = binding_span;
match import.kind {
ImportKind::Single { type_ns_only: true, .. } => {
suggestion = Some(format!("self as {suggested_name}"))
@ -381,12 +382,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
{
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span) {
if pos <= snippet.len() {
suggestion = Some(format!(
"{} as {}{}",
&snippet[..pos],
suggested_name,
if snippet.ends_with(';') { ";" } else { "" }
))
span = binding_span
.with_lo(binding_span.lo() + BytePos(pos as u32))
.with_hi(
binding_span.hi()
- BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
);
suggestion = Some(format!(" as {suggested_name}"));
}
}
}
@ -402,9 +404,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
if let Some(suggestion) = suggestion {
err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion });
err.subdiagnostic(ChangeImportBindingSuggestion { span, suggestion });
} else {
err.subdiagnostic(ChangeImportBinding { span: binding_span });
err.subdiagnostic(ChangeImportBinding { span });
}
}