diff --git a/test/libsolidity/semanticTests/userDefinedValueType/dirty_uint8_read.sol b/test/libsolidity/semanticTests/userDefinedValueType/dirty_uint8_read.sol new file mode 100644 index 000000000..4100e17f8 --- /dev/null +++ b/test/libsolidity/semanticTests/userDefinedValueType/dirty_uint8_read.sol @@ -0,0 +1,26 @@ +type MyInt8 is int8; +contract C { + MyInt8 public x = MyInt8.wrap(-5); + + /// The most significant bit is flipped to 0 + function create_dirty_slot() external { + uint mask = 2**255 -1; + assembly { + let value := sload(x.slot) + sstore(x.slot, and(mask, value)) + } + } + + function read_unclean_value() external returns (bytes32 ret) { + MyInt8 value = x; + assembly { + ret := value + } + } +} +// ==== +// compileViaYul: also +// ---- +// x() -> -5 +// create_dirty_slot() -> +// read_unclean_value() -> 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb diff --git a/test/libsolidity/semanticTests/userDefinedValueType/immutable_signed.sol b/test/libsolidity/semanticTests/userDefinedValueType/immutable_signed.sol new file mode 100644 index 000000000..8923984a4 --- /dev/null +++ b/test/libsolidity/semanticTests/userDefinedValueType/immutable_signed.sol @@ -0,0 +1,21 @@ +type MyInt is int16; +type MyBytes is bytes2; +contract C { + MyInt immutable a = MyInt.wrap(-2); + MyBytes immutable b = MyBytes.wrap("ab"); + function() internal returns (uint) immutable f = g; + function direct() view external returns (MyInt, MyBytes) { + return (a, b); + } + function viaasm() view external returns (bytes32 x, bytes32 y) { + MyInt _a = a; + MyBytes _b = b; + assembly { x := _a y := _b } + } + function g() internal pure returns (uint) { return 2; } +} +// ==== +// compileViaYul: also +// ---- +// direct() -> -2, 0x6162000000000000000000000000000000000000000000000000000000000000 +// viaasm() -> 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe, 0x6162000000000000000000000000000000000000000000000000000000000000 diff --git a/test/libsolidity/semanticTests/userDefinedValueType/storage_signed.sol b/test/libsolidity/semanticTests/userDefinedValueType/storage_signed.sol new file mode 100644 index 000000000..80745866a --- /dev/null +++ b/test/libsolidity/semanticTests/userDefinedValueType/storage_signed.sol @@ -0,0 +1,35 @@ +type MyInt is int16; +contract C { + bytes2 first = "ab"; + MyInt public a = MyInt.wrap(-2); + bytes2 third = "ef"; + function direct() external returns (MyInt) { + return a; + } + function indirect() external returns (int16) { + return MyInt.unwrap(a); + } + function toMemDirect() external returns (MyInt[1] memory) { + return [a]; + } + function toMemIndirect() external returns (int16[1] memory) { + return [MyInt.unwrap(a)]; + } + function div() external returns (int16) { + return MyInt.unwrap(a) / 2; + } + function viaasm() external returns (bytes32 x) { + MyInt st = a; + assembly { x := st } + } +} +// ==== +// compileViaYul: also +// ---- +// a() -> -2 +// direct() -> -2 +// indirect() -> -2 +// toMemDirect() -> -2 +// toMemIndirect() -> -2 +// div() -> -1 +// viaasm() -> 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe