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

fix: remove assumed parent traits #6365

Merged
merged 1 commit into from
Oct 28, 2024
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
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 @@ -219,12 +219,12 @@
interned_statement_kinds: noirc_arena::Arena<StatementKind>,

// Interned `UnresolvedTypeData`s during comptime code.
interned_unresolved_type_datas: noirc_arena::Arena<UnresolvedTypeData>,

Check warning on line 222 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)

// Interned `Pattern`s during comptime code.
interned_patterns: noirc_arena::Arena<Pattern>,

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 227 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -669,7 +669,7 @@
quoted_types: Default::default(),
interned_expression_kinds: Default::default(),
interned_statement_kinds: Default::default(),
interned_unresolved_type_datas: Default::default(),

Check warning on line 672 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
interned_patterns: Default::default(),
lsp_mode: false,
location_indices: LocationIndices::default(),
Expand Down Expand Up @@ -1875,8 +1875,33 @@

/// 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 Expand Up @@ -2180,11 +2205,11 @@
&mut self,
typ: UnresolvedTypeData,
) -> InternedUnresolvedTypeData {
InternedUnresolvedTypeData(self.interned_unresolved_type_datas.insert(typ))

Check warning on line 2208 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

pub fn get_unresolved_type_data(&self, id: InternedUnresolvedTypeData) -> &UnresolvedTypeData {
&self.interned_unresolved_type_datas[id.0]

Check warning on line 2212 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

/// Returns the type of an operator (which is always a function), along with its return type.
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);
}
Loading