Skip to content

Commit 80829ce

Browse files
Rollup merge of #112392 - jieyouxu:issue-112385, r=compiler-errors
Fix ICE for while loop with assignment condition with LHS place expr Fixes #112385.
2 parents 5b7eba6 + adbfd0d commit 80829ce

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16401640
hir::Stmt {
16411641
kind:
16421642
hir::StmtKind::Expr(hir::Expr {
1643-
kind: hir::ExprKind::Assign(..),
1643+
kind: hir::ExprKind::Assign(lhs, ..),
16441644
..
16451645
}),
16461646
..
@@ -1650,7 +1650,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16501650
} = blk
16511651
{
16521652
self.comes_from_while_condition(blk.hir_id, |_| {
1653-
err.downgrade_to_delayed_bug();
1653+
// We cannot suppress the error if the LHS of assignment
1654+
// is a syntactic place expression because E0070 would
1655+
// not be emitted by `check_lhs_assignable`.
1656+
let res = self.typeck_results.borrow().expr_ty_opt(lhs);
1657+
1658+
if !lhs.is_syntactic_place_expr()
1659+
|| res.references_error()
1660+
{
1661+
err.downgrade_to_delayed_bug();
1662+
}
16541663
})
16551664
}
16561665
}

tests/ui/suggestions/while-let-typo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn main() {
22
let foo = Some(0);
33
let bar = None;
44
while Some(x) = foo {} //~ ERROR cannot find value `x` in this scope
5-
while Some(foo) = bar {}
5+
while Some(foo) = bar {} //~ ERROR mismatched types
66
while 3 = foo {} //~ ERROR mismatched types
77
while Some(3) = foo {} //~ ERROR invalid left-hand side of assignment
88
while x = 5 {} //~ ERROR cannot find value `x` in this scope

tests/ui/suggestions/while-let-typo.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ help: you might have meant to use pattern matching
2020
LL | while let x = 5 {}
2121
| +++
2222

23+
error[E0308]: mismatched types
24+
--> $DIR/while-let-typo.rs:5:11
25+
|
26+
LL | while Some(foo) = bar {}
27+
| ^^^^^^^^^^^^^^^ expected `bool`, found `()`
28+
|
29+
help: consider adding `let`
30+
|
31+
LL | while let Some(foo) = bar {}
32+
| +++
33+
2334
error[E0308]: mismatched types
2435
--> $DIR/while-let-typo.rs:6:11
2536
|
@@ -39,7 +50,7 @@ help: you might have meant to use pattern destructuring
3950
LL | while let Some(3) = foo {}
4051
| +++
4152

42-
error: aborting due to 4 previous errors
53+
error: aborting due to 5 previous errors
4354

4455
Some errors have detailed explanations: E0070, E0308, E0425.
4556
For more information about an error, try `rustc --explain E0070`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Previously, the while loop with an assignment statement (mistakenly) as the condition
2+
// which has a place expr as the LHS would trigger an ICE in typeck.
3+
// Reduced from https://github.com/rust-lang/rust/issues/112385.
4+
5+
fn main() {
6+
let foo = Some(());
7+
while Some(foo) = None {}
8+
//~^ ERROR mismatched types
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-112385-while-assign-lhs-place-expr-ice.rs:7:11
3+
|
4+
LL | while Some(foo) = None {}
5+
| ^^^^^^^^^^^^^^^^ expected `bool`, found `()`
6+
|
7+
help: consider adding `let`
8+
|
9+
LL | while let Some(foo) = None {}
10+
| +++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)