Implement macro-based deref!() syntax for deref patterns

Stop using `box PAT` syntax for deref patterns, as it's misleading and
also causes their semantics being tangled up.
This commit is contained in:
Michael Goulet 2024-03-20 16:53:50 -04:00
parent fb1b198cea
commit bc0965e2ff
6 changed files with 11 additions and 3 deletions

View file

@ -55,7 +55,7 @@ fn unary_pattern(pat: &Pat<'_>) -> bool {
| PatKind::Err(_) => false, | PatKind::Err(_) => false,
PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)), PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)),
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => etc.as_opt_usize().is_none() && array_rec(a), PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => etc.as_opt_usize().is_none() && array_rec(a),
PatKind::Ref(x, _) | PatKind::Box(x) => unary_pattern(x), PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) => unary_pattern(x),
PatKind::Path(_) | PatKind::Lit(_) => true, PatKind::Path(_) | PatKind::Lit(_) => true,
} }
} }

View file

@ -243,7 +243,7 @@ impl<'a> NormalizedPat<'a> {
fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self { fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self {
match pat.kind { match pat.kind {
PatKind::Wild | PatKind::Binding(.., None) => Self::Wild, PatKind::Wild | PatKind::Binding(.., None) => Self::Wild,
PatKind::Binding(.., Some(pat)) | PatKind::Box(pat) | PatKind::Ref(pat, _) => { PatKind::Binding(.., Some(pat)) | PatKind::Box(pat) | PatKind::Deref(pat) | PatKind::Ref(pat, _) => {
Self::from_pat(cx, arena, pat) Self::from_pat(cx, arena, pat)
}, },
PatKind::Never => Self::Never, PatKind::Never => Self::Never,

View file

@ -242,6 +242,8 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
|k| matches!(k, Box(_)), |k| matches!(k, Box(_)),
|k| always_pat!(k, Box(p) => p), |k| always_pat!(k, Box(p) => p),
), ),
// FIXME(deref_patterns): Should we merge patterns here?
Deref(_) => false,
// Transform `&mut x | ... | &mut y` into `&mut (x | y)`. // Transform `&mut x | ... | &mut y` into `&mut (x | y)`.
Ref(target, Mutability::Mut) => extend_with_matching( Ref(target, Mutability::Mut) => extend_with_matching(
target, start, alternatives, target, start, alternatives,

View file

@ -689,6 +689,11 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
kind!("Box({pat})"); kind!("Box({pat})");
self.pat(pat); self.pat(pat);
}, },
PatKind::Deref(pat) => {
bind!(self, pat);
kind!("Deref({pat})");
self.pat(pat);
},
PatKind::Ref(pat, muta) => { PatKind::Ref(pat, muta) => {
bind!(self, pat); bind!(self, pat);
kind!("Ref({pat}, Mutability::{muta:?})"); kind!("Ref({pat}, Mutability::{muta:?})");

View file

@ -955,6 +955,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
} }
}, },
PatKind::Box(pat) => self.hash_pat(pat), PatKind::Box(pat) => self.hash_pat(pat),
PatKind::Deref(pat) => self.hash_pat(pat),
PatKind::Lit(expr) => self.hash_expr(expr), PatKind::Lit(expr) => self.hash_expr(expr),
PatKind::Or(pats) => { PatKind::Or(pats) => {
for pat in pats { for pat in pats {

View file

@ -1678,7 +1678,7 @@ pub fn is_refutable(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
match pat.kind { match pat.kind {
PatKind::Wild | PatKind::Never => false, // If `!` typechecked then the type is empty, so not refutable. PatKind::Wild | PatKind::Never => false, // If `!` typechecked then the type is empty, so not refutable.
PatKind::Binding(_, _, _, pat) => pat.map_or(false, |pat| is_refutable(cx, pat)), PatKind::Binding(_, _, _, pat) => pat.map_or(false, |pat| is_refutable(cx, pat)),
PatKind::Box(pat) | PatKind::Ref(pat, _) => is_refutable(cx, pat), PatKind::Box(pat) | PatKind::Deref(pat) | PatKind::Ref(pat, _) => is_refutable(cx, pat),
PatKind::Path(ref qpath) => is_enum_variant(cx, qpath, pat.hir_id), PatKind::Path(ref qpath) => is_enum_variant(cx, qpath, pat.hir_id),
PatKind::Or(pats) => { PatKind::Or(pats) => {
// TODO: should be the honest check, that pats is exhaustive set // TODO: should be the honest check, that pats is exhaustive set