From b0def14513c60a8ea44fc03e1b08cbf55caceecf Mon Sep 17 00:00:00 2001 From: Ethan-000 Date: Mon, 7 Aug 2023 15:13:41 +0100 Subject: [PATCH] chore: Improve unary error (#2199) * unary error * . * Update crates/noirc_frontend/src/hir/type_check/errors.rs --------- Co-authored-by: jfecher --- crates/noirc_frontend/src/hir/type_check/errors.rs | 3 +++ crates/noirc_frontend/src/hir/type_check/expr.rs | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/hir/type_check/errors.rs b/crates/noirc_frontend/src/hir/type_check/errors.rs index 4f032503f3d..8b3623db3c3 100644 --- a/crates/noirc_frontend/src/hir/type_check/errors.rs +++ b/crates/noirc_frontend/src/hir/type_check/errors.rs @@ -71,6 +71,8 @@ pub enum TypeCheckError { IntegerBitWidth { bit_width_x: u32, bit_width_y: u32, span: Span }, #[error("{kind} cannot be used in an infix operation")] InvalidInfixOp { kind: &'static str, span: Span }, + #[error("{kind} cannot be used in a unary operation")] + InvalidUnaryOp { kind: String, span: Span }, #[error("Bitwise operations are invalid on Field types. Try casting the operands to a sized integer type first.")] InvalidBitwiseOperationOnField { span: Span }, #[error("Integer cannot be used with type {typ}")] @@ -174,6 +176,7 @@ impl From for Diagnostic { | TypeCheckError::IntegerSignedness { span, .. } | TypeCheckError::IntegerBitWidth { span, .. } | TypeCheckError::InvalidInfixOp { span, .. } + | TypeCheckError::InvalidUnaryOp { span, .. } | TypeCheckError::InvalidBitwiseOperationOnField { span, .. } | TypeCheckError::IntegerTypeMismatch { span, .. } | TypeCheckError::FieldComparison { span, .. } diff --git a/crates/noirc_frontend/src/hir/type_check/expr.rs b/crates/noirc_frontend/src/hir/type_check/expr.rs index 6c111a1d6a0..5bc6aa45359 100644 --- a/crates/noirc_frontend/src/hir/type_check/expr.rs +++ b/crates/noirc_frontend/src/hir/type_check/expr.rs @@ -1088,7 +1088,13 @@ impl<'interner> TypeChecker<'interner> { }; match op { - crate::UnaryOp::Minus => unify(Type::polymorphic_integer(self.interner)), + crate::UnaryOp::Minus => { + let expected = Type::polymorphic_integer(self.interner); + rhs_type.unify(&expected, span, &mut self.errors, || { + TypeCheckError::InvalidUnaryOp { kind: rhs_type.to_string(), span } + }); + expected + } crate::UnaryOp::Not => { let rhs_type = rhs_type.follow_bindings();