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

Remove res.namedArgLoc attribute and store the location information directly into the label. #7247

Merged
merged 9 commits into from
Jan 30, 2025

Conversation

cristianoc
Copy link
Collaborator

@cristianoc cristianoc commented Jan 14, 2025

Instead of the attribute res.namedArgLoc, there's now Asttypes.arg_label_loc to store labels with location.

@cristianoc cristianoc force-pushed the named-arg-loc-fundef branch 3 times, most recently from 21d50c8 to be89110 Compare January 21, 2025 15:42
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: a47a3cb Previous: e1b7fb7 Ratio
Parse RedBlackTree.res - time/run 1.3690002266666665 ms 1.2123143266666667 ms 1.13
Parse Napkinscript.res - time/run 42.84875455333333 ms 39.28006235333333 ms 1.09
Parse HeroGraphic.res - time/run 5.7314326866666665 ms 5.13472718 ms 1.12

This comment was automatically generated by workflow using github-action-benchmark.

@nojaf
Copy link
Collaborator

nojaf commented Jan 21, 2025

So, for my understanding, this moves the location from @res.namedArgLoc to named argument node?

@cristianoc
Copy link
Collaborator Author

So, for my understanding, this moves the location from @res.namedArgLoc to named argument node?

Yes. It's been used in different ast nodes. The current state of the PR covers a subset of them.

@cristianoc cristianoc changed the title Experiment with storing the location of function named arguments in t… Remove res.namedArgLoc attribute and store the location information directly into the label. Jan 24, 2025
@@ -1444,10 +1447,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
functionContextPath = ctxPath;
argumentLabel =
(match lbl with
| Nolabel ->
| Nolbl ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry to nitpick, but is there any rational for these shorter names?
As a novice to all this, I'd say it make it all a bit more obscure.
But perhaps this has a reason.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No special plan for naming at the moment.
The situation is there's an existing variant type, without locations on labels.
Here I have introduced a new variant type, with locations.
The old type still exists, as labels are used e.g. in Tarrow in types.mli, and those are generated by the compiler without reference to any source location (e.g. via unification or other reasons).

So for now I have different variant names for the new type definition, as there are quite a few places where the type is inferred by the name.
One could have 2 types with identical variants names, though that might actually add more confusion (and need to explicitly add more type annotations to indicate which type is intended).
One could try to make the payload of labelled case a type variable, to be instantiated with either string or a string plus location depending on context. I haven't tried this, not sure how that would look.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One could also use a single type and stuff a lot of Location.none when that type is not supposed to have a location. Not particularly keen on that as it becomes unclear where to expect a location and where not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here is how things look when using identical variant names for the two types: with and without locations: #7254

Copy link
Member

Choose a reason for hiding this comment

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

One could have 2 types with identical variants names, though that might actually add more confusion (and need to explicitly add more type annotations to indicate which type is intended).

I also think this probably causes more confusion.

Another idea might be to use some prefix for disambiguation, similar to Pexp_xxx vs. Texp_xxx etc. Like Lloc_label / Lloc_nolabel vs. the existing Label / Nolabel.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One could have 2 types with identical variants names, though that might actually add more confusion (and need to explicitly add more type annotations to indicate which type is intended).

I also think this probably causes more confusion.

Another idea might be to use some prefix for disambiguation, similar to Pexp_xxx vs. Texp_xxx etc. Like Lloc_label / Lloc_nolabel vs. the existing Label / Nolabel.

I've tried putting the definition without location inside an inner module Noloc (and leave the rest as is).
Only a few changes needed to add Noloc. prefix..
It might be the lowest friction way, and the definition with loc can probably be renamed back to the original name hopefully with no more changes. How does it sound?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds great!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds great!

OK done, the renaming worked.

@cristianoc cristianoc requested review from cknitt and zth January 24, 2025 15:37
@@ -8,7 +8,7 @@ module Foo = {
external component: React.componentLike<props<int, string>, React.element> = "component"
}

let t = React.createElement(Foo.component, {a: 1, b: "1"})
let t = React.createElement(Foo.component, {a: 1, b: {"1"}})
Copy link
Member

Choose a reason for hiding this comment

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

Is this test output change ok/expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that this is not a printing test, but shows the result of the jsx transform. So it aims to show the effect of the transformation.
That said, the braces { were present in the source, so it's likely that these are preserved better now because there are no res.namedArgLoc attributes around to confuse the printing logic.

@@ -51,7 +51,7 @@ module Bar = {
@res.jsxComponentProps
type props = {}

let make = (_: props) => React.jsx(Foo.make, {callback: (_, _, _) => ()})
let make = (_: props) => React.jsx(Foo.make, {callback: {(_, _, _) => ()}})
Copy link
Member

Choose a reason for hiding this comment

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

And this one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same comment as for the other ppx test

@@ -21,7 +21,7 @@ module V4C = {
~props={
type_: "text",
?className,
ref: ?(Js.Nullable.toOption(ref)->Belt.Option.map(React.Ref.domRef)),
ref: ?{Js.Nullable.toOption(ref)->Belt.Option.map(React.Ref.domRef)},
Copy link
Member

Choose a reason for hiding this comment

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

And this one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same comment as for the other ppx test

@cristianoc
Copy link
Collaborator Author

This should be good to go, after checking on a real project. Though the changes should only be about formatting, as label location does not affect compilation.

@cristianoc cristianoc force-pushed the named-arg-loc-fundef branch from 8697ecc to 1a3869f Compare January 30, 2025 09:49
@cknitt
Copy link
Member

cknitt commented Jan 30, 2025

Also tested an LGTM. Needs a rebase now because of the CHANGELOG though.

@cristianoc cristianoc force-pushed the named-arg-loc-fundef branch from 1a3869f to a47a3cb Compare January 30, 2025 13:29
Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

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

Thanks!

@cknitt cknitt merged commit afa723f into master Jan 30, 2025
19 of 20 checks passed
@cknitt cknitt deleted the named-arg-loc-fundef branch January 30, 2025 13:59
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