]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/standard_library_imports.rs
Auto merge of #63937 - Nashenas88:rustdoc_57180, r=GuillaumeGomez
[rust.git] / src / libsyntax_ext / standard_library_imports.rs
1 use syntax::{ast, attr};
2 use syntax::edition::Edition;
3 use syntax::ext::expand::ExpansionConfig;
4 use syntax::ext::hygiene::AstPass;
5 use syntax::ext::base::{ExtCtxt, Resolver};
6 use syntax::parse::ParseSess;
7 use syntax::ptr::P;
8 use syntax::symbol::{Ident, Symbol, kw, sym};
9 use syntax_pos::DUMMY_SP;
10
11 pub fn inject(
12     mut krate: ast::Crate,
13     resolver: &mut dyn Resolver,
14     sess: &ParseSess,
15     alt_std_name: Option<Symbol>,
16 ) -> (ast::Crate, Option<Symbol>) {
17     let rust_2018 = sess.edition >= Edition::Edition2018;
18
19     // the first name in this list is the crate name of the crate with the prelude
20     let names: &[Symbol] = if attr::contains_name(&krate.attrs, sym::no_core) {
21         return (krate, None);
22     } else if attr::contains_name(&krate.attrs, sym::no_std) {
23         if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
24             &[sym::core]
25         } else {
26             &[sym::core, sym::compiler_builtins]
27         }
28     } else {
29         &[sym::std]
30     };
31
32     let expn_id = resolver.expansion_for_ast_pass(
33         DUMMY_SP,
34         AstPass::StdImports,
35         &[sym::prelude_import],
36         None,
37     );
38     let span = DUMMY_SP.with_def_site_ctxt(expn_id);
39     let call_site = DUMMY_SP.with_call_site_ctxt(expn_id);
40
41     let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
42     let cx = ExtCtxt::new(sess, ecfg, resolver);
43
44
45     // .rev() to preserve ordering above in combination with insert(0, ...)
46     for &name in names.iter().rev() {
47         let ident = if rust_2018 {
48             Ident::new(name, span)
49         } else {
50             Ident::new(name, call_site)
51         };
52         krate.module.items.insert(0, cx.item(
53             span,
54             ident,
55             vec![cx.attribute(cx.meta_word(span, sym::macro_use))],
56             ast::ItemKind::ExternCrate(alt_std_name),
57         ));
58     }
59
60     // The crates have been injected, the assumption is that the first one is
61     // the one with the prelude.
62     let name = names[0];
63
64     let import_path = if rust_2018 {
65         [name, sym::prelude, sym::v1].iter()
66             .map(|symbol| ast::Ident::new(*symbol, span)).collect()
67     } else {
68         [kw::PathRoot, name, sym::prelude, sym::v1].iter()
69             .map(|symbol| ast::Ident::new(*symbol, span)).collect()
70     };
71
72     let use_item = cx.item(
73         span,
74         ast::Ident::invalid(),
75         vec![cx.attribute(cx.meta_word(span, sym::prelude_import))],
76         ast::ItemKind::Use(P(ast::UseTree {
77             prefix: cx.path(span, import_path),
78             kind: ast::UseTreeKind::Glob,
79             span,
80         })),
81     );
82
83     krate.module.items.insert(0, use_item);
84
85     (krate, Some(name))
86 }