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

Add parameter location information to parsetree/typedtree #12391

Merged

Conversation

ncik-roberts
Copy link
Contributor

As a follow-up to #12236, this PR adds parameter locations to the parsetree/typedtree for merlin's benefit. I discussed this change with @voodoos, who suggested this PR would be a good idea — maybe he would be a good reviewer as a merlin maintainer?

As an example:

Prior to #12236:

let f x ~(y : int) = ()
        ^^^^^^^^^^^^^^^ exp_loc2
      ^^^^^^^^^^^^^^^^^ exp_loc1

where exp_loc1 is for the outer Texp_function and exp_loc2 is for the inner Texp_function.

After #12236:

let f x ~(y : int) = ()
          ^ pat_loc2
      ^ pat_loc1
      ^^^^^^^^^^^^^^^^^ exp_loc

There's now just a single Texp_function. Notably, there's no location that identifies the tilde or the opening paren as being associated with the y param — previously this could have been obtained from the start position of the exp_loc2 location.

After this PR:

let f x ~(y : int) = ()
        ^^^^^^^^^^ param_loc2
      ^ param_loc1
      ^^^^^^^^^^^^^^^^^ exp_loc

(pat_loc1 and pat_loc2 are the same as the previous example.) Now, merlin can see if the user's cursor is on any part of the parameter, including the tilde or the surrounding parens.

I also take this opportunity to reverse a change in the column number indicated by a Match_failure exception that was introduced in #12236 and that we discussed in the comments. It seems better to avoid changing the exception's arguments if we can, and this PR makes it easy to do this.

Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

Overall I like this PR, we want to keep Merlin happy and adding more locations is rarely a bad move. I decided to add some decision fatigue with a bike-sheddy question on the parsetree definition, to resolve before merging. Otherwise I read the code and it is fine.

and function_param =
{ pparam_loc : Location.t;
pparam_desc : function_param_desc;
}
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure whether one should rather use this or something like

and function_param = function_param_desc loc

which is isomorphic.

Today in the parsetree codebase:

  • We only use the _desc / _loc patterns for records that have other metadata fields than just locations (typically attributes, sometimes more). This suggests using ` loc .
  • We only use foo loc for "external" foos that are themselves defined in Parsetree, typically string or label or string option. This suggests using a _desc / _loc record.

Any opinion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My opinion is that we should use a dedicated record for the second reason you gave. As another point in that direction, the two fields of loc are loc and txt, and txt does not seem like a very good name for a field that contains the parsed function parameter.

@voodoos
Copy link
Contributor

voodoos commented Jul 25, 2023

Thanks Nick. I didn't have a close look at the code but the proposed changes should effectively allow Merlin to give more relevant information when queried on the enclosing parenthesis or another token that is not part of the parameter name itself.

Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

My bike-shedding question has been answered, I think this is good to go.

@gasche
Copy link
Member

gasche commented Jul 25, 2023

(This needs a rebase apparently?)

@ncik-roberts ncik-roberts force-pushed the syntactic-arity-more-typedtree-locations branch from ff1d647 to 694ed0a Compare July 25, 2023 12:47
@ncik-roberts
Copy link
Contributor Author

All rebased.

@ncik-roberts ncik-roberts force-pushed the syntactic-arity-more-typedtree-locations branch from 694ed0a to 93e918d Compare August 2, 2023 19:17
@ncik-roberts
Copy link
Contributor Author

I rebased to resolve a small merge conflict with #12428.

@gasche gasche merged commit a8f8726 into ocaml:trunk Aug 2, 2023
8 of 9 checks passed
@gasche
Copy link
Member

gasche commented Aug 2, 2023

(cc @panglesd)

@gasche
Copy link
Member

gasche commented Aug 2, 2023

Thanks @ncik-roberts, and apologies for missing the merge window last time.

@panglesd
Copy link
Contributor

panglesd commented Aug 3, 2023

Thanks @gasche for the ping!

I opened ocaml-ppx/ppxlib#451 two days ago when discovering #12044 and #12236, but I keep missing parsetree changes PRs until after they are merged...

On #12236, when writing the migration functions, I indeed noticed that some locations were missing. I will update my PR with this one to check if the migrations can now be written successfully.

If the new parsetree does not include at least all the information for a downgrade migration, using ppxlib with an old version of OCaml will mean working with a parsetree where some informations have been lost...
In order to test that the new parsetree has at least as much information as before, I think it would be really nice to ping us ppxlib maintainer before merge: so that we can write the migration functions before the PR is merged, and suggest modification to the parsetree if needed.

By the way, I liked the parsetree-change label, I think it helps PPX-related people! It's missing here, and in the previous two PRs

@Octachron Octachron added the parsetree-change Track changes to the parsetree for that affects ppxs label Aug 3, 2023
@Octachron
Copy link
Member

The lack of label was a lapse on my side, I have fixed it, but this sounds like a task well-suited for a bot.

@gasche
Copy link
Member

gasche commented Aug 3, 2023

@panglesd would you like to contribute some form of automation that enforces a label on PRs that modify parsetree.mli? One way I personally know how to do it is to reuse the logic in the "typo" CI check that looks for Changes modifications, and can be disabled by using the no-changes-related label. (The UI if we do this would be: if people forget to add the label, they get a CI failure. It is more manual than an entirely automated labelling, but better than what we have today.)

@panglesd
Copy link
Contributor

panglesd commented Aug 3, 2023

would you like to contribute some form of automation that enforces a label on PRs that modify parsetree.mli?

Sure! Thanks for the idea, I think with the typo example I should be able to do it. (Also, that's on github's size but I would love to be able to "watch" a specific label, but that seems to not be possible...)

ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Sep 11, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Sep 12, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Sep 13, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Sep 14, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Sep 15, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Oct 4, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Oct 5, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Oct 5, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Oct 5, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ncik-roberts/flambda-backend that referenced this pull request Oct 17, 2023
Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)
ncik-roberts added a commit to ocaml-flambda/flambda-backend that referenced this pull request Dec 28, 2023
* Newtypes

* Constraint/coercion

* Add map_half_typed_cases

* Implement type-checking/translation

This also promotes tests whose output changes.

* Add upstream tests

Tests from:
  - ocaml/ocaml#12236 (and the corresponding updates to outputs found in ocaml/ocaml#12386 and ocaml/ocaml#12391)
  - ocaml/ocaml#12496 (not merged)

* Fix ocamldoc

* Update chamelon minimizer

* Respond to requested changes to minimizer

* update new test brought in from rebase

* Fix bug in chunking code

* `make bootstrap`

* Add Ast_invariant check

* Fix type-directed disambiguation of optional arg defaults

* Minor comments from review

* Run syntactic-arity test, update output, and fix printing bug

* Remove unnecessary call to escape

* Backport changes from upstream to comparative alloc tests

* Avoid the confusing [Split_function_ty] module

* Comment [split_function_ty] better.

* [contains_gadt] as variant instead of bool

* Calculate is_final_val_param on the fly rather than precomputing indexes

* Note suboptimality

* Get typecore typechecking

* Finish resolving merge conflicts and run tests

* make bootstrap

* Add iteration on / mapping over locations and attributes

* Reduce diff and fix typo in comment:

* promote change to zero-alloc arg structure

* Undo unintentional formatting changes to chamelon

* Fix minimizer

* Minimize diff

* Fix bug with local-returning method

* Fix regression where polymorphic parameters weren't allowed to be used in same parameter list as GADTs

* Fix merge conflicts and make bootstrap

* Apply expected diff to zero-alloc test changed in this PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-me parsetree-change Track changes to the parsetree for that affects ppxs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants