Skip to content

macro-generated attributes applied to item-generating macros are silently ignored #30125

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

Closed
durka opened this issue Nov 30, 2015 · 1 comment
Closed

Comments

@durka
Copy link
Contributor

durka commented Nov 30, 2015

(Sorry about the title. Yo dawg, I heard you like generators...)

This code contains a macro that expands to an cfg-annotated call to another macro, which defines an item. The cfg should cause everything to be dropped, but surprisingly the item is defined anyway.

macro_rules! group_attr {
    (#[cfg($attr:meta)] $($yes:item)*) => {
        $(#[cfg($attr)] $yes)*
    };
}

macro_rules! define {
    ($i:item) => { $i }
}

group_attr! {
    #[cfg(nope)]

    define! {
        fn foo() {}
    }
}

fn main() {
    foo(); // ~ERROR unresolved name
}

This is a near-minimal test case because combinations with fewer layers of indirection (i.e. removing define!) produce the expected behavior.

I think the root cause is that cfg stripping is only performed before and after, but not during, macro expansion. So here, (1) initial cfg stripping does nothing, then during macro expansion (2a) group_attr! is expanded producing the annotated call to define!, (2b) define! is expanded, dropping the attribute, and finally (3) final cfg stripping again does nothing.

Ideally this could be restructured somehow so that cfg stripping can be done during macro expansion. But if not, at least we could have a warning during step (2b) above when the attribute evaporates.

Is my understanding correct?

@durka
Copy link
Contributor Author

durka commented Dec 9, 2015

Whoops, this is #22250.

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

1 participant