Skip to content

Commit 79cbcec

Browse files
committed
change evaluate_const to take a UnevaluatedConst
1 parent 57cb10a commit 79cbcec

12 files changed

Lines changed: 165 additions & 164 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,23 +1468,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14681468
let ct = self.resolve_vars_with_obligations(ct);
14691469

14701470
if self.next_trait_solver()
1471-
&& let ty::ConstKind::Unevaluated(..) = ct.kind()
1471+
&& let ty::ConstKind::Unevaluated(uv) = ct.kind()
14721472
{
14731473
// We need to use a separate variable here as otherwise the temporary for
14741474
// `self.fulfillment_cx.borrow_mut()` is alive in the `Err` branch, resulting
14751475
// in a reentrant borrow, causing an ICE.
14761476
let result = self
14771477
.at(&self.misc(sp), self.param_env)
1478-
.structurally_normalize_const(ct, &mut **self.fulfillment_cx.borrow_mut());
1478+
.structurally_normalize_const(uv, &mut **self.fulfillment_cx.borrow_mut());
14791479
match result {
14801480
Ok(normalized_ct) => normalized_ct,
14811481
Err(errors) => {
14821482
let guar = self.err_ctxt().report_fulfillment_errors(errors);
14831483
return ty::Const::new_error(self.tcx, guar);
14841484
}
14851485
}
1486-
} else if self.tcx.features().generic_const_exprs() {
1487-
rustc_trait_selection::traits::evaluate_const(&self.infcx, ct, self.param_env)
1486+
} else if self.tcx.features().generic_const_exprs()
1487+
&& let ty::ConstKind::Unevaluated(uv) = ct.kind()
1488+
{
1489+
rustc_trait_selection::traits::evaluate_const(&self.infcx, uv, self.param_env)
14881490
} else {
14891491
ct
14901492
}

compiler/rustc_trait_selection/src/solve/delegate.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
178178
param_env: ty::ParamEnv<'tcx>,
179179
uv: ty::UnevaluatedConst<'tcx>,
180180
) -> Option<ty::Const<'tcx>> {
181-
let ct = ty::Const::new_unevaluated(self.tcx, uv);
182-
183-
match crate::traits::try_evaluate_const(&self.0, ct, param_env) {
181+
match crate::traits::try_evaluate_const(&self.0, uv, param_env) {
184182
Ok(ct) => Some(ct),
185183
Err(EvaluateConstErr::EvaluationFailure(e)) => Some(ty::Const::new_error(self.tcx, e)),
186184
Err(

compiler/rustc_trait_selection/src/traits/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
765765
if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
766766
let ct = super::try_evaluate_const(
767767
selcx.infcx,
768-
c,
768+
unevaluated,
769769
obligation.param_env,
770770
);
771771

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ pub fn is_const_evaluatable<'tcx>(
6868
// here.
6969
tcx.dcx().span_bug(span, "evaluating `ConstKind::Expr` is not currently supported");
7070
}
71-
ty::ConstKind::Unevaluated(_) => {
72-
match crate::traits::try_evaluate_const(infcx, unexpanded_ct, param_env) {
71+
ty::ConstKind::Unevaluated(uv) => {
72+
match crate::traits::try_evaluate_const(infcx, uv, param_env) {
7373
Err(EvaluateConstErr::HasGenericsOrInfers) => {
7474
Err(NotConstEvaluatable::Error(infcx.dcx().span_delayed_bug(
7575
span,
@@ -89,7 +89,14 @@ pub fn is_const_evaluatable<'tcx>(
8989
// This is a sanity check to make sure that non-generics consts are checked to
9090
// be evaluatable in case they aren't cchecked elsewhere. This will NOT error
9191
// if the const uses generics, as desired.
92-
crate::traits::evaluate_const(infcx, unexpanded_ct, param_env);
92+
let uv = match unexpanded_ct.kind() {
93+
ty::ConstKind::Unevaluated(uv) => uv,
94+
ty::ConstKind::Expr(_) => {
95+
bug!("`ConstKind::Expr` without `feature(generic_const_exprs)` enabled")
96+
}
97+
_ => bug!("unexpected constkind in `is_const_evalautable: {unexpanded_ct:?}`"),
98+
};
99+
crate::traits::evaluate_const(infcx, uv, param_env);
93100
Ok(())
94101
} else {
95102
let uv = match unexpanded_ct.kind() {
@@ -100,7 +107,7 @@ pub fn is_const_evaluatable<'tcx>(
100107
_ => bug!("unexpected constkind in `is_const_evalautable: {unexpanded_ct:?}`"),
101108
};
102109

103-
match crate::traits::try_evaluate_const(infcx, unexpanded_ct, param_env) {
110+
match crate::traits::try_evaluate_const(infcx, uv, param_env) {
104111
// If we're evaluating a generic foreign constant, under a nightly compiler while
105112
// the current crate does not enable `feature(generic_const_exprs)`, abort
106113
// compilation with a useful error.

compiler/rustc_trait_selection/src/traits/engine.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,13 @@ where
368368
param_env: ty::ParamEnv<'tcx>,
369369
value: ty::Const<'tcx>,
370370
) -> Result<ty::Const<'tcx>, Vec<E>> {
371-
self.infcx
372-
.at(cause, param_env)
373-
.structurally_normalize_const(value, &mut **self.engine.borrow_mut())
371+
if let ty::ConstKind::Unevaluated(uv) = value.kind() {
372+
self.infcx
373+
.at(cause, param_env)
374+
.structurally_normalize_const(uv, &mut **self.engine.borrow_mut())
375+
} else {
376+
Ok(value)
377+
}
374378
}
375379

376380
pub fn structurally_normalize_term(

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
731731
if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
732732
match super::try_evaluate_const(
733733
self.selcx.infcx,
734-
c,
734+
unevaluated,
735735
obligation.param_env,
736736
) {
737737
Ok(val) => Ok(val),

0 commit comments

Comments
 (0)