Skip to content

Commit 141f143

Browse files
committed
avoid some ValTree-ConstValue roundtrips
1 parent ae341f7 commit 141f143

File tree

1 file changed

+14
-9
lines changed
  • compiler/rustc_middle/src/mir

1 file changed

+14
-9
lines changed

compiler/rustc_middle/src/mir/mod.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -2373,17 +2373,22 @@ impl<'tcx> ConstantKind<'tcx> {
23732373
param_env: ty::ParamEnv<'tcx>,
23742374
span: Option<Span>,
23752375
) -> Result<interpret::ConstValue<'tcx>, ErrorHandled> {
2376-
match self {
2376+
let uneval = match self {
23772377
ConstantKind::Ty(c) => {
2378-
let val = c.eval(tcx, param_env, span)?;
2379-
Ok(tcx.valtree_to_const_val((self.ty(), val)))
2380-
}
2381-
ConstantKind::Unevaluated(uneval, _) => {
2382-
// FIXME: We might want to have a `try_eval`-like function on `Unevaluated`
2383-
tcx.const_eval_resolve(param_env, uneval, span)
2378+
if let ty::ConstKind::Unevaluated(uv) = c.kind() {
2379+
// Avoid the round-trip via valtree, evaluate directly to ConstValue.
2380+
uv.expand()
2381+
} else {
2382+
// It's already a valtree, or an error.
2383+
let val = c.eval(tcx, param_env, span)?;
2384+
return Ok(tcx.valtree_to_const_val((self.ty(), val)));
2385+
}
23842386
}
2385-
ConstantKind::Val(val, _) => Ok(val),
2386-
}
2387+
ConstantKind::Unevaluated(uneval, _) => uneval,
2388+
ConstantKind::Val(val, _) => return Ok(val),
2389+
};
2390+
// FIXME: We might want to have a `try_eval`-like function on `Unevaluated`
2391+
tcx.const_eval_resolve(param_env, uneval, span)
23872392
}
23882393

23892394
/// Normalizes the constant to a value if possible.

0 commit comments

Comments
 (0)