Skip to content

Commit

Permalink
Auto merge of #116281 - Nadrieril:eager-const-eval, r=<try>
Browse files Browse the repository at this point in the history
Cleanup number handling in match exhaustiveness

Doing a little bit of cleanup; handling number constants was somewhat messy. In particular, this:

- evals float consts once instead of repetitively
- reduces `Constructor` from 88 bytes to 56 (`mir::Const` is big!)

The `fast_try_eval_bits` function was mostly constructed from inlining existing code but I don't fully understand it; I don't follow how consts work and are evaluated very well.
  • Loading branch information
bors committed Oct 1, 2023
2 parents ca62d2c + bc3ee37 commit 1451ae6
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 139 deletions.
15 changes: 14 additions & 1 deletion compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,20 @@ impl<'tcx> Const<'tcx> {

#[inline]
pub fn try_eval_bits(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u128> {
let int = self.try_eval_scalar_int(tcx, param_env)?;
debug_assert!(matches!(
self.ty().kind(),
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char
));
let int = match self {
// If the constant is already evaluated, we shortcut here.
Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => {
valtree.unwrap_leaf()
},
// This is a more general form of the previous case.
_ => {
self.try_eval_scalar_int(tcx, param_env)?
},
};
let size =
tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(self.ty())).ok()?.size;
int.to_bits(size).ok()
Expand Down
Loading

0 comments on commit 1451ae6

Please sign in to comment.