Skip to content

Commit 02d144c

Browse files
cknittclaude
andcommitted
Share which literal passes which payload check
Code generation and constant folding each had a table of the literals a payload case accepts, and they disagreed on null and an object case. Variant_runtime.literal_passes is now the one table; folding follows the generated code, where `typeof null` is "object", so null without a null case folds into the object case as it is matched at run time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent c68ef9a commit 02d144c

5 files changed

Lines changed: 35 additions & 22 deletions

File tree

‎compiler/core/lam_compile.ml‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -792,23 +792,9 @@ let compile output_prefix =
792792
(* A tagged block, an object *)
793793
Some Variant_runtime.ObjectType)
794794
in
795-
let reaches_a_payload_arm (literal : Variant_runtime.literal_tag)
796-
=
795+
let reaches_a_payload_arm literal =
797796
List.exists
798-
(fun (block_type : Variant_runtime.block_type) ->
799-
match (block_type, literal) with
800-
| UnknownType, _
801-
| StringType, String _
802-
| NumberType, (Int _ | Float _)
803-
| BigintType, BigInt _
804-
| BooleanType, Bool _
805-
| ObjectType, Null ->
806-
true
807-
| ( ( StringType | NumberType | BigintType | BooleanType
808-
| ObjectType | InstanceType _ | SymbolType
809-
| FunctionType ),
810-
_ ) ->
811-
false)
797+
(Variant_runtime.literal_passes literal)
812798
payload_arms
813799
in
814800
let has_class_case =

‎compiler/ml/lambda.ml‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -846,19 +846,18 @@ let runtime_value_kind (c : structured_constant) =
846846
Integer tags are emitted through [Int32.of_int], just like [Const_int]. *)
847847
let literal_denotes_same = Variant_runtime.same_literal_value
848848

849-
(* Mirror [Dynamic_checks]: literals take precedence; the object case excludes
850-
arrays when an array case exists. Int and float share one runtime type. *)
849+
(* Whether the payload dispatch sends a value of this kind to a case of this
850+
block type. Literals are checked as the generated code checks them; the
851+
object case excludes arrays when an array case exists. *)
851852
let value_has_block_type ~block_types (kind : value_kind)
852853
(block_type : Variant_runtime.block_type) =
853854
match (block_type, kind) with
854-
| NumberType, Is_literal (Int _ | Float _) -> true
855-
| StringType, Is_literal (String _) -> true
856-
| BooleanType, Is_literal (Bool _) -> true
855+
| _, Is_literal literal -> Variant_runtime.literal_passes literal block_type
857856
| ObjectType, Is_object -> true
858857
| InstanceType Array, Is_array -> true
859858
| ObjectType, Is_array ->
860859
not (List.mem (Variant_runtime.InstanceType Array) block_types)
861-
| UnknownType, (Is_literal _ | Is_object | Is_array) -> true
860+
| UnknownType, (Is_object | Is_array) -> true
862861
| UnknownType, Unknown_value -> false
863862
| ( ( NumberType | StringType | BooleanType | ObjectType | BigintType
864863
| SymbolType | FunctionType | InstanceType _ ),

‎compiler/ml/variant_runtime.ml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ let same_literal_value a b = literal_value_key a = literal_value_key b
9494
(** Information used to recognize a constructor during matching. A literal
9595
identifies a constant or an object's tag; a payload shape identifies a
9696
value represented directly by its payload. *)
97+
let literal_passes (literal : literal_tag) (block_type : block_type) =
98+
match (block_type, literal) with
99+
| UnknownType, _
100+
| StringType, String _
101+
| NumberType, (Int _ | Float _)
102+
| BigintType, BigInt _
103+
| BooleanType, Bool _
104+
| ObjectType, Null ->
105+
true
106+
| ( ( StringType | NumberType | BigintType | BooleanType | ObjectType
107+
| InstanceType _ | SymbolType | FunctionType ),
108+
_ ) ->
109+
false
110+
97111
type tag_type = Literal of literal_tag | Payload_shape of block_type
98112

99113
type tag = {name: string; literal: literal_tag}

‎compiler/ml/variant_runtime.mli‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ val literal_value_key : literal_tag -> string
6161

6262
val same_literal_value : literal_tag -> literal_tag -> bool
6363

64+
val literal_passes : literal_tag -> block_type -> bool
65+
(** Whether a literal passes the check of a payload case of this kind, as
66+
[typeof] sees it: ["a"] a string case, [null] an object case, anything an
67+
unknown case, which is not checked *)
68+
6469
(** Information used to recognize a constructor during matching. A literal
6570
identifies a constant or an object's tag; a payload shape identifies a
6671
value represented directly by its payload. *)

‎tests/ounit_tests/ounit_lambda_constant_tests.ml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ let switch_tests =
139139
( Lambda.blk_record [|("x", false)|] Asttypes.Immutable,
140140
[Const_int 1l] ))
141141
"object" );
142+
( "null without a null case is an object" >:: fun _ ->
143+
let record = block "Record" ObjectType in
144+
let text = block "Text" StringType in
145+
let sw =
146+
variant_switch ~constructors:[record; text]
147+
~cases:[(record, "record"); (text, "text")]
148+
~default:None
149+
in
150+
assert_fold sw Const_js_null "record" );
142151
( "empty list is zero at runtime" >:: fun _ ->
143152
let zero = literal "Zero" (Int 0) in
144153
let values = block "Values" UnknownType in

0 commit comments

Comments
 (0)