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 with path attribute #25544

Closed
klutzy opened this issue May 17, 2015 · 6 comments
Closed

#[cfg_attr] doesn't work with path attribute #25544

klutzy opened this issue May 17, 2015 · 6 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`)

Comments

@klutzy
Copy link
Contributor

klutzy commented May 17, 2015

$ rustc -Vv
rustc 1.1.0-nightly (e5394240a 2015-05-14) (built 2015-05-14)
binary: rustc
commit-hash: e5394240a295650b567aa406b4a0e1e3a6749a5f
commit-date: 2015-05-14
build-date: 2015-05-14
host: x86_64-unknown-linux-gnu
release: 1.1.0-nightly

$ echo 'fn i_am_b() {}' > b.rs
$ echo '#[cfg_attr(foo, path="b.rs")] mod c; fn main() {}' > a.rs
$ rustc a.rs --cfg foo
a.rs:2:5: 2:6 error: file not found for module `c`
a.rs:2 mod c;
           ^
a.rs:2:5: 2:6 help: name the file either c.rs or c/mod.rs inside the directory ""

$ echo 'fn i_am_c() {}' > c.rs
$ rustc a.rs --cfg foo
c.rs:1:1: 1:15 warning: function is never used: `i_am_c`, #[warn(dead_code)] on by default
c.rs:1 fn i_am_c() {}
       ^~~~~~~~~~~~~~
a.rs:1:17: 1:29 warning: unused attribute, #[warn(unused_attributes)] on by default
a.rs:1 #[cfg_attr(foo, path="b.rs")]
                       ^~~~~~~~~~~~

This occurs because #[cfg_attr] is processed at phase 2 (configure and expand) but #[path] is done at phase 1 (parse).

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

brson commented May 27, 2015

Yeah, #[path] is kinda special since it gets used so early. I don't know offhand of any other attributes like that.

robertknight pushed a commit to robertknight/x11-rs that referenced this issue Jun 21, 2015
x11/src/lib.rs and x11-dl/src/lib.rs are most duplicates
of each other. It would be preferable to combine them into
a single file. This could be done by building
src/lib.rs with different config attributes (eg. cfg=dynamic) but
at present it doesn't look like it is possible
to have different exports depending on #[cfg(attribute)]

See rust-lang/rust#25544
@crazymykl
Copy link

A similar issue is found with conditional including of modules.

Consider the program:

#[cfg(test)]
mod fake;

fn main() {
  println!("testing");
}

This should only fail to compile in test mode, instead we see:

<anon>:2:5: 2:9 error: file not found for module `fake`
<anon>:2 mod fake;
             ^~~~
<anon>:2:5: 2:9 help: name the file either fake.rs or fake/mod.rs inside the directory ""
playpen: application terminated with error code 101

Interactive repro here.

@eddyb
Copy link
Member

eddyb commented Mar 19, 2016

We should totally move cfg expansion to parse-time, so that #[path] gets seen by the compiler, instead of having two hacky passes.

Also, we might want to avoid loading and parsing a module if we see #[cfg(foo)] and --cfg foo is not enabled. This would allow toggling the use of experimental syntax based on cfg flags which is easier than what we have to go through today.

@eddyb eddyb self-assigned this Mar 19, 2016
@eddyb eddyb removed their assignment Apr 21, 2016
@eddyb
Copy link
Member

eddyb commented Apr 21, 2016

I didn't get to this before the beta.
@jseyfried Did you say you wanted to work on something like this?

@jseyfried
Copy link
Contributor

jseyfried commented Apr 22, 2016

@eddyb yeah, I was plannning on doing cfg processing after parsing during expansion, but it looks like this will require at least some cfg processing during parsing.

I like the idea of not loading or parsing an unconfigured module, but that will probably need more discussion.

cf #32846 and #32006
cc @nrc

bors added a commit that referenced this issue Jul 6, 2016
Support `cfg_attr` on `path` attributes

Fixes #25544.
This is technically a [breaking-change]. For example, the following would break:
```rust
mod foo; // Suppose `foo.rs` existed in the appropriate location
```
@whitequark
Copy link
Member

Heads up: #35584

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

No branches or pull requests

7 participants