Skip to content

Commit 8c40360

Browse files
committed
Put checking if anonct is a default into a method on hir map
1 parent e276b86 commit 8c40360

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,9 @@ pub type Lit = Spanned<LitKind>;
14221422
/// These are usually found nested inside types (e.g., array lengths)
14231423
/// or expressions (e.g., repeat counts), and also used to define
14241424
/// explicit discriminant values for enum variants.
1425+
///
1426+
/// You can check if this anon const is a default in a const param
1427+
/// `const N: usize = { ... }` with [Map::opt_const_param_default_param_hir_id]
14251428
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
14261429
pub struct AnonConst {
14271430
pub hir_id: HirId,

compiler/rustc_middle/src/hir/map/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,19 @@ impl<'hir> Map<'hir> {
901901
pub fn node_to_string(&self, id: HirId) -> String {
902902
hir_id_to_string(self, id)
903903
}
904+
905+
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
906+
/// called with the HirId for the `{ ... }` anon const
907+
pub fn opt_const_param_default_param_hir_id(&self, anon_const: HirId) -> Option<HirId> {
908+
match self.get(self.get_parent_node(anon_const)) {
909+
Node::GenericParam(GenericParam {
910+
hir_id: param_id,
911+
kind: GenericParamKind::Const { .. },
912+
..
913+
}) => Some(*param_id),
914+
_ => None,
915+
}
916+
}
904917
}
905918

906919
impl<'hir> intravisit::Map<'hir> for Map<'hir> {

compiler/rustc_typeck/src/collect.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -1441,17 +1441,10 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
14411441
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
14421442
None
14431443
} else if tcx.lazy_normalization() {
1444-
// Only provide backwards declared generics to cg defaults (#83938)
1445-
if let Node::GenericParam(GenericParam {
1446-
hir_id: param_id,
1447-
kind: GenericParamKind::Const { .. },
1448-
..
1449-
}) = tcx.hir().get(tcx.hir().get_parent_node(hir_id))
1450-
{
1451-
let item_id = tcx.hir().get_parent_node(*param_id);
1452-
let item_def_id = tcx.hir().local_def_id(item_id);
1453-
let generics = tcx.generics_of(item_def_id.to_def_id());
1454-
let param_def = tcx.hir().local_def_id(*param_id).to_def_id();
1444+
// Only provide backwards declared generics to cg defaults (#86580)
1445+
if let Some(param_id) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) {
1446+
let generics = tcx.generics_of(parent_def_id.to_def_id());
1447+
let param_def = tcx.hir().local_def_id(param_id).to_def_id();
14551448
let param_def_idx = generics.param_def_id_to_index[&param_def];
14561449
let params = generics.params[..param_def_idx as usize].to_owned();
14571450
let param_def_id_to_index =
@@ -2432,16 +2425,11 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
24322425
}
24332426
} else {
24342427
if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
2435-
// Provide predicates of parent item of cg defaults manually
2436-
// as generics_of doesn't return a parent for the generics
2428+
// Provide predicates of parent item of cg defaults manually as `generics_of`
2429+
// doesn't set the parent item as the parent for the generics (#86580)
24372430
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
2438-
if let Node::GenericParam(hir::GenericParam {
2439-
hir_id: param_id,
2440-
kind: hir::GenericParamKind::Const { .. },
2441-
..
2442-
}) = tcx.hir().get(tcx.hir().get_parent_node(hir_id))
2443-
{
2444-
let item_id = tcx.hir().get_parent_node(*param_id);
2431+
if let Some(_) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) {
2432+
let item_id = tcx.hir().get_parent_item(hir_id);
24452433
let item_def_id = tcx.hir().local_def_id(item_id).to_def_id();
24462434
return tcx.explicit_predicates_of(item_def_id);
24472435
}

compiler/rustc_typeck/src/outlives/mod.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ 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-
// Provide inferred outlive preds of parent item of cg defaults manually
26-
// as generics_of doesn't return a parent for the generics
27-
if let Node::GenericParam(hir::GenericParam {
28-
hir_id: param_id,
29-
kind: hir::GenericParamKind::Const { .. },
30-
..
31-
}) = tcx.hir().get(tcx.hir().get_parent_node(id))
32-
{
33-
let item_id = tcx.hir().get_parent_node(*param_id);
25+
// Provide predicates of parent item of cg defaults manually as `generics_of`
26+
// doesn't set the parent item as the parent for the generics (#86580)
27+
if let Some(_) = tcx.hir().opt_const_param_default_param_hir_id(id) {
28+
let item_id = tcx.hir().get_parent_item(id);
3429
let item_def_id = tcx.hir().local_def_id(item_id).to_def_id();
3530
return tcx.inferred_outlives_of(item_def_id);
3631
}

0 commit comments

Comments
 (0)