diff --git a/sway-core/src/decl_engine/id.rs b/sway-core/src/decl_engine/id.rs index 426f46c3a66..45b241b04a7 100644 --- a/sway-core/src/decl_engine/id.rs +++ b/sway-core/src/decl_engine/id.rs @@ -143,10 +143,10 @@ where } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -155,10 +155,10 @@ impl SubstTypes for DeclId { } } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -167,10 +167,10 @@ impl SubstTypes for DeclId { } } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -179,10 +179,10 @@ impl SubstTypes for DeclId { } } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -191,10 +191,10 @@ impl SubstTypes for DeclId { } } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -203,10 +203,10 @@ impl SubstTypes for DeclId { } } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -215,10 +215,10 @@ impl SubstTypes for DeclId { } } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -228,10 +228,10 @@ impl SubstTypes for DeclId { } impl SubstTypes for DeclId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(*self, decl); HasChanges::Yes } else { @@ -248,11 +248,11 @@ where pub(crate) fn subst_types_and_insert_new( &self, type_mapping: &TypeSubstMap, - engines: &Engines, + ctx: &SubstTypesContext, ) -> Option> { - let decl_engine = engines.de(); + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(self)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { Some(decl_engine.insert(decl, decl_engine.get_parsed_decl_id(self).as_ref())) } else { None diff --git a/sway-core/src/decl_engine/ref.rs b/sway-core/src/decl_engine/ref.rs index 828553ce624..a99ebf98b98 100644 --- a/sway-core/src/decl_engine/ref.rs +++ b/sway-core/src/decl_engine/ref.rs @@ -102,14 +102,14 @@ where pub(crate) fn subst_types_and_insert_new( &self, type_mapping: &TypeSubstMap, - engines: &Engines, + ctx: &SubstTypesContext, ) -> Option { - let decl_engine = engines.de(); - if type_mapping.source_ids_contains_concrete_type(engines) - || !decl_engine.get(&self.id).is_concrete(engines) + let decl_engine = ctx.engines.de(); + if type_mapping.source_ids_contains_concrete_type(ctx.engines) + || !decl_engine.get(&self.id).is_concrete(ctx.engines) { let mut decl = (*decl_engine.get(&self.id)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { Some(decl_engine.insert(decl, decl_engine.get_parsed_decl_id(&self.id).as_ref())) } else { None @@ -144,14 +144,14 @@ where pub(crate) fn subst_types_and_insert_new_with_parent( &self, type_mapping: &TypeSubstMap, - engines: &Engines, + ctx: &SubstTypesContext, ) -> Option { - let decl_engine = engines.de(); - if type_mapping.source_ids_contains_concrete_type(engines) - || !decl_engine.get(&self.id).is_concrete(engines) + let decl_engine = ctx.engines.de(); + if type_mapping.source_ids_contains_concrete_type(ctx.engines) + || !decl_engine.get(&self.id).is_concrete(ctx.engines) { let mut decl = (*decl_engine.get(&self.id)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { Some( decl_engine .insert(decl, decl_engine.get_parsed_decl_id(&self.id).as_ref()) @@ -269,10 +269,10 @@ where DeclEngine: DeclEngineIndex, T: Named + Spanned + SubstTypes + Clone, { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let decl_engine = engines.de(); + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let decl_engine = ctx.engines.de(); let mut decl = (*decl_engine.get(&self.id)).clone(); - if decl.subst(type_mapping, engines).has_changes() { + if decl.subst(type_mapping, ctx).has_changes() { decl_engine.replace(self.id, decl); HasChanges::Yes } else { diff --git a/sway-core/src/language/ty/ast_node.rs b/sway-core/src/language/ty/ast_node.rs index 75420b04c55..fd7c2344091 100644 --- a/sway-core/src/language/ty/ast_node.rs +++ b/sway-core/src/language/ty/ast_node.rs @@ -61,10 +61,10 @@ impl DebugWithEngines for TyAstNode { } impl SubstTypes for TyAstNode { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { match self.content { - TyAstNodeContent::Declaration(ref mut decl) => decl.subst(type_mapping, engines), - TyAstNodeContent::Expression(ref mut expr) => expr.subst(type_mapping, engines), + TyAstNodeContent::Declaration(ref mut decl) => decl.subst(type_mapping, ctx), + TyAstNodeContent::Expression(ref mut expr) => expr.subst(type_mapping, ctx), TyAstNodeContent::SideEffect(_) | TyAstNodeContent::Error(_, _) => HasChanges::No, } } diff --git a/sway-core/src/language/ty/code_block.rs b/sway-core/src/language/ty/code_block.rs index e0642c9324a..88d24ba2de6 100644 --- a/sway-core/src/language/ty/code_block.rs +++ b/sway-core/src/language/ty/code_block.rs @@ -38,8 +38,12 @@ impl HashWithEngines for TyCodeBlock { } impl SubstTypes for TyCodeBlock { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.contents.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + if ctx.subst_codeblocks { + self.contents.subst(type_mapping, ctx) + } else { + HasChanges::No + } } } diff --git a/sway-core/src/language/ty/declaration/configurable.rs b/sway-core/src/language/ty/declaration/configurable.rs index ee9f7171dc6..bcd0220daad 100644 --- a/sway-core/src/language/ty/declaration/configurable.rs +++ b/sway-core/src/language/ty/declaration/configurable.rs @@ -89,11 +89,11 @@ impl Spanned for TyConfigurableDecl { } impl SubstTypes for TyConfigurableDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.return_type.subst(type_mapping, engines); - self.type_ascription.subst(type_mapping, engines); - self.value.subst(type_mapping, engines); + self.return_type.subst(type_mapping, ctx); + self.type_ascription.subst(type_mapping, ctx); + self.value.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/declaration/constant.rs b/sway-core/src/language/ty/declaration/constant.rs index 6382ca3b885..7b56fd73f6f 100644 --- a/sway-core/src/language/ty/declaration/constant.rs +++ b/sway-core/src/language/ty/declaration/constant.rs @@ -92,11 +92,11 @@ impl IsConcrete for TyConstantDecl { } impl SubstTypes for TyConstantDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.return_type.subst(type_mapping, engines); - self.type_ascription.subst(type_mapping, engines); - self.value.subst(type_mapping, engines); + self.return_type.subst(type_mapping, ctx); + self.type_ascription.subst(type_mapping, ctx); + self.value.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 61363124c11..8038859c6ed 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -250,33 +250,33 @@ impl HashWithEngines for TyDecl { } impl SubstTypes for TyDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { match self { - TyDecl::VariableDecl(ref mut var_decl) => var_decl.subst(type_mapping, engines), + TyDecl::VariableDecl(ref mut var_decl) => var_decl.subst(type_mapping, ctx), TyDecl::FunctionDecl(FunctionDecl { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), TyDecl::TraitDecl(TraitDecl { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), TyDecl::StructDecl(StructDecl { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), TyDecl::EnumDecl(EnumDecl { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), TyDecl::EnumVariantDecl(EnumVariantDecl { ref mut enum_ref, .. - }) => enum_ref.subst(type_mapping, engines), + }) => enum_ref.subst(type_mapping, ctx), TyDecl::ImplSelfOrTrait(ImplSelfOrTrait { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), TyDecl::TypeAliasDecl(TypeAliasDecl { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), TyDecl::TraitTypeDecl(TraitTypeDecl { ref mut decl_id, .. - }) => decl_id.subst(type_mapping, engines), + }) => decl_id.subst(type_mapping, ctx), // generics in an ABI is unsupported by design TyDecl::AbiDecl(_) | TyDecl::ConstantDecl(_) diff --git a/sway-core/src/language/ty/declaration/enum.rs b/sway-core/src/language/ty/declaration/enum.rs index c7400c30d87..44cf87f553d 100644 --- a/sway-core/src/language/ty/declaration/enum.rs +++ b/sway-core/src/language/ty/declaration/enum.rs @@ -70,10 +70,10 @@ impl HashWithEngines for TyEnumDecl { } impl SubstTypes for TyEnumDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.variants.subst(type_mapping, engines); - self.type_parameters.subst(type_mapping, engines); + self.variants.subst(type_mapping, ctx); + self.type_parameters.subst(type_mapping, ctx); } } } @@ -186,7 +186,7 @@ impl OrdWithEngines for TyEnumVariant { } impl SubstTypes for TyEnumVariant { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.type_argument.subst_inner(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.type_argument.subst_inner(type_mapping, ctx) } } diff --git a/sway-core/src/language/ty/declaration/function.rs b/sway-core/src/language/ty/declaration/function.rs index 3887d4da1e9..a9e6eff1025 100644 --- a/sway-core/src/language/ty/declaration/function.rs +++ b/sway-core/src/language/ty/declaration/function.rs @@ -220,13 +220,13 @@ impl HashWithEngines for TyFunctionDecl { } impl SubstTypes for TyFunctionDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.type_parameters.subst(type_mapping, engines); - self.parameters.subst(type_mapping, engines); - self.return_type.subst(type_mapping, engines); - self.body.subst(type_mapping, engines); - self.implementing_for_typeid.subst(type_mapping, engines); + self.type_parameters.subst(type_mapping, ctx); + self.parameters.subst(type_mapping, ctx); + self.return_type.subst(type_mapping, ctx); + self.body.subst(type_mapping, ctx); + self.implementing_for_typeid.subst(type_mapping, ctx); } } } @@ -518,8 +518,8 @@ impl HashWithEngines for TyFunctionParameter { } impl SubstTypes for TyFunctionParameter { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.type_argument.type_id.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.type_argument.type_id.subst(type_mapping, ctx) } } diff --git a/sway-core/src/language/ty/declaration/impl_trait.rs b/sway-core/src/language/ty/declaration/impl_trait.rs index 60d21941086..42b7c04b311 100644 --- a/sway-core/src/language/ty/declaration/impl_trait.rs +++ b/sway-core/src/language/ty/declaration/impl_trait.rs @@ -88,11 +88,11 @@ impl HashWithEngines for TyImplSelfOrTrait { } impl SubstTypes for TyImplSelfOrTrait { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.impl_type_parameters.subst(type_mapping, engines); - self.implementing_for.subst_inner(type_mapping, engines); - self.items.subst(type_mapping, engines); + self.impl_type_parameters.subst(type_mapping, ctx); + self.implementing_for.subst_inner(type_mapping, ctx); + self.items.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/declaration/struct.rs b/sway-core/src/language/ty/declaration/struct.rs index d885f69c510..32c4409b0d9 100644 --- a/sway-core/src/language/ty/declaration/struct.rs +++ b/sway-core/src/language/ty/declaration/struct.rs @@ -68,10 +68,10 @@ impl HashWithEngines for TyStructDecl { } impl SubstTypes for TyStructDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.fields.subst(type_mapping, engines); - self.type_parameters.subst(type_mapping, engines); + self.fields.subst(type_mapping, ctx); + self.type_parameters.subst(type_mapping, ctx); } } } @@ -279,7 +279,7 @@ impl OrdWithEngines for TyStructField { } impl SubstTypes for TyStructField { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.type_argument.subst_inner(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.type_argument.subst_inner(type_mapping, ctx) } } diff --git a/sway-core/src/language/ty/declaration/trait.rs b/sway-core/src/language/ty/declaration/trait.rs index 340c07520b3..ed6258cd71e 100644 --- a/sway-core/src/language/ty/declaration/trait.rs +++ b/sway-core/src/language/ty/declaration/trait.rs @@ -276,26 +276,26 @@ impl Spanned for TyTraitItem { } impl SubstTypes for TyTraitDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.type_parameters.subst(type_mapping, engines); + self.type_parameters.subst(type_mapping, ctx); self.interface_surface .iter_mut() .fold(HasChanges::No, |has_changes, item| match item { TyTraitInterfaceItem::TraitFn(item_ref) => { if let Some(new_item_ref) = item_ref .clone() - .subst_types_and_insert_new_with_parent(type_mapping, engines) { + .subst_types_and_insert_new_with_parent(type_mapping, ctx) { item_ref.replace_id(*new_item_ref.id()); HasChanges::Yes - } else{ + } else { HasChanges::No } } TyTraitInterfaceItem::Constant(decl_ref) => { if let Some(new_decl_ref) = decl_ref .clone() - .subst_types_and_insert_new(type_mapping, engines) { + .subst_types_and_insert_new(type_mapping, ctx) { decl_ref.replace_id(*new_decl_ref.id()); HasChanges::Yes } else{ @@ -305,7 +305,7 @@ impl SubstTypes for TyTraitDecl { TyTraitInterfaceItem::Type(decl_ref) => { if let Some(new_decl_ref) = decl_ref .clone() - .subst_types_and_insert_new(type_mapping, engines) { + .subst_types_and_insert_new(type_mapping, ctx) { decl_ref.replace_id(*new_decl_ref.id()); HasChanges::Yes } else{ @@ -317,7 +317,7 @@ impl SubstTypes for TyTraitDecl { TyTraitItem::Fn(item_ref) => { if let Some(new_item_ref) = item_ref .clone() - .subst_types_and_insert_new_with_parent(type_mapping, engines) + .subst_types_and_insert_new_with_parent(type_mapping, ctx) { item_ref.replace_id(*new_item_ref.id()); HasChanges::Yes @@ -328,7 +328,7 @@ impl SubstTypes for TyTraitDecl { TyTraitItem::Constant(item_ref) => { if let Some(new_decl_ref) = item_ref .clone() - .subst_types_and_insert_new_with_parent(type_mapping, engines) + .subst_types_and_insert_new_with_parent(type_mapping, ctx) { item_ref.replace_id(*new_decl_ref.id()); HasChanges::Yes @@ -339,7 +339,7 @@ impl SubstTypes for TyTraitDecl { TyTraitItem::Type(item_ref) => { if let Some(new_decl_ref) = item_ref .clone() - .subst_types_and_insert_new_with_parent(type_mapping, engines) + .subst_types_and_insert_new_with_parent(type_mapping, ctx) { item_ref.replace_id(*new_decl_ref.id()); HasChanges::Yes @@ -353,11 +353,11 @@ impl SubstTypes for TyTraitDecl { } impl SubstTypes for TyTraitItem { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { match self { - TyTraitItem::Fn(fn_decl) => fn_decl.subst(type_mapping, engines), - TyTraitItem::Constant(const_decl) => const_decl.subst(type_mapping, engines), - TyTraitItem::Type(type_decl) => type_decl.subst(type_mapping, engines), + TyTraitItem::Fn(fn_decl) => fn_decl.subst(type_mapping, ctx), + TyTraitItem::Constant(const_decl) => const_decl.subst(type_mapping, ctx), + TyTraitItem::Type(type_decl) => type_decl.subst(type_mapping, ctx), } } } diff --git a/sway-core/src/language/ty/declaration/trait_fn.rs b/sway-core/src/language/ty/declaration/trait_fn.rs index df15be99b63..a8ff7ef1bc3 100644 --- a/sway-core/src/language/ty/declaration/trait_fn.rs +++ b/sway-core/src/language/ty/declaration/trait_fn.rs @@ -122,10 +122,10 @@ impl HashWithEngines for TyTraitFn { } impl SubstTypes for TyTraitFn { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.parameters.subst(type_mapping, engines); - self.return_type.subst(type_mapping, engines); + self.parameters.subst(type_mapping, ctx); + self.return_type.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/declaration/trait_type.rs b/sway-core/src/language/ty/declaration/trait_type.rs index 25200c48933..c45db2fba26 100644 --- a/sway-core/src/language/ty/declaration/trait_type.rs +++ b/sway-core/src/language/ty/declaration/trait_type.rs @@ -74,10 +74,10 @@ impl HashWithEngines for TyTraitType { } impl SubstTypes for TyTraitType { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.ty.subst(type_mapping, engines); - self.implementing_type.subst(type_mapping, engines); + self.ty.subst(type_mapping, ctx); + self.implementing_type.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/declaration/type_alias.rs b/sway-core/src/language/ty/declaration/type_alias.rs index ddb74aaca2d..a6df20ca594 100644 --- a/sway-core/src/language/ty/declaration/type_alias.rs +++ b/sway-core/src/language/ty/declaration/type_alias.rs @@ -57,8 +57,8 @@ impl HashWithEngines for TyTypeAliasDecl { } impl SubstTypes for TyTypeAliasDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.ty.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.ty.subst(type_mapping, ctx) } } diff --git a/sway-core/src/language/ty/declaration/variable.rs b/sway-core/src/language/ty/declaration/variable.rs index 02adbfc408f..4b6663f8ba0 100644 --- a/sway-core/src/language/ty/declaration/variable.rs +++ b/sway-core/src/language/ty/declaration/variable.rs @@ -59,10 +59,10 @@ impl HashWithEngines for TyVariableDecl { } impl SubstTypes for TyVariableDecl { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.return_type.subst(type_mapping, engines); - self.type_ascription.subst(type_mapping, engines); - self.body.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.return_type.subst(type_mapping, ctx); + self.type_ascription.subst(type_mapping, ctx); + self.body.subst(type_mapping, ctx) } } diff --git a/sway-core/src/language/ty/expression/asm.rs b/sway-core/src/language/ty/expression/asm.rs index 70227f5bb2d..874948ad626 100644 --- a/sway-core/src/language/ty/expression/asm.rs +++ b/sway-core/src/language/ty/expression/asm.rs @@ -32,7 +32,7 @@ impl HashWithEngines for TyAsmRegisterDeclaration { } impl SubstTypes for TyAsmRegisterDeclaration { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.initializer.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.initializer.subst(type_mapping, ctx) } } diff --git a/sway-core/src/language/ty/expression/expression.rs b/sway-core/src/language/ty/expression/expression.rs index 14a2da461d6..c6d9e0c5e37 100644 --- a/sway-core/src/language/ty/expression/expression.rs +++ b/sway-core/src/language/ty/expression/expression.rs @@ -56,10 +56,10 @@ impl HashWithEngines for TyExpression { } impl SubstTypes for TyExpression { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.return_type.subst(type_mapping, engines); - self.expression.subst(type_mapping, engines); + self.return_type.subst(type_mapping, ctx); + self.expression.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/expression/expression_variant.rs b/sway-core/src/language/ty/expression/expression_variant.rs index 4f5643a5020..8e80a4bc4d0 100644 --- a/sway-core/src/language/ty/expression/expression_variant.rs +++ b/sway-core/src/language/ty/expression/expression_variant.rs @@ -649,7 +649,7 @@ impl HashWithEngines for TyExpressionVariant { } impl SubstTypes for TyExpressionVariant { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { use TyExpressionVariant::*; match self { Literal(..) => HasChanges::No, @@ -659,36 +659,36 @@ impl SubstTypes for TyExpressionVariant { ref mut call_path_typeid, .. } => has_changes! { - arguments.subst(type_mapping, engines); + arguments.subst(type_mapping, ctx); if let Some(new_decl_ref) = fn_ref .clone() - .subst_types_and_insert_new_with_parent(type_mapping, engines) + .subst_types_and_insert_new_with_parent(type_mapping, ctx) { fn_ref.replace_id(*new_decl_ref.id()); HasChanges::Yes } else { HasChanges::No }; - call_path_typeid.subst(type_mapping, engines); + call_path_typeid.subst(type_mapping, ctx); }, LazyOperator { lhs, rhs, .. } => has_changes! { - lhs.subst(type_mapping, engines); - rhs.subst(type_mapping, engines); + lhs.subst(type_mapping, ctx); + rhs.subst(type_mapping, ctx); }, - ConstantExpression { decl, .. } => decl.subst(type_mapping, engines), - ConfigurableExpression { decl, .. } => decl.subst(type_mapping, engines), + ConstantExpression { decl, .. } => decl.subst(type_mapping, ctx), + ConfigurableExpression { decl, .. } => decl.subst(type_mapping, ctx), VariableExpression { .. } => HasChanges::No, - Tuple { fields } => fields.subst(type_mapping, engines), + Tuple { fields } => fields.subst(type_mapping, ctx), Array { ref mut elem_type, contents, } => has_changes! { - elem_type.subst(type_mapping, engines); - contents.subst(type_mapping, engines); + elem_type.subst(type_mapping, ctx); + contents.subst(type_mapping, ctx); }, ArrayIndex { prefix, index } => has_changes! { - prefix.subst(type_mapping, engines); - index.subst(type_mapping, engines); + prefix.subst(type_mapping, ctx); + index.subst(type_mapping, ctx); }, StructExpression { struct_id, @@ -698,30 +698,30 @@ impl SubstTypes for TyExpressionVariant { } => has_changes! { if let Some(new_struct_ref) = struct_id .clone() - .subst_types_and_insert_new(type_mapping, engines) { + .subst_types_and_insert_new(type_mapping, ctx) { struct_id.replace_id(*new_struct_ref.id()); HasChanges::Yes } else { HasChanges::No }; - fields.subst(type_mapping, engines); + fields.subst(type_mapping, ctx); }, - CodeBlock(block) => block.subst(type_mapping, engines), + CodeBlock(block) => block.subst(type_mapping, ctx), FunctionParameter => HasChanges::No, - MatchExp { desugared, .. } => desugared.subst(type_mapping, engines), + MatchExp { desugared, .. } => desugared.subst(type_mapping, ctx), IfExp { condition, then, r#else, } => has_changes! { - condition.subst(type_mapping, engines); - then.subst(type_mapping, engines); - r#else.subst(type_mapping, engines); + condition.subst(type_mapping, ctx); + then.subst(type_mapping, ctx); + r#else.subst(type_mapping, ctx); }, AsmExpression { registers, //: Vec, .. - } => registers.subst(type_mapping, engines), + } => registers.subst(type_mapping, ctx), // like a variable expression but it has multiple parts, // like looking up a field in a struct StructFieldAccess { @@ -730,59 +730,59 @@ impl SubstTypes for TyExpressionVariant { ref mut resolved_type_of_parent, .. } => has_changes! { - resolved_type_of_parent.subst(type_mapping, engines); - field_to_access.subst(type_mapping, engines); - prefix.subst(type_mapping, engines); + resolved_type_of_parent.subst(type_mapping, ctx); + field_to_access.subst(type_mapping, ctx); + prefix.subst(type_mapping, ctx); }, TupleElemAccess { prefix, ref mut resolved_type_of_parent, .. } => has_changes! { - resolved_type_of_parent.subst(type_mapping, engines); - prefix.subst(type_mapping, engines); + resolved_type_of_parent.subst(type_mapping, ctx); + prefix.subst(type_mapping, ctx); }, EnumInstantiation { enum_ref, contents, .. } => has_changes! { if let Some(new_enum_ref) = enum_ref .clone() - .subst_types_and_insert_new(type_mapping, engines) + .subst_types_and_insert_new(type_mapping, ctx) { enum_ref.replace_id(*new_enum_ref.id()); HasChanges::Yes } else { HasChanges::No }; - contents.subst(type_mapping, engines); + contents.subst(type_mapping, ctx); }, - AbiCast { address, .. } => address.subst(type_mapping, engines), + AbiCast { address, .. } => address.subst(type_mapping, ctx), // storage is never generic and cannot be monomorphized StorageAccess { .. } => HasChanges::No, - IntrinsicFunction(kind) => kind.subst(type_mapping, engines), - EnumTag { exp } => exp.subst(type_mapping, engines), + IntrinsicFunction(kind) => kind.subst(type_mapping, ctx), + EnumTag { exp } => exp.subst(type_mapping, ctx), UnsafeDowncast { exp, variant, call_path_decl: _, } => has_changes! { - exp.subst(type_mapping, engines); - variant.subst(type_mapping, engines); + exp.subst(type_mapping, ctx); + variant.subst(type_mapping, ctx); }, AbiName(_) => HasChanges::No, WhileLoop { ref mut condition, ref mut body, } => { - condition.subst(type_mapping, engines); - body.subst(type_mapping, engines) + condition.subst(type_mapping, ctx); + body.subst(type_mapping, ctx) } - ForLoop { ref mut desugared } => desugared.subst(type_mapping, engines), + ForLoop { ref mut desugared } => desugared.subst(type_mapping, ctx), Break => HasChanges::No, Continue => HasChanges::No, - Reassignment(reassignment) => reassignment.subst(type_mapping, engines), - ImplicitReturn(expr) | Return(expr) => expr.subst(type_mapping, engines), - Ref(exp) | Deref(exp) => exp.subst(type_mapping, engines), + Reassignment(reassignment) => reassignment.subst(type_mapping, ctx), + ImplicitReturn(expr) | Return(expr) => expr.subst(type_mapping, ctx), + Ref(exp) | Deref(exp) => exp.subst(type_mapping, ctx), } } } diff --git a/sway-core/src/language/ty/expression/intrinsic_function.rs b/sway-core/src/language/ty/expression/intrinsic_function.rs index 681fa6da873..081eeda7682 100644 --- a/sway-core/src/language/ty/expression/intrinsic_function.rs +++ b/sway-core/src/language/ty/expression/intrinsic_function.rs @@ -73,10 +73,10 @@ impl HashWithEngines for TyIntrinsicFunctionKind { } impl SubstTypes for TyIntrinsicFunctionKind { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.arguments.subst(type_mapping, engines); - self.type_arguments.subst(type_mapping, engines); + self.arguments.subst(type_mapping, ctx); + self.type_arguments.subst(type_mapping, ctx); } } } diff --git a/sway-core/src/language/ty/expression/reassignment.rs b/sway-core/src/language/ty/expression/reassignment.rs index d643ef63227..5d50e93eed2 100644 --- a/sway-core/src/language/ty/expression/reassignment.rs +++ b/sway-core/src/language/ty/expression/reassignment.rs @@ -114,14 +114,14 @@ impl HashWithEngines for TyReassignment { } impl SubstTypes for TyReassignmentTarget { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { match self { - TyReassignmentTarget::Deref(exp) => exp.subst(type_mapping, engines), + TyReassignmentTarget::Deref(exp) => exp.subst(type_mapping, ctx), TyReassignmentTarget::ElementAccess { base_type, indices, .. } => { has_changes! { - base_type.subst(type_mapping, engines); - indices.subst(type_mapping, engines); + base_type.subst(type_mapping, ctx); + indices.subst(type_mapping, ctx); } } }; @@ -130,10 +130,10 @@ impl SubstTypes for TyReassignmentTarget { } impl SubstTypes for TyReassignment { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.lhs.subst(type_mapping, engines); - self.rhs.subst(type_mapping, engines); + self.lhs.subst(type_mapping, ctx); + self.rhs.subst(type_mapping, ctx); } } } @@ -330,10 +330,10 @@ impl HashWithEngines for ProjectionKind { } impl SubstTypes for ProjectionKind { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { use ProjectionKind::*; match self { - ArrayIndex { index, .. } => index.subst(type_mapping, engines), + ArrayIndex { index, .. } => index.subst(type_mapping, ctx), _ => HasChanges::No, } } diff --git a/sway-core/src/language/ty/expression/struct_exp_field.rs b/sway-core/src/language/ty/expression/struct_exp_field.rs index 7e6f49ac415..103cdbaa9c7 100644 --- a/sway-core/src/language/ty/expression/struct_exp_field.rs +++ b/sway-core/src/language/ty/expression/struct_exp_field.rs @@ -33,8 +33,8 @@ impl HashWithEngines for TyStructExpressionField { } impl SubstTypes for TyStructExpressionField { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.value.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.value.subst(type_mapping, ctx) } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs b/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs index 7e318a17eb2..a1c2f485af5 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/configurable.rs @@ -15,7 +15,7 @@ use crate::{ CallPath, }, semantic_analysis::{type_check_context::EnforceTypeArguments, *}, - SubstTypes, TypeArgument, TypeBinding, TypeCheckTypeBinding, TypeInfo, + SubstTypes, SubstTypesContext, TypeArgument, TypeBinding, TypeCheckTypeBinding, TypeInfo, }; impl ty::TyConfigurableDecl { @@ -48,7 +48,10 @@ impl ty::TyConfigurableDecl { .unwrap_or_else(|err| type_engine.insert(engines, TypeInfo::ErrorRecovery(err), None)); // this subst is required to replace associated types, namely TypeInfo::TraitType. - type_ascription.type_id.subst(&ctx.type_subst(), engines); + type_ascription.type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); if !is_screaming_snake_case(name.as_str()) { handler.emit_warn(CompileWarning { diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/constant.rs b/sway-core/src/semantic_analysis/ast_node/declaration/constant.rs index da95a2a2dad..d911b9b99e3 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/constant.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/constant.rs @@ -11,7 +11,7 @@ use crate::{ CallPath, }, semantic_analysis::{type_check_context::EnforceTypeArguments, *}, - Engines, SubstTypes, TypeInfo, + Engines, SubstTypes, SubstTypesContext, TypeInfo, }; impl ty::TyConstantDecl { @@ -43,7 +43,10 @@ impl ty::TyConstantDecl { .unwrap_or_else(|err| type_engine.insert(engines, TypeInfo::ErrorRecovery(err), None)); // this subst is required to replace associated types, namely TypeInfo::TraitType. - type_ascription.type_id.subst(&ctx.type_subst(), engines); + type_ascription.type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); if !is_screaming_snake_case(name.as_str()) { handler.emit_warn(CompileWarning { diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs index 35e03b4f75c..51624044d59 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs @@ -646,6 +646,7 @@ fn type_check_trait_implementation( let type_engine = ctx.engines.te(); let decl_engine = ctx.engines.de(); let engines = ctx.engines(); + let collecting_unifications = ctx.collecting_unifications(); // Check to see if the type that we are implementing for implements the // supertraits of this trait. @@ -664,6 +665,7 @@ fn type_check_trait_implementation( block_span, engines, TryInsertingTraitImplOnFailure::Yes, + collecting_unifications, ) })?; @@ -803,7 +805,10 @@ fn type_check_trait_implementation( ty::TyTraitType::error(ctx.engines(), type_decl.as_ref().clone()) }); - type_decl.subst(&trait_type_mapping, engines); + type_decl.subst( + &trait_type_mapping, + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); // Remove this type from the checklist. let name = type_decl.name.clone(); @@ -868,7 +873,10 @@ fn type_check_trait_implementation( ) .unwrap_or_else(|_| ty::TyFunctionDecl::error(&impl_method)); - impl_method.subst(&trait_type_mapping, engines); + impl_method.subst( + &trait_type_mapping, + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); // Remove this method from the checklist. let name = impl_method.name.clone(); @@ -891,7 +899,10 @@ fn type_check_trait_implementation( ) .unwrap_or_else(|_| ty::TyConstantDecl::error(ctx.engines(), const_decl.clone())); - const_decl.subst(&trait_type_mapping, engines); + const_decl.subst( + &trait_type_mapping, + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); // Remove this constant from the checklist. let name = const_decl.call_path.suffix.clone(); @@ -961,7 +972,10 @@ fn type_check_trait_implementation( method.implementing_for_typeid = Some(implementing_for); method.replace_decls(&decl_mapping, handler, &mut ctx)?; - method.subst(&type_mapping, engines); + method.subst( + &type_mapping, + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); all_items_refs.push(TyImplItem::Fn( decl_engine .insert( @@ -974,7 +988,10 @@ fn type_check_trait_implementation( TyImplItem::Constant(decl_ref) => { let mut const_decl = (*decl_engine.get_constant(decl_ref)).clone(); const_decl.replace_decls(&decl_mapping, handler, &mut ctx)?; - const_decl.subst(&type_mapping, engines); + const_decl.subst( + &type_mapping, + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); all_items_refs.push(TyImplItem::Constant(decl_engine.insert( const_decl, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), @@ -982,7 +999,10 @@ fn type_check_trait_implementation( } TyImplItem::Type(decl_ref) => { let mut type_decl = (*decl_engine.get_type(decl_ref)).clone(); - type_decl.subst(&type_mapping, engines); + type_decl.subst( + &type_mapping, + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); all_items_refs.push(TyImplItem::Type(decl_engine.insert( type_decl.clone(), decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), @@ -1125,11 +1145,17 @@ fn type_check_impl_method( // this subst is required to replace associated types, namely TypeInfo::TraitType. let mut impl_method_param_type_id = impl_method_param.type_argument.type_id; - impl_method_param_type_id.subst(&ctx.type_subst(), engines); + impl_method_param_type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); let mut impl_method_signature_param_type_id = impl_method_signature_param.type_argument.type_id; - impl_method_signature_param_type_id.subst(&ctx.type_subst(), engines); + impl_method_signature_param_type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); if !UnifyCheck::non_dynamic_equality(engines).check( impl_method_param_type_id, @@ -1199,11 +1225,17 @@ fn type_check_impl_method( // this subst is required to replace associated types, namely TypeInfo::TraitType. let mut impl_method_return_type_id = impl_method.return_type.type_id; - impl_method_return_type_id.subst(&ctx.type_subst(), engines); + impl_method_return_type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); let mut impl_method_signature_return_type_type_id = impl_method_signature.return_type.type_id; - impl_method_signature_return_type_type_id.subst(&ctx.type_subst(), engines); + impl_method_signature_return_type_type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); if !UnifyCheck::non_dynamic_equality(engines).check( impl_method_return_type_id, @@ -1309,10 +1341,16 @@ fn type_check_const_decl( // this subst is required to replace associated types, namely TypeInfo::TraitType. let mut const_decl_type_id = const_decl.type_ascription.type_id; - const_decl_type_id.subst(&ctx.type_subst(), engines); + const_decl_type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); let mut const_decl_signature_type_id = const_decl_signature.type_ascription.type_id; - const_decl_signature_type_id.subst(&ctx.type_subst(), engines); + const_decl_signature_type_id.subst( + &ctx.type_subst(), + &SubstTypesContext::new(engines, !ctx.collecting_unifications()), + ); // unify the types from the constant with the constant signature if !UnifyCheck::non_dynamic_equality(engines) diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs index efa7687355d..1adeca5fbb9 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs @@ -355,7 +355,16 @@ impl TyTraitDecl { ty::TyTraitItem::Fn(decl_ref) => { let mut method = (*decl_engine.get_function(&decl_ref)).clone(); let name = method.name.clone(); - let r = if method.subst(&type_mapping, engines).has_changes() { + let r = if method + .subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ) + .has_changes() + { let new_ref = decl_engine .insert( method, @@ -371,7 +380,16 @@ impl TyTraitDecl { ty::TyTraitItem::Constant(decl_ref) => { let mut const_decl = (*decl_engine.get_constant(&decl_ref)).clone(); let name = const_decl.call_path.suffix.clone(); - let r = if const_decl.subst(&type_mapping, engines).has_changes() { + let r = if const_decl + .subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ) + .has_changes() + { decl_engine.insert( const_decl, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), @@ -384,7 +402,16 @@ impl TyTraitDecl { ty::TyTraitItem::Type(decl_ref) => { let mut t = (*decl_engine.get_type(&decl_ref)).clone(); let name = t.name.clone(); - let r = if t.subst(&type_mapping, engines).has_changes() { + let r = if t + .subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ) + .has_changes() + { decl_engine .insert(t, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref()) } else { @@ -436,7 +463,13 @@ impl TyTraitDecl { match item { ty::TyTraitInterfaceItem::TraitFn(decl_ref) => { let mut method = (*decl_engine.get_trait_fn(decl_ref)).clone(); - method.subst(&type_mapping, engines); + method.subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ); all_items.push(TyImplItem::Fn( decl_engine .insert(method.to_dummy_func(AbiMode::NonAbi, Some(type_id)), None) @@ -464,7 +497,13 @@ impl TyTraitDecl { match item { ty::TyTraitItem::Fn(decl_ref) => { let mut method = (*decl_engine.get_function(decl_ref)).clone(); - method.subst(&type_mapping, engines); + method.subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ); all_items.push(TyImplItem::Fn( ctx.engines .de() @@ -477,7 +516,13 @@ impl TyTraitDecl { } ty::TyTraitItem::Constant(decl_ref) => { let mut const_decl = (*decl_engine.get_constant(decl_ref)).clone(); - const_decl.subst(&type_mapping, engines); + const_decl.subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ); all_items.push(TyImplItem::Constant(decl_engine.insert( const_decl, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), @@ -485,7 +530,13 @@ impl TyTraitDecl { } ty::TyTraitItem::Type(decl_ref) => { let mut type_decl = (*decl_engine.get_type(decl_ref)).clone(); - type_decl.subst(&type_mapping, engines); + type_decl.subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ); all_items.push(TyImplItem::Type(decl_engine.insert( type_decl, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs index ec4b5b9529e..d70f856b4b9 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs @@ -670,7 +670,13 @@ pub(crate) fn type_check_method_application( vec![t.initial_type_id], vec![call_path_typeid], ); - method.subst(&type_subst, engines); + method.subst( + &type_subst, + &SubstTypesContext::new( + engines, + !ctx.collecting_unifications(), + ), + ); } } } diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 19c50487499..f6c91b89983 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -375,7 +375,7 @@ impl Root { src_mod .current_items() .implemented_traits - .filter_by_type_item_import(type_id, engines), + .filter_by_type_item_import(type_id, engines, false), engines, ); } diff --git a/sway-core/src/semantic_analysis/namespace/trait_map.rs b/sway-core/src/semantic_analysis/namespace/trait_map.rs index 9cc2880a73b..4bcd87b7786 100644 --- a/sway-core/src/semantic_analysis/namespace/trait_map.rs +++ b/sway-core/src/semantic_analysis/namespace/trait_map.rs @@ -22,7 +22,8 @@ use crate::{ CallPath, }, type_system::{SubstTypes, TypeId}, - IncludeSelf, TraitConstraint, TypeArgument, TypeEngine, TypeInfo, TypeSubstMap, UnifyCheck, + IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo, + TypeSubstMap, UnifyCheck, }; use super::TryInsertingTraitImplOnFailure; @@ -507,8 +508,16 @@ impl TraitMap { /// re-insert them under `type_id`. Moreover, the impl block for /// `Data` needs to be able to call methods that are defined in the /// impl block of `Data` - pub(crate) fn insert_for_type(&mut self, engines: &Engines, type_id: TypeId) { - self.extend(self.filter_by_type(type_id, engines), engines); + pub(crate) fn insert_for_type( + &mut self, + engines: &Engines, + type_id: TypeId, + collecting_unifications: bool, + ) { + self.extend( + self.filter_by_type(type_id, engines, collecting_unifications), + engines, + ); } /// Given [TraitMap]s `self` and `other`, extend `self` with `other`, @@ -653,7 +662,12 @@ impl TraitMap { /// have `Data: get_first(self) -> T` and /// `Data: get_second(self) -> T`, and we can create a new [TraitMap] /// with those entries for `Data`. - pub(crate) fn filter_by_type(&self, type_id: TypeId, engines: &Engines) -> TraitMap { + pub(crate) fn filter_by_type( + &self, + type_id: TypeId, + engines: &Engines, + collecting_unifications: bool, + ) -> TraitMap { let unify_checker = UnifyCheck::constraint_subset(engines); // a curried version of the decider protocol to use in the helper functions @@ -661,7 +675,7 @@ impl TraitMap { let mut all_types = type_id.extract_inner_types(engines, IncludeSelf::No); all_types.insert(type_id); let all_types = all_types.into_iter().collect::>(); - self.filter_by_type_inner(engines, all_types, decider) + self.filter_by_type_inner(engines, all_types, decider, collecting_unifications) } /// Filters the entries in `self` with the given [TypeId] `type_id` and @@ -726,6 +740,7 @@ impl TraitMap { &self, type_id: TypeId, engines: &Engines, + collecting_unifications: bool, ) -> TraitMap { let unify_checker = UnifyCheck::constraint_subset(engines); let unify_checker_for_item_import = UnifyCheck::non_generic_constraint_subset(engines); @@ -734,7 +749,8 @@ impl TraitMap { let decider = |left: TypeId, right: TypeId| { unify_checker.check(left, right) || unify_checker_for_item_import.check(right, left) }; - let mut trait_map = self.filter_by_type_inner(engines, vec![type_id], decider); + let mut trait_map = + self.filter_by_type_inner(engines, vec![type_id], decider, collecting_unifications); let all_types = type_id .extract_inner_types(engines, IncludeSelf::No) .into_iter() @@ -743,7 +759,7 @@ impl TraitMap { let decider2 = |left: TypeId, right: TypeId| unify_checker.check(left, right); trait_map.extend( - self.filter_by_type_inner(engines, all_types, decider2), + self.filter_by_type_inner(engines, all_types, decider2, collecting_unifications), engines, ); trait_map @@ -754,6 +770,7 @@ impl TraitMap { engines: &Engines, mut all_types: Vec, decider: impl Fn(TypeId, TypeId) -> bool, + collecting_unifications: bool, ) -> TraitMap { let type_engine = engines.te(); let decl_engine = engines.de(); @@ -802,7 +819,10 @@ impl TraitMap { *map_type_id, *type_id, ); - type_id.subst(&type_mapping, engines); + type_id.subst( + &type_mapping, + &SubstTypesContext::new(engines, !collecting_unifications), + ); let trait_items: TraitItems = map_trait_items .clone() .into_iter() @@ -814,7 +834,13 @@ impl TraitMap { if decl.is_trait_method_dummy && !insertable { None } else { - decl.subst(&type_mapping, engines); + decl.subst( + &type_mapping, + &SubstTypesContext::new( + engines, + !collecting_unifications, + ), + ); let new_ref = decl_engine .insert( decl, @@ -831,7 +857,10 @@ impl TraitMap { } ty::TyTraitItem::Constant(decl_ref) => { let mut decl = (*decl_engine.get(decl_ref.id())).clone(); - decl.subst(&type_mapping, engines); + decl.subst( + &type_mapping, + &SubstTypesContext::new(engines, !collecting_unifications), + ); let new_ref = decl_engine.insert( decl, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), @@ -843,7 +872,10 @@ impl TraitMap { } ty::TyTraitItem::Type(decl_ref) => { let mut decl = (*decl_engine.get(decl_ref.id())).clone(); - decl.subst(&type_mapping, engines); + decl.subst( + &type_mapping, + &SubstTypesContext::new(engines, !collecting_unifications), + ); let new_ref = decl_engine.insert( decl, decl_engine.get_parsed_decl_id(decl_ref.id()).as_ref(), @@ -1186,6 +1218,7 @@ impl TraitMap { } /// Checks to see if the trait constraints are satisfied for a given type. + #[allow(clippy::too_many_arguments)] pub(crate) fn check_if_trait_constraints_are_satisfied_for_type( &mut self, handler: &Handler, @@ -1194,6 +1227,7 @@ impl TraitMap { access_span: &Span, engines: &Engines, try_inserting_trait_impl_on_failure: TryInsertingTraitImplOnFailure, + collecting_unifications: bool, ) -> Result<(), ErrorEmitted> { let type_engine = engines.te(); @@ -1224,6 +1258,7 @@ impl TraitMap { access_span, engines, try_inserting_trait_impl_on_failure, + collecting_unifications, ) { Ok(()) => { self.satisfied_cache.insert(hash); @@ -1233,6 +1268,7 @@ impl TraitMap { } } + #[allow(clippy::too_many_arguments)] fn check_if_trait_constraints_are_satisfied_for_type_inner( &mut self, handler: &Handler, @@ -1241,6 +1277,7 @@ impl TraitMap { access_span: &Span, engines: &Engines, try_inserting_trait_impl_on_failure: TryInsertingTraitImplOnFailure, + collecting_unifications: bool, ) -> Result<(), ErrorEmitted> { let type_engine = engines.te(); @@ -1344,7 +1381,7 @@ impl TraitMap { try_inserting_trait_impl_on_failure, TryInsertingTraitImplOnFailure::Yes ) { - self.insert_for_type(engines, type_id); + self.insert_for_type(engines, type_id, collecting_unifications); return self.check_if_trait_constraints_are_satisfied_for_type( handler, type_id, @@ -1352,6 +1389,7 @@ impl TraitMap { access_span, engines, TryInsertingTraitImplOnFailure::No, + collecting_unifications, ); } else { let mut type_arguments_string = "".to_string(); diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index 336554e479d..177ceed26ed 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -18,7 +18,7 @@ use crate::{ Namespace, }, type_system::{SubstTypes, TypeArgument, TypeId, TypeInfo}, - CreateTypeId, TraitConstraint, TypeParameter, TypeSubstMap, UnifyCheck, + CreateTypeId, SubstTypesContext, TraitConstraint, TypeParameter, TypeSubstMap, UnifyCheck, }; use sway_error::{ error::CompileError, @@ -700,7 +700,10 @@ impl<'a> TypeCheckContext<'a> { }; let mut type_id = type_id; - type_id.subst(&self.type_subst(), self.engines()); + type_id.subst( + &self.type_subst(), + &SubstTypesContext::new(engines, !self.collecting_unifications()), + ); Ok(type_id) } @@ -1436,7 +1439,7 @@ impl<'a> TypeCheckContext<'a> { src_mod .current_items() .implemented_traits - .filter_by_type_item_import(type_id, engines), + .filter_by_type_item_import(type_id, engines, self.collecting_unifications()), engines, ); @@ -1608,7 +1611,10 @@ impl<'a> TypeCheckContext<'a> { call_site_span, mod_path, )?; - value.subst(&type_mapping, self.engines); + value.subst( + &type_mapping, + &SubstTypesContext::new(self.engines, !self.collecting_unifications()), + ); Ok(()) } @@ -1732,11 +1738,12 @@ impl<'a> TypeCheckContext<'a> { pub(crate) fn insert_trait_implementation_for_type(&mut self, type_id: TypeId) { let engines = self.engines; + let collecting_unifications = self.collecting_unifications(); self.namespace_mut() .module_mut(engines) .current_items_mut() .implemented_traits - .insert_for_type(engines, type_id); + .insert_for_type(engines, type_id, collecting_unifications); } pub fn check_type_impls_traits( @@ -1746,7 +1753,7 @@ impl<'a> TypeCheckContext<'a> { ) -> bool { let handler = Handler::default(); let engines = self.engines; - + let collecting_unifications = self.collecting_unifications(); self.namespace_mut() .module_mut(engines) .current_items_mut() @@ -1758,6 +1765,7 @@ impl<'a> TypeCheckContext<'a> { &Span::dummy(), engines, crate::namespace::TryInsertingTraitImplOnFailure::Yes, + collecting_unifications, ) .is_ok() } diff --git a/sway-core/src/type_system/ast_elements/trait_constraint.rs b/sway-core/src/type_system/ast_elements/trait_constraint.rs index 67e56d481c6..e9fbff449fa 100644 --- a/sway-core/src/type_system/ast_elements/trait_constraint.rs +++ b/sway-core/src/type_system/ast_elements/trait_constraint.rs @@ -98,8 +98,8 @@ impl Spanned for TraitConstraint { } impl SubstTypes for TraitConstraint { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.type_arguments.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.type_arguments.subst(type_mapping, ctx) } } diff --git a/sway-core/src/type_system/ast_elements/type_argument.rs b/sway-core/src/type_system/ast_elements/type_argument.rs index 0d7f6af3ed6..03ab8ab1ec6 100644 --- a/sway-core/src/type_system/ast_elements/type_argument.rs +++ b/sway-core/src/type_system/ast_elements/type_argument.rs @@ -109,7 +109,7 @@ impl From<&TypeParameter> for TypeArgument { } impl SubstTypes for TypeArgument { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.type_id.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.type_id.subst(type_mapping, ctx) } } diff --git a/sway-core/src/type_system/ast_elements/type_parameter.rs b/sway-core/src/type_system/ast_elements/type_parameter.rs index 14448596993..0acfbc8a18e 100644 --- a/sway-core/src/type_system/ast_elements/type_parameter.rs +++ b/sway-core/src/type_system/ast_elements/type_parameter.rs @@ -96,10 +96,10 @@ impl OrdWithEngines for TypeParameter { } impl SubstTypes for TypeParameter { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { has_changes! { - self.type_id.subst(type_mapping, engines); - self.trait_constraints.subst(type_mapping, engines); + self.type_id.subst(type_mapping, ctx); + self.trait_constraints.subst(type_mapping, ctx); } } } @@ -507,7 +507,8 @@ impl TypeParameter { .. } = type_param; - if !ctx.collecting_unifications() { + let collecting_unifications = ctx.collecting_unifications(); + if !collecting_unifications { // Check to see if the trait constraints are satisfied. match ctx .namespace_mut() @@ -521,6 +522,7 @@ impl TypeParameter { access_span, engines, TryInsertingTraitImplOnFailure::Yes, + collecting_unifications, ) { Ok(res) => res, Err(_) => continue, diff --git a/sway-core/src/type_system/id.rs b/sway-core/src/type_system/id.rs index 4e523804df4..f252e9bd830 100644 --- a/sway-core/src/type_system/id.rs +++ b/sway-core/src/type_system/id.rs @@ -89,9 +89,9 @@ impl CollectTypesMetadata for TypeId { } impl SubstTypes for TypeId { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - let type_engine = engines.te(); - if let Some(matching_id) = type_mapping.find_match(*self, engines) { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + let type_engine = ctx.engines.te(); + if let Some(matching_id) = type_mapping.find_match(*self, ctx.engines) { if !matches!(&*type_engine.get(matching_id), TypeInfo::ErrorRecovery(_)) { *self = matching_id; HasChanges::Yes diff --git a/sway-core/src/type_system/priv_prelude.rs b/sway-core/src/type_system/priv_prelude.rs index f2c7449865f..a1404ae9785 100644 --- a/sway-core/src/type_system/priv_prelude.rs +++ b/sway-core/src/type_system/priv_prelude.rs @@ -6,7 +6,11 @@ pub(crate) use super::{ create_type_id::CreateTypeId, }, info::VecSet, - substitute::{subst_map::TypeSubstMap, subst_types::HasChanges, subst_types::SubstTypes}, + substitute::{ + subst_map::TypeSubstMap, + subst_types::HasChanges, + subst_types::{SubstTypes, SubstTypesContext}, + }, unify::unify_check::UnifyCheck, }; diff --git a/sway-core/src/type_system/substitute/subst_list.rs b/sway-core/src/type_system/substitute/subst_list.rs index ac63696608c..4c5dad10b5f 100644 --- a/sway-core/src/type_system/substitute/subst_list.rs +++ b/sway-core/src/type_system/substitute/subst_list.rs @@ -86,7 +86,7 @@ impl OrdWithEngines for SubstList { } impl SubstTypes for SubstList { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.list.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.list.subst(type_mapping, ctx) } } diff --git a/sway-core/src/type_system/substitute/subst_types.rs b/sway-core/src/type_system/substitute/subst_types.rs index 9d61fe3a63c..95141955b62 100644 --- a/sway-core/src/type_system/substitute/subst_types.rs +++ b/sway-core/src/type_system/substitute/subst_types.rs @@ -24,42 +24,56 @@ impl std::ops::BitOr for HasChanges { } } +pub struct SubstTypesContext<'a> { + pub engines: &'a Engines, + pub subst_codeblocks: bool, +} + +impl<'a> SubstTypesContext<'a> { + pub fn new(engines: &Engines, subst_codeblocks: bool) -> SubstTypesContext { + SubstTypesContext { + engines, + subst_codeblocks, + } + } +} + pub trait SubstTypes { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges; + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges; - fn subst(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { if type_mapping.is_empty() { HasChanges::No } else { - self.subst_inner(type_mapping, engines) + self.subst_inner(type_mapping, ctx) } } } impl SubstTypes for (A, B) { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.1.subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.1.subst(type_mapping, ctx) } } impl SubstTypes for Box { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { - self.as_mut().subst(type_mapping, engines) + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { + self.as_mut().subst(type_mapping, ctx) } } impl SubstTypes for Option { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { self.as_mut() - .map(|x| x.subst(type_mapping, engines)) + .map(|x| x.subst(type_mapping, ctx)) .unwrap_or_default() } } impl SubstTypes for Vec { - fn subst_inner(&mut self, type_mapping: &TypeSubstMap, engines: &Engines) -> HasChanges { + fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges { self.iter_mut().fold(HasChanges::No, |has_change, x| { - x.subst(type_mapping, engines) | has_change + x.subst(type_mapping, ctx) | has_change }) } }