ast: pub patterns, and, since Pat replaced Ty, !(never) patterns.

This commit is contained in:
2025-10-21 07:42:04 -04:00
parent ccb6bef6d9
commit c7e2e3d31d
3 changed files with 11 additions and 1 deletions

View File

@@ -65,6 +65,8 @@ pub enum Use {
pub enum Pat { pub enum Pat {
/// Matches anything without binding /// Matches anything without binding
Ignore, Ignore,
/// Matches nothing, ever
Never,
/// Matches nothing; used for macro substitution /// Matches nothing; used for macro substitution
MetId(String), MetId(String),
/// Matches anything, and binds it to a name /// Matches anything, and binds it to a name
@@ -84,6 +86,8 @@ pub enum Pat {
/// Operators on lists of patterns /// Operators on lists of patterns
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum PatOp { pub enum PatOp {
/// Changes the visibility mode to "public"
Pub,
/// Changes the binding mode to "mutable" /// Changes the binding mode to "mutable"
Mut, Mut,
/// Matches the dereference of a pointer (`&pat`) /// Matches the dereference of a pointer (`&pat`)
@@ -541,6 +545,7 @@ impl Display for Pat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::Ignore => "_".fmt(f), Self::Ignore => "_".fmt(f),
Self::Never => "!".fmt(f),
Self::Lit(literal) => literal.fmt(f), Self::Lit(literal) => literal.fmt(f),
Self::MetId(name) => write!(f, "`{name}"), Self::MetId(name) => write!(f, "`{name}"),
Self::Name(name) => name.fmt(f), Self::Name(name) => name.fmt(f),
@@ -569,6 +574,7 @@ impl Display for Pat {
impl Display for PatOp { impl Display for PatOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self { f.write_str(match self {
Self::Pub => "pub ",
Self::Mut => "mut ", Self::Mut => "mut ",
Self::Ref => "&", Self::Ref => "&",
Self::Rest => "..", Self::Rest => "..",

View File

@@ -180,6 +180,8 @@ impl<A: Annotation> Match<A> for Pat {
(Pat::MetId(name), _) => sub.add_pat(name.clone(), expr), (Pat::MetId(name), _) => sub.add_pat(name.clone(), expr),
(Pat::Ignore, Pat::Ignore) => true, (Pat::Ignore, Pat::Ignore) => true,
(Pat::Ignore, _) => false, (Pat::Ignore, _) => false,
(Pat::Never, Pat::Never) => true,
(Pat::Never, _) => false,
(Pat::Name(pat), Pat::Name(expr)) => pat == expr, (Pat::Name(pat), Pat::Name(expr)) => pat == expr,
(Pat::Name(_), _) => false, (Pat::Name(_), _) => false,
(Pat::Path(_), Pat::Path(_)) => true, (Pat::Path(_), Pat::Path(_)) => true,
@@ -197,7 +199,7 @@ impl<A: Annotation> Match<A> for Pat {
fn apply(&mut self, sub: &Subst<A>) { fn apply(&mut self, sub: &Subst<A>) {
match self { match self {
Pat::Ignore | Pat::Name(_) | Pat::Path(_) | Pat::Lit(_) => {} Pat::Ignore | Pat::Never | Pat::Name(_) | Pat::Path(_) | Pat::Lit(_) => {}
Pat::MetId(id) => { Pat::MetId(id) => {
if let Some(expr) = sub.pat.get(id) { if let Some(expr) = sub.pat.get(id) {
*self = expr.clone() *self = expr.clone()

View File

@@ -359,8 +359,10 @@ impl<'t> Parse<'t> for Pat {
Pat::Lit(p.parse(())?) Pat::Lit(p.parse(())?)
} }
TKind::Bar => p.consume().parse(level)?, TKind::Bar => p.consume().parse(level)?,
TKind::Bang => p.consume().then(Pat::Never),
TKind::Amp => Pat::Op(PatOp::Ref, vec![p.consume().parse(PPrec::Max)?]), TKind::Amp => Pat::Op(PatOp::Ref, vec![p.consume().parse(PPrec::Max)?]),
TKind::Mut => Pat::Op(PatOp::Mut, vec![p.consume().parse(level)?]), TKind::Mut => Pat::Op(PatOp::Mut, vec![p.consume().parse(level)?]),
TKind::Public => Pat::Op(PatOp::Pub, vec![p.consume().parse(level)?]),
TKind::AmpAmp => Pat::Op( TKind::AmpAmp => Pat::Op(
PatOp::Ref, PatOp::Ref,
vec![Pat::Op(PatOp::Ref, vec![p.consume().parse(PPrec::Max)?])], vec![Pat::Op(PatOp::Ref, vec![p.consume().parse(PPrec::Max)?])],