From 8c40360ed413c0024787cf14fc83af0d98037c7a Mon Sep 17 00:00:00 2001 From: Ellen Date: Tue, 13 Jul 2021 17:23:26 +0100 Subject: [PATCH] Put checking if anonct is a default into a method on hir map --- compiler/rustc_hir/src/hir.rs | 3 +++ compiler/rustc_middle/src/hir/map/mod.rs | 13 +++++++++++ compiler/rustc_typeck/src/collect.rs | 28 +++++++---------------- compiler/rustc_typeck/src/outlives/mod.rs | 13 ++++------- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index a7ce92ea579..04c29c50e75 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1422,6 +1422,9 @@ pub type Lit = Spanned; /// These are usually found nested inside types (e.g., array lengths) /// or expressions (e.g., repeat counts), and also used to define /// explicit discriminant values for enum variants. +/// +/// You can check if this anon const is a default in a const param +/// `const N: usize = { ... }` with [Map::opt_const_param_default_param_hir_id] #[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)] pub struct AnonConst { pub hir_id: HirId, diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 07b39c97c49..9d81407c330 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -901,6 +901,19 @@ impl<'hir> Map<'hir> { pub fn node_to_string(&self, id: HirId) -> String { hir_id_to_string(self, id) } + + /// Returns the HirId of `N` in `struct Foo` when + /// called with the HirId for the `{ ... }` anon const + pub fn opt_const_param_default_param_hir_id(&self, anon_const: HirId) -> Option { + match self.get(self.get_parent_node(anon_const)) { + Node::GenericParam(GenericParam { + hir_id: param_id, + kind: GenericParamKind::Const { .. }, + .. + }) => Some(*param_id), + _ => None, + } + } } impl<'hir> intravisit::Map<'hir> for Map<'hir> { diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 924a0b8410a..31cafb9d966 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -1441,17 +1441,10 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { // of a const parameter type, e.g. `struct Foo` is not allowed. None } else if tcx.lazy_normalization() { - // Only provide backwards declared generics to cg defaults (#83938) - if let Node::GenericParam(GenericParam { - hir_id: param_id, - kind: GenericParamKind::Const { .. }, - .. - }) = tcx.hir().get(tcx.hir().get_parent_node(hir_id)) - { - let item_id = tcx.hir().get_parent_node(*param_id); - let item_def_id = tcx.hir().local_def_id(item_id); - let generics = tcx.generics_of(item_def_id.to_def_id()); - let param_def = tcx.hir().local_def_id(*param_id).to_def_id(); + // Only provide backwards declared generics to cg defaults (#86580) + if let Some(param_id) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) { + let generics = tcx.generics_of(parent_def_id.to_def_id()); + let param_def = tcx.hir().local_def_id(param_id).to_def_id(); let param_def_idx = generics.param_def_id_to_index[¶m_def]; let params = generics.params[..param_def_idx as usize].to_owned(); let param_def_id_to_index = @@ -2432,16 +2425,11 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } } else { if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() { - // Provide predicates of parent item of cg defaults manually - // as generics_of doesn't return a parent for the generics + // Provide predicates of parent item of cg defaults manually as `generics_of` + // doesn't set the parent item as the parent for the generics (#86580) let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - if let Node::GenericParam(hir::GenericParam { - hir_id: param_id, - kind: hir::GenericParamKind::Const { .. }, - .. - }) = tcx.hir().get(tcx.hir().get_parent_node(hir_id)) - { - let item_id = tcx.hir().get_parent_node(*param_id); + if let Some(_) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) { + let item_id = tcx.hir().get_parent_item(hir_id); let item_def_id = tcx.hir().local_def_id(item_id).to_def_id(); return tcx.explicit_predicates_of(item_def_id); } diff --git a/compiler/rustc_typeck/src/outlives/mod.rs b/compiler/rustc_typeck/src/outlives/mod.rs index c2f8525f7ab..16d698fc8cb 100644 --- a/compiler/rustc_typeck/src/outlives/mod.rs +++ b/compiler/rustc_typeck/src/outlives/mod.rs @@ -22,15 +22,10 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization() { - // Provide inferred outlive preds of parent item of cg defaults manually - // as generics_of doesn't return a parent for the generics - if let Node::GenericParam(hir::GenericParam { - hir_id: param_id, - kind: hir::GenericParamKind::Const { .. }, - .. - }) = tcx.hir().get(tcx.hir().get_parent_node(id)) - { - let item_id = tcx.hir().get_parent_node(*param_id); + // Provide predicates of parent item of cg defaults manually as `generics_of` + // doesn't set the parent item as the parent for the generics (#86580) + if let Some(_) = tcx.hir().opt_const_param_default_param_hir_id(id) { + let item_id = tcx.hir().get_parent_item(id); let item_def_id = tcx.hir().local_def_id(item_id).to_def_id(); return tcx.inferred_outlives_of(item_def_id); }