Skip to content

Commit 93efd53

Browse files
committed
Auto merge of #54650 - eddyb:no-extern's-land, r=alexcrichton
Don't lint non-extern-prelude extern crate's in Rust 2018. Fixes #54381 by silencing the lint telling users to remove `extern crate` when `use` doesn't work. r? @alexcrichton cc @petrochenkov @nikomatsakis @Centril
2 parents fc403ad + 81ca8eb commit 93efd53

File tree

10 files changed

+59
-110
lines changed

10 files changed

+59
-110
lines changed

Diff for: src/librustc/session/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use syntax::parse;
3838
use syntax::parse::ParseSess;
3939
use syntax::{ast, source_map};
4040
use syntax::feature_gate::AttributeType;
41-
use syntax_pos::{MultiSpan, Span};
41+
use syntax_pos::{MultiSpan, Span, symbol::Symbol};
4242
use util::profiling::SelfProfiler;
4343

4444
use rustc_target::spec::PanicStrategy;
@@ -168,6 +168,10 @@ pub struct Session {
168168

169169
/// Cap lint level specified by a driver specifically.
170170
pub driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
171+
172+
/// All the crate names specified with `--extern`, and the builtin ones.
173+
/// Starting with the Rust 2018 edition, absolute paths resolve in this set.
174+
pub extern_prelude: FxHashSet<Symbol>,
171175
}
172176

173177
pub struct PerfStats {
@@ -1126,6 +1130,18 @@ pub fn build_session_(
11261130
CguReuseTracker::new_disabled()
11271131
};
11281132

1133+
1134+
let mut extern_prelude: FxHashSet<Symbol> =
1135+
sopts.externs.iter().map(|kv| Symbol::intern(kv.0)).collect();
1136+
1137+
// HACK(eddyb) this ignores the `no_{core,std}` attributes.
1138+
// FIXME(eddyb) warn (somewhere) if core/std is used with `no_{core,std}`.
1139+
// if !attr::contains_name(&krate.attrs, "no_core") {
1140+
// if !attr::contains_name(&krate.attrs, "no_std") {
1141+
extern_prelude.insert(Symbol::intern("core"));
1142+
extern_prelude.insert(Symbol::intern("std"));
1143+
extern_prelude.insert(Symbol::intern("meta"));
1144+
11291145
let sess = Session {
11301146
target: target_cfg,
11311147
host,
@@ -1201,6 +1217,7 @@ pub fn build_session_(
12011217
has_global_allocator: Once::new(),
12021218
has_panic_handler: Once::new(),
12031219
driver_lint_caps: FxHashMap(),
1220+
extern_prelude,
12041221
};
12051222

12061223
validate_commandline_args_with_session_available(&sess);

Diff for: src/librustc_resolve/lib.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,6 @@ pub struct Resolver<'a, 'b: 'a> {
13511351
graph_root: Module<'a>,
13521352

13531353
prelude: Option<Module<'a>>,
1354-
extern_prelude: FxHashSet<Name>,
13551354

13561355
/// n.b. This is used only for better diagnostics, not name resolution itself.
13571356
has_self: FxHashSet<DefId>,
@@ -1667,17 +1666,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
16671666
DefCollector::new(&mut definitions, Mark::root())
16681667
.collect_root(crate_name, session.local_crate_disambiguator());
16691668

1670-
let mut extern_prelude: FxHashSet<Name> =
1671-
session.opts.externs.iter().map(|kv| Symbol::intern(kv.0)).collect();
1672-
1673-
// HACK(eddyb) this ignore the `no_{core,std}` attributes.
1674-
// FIXME(eddyb) warn (elsewhere) if core/std is used with `no_{core,std}`.
1675-
// if !attr::contains_name(&krate.attrs, "no_core") {
1676-
// if !attr::contains_name(&krate.attrs, "no_std") {
1677-
extern_prelude.insert(Symbol::intern("core"));
1678-
extern_prelude.insert(Symbol::intern("std"));
1679-
extern_prelude.insert(Symbol::intern("meta"));
1680-
16811669
let mut invocations = FxHashMap();
16821670
invocations.insert(Mark::root(),
16831671
arenas.alloc_invocation_data(InvocationData::root(graph_root)));
@@ -1696,7 +1684,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
16961684
// AST.
16971685
graph_root,
16981686
prelude: None,
1699-
extern_prelude,
17001687

17011688
has_self: FxHashSet(),
17021689
field_names: FxHashMap(),
@@ -1968,7 +1955,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
19681955

19691956
if !module.no_implicit_prelude {
19701957
// `record_used` means that we don't try to load crates during speculative resolution
1971-
if record_used && ns == TypeNS && self.extern_prelude.contains(&ident.name) {
1958+
if record_used && ns == TypeNS && self.session.extern_prelude.contains(&ident.name) {
19721959
let crate_id = self.crate_loader.process_path_extern(ident.name, ident.span);
19731960
let crate_root = self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
19741961
self.populate_module_if_necessary(&crate_root);
@@ -3975,7 +3962,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
39753962
} else {
39763963
// Items from the prelude
39773964
if !module.no_implicit_prelude {
3978-
names.extend(self.extern_prelude.iter().cloned());
3965+
names.extend(self.session.extern_prelude.iter().cloned());
39793966
if let Some(prelude) = self.prelude {
39803967
add_module_candidates(prelude, &mut names);
39813968
}
@@ -4421,8 +4408,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
44214408
);
44224409

44234410
if self.session.rust_2018() {
4424-
let extern_prelude_names = self.extern_prelude.clone();
4425-
for &name in extern_prelude_names.iter() {
4411+
for &name in &self.session.extern_prelude {
44264412
let ident = Ident::with_empty_ctxt(name);
44274413
match self.crate_loader.maybe_process_path_extern(name, ident.span) {
44284414
Some(crate_id) => {

Diff for: src/librustc_resolve/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
682682
result
683683
}
684684
WhereToResolve::ExternPrelude => {
685-
if use_prelude && self.extern_prelude.contains(&ident.name) {
685+
if use_prelude && self.session.extern_prelude.contains(&ident.name) {
686686
let crate_id =
687687
self.crate_loader.process_path_extern(ident.name, ident.span);
688688
let crate_root =

Diff for: src/librustc_resolve/resolve_imports.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
199199
if !(
200200
ns == TypeNS &&
201201
!ident.is_path_segment_keyword() &&
202-
self.extern_prelude.contains(&ident.name)
202+
self.session.extern_prelude.contains(&ident.name)
203203
) {
204204
// ... unless the crate name is not in the `extern_prelude`.
205205
return binding;
@@ -218,7 +218,7 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
218218
} else if
219219
ns == TypeNS &&
220220
!ident.is_path_segment_keyword() &&
221-
self.extern_prelude.contains(&ident.name)
221+
self.session.extern_prelude.contains(&ident.name)
222222
{
223223
let crate_id =
224224
self.crate_loader.process_path_extern(ident.name, ident.span);
@@ -735,7 +735,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
735735
let uniform_paths_feature = self.session.features_untracked().uniform_paths;
736736
for ((span, _, ns), results) in uniform_paths_canaries {
737737
let name = results.name;
738-
let external_crate = if ns == TypeNS && self.extern_prelude.contains(&name) {
738+
let external_crate = if ns == TypeNS && self.session.extern_prelude.contains(&name) {
739739
let crate_id =
740740
self.crate_loader.process_path_extern(name, span);
741741
Some(Def::Mod(DefId { krate: crate_id, index: CRATE_DEF_INDEX }))

Diff for: src/librustc_typeck/check_unused.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
130130
});
131131

132132
for extern_crate in &crates_to_lint {
133-
assert!(extern_crate.def_id.is_local());
133+
let id = tcx.hir.as_local_node_id(extern_crate.def_id).unwrap();
134+
let item = tcx.hir.expect_item(id);
134135

135136
// If the crate is fully unused, we suggest removing it altogether.
136137
// We do this in any edition.
137138
if extern_crate.warn_if_unused {
138139
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
139-
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
140-
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
141-
let id = tcx.hir.hir_to_node_id(hir_id);
142140
let msg = "unused extern crate";
143141
tcx.struct_span_lint_node(lint, id, span, msg)
144142
.span_suggestion_short_with_applicability(
@@ -157,6 +155,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
157155
continue;
158156
}
159157

158+
// If the extern crate isn't in the extern prelude,
159+
// there is no way it can be written as an `use`.
160+
let orig_name = extern_crate.orig_name.unwrap_or(item.name);
161+
if !tcx.sess.extern_prelude.contains(&orig_name) {
162+
continue;
163+
}
164+
160165
// If the extern crate has any attributes, they may have funky
161166
// semantics we can't faithfully represent using `use` (most
162167
// notably `#[macro_use]`). Ignore it.
@@ -165,9 +170,6 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
165170
}
166171

167172
// Otherwise, we can convert it into a `use` of some kind.
168-
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
169-
let id = tcx.hir.hir_to_node_id(hir_id);
170-
let item = tcx.hir.expect_item(id);
171173
let msg = "`extern crate` is not idiomatic in the new edition";
172174
let help = format!(
173175
"convert it to a `{}`",

Diff for: src/test/ui-fulldeps/unnecessary-extern-crate.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,23 @@ extern crate alloc as x;
2020
//~^ ERROR unused extern crate
2121
//~| HELP remove
2222

23+
extern crate proc_macro;
24+
2325
#[macro_use]
2426
extern crate test;
2527

2628
pub extern crate test as y;
27-
//~^ ERROR `extern crate` is not idiomatic in the new edition
28-
//~| HELP convert it to a `pub use`
2929

3030
pub extern crate libc;
31-
//~^ ERROR `extern crate` is not idiomatic in the new edition
32-
//~| HELP convert it to a `pub use`
3331

3432
pub(crate) extern crate libc as a;
35-
//~^ ERROR `extern crate` is not idiomatic in the new edition
36-
//~| HELP convert it to a `pub(crate) use`
3733

3834
crate extern crate libc as b;
39-
//~^ ERROR `extern crate` is not idiomatic in the new edition
40-
//~| HELP convert it to a `crate use`
4135

4236
mod foo {
4337
pub(in crate::foo) extern crate libc as c;
44-
//~^ ERROR `extern crate` is not idiomatic in the new edition
45-
//~| HELP convert it to a `pub(in crate::foo) use`
4638

4739
pub(super) extern crate libc as d;
48-
//~^ ERROR `extern crate` is not idiomatic in the new edition
49-
//~| HELP convert it to a `pub(super) use`
5040

5141
extern crate alloc;
5242
//~^ ERROR unused extern crate
@@ -57,12 +47,8 @@ mod foo {
5747
//~| HELP remove
5848

5949
pub extern crate test;
60-
//~^ ERROR `extern crate` is not idiomatic in the new edition
61-
//~| HELP convert it
6250

6351
pub extern crate test as y;
64-
//~^ ERROR `extern crate` is not idiomatic in the new edition
65-
//~| HELP convert it
6652

6753
mod bar {
6854
extern crate alloc;
@@ -74,8 +60,6 @@ mod foo {
7460
//~| HELP remove
7561

7662
pub(in crate::foo::bar) extern crate libc as e;
77-
//~^ ERROR `extern crate` is not idiomatic in the new edition
78-
//~| HELP convert it to a `pub(in crate::foo::bar) use`
7963

8064
fn dummy() {
8165
unsafe {
@@ -96,4 +80,6 @@ mod foo {
9680
fn main() {
9781
unsafe { a::getpid(); }
9882
unsafe { b::getpid(); }
83+
84+
proc_macro::TokenStream::new();
9985
}

Diff for: src/test/ui-fulldeps/unnecessary-extern-crate.stderr

+5-59
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,29 @@ error: unused extern crate
1616
LL | extern crate alloc as x;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
1818

19-
error: `extern crate` is not idiomatic in the new edition
20-
--> $DIR/unnecessary-extern-crate.rs:26:1
21-
|
22-
LL | pub extern crate test as y;
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
24-
25-
error: `extern crate` is not idiomatic in the new edition
26-
--> $DIR/unnecessary-extern-crate.rs:30:1
27-
|
28-
LL | pub extern crate libc;
29-
| ^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
30-
31-
error: `extern crate` is not idiomatic in the new edition
32-
--> $DIR/unnecessary-extern-crate.rs:34:1
33-
|
34-
LL | pub(crate) extern crate libc as a;
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(crate) use`
36-
37-
error: `extern crate` is not idiomatic in the new edition
38-
--> $DIR/unnecessary-extern-crate.rs:38:1
39-
|
40-
LL | crate extern crate libc as b;
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `crate use`
42-
43-
error: `extern crate` is not idiomatic in the new edition
44-
--> $DIR/unnecessary-extern-crate.rs:43:5
45-
|
46-
LL | pub(in crate::foo) extern crate libc as c;
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(in crate::foo) use`
48-
49-
error: `extern crate` is not idiomatic in the new edition
50-
--> $DIR/unnecessary-extern-crate.rs:47:5
51-
|
52-
LL | pub(super) extern crate libc as d;
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(super) use`
54-
5519
error: unused extern crate
56-
--> $DIR/unnecessary-extern-crate.rs:51:5
20+
--> $DIR/unnecessary-extern-crate.rs:41:5
5721
|
5822
LL | extern crate alloc;
5923
| ^^^^^^^^^^^^^^^^^^^ help: remove it
6024

6125
error: unused extern crate
62-
--> $DIR/unnecessary-extern-crate.rs:55:5
26+
--> $DIR/unnecessary-extern-crate.rs:45:5
6327
|
6428
LL | extern crate alloc as x;
6529
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
6630

67-
error: `extern crate` is not idiomatic in the new edition
68-
--> $DIR/unnecessary-extern-crate.rs:59:5
69-
|
70-
LL | pub extern crate test;
71-
| ^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
72-
73-
error: `extern crate` is not idiomatic in the new edition
74-
--> $DIR/unnecessary-extern-crate.rs:63:5
75-
|
76-
LL | pub extern crate test as y;
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
78-
7931
error: unused extern crate
80-
--> $DIR/unnecessary-extern-crate.rs:68:9
32+
--> $DIR/unnecessary-extern-crate.rs:54:9
8133
|
8234
LL | extern crate alloc;
8335
| ^^^^^^^^^^^^^^^^^^^ help: remove it
8436

8537
error: unused extern crate
86-
--> $DIR/unnecessary-extern-crate.rs:72:9
38+
--> $DIR/unnecessary-extern-crate.rs:58:9
8739
|
8840
LL | extern crate alloc as x;
8941
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
9042

91-
error: `extern crate` is not idiomatic in the new edition
92-
--> $DIR/unnecessary-extern-crate.rs:76:9
93-
|
94-
LL | pub(in crate::foo::bar) extern crate libc as e;
95-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(in crate::foo::bar) use`
96-
97-
error: aborting due to 15 previous errors
43+
error: aborting due to 6 previous errors
9844

Diff for: src/test/ui/rust-2018/remove-extern-crate.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// aux-build:remove-extern-crate.rs
1515
// compile-flags:--extern remove_extern_crate
1616

17+
#![feature(alloc)]
1718
#![warn(rust_2018_idioms)]
1819

1920

@@ -22,11 +23,16 @@ use remove_extern_crate;
2223
#[macro_use]
2324
extern crate remove_extern_crate as something_else;
2425

26+
// Shouldn't suggest changing to `use`, as the `alloc`
27+
// crate is not in the extern prelude - see #54381.
28+
extern crate alloc;
29+
2530
fn main() {
2631
another_name::mem::drop(3);
2732
another::foo();
2833
remove_extern_crate::foo!();
2934
bar!();
35+
alloc::vec![5];
3036
}
3137

3238
mod another {

Diff for: src/test/ui/rust-2018/remove-extern-crate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// aux-build:remove-extern-crate.rs
1515
// compile-flags:--extern remove_extern_crate
1616

17+
#![feature(alloc)]
1718
#![warn(rust_2018_idioms)]
1819

1920
extern crate core;
@@ -22,11 +23,16 @@ use remove_extern_crate;
2223
#[macro_use]
2324
extern crate remove_extern_crate as something_else;
2425

26+
// Shouldn't suggest changing to `use`, as the `alloc`
27+
// crate is not in the extern prelude - see #54381.
28+
extern crate alloc;
29+
2530
fn main() {
2631
another_name::mem::drop(3);
2732
another::foo();
2833
remove_extern_crate::foo!();
2934
bar!();
35+
alloc::vec![5];
3036
}
3137

3238
mod another {

0 commit comments

Comments
 (0)