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 https://github.com/rust-lang/rust/issues/51418#issuecomment-395630382.) 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`
This commit is contained in:
commit
759204ffc4
20 changed files with 113 additions and 33 deletions
|
@ -2,7 +2,7 @@ use rustc_ast as ast;
|
|||
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;
|
||||
|
@ -13,7 +13,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) {
|
||||
|
@ -42,7 +42,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(
|
||||
|
@ -58,14 +62,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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue