Skip to content

Commit

Permalink
Unboxed records may be null (#2648)
Browse files Browse the repository at this point in the history
* Tests

* Fix incorrect assumption about records

---------

Co-authored-by: Diana Kalinichenko <[email protected]>
  • Loading branch information
dkalinichenko-js and d-kalinichenko authored Jun 3, 2024
1 parent 50d00b6 commit 771f0bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
44 changes: 44 additions & 0 deletions ocaml/testsuite/tests/typing-layouts-non-null-value/arguments.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,47 @@ let _ = my_id4 (Fake_or_null.some 4)
- : int Fake_or_null.t = <abstr>
- : int Fake_or_null.t = <abstr>
|}]

(* Check behavior of type arguments and unboxed annotations. *)

module M2 : sig
type 'a t = { v : 'a } [@@unboxed]

val box : 'a -> 'a t
val unbox : 'a t -> 'a
end = struct
type 'a t = { v : 'a } [@@unboxed]

let box v = { v }
let unbox { v } = v
end

[%%expect{|
module M2 :
sig
type 'a t = { v : 'a; } [@@unboxed]
val box : 'a -> 'a t
val unbox : 'a t -> 'a
end
|}]

module M3 : sig
type 'a t = V of 'a [@@unboxed]

val box : 'a -> 'a t
val unbox : 'a t -> 'a
end = struct
type 'a t = V of 'a [@@unboxed]

let box v = V v
let unbox (V v) = v
end

[%%expect{|
module M3 :
sig
type 'a t = V of 'a [@@unboxed]
val box : 'a -> 'a t
val unbox : 'a t -> 'a
end
|}]
12 changes: 6 additions & 6 deletions ocaml/testsuite/tests/typing-layouts/basics_alpha.ml
Original file line number Diff line number Diff line change
Expand Up @@ -693,15 +693,15 @@ module M9_4 = struct
| ({vur_void = _},i) -> i
end;;
[%%expect {|
Line 4, characters 8-16:
Line 4, characters 7-21:
4 | | ({vur_void = _},i) -> i
^^^^^^^^
Error: The record field vur_void belongs to the type void_unboxed_record
but is mixed here with fields of type ('a : non_null_value)
^^^^^^^^^^^^^^
Error: This pattern matches values of type void_unboxed_record
but a pattern was expected which matches values of type ('a : value)
The layout of void_unboxed_record is void, because
of the definition of t_void at line 6, characters 0-19.
But the layout of void_unboxed_record must be a sublayout of non_null_value, because
it's a boxed record type.
But the layout of void_unboxed_record must be a sublayout of value, because
it's the type of a tuple element.
|}];;

module M9_5 = struct
Expand Down
4 changes: 3 additions & 1 deletion ocaml/typing/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,9 @@ and type_pat_aux
let ty = generic_instance expected_ty in
Some (p0, p, is_principal expected_ty), ty
| Maybe_a_record_type ->
None, newvar (Jkind.non_null_value ~why:Boxed_record)
(* We can't assume that the jkind of a record type is [non_null_value]
because of unboxed records. *)
None, newvar (Jkind.any ~why:Dummy_jkind)
| Not_a_record_type ->
let error = Wrong_expected_kind(Record, Pattern, expected_ty) in
raise (Error (loc, !env, error))
Expand Down

0 comments on commit 771f0bc

Please sign in to comment.