mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #10224 from ethereum/strict-literal-to-enum-conversion
[BREAKING] Strict explicit conversion between literals and enums
This commit is contained in:
@@ -13,8 +13,8 @@ contract test {
|
||||
d = uint256(choice);
|
||||
}
|
||||
|
||||
function getChoiceFromNegativeLiteral() public returns (uint256 d) {
|
||||
choice = ActionChoices(-1);
|
||||
function getChoiceFromMax() public returns (uint256 d) {
|
||||
choice = ActionChoices(type(uint).max);
|
||||
d = uint256(choice);
|
||||
}
|
||||
|
||||
@@ -25,8 +25,9 @@ contract test {
|
||||
// compileViaYul: also
|
||||
// EVMVersion: >=byzantium
|
||||
// ----
|
||||
// getChoiceExp(uint256): 2 -> 2
|
||||
// getChoiceExp(uint256): 3 -> FAILURE, hex"4e487b71", 33 # These should throw #
|
||||
// getChoiceFromSigned(int256): -1 -> FAILURE, hex"4e487b71", 33
|
||||
// getChoiceFromNegativeLiteral() -> FAILURE, hex"4e487b71", 33
|
||||
// getChoiceFromMax() -> FAILURE, hex"4e487b71", 33
|
||||
// getChoiceExp(uint256): 2 -> 2 # These should work #
|
||||
// getChoiceExp(uint256): 0 -> 0
|
||||
|
||||
@@ -13,8 +13,8 @@ contract test {
|
||||
d = uint256(choice);
|
||||
}
|
||||
|
||||
function getChoiceFromNegativeLiteral() public returns (uint256 d) {
|
||||
choice = ActionChoices(-1);
|
||||
function getChoiceFromMax() public returns (uint256 d) {
|
||||
choice = ActionChoices(type(uint256).max);
|
||||
d = uint256(choice);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,6 @@ contract test {
|
||||
// ----
|
||||
// getChoiceExp(uint256): 3 -> FAILURE # These should throw #
|
||||
// getChoiceFromSigned(int256): -1 -> FAILURE
|
||||
// getChoiceFromNegativeLiteral() -> FAILURE
|
||||
// getChoiceFromMax() -> FAILURE
|
||||
// getChoiceExp(uint256): 2 -> 2 # These should work #
|
||||
// getChoiceExp(uint256): 0 -> 0
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
enum Test { One, Two }
|
||||
function f() public pure {
|
||||
Test a = Test(0);
|
||||
Test b = Test(1);
|
||||
Test c = Test(type(uint).max);
|
||||
a; b; c;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,16 @@
|
||||
contract C {
|
||||
enum Test { One, Two }
|
||||
function f() public {
|
||||
Test(-1);
|
||||
Test(2);
|
||||
Test(13);
|
||||
Test(5/3);
|
||||
Test(0.5);
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError 9640: (74-82): Explicit type conversion not allowed from "int_const -1" to "enum C.Test".
|
||||
// TypeError 9640: (92-99): Explicit type conversion not allowed from "int_const 2" to "enum C.Test".
|
||||
// TypeError 9640: (109-117): Explicit type conversion not allowed from "int_const 13" to "enum C.Test".
|
||||
// TypeError 9640: (127-136): Explicit type conversion not allowed from "rational_const 5 / 3" to "enum C.Test".
|
||||
// TypeError 9640: (146-155): Explicit type conversion not allowed from "rational_const 1 / 2" to "enum C.Test".
|
||||
Reference in New Issue
Block a user