Skip to content

Identity macro 'launders' hygiene information #59011

Closed
@pierzchalski

Description

@pierzchalski

Consider this hygiene demonstration (playground):

#![feature(decl_macro)]

macro x() {
    println!("x");
}

macro y() {
    x!(); // This `x` identifier is in a fresh hygiene scope within `y`,
    macro x() { println!("y"); } // so it resolves to this definition.
}

fn main() {
    y!();
}

This prints "y", as expected.

However, if we 'launder' the definition macro x() using an identity macro (playground):

#![feature(decl_macro)]

macro id($($tt:tt)*) {
    $($tt)*
}

macro x() {
    println!("x");
}

macro y() {
    x!();
    id!(macro x() { println!("y"); }); // What is the scope of this `x`?
}

fn main() {
    y!();
}

We get an error that the invocation of x is ambiguous.

Is this the expected behaviour?

Metadata

Metadata

Assignees

Labels

A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions