diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-07 14:22:18 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-07 14:22:18 +0100 |
commit | 1e2178eb8e02b0118c8cad7e631368cbee94ea72 (patch) | |
tree | 141bc3c57c8b1436e188abb6fd73468567a7d411 /crates/ra_hir/src/ty/infer.rs | |
parent | 36f5d997565b6390a4b524e7e1d0d805f0f26bdb (diff) | |
parent | b27fa33a9f459feb442682026670ca8e6001a424 (diff) |
Merge #1103
1103: Array inference r=flodiebold a=Lapz
Fixes the final item in #394. The only problem is that infering the repeat cause some types to be infered twices.
i.e
```rust
fn test() {
let y = unknown;
[y, &y];
}
```
results in the following diff:
```diff
[11; 48) '{ ...&y]; }': ()
[21; 22) 'y': &{unknown}
[25; 32) 'unknown': &{unknown}
-[38; 45) '[y, &y]': [&&{unknown}]
+[38; 45) '[y, &y]': [&&{unknown};usize]
[39; 40) 'y': &{unknown}
+[39; 40) 'y': &{unknown}
[42; 44) '&y': &&{unknown}
[43; 44) 'y': &{unknown}
```
Should the code produce two inference results for 'y' and if not could any tell me what needs to change.
Co-authored-by: Lenard Pratt <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 887153484..9ace6b13a 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -32,7 +32,7 @@ use crate::{ | |||
32 | DefWithBody, | 32 | DefWithBody, |
33 | ImplItem, | 33 | ImplItem, |
34 | type_ref::{TypeRef, Mutability}, | 34 | type_ref::{TypeRef, Mutability}, |
35 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, | 35 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat,Array, self}, |
36 | generics::GenericParams, | 36 | generics::GenericParams, |
37 | path::{GenericArgs, GenericArg}, | 37 | path::{GenericArgs, GenericArg}, |
38 | adt::VariantDef, | 38 | adt::VariantDef, |
@@ -1074,7 +1074,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1074 | 1074 | ||
1075 | Ty::apply(TypeCtor::Tuple, Substs(ty_vec.into())) | 1075 | Ty::apply(TypeCtor::Tuple, Substs(ty_vec.into())) |
1076 | } | 1076 | } |
1077 | Expr::Array { exprs } => { | 1077 | Expr::Array(array) => { |
1078 | let elem_ty = match &expected.ty { | 1078 | let elem_ty = match &expected.ty { |
1079 | Ty::Apply(a_ty) => match a_ty.ctor { | 1079 | Ty::Apply(a_ty) => match a_ty.ctor { |
1080 | TypeCtor::Slice | TypeCtor::Array => { | 1080 | TypeCtor::Slice | TypeCtor::Array => { |
@@ -1085,8 +1085,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1085 | _ => self.new_type_var(), | 1085 | _ => self.new_type_var(), |
1086 | }; | 1086 | }; |
1087 | 1087 | ||
1088 | for expr in exprs.iter() { | 1088 | match array { |
1089 | self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); | 1089 | Array::ElementList(items) => { |
1090 | for expr in items.iter() { | ||
1091 | self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); | ||
1092 | } | ||
1093 | } | ||
1094 | Array::Repeat { initializer, repeat } => { | ||
1095 | self.infer_expr(*initializer, &Expectation::has_type(elem_ty.clone())); | ||
1096 | self.infer_expr( | ||
1097 | *repeat, | ||
1098 | &Expectation::has_type(Ty::simple(TypeCtor::Int( | ||
1099 | primitive::UncertainIntTy::Known(primitive::IntTy::usize()), | ||
1100 | ))), | ||
1101 | ); | ||
1102 | } | ||
1090 | } | 1103 | } |
1091 | 1104 | ||
1092 | Ty::apply_one(TypeCtor::Array, elem_ty) | 1105 | Ty::apply_one(TypeCtor::Array, elem_ty) |