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 ICE in struct construction with base expression #91538

Closed
Closed
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
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.diverges.set(Diverges::Maybe);
match g {
hir::Guard::If(e) => {
self.check_expr_has_type_or_error(e, tcx.types.bool, |_| {});
self.check_expr_has_type_or_error(e, tcx.types.bool, |_, _| {});
}
hir::Guard::IfLet(pat, e) => {
let scrutinee_ty = self.demand_scrutinee_type(
Expand Down Expand Up @@ -478,7 +478,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
kind: TypeVariableOriginKind::TypeInference,
span: scrut.span,
});
self.check_expr_has_type_or_error(scrut, scrut_ty, |_| {});
self.check_expr_has_type_or_error(scrut, scrut_ty, |_, _| {});
scrut_ty
}
}
Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
expr: &'tcx hir::Expr<'tcx>,
expected: Ty<'tcx>,
extend_err: impl Fn(&mut DiagnosticBuilder<'_>),
extend_err: impl Fn(&mut DiagnosticBuilder<'_>, Ty<'tcx>),
) -> Ty<'tcx> {
self.check_expr_meets_expectation_or_error(expr, ExpectHasType(expected), extend_err)
}
Expand All @@ -68,7 +68,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
expr: &'tcx hir::Expr<'tcx>,
expected: Expectation<'tcx>,
extend_err: impl Fn(&mut DiagnosticBuilder<'_>),
extend_err: impl Fn(&mut DiagnosticBuilder<'_>, Ty<'tcx>),
) -> Ty<'tcx> {
let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool);
let mut ty = self.check_expr_with_expectation(expr, expected);
Expand All @@ -94,7 +94,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
let expr = expr.peel_drop_temps();
self.suggest_deref_ref_or_into(&mut err, expr, expected_ty, ty, None);
extend_err(&mut err);
extend_err(&mut err, ty);
err.emit();
}
ty
Expand Down Expand Up @@ -907,7 +907,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sp: Span,
orig_expected: Expectation<'tcx>,
) -> Ty<'tcx> {
let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_| {});
let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_, _| {});

self.warn_if_unreachable(
cond_expr.hir_id,
Expand Down Expand Up @@ -1264,7 +1264,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
kind: TypeVariableOriginKind::MiscVariable,
span: element.span,
});
let element_ty = self.check_expr_has_type_or_error(&element, ty, |_| {});
let element_ty = self.check_expr_has_type_or_error(&element, ty, |_, _| {});
(element_ty, ty)
}
};
Expand Down Expand Up @@ -1508,8 +1508,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
} else {
self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
let base_ty = self.check_expr(base_expr);
self.check_expr_has_type_or_error(base_expr, adt_ty, |_, base_ty| {
let same_adt = match (adt_ty.kind(), base_ty.kind()) {
(ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt => true,
_ => false,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::StmtKind::Item(_) => {}
hir::StmtKind::Expr(ref expr) => {
// Check with expected type of `()`.
self.check_expr_has_type_or_error(&expr, self.tcx.mk_unit(), |err| {
self.check_expr_has_type_or_error(&expr, self.tcx.mk_unit(), |err, _| {
if expr.can_have_side_effects() {
self.suggest_semicolon_at_end(expr.span, err);
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/structs/issue-91502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct Bar {}

fn main() {
let old: Option<Bar> = None;

let b = Bar {
..old.as_ref().unwrap() //~ ERROR E0308
};
}
9 changes: 9 additions & 0 deletions src/test/ui/structs/issue-91502.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/issue-91502.rs:7:11
|
LL | ..old.as_ref().unwrap()
| ^^^^^^^^^^^^^^^^^^^^^ expected struct `Bar`, found `&Bar`

error: aborting due to previous error

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