syntax: gensym the injected std/core extern crates in the Rust 2018 edition.
This commit is contained in:
@@ -865,7 +865,7 @@ where
|
|||||||
|
|
||||||
krate = time(sess, "crate injection", || {
|
krate = time(sess, "crate injection", || {
|
||||||
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
|
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
|
||||||
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
|
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name, sess.edition())
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut addl_plugins = Some(addl_plugins);
|
let mut addl_plugins = Some(addl_plugins);
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
use ast;
|
use ast;
|
||||||
use attr;
|
use attr;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
use std::iter;
|
||||||
|
use edition::Edition;
|
||||||
use ext::hygiene::{Mark, SyntaxContext};
|
use ext::hygiene::{Mark, SyntaxContext};
|
||||||
use symbol::{Symbol, keywords};
|
use symbol::{Symbol, keywords};
|
||||||
use syntax_pos::{DUMMY_SP, Span};
|
use syntax_pos::{DUMMY_SP, Span};
|
||||||
@@ -43,7 +45,13 @@ thread_local! {
|
|||||||
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
|
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
|
pub fn maybe_inject_crates_ref(
|
||||||
|
mut krate: ast::Crate,
|
||||||
|
alt_std_name: Option<&str>,
|
||||||
|
edition: Edition,
|
||||||
|
) -> ast::Crate {
|
||||||
|
let rust_2018 = edition >= Edition::Edition2018;
|
||||||
|
|
||||||
// the first name in this list is the crate name of the crate with the prelude
|
// the first name in this list is the crate name of the crate with the prelude
|
||||||
let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
|
let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
|
||||||
return krate;
|
return krate;
|
||||||
@@ -58,14 +66,27 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>
|
|||||||
};
|
};
|
||||||
|
|
||||||
// .rev() to preserve ordering above in combination with insert(0, ...)
|
// .rev() to preserve ordering above in combination with insert(0, ...)
|
||||||
for name in names.iter().rev() {
|
let alt_std_name = alt_std_name.map(Symbol::intern);
|
||||||
|
for orig_name in names.iter().rev() {
|
||||||
|
let orig_name = Symbol::intern(orig_name);
|
||||||
|
let mut rename = orig_name;
|
||||||
|
// HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
|
||||||
|
// so they don't accidentally interfere with the new import paths.
|
||||||
|
if rust_2018 {
|
||||||
|
rename = orig_name.gensymed();
|
||||||
|
}
|
||||||
|
let orig_name = if rename != orig_name {
|
||||||
|
Some(orig_name)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
krate.module.items.insert(0, P(ast::Item {
|
krate.module.items.insert(0, P(ast::Item {
|
||||||
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
|
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
|
||||||
attr::mk_attr_id(),
|
attr::mk_attr_id(),
|
||||||
attr::mk_word_item(ast::Ident::from_str("macro_use")))],
|
attr::mk_word_item(ast::Ident::from_str("macro_use")))],
|
||||||
vis: dummy_spanned(ast::VisibilityKind::Inherited),
|
vis: dummy_spanned(ast::VisibilityKind::Inherited),
|
||||||
node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
|
node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
|
||||||
ident: ast::Ident::from_str(name),
|
ident: ast::Ident::with_empty_ctxt(rename),
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
tokens: None,
|
tokens: None,
|
||||||
@@ -91,9 +112,11 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>
|
|||||||
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||||
node: ast::ItemKind::Use(P(ast::UseTree {
|
node: ast::ItemKind::Use(P(ast::UseTree {
|
||||||
prefix: ast::Path {
|
prefix: ast::Path {
|
||||||
segments: [name, "prelude", "v1"].into_iter().map(|name| {
|
segments: iter::once(keywords::CrateRoot.ident())
|
||||||
ast::PathSegment::from_ident(ast::Ident::from_str(name))
|
.chain(
|
||||||
}).collect(),
|
[name, "prelude", "v1"].iter().cloned()
|
||||||
|
.map(ast::Ident::from_str)
|
||||||
|
).map(ast::PathSegment::from_ident).collect(),
|
||||||
span,
|
span,
|
||||||
},
|
},
|
||||||
kind: ast::UseTreeKind::Glob,
|
kind: ast::UseTreeKind::Glob,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#![feature(prelude_import)]
|
#![feature(prelude_import)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#[prelude_import]
|
#[prelude_import]
|
||||||
use std::prelude::v1::*;
|
use ::std::prelude::v1::*;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate std;
|
extern crate std;
|
||||||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#[prelude_import]
|
#[prelude_import]
|
||||||
use std::prelude::v1::*;
|
use ::std::prelude::v1::*;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate std;
|
extern crate std;
|
||||||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
|||||||
Reference in New Issue
Block a user