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

Be more specific when flagging imports as redundant due to the extern prelude #122954

Merged
merged 1 commit into from
Apr 11, 2024

Conversation

fmease
Copy link
Member

@fmease fmease commented Mar 23, 2024

There are multiple distinct kinds of preludes. Be more specific when flagging imports as redundant due to the extern prelude.

r? Nilstrieb or compiler

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 23, 2024
@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 23, 2024
@fmease fmease changed the title Be more specific when flagging imports that are redundant due to the extern prelude Be more specific when flagging imports as redundant due to the extern prelude Mar 23, 2024
@Noratrieb
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 23, 2024

📌 Commit 1acd048 has been approved by Nilstrieb

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 23, 2024
@petrochenkov
Copy link
Contributor

There are multiple distinct kinds of preludes.

And this diagnostic may fire for all of them, not just extern prelude.
(Except libstd prelude, which has non-dummy spans.)

@fmease
Copy link
Member Author

fmease commented Mar 23, 2024

I see
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 23, 2024
@fmease
Copy link
Member Author

fmease commented Mar 23, 2024

Std Library Prelude

Span is hopefully never dummy, so we can assume it's unreachable in this code path.

Language Prelude

use std::primitive::u8; //~ WARN the item `u8` is imported redundantly
const _: u8 = 0;

You are right, this one is reachable and this PR currently misclassifies the prelude.

macro_use Prelude

Seems like the code path that emits redundant import won't get reached in this case since unused `#[macro_use]` import takes precedence:

// ::: dep.rs :::   rustc dep.rs --crate-type=lib
#[macro_export] macro_rules! m { () => {} }

// ::: usr.rs :::   rustc usr.rs --crate-tyype=lib -L.
#[macro_use] extern crate dep; //~ WARN unused `#[macro_use]` import
use dep::m;
m!();

Ergo it might not be reachable in this code path.

Tool Prelude

Doesn't apply to this code path.

@fmease
Copy link
Member Author

fmease commented Mar 23, 2024

All that to say, I will look into emitting extern prelude or language prelude depending on the prelude kind. I'm in favor making it more explicit since I presume that most users will probably think of the libstd prelude when they see the word prelude.

@fmease
Copy link
Member Author

fmease commented Mar 23, 2024

Ah, and if we do use the #[macro_use] import and import sth. redundantly due to the #[macro_use], we currently don't emit any warnings which could be considered a bug I guess 🤔

// ::: dep.rs :::   rustc dep.rs --crate-type=lib
#[macro_export]
macro_rules! m { () => {} }
#[macro_export]
macro_rules! g { () => {} }

// ::: usr.rs :::   rustc usr.rs --crate-type=lib -L.
#[macro_use] extern crate dep;
use dep::m; // <-- this is actually redundant but we don't emit any warnings
m!();
g!();

@petrochenkov petrochenkov self-assigned this Mar 24, 2024
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 24, 2024
@petrochenkov
Copy link
Contributor

Ah, and if we do use the #[macro_use] import and import sth. redundantly due to the #[macro_use], we currently don't emit any warnings which could be considered a bug I guess 🤔

The lint is just conservative with macros in general.
We don't set Used::Scope for early_resolve_ident_in_lexical_scope in finalize_macro_resolutions, so m!(); isn't classified as a scope-relative use.
Maybe we should do that.

In any case, the span won't be dummy in this scenario.
It will point to #[macro_use] or to the imported macro declaration itself.

@petrochenkov
Copy link
Contributor

Tool Prelude

Doesn't apply to this code path.

Yeah, seems right, it produces an error in that case https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=8718593e4c9d888ae72a8ac08164b38d

@petrochenkov
Copy link
Contributor

So we have 3 prelude cases with dummy spans:

  • Extern prelude
  • Built-in types
  • Built-in attributes
use allow; // warning: the item `allow` is imported redundantly

#[allow(unused)]
fn main() {}

From those, extern prelude should be most common.
Maybe be it's fine to just say "extern prelude" in the diagnostics.
I don't want add any extra logic for tracking prelude kinds just for this message.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 25, 2024
@petrochenkov
Copy link
Contributor

@fmease
Could you add test cases for non-extern preludes (including the erroring ones, like tool modules)?

@fmease fmease force-pushed the defined-by-extern-prelude branch from 1acd048 to bfffbd0 Compare April 8, 2024 15:30
@fmease
Copy link
Member Author

fmease commented Apr 8, 2024

Sorry for the delay.

We don't set Used::Scope for early_resolve_ident_in_lexical_scope in finalize_macro_resolutions, so m!(); isn't classified as a scope-relative use.
Maybe we should do that. [emphasis mine]

I've added that to my internal rustc issue tracker to be revisited by me at some point.

Could you add test cases for non-extern preludes (including the erroring ones, like tool modules)?

For the #[macro_use] prelude case, we already have tests/ui/imports/unused-macro-use.rs and tests/ui/rust-2018/macro-use-warned-against.rs. Do they suffice? They obviously don't match the snippet I posted above 100%.

For the tool prelude case, we already have tests/ui/rust-2018/uniform-paths/cross-crate.rs and tests/ui/rust-2018/uniform-paths/prelude-fail-2.rs. They should sufficiently cover this case, I think.


I've noticed that we currently have tests for redundant imports in two places, namely tests/ui/lint/use-redundant/ and tests/ui/imports/ (as tests/ui/imports/redundant-import-*.rs). I can move the newly added tests as well as the preexisting ones into tests/ui/lint/use-redundant/ if you'd like me to.

@fmease fmease added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 8, 2024
@fmease fmease force-pushed the defined-by-extern-prelude branch from bfffbd0 to 8a24ddf Compare April 8, 2024 15:34
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 10, 2024

📌 Commit 8a24ddf has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2024
fmease added a commit to fmease/rust that referenced this pull request Apr 10, 2024
…r=petrochenkov

Be more specific when flagging imports as redundant due to the extern prelude

There are multiple distinct kinds of [preludes](https://doc.rust-lang.org/reference/names/preludes.html). Be more specific when flagging imports as redundant due to the [extern prelude](https://doc.rust-lang.org/reference/names/preludes.html#extern-prelude).

r? Nilstrieb or compiler
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123732 (Factor some common `io::Error` constants)

r? `@ghost`
`@rustbot` modify labels: rollup
fmease added a commit to fmease/rust that referenced this pull request Apr 10, 2024
…r=petrochenkov

Be more specific when flagging imports as redundant due to the extern prelude

There are multiple distinct kinds of [preludes](https://doc.rust-lang.org/reference/names/preludes.html). Be more specific when flagging imports as redundant due to the [extern prelude](https://doc.rust-lang.org/reference/names/preludes.html#extern-prelude).

r? Nilstrieb or compiler
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123661 (Stabilize `cstr_count_bytes`)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123704 (Tweak value suggestions in `borrowck` and `hir_analysis`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123661 (Stabilize `cstr_count_bytes`)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123756 (clean up docs for `File::sync_*`)
 - rust-lang#123761 (Use `suggest_impl_trait` in return type suggestion on type error)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123661 (Stabilize `cstr_count_bytes`)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123756 (clean up docs for `File::sync_*`)
 - rust-lang#123761 (Use `suggest_impl_trait` in return type suggestion on type error)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ccab2b1 into rust-lang:master Apr 11, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
Rollup merge of rust-lang#122954 - fmease:defined-by-extern-prelude, r=petrochenkov

Be more specific when flagging imports as redundant due to the extern prelude

There are multiple distinct kinds of [preludes](https://doc.rust-lang.org/reference/names/preludes.html). Be more specific when flagging imports as redundant due to the [extern prelude](https://doc.rust-lang.org/reference/names/preludes.html#extern-prelude).

r? Nilstrieb or compiler
@fmease fmease deleted the defined-by-extern-prelude branch April 11, 2024 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants