From a9d0c9b2a4601835d1dd480b711c5d26f9c97430 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Wed, 1 Apr 2020 03:37:51 -0700 Subject: lower literal patterns --- crates/ra_hir_def/src/body/lower.rs | 54 ++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index e8443dde8..7b809cf4f 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -417,26 +417,7 @@ impl ExprCollector<'_> { } } - ast::Expr::Literal(e) => { - let lit = match e.kind() { - LiteralKind::IntNumber { suffix } => { - let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it)); - - Literal::Int(Default::default(), known_name) - } - LiteralKind::FloatNumber { suffix } => { - let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it)); - - Literal::Float(Default::default(), known_name) - } - LiteralKind::ByteString => Literal::ByteString(Default::default()), - LiteralKind::String => Literal::String(Default::default()), - LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)), - LiteralKind::Bool => Literal::Bool(Default::default()), - LiteralKind::Char => Literal::Char(Default::default()), - }; - self.alloc_expr(Expr::Literal(lit), syntax_ptr) - } + ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr), ast::Expr::IndexExpr(e) => { let base = self.collect_expr_opt(e.base()); let index = self.collect_expr_opt(e.index()); @@ -679,10 +660,19 @@ impl ExprCollector<'_> { suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(), } } + ast::Pat::LiteralPat(lit) => { + if let Some(ast_lit) = lit.literal() { + let expr = Expr::Literal(ast_lit.kind().into()); + let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit)); + let expr_id = self.alloc_expr(expr, expr_ptr); + Pat::Lit(expr_id) + } else { + Pat::Missing + } + } // FIXME: implement ast::Pat::BoxPat(_) => Pat::Missing, - ast::Pat::LiteralPat(_) => Pat::Missing, ast::Pat::RangePat(_) => Pat::Missing, }; let ptr = AstPtr::new(&pat); @@ -741,3 +731,25 @@ impl From for BinaryOp { } } } + +impl From for Literal { + fn from(ast_lit_kind: ast::LiteralKind) -> Self { + match ast_lit_kind { + LiteralKind::IntNumber { suffix } => { + let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it)); + + Literal::Int(Default::default(), known_name) + } + LiteralKind::FloatNumber { suffix } => { + let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it)); + + Literal::Float(Default::default(), known_name) + } + LiteralKind::ByteString => Literal::ByteString(Default::default()), + LiteralKind::String => Literal::String(Default::default()), + LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)), + LiteralKind::Bool => Literal::Bool(Default::default()), + LiteralKind::Char => Literal::Char(Default::default()), + } + } +} -- cgit v1.2.3