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
|
@ -441,7 +441,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|||
// so we will continue to exclude them for compatibility.
|
||||
//
|
||||
// The documentation on `ExternCrate` is not used at the moment so no need to warn for it.
|
||||
if let hir::ItemKind::Impl(..) | hir::ItemKind::Use(..) | hir::ItemKind::ExternCrate(_) =
|
||||
if let hir::ItemKind::Impl(..) | hir::ItemKind::Use(..) | hir::ItemKind::ExternCrate(..) =
|
||||
it.kind
|
||||
{
|
||||
return;
|
||||
|
@ -545,21 +545,21 @@ impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
|
|||
return;
|
||||
}
|
||||
let (def, ty) = match item.kind {
|
||||
hir::ItemKind::Struct(_, ast_generics) => {
|
||||
hir::ItemKind::Struct(_, _, ast_generics) => {
|
||||
if !ast_generics.params.is_empty() {
|
||||
return;
|
||||
}
|
||||
let def = cx.tcx.adt_def(item.owner_id);
|
||||
(def, Ty::new_adt(cx.tcx, def, ty::List::empty()))
|
||||
}
|
||||
hir::ItemKind::Union(_, ast_generics) => {
|
||||
hir::ItemKind::Union(_, _, ast_generics) => {
|
||||
if !ast_generics.params.is_empty() {
|
||||
return;
|
||||
}
|
||||
let def = cx.tcx.adt_def(item.owner_id);
|
||||
(def, Ty::new_adt(cx.tcx, def, ty::List::empty()))
|
||||
}
|
||||
hir::ItemKind::Enum(_, ast_generics) => {
|
||||
hir::ItemKind::Enum(_, _, ast_generics) => {
|
||||
if !ast_generics.params.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
@ -1416,7 +1416,7 @@ impl TypeAliasBounds {
|
|||
|
||||
impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
|
||||
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
|
||||
let hir::ItemKind::TyAlias(hir_ty, generics) = item.kind else { return };
|
||||
let hir::ItemKind::TyAlias(_, hir_ty, generics) = item.kind else { return };
|
||||
|
||||
// There must not be a where clause.
|
||||
if generics.predicates.is_empty() {
|
||||
|
@ -2119,9 +2119,9 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
|
|||
use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
|
||||
|
||||
let def_id = item.owner_id.def_id;
|
||||
if let hir::ItemKind::Struct(_, hir_generics)
|
||||
| hir::ItemKind::Enum(_, hir_generics)
|
||||
| hir::ItemKind::Union(_, hir_generics) = item.kind
|
||||
if let hir::ItemKind::Struct(_, _, hir_generics)
|
||||
| hir::ItemKind::Enum(_, _, hir_generics)
|
||||
| hir::ItemKind::Union(_, _, hir_generics) = item.kind
|
||||
{
|
||||
let inferred_outlives = cx.tcx.inferred_outlives_of(def_id);
|
||||
if inferred_outlives.is_empty() {
|
||||
|
@ -2257,7 +2257,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
|
|||
// generics, except for tuple struct, which have the `where`
|
||||
// after the fields of the struct.
|
||||
let full_where_span =
|
||||
if let hir::ItemKind::Struct(hir::VariantData::Tuple(..), _) = item.kind {
|
||||
if let hir::ItemKind::Struct(_, hir::VariantData::Tuple(..), _) = item.kind {
|
||||
where_span
|
||||
} else {
|
||||
hir_generics.span.shrink_to_hi().to(where_span)
|
||||
|
|
|
@ -93,7 +93,11 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived {
|
|||
let orig_fields = match cx.tcx.hir_get_if_local(type_def_id) {
|
||||
Some(hir::Node::Item(hir::Item {
|
||||
kind:
|
||||
hir::ItemKind::Struct(hir::VariantData::Struct { fields, recovered: _ }, _generics),
|
||||
hir::ItemKind::Struct(
|
||||
_,
|
||||
hir::VariantData::Struct { fields, recovered: _ },
|
||||
_generics,
|
||||
),
|
||||
..
|
||||
})) => fields.iter().map(|f| (f.ident.name, f)).collect::<FxHashMap<_, _>>(),
|
||||
_ => return,
|
||||
|
|
|
@ -308,18 +308,18 @@ impl<'tcx> LateLintPass<'tcx> for TypeIr {
|
|||
[.., penultimate, segment]
|
||||
if penultimate.res.opt_def_id().is_some_and(is_mod_inherent) =>
|
||||
{
|
||||
(segment.ident.span, item.ident.span, "*")
|
||||
(segment.ident.span, item.kind.ident().unwrap().span, "*")
|
||||
}
|
||||
[.., segment]
|
||||
if path.res.iter().flat_map(Res::opt_def_id).any(is_mod_inherent)
|
||||
&& let rustc_hir::UseKind::Single = kind =>
|
||||
&& let rustc_hir::UseKind::Single(ident) = kind =>
|
||||
{
|
||||
let (lo, snippet) =
|
||||
match cx.tcx.sess.source_map().span_to_snippet(path.span).as_deref() {
|
||||
Ok("self") => (path.span, "*"),
|
||||
_ => (segment.ident.span.shrink_to_hi(), "::*"),
|
||||
};
|
||||
(lo, if segment.ident == item.ident { lo } else { item.ident.span }, snippet)
|
||||
(lo, if segment.ident == ident { lo } else { ident.span }, snippet)
|
||||
}
|
||||
_ => return,
|
||||
};
|
||||
|
|
|
@ -39,7 +39,7 @@ impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
|
|||
let def_id = item.owner_id.to_def_id();
|
||||
// NOTE(nbdd0121): use `dyn_compatibility_violations` instead of `is_dyn_compatible` because
|
||||
// the latter will report `where_clause_object_safety` lint.
|
||||
if let hir::ItemKind::Trait(_, _, _, _, _) = item.kind
|
||||
if let hir::ItemKind::Trait(_, _, ident, ..) = item.kind
|
||||
&& cx.tcx.is_dyn_compatible(def_id)
|
||||
{
|
||||
let direct_super_traits_iter = cx
|
||||
|
@ -51,7 +51,7 @@ impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
|
|||
cx.emit_span_lint(
|
||||
MULTIPLE_SUPERTRAIT_UPCASTABLE,
|
||||
cx.tcx.def_span(def_id),
|
||||
crate::lints::MultipleSupertraitUpcastable { ident: item.ident },
|
||||
crate::lints::MultipleSupertraitUpcastable { ident },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -181,10 +181,10 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
|
|||
&& parent_opt_item_name != Some(kw::Underscore)
|
||||
&& let Some(parent) = parent.as_local()
|
||||
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
|
||||
&& let ItemKind::Const(ty, _, _) = item.kind
|
||||
&& let ItemKind::Const(ident, ty, _, _) = item.kind
|
||||
&& let TyKind::Tup(&[]) = ty.kind
|
||||
{
|
||||
Some(item.ident.span)
|
||||
Some(ident.span)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
@ -238,7 +238,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
|
|||
},
|
||||
)
|
||||
}
|
||||
ItemKind::Macro(_macro, MacroKind::Bang)
|
||||
ItemKind::Macro(_, _macro, MacroKind::Bang)
|
||||
if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
|
||||
{
|
||||
cx.emit_span_lint(
|
||||
|
|
|
@ -415,8 +415,8 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
|
|||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
|
||||
if let hir::ItemKind::Mod(_) = it.kind {
|
||||
self.check_snake_case(cx, "module", &it.ident);
|
||||
if let hir::ItemKind::Mod(ident, _) = it.kind {
|
||||
self.check_snake_case(cx, "module", &ident);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -498,11 +498,13 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
|
|||
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
|
||||
let attrs = cx.tcx.hir_attrs(it.hir_id());
|
||||
match it.kind {
|
||||
hir::ItemKind::Static(..) if !ast::attr::contains_name(attrs, sym::no_mangle) => {
|
||||
NonUpperCaseGlobals::check_upper_case(cx, "static variable", &it.ident);
|
||||
hir::ItemKind::Static(ident, ..)
|
||||
if !ast::attr::contains_name(attrs, sym::no_mangle) =>
|
||||
{
|
||||
NonUpperCaseGlobals::check_upper_case(cx, "static variable", &ident);
|
||||
}
|
||||
hir::ItemKind::Const(..) => {
|
||||
NonUpperCaseGlobals::check_upper_case(cx, "constant", &it.ident);
|
||||
hir::ItemKind::Const(ident, ..) => {
|
||||
NonUpperCaseGlobals::check_upper_case(cx, "constant", &ident);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
@ -1664,7 +1664,7 @@ impl ImproperCTypesDefinitions {
|
|||
&& cx.tcx.sess.target.os == "aix"
|
||||
&& !adt_def.all_fields().next().is_none()
|
||||
{
|
||||
let struct_variant_data = item.expect_struct().0;
|
||||
let struct_variant_data = item.expect_struct().1;
|
||||
for (index, ..) in struct_variant_data.fields().iter().enumerate() {
|
||||
// Struct fields (after the first field) are checked for the
|
||||
// power alignment rule, as fields after the first are likely
|
||||
|
@ -1696,9 +1696,9 @@ impl ImproperCTypesDefinitions {
|
|||
impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
|
||||
match item.kind {
|
||||
hir::ItemKind::Static(ty, ..)
|
||||
| hir::ItemKind::Const(ty, ..)
|
||||
| hir::ItemKind::TyAlias(ty, ..) => {
|
||||
hir::ItemKind::Static(_, ty, ..)
|
||||
| hir::ItemKind::Const(_, ty, ..)
|
||||
| hir::ItemKind::TyAlias(_, ty, ..) => {
|
||||
self.check_ty_maybe_containing_foreign_fnptr(
|
||||
cx,
|
||||
ty,
|
||||
|
@ -1765,7 +1765,7 @@ declare_lint_pass!(VariantSizeDifferences => [VARIANT_SIZE_DIFFERENCES]);
|
|||
|
||||
impl<'tcx> LateLintPass<'tcx> for VariantSizeDifferences {
|
||||
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
|
||||
if let hir::ItemKind::Enum(ref enum_definition, _) = it.kind {
|
||||
if let hir::ItemKind::Enum(_, ref enum_definition, _) = it.kind {
|
||||
let t = cx.tcx.type_of(it.owner_id).instantiate_identity();
|
||||
let ty = cx.tcx.erase_regions(t);
|
||||
let Ok(layout) = cx.layout_of(ty) else { return };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue