6 use ext::hygiene::{Mark, SyntaxContext};
7 use symbol::{Symbol, keywords};
8 use syntax_pos::{DUMMY_SP, Span};
9 use source_map::{ExpnInfo, MacroAttribute, dummy_spanned, hygiene, respan};
11 use tokenstream::TokenStream;
13 /// Craft a span that will be ignored by the stability lint's
14 /// call to source_map's `is_internal` check.
15 /// The expanded code uses the unstable `#[prelude_import]` attribute.
16 fn ignored_span(sp: Span) -> Span {
17 let mark = Mark::fresh(Mark::root());
18 mark.set_expn_info(ExpnInfo {
21 format: MacroAttribute(Symbol::intern("std_inject")),
22 allow_internal_unstable: true,
23 allow_internal_unsafe: false,
24 local_inner_macros: false,
25 edition: hygiene::default_edition(),
27 sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
30 pub fn injected_crate_name() -> Option<&'static str> {
31 INJECTED_CRATE_NAME.with(|name| name.get())
35 static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
38 pub fn maybe_inject_crates_ref(
39 mut krate: ast::Crate,
40 alt_std_name: Option<&str>,
43 let rust_2018 = edition >= Edition::Edition2018;
45 // the first name in this list is the crate name of the crate with the prelude
46 let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
48 } else if attr::contains_name(&krate.attrs, "no_std") {
49 if attr::contains_name(&krate.attrs, "compiler_builtins") {
52 &["core", "compiler_builtins"]
58 // .rev() to preserve ordering above in combination with insert(0, ...)
59 let alt_std_name = alt_std_name.map(Symbol::intern);
60 for orig_name in names.iter().rev() {
61 let orig_name = Symbol::intern(orig_name);
62 let mut rename = orig_name;
63 // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
64 // so they don't accidentally interfere with the new import paths.
66 rename = orig_name.gensymed();
68 let orig_name = if rename != orig_name {
73 krate.module.items.insert(0, P(ast::Item {
74 attrs: vec![attr::mk_attr_outer(DUMMY_SP,
76 attr::mk_word_item(ast::Ident::from_str("macro_use")))],
77 vis: dummy_spanned(ast::VisibilityKind::Inherited),
78 node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
79 ident: ast::Ident::with_empty_ctxt(rename),
80 id: ast::DUMMY_NODE_ID,
86 // the crates have been injected, the assumption is that the first one is the one with
90 INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
92 let span = ignored_span(DUMMY_SP);
93 krate.module.items.insert(0, P(ast::Item {
94 attrs: vec![ast::Attribute {
95 style: ast::AttrStyle::Outer,
96 path: ast::Path::from_ident(ast::Ident::new(Symbol::intern("prelude_import"), span)),
97 tokens: TokenStream::empty(),
98 id: attr::mk_attr_id(),
99 is_sugared_doc: false,
102 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
103 node: ast::ItemKind::Use(P(ast::UseTree {
105 segments: iter::once(keywords::PathRoot.ident())
107 [name, "prelude", "v1"].iter().cloned()
108 .map(ast::Ident::from_str)
109 ).map(ast::PathSegment::from_ident).collect(),
112 kind: ast::UseTreeKind::Glob,
115 id: ast::DUMMY_NODE_ID,
116 ident: keywords::Invalid.ident(),