From d92274886b08b20405a087bcf9dba7e8a1c7e0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Matos?= Date: Mon, 1 Jul 2024 14:03:50 +0100 Subject: [PATCH] Remove deferred monomorphization. (#6196) ## Description This removes the deferred monomorphization feature as its not needed anymore. Have also tried removing the type check finalization feature, but that is still needed for out-of-order declaration processing, we will remove that soon once a more accurate AST resolving pass is added. Fixes the following bugs: https://github.com/FuelLabs/sway/issues/5279 https://github.com/FuelLabs/sway/issues/5854 https://github.com/FuelLabs/sway/issues/6099 https://github.com/FuelLabs/sway/issues/6171 ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --- sway-core/src/ir_generation/function.rs | 4 ---- .../ty/expression/expression_variant.rs | 13 +--------- .../ast_node/declaration/impl_trait.rs | 7 +----- .../ast_node/expression/typed_expression.rs | 1 - .../typed_expression/function_application.rs | 1 - .../typed_expression/method_application.rs | 6 +---- .../semantic_analysis/type_check_context.rs | 24 ------------------- 7 files changed, 3 insertions(+), 53 deletions(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index bbd2f6e5d7..c459a5cd6f 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -465,12 +465,8 @@ impl<'eng> FnCompiler<'eng> { selector, type_binding: _, call_path_typeid: _, - deferred_monomorphization, .. } => { - if *deferred_monomorphization { - return Err(CompileError::Internal("Trying to compile a deferred function application with deferred monomorphization", name.span())); - } if let Some(metadata) = selector { self.compile_contract_call_encoding_v0( context, diff --git a/sway-core/src/language/ty/expression/expression_variant.rs b/sway-core/src/language/ty/expression/expression_variant.rs index cfbac00177..4a804c4356 100644 --- a/sway-core/src/language/ty/expression/expression_variant.rs +++ b/sway-core/src/language/ty/expression/expression_variant.rs @@ -33,8 +33,6 @@ pub enum TyExpressionVariant { type_binding: Option>, /// In case it is a method should contain a TypeId to either an enum, struct or a type alias. call_path_typeid: Option, - /// This tracks whether monomorphization has been deferred between compiler stages. - deferred_monomorphization: bool, contract_call_params: IndexMap, contract_caller: Option>, }, @@ -447,7 +445,6 @@ impl HashWithEngines for TyExpressionVariant { selector: _, type_binding: _, call_path_typeid: _, - deferred_monomorphization: _, .. } => { call_path.hash(state); @@ -1123,15 +1120,7 @@ impl TypeCheckFinalization for TyExpressionVariant { handler.scope(|handler| { match self { TyExpressionVariant::Literal(_) => {} - TyExpressionVariant::FunctionApplication { - arguments, - deferred_monomorphization, - .. - } => { - // If the function application was deferred we need to monomorphize it here. - // But at the moment monomorphization is fully resolved before type check finalization. - assert!(!(*deferred_monomorphization)); - + TyExpressionVariant::FunctionApplication { arguments, .. } => { for (_, arg) in arguments.iter_mut() { let _ = arg.type_check_finalize(handler, ctx); } 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 1f022a1b15..36297c6730 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 @@ -459,11 +459,6 @@ impl TyImplTrait { IsExtendingExistingImpl::No, )?; - // Now lets do a partial type check of the body of the functions (while deferring full - // monomorphization of function applications). We will use this tree to perform type check - // analysis (mainly dependency analysis), and re-type check the items ordered by dependency. - let mut defer_ctx = ctx.by_ref().with_defer_monomorphization(); - let new_items = &impl_trait.items; for (item, new_item) in items.clone().into_iter().zip(new_items) { match (item, new_item) { @@ -473,7 +468,7 @@ impl TyImplTrait { (*decl_engine.get_function(decl_ref.id())).clone(); let new_ty_fn_decl = match ty::TyFunctionDecl::type_check_body( handler, - defer_ctx.by_ref(), + ctx.by_ref(), &fn_decl, &mut ty_fn_decl, ) { diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 3e9b0265f7..c9dcd6a796 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -128,7 +128,6 @@ impl ty::TyExpression { selector: None, type_binding: None, call_path_typeid: None, - deferred_monomorphization: false, contract_call_params: IndexMap::new(), contract_caller: None, }, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs index ba2d01049e..6b9c0aecbf 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/function_application.rs @@ -123,7 +123,6 @@ pub(crate) fn instantiate_function_application( selector: None, type_binding: Some(call_path_binding.strip_inner()), call_path_typeid: None, - deferred_monomorphization: false, contract_call_params: IndexMap::new(), contract_caller: None, }, 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 a76a9339c1..973df97efe 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 @@ -683,9 +683,7 @@ pub(crate) fn type_check_method_application( .ok(); if let Some(decl_mapping) = decl_mapping { - if !ctx.defer_monomorphization() { - method.replace_decls(&decl_mapping, handler, &mut ctx)?; - } + method.replace_decls(&decl_mapping, handler, &mut ctx)?; } let method_sig = TyFunctionSig::from_fn_decl(&method); @@ -696,7 +694,6 @@ pub(crate) fn type_check_method_application( if method_sig.is_concrete(engines) && method.is_type_check_finalized && !method.is_trait_method_dummy - && !ctx.defer_monomorphization() { ctx.engines() .qe() @@ -711,7 +708,6 @@ pub(crate) fn type_check_method_application( selector, type_binding: Some(method_name_binding.strip_inner()), call_path_typeid: Some(call_path_typeid), - deferred_monomorphization: ctx.defer_monomorphization(), contract_call_params: contract_call_params_map, contract_caller: None, }; diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index af96bebfaf..27f8c7267a 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -92,12 +92,6 @@ pub struct TypeCheckContext<'a> { /// body). disallow_functions: bool, - /// Indicates when semantic analysis should be deferred for function/method applications. - /// This is currently used to perform the final type checking and monomorphization in the - /// case of impl trait methods after the initial type checked AST is constructed, and - /// after we perform a dependency analysis on the tree. - defer_monomorphization: bool, - /// Indicates when semantic analysis is type checking storage declaration. storage_declaration: bool, @@ -127,7 +121,6 @@ impl<'a> TypeCheckContext<'a> { purity: Purity::default(), kind: TreeType::Contract, disallow_functions: false, - defer_monomorphization: false, storage_declaration: false, experimental, } @@ -169,7 +162,6 @@ impl<'a> TypeCheckContext<'a> { purity: Purity::default(), kind: TreeType::Contract, disallow_functions: false, - defer_monomorphization: false, storage_declaration: false, experimental, } @@ -199,7 +191,6 @@ impl<'a> TypeCheckContext<'a> { kind: self.kind, engines: self.engines, disallow_functions: self.disallow_functions, - defer_monomorphization: self.defer_monomorphization, storage_declaration: self.storage_declaration, experimental: self.experimental, } @@ -226,7 +217,6 @@ impl<'a> TypeCheckContext<'a> { kind: self.kind, engines: self.engines, disallow_functions: self.disallow_functions, - defer_monomorphization: self.defer_monomorphization, storage_declaration: self.storage_declaration, experimental: self.experimental, }; @@ -254,7 +244,6 @@ impl<'a> TypeCheckContext<'a> { kind: self.kind, engines: self.engines, disallow_functions: self.disallow_functions, - defer_monomorphization: self.defer_monomorphization, storage_declaration: self.storage_declaration, experimental: self.experimental, }; @@ -391,15 +380,6 @@ impl<'a> TypeCheckContext<'a> { } } - /// Map this `TypeCheckContext` instance to a new one with - /// `defer_method_application` set to `true`. - pub(crate) fn with_defer_monomorphization(self) -> Self { - Self { - defer_monomorphization: true, - ..self - } - } - /// Map this `TypeCheckContext` instance to a new one with /// `storage_declaration` set to `true`. pub(crate) fn with_storage_declaration(self) -> Self { @@ -453,10 +433,6 @@ impl<'a> TypeCheckContext<'a> { self.disallow_functions } - pub(crate) fn defer_monomorphization(&self) -> bool { - self.defer_monomorphization - } - pub(crate) fn storage_declaration(&self) -> bool { self.storage_declaration }