-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Warn about not explicitly requested conversions from rational number types to their mobile type. #12677
Comments
Thanks for reporting the issue @donutcrypto Notes: Fuzzer wouldn't have helped because both legacy and IR fail on construction. Semantic test below:
|
The reason is that unlimited precision only works as long as you are dealing with arithmetic expressions on literal constants. There are other similar situations where this can happen. I think we could warn when conversions from rational constants to their mobile type happen instead of a conversion to an explicit type. |
Thanks for the reponse. I just wanted to make sure I understand, but the values of For example:
|
@donutcrypto That would fail with "No unique declaration found after argument-dependent lookup.", because such a lookup is ambiguous. There is probably an open issue about choosing the closest one. |
Do you mean in general or just in the ternary operator? In a general case this could be pretty annoying. For example in something like |
Answer from gitter:
Still, I'm not really convinced that it's the rule we need here. On the other hand with some operators like I think it's really not just a different mobile type but also the fact that the expression directly returns the value one of its arguments so the user would expect the literal to remain a literal. Maybe let's try to figure out which specific situations are problematic. So far I came up with these:
On the other hand, I think a warning is not necessary for these:
|
I think the probelmatic case is when a type is derived only from rational numbers, i.e. only from mobile types. As soon as an actual "nameable" type that does not come from a mobile type dictates the result type, it's fine. Can you explain why the assignment operator is problematic? |
ok, maybe it's not. Depends on how we define its return type. I guess the most obvious way is to make it the type of the variable on the left side - and then you're right, if user is aware of that, there's no surprise. I treated it as if it was returning the type of the expression on the right side. |
We discussed this briefly on the call today:
@nishant-sachdeva regarding 3), could you replace the warning for ternary operator with an error as suggested by @chriseth and then see how many Also, I think this behavior is not even documented. Could you mention it on the documentation page about Operators? |
Why do you think it is not documented? Rational literals use unlimited precision until they are used together with a non rational literal, and the condition in the ternary operator is never a rational literal, but we can of course make it more explicit. |
ok, you're right. It is documented:
But yeah, it's easy to miss all the consequences of this paragraph unless you really carefully go through the possible operators. I think we should add a note there mentioning ternary operator. I think I'd mention it under Operators too. |
Also, this paragraph is not entirely true. I just checked and this reverts: contract C {
function f() public {
(true ? 255 : 254) + (true ? 255 : 254);
}
} According to that paragraph it should not overflow - it should be computed in unlimited precision. There's no explicit conversion and no non-literal expressions. |
The issue is that |
Here's what @nishant-sachdeva's check found.
|
This issue has been marked as stale due to inactivity for the last 90 days. |
Hi everyone! This issue has been automatically closed due to inactivity. |
Description
The ternary operator doesn't appear to be producing the correct return type.
Environment
Steps to Reproduce
Deploying the following contract will overflow on uint8 addition. The expected value of
result
is 385.The text was updated successfully, but these errors were encountered: