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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#### :bug: Bug fix

- Fix tuple coercion. https://github.com/rescript-lang/rescript-compiler/pull/7024
- Fix attribute printing. https://github.com/rescript-lang/rescript-compiler/pull/7025

#### :nail_care: Polish

Expand Down
9 changes: 5 additions & 4 deletions jscomp/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Parsetree

let arrow_type ?(arity = max_int) ct =
let arrow_type ?(arity = max_int) ?(attrs = []) ct =
let has_as_attr attrs =
Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as")
in
Expand Down Expand Up @@ -45,10 +45,11 @@ let arrow_type ?(arity = max_int) ct =
| typ -> (attrs_before, List.rev acc, typ)
in
match ct with
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs} as
typ ->
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs1}
as typ ->
let attrs = attrs @ attrs1 in
process attrs [] {typ with ptyp_attributes = []} arity
| typ -> process [] [] typ arity
| typ -> process attrs [] typ arity

let functor_type modtype =
let rec process acc modtype =
Expand Down
1 change: 1 addition & 0 deletions jscomp/syntax/src/res_parsetree_viewer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* we restructure the tree into (a, b, c) and its returnType d *)
val arrow_type :
?arity:int ->
?attrs:Parsetree.attributes ->
Parsetree.core_type ->
Parsetree.attributes
* (Parsetree.attributes * Asttypes.arg_label * Parsetree.core_type) list
Expand Down
8 changes: 7 additions & 1 deletion jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1591,9 +1591,13 @@ and print_label_declaration ~state (ld : Parsetree.label_declaration) cmt_tbl =
])

and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
let parent_attrs =
let attrs = ParsetreeViewer.filter_parsing_attrs typ_expr.ptyp_attributes in
if Ast_uncurried.core_type_is_uncurried_fun typ_expr then attrs else []
in
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.

in
let return_type_needs_parens =
match return_type.ptyp_desc with
Expand Down Expand Up @@ -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

true
| Ptyp_arrow _ (* es6 arrow types print their own attributes *) -> true
| _ -> false
in
Expand Down
44 changes: 22 additions & 22 deletions jscomp/syntax/tests/printer/typexpr/expected/arrow.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ type t = (
type t = (@attr string, @attr float) => unit
type t = (@attr @attr2 string, @attr @attr2 float, @attr3 int) => unit

type t = @attr string => unit
type t = @attr (string => unit)
type t = @attr (foo, bar, baz) => unit
type t = @attr (foo, @attr2 ~f: bar, @attr3 ~f: baz) => unit

type t = @attr string => @attr int => unit
type t = @attr (string => @attr (int => unit))
type t = @attr (string, int) => @attr (int, float) => unit
type t = @attr int => @attr (int, float) => @attr unit => unit => unit
type t = @attr (int => @attr (int, float) => @attr (unit => unit => unit))
type t = @attr (@attr2 ~f: int, @attr3 ~g: float) => unit

type f = (
Expand All @@ -144,50 +144,50 @@ type f = (
@attr3 ~h: ccccrazysldkfjslkdjflksdjkf=?,
) => unit

type t = @attr
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr2
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr3
intWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => unitWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
type t = @attr (
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr2 (
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr3 (
intWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => unitWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
)
)
)

type t = @attr
(
type t = @attr (
fooWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
barWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
bazWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
) => @attr2
(
) => @attr2 (
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
) => unit

type t = @attr
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
(
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong (
fooWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
barWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
bazWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
) => @attr2
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
(
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong (
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
) => unit

external debounce: (int, @meth unit) => unit = "debounce"

external debounce: int => @meth unit => unit = "debounce"
external debounce: int => @meth (unit => unit) = "debounce"

external debounce: (int, @meth unit => unit) => @meth unit => unit = "debounce"
external debounce: (int, @meth (unit => unit)) => @meth (unit => unit) = "debounce"

external debounce: (int, @meth unit => unit, @meth unit => unit) => @meth unit => unit = "debounce"
external debounce: (int, @meth (unit => unit), @meth (unit => unit)) => @meth (unit => unit) =
"debounce"

external debounce: (
int,
@meth unit => unit,
@meth unit => @meth unit => unit,
) => @meth unit => unit = "debounce"
@meth (unit => unit),
@meth (unit => @meth (unit => unit)),
) => @meth (unit => unit) = "debounce"

type returnTyp = (int, int) => @magic float
type returnTyp = (
Expand All @@ -214,7 +214,7 @@ type t = (@attrOnInt int, @attrOnInt int, @attrOnInt int, @attrOnInt int) => int
type t = (@attr ~x: int, ~y: int, @attr ~z: int, @attr ~omega: int) => unit

@val external requestAnimationFrame: (float => unit) => unit = "requestAnimationFrame"
@val external requestAnimationFrame: @attr (float => unit) => unit = "requestAnimationFrame"
@val external requestAnimationFrame: @attr ((float => unit) => unit) = "requestAnimationFrame"

type arrows = (int, (float => unit) => unit, float) => unit

Expand Down
3 changes: 1 addition & 2 deletions jscomp/test/reasonReact.res
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ and oldNewSelf<'state, 'retainedProps, 'action> = {
type rec jsComponentThis<'state, 'props, 'retainedProps, 'action> = {
"state": totalState<'state, 'retainedProps, 'action>,
"props": {"reasonProps": 'props},
"setState": @meth
(
"setState": @meth (
(
totalState<'state, 'retainedProps, 'action>,
'props,
Expand Down