mirror of
https://github.com/golang/go
synced 2024-10-06 08:00:07 +00:00
cmd/compile: ensure we replace package placeholder in type names
We want package names exposed by reflect to be things like main.F[main.foo], not main.F["".foo]. Fixes #49547 Change-Id: I182411a75d56ce1f64fde847e5b9ee74ce44e00b Reviewed-on: https://go-review.googlesource.com/c/go/+/363656 Trust: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dan Scales <danscales@google.com>
This commit is contained in:
parent
dfa62c79de
commit
5337e53dfa
|
@ -140,11 +140,17 @@ func sconv2(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func symfmt(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
|
func symfmt(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
|
||||||
|
name := s.Name
|
||||||
if q := pkgqual(s.Pkg, verb, mode); q != "" {
|
if q := pkgqual(s.Pkg, verb, mode); q != "" {
|
||||||
b.WriteString(q)
|
b.WriteString(q)
|
||||||
b.WriteByte('.')
|
b.WriteByte('.')
|
||||||
|
if mode == fmtTypeIDName {
|
||||||
|
// If name is a generic instantiation, it might have local package placeholders
|
||||||
|
// in it. Replace those placeholders with the package name. See issue 49547.
|
||||||
|
name = strings.Replace(name, LocalPkg.Prefix, q, -1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
b.WriteString(s.Name)
|
b.WriteString(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pkgqual returns the qualifier that should be used for printing
|
// pkgqual returns the qualifier that should be used for printing
|
||||||
|
|
22
test/typeparam/issue49547.go
Normal file
22
test/typeparam/issue49547.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
// run -gcflags=-G=3
|
||||||
|
|
||||||
|
// Copyright 2021 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type foo int
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
want := "main.F[main.foo]"
|
||||||
|
got := fmt.Sprintf("%T", F[foo]{})
|
||||||
|
if got != want {
|
||||||
|
fmt.Printf("want: %s, got: %s\n", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type F[T any] struct {
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
0,3: main.T·2[int;int]
|
0,3: main.T·2[int;int]
|
||||||
4,7: main.T·2[int;"".U·3[int;int]]
|
4,7: main.T·2[int;main.U·3[int;int]]
|
||||||
22,23: main.T·2["".Int;"".Int]
|
22,23: main.T·2[main.Int;main.Int]
|
||||||
26,27: main.T·2["".Int;"".U·3["".Int;"".Int]]
|
26,27: main.T·2[main.Int;main.U·3[main.Int;main.Int]]
|
||||||
|
|
Loading…
Reference in a new issue