Skip to content

Commit 6f5ea7f

Browse files
skip const-eval if evaluatable predicate is trivial
1 parent 7d97c59 commit 6f5ea7f

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ pub fn is_const_evaluatable<'tcx>(
3737
| ty::ConstKind::Value(_, _)
3838
| ty::ConstKind::Error(_) => return Ok(()),
3939
ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
40-
};
40+
}
4141

4242
if tcx.features().generic_const_exprs {
4343
let ct = tcx.expand_abstract_consts(unexpanded_ct);
4444

45+
if trivially_satisfied_from_param_env(ct, param_env) {
46+
return Ok(());
47+
}
48+
4549
let is_anon_ct = if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
46-
tcx.def_kind(uv.def) == DefKind::AnonConst
50+
matches!(tcx.def_kind(uv.def), DefKind::AnonConst)
4751
} else {
4852
false
4953
};
@@ -150,6 +154,23 @@ pub fn is_const_evaluatable<'tcx>(
150154
}
151155
}
152156

157+
fn trivially_satisfied_from_param_env<'tcx>(
158+
ct: ty::Const<'tcx>,
159+
param_env: ty::ParamEnv<'tcx>,
160+
) -> bool {
161+
for pred in param_env.caller_bounds() {
162+
if let ty::ClauseKind::ConstEvaluatable(ce) = pred.kind().skip_binder() {
163+
if let ty::ConstKind::Unevaluated(uv_env) = ce.kind()
164+
&& let ty::ConstKind::Unevaluated(uv) = ct.kind()
165+
&& uv == uv_env
166+
{
167+
return true;
168+
}
169+
}
170+
}
171+
false
172+
}
173+
153174
#[instrument(skip(infcx, tcx), level = "debug")]
154175
fn satisfied_from_param_env<'tcx>(
155176
tcx: TyCtxt<'tcx>,

0 commit comments

Comments
 (0)