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: Variables from trait constraints being permanently bound over when used within a trait impl #4450

Merged
merged 5 commits into from
Mar 1, 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
5 changes: 4 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ impl<'interner> TypeChecker<'interner> {
assert_eq!(the_trait.generics.len(), constraint.trait_generics.len());

for (param, arg) in the_trait.generics.iter().zip(&constraint.trait_generics) {
bindings.insert(param.id(), (param.clone(), arg.clone()));
// Avoid binding t = t
if !arg.occurs(param.id()) {
bindings.insert(param.id(), (param.clone(), arg.clone()));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,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 1529 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 @@ -1558,7 +1558,7 @@
}

/// True if the given TypeVariableId is free anywhere within self
fn occurs(&self, target_id: TypeVariableId) -> bool {
pub fn occurs(&self, target_id: TypeVariableId) -> bool {
match self {
Type::Array(len, elem) => len.occurs(target_id) || elem.occurs(target_id),
Type::String(len) => len.occurs(target_id),
Expand Down
5 changes: 5 additions & 0 deletions test_programs/execution_success/regression_4436/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "regression_4436"
type = "bin"
authors = [""]
compiler_version = ">=0.22.0"
31 changes: 31 additions & 0 deletions test_programs/execution_success/regression_4436/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
trait LibTrait<N> {
fn broadcast();
fn get_constant() -> Field;
}

global STRUCT_A_LEN: Field = 3;
global STRUCT_B_LEN: Field = 5;

struct StructA;
struct StructB;

impl LibTrait<u32> for StructA {
fn broadcast() {
Self::get_constant();
}

fn get_constant() -> Field {
1
}
}
impl LibTrait<u64> for StructB {
fn broadcast() {
Self::get_constant();
}

fn get_constant() -> Field {
1
}
}

fn main() {}
Loading