Skip to content

Erroneous expression parsing of macro invocations with paths #44065

Closed as not planned
@SergioBenitez

Description

@SergioBenitez

Consider the following rustc internal code which parses an ast::Path and returns an invocation of a macro at that path:

let mut parser = ecx.new_parser_from_tts(args);
let path = parser.parse_path(PathStyle::Mod).expect("path");
MacEager::expr(quote_expr!(ecx, $path!()))

The expected expansion of a procedural macro implemented in this manner is PATH ! ( ). Instead, this expands to PATH erroneously. Said another way, if the code above corresponded to a macro named m, we would expect m!(some_path); to expand to some_path!() when it instead expands to some_path.

Note that if instead an ast::Ident is used in place of the ast::Path above, things work as expected. This corresponds to the code below:

let mut parser = ecx.new_parser_from_tts(args);
let ident = parser.parse_ident().expect("ident");
MacEager::expr(quote_expr!(ecx, $ident!()))

After some investigation, the issue appears to reside in Parser::parse_expr. I've created a repository where the issue can be reproduced: https://github.com/SergioBenitez/path-macro-repro. The repository also includes a work around for the problem (https://github.com/SergioBenitez/path-macro-repro/blob/master/src/lib.rs#L27).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions