Skip to content

Convert -extension local to use Jane syntax #1586

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

Closed
wants to merge 6 commits into from

Conversation

antalsz
Copy link
Contributor

@antalsz antalsz commented Jul 21, 2023

Naturally, this comes with some Jane syntax changes, designed to make it easier to work with the attribute encoding without having to explicitly think about attributes.

This shouldn't change the user-facing syntax at all, but it removes the ability to explicitly write attributes (e.g., [@local]) or extension nodes (e.g., [%exclave]). Some things are left as attributes: [@tail] and [@nontail], because they really do want to be explicit modifiers on terms; and [@curry] in terms, although see below. The implicit [@curry] in types is left alone as an attribute, but its name is changed.

There is one outstanding question: I left the handling of [@curry] on fun terms alone, as an explicit attribute, but I wasn't sure if that was correct.

We leave %extension.escape alone because @ncik-roberts's forthcoming syntactic function arity parsing PR should eliminate it.

Reviewer: @ncik-roberts, I think!

EDIT: unboxed-primitive-args/test.ml is failing locally because it can't find caml/mlvalues.h; I haven't tried to debug this yet, but I can't see how it's related to these changes.

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

I've given it some light review but haven't engaged with the substantial Jane Syntax changes yet.

@antalsz
Copy link
Contributor Author

antalsz commented Jul 26, 2023

This PR needs updating to handle locations on local_ x without encoding them as (); x; this is semantically idempotent, but is probably a weaker notion of erasability than we want. Open to suggestions that I ought to patch ocamlformat for this instead, though. This is now done.

@antalsz antalsz force-pushed the local-modular-extension branch 2 times, most recently from 944ca6e to c862514 Compare July 26, 2023 23:52
@antalsz
Copy link
Contributor Author

antalsz commented Jul 27, 2023

@ncik-roberts, I think the open conversations and new code need your review now, but I've addressed most of your comments!

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

Not completely done yet, but here are some more comments.

@ncik-roberts
Copy link
Contributor

My most recent commit reworks the type of match_jane_syntax_piece to make it appear more clearly inverse to make_jane_syntax. This involves pushing an error message generation out to the clients in jane_syntax.ml, so there is a trade-off involved, but I feel like the most straightforward (IMO) code structure is a good trade. Dropping this commit is certainly on the table.

@ncik-roberts ncik-roberts force-pushed the local-modular-extension branch from e98bc19 to 9886a02 Compare July 28, 2023 18:46
Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

I just pushed a failing test highlighting some issues with how attributes are handled.

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

A little more review, a few more comments. Just typechecking left, and then this will be fully reviewed.

Copy link
Collaborator

@goldfirere goldfirere left a comment

Choose a reason for hiding this comment

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

In a conversation with @antalsz, we agreed that this patch would have important effects on ppxs. Merge only with caution. To help us not forget this, I'm putting in a "Request changes". (I'm not sure the exact semantics of this, but I think it shows up in the block that appears above the merge button as a helpful reminder.)

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

Ok, I think I'm all done with this review attempt. Let me know when you'd like me to take a look after you've had a chance to look at my comments.

@antalsz
Copy link
Contributor Author

antalsz commented Aug 4, 2023

@ncik-roberts I made the location saving/restoring automatic, good call

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

The changes look good. The remaining things left:

  • I have a minor comment for trying to reduce the diff of this last piece.
  • Richard left a comment to mark the fact that this may affect ppxes.
  • There are a few threads to mark resolved if you agree that they are in fact resolved.

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

Let's fix the test that's commented out in #1727 — currently, this program's parsetree doesn't round-trip with respect to Pprintast:

let local_ f x : int -> int = fun y -> x + y in
()

The output is something like:

let local_ f x = ((fun y -> x + y) : (int -> int)) in
()

But the parser treats the type annotation on the LHS of = specially for let local_ f: it inserts an Lexp_constrain_local.

Fixing this bug will allow us to uncomment the test in #1727.

Copy link
Contributor

@ncik-roberts ncik-roberts 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. (Note Richard's unresolved comment — you were going to check that ppxes are ok before we merge this.)

@antalsz antalsz force-pushed the local-modular-extension branch from ecba032 to 71d4b5c Compare August 15, 2023 00:27
@antalsz
Copy link
Contributor Author

antalsz commented Aug 15, 2023

@ncik-roberts: Sorry about the force push; I needed to add some new tests to validate the issue in #1727. Luckily it actually only adds two commits for you to review:

  1. c3aa354: Adds some new tests about printing of type annotations in bindings with and without local_, and adds some round trip checks; at this time, local_ fails those checks.
  2. 71d4b5c: Fix printing and fix the aforementioned tests.

It's worth noting that this uncovers an existing bug in pprintast's roundtripping:

let broken_coerce_to_lhs :> int = 42;;

becomes

let broken_coerce_to_lhs : int = (42 :> int);;

I haven't tried to fix that in this PR; unless it's very simple to do, it probably belongs elsewhere.

@antalsz
Copy link
Contributor Author

antalsz commented Aug 15, 2023

Sorry, I should clarify: this does fix the printing issue highlighted in #1727!

@ncik-roberts
Copy link
Contributor

ncik-roberts commented Aug 15, 2023

I think these last two commits should be split out into another PR. This PR is already massive, and the last two commits were (a) already issues at the base, and (b) are semantically fairly separate from the reset of the work you did. I think that will make this PR easier to merge and those two commits easier to review in isolation.

(But thanks for fixing this problem — pprintast bugs are annoying.)

@antalsz
Copy link
Contributor Author

antalsz commented Aug 21, 2023

Just caught a Pprintast bug and added a couple of commits to fix it

Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

The most recent two commits (ending in "Fix printing of global_ in tupled constructors") look good to me -- I just have a minor comment that you can resolve upon addressing.

@antalsz antalsz force-pushed the local-modular-extension branch from 836a1ee to 5738720 Compare August 23, 2023 21:03
@antalsz
Copy link
Contributor Author

antalsz commented Aug 23, 2023

@ncik-roberts: I have split off the two commits you requested and force-pushed. The current range of commits is 85c202c..5738720. I will be squashing and rebasing this PR soon.

@antalsz antalsz force-pushed the local-modular-extension branch from 5738720 to 5d807a3 Compare August 28, 2023 19:34
This is the squashed form of commits
85c202c..5738720,
whose individual commit messages are below, followed by rebasing onto `main`.

I'm committing the conflicts to hopefully make it possible to review the
resolution of the rebase.  We'll see how well this works.

----------

Add matches on `Jane_syntax.Core_type.of_ast`

Add the `local` extension, but don't use it yet

Power `local_` with the modular syntax (but not other modal things)

Power `global_` with the modular syntax machinery

Update the parser to use Jane syntax for `local_`; buggy w/ `global_`

Fix `Local.of_constr_arg`'s attribute handling

Update ocamlprof

Add constructor argument to `depend.ml`

Make `local_` in types contain more attributes

This lets us distinguish it from constructor arguments better

Correctly update attributes when matching on types/constructor args

Bootstrap

`make alldepend`

Fix printing bug for `local_`

Refactor attribute handling

This is a big change to `Jane_syntax_parsing`

Update passing tests

Add `Lexp_constrain_local`

Fix `local_` in patterns & the location of `local_` in expressions

Delete stray outdated comments

Move `exclave_` over to Jane syntax

Remove `extension.` options for `[@{non,}tail]`

Move curry-tracking over to Jane syntax

Reorder attribute addition for comprehensions

Rewrite the missed uses of `[%exclave]` to `exclave_`

Add some missing documentation

Make `Jane_syntax_parsing` no longer depend on `Misc`

Bootstrap

Update ocamlprof

Respond to simple parts of Nick's review

Formatting (and content) update for `Jane_syntax` example comments

Add test of attributes in `local_ (t[@A])`; currently handled wrong

Update: `get_alloc_mode` → `unwrap_mode`, document it, and fix a bug

Jane syntax constructor arguments don't come with attributes

Add missing invariant documentation

Handle locations for `local_` and `exclave_` without using `(); x`

Add some missing uses of `Location.ghostify`

Rework type of match_jane_syntax_piece

Add regression test (currently failing) for attribute handling

Respond to small pieces of @ncick-roberts's review

Remove `~attr` arguments from individual `Jane_syntax` extensions

More reponses to @ncick-roberts's review, including a bug fix

Fix `pprintast`'s handling of attributes on `let x = local_ e`

Fix bug with parenthesization of `local_`

Handle saving/restoring locations with `loc_stack`s

Minor review response

Remove `Embedded_name.marker_attribute_handler` machinery

Tweak `match_jane_syntax` (formerly `match_jane_syntax_piece`)

Make `save_location` and `restore_location` automatic

Reduce the parser diff

Per @riaqn, adjust the type of `check_eq_mode`; also, rename it

Added test demonstrating a bug in `Pprintast`

Currently, `Pprintast` does not print `global_` on tupled constructor fields
correctly, and instead crashes.

```
% cat test.ml; echo; _install/bin/ocamlc -dsource test.ml
type t = C of global_ string

type t =
  | C of
  File "test.ml", line 1, characters 14-21:
  1 | type t = C of global_ string
                    ^^^^^^^
  Error: Tried to restore a saved location here, but none were found.
```

This is a regression from the previous broken behavior of printing the attribute
literally:

```
% cat test.ml; echo; ocamlc -dsource test.ml
type t = C of global_ string

type t =
  | C of ((string)[@extension.global ])
```

The next commit will fix `Pprintast` to have the correct behavior

Fix printing of `global_` in tupled constructors

Switch from `%(%)` to `%t`
@antalsz antalsz force-pushed the local-modular-extension branch from 5d807a3 to 8dca801 Compare August 28, 2023 19:37
@goldfirere
Copy link
Collaborator

In the end, we've decided to go a different direction here, keeping a lighter-weight encoding for local annotations. There is one piece of it all not yet complete, but is tracked by an internal ticket. Closing.

@goldfirere goldfirere closed this Feb 21, 2024
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.

4 participants