Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve unary error #2199

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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