Skip to content

Add FCW to disallow $crate in macro transcriber#155121

Open
nik-rev wants to merge 1 commit intorust-lang:mainfrom
nik-contrib:dollar-crate-in-transcriber
Open

Add FCW to disallow $crate in macro transcriber#155121
nik-rev wants to merge 1 commit intorust-lang:mainfrom
nik-contrib:dollar-crate-in-transcriber

Conversation

@nik-rev
Copy link
Copy Markdown
Contributor

@nik-rev nik-rev commented Apr 10, 2026

This PR adds a new warn-by-default future incompatibility lint that lints on usage of $crate inside of a transcriber.

#![deny(dollar_crate_in_transcriber)]

macro_rules! x {
    ($crate) => {};
    //~^ ERROR usage of `$crate` in transcriber [dollar_crate_in_transcriber]
    //~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
}

Tracking Issue: #155123

Reason:

$crate even in macro patterns is getting glued into a single identifier token. Thus, in the macro pattern, it can only be fulfilled by another glued $crate token, which is only possible do in the expansion of a macro.

I think it is better to forbid this usage, as it is strongly inconsistent with the behavior of other keywords in macro binders (they work like any other identifier and are currently not reserved in this position)

Example:

This shows the strange property of $crate in a transcriber: the call! macro is defined inside lib.rs, so the $crate token belongs to lib.rs. Yet, despite that, it still matches against the $crate token produced inside of main.rs.

// lib.rs
#[macro_export]
macro_rules! lib_macro {
    ($crate) => {};
}

#[macro_export]
macro_rules! call {
    ($m:path) => {
        $m!($crate);
    };
}

// main.rs
use lib::*;

macro_rules! main_macro {
    ($crate) => {};
}

call!(lib_macro);
call!(main_macro);

fn main() {}

Expected behavior:

  • $crate: without a fragment specifier, error indicating that a fragment specifier is missing
  • $crate:tt: with a fragment specifier, error indicating that $crate is a reserved identifier that cannot be used as the name of a metavariable.

Explanation and example is from #99037

History:

  • Original PR tried to make it an error: Forbid $crate in macro patterns #99447
  • From crater, we found 2 breakages from this, notably in ndarray - so it needed to be updated to a future-compatibility lint. Especially now, since 4 years have passed.
  • This PR picks up the work by @CAD97 and turns the lint into a future incompatibility lint

Previous PR: #99447, which received no response from the author, so I'm picking it up

Issue: #99037

CC @CAD97

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 12 candidates

@nik-rev
Copy link
Copy Markdown
Contributor Author

nik-rev commented Apr 10, 2026

@rustbot label +I-lang-nominated

@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Apr 10, 2026
@rust-log-analyzer

This comment has been minimized.

@nik-rev nik-rev force-pushed the dollar-crate-in-transcriber branch from 67232bd to 30cb21b Compare April 10, 2026 20:09
@fmease
Copy link
Copy Markdown
Member

fmease commented Apr 10, 2026

The list before the => is the matcher, not the transcriber. The $crate in macro_rules! x { ($crate) => {}; } is in the matcher. What's to the right of => is the transcriber.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants