Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22707 from Wilfred/mir_crash_projection
fix: Crash when computing diagnostics with MIR and error types
| -rw-r--r-- | crates/hir-ty/src/mir/lower.rs | 7 | ||||
| -rw-r--r-- | crates/hir-ty/src/mir/lower/pattern_matching.rs | 3 | ||||
| -rw-r--r-- | crates/hir-ty/src/mir/lower/tests.rs | 26 |
3 files changed, 33 insertions, 3 deletions
diff --git a/crates/hir-ty/src/mir/lower.rs b/crates/hir-ty/src/mir/lower.rs index d1277762eb..ab7e6df3f5 100644 --- a/crates/hir-ty/src/mir/lower.rs +++ b/crates/hir-ty/src/mir/lower.rs @@ -1404,11 +1404,14 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> { expr_id: ExprId, ) -> Result<'db, ()> { if let Expr::Field { expr, name } = &self.store[expr_id] { - if let TyKind::Tuple(..) = self.expr_ty_after_adjustments(*expr).kind() { + if let TyKind::Tuple(tys) = self.expr_ty_after_adjustments(*expr).kind() { let index = name.as_tuple_index().ok_or(MirLowerError::TypeError("named field on tuple"))? as u32; - *place = place.project(ProjectionElem::Field(FieldIndex(index))) + if tys.get(index as usize).is_none() { + return Err(MirLowerError::TypeError("tuple field index out of range")); + } + *place = place.project(ProjectionElem::Field(FieldIndex(index))); } else { let field = self .infer diff --git a/crates/hir-ty/src/mir/lower/pattern_matching.rs b/crates/hir-ty/src/mir/lower/pattern_matching.rs index f273a823ba..66b51a0e95 100644 --- a/crates/hir-ty/src/mir/lower/pattern_matching.rs +++ b/crates/hir-ty/src/mir/lower/pattern_matching.rs @@ -137,7 +137,8 @@ impl<'db> MirLowerCtx<'_, 'db> { } Pat::Wild => (current, current_else), Pat::Tuple { args, ellipsis } => { - let subst = match self.infer.pat_ty(pattern).kind() { + let place_ty = cond_place.ty(&self.result, &self.infcx, self.env).ty; + let subst = match place_ty.kind() { TyKind::Tuple(s) => s, _ => { return Err(MirLowerError::TypeError( diff --git a/crates/hir-ty/src/mir/lower/tests.rs b/crates/hir-ty/src/mir/lower/tests.rs index 8e10284cc1..d8f7d549d6 100644 --- a/crates/hir-ty/src/mir/lower/tests.rs +++ b/crates/hir-ty/src/mir/lower/tests.rs @@ -108,3 +108,29 @@ pub struct AssocTy { "#, ); } + +#[test] +fn borrowck_tuple_field_projection_recovery_does_not_panic() { + check_borrowck( + r#" +//- minicore: sized +fn tuple_field() { + let t = (1,); + let x = t.1; +} + "#, + ); +} + +#[test] +fn borrowck_alias_projection_recovery_does_not_panic() { + check_borrowck( + r#" +//- minicore: sized +trait Tr { type A; } +fn alias<T: Tr>(x: T::A) { + let (a, b) = x; +} + "#, + ); +} |