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: Prevent overlapping associated types impls #7047

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,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 217 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 222 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 @@ -1716,8 +1716,18 @@
let instantiated_object_type = object_type.substitute(&substitutions);

let trait_generics = &trait_impl.borrow().trait_generics;

// Replace any associated types with fresh type variables so that we match
// any existing impl regardless of associated types if one already exists.
// E.g. if we already have an `impl Foo<Bar = i32> for Baz`, we should
// reject `impl Foo<Bar = u32> for Baz` if it were to be added.
let associated_types = self.get_associated_types_for_impl(impl_id);

let associated_types = vecmap(associated_types, |named| {
let typ = self.next_type_variable();
NamedType { name: named.name.clone(), typ }
});

// Ignoring overlapping `TraitImplKind::Assumed` impls here is perfectly fine.
// It should never happen since impls are defined at global scope, but even
// if they were, we should never prevent defining a new impl because a 'where'
Expand All @@ -1726,7 +1736,7 @@
&instantiated_object_type,
trait_id,
trait_generics,
associated_types,
&associated_types,
) {
let existing_impl = self.get_trait_implementation(existing);
let existing_impl = existing_impl.borrow();
Expand Down Expand Up @@ -2134,11 +2144,11 @@
&mut self,
typ: UnresolvedTypeData,
) -> InternedUnresolvedTypeData {
InternedUnresolvedTypeData(self.interned_unresolved_type_datas.insert(typ))

Check warning on line 2147 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 2151 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
59 changes: 59 additions & 0 deletions compiler/noirc_frontend/src/tests/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::hir::def_collector::dc_crate::CompilationError;
use crate::hir::def_collector::errors::DefCollectorErrorKind;
use crate::hir::resolution::errors::ResolverError;
use crate::hir::resolution::import::PathResolutionError;
use crate::hir::type_check::TypeCheckError;
Expand Down Expand Up @@ -1236,3 +1237,61 @@ fn warns_if_trait_is_not_in_scope_for_generic_function_call_and_there_is_only_on
assert_eq!(ident.to_string(), "foo");
assert_eq!(trait_name, "private_mod::Foo");
}

#[test]
fn error_on_duplicate_impl_with_associated_type() {
let src = r#"
trait Foo {
type Bar;
}

impl Foo for i32 {
type Bar = u32;
}

impl Foo for i32 {
type Bar = u8;
}

fn main() {}
"#;

// Expect "Impl for type `i32` overlaps with existing impl"
// and "Previous impl defined here"
let errors = get_program_errors(src);
assert_eq!(errors.len(), 2);

use CompilationError::DefinitionError;
use DefCollectorErrorKind::*;
assert!(matches!(&errors[0].0, DefinitionError(OverlappingImpl { .. })));
assert!(matches!(&errors[1].0, DefinitionError(OverlappingImplNote { .. })));
}

#[test]
fn error_on_duplicate_impl_with_associated_constant() {
let src = r#"
trait Foo {
let Bar: u32;
}

impl Foo for i32 {
let Bar = 5;
}

impl Foo for i32 {
let Bar = 6;
}

fn main() {}
"#;

// Expect "Impl for type `i32` overlaps with existing impl"
// and "Previous impl defined here"
let errors = get_program_errors(src);
assert_eq!(errors.len(), 2);

use CompilationError::DefinitionError;
use DefCollectorErrorKind::*;
assert!(matches!(&errors[0].0, DefinitionError(OverlappingImpl { .. })));
assert!(matches!(&errors[1].0, DefinitionError(OverlappingImplNote { .. })));
}
Loading