Skip to content

Commit

Permalink
Rollup merge of #125795 - lucasscharenbroch:undescore-prefix-suggesti…
Browse files Browse the repository at this point in the history
…on, r=compiler-errors

Improve renaming suggestion for names with leading underscores

Fixes #125650

Before:
```
error[E0425]: cannot find value `p` in this scope
 --> test.rs:2:13
  |
2 |     let _ = p;
  |             ^
  |
help: a local variable with a similar name exists, consider renaming `_p` into `p`
  |
1 | fn a(p: i32) {
  |      ~
```

After:
```
error[E0425]: cannot find value `p` in this scope
 --> test.rs:2:13
  |
1 | fn a(_p: i32) {
  |      -- `_p` defined here
2 |     let _ = p;
  |             ^
  |
help: the leading underscore in `_p` marks it as unused, consider renaming it to `p`
  |
1 | fn a(p: i32) {
  |      ~
```

This change doesn't exactly conform to what was proposed in the issue:

1. I've kept the suggested code instead of solely replacing it with the label
2. I've removed the "...similar name exists..." message instead of relocating to the usage span
3. You could argue that it still isn't completely clear that the change is referring to the definition (not the usage), but I'm not sure how to do this without playing down the fact that the error was caused by the usage of an undefined name.
  • Loading branch information
compiler-errors committed Jun 4, 2024
2 parents 46a0339 + 9af9674 commit 7e5528f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
38 changes: 24 additions & 14 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
Some(suggestion) => suggestion,
};

let mut did_label_def_span = false;

if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) {
if span.overlaps(def_span) {
// Don't suggest typo suggestion for itself like in the following:
Expand Down Expand Up @@ -1595,31 +1598,38 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
errors::DefinedHere::SingleItem { span, candidate_descr, candidate }
}
};
did_label_def_span = true;
err.subdiagnostic(self.tcx.dcx(), label);
}

let (span, sugg, post) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
let (span, msg, sugg) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
&& let Some(span) = suggestion.span
&& let Some(candidate) = suggestion.candidate.as_str().strip_prefix('_')
&& snippet == candidate
{
let candidate = suggestion.candidate;
// When the suggested binding change would be from `x` to `_x`, suggest changing the
// original binding definition instead. (#60164)
let post = format!(", consider renaming `{}` into `{snippet}`", suggestion.candidate);
(span, snippet, post)
} else {
(span, suggestion.candidate.to_ident_string(), String::new())
};
let msg = match suggestion.target {
SuggestionTarget::SimilarlyNamed => format!(
"{} {} with a similar name exists{post}",
suggestion.res.article(),
suggestion.res.descr()
),
SuggestionTarget::SingleItem => {
format!("maybe you meant this {}", suggestion.res.descr())
let msg = format!(
"the leading underscore in `{candidate}` marks it as unused, consider renaming it to `{snippet}`"
);
if !did_label_def_span {
err.span_label(span, format!("`{candidate}` defined here"));
}
(span, msg, snippet)
} else {
let msg = match suggestion.target {
SuggestionTarget::SimilarlyNamed => format!(
"{} {} with a similar name exists",
suggestion.res.article(),
suggestion.res.descr()
),
SuggestionTarget::SingleItem => {
format!("maybe you meant this {}", suggestion.res.descr())
}
};
(span, msg, suggestion.candidate.to_ident_string())
};
err.span_suggestion(span, msg, sugg, Applicability::MaybeIncorrect);
true
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/macros/expand-full-no-resolution.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | macro_rules! _a {
LL | format_args!(a!());
| ^
|
help: a macro with a similar name exists, consider renaming `_a` into `a`
help: the leading underscore in `_a` marks it as unused, consider renaming it to `a`
|
LL | macro_rules! a {
| ~
Expand All @@ -21,7 +21,7 @@ LL | macro_rules! _a {
LL | env!(a!());
| ^
|
help: a macro with a similar name exists, consider renaming `_a` into `a`
help: the leading underscore in `_a` marks it as unused, consider renaming it to `a`
|
LL | macro_rules! a {
| ~
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/suggestions/silenced-binding-typo.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error[E0425]: cannot find value `x` in this scope
--> $DIR/silenced-binding-typo.rs:4:14
|
LL | let _x = 42;
| -- `_x` defined here
LL | let _y = x;
| ^
|
help: a local variable with a similar name exists, consider renaming `_x` into `x`
help: the leading underscore in `_x` marks it as unused, consider renaming it to `x`
|
LL | let x = 42;
| ~
Expand Down

0 comments on commit 7e5528f

Please sign in to comment.