Skip to content

Commit a552beb

Browse files
authored
Rollup merge of rust-lang#59697 - euclio:label-fixes, r=zackmdavis
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?
2 parents 5440cf1 + 3b686d5 commit a552beb

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

src/librustc_resolve/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,12 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
364364
"use of undeclared label `{}`",
365365
name);
366366
if let Some(lev_candidate) = lev_candidate {
367-
err.span_label(span, format!("did you mean `{}`?", lev_candidate));
367+
err.span_suggestion(
368+
span,
369+
"a label with a similar name exists in this scope",
370+
lev_candidate.to_string(),
371+
Applicability::MaybeIncorrect,
372+
);
368373
} else {
369374
err.span_label(span, format!("undeclared label `{}`", name));
370375
}
@@ -4280,7 +4285,13 @@ impl<'a> Resolver<'a> {
42804285
// Picks the first label that is "close enough", which is not necessarily
42814286
// the closest match
42824287
let close_match = self.search_label(label.ident, |rib, ident| {
4283-
let names = rib.bindings.iter().map(|(id, _)| &id.name);
4288+
let names = rib.bindings.iter().filter_map(|(id, _)| {
4289+
if id.span.ctxt() == label.ident.span.ctxt() {
4290+
Some(&id.name)
4291+
} else {
4292+
None
4293+
}
4294+
});
42844295
find_best_match_for_name(names, &*ident.as_str(), None)
42854296
});
42864297
self.record_def(expr.id, err_path_resolution());

src/test/ui/hygiene/hygienic-label-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
22
--> $DIR/hygienic-label-1.rs:2:19
33
|
44
LL | () => { break 'x; }
5-
| ^^ did you mean `'x`?
5+
| ^^ undeclared label `'x`
66
...
77
LL | 'x: loop { foo!() }
88
| ------ in this macro invocation

src/test/ui/hygiene/hygienic-label-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
22
--> $DIR/hygienic-label-2.rs:6:16
33
|
44
LL | foo!(break 'x);
5-
| ^^ did you mean `'x`?
5+
| ^^ undeclared label `'x`
66

77
error: aborting due to previous error
88

src/test/ui/hygiene/hygienic-label-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
22
--> $DIR/hygienic-label-3.rs:2:19
33
|
44
LL | () => { break 'x; }
5-
| ^^ did you mean `'x`?
5+
| ^^ undeclared label `'x`
66
...
77
LL | foo!()
88
| ------ in this macro invocation

src/test/ui/hygiene/hygienic-label-4.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
22
--> $DIR/hygienic-label-4.rs:6:16
33
|
44
LL | foo!(break 'x);
5-
| ^^ did you mean `'x`?
5+
| ^^ undeclared label `'x`
66

77
error: aborting due to previous error
88

src/test/ui/suggestions/suggest-labels.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@ error[E0426]: use of undeclared label `'fo`
22
--> $DIR/suggest-labels.rs:4:15
33
|
44
LL | break 'fo;
5-
| ^^^ did you mean `'foo`?
5+
| ^^^
6+
help: a label with a similar name exists in this scope
7+
|
8+
LL | break 'foo;
9+
| ^^^^
610

711
error[E0426]: use of undeclared label `'bor`
812
--> $DIR/suggest-labels.rs:8:18
913
|
1014
LL | continue 'bor;
11-
| ^^^^ did you mean `'bar`?
15+
| ^^^^
16+
help: a label with a similar name exists in this scope
17+
|
18+
LL | continue 'bar;
19+
| ^^^^
1220

1321
error[E0426]: use of undeclared label `'longlable`
1422
--> $DIR/suggest-labels.rs:13:19
1523
|
1624
LL | break 'longlable;
17-
| ^^^^^^^^^^ did you mean `'longlabel1`?
25+
| ^^^^^^^^^^
26+
help: a label with a similar name exists in this scope
27+
|
28+
LL | break 'longlabel1;
29+
| ^^^^^^^^^^^
1830

1931
error: aborting due to 3 previous errors
2032

0 commit comments

Comments
 (0)