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

fix: add negative integer literals #3690

Merged
merged 8 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 1 addition & 7 deletions compiler/noirc_evaluator/src/ssa/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,7 @@ impl NumericType {
/// for the current NumericType.
pub(crate) fn value_is_within_limits(self, field: FieldElement) -> bool {
match self {
NumericType::Signed { bit_size } => {
let min = -(2i128.pow(bit_size - 1));
let max = 2u128.pow(bit_size - 1) - 1;
// Signed integers are odd since they will overflow the field value
field <= max.into() || field >= min.into()
}
NumericType::Unsigned { bit_size } => {
NumericType::Signed { bit_size } | NumericType::Unsigned { bit_size } => {
let max = 2u128.pow(bit_size) - 1;
field <= max.into()
}
Expand Down
11 changes: 10 additions & 1 deletion compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ impl ExpressionKind {
}

pub fn prefix(operator: UnaryOp, rhs: Expression) -> ExpressionKind {
ExpressionKind::Prefix(Box::new(PrefixExpression { operator, rhs }))
match (operator, &rhs) {
(
UnaryOp::Minus,
Expression { kind: ExpressionKind::Literal(Literal::Integer(field)), .. },
) => ExpressionKind::Literal(Literal::NegativeInteger(*field)),
_ => ExpressionKind::Prefix(Box::new(PrefixExpression { operator, rhs })),
}
}

pub fn array(contents: Vec<Expression>) -> ExpressionKind {
Expand All @@ -65,6 +71,7 @@ impl ExpressionKind {
}

pub fn integer(contents: FieldElement) -> ExpressionKind {
// todo!();
ExpressionKind::Literal(Literal::Integer(contents))
}

Expand Down Expand Up @@ -315,6 +322,7 @@ pub enum Literal {
Array(ArrayLiteral),
Bool(bool),
Integer(FieldElement),
NegativeInteger(FieldElement),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
Str(String),
RawStr(String, u8),
FmtStr(String),
Expand Down Expand Up @@ -519,6 +527,7 @@ impl Display for Literal {
}
Literal::FmtStr(string) => write!(f, "f\"{string}\""),
Literal::Unit => write!(f, "()"),
Literal::NegativeInteger(integer) => write!(f, "-{}", integer.to_u128()),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,7 @@ impl<'a> Resolver<'a> {
HirLiteral::Array(HirArrayLiteral::Repeated { repeated_element, length })
}
Literal::Integer(integer) => HirLiteral::Integer(integer),
Literal::NegativeInteger(integer) => HirLiteral::NegativeInteger(integer),
Literal::Str(str) => HirLiteral::Str(str),
Literal::RawStr(str, _) => HirLiteral::Str(str),
Literal::FmtStr(str) => self.resolve_fmt_str_literal(str, expr.span),
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl<'interner> TypeChecker<'interner> {
Type::Array(Box::new(length), Box::new(elem_type))
}
HirLiteral::Bool(_) => Type::Bool,
HirLiteral::Integer(_) => Type::polymorphic_integer(self.interner),
HirLiteral::Integer(_) | HirLiteral::NegativeInteger(_) => {
Type::polymorphic_integer(self.interner)
}
HirLiteral::Str(string) => {
let len = Type::Constant(string.len() as u64);
Type::String(Box::new(len))
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir_def/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub enum HirLiteral {
Array(HirArrayLiteral),
Bool(bool),
Integer(FieldElement),
NegativeInteger(FieldElement),
Str(String),
FmtStr(String, Vec<ExprId>),
Unit,
Expand Down
12 changes: 12 additions & 0 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ impl<'interner> Monomorphizer<'interner> {
let location = self.interner.id_location(expr);
Literal(Integer(value, typ, location))
}
HirExpression::Literal(HirLiteral::NegativeInteger(value)) => {
let typ = self.convert_type(&self.interner.id_type(expr));
let location = self.interner.id_location(expr);
match typ {
ast::Type::Field => Literal(Integer(-value, typ, location)),
ast::Type::Integer(_, bit_size) => {
let base = 1_u128 << bit_size;
Literal(Integer(FieldElement::from(base) - value, typ, location))
}
_ => unreachable!("Integer literal must be numeric"),
}
}
HirExpression::Literal(HirLiteral::Array(array)) => match array {
HirArrayLiteral::Standard(array) => self.standard_array(expr, array),
HirArrayLiteral::Repeated { repeated_element, length } => {
Expand Down
6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_3394/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "regression_3394"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_3394/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::std;

fn main() {
let x : i8 = -128;
std::println(x);
}
1 change: 1 addition & 0 deletions tooling/nargo_fmt/src/rewrite/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub(crate) fn rewrite(
),
ExpressionKind::Literal(literal) => match literal {
Literal::Integer(_)
| Literal::NegativeInteger(_)
| Literal::Bool(_)
| Literal::Str(_)
| Literal::RawStr(..)
Expand Down
Loading