From 64174bfb76eb67addf2d3933f9f2a97116356e27 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 19:41:50 +0000 Subject: [PATCH 1/2] Fix malformed @warning payloads reported several times Why: Builtin_attributes.warning_attribute both applied a warning setting and reported problems with it. The type checker enters a binding's warning scope several times, 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 its warning state stayed in place for the type checker. 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, 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 the warning state is restored before type checking. The type checker then applies and reports these items in order, as it already does when a build compiles the parsed .ast file. 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 Fixes #9488 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel --- .../malformed-warning-reported-once.fix.md | 1 + compiler/frontend/bs_ast_invariant.ml | 28 +++----- compiler/frontend/bs_ast_invariant.mli | 8 ++- compiler/frontend/ppx_entry.ml | 12 +++- compiler/typing/builtin_attributes.ml | 31 ++++---- compiler/typing/builtin_attributes.mli | 32 ++++----- compiler/typing/inline_not_stored.ml | 11 ++- compiler/typing/translcore.ml | 11 ++- compiler/typing/translmod.ml | 3 +- compiler/typing/typecore.ml | 13 ++-- ...rning_47_warning_payload_once.res.expected | 72 +++++++++++++++++++ ...ning_47_warning_payload_order.res.expected | 23 ++++++ .../warning_47_warning_payload_once.res | 7 ++ .../warning_47_warning_payload_order.res | 7 ++ 14 files changed, 188 insertions(+), 71 deletions(-) create mode 100644 changes/malformed-warning-reported-once.fix.md create mode 100644 tests/error_tests/single/expected/warning_47_warning_payload_once.res.expected create mode 100644 tests/error_tests/single/expected/warning_47_warning_payload_order.res.expected create mode 100644 tests/error_tests/single/fixtures/warning_47_warning_payload_once.res create mode 100644 tests/error_tests/single/fixtures/warning_47_warning_payload_order.res diff --git a/changes/malformed-warning-reported-once.fix.md b/changes/malformed-warning-reported-once.fix.md new file mode 100644 index 0000000000..4e16b95d23 --- /dev/null +++ b/changes/malformed-warning-reported-once.fix.md @@ -0,0 +1 @@ +- Report a malformed `@warning` or `@warnerror` payload and `@@ppwarning` only once. diff --git a/compiler/frontend/bs_ast_invariant.ml b/compiler/frontend/bs_ast_invariant.ml index b21a4a8b9b..7ad62aae81 100644 --- a/compiler/frontend/bs_ast_invariant.ml +++ b/compiler/frontend/bs_ast_invariant.ml @@ -26,25 +26,19 @@ type iterator = Ast_iterator.iterator let super = Ast_iterator.default_iterator -let rec iter_warnings_on_stru (stru : Parsetree.structure) = +(* The floating attributes at the start of a structure or signature, which + the built-in PPX respects, in source order *) +let rec leading_attributes_of_structure (stru : Parsetree.structure) = match stru with - | [] -> () - | head :: rest -> ( - match head.pstr_desc with - | Pstr_attribute attr -> - Builtin_attributes.warning_attribute attr; - iter_warnings_on_stru rest - | _ -> ()) + | {pstr_desc = Pstr_attribute attr} :: rest -> + attr :: leading_attributes_of_structure rest + | _ -> [] -let rec iter_warnings_on_sigi (stru : Parsetree.signature) = - match stru with - | [] -> () - | head :: rest -> ( - match head.psig_desc with - | Psig_attribute attr -> - Builtin_attributes.warning_attribute attr; - iter_warnings_on_sigi rest - | _ -> ()) +let rec leading_attributes_of_signature (sigi : Parsetree.signature) = + match sigi with + | {psig_desc = Psig_attribute attr} :: rest -> + attr :: leading_attributes_of_signature rest + | _ -> [] let emit_external_warnings_on_structure (stru : Parsetree.structure) = Ast_checks.iterator.structure Ast_checks.iterator stru diff --git a/compiler/frontend/bs_ast_invariant.mli b/compiler/frontend/bs_ast_invariant.mli index 520e39ed51..ab724a784e 100644 --- a/compiler/frontend/bs_ast_invariant.mli +++ b/compiler/frontend/bs_ast_invariant.mli @@ -24,9 +24,13 @@ type iterator = Ast_iterator.iterator -val iter_warnings_on_stru : Parsetree.structure -> unit +val leading_attributes_of_structure : + Parsetree.structure -> Parsetree.attributes +(** The floating attributes at the start of a structure, in source order *) -val iter_warnings_on_sigi : Parsetree.signature -> unit +val leading_attributes_of_signature : + Parsetree.signature -> Parsetree.attributes +(** The floating attributes at the start of a signature, in source order *) (** Run [Ast_checks.iterator] over a file after its PPXes. It skips FFI externals, which the type checker checks in their resolved form. *) diff --git a/compiler/frontend/ppx_entry.ml b/compiler/frontend/ppx_entry.ml index e59a6ec5a0..f48e8ba125 100644 --- a/compiler/frontend/ppx_entry.ml +++ b/compiler/frontend/ppx_entry.ml @@ -24,8 +24,14 @@ let unsafe_mapper = Bs_builtin_ppx.mapper +(* The built-in PPX runs in the scope of the leading [@@warning] items. The + type checker applies and reports them, so here they are only applied, and + the scope ends before type checking. [replay_warning_scope] applies its + list from the end. *) let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature = - Bs_ast_invariant.iter_warnings_on_sigi ast; + Builtin_attributes.replay_warning_scope + (List.rev (Bs_ast_invariant.leading_attributes_of_signature ast)) + @@ fun () -> Ast_config.process_sig ast; let ast = match !((Js_config.current ()).jsx_version) with @@ -44,7 +50,9 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature = result let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure = - Bs_ast_invariant.iter_warnings_on_stru ast; + Builtin_attributes.replay_warning_scope + (List.rev (Bs_ast_invariant.leading_attributes_of_structure ast)) + @@ fun () -> Ast_config.process_str ast; let ast = match !((Js_config.current ()).jsx_version) with diff --git a/compiler/typing/builtin_attributes.ml b/compiler/typing/builtin_attributes.ml index 71239565a7..f4b2b8e8cd 100644 --- a/compiler/typing/builtin_attributes.ml +++ b/compiler/typing/builtin_attributes.ml @@ -141,35 +141,39 @@ let rec deprecated_of_str = function | Some _ as r -> r) | _ -> None -let warning_attribute ?(ppwarning = true) = +(* Applies a [@warning] or [@warnerror] attribute to the warning state. With + [report], also reports a malformed payload and a [@ppwarning]. *) +let apply_warning_attribute ~report = let process loc txt errflag payload = + let malformed msg = + if report then + Loc_report.prerr_warning loc (Warnings.Attribute_payload (txt, msg)) + in match string_of_payload payload with | Some s -> ( try Warnings.parse_options errflag s - with Arg.Bad _ -> - Loc_report.prerr_warning loc - (Warnings.Attribute_payload (txt, "Ill-formed list of warnings"))) - | None -> - Loc_report.prerr_warning loc - (Warnings.Attribute_payload (txt, "A single string literal is expected")) + with Arg.Bad _ -> malformed "Ill-formed list of warnings") + | None -> malformed "A single string literal is expected" in function | {txt = "warning" as txt; loc}, payload -> process loc txt false payload | {txt = "warnerror" as txt; loc}, payload -> process loc txt true payload - | {txt = "ppwarning"}, (PStr [{pstr_loc; _}] as payload) when ppwarning -> ( + | {txt = "ppwarning"}, (PStr [{pstr_loc; _}] as payload) when report -> ( match string_of_payload payload with | Some s -> Loc_report.prerr_warning pstr_loc (Warnings.Preprocessor s) | None -> ()) | _ -> () -let warning_scope ?ppwarning attrs f = +let warning_attribute = apply_warning_attribute ~report:true +let replay_warning_attribute = apply_warning_attribute ~report:false + +let scope apply attrs f = let prev = Warnings.backup () in try - (* Most nodes have no attributes; skipping them also saves the closure the - partial application would allocate on every call *) + (* Most nodes have no attributes *) (match attrs with | [] -> () - | _ -> List.iter (warning_attribute ?ppwarning) (List.rev attrs)); + | _ -> List.iter apply (List.rev attrs)); let ret = f () in Warnings.restore prev; ret @@ -177,6 +181,9 @@ let warning_scope ?ppwarning attrs f = Warnings.restore prev; raise exn +let warning_scope attrs f = scope warning_attribute attrs f +let replay_warning_scope attrs f = scope replay_warning_attribute attrs f + let warn_on_literal_pattern = List.exists (function | {txt = "warn_on_literal_pattern"; _}, _ -> true diff --git a/compiler/typing/builtin_attributes.mli b/compiler/typing/builtin_attributes.mli index 3288ea8d61..d342a77ba4 100644 --- a/compiler/typing/builtin_attributes.mli +++ b/compiler/typing/builtin_attributes.mli @@ -57,25 +57,25 @@ val error_of_extension : Parsetree.extension -> Loc_report.error val uninterpreted_extension : loc:Location.t -> string -> Loc_report.error (** The error for an extension that nothing interpreted. *) -val warning_attribute : ?ppwarning:bool -> Parsetree.attribute -> unit -(** Apply warning settings from the specified attribute. - "warning" and "warnerror" are processed and other attributes are - ignored. +val warning_attribute : Parsetree.attribute -> unit +(** Apply the warning settings of a [@warning] or [@warnerror] attribute, and + report a malformed payload and a [@ppwarning]. Other attributes are + ignored. Each attribute is reported once, by the type checker; other + passes over it use [replay_warning_attribute]. *) - Also implement ppwarning (unless ~ppwarning:false is passed). - *) +val replay_warning_attribute : Parsetree.attribute -> unit +(** Apply the warning settings of an attribute that has been or will be + reported elsewhere, without reporting anything. *) -val warning_scope : - ?ppwarning:bool -> Parsetree.attributes -> (unit -> 'a) -> 'a -(** Execute a function in a new scope for warning settings. This - means that the effect of any call to [warning_attribute] during - the execution of this function will be discarded after - execution. +val warning_scope : Parsetree.attributes -> (unit -> 'a) -> 'a +(** Run a function in a new scope for warning settings: the warning state is + restored when it returns. The attributes are applied with + [warning_attribute] first. The list is applied from its end. *) - The function also takes a list of attributes which are processed - with [warning_attribute] in the fresh scope before the function - is executed. - *) +val replay_warning_scope : Parsetree.attributes -> (unit -> 'a) -> 'a +(** Like [warning_scope], but applies the attributes with + [replay_warning_attribute]: for a scope entered again for attributes that + another scope reports, such as a later pass over the same node. *) val warn_on_literal_pattern : Parsetree.attributes -> bool diff --git a/compiler/typing/inline_not_stored.ml b/compiler/typing/inline_not_stored.ml index c2b1d57e54..a97067b894 100644 --- a/compiler/typing/inline_not_stored.ml +++ b/compiler/typing/inline_not_stored.ml @@ -15,15 +15,12 @@ let has_inline (attributes : Parsetree.attributes) = let record_binding (attributes : Parsetree.attributes) (inner_attributes : Parsetree.attributes) ids = if has_inline attributes || has_inline inner_attributes then - (* Type checking applied these scopes already and reported what they - report, such as [@ppwarning] or a malformed [@warning]; here they are + (* Type checking reported what these attributes report; here they are only read. *) let state = - Warnings.without_warnings (fun () -> - Builtin_attributes.warning_scope ~ppwarning:false attributes - (fun () -> - Builtin_attributes.warning_scope ~ppwarning:false inner_attributes - Warnings.backup)) + Builtin_attributes.replay_warning_scope attributes (fun () -> + Builtin_attributes.replay_warning_scope inner_attributes + Warnings.backup) in List.iter (fun id -> Hashtbl.replace (Domain.DLS.get states_key) id state) diff --git a/compiler/typing/translcore.ml b/compiler/typing/translcore.ml index f4741f028f..6405c147d1 100644 --- a/compiler/typing/translcore.ml +++ b/compiler/typing/translcore.ml @@ -1229,12 +1229,11 @@ let jsx_call env (funct : expression) (p : Primitive.description) (* A primitive applied in place is not translated with [transl_exp], so check its attributes here, in the warning scope [transl_exp] would use *) let check_callee_attributes (funct : expression) = - Builtin_attributes.warning_scope ~ppwarning:false funct.exp_attributes - (fun () -> + Builtin_attributes.replay_warning_scope funct.exp_attributes (fun () -> List.iter (Translattribute.check_attribute funct) funct.exp_attributes) let rec transl_exp e = - Builtin_attributes.warning_scope ~ppwarning:false e.exp_attributes (fun () -> + Builtin_attributes.replay_warning_scope e.exp_attributes (fun () -> List.iter (Translattribute.check_attribute e) e.exp_attributes; transl_exp0 e) @@ -1730,7 +1729,7 @@ and transl_let rec_flag pat_expr_list body = | [] -> body | {vb_pat = pat; vb_expr = expr; vb_attributes = attr; vb_loc} :: rem -> let lam = - Builtin_attributes.warning_scope ~ppwarning:false attr (fun () -> + Builtin_attributes.replay_warning_scope attr (fun () -> transl_exp expr) in let lam = Translattribute.add_inline_attribute lam vb_loc attr in @@ -1749,8 +1748,8 @@ and transl_let rec_flag pat_expr_list body = *) in let lam = - Builtin_attributes.warning_scope ~ppwarning:false vb_attributes - (fun () -> transl_exp expr) + Builtin_attributes.replay_warning_scope vb_attributes (fun () -> + transl_exp expr) in let lam = Translattribute.add_inline_attribute lam vb_loc vb_attributes in (id, lam) diff --git a/compiler/typing/translmod.ml b/compiler/typing/translmod.ml index 651ff8ec24..1a06607467 100644 --- a/compiler/typing/translmod.ml +++ b/compiler/typing/translmod.ml @@ -249,8 +249,7 @@ let rec compile_functor mexp coercion root_path loc = (* Compile a module expression, in the warning scope of its attributes like [Translcore.transl_exp] *) and transl_module cc rootpath mexp = - Builtin_attributes.warning_scope ~ppwarning:false mexp.mod_attributes - (fun () -> + Builtin_attributes.replay_warning_scope mexp.mod_attributes (fun () -> List.iter (Translattribute.check_attribute_on_module mexp) mexp.mod_attributes; diff --git a/compiler/typing/typecore.ml b/compiler/typing/typecore.ml index ecc8bb1d93..d9f7434e67 100644 --- a/compiler/typing/typecore.ml +++ b/compiler/typing/typecore.ml @@ -1983,8 +1983,7 @@ let type_pattern ~lev env spat expected_ty = the pattern's warning scope: enter it again so that [@warning] on the pattern covers its variables. *) let new_env, unpacks = - Builtin_attributes.warning_scope ~ppwarning:false spat.ppat_attributes - (fun () -> + Builtin_attributes.replay_warning_scope spat.ppat_attributes (fun () -> add_pattern_variables !new_env ~check:(fun s -> Warnings.Unused_var_strict s) ~check_as:(fun s -> Warnings.Unused_var s)) @@ -1995,7 +1994,7 @@ let type_pattern_list env spatl expected_tys allow = reset_pattern allow; let new_env = ref env in let type_pat (attrs, pat) ty = - Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () -> + Builtin_attributes.replay_warning_scope attrs (fun () -> type_pat new_env pat ty) in 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) let warn_about_unused_bindings = List.exists (fun attrs -> - Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () -> + Builtin_attributes.replay_warning_scope attrs (fun () -> Warnings.is_active (check "") || Warnings.is_active (check_strict "") || (is_recursive && Warnings.is_active Warnings.Unused_rec_flag))) @@ -5038,7 +5037,7 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s) *) List.map2 (fun attrs pat -> - Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () -> + Builtin_attributes.replay_warning_scope attrs (fun () -> if not warn_about_unused_bindings then (pat, None) else let some_used = ref false in @@ -5118,11 +5117,11 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s) then let {pvb_pat; pvb_attributes} = List.hd spat_sexp_list in (* See PR#6677 *) - Builtin_attributes.warning_scope ~ppwarning:false pvb_attributes (fun () -> + Builtin_attributes.replay_warning_scope pvb_attributes (fun () -> Loc_report.prerr_warning pvb_pat.ppat_loc Warnings.Unused_rec_flag)); List.iter2 (fun pat (attrs, exp) -> - Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () -> + Builtin_attributes.replay_warning_scope attrs (fun () -> let partial_match_warning_hint = if Experimental_features.is_enabled Experimental_features.LetUnwrap then diff --git a/tests/error_tests/single/expected/warning_47_warning_payload_once.res.expected b/tests/error_tests/single/expected/warning_47_warning_payload_once.res.expected new file mode 100644 index 0000000000..e6c23100a8 --- /dev/null +++ b/tests/error_tests/single/expected/warning_47_warning_payload_once.res.expected @@ -0,0 +1,72 @@ + + Warning number 47 + /.../fixtures/warning_47_warning_payload_once.res:1:1-9 + + 1 │ @@warning(3) + 2 │ @@ppwarning("reported once") + 3 │ + + illegal payload for attribute @warning. +A single string literal is expected + + + Warning number 22 + /.../fixtures/warning_47_warning_payload_once.res:2:13-27 + + 1 │ @@warning(3) + 2 │ @@ppwarning("reported once") + 3 │ + 4 │ @warning(3) let x = 1 + + reported once + + + Warning number 47 + /.../fixtures/warning_47_warning_payload_once.res:4:1-8 + + 2 │ @@ppwarning("reported once") + 3 │ + 4 │ @warning(3) let x = 1 + 5 │ @warnerror("bad!") let y = 1 + 6 │ let z = @warning(3) 1 + + illegal payload for attribute @warning. +A single string literal is expected + + + Warning number 47 + /.../fixtures/warning_47_warning_payload_once.res:5:1-10 + + 3 │ + 4 │ @warning(3) let x = 1 + 5 │ @warnerror("bad!") let y = 1 + 6 │ let z = @warning(3) 1 + 7 │ module M = @warning(3) {} + + illegal payload for attribute @warnerror. +Ill-formed list of warnings + + + Warning number 47 + /.../fixtures/warning_47_warning_payload_once.res:6:9-16 + + 4 │ @warning(3) let x = 1 + 5 │ @warnerror("bad!") let y = 1 + 6 │ let z = @warning(3) 1 + 7 │ module M = @warning(3) {} + 8 │ + + illegal payload for attribute @warning. +A single string literal is expected + + + Warning number 47 + /.../fixtures/warning_47_warning_payload_once.res:7:12-19 + + 5 │ @warnerror("bad!") let y = 1 + 6 │ let z = @warning(3) 1 + 7 │ module M = @warning(3) {} + 8 │ + + illegal payload for attribute @warning. +A single string literal is expected \ No newline at end of file diff --git a/tests/error_tests/single/expected/warning_47_warning_payload_order.res.expected b/tests/error_tests/single/expected/warning_47_warning_payload_order.res.expected new file mode 100644 index 0000000000..c06f97fcac --- /dev/null +++ b/tests/error_tests/single/expected/warning_47_warning_payload_order.res.expected @@ -0,0 +1,23 @@ + + Warning number 47 + /.../fixtures/warning_47_warning_payload_order.res:2:1-9 + + 1 │ // A later @@warning does not hide what an earlier one reports. + 2 │ @@warning(3) + 3 │ @@warning("-47") + 4 │ @@ppwarning("reported") + + illegal payload for attribute @warning. +A single string literal is expected + + + Warning number 22 + /.../fixtures/warning_47_warning_payload_order.res:4:13-22 + + 2 │ @@warning(3) + 3 │ @@warning("-47") + 4 │ @@ppwarning("reported") + 5 │ @@warning("-22") + 6 │ + + reported \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/warning_47_warning_payload_once.res b/tests/error_tests/single/fixtures/warning_47_warning_payload_once.res new file mode 100644 index 0000000000..abb8def1d8 --- /dev/null +++ b/tests/error_tests/single/fixtures/warning_47_warning_payload_once.res @@ -0,0 +1,7 @@ +@@warning(3) +@@ppwarning("reported once") + +@warning(3) let x = 1 +@warnerror("bad!") let y = 1 +let z = @warning(3) 1 +module M = @warning(3) {} diff --git a/tests/error_tests/single/fixtures/warning_47_warning_payload_order.res b/tests/error_tests/single/fixtures/warning_47_warning_payload_order.res new file mode 100644 index 0000000000..4739471a61 --- /dev/null +++ b/tests/error_tests/single/fixtures/warning_47_warning_payload_order.res @@ -0,0 +1,7 @@ +// A later @@warning does not hide what an earlier one reports. +@@warning(3) +@@warning("-47") +@@ppwarning("reported") +@@warning("-22") + +let x = 1 From 96f03a347a25dd882ff2d7abfa50fcbfa6be4515 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 19:43:38 +0000 Subject: [PATCH 2/2] Fix @warning not applying to warnings from translation Why: Translation to Lambda reports some warnings itself, such as 102 for a polymorphic comparison. It applies the warning attributes of the nodes it translates again, but only on let bindings, expressions and module expressions. It skipped floating @@warning items and the attributes of module bindings, recursive modules, includes and type constraints. So `@warning("-102")` on a module did not silence 102 inside it, and in a build `@@warning("+102")` never enabled it. Translmod now 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 (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) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel --- .../warning-attributes-in-translation.fix.md | 1 + compiler/typing/translcore.ml | 2 +- compiler/typing/translmod.ml | 23 +++++++----- compiler/typing/typedtree.ml | 9 +++++ compiler/typing/typedtree.mli | 5 +++ compiler/typing/typemod.ml | 13 ++----- .../warning_102_attribute_scopes.res.expected | 23 ++++++++++++ .../fixtures/warning_102_attribute_scopes.res | 35 +++++++++++++++++++ 8 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 changes/warning-attributes-in-translation.fix.md create mode 100644 tests/error_tests/single/expected/warning_102_attribute_scopes.res.expected create mode 100644 tests/error_tests/single/fixtures/warning_102_attribute_scopes.res diff --git a/changes/warning-attributes-in-translation.fix.md b/changes/warning-attributes-in-translation.fix.md new file mode 100644 index 0000000000..919147730a --- /dev/null +++ b/changes/warning-attributes-in-translation.fix.md @@ -0,0 +1 @@ +- Apply `@warning` and `@@warning` to warnings reported during translation, such as warning 102, wherever they apply during type checking. diff --git a/compiler/typing/translcore.ml b/compiler/typing/translcore.ml index 6405c147d1..a70b191222 100644 --- a/compiler/typing/translcore.ml +++ b/compiler/typing/translcore.ml @@ -1233,7 +1233,7 @@ let check_callee_attributes (funct : expression) = List.iter (Translattribute.check_attribute funct) funct.exp_attributes) let rec transl_exp e = - Builtin_attributes.replay_warning_scope e.exp_attributes (fun () -> + Builtin_attributes.replay_warning_scope (exp_warning_attributes e) (fun () -> List.iter (Translattribute.check_attribute e) e.exp_attributes; transl_exp0 e) diff --git a/compiler/typing/translmod.ml b/compiler/typing/translmod.ml index 1a06607467..0249171cab 100644 --- a/compiler/typing/translmod.ml +++ b/compiler/typing/translmod.ml @@ -391,11 +391,10 @@ and transl_structure loc fields cc rootpath final_env = function cc rootpath final_env rem in let module_body = - transl_module Tcoerce_none (field_path rootpath id) mb.mb_expr - in - let module_body = - Translattribute.add_inline_attribute module_body mb.mb_loc - mb.mb_attributes + Builtin_attributes.replay_warning_scope mb.mb_attributes (fun () -> + Translattribute.add_inline_attribute + (transl_module Tcoerce_none (field_path rootpath id) mb.mb_expr) + mb.mb_loc mb.mb_attributes) in (Lambda.let_ (pure_module mb.mb_expr) id module_body body, size) | Tstr_recmodule bindings -> @@ -408,7 +407,9 @@ and transl_structure loc fields cc rootpath final_env = function let lam = Transl_recmodule.compile_recmodule (fun id modl _loc -> - transl_module Tcoerce_none (field_path rootpath id) modl) + let mb = List.find (fun mb -> Ident.same mb.mb_id id) bindings in + Builtin_attributes.replay_warning_scope mb.mb_attributes (fun () -> + transl_module Tcoerce_none (field_path rootpath id) modl)) bindings body in (lam, size) @@ -430,11 +431,15 @@ and transl_structure loc fields cc rootpath final_env = function in let body, size = rebind_idents 0 fields ids in ( Lambda.let_ (pure_module modl) mid - (transl_module Tcoerce_none None modl) + (Builtin_attributes.replay_warning_scope incl.incl_attributes + (fun () -> transl_module Tcoerce_none None modl)) body, size ) - | Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _ - | Tstr_attribute _ -> + | Tstr_attribute attr -> + (* A floating attribute applies to the rest of the structure *) + Builtin_attributes.replay_warning_scope [attr] (fun () -> + transl_structure loc fields cc rootpath final_env rem) + | Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _ -> transl_structure loc fields cc rootpath final_env rem) (* Update forward declaration in Translcore *) diff --git a/compiler/typing/typedtree.ml b/compiler/typing/typedtree.ml index 99ce8e8556..1a688c604c 100644 --- a/compiler/typing/typedtree.ml +++ b/compiler/typing/typedtree.ml @@ -510,6 +510,15 @@ let rec bound_idents pat = iter_pattern_desc bound_idents pat.pat_desc | d -> iter_pattern_desc bound_idents d +let exp_warning_attributes e = + match e.exp_extra with + | [] -> e.exp_attributes + | extra -> + (* [warning_scope] applies a list from its end, so the innermost + attributes go first *) + e.exp_attributes + @ List.concat_map (fun (_, _, attrs) -> attrs) (List.rev extra) + let pat_bound_idents pat = idents () := []; bound_idents pat; diff --git a/compiler/typing/typedtree.mli b/compiler/typing/typedtree.mli index 4c45443d73..558cc3821f 100644 --- a/compiler/typing/typedtree.mli +++ b/compiler/typing/typedtree.mli @@ -594,3 +594,8 @@ val mknoloc : 'a -> 'a Asttypes.loc val mkloc : 'a -> Location.t -> 'a Asttypes.loc val pat_bound_idents : pattern -> Ident.t list + +val exp_warning_attributes : expression -> attributes +(** The attributes whose warning settings apply to an expression, in the order + [Builtin_attributes.warning_scope] takes them: its own, then those of the + type constraints around it, which [exp_extra] holds outermost first. *) diff --git a/compiler/typing/typemod.ml b/compiler/typing/typemod.ml index 92ae8cebdb..03e259543d 100644 --- a/compiler/typing/typemod.ml +++ b/compiler/typing/typemod.ml @@ -1474,18 +1474,9 @@ and type_structure funct_body anchor env sstr = warning state of [@inline] bindings here. *) List.iter (fun (vb : value_binding) -> - (* A type constraint keeps its attributes in [exp_extra], the - outermost first. [warning_scope] applies a list from its end, - so the innermost attributes go first. *) - let expression_attributes = - match vb.vb_expr.exp_extra with - | [] -> vb.vb_expr.exp_attributes - | extra -> - vb.vb_expr.exp_attributes - @ List.concat_map (fun (_, _, attrs) -> attrs) (List.rev extra) - in Inline_not_stored.record_binding vb.vb_attributes - expression_attributes (fun () -> pat_bound_idents vb.vb_pat)) + (exp_warning_attributes vb.vb_expr) (fun () -> + pat_bound_idents vb.vb_pat)) defs; let () = if rec_flag = Recursive then Rec_check.check_recursive_bindings defs diff --git a/tests/error_tests/single/expected/warning_102_attribute_scopes.res.expected b/tests/error_tests/single/expected/warning_102_attribute_scopes.res.expected new file mode 100644 index 0000000000..9acbdd9267 --- /dev/null +++ b/tests/error_tests/single/expected/warning_102_attribute_scopes.res.expected @@ -0,0 +1,23 @@ + + Warning number 102 + /.../fixtures/warning_102_attribute_scopes.res:32:19-28 + + 30 │ module Inner = { + 31 │ @@warning("+102") + 32 │ let reported2 = [1] == [2] + 33 │ } + 34 │ + + Polymorphic comparison introduced (maybe unsafe) + + + Warning number 102 + /.../fixtures/warning_102_attribute_scopes.res:24:17-26 + + 22 │ let silencedConstraint = @warning("-102") ([1] == [2]: bool) + 23 │ + 24 │ let reported1 = [1] == [2] + 25 │ + 26 │ @@warning("-102") + + Polymorphic comparison introduced (maybe unsafe) \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/warning_102_attribute_scopes.res b/tests/error_tests/single/fixtures/warning_102_attribute_scopes.res new file mode 100644 index 0000000000..f9e87bc155 --- /dev/null +++ b/tests/error_tests/single/fixtures/warning_102_attribute_scopes.res @@ -0,0 +1,35 @@ +// Warning 102 is reported while translating, outside type checking. Each +// attribute below must still apply to it: only `reported1` and `reported2` +// are reported. + +@warning("-102") +module OnModule = { + let silenced = [1] == [2] +} + +@warning("-102") +module rec OnRecModule: { + let silenced: bool +} = { + let silenced = [1] == [2] +} + +@warning("-102") +include { + let silencedInclude = [1] == [2] +} + +let silencedConstraint = @warning("-102") ([1] == [2]: bool) + +let reported1 = [1] == [2] + +@@warning("-102") + +let silencedFloating = [1] == [2] + +module Inner = { + @@warning("+102") + let reported2 = [1] == [2] +} + +let silencedAfterInner = [1] == [2]