From 51108cb6612870f1186387e598fe432a2fdce511 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 11 Sep 2022 07:31:37 +0200 Subject: [PATCH 1/2] Synx syntax to latest master. commit: ac56ff7e00bcfde6e7830a05b3f65a7e9723a9d4 Fixes https://github.com/rescript-lang/rescript-compiler/issues/5654 --- CHANGELOG.md | 4 +++- lib/4.06.1/unstable/js_compiler.ml | 9 ++++--- lib/4.06.1/unstable/js_playground_compiler.ml | 24 +++++++++++++------ lib/4.06.1/whole_compiler.ml | 24 +++++++++++++------ syntax | 2 +- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e082738b7e..7a1b8afaf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,9 @@ #### :bug: Bug Fix -- Fix printing of type declarations in error message where they would be considered recursive by default +- Fix printing of type declarations in error message where they would be considered recursive by default +- Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629 +- Fix printing of optional fiels in records https://github.com/rescript-lang/rescript-compiler/issues/5654 # 10.1.0-alpha.1 diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index e9e0c50a0c..35bfc59e44 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -54879,13 +54879,16 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl = [(Nolabel, lhs); (Nolabel, rhs)] ) when not (ParsetreeViewer.isBinaryExpression lhs - || ParsetreeViewer.isBinaryExpression rhs) -> + || ParsetreeViewer.isBinaryExpression rhs + || printAttributes ~customLayout expr.pexp_attributes cmtTbl + <> Doc.nil) -> let lhsHasCommentBelow = hasCommentBelow cmtTbl lhs.pexp_loc in let lhsDoc = printOperand ~isLhs:true lhs op in let rhsDoc = printOperand ~isLhs:false rhs op in Doc.group (Doc.concat [ + printAttributes ~customLayout expr.pexp_attributes cmtTbl; lhsDoc; (match (lhsHasCommentBelow, op) with | true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"] @@ -272130,8 +272133,8 @@ module V3 = struct | Pexp_fun (Labelled "ref", _, _, _) | Pexp_fun (Optional "ref", _, _, _) -> raiseError ~loc:expr.pexp_loc - "Ref cannot be passed as a normal prop. Please use `forwardRef` API \ - instead." + "Ref cannot be passed as a normal prop. Either give the prop a \ + different name or use the `forwardRef` API instead." | Pexp_fun (arg, default, pattern, expression) when isOptional arg || isLabelled arg -> let () = diff --git a/lib/4.06.1/unstable/js_playground_compiler.ml b/lib/4.06.1/unstable/js_playground_compiler.ml index d33b456cac..fc16855cac 100644 --- a/lib/4.06.1/unstable/js_playground_compiler.ml +++ b/lib/4.06.1/unstable/js_playground_compiler.ml @@ -54879,13 +54879,16 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl = [(Nolabel, lhs); (Nolabel, rhs)] ) when not (ParsetreeViewer.isBinaryExpression lhs - || ParsetreeViewer.isBinaryExpression rhs) -> + || ParsetreeViewer.isBinaryExpression rhs + || printAttributes ~customLayout expr.pexp_attributes cmtTbl + <> Doc.nil) -> let lhsHasCommentBelow = hasCommentBelow cmtTbl lhs.pexp_loc in let lhsDoc = printOperand ~isLhs:true lhs op in let rhsDoc = printOperand ~isLhs:false rhs op in Doc.group (Doc.concat [ + printAttributes ~customLayout expr.pexp_attributes cmtTbl; lhsDoc; (match (lhsHasCommentBelow, op) with | true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"] @@ -273593,8 +273596,8 @@ module V3 = struct | Pexp_fun (Labelled "ref", _, _, _) | Pexp_fun (Optional "ref", _, _, _) -> raiseError ~loc:expr.pexp_loc - "Ref cannot be passed as a normal prop. Please use `forwardRef` API \ - instead." + "Ref cannot be passed as a normal prop. Either give the prop a \ + different name or use the `forwardRef` API instead." | Pexp_fun (arg, default, pattern, expression) when isOptional arg || isLabelled arg -> let () = @@ -287080,6 +287083,7 @@ and parseFieldDeclaration p = match p.token with | _ -> parseLident p in + let optional = parseOptionalLabel p in let name = Location.mkloc lident loc in let typ = match p.Parser.token with @@ -287090,7 +287094,7 @@ and parseFieldDeclaration p = Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} [] in let loc = mkLoc startPos typ.ptyp_loc.loc_end in - Ast_helper.Type.field ~attrs ~loc ~mut name typ + (optional, Ast_helper.Type.field ~attrs ~loc ~mut name typ) and parseFieldDeclarationRegion p = let startPos = p.Parser.startPos in @@ -287103,6 +287107,7 @@ and parseFieldDeclarationRegion p = | Lident _ -> let lident, loc = parseLident p in let name = Location.mkloc lident loc in + (* XXX *) let optional = parseOptionalLabel p in let typ = match p.Parser.token with @@ -287282,7 +287287,10 @@ and parseConstrDeclArgs p = ~closing:Rbrace ~f:parseFieldDeclarationRegion p | attrs -> let first = - let field = parseFieldDeclaration p in + let optional, field = parseFieldDeclaration p in + let attrs = + if optional then optionalAttr :: attrs else attrs + in Parser.expect Comma p; {field with Parsetree.pld_attributes = attrs} in @@ -287679,13 +287687,15 @@ and parseRecordOrObjectDecl p = | _ -> Parser.leaveBreadcrumb p Grammar.RecordDecl; let fields = + (* XXX *) match attrs with | [] -> parseCommaDelimitedRegion ~grammar:Grammar.FieldDeclarations ~closing:Rbrace ~f:parseFieldDeclarationRegion p | attr :: _ as attrs -> let first = - let field = parseFieldDeclaration p in + let optional, field = parseFieldDeclaration p in + let attrs = if optional then optionalAttr :: attrs else attrs in Parser.optional p Comma |> ignore; { field with @@ -289843,9 +289853,9 @@ and printRecordDeclRowDoc (name, mut, opt, arg) = Doc.group (Doc.concat [ - (if opt then Doc.text "?" else Doc.nil); (if mut then Doc.text "mutable " else Doc.nil); printIdentLike ~allowUident:false name; + (if opt then Doc.text "?" else Doc.nil); Doc.text ": "; printOutTypeDoc arg; ]) diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index f60833eccc..ba1bdc149a 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -231066,13 +231066,16 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl = [(Nolabel, lhs); (Nolabel, rhs)] ) when not (ParsetreeViewer.isBinaryExpression lhs - || ParsetreeViewer.isBinaryExpression rhs) -> + || ParsetreeViewer.isBinaryExpression rhs + || printAttributes ~customLayout expr.pexp_attributes cmtTbl + <> Doc.nil) -> let lhsHasCommentBelow = hasCommentBelow cmtTbl lhs.pexp_loc in let lhsDoc = printOperand ~isLhs:true lhs op in let rhsDoc = printOperand ~isLhs:false rhs op in Doc.group (Doc.concat [ + printAttributes ~customLayout expr.pexp_attributes cmtTbl; lhsDoc; (match (lhsHasCommentBelow, op) with | true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"] @@ -283972,8 +283975,8 @@ module V3 = struct | Pexp_fun (Labelled "ref", _, _, _) | Pexp_fun (Optional "ref", _, _, _) -> raiseError ~loc:expr.pexp_loc - "Ref cannot be passed as a normal prop. Please use `forwardRef` API \ - instead." + "Ref cannot be passed as a normal prop. Either give the prop a \ + different name or use the `forwardRef` API instead." | Pexp_fun (arg, default, pattern, expression) when isOptional arg || isLabelled arg -> let () = @@ -300604,6 +300607,7 @@ and parseFieldDeclaration p = match p.token with | _ -> parseLident p in + let optional = parseOptionalLabel p in let name = Location.mkloc lident loc in let typ = match p.Parser.token with @@ -300614,7 +300618,7 @@ and parseFieldDeclaration p = Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} [] in let loc = mkLoc startPos typ.ptyp_loc.loc_end in - Ast_helper.Type.field ~attrs ~loc ~mut name typ + (optional, Ast_helper.Type.field ~attrs ~loc ~mut name typ) and parseFieldDeclarationRegion p = let startPos = p.Parser.startPos in @@ -300627,6 +300631,7 @@ and parseFieldDeclarationRegion p = | Lident _ -> let lident, loc = parseLident p in let name = Location.mkloc lident loc in + (* XXX *) let optional = parseOptionalLabel p in let typ = match p.Parser.token with @@ -300806,7 +300811,10 @@ and parseConstrDeclArgs p = ~closing:Rbrace ~f:parseFieldDeclarationRegion p | attrs -> let first = - let field = parseFieldDeclaration p in + let optional, field = parseFieldDeclaration p in + let attrs = + if optional then optionalAttr :: attrs else attrs + in Parser.expect Comma p; {field with Parsetree.pld_attributes = attrs} in @@ -301203,13 +301211,15 @@ and parseRecordOrObjectDecl p = | _ -> Parser.leaveBreadcrumb p Grammar.RecordDecl; let fields = + (* XXX *) match attrs with | [] -> parseCommaDelimitedRegion ~grammar:Grammar.FieldDeclarations ~closing:Rbrace ~f:parseFieldDeclarationRegion p | attr :: _ as attrs -> let first = - let field = parseFieldDeclaration p in + let optional, field = parseFieldDeclaration p in + let attrs = if optional then optionalAttr :: attrs else attrs in Parser.optional p Comma |> ignore; { field with @@ -304269,9 +304279,9 @@ and printRecordDeclRowDoc (name, mut, opt, arg) = Doc.group (Doc.concat [ - (if opt then Doc.text "?" else Doc.nil); (if mut then Doc.text "mutable " else Doc.nil); printIdentLike ~allowUident:false name; + (if opt then Doc.text "?" else Doc.nil); Doc.text ": "; printOutTypeDoc arg; ]) diff --git a/syntax b/syntax index cbc5e69b5a..ac56ff7e00 160000 --- a/syntax +++ b/syntax @@ -1 +1 @@ -Subproject commit cbc5e69b5abab3e383c022b0d0f1b1e901a34644 +Subproject commit ac56ff7e00bcfde6e7830a05b3f65a7e9723a9d4 From 23d8ab1b7510a613e4f584f96e1ae950e599c097 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 11 Sep 2022 07:35:58 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a1b8afaf9..3774e32b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Fix printing of type declarations in error message where they would be considered recursive by default - Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629 -- Fix printing of optional fiels in records https://github.com/rescript-lang/rescript-compiler/issues/5654 +- Fix printing of optional fields in records https://github.com/rescript-lang/rescript-compiler/issues/5654 # 10.1.0-alpha.1