diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index fef02e63b70..2bb384ba22d 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -558,7 +558,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { } .intern(Interner) } - &Expr::Box { expr } => self.infer_expr_box(expr), + &Expr::Box { expr } => self.infer_expr_box(expr, expected), Expr::UnaryOp { expr, op } => { let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); let inner_ty = self.resolve_ty_shallow(&inner_ty); @@ -786,10 +786,23 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { ty } - fn infer_expr_box(&mut self, inner_expr: ExprId) -> chalk_ir::Ty { - let inner_ty = self.infer_expr_inner(inner_expr, &Expectation::none()); - if let Some(box_) = self.resolve_boxed_box() { - TyBuilder::adt(self.db, box_) + fn infer_expr_box(&mut self, inner_expr: ExprId, expected: &Expectation) -> Ty { + if let Some(box_id) = self.resolve_boxed_box() { + let table = &mut self.table; + let inner_exp = expected + .to_option(table) + .as_ref() + .map(|e| e.as_adt()) + .flatten() + .filter(|(e_adt, _)| e_adt == &box_id) + .map(|(_, subts)| { + let g = subts.at(Interner, 0); + Expectation::rvalue_hint(table, Ty::clone(g.assert_ty_ref(Interner))) + }) + .unwrap_or_else(Expectation::none); + + let inner_ty = self.infer_expr_inner(inner_expr, &inner_exp); + TyBuilder::adt(self.db, box_id) .push(inner_ty) .fill_with_defaults(self.db, || self.table.new_type_var()) .build() diff --git a/crates/hir-ty/src/tests/simple.rs b/crates/hir-ty/src/tests/simple.rs index d4d61c2167c..46ab2377b54 100644 --- a/crates/hir-ty/src/tests/simple.rs +++ b/crates/hir-ty/src/tests/simple.rs @@ -2539,19 +2539,31 @@ pub fn into_vec(self: Box) -> Vec { fn test() { let vec = <[_]>::into_vec(box [1i32]); + let v: Vec> = <[_]> :: into_vec(box [box Astruct]); } + +trait B{} +struct Astruct; +impl B for Astruct {} "#, expect![[r#" 569..573 'self': Box<[T], A> 602..634 '{ ... }': Vec 612..628 'unimpl...ted!()': Vec - 648..694 '{ ...2]); }': () + 648..761 '{ ...t]); }': () 658..661 'vec': Vec 664..679 '<[_]>::into_vec': fn into_vec(Box<[i32], Global>) -> Vec 664..691 '<[_]>:...1i32])': Vec 680..690 'box [1i32]': Box<[i32; 1], Global> 684..690 '[1i32]': [i32; 1] 685..689 '1i32': i32 + 701..702 'v': Vec, Global> + 722..739 '<[_]> ...to_vec': fn into_vec, Global>(Box<[Box], Global>) -> Vec, Global> + 722..758 '<[_]> ...ruct])': Vec, Global> + 740..757 'box [b...truct]': Box<[Box; 1], Global> + 744..757 '[box Astruct]': [Box; 1] + 745..756 'box Astruct': Box + 749..756 'Astruct': Astruct "#]], ) } diff --git a/crates/hir-ty/src/tests/traits.rs b/crates/hir-ty/src/tests/traits.rs index 0b08aa4711c..b1d295ca34a 100644 --- a/crates/hir-ty/src/tests/traits.rs +++ b/crates/hir-ty/src/tests/traits.rs @@ -2999,15 +2999,15 @@ fn foo() { 216..217 's': Option 220..224 'None': Option 234..235 'f': Box)> - 269..282 'box (|ps| {})': Box<|{unknown}| -> ()> - 274..281 '|ps| {}': |{unknown}| -> () - 275..277 'ps': {unknown} + 269..282 'box (|ps| {})': Box<|&Option| -> ()> + 274..281 '|ps| {}': |&Option| -> () + 275..277 'ps': &Option 279..281 '{}': () 288..289 'f': Box)> 288..293 'f(&s)': () 290..292 '&s': &Option 291..292 's': Option - 269..282: expected Box)>, got Box<|{unknown}| -> ()> + 269..282: expected Box)>, got Box<|&Option| -> ()> "#]], ); }