]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/syntax: fix printing of channel types
authorRobert Griesemer <gri@golang.org>
Thu, 15 Oct 2020 05:09:47 +0000 (22:09 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 15 Oct 2020 18:51:03 +0000 (18:51 +0000)
Change-Id: I80a3ca77d0642711913c9584e70059e4ed668860
Reviewed-on: https://go-review.googlesource.com/c/go/+/262444
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/syntax/printer.go
src/cmd/compile/internal/syntax/printer_test.go

index 8ff3bfa79443523e9009e23cbe95ce3647088965..c8bf59675a2fedb8291580842e005275ba3d9400 100644 (file)
@@ -484,7 +484,15 @@ func (p *printer) printRawNode(n Node) {
                if n.Dir == SendOnly {
                        p.print(_Arrow)
                }
-               p.print(blank, n.Elem)
+               p.print(blank)
+               if e, _ := n.Elem.(*ChanType); n.Dir == 0 && e != nil && e.Dir == RecvOnly {
+                       // don't print chan (<-chan T) as chan <-chan T
+                       p.print(_Lparen)
+                       p.print(n.Elem)
+                       p.print(_Rparen)
+               } else {
+                       p.print(n.Elem)
+               }
 
        // statements
        case *DeclStmt:
index c3b9aca229c3358a69ef17b7b72b3fbf8458fb87..cae2c40f6c96644cd1815de8fe66bffefc442e20 100644 (file)
@@ -30,12 +30,28 @@ func TestPrint(t *testing.T) {
        }
 }
 
+var stringTests = []string{
+       "package p",
+       "package p; type _ int; type T1 = struct{}; type ( _ *struct{}; T2 = float32 )",
+
+       // channels
+       "package p; type _ chan chan int",
+       "package p; type _ chan (<-chan int)",
+       "package p; type _ chan chan<- int",
+
+       "package p; type _ <-chan chan int",
+       "package p; type _ <-chan <-chan int",
+       "package p; type _ <-chan chan<- int",
+
+       "package p; type _ chan<- chan int",
+       "package p; type _ chan<- <-chan int",
+       "package p; type _ chan<- chan<- int",
+
+       // TODO(gri) expand
+}
+
 func TestPrintString(t *testing.T) {
-       for _, want := range []string{
-               "package p",
-               "package p; type _ = int; type T1 = struct{}; type ( _ = *struct{}; T2 = float32 )",
-               // TODO(gri) expand
-       } {
+       for _, want := range stringTests {
                ast, err := Parse(nil, strings.NewReader(want), nil, nil, 0)
                if err != nil {
                        t.Error(err)