Skip to content

Commit f9e78f2

Browse files
committed
Fix
1 parent 1654767 commit f9e78f2

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

compiler/ml/translcore.ml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,35 @@ let primitives_table =
404404

405405
let find_primitive prim_name = Hashtbl.find primitives_table prim_name
406406

407-
let warn_on_polymorphic_comparison loc = function
407+
let is_null_undefined_constant (exp : Typedtree.expression) =
408+
match exp.exp_desc with
409+
| Texp_ident
410+
( _,
411+
_,
412+
{
413+
val_kind =
414+
Val_prim
415+
{prim_name = "%null" | "%undefined" | "#null" | "#undefined"; _};
416+
_;
417+
} ) ->
418+
true
419+
| _ -> false
420+
421+
let warn_on_polymorphic_comparison loc prim_name args = function
422+
(* Preserve the backend's old exemption for == / != against nullish constants,
423+
which compile to dedicated null/undefined checks rather than object compare. *)
424+
| Pobjcomp (Ceq | Cneq)
425+
when prim_name = "%equal_null"
426+
|| prim_name = "%equal_undefined"
427+
|| prim_name = "%equal_nullable"
428+
|| List.exists is_null_undefined_constant args ->
429+
()
408430
| Pobjcomp _ | Pobjorder | Pobjmin | Pobjmax ->
409431
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison
410432
| _ -> ()
411433

412-
let emit_primitive_warnings loc primitive =
413-
warn_on_polymorphic_comparison loc primitive;
434+
let emit_primitive_warnings loc prim_name args primitive =
435+
warn_on_polymorphic_comparison loc prim_name args primitive;
414436
primitive
415437

416438
let specialize_comparison
@@ -455,7 +477,7 @@ let transl_primitive loc p env ty =
455477
let prim =
456478
(try specialize_primitive p env ty (* ~has_constant_constructor:false *)
457479
with Not_found -> Pccall p)
458-
|> emit_primitive_warnings loc
480+
|> emit_primitive_warnings loc p.prim_name []
459481
in
460482
match prim with
461483
| Ploc kind -> (
@@ -535,7 +557,7 @@ let transl_primitive_application loc prim env ty args =
535557
if String.length prim_name > 0 && prim_name.[0] = '%' then
536558
raise (Error (loc, Unknown_builtin_primitive prim_name));
537559
Pccall prim))
538-
|> emit_primitive_warnings loc
560+
|> emit_primitive_warnings loc prim_name args
539561

540562
(* To propagate structured constants *)
541563

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let undefinedEq = undefined == undefined
2+
let undefinedNeq = undefined != undefined
3+
4+
let nullEq = null == null
5+
let nullNeq = null != null
6+
7+
let nullableEq = Nullable.undefined == Nullable.make(3)
8+
let nullableNeq = Nullable.undefined != Nullable.make(3)

0 commit comments

Comments
 (0)