mirror of https://github.com/golang/go.git
cmd/api: fix panic on exported basic type aliases
The order of emitting named type and type aliases in the `Walker`'s `emitType` function is inverted. When the type alias references a basic type, this causes a panic as the type assertion on `*types.Named` fails. This change reorders the logic such that type aliases are emitted prior to this type assertion. Fixes #64958 Change-Id: I52dbe13999978912ded788d9cf4948103869bcfa Reviewed-on: https://go-review.googlesource.com/c/go/+/554076 Reviewed-by: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Bryan Mills <bcmills@google.com>
This commit is contained in:
parent
6db1102605
commit
15dcdeb5aa
|
|
@ -285,6 +285,25 @@ func TestIssue41358(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssue64958(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if x := recover(); x != nil {
|
||||||
|
t.Errorf("expected no panic; recovered %v", x)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
testenv.MustHaveGoBuild(t)
|
||||||
|
|
||||||
|
for _, context := range contexts {
|
||||||
|
w := NewWalker(context, "testdata/src/issue64958")
|
||||||
|
pkg, err := w.importFrom("p", "", 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected no error importing; got %T", err)
|
||||||
|
}
|
||||||
|
w.export(pkg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCheck(t *testing.T) {
|
func TestCheck(t *testing.T) {
|
||||||
if !*flagCheck {
|
if !*flagCheck {
|
||||||
t.Skip("-check not specified")
|
t.Skip("-check not specified")
|
||||||
|
|
|
||||||
|
|
@ -957,17 +957,17 @@ func (w *Walker) emitType(obj *types.TypeName) {
|
||||||
if w.isDeprecated(obj) {
|
if w.isDeprecated(obj) {
|
||||||
w.emitf("type %s //deprecated", name)
|
w.emitf("type %s //deprecated", name)
|
||||||
}
|
}
|
||||||
|
typ := obj.Type()
|
||||||
|
if obj.IsAlias() {
|
||||||
|
w.emitf("type %s = %s", name, w.typeString(typ))
|
||||||
|
return
|
||||||
|
}
|
||||||
if tparams := obj.Type().(*types.Named).TypeParams(); tparams != nil {
|
if tparams := obj.Type().(*types.Named).TypeParams(); tparams != nil {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
buf.WriteString(name)
|
buf.WriteString(name)
|
||||||
w.writeTypeParams(&buf, tparams, true)
|
w.writeTypeParams(&buf, tparams, true)
|
||||||
name = buf.String()
|
name = buf.String()
|
||||||
}
|
}
|
||||||
typ := obj.Type()
|
|
||||||
if obj.IsAlias() {
|
|
||||||
w.emitf("type %s = %s", name, w.typeString(typ))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch typ := typ.Underlying().(type) {
|
switch typ := typ.Underlying().(type) {
|
||||||
case *types.Struct:
|
case *types.Struct:
|
||||||
w.emitStructType(name, typ)
|
w.emitStructType(name, typ)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
package p
|
||||||
|
|
||||||
|
type BasicAlias = uint8
|
||||||
Loading…
Reference in New Issue