Skip to content
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

Fix an ICE caused by type mismatch errors being ignored #88451

Merged
merged 2 commits into from
Oct 4, 2021
Merged
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
16 changes: 1 addition & 15 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
@@ -1494,21 +1494,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());
}
14 changes: 0 additions & 14 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
@@ -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))
@@ -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(
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
@@ -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
}
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`.