Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for list emptiness before checking variants #178

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/lib/pythongen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,12 @@ let rec typecheck : type a. a typ -> string -> t list =
variants
in
let check_contents =
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true (List.hd variants_to_check))
(List.tl variants_to_check)
match variants_to_check with
| [] -> []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vincent-lau I think @mseri meant that you didn't need the | [] -> [] if you used | v :: vs -> form

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need both, the second one pattern matches only on nonempty lists

| v :: vs ->
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true v) vs
in
let all_tags = List.map (fun (BoxedTag t) -> t.tname) variants in
let pylist =
Expand Down
49 changes: 48 additions & 1 deletion tests/rpc/test_pythongen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,50 @@ module Interface (R : Idl.RPC) = struct
end

module IfCode = Interface (Codegen.Gen ())
module UnitVInterface (R : Idl.RPC) = struct
open R

type unit_variant =
| Empty
| Hollow
| Vacant
| Void
[@@deriving rpcty]

let unit_variant_p = Idl.Param.mk ~name:"unit_variant" unit_variant
let int_p = Idl.Param.mk Rpc.Types.int

let discard_v =
R.declare
"discard_v"
[ "constant function taking a unit variant and discards it by returning an integer"
]
(unit_variant_p @-> returning int_p Idl.DefaultError.err)

let implementation =
implement
{ Idl.Interface.name = "UnitVInterface"
; namespace = Some "UnitVInterface"
; description =
[ "Unit variant interface which does absolutely nothing. Only used to test \
whether the pythongen code can handle variants with zero argument \
constructors."
]
; version = 1, 0, 0
}
end

module UnitVCode : sig
val implementation : unit -> Codegen.Interface.t
end =
UnitVInterface (Codegen.Gen ())

let unitv_interface =
Codegen.Interfaces.create
~name:"unitv"
~title:"Unit Variant"
~description:[ "Interface for Unit variant" ]
~interfaces:[ UnitVCode.implementation () ]

let interfaces =
Codegen.Interfaces.create
Expand Down Expand Up @@ -146,12 +190,15 @@ let check_exceptions () =
gen_python_bindings "python/bindings.py";
run_cmd "Exceptions should be correctly generated" "python python/exn_test.py"

let check_unit_variants () =
Pythongen.of_interfaces interfaces |> Pythongen.string_of_ts |> ignore

let tests =
[ ( "Check generated test interface bindings with pylint & pycodestyle"
, `Slow
, lint_bindings )
; "Check generated commandline bindings", `Slow, test_commandline
; "Check generated test class with commandline bindings", `Slow, check_test_class
; "Cehck generated exceptions", `Slow, check_exceptions
; "Check generated exceptions", `Slow, check_exceptions
; "Check python generation on variants with zero-arg constructors", `Quick, check_unit_variants
]
Loading