rust/compiler/rustc_hir_analysis/src
Nicholas Nethercote f2ddbcd24b 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`.
2025-03-18 06:29:50 +11:00
..
check Move hir::Item::ident into hir::ItemKind. 2025-03-18 06:29:50 +11:00
coherence Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
collect Move hir::Item::ident into hir::ItemKind. 2025-03-18 06:29:50 +11:00
errors Remove the Option part of range ends in the HIR 2025-03-06 10:47:40 +00:00
hir_ty_lowering Move hir::Item::ident into hir::ItemKind. 2025-03-18 06:29:50 +11:00
impl_wf_check Fix a comment typo. 2025-01-31 08:28:14 +11:00
outlives Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
variance Remove the Option part of range ends in the HIR 2025-03-06 10:47:40 +00:00
autoderef.rs Rename structurally_normalize to structurally_normalize_ty 2025-01-22 07:04:53 +00:00
check_unused.rs Move methods from Map to TyCtxt, part 4. 2025-03-12 08:55:37 +11:00
collect.rs Move hir::Item::ident into hir::ItemKind. 2025-03-18 06:29:50 +11:00
constrained_generic_params.rs Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
delegation.rs Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
errors.rs Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
hir_wf_check.rs Move hir::Item::ident into hir::ItemKind. 2025-03-18 06:29:50 +11:00
impl_wf_check.rs Rename tcx.ensure() to tcx.ensure_ok() 2025-02-01 12:38:54 +11:00
lib.rs Add RTN support to rustdoc 2025-03-15 18:13:27 +00:00