]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/standard_library_imports.rs
Rollup merge of #90098 - GuillaumeGomez:add-test-foreign-impl-missing-doc-code-exampl...
[rust.git] / compiler / rustc_builtin_macros / src / standard_library_imports.rs
1 use rustc_ast as ast;
2 use rustc_expand::base::{ExtCtxt, ResolverExpand};
3 use rustc_expand::expand::ExpansionConfig;
4 use rustc_session::Session;
5 use rustc_span::edition::Edition::*;
6 use rustc_span::hygiene::AstPass;
7 use rustc_span::symbol::{kw, sym, Ident, Symbol};
8 use rustc_span::DUMMY_SP;
9
10 pub fn inject(
11     mut krate: ast::Crate,
12     resolver: &mut dyn ResolverExpand,
13     sess: &Session,
14     alt_std_name: Option<Symbol>,
15 ) -> ast::Crate {
16     let edition = sess.parse_sess.edition;
17
18     // the first name in this list is the crate name of the crate with the prelude
19     let names: &[Symbol] = if sess.contains_name(&krate.attrs, sym::no_core) {
20         return krate;
21     } else if sess.contains_name(&krate.attrs, sym::no_std) {
22         if sess.contains_name(&krate.attrs, sym::compiler_builtins) {
23             &[sym::core]
24         } else {
25             &[sym::core, sym::compiler_builtins]
26         }
27     } else {
28         &[sym::std]
29     };
30
31     let expn_id = resolver.expansion_for_ast_pass(
32         DUMMY_SP,
33         AstPass::StdImports,
34         &[sym::prelude_import],
35         None,
36     );
37     let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
38     let call_site = DUMMY_SP.with_call_site_ctxt(expn_id.to_expn_id());
39
40     let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
41     let cx = ExtCtxt::new(sess, ecfg, resolver, None);
42
43     // .rev() to preserve ordering above in combination with insert(0, ...)
44     for &name in names.iter().rev() {
45         let ident = if edition >= Edition2018 {
46             Ident::new(name, span)
47         } else {
48             Ident::new(name, call_site)
49         };
50         krate.items.insert(
51             0,
52             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
61     // The crates have been injected, the assumption is that the first one is
62     // the one with the prelude.
63     let name = names[0];
64
65     let root = (edition == Edition2015).then(|| kw::PathRoot);
66
67     let import_path = root
68         .iter()
69         .chain(&[name, sym::prelude])
70         .chain(&[match edition {
71             Edition2015 => sym::rust_2015,
72             Edition2018 => sym::rust_2018,
73             Edition2021 => sym::rust_2021,
74         }])
75         .map(|&symbol| Ident::new(symbol, span))
76         .collect();
77
78     let use_item = cx.item(
79         span,
80         Ident::empty(),
81         vec![cx.attribute(cx.meta_word(span, sym::prelude_import))],
82         ast::ItemKind::Use(ast::UseTree {
83             prefix: cx.path(span, import_path),
84             kind: ast::UseTreeKind::Glob,
85             span,
86         }),
87     );
88
89     krate.items.insert(0, use_item);
90
91     krate
92 }