2019-07-18 14:29:07 -05:00
|
|
|
use syntax::{ast, attr};
|
|
|
|
use syntax::edition::Edition;
|
2019-08-25 15:03:24 -05:00
|
|
|
use syntax::ext::hygiene::AstPass;
|
|
|
|
use syntax::ext::base::Resolver;
|
2019-07-18 14:29:07 -05:00
|
|
|
use syntax::ptr::P;
|
2019-08-25 15:03:24 -05:00
|
|
|
use syntax::source_map::respan;
|
2019-07-18 14:29:07 -05:00
|
|
|
use syntax::symbol::{Ident, Symbol, kw, sym};
|
2019-07-06 13:02:45 -05:00
|
|
|
use syntax_pos::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,
|
|
|
|
resolver: &mut dyn Resolver,
|
|
|
|
alt_std_name: Option<Symbol>,
|
|
|
|
edition: Edition,
|
2019-07-18 14:29:07 -05:00
|
|
|
) -> (ast::Crate, Option<Symbol>) {
|
2018-08-10 08:01:32 -05:00
|
|
|
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-08-25 15:03:24 -05:00
|
|
|
let names: &[Symbol] = if attr::contains_name(&krate.attrs, sym::no_core) {
|
2019-07-18 14:29:07 -05:00
|
|
|
return (krate, None);
|
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) {
|
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-25 15:03:24 -05:00
|
|
|
let span = resolver.span_for_ast_pass(
|
|
|
|
DUMMY_SP,
|
|
|
|
AstPass::StdImports,
|
|
|
|
&[sym::prelude_import],
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
2018-04-16 14:28:30 -05:00
|
|
|
// .rev() to preserve ordering above in combination with insert(0, ...)
|
2019-08-25 15:03:24 -05:00
|
|
|
for &orig_name_sym in names.iter().rev() {
|
2019-05-16 19:27:17 -05:00
|
|
|
let (rename, orig_name) = if rust_2018 {
|
2019-08-25 15:03:24 -05:00
|
|
|
(Ident::new(kw::Underscore, span), Some(orig_name_sym))
|
2018-08-10 08:01:32 -05:00
|
|
|
} else {
|
2019-08-25 15:03:24 -05:00
|
|
|
(Ident::with_dummy_span(orig_name_sym), 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(
|
2019-08-25 15:03:24 -05:00
|
|
|
attr::mk_word_item(ast::Ident::new(sym::macro_use, span))
|
2019-05-17 03:37:53 -05:00
|
|
|
)],
|
2019-08-25 15:03:24 -05:00
|
|
|
vis: respan(span, 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,
|
2019-08-25 15:03:24 -05:00
|
|
|
span,
|
2018-03-30 06:06:34 -05:00
|
|
|
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];
|
|
|
|
|
2019-08-25 15:03:24 -05:00
|
|
|
let segments = if rust_2018 {
|
|
|
|
[name, sym::prelude, sym::v1].iter()
|
|
|
|
.map(|symbol| ast::PathSegment::from_ident(ast::Ident::new(*symbol, span)))
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
[kw::PathRoot, name, sym::prelude, sym::v1].iter()
|
|
|
|
.map(|symbol| ast::PathSegment::from_ident(ast::Ident::with_dummy_span(*symbol)))
|
|
|
|
.collect()
|
|
|
|
};
|
2019-07-06 13:02:45 -05:00
|
|
|
|
2019-08-25 15:03:24 -05:00
|
|
|
let use_item = P(ast::Item {
|
2019-07-30 13:49:46 -05:00
|
|
|
attrs: vec![attr::mk_attr_outer(
|
|
|
|
attr::mk_word_item(ast::Ident::new(sym::prelude_import, span)))],
|
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 {
|
2019-08-25 15:03:24 -05:00
|
|
|
prefix: ast::Path { segments, span },
|
2017-09-26 16:04:00 -05:00
|
|
|
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,
|
2019-08-25 15:03:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let prelude_import_item = if rust_2018 {
|
|
|
|
let hygienic_extern_crate = P(ast::Item {
|
|
|
|
attrs: vec![],
|
|
|
|
vis: respan(span, ast::VisibilityKind::Inherited),
|
|
|
|
node: ast::ItemKind::ExternCrate(alt_std_name),
|
|
|
|
ident: ast::Ident::new(name, span),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span,
|
|
|
|
tokens: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Use an anonymous const to hide `extern crate std as hygienic_std`
|
|
|
|
// FIXME: Once inter-crate hygiene exists, this can just be `use_item`.
|
|
|
|
P(ast::Item {
|
|
|
|
attrs: Vec::new(),
|
|
|
|
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
|
|
|
node: ast::ItemKind::Const(
|
|
|
|
P(ast::Ty {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::TyKind::Tup(Vec::new()),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
attrs: syntax::ThinVec::new(),
|
|
|
|
node: ast::ExprKind::Block(P(ast::Block {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
rules: ast::BlockCheckMode::Default,
|
|
|
|
stmts: vec![
|
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Item(use_item),
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Item(hygienic_extern_crate),
|
|
|
|
span,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
span,
|
|
|
|
}), None),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
ident: ast::Ident::new(kw::Underscore, span),
|
|
|
|
span,
|
|
|
|
tokens: None,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Have `extern crate std` at the root, so don't need to create a named
|
|
|
|
// extern crate item.
|
|
|
|
use_item
|
|
|
|
};
|
|
|
|
|
|
|
|
krate.module.items.insert(0, prelude_import_item);
|
2016-06-05 04:56:05 -05:00
|
|
|
|
2019-08-25 15:03:24 -05:00
|
|
|
(krate, Some(name))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|