1
0
mirror of https://github.com/golang/go synced 2024-11-17 05:54:46 -07:00

go/types,types2: disallow illegal cycles through Unions

Checker.validType was not considering Unions when looking for illegal
cycles.

Fixes #48582

Change-Id: I11ad0279eeaaa56bb6d5731b0572c1c3a0c459eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/351829
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Findley 2021-09-23 12:19:05 -04:00
parent 5961134fa5
commit 13f3c57cef
6 changed files with 75 additions and 3 deletions

View File

@ -315,6 +315,13 @@ func (check *Checker) validType(typ Type, path []Object) typeInfo {
}
}
case *Union:
for _, t := range t.terms {
if check.validType(t.typ, path) == invalid {
return invalid
}
}
case *Interface:
for _, etyp := range t.embeddeds {
if check.validType(etyp, path) == invalid {

View File

@ -6,7 +6,7 @@ package p
// Test case from issue.
type Nat interface {
type Nat /* ERROR cycle */ interface {
Zero|Succ
}

View File

@ -0,0 +1,29 @@
// 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
type N /* ERROR cycle */ interface {
int | N
}
type A /* ERROR cycle */ interface {
int | B
}
type B interface {
int | A
}
type S /* ERROR cycle */ struct {
I // ERROR interface contains type constraints
}
type I interface {
int | S
}
type P interface {
*P // ERROR interface contains type constraints
}

View File

@ -314,6 +314,13 @@ func (check *Checker) validType(typ Type, path []Object) typeInfo {
}
}
case *Union:
for _, t := range t.terms {
if check.validType(t.typ, path) == invalid {
return invalid
}
}
case *Interface:
for _, etyp := range t.embeddeds {
if check.validType(etyp, path) == invalid {

View File

@ -6,13 +6,13 @@ package p
// Test case from issue.
type Nat interface {
type Nat /* ERROR cycle */ interface {
Zero|Succ
}
type Zero struct{}
type Succ struct{
Nat // ERROR interface contains type constraints
Nat /* ERROR interface contains type constraints */
}
// Struct tests.

View File

@ -0,0 +1,29 @@
// 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
type N /* ERROR cycle */ interface {
int | N
}
type A /* ERROR cycle */ interface {
int | B
}
type B interface {
int | A
}
type S /* ERROR cycle */ struct {
I // ERROR interface contains type constraints
}
type I interface {
int | S
}
type P interface {
*P // ERROR interface contains type constraints
}