about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros/src/standard_library_imports.rs
blob: 2068b5ca54dd05bc5b6c2caa4996e94a1f24ef96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use rustc_ast::{self as ast, attr};
use rustc_expand::base::{ExtCtxt, ResolverExpand};
use rustc_expand::expand::ExpansionConfig;
use rustc_feature::Features;
use rustc_session::Session;
use rustc_span::edition::Edition::*;
use rustc_span::hygiene::AstPass;
use rustc_span::{DUMMY_SP, Ident, Symbol, kw, sym};
use thin_vec::thin_vec;

pub fn inject(
    krate: &mut ast::Crate,
    pre_configured_attrs: &[ast::Attribute],
    resolver: &mut dyn ResolverExpand,
    sess: &Session,
    features: &Features,
) -> usize {
    let orig_num_items = krate.items.len();
    let edition = sess.psess.edition;

    // the first name in this list is the crate name of the crate with the prelude
    let name: Symbol = if attr::contains_name(pre_configured_attrs, sym::no_core) {
        return 0;
    } else if attr::contains_name(pre_configured_attrs, sym::no_std) {
        sym::core
    } else {
        sym::std
    };

    let expn_id = resolver.expansion_for_ast_pass(
        DUMMY_SP,
        AstPass::StdImports,
        &[sym::prelude_import],
        None,
    );
    let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
    let call_site = DUMMY_SP.with_call_site_ctxt(expn_id.to_expn_id());

    let ecfg = ExpansionConfig::default(sym::std_lib_injection, features);
    let cx = ExtCtxt::new(sess, ecfg, resolver, None);

    let ident_span = if edition >= Edition2018 { span } else { call_site };

    let item = cx.item(
        span,
        thin_vec![cx.attr_word(sym::macro_use, span)],
        ast::ItemKind::ExternCrate(None, Ident::new(name, ident_span)),
    );

    let root = (edition == Edition2015).then_some(kw::PathRoot);

    let import_path = root
        .iter()
        .chain(&[name, sym::prelude])
        .chain(&[match edition {
            Edition2015 => sym::rust_2015,
            Edition2018 => sym::rust_2018,
            Edition2021 => sym::rust_2021,
            Edition2024 => sym::rust_2024,
            EditionFuture => sym::rust_future,
        }])
        .map(|&symbol| Ident::new(symbol, span))
        .collect();

    // Inject the relevant crate's prelude.
    let use_item = cx.item(
        span,
        thin_vec![cx.attr_word(sym::prelude_import, span)],
        ast::ItemKind::Use(ast::UseTree {
            prefix: cx.path(span, import_path),
            kind: ast::UseTreeKind::Glob,
            span,
        }),
    );

    krate.items.splice(0..0, [item, use_item]);
    krate.items.len() - orig_num_items
}