From 09ba6c653bc883b3968bc80ffff92e7b2194b5a8 Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Mon, 10 Jul 2023 12:28:46 +0100 Subject: [PATCH] Fixes missing replace_decl for variable declarations. (#4777) ## Description We were not calling replace_decl for variable declarations. This was making some trait method calls to not be replaced. Unblocks #4701 Fixes #4773 ## Checklist - [x] I have linked to any relevant issues. - [x] 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). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] 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/language/ty/ast_node.rs | 3 ++ .../generic_where_in_impl_self2/Forc.lock | 13 +++++++ .../generic_where_in_impl_self2/Forc.toml | 8 +++++ .../json_abi_oracle.json | 25 +++++++++++++ .../generic_where_in_impl_self2/src/main.sw | 36 +++++++++++++++++++ .../generic_where_in_impl_self2/test.toml | 3 ++ 6 files changed, 88 insertions(+) create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/test.toml diff --git a/sway-core/src/language/ty/ast_node.rs b/sway-core/src/language/ty/ast_node.rs index 99b651a21b9..3d17dd0f441 100644 --- a/sway-core/src/language/ty/ast_node.rs +++ b/sway-core/src/language/ty/ast_node.rs @@ -92,6 +92,9 @@ impl ReplaceDecls for TyAstNode { TyAstNodeContent::ImplicitReturnExpression(ref mut exp) => { exp.replace_decls(decl_mapping, engines) } + TyAstNodeContent::Declaration(TyDecl::VariableDecl(ref mut decl)) => { + decl.body.replace_decls(decl_mapping, engines); + } TyAstNodeContent::Declaration(_) => {} TyAstNodeContent::Expression(ref mut expr) => expr.replace_decls(decl_mapping, engines), TyAstNodeContent::SideEffect(_) => (), diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.lock new file mode 100644 index 00000000000..3c33721a828 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = 'core' +source = 'path+from-root-B5A424CFF35B4F1C' + +[[package]] +name = 'generic_where_in_impl_self2' +source = 'member' +dependencies = ['std'] + +[[package]] +name = 'std' +source = 'path+from-root-B5A424CFF35B4F1C' +dependencies = ['core'] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.toml new file mode 100644 index 00000000000..e818c8d4916 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "generic_where_in_impl_self2" + +[dependencies] +std = { path = "../../../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/json_abi_oracle.json new file mode 100644 index 00000000000..03b2f150939 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/json_abi_oracle.json @@ -0,0 +1,25 @@ +{ + "configurables": [], + "functions": [ + { + "attributes": null, + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": null, + "type": "bool", + "typeId": 0, + "typeParameters": null + } + ] +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/src/main.sw new file mode 100644 index 00000000000..2c8bcd21a0a --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/src/main.sw @@ -0,0 +1,36 @@ +script; + +use std::assert::*; + +trait Trait { + #[inline(never)] + fn method(self) -> u64; +} + +impl Trait for u64 { + #[inline(never)] + fn method(self) -> u64{ + 42 + } +} + +struct CallTrait {} + +#[inline(never)] +fn call_trait(t: T) -> u64 where T: Trait { + t.method() +} + +impl CallTrait where K: Trait { + pub fn call_trait(self, key: K) -> u64 { + let v = call_trait(key); + v + } +} + +fn main() -> bool { + let _ = call_trait(1); + let ct = CallTrait:: {}; + assert(ct.call_trait(1) == 42); + true +} diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/test.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/test.toml new file mode 100644 index 00000000000..24cd9f3291c --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self2/test.toml @@ -0,0 +1,3 @@ +category = "run" +expected_result = { action = "return", value = 1 } +validate_abi = true \ No newline at end of file