]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.17] go/types: break cycles in invalid types
authorRobert Griesemer <gri@golang.org>
Wed, 6 Oct 2021 16:33:55 +0000 (09:33 -0700)
committerMichael Knyszek <mknyszek@google.com>
Wed, 1 Dec 2021 23:34:46 +0000 (23:34 +0000)
This is a partial port of CL 354329 from types2 to go/types.
It contains an adjustment to type.go to deal with possibly
invalid type bounds.

Fixes #48825.
For #48819.

Change-Id: I9efdcdbfa6432f3cee64d924a4c67ecc6793cf86
Reviewed-on: https://go-review.googlesource.com/c/go/+/354349
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/368456
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/go/types/decl.go
src/go/types/testdata/fixedbugs/issue48819.src [new file with mode: 0644]
src/go/types/type.go

index 9211febc6da1620a43e9867af2f37c149a6f0753..ec173ad1ccc9cb62ed3ae292ae303e5e1f23c057 100644 (file)
@@ -343,6 +343,7 @@ func (check *Checker) validType(typ Type, path []Object) typeInfo {
                                if tn == t.obj {
                                        check.cycleError(path[i:])
                                        t.info = invalid
+                                       t.underlying = Typ[Invalid]
                                        return t.info
                                }
                        }
diff --git a/src/go/types/testdata/fixedbugs/issue48819.src b/src/go/types/testdata/fixedbugs/issue48819.src
new file mode 100644 (file)
index 0000000..9262110
--- /dev/null
@@ -0,0 +1,15 @@
+// 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 p
+
+import "unsafe"
+
+type T /* ERROR illegal cycle in declaration of T */ struct {
+       T
+}
+
+func _(t T) {
+       _ = unsafe.Sizeof(t) // should not go into infinite recursion here
+}
index 2660ce4408c1b640f67bf517ca72d3819055907e..20c4bec0bce241ac84fae75a309f90e81948f1e6 100644 (file)
@@ -759,6 +759,9 @@ func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *_TypeP
 
 func (t *_TypeParam) Bound() *Interface {
        iface := asInterface(t.bound)
+       if iface == nil {
+               return &emptyInterface
+       }
        // use the type bound position if we have one
        pos := token.NoPos
        if n, _ := t.bound.(*Named); n != nil {