Skip to content
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
6 changes: 4 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ir/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ fn display_instruction_inner(
Instruction::DecrementRc { value } => {
writeln!(f, "dec_rc {}", show(*value))
}
Instruction::RangeCheck { value, max_bit_size, .. } => {
writeln!(f, "range_check {} to {} bits", show(*value), *max_bit_size,)
Instruction::RangeCheck { value, max_bit_size, assert_message } => {
let message =
assert_message.as_ref().map(|message| format!(", {message:?}")).unwrap_or_default();
writeln!(f, "range_check {} to {} bits{}", show(*value), *max_bit_size, message)
}
Instruction::IfElse { then_condition, then_value, else_condition, else_value } => {
let then_condition = show(*then_condition);
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub(crate) enum ParsedInstruction {
RangeCheck {
value: ParsedValue,
max_bit_size: u32,
assert_message: Option<String>,
},
Store {
value: ParsedValue,
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ impl Translator {
let value_id = self.builder.insert_not(value);
self.define_variable(target, value_id)?;
}
ParsedInstruction::RangeCheck { value, max_bit_size } => {
ParsedInstruction::RangeCheck { value, max_bit_size, assert_message } => {
let value = self.translate_value(value)?;
self.builder.insert_range_check(value, max_bit_size, None);
self.builder.insert_range_check(value, max_bit_size, assert_message);
}
ParsedInstruction::Store { value, address } => {
let value = self.translate_value(value)?;
Expand Down
20 changes: 19 additions & 1 deletion compiler/noirc_evaluator/src/ssa/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ impl<'a> Parser<'a> {
self.eat_or_error(Token::Keyword(Keyword::To))?;
let max_bit_size = self.eat_int_or_error()?.to_u128() as u32;
self.eat_or_error(Token::Keyword(Keyword::Bits))?;
Ok(Some(ParsedInstruction::RangeCheck { value, max_bit_size }))

let assert_message =
if self.eat(Token::Comma)? { Some(self.eat_str_or_error()?) } else { None };

Ok(Some(ParsedInstruction::RangeCheck { value, max_bit_size, assert_message }))
}

fn parse_store(&mut self) -> ParseResult<Option<ParsedInstruction>> {
Expand Down Expand Up @@ -988,6 +992,10 @@ impl<'a> Parser<'a> {
}
}

fn eat_str_or_error(&mut self) -> ParseResult<String> {
if let Some(message) = self.eat_str()? { Ok(message) } else { self.expected_string() }
}

fn eat_byte_str(&mut self) -> ParseResult<Option<String>> {
if matches!(self.token.token(), Token::ByteStr(..)) {
let token = self.bump()?;
Expand Down Expand Up @@ -1037,6 +1045,13 @@ impl<'a> Parser<'a> {
})
}

fn expected_string<T>(&mut self) -> ParseResult<T> {
Err(ParserError::ExpectedString {
found: self.token.token().clone(),
span: self.token.span(),
})
}

fn expected_string_or_data<T>(&mut self) -> ParseResult<T> {
Err(ParserError::ExpectedStringOrData {
found: self.token.token().clone(),
Expand Down Expand Up @@ -1120,6 +1135,8 @@ pub(crate) enum ParserError {
ExpectedType { found: Token, span: Span },
#[error("Expected an instruction or terminator, found '{found}'")]
ExpectedInstructionOrTerminator { found: Token, span: Span },
#[error("Expected a string literal, found '{found}'")]
ExpectedString { found: Token, span: Span },
#[error("Expected a string literal or 'data', found '{found}'")]
ExpectedStringOrData { found: Token, span: Span },
#[error("Expected a byte string literal, found '{found}'")]
Expand All @@ -1146,6 +1163,7 @@ impl ParserError {
| ParserError::ExpectedInt { span, .. }
| ParserError::ExpectedType { span, .. }
| ParserError::ExpectedInstructionOrTerminator { span, .. }
| ParserError::ExpectedString { span, .. }
| ParserError::ExpectedStringOrData { span, .. }
| ParserError::ExpectedByteString { span, .. }
| ParserError::ExpectedValue { span, .. }
Expand Down
12 changes: 12 additions & 0 deletions compiler/noirc_evaluator/src/ssa/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ fn test_range_check() {
assert_ssa_roundtrip(src);
}

#[test]
fn test_range_check_with_message() {
let src = r#"
acir(inline) fn main f0 {
b0(v0: Field):
range_check v0 to 8 bits, "overflow error\n\t"
return
}
"#;
assert_ssa_roundtrip(src);
}

#[test]
fn test_allocate() {
let src = "
Expand Down
Loading