]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
Auto merge of #57760 - dlrobertson:varargs1, r=alexreg
[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::{Mark, SyntaxContext};
5 use crate::symbol::{Symbol, keywords};
6 use crate::source_map::{ExpnInfo, MacroAttribute, dummy_spanned, hygiene, 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, Span};
13
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) -> Span {
18     let mark = Mark::fresh(Mark::root());
19     mark.set_expn_info(ExpnInfo {
20         call_site: DUMMY_SP,
21         def_site: None,
22         format: MacroAttribute(Symbol::intern("std_inject")),
23         allow_internal_unstable: Some(vec![
24             Symbol::intern("prelude_import"),
25         ].into()),
26         allow_internal_unsafe: false,
27         local_inner_macros: false,
28         edition: hygiene::default_edition(),
29     });
30     sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
31 }
32
33 pub fn injected_crate_name() -> Option<&'static str> {
34     INJECTED_CRATE_NAME.with(|name| name.get())
35 }
36
37 thread_local! {
38     static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
39 }
40
41 pub fn maybe_inject_crates_ref(
42     mut krate: ast::Crate,
43     alt_std_name: Option<&str>,
44     edition: Edition,
45 ) -> ast::Crate {
46     let rust_2018 = edition >= Edition::Edition2018;
47
48     // the first name in this list is the crate name of the crate with the prelude
49     let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
50         return krate;
51     } else if attr::contains_name(&krate.attrs, "no_std") {
52         if attr::contains_name(&krate.attrs, "compiler_builtins") {
53             &["core"]
54         } else {
55             &["core", "compiler_builtins"]
56         }
57     } else {
58         &["std"]
59     };
60
61     // .rev() to preserve ordering above in combination with insert(0, ...)
62     let alt_std_name = alt_std_name.map(Symbol::intern);
63     for orig_name in names.iter().rev() {
64         let orig_name = Symbol::intern(orig_name);
65         let mut rename = orig_name;
66         // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
67         // so they don't accidentally interfere with the new import paths.
68         if rust_2018 {
69             rename = orig_name.gensymed();
70         }
71         let orig_name = if rename != orig_name {
72             Some(orig_name)
73         } else {
74             None
75         };
76         krate.module.items.insert(0, P(ast::Item {
77             attrs: vec![attr::mk_attr_outer(DUMMY_SP,
78                                             attr::mk_attr_id(),
79                                             attr::mk_word_item(ast::Ident::from_str("macro_use")))],
80             vis: dummy_spanned(ast::VisibilityKind::Inherited),
81             node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
82             ident: ast::Ident::with_empty_ctxt(rename),
83             id: ast::DUMMY_NODE_ID,
84             span: DUMMY_SP,
85             tokens: None,
86         }));
87     }
88
89     // the crates have been injected, the assumption is that the first one is the one with
90     // the prelude.
91     let name = names[0];
92
93     INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
94
95     let span = ignored_span(DUMMY_SP);
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(Symbol::intern("prelude_import"), span)),
100             tokens: TokenStream::empty(),
101             id: attr::mk_attr_id(),
102             is_sugared_doc: false,
103             span,
104         }],
105         vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
106         node: ast::ItemKind::Use(P(ast::UseTree {
107             prefix: ast::Path {
108                 segments: iter::once(keywords::PathRoot.ident())
109                     .chain(
110                         [name, "prelude", "v1"].iter().cloned()
111                             .map(ast::Ident::from_str)
112                     ).map(ast::PathSegment::from_ident).collect(),
113                 span,
114             },
115             kind: ast::UseTreeKind::Glob,
116             span,
117         })),
118         id: ast::DUMMY_NODE_ID,
119         ident: keywords::Invalid.ident(),
120         span,
121         tokens: None,
122     }));
123
124     krate
125 }