Skip to content

Commit d3b564c

Browse files
committed
Pick the injected prelude based on the edition.
1 parent 1ab9fe5 commit d3b564c

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

compiler/rustc_builtin_macros/src/standard_library_imports.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::ptr::P;
33
use rustc_expand::base::{ExtCtxt, ResolverExpand};
44
use rustc_expand::expand::ExpansionConfig;
55
use rustc_session::Session;
6-
use rustc_span::edition::Edition;
6+
use rustc_span::edition::Edition::*;
77
use rustc_span::hygiene::AstPass;
88
use rustc_span::symbol::{kw, sym, Ident, Symbol};
99
use rustc_span::DUMMY_SP;
@@ -14,7 +14,7 @@ pub fn inject(
1414
sess: &Session,
1515
alt_std_name: Option<Symbol>,
1616
) -> ast::Crate {
17-
let rust_2018 = sess.parse_sess.edition >= Edition::Edition2018;
17+
let edition = sess.parse_sess.edition;
1818

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

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

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

7179
let use_item = cx.item(
7280
span,

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,11 @@ symbols! {
944944
rt,
945945
rtm_target_feature,
946946
rust,
947+
rust_2015,
947948
rust_2015_preview,
949+
rust_2018,
948950
rust_2018_preview,
951+
rust_2021,
949952
rust_2021_preview,
950953
rust_begin_unwind,
951954
rust_eh_catch_typeinfo,

0 commit comments

Comments
 (0)