1 use syntax::{ast, attr};
2 use syntax::edition::Edition;
3 use syntax::ext::hygiene::MacroKind;
5 use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
6 use syntax::symbol::{Ident, Symbol, kw, sym};
7 use syntax_pos::DUMMY_SP;
12 mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
13 ) -> (ast::Crate, Option<Symbol>) {
14 let rust_2018 = edition >= Edition::Edition2018;
16 // the first name in this list is the crate name of the crate with the prelude
17 let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
19 } else if attr::contains_name(&krate.attrs, sym::no_std) {
20 if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
23 &["core", "compiler_builtins"]
29 // .rev() to preserve ordering above in combination with insert(0, ...)
30 let alt_std_name = alt_std_name.map(Symbol::intern);
31 for orig_name_str in names.iter().rev() {
32 // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
33 // so they don't accidentally interfere with the new import paths.
34 let orig_name_sym = Symbol::intern(orig_name_str);
35 let orig_name_ident = Ident::with_dummy_span(orig_name_sym);
36 let (rename, orig_name) = if rust_2018 {
37 (orig_name_ident.gensym(), Some(orig_name_sym))
39 (orig_name_ident, None)
41 krate.module.items.insert(0, P(ast::Item {
42 attrs: vec![attr::mk_attr_outer(
43 attr::mk_word_item(ast::Ident::with_dummy_span(sym::macro_use))
45 vis: dummy_spanned(ast::VisibilityKind::Inherited),
46 node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
48 id: ast::DUMMY_NODE_ID,
54 // the crates have been injected, the assumption is that the first one is the one with
58 let span = DUMMY_SP.fresh_expansion(ExpnInfo::allow_unstable(
59 ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
60 [sym::prelude_import][..].into(),
63 krate.module.items.insert(0, P(ast::Item {
64 attrs: vec![attr::mk_attr_outer(
65 attr::mk_word_item(ast::Ident::new(sym::prelude_import, span)))],
66 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
67 node: ast::ItemKind::Use(P(ast::UseTree {
69 segments: iter::once(ast::Ident::with_dummy_span(kw::PathRoot))
71 [name, "prelude", "v1"].iter().cloned()
72 .map(ast::Ident::from_str)
73 ).map(ast::PathSegment::from_ident).collect(),
76 kind: ast::UseTreeKind::Glob,
79 id: ast::DUMMY_NODE_ID,
80 ident: ast::Ident::invalid(),
85 (krate, Some(Symbol::intern(name)))