1
0
mirror of https://github.com/golang/go synced 2024-11-17 23:04:56 -07:00

cmd/compile/internal/types2: remove subscripts from type parameter names

This is a port of CL 357814 from go/types to types2 with minor
adjustments due to small differences in error handling code.

Change-Id: I72ecc4532e8349f569cabb38006f3d8ff517bf30
Reviewed-on: https://go-review.googlesource.com/c/go/+/360276
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-10-31 12:08:32 -07:00
parent d2b512160e
commit e2e910ef30
5 changed files with 35 additions and 25 deletions

View File

@ -326,25 +326,25 @@ func TestTypesInfo(t *testing.T) {
{brokenPkg + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string]invalid type`},
// parameterized functions
{genericPkg + `p0; func f[T any](T) {}; var _ = f[int]`, `f`, `func[generic_p0.T interface{}](generic_p0.T)`},
{genericPkg + `p0; func f[T any](T) {}; var _ = f[int]`, `f`, `func[generic_p0.T interface{}](generic_p0.T)`},
{genericPkg + `p1; func f[T any](T) {}; var _ = f[int]`, `f[int]`, `func(int)`},
{genericPkg + `p2; func f[T any](T) {}; func _() { f(42) }`, `f`, `func(int)`},
{genericPkg + `p3; func f[T any](T) {}; func _() { f[int](42) }`, `f[int]`, `func(int)`},
{genericPkg + `p4; func f[T any](T) {}; func _() { f[int](42) }`, `f`, `func[generic_p4.T interface{}](generic_p4.T)`},
{genericPkg + `p4; func f[T any](T) {}; func _() { f[int](42) }`, `f`, `func[generic_p4.T interface{}](generic_p4.T)`},
{genericPkg + `p5; func f[T any](T) {}; func _() { f(42) }`, `f(42)`, `()`},
// type parameters
{genericPkg + `t0; type t[] int; var _ t`, `t`, `generic_t0.t`}, // t[] is a syntax error that is ignored in this test in favor of t
{genericPkg + `t1; type t[P any] int; var _ t[int]`, `t`, `generic_t1.t[generic_t1.P interface{}]`},
{genericPkg + `t2; type t[P interface{}] int; var _ t[int]`, `t`, `generic_t2.t[generic_t2.P interface{}]`},
{genericPkg + `t3; type t[P, Q interface{}] int; var _ t[int, int]`, `t`, `generic_t3.t[generic_t3.P, generic_t3.Q interface{}]`},
{brokenPkg + `t4; type t[P, Q interface{ m() }] int; var _ t[int, int]`, `t`, `broken_t4.t[broken_t4.P, broken_t4.Q interface{m()}]`},
{genericPkg + `t1; type t[P any] int; var _ t[int]`, `t`, `generic_t1.t[generic_t1.P interface{}]`},
{genericPkg + `t2; type t[P interface{}] int; var _ t[int]`, `t`, `generic_t2.t[generic_t2.P interface{}]`},
{genericPkg + `t3; type t[P, Q interface{}] int; var _ t[int, int]`, `t`, `generic_t3.t[generic_t3.P, generic_t3.Q interface{}]`},
{brokenPkg + `t4; type t[P, Q interface{ m() }] int; var _ t[int, int]`, `t`, `broken_t4.t[broken_t4.P, broken_t4.Q interface{m()}]`},
// instantiated types must be sanitized
{genericPkg + `g0; type t[P any] int; var x struct{ f t[int] }; var _ = x.f`, `x.f`, `generic_g0.t[int]`},
// issue 45096
{genericPkg + `issue45096; func _[T interface{ ~int8 | ~int16 | ~int32 }](x T) { _ = x < 0 }`, `0`, `generic_issue45096.T`},
{genericPkg + `issue45096; func _[T interface{ ~int8 | ~int16 | ~int32 }](x T) { _ = x < 0 }`, `0`, `generic_issue45096.T`},
// issue 47895
{`package p; import "unsafe"; type S struct { f int }; var s S; var _ = unsafe.Offsetof(s.f)`, `s.f`, `int`},

View File

@ -112,15 +112,15 @@ var builtinCalls = []struct {
{"Alignof", `_ = unsafe.Alignof(0)`, `invalid type`}, // constant
{"Alignof", `var x struct{}; _ = unsafe.Alignof(x)`, `invalid type`}, // constant
{"Alignof", `var x P; _ = unsafe.Alignof(x)`, `func(p.P) uintptr`},
{"Alignof", `var x P; _ = unsafe.Alignof(x)`, `func(p.P) uintptr`},
{"Offsetof", `var x struct{f bool}; _ = unsafe.Offsetof(x.f)`, `invalid type`}, // constant
{"Offsetof", `var x struct{_ int; f bool}; _ = unsafe.Offsetof((&x).f)`, `invalid type`}, // constant
{"Offsetof", `var x struct{_ int; f P}; _ = unsafe.Offsetof((&x).f)`, `func(p.P) uintptr`},
{"Offsetof", `var x struct{_ int; f P}; _ = unsafe.Offsetof((&x).f)`, `func(p.P) uintptr`},
{"Sizeof", `_ = unsafe.Sizeof(0)`, `invalid type`}, // constant
{"Sizeof", `var x struct{}; _ = unsafe.Sizeof(x)`, `invalid type`}, // constant
{"Sizeof", `var x P; _ = unsafe.Sizeof(x)`, `func(p.P) uintptr`},
{"Sizeof", `var x P; _ = unsafe.Sizeof(x)`, `func(p.P) uintptr`},
{"Slice", `var p *int; _ = unsafe.Slice(p, 1)`, `func(*int, int) []int`},
{"Slice", `var p *byte; var n uintptr; _ = unsafe.Slice(p, n)`, `func(*byte, uintptr) []byte`},

View File

@ -66,7 +66,7 @@ func (err *error_) msg(qf Qualifier) string {
fmt.Fprintf(&buf, "%s: ", p.pos)
}
}
buf.WriteString(sprintf(qf, p.format, p.args...))
buf.WriteString(sprintf(qf, false, p.format, p.args...))
}
return buf.String()
}
@ -85,7 +85,7 @@ func (err *error_) errorf(at poser, format string, args ...interface{}) {
err.desc = append(err.desc, errorDesc{posFor(at), format, args})
}
func sprintf(qf Qualifier, format string, args ...interface{}) string {
func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) string {
for i, arg := range args {
switch a := arg.(type) {
case nil:
@ -101,7 +101,7 @@ func sprintf(qf Qualifier, format string, args ...interface{}) string {
case Object:
arg = ObjectString(a, qf)
case Type:
arg = TypeString(a, qf)
arg = typeString(a, qf, debug)
}
args[i] = arg
}
@ -146,7 +146,7 @@ func (check *Checker) markImports(pkg *Package) {
}
func (check *Checker) sprintf(format string, args ...interface{}) string {
return sprintf(check.qualifier, format, args...)
return sprintf(check.qualifier, false, format, args...)
}
func (check *Checker) report(err *error_) {
@ -160,13 +160,13 @@ func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{})
fmt.Printf("%s:\t%s%s\n",
pos,
strings.Repeat(". ", check.indent),
check.sprintf(format, args...),
sprintf(check.qualifier, true, format, args...),
)
}
// dump is only needed for debugging
func (check *Checker) dump(format string, args ...interface{}) {
fmt.Println(check.sprintf(format, args...))
fmt.Println(sprintf(check.qualifier, true, format, args...))
}
func (check *Checker) err(at poser, msg string, soft bool) {

View File

@ -155,7 +155,7 @@ func (check *Checker) satisfies(pos syntax.Pos, targ Type, tpar *TypeParam, smap
qf = check.qualifier
}
errorf := func(format string, args ...interface{}) error {
return errors.New(sprintf(qf, format, args...))
return errors.New(sprintf(qf, false, format, args...))
}
// No type argument with non-empty type set satisfies the empty type set.

View File

@ -43,8 +43,14 @@ func RelativeTo(pkg *Package) Qualifier {
// The Qualifier controls the printing of
// package-level objects, and may be nil.
func TypeString(typ Type, qf Qualifier) string {
return typeString(typ, qf, false)
}
func typeString(typ Type, qf Qualifier, debug bool) string {
var buf bytes.Buffer
WriteType(&buf, typ, qf)
w := newTypeWriter(&buf, qf)
w.debug = debug
w.typ(typ)
return buf.String()
}
@ -68,15 +74,16 @@ type typeWriter struct {
seen map[Type]bool
qf Qualifier
ctxt *Context // if non-nil, we are type hashing
debug bool // if true, write debug annotations
}
func newTypeWriter(buf *bytes.Buffer, qf Qualifier) *typeWriter {
return &typeWriter{buf, make(map[Type]bool), qf, nil}
return &typeWriter{buf, make(map[Type]bool), qf, nil, false}
}
func newTypeHasher(buf *bytes.Buffer, ctxt *Context) *typeWriter {
assert(ctxt != nil)
return &typeWriter{buf, make(map[Type]bool), nil, ctxt}
return &typeWriter{buf, make(map[Type]bool), nil, ctxt, false}
}
func (w *typeWriter) byte(b byte) {
@ -270,7 +277,10 @@ func (w *typeWriter) typ(typ Type) {
if t.obj.pkg != nil {
writePackage(w.buf, t.obj.pkg, w.qf)
}
w.string(t.obj.name + subscript(t.id))
w.string(t.obj.name)
if w.debug || w.ctxt != nil {
w.string(subscript(t.id))
}
default:
// For externally defined implementations of Type.