]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
Rollup merge of #51765 - jonas-schievink:patch-1, r=KodrAus
[rust.git] / src / libsyntax / std_inject.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast;
12 use attr;
13 use std::cell::Cell;
14 use ext::hygiene::{Mark, SyntaxContext};
15 use symbol::{Symbol, keywords};
16 use syntax_pos::{DUMMY_SP, Span};
17 use codemap::{ExpnInfo, MacroAttribute, dummy_spanned, hygiene, respan};
18 use ptr::P;
19 use tokenstream::TokenStream;
20
21 /// Craft a span that will be ignored by the stability lint's
22 /// call to codemap's `is_internal` check.
23 /// The expanded code uses the unstable `#[prelude_import]` attribute.
24 fn ignored_span(sp: Span) -> Span {
25     let mark = Mark::fresh(Mark::root());
26     mark.set_expn_info(ExpnInfo {
27         call_site: DUMMY_SP,
28         def_site: None,
29         format: MacroAttribute(Symbol::intern("std_inject")),
30         allow_internal_unstable: true,
31         allow_internal_unsafe: false,
32         local_inner_macros: false,
33         edition: hygiene::default_edition(),
34     });
35     sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
36 }
37
38 pub fn injected_crate_name() -> Option<&'static str> {
39     INJECTED_CRATE_NAME.with(|name| name.get())
40 }
41
42 thread_local! {
43     static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
44 }
45
46 pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
47     // the first name in this list is the crate name of the crate with the prelude
48     let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
49         return krate;
50     } else if attr::contains_name(&krate.attrs, "no_std") {
51         if attr::contains_name(&krate.attrs, "compiler_builtins") {
52             &["core"]
53         } else {
54             &["core", "compiler_builtins"]
55         }
56     } else {
57         &["std"]
58     };
59
60     // .rev() to preserve ordering above in combination with insert(0, ...)
61     for name in names.iter().rev() {
62         krate.module.items.insert(0, P(ast::Item {
63             attrs: vec![attr::mk_attr_outer(DUMMY_SP,
64                                             attr::mk_attr_id(),
65                                             attr::mk_word_item(ast::Ident::from_str("macro_use")))],
66             vis: dummy_spanned(ast::VisibilityKind::Inherited),
67             node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
68             ident: ast::Ident::from_str(name),
69             id: ast::DUMMY_NODE_ID,
70             span: DUMMY_SP,
71             tokens: None,
72         }));
73     }
74
75     // the crates have been injected, the assumption is that the first one is the one with
76     // the prelude.
77     let name = names[0];
78
79     INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
80
81     let span = ignored_span(DUMMY_SP);
82     krate.module.items.insert(0, P(ast::Item {
83         attrs: vec![ast::Attribute {
84             style: ast::AttrStyle::Outer,
85             path: ast::Path::from_ident(ast::Ident::new(Symbol::intern("prelude_import"), span)),
86             tokens: TokenStream::empty(),
87             id: attr::mk_attr_id(),
88             is_sugared_doc: false,
89             span,
90         }],
91         vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
92         node: ast::ItemKind::Use(P(ast::UseTree {
93             prefix: ast::Path {
94                 segments: [name, "prelude", "v1"].into_iter().map(|name| {
95                     ast::PathSegment::from_ident(ast::Ident::from_str(name))
96                 }).collect(),
97                 span,
98             },
99             kind: ast::UseTreeKind::Glob,
100             span,
101         })),
102         id: ast::DUMMY_NODE_ID,
103         ident: keywords::Invalid.ident(),
104         span,
105         tokens: None,
106     }));
107
108     krate
109 }