Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ccasin committed Nov 14, 2024
1 parent ec52f14 commit caec0ef
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
74 changes: 74 additions & 0 deletions testsuite/tests/typing-layouts/omitted_arguments.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
(* TEST
reference = "${test_source_directory}/omitted_arguments.reference";
include stdlib_upstream_compatible;
flambda2;
{
native;
} {
flags = "-Oclassic";
native;
} {
flags = "-O3";
native;
} {
bytecode;
}
*)

(* This is a test for applications where some arguments are omitted. We
previously had a bug where the layouts recorded in the eta-expansion the
compiler generates for such functions were incorrect. *)

external box_float : float# -> float = "%box_float"
external unbox_float : float -> float# = "%unbox_float"

(* Omitting named arg *)
let f1 ~name:x y = unbox_float (x +. y)

let f2 (f : name:float -> float#) = f ~name:3.14

let f3 () = f2 (f1 3.15)

let _ =
Printf.printf "Omitting named arg (6.29): %.2f\n" (box_float (f3 ()))

(* Omitting unnamed arg *)
let f4 x ~name:y = unbox_float (x +. y)

let f5 (f : float -> float#) = f 3.14

let f6 () = f5 (f4 ~name:3.13)

let _ =
Printf.printf "Omitting named arg (6.27): %.2f\n" (box_float (f6 ()))

(* Omitting two named args*)
let f7 ~name1 ~name2 x = unbox_float (name1 +. name2 +. x)

let f8 (f : name1:float -> name2:float -> float#) = f ~name1:3.13 ~name2:3.15

let f9 () = f8 (f7 3.14)

let _ =
Printf.printf "Omitting named arg (9.42): %.2f\n" (box_float (f9 ()))

(* Example adapted from real code *)

let float_test : float# -> bool =
fun f -> Stdlib_upstream_compatible.Float_u.(equal f #3.0)
let e : int -> size:int -> float# =
fun s ~size -> unbox_float (Float.of_int s +. Float.of_int size)

let evaluate_1 () : 'a Array.t -> eval:local_ ('a -> float#) -> float# =
fun ts ~(local_ eval) ->
let eval i = eval (Array.get ts i) in
if float_test (eval 0) then #3.14 else #10.0

let evaluate =
let evaluate_2 = evaluate_1 () in
fun t ~size ->
evaluate_2 t ~eval:(e ~size)

let _ =
Printf.printf "\"real\" code (3.14): %.2f\n"
(box_float (evaluate [|2; 3; 4|] ~size:1))
4 changes: 4 additions & 0 deletions testsuite/tests/typing-layouts/omitted_arguments.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Omitting named arg (6.29): 6.29
Omitting named arg (6.27): 6.27
Omitting named arg (9.42): 9.42
"real" code (3.14): 3.14

0 comments on commit caec0ef

Please sign in to comment.