Skip to content

Commit 825ce48

Browse files
committed
Auto merge of rust-lang#12271 - bitgaoshu:box_with_expec, r=flodiebold
fix rust-lang#12227 Type mismatch error shown add box expectation hint
2 parents 7959307 + e362929 commit 825ce48

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

crates/hir-ty/src/infer/expr.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'a> InferenceContext<'a> {
558558
}
559559
.intern(Interner)
560560
}
561-
&Expr::Box { expr } => self.infer_expr_box(expr),
561+
&Expr::Box { expr } => self.infer_expr_box(expr, expected),
562562
Expr::UnaryOp { expr, op } => {
563563
let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
564564
let inner_ty = self.resolve_ty_shallow(&inner_ty);
@@ -786,10 +786,23 @@ impl<'a> InferenceContext<'a> {
786786
ty
787787
}
788788

789-
fn infer_expr_box(&mut self, inner_expr: ExprId) -> chalk_ir::Ty<Interner> {
790-
let inner_ty = self.infer_expr_inner(inner_expr, &Expectation::none());
791-
if let Some(box_) = self.resolve_boxed_box() {
792-
TyBuilder::adt(self.db, box_)
789+
fn infer_expr_box(&mut self, inner_expr: ExprId, expected: &Expectation) -> Ty {
790+
if let Some(box_id) = self.resolve_boxed_box() {
791+
let table = &mut self.table;
792+
let inner_exp = expected
793+
.to_option(table)
794+
.as_ref()
795+
.map(|e| e.as_adt())
796+
.flatten()
797+
.filter(|(e_adt, _)| e_adt == &box_id)
798+
.map(|(_, subts)| {
799+
let g = subts.at(Interner, 0);
800+
Expectation::rvalue_hint(table, Ty::clone(g.assert_ty_ref(Interner)))
801+
})
802+
.unwrap_or_else(Expectation::none);
803+
804+
let inner_ty = self.infer_expr_inner(inner_expr, &inner_exp);
805+
TyBuilder::adt(self.db, box_id)
793806
.push(inner_ty)
794807
.fill_with_defaults(self.db, || self.table.new_type_var())
795808
.build()

crates/hir-ty/src/tests/simple.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -2539,19 +2539,31 @@ impl<T> [T] {
25392539
25402540
fn test() {
25412541
let vec = <[_]>::into_vec(box [1i32]);
2542+
let v: Vec<Box<dyn B>> = <[_]> :: into_vec(box [box Astruct]);
25422543
}
2544+
2545+
trait B{}
2546+
struct Astruct;
2547+
impl B for Astruct {}
25432548
"#,
25442549
expect![[r#"
25452550
569..573 'self': Box<[T], A>
25462551
602..634 '{ ... }': Vec<T, A>
25472552
612..628 'unimpl...ted!()': Vec<T, A>
2548-
648..694 '{ ...2]); }': ()
2553+
648..761 '{ ...t]); }': ()
25492554
658..661 'vec': Vec<i32, Global>
25502555
664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
25512556
664..691 '<[_]>:...1i32])': Vec<i32, Global>
25522557
680..690 'box [1i32]': Box<[i32; 1], Global>
25532558
684..690 '[1i32]': [i32; 1]
25542559
685..689 '1i32': i32
2560+
701..702 'v': Vec<Box<dyn B, Global>, Global>
2561+
722..739 '<[_]> ...to_vec': fn into_vec<Box<dyn B, Global>, Global>(Box<[Box<dyn B, Global>], Global>) -> Vec<Box<dyn B, Global>, Global>
2562+
722..758 '<[_]> ...ruct])': Vec<Box<dyn B, Global>, Global>
2563+
740..757 'box [b...truct]': Box<[Box<dyn B, Global>; 1], Global>
2564+
744..757 '[box Astruct]': [Box<dyn B, Global>; 1]
2565+
745..756 'box Astruct': Box<Astruct, Global>
2566+
749..756 'Astruct': Astruct
25552567
"#]],
25562568
)
25572569
}

crates/hir-ty/src/tests/traits.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2999,15 +2999,15 @@ fn foo() {
29992999
216..217 's': Option<i32>
30003000
220..224 'None': Option<i32>
30013001
234..235 'f': Box<dyn FnOnce(&Option<i32>)>
3002-
269..282 'box (|ps| {})': Box<|{unknown}| -> ()>
3003-
274..281 '|ps| {}': |{unknown}| -> ()
3004-
275..277 'ps': {unknown}
3002+
269..282 'box (|ps| {})': Box<|&Option<i32>| -> ()>
3003+
274..281 '|ps| {}': |&Option<i32>| -> ()
3004+
275..277 'ps': &Option<i32>
30053005
279..281 '{}': ()
30063006
288..289 'f': Box<dyn FnOnce(&Option<i32>)>
30073007
288..293 'f(&s)': ()
30083008
290..292 '&s': &Option<i32>
30093009
291..292 's': Option<i32>
3010-
269..282: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|{unknown}| -> ()>
3010+
269..282: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|&Option<i32>| -> ()>
30113011
"#]],
30123012
);
30133013
}

0 commit comments

Comments
 (0)