From 66007e5cfc9e151485791d440b7e67d44af1b29f Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 6 Oct 2021 09:33:55 -0700 Subject: [PATCH] [release-branch.go1.17] go/types: break cycles in invalid types 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 Reviewed-by: Robert Findley Reviewed-on: https://go-review.googlesource.com/c/go/+/368456 Run-TryBot: Robert Griesemer TryBot-Result: Go Bot --- src/go/types/decl.go | 1 + src/go/types/testdata/fixedbugs/issue48819.src | 15 +++++++++++++++ src/go/types/type.go | 3 +++ 3 files changed, 19 insertions(+) create mode 100644 src/go/types/testdata/fixedbugs/issue48819.src diff --git a/src/go/types/decl.go b/src/go/types/decl.go index 9211febc6d..ec173ad1cc 100644 --- a/src/go/types/decl.go +++ b/src/go/types/decl.go @@ -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 index 0000000000..9262110ea0 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue48819.src @@ -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 +} diff --git a/src/go/types/type.go b/src/go/types/type.go index 2660ce4408..20c4bec0bc 100644 --- a/src/go/types/type.go +++ b/src/go/types/type.go @@ -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 { -- 2.50.0