Skip to content

Commit

Permalink
Put checking if anonct is a default into a method on hir map
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jul 13, 2021
1 parent e276b86 commit 8c40360
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
28 changes: 8 additions & 20 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_typeck/src/outlives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8c40360

Please sign in to comment.