Help debugging a ppx extension?

I’ve been converting sedlex to use Uchar.t instead of int (partially as an exercise as I’m not very experienced with OCaml, partially because it seems worthwhile.) I’m pretty sure I messed up and forgot to do the conversion somewhere because running the tests I get this error:

$ make test
ocamlfind ocamlc -package gen -ppx "../src/syntax/ppx_sedlex --as-ppx" -I ../src/lib -o tokenizer -linkpkg sedlexing.cma tokenizer.ml
File "_none_", line 1:
Error: This expression has type Uchar.t option
   but an expression was expected of type int
make: *** [tokenizer] Error 2

Unfortunately, this error message is a bit opaque, because rather than telling me where in my actual code the problem is, it is telling me something went wrong in an unknown place in my generated code.

I’m pretty sure I could find this immediately if I could see the specific code that was generated by the ppx_sedlex rewriter, but I’m not really aware of how to do that.

Can anyone tell me a reasonable way to produce the rewritten file, so I can run it through the compiler and get a line number corresponding to the piece of code with the problem and I can get a hint about what I messed up?

Alternatively, is there a better way to debug a ppx rewriter?

Also, I should note that this points out an unfortunate problem: if there’s a bug in ppx preprocessed code, the error messages you get are less than ideal. I’m not sure what to do about that, but it is a problem…

1 Like

I usually pass -dsource to the compiler so that it will output the source to stderr.
Then I redirect this output to a temporary file, edit it to remove the last lines (that
are the error found by the compiler - as you reported it), and finally compile this
temporary file.

You then get the location of the error in the generated code (that has been output
to the temporary file) instead of the phantom location.

1 Like