From cf6f989a8d638b76ec7e14d00f7ed095b20ffed6 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Thu, 6 May 2021 19:39:27 +0500 Subject: Lower bool literals --- crates/hir_ty/src/diagnostics/pattern.rs | 19 ++++++++++++++++++ .../src/diagnostics/pattern/deconstruct_pat.rs | 23 ++++++++++------------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/crates/hir_ty/src/diagnostics/pattern.rs b/crates/hir_ty/src/diagnostics/pattern.rs index 7b0b63d0b..14dd736a6 100644 --- a/crates/hir_ty/src/diagnostics/pattern.rs +++ b/crates/hir_ty/src/diagnostics/pattern.rs @@ -67,6 +67,11 @@ pub enum PatKind { subpattern: Pat, }, + // only bool for now + LiteralBool { + value: bool, + }, + /// An or-pattern, e.g. `p | q`. /// Invariant: `pats.len() >= 2`. Or { @@ -99,6 +104,8 @@ impl<'a> PatCtxt<'a> { let kind = match self.body[pat] { hir_def::expr::Pat::Wild => PatKind::Wild, + hir_def::expr::Pat::Lit(expr) => self.lower_lit(expr), + hir_def::expr::Pat::Path(ref path) => { return self.lower_path(pat, path); } @@ -211,6 +218,18 @@ impl<'a> PatCtxt<'a> { } } } + + fn lower_lit(&mut self, expr: hir_def::expr::ExprId) -> PatKind { + use hir_def::expr::{Expr, Literal::Bool}; + + match self.body[expr] { + Expr::Literal(Bool(value)) => PatKind::LiteralBool { value }, + _ => { + self.errors.push(PatternError::Unimplemented); + PatKind::Wild + } + } + } } #[cfg(test)] diff --git a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs index 1319745ce..91b9c7184 100644 --- a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs +++ b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs @@ -275,14 +275,8 @@ impl Constructor { PatKind::Binding { .. } | PatKind::Wild => Wildcard, PatKind::Leaf { .. } | PatKind::Deref { .. } => Single, &PatKind::Variant { enum_variant, .. } => Variant(enum_variant), - - //Todo - // &Pat::Lit(expr_id) => match cx.body[expr_id] { - // Expr::Literal(Literal::Bool(val)) => IntRange(IntRange::from_bool(val)), - // _ => todo!(), - // }, + &PatKind::LiteralBool { value } => IntRange(IntRange::from_bool(value)), PatKind::Or { .. } => panic!("bug: Or-pattern should have been expanded earlier on."), - pat => todo!("Constructor::from_pat {:?}", pat), } } @@ -690,7 +684,7 @@ impl Fields { let mut subpatterns = subpatterns_and_indices.iter().map(|&(_, p)| pcx.cx.pattern_arena.borrow()[p].clone()); // FIXME(iDawer) witnesses are not yet used - const UNIMPLEMENTED: PatKind = PatKind::Wild; + const UNHANDLED: PatKind = PatKind::Wild; let pat = match ctor { Single | Variant(_) => match pcx.ty.kind(&Interner) { @@ -728,10 +722,10 @@ impl Fields { } _ => PatKind::Wild, }, - Constructor::Slice(slice) => UNIMPLEMENTED, - Str(_) => UNIMPLEMENTED, - FloatRange(..) => UNIMPLEMENTED, - Constructor::IntRange(_) => UNIMPLEMENTED, + Constructor::Slice(slice) => UNHANDLED, + Str(_) => UNHANDLED, + FloatRange(..) => UNHANDLED, + Constructor::IntRange(_) => UNHANDLED, NonExhaustive => PatKind::Wild, Wildcard => return Pat::wildcard_from_ty(pcx.ty), Opaque => panic!("bug: we should not try to apply an opaque constructor"), @@ -855,7 +849,10 @@ impl Fields { self.replace_with_fieldpats(subpatterns) } - PatKind::Wild | PatKind::Binding { .. } | PatKind::Or { .. } => self.clone(), + PatKind::Wild + | PatKind::Binding { .. } + | PatKind::LiteralBool { .. } + | PatKind::Or { .. } => self.clone(), } } } -- cgit v1.2.3