From 4992d2bf79e9da6db759eb8e1715f90f31ec7eb9 Mon Sep 17 00:00:00 2001 From: oxalica Date: Fri, 29 Nov 2019 03:10:16 +0800 Subject: Infer range types --- crates/ra_hir_def/src/body/lower.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/body/lower.rs') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 331736cb2..d18964d54 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -8,7 +8,7 @@ use hir_expand::{ use ra_arena::Arena; use ra_syntax::{ ast::{ - self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, + self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, RangeOp, TypeAscriptionOwner, }, AstNode, AstPtr, @@ -429,10 +429,28 @@ where let index = self.collect_expr_opt(e.index()); self.alloc_expr(Expr::Index { base, index }, syntax_ptr) } + ast::Expr::RangeExpr(e) => { + let lhs = e.start().map(|lhs| self.collect_expr(lhs)); + let rhs = e.end().map(|rhs| self.collect_expr(rhs)); + match (lhs, e.op_kind(), rhs) { + (None, _, None) => self.alloc_expr(Expr::RangeFull, syntax_ptr), + (Some(lhs), _, None) => self.alloc_expr(Expr::RangeFrom { lhs }, syntax_ptr), + (None, Some(RangeOp::Inclusive), Some(rhs)) => { + self.alloc_expr(Expr::RangeToInclusive { rhs }, syntax_ptr) + } + (Some(lhs), Some(RangeOp::Inclusive), Some(rhs)) => { + self.alloc_expr(Expr::RangeInclusive { lhs, rhs }, syntax_ptr) + } + // If RangeOp is missing, fallback to exclusive range. + (None, _, Some(rhs)) => self.alloc_expr(Expr::RangeTo { rhs }, syntax_ptr), + (Some(lhs), _, Some(rhs)) => { + self.alloc_expr(Expr::Range { lhs, rhs }, syntax_ptr) + } + } + } // FIXME implement HIR for these: ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), - ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::Expr::MacroCall(e) => match self.expander.enter_expand(self.db, e) { Some((mark, expansion)) => { let id = self.collect_expr(expansion); -- cgit v1.2.3