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

Better record and value formatting in tools #7021

Merged
merged 25 commits into from
Jul 12, 2019
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
22 changes: 14 additions & 8 deletions src/fsharp/NicePrint.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ module internal PrintUtilities =
let bracketIfL x lyt = if x then bracketL lyt else lyt
let squareAngleL x = LeftL.leftBracketAngle ^^ x ^^ RightL.rightBracketAngle
let angleL x = sepL Literals.leftAngle ^^ x ^^ rightL Literals.rightAngle
let braceL x = leftL Literals.leftBrace ^^ x ^^ rightL Literals.rightBrace
let braceBarL x = leftL Literals.leftBraceBar ^^ x ^^ rightL Literals.rightBraceBar
let braceL x = wordL Literals.leftBrace ^^ x ^^ wordL Literals.rightBrace
let braceBarL x = wordL Literals.leftBraceBar ^^ x ^^ wordL Literals.rightBraceBar

let comment str = wordL (tagText (sprintf "(* %s *)" str))

Expand Down Expand Up @@ -942,7 +942,7 @@ module private PrintTypes =

// Layout a tuple type
| TType_anon (anonInfo, tys) ->
let core = sepListL (wordL (tagPunctuation ";")) (List.map2 (fun nm ty -> wordL (tagField nm) ^^ wordL (tagPunctuation ":") ^^ layoutTypeWithInfoAndPrec denv env prec ty) (Array.toList anonInfo.SortedNames) tys)
let core = sepListL (rightL (tagPunctuation ";")) (List.map2 (fun nm ty -> wordL (tagField nm) ^^ rightL (tagPunctuation ":") ^^ layoutTypeWithInfoAndPrec denv env prec ty) (Array.toList anonInfo.SortedNames) tys)
if evalAnonInfoIsStruct anonInfo then
WordL.keywordStruct --- braceBarL core
else
Expand Down Expand Up @@ -1457,7 +1457,7 @@ module private TastDefinitionPrinting =
let lhs =
tagRecordField fld.Name
|> mkNav fld.DefinitionRange
|> wordL
|> wordL
let lhs = (if addAccess then layoutAccessibility denv fld.Accessibility lhs else lhs)
let lhs = if fld.IsMutable then wordL (tagKeyword "mutable") --- lhs else lhs
(lhs ^^ RightL.colon) --- layoutType denv fld.FormalType
Expand Down Expand Up @@ -1738,8 +1738,15 @@ module private TastDefinitionPrinting =
let denv = denv.AddAccessibility tycon.TypeReprAccessibility
match repr with
| TRecdRepr _ ->
let recdFieldRefL fld = layoutRecdField false denv fld ^^ rightL (tagPunctuation ";")
let recdL = tycon.TrueFieldsAsList |> List.map recdFieldRefL |> applyMaxMembers denv.maxMembers |> aboveListL |> braceL
let recdFieldRefL fld = layoutRecdField false denv fld

let recdL =
tycon.TrueFieldsAsList
|> List.map recdFieldRefL
|> applyMaxMembers denv.maxMembers
|> aboveListL
|> braceL

Some (addMembersAsWithEnd (addReprAccessL recdL))

| TFSharpObjectRepr r ->
Expand Down Expand Up @@ -1771,8 +1778,7 @@ module private TastDefinitionPrinting =
| _ -> []
let vsprs =
tycon.MembersOfFSharpTyconSorted
|> List.filter (fun v -> isNil (Option.get v.MemberInfo).ImplementedSlotSigs)
|> List.filter (fun v -> v.IsDispatchSlot)
|> List.filter (fun v -> isNil (Option.get v.MemberInfo).ImplementedSlotSigs && v.IsDispatchSlot)
|> List.map (fun vref -> PrintTastMemberOrVals.prettyLayoutOfValOrMemberNoInst denv vref.Deref)
let staticValsLs =
tycon.TrueFieldsAsList
Expand Down
20 changes: 8 additions & 12 deletions src/utils/sformat.fs
Original file line number Diff line number Diff line change
Expand Up @@ -759,18 +759,14 @@ namespace Microsoft.FSharp.Text.StructuredPrintfImpl
// pprinter: attributes
// --------------------------------------------------------------------

let makeRecordVerticalL nameXs =
let itemL (name,xL) = let labelL = wordL name in ((labelL ^^ wordL Literals.equals)) -- (xL ^^ (rightL Literals.semicolon))
let braceL xs = (leftL Literals.leftBrace) ^^ xs ^^ (rightL Literals.rightBrace)
braceL (aboveListL (List.map itemL nameXs))

// This is a more compact rendering of records - and is more like tuples
let makeRecordHorizontalL nameXs =
let itemL (name,xL) = let labelL = wordL name in ((labelL ^^ wordL Literals.equals)) -- xL
let braceL xs = (leftL Literals.leftBrace) ^^ xs ^^ (rightL Literals.rightBrace)
braceL (sepListL (rightL Literals.semicolon) (List.map itemL nameXs))

let makeRecordL nameXs = makeRecordVerticalL nameXs
let makeRecordL nameXs =
let itemL (name,xL) = wordL name ^^ wordL Literals.equals -- xL
let braceL xs = (wordL Literals.leftBrace) ^^ xs ^^ (wordL Literals.rightBrace)

nameXs
|> List.map itemL
|> aboveListL
|> braceL

let makePropertiesL nameXs =
let itemL (name,v) =
Expand Down
4 changes: 2 additions & 2 deletions tests/fsharp/Compiler/Language/AnonRecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let rAnon = RefClass<struct {| R: int |}>()
FSharpErrorSeverity.Error
1
(3, 13, 3, 42)
"A generic construct requires that the type 'struct {|R : int|}' have reference semantics, but it does not, i.e. it is a struct"
"A generic construct requires that the type 'struct {| R: int |}' have reference semantics, but it does not, i.e. it is a struct"

[<Test>]
let StructConstraintFail() =
Expand All @@ -46,4 +46,4 @@ let sAnon = StructClass<{| S: int |}>()
FSharpErrorSeverity.Error
1
(3, 13, 3, 38)
"A generic construct requires that the type '{|S : int|}' is a CLI or F# struct type"
"A generic construct requires that the type '{| S: int |}' is a CLI or F# struct type"
4 changes: 2 additions & 2 deletions tests/fsharp/core/anon/lib.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ module KindB1 =
check "coijoiwcnkwle1" {| a = 1 |} {| a = 1 |}
check "coijoiwcnkwle2" {| a = 2 |} {| a = 2 |}

check "coijoiwcnkwle3" (sprintf "%A" {| X = 10 |}) "{X = 10;}"
check "coijoiwcnkwle4" (sprintf "%A" {| X = 10; Y = 1 |} |> fun s -> s.Replace("\n","").Replace("\r","")) ("{X = 10; Y = 1;}".Replace("\n","").Replace("\r",""))
check "coijoiwcnkwle3" (sprintf "%A" {| X = 10 |}) "{ X = 10 }"
check "coijoiwcnkwle4" (sprintf "%A" {| X = 10; Y = 1 |}) "{ X = 10\n Y = 1 }"
check "clekoiew09" (f2 {| X = {| X = 10 |} |}) 10
check "cewkew0oijew" (f2 {| X = {| X = 20 |} |}) 20

Expand Down
6 changes: 3 additions & 3 deletions tests/fsharp/core/anon/test.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ module Test =

let testAccess = (KindB1.data1.X, KindB1.data3.X)

check "coijoiwcnkwle2" (sprintf "%A" KindB1.data1) "{X = 1;}"
check "coijoiwcnkwle2" (sprintf "%A" KindB1.data1) "{ X = 1 }"

module Tests2 =

let testAccess = (KindB2.data1.X, KindB2.data3.X, KindB2.data3.Y)

check "coijoiwcnkwle3" (sprintf "%A" KindB2.data1) "{X = 1;}"
check "coijoiwcnkwle3" (sprintf "%A" KindB2.data1) "{ X = 1 }"

let _ = (KindB2.data1 = KindB2.data1)

Expand All @@ -49,7 +49,7 @@ module CrossAssemblyTest =
check "vrknvio1" (SampleAPI.SampleFunction {| A=1; B = "abc" |}) 4 // note, this is creating an instance of an anonymous record from another assembly.
check "vrknvio2" (SampleAPI.SampleFunctionAcceptingList [ {| A=1; B = "abc" |}; {| A=2; B = "def" |} ]) [4; 5] // note, this is creating an instance of an anonymous record from another assembly.
check "vrknvio3" (let d = SampleAPI.SampleFunctionReturningAnonRecd() in d.A + d.B.Length) 4
check "vrknvio4" (let d = SampleAPIStruct.SampleFunctionReturningAnonRecd() in d.ToString().Replace("\n","").Replace("\r","")) """{A = 1; B = "abc";}"""
check "vrknvio4" (let d = SampleAPIStruct.SampleFunctionReturningAnonRecd() in d.ToString()) ("{ A = 1\n " + """B = "abc" }""")
tests()

module CrossAssemblyTestStruct =
Expand Down
4 changes: 2 additions & 2 deletions tests/fsharp/core/members/basics/test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,8 @@ module ToStringOnRecordTest = begin

let a1 = {A = "201"; B = 7}
let c1 = {C = "20"; D = 17}
let expected1 = "{A = \"201\";\n B = 7;}"
let expected2 = "{C = \"20\";\n D = 17;}"
let expected1 = "{ A = \"201\"\n B = 7 }"
let expected2 = "{ C = \"20\"\n D = 17 }"

do test "record-tostring-def" (a1.ToString() = expected1)
do test "record-sprintfO-def" ((sprintf "%O" a1) = expected1)
Expand Down
Loading