From b5e10de27b9331fc73d8e8733230cfef93f97689 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 20 Mar 2024 21:47:38 -0400 Subject: [PATCH] Implement deref patterns via builtin# --- .../rustc_builtin_macros/src/deref_pat.rs | 23 ------------------- compiler/rustc_builtin_macros/src/lib.rs | 2 -- compiler/rustc_parse/src/parser/expr.rs | 9 ++++---- compiler/rustc_parse/src/parser/pat.rs | 20 +++++++++++++++- library/core/src/macros/mod.rs | 4 ++-- 5 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 compiler/rustc_builtin_macros/src/deref_pat.rs diff --git a/compiler/rustc_builtin_macros/src/deref_pat.rs b/compiler/rustc_builtin_macros/src/deref_pat.rs deleted file mode 100644 index 7351267e23200..0000000000000 --- a/compiler/rustc_builtin_macros/src/deref_pat.rs +++ /dev/null @@ -1,23 +0,0 @@ -use rustc_ast as ast; -use rustc_ast::tokenstream::TokenStream; -use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult}; -use rustc_parse::parser::{CommaRecoveryMode, RecoverColon, RecoverComma}; -use rustc_span::Span; - -pub fn expand_deref_pat( - cx: &mut ExtCtxt<'_>, - span: Span, - tts: TokenStream, -) -> MacroExpanderResult<'static> { - ExpandResult::Ready( - match cx.new_parser_from_tts(tts).parse_pat_allow_top_alt( - None, - RecoverComma::Yes, - RecoverColon::Yes, - CommaRecoveryMode::LikelyTuple, - ) { - Ok(parsed) => MacEager::pat(cx.pat(span, ast::PatKind::Deref(parsed))), - Err(err) => DummyResult::any(span, err.emit()), - }, - ) -} diff --git a/compiler/rustc_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index 55ebf69d3028e..f344dbcd10c06 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -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; @@ -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, diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 136145dd1826d..18f8e492b218f 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1939,11 +1939,10 @@ impl<'a> Parser<'a> { /// Parse `builtin # ident(args,*)`. fn parse_expr_builtin(&mut self) -> PResult<'a, P> { 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, + }) }) } diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 88ae1b5420fe7..6837c99758205 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -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` @@ -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; diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index b2656d045e7e4..8a3bd14a49299 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -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.