From 4729a1b05427aa7d63b1dcb9ee6dceef2cfc613e Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 13 Jan 2019 22:46:10 +0900 Subject: [PATCH 1/8] Update ARRAY_EXPR grammar --- crates/ra_syntax/src/ast/generated.rs | 6 +++++- crates/ra_syntax/src/grammar.ron | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 3471d52264..2d9603d904 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -67,7 +67,11 @@ impl AstNode for ArrayExpr { } -impl ArrayExpr {} +impl ArrayExpr { + pub fn exprs(&self) -> impl Iterator { + super::children(self) + } +} // ArrayType #[derive(Debug, PartialEq, Eq, Hash)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index bd8c5b4110..2aaad46b1b 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -360,7 +360,9 @@ Grammar( "TupleExpr": ( collections: [["exprs", "Expr"]] ), - "ArrayExpr": (), + "ArrayExpr": ( + collections: [["exprs", "Expr"]] + ), "ParenExpr": (options: ["Expr"]), "PathExpr": (options: ["Path"]), "LambdaExpr": ( From d665acbbec39ffd26eb56b1194653459a0c495e7 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 13 Jan 2019 22:46:36 +0900 Subject: [PATCH 2/8] Implement array inference --- crates/ra_hir/src/expr.rs | 10 ++++++++-- crates/ra_hir/src/ty.rs | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 6633388447..6e98ebc697 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -197,6 +197,9 @@ pub enum Expr { Tuple { exprs: Vec, }, + Array { + exprs: Vec, + }, Literal(Literal), } @@ -312,7 +315,7 @@ impl Expr { | Expr::UnaryOp { expr, .. } => { f(*expr); } - Expr::Tuple { exprs } => { + Expr::Tuple { exprs } | Expr::Array { exprs } => { for expr in exprs { f(*expr); } @@ -649,6 +652,10 @@ impl ExprCollector { let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) } + ast::ExprKind::ArrayExpr(e) => { + let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); + self.alloc_expr(Expr::Array { exprs }, syntax_ptr) + } ast::ExprKind::Literal(e) => { let child = if let Some(child) = e.literal_expr() { child @@ -691,7 +698,6 @@ impl ExprCollector { // TODO implement HIR for these: ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), - ast::ExprKind::ArrayExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), } } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 85d4dc05ce..676ed3ac99 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -227,6 +227,9 @@ pub enum Ty { /// A tuple type. For example, `(i32, bool)`. Tuple(Arc<[Ty]>), + /// A array type. For example, `[i32]`. + Array(Arc<[Ty]>), + // The projection of an associated type. For example, // `>::N`.pub // Projection(ProjectionTy), @@ -414,6 +417,16 @@ impl fmt::Display for Ty { .to_fmt(f) } } + Ty::Array(ts) => { + if ts.len() == 1 { + write!(f, "[{},]", ts[0]) + } else { + join(ts.iter()) + .surround_with("[", "]") + .separator(", ") + .to_fmt(f) + } + } Ty::FnPtr(sig) => { join(sig.input.iter()) .surround_with("fn(", ")") @@ -1101,7 +1114,15 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Ty::Tuple(Arc::from(ty_vec)) - } + }, + Expr::Array { exprs } => { + let mut ty_vec = Vec::with_capacity(exprs.len()); + for arg in exprs.iter() { + ty_vec.push(self.infer_expr(*arg, &Expectation::none())); + } + + Ty::Array(Arc::from(ty_vec)) + }, Expr::Literal(lit) => match lit { Literal::Bool(..) => Ty::Bool, Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), From 2a3262c75792d90341e21c241b2f95ef8cdccb23 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 13 Jan 2019 22:46:52 +0900 Subject: [PATCH 3/8] Add infer array test --- crates/ra_hir/src/ty/tests.rs | 23 ++++++++++++++ crates/ra_hir/src/ty/tests/data/array.txt | 38 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 crates/ra_hir/src/ty/tests/data/array.txt diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 5d7bc25cc4..09c8644f78 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -334,6 +334,29 @@ fn test(x: &str, y: isize) { ); } +#[test] +fn infer_array() { + check_inference( + r#" +fn test(x: &str, y: isize) { + let a = [x]; + let b = [a, a]; + let c = [b, b]; + + let d = [y]; + let e = [d, d]; + let f = [e, e]; + + // we have not infered these case yet. + let g = [1, 2]; + let h = ["a", "b"]; + let b = [a, ["b"]]; +} +"#, + "array.txt", + ); +} + fn infer(content: &str) -> String { let (db, _, file_id) = MockDatabase::with_single_file(content); let source_file = db.source_file(file_id); diff --git a/crates/ra_hir/src/ty/tests/data/array.txt b/crates/ra_hir/src/ty/tests/data/array.txt new file mode 100644 index 0000000000..7711e378bc --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/array.txt @@ -0,0 +1,38 @@ +[9; 10) 'x': &str +[18; 19) 'y': isize +[28; 258) '{ ..."]]; }': () +[38; 39) 'a': [&str,] +[42; 45) '[x]': [&str,] +[43; 44) 'x': &str +[55; 56) 'b': [[&str,], [&str,]] +[59; 65) '[a, a]': [[&str,], [&str,]] +[60; 61) 'a': [&str,] +[63; 64) 'a': [&str,] +[75; 76) 'c': [[[&str,], [&str,]], [[&str,], [&str,]]] +[79; 85) '[b, b]': [[[&str,], [&str,]], [[&str,], [&str,]]] +[80; 81) 'b': [[&str,], [&str,]] +[83; 84) 'b': [[&str,], [&str,]] +[96; 97) 'd': [isize,] +[100; 103) '[y]': [isize,] +[101; 102) 'y': isize +[113; 114) 'e': [[isize,], [isize,]] +[117; 123) '[d, d]': [[isize,], [isize,]] +[118; 119) 'd': [isize,] +[121; 122) 'd': [isize,] +[133; 134) 'f': [[[isize,], [isize,]], [[isize,], [isize,]]] +[137; 143) '[e, e]': [[[isize,], [isize,]], [[isize,], [isize,]]] +[138; 139) 'e': [[isize,], [isize,]] +[141; 142) 'e': [[isize,], [isize,]] +[197; 198) 'g': [_, _] +[201; 207) '[1, 2]': [_, _] +[202; 203) '1': [unknown] +[205; 206) '2': [unknown] +[217; 218) 'h': [_, _] +[221; 231) '["a", "b"]': [_, _] +[222; 225) '"a"': [unknown] +[227; 230) '"b"': [unknown] +[241; 242) 'b': [[&str,], [_,]] +[245; 255) '[a, ["b"]]': [[&str,], [_,]] +[246; 247) 'a': [&str,] +[249; 254) '["b"]': [_,] +[250; 253) '"b"': [unknown] From 5d3884d5b49e06991f8f1b0d9031f8dd37edd6ab Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Mon, 14 Jan 2019 22:51:54 +0900 Subject: [PATCH 4/8] Fix Ty::Array --- crates/ra_hir/src/ty.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 676ed3ac99..d3373644d4 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -181,11 +181,12 @@ pub enum Ty { /// The pointee of a string slice. Written as `str`. Str, - // An array with the given length. Written as `[T; n]`. - // Array(Ty, ty::Const), /// The pointee of an array slice. Written as `[T]`. Slice(Arc), + // An array with the given length. Written as `[T; n]`. + Array(Arc), + /// A raw pointer. Written as `*mut T` or `*const T` RawPtr(Arc, Mutability), @@ -227,9 +228,6 @@ pub enum Ty { /// A tuple type. For example, `(i32, bool)`. Tuple(Arc<[Ty]>), - /// A array type. For example, `[i32]`. - Array(Arc<[Ty]>), - // The projection of an associated type. For example, // `>::N`.pub // Projection(ProjectionTy), @@ -279,7 +277,10 @@ impl Ty { let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::RawPtr(Arc::new(inner_ty), *mutability) } - TypeRef::Array(_inner) => Ty::Unknown, // TODO + TypeRef::Array(inner) => { + let inner_ty = Ty::from_hir(db, module, impl_block, inner); + Ty::Array(Arc::new(inner_ty)) + } TypeRef::Slice(inner) => { let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::Slice(Arc::new(inner_ty)) @@ -403,7 +404,7 @@ impl fmt::Display for Ty { Ty::Int(t) => write!(f, "{}", t.ty_to_string()), Ty::Float(t) => write!(f, "{}", t.ty_to_string()), Ty::Str => write!(f, "str"), - Ty::Slice(t) => write!(f, "[{}]", t), + Ty::Slice(t) | Ty::Array(t) => write!(f, "[{}]", t), Ty::RawPtr(t, m) => write!(f, "*{}{}", m.as_keyword_for_ptr(), t), Ty::Ref(t, m) => write!(f, "&{}{}", m.as_keyword_for_ref(), t), Ty::Never => write!(f, "!"), @@ -417,16 +418,6 @@ impl fmt::Display for Ty { .to_fmt(f) } } - Ty::Array(ts) => { - if ts.len() == 1 { - write!(f, "[{},]", ts[0]) - } else { - join(ts.iter()) - .surround_with("[", "]") - .separator(", ") - .to_fmt(f) - } - } Ty::FnPtr(sig) => { join(sig.input.iter()) .surround_with("fn(", ")") @@ -1116,12 +1107,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Ty::Tuple(Arc::from(ty_vec)) }, Expr::Array { exprs } => { - let mut ty_vec = Vec::with_capacity(exprs.len()); - for arg in exprs.iter() { - ty_vec.push(self.infer_expr(*arg, &Expectation::none())); + let mut elem_ty = match &expected.ty { + Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner), + _ => self.new_type_var(), + }; + + for expr in exprs.iter() { + elem_ty = self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); } - Ty::Array(Arc::from(ty_vec)) + Ty::Array(Arc::new(elem_ty)) }, Expr::Literal(lit) => match lit { Literal::Bool(..) => Ty::Bool, From 478ce1c8b5288432509f39aeb5b611e7614d0e03 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Mon, 14 Jan 2019 22:52:05 +0900 Subject: [PATCH 5/8] Fix infer array test --- crates/ra_hir/src/ty/tests.rs | 15 +++-- crates/ra_hir/src/ty/tests/data/array.txt | 82 +++++++++++++---------- 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 09c8644f78..dd574162ac 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -343,14 +343,17 @@ fn test(x: &str, y: isize) { let b = [a, a]; let c = [b, b]; - let d = [y]; - let e = [d, d]; - let f = [e, e]; + let d = [y, 1, 2, 3] + let d = [1, y, 2, 3] + let e = [y]; + let f = [d, d]; + let g = [e, e]; + + let h = [1, 2]; + let i = ["a", "b"]; - // we have not infered these case yet. - let g = [1, 2]; - let h = ["a", "b"]; let b = [a, ["b"]]; + let x: [u8; 0] = []; } "#, "array.txt", diff --git a/crates/ra_hir/src/ty/tests/data/array.txt b/crates/ra_hir/src/ty/tests/data/array.txt index 7711e378bc..66f09485d7 100644 --- a/crates/ra_hir/src/ty/tests/data/array.txt +++ b/crates/ra_hir/src/ty/tests/data/array.txt @@ -1,38 +1,52 @@ [9; 10) 'x': &str [18; 19) 'y': isize -[28; 258) '{ ..."]]; }': () -[38; 39) 'a': [&str,] -[42; 45) '[x]': [&str,] +[28; 291) '{ ... []; }': () +[38; 39) 'a': [&str] +[42; 45) '[x]': [&str] [43; 44) 'x': &str -[55; 56) 'b': [[&str,], [&str,]] -[59; 65) '[a, a]': [[&str,], [&str,]] -[60; 61) 'a': [&str,] -[63; 64) 'a': [&str,] -[75; 76) 'c': [[[&str,], [&str,]], [[&str,], [&str,]]] -[79; 85) '[b, b]': [[[&str,], [&str,]], [[&str,], [&str,]]] -[80; 81) 'b': [[&str,], [&str,]] -[83; 84) 'b': [[&str,], [&str,]] -[96; 97) 'd': [isize,] -[100; 103) '[y]': [isize,] +[55; 56) 'b': [[&str]] +[59; 65) '[a, a]': [[&str]] +[60; 61) 'a': [&str] +[63; 64) 'a': [&str] +[75; 76) 'c': [[[&str]]] +[79; 85) '[b, b]': [[[&str]]] +[80; 81) 'b': [[&str]] +[83; 84) 'b': [[&str]] +[96; 97) 'd': [isize] +[100; 112) '[y, 1, 2, 3]': [isize] [101; 102) 'y': isize -[113; 114) 'e': [[isize,], [isize,]] -[117; 123) '[d, d]': [[isize,], [isize,]] -[118; 119) 'd': [isize,] -[121; 122) 'd': [isize,] -[133; 134) 'f': [[[isize,], [isize,]], [[isize,], [isize,]]] -[137; 143) '[e, e]': [[[isize,], [isize,]], [[isize,], [isize,]]] -[138; 139) 'e': [[isize,], [isize,]] -[141; 142) 'e': [[isize,], [isize,]] -[197; 198) 'g': [_, _] -[201; 207) '[1, 2]': [_, _] -[202; 203) '1': [unknown] -[205; 206) '2': [unknown] -[217; 218) 'h': [_, _] -[221; 231) '["a", "b"]': [_, _] -[222; 225) '"a"': [unknown] -[227; 230) '"b"': [unknown] -[241; 242) 'b': [[&str,], [_,]] -[245; 255) '[a, ["b"]]': [[&str,], [_,]] -[246; 247) 'a': [&str,] -[249; 254) '["b"]': [_,] -[250; 253) '"b"': [unknown] +[104; 105) '1': isize +[107; 108) '2': isize +[110; 111) '3': isize +[121; 122) 'd': [isize] +[125; 137) '[1, y, 2, 3]': [isize] +[126; 127) '1': isize +[129; 130) 'y': isize +[132; 133) '2': isize +[135; 136) '3': isize +[146; 147) 'e': [isize] +[150; 153) '[y]': [isize] +[151; 152) 'y': isize +[163; 164) 'f': [[isize]] +[167; 173) '[d, d]': [[isize]] +[168; 169) 'd': [isize] +[171; 172) 'd': [isize] +[183; 184) 'g': [[isize]] +[187; 193) '[e, e]': [[isize]] +[188; 189) 'e': [isize] +[191; 192) 'e': [isize] +[204; 205) 'h': [_] +[208; 214) '[1, 2]': [_] +[209; 210) '1': [unknown] +[212; 213) '2': [unknown] +[224; 225) 'i': [_] +[228; 238) '["a", "b"]': [_] +[229; 232) '"a"': [unknown] +[234; 237) '"b"': [unknown] +[249; 250) 'b': [[&str]] +[253; 263) '[a, ["b"]]': [[&str]] +[254; 255) 'a': [&str] +[257; 262) '["b"]': [&str] +[258; 261) '"b"': &str +[273; 274) 'x': [u8] +[286; 288) '[]': [u8] From 5c570fa77016f1a0dc96bde29b8cf44eab9fec17 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Tue, 15 Jan 2019 09:26:20 +0900 Subject: [PATCH 6/8] Remove unneeded code --- crates/ra_hir/src/ty.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index d3373644d4..55373b05af 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -1107,13 +1107,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Ty::Tuple(Arc::from(ty_vec)) }, Expr::Array { exprs } => { - let mut elem_ty = match &expected.ty { + let elem_ty = match &expected.ty { Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner), _ => self.new_type_var(), }; for expr in exprs.iter() { - elem_ty = self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); + self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); } Ty::Array(Arc::new(elem_ty)) From f66ca1bd2e0e5e198f60d0b0b6e2d953c81b0645 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Tue, 15 Jan 2019 09:30:18 +0900 Subject: [PATCH 7/8] Fix test case invalid syntax --- crates/ra_hir/src/ty/tests.rs | 4 +- crates/ra_hir/src/ty/tests/data/array.txt | 90 +++++++++++------------ 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index dd574162ac..affd63a850 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -343,8 +343,8 @@ fn test(x: &str, y: isize) { let b = [a, a]; let c = [b, b]; - let d = [y, 1, 2, 3] - let d = [1, y, 2, 3] + let d = [y, 1, 2, 3]; + let d = [1, y, 2, 3]; let e = [y]; let f = [d, d]; let g = [e, e]; diff --git a/crates/ra_hir/src/ty/tests/data/array.txt b/crates/ra_hir/src/ty/tests/data/array.txt index 66f09485d7..5fdbc67b6e 100644 --- a/crates/ra_hir/src/ty/tests/data/array.txt +++ b/crates/ra_hir/src/ty/tests/data/array.txt @@ -1,52 +1,52 @@ [9; 10) 'x': &str [18; 19) 'y': isize -[28; 291) '{ ... []; }': () -[38; 39) 'a': [&str] -[42; 45) '[x]': [&str] +[28; 293) '{ ... []; }': () +[38; 39) 'a': [_] +[42; 45) '[x]': [_] [43; 44) 'x': &str -[55; 56) 'b': [[&str]] -[59; 65) '[a, a]': [[&str]] -[60; 61) 'a': [&str] -[63; 64) 'a': [&str] -[75; 76) 'c': [[[&str]]] -[79; 85) '[b, b]': [[[&str]]] -[80; 81) 'b': [[&str]] -[83; 84) 'b': [[&str]] -[96; 97) 'd': [isize] -[100; 112) '[y, 1, 2, 3]': [isize] +[55; 56) 'b': [_] +[59; 65) '[a, a]': [_] +[60; 61) 'a': [_] +[63; 64) 'a': [_] +[75; 76) 'c': [_] +[79; 85) '[b, b]': [_] +[80; 81) 'b': [_] +[83; 84) 'b': [_] +[96; 97) 'd': [_] +[100; 112) '[y, 1, 2, 3]': [_] [101; 102) 'y': isize [104; 105) '1': isize [107; 108) '2': isize [110; 111) '3': isize -[121; 122) 'd': [isize] -[125; 137) '[1, y, 2, 3]': [isize] -[126; 127) '1': isize -[129; 130) 'y': isize -[132; 133) '2': isize -[135; 136) '3': isize -[146; 147) 'e': [isize] -[150; 153) '[y]': [isize] -[151; 152) 'y': isize -[163; 164) 'f': [[isize]] -[167; 173) '[d, d]': [[isize]] -[168; 169) 'd': [isize] -[171; 172) 'd': [isize] -[183; 184) 'g': [[isize]] -[187; 193) '[e, e]': [[isize]] -[188; 189) 'e': [isize] -[191; 192) 'e': [isize] -[204; 205) 'h': [_] -[208; 214) '[1, 2]': [_] -[209; 210) '1': [unknown] -[212; 213) '2': [unknown] -[224; 225) 'i': [_] -[228; 238) '["a", "b"]': [_] -[229; 232) '"a"': [unknown] -[234; 237) '"b"': [unknown] -[249; 250) 'b': [[&str]] -[253; 263) '[a, ["b"]]': [[&str]] -[254; 255) 'a': [&str] -[257; 262) '["b"]': [&str] -[258; 261) '"b"': &str -[273; 274) 'x': [u8] -[286; 288) '[]': [u8] +[122; 123) 'd': [_] +[126; 138) '[1, y, 2, 3]': [_] +[127; 128) '1': isize +[130; 131) 'y': isize +[133; 134) '2': isize +[136; 137) '3': isize +[148; 149) 'e': [_] +[152; 155) '[y]': [_] +[153; 154) 'y': isize +[165; 166) 'f': [_] +[169; 175) '[d, d]': [_] +[170; 171) 'd': [_] +[173; 174) 'd': [_] +[185; 186) 'g': [_] +[189; 195) '[e, e]': [_] +[190; 191) 'e': [_] +[193; 194) 'e': [_] +[206; 207) 'h': [_] +[210; 216) '[1, 2]': [_] +[211; 212) '1': i32 +[214; 215) '2': i32 +[226; 227) 'i': [_] +[230; 240) '["a", "b"]': [_] +[231; 234) '"a"': &str +[236; 239) '"b"': &str +[251; 252) 'b': [_] +[255; 265) '[a, ["b"]]': [_] +[256; 257) 'a': [_] +[259; 264) '["b"]': [_] +[260; 263) '"b"': &str +[275; 276) 'x': [u8] +[288; 290) '[]': [u8] From 0aedd4fb2f28ec24902d26c7d8a24d6146263d2f Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Thu, 17 Jan 2019 00:08:53 +0900 Subject: [PATCH 8/8] Add Ty::Array to walk_mut --- crates/ra_hir/src/ty.rs | 6 +-- crates/ra_hir/src/ty/tests/data/array.txt | 64 +++++++++++------------ 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 55373b05af..c7c0636017 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -356,7 +356,7 @@ impl Ty { fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { f(self); match self { - Ty::Slice(t) => Arc::make_mut(t).walk_mut(f), + Ty::Slice(t) | Ty::Array(t) => Arc::make_mut(t).walk_mut(f), Ty::RawPtr(t, _) => Arc::make_mut(t).walk_mut(f), Ty::Ref(t, _) => Arc::make_mut(t).walk_mut(f), Ty::Tuple(ts) => { @@ -1105,7 +1105,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Ty::Tuple(Arc::from(ty_vec)) - }, + } Expr::Array { exprs } => { let elem_ty = match &expected.ty { Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner), @@ -1117,7 +1117,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Ty::Array(Arc::new(elem_ty)) - }, + } Expr::Literal(lit) => match lit { Literal::Bool(..) => Ty::Bool, Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), diff --git a/crates/ra_hir/src/ty/tests/data/array.txt b/crates/ra_hir/src/ty/tests/data/array.txt index 5fdbc67b6e..acdf74ba49 100644 --- a/crates/ra_hir/src/ty/tests/data/array.txt +++ b/crates/ra_hir/src/ty/tests/data/array.txt @@ -1,52 +1,52 @@ [9; 10) 'x': &str [18; 19) 'y': isize [28; 293) '{ ... []; }': () -[38; 39) 'a': [_] -[42; 45) '[x]': [_] +[38; 39) 'a': [&str] +[42; 45) '[x]': [&str] [43; 44) 'x': &str -[55; 56) 'b': [_] -[59; 65) '[a, a]': [_] -[60; 61) 'a': [_] -[63; 64) 'a': [_] -[75; 76) 'c': [_] -[79; 85) '[b, b]': [_] -[80; 81) 'b': [_] -[83; 84) 'b': [_] -[96; 97) 'd': [_] -[100; 112) '[y, 1, 2, 3]': [_] +[55; 56) 'b': [[&str]] +[59; 65) '[a, a]': [[&str]] +[60; 61) 'a': [&str] +[63; 64) 'a': [&str] +[75; 76) 'c': [[[&str]]] +[79; 85) '[b, b]': [[[&str]]] +[80; 81) 'b': [[&str]] +[83; 84) 'b': [[&str]] +[96; 97) 'd': [isize] +[100; 112) '[y, 1, 2, 3]': [isize] [101; 102) 'y': isize [104; 105) '1': isize [107; 108) '2': isize [110; 111) '3': isize -[122; 123) 'd': [_] -[126; 138) '[1, y, 2, 3]': [_] +[122; 123) 'd': [isize] +[126; 138) '[1, y, 2, 3]': [isize] [127; 128) '1': isize [130; 131) 'y': isize [133; 134) '2': isize [136; 137) '3': isize -[148; 149) 'e': [_] -[152; 155) '[y]': [_] +[148; 149) 'e': [isize] +[152; 155) '[y]': [isize] [153; 154) 'y': isize -[165; 166) 'f': [_] -[169; 175) '[d, d]': [_] -[170; 171) 'd': [_] -[173; 174) 'd': [_] -[185; 186) 'g': [_] -[189; 195) '[e, e]': [_] -[190; 191) 'e': [_] -[193; 194) 'e': [_] -[206; 207) 'h': [_] -[210; 216) '[1, 2]': [_] +[165; 166) 'f': [[isize]] +[169; 175) '[d, d]': [[isize]] +[170; 171) 'd': [isize] +[173; 174) 'd': [isize] +[185; 186) 'g': [[isize]] +[189; 195) '[e, e]': [[isize]] +[190; 191) 'e': [isize] +[193; 194) 'e': [isize] +[206; 207) 'h': [i32] +[210; 216) '[1, 2]': [i32] [211; 212) '1': i32 [214; 215) '2': i32 -[226; 227) 'i': [_] -[230; 240) '["a", "b"]': [_] +[226; 227) 'i': [&str] +[230; 240) '["a", "b"]': [&str] [231; 234) '"a"': &str [236; 239) '"b"': &str -[251; 252) 'b': [_] -[255; 265) '[a, ["b"]]': [_] -[256; 257) 'a': [_] -[259; 264) '["b"]': [_] +[251; 252) 'b': [[&str]] +[255; 265) '[a, ["b"]]': [[&str]] +[256; 257) 'a': [&str] +[259; 264) '["b"]': [&str] [260; 263) '"b"': &str [275; 276) 'x': [u8] [288; 290) '[]': [u8]