diff --git a/reporting/src/error/canonicalize.rs b/reporting/src/error/canonicalize.rs index 48db9aa376..3ad20cebf6 100644 --- a/reporting/src/error/canonicalize.rs +++ b/reporting/src/error/canonicalize.rs @@ -1141,16 +1141,24 @@ fn pretty_runtime_error<'b>( let (big_or_small, info) = if let IntErrorKind::Underflow = error_kind { ( "small", - alloc.reflow( - "The smallest number representable in Roc is the minimum I128 value, -170_141_183_460_469_231_731_687_303_715_884_105_728.", - ), + alloc.concat(vec![ + alloc.reflow( + "The smallest number representable in Roc is the minimum I128 value, ", + ), + alloc.int_literal(i128::MIN), + alloc.text("."), + ]), ) } else { ( "big", - alloc.reflow( - "The largest number representable in Roc is the maximum U128 value, 340_282_366_920_938_463_463_374_607_431_768_211_455.", - ), + alloc.concat(vec![ + alloc.reflow( + "The largest number representable in Roc is the maximum U128 value, ", + ), + alloc.int_literal(u128::MAX), + alloc.text("."), + ]), ) }; @@ -1199,7 +1207,8 @@ fn pretty_runtime_error<'b>( alloc.reflow("The suffix indicates this integer is a "), alloc.type_str(suffix_type), alloc.reflow(", whose maximum value is "), - alloc.text(format!("{}.", max_value)), + alloc.int_literal(max_value), + alloc.reflow("."), ])), ]); @@ -1223,7 +1232,8 @@ fn pretty_runtime_error<'b>( alloc.reflow("The suffix indicates this integer is a "), alloc.type_str(suffix_type), alloc.reflow(", whose minimum value is "), - alloc.text(format!("{}.", min_value)), + alloc.int_literal(min_value), + alloc.reflow("."), ])), ]); diff --git a/reporting/src/report.rs b/reporting/src/report.rs index 07a7cbfca7..a0dc1261a2 100644 --- a/reporting/src/report.rs +++ b/reporting/src/report.rs @@ -632,6 +632,39 @@ impl<'a> RocDocAllocator<'a> { self.text(format!("{}", ident.as_inline_str())) .annotate(Annotation::Symbol) } + + pub fn int_literal(&'a self, int: I) -> DocBuilder<'a, Self, Annotation> + where + I: ToString, + { + let s = int.to_string(); + + let is_negative = s.starts_with("-"); + + if s.len() < 7 + (is_negative as usize) { + // If the number is not at least in the millions, return it as-is. + return self.text(s); + } + + // Otherwise, let's add numeric separators to make it easier to read. + let mut result = String::with_capacity(s.len() + s.len() / 3); + for (idx, c) in s + .get((is_negative as usize)..) + .unwrap() + .chars() + .rev() + .enumerate() + { + if idx != 0 && idx % 3 == 0 { + result.push('_'); + } + result.push(c); + } + if is_negative { + result.push('-'); + } + self.text(result.chars().rev().collect::()) + } } #[derive(Copy, Clone)] diff --git a/reporting/tests/test_reporting.rs b/reporting/tests/test_reporting.rs index 8fbbecb906..8535556e6c 100644 --- a/reporting/tests/test_reporting.rs +++ b/reporting/tests/test_reporting.rs @@ -7622,7 +7622,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a U32, whose maximum value - is 4294967295. + is 4_294_967_295. "# ), ) @@ -7662,7 +7662,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a U64, whose maximum value - is 18446744073709551615. + is 18_446_744_073_709_551_615. "# ), ) @@ -7802,7 +7802,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a I32, whose maximum value - is 2147483647. + is 2_147_483_647. "# ), ) @@ -7822,7 +7822,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a I32, whose minimum value - is -2147483648. + is -2_147_483_648. "# ), ) @@ -7842,7 +7842,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a I64, whose maximum value - is 9223372036854775807. + is 9_223_372_036_854_775_807. "# ), ) @@ -7862,7 +7862,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a I64, whose minimum value - is -9223372036854775808. + is -9_223_372_036_854_775_808. "# ), ) @@ -7882,7 +7882,7 @@ I need all branches in an `if` to have the same type! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Tip: The suffix indicates this integer is a I128, whose maximum value - is 170141183460469231731687303715884105727. + is 170_141_183_460_469_231_731_687_303_715_884_105_727. "# ), )