Skip to content

Commit

Permalink
Rollup merge of rust-lang#88451 - theo-lw:issue-87771, r=jackh726
Browse files Browse the repository at this point in the history
Fix an ICE caused by type mismatch errors being ignored

This PR fixes rust-lang#87771. It turns out that the check on `compiler/rustc_typeck/src/check/demand.rs:148` leads to the ICE. I removed it because the early return in [`check_expr_assign`](https://github.com/theo-lw/rust/blob/dec7fc3ced5bc3c18d0e5d29921d087f93189cb8/compiler/rustc_typeck/src/check/expr.rs#L928) already prevents unnecessary error messages from the call to `check_expr_coercable_to_type`.
  • Loading branch information
Manishearth authored Oct 4, 2021
2 parents 6a00ed9 + 09d55d5 commit 7b1ad3e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
16 changes: 1 addition & 15 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1484,21 +1484,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
fcx.emit_coerce_suggestions(&mut err, expr, found, expected, None);
}

// Error possibly reported in `check_assign` so avoid emitting error again.
let assign_to_bool = expression
// #67273: Use initial expected type as opposed to `expected`.
// Otherwise we end up using prior coercions in e.g. a `match` expression:
// ```
// match i {
// 0 => true, // Because of this...
// 1 => i = 1, // ...`expected == bool` now, but not when checking `i = 1`.
// _ => (),
// };
// ```
.filter(|e| fcx.is_assign_to_bool(e, self.expected_ty()))
.is_some();

err.emit_unless(assign_to_bool || unsized_return);
err.emit_unless(unsized_return);

self.final_ty = Some(fcx.tcx.ty_error());
}
Expand Down
14 changes: 0 additions & 14 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let expr_ty = self.resolve_vars_with_obligations(checked_ty);
let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);

if self.is_assign_to_bool(expr, expected) {
// Error reported in `check_assign` so avoid emitting error again.
err.delay_as_bug();
return (expected, None);
}

self.emit_coerce_suggestions(&mut err, expr, expr_ty, expected, expected_ty_expr);

(expected, Some(err))
Expand All @@ -172,14 +166,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

/// Returns whether the expected type is `bool` and the expression is `x = y`.
pub fn is_assign_to_bool(&self, expr: &hir::Expr<'_>, expected: Ty<'tcx>) -> bool {
if let hir::ExprKind::Assign(..) = expr.kind {
return expected == self.tcx.types.bool;
}
false
}

/// If the expected type is an enum (Issue #55250) with any variants whose
/// sole field is of the found type, suggest such variants. (Issue #42764)
fn suggest_compatible_variants(
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let expr = expr.peel_drop_temps();
self.suggest_deref_ref_or_into(&mut err, expr, expected_ty, ty, None);
extend_err(&mut err);
// Error possibly reported in `check_assign` so avoid emitting error again.
err.emit_unless(self.is_assign_to_bool(expr, expected_ty));
err.emit();
}
ty
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/typeck/issue-87771-ice-assign-assign-to-bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let mut a;
a = a = true; //~ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/issue-87771-ice-assign-assign-to-bool.rs:3:9
|
LL | a = a = true;
| ^^^^^^^^ expected `bool`, found `()`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 7b1ad3e

Please sign in to comment.