Lower bool literals

This commit is contained in:
Dawer 2021-05-06 19:39:27 +05:00
parent d6d77e8a35
commit cf6f989a8d
2 changed files with 29 additions and 13 deletions

View File

@ -67,6 +67,11 @@ pub enum PatKind {
subpattern: Pat, subpattern: Pat,
}, },
// only bool for now
LiteralBool {
value: bool,
},
/// An or-pattern, e.g. `p | q`. /// An or-pattern, e.g. `p | q`.
/// Invariant: `pats.len() >= 2`. /// Invariant: `pats.len() >= 2`.
Or { Or {
@ -99,6 +104,8 @@ impl<'a> PatCtxt<'a> {
let kind = match self.body[pat] { let kind = match self.body[pat] {
hir_def::expr::Pat::Wild => PatKind::Wild, hir_def::expr::Pat::Wild => PatKind::Wild,
hir_def::expr::Pat::Lit(expr) => self.lower_lit(expr),
hir_def::expr::Pat::Path(ref path) => { hir_def::expr::Pat::Path(ref path) => {
return self.lower_path(pat, 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)] #[cfg(test)]

View File

@ -275,14 +275,8 @@ impl Constructor {
PatKind::Binding { .. } | PatKind::Wild => Wildcard, PatKind::Binding { .. } | PatKind::Wild => Wildcard,
PatKind::Leaf { .. } | PatKind::Deref { .. } => Single, PatKind::Leaf { .. } | PatKind::Deref { .. } => Single,
&PatKind::Variant { enum_variant, .. } => Variant(enum_variant), &PatKind::Variant { enum_variant, .. } => Variant(enum_variant),
&PatKind::LiteralBool { value } => IntRange(IntRange::from_bool(value)),
//Todo
// &Pat::Lit(expr_id) => match cx.body[expr_id] {
// Expr::Literal(Literal::Bool(val)) => IntRange(IntRange::from_bool(val)),
// _ => todo!(),
// },
PatKind::Or { .. } => panic!("bug: Or-pattern should have been expanded earlier on."), 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 = let mut subpatterns =
subpatterns_and_indices.iter().map(|&(_, p)| pcx.cx.pattern_arena.borrow()[p].clone()); subpatterns_and_indices.iter().map(|&(_, p)| pcx.cx.pattern_arena.borrow()[p].clone());
// FIXME(iDawer) witnesses are not yet used // FIXME(iDawer) witnesses are not yet used
const UNIMPLEMENTED: PatKind = PatKind::Wild; const UNHANDLED: PatKind = PatKind::Wild;
let pat = match ctor { let pat = match ctor {
Single | Variant(_) => match pcx.ty.kind(&Interner) { Single | Variant(_) => match pcx.ty.kind(&Interner) {
@ -728,10 +722,10 @@ impl Fields {
} }
_ => PatKind::Wild, _ => PatKind::Wild,
}, },
Constructor::Slice(slice) => UNIMPLEMENTED, Constructor::Slice(slice) => UNHANDLED,
Str(_) => UNIMPLEMENTED, Str(_) => UNHANDLED,
FloatRange(..) => UNIMPLEMENTED, FloatRange(..) => UNHANDLED,
Constructor::IntRange(_) => UNIMPLEMENTED, Constructor::IntRange(_) => UNHANDLED,
NonExhaustive => PatKind::Wild, NonExhaustive => PatKind::Wild,
Wildcard => return Pat::wildcard_from_ty(pcx.ty), Wildcard => return Pat::wildcard_from_ty(pcx.ty),
Opaque => panic!("bug: we should not try to apply an opaque constructor"), Opaque => panic!("bug: we should not try to apply an opaque constructor"),
@ -855,7 +849,10 @@ impl Fields {
self.replace_with_fieldpats(subpatterns) self.replace_with_fieldpats(subpatterns)
} }
PatKind::Wild | PatKind::Binding { .. } | PatKind::Or { .. } => self.clone(), PatKind::Wild
| PatKind::Binding { .. }
| PatKind::LiteralBool { .. }
| PatKind::Or { .. } => self.clone(),
} }
} }
} }