]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/standard_library_imports.rs
Rollup merge of #107424 - bpeel:clone-into-from-share-code, r=scottmcm
[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 use thin_vec::thin_vec;
10
11 pub fn inject(
12     mut krate: ast::Crate,
13     resolver: &mut dyn ResolverExpand,
14     sess: &Session,
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                 thin_vec![cx.attr_word(sym::macro_use, span)],
56                 ast::ItemKind::ExternCrate(None),
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             Edition2024 => sym::rust_2024,
75         }])
76         .map(|&symbol| Ident::new(symbol, span))
77         .collect();
78
79     let use_item = cx.item(
80         span,
81         Ident::empty(),
82         thin_vec![cx.attr_word(sym::prelude_import, span)],
83         ast::ItemKind::Use(ast::UseTree {
84             prefix: cx.path(span, import_path),
85             kind: ast::UseTreeKind::Glob,
86             span,
87         }),
88     );
89
90     krate.items.insert(0, use_item);
91
92     krate
93 }