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