From af50e8d955caa0b689f3e5b02f0d8ff0302fd3e3 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 21 Mar 2021 18:18:25 +0100 Subject: Fix box pattern inference panic --- crates/hir_ty/src/infer/expr.rs | 6 +++--- crates/hir_ty/src/infer/pat.rs | 32 ++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 79bbc5dab..24deff707 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -513,10 +513,10 @@ impl<'a> InferenceContext<'a> { let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); if let Some(box_) = self.resolve_boxed_box() { let mut sb = - Substitution::builder(generics(self.db.upcast(), box_.into()).len()); + Substitution::build_for_generics(&generics(self.db.upcast(), box_.into())); sb = sb.push(inner_ty); - match self.db.generic_defaults(box_.into()).as_ref() { - [_, alloc_ty, ..] if !alloc_ty.value.is_unknown() => { + match self.db.generic_defaults(box_.into()).get(1) { + Some(alloc_ty) if !alloc_ty.value.is_unknown() && sb.remaining() > 0 => { sb = sb.push(alloc_ty.value.clone()); } _ => (), diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index ec491648f..474363709 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -13,7 +13,9 @@ use hir_expand::name::Name; use super::{BindingMode, Expectation, InferenceContext}; use crate::{ - lower::lower_to_chalk_mutability, utils::variant_data, Interner, Substitution, Ty, TyKind, + lower::lower_to_chalk_mutability, + utils::{generics, variant_data}, + Interner, Substitution, Ty, TyKind, }; impl<'a> InferenceContext<'a> { @@ -233,13 +235,31 @@ impl<'a> InferenceContext<'a> { Pat::Lit(expr) => self.infer_expr(*expr, &Expectation::has_type(expected.clone())), Pat::Box { inner } => match self.resolve_boxed_box() { Some(box_adt) => { - let inner_expected = match expected.as_adt() { - Some((adt, substs)) if adt == box_adt => substs.as_single().clone(), - _ => self.result.standard_types.unknown.clone(), + let (inner_ty, alloc_ty) = match expected.as_adt() { + Some((adt, subst)) if adt == box_adt => { + (subst[0].clone(), subst.get(1).cloned()) + } + _ => (self.result.standard_types.unknown.clone(), None), }; - let inner_ty = self.infer_pat(*inner, &inner_expected, default_bm); - Ty::adt_ty(box_adt, Substitution::single(inner_ty)) + let inner_ty = self.infer_pat(*inner, &inner_ty, default_bm); + let mut sb = Substitution::build_for_generics(&generics( + self.db.upcast(), + box_adt.into(), + )); + sb = sb.push(inner_ty); + if sb.remaining() == 1 { + sb = sb.push(match alloc_ty { + Some(alloc_ty) if !alloc_ty.is_unknown() => alloc_ty, + _ => match self.db.generic_defaults(box_adt.into()).get(1) { + Some(alloc_ty) if !alloc_ty.value.is_unknown() => { + alloc_ty.value.clone() + } + _ => self.table.new_type_var(), + }, + }); + } + Ty::adt_ty(box_adt, sb.build()) } None => self.err_ty(), }, -- cgit v1.2.3