Ppxlib erases the AST when embedding an error
Current state
Currently, in case of a ppx raising an exception, ppxlib does the following:
- If it’s a located exception and either the
embed_errorsor theas-ppxflag is set, the whole AST is trashed and replaced by a single error extension node, - Otherwise, raised exception are not caught at all.
In terms of tools:
- Merlin is calling the ppx with the
as-ppxflag, - Dune is calling the ppx without the
embed_errorsoras-ppxflag (it uses the-dump-astflag).
The problem
The problem with this is the use of ppx with Merlin.
When a ppx raise a located exception, the whole AST is removed, which means that any other analysis or feature from Merlin is not available anymore. Moreover, the confused user is not given a clue about why this happen.
Finally, the ppxlib API, docs and examples does not incentive the ppx authors to embed errors in the AST but to raise exceptions, which results in many ppxes raising.
The proposed solution
The behaviour of ppxlib should be modified to replace:
- When a ppx raise a located exception and the
embed_errorsflag is set, the whole AST is trashed and replaced by a single error extension node
by
- When a ppx raise a located exception and the
embed_errorsflag is set, the last valid AST (the one given to the faulty rewriter) is kept, prefixed by an extension error node containing the error.
Moreover, the docs and API should be changed to reflect that embedding errors in the AST is the preferred way, to explain that raising located errors stops the rewriting process and that unlocated exceptions should never happen.
The tasks are the following:
- Modification of the behaviour:
- [ ] Add the last valid AST after the error exception node.
- Modification of the doc:
- [ ] Add an
error_extensionffunction is the public API, and update examples to use it. - [ ] Add a section about error reporting in the manual.
- [ ] Add an