2019-02-06 11:33:01 -06:00
|
|
|
use crate::ast;
|
|
|
|
use crate::attr;
|
|
|
|
use crate::edition::Edition;
|
2019-06-30 07:58:56 -05:00
|
|
|
use crate::ext::hygiene::{Mark, SyntaxContext, MacroKind};
|
2019-05-11 09:41:37 -05:00
|
|
|
use crate::symbol::{Ident, Symbol, kw, sym};
|
2019-06-18 17:08:45 -05:00
|
|
|
use crate::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
|
2019-02-06 11:33:01 -06:00
|
|
|
use crate::ptr::P;
|
|
|
|
use crate::tokenstream::TokenStream;
|
|
|
|
|
2017-12-12 13:57:58 -06:00
|
|
|
use std::cell::Cell;
|
2018-08-10 08:01:32 -05:00
|
|
|
use std::iter;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::{DUMMY_SP, Span};
|
2012-01-26 17:20:29 -06:00
|
|
|
|
2015-06-30 22:05:17 -05:00
|
|
|
/// Craft a span that will be ignored by the stability lint's
|
2018-08-18 05:14:14 -05:00
|
|
|
/// call to source_map's `is_internal` check.
|
2015-06-30 22:05:17 -05:00
|
|
|
/// The expanded code uses the unstable `#[prelude_import]` attribute.
|
2019-04-05 17:15:49 -05:00
|
|
|
fn ignored_span(sp: Span, edition: Edition) -> Span {
|
2017-03-22 03:39:51 -05:00
|
|
|
let mark = Mark::fresh(Mark::root());
|
2019-06-17 14:18:56 -05:00
|
|
|
mark.set_expn_info(ExpnInfo::with_unstable(
|
2019-06-30 07:58:56 -05:00
|
|
|
ExpnKind::Macro(MacroKind::Attr, Symbol::intern("std_inject")), sp, edition,
|
|
|
|
&[sym::prelude_import],
|
2019-06-17 14:18:56 -05:00
|
|
|
));
|
2017-07-31 15:04:34 -05:00
|
|
|
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
|
2015-06-30 22:05:17 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:57:58 -06:00
|
|
|
pub fn injected_crate_name() -> Option<&'static str> {
|
|
|
|
INJECTED_CRATE_NAME.with(|name| name.get())
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
2019-05-07 01:03:44 -05:00
|
|
|
// A `Symbol` might make more sense here, but it doesn't work, probably for
|
|
|
|
// reasons relating to the use of thread-local storage for the Symbol
|
|
|
|
// interner.
|
2017-12-12 13:57:58 -06:00
|
|
|
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
|
2012-01-26 18:23:34 -06:00
|
|
|
}
|
2013-07-19 00:38:55 -05:00
|
|
|
|
2018-08-10 08:01:32 -05:00
|
|
|
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;
|
|
|
|
|
2018-03-30 06:06:34 -05:00
|
|
|
// the first name in this list is the crate name of the crate with the prelude
|
2019-05-07 22:21:18 -05:00
|
|
|
let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
|
2017-12-12 13:57:58 -06:00
|
|
|
return krate;
|
2019-05-07 22:21:18 -05:00
|
|
|
} else if attr::contains_name(&krate.attrs, sym::no_std) {
|
|
|
|
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
|
2018-03-30 06:06:34 -05:00
|
|
|
&["core"]
|
|
|
|
} else {
|
|
|
|
&["core", "compiler_builtins"]
|
|
|
|
}
|
2017-12-12 13:57:58 -06:00
|
|
|
} else {
|
2018-03-30 06:06:34 -05:00
|
|
|
&["std"]
|
2016-09-28 17:28:19 -05:00
|
|
|
};
|
2013-08-29 14:10:02 -05:00
|
|
|
|
2018-04-16 14:28:30 -05:00
|
|
|
// .rev() to preserve ordering above in combination with insert(0, ...)
|
2018-08-10 08:01:32 -05:00
|
|
|
let alt_std_name = alt_std_name.map(Symbol::intern);
|
2019-05-16 19:27:17 -05:00
|
|
|
for orig_name_str in names.iter().rev() {
|
2018-08-10 08:01:32 -05:00
|
|
|
// HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
|
|
|
|
// so they don't accidentally interfere with the new import paths.
|
2019-05-16 19:44:51 -05:00
|
|
|
let orig_name_sym = Symbol::intern(orig_name_str);
|
|
|
|
let orig_name_ident = Ident::with_empty_ctxt(orig_name_sym);
|
2019-05-16 19:27:17 -05:00
|
|
|
let (rename, orig_name) = if rust_2018 {
|
2019-05-16 19:44:51 -05:00
|
|
|
(orig_name_ident.gensym(), Some(orig_name_sym))
|
2018-08-10 08:01:32 -05:00
|
|
|
} else {
|
2019-05-16 19:44:51 -05:00
|
|
|
(orig_name_ident, None)
|
2018-08-10 08:01:32 -05:00
|
|
|
};
|
2018-03-30 06:06:34 -05:00
|
|
|
krate.module.items.insert(0, P(ast::Item {
|
2019-05-17 03:37:53 -05:00
|
|
|
attrs: vec![attr::mk_attr_outer(
|
|
|
|
DUMMY_SP,
|
|
|
|
attr::mk_attr_id(),
|
|
|
|
attr::mk_word_item(ast::Ident::with_empty_ctxt(sym::macro_use))
|
|
|
|
)],
|
2018-03-30 06:06:34 -05:00
|
|
|
vis: dummy_spanned(ast::VisibilityKind::Inherited),
|
2018-08-10 08:01:32 -05:00
|
|
|
node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
|
2019-05-16 19:44:51 -05:00
|
|
|
ident: rename,
|
2018-03-30 06:06:34 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: DUMMY_SP,
|
|
|
|
tokens: None,
|
|
|
|
}));
|
|
|
|
}
|
2017-12-12 13:57:58 -06:00
|
|
|
|
2018-03-30 06:06:34 -05:00
|
|
|
// the crates have been injected, the assumption is that the first one is the one with
|
|
|
|
// the prelude.
|
|
|
|
let name = names[0];
|
|
|
|
|
|
|
|
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
|
2016-06-05 04:56:05 -05:00
|
|
|
|
2019-04-05 17:15:49 -05:00
|
|
|
let span = ignored_span(DUMMY_SP, edition);
|
2016-06-05 04:56:05 -05:00
|
|
|
krate.module.items.insert(0, P(ast::Item {
|
|
|
|
attrs: vec![ast::Attribute {
|
2016-11-14 06:00:25 -06:00
|
|
|
style: ast::AttrStyle::Outer,
|
2019-05-21 21:42:23 -05:00
|
|
|
path: ast::Path::from_ident(ast::Ident::new(sym::prelude_import, span)),
|
2017-03-03 03:23:59 -06:00
|
|
|
tokens: TokenStream::empty(),
|
2016-11-14 06:00:25 -06:00
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
is_sugared_doc: false,
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2016-06-05 04:56:05 -05:00
|
|
|
}],
|
2018-03-10 08:45:47 -06:00
|
|
|
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
2017-09-26 16:04:00 -05:00
|
|
|
node: ast::ItemKind::Use(P(ast::UseTree {
|
|
|
|
prefix: ast::Path {
|
2019-05-11 09:41:37 -05:00
|
|
|
segments: iter::once(ast::Ident::with_empty_ctxt(kw::PathRoot))
|
2018-08-10 08:01:32 -05:00
|
|
|
.chain(
|
|
|
|
[name, "prelude", "v1"].iter().cloned()
|
|
|
|
.map(ast::Ident::from_str)
|
|
|
|
).map(ast::PathSegment::from_ident).collect(),
|
2017-09-26 16:04:00 -05:00
|
|
|
span,
|
|
|
|
},
|
|
|
|
kind: ast::UseTreeKind::Glob,
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2017-09-26 16:04:00 -05:00
|
|
|
})),
|
2016-06-05 04:56:05 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2019-05-11 11:08:09 -05:00
|
|
|
ident: ast::Ident::invalid(),
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2017-07-10 19:44:46 -05:00
|
|
|
tokens: None,
|
2016-06-05 04:56:05 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
krate
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|