1
0
mirror of https://github.com/golang/go synced 2024-11-26 18:16:48 -07:00

go/types: match types2 string when printing composite literals

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>
This commit is contained in:
Robert Griesemer 2022-08-19 18:31:56 -07:00
parent 1df2a03b17
commit 83b5fe6351
7 changed files with 16 additions and 13 deletions

View 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`},

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

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

View 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" */
var _ = R0{}.X /* ERROR "ambiguous selector R0{}.X" */

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

View 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 */ ()

View 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{}