Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit bc7c940

Browse files
jjcnnjubnzv
andcommitted
Disallow duplicate fields in address types (#1208)
* Disallow duplicate fields in address types * feat(test): Add test for duplicate field in address type * Rename testcase to make it clearer what it tests Co-authored-by: Georgiy Komarov <[email protected]>
1 parent 7553ad7 commit bc7c940

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/base/ScillaParser.mly

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,12 @@ address_typ :
251251
| d = CID; WITH; CONTRACT; fs = separated_list(COMMA, address_type_field); END;
252252
{ if d = "ByStr20"
253253
then
254-
(* Add _this_address : ByStr20 to field list. This ensures the type is treated as a contract address *)
255254
let fs' = List.fold_left (fun acc (id, t) ->
256-
SType.IdLoc_Comp.Map.set acc ~key:id ~data:t) SType.IdLoc_Comp.Map.empty fs
255+
match SType.IdLoc_Comp.Map.add acc ~key:id ~data:t with
256+
| `Ok new_map -> new_map
257+
| `Duplicate ->
258+
raise (SyntaxError (Printf.sprintf "Duplicate field name %s in address type" (ParserIdentifier.as_string id), toLoc $startpos(d))))
259+
SType.IdLoc_Comp.Map.empty fs
257260
in
258261
Address (ContrAddr fs')
259262
else raise (SyntaxError ("Invalid type", toLoc $startpos(d))) }

tests/base/parser/bad/Bad.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module Tests = Scilla_test.Util.DiffBasedTests (struct
3434

3535
let tests =
3636
[
37+
"address_duplicate_field.scilla";
38+
"address_spid_as_field.scilla";
3739
"bad_cast_2.scilla";
3840
"bad_cast_3.scilla";
3941
"bad_map_key_2.scilla";
@@ -69,6 +71,7 @@ module Tests = Scilla_test.Util.DiffBasedTests (struct
6971
"lmodule-import-cid-as-cid-with.scilla";
7072
"lmodule-import-cid-as-with.scilla";
7173
"lmodule-import-cid-with.scilla";
74+
"remote_read_namespace.scilla";
7275
"stmts_t-accept-with.scilla";
7376
"stmts_t-cid-with.scilla";
7477
"stmts_t-delete-id-with.scilla";
@@ -136,8 +139,6 @@ module Tests = Scilla_test.Util.DiffBasedTests (struct
136139
"type_t-map-with.scilla";
137140
"type_t-tid-arrow-tid-with.scilla";
138141
"type_t-tid-arrow-with.scilla";
139-
"address_spid_as_field.scilla";
140-
"remote_read_namespace.scilla";
141142
]
142143

143144
let exit_code : UnixLabels.process_status = WEXITED 1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
scilla_version 0
2+
3+
contract DuplicateField ()
4+
5+
procedure test
6+
(
7+
a :
8+
ByStr20 with contract
9+
field a : ByStr20 with end,
10+
field b : Map (ByStr20 with end) Bool,
11+
field b : Map (ByStr20 with end) Bool
12+
end
13+
)
14+
15+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"errors": [
3+
{
4+
"error_message": "Syntax error: Duplicate field name b in address type",
5+
"start_location": {
6+
"file": "base/parser/bad/address_duplicate_field.scilla",
7+
"line": 8,
8+
"column": 7
9+
},
10+
"end_location": { "file": "", "line": 0, "column": 0 }
11+
}
12+
],
13+
"warnings": []
14+
}

0 commit comments

Comments
 (0)