ppxlib icon indicating copy to clipboard operation
ppxlib copied to clipboard

Ppxlib erases the AST when embedding an error

Open panglesd opened this issue 4 years ago • 0 comments

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_errors or the as-ppx flag 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-ppx flag,
  • Dune is calling the ppx without the embed_errors or as-ppx flag (it uses the -dump-ast flag).

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_errors flag 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_errors flag 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_extensionf function is the public API, and update examples to use it.
    • [ ] Add a section about error reporting in the manual.

panglesd avatar Jan 12 '22 09:01 panglesd