mirror of
https://github.com/rust-lang/rust.git
synced 2025-11-01 05:25:38 +00:00
`ast::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, `Trait`, `TraitAlias`, `MacroDef`, `Delegation`. - It's always empty for these item kinds: `Use`, `ForeignMod`, `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`. There is a similar story for `AssocItemKind` and `ForeignItemKind`. Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - `ast::Item` got 8 bytes bigger. This could be avoided by boxing the fields within some of the `ast::ItemKind` variants (specifically: `Struct`, `Union`, `Enum`). I might do that in a follow-up; this commit is big enough already. - For the visitors: `FnKind` no longer needs an `ident` field because the `Fn` within how has one. - In the parser, the `ItemInfo` typedef is no longer needed. It was used in various places to return an `Ident` alongside an `ItemKind`, but now the `Ident` (if present) is within the `ItemKind`. - In a few places I renamed identifier variables called `name` (or `foo_name`) as `ident` (or `foo_ident`), to better match the type, and because `name` is normally used for `Symbol`s. It's confusing to see something like `foo_name.name`.
81 lines
2.4 KiB
Rust
81 lines
2.4 KiB
Rust
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("std_lib_injection".to_string(), 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)),
|
|
);
|
|
|
|
krate.items.insert(0, item);
|
|
|
|
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.insert(0, use_item);
|
|
krate.items.len() - orig_num_items
|
|
}
|