Skip to content

Commit 37ba107

Browse files
committed
Auto merge of #54671 - petrochenkov:extpre2015, r=nikomatsakis
resolve: Scale back hard-coded extern prelude additions on 2015 edition #54404 stabilized `feature(extern_prelude)` on 2015 edition, including the hard-coded parts not passed with `--extern`. First of all, I'd want to confirm that this is intended stabilization, rather than a part of the "extended beta" scheme that's going to be reverted before releasing stable. (EDIT: to clarify - this is a question, I'm \*asking\* for confirmation, rather than give it.) Second, on 2015 edition extern prelude is not so fundamentally tied to imports and is a mere convenience, so this PR scales them back to the uncontroversial subset. The "uncontroversial subset" means that if libcore is injected it brings `core` into prelude, if libstd is injected it brings `std` and `core` into prelude. On 2015 edition this can be implemented through the library prelude (rather than hard-coding in the compiler) right now, I'll do it in a follow-up PR. UPDATE: The change is done for both 2015 and 2018 editions now as discussed below. Closes #53166
2 parents 01ca85b + 894a8d5 commit 37ba107

File tree

12 files changed

+33
-31
lines changed

12 files changed

+33
-31
lines changed

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

-18
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use syntax::edition::Edition;
3535
use syntax::feature_gate::{self, AttributeType};
3636
use syntax::json::JsonEmitter;
3737
use syntax::source_map;
38-
use syntax::symbol::Symbol;
3938
use syntax::parse::{self, ParseSess};
4039
use syntax_pos::{MultiSpan, Span};
4140
use util::profiling::SelfProfiler;
@@ -166,10 +165,6 @@ pub struct Session {
166165

167166
/// Cap lint level specified by a driver specifically.
168167
pub driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
169-
170-
/// All the crate names specified with `--extern`, and the builtin ones.
171-
/// Starting with the Rust 2018 edition, absolute paths resolve in this set.
172-
pub extern_prelude: FxHashSet<Symbol>,
173168
}
174169

175170
pub struct PerfStats {
@@ -1137,18 +1132,6 @@ pub fn build_session_(
11371132
CguReuseTracker::new_disabled()
11381133
};
11391134

1140-
1141-
let mut extern_prelude: FxHashSet<Symbol> =
1142-
sopts.externs.iter().map(|kv| Symbol::intern(kv.0)).collect();
1143-
1144-
// HACK(eddyb) this ignores the `no_{core,std}` attributes.
1145-
// FIXME(eddyb) warn (somewhere) if core/std is used with `no_{core,std}`.
1146-
// if !attr::contains_name(&krate.attrs, "no_core") {
1147-
// if !attr::contains_name(&krate.attrs, "no_std") {
1148-
extern_prelude.insert(Symbol::intern("core"));
1149-
extern_prelude.insert(Symbol::intern("std"));
1150-
extern_prelude.insert(Symbol::intern("meta"));
1151-
11521135
let sess = Session {
11531136
target: target_cfg,
11541137
host,
@@ -1224,7 +1207,6 @@ pub fn build_session_(
12241207
has_global_allocator: Once::new(),
12251208
has_panic_handler: Once::new(),
12261209
driver_lint_caps: FxHashMap(),
1227-
extern_prelude,
12281210
};
12291211

12301212
validate_commandline_args_with_session_available(&sess);

Diff for: src/librustc/ty/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ pub struct GlobalCtxt<'tcx> {
936936
freevars: FxHashMap<DefId, Lrc<Vec<hir::Freevar>>>,
937937

938938
maybe_unused_trait_imports: FxHashSet<DefId>,
939-
940939
maybe_unused_extern_crates: Vec<(DefId, Span)>,
940+
pub extern_prelude: FxHashSet<ast::Name>,
941941

942942
// Internal cache for metadata decoding. No need to track deps on this.
943943
pub rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1223,6 +1223,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12231223
.into_iter()
12241224
.map(|(id, sp)| (hir.local_def_id(id), sp))
12251225
.collect(),
1226+
extern_prelude: resolutions.extern_prelude,
12261227
hir,
12271228
def_path_hash_to_def_id,
12281229
queries: query::Queries::new(

Diff for: src/librustc/ty/item_path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
289289
// printing the `CrateRoot` so we don't prepend a `crate::` to paths.
290290
let mut is_prelude_crate = false;
291291
if let DefPathData::CrateRoot = self.def_key(parent_did).disambiguated_data.data {
292-
if self.sess.extern_prelude.contains(&data.as_interned_str().as_symbol()) {
292+
if self.extern_prelude.contains(&data.as_interned_str().as_symbol()) {
293293
is_prelude_crate = true;
294294
}
295295
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use ty::subst::{Subst, Substs};
3636
use ty::util::{IntTypeExt, Discr};
3737
use ty::walk::TypeWalker;
3838
use util::captures::Captures;
39-
use util::nodemap::{NodeSet, DefIdMap, FxHashMap};
39+
use util::nodemap::{NodeSet, DefIdMap, FxHashMap, FxHashSet};
4040
use arena::SyncDroplessArena;
4141
use session::DataTypeKind;
4242

@@ -141,6 +141,7 @@ pub struct Resolutions {
141141
pub maybe_unused_trait_imports: NodeSet,
142142
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
143143
pub export_map: ExportMap,
144+
pub extern_prelude: FxHashSet<Name>,
144145
}
145146

146147
#[derive(Clone, Copy, PartialEq, Eq, Debug)]

Diff for: src/librustc_driver/driver.rs

+1
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ where
790790
trait_map: resolver.trait_map,
791791
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
792792
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates,
793+
extern_prelude: resolver.extern_prelude,
793794
},
794795

795796
analysis: ty::CrateAnalysis {

Diff for: src/librustc_resolve/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
136136
// Need to clone else we can't call `resolve_path` without a borrow error. We also store
137137
// into a `BTreeMap` so we can get consistent ordering (and therefore the same diagnostic)
138138
// each time.
139-
let external_crate_names: BTreeSet<Symbol> = self.resolver.session.extern_prelude
139+
let external_crate_names: BTreeSet<Symbol> = self.resolver.extern_prelude
140140
.clone().drain().collect();
141141

142142
// Insert a new path segment that we can replace.

Diff for: src/librustc_resolve/lib.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,7 @@ pub struct Resolver<'a, 'b: 'a> {
13601360
graph_root: Module<'a>,
13611361

13621362
prelude: Option<Module<'a>>,
1363+
pub extern_prelude: FxHashSet<Name>,
13631364

13641365
/// n.b. This is used only for better diagnostics, not name resolution itself.
13651366
has_self: FxHashSet<DefId>,
@@ -1676,6 +1677,19 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
16761677
DefCollector::new(&mut definitions, Mark::root())
16771678
.collect_root(crate_name, session.local_crate_disambiguator());
16781679

1680+
let mut extern_prelude: FxHashSet<Name> =
1681+
session.opts.externs.iter().map(|kv| Symbol::intern(kv.0)).collect();
1682+
1683+
if !attr::contains_name(&krate.attrs, "no_core") {
1684+
extern_prelude.insert(Symbol::intern("core"));
1685+
if !attr::contains_name(&krate.attrs, "no_std") {
1686+
extern_prelude.insert(Symbol::intern("std"));
1687+
if session.rust_2018() {
1688+
extern_prelude.insert(Symbol::intern("meta"));
1689+
}
1690+
}
1691+
}
1692+
16791693
let mut invocations = FxHashMap();
16801694
invocations.insert(Mark::root(),
16811695
arenas.alloc_invocation_data(InvocationData::root(graph_root)));
@@ -1694,6 +1708,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
16941708
// AST.
16951709
graph_root,
16961710
prelude: None,
1711+
extern_prelude,
16971712

16981713
has_self: FxHashSet(),
16991714
field_names: FxHashMap(),
@@ -1966,7 +1981,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
19661981

19671982
if !module.no_implicit_prelude {
19681983
// `record_used` means that we don't try to load crates during speculative resolution
1969-
if record_used && ns == TypeNS && self.session.extern_prelude.contains(&ident.name) {
1984+
if record_used && ns == TypeNS && self.extern_prelude.contains(&ident.name) {
19701985
let crate_id = self.crate_loader.process_path_extern(ident.name, ident.span);
19711986
let crate_root = self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
19721987
self.populate_module_if_necessary(&crate_root);
@@ -4018,7 +4033,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
40184033
} else {
40194034
// Items from the prelude
40204035
if !module.no_implicit_prelude {
4021-
names.extend(self.session.extern_prelude.iter().cloned());
4036+
names.extend(self.extern_prelude.iter().cloned());
40224037
if let Some(prelude) = self.prelude {
40234038
add_module_candidates(prelude, &mut names);
40244039
}
@@ -4464,7 +4479,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
44644479
);
44654480

44664481
if self.session.rust_2018() {
4467-
for &name in &self.session.extern_prelude {
4482+
let extern_prelude_names = self.extern_prelude.clone();
4483+
for &name in extern_prelude_names.iter() {
44684484
let ident = Ident::with_empty_ctxt(name);
44694485
match self.crate_loader.maybe_process_path_extern(name, ident.span) {
44704486
Some(crate_id) => {

Diff for: src/librustc_resolve/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
692692
}
693693
}
694694
WhereToResolve::ExternPrelude => {
695-
if use_prelude && self.session.extern_prelude.contains(&ident.name) {
695+
if use_prelude && self.extern_prelude.contains(&ident.name) {
696696
let crate_id =
697697
self.crate_loader.process_path_extern(ident.name, ident.span);
698698
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.session.extern_prelude.contains(&ident.name)
202+
self.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.session.extern_prelude.contains(&ident.name)
221+
self.extern_prelude.contains(&ident.name)
222222
{
223223
let crate_id =
224224
self.crate_loader.process_path_extern(ident.name, ident.span);
@@ -736,7 +736,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
736736
let uniform_paths_feature = self.session.features_untracked().uniform_paths;
737737
for ((span, _, ns), results) in uniform_paths_canaries {
738738
let name = results.name;
739-
let external_crate = if ns == TypeNS && self.session.extern_prelude.contains(&name) {
739+
let external_crate = if ns == TypeNS && self.extern_prelude.contains(&name) {
740740
let crate_id =
741741
self.crate_loader.process_path_extern(name, span);
742742
Some(Def::Mod(DefId { krate: crate_id, index: CRATE_DEF_INDEX }))

Diff for: src/librustc_typeck/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
164164
// If the extern crate isn't in the extern prelude,
165165
// there is no way it can be written as an `use`.
166166
let orig_name = extern_crate.orig_name.unwrap_or(item.name);
167-
if !tcx.sess.extern_prelude.contains(&orig_name) {
167+
if !tcx.extern_prelude.contains(&orig_name) {
168168
continue;
169169
}
170170

Diff for: src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ pub fn run_core(search_paths: SearchPaths,
474474
trait_map: resolver.trait_map.clone(),
475475
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports.clone(),
476476
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates.clone(),
477+
extern_prelude: resolver.extern_prelude.clone(),
477478
};
478479
let analysis = ty::CrateAnalysis {
479480
access_levels: Lrc::new(AccessLevels::default()),

Diff for: src/test/ui/rust-2018/issue-54006.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0432]: unresolved import `alloc`
22
--> $DIR/issue-54006.rs:16:5
33
|
44
LL | use alloc::vec;
5-
| ^^^^^ Did you mean `std::alloc`?
5+
| ^^^^^ Did you mean `core::alloc`?
66

77
error: cannot determine resolution for the macro `vec`
88
--> $DIR/issue-54006.rs:20:18

0 commit comments

Comments
 (0)