]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/standard_library_imports.rs
c004124865223df761c3d78511e377fcd0122fa8
[rust.git] / src / libsyntax_ext / standard_library_imports.rs
1 use syntax::{ast, attr};
2 use syntax::edition::Edition;
3 use syntax::ext::hygiene::MacroKind;
4 use syntax::ptr::P;
5 use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
6 use syntax::symbol::{Ident, Symbol, kw, sym};
7 use syntax_pos::DUMMY_SP;
8
9 use std::iter;
10
11 pub fn inject(
12     mut krate: ast::Crate, alt_std_name: Option<&str>, edition: Edition
13 ) -> (ast::Crate, Option<Symbol>) {
14     let rust_2018 = edition >= Edition::Edition2018;
15
16     // the first name in this list is the crate name of the crate with the prelude
17     let names: &[&str] = if attr::contains_name(&krate.attrs, sym::no_core) {
18         return (krate, None);
19     } else if attr::contains_name(&krate.attrs, sym::no_std) {
20         if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
21             &["core"]
22         } else {
23             &["core", "compiler_builtins"]
24         }
25     } else {
26         &["std"]
27     };
28
29     // .rev() to preserve ordering above in combination with insert(0, ...)
30     let alt_std_name = alt_std_name.map(Symbol::intern);
31     for orig_name_str in names.iter().rev() {
32         // HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
33         // so they don't accidentally interfere with the new import paths.
34         let orig_name_sym = Symbol::intern(orig_name_str);
35         let orig_name_ident = Ident::with_dummy_span(orig_name_sym);
36         let (rename, orig_name) = if rust_2018 {
37             (orig_name_ident.gensym(), Some(orig_name_sym))
38         } else {
39             (orig_name_ident, None)
40         };
41         krate.module.items.insert(0, P(ast::Item {
42             attrs: vec![attr::mk_attr_outer(
43                 attr::mk_word_item(ast::Ident::with_dummy_span(sym::macro_use))
44             )],
45             vis: dummy_spanned(ast::VisibilityKind::Inherited),
46             node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
47             ident: rename,
48             id: ast::DUMMY_NODE_ID,
49             span: DUMMY_SP,
50             tokens: None,
51         }));
52     }
53
54     // the crates have been injected, the assumption is that the first one is the one with
55     // the prelude.
56     let name = names[0];
57
58     let span = DUMMY_SP.fresh_expansion(ExpnInfo::allow_unstable(
59         ExpnKind::Macro(MacroKind::Attr, sym::std_inject), DUMMY_SP, edition,
60         [sym::prelude_import][..].into(),
61     ));
62
63     krate.module.items.insert(0, P(ast::Item {
64         attrs: vec![attr::mk_attr_outer(
65             attr::mk_word_item(ast::Ident::new(sym::prelude_import, span)))],
66         vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
67         node: ast::ItemKind::Use(P(ast::UseTree {
68             prefix: ast::Path {
69                 segments: iter::once(ast::Ident::with_dummy_span(kw::PathRoot))
70                     .chain(
71                         [name, "prelude", "v1"].iter().cloned()
72                             .map(ast::Ident::from_str)
73                     ).map(ast::PathSegment::from_ident).collect(),
74                 span,
75             },
76             kind: ast::UseTreeKind::Glob,
77             span,
78         })),
79         id: ast::DUMMY_NODE_ID,
80         ident: ast::Ident::invalid(),
81         span,
82         tokens: None,
83     }));
84
85     (krate, Some(Symbol::intern(name)))
86 }