Skip to content

Commit b243d01

Browse files
Rollup merge of #151854 - chenyukang:yukang-fix-142602-let-else-break-diag, r=Kivooeo
Show break type expectation cause for let-else Fixes #142602
2 parents cfadcbe + 9c135ad commit b243d01

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
605605
parent_id = self.tcx.parent_hir_id(*hir_id);
606606
parent
607607
}
608+
hir::Node::Stmt(hir::Stmt { hir_id, kind: hir::StmtKind::Let(_), .. }) => {
609+
parent_id = self.tcx.parent_hir_id(*hir_id);
610+
parent
611+
}
612+
hir::Node::LetStmt(hir::LetStmt { hir_id, .. }) => {
613+
parent_id = self.tcx.parent_hir_id(*hir_id);
614+
parent
615+
}
608616
hir::Node::Block(_) => {
609617
parent_id = self.tcx.parent_hir_id(parent_id);
610618
parent
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// testcase from https://github.com/rust-lang/rust/issues/142602
2+
3+
pub fn main() {
4+
// Case 1: break before let-else
5+
let _a = loop {
6+
if true {
7+
break;
8+
}
9+
let Some(_) = Some(5) else {
10+
break 3; //~ ERROR mismatched types
11+
};
12+
};
13+
14+
// Case 2: two let-else statements
15+
let _b = loop {
16+
let Some(_) = Some(5) else {
17+
break;
18+
};
19+
let Some(_) = Some(4) else {
20+
break 3; //~ ERROR mismatched types
21+
};
22+
};
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/let-else-break-help-issue-142602.rs:10:19
3+
|
4+
LL | break;
5+
| ----- expected because of this `break`
6+
...
7+
LL | break 3;
8+
| ^ expected `()`, found integer
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/let-else-break-help-issue-142602.rs:20:19
12+
|
13+
LL | break;
14+
| ----- expected because of this `break`
15+
...
16+
LL | break 3;
17+
| ^ expected `()`, found integer
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)