Skip to content

Commit a9ddedf

Browse files
Rollup merge of #162937 - khyperia:gca-fix-unreachable, r=BoxyUwU
do generic_const_args Const::Ty hack only on projection consts Under `generic_const_args`, we currently do a bit of a hack to lower all consts to `Const::Ty`, because under `generic_const_args`, projection consts not marked with `#[rustc_always_gca]` could be `impl`'d by a const with a `gca!` rhs, and `Const::Unevaluated` does not support direct consts. Instead, only lower a const to `Const::Ty` if: - it is a direct const (under `min_generic_const_args`), OR - it is a projection const (under `generic_const_args`) This excludes all anon consts, and regular (not direct) free and inherent consts from being lowered as `Const::Ty` under full GCA. It does not fix the underlying issue that lowering a potentially regular projection const to `Const::Ty` is incorrect. Fixes #162923 Introduced in #162760 r? @BoxyUwU
2 parents be3bb9f + acc7501 commit a9ddedf

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

‎compiler/rustc_mir_build/src/builder/expr/as_constant.rs‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,41 @@ pub(crate) fn as_constant_inner<'tcx>(
7474
ExprKind::NamedConst { def_id, args, ref user_ty } => {
7575
let user_ty = user_ty.as_ref().and_then(push_cuta);
7676

77-
// Under generic_const_args, `def_id` might be a regular const declared in a trait, but
78-
// is `impl`d as a directly represented const. We do not know whether it is here, so we
79-
// must use type system normalization for all consts under generic_const_args.
80-
// FIXME(generic_const_args): there's a lot to consider here! `Const::Ty` uses valtrees
81-
// and `Const::Unevaluated` does not, we should revisit this before stabilization.
82-
let def_kind = tcx.def_kind(def_id);
83-
if tcx.features().generic_const_args()
84-
|| matches!(def_kind, DefKind::Const | DefKind::AssocConst)
85-
&& tcx.is_direct_const(def_id)
86-
{
87-
let kind = match def_kind {
88-
DefKind::AssocConst => {
89-
if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(def_id))
90-
{
91-
ty::AliasConstKind::InherentImpl { def_id }
92-
} else {
93-
ty::AliasConstKind::Projection { def_id }
94-
}
77+
let get_kind = |def_id, def_kind| match def_kind {
78+
DefKind::AssocConst => {
79+
if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(def_id)) {
80+
ty::AliasConstKind::InherentImpl { def_id }
81+
} else {
82+
ty::AliasConstKind::Projection { def_id }
9583
}
96-
DefKind::Const => ty::AliasConstKind::Free { def_id },
97-
_ => unreachable!(),
84+
}
85+
DefKind::Const => ty::AliasConstKind::Free { def_id },
86+
kind => bug!("unexpected DefKind in THIR ExprKind::NamedConst: {kind:?}"),
87+
};
88+
89+
let could_be_direct_const = |def_id| {
90+
let def_kind = tcx.def_kind(def_id);
91+
let (DefKind::Const | DefKind::AssocConst) = def_kind else {
92+
return None;
9893
};
94+
if tcx.is_direct_const(def_id) {
95+
return Some(get_kind(def_id, def_kind));
96+
}
97+
// Under generic_const_args, `def_id` might be a regular const declared in a trait,
98+
// but is `impl`d as a directly represented const. We do not know whether it is
99+
// here, so we must use type system normalization for all const projections.
100+
// FIXME(generic_const_args): there's a lot to consider here! `Const::Ty` uses
101+
// valtrees and `Const::Unevaluated` does not, we should revisit this before
102+
// stabilization.
103+
if tcx.features().generic_const_args()
104+
&& let kind @ ty::AliasConstKind::Projection { .. } = get_kind(def_id, def_kind)
105+
{
106+
return Some(kind);
107+
}
108+
None
109+
};
110+
111+
if let Some(kind) = could_be_direct_const(def_id) {
99112
let alias = ty::AliasConst::new(tcx, kind, args);
100113
let ct = ty::Const::new_alias(tcx, ty::IsRigid::No, alias);
101114
let const_ = Const::Ty(ty, ct);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-pass
2+
//@ compile-flags: -Znext-solver
3+
//! https://github.com/rust-lang/rust/issues/162923
4+
#![feature(min_generic_const_args)]
5+
#![feature(generic_const_args)]
6+
enum T<const N: u8 = { T::<0>::B as u8 }> {
7+
A = 2,
8+
B,
9+
}
10+
fn main() {}

0 commit comments

Comments
 (0)