2019-07-18 22:29:07 +03:00
|
|
|
use syntax::{ast, attr};
|
|
|
|
use syntax::edition::Edition;
|
2019-08-13 03:34:46 +03:00
|
|
|
use syntax::ext::hygiene::MacroKind;
|
2019-07-18 22:29:07 +03:00
|
|
|
use syntax::ptr::P;
|
2019-08-13 23:56:42 +03:00
|
|
|
use syntax::source_map::{ExpnData, ExpnKind, dummy_spanned, respan};
|
2019-07-18 22:29:07 +03:00
|
|
|
use syntax::symbol::{Ident, Symbol, kw, sym};
|
2019-07-06 21:02:45 +03:00
|
|
|
use syntax_pos::DUMMY_SP;
|
2015-07-01 06:05:17 +03:00
|
|
|
|
2019-07-18 22:29:07 +03:00
|
|
|
use std::iter;
|
2013-07-19 07:38:55 +02:00
|
|
|
|
2019-07-18 22:29:07 +03:00
|
|
|
pub fn inject(
|
|
|
|
mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
|
|
|
|
) -> (ast::Crate, Option<Symbol>) {
|
2018-08-10 16:01:32 +03:00
|
|
|
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) {
|
2019-07-18 22:29:07 +03:00
|
|
|
return (krate, None);
|
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);
|
2019-08-11 02:20:18 +03:00
|
|
|
let orig_name_ident = Ident::with_dummy_span(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(
|
2019-08-11 02:20:18 +03:00
|
|
|
attr::mk_word_item(ast::Ident::with_dummy_span(sym::macro_use))
|
2019-05-17 18:37:53 +10:00
|
|
|
)],
|
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];
|
|
|
|
|
2019-08-13 23:56:42 +03:00
|
|
|
let span = DUMMY_SP.fresh_expansion(ExpnData::allow_unstable(
|
2019-07-06 21:02:45 +03:00
|
|
|
ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
|
|
|
|
[sym::prelude_import][..].into(),
|
|
|
|
));
|
|
|
|
|
2016-06-05 09:56:05 +00:00
|
|
|
krate.module.items.insert(0, P(ast::Item {
|
2019-07-30 14:49:46 -04:00
|
|
|
attrs: vec![attr::mk_attr_outer(
|
|
|
|
attr::mk_word_item(ast::Ident::new(sym::prelude_import, span)))],
|
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-08-11 02:20:18 +03:00
|
|
|
segments: iter::once(ast::Ident::with_dummy_span(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
|
|
|
}));
|
|
|
|
|
2019-07-18 22:29:07 +03:00
|
|
|
(krate, Some(Symbol::intern(name)))
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|