Skip to content

Commit e175921

Browse files
committed
Added ArrayExprKind,
changed the display for fixed array types, Added Array Enum to ra_hir/expr
1 parent 2d73c90 commit e175921

File tree

5 files changed

+76
-31
lines changed

5 files changed

+76
-31
lines changed

crates/ra_hir/src/expr.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
66
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
77
use ra_syntax::{
88
SyntaxNodePtr, AstPtr, AstNode,
9-
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind, TypeAscriptionOwner}
9+
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner}
1010
};
1111

1212
use crate::{
@@ -238,15 +238,17 @@ pub enum Expr {
238238
Tuple {
239239
exprs: Vec<ExprId>,
240240
},
241-
Array {
242-
exprs: Vec<ExprId>,
243-
repeat: Option<ExprId>,
244-
},
241+
Array(Array),
245242
Literal(Literal),
246243
}
247244

248245
pub use ra_syntax::ast::PrefixOp as UnaryOp;
249246
pub use ra_syntax::ast::BinOp as BinaryOp;
247+
#[derive(Debug, Clone, Eq, PartialEq)]
248+
pub enum Array {
249+
ElementList(Vec<ExprId>),
250+
Repeat { initializer: ExprId, repeat: ExprId },
251+
}
250252

251253
#[derive(Debug, Clone, Eq, PartialEq)]
252254
pub struct MatchArm {
@@ -354,15 +356,17 @@ impl Expr {
354356
f(*expr);
355357
}
356358
}
357-
Expr::Array { exprs, repeat } => {
358-
for expr in exprs {
359-
f(*expr);
359+
Expr::Array(a) => match a {
360+
Array::ElementList(exprs) => {
361+
for expr in exprs {
362+
f(*expr);
363+
}
360364
}
361-
362-
if let Some(expr) = repeat {
363-
f(*expr)
365+
Array::Repeat { initializer, repeat } => {
366+
f(*initializer);
367+
f(*repeat)
364368
}
365-
}
369+
},
366370
Expr::Literal(_) => {}
367371
}
368372
}
@@ -733,11 +737,26 @@ impl ExprCollector {
733737
let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
734738
self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
735739
}
740+
736741
ast::ExprKind::ArrayExpr(e) => {
737-
let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
738-
let repeat = e.repeat().map(|e| self.collect_expr(e));
739-
self.alloc_expr(Expr::Array { exprs, repeat }, syntax_ptr)
742+
let kind = e.kind();
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+
}
740758
}
759+
741760
ast::ExprKind::Literal(e) => {
742761
let lit = match e.kind() {
743762
LiteralKind::IntNumber { suffix } => {

crates/ra_hir/src/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl HirDisplay for ApplicationTy {
359359
}
360360
TypeCtor::Array => {
361361
let t = self.parameters.as_single();
362-
write!(f, "[{};usize]", t.display(f.db))?;
362+
write!(f, "[{};_]", t.display(f.db))?;
363363
}
364364
TypeCtor::RawPtr(m) => {
365365
let t = self.parameters.as_single();

crates/ra_hir/src/ty/infer.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
DefWithBody,
3333
ImplItem,
3434
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},
3636
generics::GenericParams,
3737
path::{GenericArgs, GenericArg},
3838
adt::VariantDef,
@@ -1074,7 +1074,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
10741074

10751075
Ty::apply(TypeCtor::Tuple, Substs(ty_vec.into()))
10761076
}
1077-
Expr::Array { exprs, repeat } => {
1077+
Expr::Array(array) => {
10781078
let elem_ty = match &expected.ty {
10791079
Ty::Apply(a_ty) => match a_ty.ctor {
10801080
TypeCtor::Slice | TypeCtor::Array => {
@@ -1085,17 +1085,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
10851085
_ => self.new_type_var(),
10861086
};
10871087

1088-
for expr in exprs.iter() {
1089-
self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone()));
1090-
}
1091-
1092-
if let Some(expr) = repeat {
1093-
self.infer_expr(
1094-
*expr,
1095-
&Expectation::has_type(Ty::simple(TypeCtor::Int(
1096-
primitive::UncertainIntTy::Known(primitive::IntTy::usize()),
1097-
))),
1098-
);
1088+
match array {
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+
}
10991103
}
11001104

11011105
Ty::apply_one(TypeCtor::Array, elem_ty)

crates/ra_syntax/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub use self::{
1717
generated::*,
1818
traits::*,
1919
tokens::*,
20-
extensions::{PathSegmentKind, StructKind, FieldKind, SelfParamKind},
21-
expr_extensions::{ElseBranch, PrefixOp, BinOp, LiteralKind},
20+
extensions::{PathSegmentKind, StructKind, SelfParamKind},
21+
expr_extensions::{ElseBranch, PrefixOp, BinOp, LiteralKind,ArrayExprKind},
2222
};
2323

2424
/// The main trait to go from untyped `SyntaxNode` to a typed ast. The

crates/ra_syntax/src/ast/expr_extensions.rs

+22
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ impl ast::BinExpr {
193193
}
194194
}
195195

196+
pub enum ArrayExprKind<'a> {
197+
Repeat { initializer: Option<&'a ast::Expr>, repeat: Option<&'a ast::Expr> },
198+
ElementList(AstChildren<'a, ast::Expr>),
199+
}
200+
201+
impl ast::ArrayExpr {
202+
pub fn kind(&self) -> ArrayExprKind {
203+
if self.is_repeat() {
204+
ArrayExprKind::Repeat {
205+
initializer: children(self).nth(0),
206+
repeat: children(self).nth(2),
207+
}
208+
} else {
209+
ArrayExprKind::ElementList(children(self))
210+
}
211+
}
212+
213+
fn is_repeat(&self) -> bool {
214+
self.syntax().children_with_tokens().any(|it| it.kind() == SEMI)
215+
}
216+
}
217+
196218
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
197219
pub enum LiteralKind {
198220
String,

0 commit comments

Comments
 (0)