Move hir::Item::ident
into hir::ItemKind
.
`hir::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, Trait`, TraitAalis`. - It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`, `Impl`. - For `Use`, it is non-empty for `UseKind::Single` and empty for `UseKind::{Glob,ListStem}`. All of this is quite non-obvious; the only documentation is a single comment saying "The name might be a dummy name in case of anonymous items". 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. - A similar transformation makes sense for `ast::Item`, but this is already a big change. That can be done later. - Lots of assertions are added to item lowering to ensure that identifiers are empty/non-empty as expected. These will be removable when `ast::Item` is done later. - `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does. - `lower_use_tree` is significantly simpler. No more confusing `&mut Ident` to deal with. - `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's used with `unwrap` in a few places; sometimes it's hard to tell exactly which item kinds might occur. None of these unwraps fail on the test suite. It's conceivable that some might fail on alternative input. We can deal with those if/when they happen. - In `trait_path` the `find_map`/`if let` is replaced with a loop, and things end up much clearer that way. - `named_span` no longer checks for an empty name; instead the call site now checks for a missing identifier if necessary. - `maybe_inline_local` doesn't need the `glob` argument, it can be computed in-function from the `renamed` argument. - `arbitrary_source_item_ordering::check_mod` had a big `if` statement that was just getting the ident from the item kinds that had one. It could be mostly replaced by a single call to the new `ItemKind::ident` method. - `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size, and that's what matters, because `ItemKind` only occurs within `Item`.
This commit is contained in:
parent
6698c26b3a
commit
f2ddbcd24b
82 changed files with 666 additions and 555 deletions
|
@ -385,7 +385,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
pub fn hir_get_module(self, module: LocalModDefId) -> (&'tcx Mod<'tcx>, Span, HirId) {
|
||||
let hir_id = HirId::make_owner(module.to_local_def_id());
|
||||
match self.hir_owner_node(hir_id.owner) {
|
||||
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
|
||||
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(_, m), .. }) => (m, span, hir_id),
|
||||
OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
|
||||
node => panic!("not a module: {node:?}"),
|
||||
}
|
||||
|
@ -847,7 +847,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
// A `Ctor` doesn't have an identifier itself, but its parent
|
||||
// struct/variant does. Compare with `hir::Map::span`.
|
||||
Node::Ctor(..) => match self.parent_hir_node(id) {
|
||||
Node::Item(item) => Some(item.ident),
|
||||
Node::Item(item) => Some(item.kind.ident().unwrap()),
|
||||
Node::Variant(variant) => Some(variant.ident),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
|
@ -894,18 +894,14 @@ impl<'hir> Map<'hir> {
|
|||
}
|
||||
|
||||
fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
|
||||
if ident.name != kw::Empty {
|
||||
let mut span = until_within(item_span, ident.span);
|
||||
if let Some(g) = generics
|
||||
&& !g.span.is_dummy()
|
||||
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
|
||||
{
|
||||
span = span.to(g_span);
|
||||
}
|
||||
span
|
||||
} else {
|
||||
item_span
|
||||
let mut span = until_within(item_span, ident.span);
|
||||
if let Some(g) = generics
|
||||
&& !g.span.is_dummy()
|
||||
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
|
||||
{
|
||||
span = span.to(g_span);
|
||||
}
|
||||
span
|
||||
}
|
||||
|
||||
let span = match self.tcx.hir_node(hir_id) {
|
||||
|
@ -936,7 +932,7 @@ impl<'hir> Map<'hir> {
|
|||
}) => until_within(*outer_span, generics.where_clause_span),
|
||||
// Constants and Statics.
|
||||
Node::Item(Item {
|
||||
kind: ItemKind::Const(ty, ..) | ItemKind::Static(ty, ..),
|
||||
kind: ItemKind::Const(_, ty, ..) | ItemKind::Static(_, ty, ..),
|
||||
span: outer_span,
|
||||
..
|
||||
})
|
||||
|
@ -957,7 +953,7 @@ impl<'hir> Map<'hir> {
|
|||
}) => until_within(*outer_span, ty.span),
|
||||
// With generics and bounds.
|
||||
Node::Item(Item {
|
||||
kind: ItemKind::Trait(_, _, generics, bounds, _),
|
||||
kind: ItemKind::Trait(_, _, _, generics, bounds, _),
|
||||
span: outer_span,
|
||||
..
|
||||
})
|
||||
|
@ -977,7 +973,13 @@ impl<'hir> Map<'hir> {
|
|||
// SyntaxContext of the path.
|
||||
path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
|
||||
}
|
||||
_ => named_span(item.span, item.ident, item.kind.generics()),
|
||||
_ => {
|
||||
if let Some(ident) = item.kind.ident() {
|
||||
named_span(item.span, ident, item.kind.generics())
|
||||
} else {
|
||||
item.span
|
||||
}
|
||||
}
|
||||
},
|
||||
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
|
||||
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
|
||||
|
@ -1327,7 +1329,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
|
|||
self.items.push(item.item_id());
|
||||
|
||||
// Items that are modules are handled here instead of in visit_mod.
|
||||
if let ItemKind::Mod(module) = &item.kind {
|
||||
if let ItemKind::Mod(_, module) = &item.kind {
|
||||
self.submodules.push(item.owner_id);
|
||||
// A module collector does not recurse inside nested modules.
|
||||
if self.crate_collector {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue