OCaml warning 31, compiler-libs, and ppx

2019-05-18 15:46发布

问题:

I'm porting my application from OCaml 4.02.3 to 4.03.0.

Say you have the following in lexer.ml:

type t = T [@@deriving sexp]

let () =
  sexp_of_t |> ignore;
  print_endline "hai"

I'm trying to run it as following:

ocamlbuild -use-ocamlfind -pkg ppx_sexp_conv -cflags '-w @a-4-31' lexer.byte --

But I'm getting the following error:

Warning 31: files lexer.cmo and /Users/vladimir/.opam/4.03.0+flambda/lib/ocaml/compiler-libs/ocamlcommon.cma(Lexer) both define a module named Lexer
File "_none_", line 1:
Error: Some fatal warnings were triggered (1 occurrences)

I understand that compiler-libs has a module called Lexer as well and it clashes with my lexer, however:

  • I'm not trying to link compiler-libs. I understand that it is used by ppx_sexp_conv, but it's a preprocessor, it should not need to link compiler-libs into my app.

  • Warning 31 is just a warning, and I'm explicitly trying to dismiss it (-w @a-4-31) as a workaround, but that does not work. It used to work in 4.02.3.

回答1:

This error of Warning 31 is a new default behaviour of ocaml 4.03.0 compiler.

OCaml gives you Warning 31 when you link two modules of the same name. This is not specific to 4.03.0:

$ touch a.ml
$ ocamlc a.ml a.ml
File "a.cmo", line 1:
Warning 31: files a.cmo and a.cmo both define a module named A
File "a.ml", line 1:
Error: Some fatal warnings were triggered (1 occurrences)  <-- This is new in 4.03.0

OCaml 4.02.3 does not handle Warning 31 as an error by default, but OCaml 4.03.0 does:

$ ocamlc -v
The OCaml compiler, version 4.03.0
Standard library directory: /Users/XXX/.opam/4.03.0/lib/ocaml
$ ocamlc -help
...
  -warn-error <list>  Enable or disable error status for warnings according
     to <list>.  See option -w for the syntax of <list>.
     Default setting is "-a+31"

+31 makes Warning 31 error. In OCaml 4.02.3, the default setting is "-a". That's why your code is rejected not by 4.02.3 but by 4.03.0.

A workaround is to remove +31 from -warn-error switch. But the best in general is to rename your module. People have had many linking troubles hard to track down by having more than one module with the same name, and this is why 31 is now an error by default.

Additional note

Warning 31 is not a compile time warning but a link time one. Therefore if you use ocamlbuild, you have to specify -warn-error -a with -lflags instead of -cflags.