Skip to content

Commit 759204f

Browse files
authored
Rollup merge of #82217 - m-ou-se:edition-prelude, r=nikomatsakis
Edition-specific preludes This changes `{std,core}::prelude` to export edition-specific preludes under `rust_2015`, `rust_2018` and `rust_2021`. (As suggested in #51418 (comment).) For now they all just re-export `v1::*`, but this allows us to add things to the 2021edition prelude soon. This also changes the compiler to make the automatically injected prelude import dependent on the selected edition. cc `@rust-lang/libs` `@djc`
2 parents 49bf48a + 76fd8d7 commit 759204f

20 files changed

+113
-33
lines changed

compiler/rustc_builtin_macros/src/standard_library_imports.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast as ast;
22
use rustc_expand::base::{ExtCtxt, ResolverExpand};
33
use rustc_expand::expand::ExpansionConfig;
44
use rustc_session::Session;
5-
use rustc_span::edition::Edition;
5+
use rustc_span::edition::Edition::*;
66
use rustc_span::hygiene::AstPass;
77
use rustc_span::symbol::{kw, sym, Ident, Symbol};
88
use rustc_span::DUMMY_SP;
@@ -13,7 +13,7 @@ pub fn inject(
1313
sess: &Session,
1414
alt_std_name: Option<Symbol>,
1515
) -> ast::Crate {
16-
let rust_2018 = sess.parse_sess.edition >= Edition::Edition2018;
16+
let edition = sess.parse_sess.edition;
1717

1818
// the first name in this list is the crate name of the crate with the prelude
1919
let names: &[Symbol] = if sess.contains_name(&krate.attrs, sym::no_core) {
@@ -42,7 +42,11 @@ pub fn inject(
4242

4343
// .rev() to preserve ordering above in combination with insert(0, ...)
4444
for &name in names.iter().rev() {
45-
let ident = if rust_2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
45+
let ident = if edition >= Edition2018 {
46+
Ident::new(name, span)
47+
} else {
48+
Ident::new(name, call_site)
49+
};
4650
krate.items.insert(
4751
0,
4852
cx.item(
@@ -58,14 +62,18 @@ pub fn inject(
5862
// the one with the prelude.
5963
let name = names[0];
6064

61-
let import_path = if rust_2018 {
62-
[name, sym::prelude, sym::v1].iter().map(|symbol| Ident::new(*symbol, span)).collect()
63-
} else {
64-
[kw::PathRoot, name, sym::prelude, sym::v1]
65-
.iter()
66-
.map(|symbol| Ident::new(*symbol, span))
67-
.collect()
68-
};
65+
let root = (edition == Edition2015).then(|| kw::PathRoot);
66+
67+
let import_path = root
68+
.iter()
69+
.chain(&[name, sym::prelude])
70+
.chain(&[match edition {
71+
Edition2015 => sym::rust_2015,
72+
Edition2018 => sym::rust_2018,
73+
Edition2021 => sym::rust_2021,
74+
}])
75+
.map(|&symbol| Ident::new(symbol, span))
76+
.collect();
6977

7078
let use_item = cx.item(
7179
span,

compiler/rustc_span/src/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,11 @@ symbols! {
958958
rt,
959959
rtm_target_feature,
960960
rust,
961+
rust_2015,
961962
rust_2015_preview,
963+
rust_2018,
962964
rust_2018_preview,
965+
rust_2021,
963966
rust_2021_preview,
964967
rust_begin_unwind,
965968
rust_eh_catch_typeinfo,

library/core/src/prelude/mod.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
//! The libcore prelude
2+
//!
3+
//! This module is intended for users of libcore which do not link to libstd as
4+
//! well. This module is imported by default when `#![no_std]` is used in the
5+
//! same manner as the standard library's prelude.
26
37
#![stable(feature = "core_prelude", since = "1.4.0")]
48

59
pub mod v1;
10+
11+
/// The 2015 version of the core prelude.
12+
///
13+
/// See the [module-level documentation](self) for more.
14+
#[unstable(feature = "prelude_2015", issue = "none")]
15+
pub mod rust_2015 {
16+
#[unstable(feature = "prelude_2015", issue = "none")]
17+
#[doc(no_inline)]
18+
pub use super::v1::*;
19+
}
20+
21+
/// The 2018 version of the core prelude.
22+
///
23+
/// See the [module-level documentation](self) for more.
24+
#[unstable(feature = "prelude_2018", issue = "none")]
25+
pub mod rust_2018 {
26+
#[unstable(feature = "prelude_2018", issue = "none")]
27+
#[doc(no_inline)]
28+
pub use super::v1::*;
29+
}
30+
31+
/// The 2021 version of the core prelude.
32+
///
33+
/// See the [module-level documentation](self) for more.
34+
#[unstable(feature = "prelude_2021", issue = "none")]
35+
pub mod rust_2021 {
36+
#[unstable(feature = "prelude_2021", issue = "none")]
37+
#[doc(no_inline)]
38+
pub use super::v1::*;
39+
40+
// FIXME: Add more things.
41+
}

library/core/src/prelude/v1.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
//! The core prelude
1+
//! The first version of the core prelude.
22
//!
3-
//! This module is intended for users of libcore which do not link to libstd as
4-
//! well. This module is imported by default when `#![no_std]` is used in the
5-
//! same manner as the standard library's prelude.
3+
//! See the [module-level documentation](super) for more.
64
75
#![stable(feature = "core_prelude", since = "1.4.0")]
86

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
#![feature(panic_internals)]
303303
#![feature(panic_unwind)]
304304
#![feature(pin_static_ref)]
305+
#![feature(prelude_2021)]
305306
#![feature(prelude_import)]
306307
#![feature(ptr_internals)]
307308
#![feature(raw)]

library/std/src/prelude/mod.rs

+34
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,37 @@
8484
#![stable(feature = "rust1", since = "1.0.0")]
8585

8686
pub mod v1;
87+
88+
/// The 2015 version of the prelude of The Rust Standard Library.
89+
///
90+
/// See the [module-level documentation](self) for more.
91+
#[unstable(feature = "prelude_2015", issue = "none")]
92+
pub mod rust_2015 {
93+
#[unstable(feature = "prelude_2015", issue = "none")]
94+
#[doc(no_inline)]
95+
pub use super::v1::*;
96+
}
97+
98+
/// The 2018 version of the prelude of The Rust Standard Library.
99+
///
100+
/// See the [module-level documentation](self) for more.
101+
#[unstable(feature = "prelude_2018", issue = "none")]
102+
pub mod rust_2018 {
103+
#[unstable(feature = "prelude_2018", issue = "none")]
104+
#[doc(no_inline)]
105+
pub use super::v1::*;
106+
}
107+
108+
/// The 2021 version of the prelude of The Rust Standard Library.
109+
///
110+
/// See the [module-level documentation](self) for more.
111+
#[unstable(feature = "prelude_2021", issue = "none")]
112+
pub mod rust_2021 {
113+
#[unstable(feature = "prelude_2021", issue = "none")]
114+
#[doc(no_inline)]
115+
pub use super::v1::*;
116+
117+
#[unstable(feature = "prelude_2021", issue = "none")]
118+
#[doc(no_inline)]
119+
pub use core::prelude::rust_2021::*;
120+
}

library/std/src/prelude/v1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The first version of the prelude of The Rust Standard Library.
22
//!
3-
//! See the [module-level documentation](../index.html) for more.
3+
//! See the [module-level documentation](super) for more.
44
55
#![stable(feature = "rust1", since = "1.0.0")]
66

src/test/pretty/asm.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![no_std]
33
#![feature(asm)]
44
#[prelude_import]
5-
use ::std::prelude::v1::*;
5+
use ::std::prelude::rust_2015::*;
66
#[macro_use]
77
extern crate std;
88

src/test/pretty/cast-lt.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(prelude_import)]
22
#![no_std]
33
#[prelude_import]
4-
use ::std::prelude::v1::*;
4+
use ::std::prelude::rust_2015::*;
55
#[macro_use]
66
extern crate std;
77
// pretty-compare-only

src/test/pretty/dollar-crate.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(prelude_import)]
22
#![no_std]
33
#[prelude_import]
4-
use ::std::prelude::v1::*;
4+
use ::std::prelude::rust_2015::*;
55
#[macro_use]
66
extern crate std;
77
// pretty-compare-only

src/test/pretty/expanded-and-path-remap-80832.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(prelude_import)]
22
#![no_std]
33
#[prelude_import]
4-
use ::std::prelude::v1::*;
4+
use ::std::prelude::rust_2015::*;
55
#[macro_use]
66
extern crate std;
77
// Test for issue 80832

src/test/pretty/issue-12590-c.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(prelude_import)]
22
#![no_std]
33
#[prelude_import]
4-
use ::std::prelude::v1::*;
4+
use ::std::prelude::rust_2015::*;
55
#[macro_use]
66
extern crate std;
77
// pretty-compare-only

src/test/pretty/issue-4264.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[prelude_import]
2-
use ::std::prelude::v1::*;
2+
use ::std::prelude::rust_2015::*;
33
#[macro_use]
44
extern crate std;
55
// pretty-compare-only
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"crate_type","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"args":{"variant":"Eq","fields":[{"lo":0,"hi":0},{"kind":{"variant":"Interpolated","fields":[{"variant":"NtExpr","fields":[{"id":0,"kind":{"variant":"Lit","fields":[{"token":{"kind":"Str","symbol":"lib","suffix":null},"kind":{"variant":"Str","fields":["lib","Cooked"]},"span":{"lo":0,"hi":0}}]},"span":{"lo":0,"hi":0},"attrs":{"0":null},"tokens":{"0":[[{"variant":"Token","fields":[{"kind":{"variant":"Literal","fields":[{"kind":"Str","symbol":"lib","suffix":null}]},"span":{"lo":0,"hi":0}}]},"Alone"]]}}]}]},"span":{"lo":0,"hi":0}}]},"tokens":null},{"0":[[{"variant":"Token","fields":[{"kind":"Pound","span":{"lo":0,"hi":0}}]},"Joint"],[{"variant":"Token","fields":[{"kind":"Not","span":{"lo":0,"hi":0}}]},"Alone"],[{"variant":"Delimited","fields":[{"open":{"lo":0,"hi":0},"close":{"lo":0,"hi":0}},"Bracket",{"0":[[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["crate_type",false]},"span":{"lo":0,"hi":0}}]},"Alone"],[{"variant":"Token","fields":[{"kind":"Eq","span":{"lo":0,"hi":0}}]},"Alone"],[{"variant":"Token","fields":[{"kind":{"variant":"Literal","fields":[{"kind":"Str","symbol":"lib","suffix":null}]},"span":{"lo":0,"hi":0}}]},"Alone"]]}]},"Alone"]]}]},"id":null,"style":"Inner","span":{"lo":0,"hi":0}}],"items":[{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"prelude_import","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"args":"Empty","tokens":null},null]},"id":null,"style":"Outer","span":{"lo":0,"hi":0}}],"id":0,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"","span":{"lo":0,"hi":0}},"kind":{"variant":"Use","fields":[{"prefix":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"{{root}}","span":{"lo":0,"hi":0}},"id":0,"args":null},{"ident":{"name":"std","span":{"lo":0,"hi":0}},"id":0,"args":null},{"ident":{"name":"prelude","span":{"lo":0,"hi":0}},"id":0,"args":null},{"ident":{"name":"v1","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"kind":"Glob","span":{"lo":0,"hi":0}}]},"tokens":null},{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"macro_use","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"args":"Empty","tokens":null},null]},"id":null,"style":"Outer","span":{"lo":0,"hi":0}}],"id":0,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"std","span":{"lo":0,"hi":0}},"kind":{"variant":"ExternCrate","fields":[null]},"tokens":null},{"attrs":[],"id":0,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"core","span":{"lo":0,"hi":0}},"kind":{"variant":"ExternCrate","fields":[null]},"tokens":null}],"span":{"lo":0,"hi":0},"proc_macros":[]}
1+
{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"crate_type","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"args":{"variant":"Eq","fields":[{"lo":0,"hi":0},{"kind":{"variant":"Interpolated","fields":[{"variant":"NtExpr","fields":[{"id":0,"kind":{"variant":"Lit","fields":[{"token":{"kind":"Str","symbol":"lib","suffix":null},"kind":{"variant":"Str","fields":["lib","Cooked"]},"span":{"lo":0,"hi":0}}]},"span":{"lo":0,"hi":0},"attrs":{"0":null},"tokens":{"0":[[{"variant":"Token","fields":[{"kind":{"variant":"Literal","fields":[{"kind":"Str","symbol":"lib","suffix":null}]},"span":{"lo":0,"hi":0}}]},"Alone"]]}}]}]},"span":{"lo":0,"hi":0}}]},"tokens":null},{"0":[[{"variant":"Token","fields":[{"kind":"Pound","span":{"lo":0,"hi":0}}]},"Joint"],[{"variant":"Token","fields":[{"kind":"Not","span":{"lo":0,"hi":0}}]},"Alone"],[{"variant":"Delimited","fields":[{"open":{"lo":0,"hi":0},"close":{"lo":0,"hi":0}},"Bracket",{"0":[[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["crate_type",false]},"span":{"lo":0,"hi":0}}]},"Alone"],[{"variant":"Token","fields":[{"kind":"Eq","span":{"lo":0,"hi":0}}]},"Alone"],[{"variant":"Token","fields":[{"kind":{"variant":"Literal","fields":[{"kind":"Str","symbol":"lib","suffix":null}]},"span":{"lo":0,"hi":0}}]},"Alone"]]}]},"Alone"]]}]},"id":null,"style":"Inner","span":{"lo":0,"hi":0}}],"items":[{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"prelude_import","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"args":"Empty","tokens":null},null]},"id":null,"style":"Outer","span":{"lo":0,"hi":0}}],"id":0,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"","span":{"lo":0,"hi":0}},"kind":{"variant":"Use","fields":[{"prefix":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"{{root}}","span":{"lo":0,"hi":0}},"id":0,"args":null},{"ident":{"name":"std","span":{"lo":0,"hi":0}},"id":0,"args":null},{"ident":{"name":"prelude","span":{"lo":0,"hi":0}},"id":0,"args":null},{"ident":{"name":"rust_2015","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"kind":"Glob","span":{"lo":0,"hi":0}}]},"tokens":null},{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"macro_use","span":{"lo":0,"hi":0}},"id":0,"args":null}],"tokens":null},"args":"Empty","tokens":null},null]},"id":null,"style":"Outer","span":{"lo":0,"hi":0}}],"id":0,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"std","span":{"lo":0,"hi":0}},"kind":{"variant":"ExternCrate","fields":[null]},"tokens":null},{"attrs":[],"id":0,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"core","span":{"lo":0,"hi":0}},"kind":{"variant":"ExternCrate","fields":[null]},"tokens":null}],"span":{"lo":0,"hi":0},"proc_macros":[]}

src/test/ui/imports/extern-prelude-extern-crate-restricted-shadowing.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ LL | extern crate std as Vec;
2424
LL | define_vec!();
2525
| -------------- in this macro invocation
2626
note: `Vec` could also refer to the struct defined here
27-
--> $SRC_DIR/std/src/prelude/v1.rs:LL:COL
27+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
2828
|
29-
LL | pub use crate::vec::Vec;
30-
| ^^^^^^^^^^^^^^^
29+
LL | pub use super::v1::*;
30+
| ^^^^^^^^^^^^
3131
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3232

3333
error: aborting due to 2 previous errors

src/test/ui/issues/issue-27033.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ error[E0530]: match bindings cannot shadow unit variants
44
LL | None @ _ => {}
55
| ^^^^ cannot be named the same as a unit variant
66
|
7-
::: $SRC_DIR/std/src/prelude/v1.rs:LL:COL
7+
::: $SRC_DIR/std/src/prelude/mod.rs:LL:COL
88
|
9-
LL | pub use crate::option::Option::{self, None, Some};
10-
| ---- the unit variant `None` is defined here
9+
LL | pub use super::v1::*;
10+
| ------------ the unit variant `None` is defined here
1111

1212
error[E0530]: match bindings cannot shadow constants
1313
--> $DIR/issue-27033.rs:7:9

src/test/ui/issues/issue-60662.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#![feature(type_alias_impl_trait)]
55
#[prelude_import]
6-
use ::std::prelude::v1::*;
6+
use ::std::prelude::rust_2015::*;
77
#[macro_use]
88
extern crate std;
99

src/test/ui/proc-macro/meta-macro-hygiene.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Respanned: TokenStream [Ident { ident: "$crate", span: $DIR/auxiliary/make-macro
1515

1616
#![no_std /* 0#0 */]
1717
#[prelude_import /* 0#1 */]
18-
use core /* 0#1 */::prelude /* 0#1 */::v1 /* 0#1 */::*;
18+
use core /* 0#1 */::prelude /* 0#1 */::rust_2018 /* 0#1 */::*;
1919
#[macro_use /* 0#1 */]
2020
extern crate core /* 0#1 */;
2121
#[macro_use /* 0#1 */]

src/test/ui/proc-macro/nonterminal-token-hygiene.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
3535

3636
#![no_std /* 0#0 */]
3737
#[prelude_import /* 0#1 */]
38-
use ::core /* 0#1 */::prelude /* 0#1 */::v1 /* 0#1 */::*;
38+
use ::core /* 0#1 */::prelude /* 0#1 */::rust_2015 /* 0#1 */::*;
3939
#[macro_use /* 0#1 */]
4040
extern crate core /* 0#2 */;
4141
#[macro_use /* 0#1 */]

src/test/ui/rfc-2497-if-let-chains/ast-pretty-check.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(prelude_import)]
22
#![no_std]
33
#[prelude_import]
4-
use ::std::prelude::v1::*;
4+
use ::std::prelude::rust_2015::*;
55
#[macro_use]
66
extern crate std;
77
// build-pass (FIXME(62277): could be check-pass?)

0 commit comments

Comments
 (0)