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

Ppxlib #54

Merged
merged 14 commits into from
Jan 10, 2022
Merged

Ppxlib #54

merged 14 commits into from
Jan 10, 2022

Conversation

tatchi
Copy link
Collaborator

@tatchi tatchi commented May 16, 2021

This is my attempt at addressing ocaml-ppx/ppxlib#143. It would need careful review as I'm definitely not a ppx expert. At least the tests are passing which I guess is a good sign 😁

  • First commit d8b346c only formats the code (should make it easier to review the rest)
  • Second commit 233d9d0 actually migrate the code to using ppxlib

Closes #44

(* The equivalent of not specifying the variance explicitly.
Since the very purpose of ppx_import is to include the full definition,
it should always be sufficient to rely on the inferencer to deduce variance. *)
(Ppxlib.Asttypes.NoVariance, Ppxlib.Asttypes.NoInjectivity) ))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Definitely not sure about that one. Is Ppxlib.Asttypes.NoInjectivity correct ?

Copy link
Collaborator Author

@tatchi tatchi May 20, 2021

Choose a reason for hiding this comment

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

This change was introduced in ppxlib 0.22.0

Looks like Invariant was replaced by NoVariant but I've no clue what should be the injectivity 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is indeed tricky stuff, @gasche do you know who could help with this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I just looked at this (sorry for the delay). It's a bit tricky but I think that the current code in the PR is correct:

  • for abstract type, the default is "non injective", so NoInjectivity is the conservative choice
  • non-abstract types are always injective, so you could use Injective here, but it looks like the OCaml parser uses NoInjective by default as well and lets the type-checker figure things out afterwards.

Comment on lines 353 to 345
let find_attr s attrs =
try
Some
(List.find (fun { attr_name = x; _ } -> x.txt = s) attrs).attr_payload
with Not_found -> None
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't find the Ast_convenience.find_attr equivalent so I just inlined the function

@ejgallego
Copy link
Collaborator

Thanks a lot @tatchi ! I will give it some extended testing this week, I have some development which make extensive use of ppx_import

I'm OK with the code formatting, tho I do believe that it'd be better if you can submit it as a separate PR so we can review this one free of formatting changes.

If we are gonna format ppx_import automatically we should have it happen as part of the CI , add the ocamlformat config, and a makefile target fmt that invokes dune fmt, we should also check that @gasche and @whitequark are Ok with it.

@gasche
Copy link
Contributor

gasche commented May 16, 2021

Those who do the work decide; @ejgallego if you are doing the main work of integrating this nice-sounding PR, you get to decide on the formatting :-)

@ejgallego
Copy link
Collaborator

Those who do the work decide; @ejgallego if you are doing the main work of integrating this nice-sounding PR, you get to decide on the formatting :-)

I have a similar view, if @tatchi who is writing this PR would like to have the code formatted, that's fine to me.

@ejgallego
Copy link
Collaborator

Needs fixing for OCaml versions < 4.12:

File "src/ppx_import.ml", line 193, characters 16-21:
- 193 |       Typ.arrow label lhs (core_type_of_type_expr ~subst rhs)
-                       ^^^^^
- Error: This expression has type Asttypes.arg_label
-        but an expression was expected of type
-          Migrate_parsetree.Ast_412.Asttypes.arg_label
- ```

@tatchi tatchi force-pushed the ppxlib branch 3 times, most recently from 8d13b18 to ceb74c5 Compare May 17, 2021 05:37
@tatchi
Copy link
Collaborator Author

tatchi commented May 17, 2021

Those who do the work decide; @ejgallego if you are doing the main work of integrating this nice-sounding PR, you get to decide on the formatting :-)

I have a similar view, if @tatchi who is writing this PR would like to have the code formatted, that's fine to me.

The reason why I formatted the code is because I'm not used to doing that manually. Therefore, I knew I was going to auto-format the files changed by this PR, and figured it would be better to have a first commit that only formats the files. That would make the review easier to avoid extra diff related to formatting changes. I can definitely revert my commit and try to format the changes by hand if you prefer 😄

Needs fixing for OCaml versions < 4.12:

File "src/ppx_import.ml", line 193, characters 16-21:
- 193 |       Typ.arrow label lhs (core_type_of_type_expr ~subst rhs)
-                       ^^^^^
- Error: This expression has type Asttypes.arg_label
-        but an expression was expected of type
-          Migrate_parsetree.Ast_412.Asttypes.arg_label
- ```

I'm a bit lost between all these ast conversions 😅 I tried to re-use the Convert module but it didn't work. After some research, I stumbled upon this example and used the same method. Everything seems to compile now except for OCaml 4.04.2 due to dependencies constraints that can't be met

@tatchi
Copy link
Collaborator Author

tatchi commented May 17, 2021

Would that make sense to restrict ppx_deriving >= 5.0 since this is the version that switched to ppxlib? If so, I guess we would have to drop support for OCaml < 4.05.0 anyway since previous versions are not compatible with ppx_deriving >= 5.0

@gasche
Copy link
Contributor

gasche commented May 17, 2021

Personally I think that requiring (OCaml >= 4.05) is now very reasonable.
My compass for "which old version should we still support?" is "Debian stable", which currently uses 4.05.0.

@ejgallego
Copy link
Collaborator

The reason why I formatted the code is because I'm not used to doing that manually. Therefore, I knew I was going to auto-format the files changed by this PR, and figured it would be better to have a first commit that only formats the files. That would make the review easier to avoid extra diff related to formatting changes. I can definitely revert my commit and try to format the changes by hand if you prefer

On the contrary, I'd say let's try to avoid doing more work than what we need. If you pass me your ocamlformat config we can already merge the formatting changes in a separate PR.

@gasche
Copy link
Contributor

gasche commented May 17, 2021

I think that what @ejgallego has in mind is to have a first PR (or himself committing directly) with your pure-reformat change, and a second PR with the actual changes, in the interest of making the "whole diff" review of the PR easier.

@tatchi
Copy link
Collaborator Author

tatchi commented May 17, 2021

I think that what @ejgallego has in mind is to have a first PR (or himself committing directly) with your pure-reformat change, and a second PR with the actual changes, in the interest of making the "whole diff" review of the PR easier.

Sounds good to me, I'll open a PR with the formatting changes 😊

@tatchi tatchi mentioned this pull request May 17, 2021
@tatchi tatchi force-pushed the ppxlib branch 2 times, most recently from 97732da to 7db8969 Compare May 20, 2021 11:43
@tatchi
Copy link
Collaborator Author

tatchi commented May 20, 2021

I rebased my changes on top of master. The second commit bumps ocamlformat to the latest version because the 0.15.0 version was conflicting with ppxlib. Fortunately, it didn't introduce too many formatting changes. Let me know though if you prefer to have that as a separate PR.

@ejgallego
Copy link
Collaborator

I rebased my changes on top of master. The second commit bumps ocamlformat to the latest version because the 0.15.0 version was conflicting with ppxlib. Fortunately, it didn't introduce too many formatting changes. Let me know though if you prefer to have that as a separate PR.

Thanks @tatchi , I'd prefer to have as a separate PR if you don't mind, so review is easier.

I assume the PR is ready for review, correct? What about the CI failure?

@ejgallego ejgallego self-assigned this May 26, 2021
@tatchi tatchi force-pushed the ppxlib branch 3 times, most recently from 72b46db to c51d46f Compare May 31, 2021 06:46
@tatchi
Copy link
Collaborator Author

tatchi commented May 31, 2021

Thanks @tatchi , I'd prefer to have as a separate PR if you don't mind, so review is easier.

Alright, I removed the commit :)

I assume the PR is ready for review, correct? What about the CI failure?

Yes, it should be ready for review. The CI was failing for OCaml 4.04.2. I removed this version since it won't be supported anymore (see #54 (comment)) and now it looks ok :)

@ejgallego
Copy link
Collaborator

Great, thanks @tatchi , will review ASAP (cc: @gasche in case he want to do a pass too)

ppx_import.opam Outdated Show resolved Hide resolved
Copy link
Collaborator

@ejgallego ejgallego left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

I will leave a couple of days to get comments, do some more testing on my own, then merge.

@gasche
Copy link
Contributor

gasche commented Jun 2, 2021

I skimmed the code and it looks overall fine (the port is rather non-invasive, which is good news). There is one question on variance that is subtle, I will look at it later. (It's about moving from one version of the AST to another, rather than ppxlib per se, in a corner case of the type declaration syntax.)

My experience with these changes is that there are some failures that are not caught by running the code, but by trying them in the real world. Stuff like breakage with older OCaml versions, or breakage when people combine it with other ppxes, or use a non-Dune build system. I think that the real test with this PR will be building the reverse dependencies of ppx_import on the opam repository. I would encourage @ejgallego to try to do this as soon as possible (after merging the PR), for example (if you don't have a better workflow) by preparing a new ppx_import release on opam-repository, with the idea that we may iterate a bit more to fix issues as they come up.

@ejgallego
Copy link
Collaborator

ejgallego commented Jun 10, 2021

Note that the opam file seems to have a couple of issues:

  • the ocaml lower bound is incorrect
  • I am not sure why dune 1.2 is now the lower bound for ppx_import, I think that version won't work actually, the 1.11 bound is hard AFAICT [and in fact we should raise it to 2.0]

@tatchi , if you feel like addressing these issues it is fine, otherwise I'll tweak them myself before the release.

Regarding my testing, I'm getting this problem:

Error: Type unsupported for ppx [of_sexp] conversion
File "serlib/ser_sorts.ml", line 19, characters 2-25:
19 |   [%import: Sorts.family]

where the serlib/ser_sorts.ml file is:

type family =
  [%import: Sorts.family]
  [@@deriving sexp,yojson]

and the original type is:

type family = InSProp | InProp | InSet | InType

the build definition for this library is

(library
 (name serlib)
 (public_name coq-serapi.serlib)
 (synopsis "Serialization Library for Coq")
 (preprocess (staged_pps ppx_import ppx_sexp_conv ppx_deriving_yojson))
 (libraries coq.stm sexplib))

I tried a few tweaks without success, didn't investigate more yet.

This is a showstopper I'm afraid, as this is a basic use case of ppx_import IMHO.

Let me of course know if you want me to produce a self-contained reproduction case.

@tatchi
Copy link
Collaborator Author

tatchi commented Sep 20, 2021

I rebased the branch to bring in #61 (the failing test) and modified the code to use the new ppx_import syntax. The relevant commit is 23fd6a8

To make it work, I slightly had to change the ppxlib code in order to expose an input_name function in the Expansion_context.Extension module (see ocaml-ppx/ppxlib#284).

For the type_declaration, the only thing I had to do is to write the expand function. Then I was able to re-use the type_declaration function that contains all the logic.

For the module_type_declaration, I had to slightly change the code. The module_type function was previously receiving an input of type Ppxlib.module_type. It receives now an input of type Ppxlib__Import.package_type.

The challenge I faced is that there were some code paths that were using the module_type received as input, modifying some of its properties and returned it. Since we no longer receive a module_type as input, I had now to construct one from scratch. Not sure how it matters, especially since all the tests are passing (including the one that was failing before).

@NathanReb
Copy link

What's the status of this PR?

I think it's based on the latest ppxlib additions and should be ready for review isn't it?

I'm currently working on some follow up changes and bug fixes on ppxlib and wanted to make sure that ppx_import does not modify the type declaration attributes. I'll take a look at the code to make sure and if it is indeed the case, that should be part of the contract between ppxlib and ppx_import.

If I was wrong to assume that the need to discuss how ppx_import modifies attributes of type declarations and how to properly interpret this in ppxlib.

@tatchi
Copy link
Collaborator Author

tatchi commented Nov 15, 2021

What's the status of this PR?

I think it's based on the latest ppxlib additions and should be ready for review isn't it?

From what I remember, I should have adapted the code to what is described in this PR: ocaml-ppx/ppxlib#271 (comment) (or at least from what I understood).

All the current + the failing test are passing. So yes it should be ready for a review 😁

Copy link
Member

@pitag-ha pitag-ha left a comment

Choose a reason for hiding this comment

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

Nice, thanks for all the work @tatchi and sorry for the delay in having another look at it! By the way, you can always ping me.

The challenge I faced is that there were some code paths that were using the module_type received as input, modifying some of its properties and returned it. Since we no longer receive a module_type as input, I had now to construct one from scratch. Not sure how it matters, especially since all the tests are passing (including the one that was failing before).

See my comment below about that.

src/ppx_import.ml Outdated Show resolved Hide resolved
src/ppx_import.ml Show resolved Hide resolved
let psig =
psig_of_tsig ~subst (List.map Compat.migrate_signature_item tsig)
in
Ast_helper.Mty.mk ~attrs:[] (Pmty_signature psig)
Copy link
Member

Choose a reason for hiding this comment

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

Since we no longer receive a module_type as input, I had now to construct one from scratch. Not sure how it matters, especially since all the tests are passing (including the one that was failing before).

This place is what you were referring to, isn't it?

I'm not sure if that ever might happen, but if someone wrote

module type Foo = [%import: ...] [@inner_attribute] [@@deriving ...]

then the inner attribute would get lost now.

@ejgallego, do you think that's a problem? By the way, I'm not really sure: what's the use case of ppx_import in the context of module types? Is it really also for derivers (as in my example above) or is it actually something different? Or both?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't tell, I'll test when I can, but I think it should be OK for my use cases.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Fingers crossed.

Copy link
Member

Choose a reason for hiding this comment

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

So you have use cases of ppx_import on module types yourself?

Copy link
Collaborator

Choose a reason for hiding this comment

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

So you have use cases of ppx_import on module types yourself?

I was experimenting with something like that to avoid boilerplate as someone recently pointed out to me that this was possible, but not sure we have something in production, need to check anyways, I think coq-serapi has ~700 import statements

Copy link
Member

Choose a reason for hiding this comment

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

@ejgallego, how is this going? Have you already had time to test this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nope sorry, but will go ahead ASAP.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This place is what you were referring to, isn't it?

Yes indeed. And also here and here but as you pointed out, these are just for ocamldep so that's probably fine ?

Copy link
Member

Choose a reason for hiding this comment

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

@ejgallego, kind reminder. Have you already had the chance to look into this? :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@pitag-ha @tatchi , sorry for the delay, I'm happy to report that pinning ppx_import to this PR and using OCaml 4.12.1, coq-serapi test suite seems work fine and passes the test suite! All without a single change to the source code, congrats on the amazing work!

@pitag-ha
Copy link
Member

Hey @tatchi and @ejgallego, we've now released ppxlib.0.24.0 which contains all the new changes needed (see entries 2, 3 and 4 in release notes). Could you unpin ppxlib and instead bound it to >=0.24.0, @tatchi? And could you dig a bit into the last open question, @ejgallego? We might be close to having this mergable soon 🤞 !

@tatchi
Copy link
Collaborator Author

tatchi commented Dec 13, 2021

Thanks @pitag-ha for the review 😊 I unpinned ppxlib and bound it to >=0.24.0 as suggested.

@ejgallego
Copy link
Collaborator

Hi folks, I could finally perform some testing on my end, and this PR seems to work very well!

I thus propose to merge and do a release if everyone is happy.

@tatchi
Copy link
Collaborator Author

tatchi commented Jan 6, 2022 via email

@ejgallego
Copy link
Collaborator

I was waiting for that PR to be merged before opening a new one with the above mentioned fix. Up to you to decide if it should be included in the next release :)

I think no problem at all; I propose to merge on Monday evening, so we can maybe cook the release end of next week.

@ejgallego ejgallego merged commit 8f1892b into ocaml-ppx:master Jan 10, 2022
@ejgallego ejgallego mentioned this pull request Jan 10, 2022
@gasche
Copy link
Contributor

gasche commented Jan 10, 2022

Thanks @tatchi and @pitag-ha for the hard work, and thanks @ejgallego for the review!

@ejgallego, will you take care of preparing a new release?
(This is a major change, so I think we should be careful about the build reports for our reverse dependencies, as provided by the opam-repository CI.)

@ejgallego
Copy link
Collaborator

@ejgallego, will you take care of preparing a new release?

Yup, as soon as @tatchi adds the PR for 4.13 support.

(This is a major change, so I think we should be careful about the build reports for our reverse dependencies, as provided by the opam-repository CI.)

Sure, I think it will be 1.9.0 tho, as 2.0 may be reserved for #62 if finally goes ahead.

@tatchi
Copy link
Collaborator Author

tatchi commented Jan 10, 2022

Yup, as soon as @tatchi adds the PR for 4.13 support.

I will open a PR for that later on today :)

kit-ty-kate pushed a commit to ocaml/opam-repository that referenced this pull request Jan 13, 2022
CHANGES:

  * Migrate to ppxlib ocaml-ppx/ppx_import#54 , closes ocaml-ppx/ppx_import#44 (tatchi)

  * Drop support for OCaml `4.04.2`. Minimal supported version is now `4.05.0` ocaml-ppx/ppx_import#54 (tatchi)

  * Bump minimum dune version to 1.11 ocaml-ppx/ppx_import#56 (tatchi)

  * Update CI to test OCaml 4.12.0, no changes required
    (ocaml-ppx/ppx_import#53, Emilio J. Gallego Arias)

  * Remove the OCaml upper bound in the opam file
    (ocaml-ppx/ppx_import#53, Emilio J. Gallego Arias, kit-ty-kate)
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.

Migrate to ppxlib
6 participants