Skip to content

Commit c7c7b9d

Browse files
committed
fast-path for try_eval_scalar (instead of try_eval_scalar_int)
1 parent 5236c8e commit c7c7b9d

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

compiler/rustc_middle/src/mir/consts.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,14 @@ impl<'tcx> Const<'tcx> {
279279
tcx: TyCtxt<'tcx>,
280280
param_env: ty::ParamEnv<'tcx>,
281281
) -> Option<Scalar> {
282-
self.eval(tcx, param_env, None).ok()?.try_to_scalar()
282+
match self {
283+
Const::Ty(c) => {
284+
// Avoid the `valtree_to_const_val` query.
285+
let val = c.eval(tcx, param_env, None).ok()?;
286+
val.try_to_scalar()
287+
}
288+
_ => self.eval(tcx, param_env, None).ok()?.try_to_scalar(),
289+
}
283290
}
284291

285292
#[inline]
@@ -288,16 +295,7 @@ impl<'tcx> Const<'tcx> {
288295
tcx: TyCtxt<'tcx>,
289296
param_env: ty::ParamEnv<'tcx>,
290297
) -> Option<ScalarInt> {
291-
match self {
292-
// If the constant is already evaluated, we shortcut here.
293-
Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => {
294-
valtree.try_to_scalar_int()
295-
},
296-
// This is a more general form of the previous case.
297-
_ => {
298-
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
299-
},
300-
}
298+
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
301299
}
302300

303301
#[inline]

0 commit comments

Comments
 (0)