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

Fix @this attribute dropping parens #7025

Merged
merged 7 commits into from
Sep 27, 2024

Conversation

shulhi
Copy link
Member

@shulhi shulhi commented Sep 8, 2024

Fix #6638

@cknitt Can you look at the diff output for the test and see whether they are acceptable in terms of formatting changes? They look okay to me. https://github.com/rescript-lang/rescript-compiler/pull/7025/files#diff-2333e082552a974a4c2fc20db5dbd0415ae96a46b675d967a6d9804f7497ad08

I've checked all the attributes we have here (https://rescript-lang.org/syntax-lookup) and there are only a few that is at field level (e.g. @as, @this, etc).

I had worked on this during the retreat but Max preferred not to hard code attribute value for the check, so this PR try to generalize the check without checking for specific attribute. (Prev PR: #6773).

Comment on lines 3 to 26
let attrs_string attrs =
List.map (fun (attr, _) -> print_endline attr.Asttypes.txt) attrs

let ident_string ident =
match ident with
| Longident.Lident v -> v
| Longident.Ldot (_, v) -> v
| Longident.Lapply _ -> "Lapply"

let print_ct ct =
match ct with
| {ptyp_desc = Ptyp_constr (ident, _); ptyp_attributes = attrs} ->
let _ =
print_endline
("Ptyp_constr: " ^ ident_string ident.txt ^ " attrs: "
^ string_of_int (List.length attrs))
in
let _ = attrs_string attrs in
()
| {ptyp_desc = Ptyp_arrow _; ptyp_attributes = attrs} ->
print_endline
("Ptyp_arrow: " ^ " attrs: " ^ string_of_int (List.length attrs))
| {ptyp_desc = Ptyp_variant _} -> print_endline "Ptyp_variant"
| _ -> print_endline "Something else"
Copy link
Member Author

Choose a reason for hiding this comment

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

Just some debugging code, I'll remove it if we think the solution to this is fine.

let print_arrow ?(arity = max_int) typ_expr =
let attrs_before, args, return_type =
ParsetreeViewer.arrow_type ~arity typ_expr
ParsetreeViewer.arrow_type ~arity ~attrs:parent_attrs typ_expr
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's where the issue lies, the @this attribute is attached to the Ptyp_constr not Ptyp_arrow. Essentially what I'm doing here is just picking up the attributes from the Ptyp_constr and pass it as the initial attrs for the ParsetreeViewer.arrow_type to work with.

@@ -1841,6 +1845,8 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
in
let should_print_its_own_attributes =
match typ_expr.ptyp_desc with
| Ptyp_constr _ when Ast_uncurried.core_type_is_uncurried_fun typ_expr ->
Copy link
Member Author

Choose a reason for hiding this comment

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

Let the attribute printing handled by print_typ_expr, otherwise it's gonna cause duplicated attributes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to add a task to remove dead code once uncurried is the only possible function type

@shulhi
Copy link
Member Author

shulhi commented Sep 8, 2024

If they are okay, I'll clean this PR and add more tests. The linked diff is the only broken existing test.

@cknitt
Copy link
Member

cknitt commented Sep 8, 2024

Awesome! Output diff looks ok to me.

Let's ask @IwanKaramazow and @cristianoc to have a look at the changes, too.

@cknitt
Copy link
Member

cknitt commented Sep 12, 2024

@cristianoc @zth could you have a look at this, too?

Here's where the issue lies, the @this attribute is attached to the Ptyp_constr not Ptyp_arrow. Essentially what I'm doing here is just picking up the attributes from the Ptyp_constr and pass it as the initial attrs for the ParsetreeViewer.arrow_type to work with.

Is this approach fine? Or is there something that should be changed in the parser?

@zth
Copy link
Collaborator

zth commented Sep 17, 2024

My hunch would be that the parser should parse this correctly instead. But I'm not very familiar with the parser. @cristianoc what do you think?

@cristianoc
Copy link
Collaborator

What's special about @this for parsing?
Shouldn't it be like any other annotation?

@shulhi
Copy link
Member Author

shulhi commented Sep 18, 2024

What's special about @this for parsing? Shouldn't it be like any other annotation?

It should just be like any other annotation. This PR just changed how the attribute is printed (i.e. with parens).

@cristianoc
Copy link
Collaborator

What's special about @this for parsing? Shouldn't it be like any other annotation?

It should just be like any other annotation. This PR just changed how the attribute is printed (i.e. with parens).

OK sorry same Q: does @this need special printing, or it it the same for all attributes?

@cristianoc
Copy link
Collaborator

What's special about @this for parsing? Shouldn't it be like any other annotation?

It should just be like any other annotation. This PR just changed how the attribute is printed (i.e. with parens).

OK sorry same Q: does @this need special printing, or it it the same for all attributes?

Which answer itself as soon as one looks at the changed files. So please ignore the question.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

There's a simple way to check if this is correct. Which you might have done already.

Add an example so that the roundtrip tests fail. This PR then fixes that issue.

If that's the case, then ready to go after cleanup.

@shulhi shulhi force-pushed the fix-arrow-attr-parens branch 2 times, most recently from fb7b67e to 9254df9 Compare September 23, 2024 06:47
Shulhi Sapli and others added 5 commits September 26, 2024 19:55
Check for existence of attr at Ptyp_constr (function$) as the @this
attribute is attached to the Ptyp_constr, not the Ptyp_arrow.
@shulhi shulhi force-pushed the fix-arrow-attr-parens branch from 9254df9 to 0b48963 Compare September 26, 2024 11:55
Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Looks great -- would you add a changeling?

@shulhi shulhi marked this pull request as ready for review September 27, 2024 01:27
@cknitt cknitt merged commit 9c4af76 into rescript-lang:master Sep 27, 2024
20 checks passed
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.

@this is breaking in rescript v11
4 participants