Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Macros 2.0 to generate a module does not allow access to items #47797

Closed
alexreg opened this issue Jan 27, 2018 · 3 comments
Closed

Using Macros 2.0 to generate a module does not allow access to items #47797

alexreg opened this issue Jan 27, 2018 · 3 comments

Comments

@alexreg
Copy link
Contributor

alexreg commented Jan 27, 2018

I see no reason why the following code shouldn't work on the latest nightly,

#![feature(decl_macro)]

macro foo($mod_name:ident) {
    pub mod $mod_name {
        pub const BAR: u32 = 1;
    }
}

foo!(foo_mod);

fn main() {
	println!("{}", foo_mod::BAR);
}

yet I get an odd error.

error[E0425]: cannot find value `BAR` in module `FooMod`
  --> src/main.rs:12:25
   |
12 |     println!("{}", FooMod::BAR);
   |                            ^^^ not found in `FooMod`
help: possible candidate is found in another module, you can import it into scope
   |
3  | use FooMod::BAR;
   |

error: aborting due to previous error

Perhaps I'm missing something about how hygiene works in macros 2.0?

@dtolnay
Copy link
Member

dtolnay commented Jan 27, 2018

Thanks! I believe this is a duplicate of #46342. The BAR defined in the macro is protected by hygiene 2.0 and cannot be named from the outside the macro. As a workaround, it should work if the name of BAR originates from outside the macro.

macro foo($mod_name:ident, $bar:ident) {
    pub mod $mod_name {
        pub const $bar: u32 = 1;
    }
}

foo!(FooMod, BAR);

@dtolnay dtolnay closed this as completed Jan 27, 2018
@alexreg
Copy link
Contributor Author

alexreg commented Jan 27, 2018

@dtolnay Ah okay, thanks. Looks like we need hygiene opt-out desperately. Do you know the state of that?

@dtolnay
Copy link
Member

dtolnay commented Jan 27, 2018

Yep the hygiene opt-out is tracked in #39412 where it talks about "hygiene bending".

@alexreg alexreg changed the title Using macro 2.0 to generate a module does not allow access to items Using Macros 2.0 to generate a module does not allow access to items Feb 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants