Skip to content

Commit a6f0665

Browse files
authored
Rollup merge of rust-lang#116187 - estebank:small-tweak, r=compiler-errors
Add context to `let: Ty = loop { break };` We weren't accounting for the case where `break` was immediately within the `loop` block.
2 parents 243ce35 + 4b15959 commit a6f0665

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
545545
// Climb the HIR tree to see if the current `Expr` is part of a `break;` statement.
546546
let Some(
547547
hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Semi(&ref p), .. })
548+
| hir::Node::Block(hir::Block { expr: Some(&ref p), .. })
548549
| hir::Node::Expr(&ref p),
549550
) = self.tcx.hir().find(parent_id)
550551
else {

tests/ui/issues/issue-27042.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ LL | | while true { break }; // but here we cite the whole loop
1111
error[E0308]: mismatched types
1212
--> $DIR/issue-27042.rs:6:16
1313
|
14+
LL | let _: i32 =
15+
| - expected because of this assignment
16+
LL | 'a: // in this case, the citation is just the `break`:
1417
LL | loop { break };
15-
| ^^^^^ expected `i32`, found `()`
18+
| ---- ^^^^^ expected `i32`, found `()`
19+
| |
20+
| this loop is expected to be of type `i32`
1621
|
1722
help: give it a value of the expected type
1823
|

tests/ui/loops/loop-labeled-break-value.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0308]: mismatched types
22
--> $DIR/loop-labeled-break-value.rs:3:29
33
|
44
LL | let _: i32 = loop { break };
5-
| ^^^^^ expected `i32`, found `()`
5+
| - ---- ^^^^^ expected `i32`, found `()`
6+
| | |
7+
| | this loop is expected to be of type `i32`
8+
| expected because of this assignment
69
|
710
help: give it a value of the expected type
811
|
@@ -13,7 +16,10 @@ error[E0308]: mismatched types
1316
--> $DIR/loop-labeled-break-value.rs:6:37
1417
|
1518
LL | let _: i32 = 'inner: loop { break 'inner };
16-
| ^^^^^^^^^^^^ expected `i32`, found `()`
19+
| - ---- ^^^^^^^^^^^^ expected `i32`, found `()`
20+
| | |
21+
| | this loop is expected to be of type `i32`
22+
| expected because of this assignment
1723
|
1824
help: give it a value of the expected type
1925
|
@@ -24,7 +30,10 @@ error[E0308]: mismatched types
2430
--> $DIR/loop-labeled-break-value.rs:9:45
2531
|
2632
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } };
27-
| ^^^^^^^^^^^^^ expected `i32`, found `()`
33+
| - ---- ^^^^^^^^^^^^^ expected `i32`, found `()`
34+
| | |
35+
| | this loop is expected to be of type `i32`
36+
| expected because of this assignment
2837
|
2938
help: give it a value of the expected type
3039
|

tests/ui/loops/loop-properly-diverging-2.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0308]: mismatched types
22
--> $DIR/loop-properly-diverging-2.rs:2:23
33
|
44
LL | let x: i32 = loop { break };
5-
| ^^^^^ expected `i32`, found `()`
5+
| - ---- ^^^^^ expected `i32`, found `()`
6+
| | |
7+
| | this loop is expected to be of type `i32`
8+
| expected because of this assignment
69
|
710
help: give it a value of the expected type
811
|

0 commit comments

Comments
 (0)