Skip to content

Commit a701ff9

Browse files
committed
Suggest 'a when given a only when appropriate
When encountering a name `a` that isn't resolved, but a label `'a` is found in the current ribs, only suggest `'a` if this name is the value expression of a `break` statement. Solve FIXME.
1 parent 707ce2b commit a701ff9

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22662266
visit::walk_expr(self, expr);
22672267
}
22682268

2269+
ExprKind::Break(None, Some(ref e)) => {
2270+
// We use this instead of `visit::walk_expr` to keep the parent expr around for
2271+
// better
2272+
self.resolve_expr(e, Some(&expr));
2273+
}
2274+
22692275
ExprKind::Let(ref pat, ref scrutinee) => {
22702276
self.visit_expr(scrutinee);
22712277
self.resolve_pattern_top(pat, PatternSource::Let);

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,15 +547,19 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
547547
for label_rib in &self.label_ribs {
548548
for (label_ident, _) in &label_rib.bindings {
549549
if format!("'{}", ident) == label_ident.to_string() {
550-
let msg = "a label with a similar name exists";
551-
// FIXME: consider only emitting this suggestion if a label would be valid here
552-
// which is pretty much only the case for `break` expressions.
553-
err.span_suggestion(
554-
span,
555-
&msg,
556-
label_ident.name.to_string(),
557-
Applicability::MaybeIncorrect,
558-
);
550+
err.span_label(label_ident.span, "a label with a similar name exists");
551+
if let PathSource::Expr(Some(Expr {
552+
kind: ExprKind::Break(None, Some(_)),
553+
..
554+
})) = source
555+
{
556+
err.span_suggestion(
557+
span,
558+
"use the similarly named label",
559+
label_ident.name.to_string(),
560+
Applicability::MaybeIncorrect,
561+
);
562+
}
559563
}
560564
}
561565
}

src/test/ui/label/label_misspelled.stderr

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,78 @@
11
error[E0425]: cannot find value `LOOP` in this scope
22
--> $DIR/label_misspelled.rs:3:9
33
|
4+
LL | 'LOOP: loop {
5+
| ----- a label with a similar name exists
46
LL | LOOP;
5-
| ^^^^
6-
| |
7-
| not found in this scope
8-
| help: a label with a similar name exists: `'LOOP`
7+
| ^^^^ not found in this scope
98

109
error[E0425]: cannot find value `while_loop` in this scope
1110
--> $DIR/label_misspelled.rs:7:9
1211
|
12+
LL | 'while_loop: while true {
13+
| ----------- a label with a similar name exists
1314
LL | while_loop;
14-
| ^^^^^^^^^^
15-
| |
16-
| not found in this scope
17-
| help: a label with a similar name exists: `'while_loop`
15+
| ^^^^^^^^^^ not found in this scope
1816

1917
error[E0425]: cannot find value `while_let` in this scope
2018
--> $DIR/label_misspelled.rs:11:9
2119
|
20+
LL | 'while_let: while let Some(_) = Some(()) {
21+
| ---------- a label with a similar name exists
2222
LL | while_let;
23-
| ^^^^^^^^^
24-
| |
25-
| not found in this scope
26-
| help: a label with a similar name exists: `'while_let`
23+
| ^^^^^^^^^ not found in this scope
2724

2825
error[E0425]: cannot find value `for_loop` in this scope
2926
--> $DIR/label_misspelled.rs:15:9
3027
|
28+
LL | 'for_loop: for _ in 0..3 {
29+
| --------- a label with a similar name exists
3130
LL | for_loop;
32-
| ^^^^^^^^
33-
| |
34-
| not found in this scope
35-
| help: a label with a similar name exists: `'for_loop`
31+
| ^^^^^^^^ not found in this scope
3632

3733
error[E0425]: cannot find value `LOOP` in this scope
3834
--> $DIR/label_misspelled.rs:22:15
3935
|
36+
LL | 'LOOP: loop {
37+
| ----- a label with a similar name exists
4038
LL | break LOOP;
4139
| ^^^^
4240
| |
4341
| not found in this scope
44-
| help: a label with a similar name exists: `'LOOP`
42+
| help: use the similarly named label: `'LOOP`
4543

4644
error[E0425]: cannot find value `while_loop` in this scope
4745
--> $DIR/label_misspelled.rs:26:15
4846
|
47+
LL | 'while_loop: while true {
48+
| ----------- a label with a similar name exists
4949
LL | break while_loop;
5050
| ^^^^^^^^^^
5151
| |
5252
| not found in this scope
53-
| help: a label with a similar name exists: `'while_loop`
53+
| help: use the similarly named label: `'while_loop`
5454

5555
error[E0425]: cannot find value `while_let` in this scope
5656
--> $DIR/label_misspelled.rs:31:15
5757
|
58+
LL | 'while_let: while let Some(_) = Some(()) {
59+
| ---------- a label with a similar name exists
5860
LL | break while_let;
5961
| ^^^^^^^^^
6062
| |
6163
| not found in this scope
62-
| help: a label with a similar name exists: `'while_let`
64+
| help: use the similarly named label: `'while_let`
6365

6466
error[E0425]: cannot find value `for_loop` in this scope
6567
--> $DIR/label_misspelled.rs:36:15
6668
|
69+
LL | 'for_loop: for _ in 0..3 {
70+
| --------- a label with a similar name exists
6771
LL | break for_loop;
6872
| ^^^^^^^^
6973
| |
7074
| not found in this scope
71-
| help: a label with a similar name exists: `'for_loop`
75+
| help: use the similarly named label: `'for_loop`
7276

7377
warning: denote infinite loops with `loop { ... }`
7478
--> $DIR/label_misspelled.rs:6:5

src/test/ui/loops/loop-break-value.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error[E0425]: cannot find value `LOOP` in this scope
22
--> $DIR/loop-break-value.rs:95:15
33
|
4+
LL | 'LOOP: for _ in 0 .. 9 {
5+
| ----- a label with a similar name exists
46
LL | break LOOP;
57
| ^^^^
68
| |
79
| not found in this scope
8-
| help: a label with a similar name exists: `'LOOP`
10+
| help: use the similarly named label: `'LOOP`
911

1012
warning: denote infinite loops with `loop { ... }`
1113
--> $DIR/loop-break-value.rs:26:5

0 commit comments

Comments
 (0)