]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/std_inject.rs
remove extern_in_paths.
[rust.git] / src / libsyntax / std_inject.rs
1 use ast;
2 use attr;
3 use std::cell::Cell;
4 use std::iter;
5 use edition::Edition;
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};
10 use ptr::P;
11 use tokenstream::TokenStream;
12
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 {
19         call_site: DUMMY_SP,
20         def_site: None,
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(),
26     });
27     sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
28 }
29
30 pub fn injected_crate_name() -> Option<&'static str> {
31     INJECTED_CRATE_NAME.with(|name| name.get())
32 }
33
34 thread_local! {
35     static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
36 }
37
38 pub fn maybe_inject_crates_ref(
39     mut krate: ast::Crate,
40     alt_std_name: Option<&str>,
41     edition: Edition,
42 ) -> ast::Crate {
43     let rust_2018 = edition >= Edition::Edition2018;
44
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") {
47         return krate;
48     } else if attr::contains_name(&krate.attrs, "no_std") {
49         if attr::contains_name(&krate.attrs, "compiler_builtins") {
50             &["core"]
51         } else {
52             &["core", "compiler_builtins"]
53         }
54     } else {
55         &["std"]
56     };
57
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.
65         if rust_2018 {
66             rename = orig_name.gensymed();
67         }
68         let orig_name = if rename != orig_name {
69             Some(orig_name)
70         } else {
71             None
72         };
73         krate.module.items.insert(0, P(ast::Item {
74             attrs: vec![attr::mk_attr_outer(DUMMY_SP,
75                                             attr::mk_attr_id(),
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,
81             span: DUMMY_SP,
82             tokens: None,
83         }));
84     }
85
86     // the crates have been injected, the assumption is that the first one is the one with
87     // the prelude.
88     let name = names[0];
89
90     INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
91
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,
100             span,
101         }],
102         vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
103         node: ast::ItemKind::Use(P(ast::UseTree {
104             prefix: ast::Path {
105                 segments: iter::once(keywords::PathRoot.ident())
106                     .chain(
107                         [name, "prelude", "v1"].iter().cloned()
108                             .map(ast::Ident::from_str)
109                     ).map(ast::PathSegment::from_ident).collect(),
110                 span,
111             },
112             kind: ast::UseTreeKind::Glob,
113             span,
114         })),
115         id: ast::DUMMY_NODE_ID,
116         ident: keywords::Invalid.ident(),
117         span,
118         tokens: None,
119     }));
120
121     krate
122 }