Skip to content

Commit 2e0a6e5

Browse files
committed
Inherit generics correctly for anon const as const item RHS
1 parent 5ea917f commit 2e0a6e5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
8787
// would implicitly have a closure in its body that would be the parent of
8888
// the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
8989
// instead of `['a]`.
90-
let parent_did = if let DefKind::AnonConst = tcx.def_kind(parent_did) {
90+
let parent_def_kind = tcx.def_kind(parent_did);
91+
let parent_did = if let DefKind::AnonConst = parent_def_kind {
9192
parent_did
9293
} else {
9394
tcx.hir_get_parent_item(hir_id).to_def_id()
@@ -105,6 +106,13 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
105106
}
106107

107108
match tcx.anon_const_kind(def_id) {
109+
// Stable: the RHS of a const item is desugared to be either a const path or an anon const.
110+
// If it's an anon const, it should inherit the const item's generics.
111+
ty::AnonConstKind::MCG
112+
if matches!(parent_def_kind, DefKind::Const | DefKind::AssocConst) =>
113+
{
114+
Some(parent_did)
115+
}
108116
// Stable: anon consts are not able to use any generic parameters...
109117
ty::AnonConstKind::MCG => None,
110118
// we provide generics to repeat expr counts as a backwards compatibility hack. #76200

0 commit comments

Comments
 (0)