3 use crate::edition::Edition;
4 use crate::ext::hygiene::{Mark, SyntaxContext};
5 use crate::symbol::{Ident, Symbol, kw, sym};
6 use crate::source_map::{ExpnInfo, MacroAttribute, dummy_spanned, respan};
8 use crate::tokenstream::TokenStream;
12 use syntax_pos::{DUMMY_SP, Span};
14 /// Craft a span that will be ignored by the stability lint's
15 /// call to source_map's `is_internal` check.
16 /// The expanded code uses the unstable `#[prelude_import]` attribute.
17 fn ignored_span(sp: Span, edition: Edition) -> Span {
18 let mark = Mark::fresh(Mark::root());
19 mark.set_expn_info(ExpnInfo {
22 format: MacroAttribute(Symbol::intern("std_inject")),
23 allow_internal_unstable: Some(vec![sym::prelude_import].into()),
24 allow_internal_unsafe: false,
25 local_inner_macros: false,
28 sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
31 pub fn injected_crate_name() -> Option<&'static str> {
32 INJECTED_CRATE_NAME.with(|name| name.get())
36 // A `Symbol` might make more sense here, but it doesn't work, probably for
37 // reasons relating to the use of thread-local storage for the Symbol
39 static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
42 pub fn maybe_inject_crates_ref(
43 mut krate: ast::Crate,
44 alt_std_name: Option<&str>,
47 let rust_2018 = edition >= Edition::Edition2018;
49 // the first name in this list is the crate name of the crate with the prelude
50 let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
52 } else if attr::contains_name(&krate.attrs, sym::no_std) {
53 if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
56 &["core", "compiler_builtins"]
62 // .rev() to preserve ordering above in combination with insert(0, ...)
63 let alt_std_name = alt_std_name.map(Symbol::intern);
64 for orig_name_str in names.iter().rev() {
65 // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
66 // so they don't accidentally interfere with the new import paths.
67 let orig_name_sym = Symbol::intern(orig_name_str);
68 let orig_name_ident = Ident::with_empty_ctxt(orig_name_sym);
69 let (rename, orig_name) = if rust_2018 {
70 (orig_name_ident.gensym(), Some(orig_name_sym))
72 (orig_name_ident, None)
74 krate.module.items.insert(0, P(ast::Item {
75 attrs: vec![attr::mk_attr_outer(
78 attr::mk_word_item(ast::Ident::with_empty_ctxt(sym::macro_use))
80 vis: dummy_spanned(ast::VisibilityKind::Inherited),
81 node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
83 id: ast::DUMMY_NODE_ID,
89 // the crates have been injected, the assumption is that the first one is the one with
93 INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
95 let span = ignored_span(DUMMY_SP, edition);
96 krate.module.items.insert(0, P(ast::Item {
97 attrs: vec![ast::Attribute {
98 style: ast::AttrStyle::Outer,
99 path: ast::Path::from_ident(ast::Ident::new(sym::prelude_import, span)),
100 tokens: TokenStream::empty(),
101 id: attr::mk_attr_id(),
102 is_sugared_doc: false,
105 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
106 node: ast::ItemKind::Use(P(ast::UseTree {
108 segments: iter::once(ast::Ident::with_empty_ctxt(kw::PathRoot))
110 [name, "prelude", "v1"].iter().cloned()
111 .map(ast::Ident::from_str)
112 ).map(ast::PathSegment::from_ident).collect(),
115 kind: ast::UseTreeKind::Glob,
118 id: ast::DUMMY_NODE_ID,
119 ident: ast::Ident::invalid(),