Skip to content

Commit f24fd37

Browse files
committed
make warnings switch applicable to bucklescript internal ppx transform
note that ideally the check should be done in the parsing phase which makes more sense, but it is not done since it only benefits ocaml syntax and requires more work to do
1 parent f861952 commit f24fd37

22 files changed

+10708
-10299
lines changed

jscomp/common/bs_warnings.ml

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -39,92 +39,25 @@ let to_string t =
3939
->
4040
"Here a OCaml polymorphic variant type passed into JS, probably you forgot annotations like `[@bs.int]` or `[@bs.string]` "
4141

42-
let warning_formatter = Format.err_formatter
43-
44-
let print_string_warning (loc : Location.t) x =
45-
if loc.loc_ghost then
46-
Format.fprintf warning_formatter "File %s@." !Location.input_name
47-
else
48-
Location.print warning_formatter loc ;
49-
Format.fprintf warning_formatter "@{<error>Warning@}: %s@." x
5042

5143
let prerr_bs_ffi_warning loc x =
52-
Location.prerr_warning loc (Warnings.Bs_ffi_warning (to_string x))
53-
54-
let unimplemented_primitive = "Unimplemented primitive used:"
55-
type error =
56-
| Uninterpreted_delimiters of string
57-
| Unimplemented_primitive of string
58-
exception Error of Location.t * error
59-
60-
let pp_error fmt x =
61-
match x with
62-
| Unimplemented_primitive str ->
63-
Format.pp_print_string fmt unimplemented_primitive;
64-
Format.pp_print_string fmt str
65-
66-
| Uninterpreted_delimiters str ->
67-
Format.pp_print_string fmt "Uninterpreted delimiters" ;
68-
Format.pp_print_string fmt str
69-
70-
44+
Location.prerr_warning loc (Bs_ffi_warning (to_string x))
7145

72-
let () =
73-
Location.register_error_of_exn (function
74-
| Error (loc,err) ->
75-
Some (Location.error_of_printer loc pp_error err)
76-
| _ -> None
77-
)
7846

7947

8048

8149

82-
let warn_missing_primitive loc txt =
83-
if not !Js_config.no_warn_unimplemented_external && not !Clflags.bs_quiet then
84-
begin
85-
print_string_warning loc ( unimplemented_primitive ^ txt ^ " \n" );
86-
Format.pp_print_flush warning_formatter ()
87-
end
50+
let warn_missing_primitive loc txt =
51+
Location.prerr_warning loc (Bs_unimplemented_primitive txt)
8852

8953
let warn_literal_overflow loc =
90-
if not !Clflags.bs_quiet then
91-
begin
92-
print_string_warning loc
93-
"Integer literal exceeds the range of representable integers of type int";
94-
Format.pp_print_flush warning_formatter ()
95-
end
54+
Location.prerr_warning loc Bs_integer_literal_overflow
9655

9756

9857

9958
let error_unescaped_delimiter loc txt =
100-
raise (Error(loc, Uninterpreted_delimiters txt))
101-
102-
103-
104-
105-
106-
107-
(**
108-
Note the standard way of reporting error in compiler:
109-
110-
val Location.register_error_of_exn : (exn -> Location.error option) -> unit
111-
val Location.error_of_printer : Location.t ->
112-
(Format.formatter -> error -> unit) -> error -> Location.error
113-
114-
Define an error type
115-
116-
type error
117-
exception Error of Location.t * error
59+
Location.prerr_warning loc (Bs_uninterpreted_delimiters txt)
60+
11861

119-
Provide a printer to error
12062

121-
{[
122-
let () =
123-
Location.register_error_of_exn
124-
(function
125-
| Error(loc,err) ->
126-
Some (Location.error_of_printer loc pp_error err)
127-
| _ -> None
128-
)
129-
]}
130-
*)
63+

jscomp/common/js_config.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ let no_builtin_ppx_ml = ref false
6969
let no_builtin_ppx_mli = ref false
7070

7171

72-
(** TODO: will flip the option when it is ready *)
73-
let no_warn_unimplemented_external = ref false
74-
7572
let debug_file = ref ""
7673

7774

jscomp/common/js_config.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ val no_builtin_ppx_mli : bool ref
6363

6464

6565

66-
val no_warn_unimplemented_external : bool ref
66+
6767

6868
(** check-div-by-zero option *)
6969
val check_div_by_zero : bool ref

jscomp/main/js_main.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ let buckle_script_flags : (string * Arg.spec * string) list =
201201
("-bs-D", Arg.String define_variable,
202202
" Define conditional variable e.g, -D DEBUG=true"
203203
)
204-
::
205-
("-bs-quiet", Arg.Set Clflags.bs_quiet,
206-
" Quiet mode (no warnings printed)"
204+
::
205+
("-bs-quiet", Arg.Unit (fun _ -> ()),
206+
" (Deprecated using -w a) Quiet mode (no warnings printed)"
207207
)
208208
::
209209
("-bs-list-conditionals",
@@ -294,8 +294,8 @@ let buckle_script_flags : (string * Arg.spec * string) list =
294294
" set npm-output-path: [opt_module]:path, for example: 'lib/cjs', 'amdjs:lib/amdjs', 'es6:lib/es6' ")
295295
::
296296
("-bs-no-warn-unimplemented-external",
297-
Arg.Set Js_config.no_warn_unimplemented_external,
298-
" disable warnings on unimplmented c externals"
297+
Arg.Unit (fun _ -> ()),
298+
" Deprecated: use warning 106"
299299
)
300300
::
301301
("-bs-no-builtin-ppx-ml",

jscomp/syntax/bs_ast_invariant.ml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,30 @@ let emit_external_warnings : iterator=
145145
end
146146
}
147147

148-
let emit_external_warnings_on_structure (stru : Parsetree.structure) =
148+
let rec iter_warnings_on_stru (stru : Parsetree.structure) =
149+
match stru with
150+
| [] -> ()
151+
| head :: rest ->
152+
begin match head.pstr_desc with
153+
| Pstr_attribute attr ->
154+
Builtin_attributes.warning_attribute attr;
155+
iter_warnings_on_stru rest
156+
| _ -> ()
157+
end
158+
159+
let rec iter_warnings_on_sigi (stru : Parsetree.signature) =
160+
match stru with
161+
| [] -> ()
162+
| head :: rest ->
163+
begin match head.psig_desc with
164+
| Psig_attribute attr ->
165+
Builtin_attributes.warning_attribute attr;
166+
iter_warnings_on_sigi rest
167+
| _ -> ()
168+
end
169+
170+
171+
let emit_external_warnings_on_structure (stru : Parsetree.structure) =
149172
if Warnings.is_active dummy_unused_attribute then
150173
emit_external_warnings.structure emit_external_warnings stru
151174

jscomp/syntax/bs_ast_invariant.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ val warn_discarded_unused_attributes :
3434
Parsetree.attributes -> unit
3535
(** Ast invariant checking for detecting errors *)
3636

37+
38+
val iter_warnings_on_stru:
39+
Parsetree.structure -> unit
40+
41+
val iter_warnings_on_sigi:
42+
Parsetree.signature -> unit
43+
3744
val emit_external_warnings_on_structure:
3845
Parsetree.structure -> unit
3946

jscomp/syntax/bs_builtin_ppx.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ let signature_config_table : action_table =
382382

383383

384384
let rewrite_signature (x : Parsetree.signature) =
385+
Bs_ast_invariant.iter_warnings_on_sigi x;
385386
let result =
386387
match x with
387388
| {psig_desc = Psig_attribute ({txt = "ocaml.ppx.context"},_)}
@@ -399,7 +400,8 @@ let rewrite_signature (x : Parsetree.signature) =
399400
result
400401

401402
(* Note we also drop attributes like [@@@bs.deriving ] for convenience*)
402-
let rewrite_implementation (x : Parsetree.structure) =
403+
let rewrite_implementation (x : Parsetree.structure) =
404+
Bs_ast_invariant.iter_warnings_on_stru x ;
403405
let result =
404406
match x with
405407
| {pstr_desc = Pstr_attribute ({txt = "ocaml.ppx.context"},_)}

0 commit comments

Comments
 (0)