about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-02-17 13:34:58 +0100
committerMara Bos <m-ou.se@m-ou.se>2021-02-25 12:47:58 +0100
commitd3b564c3d79fd33ea7da3b9b2ea046daacab4467 (patch)
tree4676fe9f32d1c9f96a3b971da3e7921ef5687cdb
parent1ab9fe5d44860050232438967bbbf9bdc35dbde1 (diff)
downloadrust-d3b564c3d79fd33ea7da3b9b2ea046daacab4467.tar.gz
rust-d3b564c3d79fd33ea7da3b9b2ea046daacab4467.zip
Pick the injected prelude based on the edition.
-rw-r--r--compiler/rustc_builtin_macros/src/standard_library_imports.rs30
-rw-r--r--compiler/rustc_span/src/symbol.rs3
2 files changed, 22 insertions, 11 deletions
diff --git a/compiler/rustc_builtin_macros/src/standard_library_imports.rs b/compiler/rustc_builtin_macros/src/standard_library_imports.rs
index 3a81d076dc5..efda7313307 100644
--- a/compiler/rustc_builtin_macros/src/standard_library_imports.rs
+++ b/compiler/rustc_builtin_macros/src/standard_library_imports.rs
@@ -3,7 +3,7 @@ use rustc_ast::ptr::P;
 use rustc_expand::base::{ExtCtxt, ResolverExpand};
 use rustc_expand::expand::ExpansionConfig;
 use rustc_session::Session;
-use rustc_span::edition::Edition;
+use rustc_span::edition::Edition::*;
 use rustc_span::hygiene::AstPass;
 use rustc_span::symbol::{kw, sym, Ident, Symbol};
 use rustc_span::DUMMY_SP;
@@ -14,7 +14,7 @@ pub fn inject(
     sess: &Session,
     alt_std_name: Option<Symbol>,
 ) -> ast::Crate {
-    let rust_2018 = sess.parse_sess.edition >= Edition::Edition2018;
+    let edition = sess.parse_sess.edition;
 
     // the first name in this list is the crate name of the crate with the prelude
     let names: &[Symbol] = if sess.contains_name(&krate.attrs, sym::no_core) {
@@ -43,7 +43,11 @@ pub fn inject(
 
     // .rev() to preserve ordering above in combination with insert(0, ...)
     for &name in names.iter().rev() {
-        let ident = if rust_2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
+        let ident = if edition >= Edition2018 {
+            Ident::new(name, span)
+        } else {
+            Ident::new(name, call_site)
+        };
         krate.items.insert(
             0,
             cx.item(
@@ -59,14 +63,18 @@ pub fn inject(
     // the one with the prelude.
     let name = names[0];
 
-    let import_path = if rust_2018 {
-        [name, sym::prelude, sym::v1].iter().map(|symbol| Ident::new(*symbol, span)).collect()
-    } else {
-        [kw::PathRoot, name, sym::prelude, sym::v1]
-            .iter()
-            .map(|symbol| Ident::new(*symbol, span))
-            .collect()
-    };
+    let root = (edition == Edition2015).then(|| kw::PathRoot);
+
+    let import_path = root
+        .iter()
+        .chain(&[name, sym::prelude])
+        .chain(&[match edition {
+            Edition2015 => sym::rust_2015,
+            Edition2018 => sym::rust_2018,
+            Edition2021 => sym::rust_2021,
+        }])
+        .map(|&symbol| Ident::new(symbol, span))
+        .collect();
 
     let use_item = cx.item(
         span,
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index b112402ffe3..7b85c964dfd 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -944,8 +944,11 @@ symbols! {
         rt,
         rtm_target_feature,
         rust,
+        rust_2015,
         rust_2015_preview,
+        rust_2018,
         rust_2018_preview,
+        rust_2021,
         rust_2021_preview,
         rust_begin_unwind,
         rust_eh_catch_typeinfo,