2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{ExtCtxt, Resolver};
|
|
|
|
use rustc_expand::expand::ExpansionConfig;
|
2020-01-01 19:40:49 +01:00
|
|
|
use rustc_span::edition::Edition;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::hygiene::AstPass;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::DUMMY_SP;
|
2019-07-18 22:29:07 +03:00
|
|
|
use syntax::ptr::P;
|
2019-10-14 10:08:26 +02:00
|
|
|
use syntax::sess::ParseSess;
|
2019-12-22 17:42:04 -05:00
|
|
|
use syntax::{ast, attr};
|
2015-07-01 06:05:17 +03:00
|
|
|
|
2019-07-18 22:29:07 +03:00
|
|
|
pub fn inject(
|
2019-08-25 21:03:24 +01:00
|
|
|
mut krate: ast::Crate,
|
|
|
|
resolver: &mut dyn Resolver,
|
2019-09-05 15:05:58 +01:00
|
|
|
sess: &ParseSess,
|
2019-08-25 21:03:24 +01:00
|
|
|
alt_std_name: Option<Symbol>,
|
2019-07-18 22:29:07 +03:00
|
|
|
) -> (ast::Crate, Option<Symbol>) {
|
2019-09-05 15:05:58 +01:00
|
|
|
let rust_2018 = sess.edition >= Edition::Edition2018;
|
2018-08-10 16:01:32 +03:00
|
|
|
|
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-08-25 21:03:24 +01:00
|
|
|
let names: &[Symbol] = 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) {
|
2019-08-25 21:03:24 +01:00
|
|
|
&[sym::core]
|
2018-03-30 13:06:34 +02:00
|
|
|
} else {
|
2019-08-25 21:03:24 +01:00
|
|
|
&[sym::core, sym::compiler_builtins]
|
2018-03-30 13:06:34 +02:00
|
|
|
}
|
2017-12-12 11:57:58 -08:00
|
|
|
} else {
|
2019-08-25 21:03:24 +01:00
|
|
|
&[sym::std]
|
2016-09-28 22:28:19 +00:00
|
|
|
};
|
2013-08-29 12:10:02 -07:00
|
|
|
|
2019-08-28 12:41:29 +03:00
|
|
|
let expn_id = resolver.expansion_for_ast_pass(
|
2019-08-25 21:03:24 +01:00
|
|
|
DUMMY_SP,
|
|
|
|
AstPass::StdImports,
|
|
|
|
&[sym::prelude_import],
|
|
|
|
None,
|
|
|
|
);
|
2019-08-28 12:41:29 +03:00
|
|
|
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
|
|
|
|
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id);
|
2019-08-25 21:03:24 +01:00
|
|
|
|
2019-09-05 15:05:58 +01:00
|
|
|
let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
|
|
|
|
let cx = ExtCtxt::new(sess, ecfg, resolver);
|
|
|
|
|
2018-04-16 12:28:30 -07:00
|
|
|
// .rev() to preserve ordering above in combination with insert(0, ...)
|
2019-09-05 15:05:58 +01:00
|
|
|
for &name in names.iter().rev() {
|
2019-12-22 17:42:04 -05:00
|
|
|
let ident = if rust_2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
|
|
|
|
krate.module.items.insert(
|
|
|
|
0,
|
|
|
|
cx.item(
|
|
|
|
span,
|
|
|
|
ident,
|
|
|
|
vec![cx.attribute(cx.meta_word(span, sym::macro_use))],
|
|
|
|
ast::ItemKind::ExternCrate(alt_std_name),
|
|
|
|
),
|
|
|
|
);
|
2018-03-30 13:06:34 +02:00
|
|
|
}
|
2017-12-12 11:57:58 -08:00
|
|
|
|
2019-09-05 15:05:58 +01:00
|
|
|
// The crates have been injected, the assumption is that the first one is
|
|
|
|
// the one with the prelude.
|
2018-03-30 13:06:34 +02:00
|
|
|
let name = names[0];
|
|
|
|
|
2019-09-05 15:05:58 +01:00
|
|
|
let import_path = if rust_2018 {
|
2019-12-22 17:42:04 -05:00
|
|
|
[name, sym::prelude, sym::v1].iter().map(|symbol| ast::Ident::new(*symbol, span)).collect()
|
2019-08-25 21:03:24 +01:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
[kw::PathRoot, name, sym::prelude, sym::v1]
|
|
|
|
.iter()
|
|
|
|
.map(|symbol| ast::Ident::new(*symbol, span))
|
|
|
|
.collect()
|
2019-08-25 21:03:24 +01:00
|
|
|
};
|
2019-07-06 21:02:45 +03:00
|
|
|
|
2019-09-05 15:05:58 +01:00
|
|
|
let use_item = cx.item(
|
|
|
|
span,
|
|
|
|
ast::Ident::invalid(),
|
|
|
|
vec![cx.attribute(cx.meta_word(span, sym::prelude_import))],
|
|
|
|
ast::ItemKind::Use(P(ast::UseTree {
|
|
|
|
prefix: cx.path(span, import_path),
|
2017-09-26 23:04:00 +02:00
|
|
|
kind: ast::UseTreeKind::Glob,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2017-09-26 23:04:00 +02:00
|
|
|
})),
|
2019-09-05 15:05:58 +01:00
|
|
|
);
|
2019-08-25 21:03:24 +01:00
|
|
|
|
2019-09-05 15:05:58 +01:00
|
|
|
krate.module.items.insert(0, use_item);
|
2016-06-05 09:56:05 +00:00
|
|
|
|
2019-08-25 21:03:24 +01:00
|
|
|
(krate, Some(name))
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|