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: add Type::as_struct #5680

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"type_as_constant" => type_as_constant(arguments, return_type, location),
"type_as_integer" => type_as_integer(arguments, return_type, location),
"type_as_slice" => type_as_slice(arguments, return_type, location),
"type_as_struct" => type_as_struct(arguments, return_type, location),
"type_as_tuple" => type_as_tuple(arguments, return_type, location),
"type_eq" => type_eq(arguments, location),
"type_is_bool" => type_is_bool(arguments, location),
Expand Down Expand Up @@ -485,7 +486,7 @@
})?;

let typ =
interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ));

Check warning on line 489 in compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)

Check warning on line 489 in compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)

Ok(Value::Type(typ))
}
Expand Down Expand Up @@ -550,6 +551,27 @@
})
}

// fn as_struct(self) -> Option<(StructDefinition, [Type])>
fn type_as_struct(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
type_as(arguments, return_type, location, |typ| {
if let Type::Struct(struct_type, generics) = typ {
Some(Value::Tuple(vec![
Value::StructDefinition(struct_type.borrow().id),
Value::Slice(
generics.into_iter().map(Value::Type).collect(),
Type::Quoted(QuotedType::Type),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
),
]))
} else {
None
}
})
}

// fn as_tuple(self) -> Option<[Type]>
fn type_as_tuple(
arguments: Vec<(Value, Location)>,
Expand Down
3 changes: 3 additions & 0 deletions noir_stdlib/src/meta/typ.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ impl Type {
#[builtin(type_as_slice)]
fn as_slice(self) -> Option<Type> {}

#[builtin(type_as_struct)]
fn as_struct(self) -> Option<(StructDefinition, [Type])> {}

#[builtin(type_as_tuple)]
fn as_tuple(self) -> Option<[Type]> {}

Expand Down
16 changes: 16 additions & 0 deletions test_programs/compile_success_empty/comptime_type/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::meta::type_of;

struct Foo<T> {
x: T
}

fn main() {
comptime
{
Expand Down Expand Up @@ -66,5 +70,17 @@ fn main() {
let yes = true;
let bool_type = type_of(yes);
assert(bool_type.is_bool());

// Check Type::as_struct
assert(u8_type.as_struct().is_none());

let foo = Foo { x: 0 };
let foo_type = type_of(foo);
let (struct_definition, generics) = foo_type.as_struct().unwrap();
let fields = struct_definition.fields();
assert_eq(fields.len(), 1);
jfecher marked this conversation as resolved.
Show resolved Hide resolved

assert_eq(generics.len(), 1);
assert_eq(generics[0], field_type_1);
}
}
Loading