Move ast::Item::ident
into ast::ItemKind
.
`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`.
This commit is contained in:
parent
43018eacb6
commit
df247968f2
54 changed files with 1072 additions and 864 deletions
|
@ -1424,12 +1424,11 @@ pub fn parse_macro_name_and_helper_attrs(
|
|||
/// See #73345 and #83125 for more details.
|
||||
/// FIXME(#73933): Remove this eventually.
|
||||
fn pretty_printing_compatibility_hack(item: &Item, psess: &ParseSess) {
|
||||
let name = item.ident.name;
|
||||
if name == sym::ProceduralMasqueradeDummyType
|
||||
&& let ast::ItemKind::Enum(enum_def, _) = &item.kind
|
||||
if let ast::ItemKind::Enum(ident, enum_def, _) = &item.kind
|
||||
&& ident.name == sym::ProceduralMasqueradeDummyType
|
||||
&& let [variant] = &*enum_def.variants
|
||||
&& variant.ident.name == sym::Input
|
||||
&& let FileName::Real(real) = psess.source_map().span_to_filename(item.ident.span)
|
||||
&& let FileName::Real(real) = psess.source_map().span_to_filename(ident.span)
|
||||
&& let Some(c) = real
|
||||
.local_path()
|
||||
.unwrap_or(Path::new(""))
|
||||
|
|
|
@ -662,15 +662,8 @@ impl<'a> ExtCtxt<'a> {
|
|||
P(ast::FnDecl { inputs, output })
|
||||
}
|
||||
|
||||
pub fn item(
|
||||
&self,
|
||||
span: Span,
|
||||
name: Ident,
|
||||
attrs: ast::AttrVec,
|
||||
kind: ast::ItemKind,
|
||||
) -> P<ast::Item> {
|
||||
pub fn item(&self, span: Span, attrs: ast::AttrVec, kind: ast::ItemKind) -> P<ast::Item> {
|
||||
P(ast::Item {
|
||||
ident: name,
|
||||
attrs,
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
kind,
|
||||
|
@ -687,17 +680,17 @@ impl<'a> ExtCtxt<'a> {
|
|||
pub fn item_static(
|
||||
&self,
|
||||
span: Span,
|
||||
name: Ident,
|
||||
ident: Ident,
|
||||
ty: P<ast::Ty>,
|
||||
mutability: ast::Mutability,
|
||||
expr: P<ast::Expr>,
|
||||
) -> P<ast::Item> {
|
||||
self.item(
|
||||
span,
|
||||
name,
|
||||
AttrVec::new(),
|
||||
ast::ItemKind::Static(
|
||||
ast::StaticItem {
|
||||
ident,
|
||||
ty,
|
||||
safety: ast::Safety::Default,
|
||||
mutability,
|
||||
|
@ -712,18 +705,18 @@ impl<'a> ExtCtxt<'a> {
|
|||
pub fn item_const(
|
||||
&self,
|
||||
span: Span,
|
||||
name: Ident,
|
||||
ident: Ident,
|
||||
ty: P<ast::Ty>,
|
||||
expr: P<ast::Expr>,
|
||||
) -> P<ast::Item> {
|
||||
let defaultness = ast::Defaultness::Final;
|
||||
self.item(
|
||||
span,
|
||||
name,
|
||||
AttrVec::new(),
|
||||
ast::ItemKind::Const(
|
||||
ast::ConstItem {
|
||||
defaultness,
|
||||
ident,
|
||||
// FIXME(generic_const_items): Pass the generics as a parameter.
|
||||
generics: ast::Generics::default(),
|
||||
ty,
|
||||
|
|
|
@ -743,6 +743,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||
&& matches!(
|
||||
item_inner.kind,
|
||||
ItemKind::Mod(
|
||||
_,
|
||||
_,
|
||||
ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _, _),
|
||||
)
|
||||
|
@ -911,7 +912,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||
impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
|
||||
fn visit_item(&mut self, item: &'ast ast::Item) {
|
||||
match &item.kind {
|
||||
ItemKind::Mod(_, mod_kind)
|
||||
ItemKind::Mod(_, _, mod_kind)
|
||||
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _, _)) =>
|
||||
{
|
||||
feature_err(
|
||||
|
@ -1221,8 +1222,9 @@ impl InvocationCollectorNode for P<ast::Item> {
|
|||
}
|
||||
|
||||
// Work around borrow checker not seeing through `P`'s deref.
|
||||
let (ident, span, mut attrs) = (node.ident, node.span, mem::take(&mut node.attrs));
|
||||
let ItemKind::Mod(_, mod_kind) = &mut node.kind else { unreachable!() };
|
||||
let (span, mut attrs) = (node.span, mem::take(&mut node.attrs));
|
||||
let ItemKind::Mod(_, ident, mod_kind) = &mut node.kind else { unreachable!() };
|
||||
let ident = *ident;
|
||||
|
||||
let ecx = &mut collector.cx;
|
||||
let (file_path, dir_path, dir_ownership) = match mod_kind {
|
||||
|
@ -1305,6 +1307,7 @@ impl InvocationCollectorNode for P<ast::Item> {
|
|||
collector.cx.current_expansion.module = orig_module;
|
||||
res
|
||||
}
|
||||
|
||||
fn declared_names(&self) -> Vec<Ident> {
|
||||
if let ItemKind::Use(ut) = &self.kind {
|
||||
fn collect_use_tree_leaves(ut: &ast::UseTree, idents: &mut Vec<Ident>) {
|
||||
|
@ -1324,7 +1327,7 @@ impl InvocationCollectorNode for P<ast::Item> {
|
|||
return idents;
|
||||
}
|
||||
|
||||
vec![self.ident]
|
||||
if let Some(ident) = self.kind.ident() { vec![ident] } else { vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1844,11 +1847,11 @@ fn build_single_delegations<'a, Node: InvocationCollectorNode>(
|
|||
id: ast::DUMMY_NODE_ID,
|
||||
span: if from_glob { item_span } else { ident.span },
|
||||
vis: item.vis.clone(),
|
||||
ident: rename.unwrap_or(ident),
|
||||
kind: Node::delegation_item_kind(Box::new(ast::Delegation {
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
qself: deleg.qself.clone(),
|
||||
path,
|
||||
ident: rename.unwrap_or(ident),
|
||||
rename,
|
||||
body: deleg.body.clone(),
|
||||
from_glob,
|
||||
|
|
|
@ -26,7 +26,7 @@ pub(crate) fn placeholder(
|
|||
})
|
||||
}
|
||||
|
||||
let ident = Ident::empty();
|
||||
let ident = Ident::dummy();
|
||||
let attrs = ast::AttrVec::new();
|
||||
let vis = vis.unwrap_or(ast::Visibility {
|
||||
span: DUMMY_SP,
|
||||
|
@ -62,7 +62,6 @@ pub(crate) fn placeholder(
|
|||
AstFragmentKind::Items => AstFragment::Items(smallvec![P(ast::Item {
|
||||
id,
|
||||
span,
|
||||
ident,
|
||||
vis,
|
||||
attrs,
|
||||
kind: ast::ItemKind::MacCall(mac_placeholder()),
|
||||
|
@ -71,7 +70,6 @@ pub(crate) fn placeholder(
|
|||
AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![P(ast::AssocItem {
|
||||
id,
|
||||
span,
|
||||
ident,
|
||||
vis,
|
||||
attrs,
|
||||
kind: ast::AssocItemKind::MacCall(mac_placeholder()),
|
||||
|
@ -80,7 +78,6 @@ pub(crate) fn placeholder(
|
|||
AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![P(ast::AssocItem {
|
||||
id,
|
||||
span,
|
||||
ident,
|
||||
vis,
|
||||
attrs,
|
||||
kind: ast::AssocItemKind::MacCall(mac_placeholder()),
|
||||
|
@ -90,7 +87,6 @@ pub(crate) fn placeholder(
|
|||
AstFragment::TraitImplItems(smallvec![P(ast::AssocItem {
|
||||
id,
|
||||
span,
|
||||
ident,
|
||||
vis,
|
||||
attrs,
|
||||
kind: ast::AssocItemKind::MacCall(mac_placeholder()),
|
||||
|
@ -101,7 +97,6 @@ pub(crate) fn placeholder(
|
|||
AstFragment::ForeignItems(smallvec![P(ast::ForeignItem {
|
||||
id,
|
||||
span,
|
||||
ident,
|
||||
vis,
|
||||
attrs,
|
||||
kind: ast::ForeignItemKind::MacCall(mac_placeholder()),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue