OCamlPro / ocaml-solidity

Ocaml-solidity provides a Solidity parser and typechecker
https://ocamlpro.github.io/ocaml-solidity
Other
32 stars 10 forks source link

Fix Handling of Higher-Order Functions #40

Closed dmzimmerman closed 1 year ago

dmzimmerman commented 1 year ago

The current OCaml-Solidity parser does not properly handle the use of higher-order functions. For example, given this code:

contract A {
    uint256 internal a;

    function accessibleInternally(int256 b) public {
        b = b + 1;
    }

    function hof(function(int256) somfunc, int256 a) internal {
        a = a + 1;
    }

    function useHof() public {
        hof(accessibleInternally, 10);
        a = 1 + 1;
    }
}

it returns the following error: Type error: Invalid type for argument in "function call". Invalid implicit conversion from "function(int256) nonpayable " to "function(int256) nonpayable " requested. This is because it doesn't realize that a public function is an acceptable substitute for the internal function expected as a parameter to hof.

This PR fixes this issue (and other similar type conversion issues) with higher-order functions by implementing the allowed conversions for function types and mutabilities; it also includes several test cases to exercise the functionality.

CLAassistant commented 1 year ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

dmzimmerman commented 1 year ago

I'm not sure why it thinks I haven't signed the CLA; I signed it for the other PR (and just went through it again now).