From 2a68af83e31fade392b030deed8e3177df6133ad Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 5 Jul 2024 08:40:48 -0300 Subject: [PATCH] Check error output --- .../src/ssa/ssa_gen/context.rs | 2 +- .../noirc_frontend/src/hir/type_check/stmt.rs | 2 +- compiler/noirc_frontend/src/tests.rs | 36 ++++++++++--------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs index 66a972a51a5..e013546f14a 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs @@ -275,7 +275,7 @@ impl<'a> FunctionContext<'a> { if let Some(range) = numeric_type.value_is_outside_limits(value, negative) { let call_stack = self.builder.get_call_stack(); return Err(RuntimeError::IntegerOutOfBounds { - value, + value: if negative { -value } else { value }, typ: numeric_type, range, call_stack, diff --git a/compiler/noirc_frontend/src/hir/type_check/stmt.rs b/compiler/noirc_frontend/src/hir/type_check/stmt.rs index 3a570922c81..9abd1b34690 100644 --- a/compiler/noirc_frontend/src/hir/type_check/stmt.rs +++ b/compiler/noirc_frontend/src/hir/type_check/stmt.rs @@ -368,7 +368,7 @@ impl<'interner> TypeChecker<'interner> { let max = 1 << bit_count; if v >= max { self.errors.push(TypeCheckError::OverflowingAssignment { - expr: value, + expr: -value, ty: annotated_type.clone(), range: format!("0..={}", max - 1), span, diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index 28ef597a60c..631f83f7f1b 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -1917,10 +1917,11 @@ fn overflowing_u8() { let errors = get_program_errors(src); assert_eq!(errors.len(), 1); - if let CompilationError::TypeError(TypeCheckError::OverflowingAssignment { range, .. }) = - &errors[0].0 - { - assert_eq!(range, "0..=255"); + if let CompilationError::TypeError(error) = &errors[0].0 { + assert_eq!( + error.to_string(), + "The literal `2⁸` cannot fit into `u8` which has range `0..=255`" + ); } else { panic!("Expected OverflowingAssignment error, got {:?}", errors[0].0); } @@ -1935,10 +1936,11 @@ fn underflowing_u8() { let errors = get_program_errors(src); assert_eq!(errors.len(), 1); - if let CompilationError::TypeError(TypeCheckError::OverflowingAssignment { range, .. }) = - &errors[0].0 - { - assert_eq!(range, "0..=255"); + if let CompilationError::TypeError(error) = &errors[0].0 { + assert_eq!( + error.to_string(), + "The literal `-1` cannot fit into `u8` which has range `0..=255`" + ); } else { panic!("Expected OverflowingAssignment error, got {:?}", errors[0].0); } @@ -1953,10 +1955,11 @@ fn overflowing_i8() { let errors = get_program_errors(src); assert_eq!(errors.len(), 1); - if let CompilationError::TypeError(TypeCheckError::OverflowingAssignment { range, .. }) = - &errors[0].0 - { - assert_eq!(range, "-128..=127"); + if let CompilationError::TypeError(error) = &errors[0].0 { + assert_eq!( + error.to_string(), + "The literal `2⁷` cannot fit into `i8` which has range `-128..=127`" + ); } else { panic!("Expected OverflowingAssignment error, got {:?}", errors[0].0); } @@ -1971,10 +1974,11 @@ fn underflowing_i8() { let errors = get_program_errors(src); assert_eq!(errors.len(), 1); - if let CompilationError::TypeError(TypeCheckError::OverflowingAssignment { range, .. }) = - &errors[0].0 - { - assert_eq!(range, "-128..=127"); + if let CompilationError::TypeError(error) = &errors[0].0 { + assert_eq!( + error.to_string(), + "The literal `-129` cannot fit into `i8` which has range `-128..=127`" + ); } else { panic!("Expected OverflowingAssignment error, got {:?}", errors[0].0); }