2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-06-27 15:51:28 -05:00
|
|
|
use rustc_expand::base::{ExtCtxt, ResolverExpand};
|
2019-12-29 08:23:55 -06:00
|
|
|
use rustc_expand::expand::ExpansionConfig;
|
2020-07-29 20:27:50 -05:00
|
|
|
use rustc_session::Session;
|
2021-02-17 06:34:58 -06:00
|
|
|
use rustc_span::edition::Edition::*;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::hygiene::AstPass;
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::DUMMY_SP;
|
2015-06-30 22:05:17 -05:00
|
|
|
|
2019-07-18 14:29:07 -05:00
|
|
|
pub fn inject(
|
2019-08-25 15:03:24 -05:00
|
|
|
mut krate: ast::Crate,
|
2020-06-27 15:51:28 -05:00
|
|
|
resolver: &mut dyn ResolverExpand,
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &Session,
|
2020-11-12 15:35:46 -06:00
|
|
|
) -> ast::Crate {
|
2021-02-17 06:34:58 -06:00
|
|
|
let edition = sess.parse_sess.edition;
|
2018-08-10 08:01:32 -05:00
|
|
|
|
2018-03-30 06:06:34 -05:00
|
|
|
// the first name in this list is the crate name of the crate with the prelude
|
2020-07-29 20:27:50 -05:00
|
|
|
let names: &[Symbol] = if sess.contains_name(&krate.attrs, sym::no_core) {
|
2020-11-12 15:35:46 -06:00
|
|
|
return krate;
|
2020-07-29 20:27:50 -05:00
|
|
|
} else if sess.contains_name(&krate.attrs, sym::no_std) {
|
|
|
|
if sess.contains_name(&krate.attrs, sym::compiler_builtins) {
|
2019-08-25 15:03:24 -05:00
|
|
|
&[sym::core]
|
2018-03-30 06:06:34 -05:00
|
|
|
} else {
|
2019-08-25 15:03:24 -05:00
|
|
|
&[sym::core, sym::compiler_builtins]
|
2018-03-30 06:06:34 -05:00
|
|
|
}
|
2017-12-12 13:57:58 -06:00
|
|
|
} else {
|
2019-08-25 15:03:24 -05:00
|
|
|
&[sym::std]
|
2016-09-28 17:28:19 -05:00
|
|
|
};
|
2013-08-29 14:10:02 -05:00
|
|
|
|
2019-08-28 04:41:29 -05:00
|
|
|
let expn_id = resolver.expansion_for_ast_pass(
|
2019-08-25 15:03:24 -05:00
|
|
|
DUMMY_SP,
|
|
|
|
AstPass::StdImports,
|
|
|
|
&[sym::prelude_import],
|
|
|
|
None,
|
|
|
|
);
|
2021-06-25 13:43:04 -05:00
|
|
|
let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
|
|
|
|
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id.to_expn_id());
|
2019-08-25 15:03:24 -05:00
|
|
|
|
2019-09-05 09:05:58 -05:00
|
|
|
let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
|
2020-03-15 18:43:37 -05:00
|
|
|
let cx = ExtCtxt::new(sess, ecfg, resolver, None);
|
2019-09-05 09:05:58 -05:00
|
|
|
|
2018-04-16 14:28:30 -05:00
|
|
|
// .rev() to preserve ordering above in combination with insert(0, ...)
|
2019-09-05 09:05:58 -05:00
|
|
|
for &name in names.iter().rev() {
|
2021-02-17 06:34:58 -06:00
|
|
|
let ident = if edition >= Edition2018 {
|
|
|
|
Ident::new(name, span)
|
|
|
|
} else {
|
|
|
|
Ident::new(name, call_site)
|
|
|
|
};
|
2021-02-14 12:14:12 -06:00
|
|
|
krate.items.insert(
|
2019-09-05 09:05:58 -05:00
|
|
|
0,
|
|
|
|
cx.item(
|
2019-08-25 15:03:24 -05:00
|
|
|
span,
|
2019-09-05 09:05:58 -05:00
|
|
|
ident,
|
|
|
|
vec![cx.attribute(cx.meta_word(span, sym::macro_use))],
|
2022-01-14 07:25:52 -06:00
|
|
|
ast::ItemKind::ExternCrate(None),
|
2019-12-22 16:42:04 -06:00
|
|
|
),
|
2019-09-05 09:05:58 -05:00
|
|
|
);
|
2018-03-30 06:06:34 -05:00
|
|
|
}
|
2017-12-12 13:57:58 -06:00
|
|
|
|
2019-09-05 09:05:58 -05:00
|
|
|
// The crates have been injected, the assumption is that the first one is
|
|
|
|
// the one with the prelude.
|
2018-03-30 06:06:34 -05:00
|
|
|
let name = names[0];
|
|
|
|
|
2021-02-17 06:34:58 -06:00
|
|
|
let root = (edition == Edition2015).then(|| kw::PathRoot);
|
|
|
|
|
|
|
|
let import_path = root
|
|
|
|
.iter()
|
|
|
|
.chain(&[name, sym::prelude])
|
|
|
|
.chain(&[match edition {
|
|
|
|
Edition2015 => sym::rust_2015,
|
|
|
|
Edition2018 => sym::rust_2018,
|
|
|
|
Edition2021 => sym::rust_2021,
|
2022-02-28 17:13:24 -06:00
|
|
|
Edition2024 => sym::rust_2024,
|
2021-02-17 06:34:58 -06:00
|
|
|
}])
|
|
|
|
.map(|&symbol| Ident::new(symbol, span))
|
|
|
|
.collect();
|
2019-07-06 13:02:45 -05:00
|
|
|
|
2019-09-05 09:05:58 -05:00
|
|
|
let use_item = cx.item(
|
|
|
|
span,
|
2021-10-17 15:20:30 -05:00
|
|
|
Ident::empty(),
|
2019-09-05 09:05:58 -05:00
|
|
|
vec![cx.attribute(cx.meta_word(span, sym::prelude_import))],
|
2021-02-20 03:51:26 -06:00
|
|
|
ast::ItemKind::Use(ast::UseTree {
|
2019-09-05 09:05:58 -05:00
|
|
|
prefix: cx.path(span, import_path),
|
2017-09-26 16:04:00 -05:00
|
|
|
kind: ast::UseTreeKind::Glob,
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2021-02-20 03:51:26 -06:00
|
|
|
}),
|
2019-09-05 09:05:58 -05:00
|
|
|
);
|
2019-08-25 15:03:24 -05:00
|
|
|
|
2021-02-14 12:14:12 -06:00
|
|
|
krate.items.insert(0, use_item);
|
2016-06-05 04:56:05 -05:00
|
|
|
|
2020-11-12 15:35:46 -06:00
|
|
|
krate
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|