From 8a47e088458866b6dd8aa28acc9db1e9e22be997 Mon Sep 17 00:00:00 2001 From: David Wood Date: Thu, 25 Apr 2019 07:53:06 +0100 Subject: [PATCH 1/2] Add test for current behaviour. This commit adds a test that causes a suggestion to replace `extern crate` with `use` when doing so would cause a compliation error, as the new name of the import would not be added to the prelude if a `use` was used. --- src/test/ui/suggestions/issue-57672.rs | 14 ++++++++++++++ src/test/ui/suggestions/issue-57672.stderr | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/test/ui/suggestions/issue-57672.rs create mode 100644 src/test/ui/suggestions/issue-57672.stderr diff --git a/src/test/ui/suggestions/issue-57672.rs b/src/test/ui/suggestions/issue-57672.rs new file mode 100644 index 00000000000..c8ea6e59582 --- /dev/null +++ b/src/test/ui/suggestions/issue-57672.rs @@ -0,0 +1,14 @@ +// aux-build:foo.rs +// compile-flags:--extern foo +// edition:2018 + +#![deny(unused_extern_crates)] + +extern crate foo as foo_renamed; +//~^ ERROR `extern crate` is not idiomatic in the new edition + +pub mod m { + pub use foo_renamed::Foo; +} + +fn main() {} diff --git a/src/test/ui/suggestions/issue-57672.stderr b/src/test/ui/suggestions/issue-57672.stderr new file mode 100644 index 00000000000..c9e4c373799 --- /dev/null +++ b/src/test/ui/suggestions/issue-57672.stderr @@ -0,0 +1,14 @@ +error: `extern crate` is not idiomatic in the new edition + --> $DIR/issue-57672.rs:7:1 + | +LL | extern crate foo as foo_renamed; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` + | +note: lint level defined here + --> $DIR/issue-57672.rs:5:9 + | +LL | #![deny(unused_extern_crates)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + From 8869bc5adade466e9eca01172dfd257f14ab3392 Mon Sep 17 00:00:00 2001 From: David Wood Date: Thu, 25 Apr 2019 08:06:49 +0100 Subject: [PATCH 2/2] Do not suggest use over extern crate w/ alias. This commit stops `unused_extern_crates` lints from occuring on `extern crate` statements that alias the crate as the suggestion to change to a `use` statement would result in the aliased name no longer being added to the prelude, thereby causing compilation errors if other imports expected this to be the case. --- src/librustc_typeck/check_unused.rs | 7 ++++ src/test/ui/imports/extern-crate-used.rs | 12 ++++--- src/test/ui/imports/extern-crate-used.stderr | 34 +++---------------- .../extern-crate-idiomatic-in-2018.fixed | 7 ++-- .../extern-crate-idiomatic-in-2018.rs | 5 ++- .../extern-crate-idiomatic-in-2018.stderr | 8 +---- .../ui/rust-2018/remove-extern-crate.fixed | 6 +++- src/test/ui/rust-2018/remove-extern-crate.rs | 4 +++ .../ui/rust-2018/remove-extern-crate.stderr | 8 +---- src/test/ui/suggestions/issue-57672.rs | 2 +- src/test/ui/suggestions/issue-57672.stderr | 14 -------- 11 files changed, 41 insertions(+), 66 deletions(-) delete mode 100644 src/test/ui/suggestions/issue-57672.stderr diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index cbb6d9b29f5..3c3509f24ce 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -158,6 +158,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) { continue; } + // If the extern crate is renamed, then we cannot suggest replacing it with a use as this + // would not insert the new name into the prelude, where other imports in the crate may be + // expecting it. + if extern_crate.orig_name.is_some() { + continue; + } + // If the extern crate has any attributes, they may have funky // semantics we can't faithfully represent using `use` (most // notably `#[macro_use]`). Ignore it. diff --git a/src/test/ui/imports/extern-crate-used.rs b/src/test/ui/imports/extern-crate-used.rs index 2d91cbc00f2..26150c7d4a1 100644 --- a/src/test/ui/imports/extern-crate-used.rs +++ b/src/test/ui/imports/extern-crate-used.rs @@ -5,10 +5,14 @@ #![deny(unused_extern_crates)] -extern crate core as iso1; //~ ERROR `extern crate` is not idiomatic in the new edition -extern crate core as iso2; //~ ERROR `extern crate` is not idiomatic in the new edition -extern crate core as iso3; //~ ERROR `extern crate` is not idiomatic in the new edition -extern crate core as iso4; //~ ERROR `extern crate` is not idiomatic in the new edition +// Shouldn't suggest changing to `use`, as new name +// would no longer be added to the prelude which could cause +// compilation errors for imports that use the new name in +// other modules. See #57672. +extern crate core as iso1; +extern crate core as iso2; +extern crate core as iso3; +extern crate core as iso4; // Doesn't introduce its extern prelude entry, so it's still considered unused. extern crate core; //~ ERROR unused extern crate diff --git a/src/test/ui/imports/extern-crate-used.stderr b/src/test/ui/imports/extern-crate-used.stderr index c0783feb794..397bfa02902 100644 --- a/src/test/ui/imports/extern-crate-used.stderr +++ b/src/test/ui/imports/extern-crate-used.stderr @@ -1,8 +1,8 @@ -error: `extern crate` is not idiomatic in the new edition - --> $DIR/extern-crate-used.rs:8:1 +error: unused extern crate + --> $DIR/extern-crate-used.rs:18:1 | -LL | extern crate core as iso1; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` +LL | extern crate core; + | ^^^^^^^^^^^^^^^^^^ help: remove it | note: lint level defined here --> $DIR/extern-crate-used.rs:6:9 @@ -10,29 +10,5 @@ note: lint level defined here LL | #![deny(unused_extern_crates)] | ^^^^^^^^^^^^^^^^^^^^ -error: `extern crate` is not idiomatic in the new edition - --> $DIR/extern-crate-used.rs:9:1 - | -LL | extern crate core as iso2; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` - -error: `extern crate` is not idiomatic in the new edition - --> $DIR/extern-crate-used.rs:10:1 - | -LL | extern crate core as iso3; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` - -error: `extern crate` is not idiomatic in the new edition - --> $DIR/extern-crate-used.rs:11:1 - | -LL | extern crate core as iso4; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` - -error: unused extern crate - --> $DIR/extern-crate-used.rs:14:1 - | -LL | extern crate core; - | ^^^^^^^^^^^^^^^^^^ help: remove it - -error: aborting due to 5 previous errors +error: aborting due to previous error diff --git a/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed b/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed index ddd9abbd50e..e51ce5d1d5b 100644 --- a/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed +++ b/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed @@ -12,8 +12,11 @@ //~^ ERROR unused extern crate -use edition_lint_paths as bar; -//~^ ERROR `extern crate` is not idiomatic in the new edition +// Shouldn't suggest changing to `use`, as `bar` +// would no longer be added to the prelude which could cause +// compilation errors for imports that use `bar` in other +// modules. See #57672. +extern crate edition_lint_paths as bar; fn main() { // This is not considered to *use* the `extern crate` in Rust 2018: diff --git a/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs b/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs index 47674bc19fc..debbf085d61 100644 --- a/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs +++ b/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs @@ -12,8 +12,11 @@ extern crate edition_lint_paths; //~^ ERROR unused extern crate +// Shouldn't suggest changing to `use`, as `bar` +// would no longer be added to the prelude which could cause +// compilation errors for imports that use `bar` in other +// modules. See #57672. extern crate edition_lint_paths as bar; -//~^ ERROR `extern crate` is not idiomatic in the new edition fn main() { // This is not considered to *use* the `extern crate` in Rust 2018: diff --git a/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr b/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr index f3f91939486..13980c70a82 100644 --- a/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr +++ b/src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr @@ -11,11 +11,5 @@ LL | #![deny(rust_2018_idioms)] | ^^^^^^^^^^^^^^^^ = note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)] -error: `extern crate` is not idiomatic in the new edition - --> $DIR/extern-crate-idiomatic-in-2018.rs:15:1 - | -LL | extern crate edition_lint_paths as bar; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` - -error: aborting due to 2 previous errors +error: aborting due to previous error diff --git a/src/test/ui/rust-2018/remove-extern-crate.fixed b/src/test/ui/rust-2018/remove-extern-crate.fixed index 17449caf84f..14575d18c23 100644 --- a/src/test/ui/rust-2018/remove-extern-crate.fixed +++ b/src/test/ui/rust-2018/remove-extern-crate.fixed @@ -7,7 +7,11 @@ #![warn(rust_2018_idioms)] -use core as another_name; +// Shouldn't suggest changing to `use`, as `another_name` +// would no longer be added to the prelude which could cause +// compilation errors for imports that use `another_name` in other +// modules. See #57672. +extern crate core as another_name; use remove_extern_crate; #[macro_use] extern crate remove_extern_crate as something_else; diff --git a/src/test/ui/rust-2018/remove-extern-crate.rs b/src/test/ui/rust-2018/remove-extern-crate.rs index fb2217df000..0ee85f34e40 100644 --- a/src/test/ui/rust-2018/remove-extern-crate.rs +++ b/src/test/ui/rust-2018/remove-extern-crate.rs @@ -7,6 +7,10 @@ #![warn(rust_2018_idioms)] extern crate core; +// Shouldn't suggest changing to `use`, as `another_name` +// would no longer be added to the prelude which could cause +// compilation errors for imports that use `another_name` in other +// modules. See #57672. extern crate core as another_name; use remove_extern_crate; #[macro_use] diff --git a/src/test/ui/rust-2018/remove-extern-crate.stderr b/src/test/ui/rust-2018/remove-extern-crate.stderr index 549693201b7..5de0dfe9613 100644 --- a/src/test/ui/rust-2018/remove-extern-crate.stderr +++ b/src/test/ui/rust-2018/remove-extern-crate.stderr @@ -12,13 +12,7 @@ LL | #![warn(rust_2018_idioms)] = note: #[warn(unused_extern_crates)] implied by #[warn(rust_2018_idioms)] warning: `extern crate` is not idiomatic in the new edition - --> $DIR/remove-extern-crate.rs:10:1 - | -LL | extern crate core as another_name; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` - -warning: `extern crate` is not idiomatic in the new edition - --> $DIR/remove-extern-crate.rs:28:5 + --> $DIR/remove-extern-crate.rs:32:5 | LL | extern crate core; | ^^^^^^^^^^^^^^^^^^ help: convert it to a `use` diff --git a/src/test/ui/suggestions/issue-57672.rs b/src/test/ui/suggestions/issue-57672.rs index c8ea6e59582..1773f72fc74 100644 --- a/src/test/ui/suggestions/issue-57672.rs +++ b/src/test/ui/suggestions/issue-57672.rs @@ -1,11 +1,11 @@ // aux-build:foo.rs // compile-flags:--extern foo +// compile-pass // edition:2018 #![deny(unused_extern_crates)] extern crate foo as foo_renamed; -//~^ ERROR `extern crate` is not idiomatic in the new edition pub mod m { pub use foo_renamed::Foo; diff --git a/src/test/ui/suggestions/issue-57672.stderr b/src/test/ui/suggestions/issue-57672.stderr deleted file mode 100644 index c9e4c373799..00000000000 --- a/src/test/ui/suggestions/issue-57672.stderr +++ /dev/null @@ -1,14 +0,0 @@ -error: `extern crate` is not idiomatic in the new edition - --> $DIR/issue-57672.rs:7:1 - | -LL | extern crate foo as foo_renamed; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use` - | -note: lint level defined here - --> $DIR/issue-57672.rs:5:9 - | -LL | #![deny(unused_extern_crates)] - | ^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to previous error -