Skip to content

Commit 8f4f487

Browse files
committed
Fix @warning("-102") not working
1 parent d680426 commit 8f4f487

11 files changed

Lines changed: 60 additions & 21 deletions

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/builtin_attributes.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,20 @@ let rec deprecated_of_str = function
182182
| Some _ as r -> r)
183183
| _ -> None
184184

185-
let warning_attribute ?(ppwarning = true) =
185+
let warning_attribute ?(ppwarning = true) ?(report_attribute_errors = true) =
186186
let process loc txt errflag payload =
187187
match string_of_payload payload with
188188
| Some s -> (
189189
try Warnings.parse_options errflag s
190190
with Arg.Bad _ ->
191-
Location.prerr_warning loc
192-
(Warnings.Attribute_payload (txt, "Ill-formed list of warnings")))
191+
if report_attribute_errors then
192+
Location.prerr_warning loc
193+
(Warnings.Attribute_payload (txt, "Ill-formed list of warnings")))
193194
| None ->
194-
Location.prerr_warning loc
195-
(Warnings.Attribute_payload (txt, "A single string literal is expected"))
195+
if report_attribute_errors then
196+
Location.prerr_warning loc
197+
(Warnings.Attribute_payload
198+
(txt, "A single string literal is expected"))
196199
in
197200
function
198201
| {txt = ("ocaml.warning" | "warning") as txt; loc}, payload ->
@@ -212,10 +215,12 @@ let warning_attribute ?(ppwarning = true) =
212215
Location.prerr_warning pstr_loc (Warnings.Preprocessor s)
213216
| _ -> ()
214217

215-
let warning_scope ?ppwarning attrs f =
218+
let warning_scope ?ppwarning ?report_attribute_errors attrs f =
216219
let prev = Warnings.backup () in
217220
try
218-
List.iter (warning_attribute ?ppwarning) (List.rev attrs);
221+
List.iter
222+
(warning_attribute ?ppwarning ?report_attribute_errors)
223+
(List.rev attrs);
219224
let ret = f () in
220225
Warnings.restore prev;
221226
ret

compiler/ml/builtin_attributes.mli

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,29 @@ val check_duplicated_labels :
6767
(Parsetree.label_declaration list -> string Asttypes.loc option) ref
6868
val error_of_extension : Parsetree.extension -> Location.error
6969

70-
val warning_attribute : ?ppwarning:bool -> Parsetree.attribute -> unit
70+
val warning_attribute :
71+
?ppwarning:bool ->
72+
?report_attribute_errors:bool ->
73+
Parsetree.attribute ->
74+
unit
7175
(** Apply warning settings from the specified attribute.
7276
"ocaml.warning"/"ocaml.warnerror" (and variants without the prefix)
7377
are processed and other attributes are ignored.
7478
7579
Also implement ocaml.ppwarning (unless ~ppwarning:false is
7680
passed).
81+
82+
[report_attribute_errors] only controls whether malformed warning
83+
attributes emit diagnostics; valid warning settings are still applied
84+
regardless.
7785
*)
7886

7987
val warning_scope :
80-
?ppwarning:bool -> Parsetree.attributes -> (unit -> 'a) -> 'a
88+
?ppwarning:bool ->
89+
?report_attribute_errors:bool ->
90+
Parsetree.attributes ->
91+
(unit -> 'a) ->
92+
'a
8193
(** Execute a function in a new scope for warning settings. This
8294
means that the effect of any call to [warning_attribute] during
8395
the execution of this function will be discarded after

compiler/ml/translcore.ml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,15 @@ 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
408+
| Pobjcomp _ | Pobjorder | Pobjmin | Pobjmax ->
409+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison
410+
| _ -> ()
411+
412+
let emit_primitive_warnings loc primitive =
413+
warn_on_polymorphic_comparison loc primitive;
414+
primitive
415+
407416
let specialize_comparison
408417
({objcomp; intcomp; floatcomp; stringcomp; bigintcomp; boolcomp} :
409418
specialized) env ty =
@@ -444,8 +453,9 @@ let specialize_primitive p env ty (* ~has_constant_constructor *) =
444453
let transl_primitive loc p env ty =
445454
(* Printf.eprintf "----transl_primitive %s----\n" p.prim_name; *)
446455
let prim =
447-
try specialize_primitive p env ty (* ~has_constant_constructor:false *)
448-
with Not_found -> Pccall p
456+
(try specialize_primitive p env ty (* ~has_constant_constructor:false *)
457+
with Not_found -> Pccall p)
458+
|> emit_primitive_warnings loc
449459
in
450460
match prim with
451461
| Ploc kind -> (
@@ -492,7 +502,7 @@ let transl_primitive_application loc prim env ty args =
492502
| [arg1] | [arg1; _] -> translate_unified_ops prim env arg1.exp_type
493503
| _ -> None
494504
in
495-
match unified with
505+
(match unified with
496506
| Some primitive -> primitive
497507
| None -> (
498508
try
@@ -524,7 +534,8 @@ let transl_primitive_application loc prim env ty args =
524534
with Not_found ->
525535
if String.length prim_name > 0 && prim_name.[0] = '%' then
526536
raise (Error (loc, Unknown_builtin_primitive prim_name));
527-
Pccall prim)
537+
Pccall prim))
538+
|> emit_primitive_warnings loc
528539

529540
(* To propagate structured constants *)
530541

@@ -653,8 +664,10 @@ let extract_directive_for_fn exp =
653664
else None)
654665

655666
let rec transl_exp e =
656-
List.iter (Translattribute.check_attribute e) e.exp_attributes;
657-
transl_exp0 e
667+
Builtin_attributes.warning_scope ~ppwarning:false
668+
~report_attribute_errors:false e.exp_attributes (fun () ->
669+
List.iter (Translattribute.check_attribute e) e.exp_attributes;
670+
transl_exp0 e)
658671

659672
and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
660673
match e.exp_desc with

compiler/ml/translmod.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,11 @@ and transl_structure loc fields cc rootpath final_env = function
446446
transl_module Tcoerce_none None modl,
447447
body ),
448448
size )
449-
| Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _
450-
| Tstr_attribute _ ->
449+
| Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _ ->
450+
transl_structure loc fields cc rootpath final_env rem
451+
| Tstr_attribute x ->
452+
Builtin_attributes.warning_attribute ~ppwarning:false
453+
~report_attribute_errors:false x;
451454
transl_structure loc fields cc rootpath final_env rem)
452455

453456
(* Update forward declaration in Translcore *)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let x = (a, b) => {
2+
@warning("-102") (a->Pair.second > b->Pair.second)
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@@warning("-102")
2+
3+
let x = (a, b) => a->Pair.second > b->Pair.second

0 commit comments

Comments
 (0)