Skip to content

Commit a4b95ee

Browse files
authored
Rollup merge of #80023 - sasurau4:feature/enhance-error-message-when-wrongly-written-broken-label, r=lcnr
Enhance error message when misspelled label to value in break expression Fix #79424
2 parents 2e9ed6f + e9ca290 commit a4b95ee

File tree

5 files changed

+113
-2
lines changed

5 files changed

+113
-2
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+20
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,26 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
542542
err.span_label(base_span, fallback_label);
543543
}
544544
}
545+
if let Some(err_code) = &err.code {
546+
if err_code == &rustc_errors::error_code!(E0425) {
547+
for label_rib in &self.label_ribs {
548+
for (label_ident, _) in &label_rib.bindings {
549+
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+
);
559+
}
560+
}
561+
}
562+
}
563+
}
564+
545565
(err, candidates)
546566
}
547567

src/test/ui/label/label_misspelled.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn main() {
2+
'LOOP: loop {
3+
LOOP;
4+
//~^ ERROR cannot find value `LOOP` in this scope
5+
};
6+
'while_loop: while true { //~ WARN denote infinite loops with
7+
while_loop;
8+
//~^ ERROR cannot find value `while_loop` in this scope
9+
};
10+
'while_let: while let Some(_) = Some(()) {
11+
while_let;
12+
//~^ ERROR cannot find value `while_let` in this scope
13+
}
14+
'for_loop: for _ in 0..3 {
15+
for_loop;
16+
//~^ ERROR cannot find value `for_loop` in this scope
17+
};
18+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0425]: cannot find value `LOOP` in this scope
2+
--> $DIR/label_misspelled.rs:3:9
3+
|
4+
LL | LOOP;
5+
| ^^^^
6+
| |
7+
| not found in this scope
8+
| help: a label with a similar name exists: `'LOOP`
9+
10+
error[E0425]: cannot find value `while_loop` in this scope
11+
--> $DIR/label_misspelled.rs:7:9
12+
|
13+
LL | while_loop;
14+
| ^^^^^^^^^^
15+
| |
16+
| not found in this scope
17+
| help: a label with a similar name exists: `'while_loop`
18+
19+
error[E0425]: cannot find value `while_let` in this scope
20+
--> $DIR/label_misspelled.rs:11:9
21+
|
22+
LL | while_let;
23+
| ^^^^^^^^^
24+
| |
25+
| not found in this scope
26+
| help: a label with a similar name exists: `'while_let`
27+
28+
error[E0425]: cannot find value `for_loop` in this scope
29+
--> $DIR/label_misspelled.rs:15:9
30+
|
31+
LL | for_loop;
32+
| ^^^^^^^^
33+
| |
34+
| not found in this scope
35+
| help: a label with a similar name exists: `'for_loop`
36+
37+
warning: denote infinite loops with `loop { ... }`
38+
--> $DIR/label_misspelled.rs:6:5
39+
|
40+
LL | 'while_loop: while true {
41+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
42+
|
43+
= note: `#[warn(while_true)]` on by default
44+
45+
error: aborting due to 4 previous errors; 1 warning emitted
46+
47+
For more information about this error, try `rustc --explain E0425`.

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

+6
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@ fn main() {
9090
break; //~ ERROR mismatched types
9191
break 4;
9292
};
93+
94+
'LOOP: for _ in 0 .. 9 {
95+
break LOOP;
96+
//~^ ERROR cannot find value `LOOP` in this scope
97+
//~| ERROR `break` with value from a `for` loop
98+
}
9399
}

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error[E0425]: cannot find value `LOOP` in this scope
2+
--> $DIR/loop-break-value.rs:95:15
3+
|
4+
LL | break LOOP;
5+
| ^^^^
6+
| |
7+
| not found in this scope
8+
| help: a label with a similar name exists: `'LOOP`
9+
110
warning: denote infinite loops with `loop { ... }`
211
--> $DIR/loop-break-value.rs:26:5
312
|
@@ -94,6 +103,17 @@ help: instead, use `break` on its own without a value inside this `for` loop
94103
LL | break;
95104
| ^^^^^
96105

106+
error[E0571]: `break` with value from a `for` loop
107+
--> $DIR/loop-break-value.rs:95:9
108+
|
109+
LL | break LOOP;
110+
| ^^^^^^^^^^ can only break with a value inside `loop` or breakable block
111+
|
112+
help: instead, use `break` on its own without a value inside this `for` loop
113+
|
114+
LL | break;
115+
| ^^^^^
116+
97117
error[E0308]: mismatched types
98118
--> $DIR/loop-break-value.rs:4:31
99119
|
@@ -151,7 +171,7 @@ LL | break;
151171
| expected integer, found `()`
152172
| help: give it a value of the expected type: `break value`
153173

154-
error: aborting due to 16 previous errors; 1 warning emitted
174+
error: aborting due to 18 previous errors; 1 warning emitted
155175

156-
Some errors have detailed explanations: E0308, E0571.
176+
Some errors have detailed explanations: E0308, E0425, E0571.
157177
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)