From f783f33c758f8e40c2394aaaa26505f8b1d75824 Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 1 Sep 2022 15:16:53 +0200 Subject: [PATCH] Add extra space when first type argument is a statically resolved type parameter. --- src/Compiler/Checking/NicePrint.fs | 17 ++++-- .../FSharp.Compiler.ComponentTests.fsproj | 1 + .../Signatures/RecordTests.fs | 2 +- .../StaticallyResolvedTypeParameterTests.fs | 53 +++++++++++++++++++ tests/FSharp.Test.Utilities/Compiler.fs | 2 +- 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 tests/FSharp.Compiler.ComponentTests/Signatures/StaticallyResolvedTypeParameterTests.fs diff --git a/src/Compiler/Checking/NicePrint.fs b/src/Compiler/Checking/NicePrint.fs index 3913253a0e..938d2982fa 100644 --- a/src/Compiler/Checking/NicePrint.fs +++ b/src/Compiler/Checking/NicePrint.fs @@ -862,11 +862,22 @@ module PrintTypes = /// Layout type arguments, either NAME or (ty, ..., ty) NAME *) and layoutTypeAppWithInfoAndPrec denv env tcL prec prefix argTys = - if prefix then + if prefix then + let addExtraSpace argTy = + match argTy with + | TType_var(typar, _) when typar.StaticReq = TyparStaticReq.HeadType -> SepL.space + | _ -> emptyL + match argTys with | [] -> tcL - | [argTy] -> tcL ^^ sepL (tagPunctuation "<") ^^ (layoutTypeWithInfoAndPrec denv env 4 argTy) ^^ rightL (tagPunctuation">") - | _ -> bracketIfL (prec <= 1) (tcL ^^ angleL (layoutTypesWithInfoAndPrec denv env 2 (sepL (tagPunctuation ",")) argTys)) + | [argTy] -> + tcL + ^^ sepL (tagPunctuation "<") + ^^ addExtraSpace argTy + ^^ (layoutTypeWithInfoAndPrec denv env 4 argTy) + ^^ rightL (tagPunctuation">") + | firstArgTy :: _ -> + bracketIfL (prec <= 1) (tcL ^^ angleL (addExtraSpace firstArgTy ^^ layoutTypesWithInfoAndPrec denv env 2 (sepL (tagPunctuation ",")) argTys)) else match argTys with | [] -> tcL diff --git a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj index d8bd7df90c..5dace5060a 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj +++ b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj @@ -199,6 +199,7 @@ + diff --git a/tests/FSharp.Compiler.ComponentTests/Signatures/RecordTests.fs b/tests/FSharp.Compiler.ComponentTests/Signatures/RecordTests.fs index e51da66215..1d78be1cfb 100644 --- a/tests/FSharp.Compiler.ComponentTests/Signatures/RecordTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Signatures/RecordTests.fs @@ -18,7 +18,7 @@ type PullActions = Log : int } """ - |> printSignaturesWith 80 + |> printSignaturesWithPageWidth 80 |> should equal """ diff --git a/tests/FSharp.Compiler.ComponentTests/Signatures/StaticallyResolvedTypeParameterTests.fs b/tests/FSharp.Compiler.ComponentTests/Signatures/StaticallyResolvedTypeParameterTests.fs new file mode 100644 index 0000000000..261d47cf89 --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Signatures/StaticallyResolvedTypeParameterTests.fs @@ -0,0 +1,53 @@ +module FSharp.Compiler.ComponentTests.Signatures.StaticallyResolvedTypeParameterTests + +open Xunit +open FsUnit +open FSharp.Test.Compiler +open FSharp.Compiler.ComponentTests.Signatures.TestHelpers + +[] +let ``Generic function with constraint`` () = + FSharp + """ +module MyApp.GoodStuff + +let inline toString< ^revision when ^revision: (static member GetCommitHash: ^revision -> string)> + (p: System.Threading.Tasks.Task< ^revision >) + : string = + "" +""" + |> printSignaturesWithPageWidth 80 + |> should + equal + """ +module MyApp.GoodStuff + +val inline toString: + p: System.Threading.Tasks.Task< ^revision> -> string + when ^revision: (static member GetCommitHash: ^revision -> string)""" + +[] +let ``Multiple constraints parameters in type definition`` () = + FSharp + """ +module Foo + +type Meh<'g, 'f> = + class + end + +let inline toString< ^revision> + (p: Meh< ^revision, ^revision>) + : string = + "" +""" + |> printSignaturesWithPageWidth 80 + |> should + equal + """ +module Foo + +type Meh<'g,'f> = + class end + +val inline toString: p: Meh< ^revision,^revision> -> string""" diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs index 2d24d2f678..2ce1fc6793 100644 --- a/tests/FSharp.Test.Utilities/Compiler.fs +++ b/tests/FSharp.Test.Utilities/Compiler.fs @@ -1342,4 +1342,4 @@ module rec Compiler = |> String.concat "\n" let printSignatures cUnit = printSignaturesImpl None cUnit - let printSignaturesWith pageWidth cUnit = printSignaturesImpl (Some pageWidth) cUnit + let printSignaturesWithPageWidth pageWidth cUnit = printSignaturesImpl (Some pageWidth) cUnit