@@ -404,13 +404,35 @@ let primitives_table =
404404
405405let 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
416438let 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
0 commit comments