Skip to content

Commit e4ba1d4

Browse files
committed
Auto merge of #54116 - eddyb:extern-prelude, r=petrochenkov
rustc_resolve: allow only core, std, meta and --extern in Rust 2018 paths. As per #53166 (comment): * Rust 2018 imports can no longer refer to crates not in "extern prelude" * `::foo` won't load a crate named `foo` unless `foo` is in the "extern prelude" * `extern crate foo;`, however, remains unchanged (can load arbitrary crates) * `--extern crate_name` is added (note the lack of `=path`) as an unstable option * adds `crate_name` to the "extern prelude" (see above) * crate is searched in sysroot & library paths, just like `extern crate crate_name`. * `Cargo` support will be added later * `core`, `std` and ~~`proc_macro`~~ `meta` are *always* available in the extern prelude * warning for interaction with `no_std` / `no_core` will be added later * **EDIT**: `proc_macro` was replaced by `meta`, see #53166 (comment) * note that there is no crate named `meta` being added, so `use meta::...;` will fail, we're only whitelisting it so we can start producing `uniform_paths` compatibility errors Fixes #54006 (as the example now requires `--extern alloc`, which is unstable). Fixes #54253 (hit during fixing RLS). r? @petrochenkov cc @aturon @alexcrichton @Centril @joshtriplett
2 parents 9f53c87 + bde0a54 commit e4ba1d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+516
-254
lines changed

src/Cargo.lock

+10-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ version = "0.0.0"
1212
dependencies = [
1313
"compiler_builtins 0.0.0",
1414
"core 0.0.0",
15-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
15+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
1616
]
1717

1818
[[package]]
@@ -433,7 +433,7 @@ dependencies = [
433433
name = "core"
434434
version = "0.0.0"
435435
dependencies = [
436-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
436+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
437437
]
438438

439439
[[package]]
@@ -475,7 +475,7 @@ dependencies = [
475475
"crossbeam-epoch 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
476476
"crossbeam-utils 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
477477
"parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
478-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
478+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
479479
"smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
480480
]
481481

@@ -1444,7 +1444,7 @@ version = "0.3.0"
14441444
source = "registry+https://github.com/rust-lang/crates.io-index"
14451445
dependencies = [
14461446
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
1447-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
1447+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
14481448
"smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
14491449
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
14501450
]
@@ -1644,7 +1644,7 @@ dependencies = [
16441644

16451645
[[package]]
16461646
name = "rand"
1647-
version = "0.5.4"
1647+
version = "0.5.5"
16481648
source = "registry+https://github.com/rust-lang/crates.io-index"
16491649
dependencies = [
16501650
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1767,6 +1767,7 @@ dependencies = [
17671767
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
17681768
"ordslice 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
17691769
"racer 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
1770+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
17701771
"rayon 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
17711772
"regex 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
17721773
"rls-analysis 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2191,7 +2192,7 @@ version = "0.0.0"
21912192
dependencies = [
21922193
"graphviz 0.0.0",
21932194
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
2194-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
2195+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
21952196
"rustc 0.0.0",
21962197
"rustc_data_structures 0.0.0",
21972198
"rustc_fs_util 0.0.0",
@@ -2627,7 +2628,7 @@ dependencies = [
26272628
"panic_abort 0.0.0",
26282629
"panic_unwind 0.0.0",
26292630
"profiler_builtins 0.0.0",
2630-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
2631+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
26312632
"rustc_asan 0.0.0",
26322633
"rustc_lsan 0.0.0",
26332634
"rustc_msan 0.0.0",
@@ -2794,7 +2795,7 @@ version = "3.0.3"
27942795
source = "registry+https://github.com/rust-lang/crates.io-index"
27952796
dependencies = [
27962797
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
2797-
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
2798+
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
27982799
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
27992800
"remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
28002801
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3262,7 +3263,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32623263
"checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5"
32633264
"checksum racer 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4bc3847329b20ff5ba56c298938c179ae9911af15c9c10553f683b65164533"
32643265
"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
3265-
"checksum rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "12397506224b2f93e6664ffc4f664b29be8208e5157d3d90b44f09b5fae470ea"
3266+
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
32663267
"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2"
32673268
"checksum rayon 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80e811e76f1dbf68abf87a759083d34600017fc4e10b6bd5ad84a700f9dba4b1"
32683269
"checksum rayon-core 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d24ad214285a7729b174ed6d3bcfcb80177807f959d95fafd5bfc5c4f201ac8"

src/librustc/session/config.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,18 @@ impl OutputTypes {
275275
// DO NOT switch BTreeMap or BTreeSet out for an unsorted container type! That
276276
// would break dependency tracking for commandline arguments.
277277
#[derive(Clone, Hash)]
278-
pub struct Externs(BTreeMap<String, BTreeSet<String>>);
278+
pub struct Externs(BTreeMap<String, BTreeSet<Option<String>>>);
279279

280280
impl Externs {
281-
pub fn new(data: BTreeMap<String, BTreeSet<String>>) -> Externs {
281+
pub fn new(data: BTreeMap<String, BTreeSet<Option<String>>>) -> Externs {
282282
Externs(data)
283283
}
284284

285-
pub fn get(&self, key: &str) -> Option<&BTreeSet<String>> {
285+
pub fn get(&self, key: &str) -> Option<&BTreeSet<Option<String>>> {
286286
self.0.get(key)
287287
}
288288

289-
pub fn iter<'a>(&'a self) -> BTreeMapIter<'a, String, BTreeSet<String>> {
289+
pub fn iter<'a>(&'a self) -> BTreeMapIter<'a, String, BTreeSet<Option<String>>> {
290290
self.0.iter()
291291
}
292292
}
@@ -2169,6 +2169,8 @@ pub fn build_session_options_and_crate_config(
21692169
let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
21702170
let test = matches.opt_present("test");
21712171

2172+
let is_unstable_enabled = nightly_options::is_unstable_enabled(matches);
2173+
21722174
prints.extend(matches.opt_strs("print").into_iter().map(|s| match &*s {
21732175
"crate-name" => PrintRequest::CrateName,
21742176
"file-names" => PrintRequest::FileNames,
@@ -2182,15 +2184,13 @@ pub fn build_session_options_and_crate_config(
21822184
"tls-models" => PrintRequest::TlsModels,
21832185
"native-static-libs" => PrintRequest::NativeStaticLibs,
21842186
"target-spec-json" => {
2185-
if nightly_options::is_unstable_enabled(matches) {
2187+
if is_unstable_enabled {
21862188
PrintRequest::TargetSpec
21872189
} else {
21882190
early_error(
21892191
error_format,
2190-
&format!(
2191-
"the `-Z unstable-options` flag must also be passed to \
2192-
enable the target-spec-json print option"
2193-
),
2192+
"the `-Z unstable-options` flag must also be passed to \
2193+
enable the target-spec-json print option",
21942194
);
21952195
}
21962196
}
@@ -2220,18 +2220,19 @@ pub fn build_session_options_and_crate_config(
22202220
Some(s) => s,
22212221
None => early_error(error_format, "--extern value must not be empty"),
22222222
};
2223-
let location = match parts.next() {
2224-
Some(s) => s,
2225-
None => early_error(
2223+
let location = parts.next().map(|s| s.to_string());
2224+
if location.is_none() && !is_unstable_enabled {
2225+
early_error(
22262226
error_format,
2227-
"--extern value must be of the format `foo=bar`",
2228-
),
2227+
"the `-Z unstable-options` flag must also be passed to \
2228+
enable `--extern crate_name` without `=path`",
2229+
);
22292230
};
22302231

22312232
externs
22322233
.entry(name.to_string())
22332234
.or_default()
2234-
.insert(location.to_string());
2235+
.insert(location);
22352236
}
22362237

22372238
let crate_name = matches.opt_str("crate-name");
@@ -2687,33 +2688,33 @@ mod tests {
26872688
v1.externs = Externs::new(mk_map(vec![
26882689
(
26892690
String::from("a"),
2690-
mk_set(vec![String::from("b"), String::from("c")]),
2691+
mk_set(vec![Some(String::from("b")), Some(String::from("c"))]),
26912692
),
26922693
(
26932694
String::from("d"),
2694-
mk_set(vec![String::from("e"), String::from("f")]),
2695+
mk_set(vec![Some(String::from("e")), Some(String::from("f"))]),
26952696
),
26962697
]));
26972698

26982699
v2.externs = Externs::new(mk_map(vec![
26992700
(
27002701
String::from("d"),
2701-
mk_set(vec![String::from("e"), String::from("f")]),
2702+
mk_set(vec![Some(String::from("e")), Some(String::from("f"))]),
27022703
),
27032704
(
27042705
String::from("a"),
2705-
mk_set(vec![String::from("b"), String::from("c")]),
2706+
mk_set(vec![Some(String::from("b")), Some(String::from("c"))]),
27062707
),
27072708
]));
27082709

27092710
v3.externs = Externs::new(mk_map(vec![
27102711
(
27112712
String::from("a"),
2712-
mk_set(vec![String::from("b"), String::from("c")]),
2713+
mk_set(vec![Some(String::from("b")), Some(String::from("c"))]),
27132714
),
27142715
(
27152716
String::from("d"),
2716-
mk_set(vec![String::from("f"), String::from("e")]),
2717+
mk_set(vec![Some(String::from("f")), Some(String::from("e"))]),
27172718
),
27182719
]));
27192720

src/librustc/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12471247
.collect(),
12481248
hir,
12491249
def_path_hash_to_def_id,
1250-
queries: query::Queries::new(providers, on_disk_query_result_cache),
1250+
queries: query::Queries::new(
1251+
providers,
1252+
extern_providers,
1253+
on_disk_query_result_cache,
1254+
),
12511255
rcache: Lock::new(FxHashMap()),
12521256
selection_cache: traits::SelectionCache::new(),
12531257
evaluation_cache: traits::EvaluationCache::new(),

src/librustc/ty/query/plumbing.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,12 @@ macro_rules! define_queries_inner {
692692
impl<$tcx> Queries<$tcx> {
693693
pub fn new(
694694
providers: IndexVec<CrateNum, Providers<$tcx>>,
695+
fallback_extern_providers: Providers<$tcx>,
695696
on_disk_cache: OnDiskCache<'tcx>,
696697
) -> Self {
697698
Queries {
698699
providers,
700+
fallback_extern_providers: Box::new(fallback_extern_providers),
699701
on_disk_cache,
700702
$($name: Lock::new(QueryCache::new())),*
701703
}
@@ -818,7 +820,13 @@ macro_rules! define_queries_inner {
818820
#[inline]
819821
fn compute(tcx: TyCtxt<'_, 'tcx, '_>, key: Self::Key) -> Self::Value {
820822
__query_compute::$name(move || {
821-
let provider = tcx.queries.providers[key.query_crate()].$name;
823+
let provider = tcx.queries.providers.get(key.query_crate())
824+
// HACK(eddyb) it's possible crates may be loaded after
825+
// the query engine is created, and because crate loading
826+
// is not yet integrated with the query engine, such crates
827+
// would be be missing appropriate entries in `providers`.
828+
.unwrap_or(&tcx.queries.fallback_extern_providers)
829+
.$name;
822830
provider(tcx.global_tcx(), key)
823831
})
824832
}
@@ -899,6 +907,7 @@ macro_rules! define_queries_struct {
899907
pub(crate) on_disk_cache: OnDiskCache<'tcx>,
900908

901909
providers: IndexVec<CrateNum, Providers<$tcx>>,
910+
fallback_extern_providers: Box<Providers<$tcx>>,
902911

903912
$($(#[$attr])* $name: Lock<QueryCache<$tcx, queries::$name<$tcx>>>,)*
904913
}

0 commit comments

Comments
 (0)