Skip to content

Commit 660ca48

Browse files
committed
change ConstEvaluatable to use ty::Const
1 parent 98a5ac2 commit 660ca48

File tree

15 files changed

+62
-42
lines changed

15 files changed

+62
-42
lines changed

compiler/rustc_hir_analysis/src/check/dropck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
198198
(
199199
ty::PredicateKind::ConstEvaluatable(a),
200200
ty::PredicateKind::ConstEvaluatable(b),
201-
) => tcx.try_unify_abstract_consts(self_param_env.and((a, b))),
201+
) => relator.relate(predicate.rebind(a), predicate.rebind(b)).is_ok(),
202202
(
203203
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_a, lt_a)),
204204
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_b, lt_b)),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,6 @@ fn check_type_defn<'tcx, F>(
11011101

11021102
// Explicit `enum` discriminant values must const-evaluate successfully.
11031103
if let Some(discr_def_id) = variant.explicit_discr {
1104-
let discr_substs = InternalSubsts::identity_for_item(tcx, discr_def_id.to_def_id());
1105-
11061104
let cause = traits::ObligationCause::new(
11071105
tcx.def_span(discr_def_id),
11081106
wfcx.body_id,
@@ -1112,10 +1110,7 @@ fn check_type_defn<'tcx, F>(
11121110
cause,
11131111
wfcx.param_env,
11141112
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(
1115-
ty::UnevaluatedConst::new(
1116-
ty::WithOptConstParam::unknown(discr_def_id.to_def_id()),
1117-
discr_substs,
1118-
),
1113+
ty::Const::from_anon_const(tcx, discr_def_id),
11191114
))
11201115
.to_predicate(tcx),
11211116
));

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ fn const_evaluatable_predicates_of<'tcx>(
318318
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
319319
let def_id = self.tcx.hir().local_def_id(c.hir_id);
320320
let ct = ty::Const::from_anon_const(self.tcx, def_id);
321-
if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
321+
if let ty::ConstKind::Unevaluated(_) = ct.kind() {
322322
let span = self.tcx.hir().span(c.hir_id);
323323
self.preds.insert((
324-
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(uv))
324+
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
325325
.to_predicate(self.tcx),
326326
span,
327327
));

compiler/rustc_middle/src/ty/consts.rs

+4
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ impl<'tcx> Const<'tcx> {
263263
self.try_eval_usize(tcx, param_env)
264264
.unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
265265
}
266+
267+
pub fn is_ct_infer(self) -> bool {
268+
matches!(self.kind(), ty::ConstKind::Infer(_))
269+
}
266270
}
267271

268272
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> {

compiler/rustc_middle/src/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl FlagComputation {
256256
self.add_substs(substs);
257257
}
258258
ty::PredicateKind::ConstEvaluatable(uv) => {
259-
self.add_unevaluated_const(uv);
259+
self.add_const(uv);
260260
}
261261
ty::PredicateKind::ConstEquate(expected, found) => {
262262
self.add_const(expected);

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ pub enum PredicateKind<'tcx> {
682682
Coerce(CoercePredicate<'tcx>),
683683

684684
/// Constant initializer must evaluate successfully.
685-
ConstEvaluatable(ty::UnevaluatedConst<'tcx>),
685+
ConstEvaluatable(ty::Const<'tcx>),
686686

687687
/// Constants must be equal. The first component is the const that is expected.
688688
ConstEquate(Const<'tcx>, Const<'tcx>),

compiler/rustc_middle/src/ty/print/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2702,8 +2702,8 @@ define_print_and_forward_display! {
27022702
print_value_path(closure_def_id, &[]),
27032703
write("` implements the trait `{}`", kind))
27042704
}
2705-
ty::PredicateKind::ConstEvaluatable(uv) => {
2706-
p!("the constant `", print_value_path(uv.def.did, uv.substs), "` can be evaluated")
2705+
ty::PredicateKind::ConstEvaluatable(ct) => {
2706+
p!("the constant `", print(ct), "` can be evaluated")
27072707
}
27082708
ty::PredicateKind::ConstEquate(c1, c2) => {
27092709
p!("the constant `", print(c1), "` equals `", print(c2), "`")

compiler/rustc_middle/src/ty/structural_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
166166
ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
167167
write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
168168
}
169-
ty::PredicateKind::ConstEvaluatable(uv) => {
170-
write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs)
169+
ty::PredicateKind::ConstEvaluatable(ct) => {
170+
write!(f, "ConstEvaluatable({ct:?})")
171171
}
172172
ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
173173
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {

compiler/rustc_middle/src/ty/subst.rs

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ impl<'tcx> GenericArg<'tcx> {
188188
_ => bug!("expected a const, but found another kind"),
189189
}
190190
}
191+
192+
pub fn is_non_region_infer(self) -> bool {
193+
match self.unpack() {
194+
GenericArgKind::Lifetime(_) => false,
195+
GenericArgKind::Type(ty) => ty.is_ty_infer(),
196+
GenericArgKind::Const(ct) => ct.is_ct_infer(),
197+
}
198+
}
191199
}
192200

193201
impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> {

compiler/rustc_middle/src/ty/walk.rs

+16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ impl<'tcx> Ty<'tcx> {
112112
}
113113
}
114114

115+
impl<'tcx> ty::Const<'tcx> {
116+
/// Iterator that walks `self` and any types reachable from
117+
/// `self`, in depth-first order. Note that just walks the types
118+
/// that appear in `self`, it does not descend into the fields of
119+
/// structs or variants. For example:
120+
///
121+
/// ```text
122+
/// isize => { isize }
123+
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
124+
/// [isize] => { [isize], isize }
125+
/// ```
126+
pub fn walk(self) -> TypeWalker<'tcx> {
127+
TypeWalker::new(self.into())
128+
}
129+
}
130+
115131
/// We push `GenericArg`s on the stack in reverse order so as to
116132
/// maintain a pre-order traversal. As of the time of this
117133
/// writing, the fact that the traversal is pre-order is not

compiler/rustc_privacy/src/lib.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,7 @@ where
159159
ty.visit_with(self)
160160
}
161161
ty::PredicateKind::RegionOutlives(..) => ControlFlow::CONTINUE,
162-
ty::PredicateKind::ConstEvaluatable(uv)
163-
if self.def_id_visitor.tcx().features().generic_const_exprs =>
164-
{
165-
let tcx = self.def_id_visitor.tcx();
166-
if let Ok(Some(ct)) = AbstractConst::new(tcx, uv) {
167-
self.visit_abstract_const_expr(tcx, ct)?;
168-
}
169-
ControlFlow::CONTINUE
170-
}
162+
ty::PredicateKind::ConstEvaluatable(ct) => ct.visit_with(self),
171163
ty::PredicateKind::WellFormed(arg) => arg.visit_with(self),
172164
_ => bug!("unexpected predicate: {:?}", predicate),
173165
}

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,20 @@ pub fn try_unify_abstract_consts<'tcx>(
161161
#[instrument(skip(infcx), level = "debug")]
162162
pub fn is_const_evaluatable<'tcx>(
163163
infcx: &InferCtxt<'tcx>,
164-
uv: ty::UnevaluatedConst<'tcx>,
164+
ct: ty::Const<'tcx>,
165165
param_env: ty::ParamEnv<'tcx>,
166166
span: Span,
167167
) -> Result<(), NotConstEvaluatable> {
168168
let tcx = infcx.tcx;
169+
let uv = match ct.kind() {
170+
ty::ConstKind::Unevaluated(uv) => uv,
171+
ty::ConstKind::Param(_)
172+
| ty::ConstKind::Bound(_, _)
173+
| ty::ConstKind::Placeholder(_)
174+
| ty::ConstKind::Value(_)
175+
| ty::ConstKind::Error(_) => return Ok(()),
176+
ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
177+
};
169178

170179
if tcx.features().generic_const_exprs {
171180
if let Some(ct) = AbstractConst::new(tcx, uv)? {
@@ -285,7 +294,7 @@ fn satisfied_from_param_env<'tcx>(
285294
for pred in param_env.caller_bounds() {
286295
match pred.kind().skip_binder() {
287296
ty::PredicateKind::ConstEvaluatable(uv) => {
288-
if let Some(b_ct) = AbstractConst::new(tcx, uv)? {
297+
if let Some(b_ct) = AbstractConst::from_const(tcx, uv)? {
289298
let const_unify_ctxt = ConstUnifyCtxt { tcx, param_env };
290299

291300
// Try to unify with each subtree in the AbstractConst to allow for

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
13041304
}
13051305

13061306
match obligation.predicate.kind().skip_binder() {
1307-
ty::PredicateKind::ConstEvaluatable(uv) => {
1307+
ty::PredicateKind::ConstEvaluatable(ct) => {
1308+
let ty::ConstKind::Unevaluated(uv) = ct.kind() else {
1309+
bug!("const evaluatable failed for non-unevaluated const `{ct:?}`");
1310+
};
13081311
let mut err =
13091312
self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
13101313
let const_span = self.tcx.def_span(uv.def.did);
@@ -2368,7 +2371,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23682371
if predicate.references_error() || self.is_tainted_by_errors() {
23692372
return;
23702373
}
2371-
let subst = data.substs.iter().find(|g| g.has_non_region_infer());
2374+
let subst = data.walk().find(|g| g.is_non_region_infer());
23722375
if let Some(subst) = subst {
23732376
let err = self.emit_inference_failure_err(
23742377
body_id,

compiler/rustc_trait_selection/src/traits/fulfill.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
476476
Err(NotConstEvaluatable::MentionsInfer) => {
477477
pending_obligation.stalled_on.clear();
478478
pending_obligation.stalled_on.extend(
479-
uv.substs
480-
.iter()
481-
.filter_map(TyOrConstInferVar::maybe_from_generic_arg),
479+
uv.walk().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
482480
);
483481
ProcessResult::Unchanged
484482
}

compiler/rustc_trait_selection/src/traits/wf.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,8 @@ pub fn predicate_obligations<'tcx>(
148148
wf.compute(a.into());
149149
wf.compute(b.into());
150150
}
151-
ty::PredicateKind::ConstEvaluatable(uv) => {
152-
let obligations = wf.nominal_obligations(uv.def.did, uv.substs);
153-
wf.out.extend(obligations);
154-
155-
for arg in uv.substs.iter() {
156-
wf.compute(arg);
157-
}
151+
ty::PredicateKind::ConstEvaluatable(ct) => {
152+
wf.compute(ct.into());
158153
}
159154
ty::PredicateKind::ConstEquate(c1, c2) => {
160155
wf.compute(c1.into());
@@ -476,14 +471,14 @@ impl<'tcx> WfPredicates<'tcx> {
476471
// obligations are handled by the parent (e.g. `ty::Ref`).
477472
GenericArgKind::Lifetime(_) => continue,
478473

479-
GenericArgKind::Const(constant) => {
480-
match constant.kind() {
474+
GenericArgKind::Const(ct) => {
475+
match ct.kind() {
481476
ty::ConstKind::Unevaluated(uv) => {
482477
let obligations = self.nominal_obligations(uv.def.did, uv.substs);
483478
self.out.extend(obligations);
484479

485480
let predicate =
486-
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(uv))
481+
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
487482
.to_predicate(self.tcx());
488483
let cause = self.cause(traits::WellFormed(None));
489484
self.out.push(traits::Obligation::with_depth(
@@ -500,7 +495,7 @@ impl<'tcx> WfPredicates<'tcx> {
500495
cause,
501496
self.recursion_depth,
502497
self.param_env,
503-
ty::Binder::dummy(ty::PredicateKind::WellFormed(constant.into()))
498+
ty::Binder::dummy(ty::PredicateKind::WellFormed(ct.into()))
504499
.to_predicate(self.tcx()),
505500
));
506501
}

0 commit comments

Comments
 (0)