Unnamed repository; edit this file 'description' to name the repository.
fix: Crash on MIR lowering for out-of-range tuple access
Projection assumes the tuple is big enough for the field access, so return a MIR lowering error earlier if the tuple isn't valid. AI disclosure: Some Fable 5 and GPT 5.5 usage.
Wilfred Hughes 5 weeks ago
parent 71f31c4 · commit 4f4433e
-rw-r--r--crates/hir-ty/src/mir/lower.rs7
-rw-r--r--crates/hir-ty/src/mir/lower/tests.rs13
2 files changed, 18 insertions, 2 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/tests.rs b/crates/hir-ty/src/mir/lower/tests.rs
index 8e10284cc1..bf13b4c46e 100644
--- a/crates/hir-ty/src/mir/lower/tests.rs
+++ b/crates/hir-ty/src/mir/lower/tests.rs
@@ -108,3 +108,16 @@ 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;
+}
+ "#,
+ );
+}