Skip to content

Commit

Permalink
Auto merge of rust-lang#12271 - bitgaoshu:box_with_expec, r=flodiebold
Browse files Browse the repository at this point in the history
fix rust-lang#12227 Type mismatch error shown

add box expectation hint
  • Loading branch information
bors committed May 16, 2022
2 parents 7959307 + e362929 commit 825ce48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
23 changes: 18 additions & 5 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -786,10 +786,23 @@ impl<'a> InferenceContext<'a> {
ty
}

fn infer_expr_box(&mut self, inner_expr: ExprId) -> chalk_ir::Ty<Interner> {
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()
Expand Down
14 changes: 13 additions & 1 deletion crates/hir-ty/src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2539,19 +2539,31 @@ impl<T> [T] {
fn test() {
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#"
569..573 'self': Box<[T], A>
602..634 '{ ... }': Vec<T, A>
612..628 'unimpl...ted!()': Vec<T, A>
648..694 '{ ...2]); }': ()
648..761 '{ ...t]); }': ()
658..661 'vec': Vec<i32, Global>
664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
664..691 '<[_]>:...1i32])': Vec<i32, Global>
680..690 'box [1i32]': Box<[i32; 1], Global>
684..690 '[1i32]': [i32; 1]
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
"#]],
)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2999,15 +2999,15 @@ fn foo() {
216..217 's': Option<i32>
220..224 'None': Option<i32>
234..235 'f': Box<dyn FnOnce(&Option<i32>)>
269..282 'box (|ps| {})': Box<|{unknown}| -> ()>
274..281 '|ps| {}': |{unknown}| -> ()
275..277 'ps': {unknown}
269..282 'box (|ps| {})': Box<|&Option<i32>| -> ()>
274..281 '|ps| {}': |&Option<i32>| -> ()
275..277 'ps': &Option<i32>
279..281 '{}': ()
288..289 'f': Box<dyn FnOnce(&Option<i32>)>
288..293 'f(&s)': ()
290..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>| -> ()>
"#]],
);
}
Expand Down

0 comments on commit 825ce48

Please sign in to comment.