Skip to content

Commit

Permalink
feat: unquote some value as tokens, not as unquote markers
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Sep 4, 2024
1 parent 44cf9a2 commit 2b80fe3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ impl Value {
location: Location,
) -> IResult<Vec<Token>> {
let token = match self {
Value::Unit => {
return Ok(vec![Token::LeftParen, Token::RightParen]);
}
Value::Quoted(tokens) => return Ok(unwrap_rc(tokens)),
Value::Type(typ) => Token::QuotedType(interner.push_quoted_type(typ)),
Value::Expr(ExprValue::Expression(expr)) => {
Expand All @@ -443,6 +446,40 @@ impl Value {
Value::UnresolvedType(typ) => {
Token::InternedUnresolvedTypeData(interner.push_unresolved_type_data(typ))
}
Value::U1(bool) => Token::Bool(bool),
Value::U8(value) => Token::Int((value as u128).into()),
Value::U16(value) => Token::Int((value as u128).into()),
Value::U32(value) => Token::Int((value as u128).into()),
Value::U64(value) => Token::Int((value as u128).into()),
Value::I8(value) => {
if value < 0 {
return Ok(vec![Token::Minus, Token::Int((-value as u128).into())]);
} else {
Token::Int((value as u128).into())
}
}
Value::I16(value) => {
if value < 0 {
return Ok(vec![Token::Minus, Token::Int((-value as u128).into())]);
} else {
Token::Int((value as u128).into())
}
}
Value::I32(value) => {
if value < 0 {
return Ok(vec![Token::Minus, Token::Int((-value as u128).into())]);
} else {
Token::Int((value as u128).into())
}
}
Value::I64(value) => {
if value < 0 {
return Ok(vec![Token::Minus, Token::Int((-value as u128).into())]);
} else {
Token::Int((value as u128).into())
}
}
Value::Field(value) => Token::Int(value),
other => Token::UnquoteMarker(other.into_hir_expression(interner, location)?),
};
Ok(vec![token])
Expand Down
28 changes: 28 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3341,3 +3341,31 @@ fn warns_on_re_export_of_item_with_less_visibility() {
)
));
}

#[test]
fn unoquted_integer_as_integer_token() {

Check warning on line 3346 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unoquted)
let src = r#"
trait Serialize<let N: u32> {
fn serialize() {}
}
#[attr]
fn foobar() {}
fn attr(_f: FunctionDefinition) -> Quoted {
let serialized_len = 1;
// We are testing that when we unoqute $serialized_len, it's unquoted

Check warning on line 3357 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unoqute)
// as the token `1` and not as something else that later won't be parsed correctly
// in the context of a generic argument.
quote {
impl Serialize<$serialized_len> for Field {
fn serialize() { }
}
}
}
fn main() {}
"#;

assert_no_errors(src);
}

0 comments on commit 2b80fe3

Please sign in to comment.