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
11 changes: 8 additions & 3 deletions jscomp/core/melc_warnings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@
- 102 Mel_polymorphic_comparison
*)

open Import

let defaults_w = "+a-4-29-40-41-42-44-45-48-58-59-60-61-63..70-102"
let defaults_warn_error = "-a+5+6+101+109"
(*TODO: add +10*)

let parse_warnings ~warn_error w =
Option.iter Location.(prerr_alert none) (Warnings.parse_options warn_error w)
(* Set warnings for the OCaml lexer / parser, which also emit
`Illegal_backslash` (warning 14) *)
Option.iter Location.(prerr_alert none) (Warnings.parse_options warn_error w);

(* Now set the warnings for Melange_compiler_libs *)
Option.iter
Location.(prerr_alert none)
(Melange_compiler_libs.Warnings.parse_options warn_error w)
141 changes: 141 additions & 0 deletions test/blackbox-tests/flags.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
Test flags and compile_flags fields on melange.emit stanza

$ . ./setup.sh
$ cat > dune-project <<EOF
> (lang dune 3.8)
> (using melange 0.1)
> EOF

Using flags field in melange.emit stanzas is not supported

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (modules main)
> (flags -w -14-26))
> EOF

$ dune build @mel
File "dune", line 5, characters 2-7:
5 | (flags -w -14-26))
^^^^^
Error: Unknown field flags
[1]

Adds a module that contains unused var (warning 26) and illegal backlash (warning 14)

$ cat > main.ml <<EOF
> let t = "\e\n" in
> print_endline "hello"
> EOF

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (modules main)
> (alias mel))
> EOF

Trying to build triggers both warnings

$ dune build @mel
File "main.ml", line 1, characters 9-11:
1 | let t = "\e\n" in
^^
Error (warning 14 [illegal-backslash]): illegal backslash escape in string.
Hint: Single backslashes \ are reserved for escape sequences
(\n, \r, ...). Did you check the list of OCaml escape sequences?
To get a backslash character, escape it with a second backslash: \\.
File "main.ml", line 1, characters 4-5:
1 | let t = "\e\n" in
^
Error (warning 26 [unused-var]): unused variable t.
[1]

Let's ignore them using compile_flags

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (modules main)
> (emit_stdlib false)
> (alias mel)
> (compile_flags -w -14-26))
> EOF

$ dune build @mel
$ node _build/default/output/main.js
hello

Can also pass flags from the env stanza. Let's go back to failing state:

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (modules main)
> (alias mel))
> EOF

$ dune build @mel
File "main.ml", line 1, characters 9-11:
1 | let t = "\e\n" in
^^
Error (warning 14 [illegal-backslash]): illegal backslash escape in string.
Hint: Single backslashes \ are reserved for escape sequences
(\n, \r, ...). Did you check the list of OCaml escape sequences?
To get a backslash character, escape it with a second backslash: \\.
File "main.ml", line 1, characters 4-5:
1 | let t = "\e\n" in
^
Error (warning 26 [unused-var]): unused variable t.
[1]

Adding env stanza with both warnings silenced allows the build to pass successfully

$ cat > dune <<EOF
> (env
> (_
> (melange.compile_flags -w -14-26)))
> (melange.emit
> (alias mel)
> (target output)
> (emit_stdlib false)
> (modules main))
> EOF

$ dune build @mel
$ node _build/default/output/main.js
hello

Warning 102 (Melange only) is available if explicitly set

$ cat > main.ml <<EOF
> let compare a b = compare a b
> EOF

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (modules main)
> (compile_flags -w +a-70))
> EOF

$ dune build output/main.js
File "main.ml", line 1, characters 18-29:
1 | let compare a b = compare a b
^^^^^^^^^^^
Warning 102 [polymorphic-comparison-introduced]: Polymorphic comparison introduced (maybe unsafe)

But it is disabled by default

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (modules main))
> EOF

$ dune build output/main.js