Skip to content

Commit

Permalink
chore: Improve unary error (#2199)
Browse files Browse the repository at this point in the history
* unary error

* .

* Update crates/noirc_frontend/src/hir/type_check/errors.rs

---------

Co-authored-by: jfecher <[email protected]>
  • Loading branch information
Ethan-000 and jfecher authored Aug 7, 2023
1 parent c519ad2 commit b0def14
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down Expand Up @@ -174,6 +176,7 @@ impl From<TypeCheckError> for Diagnostic {
| TypeCheckError::IntegerSignedness { span, .. }
| TypeCheckError::IntegerBitWidth { span, .. }
| TypeCheckError::InvalidInfixOp { span, .. }
| TypeCheckError::InvalidUnaryOp { span, .. }
| TypeCheckError::InvalidBitwiseOperationOnField { span, .. }
| TypeCheckError::IntegerTypeMismatch { span, .. }
| TypeCheckError::FieldComparison { span, .. }
Expand Down
8 changes: 7 additions & 1 deletion crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit b0def14

Please sign in to comment.