Skip to content

Commit f10a9e0

Browse files
committed
Improve handling of item bodies in generics_of
1 parent c949908 commit f10a9e0

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,14 +1527,27 @@ fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKin
15271527
let const_arg_id = tcx.parent_hir_id(hir_id);
15281528
match tcx.hir_node(const_arg_id) {
15291529
hir::Node::ConstArg(_) => {
1530-
if tcx.features().generic_const_exprs() {
1530+
let parent_hir_node = tcx.hir_node(tcx.parent_hir_id(const_arg_id));
1531+
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(.., body), .. })
1532+
| hir::Node::TraitItem(hir::TraitItem {
1533+
kind: hir::TraitItemKind::Const(.., Some(body)),
1534+
..
1535+
})
1536+
| hir::Node::ImplItem(hir::ImplItem {
1537+
kind: hir::ImplItemKind::Const(.., body),
1538+
..
1539+
}) = parent_hir_node
1540+
&& body.hir_id == const_arg_id
1541+
{
1542+
return ty::AnonConstKind::ItemBody;
1543+
} else if tcx.features().generic_const_exprs() {
15311544
ty::AnonConstKind::GCE
15321545
} else if tcx.features().min_generic_const_args() {
15331546
ty::AnonConstKind::MCG
15341547
} else if let hir::Node::Expr(hir::Expr {
15351548
kind: hir::ExprKind::Repeat(_, repeat_count),
15361549
..
1537-
}) = tcx.hir_node(tcx.parent_hir_id(const_arg_id))
1550+
}) = parent_hir_node
15381551
&& repeat_count.hir_id == const_arg_id
15391552
{
15401553
ty::AnonConstKind::RepeatExprCount

compiler/rustc_hir_analysis/src/collect/generics_of.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
7777
// stable enough and does not need a feature gate anymore.
7878
Node::AnonConst(_) => {
7979
let parent_did = tcx.parent(def_id.to_def_id());
80-
81-
// We don't do this unconditionally because the `DefId` parent of an anon const
82-
// might be an implicitly created closure during `async fn` desugaring. This would
83-
// have the wrong generics.
84-
//
85-
// i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }`
86-
// would implicitly have a closure in its body that would be the parent of
87-
// the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
88-
// instead of `['a]`.
89-
let parent_def_kind = tcx.def_kind(parent_did);
90-
let parent_did = if let DefKind::AnonConst = parent_def_kind {
91-
parent_did
92-
} else {
93-
tcx.hir_get_parent_item(hir_id).to_def_id()
94-
};
9580
debug!(?parent_did);
9681

9782
let mut in_param_ty = false;
@@ -107,11 +92,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
10792
match tcx.anon_const_kind(def_id) {
10893
// Stable: the RHS of a const item is desugared to be either a const path or an anon const.
10994
// If it's an anon const, it should inherit the const item's generics.
110-
ty::AnonConstKind::MCG
111-
if matches!(parent_def_kind, DefKind::Const | DefKind::AssocConst) =>
112-
{
113-
Some(parent_did)
114-
}
95+
ty::AnonConstKind::ItemBody => Some(parent_did),
11596
// Stable: anon consts are not able to use any generic parameters...
11697
ty::AnonConstKind::MCG => None,
11798
// we provide generics to repeat expr counts as a backwards compatibility hack. #76200

compiler/rustc_middle/src/mir/interpret/queries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl<'tcx> TyCtxt<'tcx> {
134134
// If we don't *only* FCW anon consts we can wind up incorrectly FCW'ing uses of assoc
135135
// consts in pattern positions. #140447
136136
&& self.def_kind(cid.instance.def_id()) == DefKind::AnonConst
137+
&& self.anon_const_kind(cid.instance.def_id()) == ty::AnonConstKind::RepeatExprCount
137138
{
138139
let mir_body = self.mir_for_ctfe(cid.instance.def_id());
139140
if mir_body.is_polymorphic {

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ pub enum AnonConstKind {
287287
GCE,
288288
/// stable `min_const_generics` anon consts are not allowed to use any generic parameters
289289
MCG,
290+
/// We sometimes use anon consts for the bodies of free and associated const items,
291+
/// and they should be handled differently from anon consts used in repeat exprs and const args.
292+
///
293+
/// The bodies are allowed to use generics from the parent, whereas anon consts in the type
294+
/// of the const item have more restrictions.
295+
ItemBody,
290296
/// anon consts used as the length of a repeat expr are syntactically allowed to use generic parameters
291297
/// but must not depend on the actual instantiation. See #76200 for more information
292298
RepeatExprCount,

0 commit comments

Comments
 (0)