]>
git.proxmox.com Git - rustc.git/blob - src/libsyntax_ext/standard_library_imports.rs
1 use syntax
::{ast, attr}
;
2 use syntax
::edition
::Edition
;
4 use syntax
::sess
::ParseSess
;
5 use syntax
::symbol
::{Ident, Symbol, kw, sym}
;
6 use syntax_expand
::expand
::ExpansionConfig
;
7 use syntax_expand
::base
::{ExtCtxt, Resolver}
;
8 use syntax_pos
::DUMMY_SP
;
9 use syntax_pos
::hygiene
::AstPass
;
12 mut krate
: ast
::Crate
,
13 resolver
: &mut dyn Resolver
,
15 alt_std_name
: Option
<Symbol
>,
16 ) -> (ast
::Crate
, Option
<Symbol
>) {
17 let rust_2018
= sess
.edition
>= Edition
::Edition2018
;
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
) {
22 } else if attr
::contains_name(&krate
.attrs
, sym
::no_std
) {
23 if attr
::contains_name(&krate
.attrs
, sym
::compiler_builtins
) {
26 &[sym
::core
, sym
::compiler_builtins
]
32 let expn_id
= resolver
.expansion_for_ast_pass(
35 &[sym
::prelude_import
],
38 let span
= DUMMY_SP
.with_def_site_ctxt(expn_id
);
39 let call_site
= DUMMY_SP
.with_call_site_ctxt(expn_id
);
41 let ecfg
= ExpansionConfig
::default("std_lib_injection".to_string());
42 let cx
= ExtCtxt
::new(sess
, ecfg
, resolver
);
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
)
50 Ident
::new(name
, call_site
)
52 krate
.module
.items
.insert(0, cx
.item(
55 vec
![cx
.attribute(cx
.meta_word(span
, sym
::macro_use
))],
56 ast
::ItemKind
::ExternCrate(alt_std_name
),
60 // The crates have been injected, the assumption is that the first one is
61 // the one with the prelude.
64 let import_path
= if rust_2018
{
65 [name
, sym
::prelude
, sym
::v1
].iter()
66 .map(|symbol
| ast
::Ident
::new(*symbol
, span
)).collect()
68 [kw
::PathRoot
, name
, sym
::prelude
, sym
::v1
].iter()
69 .map(|symbol
| ast
::Ident
::new(*symbol
, span
)).collect()
72 let use_item
= cx
.item(
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
,
83 krate
.module
.items
.insert(0, use_item
);