Unnamed repository; edit this file 'description' to name the repository.
fix: MIR projection crash on tuple destructuring with type aliases
When lowering `let (x, y) = not_a_tuple`, we'd panic if `not_a_tuple` was an alias. can't project out of Alias(AliasTy { kind: Projection { def_id: TypeAliasId("A") }, .. }) This was because we checked the inferred pattern type from the `let (x, y)`, concluded it was a tuple, and then tried to project tuple elements from a non-tuple type (an alias in this case). Instead, use the place type (`not_a_tuple` in this case) to check that the expression is actually a tuple we can project out of, and emit a lowering error otherwise. AI disclosure: Some Fable 5 and GPT 5.5 usage.
Wilfred Hughes 5 weeks ago
parent 4f4433e · commit 9433250
-rw-r--r--crates/hir-ty/src/mir/lower/pattern_matching.rs3
-rw-r--r--crates/hir-ty/src/mir/lower/tests.rs13
2 files changed, 15 insertions, 1 deletions
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 bf13b4c46e..d8f7d549d6 100644
--- a/crates/hir-ty/src/mir/lower/tests.rs
+++ b/crates/hir-ty/src/mir/lower/tests.rs
@@ -121,3 +121,16 @@ fn tuple_field() {
"#,
);
}
+
+#[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;
+}
+ "#,
+ );
+}