]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types: match types2 string when printing composite literals
authorRobert Griesemer <gri@golang.org>
Sat, 20 Aug 2022 01:31:56 +0000 (18:31 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 25 Aug 2022 17:31:33 +0000 (17:31 +0000)
Given a composite literal type S, rather than always printing
(S literal) for a composite literals, print S{} if the literal
has no elements, and print S{…} as a short form (suitable for
error messages) if there are elements. This matches types2 and
also Go1.17 compiler behavior (except that the original compiler
would print ... rather than …). Using … rather than ... makes
it clearer that we don't have real Go syntax, and it's also more
compact.

For #54511.

Change-Id: I5991e8060232f16ecbf4a1fe4ae091598fc76b68
Reviewed-on: https://go-review.googlesource.com/c/go/+/425006
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/api_test.go
src/go/types/exprstring.go
src/go/types/exprstring_test.go
src/go/types/testdata/check/decls3.go
src/go/types/testdata/check/decls4.go
src/go/types/testdata/check/issues0.go
src/go/types/testdata/fixedbugs/issue49579.go

index 8e8ac84cec857786ccd095ada87bf16189504dc4..742ae3d67fa197758f2d2c65e25640b0f759cb5e 100644 (file)
@@ -951,7 +951,7 @@ func TestPredicatesInfo(t *testing.T) {
 
                // values
                {`package v0; var (a, b int; _ = a + b)`, `a + b`, `value`},
-               {`package v1; var _ = &[]int{1}`, `([]int literal)`, `value`},
+               {`package v1; var _ = &[]int{1}`, `[]int{…}`, `value`},
                {`package v2; var _ = func(){}`, `(func() literal)`, `value`},
                {`package v4; func f() { _ = f }`, `f`, `value`},
                {`package v3; var _ *int = nil`, `nil`, `value, nil`},
index 544cd84d6120ed27406388b785c45ec9f8b7ddd5..e19d79d9c8d0df1c9118c15889f454f084623bec 100644 (file)
@@ -53,9 +53,12 @@ func WriteExpr(buf *bytes.Buffer, x ast.Expr) {
                buf.WriteString(" literal)") // shortened
 
        case *ast.CompositeLit:
-               buf.WriteByte('(')
                WriteExpr(buf, x.Type)
-               buf.WriteString(" literal)") // shortened
+               buf.WriteByte('{')
+               if len(x.Elts) > 0 {
+                       buf.WriteString("…")
+               }
+               buf.WriteByte('}')
 
        case *ast.ParenExpr:
                buf.WriteByte('(')
index 27cd532c97c2715a3de8941774e37f15a36cfefe..604ceb98072ae14f5a496730edf5259390872945 100644 (file)
@@ -25,7 +25,7 @@ var testExprs = []testEntry{
        // func and composite literals
        {"func(){}", "(func() literal)"},
        {"func(x int) complex128 {}", "(func(x int) complex128 literal)"},
-       {"[]int{1, 2, 3}", "([]int literal)"},
+       {"[]int{1, 2, 3}", "[]int{…}"},
 
        // type expressions
        dup("[1 << 10]byte"),
index 745175c7109390f81eac840585d026e3074bfde3..01d4ffe4b5be163d5d568227d43da717294939db 100644 (file)
@@ -221,16 +221,16 @@ func _() {
        _ = S2{}.B
        _ = S2{}.C
        _ = S2{}.D /* ERROR "no field or method" */
-       _ = S3{}.S1 /* ERROR "ambiguous selector \(S3 literal\).S1" */
+       _ = S3{}.S1 /* ERROR "ambiguous selector S3{}.S1" */
        _ = S3{}.A
-       _ = S3{}.B /* ERROR "ambiguous selector" \(S3 literal\).B */
+       _ = S3{}.B /* ERROR "ambiguous selector" S3{}.B */
        _ = S3{}.D
        _ = S3{}.E
        _ = S4{}.A
        _ = S4{}.B /* ERROR "no field or method" */
-       _ = S5{}.X /* ERROR "ambiguous selector \(S5 literal\).X" */
+       _ = S5{}.X /* ERROR "ambiguous selector S5{}.X" */
        _ = S5{}.Y
-       _ = S10{}.X /* ERROR "ambiguous selector \(S10 literal\).X" */
+       _ = S10{}.X /* ERROR "ambiguous selector S10{}.X" */
        _ = S10{}.Y
 }
 
@@ -306,4 +306,4 @@ type R22 R21
 type R23 R21
 type R24 R21
 
-var _ = R0{}.X /* ERROR "ambiguous selector \(R0 literal\).X" */
\ No newline at end of file
+var _ = R0{}.X /* ERROR "ambiguous selector R0{}.X" */
\ No newline at end of file
index 8a9a6ffba7d5b445363e734011ef556d2de76a55..2ce180fbbbfa0e532d816da859cce6dca2abfd6a 100644 (file)
@@ -190,8 +190,8 @@ type eD struct {
 }
 
 var (
-       _ = eD{}.xf /* ERROR ambiguous selector \(eD literal\).xf */
-       _ = eD{}.xm /* ERROR ambiguous selector \(eD literal\).xm */
+       _ = eD{}.xf /* ERROR ambiguous selector eD{}.xf */
+       _ = eD{}.xm /* ERROR ambiguous selector eD{}.xm */
 )
 
 var (
index 6943796392b5aeeff78ec76646485fb2b742a2ab..95cfa2a9103df3f623c4ae3b15c43af3f41bdd00 100644 (file)
@@ -133,7 +133,7 @@ func issue10260() {
        )
 
        var x I1
-       x = T1 /* ERROR cannot use \(T1 literal\) .* as I1 value in assignment: T1 does not implement I1 \(method foo has pointer receiver\) */ {}
+       x = T1 /* ERROR cannot use T1{} .* as I1 value in assignment: T1 does not implement I1 \(method foo has pointer receiver\) */ {}
        _ = x /* ERROR impossible type assertion: x\.\(T1\)\n\tT1 does not implement I1 \(method foo has pointer receiver\) */ .(T1)
 
        T1{}.foo /* ERROR cannot call pointer method foo on T1 */ ()
index 07748bd0dc4798fe51a6dce73b7dded4589150c2..ee2d94ab8921fc11d587091e01f790916555d936 100644 (file)
@@ -9,7 +9,7 @@ type I[F any] interface {
 }
 
 func G[F any]() I[any] {
-       return g /* ERROR cannot use \(g\[F\] literal\) .* as I\[any\] value in return statement: g\[F\] does not implement I\[any\] \(method Q has pointer receiver\) */ [F]{}
+       return g /* ERROR cannot use g\[F\]{} .* as I\[any\] value in return statement: g\[F\] does not implement I\[any\] \(method Q has pointer receiver\) */ [F]{}
 }
 
 type g[F any] struct{}