Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/malformed-warning-reported-once.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Report a malformed `@warning` or `@warnerror` payload and `@@ppwarning` only once.
- Apply `@warning` and `@@warning` to warnings reported during translation, such as warning 102, also on modules, includes, type constraints, expression statements and floating `@@warning` items.
28 changes: 11 additions & 17 deletions compiler/frontend/bs_ast_invariant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions compiler/frontend/bs_ast_invariant.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down
12 changes: 10 additions & 2 deletions compiler/frontend/ppx_entry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
31 changes: 19 additions & 12 deletions compiler/typing/builtin_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -141,42 +141,49 @@ 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
with exn ->
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
Expand Down
30 changes: 13 additions & 17 deletions compiler/typing/builtin_attributes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,21 @@ 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. Only the type checker's first pass over a node uses it; other
passes over the same attributes use [replay_warning_scope]. *)

Also implement ppwarning (unless ~ppwarning:false is passed).
*)
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. *)

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.

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 without reporting
anything: for attributes that another scope reports, such as on a later
pass over the same node. *)

val warn_on_literal_pattern : Parsetree.attributes -> bool

Expand Down
11 changes: 4 additions & 7 deletions compiler/typing/inline_not_stored.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions compiler/typing/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 (exp_warning_attributes e) (fun () ->
List.iter (Translattribute.check_attribute e) e.exp_attributes;
transl_exp0 e)

Expand Down Expand Up @@ -1730,10 +1729,9 @@ 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 () ->
transl_exp expr)
Builtin_attributes.replay_warning_scope attr (fun () ->
Translattribute.add_inline_attribute (transl_exp expr) vb_loc attr)
in
let lam = Translattribute.add_inline_attribute lam vb_loc attr in
Matching.for_let pat.pat_loc lam pat (transl rem)
in
transl pat_expr_list
Expand All @@ -1749,10 +1747,10 @@ 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 () ->
Translattribute.add_inline_attribute (transl_exp expr) vb_loc
vb_attributes)
in
let lam = Translattribute.add_inline_attribute lam vb_loc vb_attributes in
(id, lam)
in
Lambda_scc.bind_rec (List.map transl_case pat_expr_list) body
Expand Down
34 changes: 21 additions & 13 deletions compiler/typing/translmod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -347,9 +346,13 @@ and transl_structure loc fields cc rootpath final_env = function
| _ -> Misc.fatal_error "Translmod.transl_structure")
| item :: rem -> (
match item.str_desc with
| Tstr_eval (expr, _) ->
| Tstr_eval (expr, attrs) ->
let body, size = transl_structure loc fields cc rootpath final_env rem in
(Lambda.seq (Translcore.transl_exp expr) body, size)
let lam =
Builtin_attributes.replay_warning_scope attrs (fun () ->
Translcore.transl_exp expr)
in
(Lambda.seq lam body, size)
| Tstr_value (rec_flag, pat_expr_list) ->
let ext_fields = rev_let_bound_idents pat_expr_list @ fields in
let body, size =
Expand Down Expand Up @@ -392,11 +395,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 ->
Expand All @@ -409,7 +411,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)
Expand All @@ -431,11 +435,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 *)
Expand Down
13 changes: 6 additions & 7 deletions compiler/typing/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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)))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions compiler/typing/typedtree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions compiler/typing/typedtree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Loading
Loading