Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion crates/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use acvm::FieldElement;
use noirc_errors::CustomDiagnostic as Diagnostic;
use noirc_errors::Span;
use thiserror::Error;
Expand Down Expand Up @@ -29,6 +30,8 @@ pub enum Source {
pub enum TypeCheckError {
#[error("Operator {op:?} cannot be used in a {place:?}")]
OpCannotBeUsed { op: HirBinaryOp, place: &'static str, span: Span },
#[error("The literal `{expr:?}` cannot fit into `{ty}` which has range `{range}`")]
OverflowingAssignment { expr: FieldElement, ty: Type, range: String, span: Span },
#[error("Type {typ:?} cannot be used in a {place:?}")]
TypeCannotBeUsed { typ: Type, place: &'static str, span: Span },
#[error("Expected type {expected_typ:?} is not the same as {expr_typ:?}")]
Expand Down Expand Up @@ -170,7 +173,8 @@ impl From<TypeCheckError> for Diagnostic {
| TypeCheckError::IntegerTypeMismatch { span, .. }
| TypeCheckError::FieldComparison { span, .. }
| TypeCheckError::AmbiguousBitWidth { span, .. }
| TypeCheckError::IntegerAndFieldBinaryOperation { span } => {
| TypeCheckError::IntegerAndFieldBinaryOperation { span }
| TypeCheckError::OverflowingAssignment { span, .. } => {
Diagnostic::simple_error(error.to_string(), String::new(), span)
}
TypeCheckError::PublicReturnType { typ, span } => Diagnostic::simple_error(
Expand Down
37 changes: 37 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,46 @@ impl<'interner> TypeChecker<'interner> {
expr_span,
}
});
if annotated_type.is_unsigned() {
self.lint_overflowing_uint(&rhs_expr, &annotated_type);
}
annotated_type
} else {
expr_type
}
}

Comment thread
jfecher marked this conversation as resolved.
fn lint_overflowing_uint(&mut self, rhs_expr: &ExprId, annotated_type: &Type) {
let expr = self.interner.expression(rhs_expr);
let span = self.interner.expr_span(rhs_expr);
match expr {
crate::hir_def::expr::HirExpression::Literal(literal) => match literal {
crate::hir_def::expr::HirLiteral::Integer(value) => {
Comment thread
TomAFrench marked this conversation as resolved.
Outdated
let v: u128 = value.to_u128();
match annotated_type {
Type::Integer(_, bit) => {
let max = 1 << bit;
if v >= max {
self.errors.push(TypeCheckError::OverflowingAssignment {
expr: value,
ty: annotated_type.clone(),
range: format!("0..={}", max - 1),
span: span,
})
}
}
_ => {}
}
}
_ => {}
},
crate::hir_def::expr::HirExpression::Prefix(_) => {
self.errors.push(TypeCheckError::InvalidUnaryOp {
kind: annotated_type.to_string(),
span: span,
})
}
_ => {}
}
}
}
4 changes: 4 additions & 0 deletions crates/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ impl Type {
matches!(self.follow_bindings(), Type::Integer(Signedness::Signed, _))
}

pub fn is_unsigned(&self) -> bool {
matches!(self.follow_bindings(), Type::Integer(Signedness::Unsigned, _))
}

fn contains_numeric_typevar(&self, target_id: TypeVariableId) -> bool {
// True if the given type is a NamedGeneric with the target_id
let named_generic_id_matches_target = |typ: &Type| {
Expand Down