aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-04-07 14:22:18 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-04-07 14:22:18 +0100
commit1e2178eb8e02b0118c8cad7e631368cbee94ea72 (patch)
tree141bc3c57c8b1436e188abb6fd73468567a7d411 /crates/ra_hir/src
parent36f5d997565b6390a4b524e7e1d0d805f0f26bdb (diff)
parentb27fa33a9f459feb442682026670ca8e6001a424 (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')
-rw-r--r--crates/ra_hir/src/expr.rs44
-rw-r--r--crates/ra_hir/src/ty.rs6
-rw-r--r--crates/ra_hir/src/ty/infer.rs21
-rw-r--r--crates/ra_hir/src/ty/tests.rs82
4 files changed, 100 insertions, 53 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index cfa824458..589a9b2db 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; 6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
7use ra_syntax::{ 7use ra_syntax::{
8 SyntaxNodePtr, AstPtr, AstNode, 8 SyntaxNodePtr, AstPtr, AstNode,
9 ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind, TypeAscriptionOwner} 9 ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner}
10}; 10};
11 11
12use crate::{ 12use crate::{
@@ -238,14 +238,17 @@ pub enum Expr {
238 Tuple { 238 Tuple {
239 exprs: Vec<ExprId>, 239 exprs: Vec<ExprId>,
240 }, 240 },
241 Array { 241 Array(Array),
242 exprs: Vec<ExprId>,
243 },
244 Literal(Literal), 242 Literal(Literal),
245} 243}
246 244
247pub use ra_syntax::ast::PrefixOp as UnaryOp; 245pub use ra_syntax::ast::PrefixOp as UnaryOp;
248pub use ra_syntax::ast::BinOp as BinaryOp; 246pub use ra_syntax::ast::BinOp as BinaryOp;
247#[derive(Debug, Clone, Eq, PartialEq)]
248pub enum Array {
249 ElementList(Vec<ExprId>),
250 Repeat { initializer: ExprId, repeat: ExprId },
251}
249 252
250#[derive(Debug, Clone, Eq, PartialEq)] 253#[derive(Debug, Clone, Eq, PartialEq)]
251pub struct MatchArm { 254pub struct MatchArm {
@@ -348,11 +351,22 @@ impl Expr {
348 | Expr::UnaryOp { expr, .. } => { 351 | Expr::UnaryOp { expr, .. } => {
349 f(*expr); 352 f(*expr);
350 } 353 }
351 Expr::Tuple { exprs } | Expr::Array { exprs } => { 354 Expr::Tuple { exprs } => {
352 for expr in exprs { 355 for expr in exprs {
353 f(*expr); 356 f(*expr);
354 } 357 }
355 } 358 }
359 Expr::Array(a) => match a {
360 Array::ElementList(exprs) => {
361 for expr in exprs {
362 f(*expr);
363 }
364 }
365 Array::Repeat { initializer, repeat } => {
366 f(*initializer);
367 f(*repeat)
368 }
369 },
356 Expr::Literal(_) => {} 370 Expr::Literal(_) => {}
357 } 371 }
358 } 372 }
@@ -723,10 +737,26 @@ impl ExprCollector {
723 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); 737 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
724 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) 738 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
725 } 739 }
740
726 ast::ExprKind::ArrayExpr(e) => { 741 ast::ExprKind::ArrayExpr(e) => {
727 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); 742 let kind = e.kind();
728 self.alloc_expr(Expr::Array { exprs }, syntax_ptr) 743
744 match kind {
745 ArrayExprKind::ElementList(e) => {
746 let exprs = e.map(|expr| self.collect_expr(expr)).collect();
747 self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
748 }
749 ArrayExprKind::Repeat { initializer, repeat } => {
750 let initializer = self.collect_expr_opt(initializer);
751 let repeat = self.collect_expr_opt(repeat);
752 self.alloc_expr(
753 Expr::Array(Array::Repeat { initializer, repeat }),
754 syntax_ptr,
755 )
756 }
757 }
729 } 758 }
759
730 ast::ExprKind::Literal(e) => { 760 ast::ExprKind::Literal(e) => {
731 let lit = match e.kind() { 761 let lit = match e.kind() {
732 LiteralKind::IntNumber { suffix } => { 762 LiteralKind::IntNumber { suffix } => {
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index d42c61e9d..20e55d92d 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -353,10 +353,14 @@ impl HirDisplay for ApplicationTy {
353 TypeCtor::Int(t) => write!(f, "{}", t)?, 353 TypeCtor::Int(t) => write!(f, "{}", t)?,
354 TypeCtor::Float(t) => write!(f, "{}", t)?, 354 TypeCtor::Float(t) => write!(f, "{}", t)?,
355 TypeCtor::Str => write!(f, "str")?, 355 TypeCtor::Str => write!(f, "str")?,
356 TypeCtor::Slice | TypeCtor::Array => { 356 TypeCtor::Slice => {
357 let t = self.parameters.as_single(); 357 let t = self.parameters.as_single();
358 write!(f, "[{}]", t.display(f.db))?; 358 write!(f, "[{}]", t.display(f.db))?;
359 } 359 }
360 TypeCtor::Array => {
361 let t = self.parameters.as_single();
362 write!(f, "[{};_]", t.display(f.db))?;
363 }
360 TypeCtor::RawPtr(m) => { 364 TypeCtor::RawPtr(m) => {
361 let t = self.parameters.as_single(); 365 let t = self.parameters.as_single();
362 write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?; 366 write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?;
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)
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index f0164f7ea..f6a325033 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -697,58 +697,58 @@ fn test(x: &str, y: isize) {
697[9; 10) 'x': &str 697[9; 10) 'x': &str
698[18; 19) 'y': isize 698[18; 19) 'y': isize
699[28; 324) '{ ... 3]; }': () 699[28; 324) '{ ... 3]; }': ()
700[38; 39) 'a': [&str] 700[38; 39) 'a': [&str;_]
701[42; 45) '[x]': [&str] 701[42; 45) '[x]': [&str;_]
702[43; 44) 'x': &str 702[43; 44) 'x': &str
703[55; 56) 'b': [[&str]] 703[55; 56) 'b': [[&str;_];_]
704[59; 65) '[a, a]': [[&str]] 704[59; 65) '[a, a]': [[&str;_];_]
705[60; 61) 'a': [&str] 705[60; 61) 'a': [&str;_]
706[63; 64) 'a': [&str] 706[63; 64) 'a': [&str;_]
707[75; 76) 'c': [[[&str]]] 707[75; 76) 'c': [[[&str;_];_];_]
708[79; 85) '[b, b]': [[[&str]]] 708[79; 85) '[b, b]': [[[&str;_];_];_]
709[80; 81) 'b': [[&str]] 709[80; 81) 'b': [[&str;_];_]
710[83; 84) 'b': [[&str]] 710[83; 84) 'b': [[&str;_];_]
711[96; 97) 'd': [isize] 711[96; 97) 'd': [isize;_]
712[100; 112) '[y, 1, 2, 3]': [isize] 712[100; 112) '[y, 1, 2, 3]': [isize;_]
713[101; 102) 'y': isize 713[101; 102) 'y': isize
714[104; 105) '1': isize 714[104; 105) '1': isize
715[107; 108) '2': isize 715[107; 108) '2': isize
716[110; 111) '3': isize 716[110; 111) '3': isize
717[122; 123) 'd': [isize] 717[122; 123) 'd': [isize;_]
718[126; 138) '[1, y, 2, 3]': [isize] 718[126; 138) '[1, y, 2, 3]': [isize;_]
719[127; 128) '1': isize 719[127; 128) '1': isize
720[130; 131) 'y': isize 720[130; 131) 'y': isize
721[133; 134) '2': isize 721[133; 134) '2': isize
722[136; 137) '3': isize 722[136; 137) '3': isize
723[148; 149) 'e': [isize] 723[148; 149) 'e': [isize;_]
724[152; 155) '[y]': [isize] 724[152; 155) '[y]': [isize;_]
725[153; 154) 'y': isize 725[153; 154) 'y': isize
726[165; 166) 'f': [[isize]] 726[165; 166) 'f': [[isize;_];_]
727[169; 175) '[d, d]': [[isize]] 727[169; 175) '[d, d]': [[isize;_];_]
728[170; 171) 'd': [isize] 728[170; 171) 'd': [isize;_]
729[173; 174) 'd': [isize] 729[173; 174) 'd': [isize;_]
730[185; 186) 'g': [[isize]] 730[185; 186) 'g': [[isize;_];_]
731[189; 195) '[e, e]': [[isize]] 731[189; 195) '[e, e]': [[isize;_];_]
732[190; 191) 'e': [isize] 732[190; 191) 'e': [isize;_]
733[193; 194) 'e': [isize] 733[193; 194) 'e': [isize;_]
734[206; 207) 'h': [i32] 734[206; 207) 'h': [i32;_]
735[210; 216) '[1, 2]': [i32] 735[210; 216) '[1, 2]': [i32;_]
736[211; 212) '1': i32 736[211; 212) '1': i32
737[214; 215) '2': i32 737[214; 215) '2': i32
738[226; 227) 'i': [&str] 738[226; 227) 'i': [&str;_]
739[230; 240) '["a", "b"]': [&str] 739[230; 240) '["a", "b"]': [&str;_]
740[231; 234) '"a"': &str 740[231; 234) '"a"': &str
741[236; 239) '"b"': &str 741[236; 239) '"b"': &str
742[251; 252) 'b': [[&str]] 742[251; 252) 'b': [[&str;_];_]
743[255; 265) '[a, ["b"]]': [[&str]] 743[255; 265) '[a, ["b"]]': [[&str;_];_]
744[256; 257) 'a': [&str] 744[256; 257) 'a': [&str;_]
745[259; 264) '["b"]': [&str] 745[259; 264) '["b"]': [&str;_]
746[260; 263) '"b"': &str 746[260; 263) '"b"': &str
747[275; 276) 'x': [u8] 747[275; 276) 'x': [u8;_]
748[288; 290) '[]': [u8] 748[288; 290) '[]': [u8;_]
749[300; 301) 'z': &[u8] 749[300; 301) 'z': &[u8;_]
750[311; 321) '&[1, 2, 3]': &[u8] 750[311; 321) '&[1, 2, 3]': &[u8;_]
751[312; 321) '[1, 2, 3]': [u8] 751[312; 321) '[1, 2, 3]': [u8;_]
752[313; 314) '1': u8 752[313; 314) '1': u8
753[316; 317) '2': u8 753[316; 317) '2': u8
754[319; 320) '3': u8"### 754[319; 320) '3': u8"###
@@ -1553,7 +1553,7 @@ fn test() {
1553[11; 48) '{ ...&y]; }': () 1553[11; 48) '{ ...&y]; }': ()
1554[21; 22) 'y': &{unknown} 1554[21; 22) 'y': &{unknown}
1555[25; 32) 'unknown': &{unknown} 1555[25; 32) 'unknown': &{unknown}
1556[38; 45) '[y, &y]': [&&{unknown}] 1556[38; 45) '[y, &y]': [&&{unknown};_]
1557[39; 40) 'y': &{unknown} 1557[39; 40) 'y': &{unknown}
1558[42; 44) '&y': &&{unknown} 1558[42; 44) '&y': &&{unknown}
1559[43; 44) 'y': &{unknown}"### 1559[43; 44) 'y': &{unknown}"###
@@ -1578,7 +1578,7 @@ fn test() {
1578[25; 32) 'unknown': &&{unknown} 1578[25; 32) 'unknown': &&{unknown}
1579[42; 43) 'y': &&{unknown} 1579[42; 43) 'y': &&{unknown}
1580[46; 53) 'unknown': &&{unknown} 1580[46; 53) 'unknown': &&{unknown}
1581[59; 77) '[(x, y..., &x)]': [(&&{unknown}, &&{unknown})] 1581[59; 77) '[(x, y..., &x)]': [(&&{unknown}, &&{unknown});_]
1582[60; 66) '(x, y)': (&&{unknown}, &&{unknown}) 1582[60; 66) '(x, y)': (&&{unknown}, &&{unknown})
1583[61; 62) 'x': &&{unknown} 1583[61; 62) 'x': &&{unknown}
1584[64; 65) 'y': &&{unknown} 1584[64; 65) 'y': &&{unknown}
@@ -1670,8 +1670,8 @@ fn test_line_buffer() {
1670"#), 1670"#),
1671 @r###" 1671 @r###"
1672[23; 53) '{ ...n']; }': () 1672[23; 53) '{ ...n']; }': ()
1673[29; 50) '&[0, b...b'\n']': &[u8] 1673[29; 50) '&[0, b...b'\n']': &[u8;_]
1674[30; 50) '[0, b'...b'\n']': [u8] 1674[30; 50) '[0, b'...b'\n']': [u8;_]
1675[31; 32) '0': u8 1675[31; 32) '0': u8
1676[34; 39) 'b'\n'': u8 1676[34; 39) 'b'\n'': u8
1677[41; 42) '1': u8 1677[41; 42) '1': u8