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

#![cfg_attr] doesn't work for crate-level attributes #25347

Closed
lilyball opened this issue May 12, 2015 · 5 comments · Fixed by #25399
Closed

#![cfg_attr] doesn't work for crate-level attributes #25347

lilyball opened this issue May 12, 2015 · 5 comments · Fixed by #25399
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`)

Comments

@lilyball
Copy link
Contributor

The #[cfg_attr] attribute doesn't work correctly for crate-level attributes. It seems to work in other cases as an inner attribute, but for crate-level attributes it yields the same warning that you'd see if you used the crate attribute on a non-root module:

#![cfg_attr(foo, crate_type="lib")]

fn main() {}

When compiled with rustc --cfg foo:

unnamed.rs:1:18: 1:35 warning: unused attribute, #[warn(unused_attributes)] on by default
unnamed.rs:1 #![cfg_attr(foo, crate_type="lib")]
                              ^~~~~~~~~~~~~~~~~
unnamed.rs:1:18: 1:35 warning: crate-level attribute should be in the root module, #[warn(unused_attributes)] on by default
unnamed.rs:1 #![cfg_attr(foo, crate_type="lib")]
                              ^~~~~~~~~~~~~~~~~
@lilyball
Copy link
Contributor Author

I'm actually kind of surprised to see this bug. I would have expected #![cfg_attr] to be used in order to enable features based on cargo features, e.g. a cargo feature nightly that enables the use of features such that the same lib can be used in the beta and nightly channels.

@lilyball
Copy link
Contributor Author

Also to note, the --pretty expanded output yields a valid crate-level attribute that works properly when compiled.

@klutzy
Copy link
Contributor

klutzy commented May 13, 2015

It's a bug/limitation that some special attributes doesn't work with cfg_attr. For example, cfg_attr doesn't work with #[path] attribute. In the other hand, #![cfg_attr(foo, allow(dead_code))] works.
This is caused because crate_type or path attributes are consumed before cfg_attr expansion.

@sfackler
Copy link
Member

It may make sense to move #[cfg_attr] and #[cfg] processing directly into the parser.

@steveklabnik steveklabnik added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label May 13, 2015
@lilyball
Copy link
Contributor Author

@klutzy It looks like the problem is it collects some crate information prior to running the first strip_unconfigured_items pass. Is there some reason it doesn't simply run that pass as the very first thing?

lilyball added a commit to lilyball/rust that referenced this issue May 14, 2015
Stripping unconfigured items prior to collecting crate metadata means we
can say things like `#![cfg_attr(foo, crate_type="lib")]`.

Fixes rust-lang#25347.
lilyball added a commit to lilyball/rust that referenced this issue May 14, 2015
Stripping unconfigured items prior to collecting crate metadata means we
can say things like `#![cfg_attr(foo, crate_type="lib")]`.

Fixes rust-lang#25347.
bors added a commit that referenced this issue May 15, 2015
…hton

Stripping unconfigured items prior to collecting crate metadata means we
can say things like `#![cfg_attr(foo, crate_type="lib")]`.

Fixes #25347.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants