Skip to content

Commit f5e1ab7

Browse files
committed
Fix warning 102 suppression for polymorphic comparisons
1 parent 973d3d4 commit f5e1ab7

4 files changed

Lines changed: 57 additions & 8 deletions

File tree

compiler/core/lam_compile_primitive.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,23 +389,19 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
389389
|| E.is_null_undefined_constant e2) ->
390390
E.neq_null_undefined_boolean e1 e2
391391
| [e1; e2] ->
392-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison;
393392
E.runtime_call Primitive_modules.object_
394393
(Lam_compile_util.runtime_of_comp cmp)
395394
args
396395
| _ -> assert false)
397396
| Pobjorder -> (
398-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison;
399397
match args with
400398
| [a; b] -> E.runtime_call Primitive_modules.object_ "compare" args
401399
| _ -> assert false)
402400
| Pobjmin -> (
403-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison;
404401
match args with
405402
| [a; b] -> E.runtime_call Primitive_modules.object_ "min" args
406403
| _ -> assert false)
407404
| Pobjmax -> (
408-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison;
409405
match args with
410406
| [a; b] -> E.runtime_call Primitive_modules.object_ "max" args
411407
| _ -> assert false)

compiler/ml/translcore.ml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,19 @@ let specialize_primitive p env ty (* ~has_constant_constructor *) =
439439
| None -> table.objcomp
440440
with Not_found -> find_primitive p.prim_name)
441441

442+
let is_null_undefined_constant = function
443+
| Lprim ((Pnull | Pundefined), [], _) -> true
444+
| _ -> false
445+
446+
let warn_polymorphic_comparison loc prim args =
447+
match (prim, args) with
448+
| Pobjcomp (Ceq | Cneq), [arg1; arg2]
449+
when is_null_undefined_constant arg1 || is_null_undefined_constant arg2 ->
450+
()
451+
| (Pobjcomp _ | Pobjorder | Pobjmin | Pobjmax), _ ->
452+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison
453+
| _ -> ()
454+
442455
(* Eta-expand a primitive *)
443456

444457
let transl_primitive loc p env ty =
@@ -447,6 +460,7 @@ let transl_primitive loc p env ty =
447460
try specialize_primitive p env ty (* ~has_constant_constructor:false *)
448461
with Not_found -> Pccall p
449462
in
463+
warn_polymorphic_comparison loc prim [];
450464
match prim with
451465
| Ploc kind -> (
452466
let lam = lam_of_loc kind loc in
@@ -653,8 +667,9 @@ let extract_directive_for_fn exp =
653667
else None)
654668

655669
let rec transl_exp e =
656-
List.iter (Translattribute.check_attribute e) e.exp_attributes;
657-
transl_exp0 e
670+
Builtin_attributes.warning_scope ~ppwarning:false e.exp_attributes (fun () ->
671+
List.iter (Translattribute.check_attribute e) e.exp_attributes;
672+
transl_exp0 e)
658673

659674
and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
660675
match e.exp_desc with
@@ -734,6 +749,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
734749
let prim =
735750
transl_primitive_application e.exp_loc p e.exp_env prim_type args
736751
in
752+
warn_polymorphic_comparison e.exp_loc prim argl;
737753
match (prim, args) with
738754
| Praise k, [_] ->
739755
let targ = List.hd argl in
@@ -1082,7 +1098,10 @@ and transl_let rec_flag pat_expr_list body =
10821098
let rec transl = function
10831099
| [] -> body
10841100
| {vb_pat = pat; vb_expr = expr; vb_attributes = attr; vb_loc} :: rem ->
1085-
let lam = transl_exp expr in
1101+
let lam =
1102+
Builtin_attributes.warning_scope ~ppwarning:false attr (fun () ->
1103+
transl_exp expr)
1104+
in
10861105
let lam = Translattribute.add_inline_attribute lam vb_loc attr in
10871106
Matching.for_let pat.pat_loc lam pat (transl rem)
10881107
in
@@ -1098,7 +1117,10 @@ and transl_let rec_flag pat_expr_list body =
10981117
Only variables are allowed as left-hand side of `let rec'
10991118
*)
11001119
in
1101-
let lam = transl_exp expr in
1120+
let lam =
1121+
Builtin_attributes.warning_scope ~ppwarning:false vb_attributes
1122+
(fun () -> transl_exp expr)
1123+
in
11021124
let lam = Translattribute.add_inline_attribute lam vb_loc vb_attributes in
11031125
(id, lam)
11041126
in
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Warning number 22
3+
/.../fixtures/warning102.res:1:32-38
4+
5+
1 │ let ppwarningOnce = @ppwarning("hello") 1
6+
2 │
7+
3 │ @warning("-102")
8+
9+
hello
10+
11+
12+
Warning number 102
13+
/.../fixtures/warning102.res:11:13-22
14+
15+
9 │ let comparesToNull = x => x == Nullable.null
16+
10 │
17+
11 │ let warns = [3] == [3]
18+
12 │
19+
20+
Polymorphic comparison introduced (maybe unsafe)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let ppwarningOnce = @ppwarning("hello") 1
2+
3+
@warning("-102")
4+
let suppressedBinding =
5+
[1] == [1]
6+
7+
let suppressedExpression = @warning("-102") ([2] == [2])
8+
9+
let comparesToNull = x => x == Nullable.null
10+
11+
let warns = [3] == [3]

0 commit comments

Comments
 (0)