Skip to content

Commit

Permalink
tweak unresolved label suggestion
Browse files Browse the repository at this point in the history
Only suggest label names in the same hygiene context, and use a
structured suggestion.
  • Loading branch information
euclio committed Apr 4, 2019
1 parent f8673e0 commit 3b686d5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
15 changes: 13 additions & 2 deletions src/librustc_resolve/lib.rs
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
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
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
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
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
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

0 comments on commit 3b686d5

Please sign in to comment.