From 35e9e4283b97d9d8cb2901a64c39515bd65eaaa6 Mon Sep 17 00:00:00 2001 From: Anton Trunov Date: Mon, 25 Sep 2023 19:06:43 +0400 Subject: [PATCH] Fix calling trait methods from each other An attempt to fix issue #4770 in the spirit of PR #5141. Unfortunately (and a bit surprisingly), this fails when compiling stdlib: ``` Compiling library std (/Users/anton/fuel/sway/sway-lib-std) Failed to compile std error --> /Users/anton/fuel/sway/sway-lib-std/src/hash.sw:322:41 | 320 | 321 | #![inline(never)] 322 | pub fn sha256(s: T) -> b256 where T: Hash { | ^^^^ Could not find symbol "Hash" in this scope. 323 | let mut hasher = Hasher::new(); 324 | s.hash(hasher); | ____ ``` --- .../ast_node/declaration/impl_trait.rs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 8af06692717..a22c0980088 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 @@ -657,7 +657,7 @@ fn type_check_trait_implementation( } } - let mut all_items_refs: Vec = impld_item_refs.values().cloned().collect(); + let all_items_refs_temp: Vec = impld_item_refs.values().cloned().collect(); // Retrieve the methods defined on the trait declaration and transform // them into the correct typing for this impl block by using the type @@ -684,6 +684,28 @@ fn type_check_trait_implementation( BTreeMap::new(), impld_item_refs, ); + + let mut all_items_refs: Vec = vec![]; + for item in all_items_refs_temp.iter() { + match item { + TyImplItem::Fn(decl_ref) => { + let mut method = decl_engine.get_function(decl_ref); + method.replace_decls(&decl_mapping, handler, &mut ctx)?; + all_items_refs.push(TyImplItem::Fn( + decl_engine + .insert(method) + .with_parent(decl_engine, (*decl_ref.id()).into()), + )); + } + TyImplItem::Constant(decl_ref) => { + let mut const_decl = decl_engine.get_constant(decl_ref); + const_decl.replace_decls(&decl_mapping, handler, &mut ctx)?; + all_items_refs.push(TyImplItem::Constant(decl_engine.insert(const_decl))); + } + TyImplItem::Type(_) => all_items_refs.push(item.clone()), + } + } + for item in trait_items.iter() { match item { TyImplItem::Fn(decl_ref) => {