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

Allowing multiple errors to be reported in one pass of the context_free phase #453

Merged
merged 20 commits into from
Jan 31, 2024

Conversation

Burnleydev1
Copy link
Contributor

No description provided.

@Burnleydev1 Burnleydev1 marked this pull request as ready for review August 4, 2023 12:41
Copy link
Collaborator

@panglesd panglesd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making good progress!

I reviewed only the "Catching extenders' exceptions". Except for minor comments, it looks good!

The embed_errors value is not passed from the driver yet, which is probably why the tests do not work yet...

src/context_free.ml Outdated Show resolved Hide resolved
Comment on lines 210 to 213
(try
E.For_context.convert_res ts ~ctxt ext
|> With_errors.of_result ~default:None
with exn when embed_errors -> (None, [ exn_to_error exn ]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to catch the exception in convert_res rather than here. One would not expect that a *_res function which returns a result type raises a located exception (the res suffix is for "result", and the result type is often used to have "more explicit errors". Here it would be a combination of both...)

That would also factor some of the code here!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess one problem here is we only catch when embed_errors is true. To preserve that behaviour we'd need to propagate the embed_errors argument to convert_res which I'm not sure is a good idea.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also unclear to me what would be the best way to deal with an exception from the actual rewriting function here. The errors returned by convert are Ast_pattern parsing errors which are slightly different from the ones we're catching here.

src/context_free.ml Outdated Show resolved Hide resolved
test/driver/exception_handling/run.t Show resolved Hide resolved
Copy link
Collaborator

@panglesd panglesd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice progress on the "constant" rewriting!

I left some small comments.

src/context_free.ml Outdated Show resolved Hide resolved
src/context_free.ml Outdated Show resolved Hide resolved
test/driver/exception_handling/run.t Outdated Show resolved Hide resolved
CHANGES.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@panglesd panglesd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvements! Only the "special function" rewriting is missing.

I left some comments.

src/context_free.ml Outdated Show resolved Hide resolved
src/context_free.ml Outdated Show resolved Hide resolved
Copy link
Collaborator

@panglesd panglesd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small batch of comments!

src/context_free.ml Outdated Show resolved Hide resolved
src/context_free.ml Outdated Show resolved Hide resolved
src/context_free.ml Outdated Show resolved Hide resolved
src/context_free.ml Outdated Show resolved Hide resolved
test/driver/exception_handling/special_functions.ml Outdated Show resolved Hide resolved
@Burnleydev1
Copy link
Contributor Author

Thank you @panglesd, I have implemented the changes requested.

Copy link
Collaborator

@NathanReb NathanReb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, great change!

Do we really need to tie this to embed_errors? Are there good reasons not to do this? It's already on by default when used through dune if I'm not mistaken. I guess this could break tests that rely on the output of the driver. Do you have any other uses cases in mind?

CHANGES.md Outdated Show resolved Hide resolved
@NathanReb
Copy link
Collaborator

The tests are broken it seems. The error extension points are printed backward and some locations were off when running the driver without -embed-errors.

I think this comes from a change in #447. I suggest we sort errors by location before inserting them in a separate PR so that the order of errors is a bit more reliable than what it is right now.

Signed-off-by: Burnleydev1 <[email protected]>
Signed-off-by: Burnleydev1 <[email protected]>
Signed-off-by: Burnleydev1 <[email protected]>
…ontext_free transformation.

Signed-off-by: Burnleydev1 <[email protected]>
Signed-off-by: Burnleydev1 <[email protected]>
Signed-off-by: Burnleydev1 <[email protected]>
Burnleydev1 and others added 4 commits January 17, 2024 16:22
Signed-off-by: Nathan Rebours <[email protected]>
Signed-off-by: Nathan Rebours <[email protected]>
@NathanReb
Copy link
Collaborator

This should be good to go except for a CI failure with tests on 4.05 and 4.04, I'm looking into it.

We discussed a slightly different approach regarding context free transformations throwing exceptions where we would inject the error node directly in the AST in place of the node that needed to be rewritten instead of leaving the original node and inserting the error at the beginning of the AST. I still believe this would be a cleaner solution but at the moment I think the solution we have is good enough to be merged. We can still improve this later on.

@NathanReb
Copy link
Collaborator

Should be good now!

Copy link
Collaborator

@panglesd panglesd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @NathanReb for the final work on this PR! I believe it is ready to be merged.

About the other approach of replacing extension nodes by error nodes in case of exceptions: I agree this is a very valid approach, and would definitely accept a PR with the changes. I would also think the current situation is good enough.

For reference, here are the pros and cons of each approach, as discussed in a dev meeting (quoting from the notes):

[Main advantages of current approach:] Coherence with error collecting from other phases, less potential for later coming whole-file transformations to mess up embedded errors. We all agree that the latter shouldn't be taken too much into consideration.
[Main advantages of replacing the extension node:] Coherence with what we ask PPX authors to do (embed the errors themselves) and no duplication of error messages in case of extension nodes, where the compiler will also emit an error if the node doesn't get replaced.

@NathanReb NathanReb merged commit edc51a4 into ocaml-ppx:main Jan 31, 2024
5 checks passed
NathanReb added a commit to NathanReb/opam-repository that referenced this pull request Feb 1, 2024
CHANGES:

- Fix `Longident.parse` so it properly handles unparenthesized dotted operators
  such as `+.` or `*.`. (ocaml-ppx/ppxlib#111, @rgrinberg, @NathanReb)

- raising an exception does no longer cancel the whole context free phase(ocaml-ppx/ppxlib#453, @Burnleydev1)

- Sort embedded errors that are appended to the AST by location so the compiler
  reports the one closer to the beginning of the file first. (ocaml-ppx/ppxlib#463, @NathanReb)

- Update `Attribute.get` to ignore `loc_ghost`. (ocaml-ppx/ppxlib#460, @ceastlund)

- Add API to manipulate attributes that are used as flags (ocaml-ppx/ppxlib#408, @dianaoigo)

- Update changelog to use ISO 8061 date format: YYYY-MM-DD. (ocaml-ppx/ppxlib#445, @ceastlund)

- Replace `Caml` with `Stdlib`. (ocaml-ppx/ppxlib#427, @ceastlund)

- When a transformation raises, the last valid AST is used as input to the upcoming
  transformations. All such errors are collected and appended as
  extension nodes to the final AST (ocaml-ppx/ppxlib#447, @Burnleydev1)

- Fix a small mistake in the man pages: Embededding errors is done by default with
  `-as-pp`, not with `-dump-ast` (ocaml-ppx/ppxlib#464, @pitag-ha)

- Set appropriate binary mode when writing to `stdout` especially for Windows
  compatibility. (ocaml-ppx/ppxlib#466, @jonahbeckford)
NathanReb added a commit to NathanReb/ppxlib that referenced this pull request Feb 2, 2024
This extra argument that was added in ocaml-ppx#453 caused breakage
in `ocsigen-i18n` that was using `Context_free.map_top_down`.
Although it's undocumented, this function is still part of the public
API. Adding an extra optional argument is not strictly speaking
non-breaking but is good enough of an effort considering the status
of this function and in particular, the fact that it only has a single
public external user for which it prevents breakage.

Signed-off-by: Nathan Rebours <[email protected]>
NathanReb added a commit to NathanReb/ppxlib that referenced this pull request Feb 2, 2024
This extra argument that was added in ocaml-ppx#453 caused breakage
in `ocsigen-i18n` that was using `Context_free.map_top_down`.
Although it's undocumented, this function is still part of the public
API. Adding an extra optional argument is not strictly speaking
non-breaking but is good enough of an effort considering the status
of this function and in particular, the fact that it only has a single
public external user for which it prevents breakage.

Signed-off-by: Nathan Rebours <[email protected]>
NathanReb added a commit to NathanReb/opam-repository that referenced this pull request Feb 5, 2024
CHANGES:

- Add an optional `embed_errors` argument to `Context_free.map_top_down` that
  controls how to deal with exceptions thrown by context-free rules.
  (ocaml-ppx/ppxlib#468, @NathanReb)

- Fix `Longident.parse` so it properly handles unparenthesized dotted operators
  such as `+.` or `*.`. (ocaml-ppx/ppxlib#111, @rgrinberg, @NathanReb)

- raising an exception does no longer cancel the whole context free phase(ocaml-ppx/ppxlib#453, @Burnleydev1)

- Sort embedded errors that are appended to the AST by location so the compiler
  reports the one closer to the beginning of the file first. (ocaml-ppx/ppxlib#463, @NathanReb)

- Update `Attribute.get` to ignore `loc_ghost`. (ocaml-ppx/ppxlib#460, @ceastlund)

- Add API to manipulate attributes that are used as flags (ocaml-ppx/ppxlib#408, @dianaoigo)

- Update changelog to use ISO 8061 date format: YYYY-MM-DD. (ocaml-ppx/ppxlib#445, @ceastlund)

- Replace `Caml` with `Stdlib`. (ocaml-ppx/ppxlib#427, @ceastlund)

- When a transformation raises, the last valid AST is used as input to the upcoming
  transformations. All such errors are collected and appended as
  extension nodes to the final AST (ocaml-ppx/ppxlib#447, @Burnleydev1)

- Fix a small mistake in the man pages: Embededding errors is done by default with
  `-as-pp`, not with `-dump-ast` (ocaml-ppx/ppxlib#464, @pitag-ha)

- Set appropriate binary mode when writing to `stdout` especially for Windows
  compatibility. (ocaml-ppx/ppxlib#466, @jonahbeckford)
@Burnleydev1 Burnleydev1 deleted the context-free branch June 9, 2024 09:10
nberth pushed a commit to nberth/opam-repository that referenced this pull request Jun 18, 2024
CHANGES:

- Add an optional `embed_errors` argument to `Context_free.map_top_down` that
  controls how to deal with exceptions thrown by context-free rules.
  (ocaml-ppx/ppxlib#468, @NathanReb)

- Fix `Longident.parse` so it properly handles unparenthesized dotted operators
  such as `+.` or `*.`. (ocaml-ppx/ppxlib#111, @rgrinberg, @NathanReb)

- raising an exception does no longer cancel the whole context free phase(ocaml-ppx/ppxlib#453, @Burnleydev1)

- Sort embedded errors that are appended to the AST by location so the compiler
  reports the one closer to the beginning of the file first. (ocaml-ppx/ppxlib#463, @NathanReb)

- Update `Attribute.get` to ignore `loc_ghost`. (ocaml-ppx/ppxlib#460, @ceastlund)

- Add API to manipulate attributes that are used as flags (ocaml-ppx/ppxlib#408, @dianaoigo)

- Update changelog to use ISO 8061 date format: YYYY-MM-DD. (ocaml-ppx/ppxlib#445, @ceastlund)

- Replace `Caml` with `Stdlib`. (ocaml-ppx/ppxlib#427, @ceastlund)

- When a transformation raises, the last valid AST is used as input to the upcoming
  transformations. All such errors are collected and appended as
  extension nodes to the final AST (ocaml-ppx/ppxlib#447, @Burnleydev1)

- Fix a small mistake in the man pages: Embededding errors is done by default with
  `-as-pp`, not with `-dump-ast` (ocaml-ppx/ppxlib#464, @pitag-ha)

- Set appropriate binary mode when writing to `stdout` especially for Windows
  compatibility. (ocaml-ppx/ppxlib#466, @jonahbeckford)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants