Skip to content

hygiene: cannot find macro foo! in this scope #63685

Closed

Description

@tesuji

This code (playpen) compiles on stable but not on nightly and beta:

macro_rules! foo {
    () => {
        "foo"
    };
}

macro_rules! bar {
    () => {
        foo!()
    };
}

fn main() {
    println!(bar!());
}

Standard Error

   Compiling playground v0.0.1 (/playground)
error: cannot find macro `foo!` in this scope
 --> src/main.rs:9:9
  |
9 |         foo!()
  |         ^^^

warning: unused macro definition
 --> src/main.rs:1:1
  |
1 | / macro_rules! foo {
2 | |     () => {
3 | |         "foo"
4 | |     };
5 | | }
  | |_^
  |
  = note: `#[warn(unused_macros)]` on by default

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Edit: Removed concat! macro

Originally posted by @lzutao in #63460 (comment)

Activity

  1. tesuji commented on Aug 18, 2019

    @tesuji
    Author
  2. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    A-resolveArea: Name/path resolution done by `rustc_resolve` specifically
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 18, 2019
  3. self-assigned this
    on Aug 18, 2019
  4. tesuji commented on Aug 18, 2019

    @tesuji
    Author
  5. petrochenkov commented on Aug 18, 2019

    @petrochenkov
    Contributor

    This is a regression from #63114 (cc @matthewjasper).
    Slightly more minimized reproduction will look like this:

    macro_rules! foo {
        () => {
            "foo"
        };
    }
    
    macro_rules! bar {
        () => {
            foo!()
        };
    }
    
    fn main() {
        format_args!(bar!()); // ERROR, `foo` is not found, happens because `format_args` is opaque now
    }

    format_args performs eager expansion on its arguments and hygienic context of foo produced by such expansion looks like it was produced by the opaque format_args for some reason, so it's resolved at format_args' definition site.

    It maybe either some issue with hygiene infra interacting with eager expansion, but it also may be caused by incorrect spans produced by format_args.

  6. petrochenkov commented on Aug 18, 2019

    @petrochenkov
    Contributor

    (I'm currently looking into the details.)

    UPD: I have a fix locally, will submit tomorrow.

  7. petrochenkov commented on Aug 19, 2019

    @petrochenkov
    Contributor

    Fixed in #63717.

  8. added a commit that references this issue on Aug 21, 2019
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-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions