From 55d6dc5cc61423795eebc1c5d5f36fd0f183bc5b Mon Sep 17 00:00:00 2001 From: collin Date: Sun, 16 Aug 2020 23:13:10 -0700 Subject: [PATCH 1/3] fix stack overflow error caused by SynthesisError --- compiler/src/errors/expression.rs | 8 +++++++- compiler/src/errors/value/address.rs | 2 +- compiler/src/errors/value/boolean.rs | 2 +- compiler/src/errors/value/field.rs | 6 +++--- compiler/src/errors/value/group.rs | 6 +++--- compiler/src/errors/value/integer.rs | 2 +- compiler/src/expression/relational/eq.rs | 2 +- compiler/src/expression/relational/ge.rs | 3 +-- compiler/src/expression/relational/gt.rs | 3 +-- compiler/src/expression/relational/le.rs | 3 +-- compiler/src/expression/relational/lt.rs | 3 +-- 11 files changed, 21 insertions(+), 19 deletions(-) diff --git a/compiler/src/errors/expression.rs b/compiler/src/errors/expression.rs index b1c5a18641..ba82b9741b 100644 --- a/compiler/src/errors/expression.rs +++ b/compiler/src/errors/expression.rs @@ -51,13 +51,19 @@ impl ExpressionError { pub fn cannot_enforce(operation: String, error: SynthesisError, span: Span) -> Self { let message = format!( - "the gadget operation `{}` failed due to synthesis error `{}`", + "the gadget operation `{}` failed due to synthesis error `{:?}`", operation, error, ); Self::new_from_span(message, span) } + pub fn cannot_evaluate(operation: String, span: Span) -> Self { + let message = format!("Mismatched types found for operation `{}`", operation); + + Self::new_from_span(message, span) + } + pub fn conditional_boolean(actual: String, span: Span) -> Self { let message = format!("if, else conditional must resolve to a boolean, found `{}`", actual); diff --git a/compiler/src/errors/value/address.rs b/compiler/src/errors/value/address.rs index 9f9c131f17..68584aaa73 100644 --- a/compiler/src/errors/value/address.rs +++ b/compiler/src/errors/value/address.rs @@ -28,7 +28,7 @@ impl AddressError { pub fn cannot_enforce(operation: String, error: SynthesisError, span: Span) -> Self { let message = format!( - "the address operation `{}` failed due to the synthesis error `{}`", + "the address operation `{:?}` failed due to the synthesis error `{}`", operation, error, ); diff --git a/compiler/src/errors/value/boolean.rs b/compiler/src/errors/value/boolean.rs index 9b96b586b4..dc385c65c4 100644 --- a/compiler/src/errors/value/boolean.rs +++ b/compiler/src/errors/value/boolean.rs @@ -22,7 +22,7 @@ impl BooleanError { pub fn cannot_enforce(operation: String, error: SynthesisError, span: Span) -> Self { let message = format!( - "the boolean operation `{}` failed due to the synthesis error `{}`", + "the boolean operation `{}` failed due to the synthesis error `{:?}`", operation, error, ); diff --git a/compiler/src/errors/value/field.rs b/compiler/src/errors/value/field.rs index 84c0a49979..d33d8408b8 100644 --- a/compiler/src/errors/value/field.rs +++ b/compiler/src/errors/value/field.rs @@ -21,14 +21,14 @@ impl FieldError { } pub fn negate_operation(error: SynthesisError, span: Span) -> Self { - let message = format!("field negation failed due to synthesis error `{}`", error,); + let message = format!("field negation failed due to synthesis error `{:?}`", error,); Self::new_from_span(message, span) } pub fn binary_operation(operation: String, error: SynthesisError, span: Span) -> Self { let message = format!( - "the field binary operation `{}` failed due to synthesis error `{}`", + "the field binary operation `{}` failed due to synthesis error `{:?}`", operation, error, ); @@ -54,7 +54,7 @@ impl FieldError { } pub fn synthesis_error(error: SynthesisError, span: Span) -> Self { - let message = format!("compilation failed due to field synthesis error `{}`", error); + let message = format!("compilation failed due to field synthesis error `{:?}`", error); Self::new_from_span(message, span) } diff --git a/compiler/src/errors/value/group.rs b/compiler/src/errors/value/group.rs index 5934282406..2f4086522f 100644 --- a/compiler/src/errors/value/group.rs +++ b/compiler/src/errors/value/group.rs @@ -21,14 +21,14 @@ impl GroupError { } pub fn negate_operation(error: SynthesisError, span: Span) -> Self { - let message = format!("group negation failed due to the synthesis error `{}`", error,); + let message = format!("group negation failed due to the synthesis error `{:?}`", error,); Self::new_from_span(message, span) } pub fn binary_operation(operation: String, error: SynthesisError, span: Span) -> Self { let message = format!( - "the group binary operation `{}` failed due to the synthesis error `{}`", + "the group binary operation `{}` failed due to the synthesis error `{:?}`", operation, error, ); @@ -48,7 +48,7 @@ impl GroupError { } pub fn synthesis_error(error: SynthesisError, span: Span) -> Self { - let message = format!("compilation failed due to group synthesis error `{}`", error); + let message = format!("compilation failed due to group synthesis error `{:?}`", error); Self::new_from_span(message, span) } diff --git a/compiler/src/errors/value/integer.rs b/compiler/src/errors/value/integer.rs index 9d7f906961..c2de51c90c 100644 --- a/compiler/src/errors/value/integer.rs +++ b/compiler/src/errors/value/integer.rs @@ -23,7 +23,7 @@ impl IntegerError { pub fn cannot_enforce(operation: String, error: SynthesisError, span: Span) -> Self { let message = format!( - "the integer operation `{}` failed due to the synthesis error `{}`", + "the integer operation `{}` failed due to the synthesis error `{:?}`", operation, error, ); diff --git a/compiler/src/expression/relational/eq.rs b/compiler/src/expression/relational/eq.rs index 6f62542001..2bc42316db 100644 --- a/compiler/src/expression/relational/eq.rs +++ b/compiler/src/expression/relational/eq.rs @@ -47,7 +47,7 @@ pub fn evaluate_eq, CS: ConstraintSystem< } }; - let boolean = constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate equal"), e, span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(format!("=="), span))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/ge.rs b/compiler/src/expression/relational/ge.rs index a91cc8d3bc..1e8bb9c818 100644 --- a/compiler/src/expression/relational/ge.rs +++ b/compiler/src/expression/relational/ge.rs @@ -36,8 +36,7 @@ pub fn evaluate_ge, CS: ConstraintSystem< } }; - let boolean = constraint_result - .map_err(|e| ExpressionError::cannot_enforce(format!("evaluate greater than or equal"), e, span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(format!(">="), span))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/gt.rs b/compiler/src/expression/relational/gt.rs index 4fc26cbe20..f81874b793 100644 --- a/compiler/src/expression/relational/gt.rs +++ b/compiler/src/expression/relational/gt.rs @@ -36,8 +36,7 @@ pub fn evaluate_gt, CS: ConstraintSystem< } }; - let boolean = - constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate greater than"), e, span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(format!(">"), span))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/le.rs b/compiler/src/expression/relational/le.rs index 06f8d47294..ccea8ecd4d 100644 --- a/compiler/src/expression/relational/le.rs +++ b/compiler/src/expression/relational/le.rs @@ -36,8 +36,7 @@ pub fn evaluate_le, CS: ConstraintSystem< } }; - let boolean = constraint_result - .map_err(|e| ExpressionError::cannot_enforce(format!("evaluate less than or equal"), e, span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(format!("<="), span))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/lt.rs b/compiler/src/expression/relational/lt.rs index 4c49c36bd1..ddfd432771 100644 --- a/compiler/src/expression/relational/lt.rs +++ b/compiler/src/expression/relational/lt.rs @@ -36,8 +36,7 @@ pub fn evaluate_lt, CS: ConstraintSystem< } }; - let boolean = - constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate less than"), e, span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(format!("<"), span))?; Ok(ConstrainedValue::Boolean(boolean)) } From 5bd2575fd5f962979b86adaac49c66dd23a0be32 Mon Sep 17 00:00:00 2001 From: collin Date: Mon, 17 Aug 2020 01:30:13 -0700 Subject: [PATCH 2/3] add test to check for stack overflow --- compiler/tests/syntax/compare_mismatched_types.leo | 3 +++ compiler/tests/syntax/mod.rs | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 compiler/tests/syntax/compare_mismatched_types.leo diff --git a/compiler/tests/syntax/compare_mismatched_types.leo b/compiler/tests/syntax/compare_mismatched_types.leo new file mode 100644 index 0000000000..8cf6327cb3 --- /dev/null +++ b/compiler/tests/syntax/compare_mismatched_types.leo @@ -0,0 +1,3 @@ +function main() { + let a = -5i8 > 342u32; +} \ No newline at end of file diff --git a/compiler/tests/syntax/mod.rs b/compiler/tests/syntax/mod.rs index 66f479796d..8eb71ed403 100644 --- a/compiler/tests/syntax/mod.rs +++ b/compiler/tests/syntax/mod.rs @@ -54,3 +54,12 @@ fn input_syntax_error() { _ => panic!("input syntax error should be a ParserError"), } } + +#[test] +fn test_compare_mismatched_types() { + let bytes = include_bytes!("compare_mismatched_types.leo"); + let program = parse_program(bytes).unwrap(); + + // previously this bug caused a stack overflow + expect_compiler_error(program); +} From 20ae244f671d2d357b71c597df0b434e00f09b8c Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 19 Aug 2020 00:44:01 -0700 Subject: [PATCH 3/3] expect compiler error for integer test --- compiler/tests/integers/int_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/tests/integers/int_macro.rs b/compiler/tests/integers/int_macro.rs index a074f5c420..cdcd3e7f3d 100644 --- a/compiler/tests/integers/int_macro.rs +++ b/compiler/tests/integers/int_macro.rs @@ -189,7 +189,7 @@ macro_rules! test_int { program.set_main_input(main_input); - expect_computation_error(program); + expect_compiler_error(program); } else { let c = match a.checked_div(b) { Some(valid) => valid,