Skip to content

tweak unresolved label suggestion #59697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,12 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
"use of undeclared label `{}`",
name);
if let Some(lev_candidate) = lev_candidate {
err.span_label(span, format!("did you mean `{}`?", lev_candidate));
err.span_suggestion(
span,
"a label with a similar name exists in this scope",
lev_candidate.to_string(),
Applicability::MaybeIncorrect,
);
} else {
err.span_label(span, format!("undeclared label `{}`", name));
}
Expand Down Expand Up @@ -4218,7 +4223,13 @@ impl<'a> Resolver<'a> {
// Picks the first label that is "close enough", which is not necessarily
// the closest match
let close_match = self.search_label(label.ident, |rib, ident| {
let names = rib.bindings.iter().map(|(id, _)| &id.name);
let names = rib.bindings.iter().filter_map(|(id, _)| {
if id.span.ctxt() == label.ident.span.ctxt() {
Some(&id.name)
} else {
None
}
});
find_best_match_for_name(names, &*ident.as_str(), None)
});
self.record_def(expr.id, err_path_resolution());
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/hygienic-label-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
--> $DIR/hygienic-label-1.rs:2:19
|
LL | () => { break 'x; }
| ^^ did you mean `'x`?
| ^^ undeclared label `'x`
...
LL | 'x: loop { foo!() }
| ------ in this macro invocation
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/hygienic-label-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
--> $DIR/hygienic-label-2.rs:6:16
|
LL | foo!(break 'x);
| ^^ did you mean `'x`?
| ^^ undeclared label `'x`

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/hygienic-label-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
--> $DIR/hygienic-label-3.rs:2:19
|
LL | () => { break 'x; }
| ^^ did you mean `'x`?
| ^^ undeclared label `'x`
...
LL | foo!()
| ------ in this macro invocation
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/hygienic-label-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
--> $DIR/hygienic-label-4.rs:6:16
|
LL | foo!(break 'x);
| ^^ did you mean `'x`?
| ^^ undeclared label `'x`

error: aborting due to previous error

Expand Down
18 changes: 15 additions & 3 deletions src/test/ui/suggestions/suggest-labels.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@ error[E0426]: use of undeclared label `'fo`
--> $DIR/suggest-labels.rs:4:15
|
LL | break 'fo;
| ^^^ did you mean `'foo`?
| ^^^
help: a label with a similar name exists in this scope
|
LL | break 'foo;
| ^^^^

error[E0426]: use of undeclared label `'bor`
--> $DIR/suggest-labels.rs:8:18
|
LL | continue 'bor;
| ^^^^ did you mean `'bar`?
| ^^^^
help: a label with a similar name exists in this scope
|
LL | continue 'bar;
| ^^^^

error[E0426]: use of undeclared label `'longlable`
--> $DIR/suggest-labels.rs:13:19
|
LL | break 'longlable;
| ^^^^^^^^^^ did you mean `'longlabel1`?
| ^^^^^^^^^^
help: a label with a similar name exists in this scope
|
LL | break 'longlabel1;
| ^^^^^^^^^^^

error: aborting due to 3 previous errors

Expand Down