Skip to content

Macros 2.0: macro defined and used in same function-like scope can't resolve its own items #52389

Open
@arielb1

Description

@arielb1

Maybe this is just a misunderstanding of hygiene, but if a macro 2.0 is defined and used in the same function-like scope (function/const) then it can't resolve its own items:

#![feature(decl_macro)]

trait Tr { fn foo(self); }

const C: () = {
    macro implit {
        () => {
            fn doit() { println!("Hi"); }
            impl ::Tr for () {
                fn foo(self) {
                    doit();
                }
            }
        }
    }
    
    
    #[cfg(works)]
    const T: () = { implit!(); () }; // this works with no error
    
    #[cfg(not(works))]
    implit!(); //~ ERROR cannot find function `doit` in this scope
};

fn main() {
    ().foo();
}

Expected Result

doit() should resolve to the fn doit in the scope of the macro, printing Hi!

Actual Result

Resolution error.

Note that

  1. Having the macro defined and used in a module (i.e., if const C: () was a mod _xyz) does work.
  2. Having the macro defined in 1 function-like scope, and used in a child scope of it (see cfg(works)) also works. This error only occurs if the macro is defined and used in the same scope.

cc @petrochenkov @nrc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyF-decl_macro`#![feature(decl_macro)]`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