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: Implement where clauses on impls #3324

Merged
merged 3 commits into from
Oct 27, 2023
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
5 changes: 3 additions & 2 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@
for trait_impl in impls {
let trait_name = trait_impl.trait_name.clone();

let unresolved_functions =
let mut unresolved_functions =
self.collect_trait_impl_function_overrides(context, &trait_impl, krate);

let module = ModuleId { krate, local_id: self.module_id };

for (_, func_id, noir_function) in &unresolved_functions.functions {
for (_, func_id, noir_function) in &mut unresolved_functions.functions {
noir_function.def.where_clause.append(&mut trait_impl.where_clause.clone());
context.def_interner.push_function(*func_id, &noir_function.def, module);
}

Expand Down Expand Up @@ -382,7 +383,7 @@
let modifiers = FunctionModifiers {
name: name.to_string(),
visibility: crate::FunctionVisibility::Public,
// TODO(Maddiaa): Investigate trait implementations with attributes see: https://github.com/noir-lang/noir/issues/2629

Check warning on line 386 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Maddiaa)
attributes: crate::token::Attributes::empty(),
is_unconstrained: false,
contract_function_type: None,
Expand Down Expand Up @@ -438,7 +439,7 @@
}
}
TraitItem::Type { name } => {
// TODO(nickysn or alexvitkov): implement context.def_interner.push_empty_type_alias and get an id, instead of using TypeAliasId::dummy_id()

Check warning on line 442 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (nickysn)

Check warning on line 442 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (alexvitkov)
if let Err((first_def, second_def)) = self.def_collector.def_map.modules
[id.0.local_id.0]
.declare_type_alias(name.clone(), TypeAliasId::dummy_id())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "impl_with_where_clause"
type = "bin"
authors = [""]
compiler_version = "0.10.5"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

fn main() {
let array: [Field; 3] = [1, 2, 3];
assert(array.eq(array));

// Ensure this still works if we have to infer the type of the integer literals
let array = [1, 2, 3];
assert(array.eq(array));
}

trait Eq {
fn eq(self, other: Self) -> bool;
}

impl<T, N> Eq for [T; N] where T: Eq {
fn eq(self, other: Self) -> bool {
let mut ret = true;
for i in 0 .. self.len() {
ret &= self[i].eq(other[i]);
}
ret
}
}

impl Eq for Field {
fn eq(self, other: Field) -> bool {
self == other
}
}
Loading