hyperledger / solang

Solidity Compiler for Solana and Polkadot
https://solang.readthedocs.io/
Apache License 2.0
1.25k stars 208 forks source link

Error: `type(int256).min` is too large when in comparison #1523

Closed BenTheKush closed 11 months ago

BenTheKush commented 11 months ago

Describe the bug The expression type(int256).min crashes during parse_and_resolve().

To Reproduce I've added this to this repo, but I'll repeat here.

Here is the code that triggers the bug:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

library Issue1523 {
    function i1523_fail1(int256 x) internal pure returns (bool) {
        return x <= type(int256).min;
    }

    function i1523_fail2(int256 x) internal pure returns (bool) {
        return x <= type(int256).min + 1;
    }

    function i1523_fail3(int256 x) internal pure returns (bool) {
        // Actual min value inlined
        return x <= -57896044618658097711785492504343953926634992332820282019728792003956564819968;
    }

    function i1523_fail4(int256 x) internal pure returns (bool) {
        // Actual min value + 1
        return x <= -57896044618658097711785492504343953926634992332820282019728792003956564819967;
    }

    function i1523_pass1() internal pure returns (int256) {
        return type(int256).min + 1;
    }

    function i1523_pass2() internal pure returns (int256) {
        return type(int256).min;
    }
}

If I run:

solang compile --target solana bug.sol

error: -57896044618658097711785492504343953926634992332820282019728792003956564819968 is too large ┌─ /Users/benku/Playground/issue1523.sol:6:21 │ 6 │ return x <= type(int256).min; │ ^^^^^^^^^^^^^^^^

error: -57896044618658097711785492504343953926634992332820282019728792003956564819968 is too large ┌─ /Users/benku/Playground/issue1523.sol:10:21 │ 10 │ return x <= type(int256).min + 1; │ ^^^^^^^^^^^^^^^^

error: -57896044618658097711785492504343953926634992332820282019728792003956564819968 is too large ┌─ /Users/benku/Playground/issue1523.sol:14:21 │ 14 │ return x <= -57896044618658097711785492504343953926634992332820282019728792003956564819968; │

Note that the first three functions fail. The fourth function I've added to the min value literal, and this passes. This sounds like we are trying to create the value 57896044618658097711785492504343953926634992332820282019728792003956564819968 as an int256 and then apply a negative unary operator to it, but only in certain contexts? I

Expected behavior This compiles w/ solc.

Hyperledger Solang version

Produced with: