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

ppx: derive signatures #32

Merged
merged 9 commits into from
Dec 4, 2024
Merged

ppx: derive signatures #32

merged 9 commits into from
Dec 4, 2024

Conversation

andreypopp
Copy link
Collaborator

@andreypopp andreypopp commented Nov 6, 2024

Fixes #31.

Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

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

This is awesome, impressive how fast you fixed it. Thanks!

@@ -11,11 +11,16 @@ class virtual deriving : object
loc:location -> path:label -> core_type -> expression
(** a deriver can be applied to as type expression as extension node. *)

method virtual generator :
method virtual str_type_decl :
ctxt:Expansion_context.Deriver.t ->
rec_flag * type_declaration list ->
structure
(** or it can be attached to a type declaration. *)
Copy link
Member

Choose a reason for hiding this comment

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

Off-topic, but I don't understand this comment.

| Ptyp_var txt -> { txt; loc }
| _ ->
Location.raise_errorf ~loc
"type variable is not a variable"
Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like:

Suggested change
"type variable is not a variable"
"unsupported type description: only type variables such as `'a` are supported"

(feel free to ignore, i saw this error msg was used already)

let name =
match t.ptyp_desc with
| Ptyp_var txt -> { txt; loc }
| _ ->
Copy link
Member

Choose a reason for hiding this comment

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

Why do derive_of_type_declaration and gen_type_ascription support Ptyp_any but not here?

Edit: oh, because we need a name to keep drilling I guess 🤔

@jchavarri
Copy link
Member

@andreypopp is this good to merge? none of my comments is blocking.

@jchavarri
Copy link
Member

Ah, we're missing a changelog entry I guess.

@andreypopp
Copy link
Collaborator Author

Actually I think this doesn’t work properly in case you are extending a polymorphic variant decoder. This is because it generates an extra function with _poly suffix which is not being exposed in signatures.

@@ -732,52 +586,6 @@ module Conv = struct
:: next)
in
pexp_match ~loc x cases

method! derive_of_type_declaration td =
Copy link
Member

Choose a reason for hiding this comment

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

what exactly are we deleting? aren't we reducing the scope of what's supported?

Copy link
Member

Choose a reason for hiding this comment

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

i thought we would be losing the polyvar composition, but from what i could check (i tested the test in the comment below), we don't, the test keeps passing fine.

Copy link
Collaborator Author

@andreypopp andreypopp Nov 18, 2024

Choose a reason for hiding this comment

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

I’m going to work a bit more on the last commit but the gist is — instead of generating a separate function for polyvariants which decodes into None on unmatched tag, we extend error type to have a case for unmatched tag. This is why we removed code here.

ppx/test/example.ml Outdated Show resolved Hide resolved
@@ -13,7 +13,11 @@ third-party libraries.
type 'a decoder = Js.Json.t -> 'a
(** The type of a decoder combinator *)

exception DecodeError of string
type error = Json_error of string | Unpexpected_variant of string
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't seem that the second branch of the variant is used, but:

Suggested change
type error = Json_error of string | Unpexpected_variant of string
type error = Json_error of string | Unexpected_variant of string

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The second case is generated by ppx

@andreypopp
Copy link
Collaborator Author

I think this can be merged once we figure out the issue with melc in be1e907

@jchavarri
Copy link
Member

ftr melange-atdgen-codec-runtime, which depends on melange-json, breaks when I try to install using commit c669c07 above:

# File "src/atdgen_codec_decode.ml", line 1:
# Error: This expression has type string but an expression was expected of type
#          error = Json__Json_decode.error

@andreypopp
Copy link
Collaborator Author

yeah, that's a breaking change...

ppx/test/example.ml Outdated Show resolved Hide resolved
@andreypopp
Copy link
Collaborator Author

andreypopp commented Dec 1, 2024 via email

@jchavarri
Copy link
Member

@andreypopp Is there anything remaining on the PR before it's merged? The only thing I can think of is CHANGES.md entry.

(ftr the bug with melange is being tracked in melange-re/melange#1238).

@jchavarri
Copy link
Member

Re: the merge of Json and ppx runtime module discussed in #36 I think we should hold a public release until that's done, so we can bundle all breaking changes in a single version.

@andreypopp
Copy link
Collaborator Author

@jchavarri updated CHANGES, this PR is good to go, I think

agreed about doing #36 first before the next release

@anmonteiro
Copy link
Member

@andreypopp I think there are some snapshots to verify/accept -- check the build failures

@andreypopp
Copy link
Collaborator Author

I’m not sure why they are failing, did the ocamlformat update?

@anmonteiro
Copy link
Member

anmonteiro commented Dec 4, 2024

they did merge an ocamlformat release within the last 24 hours ocaml/opam-repository#26998

and the test are using it:

...
  ∗ install opam-solver             2.1.6
  ∗ install opam-state              2.1.6
  ∗ install ocamlformat             0.27.0
  ∗ install ppx_base                v0.17.0
...

@andreypopp
Copy link
Collaborator Author

Should we pin ocamlformat or avoid using it for tests?

@jchavarri
Copy link
Member

There's some issue with ocamlformat because it was upgraded today. Will fix in a new PR.

@jchavarri jchavarri merged commit 438f922 into main Dec 4, 2024
1 of 7 checks passed
@jchavarri jchavarri deleted the ppx-intf branch December 4, 2024 08:18
@jchavarri jchavarri restored the ppx-intf branch December 4, 2024 08:18
@jchavarri jchavarri deleted the ppx-intf branch December 4, 2024 08:18
@jchavarri
Copy link
Member

#44

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.

ppx: support interface files
3 participants