Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ fn maybe_emit_macro_metavar_expr_concat_feature(features: &Features, sess: &Sess
}
}

fn maybe_emit_macro_metavar_dollar_dollar_crate_feature(
features: &Features,
sess: &Session,
span: Span,
) {
if !features.macro_metavar_expr_dollar_dollar_crate() {
let msg = "the `$$crate` meta-variable expression is unstable";
feature_err(sess, sym::macro_metavar_expr_dollar_dollar_crate, span, msg).emit();
}
}

/// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Specifically, this takes a
/// generic `TokenTree`, such as is used in the rest of the compiler, and returns a `TokenTree`
/// for use in parsing a macro.
Expand Down Expand Up @@ -319,6 +330,18 @@ fn parse_tree<'a>(
} else {
maybe_emit_macro_metavar_expr_feature(features, sess, dollar_span2);
}

// Gate `$$crate`.
if let Some(tokenstream::TokenTree::Token(token, _)) = iter.peek()
&& let Some((ident, is_raw)) = token.ident()
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& let Some((ident, is_raw)) = token.ident()
&& let Some((ident, IdentIsRaw::No)) = token.ident()

View changes since the review

&& ident.name == kw::Crate
&& matches!(is_raw, IdentIsRaw::No)
{
maybe_emit_macro_metavar_dollar_dollar_crate_feature(
features, sess, ident.span,
);
}

TokenTree::token(token::Dollar, dollar_span2)
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ declare_features! (
(unstable, macro_metavar_expr, "1.61.0", Some(83527)),
/// Provides a way to concatenate identifiers using metavariable expressions.
(unstable, macro_metavar_expr_concat, "1.81.0", Some(124225)),
/// Allows using `$$crate`.
(incomplete, macro_metavar_expr_dollar_dollar_crate, "CURRENT_RUSTC_VERSION", Some(155111)),
/// Allows `#[marker]` on certain traits allowing overlapping implementations.
(unstable, marker_trait_attr, "1.30.0", Some(29864)),
/// Enable mgca `type const` syntax before expansion.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ symbols! {
macro_literal_matcher,
macro_metavar_expr,
macro_metavar_expr_concat,
macro_metavar_expr_dollar_dollar_crate,
macro_reexport,
macro_use,
macro_vis_matcher,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![feature(macro_metavar_expr)]
pub const IDX: usize = 1;

macro_rules! _direct_usage_super_2 {
() => {
macro_rules! _direct_usage_sub_2 {
() => {
$$crate
//~^ ERROR the `$$crate` meta-variable expression is unstable
}
}
};
}

macro_rules! indirect_usage_crate {
($d:tt) => {
const _FOO: usize = $d$d crate::IDX;
//~^ ERROR expected expression, found `$`
};
}
macro_rules! indirect_usage_use {
($d:tt) => {
indirect_usage_crate!($d);
}
}
indirect_usage_use!($);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0658]: the `$$crate` meta-variable expression is unstable
--> $DIR/feature-gate-macro-metavar-expr-dollar-dollar-crate.rs:8:19
|
LL | $$crate
| ^^^^^
|
= note: see issue #155111 <https://github.com/rust-lang/rust/issues/155111> for more information
= help: add `#![feature(macro_metavar_expr_dollar_dollar_crate)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: expected expression, found `$`
--> $DIR/feature-gate-macro-metavar-expr-dollar-dollar-crate.rs:17:29
|
LL | const _FOO: usize = $d$d crate::IDX;
| ^^ expected expression

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
Loading