Skip to content

Do not ICE when reassigning in GatherLocalsVisitor on the bad path #140827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions compiler/rustc_hir_typeck/src/gather_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,26 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
}

fn assign(&mut self, span: Span, nid: HirId, ty_opt: Option<Ty<'tcx>>) -> Ty<'tcx> {
// We evaluate expressions twice occasionally in diagnostics for better
// type information or because it needs type information out-of-order.
// In order to not ICE and not lead to knock-on ambiguity errors, if we
// try to re-assign a type to a local, then just take out the previous
// type and delay a bug.
if let Some(&local) = self.fcx.locals.borrow_mut().get(&nid) {
self.fcx.dcx().span_delayed_bug(span, "evaluated expression more than once");
return local;
}

match ty_opt {
None => {
// Infer the variable's type.
let var_ty = self.fcx.next_ty_var(span);
assert_eq!(self.fcx.locals.borrow_mut().insert(nid, var_ty), None);
self.fcx.locals.borrow_mut().insert(nid, var_ty);
var_ty
}
Some(typ) => {
// Take type that the user specified.
assert_eq!(self.fcx.locals.borrow_mut().insert(nid, typ), None);
self.fcx.locals.borrow_mut().insert(nid, typ);
typ
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/typeck/gather-locals-twice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Regression test for <https://github.com/rust-lang/rust/issues/140785>.

fn main() {
() += { let x; };
//~^ ERROR binary assignment operation `+=` cannot be applied to type `()`
//~| ERROR invalid left-hand side of assignment
}
20 changes: 20 additions & 0 deletions tests/ui/typeck/gather-locals-twice.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0368]: binary assignment operation `+=` cannot be applied to type `()`
--> $DIR/gather-locals-twice.rs:4:5
|
LL | () += { let x; };
| --^^^^^^^^^^^^^^
| |
| cannot use `+=` on type `()`

error[E0067]: invalid left-hand side of assignment
--> $DIR/gather-locals-twice.rs:4:8
|
LL | () += { let x; };
| -- ^^
| |
| cannot assign to this expression

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0067, E0368.
For more information about an error, try `rustc --explain E0067`.
Loading