From 8654d67b731f4f512d98735ed9e725ed426772cb Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 15 Jan 2025 11:47:10 -0600 Subject: [PATCH 1/4] Fix associated types kind --- compiler/noirc_frontend/src/elaborator/mod.rs | 14 +++++++++----- .../compile_success_empty/serialize/src/main.nr | 16 +++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index 10e56364ed3..9fc32988a39 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -4,7 +4,7 @@ use std::{ }; use crate::{ - ast::ItemVisibility, graph::CrateGraph, hir_def::traits::ResolvedTraitBound, + ast::{ItemVisibility, UnresolvedType}, graph::CrateGraph, hir_def::traits::ResolvedTraitBound, node_interner::GlobalValue, usage_tracker::UsageTracker, StructField, StructType, TypeBindings, }; use crate::{ @@ -766,7 +766,7 @@ impl<'context> Elaborator<'context> { ) -> Vec { where_clause .iter_mut() - .flat_map(|constraint| self.add_missing_named_generics(&mut constraint.trait_bound)) + .flat_map(|constraint| self.add_missing_named_generics(&constraint.typ, &mut constraint.trait_bound)) .collect() } @@ -782,7 +782,7 @@ impl<'context> Elaborator<'context> { /// /// with a vector of `` returned so that the caller can then modify the function to: /// `fn foo() where T: Foo { ... }` - fn add_missing_named_generics(&mut self, bound: &mut TraitBound) -> Vec { + fn add_missing_named_generics(&mut self, object: &UnresolvedType, bound: &mut TraitBound) -> Vec { let mut added_generics = Vec::new(); let Ok(item) = self.resolve_path_or_error(bound.trait_path.clone()) else { @@ -796,6 +796,8 @@ impl<'context> Elaborator<'context> { let the_trait = self.get_trait_mut(trait_id); if the_trait.associated_types.len() > bound.trait_generics.named_args.len() { + let trait_name = the_trait.name.to_string(); + for associated_type in &the_trait.associated_types.clone() { if !bound .trait_generics @@ -806,10 +808,12 @@ impl<'context> Elaborator<'context> { // This generic isn't contained in the bound's named arguments, // so add it by creating a fresh type variable. let new_generic_id = self.interner.next_type_variable_id(); - let type_var = TypeVariable::unbound(new_generic_id, Kind::Normal); + let kind = associated_type.type_var.kind(); + let type_var = TypeVariable::unbound(new_generic_id, kind); let span = bound.trait_path.span; - let name = associated_type.name.clone(); + let name = format!("<{object} as {trait_name}>::{}",associated_type.name.clone()); + let name = Rc::new(name); let typ = Type::NamedGeneric(type_var.clone(), name.clone()); let typ = self.interner.push_quoted_type(typ); let typ = UnresolvedTypeData::Resolved(typ).with_span(span); diff --git a/test_programs/compile_success_empty/serialize/src/main.nr b/test_programs/compile_success_empty/serialize/src/main.nr index 66c79f9fc9d..a11fdf570d0 100644 --- a/test_programs/compile_success_empty/serialize/src/main.nr +++ b/test_programs/compile_success_empty/serialize/src/main.nr @@ -5,13 +5,12 @@ trait Serialize { fn serialize(self) -> [Field; Self::Size]; } -impl Serialize for (A, B) +impl Serialize for (A, B) where - A: Serialize, - B: Serialize, + A: Serialize, + B: Serialize, { - // let Size = ::Size + ::Size; - let Size: u32 = AS + BS; + let Size = ::Size + ::Size; fn serialize(self: Self) -> [Field; Self::Size] { let mut array: [Field; Self::Size] = std::mem::zeroed(); @@ -28,12 +27,11 @@ where } } -impl Serialize for [T; N] +impl Serialize for [T; N] where - T: Serialize, + T: Serialize, { - // let Size = ::Size * N; - let Size: u32 = TS * N; + let Size = ::Size * N; fn serialize(self: Self) -> [Field; Self::Size] { let mut array: [Field; Self::Size] = std::mem::zeroed(); From 29aa506c64c6a9fe6b984da256a998efcc0389e3 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 15 Jan 2025 11:48:55 -0600 Subject: [PATCH 2/4] Format --- compiler/noirc_frontend/src/elaborator/mod.rs | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index 9fc32988a39..98409168a6c 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -3,10 +3,6 @@ use std::{ rc::Rc, }; -use crate::{ - ast::{ItemVisibility, UnresolvedType}, graph::CrateGraph, hir_def::traits::ResolvedTraitBound, - node_interner::GlobalValue, usage_tracker::UsageTracker, StructField, StructType, TypeBindings, -}; use crate::{ ast::{ BlockExpression, FunctionKind, GenericTypeArgs, Ident, NoirFunction, NoirStruct, Param, @@ -41,6 +37,14 @@ use crate::{ token::SecondaryAttribute, Shared, Type, TypeVariable, }; +use crate::{ + ast::{ItemVisibility, UnresolvedType}, + graph::CrateGraph, + hir_def::traits::ResolvedTraitBound, + node_interner::GlobalValue, + usage_tracker::UsageTracker, + StructField, StructType, TypeBindings, +}; mod comptime; mod expressions; @@ -766,7 +770,9 @@ impl<'context> Elaborator<'context> { ) -> Vec { where_clause .iter_mut() - .flat_map(|constraint| self.add_missing_named_generics(&constraint.typ, &mut constraint.trait_bound)) + .flat_map(|constraint| { + self.add_missing_named_generics(&constraint.typ, &mut constraint.trait_bound) + }) .collect() } @@ -782,7 +788,11 @@ impl<'context> Elaborator<'context> { /// /// with a vector of `` returned so that the caller can then modify the function to: /// `fn foo() where T: Foo { ... }` - fn add_missing_named_generics(&mut self, object: &UnresolvedType, bound: &mut TraitBound) -> Vec { + fn add_missing_named_generics( + &mut self, + object: &UnresolvedType, + bound: &mut TraitBound, + ) -> Vec { let mut added_generics = Vec::new(); let Ok(item) = self.resolve_path_or_error(bound.trait_path.clone()) else { @@ -812,7 +822,8 @@ impl<'context> Elaborator<'context> { let type_var = TypeVariable::unbound(new_generic_id, kind); let span = bound.trait_path.span; - let name = format!("<{object} as {trait_name}>::{}",associated_type.name.clone()); + let name = + format!("<{object} as {trait_name}>::{}", associated_type.name.clone()); let name = Rc::new(name); let typ = Type::NamedGeneric(type_var.clone(), name.clone()); let typ = self.interner.push_quoted_type(typ); From ef60244f83cbc560e8e2a998dabdf107011c17fa Mon Sep 17 00:00:00 2001 From: jfecher Date: Wed, 15 Jan 2025 13:27:30 -0600 Subject: [PATCH 3/4] Update compiler/noirc_frontend/src/elaborator/mod.rs Co-authored-by: Ary Borenszweig --- compiler/noirc_frontend/src/elaborator/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index 98409168a6c..bbe195aafab 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -823,7 +823,7 @@ impl<'context> Elaborator<'context> { let span = bound.trait_path.span; let name = - format!("<{object} as {trait_name}>::{}", associated_type.name.clone()); + format!("<{object} as {trait_name}>::{}", associated_type.name); let name = Rc::new(name); let typ = Type::NamedGeneric(type_var.clone(), name.clone()); let typ = self.interner.push_quoted_type(typ); From 00d5150ed6a30d4e2f187f7bd361e4a4eb5c4e19 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 15 Jan 2025 13:37:27 -0600 Subject: [PATCH 4/4] Format --- compiler/noirc_frontend/src/elaborator/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index bbe195aafab..d3dded22ab4 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -822,8 +822,7 @@ impl<'context> Elaborator<'context> { let type_var = TypeVariable::unbound(new_generic_id, kind); let span = bound.trait_path.span; - let name = - format!("<{object} as {trait_name}>::{}", associated_type.name); + let name = format!("<{object} as {trait_name}>::{}", associated_type.name); let name = Rc::new(name); let typ = Type::NamedGeneric(type_var.clone(), name.clone()); let typ = self.interner.push_quoted_type(typ);