Skip to content

Commit

Permalink
Rollup merge of rust-lang#59697 - euclio:label-fixes, r=zackmdavis
Browse files Browse the repository at this point in the history
tweak unresolved label suggestion

Only suggest label names in the same hygiene context, and use a
structured suggestion.

Question for reviewer: Is this the right way to check for label hygiene?
  • Loading branch information
Centril committed Apr 25, 2019
2 parents 5440cf1 + 3b686d5 commit a552beb
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
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,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 @@ -4280,7 +4285,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

0 comments on commit a552beb

Please sign in to comment.