Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference bug for literals in arithmetic expressions that are implicitly cast #1535

Open
bkushigian opened this issue Sep 16, 2023 · 0 comments

Comments

@bkushigian
Copy link

bkushigian commented Sep 16, 2023

I'm assuming this is a bug in solang and not solc, but I haven't dug into the specs on how type promotions work for shifts. The following code compiles with solc but not solang:

// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

contract MultiRolesAuthority {
    function shiftTypeInference(uint8 role) public virtual returns (bytes32) {
        bytes32 x = bytes32(1 << role);
        return x;
    }
}

solang is typing 1 << role as a u8, because that is what role is.

$ solang 
=====  Error: src/auth/authorities/MultiRolesAuthority.sol  =====
error: conversion to bytes32 from uint8 not allowed
  ┌─ /Users/benku/Certora/tests/solmate/src/auth/authorities/MultiRolesAuthority.sol:8:25
  │
8 │             bytes32 x = bytes32(1 << role);
  │                         ^^^^^^^^^^^^^^^^^^

Similarly, in the following code, we have int256 x receiving 5 ** 10. I think solidity is emitting a cast on the result of 5 ** 10 (or something, I don't know EVM bytecode), while solang is inferring that 5 ** 10 is a int256 and therefore its arguments must be signed.

// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

contract LiteralTypeInfer {
    function typeInfer() public pure returns (int256 x) {
        x = 5 ** 10;
    }
}
@bkushigian bkushigian changed the title Type inference bug for shift Type inference bug for literals in arithmetic expressions that are implicitly cast Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@bkushigian and others