[dev.typeparams] cmd/compile/internal/syntax: fix printing of channel types

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>
This commit is contained in:
Robert Griesemer 2020-10-14 22:09:47 -07:00
parent 73f529845c
commit e9e58a4d49
2 changed files with 30 additions and 6 deletions

View 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:

View 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)