Skip to content

Commit 6f1588d

Browse files
Merge PR #17747: Warn when trying to enable an unknown warning
Reviewed-by: gares Ack-by: Matafou Co-authored-by: gares <[email protected]>
2 parents 12fe911 + f5218dd commit 6f1588d

File tree

10 files changed

+80
-7
lines changed

10 files changed

+80
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- **Changed:**
2+
:opt:`Warnings` and :attr:`warnings` now emit a warning when trying
3+
to enable an unknown warning (there is still no warning when
4+
disabling an unknown warning as this behavior is useful for
5+
compatibility, or when enabling an unknown warning through the
6+
command line `-w` as the warning may be in a yet to be loaded
7+
plugin) (`#17747 <https://github.com/coq/coq/pull/17747>`_, by
8+
Gaëtan Gilbert).

lib/cWarnings.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,28 @@ let create ~name ?category ?default pp =
382382
let w = create_warning ?from ?default ~name () in
383383
create_in w pp
384384

385+
let warn_unknown_warnings = create ~name:"unknown-warning" Pp.(fun flags ->
386+
str "Could not enable unknown " ++
387+
str (CString.plural (List.length flags) "warning") ++ spc() ++
388+
prlist_with_sep spc str flags)
389+
390+
let override_unknown_warning = ref false
391+
392+
let warn_unknown_warnings ?loc flags =
393+
if not !override_unknown_warning then warn_unknown_warnings ?loc flags
394+
395+
let check_unknown_warnings flags =
396+
let flags = flags_of_string flags in
397+
let flags = List.filter_map (function
398+
| Disabled, _ | _, All -> None
399+
| (Enabled|AsError), Other name ->
400+
if String.Map.mem name !graph then None
401+
else Some name)
402+
flags
403+
in
404+
if not (List.is_empty flags) then
405+
warn_unknown_warnings flags
406+
385407
module CoreCategories = struct
386408

387409
let make name = create_category ~name ()

lib/cWarnings.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ val with_warn: string -> ('b -> 'a) -> 'b -> 'a
8989
raises an exception. If both f and restoring the warnings raise
9090
exceptions, the latter is raised. *)
9191

92+
val check_unknown_warnings : string -> unit
93+
(** Warn with "unknown-warning" if any unknown warnings are in the
94+
string with non-disabled status. *)
95+
96+
val override_unknown_warning : bool ref
97+
[@@ocaml.deprecated "Do not use, internal."]
98+
(** For command line -w, this avoids using the warning system to avoid breaking
99+
"-w -unknown-warning". *)
100+
92101
module CoreCategories : sig
93102
(** Categories used in coq-core. Might not be exhaustive. *)
94103

plugins/funind/g_indfun.mlg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ VERNAC COMMAND EXTEND Function STATE CUSTOM
209209
| ["Function" ne_function_fix_definition_list_sep(recsl,"with")]
210210
=> { classify_funind recsl }
211211
-> {
212-
let warn = "-unused-pattern-matching-variable,-matching-variable,-non-recursive" in
212+
let warn = "-unused-pattern-matching-variable,-non-recursive" in
213213
if is_interactive recsl then
214214
Vernacextend.vtopenproof (fun () ->
215215
CWarnings.with_warn warn

sysinit/coqinit.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,6 @@ let handle_injection = let open Coqargs in function
195195

196196
let start_library ~top injections =
197197
Flags.verbosely Declaremods.start_library top;
198-
List.iter handle_injection injections
198+
CWarnings.override_unknown_warning[@ocaml.warning "-3"] := true;
199+
List.iter handle_injection injections;
200+
CWarnings.override_unknown_warning[@ocaml.warning "-3"] := false

test-suite/output/Function.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
File "./output/Function.v", line 28, characters 4-5:
2+
Warning: Unused variable n might be a misspelled constructor. Use _ or _n to
3+
silence this warning. [unused-pattern-matching-variable,default]

test-suite/output/Function.v

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ Fixpoint f (l:list nat) : nat :=
2121
test would then need to be updated. *)
2222

2323
(* Ensuring the warning is a warning. *)
24-
Set Warnings "matching-variable".
25-
(* But no warning generated here. *)
26-
Function f (l:list nat) : nat :=
24+
Fixpoint f (l:list nat) : nat :=
2725
match l with
2826
| nil => O
2927
| S n :: nil => 1
3028
| n :: l' => f l'
3129
end.
30+
31+
(* But no warning generated here. *)
32+
Function g (l:list nat) : nat :=
33+
match l with
34+
| nil => O
35+
| S n :: nil => 1
36+
| n :: l' => g l'
37+
end.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Set Warnings "+unknown-warning".
2+
3+
Set Warnings "-foo".
4+
Fail Set Warnings "foo".
5+
Fail Set Warnings "+foo".
6+
7+
#[warnings="-foo"] Check True.
8+
Fail #[warnings="foo"] Check True.
9+
10+
(* debatable: even though "all" overrides "+foo" we still warn *)
11+
Fail Set Warnings "+foo,-all".
12+
13+
(* debatable: changing unknown-warning has no effect for the current check *)
14+
Fail Set Warnings "-unknown-warning,foo".
15+
Fail #[warnings="-unknown-warning,foo"] Check True.
16+
Set Warnings "-unknown-warning".
17+
#[warnings="+unknown-warning,foo"] Check True.

vernac/vernacentries.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,11 @@ let () =
17631763
optwrite = (fun b -> Constrintern.parsing_explicit := b) }
17641764

17651765
let () =
1766-
declare_string_option ~preprocess:CWarnings.normalize_flags_string
1766+
let preprocess flags =
1767+
CWarnings.check_unknown_warnings flags;
1768+
CWarnings.normalize_flags_string flags
1769+
in
1770+
declare_string_option ~preprocess
17671771
{ optstage = Summary.Stage.Interp;
17681772
optdepr = None;
17691773
optkey = ["Warnings"];

vernac/vernacinterp.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ let with_generic_atts atts f =
126126
Some (match warnings with Some w -> w ^ "," ^ ui | None -> ui) in
127127
match warnings with
128128
| None -> f ~atts
129-
| Some warnings -> CWarnings.with_warn warnings (fun () -> f ~atts) ()
129+
| Some warnings ->
130+
CWarnings.check_unknown_warnings warnings;
131+
CWarnings.with_warn warnings (fun () -> f ~atts) ()
130132

131133
(* "locality" is the prefix "Local" attribute, while the "local" component
132134
* is the outdated/deprecated "Local" attribute of some vernacular commands

0 commit comments

Comments
 (0)