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::implements #5701

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"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_implements" => type_implements(interner, arguments, location),
"type_is_bool" => type_is_bool(arguments, location),
"type_is_field" => type_is_field(arguments, location),
"type_of" => type_of(arguments, location),
Expand Down Expand Up @@ -356,7 +357,7 @@
let argument = check_one_argument(arguments, location)?;
let typ = parse(argument, parser::parse_type(), "a type")?;
let typ =
interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ));

Check warning on line 360 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 360 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 @@ -490,6 +491,21 @@
Ok(Value::Bool(self_type == other_type))
}

// fn implements(self, constraint: TraitConstraint) -> bool
fn type_implements(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let (typ, constraint) = check_two_arguments(arguments, location)?;

let typ = get_type(typ)?;
let (trait_id, generics) = get_trait_constraint(constraint)?;

let implements = interner.try_lookup_trait_implementation(&typ, trait_id, &generics).is_ok();
Ok(Value::Bool(implements))
}

// fn is_bool(self) -> bool
fn type_is_bool(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
let value = check_one_argument(arguments, 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 @@ -20,6 +20,9 @@ impl Type {
#[builtin(type_as_tuple)]
fn as_tuple(self) -> Option<[Type]> {}

#[builtin(type_implements)]
fn implements(self, constraint: TraitConstraint) -> bool {}

#[builtin(type_is_bool)]
fn is_bool(self) -> bool {}

Expand Down
21 changes: 21 additions & 0 deletions test_programs/compile_success_empty/comptime_type/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ struct Foo<T> {
x: T
}

trait SomeTrait {

}
struct StructImplementsSomeTrait {

}

impl SomeTrait for StructImplementsSomeTrait {

}

struct StructDoesNotImplementSomeTrait {

}
michaeljklein marked this conversation as resolved.
Show resolved Hide resolved

fn main() {
comptime
{
Expand Down Expand Up @@ -82,5 +97,11 @@ fn main() {

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

let some_trait = quote { SomeTrait }.as_trait_constraint();
let struct_implements_some_trait = quote { StructImplementsSomeTrait }.as_type();
let struct_does_not_implement_some_trait = quote { StructDoesNotImplementSomeTrait }.as_type();
assert(struct_implements_some_trait.implements(some_trait));
assert(!struct_does_not_implement_some_trait.implements(some_trait));
}
}
Loading