Skip to content

Commit 18482f7

Browse files
committed
Store a LocalDefId in hir::GenericParam.
1 parent e82c08f commit 18482f7

File tree

19 files changed

+67
-88
lines changed

19 files changed

+67
-88
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
830830
),
831831
};
832832
let hir_id = self.lower_node_id(node_id);
833+
let def_id = self.local_def_id(node_id);
833834
Some(hir::GenericParam {
834835
hir_id,
836+
def_id,
835837
name,
836838
span: self.lower_span(ident.span),
837839
pure_wrt_drop: false,
@@ -1521,6 +1523,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15211523

15221524
hir::GenericParam {
15231525
hir_id,
1526+
def_id: lctx.local_def_id(new_node_id),
15241527
name,
15251528
span: lifetime.ident.span,
15261529
pure_wrt_drop: false,
@@ -1978,6 +1981,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19781981

19791982
hir::GenericParam {
19801983
hir_id,
1984+
def_id: this.local_def_id(new_node_id),
19811985
name,
19821986
span: lifetime.ident.span,
19831987
pure_wrt_drop: false,
@@ -2176,6 +2180,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21762180
self.lower_attrs(hir_id, &param.attrs);
21772181
hir::GenericParam {
21782182
hir_id,
2183+
def_id: self.local_def_id(param.id),
21792184
name,
21802185
span: self.lower_span(param.span()),
21812186
pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle),
@@ -2280,6 +2285,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22802285
// Set the name to `impl Bound1 + Bound2`.
22812286
let param = hir::GenericParam {
22822287
hir_id: self.lower_node_id(node_id),
2288+
def_id,
22832289
name: ParamName::Plain(self.lower_ident(ident)),
22842290
pure_wrt_drop: false,
22852291
span: self.lower_span(span),

compiler/rustc_hir/src/hir.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ pub enum GenericParamKind<'hir> {
487487
#[derive(Debug, HashStable_Generic)]
488488
pub struct GenericParam<'hir> {
489489
pub hir_id: HirId,
490+
pub def_id: LocalDefId,
490491
pub name: ParamName,
491492
pub span: Span,
492493
pub pure_wrt_drop: bool,
@@ -1628,7 +1629,7 @@ impl ArrayLen {
16281629
/// explicit discriminant values for enum variants.
16291630
///
16301631
/// You can check if this anon const is a default in a const param
1631-
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_hir_id(..)`
1632+
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)`
16321633
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
16331634
pub struct AnonConst {
16341635
pub hir_id: HirId,

compiler/rustc_hir_analysis/src/astconv/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
856856
&self,
857857
bounds: &mut Bounds<'hir>,
858858
ast_bounds: &'hir [hir::GenericBound<'hir>],
859-
self_ty_where_predicates: Option<(hir::HirId, &'hir [hir::WherePredicate<'hir>])>,
859+
self_ty_where_predicates: Option<(LocalDefId, &'hir [hir::WherePredicate<'hir>])>,
860860
span: Span,
861861
) {
862862
let tcx = self.tcx();
@@ -876,10 +876,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
876876
};
877877
search_bounds(ast_bounds);
878878
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
879-
let self_ty_def_id = tcx.hir().local_def_id(self_ty).to_def_id();
880879
for clause in where_clause {
881880
if let hir::WherePredicate::BoundPredicate(pred) = clause {
882-
if pred.is_param_bound(self_ty_def_id) {
881+
if pred.is_param_bound(self_ty.to_def_id()) {
883882
search_bounds(pred.bounds);
884883
}
885884
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
853853

854854
// Const parameters are well formed if their type is structural match.
855855
hir::GenericParamKind::Const { ty: hir_ty, default: _ } => {
856-
let ty = tcx.type_of(tcx.hir().local_def_id(param.hir_id));
856+
let ty = tcx.type_of(param.def_id);
857857

858858
if tcx.features().adt_const_params {
859859
if let Some(non_structural_match_ty) =

compiler/rustc_hir_analysis/src/collect.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,16 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
291291
match param.kind {
292292
hir::GenericParamKind::Lifetime { .. } => {}
293293
hir::GenericParamKind::Type { default: Some(_), .. } => {
294-
let def_id = self.tcx.hir().local_def_id(param.hir_id);
295-
self.tcx.ensure().type_of(def_id);
294+
self.tcx.ensure().type_of(param.def_id);
296295
}
297296
hir::GenericParamKind::Type { .. } => {}
298297
hir::GenericParamKind::Const { default, .. } => {
299-
let def_id = self.tcx.hir().local_def_id(param.hir_id);
300-
self.tcx.ensure().type_of(def_id);
298+
self.tcx.ensure().type_of(param.def_id);
301299
if let Some(default) = default {
302300
let default_def_id = self.tcx.hir().local_def_id(default.hir_id);
303301
// need to store default and type of default
304302
self.tcx.ensure().type_of(default_def_id);
305-
self.tcx.ensure().const_param_default(def_id);
303+
self.tcx.ensure().const_param_default(param.def_id);
306304
}
307305
}
308306
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
5151
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
5252
None
5353
} else if tcx.lazy_normalization() {
54-
if let Some(param_id) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) {
54+
if let Some(param_id) = tcx.hir().opt_const_param_default_param_def_id(hir_id) {
5555
// If the def_id we are calling generics_of on is an anon ct default i.e:
5656
//
5757
// struct Foo<const N: usize = { .. }>;
@@ -77,8 +77,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
7777
// This has some implications for how we get the predicates available to the anon const
7878
// see `explicit_predicates_of` for more information on this
7979
let generics = tcx.generics_of(parent_def_id.to_def_id());
80-
let param_def = tcx.hir().local_def_id(param_id).to_def_id();
81-
let param_def_idx = generics.param_def_id_to_index[&param_def];
80+
let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
8281
// In the above example this would be .params[..N#0]
8382
let params = generics.params[..param_def_idx as usize].to_owned();
8483
let param_def_id_to_index =
@@ -241,7 +240,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
241240
params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
242241
name: param.name.ident().name,
243242
index: own_start + i as u32,
244-
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
243+
def_id: param.def_id.to_def_id(),
245244
pure_wrt_drop: param.pure_wrt_drop,
246245
kind: ty::GenericParamDefKind::Lifetime,
247246
}));
@@ -286,7 +285,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
286285
Some(ty::GenericParamDef {
287286
index: next_index(),
288287
name: param.name.ident().name,
289-
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
288+
def_id: param.def_id.to_def_id(),
290289
pure_wrt_drop: param.pure_wrt_drop,
291290
kind,
292291
})
@@ -303,7 +302,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
303302
Some(ty::GenericParamDef {
304303
index: next_index(),
305304
name: param.name.ident().name,
306-
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
305+
def_id: param.def_id.to_def_id(),
307306
pure_wrt_drop: param.pure_wrt_drop,
308307
kind: ty::GenericParamDefKind::Const { has_default: default.is_some() },
309308
})

compiler/rustc_hir_analysis/src/collect/lifetimes.rs

+19-29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_hir::def_id::LocalDefId;
1515
use rustc_hir::intravisit::{self, Visitor};
1616
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeName, Node};
1717
use rustc_middle::bug;
18-
use rustc_middle::hir::map::Map;
1918
use rustc_middle::hir::nested_filter;
2019
use rustc_middle::middle::resolve_lifetime::*;
2120
use rustc_middle::ty::{self, DefIdTree, TyCtxt, TypeSuperVisitable, TypeVisitor};
@@ -25,30 +24,28 @@ use rustc_span::Span;
2524
use std::fmt;
2625

2726
trait RegionExt {
28-
fn early(hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region);
27+
fn early(param: &GenericParam<'_>) -> (LocalDefId, Region);
2928

30-
fn late(index: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region);
29+
fn late(index: u32, param: &GenericParam<'_>) -> (LocalDefId, Region);
3130

3231
fn id(&self) -> Option<DefId>;
3332

3433
fn shifted(self, amount: u32) -> Region;
3534
}
3635

3736
impl RegionExt for Region {
38-
fn early(hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) {
39-
let def_id = hir_map.local_def_id(param.hir_id);
40-
debug!("Region::early: def_id={:?}", def_id);
41-
(def_id, Region::EarlyBound(def_id.to_def_id()))
37+
fn early(param: &GenericParam<'_>) -> (LocalDefId, Region) {
38+
debug!("Region::early: def_id={:?}", param.def_id);
39+
(param.def_id, Region::EarlyBound(param.def_id.to_def_id()))
4240
}
4341

44-
fn late(idx: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) {
42+
fn late(idx: u32, param: &GenericParam<'_>) -> (LocalDefId, Region) {
4543
let depth = ty::INNERMOST;
46-
let def_id = hir_map.local_def_id(param.hir_id);
4744
debug!(
4845
"Region::late: idx={:?}, param={:?} depth={:?} def_id={:?}",
49-
idx, param, depth, def_id,
46+
idx, param, depth, param.def_id,
5047
);
51-
(def_id, Region::LateBound(depth, idx, def_id.to_def_id()))
48+
(param.def_id, Region::LateBound(depth, idx, param.def_id.to_def_id()))
5249
}
5350

5451
fn id(&self) -> Option<DefId> {
@@ -395,7 +392,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
395392
.filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
396393
.enumerate()
397394
.map(|(late_bound_idx, param)| {
398-
let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
395+
let pair = Region::late(late_bound_idx as u32, param);
399396
let r = late_region_as_bound_region(self.tcx, &pair.1);
400397
(pair, r)
401398
})
@@ -492,7 +489,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
492489
for param in generics.params {
493490
match param.kind {
494491
GenericParamKind::Lifetime { .. } => {
495-
let (def_id, reg) = Region::early(self.tcx.hir(), &param);
492+
let (def_id, reg) = Region::early(&param);
496493
lifetimes.insert(def_id, reg);
497494
}
498495
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {}
@@ -523,9 +520,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
523520
.params
524521
.iter()
525522
.filter_map(|param| match param.kind {
526-
GenericParamKind::Lifetime { .. } => {
527-
Some(Region::early(self.tcx.hir(), param))
528-
}
523+
GenericParamKind::Lifetime { .. } => Some(Region::early(param)),
529524
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None,
530525
})
531526
.collect();
@@ -573,7 +568,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
573568
.filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
574569
.enumerate()
575570
.map(|(late_bound_idx, param)| {
576-
let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
571+
let pair = Region::late(late_bound_idx as u32, param);
577572
let r = late_region_as_bound_region(self.tcx, &pair.1);
578573
(pair, r)
579574
})
@@ -731,9 +726,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
731726
.params
732727
.iter()
733728
.filter_map(|param| match param.kind {
734-
GenericParamKind::Lifetime { .. } => {
735-
Some(Region::early(self.tcx.hir(), param))
736-
}
729+
GenericParamKind::Lifetime { .. } => Some(Region::early(param)),
737730
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None,
738731
})
739732
.collect();
@@ -779,9 +772,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
779772
.params
780773
.iter()
781774
.filter_map(|param| match param.kind {
782-
GenericParamKind::Lifetime { .. } => {
783-
Some(Region::early(self.tcx.hir(), param))
784-
}
775+
GenericParamKind::Lifetime { .. } => Some(Region::early(param)),
785776
GenericParamKind::Const { .. } | GenericParamKind::Type { .. } => None,
786777
})
787778
.collect();
@@ -886,7 +877,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
886877
})
887878
.enumerate()
888879
.map(|(late_bound_idx, param)| {
889-
Region::late(late_bound_idx as u32, this.tcx.hir(), param)
880+
Region::late(late_bound_idx as u32, param)
890881
})
891882
.collect();
892883
let binders: Vec<_> =
@@ -999,8 +990,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
999990
.filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
1000991
.enumerate()
1001992
.map(|(late_bound_idx, param)| {
1002-
let pair =
1003-
Region::late(initial_bound_vars + late_bound_idx as u32, self.tcx.hir(), param);
993+
let pair = Region::late(initial_bound_vars + late_bound_idx as u32, param);
1004994
let r = late_region_as_bound_region(self.tcx, &pair.1);
1005995
lifetimes.insert(pair.0, pair.1);
1006996
r
@@ -1131,9 +1121,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
11311121
if self.tcx.is_late_bound(param.hir_id) {
11321122
let late_bound_idx = named_late_bound_vars;
11331123
named_late_bound_vars += 1;
1134-
Some(Region::late(late_bound_idx, self.tcx.hir(), param))
1124+
Some(Region::late(late_bound_idx, param))
11351125
} else {
1136-
Some(Region::early(self.tcx.hir(), param))
1126+
Some(Region::early(param))
11371127
}
11381128
}
11391129
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None,
@@ -1149,7 +1139,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
11491139
})
11501140
.enumerate()
11511141
.map(|(late_bound_idx, param)| {
1152-
let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
1142+
let pair = Region::late(late_bound_idx as u32, param);
11531143
late_region_as_bound_region(self.tcx, &pair.1)
11541144
})
11551145
.collect();

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
199199
&icx,
200200
&mut bounds,
201201
&[],
202-
Some((param.hir_id, ast_generics.predicates)),
202+
Some((param.def_id, ast_generics.predicates)),
203203
param.span,
204204
);
205205
trace!(?bounds);
@@ -429,7 +429,7 @@ pub(super) fn explicit_predicates_of<'tcx>(
429429
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
430430
let parent_def_id = tcx.hir().get_parent_item(hir_id);
431431

432-
if tcx.hir().opt_const_param_default_param_hir_id(hir_id).is_some() {
432+
if tcx.hir().opt_const_param_default_param_def_id(hir_id).is_some() {
433433
// In `generics_of` we set the generics' parent to be our parent's parent which means that
434434
// we lose out on the predicates of our actual parent if we dont return those predicates here.
435435
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
@@ -531,7 +531,7 @@ pub(super) fn super_predicates_that_define_assoc_type(
531531
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
532532
let superbounds2 = icx.type_parameter_bounds_in_generics(
533533
generics,
534-
item.hir_id(),
534+
item.owner_id.def_id,
535535
self_param_ty,
536536
OnlySelfBounds(!is_trait_alias),
537537
assoc_name,
@@ -641,7 +641,7 @@ pub(super) fn type_param_predicates(
641641
let extra_predicates = extend.into_iter().chain(
642642
icx.type_parameter_bounds_in_generics(
643643
ast_generics,
644-
param_id,
644+
def_id,
645645
ty,
646646
OnlySelfBounds(true),
647647
Some(assoc_name),
@@ -666,13 +666,11 @@ impl<'tcx> ItemCtxt<'tcx> {
666666
fn type_parameter_bounds_in_generics(
667667
&self,
668668
ast_generics: &'tcx hir::Generics<'tcx>,
669-
param_id: hir::HirId,
669+
param_def_id: LocalDefId,
670670
ty: Ty<'tcx>,
671671
only_self_bounds: OnlySelfBounds,
672672
assoc_name: Option<Ident>,
673673
) -> Vec<(ty::Predicate<'tcx>, Span)> {
674-
let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id();
675-
trace!(?param_def_id);
676674
ast_generics
677675
.predicates
678676
.iter()
@@ -681,7 +679,7 @@ impl<'tcx> ItemCtxt<'tcx> {
681679
_ => None,
682680
})
683681
.flat_map(|bp| {
684-
let bt = if bp.is_param_bound(param_def_id) {
682+
let bt = if bp.is_param_bound(param_def_id.to_def_id()) {
685683
Some(ty)
686684
} else if !only_self_bounds.0 {
687685
Some(self.to_ty(bp.bounded_ty))

compiler/rustc_hir_analysis/src/collect/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,10 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
514514
}
515515

516516
Node::GenericParam(&GenericParam {
517-
hir_id: param_hir_id,
517+
def_id: param_def_id,
518518
kind: GenericParamKind::Const { default: Some(ct), .. },
519519
..
520-
}) if ct.hir_id == hir_id => tcx.type_of(tcx.hir().local_def_id(param_hir_id)),
520+
}) if ct.hir_id == hir_id => tcx.type_of(param_def_id),
521521

522522
x => tcx.ty_error_with_message(
523523
DUMMY_SP,

compiler/rustc_hir_analysis/src/outlives/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate
2222

2323
if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization()
2424
{
25-
if tcx.hir().opt_const_param_default_param_hir_id(id).is_some() {
25+
if tcx.hir().opt_const_param_default_param_def_id(id).is_some() {
2626
// In `generics_of` we set the generics' parent to be our parent's parent which means that
2727
// we lose out on the predicates of our actual parent if we dont return those predicates here.
2828
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)

0 commit comments

Comments
 (0)