Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 759e80d

Browse files
authoredNov 4, 2024
Rollup merge of rust-lang#132576 - jdonszelmann:no-attrid-in-stats, r=nnethercote
remove attribute ids from hir stats (they're simply not needed) Turns out these are simply not needed. Yay! r? `@nnethercote`
2 parents 7155c65 + efaf9ab commit 759e80d

File tree

1 file changed

+76
-80
lines changed

1 file changed

+76
-80
lines changed
 

‎compiler/rustc_passes/src/hir_stats.rs

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// completely accurate (some things might be counted twice, others missed).
44

55
use rustc_ast::visit::BoundKind;
6-
use rustc_ast::{self as ast, AttrId, NodeId, visit as ast_visit};
6+
use rustc_ast::{self as ast, NodeId, visit as ast_visit};
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
88
use rustc_hir as hir;
99
use rustc_hir::{HirId, intravisit as hir_visit};
@@ -13,13 +13,6 @@ use rustc_middle::util::common::to_readable_str;
1313
use rustc_span::Span;
1414
use rustc_span::def_id::LocalDefId;
1515

16-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
17-
enum Id {
18-
Node(HirId),
19-
Attr(AttrId),
20-
None,
21-
}
22-
2316
struct NodeStats {
2417
count: usize,
2518
size: usize,
@@ -62,7 +55,7 @@ impl Node {
6255
struct StatCollector<'k> {
6356
krate: Option<Map<'k>>,
6457
nodes: FxHashMap<&'static str, Node>,
65-
seen: FxHashSet<Id>,
58+
seen: FxHashSet<HirId>,
6659
}
6760

6861
pub fn print_hir_stats(tcx: TyCtxt<'_>) {
@@ -87,23 +80,29 @@ pub fn print_ast_stats(krate: &ast::Crate, title: &str, prefix: &str) {
8780

8881
impl<'k> StatCollector<'k> {
8982
// Record a top-level node.
90-
fn record<T>(&mut self, label: &'static str, id: Id, val: &T) {
83+
fn record<T>(&mut self, label: &'static str, id: Option<HirId>, val: &T) {
9184
self.record_inner(label, None, id, val);
9285
}
9386

9487
// Record a two-level entry, with a top-level enum type and a variant.
95-
fn record_variant<T>(&mut self, label1: &'static str, label2: &'static str, id: Id, val: &T) {
88+
fn record_variant<T>(
89+
&mut self,
90+
label1: &'static str,
91+
label2: &'static str,
92+
id: Option<HirId>,
93+
val: &T,
94+
) {
9695
self.record_inner(label1, Some(label2), id, val);
9796
}
9897

9998
fn record_inner<T>(
10099
&mut self,
101100
label1: &'static str,
102101
label2: Option<&'static str>,
103-
id: Id,
102+
id: Option<HirId>,
104103
val: &T,
105104
) {
106-
if id != Id::None && !self.seen.insert(id) {
105+
if id.is_some_and(|x| !self.seen.insert(x)) {
107106
return;
108107
}
109108

@@ -191,7 +190,7 @@ macro_rules! record_variants {
191190

192191
impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
193192
fn visit_param(&mut self, param: &'v hir::Param<'v>) {
194-
self.record("Param", Id::Node(param.hir_id), param);
193+
self.record("Param", Some(param.hir_id), param);
195194
hir_visit::walk_param(self, param)
196195
}
197196

@@ -221,7 +220,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
221220
}
222221

223222
fn visit_item(&mut self, i: &'v hir::Item<'v>) {
224-
record_variants!((self, i, i.kind, Id::Node(i.hir_id()), hir, Item, ItemKind), [
223+
record_variants!((self, i, i.kind, Some(i.hir_id()), hir, Item, ItemKind), [
225224
ExternCrate,
226225
Use,
227226
Static,
@@ -243,47 +242,46 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
243242
}
244243

245244
fn visit_body(&mut self, b: &hir::Body<'v>) {
246-
self.record("Body", Id::None, b);
245+
self.record("Body", None, b);
247246
hir_visit::walk_body(self, b);
248247
}
249248

250249
fn visit_mod(&mut self, m: &'v hir::Mod<'v>, _s: Span, n: HirId) {
251-
self.record("Mod", Id::None, m);
250+
self.record("Mod", None, m);
252251
hir_visit::walk_mod(self, m, n)
253252
}
254253

255254
fn visit_foreign_item(&mut self, i: &'v hir::ForeignItem<'v>) {
256-
record_variants!(
257-
(self, i, i.kind, Id::Node(i.hir_id()), hir, ForeignItem, ForeignItemKind),
258-
[Fn, Static, Type]
259-
);
255+
record_variants!((self, i, i.kind, Some(i.hir_id()), hir, ForeignItem, ForeignItemKind), [
256+
Fn, Static, Type
257+
]);
260258
hir_visit::walk_foreign_item(self, i)
261259
}
262260

263261
fn visit_local(&mut self, l: &'v hir::LetStmt<'v>) {
264-
self.record("Local", Id::Node(l.hir_id), l);
262+
self.record("Local", Some(l.hir_id), l);
265263
hir_visit::walk_local(self, l)
266264
}
267265

268266
fn visit_block(&mut self, b: &'v hir::Block<'v>) {
269-
self.record("Block", Id::Node(b.hir_id), b);
267+
self.record("Block", Some(b.hir_id), b);
270268
hir_visit::walk_block(self, b)
271269
}
272270

273271
fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) {
274-
record_variants!((self, s, s.kind, Id::Node(s.hir_id), hir, Stmt, StmtKind), [
272+
record_variants!((self, s, s.kind, Some(s.hir_id), hir, Stmt, StmtKind), [
275273
Let, Item, Expr, Semi
276274
]);
277275
hir_visit::walk_stmt(self, s)
278276
}
279277

280278
fn visit_arm(&mut self, a: &'v hir::Arm<'v>) {
281-
self.record("Arm", Id::Node(a.hir_id), a);
279+
self.record("Arm", Some(a.hir_id), a);
282280
hir_visit::walk_arm(self, a)
283281
}
284282

285283
fn visit_pat(&mut self, p: &'v hir::Pat<'v>) {
286-
record_variants!((self, p, p.kind, Id::Node(p.hir_id), hir, Pat, PatKind), [
284+
record_variants!((self, p, p.kind, Some(p.hir_id), hir, Pat, PatKind), [
287285
Wild,
288286
Binding,
289287
Struct,
@@ -304,12 +302,12 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
304302
}
305303

306304
fn visit_pat_field(&mut self, f: &'v hir::PatField<'v>) {
307-
self.record("PatField", Id::Node(f.hir_id), f);
305+
self.record("PatField", Some(f.hir_id), f);
308306
hir_visit::walk_pat_field(self, f)
309307
}
310308

311309
fn visit_expr(&mut self, e: &'v hir::Expr<'v>) {
312-
record_variants!((self, e, e.kind, Id::Node(e.hir_id), hir, Expr, ExprKind), [
310+
record_variants!((self, e, e.kind, Some(e.hir_id), hir, Expr, ExprKind), [
313311
ConstBlock, Array, Call, MethodCall, Tup, Binary, Unary, Lit, Cast, Type, DropTemps,
314312
Let, If, Loop, Match, Closure, Block, Assign, AssignOp, Field, Index, Path, AddrOf,
315313
Break, Continue, Ret, Become, InlineAsm, OffsetOf, Struct, Repeat, Yield, Err
@@ -318,12 +316,12 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
318316
}
319317

320318
fn visit_expr_field(&mut self, f: &'v hir::ExprField<'v>) {
321-
self.record("ExprField", Id::Node(f.hir_id), f);
319+
self.record("ExprField", Some(f.hir_id), f);
322320
hir_visit::walk_expr_field(self, f)
323321
}
324322

325323
fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
326-
record_variants!((self, t, t.kind, Id::Node(t.hir_id), hir, Ty, TyKind), [
324+
record_variants!((self, t, t.kind, Some(t.hir_id), hir, Ty, TyKind), [
327325
InferDelegation,
328326
Slice,
329327
Array,
@@ -345,17 +343,17 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
345343
}
346344

347345
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
348-
self.record("GenericParam", Id::Node(p.hir_id), p);
346+
self.record("GenericParam", Some(p.hir_id), p);
349347
hir_visit::walk_generic_param(self, p)
350348
}
351349

352350
fn visit_generics(&mut self, g: &'v hir::Generics<'v>) {
353-
self.record("Generics", Id::None, g);
351+
self.record("Generics", None, g);
354352
hir_visit::walk_generics(self, g)
355353
}
356354

357355
fn visit_where_predicate(&mut self, p: &'v hir::WherePredicate<'v>) {
358-
record_variants!((self, p, p, Id::None, hir, WherePredicate, WherePredicate), [
356+
record_variants!((self, p, p, None, hir, WherePredicate, WherePredicate), [
359357
BoundPredicate,
360358
RegionPredicate,
361359
EqPredicate
@@ -371,66 +369,64 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
371369
_: Span,
372370
id: LocalDefId,
373371
) {
374-
self.record("FnDecl", Id::None, fd);
372+
self.record("FnDecl", None, fd);
375373
hir_visit::walk_fn(self, fk, fd, b, id)
376374
}
377375

378376
fn visit_use(&mut self, p: &'v hir::UsePath<'v>, hir_id: HirId) {
379377
// This is `visit_use`, but the type is `Path` so record it that way.
380-
self.record("Path", Id::None, p);
378+
self.record("Path", None, p);
381379
hir_visit::walk_use(self, p, hir_id)
382380
}
383381

384382
fn visit_trait_item(&mut self, ti: &'v hir::TraitItem<'v>) {
385-
record_variants!(
386-
(self, ti, ti.kind, Id::Node(ti.hir_id()), hir, TraitItem, TraitItemKind),
387-
[Const, Fn, Type]
388-
);
383+
record_variants!((self, ti, ti.kind, Some(ti.hir_id()), hir, TraitItem, TraitItemKind), [
384+
Const, Fn, Type
385+
]);
389386
hir_visit::walk_trait_item(self, ti)
390387
}
391388

392389
fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemRef) {
393-
self.record("TraitItemRef", Id::Node(ti.id.hir_id()), ti);
390+
self.record("TraitItemRef", Some(ti.id.hir_id()), ti);
394391
hir_visit::walk_trait_item_ref(self, ti)
395392
}
396393

397394
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
398-
record_variants!(
399-
(self, ii, ii.kind, Id::Node(ii.hir_id()), hir, ImplItem, ImplItemKind),
400-
[Const, Fn, Type]
401-
);
395+
record_variants!((self, ii, ii.kind, Some(ii.hir_id()), hir, ImplItem, ImplItemKind), [
396+
Const, Fn, Type
397+
]);
402398
hir_visit::walk_impl_item(self, ii)
403399
}
404400

405401
fn visit_foreign_item_ref(&mut self, fi: &'v hir::ForeignItemRef) {
406-
self.record("ForeignItemRef", Id::Node(fi.id.hir_id()), fi);
402+
self.record("ForeignItemRef", Some(fi.id.hir_id()), fi);
407403
hir_visit::walk_foreign_item_ref(self, fi)
408404
}
409405

410406
fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemRef) {
411-
self.record("ImplItemRef", Id::Node(ii.id.hir_id()), ii);
407+
self.record("ImplItemRef", Some(ii.id.hir_id()), ii);
412408
hir_visit::walk_impl_item_ref(self, ii)
413409
}
414410

415411
fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) {
416-
record_variants!((self, b, b, Id::None, hir, GenericBound, GenericBound), [
412+
record_variants!((self, b, b, None, hir, GenericBound, GenericBound), [
417413
Trait, Outlives, Use
418414
]);
419415
hir_visit::walk_param_bound(self, b)
420416
}
421417

422418
fn visit_field_def(&mut self, s: &'v hir::FieldDef<'v>) {
423-
self.record("FieldDef", Id::Node(s.hir_id), s);
419+
self.record("FieldDef", Some(s.hir_id), s);
424420
hir_visit::walk_field_def(self, s)
425421
}
426422

427423
fn visit_variant(&mut self, v: &'v hir::Variant<'v>) {
428-
self.record("Variant", Id::None, v);
424+
self.record("Variant", None, v);
429425
hir_visit::walk_variant(self, v)
430426
}
431427

432428
fn visit_generic_arg(&mut self, ga: &'v hir::GenericArg<'v>) {
433-
record_variants!((self, ga, ga, Id::Node(ga.hir_id()), hir, GenericArg, GenericArg), [
429+
record_variants!((self, ga, ga, Some(ga.hir_id()), hir, GenericArg, GenericArg), [
434430
Lifetime, Type, Const, Infer
435431
]);
436432
match ga {
@@ -442,50 +438,50 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
442438
}
443439

444440
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
445-
self.record("Lifetime", Id::Node(lifetime.hir_id), lifetime);
441+
self.record("Lifetime", Some(lifetime.hir_id), lifetime);
446442
hir_visit::walk_lifetime(self, lifetime)
447443
}
448444

449445
fn visit_path(&mut self, path: &hir::Path<'v>, _id: HirId) {
450-
self.record("Path", Id::None, path);
446+
self.record("Path", None, path);
451447
hir_visit::walk_path(self, path)
452448
}
453449

454450
fn visit_path_segment(&mut self, path_segment: &'v hir::PathSegment<'v>) {
455-
self.record("PathSegment", Id::None, path_segment);
451+
self.record("PathSegment", None, path_segment);
456452
hir_visit::walk_path_segment(self, path_segment)
457453
}
458454

459455
fn visit_generic_args(&mut self, ga: &'v hir::GenericArgs<'v>) {
460-
self.record("GenericArgs", Id::None, ga);
456+
self.record("GenericArgs", None, ga);
461457
hir_visit::walk_generic_args(self, ga)
462458
}
463459

464460
fn visit_assoc_item_constraint(&mut self, constraint: &'v hir::AssocItemConstraint<'v>) {
465-
self.record("AssocItemConstraint", Id::Node(constraint.hir_id), constraint);
461+
self.record("AssocItemConstraint", Some(constraint.hir_id), constraint);
466462
hir_visit::walk_assoc_item_constraint(self, constraint)
467463
}
468464

469465
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
470-
self.record("Attribute", Id::Attr(attr.id), attr);
466+
self.record("Attribute", None, attr);
471467
}
472468

473469
fn visit_inline_asm(&mut self, asm: &'v hir::InlineAsm<'v>, id: HirId) {
474-
self.record("InlineAsm", Id::None, asm);
470+
self.record("InlineAsm", None, asm);
475471
hir_visit::walk_inline_asm(self, asm, id);
476472
}
477473
}
478474

479475
impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
480476
fn visit_foreign_item(&mut self, i: &'v ast::ForeignItem) {
481-
record_variants!((self, i, i.kind, Id::None, ast, ForeignItem, ForeignItemKind), [
477+
record_variants!((self, i, i.kind, None, ast, ForeignItem, ForeignItemKind), [
482478
Static, Fn, TyAlias, MacCall
483479
]);
484480
ast_visit::walk_item(self, i)
485481
}
486482

487483
fn visit_item(&mut self, i: &'v ast::Item) {
488-
record_variants!((self, i, i.kind, Id::None, ast, Item, ItemKind), [
484+
record_variants!((self, i, i.kind, None, ast, Item, ItemKind), [
489485
ExternCrate,
490486
Use,
491487
Static,
@@ -510,34 +506,34 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
510506
}
511507

512508
fn visit_local(&mut self, l: &'v ast::Local) {
513-
self.record("Local", Id::None, l);
509+
self.record("Local", None, l);
514510
ast_visit::walk_local(self, l)
515511
}
516512

517513
fn visit_block(&mut self, b: &'v ast::Block) {
518-
self.record("Block", Id::None, b);
514+
self.record("Block", None, b);
519515
ast_visit::walk_block(self, b)
520516
}
521517

522518
fn visit_stmt(&mut self, s: &'v ast::Stmt) {
523-
record_variants!((self, s, s.kind, Id::None, ast, Stmt, StmtKind), [
519+
record_variants!((self, s, s.kind, None, ast, Stmt, StmtKind), [
524520
Let, Item, Expr, Semi, Empty, MacCall
525521
]);
526522
ast_visit::walk_stmt(self, s)
527523
}
528524

529525
fn visit_param(&mut self, p: &'v ast::Param) {
530-
self.record("Param", Id::None, p);
526+
self.record("Param", None, p);
531527
ast_visit::walk_param(self, p)
532528
}
533529

534530
fn visit_arm(&mut self, a: &'v ast::Arm) {
535-
self.record("Arm", Id::None, a);
531+
self.record("Arm", None, a);
536532
ast_visit::walk_arm(self, a)
537533
}
538534

539535
fn visit_pat(&mut self, p: &'v ast::Pat) {
540-
record_variants!((self, p, p.kind, Id::None, ast, Pat, PatKind), [
536+
record_variants!((self, p, p.kind, None, ast, Pat, PatKind), [
541537
Wild,
542538
Ident,
543539
Struct,
@@ -563,7 +559,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
563559
fn visit_expr(&mut self, e: &'v ast::Expr) {
564560
#[rustfmt::skip]
565561
record_variants!(
566-
(self, e, e.kind, Id::None, ast, Expr, ExprKind),
562+
(self, e, e.kind, None, ast, Expr, ExprKind),
567563
[
568564
Array, ConstBlock, Call, MethodCall, Tup, Binary, Unary, Lit, Cast, Type, Let,
569565
If, While, ForLoop, Loop, Match, Closure, Block, Await, TryBlock, Assign,
@@ -576,7 +572,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
576572
}
577573

578574
fn visit_ty(&mut self, t: &'v ast::Ty) {
579-
record_variants!((self, t, t.kind, Id::None, ast, Ty, TyKind), [
575+
record_variants!((self, t, t.kind, None, ast, Ty, TyKind), [
580576
Slice,
581577
Array,
582578
Ptr,
@@ -603,12 +599,12 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
603599
}
604600

605601
fn visit_generic_param(&mut self, g: &'v ast::GenericParam) {
606-
self.record("GenericParam", Id::None, g);
602+
self.record("GenericParam", None, g);
607603
ast_visit::walk_generic_param(self, g)
608604
}
609605

610606
fn visit_where_predicate(&mut self, p: &'v ast::WherePredicate) {
611-
record_variants!((self, p, p, Id::None, ast, WherePredicate, WherePredicate), [
607+
record_variants!((self, p, p, None, ast, WherePredicate, WherePredicate), [
612608
BoundPredicate,
613609
RegionPredicate,
614610
EqPredicate
@@ -617,12 +613,12 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
617613
}
618614

619615
fn visit_fn(&mut self, fk: ast_visit::FnKind<'v>, _: Span, _: NodeId) {
620-
self.record("FnDecl", Id::None, fk.decl());
616+
self.record("FnDecl", None, fk.decl());
621617
ast_visit::walk_fn(self, fk)
622618
}
623619

624620
fn visit_assoc_item(&mut self, i: &'v ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
625-
record_variants!((self, i, i.kind, Id::None, ast, AssocItem, AssocItemKind), [
621+
record_variants!((self, i, i.kind, None, ast, AssocItem, AssocItemKind), [
626622
Const,
627623
Fn,
628624
Type,
@@ -634,19 +630,19 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
634630
}
635631

636632
fn visit_param_bound(&mut self, b: &'v ast::GenericBound, _ctxt: BoundKind) {
637-
record_variants!((self, b, b, Id::None, ast, GenericBound, GenericBound), [
633+
record_variants!((self, b, b, None, ast, GenericBound, GenericBound), [
638634
Trait, Outlives, Use
639635
]);
640636
ast_visit::walk_param_bound(self, b)
641637
}
642638

643639
fn visit_field_def(&mut self, s: &'v ast::FieldDef) {
644-
self.record("FieldDef", Id::None, s);
640+
self.record("FieldDef", None, s);
645641
ast_visit::walk_field_def(self, s)
646642
}
647643

648644
fn visit_variant(&mut self, v: &'v ast::Variant) {
649-
self.record("Variant", Id::None, v);
645+
self.record("Variant", None, v);
650646
ast_visit::walk_variant(self, v)
651647
}
652648

@@ -660,7 +656,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
660656
// common than the former case, so we implement this visitor and tolerate
661657
// the double counting in the former case.
662658
fn visit_path_segment(&mut self, path_segment: &'v ast::PathSegment) {
663-
self.record("PathSegment", Id::None, path_segment);
659+
self.record("PathSegment", None, path_segment);
664660
ast_visit::walk_path_segment(self, path_segment)
665661
}
666662

@@ -669,7 +665,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
669665
// common, so we implement `visit_generic_args` and tolerate the double
670666
// counting in the former case.
671667
fn visit_generic_args(&mut self, g: &'v ast::GenericArgs) {
672-
record_variants!((self, g, g, Id::None, ast, GenericArgs, GenericArgs), [
668+
record_variants!((self, g, g, None, ast, GenericArgs, GenericArgs), [
673669
AngleBracketed,
674670
Parenthesized,
675671
ParenthesizedElided
@@ -678,24 +674,24 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
678674
}
679675

680676
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
681-
record_variants!((self, attr, attr.kind, Id::None, ast, Attribute, AttrKind), [
677+
record_variants!((self, attr, attr.kind, None, ast, Attribute, AttrKind), [
682678
Normal, DocComment
683679
]);
684680
ast_visit::walk_attribute(self, attr)
685681
}
686682

687683
fn visit_expr_field(&mut self, f: &'v ast::ExprField) {
688-
self.record("ExprField", Id::None, f);
684+
self.record("ExprField", None, f);
689685
ast_visit::walk_expr_field(self, f)
690686
}
691687

692688
fn visit_crate(&mut self, krate: &'v ast::Crate) {
693-
self.record("Crate", Id::None, krate);
689+
self.record("Crate", None, krate);
694690
ast_visit::walk_crate(self, krate)
695691
}
696692

697693
fn visit_inline_asm(&mut self, asm: &'v ast::InlineAsm) {
698-
self.record("InlineAsm", Id::None, asm);
694+
self.record("InlineAsm", None, asm);
699695
ast_visit::walk_inline_asm(self, asm)
700696
}
701697
}

0 commit comments

Comments
 (0)
This repository has been archived.