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
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 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"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 @@ -550,6 +551,27 @@ fn type_as_slice(
})
}

// 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::Slice(Box::new(Type::Quoted(QuotedType::Type))),
),
]))
} 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);

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