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
|
@ -743,7 +743,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||
match target {
|
||||
Target::Struct => {
|
||||
if let Some(ItemLike::Item(hir::Item {
|
||||
kind: hir::ItemKind::Struct(hir::VariantData::Struct { fields, .. }, _),
|
||||
kind: hir::ItemKind::Struct(_, hir::VariantData::Struct { fields, .. }, _),
|
||||
..
|
||||
})) = item
|
||||
&& !fields.is_empty()
|
||||
|
@ -1019,7 +1019,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||
_ => None,
|
||||
};
|
||||
match item_kind {
|
||||
Some(ItemKind::Mod(module)) => {
|
||||
Some(ItemKind::Mod(_, module)) => {
|
||||
if !module.item_ids.is_empty() {
|
||||
self.dcx().emit_err(errors::DocKeywordEmptyMod { span: meta.span() });
|
||||
return;
|
||||
|
@ -1072,9 +1072,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||
return;
|
||||
};
|
||||
match item.kind {
|
||||
ItemKind::Enum(_, generics) | ItemKind::Struct(_, generics)
|
||||
ItemKind::Enum(_, _, generics) | ItemKind::Struct(_, _, generics)
|
||||
if generics.params.len() != 0 => {}
|
||||
ItemKind::Trait(_, _, generics, _, items)
|
||||
ItemKind::Trait(_, _, _, generics, _, items)
|
||||
if generics.params.len() != 0
|
||||
|| items.iter().any(|item| matches!(item.kind, AssocItemKind::Type)) => {}
|
||||
_ => {
|
||||
|
@ -2293,7 +2293,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||
}
|
||||
} else {
|
||||
// special case when `#[macro_export]` is applied to a macro 2.0
|
||||
let (macro_definition, _) = self.tcx.hir_node(hir_id).expect_item().expect_macro();
|
||||
let (_, macro_definition, _) = self.tcx.hir_node(hir_id).expect_item().expect_macro();
|
||||
let is_decl_macro = !macro_definition.macro_rules;
|
||||
|
||||
if is_decl_macro {
|
||||
|
@ -2624,7 +2624,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
|
|||
// Historically we've run more checks on non-exported than exported macros,
|
||||
// so this lets us continue to run them while maintaining backwards compatibility.
|
||||
// In the long run, the checks should be harmonized.
|
||||
if let ItemKind::Macro(macro_def, _) = item.kind {
|
||||
if let ItemKind::Macro(_, macro_def, _) = item.kind {
|
||||
let def_id = item.owner_id.to_def_id();
|
||||
if macro_def.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) {
|
||||
check_non_exported_macro_for_invalid_attrs(self.tcx, item);
|
||||
|
@ -2736,7 +2736,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
|
|||
}
|
||||
|
||||
fn is_c_like_enum(item: &Item<'_>) -> bool {
|
||||
if let ItemKind::Enum(ref def, _) = item.kind {
|
||||
if let ItemKind::Enum(_, ref def, _) = item.kind {
|
||||
for variant in def.variants {
|
||||
match variant.data {
|
||||
hir::VariantData::Unit(..) => { /* continue */ }
|
||||
|
@ -2784,7 +2784,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
|
|||
.map(|id| tcx.hir_item(id))
|
||||
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
|
||||
.map(|item| errors::ItemFollowingInnerAttr {
|
||||
span: item.ident.span,
|
||||
span: if let Some(ident) = item.kind.ident() { ident.span } else { item.span },
|
||||
kind: item.kind.descr(),
|
||||
});
|
||||
let err = tcx.dcx().create_err(errors::InvalidAttrAtCrateLevel {
|
||||
|
|
|
@ -751,7 +751,7 @@ fn check_item<'tcx>(
|
|||
match tcx.def_kind(id.owner_id) {
|
||||
DefKind::Enum => {
|
||||
let item = tcx.hir_item(id);
|
||||
if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
|
||||
if let hir::ItemKind::Enum(_, ref enum_def, _) = item.kind {
|
||||
if let Some(comes_from_allow) = allow_dead_code {
|
||||
worklist.extend(
|
||||
enum_def.variants.iter().map(|variant| (variant.def_id, comes_from_allow)),
|
||||
|
@ -806,7 +806,7 @@ fn check_item<'tcx>(
|
|||
}
|
||||
DefKind::Struct => {
|
||||
let item = tcx.hir_item(id);
|
||||
if let hir::ItemKind::Struct(ref variant_data, _) = item.kind
|
||||
if let hir::ItemKind::Struct(_, ref variant_data, _) = item.kind
|
||||
&& let Some(ctor_def_id) = variant_data.ctor_def_id()
|
||||
{
|
||||
struct_constructors.insert(ctor_def_id, item.owner_id.def_id);
|
||||
|
@ -987,7 +987,7 @@ impl<'tcx> DeadVisitor<'tcx> {
|
|||
&& let Some(enum_did) = tcx.opt_parent(may_variant.to_def_id())
|
||||
&& let Some(enum_local_id) = enum_did.as_local()
|
||||
&& let Node::Item(item) = tcx.hir_node_by_def_id(enum_local_id)
|
||||
&& let ItemKind::Enum(_, _) = item.kind
|
||||
&& let ItemKind::Enum(..) = item.kind
|
||||
{
|
||||
enum_local_id
|
||||
} else {
|
||||
|
@ -1062,7 +1062,7 @@ impl<'tcx> DeadVisitor<'tcx> {
|
|||
let tuple_fields = if let Some(parent_id) = parent_item
|
||||
&& let node = tcx.hir_node_by_def_id(parent_id)
|
||||
&& let hir::Node::Item(hir::Item {
|
||||
kind: hir::ItemKind::Struct(hir::VariantData::Tuple(fields, _, _), _),
|
||||
kind: hir::ItemKind::Struct(_, hir::VariantData::Tuple(fields, _, _), _),
|
||||
..
|
||||
}) = node
|
||||
{
|
||||
|
|
|
@ -206,7 +206,7 @@ impl<'tcx> ReachableContext<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
hir::ItemKind::Const(_, _, init) => {
|
||||
hir::ItemKind::Const(_, _, _, init) => {
|
||||
// Only things actually ending up in the final constant value are reachable
|
||||
// for codegen. Everything else is only needed during const-eval, so even if
|
||||
// const-eval happens in a downstream crate, all they need is
|
||||
|
@ -234,7 +234,7 @@ impl<'tcx> ReachableContext<'tcx> {
|
|||
// These are normal, nothing reachable about these
|
||||
// inherently and their children are already in the
|
||||
// worklist, as determined by the privacy pass
|
||||
hir::ItemKind::ExternCrate(_)
|
||||
hir::ItemKind::ExternCrate(..)
|
||||
| hir::ItemKind::Use(..)
|
||||
| hir::ItemKind::TyAlias(..)
|
||||
| hir::ItemKind::Macro(..)
|
||||
|
|
|
@ -430,7 +430,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
|
|||
kind = AnnotationKind::DeprecationProhibited;
|
||||
const_stab_inherit = InheritConstStability::Yes;
|
||||
}
|
||||
hir::ItemKind::Struct(ref sd, _) => {
|
||||
hir::ItemKind::Struct(_, ref sd, _) => {
|
||||
if let Some(ctor_def_id) = sd.ctor_def_id() {
|
||||
self.annotate(
|
||||
ctor_def_id,
|
||||
|
@ -773,10 +773,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
|
|||
|
||||
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
||||
match item.kind {
|
||||
hir::ItemKind::ExternCrate(_) => {
|
||||
hir::ItemKind::ExternCrate(_, ident) => {
|
||||
// compiler-generated `extern crate` items have a dummy span.
|
||||
// `std` is still checked for the `restricted-std` feature.
|
||||
if item.span.is_dummy() && item.ident.name != sym::std {
|
||||
if item.span.is_dummy() && ident.name != sym::std {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue