Skip to content

Commit

Permalink
fix: remove assumed parent traits
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 28, 2024
1 parent 2f37610 commit 19b6415
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1875,8 +1875,33 @@ impl NodeInterner {

/// Removes all TraitImplKind::Assumed from the list of known impls for the given trait
pub fn remove_assumed_trait_implementations_for_trait(&mut self, trait_id: TraitId) {
self.remove_assumed_trait_implementations_for_trait_and_parents(trait_id, trait_id);
}

fn remove_assumed_trait_implementations_for_trait_and_parents(
&mut self,
trait_id: TraitId,
starting_trait_id: TraitId,
) {
let entries = self.trait_implementation_map.entry(trait_id).or_default();
entries.retain(|(_, kind)| matches!(kind, TraitImplKind::Normal(_)));

// Also remove assumed implementations for the parent traits, if any
if let Some(trait_bounds) =
self.try_get_trait(trait_id).map(|the_trait| the_trait.trait_bounds.clone())
{
for parent_trait_bound in trait_bounds {
// Avoid looping forever in case there are cycles
if parent_trait_bound.trait_id == starting_trait_id {
continue;
}

self.remove_assumed_trait_implementations_for_trait_and_parents(
parent_trait_bound.trait_id,
starting_trait_id,
);
}
}
}

/// Tags the given identifier with the selected trait_impl so that monomorphization
Expand Down
21 changes: 21 additions & 0 deletions compiler/noirc_frontend/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,24 @@ fn errors_if_impl_trait_constraint_is_not_satisfied() {
assert_eq!(typ, "SomeGreeter");
assert_eq!(impl_trait, "Foo");
}

#[test]
fn removes_assumed_parent_traits_after_function_ends() {
let src = r#"
trait Foo {}
trait Bar: Foo {}
pub fn foo<T>()
where
T: Bar,
{}
pub fn bar<T>()
where
T: Foo,
{}
fn main() {}
"#;
assert_no_errors(src);
}

0 comments on commit 19b6415

Please sign in to comment.