Allow Mapping keys to have type UserDefinedValueType.

Also added syntax and semantic test.
This commit is contained in:
hrkrshnn 2021-09-13 10:02:27 +02:00
parent 952540c3b5
commit 1fa6c71bd0
6 changed files with 27 additions and 4 deletions

View File

@ -253,12 +253,13 @@ void DeclarationTypeChecker::endVisit(Mapping const& _mapping)
{ {
case Type::Category::Enum: case Type::Category::Enum:
case Type::Category::Contract: case Type::Category::Contract:
case Type::Category::UserDefinedValueType:
break; break;
default: default:
m_errorReporter.fatalTypeError( m_errorReporter.fatalTypeError(
7804_error, 7804_error,
typeName->location(), typeName->location(),
"Only elementary types, contract types or enums are allowed as mapping keys." "Only elementary types, user defined value types, contract types or enums are allowed as mapping keys."
); );
break; break;
} }

View File

@ -0,0 +1,18 @@
type MyInt is int;
contract C {
mapping(MyInt => int) public m;
function set(MyInt key, int value) external {
m[key] = value;
}
function set_unwrapped(int key, int value) external {
m[MyInt.wrap(key)] = value;
}
}
// ====
// compileViaYul: also
// ----
// set(int256,int256): 1, 1 ->
// m(int256): 1 -> 1
// set_unwrapped(int256,int256): 1, 2 ->
// m(int256): 1 -> 2
// m(int256): 2 -> 0

View File

@ -5,4 +5,4 @@ contract c {
mapping(S => uint) data; mapping(S => uint) data;
} }
// ---- // ----
// TypeError 7804: (47-48): Only elementary types, contract types or enums are allowed as mapping keys. // TypeError 7804: (47-48): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys.

View File

@ -5,4 +5,4 @@ contract c {
mapping(S => uint) data; mapping(S => uint) data;
} }
// ---- // ----
// TypeError 7804: (49-50): Only elementary types, contract types or enums are allowed as mapping keys. // TypeError 7804: (49-50): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys.

View File

@ -6,4 +6,4 @@ contract C {
function g (S calldata) external view {} function g (S calldata) external view {}
} }
// ---- // ----
// TypeError 7804: (56-57): Only elementary types, contract types or enums are allowed as mapping keys. // TypeError 7804: (56-57): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys.

View File

@ -0,0 +1,4 @@
type MyInt is int;
contract C {
mapping(MyInt => int) m;
}