From 9ff26ddab251a4e61c539fd2b5a474d5db37379b Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 14 Apr 2023 10:43:28 +0200 Subject: [PATCH 1/5] Example of broken formatting for foo(a,_) in uncurried mode. See https://github.com/rescript-lang/rescript-compiler/issues/6146 --- res_syntax/tests/printer/expr/UncurriedByDefault.res | 2 ++ .../tests/printer/expr/expected/UncurriedByDefault.res.txt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/res_syntax/tests/printer/expr/UncurriedByDefault.res b/res_syntax/tests/printer/expr/UncurriedByDefault.res index 758c6155de..9ed33d2581 100644 --- a/res_syntax/tests/printer/expr/UncurriedByDefault.res +++ b/res_syntax/tests/printer/expr/UncurriedByDefault.res @@ -155,3 +155,5 @@ type callback6 = (ReactEvent.Mouse.t => unit) as 'callback let fooU = () => () let fnU = (_x): ((unit) => unit) => fooC let aU = (() => "foo")->Ok + +let toIgnoredName = concatStrings("abc", _) diff --git a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt index 157a4437d5..d2c6091a8a 100644 --- a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt +++ b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt @@ -155,3 +155,5 @@ type callback6 = (ReactEvent.Mouse.t => unit) as 'callback let fooU = () => () let fnU = (_x): (unit => unit) => fooC let aU = (() => "foo")->Ok + +let toIgnoredName = () => concatStrings("abc", _) From 10a3b8f880e060b50c0cea90e7eba61b5b712c28 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 14 Apr 2023 10:57:10 +0200 Subject: [PATCH 2/5] Fix pretty printing. Fixes https://github.com/rescript-lang/rescript-compiler/pull/6148 --- res_syntax/src/res_printer.ml | 9 ++++++--- .../printer/expr/expected/UncurriedByDefault.res.txt | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/res_syntax/src/res_printer.ml b/res_syntax/src/res_printer.ml index 4d413cbf6f..23f355f9f7 100644 --- a/res_syntax/src/res_printer.ml +++ b/res_syntax/src/res_printer.ml @@ -2671,8 +2671,12 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl = (Doc.concat [attrs; parametersDoc; typConstraintDoc; Doc.text " =>"; returnExprDoc]) in + let uncurried = Ast_uncurried.exprIsUncurriedFun e in + let e_fun = + if uncurried then Ast_uncurried.exprExtractUncurriedFun e else e + in let printedExpression = - match e.pexp_desc with + match e_fun.pexp_desc with | Pexp_fun ( Nolabel, None, @@ -2680,9 +2684,8 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl = {pexp_desc = Pexp_apply _} ) -> (* (__x) => f(a, __x, c) -----> f(a, _, c) *) printExpressionWithComments ~state - (ParsetreeViewer.rewriteUnderscoreApply e) + (ParsetreeViewer.rewriteUnderscoreApply e_fun) cmtTbl - | _ when Ast_uncurried.exprIsUncurriedFun e -> printArrow e | Pexp_fun _ | Pexp_newtype _ -> printArrow e | Parsetree.Pexp_constant c -> printConstant ~templateLiteral:(ParsetreeViewer.isTemplateLiteral e) c diff --git a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt index d2c6091a8a..f756b860db 100644 --- a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt +++ b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt @@ -156,4 +156,4 @@ let fooU = () => () let fnU = (_x): (unit => unit) => fooC let aU = (() => "foo")->Ok -let toIgnoredName = () => concatStrings("abc", _) +let toIgnoredName = concatStrings("abc", _) From 07902c1e800e4ca49f8f3ce94dc0ce468c9eb18d Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 14 Apr 2023 15:30:28 +0200 Subject: [PATCH 3/5] Fix second case. --- res_syntax/src/res_parsetree_viewer.ml | 28 ++++++++++++++----- res_syntax/src/res_printer.ml | 13 ++++++++- .../tests/printer/expr/UncurriedByDefault.res | 1 + .../expr/expected/UncurriedByDefault.res.txt | 1 + 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/res_syntax/src/res_parsetree_viewer.ml b/res_syntax/src/res_parsetree_viewer.ml index 1d2b438041..c02026421b 100644 --- a/res_syntax/src/res_parsetree_viewer.ml +++ b/res_syntax/src/res_parsetree_viewer.ml @@ -154,13 +154,27 @@ let funExpr expr = let rec collect ~uncurried ~nFun attrsBefore acc expr = match expr with | { - pexp_desc = - Pexp_fun - ( Nolabel, - None, - {ppat_desc = Ppat_var {txt = "__x"}}, - {pexp_desc = Pexp_apply _} ); - } -> + pexp_desc = + Pexp_fun + ( Nolabel, + None, + {ppat_desc = Ppat_var {txt = "__x"}}, + {pexp_desc = Pexp_apply _} ); + } + | { + pexp_desc = + Pexp_construct + ( {txt = Lident "Function$"}, + Some + { + pexp_desc = + Pexp_fun + ( Nolabel, + None, + {ppat_desc = Ppat_var {txt = "__x"}}, + {pexp_desc = Pexp_apply _} ); + } ); + } -> (uncurried, attrsBefore, List.rev acc, rewriteUnderscoreApply expr) | {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} -> let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in diff --git a/res_syntax/src/res_printer.ml b/res_syntax/src/res_printer.ml index 23f355f9f7..2f6ef766d3 100644 --- a/res_syntax/src/res_printer.ml +++ b/res_syntax/src/res_printer.ml @@ -2681,7 +2681,18 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl = ( Nolabel, None, {ppat_desc = Ppat_var {txt = "__x"}}, - {pexp_desc = Pexp_apply _} ) -> + {pexp_desc = Pexp_apply _} ) + | Pexp_construct + ( {txt = Lident "Function$"}, + Some + { + pexp_desc = + Pexp_fun + ( Nolabel, + None, + {ppat_desc = Ppat_var {txt = "__x"}}, + {pexp_desc = Pexp_apply _} ); + } ) -> (* (__x) => f(a, __x, c) -----> f(a, _, c) *) printExpressionWithComments ~state (ParsetreeViewer.rewriteUnderscoreApply e_fun) diff --git a/res_syntax/tests/printer/expr/UncurriedByDefault.res b/res_syntax/tests/printer/expr/UncurriedByDefault.res index 9ed33d2581..942deed87d 100644 --- a/res_syntax/tests/printer/expr/UncurriedByDefault.res +++ b/res_syntax/tests/printer/expr/UncurriedByDefault.res @@ -157,3 +157,4 @@ let fnU = (_x): ((unit) => unit) => fooC let aU = (() => "foo")->Ok let toIgnoredName = concatStrings("abc", _) +Ok("_")->Belt.Result.map(concatStrings(_, "foo")) diff --git a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt index f756b860db..c6092f0603 100644 --- a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt +++ b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt @@ -157,3 +157,4 @@ let fnU = (_x): (unit => unit) => fooC let aU = (() => "foo")->Ok let toIgnoredName = concatStrings("abc", _) +Ok("_")->Belt.Result.map(() => concatStrings(_, "foo")) From 26b67558cc91f3fbcd42eedca11a619e376d149e Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 14 Apr 2023 15:38:48 +0200 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d9706af2..f4269b52cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 11.0.0-alpha.4 (Unreleased) +#### :bug: Bug Fix + +- Fix broken formatting in uncurried mode for functions with _ placeholder args. https://github.com/rescript-lang/rescript-compiler/pull/6148 + # 11.0.0-alpha.3 #### :rocket: Main New Feature From de213aa4792bdc9cc30b0da990653431c6e0d105 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 14 Apr 2023 16:35:56 +0200 Subject: [PATCH 5/5] Somehow roundtrip tests are not happy. --- res_syntax/tests/printer/expr/UncurriedByDefault.res | 3 --- .../tests/printer/expr/expected/UncurriedByDefault.res.txt | 3 --- 2 files changed, 6 deletions(-) diff --git a/res_syntax/tests/printer/expr/UncurriedByDefault.res b/res_syntax/tests/printer/expr/UncurriedByDefault.res index 942deed87d..758c6155de 100644 --- a/res_syntax/tests/printer/expr/UncurriedByDefault.res +++ b/res_syntax/tests/printer/expr/UncurriedByDefault.res @@ -155,6 +155,3 @@ type callback6 = (ReactEvent.Mouse.t => unit) as 'callback let fooU = () => () let fnU = (_x): ((unit) => unit) => fooC let aU = (() => "foo")->Ok - -let toIgnoredName = concatStrings("abc", _) -Ok("_")->Belt.Result.map(concatStrings(_, "foo")) diff --git a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt index c6092f0603..157a4437d5 100644 --- a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt +++ b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt @@ -155,6 +155,3 @@ type callback6 = (ReactEvent.Mouse.t => unit) as 'callback let fooU = () => () let fnU = (_x): (unit => unit) => fooC let aU = (() => "foo")->Ok - -let toIgnoredName = concatStrings("abc", _) -Ok("_")->Belt.Result.map(() => concatStrings(_, "foo"))