diff --git a/compiler/can/src/num.rs b/compiler/can/src/num.rs index ba84bd4760..4bb5f8088d 100644 --- a/compiler/can/src/num.rs +++ b/compiler/can/src/num.rs @@ -80,7 +80,17 @@ pub fn float_expr_from_result( pub fn finish_parsing_int(raw: &str, is_negative: bool) -> Result { // Ignore underscores. let radix = 10; - from_str_radix::(raw.replace("_", "").as_str(), radix).map_err(|e| (raw, e.kind)) + let number = + from_str_radix::(raw.replace("_", "").as_str(), radix).map_err(|e| (raw, e.kind))?; + + if is_negative { + match number.checked_neg() { + None => Err((raw, IntErrorKind::Overflow)), + Some(value) => Ok(value), + } + } else { + Ok(number) + } } #[inline(always)] @@ -108,7 +118,11 @@ pub fn finish_parsing_base( #[inline(always)] pub fn finish_parsing_float(raw: &str, is_negative: bool) -> Result { // Ignore underscores. - match raw.replace("_", "").parse::() { + match raw + .replace("_", "") + .parse::() + .map(|x| if is_negative { -x } else { x }) + { Ok(float) if float.is_finite() => Ok(float), Ok(float) => { if float.is_sign_positive() {