Skip to content

Commit

Permalink
Auto merge of rust-lang#116281 - Nadrieril:eager-const-eval, r=cjgillot
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 51ddc74 + eac7bcd commit e0d7ed1
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 139 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,16 @@ impl<'tcx> Const<'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<ScalarInt> {
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
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()
},
}
}

#[inline]
Expand Down
Loading

0 comments on commit e0d7ed1

Please sign in to comment.