Put checking if anonct is a default into a method on hir map

This commit is contained in:
Ellen 2021-07-13 17:23:26 +01:00
parent e276b860e2
commit 8c40360ed4
4 changed files with 28 additions and 29 deletions

View File

@ -1422,6 +1422,9 @@ pub type Lit = Spanned<LitKind>;
/// 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,

View File

@ -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<const N: usize = { ... }>` when
/// called with the HirId for the `{ ... }` anon const
pub fn opt_const_param_default_param_hir_id(&self, anon_const: HirId) -> Option<HirId> {
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> {

View File

@ -1441,17 +1441,10 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` 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[&param_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);
}

View File

@ -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);
}