Skip to content

Commit 6bdf430

Browse files
committed
Auto merge of #157094 - khyperia:UnevaluatedConstKind, r=BoxyUwU
introduce UnevaluatedConstKind (no rush on this, boxy, if you get this notif, I know you're on break after rustweek) Partially fixes rust-lang/project-const-generics#98 - does not unify GCE `ConstKind::Expr` into Unevaluated, which is part of that issue - does not do more advanced refactoring to take advantage of the new kind repr, only refactoring when things are very obviously able to take advantage - does not change MIR's UnevaluatedConst to use UnevaluatedConstKind, as there's currently no benefit, and doing so instead overcomplicates many places. Possibly could be done in a followup, or I can do it here if desired. - more advanced potentially behavior-changing changes can be done in a followup, as this diff is big enough as it is, IMO. same with merging GCE Exprs into Unevaluated. - (... but if this takes a while to get reviewed, I might plop some more stuff into this PR) - I think it would be cute to rename UnevaluatedConst to AliasConst or something, to better reflect the parallels to AliasTy, but shrug, haha r? @BoxyUwU
2 parents c0bb140 + 8777302 commit 6bdf430

48 files changed

Lines changed: 385 additions & 232 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,9 +1760,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
17601760
let tcx = self.tcx();
17611761
let maybe_uneval = match constant.const_ {
17621762
Const::Ty(_, ct) => match ct.kind() {
1763-
ty::ConstKind::Unevaluated(uv) => {
1764-
Some(UnevaluatedConst { def: uv.def, args: uv.args, promoted: None })
1765-
}
1763+
ty::ConstKind::Unevaluated(uv) => Some(UnevaluatedConst {
1764+
def: uv.kind.def_id(),
1765+
args: uv.args,
1766+
promoted: None,
1767+
}),
17661768
_ => None,
17671769
},
17681770
Const::Unevaluated(uv, _) => Some(uv),

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3838
&self,
3939
constant: &mir::ConstOperand<'tcx>,
4040
) -> Result<Result<ty::ValTree<'tcx>, Ty<'tcx>>, ErrorHandled> {
41+
let tcx = self.cx.tcx();
4142
let uv = match self.monomorphize(constant.const_) {
42-
mir::Const::Unevaluated(uv, _) => uv.shrink(),
43+
mir::Const::Unevaluated(uv, _) => uv.shrink(tcx),
4344
mir::Const::Ty(_, c) => match c.kind() {
4445
// A constant that came from a const generic but was then used as an argument to
4546
// old-style simd_shuffle (passing as argument instead of as a generic param).
@@ -57,7 +58,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
5758
other => span_bug!(constant.span, "{other:#?}"),
5859
};
5960
let uv = self.monomorphize(uv);
60-
self.cx.tcx().const_eval_resolve_for_typeck(self.cx.typing_env(), uv, constant.span)
61+
tcx.const_eval_resolve_for_typeck(self.cx.typing_env(), uv, constant.span)
6162
}
6263

6364
/// process constant containing SIMD shuffle indices & constant vectors

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
776776
// need to store default and type of default
777777
let ct = tcx.const_param_default(param.def_id).skip_binder();
778778
if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
779-
tcx.ensure_ok().type_of(uv.def);
779+
tcx.ensure_ok().type_of(uv.kind.def_id());
780780
}
781781
}
782782
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,9 +1535,11 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
15351535
| ty::ConstKind::Bound(_, _) => unreachable!(),
15361536
ty::ConstKind::Error(_) | ty::ConstKind::Expr(_) => continue,
15371537
ty::ConstKind::Value(cv) => cv.ty,
1538-
ty::ConstKind::Unevaluated(uv) => {
1539-
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args).skip_norm_wip()
1540-
}
1538+
ty::ConstKind::Unevaluated(uv) => infcx
1539+
.tcx
1540+
.type_of(uv.kind.def_id())
1541+
.instantiate(infcx.tcx, uv.args)
1542+
.skip_norm_wip(),
15411543
ty::ConstKind::Param(param_ct) => {
15421544
param_ct.find_const_ty_from_env(wfcx.param_env)
15431545
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn const_evaluatable_predicates_of<'tcx>(
435435
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
436436
fn visit_const(&mut self, c: ty::Const<'tcx>) {
437437
if let ty::ConstKind::Unevaluated(uv) = c.kind() {
438-
if let Some(local) = uv.def.as_local()
438+
if let Some(local) = uv.kind.def_id().as_local()
439439
&& is_const_param_default(self.tcx, local)
440440
{
441441
// Do not look into const param defaults,
@@ -449,11 +449,11 @@ fn const_evaluatable_predicates_of<'tcx>(
449449
}
450450

451451
// Skip type consts as mGCA doesn't support evaluatable clauses.
452-
if self.tcx.is_type_const(uv.def) {
452+
if self.tcx.is_type_const(uv.kind.def_id()) {
453453
return;
454454
}
455455

456-
let span = self.tcx.def_span(uv.def);
456+
let span = self.tcx.def_span(uv.kind.def_id());
457457
self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
458458
}
459459
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13981398
)? {
13991399
TypeRelativePath::AssocItem(def_id, args) => {
14001400
self.require_type_const_attribute(def_id, span)?;
1401-
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
1401+
let ct = Const::new_unevaluated(
1402+
tcx,
1403+
ty::UnevaluatedConst::new(
1404+
tcx,
1405+
ty::UnevaluatedConstKind::new_from_def_id(tcx, def_id),
1406+
args,
1407+
),
1408+
);
14021409
let ct = self.check_param_uses_if_mcg(ct, span, false);
14031410
Ok(ct)
14041411
}
@@ -1840,6 +1847,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18401847
trait_segment: Option<&hir::PathSegment<'tcx>>,
18411848
item_segment: &hir::PathSegment<'tcx>,
18421849
) -> Result<Const<'tcx>, ErrorGuaranteed> {
1850+
let tcx = self.tcx();
18431851
let (item_def_id, item_args) = self.lower_resolved_assoc_item_path(
18441852
span,
18451853
opt_self_ty,
@@ -1849,8 +1857,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18491857
ty::AssocTag::Const,
18501858
)?;
18511859
self.require_type_const_attribute(item_def_id, span)?;
1852-
let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
1853-
Ok(Const::new_unevaluated(self.tcx(), uv))
1860+
let uv = ty::UnevaluatedConst::new(
1861+
tcx,
1862+
ty::UnevaluatedConstKind::new_from_def_id(tcx, item_def_id),
1863+
item_args,
1864+
);
1865+
Ok(Const::new_unevaluated(tcx, uv))
18541866
}
18551867

18561868
/// Lower a [resolved][hir::QPath::Resolved] (type-level) associated item path.
@@ -2692,7 +2704,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26922704
let _ = self
26932705
.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
26942706
let args = self.lower_generic_args_of_path_segment(span, did, segment);
2695-
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2707+
ty::Const::new_unevaluated(
2708+
tcx,
2709+
ty::UnevaluatedConst::new(
2710+
tcx,
2711+
ty::UnevaluatedConstKind::new_from_def_id(tcx, did),
2712+
args,
2713+
),
2714+
)
26962715
}
26972716
Res::Def(DefKind::Ctor(ctor_of, CtorKind::Const), did) => {
26982717
assert_eq!(opt_self_ty, None);
@@ -2816,10 +2835,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28162835
Some(v) => v,
28172836
None => ty::Const::new_unevaluated(
28182837
tcx,
2819-
ty::UnevaluatedConst {
2820-
def: anon.def_id.to_def_id(),
2821-
args: ty::GenericArgs::identity_for_item(tcx, anon.def_id.to_def_id()),
2822-
},
2838+
ty::UnevaluatedConst::new(
2839+
tcx,
2840+
ty::UnevaluatedConstKind::Anon { def_id: anon.def_id.to_def_id() },
2841+
ty::GenericArgs::identity_for_item(tcx, anon.def_id.to_def_id()),
2842+
),
28232843
),
28242844
}
28252845
}

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'tcx> InferCtxt<'tcx> {
184184

185185
relation.register_predicates([ty::PredicateKind::AliasRelate(lhs, rhs, direction)]);
186186
} else {
187-
let Some(source_alias) = source_term.to_alias_term(self.tcx) else {
187+
let Some(source_alias) = source_term.to_alias_term() else {
188188
bug!("generalized `{source_term:?} to infer, not an alias");
189189
};
190190
match source_alias.kind(self.tcx) {
@@ -207,7 +207,7 @@ impl<'tcx> InferCtxt<'tcx> {
207207
}
208208
ty::AliasTermKind::InherentConst { .. }
209209
| ty::AliasTermKind::FreeConst { .. }
210-
| ty::AliasTermKind::UnevaluatedConst { .. } => {
210+
| ty::AliasTermKind::AnonConst { .. } => {
211211
return Err(TypeError::CyclicConst(source_term.expect_const()));
212212
}
213213
}
@@ -701,6 +701,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
701701
c: ty::Const<'tcx>,
702702
c2: ty::Const<'tcx>,
703703
) -> RelateResult<'tcx, ty::Const<'tcx>> {
704+
let tcx = self.cx();
704705
assert_eq!(c, c2); // we are misusing TypeRelation here; both LHS and RHS ought to be ==
705706

706707
match c.kind() {
@@ -741,7 +742,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
741742
{
742743
variable_table.union(vid, new_var_id);
743744
}
744-
Ok(ty::Const::new_var(self.cx(), new_var_id))
745+
Ok(ty::Const::new_var(tcx, new_var_id))
745746
}
746747
}
747748
}
@@ -755,19 +756,18 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
755756
// Hack: Fall back to old behavior if GCE is enabled (it used to just be the Yes
756757
// path), as doing this new No path breaks some GCE things. I expect GCE to be
757758
// ripped out soon so this shouldn't matter soon.
758-
StructurallyRelateAliases::No if !self.cx().features().generic_const_exprs() => {
759-
self.generalize_alias_term(ty::AliasTerm::from_unevaluated_const(self.cx(), uv))
760-
.map(|v| v.expect_const())
759+
StructurallyRelateAliases::No if !tcx.features().generic_const_exprs() => {
760+
self.generalize_alias_term(uv.into()).map(|v| v.expect_const())
761761
}
762762
_ => {
763-
let ty::UnevaluatedConst { def, args } = uv;
763+
let ty::UnevaluatedConst { kind, args, .. } = uv;
764764
let args = self.relate_with_variance(
765765
ty::Invariant,
766766
ty::VarianceDiagInfo::default(),
767767
args,
768768
args,
769769
)?;
770-
Ok(ty::Const::new_unevaluated(self.cx(), ty::UnevaluatedConst { def, args }))
770+
Ok(ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(tcx, kind, args)))
771771
}
772772
},
773773
ty::ConstKind::Placeholder(placeholder) => {

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,13 @@ pub struct UnevaluatedConst<'tcx> {
467467

468468
impl<'tcx> UnevaluatedConst<'tcx> {
469469
#[inline]
470-
pub fn shrink(self) -> ty::UnevaluatedConst<'tcx> {
470+
pub fn shrink(self, tcx: TyCtxt<'tcx>) -> ty::UnevaluatedConst<'tcx> {
471471
assert_eq!(self.promoted, None);
472-
ty::UnevaluatedConst { def: self.def, args: self.args }
472+
ty::UnevaluatedConst::new(
473+
tcx,
474+
ty::UnevaluatedConstKind::new_from_def_id(tcx, self.def),
475+
self.args,
476+
)
473477
}
474478
}
475479

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'tcx> TyCtxt<'tcx> {
103103
bug!("did not expect inference variables here");
104104
}
105105

106-
let cid = match ty::Instance::try_resolve(self, typing_env, ct.def, ct.args) {
106+
let cid = match ty::Instance::try_resolve(self, typing_env, ct.kind.def_id(), ct.args) {
107107
Ok(Some(instance)) => GlobalId { instance, promoted: None },
108108
// For errors during resolution, we deliberately do not point at the usage site of the constant,
109109
// since for these errors the place the constant is used shouldn't matter.
@@ -138,11 +138,11 @@ impl<'tcx> TyCtxt<'tcx> {
138138
{
139139
let mir_body = self.mir_for_ctfe(cid.instance.def_id());
140140
if mir_body.is_polymorphic {
141-
let Some(local_def_id) = ct.def.as_local() else { return };
141+
let Some(local_def_id) = ct.kind.def_id().as_local() else { return };
142142
self.emit_node_span_lint(
143143
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
144144
self.local_def_id_to_hir_id(local_def_id),
145-
self.def_span(ct.def),
145+
self.def_span(ct.kind.def_id()),
146146
rustc_errors::DiagDecorator(|lint| {
147147
lint.primary_message(
148148
"cannot use constants which depend on generic parameters in types",

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,11 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
14971497
Const::Ty(_, ct) => match ct.kind() {
14981498
ty::ConstKind::Param(p) => format!("ty::Param({p})"),
14991499
ty::ConstKind::Unevaluated(uv) => {
1500-
format!("ty::Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.args,)
1500+
format!(
1501+
"ty::Unevaluated({}, {:?})",
1502+
self.tcx.def_path_str(uv.kind.def_id()),
1503+
uv.args,
1504+
)
15011505
}
15021506
ty::ConstKind::Value(cv) => {
15031507
format!("ty::Valtree({})", fmt_valtree(&cv))

0 commit comments

Comments
 (0)