Skip to content

$crate -> $ident:ident -> identity_proc_macro!($ident) panics in the proc macro #101211

Description

@rodrimati1992

Code

I tried cargo test-ing this proc macro crate:

/// ```rust
/// pub extern crate proc_macro_crate;
/// 
/// #[macro_export]
/// macro_rules! foo {
///     ($($path:ident)::*) => (
///         $crate::proc_macro_crate::identity!{
///             $($path)::*
///         }
///     )
/// }
/// 
/// #[macro_export]
/// macro_rules! baz {
///     () => (
///         $crate::foo!($crate::BAR)
///     )
/// }
/// 
/// pub const BAR: u32 = 19;
/// 
/// fn main(){ 
///     println!("{}", crate::baz!());
/// }
/// 
/// ```
#[proc_macro]
pub fn identity(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    input.into_iter().collect()
}

I expected to see this happen: The test completes successfully

Instead, this happened:
The test fails compilation with this error:

error: proc macro panicked
  --> src/lib.rs:7:9
   |
7  | /         $crate::proc_macro_crate::identity!{
8  | |             $($path)::*
9  | |         }
   | |_________^
...
23 |       println!("{}", crate::baz!());
   |                      ------------- in this macro invocation
   |
   = help: message: `"$crate"` is not a valid identifier
   = note: this error originates in the macro `$crate::foo` (in Nightly builds, run with -Z macro-backtrace for more info)

Version it worked on

It most recently worked on:

rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-unknown-linux-gnu
release: 1.60.0
LLVM version: 14.0.0

Version with regression

rustc --version --verbose:

rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0

This also fails in 1.62 and 1.63.

Meta

This appears to be fixed in the beta channel, but I could not find a mention of this bug or a related one.

It's entirely possible that the bug will trigger again in future versions if this code isn't used in tests.

Activity

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

Metadata

Metadata

Assignees

Labels

A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions