add box expection hint

This commit is contained in:
bitgaoshu 2022-05-15 21:14:11 +08:00
parent 0c881e882e
commit e362929fa1
3 changed files with 35 additions and 10 deletions

View file

@ -558,7 +558,7 @@ impl<'a> InferenceContext<'a> {
} }
.intern(Interner) .intern(Interner)
} }
&Expr::Box { expr } => self.infer_expr_box(expr), &Expr::Box { expr } => self.infer_expr_box(expr, expected),
Expr::UnaryOp { expr, op } => { Expr::UnaryOp { expr, op } => {
let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
let inner_ty = self.resolve_ty_shallow(&inner_ty); let inner_ty = self.resolve_ty_shallow(&inner_ty);
@ -786,10 +786,23 @@ impl<'a> InferenceContext<'a> {
ty ty
} }
fn infer_expr_box(&mut self, inner_expr: ExprId) -> chalk_ir::Ty<Interner> { fn infer_expr_box(&mut self, inner_expr: ExprId, expected: &Expectation) -> Ty {
let inner_ty = self.infer_expr_inner(inner_expr, &Expectation::none()); if let Some(box_id) = self.resolve_boxed_box() {
if let Some(box_) = self.resolve_boxed_box() { let table = &mut self.table;
TyBuilder::adt(self.db, box_) 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) .push(inner_ty)
.fill_with_defaults(self.db, || self.table.new_type_var()) .fill_with_defaults(self.db, || self.table.new_type_var())
.build() .build()

View file

@ -2539,19 +2539,31 @@ impl<T> [T] {
fn test() { fn test() {
let vec = <[_]>::into_vec(box [1i32]); let vec = <[_]>::into_vec(box [1i32]);
let v: Vec<Box<dyn B>> = <[_]> :: into_vec(box [box Astruct]);
} }
trait B{}
struct Astruct;
impl B for Astruct {}
"#, "#,
expect![[r#" expect![[r#"
569..573 'self': Box<[T], A> 569..573 'self': Box<[T], A>
602..634 '{ ... }': Vec<T, A> 602..634 '{ ... }': Vec<T, A>
612..628 'unimpl...ted!()': Vec<T, A> 612..628 'unimpl...ted!()': Vec<T, A>
648..694 '{ ...2]); }': () 648..761 '{ ...t]); }': ()
658..661 'vec': Vec<i32, Global> 658..661 'vec': Vec<i32, Global>
664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global> 664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
664..691 '<[_]>:...1i32])': Vec<i32, Global> 664..691 '<[_]>:...1i32])': Vec<i32, Global>
680..690 'box [1i32]': Box<[i32; 1], Global> 680..690 'box [1i32]': Box<[i32; 1], Global>
684..690 '[1i32]': [i32; 1] 684..690 '[1i32]': [i32; 1]
685..689 '1i32': i32 685..689 '1i32': i32
701..702 'v': Vec<Box<dyn B, Global>, Global>
722..739 '<[_]> ...to_vec': fn into_vec<Box<dyn B, Global>, Global>(Box<[Box<dyn B, Global>], Global>) -> Vec<Box<dyn B, Global>, Global>
722..758 '<[_]> ...ruct])': Vec<Box<dyn B, Global>, Global>
740..757 'box [b...truct]': Box<[Box<dyn B, Global>; 1], Global>
744..757 '[box Astruct]': [Box<dyn B, Global>; 1]
745..756 'box Astruct': Box<Astruct, Global>
749..756 'Astruct': Astruct
"#]], "#]],
) )
} }

View file

@ -2999,15 +2999,15 @@ fn foo() {
216..217 's': Option<i32> 216..217 's': Option<i32>
220..224 'None': Option<i32> 220..224 'None': Option<i32>
234..235 'f': Box<dyn FnOnce(&Option<i32>)> 234..235 'f': Box<dyn FnOnce(&Option<i32>)>
269..282 'box (|ps| {})': Box<|{unknown}| -> ()> 269..282 'box (|ps| {})': Box<|&Option<i32>| -> ()>
274..281 '|ps| {}': |{unknown}| -> () 274..281 '|ps| {}': |&Option<i32>| -> ()
275..277 'ps': {unknown} 275..277 'ps': &Option<i32>
279..281 '{}': () 279..281 '{}': ()
288..289 'f': Box<dyn FnOnce(&Option<i32>)> 288..289 'f': Box<dyn FnOnce(&Option<i32>)>
288..293 'f(&s)': () 288..293 'f(&s)': ()
290..292 '&s': &Option<i32> 290..292 '&s': &Option<i32>
291..292 's': Option<i32> 291..292 's': Option<i32>
269..282: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|{unknown}| -> ()> 269..282: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|&Option<i32>| -> ()>
"#]], "#]],
); );
} }