Skip to content

Super-weird compiler bug, and seemingly paradox behaviour when using non-absolute imports in 1.30 #55897

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
axos88 opened this issue Nov 12, 2018 · 5 comments
Assignees
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@axos88
Copy link

axos88 commented Nov 12, 2018

Repro repo: https://github.com/axos88/out_dir_bug/blob/master/src/foo.rs#L1

Apparently that line causes env!("whatever") to resolve to "false", so the include! fails, because it cannot find the file.

I'm a bit baffled on why this doesn't create a more meaningful compile error. Seems like it gets confused due to there being a module named env and a macro named env! in std?

By completing that line to std::env, it will compile fine.

@petrochenkov petrochenkov self-assigned this Nov 12, 2018
@petrochenkov petrochenkov added A-resolve Area: Name/path resolution done by `rustc_resolve` specifically A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Nov 12, 2018
@axos88
Copy link
Author

axos88 commented Nov 12, 2018

Actually the case is much more complex:

use env;

include!(concat!(env!("OUT_DIR"), "/data.rs"));


error: couldn't read "src/false/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(env!("OUT_DIR"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use env;

include!(concat!(std::env!("OUT_DIR"), "/data.rs"));

error: couldn't read "src/false/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(std::env!("OUT_DIR"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use std::env;

include!(concat!(env!("OUT_DIR"), "/data.rs"));

error[E0432]: unresolved import `prelude`
 --> src/main.rs:6:5
  |
6 | use prelude::*;
  |     ^^^^^^^ Did you mean `std::prelude`?

Seems like this one is resolving the env! correctly

use std::env;

include!(concat!(std::env!("OUT_DIR"), "/data.rs"));

error: couldn't read "src/false/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(std::env!("OUT_DIR"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

** NOW THE SAME WITH NON_EXISTING_ENV_VAR **

use env;

include!(concat!(env!("INVALID"), "/data.rs"));

error: couldn't read "src/false/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(std::env!("OUT_DIR"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use env;

include!(concat!(std::env!("INVALID"), "/data.rs"));

error: couldn't read "src/false/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(std::env!("INVALID"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use std::env;

include!(concat!(env!("INVALID"), "/data.rs"));

error: environment variable `INVALID` not defined
 --> src/foo.rs:3:18
  |
3 | include!(concat!(env!("INVALID"), "/data.rs"));
  |                  ^^^^^^^^^^^^^^^
error: couldn't read "src/0/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(env!("INVALID"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This one seems to try to resolve the invalid env var, and reports an error that it doesn't find it. BUT! It also tries to include src/0/data.rs?!!!

use std::env;

include!(concat!(std::env!("INVALID"), "/data.rs"));


error: couldn't read "src/false/data.rs": No such file or directory (os error 2)
 --> src/foo.rs:3:1
  |
3 | include!(concat!(std::env!("INVALID"), "/data.rs"));
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*** If the src/false/data.rs is created, they give a different result! ***

use std::env;

include!(concat!(std::env!("INVALID"), "/data.rs"));

error[E0432]: unresolved import `prelude`
 --> src/main.rs:6:5
  |
6 | use prelude::*;
  |     ^^^^^^^ Did you mean `std::prelude`?
error[E0433]: failed to resolve. Could not find `env` in `std`
 --> src/foo.rs:3:23
  |
3 | include!(concat!(std::env!("INVALID"), "/data.rs"));
  |                       ^^^ Could not find `env` in `std`

error: aborting due to 2 previous errors

Seems like it did determine the file exists, but then also said env doesn't exist in std?!
Without the file present it sais that it cannot find the file (and the only way to determine that if it has calculated the path), but if the path it is looking for exists, it reports that it cannot determine the path, because one the macros needed to determine it cannot be found?! This seems like a causality paradox!

*** If the src/0/data.rs is created, it doesn't complain about the prelude anymore,
just that the env file is missing, basically the same paradox as before ***

use std::env;

include!(concat!(env!("INVALID"), "/data.rs"));

error: environment variable `INVALID` not defined
 --> src/foo.rs:3:18
  |
3 | include!(concat!(env!("INVALID"), "/data.rs"));
  |                  ^^^^^^^^^^^^^^^

@axos88
Copy link
Author

axos88 commented Nov 12, 2018

@petrochenkov

@axos88 axos88 changed the title Super-weird compiler bug when using non-absolute imports in 1.30 Super-weird compiler bug, and seemingly paradox behaviour when using non-absolute imports in 1.30 Nov 12, 2018
@axos88
Copy link
Author

axos88 commented Nov 12, 2018

Removing the use prelude:* from main.rs and use env from foo.rs fixes the compilation errors, but this should not be happening;

@Marwes
Copy link
Contributor

Marwes commented Dec 11, 2018

A user of LALRPOP ran into a similiar issue when the build script was in the wrong directory (and so no OUT_DIR variable was set) lalrpop/lalrpop#427. The error message is indeed really weird in this instance.

For OUT_DIR specifically it might also be useful to add a hint that a build script is needed.

@petrochenkov
Copy link
Contributor

Fixed in #57205

bors added a commit that referenced this issue Dec 30, 2018
Improve error recovery for some built-in macros

Fixes #55897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants