Skip to content
Closed
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
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,13 @@
Err(UnificationError)
}
}
(Constant(field, kind), Type::NamedGeneric(types::NamedGeneric { type_var, .. }))
| (Type::NamedGeneric(types::NamedGeneric { type_var, .. }), Constant(field, kind)) => {
let other = Type::Constant(*field, kind.clone());
other.try_unify_to_type_variable(type_var, bindings, |bindings| {
other.try_bind_to(type_var, bindings, kind.clone())
})
}

(Constant(value, kind), other) | (other, Constant(value, kind)) => {
let dummy_location = Location::dummy();
Expand Down Expand Up @@ -2397,7 +2404,7 @@
}

let recur_on_binding = |id, replacement: &Type| {
// Prevent recuring forever if there's a `T := T` binding

Check warning on line 2407 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (recuring)
if replacement.type_variable_id() == Some(id) {
replacement.clone()
} else {
Expand Down Expand Up @@ -2483,7 +2490,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 2493 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down Expand Up @@ -2650,7 +2657,7 @@
}
}

/// Follow bindings if this is a type variable or generic to the first non-typevariable

Check warning on line 2660 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevariable)
/// type. Unlike `follow_bindings`, this won't recursively follow any bindings on any
/// fields or arguments of this type.
pub fn follow_bindings_shallow(&self) -> Cow<Type> {
Expand All @@ -2675,7 +2682,7 @@

/// Replace any `Type::NamedGeneric` in this type with a `Type::TypeVariable`
/// using to the same inner `TypeVariable`. This is used during monomorphization
/// to bind to named generics since they are unbindable during type checking.

Check warning on line 2685 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unbindable)
pub fn replace_named_generics_with_type_variables(&mut self) {
match self {
Type::FieldElement
Expand Down Expand Up @@ -3181,7 +3188,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

Check warning on line 3191 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)

Check warning on line 3191 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
Type::DataType(def, args) => {
def.hash(state);
args.hash(state);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "associated_constant_bound_with_different_types"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
trait TraitWithAssociatedConstant {
let N: u32;

fn foo(_: Self) -> bool {
true
}

fn make_array(self) -> [Field; Self::N];
}

struct Foo {}

impl TraitWithAssociatedConstant for Foo {
let N: u32 = 5;

fn make_array(self) -> [Field; Self::N] {
std::mem::zeroed()
}
}

struct Wrapper<T> {
inner: T,
}

impl<U> std::cmp::Eq for Wrapper<U>
where
U: TraitWithAssociatedConstant,
{

fn eq(self, _other: Self) -> bool {
let _array1: [Field; 5] = self.inner.make_array();
let _array2: [Field; 6] = self.inner.make_array(); // get the same method to return an array of a different size
self.inner.foo()
}
}

fn main() {
let wrapper = Wrapper { inner: Foo {} };
assert_eq(wrapper, wrapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Expected type [Field; 6], found type [Field; 5]
┌─ src/main.nr:32:35
32 │ let _array2: [Field; 6] = self.inner.make_array(); // get the same method to return an array of a different size
│ -----------------------

Aborting due to 1 previous error
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "regression_8252"
type = "bin"
authors = [""]

[dependencies]
32 changes: 32 additions & 0 deletions test_programs/compile_success_empty/regression_8252/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
trait TraitWithAssociatedConstant {
let N: u32;

fn foo(_: Self) -> bool {
true
}
}

struct Foo {}

impl TraitWithAssociatedConstant for Foo {
let N: u32 = 42;
}

struct Wrapper<T> {
inner: T,
}

impl<T> std::cmp::Eq for Wrapper<T>
where
T: TraitWithAssociatedConstant,
{

fn eq(self, _: Self) -> bool {
self.inner.foo()
}
}

fn main() {
let wrapper = Wrapper { inner: Foo {} };
assert_eq(wrapper, wrapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "regression_8252_2"
type = "bin"
authors = [""]

[dependencies]
41 changes: 41 additions & 0 deletions test_programs/compile_success_empty/regression_8252_2/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
trait TraitWithAssociatedConstant {
let N: u32;

fn foo(_: Self) -> bool {
true
}
}

struct Foo {}

impl TraitWithAssociatedConstant for Foo {
let N: u32 = 4;
}

struct Bar {}

impl TraitWithAssociatedConstant for Bar {
let N: u32 = 5;
}

struct Wrapper<T> {
inner: T,
}

impl<T> std::cmp::Eq for Wrapper<T>
where
T: TraitWithAssociatedConstant,
{

fn eq(self, _: Self) -> bool {
self.inner.foo()
}
}

fn main() {
let wrapper = Wrapper { inner: Foo {} };
assert_eq(wrapper, wrapper);

let wrapper = Wrapper { inner: Bar {} };
assert_eq(wrapper, wrapper);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading