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

Yielding mode axis #3283

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
<def_rec>
pattern (test_locations.ml[17,534+8]..test_locations.ml[17,534+11])
Tpat_var "fib"
value_mode global,many,nonportable;join(aliased,contended)(modevar#1[aliased,uncontended .. unique,uncontended])
value_mode global,many,nonportable,unyielding;join(aliased,contended)(modevar#1[aliased,uncontended .. unique,uncontended])
expression (test_locations.ml[17,534+14]..test_locations.ml[19,572+34])
Texp_function
alloc_mode global,many,nonportable;id(modevar#7[aliased,contended .. unique,uncontended])
alloc_mode global,many,nonportable,unyielding;id(modevar#7[aliased,contended .. unique,uncontended])
[]
Tfunction_cases (test_locations.ml[17,534+14]..test_locations.ml[19,572+34])
alloc_mode global,many,nonportable;aliased,uncontended
alloc_mode global,many,nonportable,unyielding;aliased,uncontended
value
[
<case>
Expand All @@ -110,7 +110,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
<case>
pattern (test_locations.ml[19,572+4]..test_locations.ml[19,572+5])
Tpat_var "n"
value_mode global,many,portable;unique,uncontended
value_mode global,many,portable,unyielding;unique,uncontended
expression (test_locations.ml[19,572+9]..test_locations.ml[19,572+34])
Texp_apply
apply_mode Tail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
<def_rec>
pattern
Tpat_var "fib"
value_mode global,many,nonportable;join(aliased,contended)(modevar#1[aliased,uncontended .. unique,uncontended])
value_mode global,many,nonportable,unyielding;join(aliased,contended)(modevar#1[aliased,uncontended .. unique,uncontended])
expression
Texp_function
alloc_mode global,many,nonportable;id(modevar#7[aliased,contended .. unique,uncontended])
alloc_mode global,many,nonportable,unyielding;id(modevar#7[aliased,contended .. unique,uncontended])
[]
Tfunction_cases
alloc_mode global,many,nonportable;aliased,uncontended
alloc_mode global,many,nonportable,unyielding;aliased,uncontended
value
[
<case>
Expand All @@ -110,7 +110,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
<case>
pattern
Tpat_var "n"
value_mode global,many,portable;unique,uncontended
value_mode global,many,portable,unyielding;unique,uncontended
expression
Texp_apply
apply_mode Tail
Expand Down
8 changes: 4 additions & 4 deletions testsuite/tests/typing-layouts/jkinds.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ Error: Layout void is more experimental than allowed by the enabled layouts exte
|}]

type a : immediate
type b : value mod global unique many uncontended portable external_ = a
type c : value mod global unique many uncontended portable external_
type b : value mod global unique many uncontended portable unyielding external_ = a
type c : value mod global unique many uncontended portable unyielding external_
type d : immediate = c
[%%expect{|
type a : immediate
Expand All @@ -290,8 +290,8 @@ type d = c
|}]

type a : immediate64
type b : value mod global unique many uncontended portable external64 = a
type c : value mod global unique many uncontended portable external64
type b : value mod global unique many uncontended portable unyielding external64 = a
type c : value mod global unique many uncontended portable unyielding external64
type d : immediate64 = c
[%%expect{|
type a : immediate64
Expand Down
80 changes: 80 additions & 0 deletions testsuite/tests/typing-modes/yielding.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(* TEST
expect;
*)

(* CR dkalinichenko: allow [yielding] at toplevel? *)
dkalinichenko-js marked this conversation as resolved.
Show resolved Hide resolved
let my_effect : unit -> unit @@ yielding = print_endline "Hello, world!"
[%%expect{|
Line 1, characters 4-72:
1 | let my_effect : unit -> unit @@ yielding = print_endline "Hello, world!"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This value is "yielding" but expected to be "unyielding".
|}]

let storage = ref ""

let with_effect : ((string -> unit) @ local yielding -> 'a) -> 'a =
fun f -> f ((:=) storage)

[%%expect{|
val storage : string ref = {contents = ""}
val with_effect : (local_ (string -> unit) @ yielding -> 'a) -> 'a = <fun>
|}]

let () = with_effect (fun k -> k "Hello, world!")

let _ = !storage

[%%expect{|
- : string = "Hello, world!"
|}]

let run_yielding : (string -> unit) @ local yielding -> unit = fun f -> f "my string"

let () = with_effect (fun k -> run_yielding k)

let _ = !storage

[%%expect{|
val run_yielding : local_ (string -> unit) @ yielding -> unit = <fun>
- : string = "my string"
|}]

let run_unyielding : (string -> unit) @ local unyielding -> unit = fun f -> f "another string"

let () = with_effect (fun k -> run_unyielding k)

[%%expect{|
val run_unyielding : local_ (string -> unit) -> unit = <fun>
Line 3, characters 46-47:
3 | let () = with_effect (fun k -> run_unyielding k)
^
Error: This value is "yielding" but expected to be "unyielding".
|}]

(* CR dkalinichenko: default [local] arguments to [yielding]. *)

let run_default : (string -> unit) @ local -> unit = fun f -> f "some string"

let () = with_effect (fun k -> run_default k)

[%%expect{|
val run_default : local_ (string -> unit) -> unit = <fun>
Line 3, characters 43-44:
3 | let () = with_effect (fun k -> run_default k)
^
Error: This value is "yielding" but expected to be "unyielding".
|}]

(* A closure over a [yielding] value must be [yielding]. *)

let () = with_effect (fun k ->
let closure @ local unyielding = fun () -> k () in
run_unyielding k)

[%%expect{|
Line 2, characters 45-46:
2 | let closure @ local unyielding = fun () -> k () in
^
Error: The value "k" is yielding, so cannot be used inside a function that may not yield.
|}]
1 change: 1 addition & 0 deletions typing/env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4419,6 +4419,7 @@ let report_lookup_error _loc env ppf = function
| Error (Areality, _) -> "local", "might escape"
| Error (Linearity, _) -> "once", "is many"
| Error (Portability, _) -> "nonportable", "is portable"
| Error (Yielding, _) -> "yielding", "may not yield"
in
let s, hint =
match context with
Expand Down
12 changes: 8 additions & 4 deletions typing/jkind.ml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ module Const = struct
contention = Contention.Const.min;
portability = Portability.Const.min;
uniqueness = Uniqueness.Const.max;
areality = Locality.Const.max
areality = Locality.Const.max;
yielding = Yielding.Const.min
};
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.Non_null
Expand All @@ -452,7 +453,8 @@ module Const = struct
contention = Contention.Const.max;
portability = Portability.Const.min;
uniqueness = Uniqueness.Const.max;
areality = Locality.Const.max
areality = Locality.Const.max;
yielding = Yielding.Const.min
};
externality_upper_bound = Externality.max;
nullability_upper_bound = Nullability.Non_null
Expand Down Expand Up @@ -774,7 +776,8 @@ module Const = struct
linearity = parsed_modifiers.linearity;
uniqueness = parsed_modifiers.uniqueness;
portability = parsed_modifiers.portability;
contention = parsed_modifiers.contention
contention = parsed_modifiers.contention;
yielding = parsed_modifiers.yielding
}
in
{ layout = base.layout;
Expand Down Expand Up @@ -1165,7 +1168,8 @@ let for_arrow =
areality = Locality.Const.max;
uniqueness = Uniqueness.Const.min;
portability = Portability.Const.max;
contention = Contention.Const.min
contention = Contention.Const.min;
yielding = Yielding.Const.max
};
externality_upper_bound = Externality.max;
nullability_upper_bound = Non_null
Expand Down
9 changes: 9 additions & 0 deletions typing/jkind_axis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ module Axis = struct
| Uniqueness : Mode.Uniqueness.Const.t t
| Portability : Mode.Portability.Const.t t
| Contention : Mode.Contention.Const.t t
| Yielding : Mode.Yielding.Const.t t
end

module Nonmodal = struct
Expand Down Expand Up @@ -180,6 +181,8 @@ module Axis = struct
(module Accent_lattice (Mode.Portability.Const) : Axis_s with type t = a)
| Modal Contention ->
(module Accent_lattice (Mode.Contention.Const) : Axis_s with type t = a)
| Modal Yielding ->
(module Accent_lattice (Mode.Yielding.Const) : Axis_s with type t = a)
| Nonmodal Externality -> (module Externality : Axis_s with type t = a)
| Nonmodal Nullability -> (module Nullability : Axis_s with type t = a)

Expand All @@ -189,6 +192,7 @@ module Axis = struct
Pack (Modal Uniqueness);
Pack (Modal Portability);
Pack (Modal Contention);
Pack (Modal Yielding);
Pack (Nonmodal Externality);
Pack (Nonmodal Nullability) ]

Expand All @@ -198,6 +202,7 @@ module Axis = struct
| Modal Uniqueness -> "uniqueness"
| Modal Portability -> "portability"
| Modal Contention -> "contention"
| Modal Yielding -> "yielding"
| Nonmodal Externality -> "externality"
| Nonmodal Nullability -> "nullability"
end
Expand All @@ -210,6 +215,7 @@ module Axis_collection (T : Misc.T1) = struct
uniqueness : Mode.Uniqueness.Const.t T.t;
portability : Mode.Portability.Const.t T.t;
contention : Mode.Contention.Const.t T.t;
yielding : Mode.Yielding.Const.t T.t;
externality : Externality.t T.t;
nullability : Nullability.t T.t
}
Expand All @@ -221,6 +227,7 @@ module Axis_collection (T : Misc.T1) = struct
| Modal Uniqueness -> values.uniqueness
| Modal Portability -> values.portability
| Modal Contention -> values.contention
| Modal Yielding -> values.yielding
| Nonmodal Externality -> values.externality
| Nonmodal Nullability -> values.nullability

Expand All @@ -231,6 +238,7 @@ module Axis_collection (T : Misc.T1) = struct
| Modal Uniqueness -> { values with uniqueness = value }
| Modal Portability -> { values with portability = value }
| Modal Contention -> { values with contention = value }
| Modal Yielding -> { values with yielding = value }
| Nonmodal Externality -> { values with externality = value }
| Nonmodal Nullability -> { values with nullability = value }

Expand All @@ -246,6 +254,7 @@ module Axis_collection (T : Misc.T1) = struct
uniqueness = f ~axis:Axis.(Modal Uniqueness);
portability = f ~axis:Axis.(Modal Portability);
contention = f ~axis:Axis.(Modal Contention);
yielding = f ~axis:Axis.(Modal Yielding);
externality = f ~axis:Axis.(Nonmodal Externality);
nullability = f ~axis:Axis.(Nonmodal Nullability)
}
Expand Down
2 changes: 2 additions & 0 deletions typing/jkind_axis.mli
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Axis : sig
| Uniqueness : Mode.Uniqueness.Const.t t
| Portability : Mode.Portability.Const.t t
| Contention : Mode.Contention.Const.t t
| Yielding : Mode.Yielding.Const.t t
end

module Nonmodal : sig
Expand Down Expand Up @@ -98,6 +99,7 @@ module Axis_collection (T : Misc.T1) : sig
uniqueness : Mode.Uniqueness.Const.t T.t;
portability : Mode.Portability.Const.t T.t;
contention : Mode.Contention.Const.t T.t;
yielding : Mode.Yielding.Const.t T.t;
externality : Externality.t T.t;
nullability : Nullability.t T.t
}
Expand Down
Loading
Loading