bitcoin-sv / sol2scrypt

Solidity to sCrypt Transplier
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

return Void #129

Closed xhliu closed 2 years ago

xhliu commented 2 years ago

This is wrong. This should not fail:

function foo() {
  if (true) return;
  int a = 1;
}

_Originally posted by @xhliu in https://github.com/sCrypt-Inc/sol2scrypt/pull/123#discussion_r817098907_

zhfnjust commented 2 years ago
pragma solidity ^0.8.10;

contract SimpleStorage {
    uint storedData;

    function foo() public {
        if (true) return;
        int a = 1;
    }

    function foo1() external {
        if (true) return;
        int a = 1;
    }
}

now this code will be transpile to

contract SimpleStorage {
  @state
  int storedData;

  function foo() : bool {
    bool ret = false;
    bool returned = false;
    if (true) {
      {
        ret = true;
        returned = true;
      }
    }
    if (!returned) {
      int a = 1;
    }
    return ret;
  }

  public function foo1(SigHashPreimage txPreimage) {
    bool ret = false;
    bool returned = false;
    if (true) {
      {
        ret = true;
        returned = true;
      }
    }
    if (!returned) {
      int a = 1;
    }
    require(this.propagateState(txPreimage));
  }

  function propagateState(SigHashPreimage txPreimage) : bool {
    require(Tx.checkPreimage(txPreimage));
    bytes outputScript = this.getStateScript();
    bytes output = Utils.buildOutput(outputScript, SigHash.value(txPreimage));
    return hash256(output) == SigHash.hashOutputs(txPreimage);
  }
}