Skip to content

Confusing diagnostics when forgetting to import rustc_abi crate #140840

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

Open
RalfJung opened this issue May 9, 2025 · 4 comments
Open

Confusing diagnostics when forgetting to import rustc_abi crate #140840

RalfJung opened this issue May 9, 2025 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented May 9, 2025

Code

mod rs {
    pub use rustc_abi as abi;
}

fn foo(_x: rs::abi::Type) {}

Current output

error[E0433]: failed to resolve: could not find `abi` in `rs`
 --> src/lib.rs:5:16
  |
5 | fn foo(_x: rs::abi::Type) {}
  |                ^^^ could not find `abi` in `rs`

warning: unused import: `rustc_abi as abi`
 --> src/lib.rs:2:13
  |
2 |     pub use rustc_abi as abi;
  |             ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

Desired output

It should point at `pub use rustc_abi as abi;` and complain that `rustc_abi` does not exist.

The correct fix is to add
extern crate rustc_abi;

Rationale and extra context

Neither of the actually shown errors is helpful:

  • One error says that rs::abi does not exist -- but clearly it does exist, I wrote it right there!
  • The other error says that rs::abi is unused -- but it is used, there's even an error pointing at it!

If I change rustc_abi to foobar, the error changes to something much better:

error[E0432]: unresolved import `foobar`
 --> src/lib.rs:2:13
  |
2 |     pub use foobar as abi;
  |             ^^^^^^^^^^^^^ no external crate `foobar`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `playground` (lib) due to 1 previous error

I assume this is related to the fact that rustc_abi exists as a crate, it's just not been imported.

Other cases

Rust Version

Rust 1.86.0

Anything else?

No response

@RalfJung RalfJung added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 9, 2025
@theemathas
Copy link
Contributor

use rustc_abi;  // this compiles
use rustc_abi::*;  // this doesn't compile

Is this intentional?

@RalfJung
Copy link
Member Author

RalfJung commented May 9, 2025 via email

@danielhenrymantilla
Copy link
Contributor

danielhenrymantilla commented May 9, 2025

FWIW, the diagnostic becomes pristine the moment one uses unambiguous pathing:

mod rs {
    pub use ::rustc_abi as abi;
}

fn foo(_x: rs::abi::Type) {}

yielding:

error[E0432]: unresolved import `rustc_abi`
 --> src/lib.rs:2:13
  |
2 |     pub use ::rustc_abi as abi;
  |             ^^^^^^^^^^^^^^^^^^ no external crate `rustc_abi`

So it looks like some rustc_abi item is available in the prelude; not just the crate.

  • rustc_abi! fails;

  • const _: () = rustc_abi; fails;

  • const _: rustc_abi = (); fails, but with a nice diagnostic!

    error[E0573]: expected type, found built-in attribute `rustc_abi`
     --> src/lib.rs:1:10
      |
    1 | const _: rustc_abi = ();
      |          ^^^^^^^^^ not a type

So we got our answer:

Note

#[rustc_abi] is in scope of the implicit/magic prelude (or the macro attribute fallback namespace), hence the bad diagnostics

@RalfJung
Copy link
Member Author

RalfJung commented May 9, 2025

Oh d'oh, so my pub use rustc_abi as abi; imported an attribute? Wow.^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

3 participants