Browse Source

Bugfix: External functions read their variable if they are called (#1457)

Signed-off-by: xermicus <cyrill@parity.io>
Cyrill Leutwiler 2 years ago
parent
commit
3b9c36ead1

+ 3 - 2
src/sema/unused_variable.rs

@@ -171,7 +171,9 @@ pub fn used_variable(ns: &mut Namespace, exp: &Expression, symtable: &mut Symtab
             used_variable(ns, expr, symtable);
             used_variable(ns, expr, symtable);
         }
         }
 
 
-        Expression::InternalFunctionCall { .. } | Expression::ExternalFunctionCall { .. } => {
+        Expression::ExternalFunction { .. }
+        | Expression::InternalFunctionCall { .. }
+        | Expression::ExternalFunctionCall { .. } => {
             check_function_call(ns, exp, symtable);
             check_function_call(ns, exp, symtable);
         }
         }
 
 
@@ -255,7 +257,6 @@ pub fn check_function_call(ns: &mut Namespace, exp: &Expression, symtable: &mut
                 used_variable(ns, expr, symtable);
                 used_variable(ns, expr, symtable);
             }
             }
         }
         }
-
         _ => {}
         _ => {}
     }
     }
 }
 }

+ 13 - 0
tests/contract_testcases/polkadot/variables/ext_fn_call_is_reading_variable_01.sol

@@ -0,0 +1,13 @@
+contract C {
+    function ext_func_call() public payable {
+        A a = new A();
+        function() external payable func = a.a;
+        func();
+    }
+}
+
+contract A {
+    function a() public payable {}
+}
+
+// ---- Expect: diagnostics ----

+ 13 - 0
tests/contract_testcases/polkadot/variables/ext_fn_call_is_reading_variable_02.sol

@@ -0,0 +1,13 @@
+contract C {
+    function ext_func_call() public payable {
+        A a = new A();
+        function() external payable func = a.a;
+    }
+}
+
+contract A {
+    function a() public payable {}
+}
+
+// ---- Expect: diagnostics ----
+// warning: 4:37-41: local variable 'func' has been assigned, but never read

+ 0 - 1
tests/contract_testcases/solana/expressions/selector_in_free_function_02.sol

@@ -12,4 +12,3 @@
 // ---- Expect: diagnostics ----
 // ---- Expect: diagnostics ----
 // warning: 3:13-34: function can be declared 'pure'
 // warning: 3:13-34: function can be declared 'pure'
 // warning: 7:13-52: function can be declared 'pure'
 // warning: 7:13-52: function can be declared 'pure'
-// warning: 7:26-27: function parameter 't' is unused

+ 23 - 0
tests/polkadot_tests/variables.rs

@@ -49,3 +49,26 @@ fn global_constants() {
 
 
     runtime.function("test", 0u64.encode());
     runtime.function("test", 0u64.encode());
 }
 }
+
+#[test]
+fn ext_fn_call_is_reading_variable() {
+    let mut runtime = build_solidity(
+        r##"contract C {
+            function ext_func_call(uint32 arg) public payable returns (uint32) {
+                A a = new A();
+                function(uint32) external returns (uint32) func = a.a;
+                return func(arg);
+            }
+        }
+        
+        contract A {
+            function a(uint32 arg) public pure returns (uint32) {
+                return arg;
+            }
+        }"##,
+    );
+
+    runtime.set_transferred_value(1000);
+    runtime.function("ext_func_call", 0xdeadbeefu32.encode());
+    assert_eq!(runtime.output(), 0xdeadbeefu32.encode())
+}