From 13f3c57cefef59e5d94f678483aff527807fb33b Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Thu, 23 Sep 2021 12:19:05 -0400 Subject: [PATCH] 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 Run-TryBot: Robert Findley TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/decl.go | 7 +++++ .../types2/testdata/fixedbugs/issue41124.go2 | 2 +- .../types2/testdata/fixedbugs/issue48582.go2 | 29 +++++++++++++++++++ src/go/types/decl.go | 7 +++++ .../types/testdata/fixedbugs/issue41124.go2 | 4 +-- .../types/testdata/fixedbugs/issue48582.go2 | 29 +++++++++++++++++++ 6 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/cmd/compile/internal/types2/testdata/fixedbugs/issue48582.go2 create mode 100644 src/go/types/testdata/fixedbugs/issue48582.go2 diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go index 994c19ea30a..ab2e3b875f0 100644 --- a/src/cmd/compile/internal/types2/decl.go +++ b/src/cmd/compile/internal/types2/decl.go @@ -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 { diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 index 4642ab60fc8..cef24bd2379 100644 --- a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 @@ -6,7 +6,7 @@ package p // Test case from issue. -type Nat interface { +type Nat /* ERROR cycle */ interface { Zero|Succ } diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48582.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48582.go2 new file mode 100644 index 00000000000..c12091be790 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48582.go2 @@ -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 +} diff --git a/src/go/types/decl.go b/src/go/types/decl.go index 061fc018296..77914dd1af9 100644 --- a/src/go/types/decl.go +++ b/src/go/types/decl.go @@ -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 { diff --git a/src/go/types/testdata/fixedbugs/issue41124.go2 b/src/go/types/testdata/fixedbugs/issue41124.go2 index 4642ab60fc8..ac336a2eceb 100644 --- a/src/go/types/testdata/fixedbugs/issue41124.go2 +++ b/src/go/types/testdata/fixedbugs/issue41124.go2 @@ -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. diff --git a/src/go/types/testdata/fixedbugs/issue48582.go2 b/src/go/types/testdata/fixedbugs/issue48582.go2 new file mode 100644 index 00000000000..c12091be790 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue48582.go2 @@ -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 +}