Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0759ff

Browse files
committedMar 12, 2025
Auto merge of #138384 - nnethercote:hir-ItemKind-idents, r=<try>
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. This is step towards `kw::Empty` elimination (#137978). r? `@ghost`
2 parents a21d978 + 2d77b39 commit e0759ff

File tree

83 files changed

+662
-566
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+662
-566
lines changed
 

‎compiler/rustc_ast_lowering/src/item.rs

+76-37
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
111111
}
112112

113113
fn lower_foreign_item(&mut self, item: &ForeignItem) {
114+
assert_ne!(item.ident.name, kw::Empty);
114115
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
115116
}
116117
}
@@ -154,14 +155,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
154155
}
155156

156157
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
157-
let mut ident = i.ident;
158158
let vis_span = self.lower_span(i.vis.span);
159159
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
160160
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
161-
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
161+
let kind = self.lower_item_kind(i.span, i.id, hir_id, i.ident, attrs, vis_span, &i.kind);
162162
let item = hir::Item {
163163
owner_id: hir_id.expect_owner(),
164-
ident: self.lower_ident(ident),
165164
kind,
166165
vis_span,
167166
span: self.lower_span(i.span),
@@ -174,25 +173,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
174173
span: Span,
175174
id: NodeId,
176175
hir_id: hir::HirId,
177-
ident: &mut Ident,
176+
ident: Ident,
178177
attrs: &'hir [hir::Attribute],
179178
vis_span: Span,
180179
i: &ItemKind,
181180
) -> hir::ItemKind<'hir> {
182181
match i {
183-
ItemKind::ExternCrate(orig_name) => hir::ItemKind::ExternCrate(*orig_name),
182+
ItemKind::ExternCrate(orig_name) => {
183+
assert_ne!(ident.name, kw::Empty);
184+
let ident = self.lower_ident(ident);
185+
hir::ItemKind::ExternCrate(*orig_name, ident)
186+
}
184187
ItemKind::Use(use_tree) => {
188+
assert_eq!(ident.name, kw::Empty);
185189
// Start with an empty prefix.
186190
let prefix = Path { segments: ThinVec::new(), span: use_tree.span, tokens: None };
187191

188-
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
192+
self.lower_use_tree(use_tree, &prefix, id, vis_span, attrs)
189193
}
190194
ItemKind::Static(box ast::StaticItem { ty: t, safety: _, mutability: m, expr: e }) => {
195+
assert_ne!(ident.name, kw::Empty);
196+
let ident = self.lower_ident(ident);
191197
let (ty, body_id) =
192198
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
193-
hir::ItemKind::Static(ty, *m, body_id)
199+
hir::ItemKind::Static(ident, ty, *m, body_id)
194200
}
195201
ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
202+
assert_ne!(ident.name, kw::Empty);
203+
let ident = self.lower_ident(ident);
196204
let (generics, (ty, body_id)) = self.lower_generics(
197205
generics,
198206
id,
@@ -201,7 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
201209
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
202210
},
203211
);
204-
hir::ItemKind::Const(ty, generics, body_id)
212+
hir::ItemKind::Const(ident, ty, generics, body_id)
205213
}
206214
ItemKind::Fn(box Fn {
207215
sig: FnSig { decl, header, span: fn_sig_span },
@@ -211,6 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
211219
define_opaque,
212220
..
213221
}) => {
222+
assert_ne!(ident.name, kw::Empty);
214223
self.with_new_scopes(*fn_sig_span, |this| {
215224
// Note: we don't need to change the return type from `T` to
216225
// `impl Future<Output = T>` here because lower_body
@@ -238,28 +247,44 @@ impl<'hir> LoweringContext<'_, 'hir> {
238247
span: this.lower_span(*fn_sig_span),
239248
};
240249
this.lower_define_opaque(hir_id, &define_opaque);
241-
hir::ItemKind::Fn { sig, generics, body: body_id, has_body: body.is_some() }
250+
let ident = this.lower_ident(ident);
251+
hir::ItemKind::Fn {
252+
ident,
253+
sig,
254+
generics,
255+
body: body_id,
256+
has_body: body.is_some(),
257+
}
242258
})
243259
}
244-
ItemKind::Mod(_, mod_kind) => match mod_kind {
245-
ModKind::Loaded(items, _, spans, _) => {
246-
hir::ItemKind::Mod(self.lower_mod(items, spans))
260+
ItemKind::Mod(_, mod_kind) => {
261+
assert_ne!(ident.name, kw::Empty);
262+
let ident = self.lower_ident(ident);
263+
match mod_kind {
264+
ModKind::Loaded(items, _, spans, _) => {
265+
hir::ItemKind::Mod(ident, self.lower_mod(items, spans))
266+
}
267+
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
247268
}
248-
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
249-
},
250-
ItemKind::ForeignMod(fm) => hir::ItemKind::ForeignMod {
251-
abi: fm.abi.map_or(ExternAbi::FALLBACK, |abi| self.lower_abi(abi)),
252-
items: self
253-
.arena
254-
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
255-
},
269+
}
270+
ItemKind::ForeignMod(fm) => {
271+
assert_eq!(ident.name, kw::Empty);
272+
hir::ItemKind::ForeignMod {
273+
abi: fm.abi.map_or(ExternAbi::FALLBACK, |abi| self.lower_abi(abi)),
274+
items: self
275+
.arena
276+
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
277+
}
278+
}
256279
ItemKind::GlobalAsm(asm) => {
280+
assert_eq!(ident.name, kw::Empty);
257281
let asm = self.lower_inline_asm(span, asm);
258282
let fake_body =
259283
self.lower_body(|this| (&[], this.expr(span, hir::ExprKind::InlineAsm(asm))));
260284
hir::ItemKind::GlobalAsm { asm, fake_body }
261285
}
262286
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty, .. }) => {
287+
assert_ne!(ident.name, kw::Empty);
263288
// We lower
264289
//
265290
// type Foo = impl Trait
@@ -268,6 +293,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
268293
//
269294
// type Foo = Foo1
270295
// opaque type Foo1: Trait
296+
let ident = self.lower_ident(ident);
271297
let mut generics = generics.clone();
272298
add_ty_alias_where_clause(&mut generics, *where_clauses, true);
273299
let (generics, ty) = self.lower_generics(
@@ -293,9 +319,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
293319
),
294320
},
295321
);
296-
hir::ItemKind::TyAlias(ty, generics)
322+
hir::ItemKind::TyAlias(ident, ty, generics)
297323
}
298324
ItemKind::Enum(enum_definition, generics) => {
325+
assert_ne!(ident.name, kw::Empty);
326+
let ident = self.lower_ident(ident);
299327
let (generics, variants) = self.lower_generics(
300328
generics,
301329
id,
@@ -306,25 +334,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
306334
)
307335
},
308336
);
309-
hir::ItemKind::Enum(hir::EnumDef { variants }, generics)
337+
hir::ItemKind::Enum(ident, hir::EnumDef { variants }, generics)
310338
}
311339
ItemKind::Struct(struct_def, generics) => {
340+
assert_ne!(ident.name, kw::Empty);
341+
let ident = self.lower_ident(ident);
312342
let (generics, struct_def) = self.lower_generics(
313343
generics,
314344
id,
315345
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
316346
|this| this.lower_variant_data(hir_id, struct_def),
317347
);
318-
hir::ItemKind::Struct(struct_def, generics)
348+
hir::ItemKind::Struct(ident, struct_def, generics)
319349
}
320350
ItemKind::Union(vdata, generics) => {
351+
assert_ne!(ident.name, kw::Empty);
352+
let ident = self.lower_ident(ident);
321353
let (generics, vdata) = self.lower_generics(
322354
generics,
323355
id,
324356
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
325357
|this| this.lower_variant_data(hir_id, vdata),
326358
);
327-
hir::ItemKind::Union(vdata, generics)
359+
hir::ItemKind::Union(ident, vdata, generics)
328360
}
329361
ItemKind::Impl(box Impl {
330362
safety,
@@ -336,6 +368,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
336368
self_ty: ty,
337369
items: impl_items,
338370
}) => {
371+
assert_eq!(ident.name, kw::Empty);
339372
// Lower the "impl header" first. This ordering is important
340373
// for in-band lifetimes! Consider `'a` here:
341374
//
@@ -401,6 +434,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
401434
}))
402435
}
403436
ItemKind::Trait(box Trait { is_auto, safety, generics, bounds, items }) => {
437+
assert_ne!(ident.name, kw::Empty);
438+
let ident = self.lower_ident(ident);
404439
let (generics, (safety, items, bounds)) = self.lower_generics(
405440
generics,
406441
id,
@@ -417,9 +452,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
417452
(safety, items, bounds)
418453
},
419454
);
420-
hir::ItemKind::Trait(*is_auto, safety, generics, bounds, items)
455+
hir::ItemKind::Trait(*is_auto, safety, ident, generics, bounds, items)
421456
}
422457
ItemKind::TraitAlias(generics, bounds) => {
458+
assert_ne!(ident.name, kw::Empty);
459+
let ident = self.lower_ident(ident);
423460
let (generics, bounds) = self.lower_generics(
424461
generics,
425462
id,
@@ -431,9 +468,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
431468
)
432469
},
433470
);
434-
hir::ItemKind::TraitAlias(generics, bounds)
471+
hir::ItemKind::TraitAlias(ident, generics, bounds)
435472
}
436473
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
474+
assert_ne!(ident.name, kw::Empty);
475+
let ident = self.lower_ident(ident);
437476
let body = P(self.lower_delim_args(body));
438477
let def_id = self.local_def_id(id);
439478
let def_kind = self.tcx.def_kind(def_id);
@@ -444,11 +483,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
444483
);
445484
};
446485
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
447-
hir::ItemKind::Macro(macro_def, macro_kind)
486+
hir::ItemKind::Macro(ident, macro_def, macro_kind)
448487
}
449488
ItemKind::Delegation(box delegation) => {
489+
assert_ne!(ident.name, kw::Empty);
490+
let ident = self.lower_ident(ident);
450491
let delegation_results = self.lower_delegation(delegation, id);
451492
hir::ItemKind::Fn {
493+
ident,
452494
sig: delegation_results.sig,
453495
generics: delegation_results.generics,
454496
body: delegation_results.body_id,
@@ -479,15 +521,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
479521
prefix: &Path,
480522
id: NodeId,
481523
vis_span: Span,
482-
ident: &mut Ident,
483524
attrs: &'hir [hir::Attribute],
484525
) -> hir::ItemKind<'hir> {
485526
let path = &tree.prefix;
486527
let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect();
487528

488529
match tree.kind {
489530
UseTreeKind::Simple(rename) => {
490-
*ident = tree.ident();
531+
let mut ident = tree.ident();
491532

492533
// First, apply the prefix to the path.
493534
let mut path = Path { segments, span: path.span, tokens: None };
@@ -498,13 +539,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
498539
{
499540
let _ = path.segments.pop();
500541
if rename.is_none() {
501-
*ident = path.segments.last().unwrap().ident;
542+
ident = path.segments.last().unwrap().ident;
502543
}
503544
}
504545

505546
let res = self.lower_import_res(id, path.span);
506547
let path = self.lower_use_path(res, &path, ParamMode::Explicit);
507-
hir::ItemKind::Use(path, hir::UseKind::Single)
548+
hir::ItemKind::Use(path, hir::UseKind::Single(ident))
508549
}
509550
UseTreeKind::Glob => {
510551
let res = self.expect_full_res(id);
@@ -551,20 +592,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
551592
// own its own names, we have to adjust the owner before
552593
// lowering the rest of the import.
553594
self.with_hir_id_owner(id, |this| {
554-
let mut ident = *ident;
555-
556595
// `prefix` is lowered multiple times, but in different HIR owners.
557596
// So each segment gets renewed `HirId` with the same
558597
// `ItemLocalId` and the new owner. (See `lower_node_id`)
559-
let kind =
560-
this.lower_use_tree(use_tree, &prefix, id, vis_span, &mut ident, attrs);
598+
let kind = this.lower_use_tree(use_tree, &prefix, id, vis_span, attrs);
561599
if !attrs.is_empty() {
562600
this.attrs.insert(hir::ItemLocalId::ZERO, attrs);
563601
}
564602

565603
let item = hir::Item {
566604
owner_id: hir::OwnerId { def_id: new_hir_id },
567-
ident: this.lower_ident(ident),
568605
kind,
569606
vis_span,
570607
span: this.lower_span(use_tree.span),
@@ -604,7 +641,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
604641
hir::ItemKind::Impl(impl_) => {
605642
self.is_in_trait_impl = impl_.of_trait.is_some();
606643
}
607-
hir::ItemKind::Trait(_, _, _, _, _) => {}
644+
hir::ItemKind::Trait(..) => {}
608645
kind => {
609646
span_bug!(item.span, "assoc item has unexpected kind of parent: {}", kind.descr())
610647
}
@@ -760,6 +797,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
760797
}
761798

762799
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
800+
assert_ne!(i.ident.name, kw::Empty);
763801
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
764802
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
765803
let trait_item_def_id = hir_id.expect_owner();
@@ -907,6 +945,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
907945
}
908946

909947
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
948+
assert_ne!(i.ident.name, kw::Empty);
910949
// Since `default impl` is not yet implemented, this is always true in impls.
911950
let has_value = true;
912951
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);

‎compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
691691
true,
692692
td.is_local(),
693693
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
694-
Node::Item(hir::Item { kind: hir::ItemKind::Trait(_, _, _, _, items), .. }) => {
694+
Node::Item(hir::Item {
695+
kind: hir::ItemKind::Trait(_, _, _, _, _, items), ..
696+
}) => {
695697
let mut f_in_trait_opt = None;
696698
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
697699
let hi = fi.hir_id();
@@ -980,7 +982,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
980982
let arg = match tcx.hir_get_if_local(callee_def_id) {
981983
Some(
982984
hir::Node::Item(hir::Item {
983-
ident, kind: hir::ItemKind::Fn { sig, .. }, ..
985+
kind: hir::ItemKind::Fn { ident, sig, .. }, ..
984986
})
985987
| hir::Node::TraitItem(hir::TraitItem {
986988
ident,
@@ -1021,7 +1023,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10211023
// return type.
10221024
match tcx.hir_node_by_def_id(tcx.hir_get_parent_item(fn_call_id).def_id) {
10231025
hir::Node::Item(hir::Item {
1024-
ident, kind: hir::ItemKind::Fn { sig, .. }, ..
1026+
kind: hir::ItemKind::Fn { ident, sig, .. }, ..
10251027
})
10261028
| hir::Node::TraitItem(hir::TraitItem {
10271029
ident,
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.