Skip to content

Commit f71f99a

Browse files
cknittclaude
andcommitted
Fix @warning reported several times and ignored in translation
Why: Builtin_attributes.warning_attribute both applied a warning setting and reported problems with it. The type checker enters a binding's warning scope once for each of its passes, so a malformed payload was reported up to six times. The ~ppwarning:false flag of the repeated scopes only skipped @ppwarning. The frontend also applied and reported the leading @@warning items, and left its warning state in place for the type checker and translation. Translation to Lambda reports some warnings itself, such as 102 for a polymorphic comparison. It applied the warning attributes of let bindings, expressions and module expressions again, but not those of module bindings, recursive modules, includes, type constraints and floating @@warning items. So `@warning("-102")` on a module did not silence 102 inside it, and in a build `@@warning("+102")` never enabled it. Builtin_attributes now has two kinds of scope. warning_scope applies and reports, and only the type checker's first pass over a node uses it. replay_warning_scope applies silently, for the type checker's repeated passes, the frontend, translation and Inline_not_stored. The ~ppwarning flag is gone. The frontend runs the built-in PPX in a replay_warning_scope over the leading floating attributes, so type checking starts from the command line's warning state, in one-step compiles as in builds. Translmod applies a floating attribute in a scope around the rest of its structure. It translates the rest of a structure before the item itself, so the scope ends before the items above the attribute are translated. Module bindings, recursive modules and includes get a scope of their attributes. Translcore.transl_exp also applies the attributes of type constraints, which a new Typedtree.exp_warning_attributes collects in scope order, as typemod already did for warning 113. Diagnostic modified (malformed @warning payload): Example: @warning(3) let x = 1 Before (diagnostic): Warning number 47 (reported 6 times) illegal payload for attribute @warning. A single string literal is expected After (diagnostic): Warning number 47 (reported once) illegal payload for attribute @warning. A single string literal is expected Diagnostic modified (warning 102 in a module with @warning("-102")): Example: @warning("-102") module M = { let compares = [1] == [2] } Before (diagnostic): Warning number 102 Polymorphic comparison introduced (maybe unsafe) After (diagnostic): (nothing reported) Fixes #9488 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 738c317 commit f71f99a

19 files changed

Lines changed: 277 additions & 91 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Report a malformed `@warning` or `@warnerror` payload and `@@ppwarning` only once.
2+
- Apply `@warning` and `@@warning` to warnings reported during translation, such as warning 102, wherever they apply during type checking.

‎compiler/frontend/bs_ast_invariant.ml‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,19 @@ type iterator = Ast_iterator.iterator
2626

2727
let super = Ast_iterator.default_iterator
2828

29-
let rec iter_warnings_on_stru (stru : Parsetree.structure) =
29+
(* The floating attributes at the start of a structure or signature, which
30+
the built-in PPX respects, in source order *)
31+
let rec leading_attributes_of_structure (stru : Parsetree.structure) =
3032
match stru with
31-
| [] -> ()
32-
| head :: rest -> (
33-
match head.pstr_desc with
34-
| Pstr_attribute attr ->
35-
Builtin_attributes.warning_attribute attr;
36-
iter_warnings_on_stru rest
37-
| _ -> ())
33+
| {pstr_desc = Pstr_attribute attr} :: rest ->
34+
attr :: leading_attributes_of_structure rest
35+
| _ -> []
3836

39-
let rec iter_warnings_on_sigi (stru : Parsetree.signature) =
40-
match stru with
41-
| [] -> ()
42-
| head :: rest -> (
43-
match head.psig_desc with
44-
| Psig_attribute attr ->
45-
Builtin_attributes.warning_attribute attr;
46-
iter_warnings_on_sigi rest
47-
| _ -> ())
37+
let rec leading_attributes_of_signature (sigi : Parsetree.signature) =
38+
match sigi with
39+
| {psig_desc = Psig_attribute attr} :: rest ->
40+
attr :: leading_attributes_of_signature rest
41+
| _ -> []
4842

4943
let emit_external_warnings_on_structure (stru : Parsetree.structure) =
5044
Ast_checks.iterator.structure Ast_checks.iterator stru

‎compiler/frontend/bs_ast_invariant.mli‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424

2525
type iterator = Ast_iterator.iterator
2626

27-
val iter_warnings_on_stru : Parsetree.structure -> unit
27+
val leading_attributes_of_structure :
28+
Parsetree.structure -> Parsetree.attributes
29+
(** The floating attributes at the start of a structure, in source order *)
2830

29-
val iter_warnings_on_sigi : Parsetree.signature -> unit
31+
val leading_attributes_of_signature :
32+
Parsetree.signature -> Parsetree.attributes
33+
(** The floating attributes at the start of a signature, in source order *)
3034

3135
(** Run [Ast_checks.iterator] over a file after its PPXes. It skips FFI
3236
externals, which the type checker checks in their resolved form. *)

‎compiler/frontend/ppx_entry.ml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@
2424

2525
let unsafe_mapper = Bs_builtin_ppx.mapper
2626

27+
(* The built-in PPX runs in the scope of the leading [@@warning] items. The
28+
type checker applies and reports them, so here they are only applied, and
29+
the scope ends before type checking. [replay_warning_scope] applies its
30+
list from the end. *)
2731
let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
28-
Bs_ast_invariant.iter_warnings_on_sigi ast;
32+
Builtin_attributes.replay_warning_scope
33+
(List.rev (Bs_ast_invariant.leading_attributes_of_signature ast))
34+
@@ fun () ->
2935
Ast_config.process_sig ast;
3036
let ast =
3137
match !((Js_config.current ()).jsx_version) with
@@ -44,7 +50,9 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
4450
result
4551

4652
let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
47-
Bs_ast_invariant.iter_warnings_on_stru ast;
53+
Builtin_attributes.replay_warning_scope
54+
(List.rev (Bs_ast_invariant.leading_attributes_of_structure ast))
55+
@@ fun () ->
4856
Ast_config.process_str ast;
4957
let ast =
5058
match !((Js_config.current ()).jsx_version) with

‎compiler/typing/builtin_attributes.ml‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,42 +141,49 @@ let rec deprecated_of_str = function
141141
| Some _ as r -> r)
142142
| _ -> None
143143

144-
let warning_attribute ?(ppwarning = true) =
144+
(* Applies a [@warning] or [@warnerror] attribute to the warning state. With
145+
[report], also reports a malformed payload and a [@ppwarning]. *)
146+
let apply_warning_attribute ~report =
145147
let process loc txt errflag payload =
148+
let malformed msg =
149+
if report then
150+
Loc_report.prerr_warning loc (Warnings.Attribute_payload (txt, msg))
151+
in
146152
match string_of_payload payload with
147153
| Some s -> (
148154
try Warnings.parse_options errflag s
149-
with Arg.Bad _ ->
150-
Loc_report.prerr_warning loc
151-
(Warnings.Attribute_payload (txt, "Ill-formed list of warnings")))
152-
| None ->
153-
Loc_report.prerr_warning loc
154-
(Warnings.Attribute_payload (txt, "A single string literal is expected"))
155+
with Arg.Bad _ -> malformed "Ill-formed list of warnings")
156+
| None -> malformed "A single string literal is expected"
155157
in
156158
function
157159
| {txt = "warning" as txt; loc}, payload -> process loc txt false payload
158160
| {txt = "warnerror" as txt; loc}, payload -> process loc txt true payload
159-
| {txt = "ppwarning"}, (PStr [{pstr_loc; _}] as payload) when ppwarning -> (
161+
| {txt = "ppwarning"}, (PStr [{pstr_loc; _}] as payload) when report -> (
160162
match string_of_payload payload with
161163
| Some s -> Loc_report.prerr_warning pstr_loc (Warnings.Preprocessor s)
162164
| None -> ())
163165
| _ -> ()
164166

165-
let warning_scope ?ppwarning attrs f =
167+
let warning_attribute = apply_warning_attribute ~report:true
168+
let replay_warning_attribute = apply_warning_attribute ~report:false
169+
170+
let scope apply attrs f =
166171
let prev = Warnings.backup () in
167172
try
168-
(* Most nodes have no attributes; skipping them also saves the closure the
169-
partial application would allocate on every call *)
173+
(* Most nodes have no attributes *)
170174
(match attrs with
171175
| [] -> ()
172-
| _ -> List.iter (warning_attribute ?ppwarning) (List.rev attrs));
176+
| _ -> List.iter apply (List.rev attrs));
173177
let ret = f () in
174178
Warnings.restore prev;
175179
ret
176180
with exn ->
177181
Warnings.restore prev;
178182
raise exn
179183

184+
let warning_scope attrs f = scope warning_attribute attrs f
185+
let replay_warning_scope attrs f = scope replay_warning_attribute attrs f
186+
180187
let warn_on_literal_pattern =
181188
List.exists (function
182189
| {txt = "warn_on_literal_pattern"; _}, _ -> true

‎compiler/typing/builtin_attributes.mli‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,25 @@ val error_of_extension : Parsetree.extension -> Loc_report.error
5757
val uninterpreted_extension : loc:Location.t -> string -> Loc_report.error
5858
(** The error for an extension that nothing interpreted. *)
5959

60-
val warning_attribute : ?ppwarning:bool -> Parsetree.attribute -> unit
61-
(** Apply warning settings from the specified attribute.
62-
"warning" and "warnerror" are processed and other attributes are
63-
ignored.
60+
val warning_attribute : Parsetree.attribute -> unit
61+
(** Apply the warning settings of a [@warning] or [@warnerror] attribute, and
62+
report a malformed payload and a [@ppwarning]. Other attributes are
63+
ignored. Each attribute is reported once, by the type checker; other
64+
passes over it use [replay_warning_attribute]. *)
6465

65-
Also implement ppwarning (unless ~ppwarning:false is passed).
66-
*)
66+
val replay_warning_attribute : Parsetree.attribute -> unit
67+
(** Apply the warning settings of an attribute that has been or will be
68+
reported elsewhere, without reporting anything. *)
6769

68-
val warning_scope :
69-
?ppwarning:bool -> Parsetree.attributes -> (unit -> 'a) -> 'a
70-
(** Execute a function in a new scope for warning settings. This
71-
means that the effect of any call to [warning_attribute] during
72-
the execution of this function will be discarded after
73-
execution.
70+
val warning_scope : Parsetree.attributes -> (unit -> 'a) -> 'a
71+
(** Run a function in a new scope for warning settings: the warning state is
72+
restored when it returns. The attributes are applied with
73+
[warning_attribute] first. The list is applied from its end. *)
7474

75-
The function also takes a list of attributes which are processed
76-
with [warning_attribute] in the fresh scope before the function
77-
is executed.
78-
*)
75+
val replay_warning_scope : Parsetree.attributes -> (unit -> 'a) -> 'a
76+
(** Like [warning_scope], but applies the attributes with
77+
[replay_warning_attribute]: for a scope entered again for attributes that
78+
another scope reports, such as a later pass over the same node. *)
7979

8080
val warn_on_literal_pattern : Parsetree.attributes -> bool
8181

‎compiler/typing/inline_not_stored.ml‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ let has_inline (attributes : Parsetree.attributes) =
1515
let record_binding (attributes : Parsetree.attributes)
1616
(inner_attributes : Parsetree.attributes) ids =
1717
if has_inline attributes || has_inline inner_attributes then
18-
(* Type checking applied these scopes already and reported what they
19-
report, such as [@ppwarning] or a malformed [@warning]; here they are
18+
(* Type checking reported what these attributes report; here they are
2019
only read. *)
2120
let state =
22-
Warnings.without_warnings (fun () ->
23-
Builtin_attributes.warning_scope ~ppwarning:false attributes
24-
(fun () ->
25-
Builtin_attributes.warning_scope ~ppwarning:false inner_attributes
26-
Warnings.backup))
21+
Builtin_attributes.replay_warning_scope attributes (fun () ->
22+
Builtin_attributes.replay_warning_scope inner_attributes
23+
Warnings.backup)
2724
in
2825
List.iter
2926
(fun id -> Hashtbl.replace (Domain.DLS.get states_key) id state)

‎compiler/typing/translcore.ml‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,12 +1229,11 @@ let jsx_call env (funct : expression) (p : Primitive.description)
12291229
(* A primitive applied in place is not translated with [transl_exp], so check
12301230
its attributes here, in the warning scope [transl_exp] would use *)
12311231
let check_callee_attributes (funct : expression) =
1232-
Builtin_attributes.warning_scope ~ppwarning:false funct.exp_attributes
1233-
(fun () ->
1232+
Builtin_attributes.replay_warning_scope funct.exp_attributes (fun () ->
12341233
List.iter (Translattribute.check_attribute funct) funct.exp_attributes)
12351234

12361235
let rec transl_exp e =
1237-
Builtin_attributes.warning_scope ~ppwarning:false e.exp_attributes (fun () ->
1236+
Builtin_attributes.replay_warning_scope (exp_warning_attributes e) (fun () ->
12381237
List.iter (Translattribute.check_attribute e) e.exp_attributes;
12391238
transl_exp0 e)
12401239

@@ -1730,7 +1729,7 @@ and transl_let rec_flag pat_expr_list body =
17301729
| [] -> body
17311730
| {vb_pat = pat; vb_expr = expr; vb_attributes = attr; vb_loc} :: rem ->
17321731
let lam =
1733-
Builtin_attributes.warning_scope ~ppwarning:false attr (fun () ->
1732+
Builtin_attributes.replay_warning_scope attr (fun () ->
17341733
transl_exp expr)
17351734
in
17361735
let lam = Translattribute.add_inline_attribute lam vb_loc attr in
@@ -1749,8 +1748,8 @@ and transl_let rec_flag pat_expr_list body =
17491748
*)
17501749
in
17511750
let lam =
1752-
Builtin_attributes.warning_scope ~ppwarning:false vb_attributes
1753-
(fun () -> transl_exp expr)
1751+
Builtin_attributes.replay_warning_scope vb_attributes (fun () ->
1752+
transl_exp expr)
17541753
in
17551754
let lam = Translattribute.add_inline_attribute lam vb_loc vb_attributes in
17561755
(id, lam)

‎compiler/typing/translmod.ml‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ let rec compile_functor mexp coercion root_path loc =
249249
(* Compile a module expression, in the warning scope of its attributes like
250250
[Translcore.transl_exp] *)
251251
and transl_module cc rootpath mexp =
252-
Builtin_attributes.warning_scope ~ppwarning:false mexp.mod_attributes
253-
(fun () ->
252+
Builtin_attributes.replay_warning_scope mexp.mod_attributes (fun () ->
254253
List.iter
255254
(Translattribute.check_attribute_on_module mexp)
256255
mexp.mod_attributes;
@@ -392,11 +391,10 @@ and transl_structure loc fields cc rootpath final_env = function
392391
cc rootpath final_env rem
393392
in
394393
let module_body =
395-
transl_module Tcoerce_none (field_path rootpath id) mb.mb_expr
396-
in
397-
let module_body =
398-
Translattribute.add_inline_attribute module_body mb.mb_loc
399-
mb.mb_attributes
394+
Builtin_attributes.replay_warning_scope mb.mb_attributes (fun () ->
395+
Translattribute.add_inline_attribute
396+
(transl_module Tcoerce_none (field_path rootpath id) mb.mb_expr)
397+
mb.mb_loc mb.mb_attributes)
400398
in
401399
(Lambda.let_ (pure_module mb.mb_expr) id module_body body, size)
402400
| Tstr_recmodule bindings ->
@@ -409,7 +407,9 @@ and transl_structure loc fields cc rootpath final_env = function
409407
let lam =
410408
Transl_recmodule.compile_recmodule
411409
(fun id modl _loc ->
412-
transl_module Tcoerce_none (field_path rootpath id) modl)
410+
let mb = List.find (fun mb -> Ident.same mb.mb_id id) bindings in
411+
Builtin_attributes.replay_warning_scope mb.mb_attributes (fun () ->
412+
transl_module Tcoerce_none (field_path rootpath id) modl))
413413
bindings body
414414
in
415415
(lam, size)
@@ -431,11 +431,15 @@ and transl_structure loc fields cc rootpath final_env = function
431431
in
432432
let body, size = rebind_idents 0 fields ids in
433433
( Lambda.let_ (pure_module modl) mid
434-
(transl_module Tcoerce_none None modl)
434+
(Builtin_attributes.replay_warning_scope incl.incl_attributes
435+
(fun () -> transl_module Tcoerce_none None modl))
435436
body,
436437
size )
437-
| Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _
438-
| Tstr_attribute _ ->
438+
| Tstr_attribute attr ->
439+
(* A floating attribute applies to the rest of the structure *)
440+
Builtin_attributes.replay_warning_scope [attr] (fun () ->
441+
transl_structure loc fields cc rootpath final_env rem)
442+
| Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _ ->
439443
transl_structure loc fields cc rootpath final_env rem)
440444

441445
(* Update forward declaration in Translcore *)

‎compiler/typing/typecore.ml‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,8 +1983,7 @@ let type_pattern ~lev env spat expected_ty =
19831983
the pattern's warning scope: enter it again so that [@warning] on the
19841984
pattern covers its variables. *)
19851985
let new_env, unpacks =
1986-
Builtin_attributes.warning_scope ~ppwarning:false spat.ppat_attributes
1987-
(fun () ->
1986+
Builtin_attributes.replay_warning_scope spat.ppat_attributes (fun () ->
19881987
add_pattern_variables !new_env
19891988
~check:(fun s -> Warnings.Unused_var_strict s)
19901989
~check_as:(fun s -> Warnings.Unused_var s))
@@ -1995,7 +1994,7 @@ let type_pattern_list env spatl expected_tys allow =
19951994
reset_pattern allow;
19961995
let new_env = ref env in
19971996
let type_pat (attrs, pat) ty =
1998-
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
1997+
Builtin_attributes.replay_warning_scope attrs (fun () ->
19991998
type_pat new_env pat ty)
20001999
in
20012000
let patl = List.map2 type_pat spatl expected_tys in
@@ -5013,7 +5012,7 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s)
50135012
let warn_about_unused_bindings =
50145013
List.exists
50155014
(fun attrs ->
5016-
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
5015+
Builtin_attributes.replay_warning_scope attrs (fun () ->
50175016
Warnings.is_active (check "")
50185017
|| Warnings.is_active (check_strict "")
50195018
|| (is_recursive && Warnings.is_active Warnings.Unused_rec_flag)))
@@ -5038,7 +5037,7 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s)
50385037
*)
50395038
List.map2
50405039
(fun attrs pat ->
5041-
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
5040+
Builtin_attributes.replay_warning_scope attrs (fun () ->
50425041
if not warn_about_unused_bindings then (pat, None)
50435042
else
50445043
let some_used = ref false in
@@ -5118,11 +5117,11 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s)
51185117
then
51195118
let {pvb_pat; pvb_attributes} = List.hd spat_sexp_list in
51205119
(* See PR#6677 *)
5121-
Builtin_attributes.warning_scope ~ppwarning:false pvb_attributes (fun () ->
5120+
Builtin_attributes.replay_warning_scope pvb_attributes (fun () ->
51225121
Loc_report.prerr_warning pvb_pat.ppat_loc Warnings.Unused_rec_flag));
51235122
List.iter2
51245123
(fun pat (attrs, exp) ->
5125-
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
5124+
Builtin_attributes.replay_warning_scope attrs (fun () ->
51265125
let partial_match_warning_hint =
51275126
if Experimental_features.is_enabled Experimental_features.LetUnwrap
51285127
then

0 commit comments

Comments
 (0)