Skip to content

Commit 8fafdea

Browse files
authored
Merge pull request #11939 from ethereum/userdefined-types-mapping-key
Allow Mapping keys to have type UserDefinedValueType.
2 parents 623b4ba + 2ead3f4 commit 8fafdea

File tree

9 files changed

+158
-16
lines changed

9 files changed

+158
-16
lines changed

libsolidity/analysis/DeclarationTypeChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,13 @@ void DeclarationTypeChecker::endVisit(Mapping const& _mapping)
253253
{
254254
case Type::Category::Enum:
255255
case Type::Category::Contract:
256+
case Type::Category::UserDefinedValueType:
256257
break;
257258
default:
258259
m_errorReporter.fatalTypeError(
259260
7804_error,
260261
typeName->location(),
261-
"Only elementary types, contract types or enums are allowed as mapping keys."
262+
"Only elementary types, user defined value types, contract types or enums are allowed as mapping keys."
262263
);
263264
break;
264265
}

test/libsolidity/ASTJSON/userDefinedValueType.json

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"C":
66
[
7-
21
7+
27
88
],
99
"MyAddress":
1010
[
@@ -19,7 +19,7 @@
1919
16
2020
]
2121
},
22-
"id": 22,
22+
"id": 28,
2323
"nodeType": "SourceUnit",
2424
"nodes":
2525
[
@@ -193,7 +193,7 @@
193193
"parameters": [],
194194
"src": "61:0:1"
195195
},
196-
"scope": 22,
196+
"scope": 28,
197197
"src": "48:47:1",
198198
"stateMutability": "nonpayable",
199199
"virtual": false,
@@ -205,10 +205,10 @@
205205
"contractDependencies": [],
206206
"contractKind": "contract",
207207
"fullyImplemented": true,
208-
"id": 21,
208+
"id": 27,
209209
"linearizedBaseContracts":
210210
[
211-
21
211+
27
212212
],
213213
"name": "C",
214214
"nameLocation": "105:1:1",
@@ -253,12 +253,82 @@
253253
"typeString": "uint256"
254254
}
255255
}
256+
},
257+
{
258+
"constant": false,
259+
"functionSelector": "97682884",
260+
"id": 26,
261+
"mutability": "mutable",
262+
"name": "m",
263+
"nameLocation": "205:1:1",
264+
"nodeType": "VariableDeclaration",
265+
"scope": 27,
266+
"src": "169:37:1",
267+
"stateVariable": true,
268+
"storageLocation": "default",
269+
"typeDescriptions":
270+
{
271+
"typeIdentifier": "t_mapping$_t_userDefinedValueType$_MyAddress_$18_$_t_userDefinedValueType$_MyUInt_$20_$",
272+
"typeString": "mapping(user defined type MyAddress => user defined type MyUInt)"
273+
},
274+
"typeName":
275+
{
276+
"id": 25,
277+
"keyType":
278+
{
279+
"id": 22,
280+
"nodeType": "UserDefinedTypeName",
281+
"pathNode":
282+
{
283+
"id": 21,
284+
"name": "MyAddress",
285+
"nodeType": "IdentifierPath",
286+
"referencedDeclaration": 18,
287+
"src": "177:9:1"
288+
},
289+
"referencedDeclaration": 18,
290+
"src": "177:9:1",
291+
"typeDescriptions":
292+
{
293+
"typeIdentifier": "t_userDefinedValueType$_MyAddress_$18",
294+
"typeString": "user defined type MyAddress"
295+
}
296+
},
297+
"nodeType": "Mapping",
298+
"src": "169:28:1",
299+
"typeDescriptions":
300+
{
301+
"typeIdentifier": "t_mapping$_t_userDefinedValueType$_MyAddress_$18_$_t_userDefinedValueType$_MyUInt_$20_$",
302+
"typeString": "mapping(user defined type MyAddress => user defined type MyUInt)"
303+
},
304+
"valueType":
305+
{
306+
"id": 24,
307+
"nodeType": "UserDefinedTypeName",
308+
"pathNode":
309+
{
310+
"id": 23,
311+
"name": "MyUInt",
312+
"nodeType": "IdentifierPath",
313+
"referencedDeclaration": 20,
314+
"src": "190:6:1"
315+
},
316+
"referencedDeclaration": 20,
317+
"src": "190:6:1",
318+
"typeDescriptions":
319+
{
320+
"typeIdentifier": "t_userDefinedValueType$_MyUInt_$20",
321+
"typeString": "user defined type MyUInt"
322+
}
323+
}
324+
},
325+
"visibility": "public"
256326
}
257327
],
258-
"scope": 22,
259-
"src": "96:70:1",
328+
"scope": 28,
329+
"src": "96:113:1",
260330
"usedErrors": []
261331
}
262332
],
263-
"src": "0:167:1"
333+
"src": "0:210:1"
264334
}

test/libsolidity/ASTJSON/userDefinedValueType.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function f() {
77
contract C {
88
type MyAddress is address;
99
type MyUInt is uint;
10+
mapping(MyAddress => MyUInt) public m;
1011
}
1112

1213
// ----

test/libsolidity/ASTJSON/userDefinedValueType_parseOnly.json

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"absolutePath": "a",
3-
"id": 22,
3+
"id": 28,
44
"nodeType": "SourceUnit",
55
"nodes":
66
[
@@ -154,7 +154,7 @@
154154
"baseContracts": [],
155155
"contractDependencies": [],
156156
"contractKind": "contract",
157-
"id": 21,
157+
"id": 27,
158158
"name": "C",
159159
"nameLocation": "105:1:1",
160160
"nodeType": "ContractDefinition",
@@ -190,11 +190,59 @@
190190
"src": "159:4:1",
191191
"typeDescriptions": {}
192192
}
193+
},
194+
{
195+
"constant": false,
196+
"id": 26,
197+
"mutability": "mutable",
198+
"name": "m",
199+
"nameLocation": "205:1:1",
200+
"nodeType": "VariableDeclaration",
201+
"src": "169:37:1",
202+
"stateVariable": false,
203+
"storageLocation": "default",
204+
"typeDescriptions": {},
205+
"typeName":
206+
{
207+
"id": 25,
208+
"keyType":
209+
{
210+
"id": 22,
211+
"nodeType": "UserDefinedTypeName",
212+
"pathNode":
213+
{
214+
"id": 21,
215+
"name": "MyAddress",
216+
"nodeType": "IdentifierPath",
217+
"src": "177:9:1"
218+
},
219+
"src": "177:9:1",
220+
"typeDescriptions": {}
221+
},
222+
"nodeType": "Mapping",
223+
"src": "169:28:1",
224+
"typeDescriptions": {},
225+
"valueType":
226+
{
227+
"id": 24,
228+
"nodeType": "UserDefinedTypeName",
229+
"pathNode":
230+
{
231+
"id": 23,
232+
"name": "MyUInt",
233+
"nodeType": "IdentifierPath",
234+
"src": "190:6:1"
235+
},
236+
"src": "190:6:1",
237+
"typeDescriptions": {}
238+
}
239+
},
240+
"visibility": "public"
193241
}
194242
],
195-
"src": "96:70:1",
243+
"src": "96:113:1",
196244
"usedErrors": []
197245
}
198246
],
199-
"src": "0:167:1"
247+
"src": "0:210:1"
200248
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type MyInt is int;
2+
contract C {
3+
mapping(MyInt => int) public m;
4+
function set(MyInt key, int value) external {
5+
m[key] = value;
6+
}
7+
function set_unwrapped(int key, int value) external {
8+
m[MyInt.wrap(key)] = value;
9+
}
10+
}
11+
// ====
12+
// compileViaYul: also
13+
// ----
14+
// set(int256,int256): 1, 1 ->
15+
// m(int256): 1 -> 1
16+
// set_unwrapped(int256,int256): 1, 2 ->
17+
// m(int256): 1 -> 2
18+
// m(int256): 2 -> 0

test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ contract c {
55
mapping(S => uint) data;
66
}
77
// ----
8-
// TypeError 7804: (47-48): Only elementary types, contract types or enums are allowed as mapping keys.
8+
// TypeError 7804: (47-48): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys.

test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ contract c {
55
mapping(S => uint) data;
66
}
77
// ----
8-
// TypeError 7804: (49-50): Only elementary types, contract types or enums are allowed as mapping keys.
8+
// TypeError 7804: (49-50): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys.

test/libsolidity/syntaxTests/types/struct_mapping_recursion.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ contract C {
66
function g (S calldata) external view {}
77
}
88
// ----
9-
// TypeError 7804: (56-57): Only elementary types, contract types or enums are allowed as mapping keys.
9+
// TypeError 7804: (56-57): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type MyInt is int;
2+
contract C {
3+
mapping(MyInt => int) m;
4+
}

0 commit comments

Comments
 (0)