Skip to content

Commit

Permalink
Disallow duplicate fields in address types (#1208)
Browse files Browse the repository at this point in the history
* 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 <georgiy@zilliqa.com>
  • Loading branch information
jjcnn and jubnzv committed Feb 1, 2023
1 parent 7553ad7 commit bc7c940
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/base/ScillaParser.mly
Expand Up @@ -251,9 +251,12 @@ address_typ :
| d = CID; WITH; CONTRACT; fs = separated_list(COMMA, address_type_field); END;
{ if d = "ByStr20"
then
(* Add _this_address : ByStr20 to field list. This ensures the type is treated as a contract address *)
let fs' = List.fold_left (fun acc (id, t) ->
SType.IdLoc_Comp.Map.set acc ~key:id ~data:t) SType.IdLoc_Comp.Map.empty fs
match SType.IdLoc_Comp.Map.add acc ~key:id ~data:t with
| `Ok new_map -> new_map
| `Duplicate ->
raise (SyntaxError (Printf.sprintf "Duplicate field name %s in address type" (ParserIdentifier.as_string id), toLoc $startpos(d))))
SType.IdLoc_Comp.Map.empty fs
in
Address (ContrAddr fs')
else raise (SyntaxError ("Invalid type", toLoc $startpos(d))) }
Expand Down
5 changes: 3 additions & 2 deletions tests/base/parser/bad/Bad.ml
Expand Up @@ -34,6 +34,8 @@ module Tests = Scilla_test.Util.DiffBasedTests (struct

let tests =
[
"address_duplicate_field.scilla";
"address_spid_as_field.scilla";
"bad_cast_2.scilla";
"bad_cast_3.scilla";
"bad_map_key_2.scilla";
Expand Down Expand Up @@ -69,6 +71,7 @@ module Tests = Scilla_test.Util.DiffBasedTests (struct
"lmodule-import-cid-as-cid-with.scilla";
"lmodule-import-cid-as-with.scilla";
"lmodule-import-cid-with.scilla";
"remote_read_namespace.scilla";
"stmts_t-accept-with.scilla";
"stmts_t-cid-with.scilla";
"stmts_t-delete-id-with.scilla";
Expand Down Expand Up @@ -136,8 +139,6 @@ module Tests = Scilla_test.Util.DiffBasedTests (struct
"type_t-map-with.scilla";
"type_t-tid-arrow-tid-with.scilla";
"type_t-tid-arrow-with.scilla";
"address_spid_as_field.scilla";
"remote_read_namespace.scilla";
]

let exit_code : UnixLabels.process_status = WEXITED 1
Expand Down
15 changes: 15 additions & 0 deletions tests/base/parser/bad/address_duplicate_field.scilla
@@ -0,0 +1,15 @@
scilla_version 0

contract DuplicateField ()

procedure test
(
a :
ByStr20 with contract
field a : ByStr20 with end,
field b : Map (ByStr20 with end) Bool,
field b : Map (ByStr20 with end) Bool
end
)

end
14 changes: 14 additions & 0 deletions tests/base/parser/bad/gold/address_duplicate_field.scilla.gold
@@ -0,0 +1,14 @@
{
"errors": [
{
"error_message": "Syntax error: Duplicate field name b in address type",
"start_location": {
"file": "base/parser/bad/address_duplicate_field.scilla",
"line": 8,
"column": 7
},
"end_location": { "file": "", "line": 0, "column": 0 }
}
],
"warnings": []
}

0 comments on commit bc7c940

Please sign in to comment.