]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
Simplify SaveHandler trait
[rust.git] / src / libsyntax / std_inject.rs
1 use crate::ast;
2 use crate::attr;
3 use crate::edition::Edition;
4 use crate::ext::hygiene::{ExpnId, MacroKind};
5 use crate::symbol::{Ident, Symbol, kw, sym};
6 use crate::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
7 use crate::ptr::P;
8 use crate::tokenstream::TokenStream;
9
10 use std::cell::Cell;
11 use std::iter;
12 use syntax_pos::DUMMY_SP;
13
14 pub fn injected_crate_name() -> Option<&'static str> {
15     INJECTED_CRATE_NAME.with(|name| name.get())
16 }
17
18 thread_local! {
19     // A `Symbol` might make more sense here, but it doesn't work, probably for
20     // reasons relating to the use of thread-local storage for the Symbol
21     // interner.
22     static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
23 }
24
25 pub fn maybe_inject_crates_ref(
26     mut krate: ast::Crate,
27     alt_std_name: Option<&str>,
28     edition: Edition,
29 ) -> ast::Crate {
30     let rust_2018 = edition >= Edition::Edition2018;
31
32     // the first name in this list is the crate name of the crate with the prelude
33     let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
34         return krate;
35     } else if attr::contains_name(&krate.attrs, sym::no_std) {
36         if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
37             &["core"]
38         } else {
39             &["core", "compiler_builtins"]
40         }
41     } else {
42         &["std"]
43     };
44
45     // .rev() to preserve ordering above in combination with insert(0, ...)
46     let alt_std_name = alt_std_name.map(Symbol::intern);
47     for orig_name_str in names.iter().rev() {
48         // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
49         // so they don't accidentally interfere with the new import paths.
50         let orig_name_sym = Symbol::intern(orig_name_str);
51         let orig_name_ident = Ident::with_empty_ctxt(orig_name_sym);
52         let (rename, orig_name) = if rust_2018 {
53             (orig_name_ident.gensym(), Some(orig_name_sym))
54         } else {
55             (orig_name_ident, None)
56         };
57         krate.module.items.insert(0, P(ast::Item {
58             attrs: vec![attr::mk_attr_outer(
59                 DUMMY_SP,
60                 attr::mk_attr_id(),
61                 attr::mk_word_item(ast::Ident::with_empty_ctxt(sym::macro_use))
62             )],
63             vis: dummy_spanned(ast::VisibilityKind::Inherited),
64             node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
65             ident: rename,
66             id: ast::DUMMY_NODE_ID,
67             span: DUMMY_SP,
68             tokens: None,
69         }));
70     }
71
72     // the crates have been injected, the assumption is that the first one is the one with
73     // the prelude.
74     let name = names[0];
75
76     INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
77
78     let span = DUMMY_SP.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable(
79         ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
80         [sym::prelude_import][..].into(),
81     ));
82
83     krate.module.items.insert(0, P(ast::Item {
84         attrs: vec![ast::Attribute {
85             style: ast::AttrStyle::Outer,
86             path: ast::Path::from_ident(ast::Ident::new(sym::prelude_import, span)),
87             tokens: TokenStream::empty(),
88             id: attr::mk_attr_id(),
89             is_sugared_doc: false,
90             span,
91         }],
92         vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
93         node: ast::ItemKind::Use(P(ast::UseTree {
94             prefix: ast::Path {
95                 segments: iter::once(ast::Ident::with_empty_ctxt(kw::PathRoot))
96                     .chain(
97                         [name, "prelude", "v1"].iter().cloned()
98                             .map(ast::Ident::from_str)
99                     ).map(ast::PathSegment::from_ident).collect(),
100                 span,
101             },
102             kind: ast::UseTreeKind::Glob,
103             span,
104         })),
105         id: ast::DUMMY_NODE_ID,
106         ident: ast::Ident::invalid(),
107         span,
108         tokens: None,
109     }));
110
111     krate
112 }