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

Redundant import leaks prelude import #121312

Open
eopb opened this issue Feb 19, 2024 · 4 comments
Open

Redundant import leaks prelude import #121312

eopb opened this issue Feb 19, 2024 · 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

@eopb
Copy link
Contributor

eopb commented Feb 19, 2024

Code

use std::convert::TryFrom;

pub trait Foo<T>: TryFrom<T> {}

Current output

Compiling playground v0.0.1 (/playground)
warning: the item `TryFrom` is imported redundantly
   --> src/lib.rs:1:5
    |
1   | use std::convert::TryFrom;
    |     ^^^^^^^^^^^^^^^^^^^^^
    |
   ::: /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/prelude/mod.rs:129:13
    |
129 |     pub use core::prelude::rust_2021::*;
    |             ------------------------ the item `TryFrom` is already defined here
    |
    = note: `#[warn(unused_imports)]` on by default

Desired output

Compiling playground v0.0.1 (/playground)
warning: the item `TryFrom` is imported redundantly
   --> src/lib.rs:1:5
    |
1   | use std::convert::TryFrom;
    |     ^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `TryFrom` is imported in the 2021 edition prelude
    = note: `#[warn(unused_imports)]` on by default

Rationale and extra context

The pub use core::prelude::rust_2021::*; import is a compiler detail. The user did not write that line so it's confusing to display it to them.

Other cases

No response

Rust Version

rustc 1.78.0-nightly (2bf78d12d 2024-02-18)
binary: rustc
commit-hash: 2bf78d12d33ae02d10010309a0d85dd04e7cff72
commit-date: 2024-02-18
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=cafa3c0983d012f7430097f0fbd32e4f

@eopb eopb 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 Feb 19, 2024
@petrochenkov
Copy link
Contributor

This diagnostic is not specific to the redundant import lint, it's shown every time we refer to a declaration site of any prelude name.

Those declarations are not compiler details, they are public APIs - https://doc.rust-lang.org/stable/std/prelude/rust_2021/index.html - so such diagnostics are at least acceptable.

@Kixunil
Copy link
Contributor

Kixunil commented Feb 20, 2024

@petrochenkov that's fine but as opposed to the OP I have a different output (with -D warnings):

error: the item `Box` is imported redundantly
 --> io/src/error.rs:2:5
  |
2 | use alloc::boxed::Box;
  |     ^^^^^^^^^^^^^^^^^
 --> /rustc/2bf78d12d33ae02d10010309a0d85dd04e7cff72/library/std/src/prelude/mod.rs:125:13
  |
  = note: the item `Box` is already defined here
  |
  = note: `-D unused-imports` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(unused_imports)]`

So I was confused why our CI started breaking out of blue and since I didn't see where the other location is it looked like a bug to me.

@petrochenkov
Copy link
Contributor

@Kixunil
I cannot reproduce the case with "Box is already defined here" pointing to nowhere.
It looks like it's not specific to this lint, and caused by standard library source code not being available for some reason.

@Kixunil
Copy link
Contributor

Kixunil commented Mar 8, 2024

@petrochenkov ah, OK, IIRC this was in CI using dtolnay's GH action. I guess it doesn't install std source for efficiency.

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