mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 11:20:54 +00:00
Lower bool literals
This commit is contained in:
parent
d6d77e8a35
commit
cf6f989a8d
@ -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)]
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user