Skip to content

Commit

Permalink
Implement deref patterns via builtin#
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Mar 21, 2024
1 parent a99de98 commit b5e10de
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
23 changes: 0 additions & 23 deletions compiler/rustc_builtin_macros/src/deref_pat.rs

This file was deleted.

2 changes: 0 additions & 2 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ mod compile_error;
mod concat;
mod concat_bytes;
mod concat_idents;
mod deref_pat;
mod derive;
mod deriving;
mod edition_panic;
Expand Down Expand Up @@ -85,7 +84,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
concat_idents: concat_idents::expand_concat_idents,
const_format_args: format::expand_format_args,
core_panic: edition_panic::expand_panic,
deref: deref_pat::expand_deref_pat,
env: env::expand_env,
file: source_util::expand_file,
format_args: format::expand_format_args,
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1939,11 +1939,10 @@ impl<'a> Parser<'a> {
/// Parse `builtin # ident(args,*)`.
fn parse_expr_builtin(&mut self) -> PResult<'a, P<Expr>> {
self.parse_builtin(|this, lo, ident| {
if ident.name == sym::offset_of {
return Ok(Some(this.parse_expr_offset_of(lo)?));
}

Ok(None)
Ok(match ident.name {
sym::offset_of => Some(this.parse_expr_offset_of(lo)?),
_ => None,
})
})
}

Expand Down
20 changes: 19 additions & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,14 @@ impl<'a> Parser<'a> {
} else {
PatKind::Lit(const_expr)
}
} else if self.is_builtin() {
self.parse_pat_builtin()?
}
// Don't eagerly error on semantically invalid tokens when matching
// declarative macros, as the input to those doesn't have to be
// semantically valid. For attribute/derive proc macros this is not the
// case, so doing the recovery for them is fine.
} else if self.can_be_ident_pat()
else if self.can_be_ident_pat()
|| (self.is_lit_bad_ident().is_some() && self.may_recover())
{
// Parse `ident @ pat`
Expand Down Expand Up @@ -1119,6 +1122,21 @@ impl<'a> Parser<'a> {
.contains(&self.token.kind)
}

fn parse_pat_builtin(&mut self) -> PResult<'a, PatKind> {
self.parse_builtin(|self_, _lo, ident| {
Ok(match ident.name {
// builtin#deref(PAT)
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_alt(
None,
RecoverComma::Yes,
RecoverColon::Yes,
CommaRecoveryMode::LikelyTuple,
)?)),
_ => None,
})
})
}

/// Parses `box pat`
fn parse_pat_box(&mut self) -> PResult<'a, PatKind> {
let box_span = self.prev_token.span;
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,14 +1716,14 @@ pub(crate) mod builtin {

#[cfg(not(bootstrap))]
/// Unstable placeholder for type ascription.
#[rustc_builtin_macro]
#[allow_internal_unstable(builtin_syntax)]
#[unstable(
feature = "deref_patterns",
issue = "87121",
reason = "placeholder syntax for deref patterns"
)]
pub macro deref($pat:pat) {
/* compiler built-in */
builtin # deref($pat)
}

/// Unstable implementation detail of the `rustc` compiler, do not use.
Expand Down

0 comments on commit b5e10de

Please sign in to comment.