Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed errors in the AST instead of raising #48

Closed
panglesd opened this issue Feb 28, 2023 · 6 comments · Fixed by #51
Closed

Embed errors in the AST instead of raising #48

panglesd opened this issue Feb 28, 2023 · 6 comments · Fixed by #51

Comments

@panglesd
Copy link

Currently, when your PPX encounter an error, it uses the raise_errorf function to raise a located error.

The exception is caught by ppxlib, which in this case:

  • Catch the error,
  • stops the rewriting process
  • add the error (as a [%%%ocaml.error] extension node) to the last valid ast
  • Use the resulting AST

The interruption of the rewriting is quite bad for the user experience! The implication for the users are:

  • Since your PPX runs at the "context-free" phase, the "last valid AST" is before the context-free phase. So, no other derivers/extenders get run, which generates a lot of noise in the errors (such as "uninterpreted extensions" or "unbound identifiers")
  • Only one (meaningful) error from your PPX is reported at a time.
Example

For instance:

type t = int -> int [@@deriving yaml]
type u = int -> int [@@deriving yaml]
type v = int [@@deriving yaml]

let _ = v_of_yaml

would generate several errors:

  • Cannot derive anything for t
  • unbound value v_of_yojson

when the correct set of errors would be:

  • Cannot derive anything for t
  • Cannot derive anything for u

You can find more information about error reporting in PPXs in this section of the ppxlib manual.

❓ Would you be willing to accept contributions to this issue? I'm considering assigning its resolution as part of an outreachy internship: see more information here.

@nebadesmondc
Copy link

Hello, can i work on this?

@patricoferris
Copy link
Owner

This would be an excellent addition, thank you for taking the time to right a really detailed issue @panglesd :))

Hi @Dezzy12 ! I see no reason why not, thank you for wanting to contribute, let us know if you need any further help or assistance.

@nebadesmondc
Copy link

okay thank you. i will get to back to you in case i need some assistance

@nebadesmondc
Copy link

hi @patricoferris I opened a opened a pull request for this issue, please review

@panglesd
Copy link
Author

Note that we decided that we will change the ppxlib behaviour regarding the handling of exceptions.
Catching an exception will no longer stop the rewriting process.

So, the example I gave in the original issue is not relevant any more.
However, embedding errors still have advantages! It allows to report multiple errors, while still outputing valid AST for the part that were successful.

An updated example would be that, with embedding,

type t = int -> int
and u = int -> int
and v = int [@@deriving yaml]

let _ = v_of_yaml

will show errors for both t and u, and still define the v_of_yaml value; while currently it would show only one error and not define v_of_yaml.

@nebadesmondc
Copy link

Okay thank you

patricoferris added a commit to patricoferris/opam-repository that referenced this issue Jan 6, 2024
CHANGES:

 - Embed errors in the AST (patricoferris/ppx_deriving_yaml#51, @patricoferris and special thanks to @panglesd
   for the detailed issue in patricoferris/ppx_deriving_yaml#48)
patricoferris added a commit to patricoferris/opam-repository that referenced this issue Jan 13, 2024
CHANGES:

 - Embed errors in the AST (patricoferris/ppx_deriving_yaml#51, @patricoferris and special thanks to @panglesd
   for the detailed issue in patricoferris/ppx_deriving_yaml#48)
nberth pushed a commit to nberth/opam-repository that referenced this issue Jun 18, 2024
CHANGES:

 - Embed errors in the AST (patricoferris/ppx_deriving_yaml#51, @patricoferris and special thanks to @panglesd
   for the detailed issue in patricoferris/ppx_deriving_yaml#48)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants