]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/api: fix panic on exported basic type aliases
authorDevon H. O'Dell <dhobsd@google.com>
Thu, 4 Jan 2024 16:49:17 +0000 (11:49 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 4 Jan 2024 17:31:12 +0000 (17:31 +0000)
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>

src/cmd/api/api_test.go
src/cmd/api/main_test.go
src/cmd/api/testdata/src/issue64958/p/p.go [new file with mode: 0644]

index 910e046f12e0615237c0a4f5dec8d0f899cf0fad..ba358d364d51dce627346f9668e4f9c93cf6b5ab 100644 (file)
@@ -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) {
        if !*flagCheck {
                t.Skip("-check not specified")
index 94e159e7d8fa2edcdbcc42e4b1fdb671979832ac..7985055b5c0de2ecf8634f51fa48db9ae21cb8eb 100644 (file)
@@ -957,17 +957,17 @@ func (w *Walker) emitType(obj *types.TypeName) {
        if w.isDeprecated(obj) {
                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 {
                var buf bytes.Buffer
                buf.WriteString(name)
                w.writeTypeParams(&buf, tparams, true)
                name = buf.String()
        }
-       typ := obj.Type()
-       if obj.IsAlias() {
-               w.emitf("type %s = %s", name, w.typeString(typ))
-               return
-       }
        switch typ := typ.Underlying().(type) {
        case *types.Struct:
                w.emitStructType(name, typ)
diff --git a/src/cmd/api/testdata/src/issue64958/p/p.go b/src/cmd/api/testdata/src/issue64958/p/p.go
new file mode 100644 (file)
index 0000000..feba867
--- /dev/null
@@ -0,0 +1,3 @@
+package p
+
+type BasicAlias = uint8