ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
22.64k stars 5.61k forks source link

Seems like an incorrect type inference... #15197

Closed haoyang9804 closed 1 week ago

haoyang9804 commented 2 weeks ago

The following two statements are of the same functions:

int256 a = 22;
int256 a2 = true ? 22 : 23;

However, the second statement throws an error with message: Error: Type uint8 is not implicitly convertible to expected type int256. Seems that Solidity compiler misunderstood the type of 22 and cannot take it as a int256 literal as it did for the first statement.

This bug (or, this feature) is kind of confusing and causes a little inconvenience.

Renato03110 commented 2 weeks ago

int256 a = 22; int256 a2 = true ? int256(22) : int256(23);

haoyang9804 commented 2 weeks ago

int256 a = 22; int256 a2 = true ? int256(22) : int256(23);

Oh thx. I think it's a solution. But is it not a bug? I expect the type checker can infer the type of 22 and 23 in this scenario.

mehtavishwa30 commented 1 week ago

@haoyang9804 This behaviour is not a bug and is in fact the normal behaviour (see docs for reference). The type system does infer types in cases that are currently limited. However, we are aware of all the cases, the likes of above, that aren't covered well by the current type system and are working on bringing a better experience through the new type system in Solidity.

For now, I will be closing this issue given the above reasoning.