Add method to grab default compilation width of a number

This commit is contained in:
Ayaz Hafiz 2022-11-16 13:54:48 -06:00
parent 0920fb4227
commit af81ceae38
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
2 changed files with 26 additions and 24 deletions

View file

@ -2276,33 +2276,12 @@ impl<'a> Layout<'a> {
env: &mut Env<'a, '_>,
range: NumericRange,
) -> Cacheable<LayoutResult<'a>> {
use roc_types::num::IntLitWidth;
// If we chose the default int layout then the real var might have been `Num *`, or
// similar. In this case fix-up width if we need to. Choose I64 if the range says
// that the number will fit, otherwise choose the next-largest number layout.
//
// We don't pass the range down because `RangedNumber`s are somewhat rare, they only
// appear due to number literals, so no need to increase parameter list sizes.
let num_layout = match range {
NumericRange::IntAtLeastSigned(w) | NumericRange::NumAtLeastSigned(w) => {
[IntLitWidth::I64, IntLitWidth::I128]
.iter()
.find(|candidate| candidate.is_superset(&w, true))
.expect("if number doesn't fit, should have been a type error")
}
NumericRange::IntAtLeastEitherSign(w) | NumericRange::NumAtLeastEitherSign(w) => [
IntLitWidth::I64,
IntLitWidth::U64,
IntLitWidth::I128,
IntLitWidth::U128,
]
.iter()
.find(|candidate| candidate.is_superset(&w, false))
.expect("if number doesn't fit, should have been a type error"),
};
let num_layout = range.default_compilation_width();
cacheable(Ok(Layout::int_literal_width_to_int(
*num_layout,
num_layout,
env.target_info,
)))
}