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

feat: unquote some value as tokens, not as unquote markers #5924

Merged
merged 1 commit into from
Sep 4, 2024
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
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 @@ -95,8 +95,8 @@
Value::Expr(ExprValue::Statement(statement))
}

pub(crate) fn lvalue(lvaue: LValue) -> Self {

Check warning on line 98 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
Value::Expr(ExprValue::LValue(lvaue))

Check warning on line 99 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
}

pub(crate) fn get_type(&self) -> Cow<Type> {
Expand Down Expand Up @@ -429,6 +429,9 @@
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 @@
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 @@ -2316,7 +2316,7 @@
}

#[test]
fn underflowing_u8() {

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: u8 = -1;
Expand Down Expand Up @@ -2354,7 +2354,7 @@
}

#[test]
fn underflowing_i8() {

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: i8 = -129;
Expand Down Expand Up @@ -3341,3 +3341,31 @@
)
));
}

#[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);
}
Loading