Syntax checks for user defined value types

This commit is contained in:
hrkrshnn 2021-09-07 18:43:04 +02:00
parent 54484e9795
commit 229f50eef1
10 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,4 @@
type MyInt is int;
function f(MyInt a) pure returns (MyInt b) {
b = MyInt(a);
}

View File

@ -0,0 +1,24 @@
type MyUInt is uint;
type MyAddress is address;
type AnotherUInt is uint;
function f() pure {
MyUInt(-1);
MyAddress(-1);
MyUInt(5);
MyAddress(address(5));
AnotherUInt(MyUInt.wrap(5));
MyUInt(AnotherUInt.wrap(10));
AnotherUInt.unwrap(MyUInt.wrap(5));
MyUInt.unwrap(AnotherUInt.wrap(10));
}
// ----
// TypeError 9640: (99-109): Explicit type conversion not allowed from "int_const -1" to "user defined type MyUInt".
// TypeError 9640: (115-128): Explicit type conversion not allowed from "int_const -1" to "user defined type MyAddress".
// TypeError 9640: (134-143): Explicit type conversion not allowed from "int_const 5" to "user defined type MyUInt".
// TypeError 9640: (149-170): Explicit type conversion not allowed from "address" to "user defined type MyAddress".
// TypeError 9640: (177-204): Explicit type conversion not allowed from "user defined type MyUInt" to "user defined type AnotherUInt".
// TypeError 9640: (210-238): Explicit type conversion not allowed from "user defined type AnotherUInt" to "user defined type MyUInt".
// TypeError 9553: (263-277): Invalid type for argument in function call. Invalid implicit conversion from user defined type MyUInt to user defined type AnotherUInt requested.
// TypeError 9553: (298-318): Invalid type for argument in function call. Invalid implicit conversion from user defined type AnotherUInt to user defined type MyUInt requested.

View File

@ -0,0 +1,8 @@
type MyUint is uint;
type MyAddress is address;
function f() pure {
MyUint.wrap(5);
MyAddress.wrap(address(5));
}
// ----

View File

@ -0,0 +1,9 @@
type MyAddress is address;
type MyUInt8 is uint8;
function f() pure {
MyAddress.wrap(address(5));
MyUInt8.wrap(5);
MyUInt8.wrap(50);
}
// ----

View File

@ -0,0 +1,4 @@
function f(MyIntB x) pure {}
type MyIntB is MyIntB;
// ----
// TypeError 8657: (44-50): The underlying type for a user defined value type has to be an elementary value type.

View File

@ -0,0 +1,14 @@
type MyInt is uint;
type MyAddress is address;
function f() pure {
MyInt a;
MyInt b = a;
MyAddress c;
MyAddress d = c;
b;
d;
}
function g(MyInt a) pure returns (MyInt) {
return a;
}

View File

@ -0,0 +1,22 @@
type MyInt is int;
function f(int a) pure returns (int) {
MyInt b = a;
int c = b;
address d = b;
MyInt e = d;
uint x = 0;
MyInt y = MyInt(x);
return e;
}
// ----
// TypeError 9574: (62-73): Type int256 is not implicitly convertible to expected type user defined type MyInt.
// TypeError 9574: (80-89): Type user defined type MyInt is not implicitly convertible to expected type int256.
// TypeError 9574: (96-109): Type user defined type MyInt is not implicitly convertible to expected type address.
// TypeError 9574: (116-127): Type address is not implicitly convertible to expected type user defined type MyInt.
// TypeError 9640: (160-168): Explicit type conversion not allowed from "uint256" to "user defined type MyInt".
// TypeError 6359: (182-183): Return argument type user defined type MyInt is not implicitly convertible to expected type (type of first return variable) int256.

View File

@ -0,0 +1,7 @@
type MyInt is int;
function f() pure {
(MyInt).wrap;
(MyInt).wrap(5);
(MyInt).unwrap;
(MyInt).unwrap(MyInt.wrap(5));
}

View File

@ -0,0 +1,19 @@
type MyAddress is address;
interface I {}
contract C {
function f(MyAddress a) external {
}
function f(address a) external {
}
}
contract D {
function g(MyAddress a) external {
}
}
contract E is D {
function g(I a) external {
}
}
// ----
// TypeError 9914: (104-142): Function overload clash during conversion to external types for arguments.
// TypeError 9914: (162-202): Function overload clash during conversion to external types for arguments.

View File

@ -0,0 +1,8 @@
type MyInt is int;
function test() pure {
function (MyInt) returns (int) f = MyInt.unwrap;
function (int) returns (MyInt) g = MyInt.wrap;
}
// ----
// TypeError 9574: (46-93): Type function (user defined type MyInt) pure returns (int256) is not implicitly convertible to expected type function (user defined type MyInt) returns (int256). Special functions can not be converted to function types.
// TypeError 9574: (99-144): Type function (int256) pure returns (user defined type MyInt) is not implicitly convertible to expected type function (int256) returns (user defined type MyInt). Special functions can not be converted to function types.