Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 08966af

Browse files
committedNov 25, 2022
Fix parsing/printing uncurried functions with type parameters
1 parent 0dbeadd commit 08966af

11 files changed

+226
-116
lines changed
 

‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ These are only breaking changes for unformatted code.
4242
- Fix printing of nested types in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5826
4343
- Fix issue in printing uncurried callbacks https://github.com/rescript-lang/rescript-compiler/pull/5828
4444
- Fix formatting uncurried functions with attributes https://github.com/rescript-lang/rescript-compiler/pull/5829
45-
45+
- Fix parsing/printing uncurried functions with type parameters
46+
4647
#### :nail_care: Polish
4748

4849
- Syntax: process uncurried types explicitly in the parser/printer https://github.com/rescript-lang/rescript-compiler/pull/5784 https://github.com/rescript-lang/rescript-compiler/pull/5822

‎lib/4.06.1/unstable/js_compiler.ml

+14-6
Original file line numberDiff line numberDiff line change
@@ -49806,7 +49806,7 @@ let funExpr expr =
4980649806
collectNewTypes (stringLoc :: acc) returnExpr
4980749807
| returnExpr -> (List.rev acc, returnExpr)
4980849808
in
49809-
let rec collect ~uncurried attrsBefore acc expr =
49809+
let rec collect ~uncurried ~nFun attrsBefore acc expr =
4981049810
match expr with
4981149811
| {
4981249812
pexp_desc =
@@ -49820,29 +49820,36 @@ let funExpr expr =
4982049820
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
4982149821
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
4982249822
let param = NewTypes {attrs; locs = stringLocs} in
49823-
collect ~uncurried attrsBefore (param :: acc) returnExpr
49823+
collect ~uncurried ~nFun attrsBefore (param :: acc) returnExpr
4982449824
| {
4982549825
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
4982649826
pexp_attributes = [];
4982749827
} ->
4982849828
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49829-
collect ~uncurried attrsBefore (parameter :: acc) returnExpr
49829+
collect ~uncurried ~nFun:(nFun + 1) attrsBefore (parameter :: acc)
49830+
returnExpr
4983049831
(* If a fun has an attribute, then it stops here and makes currying.
4983149832
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
4983249833
| {pexp_desc = Pexp_fun _} -> (uncurried, attrsBefore, List.rev acc, expr)
49834+
| {
49835+
pexp_desc =
49836+
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
49837+
}
49838+
when nFun = 0 ->
49839+
collect ~uncurried:true ~nFun attrsBefore acc expr
4983349840
| expr -> (uncurried, attrsBefore, List.rev acc, expr)
4983449841
in
4983549842
match expr with
4983649843
| {pexp_desc = Pexp_fun _} ->
49837-
collect ~uncurried:false expr.pexp_attributes []
49844+
collect ~uncurried:false ~nFun:0 expr.pexp_attributes []
4983849845
{expr with pexp_attributes = []}
4983949846
| {
4984049847
pexp_desc =
4984149848
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
4984249849
} ->
49843-
collect ~uncurried:true expr.pexp_attributes []
49850+
collect ~uncurried:true ~nFun:0 expr.pexp_attributes []
4984449851
{expr with pexp_attributes = []}
49845-
| _ -> collect ~uncurried:false [] [] expr
49852+
| _ -> collect ~uncurried:false ~nFun:0 [] [] expr
4984649853

4984749854
let processBracesAttr expr =
4984849855
match expr.pexp_attributes with
@@ -58043,6 +58050,7 @@ and printExpFunParameter ~state parameter cmtTbl =
5804358050
[
5804458051
printAttributes ~state attrs cmtTbl;
5804558052
Doc.text "type ";
58053+
(* XX *)
5804658054
Doc.join ~sep:Doc.space
5804758055
(List.map
5804858056
(fun lbl ->

‎lib/4.06.1/unstable/js_playground_compiler.ml

+51-36
Original file line numberDiff line numberDiff line change
@@ -49806,7 +49806,7 @@ let funExpr expr =
4980649806
collectNewTypes (stringLoc :: acc) returnExpr
4980749807
| returnExpr -> (List.rev acc, returnExpr)
4980849808
in
49809-
let rec collect ~uncurried attrsBefore acc expr =
49809+
let rec collect ~uncurried ~nFun attrsBefore acc expr =
4981049810
match expr with
4981149811
| {
4981249812
pexp_desc =
@@ -49820,29 +49820,36 @@ let funExpr expr =
4982049820
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
4982149821
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
4982249822
let param = NewTypes {attrs; locs = stringLocs} in
49823-
collect ~uncurried attrsBefore (param :: acc) returnExpr
49823+
collect ~uncurried ~nFun attrsBefore (param :: acc) returnExpr
4982449824
| {
4982549825
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
4982649826
pexp_attributes = [];
4982749827
} ->
4982849828
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49829-
collect ~uncurried attrsBefore (parameter :: acc) returnExpr
49829+
collect ~uncurried ~nFun:(nFun + 1) attrsBefore (parameter :: acc)
49830+
returnExpr
4983049831
(* If a fun has an attribute, then it stops here and makes currying.
4983149832
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
4983249833
| {pexp_desc = Pexp_fun _} -> (uncurried, attrsBefore, List.rev acc, expr)
49834+
| {
49835+
pexp_desc =
49836+
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
49837+
}
49838+
when nFun = 0 ->
49839+
collect ~uncurried:true ~nFun attrsBefore acc expr
4983349840
| expr -> (uncurried, attrsBefore, List.rev acc, expr)
4983449841
in
4983549842
match expr with
4983649843
| {pexp_desc = Pexp_fun _} ->
49837-
collect ~uncurried:false expr.pexp_attributes []
49844+
collect ~uncurried:false ~nFun:0 expr.pexp_attributes []
4983849845
{expr with pexp_attributes = []}
4983949846
| {
4984049847
pexp_desc =
4984149848
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
4984249849
} ->
49843-
collect ~uncurried:true expr.pexp_attributes []
49850+
collect ~uncurried:true ~nFun:0 expr.pexp_attributes []
4984449851
{expr with pexp_attributes = []}
49845-
| _ -> collect ~uncurried:false [] [] expr
49852+
| _ -> collect ~uncurried:false ~nFun:0 [] [] expr
4984649853

4984749854
let processBracesAttr expr =
4984849855
match expr.pexp_attributes with
@@ -58043,6 +58050,7 @@ and printExpFunParameter ~state parameter cmtTbl =
5804358050
[
5804458051
printAttributes ~state attrs cmtTbl;
5804558052
Doc.text "type ";
58053+
(* XX *)
5804658054
Doc.join ~sep:Doc.space
5804758055
(List.map
5804858056
(fun lbl ->
@@ -163461,31 +163469,34 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
163461163469
| None -> parseParameters p
163462163470
in
163463163471
let parameters =
163472+
let updateAttrs attrs = arrowAttrs @ attrs in
163473+
let updatePos pos =
163474+
match arrowStartPos with
163475+
| Some startPos -> startPos
163476+
| None -> pos
163477+
in
163464163478
match parameters with
163465163479
| TermParameter p :: rest ->
163466-
TermParameter
163467-
{
163468-
p with
163469-
attrs = arrowAttrs @ p.attrs;
163470-
pos =
163471-
(match arrowStartPos with
163472-
| Some startPos -> startPos
163473-
| None -> p.pos);
163474-
}
163480+
TermParameter {p with attrs = updateAttrs p.attrs; pos = updatePos p.pos}
163475163481
:: rest
163476163482
| TypeParameter p :: rest ->
163477-
TypeParameter
163478-
{
163479-
p with
163480-
attrs = arrowAttrs @ p.attrs;
163481-
pos =
163482-
(match arrowStartPos with
163483-
| Some startPos -> startPos
163484-
| None -> p.pos);
163485-
}
163483+
TypeParameter {p with attrs = updateAttrs p.attrs; pos = updatePos p.pos}
163486163484
:: rest
163487163485
| [] -> parameters
163488163486
in
163487+
let parameters =
163488+
(* Propagate any dots from type parameters to the first term *)
163489+
let rec loop ~dotInType params =
163490+
match params with
163491+
| (TypeParameter {dotted} as p) :: rest ->
163492+
p :: loop ~dotInType:(dotInType || dotted) rest
163493+
| TermParameter termParam :: rest ->
163494+
TermParameter {termParam with dotted = dotInType || termParam.dotted}
163495+
:: rest
163496+
| [] -> []
163497+
in
163498+
loop ~dotInType:false parameters
163499+
in
163489163500
let returnType =
163490163501
match p.Parser.token with
163491163502
| Colon ->
@@ -163505,13 +163516,19 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
163505163516
in
163506163517
Parser.eatBreadcrumb p;
163507163518
let endPos = p.prevEndPos in
163519+
let termParameters =
163520+
parameters
163521+
|> List.filter (function
163522+
| TermParameter _ -> true
163523+
| TypeParameter _ -> false)
163524+
in
163508163525
let bodyNeedsBraces =
163509163526
let isFun =
163510163527
match body.pexp_desc with
163511163528
| Pexp_fun _ -> true
163512163529
| _ -> false
163513163530
in
163514-
match parameters with
163531+
match termParameters with
163515163532
| TermParameter {dotted} :: _
163516163533
when (if p.uncurried_by_default then not dotted else dotted) && isFun ->
163517163534
true
@@ -163532,7 +163549,7 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
163532163549
in
163533163550
let _paramNum, arrowExpr, _arity =
163534163551
List.fold_right
163535-
(fun parameter (paramNum, expr, arity) ->
163552+
(fun parameter (termParamNum, expr, arity) ->
163536163553
match parameter with
163537163554
| TermParameter
163538163555
{
@@ -163550,8 +163567,8 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
163550163567
let uncurried =
163551163568
if p.uncurried_by_default then not dotted else dotted
163552163569
in
163553-
if uncurried && (paramNum = 1 || not p.uncurried_by_default) then
163554-
( paramNum - 1,
163570+
if uncurried && (termParamNum = 1 || not p.uncurried_by_default) then
163571+
( termParamNum - 1,
163555163572
(if true then
163556163573
Ast_helper.Exp.record ~loc
163557163574
[
@@ -163566,17 +163583,13 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
163566163583
None
163567163584
else funExpr),
163568163585
1 )
163569-
else (paramNum - 1, funExpr, arity + 1)
163570-
| TypeParameter {dotted; attrs; locs = newtypes; pos = startPos} ->
163571-
let uncurried =
163572-
if p.uncurried_by_default then not dotted else dotted
163573-
in
163574-
let attrs = if uncurried then uncurriedAppAttr :: attrs else attrs in
163575-
( paramNum - 1,
163586+
else (termParamNum - 1, funExpr, arity + 1)
163587+
| TypeParameter {dotted = _; attrs; locs = newtypes; pos = startPos} ->
163588+
( termParamNum,
163576163589
makeNewtypes ~attrs ~loc:(mkLoc startPos endPos) newtypes expr,
163577163590
arity ))
163578163591
parameters
163579-
(List.length parameters, body, 1)
163592+
(List.length termParameters, body, 1)
163580163593
in
163581163594
{arrowExpr with pexp_loc = {arrowExpr.pexp_loc with loc_start = startPos}}
163582163595

@@ -163797,6 +163810,8 @@ and parseParameters p =
163797163810
match parseParameterList p with
163798163811
| TermParameter p :: rest ->
163799163812
TermParameter {p with dotted = true; pos = startPos} :: rest
163813+
| TypeParameter p :: rest ->
163814+
TypeParameter {p with dotted = true; pos = startPos} :: rest
163800163815
| parameters -> parameters))
163801163816
| _ -> parseParameterList p)
163802163817
| token ->

0 commit comments

Comments
 (0)