Tests/Docs after stricter explicit conversion.

This commit is contained in:
hrkrshnn
2020-12-03 16:51:05 +01:00
committed by chriseth
parent 92ab32e532
commit 40244c5469
25 changed files with 183 additions and 64 deletions
+4 -4
View File
@@ -162,10 +162,10 @@ BOOST_AUTO_TEST_CASE(single_callvaluecheck)
a = b;
}
function f1(address b) public pure returns (uint c) {
return uint(b) + 2;
return uint160(b) + 2;
}
function f2(address b) public pure returns (uint) {
return uint(b) + 8;
return uint160(b) + 8;
}
function f3(address, uint c) pure public returns (uint) {
return c - 5;
@@ -178,10 +178,10 @@ BOOST_AUTO_TEST_CASE(single_callvaluecheck)
a = b;
}
function f1(address b) public pure returns (uint c) {
return uint(b) + 2;
return uint160(b) + 2;
}
function f2(address b) public pure returns (uint) {
return uint(b) + 8;
return uint160(b) + 8;
}
function f3(address, uint c) payable public returns (uint) {
return c - 5;
+3 -3
View File
@@ -673,9 +673,9 @@ BOOST_AUTO_TEST_CASE(sign_extension)
contract test {
function run() public returns(uint256 y) {
unchecked {
int64 x = -int32(0xff);
int64 x = -int32(int64(0xff));
if (x >= 0xff) return 0;
return 0 - uint256(x);
return 0 - uint256(int256(x));
}
}
}
@@ -5683,7 +5683,7 @@ BOOST_AUTO_TEST_CASE(dirty_scratch_space_prior_to_constant_optimiser)
return 0x0000000000001234123412431234123412412342112341234124312341234124;
}
function g(address a) internal pure returns (uint) {
unchecked { return uint(a) * 0x0000000000001234123412431234123412412342112341234124312341234124; }
unchecked { return uint(uint160(a)) * 0x0000000000001234123412431234123412412342112341234124312341234124; }
}
function h(uint a) internal pure returns (uint) {
unchecked { return a * 0x0000000000001234123412431234123412412342112341234124312341234124; }
+2 -2
View File
@@ -437,7 +437,7 @@ BOOST_AUTO_TEST_CASE(constant_optimization_early_exit)
char const* sourceCode = R"(
contract HexEncoding {
function hexEncodeTest(address addr) public returns (bytes32 ret) {
uint x = uint(addr) / 2**32;
uint x = uint(uint160(addr)) / 2**32;
// Nibble interleave
x = x & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff;
@@ -457,7 +457,7 @@ BOOST_AUTO_TEST_CASE(constant_optimization_early_exit)
assembly {
mstore(0, x)
}
x = uint(addr) * 2**96;
x = uint160(addr) * 2**96;
// Nibble interleave
x = x & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff;
@@ -7,7 +7,7 @@ contract c {
for (uint8 i = 0; i <= 40; i++)
data.push(byte(i+1));
for (int8 j = 40; j >= 0; j--) {
require(data[uint8(j)] == byte(j+1));
require(data[uint8(j)] == byte(uint8(j+1)));
require(data.length == uint8(j+1));
data.pop();
}
@@ -1,6 +1,6 @@
contract C {
function f(int16[] calldata a) external returns (bool correct) {
uint32 x = uint32(a[1]);
uint32 x = uint32(uint16(a[1]));
uint r;
assembly {
r := x
@@ -13,9 +13,9 @@ contract C {
b = (0 - uint8(a)) * 2;
c = a * int8(120) * int8(121);
}
x1 = uint256(a);
x1 = uint256(int256(a));
x2 = b;
x3 = uint256(c);
x3 = uint256(int256(c));
x4 = d;
}
}
@@ -1,8 +1,8 @@
contract Test {
function test() public returns (uint ret) { return uint(address(uint128(type(uint200).max))); }
function test() public returns (uint ret) { return uint(uint160(address(uint160(type(uint200).max)))); }
}
// ====
// compileViaYul: also
// compileToEwasm: also
// ----
// test() -> 0xffffffffffffffffffffffffffffffff
// test() -> 0xffffffffffffffffffffffffffffffffffffffff
@@ -4,7 +4,7 @@ contract C {
uint16[] m;
}
function f(S calldata s) public pure returns (bool correct) {
int8 x = int8(s.m[0]);
int8 x = int8(int16(s.m[0]));
uint r;
assembly {
r := x
@@ -4,7 +4,7 @@ contract C {
assembly {
mstore(m, 0xdeadbeef15dead)
}
int32 x = int32(m[0]);
int32 x = int32(uint32(m[0]));
uint r;
assembly {
r := x
@@ -21,19 +21,19 @@ contract C {
assert(x == 255);
// signed <- unsigned
int8 y = int8(type(uint16).max);
int8 y = int8(uint8(type(uint16).max));
assert(y == -1);
y = int8(uint16(100));
y = int8(uint8(uint16(100)));
assert(y == 100);
y = int8(uint16(200));
y = int8(uint8(uint16(200)));
assert(y == -56);
// unsigned <- signed
uint8 v = uint8(type(uint16).max);
assert(v == 255);
v = uint8(int16(300));
v = uint8(int8(int16(300)));
assert(v == 44);
v = uint8(int16(200));
v = uint8(int8(int16(200)));
assert(v == 200);
// fixed bytes
@@ -13,16 +13,16 @@ contract C {
assert(y == -(2**255) + 10);
int256 z = int256(uint(2**255 + 10));
assert(z == -(2**255) + 10);
int256 t = int256(bytes32(uint256(200)));
int256 t = int256(uint256(bytes32(uint256(200))));
assert(t == 200);
int256 v = int256(bytes32(uint256(2**255 + 10)));
int256 v = int256(uint256(bytes32(uint256(2**255 + 10))));
assert(v == -(2**255) + 10);
int160 a = int160(address(type(uint160).max));
int160 a = int160(uint160(address(type(uint160).max)));
assert(a == -1);
int160 b = int160(address(uint(2**159 + 10)));
int160 b = int160(uint160(address(uint160(uint(2**159 + 10)))));
assert(b == -(2**159) + 10);
D d;
int160 e = int160(address(d));
int160 e = int160(uint160(address(d)));
assert(e == 0);
}
@@ -71,3 +71,5 @@ contract C {
}
}
// ----
// Warning 8364: (1468-1469): Assertion checker does not yet implement type type(enum E)
// Warning 8364: (1468-1469): Assertion checker does not yet implement type type(enum E)
@@ -9,28 +9,28 @@ contract C {
assert(x == 65535);
int8 i = int8(-1);
assert(i == -1);
x = uint16(int8(-1));
assert(x == 65535);
x = uint16(uint8(int8(-1)));
assert(x == 255);
x = uint16(int16(i));
assert(x == 65535);
uint z = uint(i);
assert(z == 2**256 - 1);
uint z = uint(uint8(i));
assert(z == 255);
}
function f2() public pure {
// signed <- unsigned
int16 y = int16(uint8(uint(65535)));
int16 y = int16(uint16(uint8(uint(65535))));
assert(y == 255);
int z = int(uint8(type(uint).max));
int z = int(uint(uint8(type(uint).max)));
assert(z == 255);
z = int(uint8(255));
z = int(uint(uint8(255)));
assert(z == 255);
}
function f3() public pure {
// signed <- signed
int16 y = int16(int8(uint(65535)));
assert(y == -1);
int16 y = int16(uint16(uint8(int8(int(uint(65535))))));
assert(y == 255);
int z = int(int8(-1));
assert(z == -1);
z = int(int8(int(255)));
@@ -51,9 +51,9 @@ contract C {
assert(y == 65535);
y = uint16(uint8(type(uint16).max));
assert(y == 255);
address a = address(uint8(0));
address a = address(uint160(uint8(0)));
assert(a == address(0));
D d = D(uint8(0));
D d = D(address(uint160(uint8(0))));
assert(a == address(d));
bytes2 b1 = 0xcafe;
bytes4 b2 = bytes4(b1);
@@ -67,3 +67,5 @@ contract C {
}
}
// ----
// Warning 8364: (1284-1285): Assertion checker does not yet implement type type(contract D)
// Warning 8364: (1284-1285): Assertion checker does not yet implement type type(contract D)
@@ -1,5 +1,5 @@
contract c {
modifier mod1(uint a) { if (msg.sender == address(a)) _; }
modifier mod1(uint a) { if (msg.sender == address(uint160(a))) _; }
modifier mod2 { if (msg.sender == address(2)) _; }
function f() public mod1(7) mod2 { }
}
@@ -1,5 +1,5 @@
contract C {
function f(uint x) public pure returns (address payable) {
return address(x);
return address(uint160(x));
}
}
@@ -0,0 +1,42 @@
contract B{}
contract C
{
function f() public pure {
uint16 a = uint16(uint8(int8(-1)));
a;
int8 b = -1;
b;
uint16 c = uint16(uint8(b));
c;
int8 d = int8(int16(uint16(type(uint16).max)));
d;
uint16 e = type(uint16).max;
e;
int8 g = int8(uint8(e));
g;
address h = address(uint160(uint(type(uint).max)));
h;
uint i = uint(uint160(address(0)));
i;
uint j = type(uint).max;
j;
address k = address(uint160(j));
k;
int80 l = int80(uint80(bytes10("h")));
l;
bytes10 m = bytes10(uint80(int80(-1)));
m;
B n = B(uint(int(100)));
n;
}
}
// ----
@@ -0,0 +1,52 @@
contract B{}
enum E { Zero }
contract C
{
function f() public pure {
uint16 a = uint16(int8(-1));
int8 b = -1;
uint16 c = uint16(b);
int8 d = int8(uint16(type(uint16).max));
uint16 e = type(uint16).max;
int8 g = int8(e);
address h = address(uint(type(uint).max));
uint i = uint(address(0));
uint j = type(uint).max;
address k = address(j);
int80 l = int80(bytes10("h"));
bytes10 m = bytes10(int80(-1));
B n = B(int(100));
int o = int(new B());
B p = B(0x00);
int q = int(E(0));
int r = int(E.Zero);
}
}
// ----
// TypeError 9640: (95-111): Explicit type conversion not allowed from "int8" to "uint16".
// TypeError 9640: (154-163): Explicit type conversion not allowed from "int8" to "uint16".
// TypeError 9640: (183-213): Explicit type conversion not allowed from "uint16" to "int8".
// TypeError 9640: (270-277): Explicit type conversion not allowed from "uint16" to "int8".
// TypeError 9640: (300-329): Explicit type conversion not allowed from "uint256" to "address".
// TypeError 9640: (349-365): Explicit type conversion not allowed from "address payable" to "uint256".
// TypeError 9640: (421-431): Explicit type conversion not allowed from "uint256" to "address".
// TypeError 9640: (452-471): Explicit type conversion not allowed from "bytes10" to "int80".
// TypeError 9640: (493-511): Explicit type conversion not allowed from "int80" to "bytes10".
// TypeError 9640: (528-539): Explicit type conversion not allowed from "int256" to "contract B".
// TypeError 9640: (557-569): Explicit type conversion not allowed from "contract B" to "int256".
// TypeError 9640: (586-593): Explicit type conversion not allowed from "int_const 0" to "contract B".
// TypeError 9640: (612-621): Explicit type conversion not allowed from "enum E" to "int256".
// TypeError 9640: (639-650): Explicit type conversion not allowed from "enum E" to "int256".