Skip to content

Commit

Permalink
Auto merge of #116457 - RalfJung:try_eval_scalar_int, r=<try>
Browse files Browse the repository at this point in the history
fast-path for try_eval_scalar (instead of try_eval_scalar_int)

Cc #116281 `@Nadrieril`
  • Loading branch information
bors committed Oct 5, 2023
2 parents 3bcad65 + c7c7b9d commit 73ab614
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,14 @@ impl<'tcx> Const<'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<Scalar> {
self.eval(tcx, param_env, None).ok()?.try_to_scalar()
match self {
Const::Ty(c) => {
// Avoid the `valtree_to_const_val` query.
let val = c.eval(tcx, param_env, None).ok()?;
val.try_to_scalar()
}
_ => self.eval(tcx, param_env, None).ok()?.try_to_scalar(),
}
}

#[inline]
Expand All @@ -288,16 +295,7 @@ impl<'tcx> Const<'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<ScalarInt> {
match self {
// If the constant is already evaluated, we shortcut here.
Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => {
valtree.try_to_scalar_int()
},
// This is a more general form of the previous case.
_ => {
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
},
}
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
}

#[inline]
Expand Down

0 comments on commit 73ab614

Please sign in to comment.