From e362929fa1f691b299012ebe86e8e14541067944 Mon Sep 17 00:00:00 2001 From: bitgaoshu Date: Sun, 15 May 2022 21:14:11 +0800 Subject: [PATCH] add box expection hint --- crates/hir-ty/src/infer/expr.rs | 23 ++++++++++++++++++----- crates/hir-ty/src/tests/simple.rs | 14 +++++++++++++- crates/hir-ty/src/tests/traits.rs | 8 ++++---- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index fef02e63b70bb..2bb384ba22d52 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -558,7 +558,7 @@ impl<'a> InferenceContext<'a> { } .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 @@ impl<'a> InferenceContext<'a> { 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 4b8a7e782de6b..86988e484fb0d 100644 --- a/crates/hir-ty/src/tests/simple.rs +++ b/crates/hir-ty/src/tests/simple.rs @@ -2539,19 +2539,31 @@ impl [T] { 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 0b08aa4711ca3..b1d295ca34a8c 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| -> ()> "#]], ); }