From 35fb79be6abc1a9a0860cd28cae89dda038f5125 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 1 Apr 2022 17:02:28 -0700 Subject: [PATCH] go/types, types2: fix overlap test for union termlist Per the spec, "the type sets of all non-interface terms must be pairwise disjoint (the pairwise intersection of the type sets must be empty)" in a union. For the overlap test, the existing implementation casually mixed syntactic union terms (which may have interface type) with type set terms (which are normalized/expanded and must not have interface type). As a consequence, in some cases the overlap test failed. This change skips terms with interface types in the overlap test. Fixes #51607. Change-Id: I8ae9953db31f0a0428389c6a45a6696aa2450219 Reviewed-on: https://go-review.googlesource.com/c/go/+/397695 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Reviewed-by: Robert Findley --- .../types2/testdata/examples/constraints.go | 3 +- .../types2/testdata/fixedbugs/issue51607.go | 65 +++++++++++++++++++ src/cmd/compile/internal/types2/union.go | 14 ++-- src/go/types/testdata/examples/constraints.go | 3 +- src/go/types/testdata/fixedbugs/issue51607.go | 65 +++++++++++++++++++ src/go/types/union.go | 14 ++-- 6 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 src/cmd/compile/internal/types2/testdata/fixedbugs/issue51607.go create mode 100644 src/go/types/testdata/fixedbugs/issue51607.go diff --git a/src/cmd/compile/internal/types2/testdata/examples/constraints.go b/src/cmd/compile/internal/types2/testdata/examples/constraints.go index fb01be56a2..5b144893ce 100644 --- a/src/cmd/compile/internal/types2/testdata/examples/constraints.go +++ b/src/cmd/compile/internal/types2/testdata/examples/constraints.go @@ -24,7 +24,8 @@ type ( _ interface{int|any} _ interface{int|~string|union} _ interface{int|~string|interface{int}} - _ interface{union|union /* ERROR overlapping terms p.union and p.union */ } + _ interface{union|int} // interfaces (here: union) are ignored when checking for overlap + _ interface{union|union} // ditto // For now we do not permit interfaces with methods in unions. _ interface{~ /* ERROR invalid use of ~ */ any} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51607.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51607.go new file mode 100644 index 0000000000..d8df143627 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51607.go @@ -0,0 +1,65 @@ +// Copyright 2022 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 + +// Interface types must be ignored during overlap test. + +type ( + T1 interface{int} + T2 interface{~int} + T3 interface{T1 | bool | string} + T4 interface{T2 | ~bool | ~string} +) + +type ( + // overlap errors for non-interface terms + // (like the interface terms, but explicitly inlined) + _ interface{int | int /* ERROR overlapping terms int and int */ } + _ interface{int | ~ /* ERROR overlapping terms ~int and int */ int} + _ interface{~int | int /* ERROR overlapping terms int and ~int */ } + _ interface{~int | ~ /* ERROR overlapping terms ~int and ~int */ int} + + _ interface{T1 | bool | string | T1 | bool /* ERROR overlapping terms bool and bool */ | string /* ERROR overlapping terms string and string */ } + _ interface{T1 | bool | string | T2 | ~ /* ERROR overlapping terms ~bool and bool */ bool | ~ /* ERROR overlapping terms ~string and string */ string} + + // no errors for interface terms + _ interface{T1 | T1} + _ interface{T1 | T2} + _ interface{T2 | T1} + _ interface{T2 | T2} + + _ interface{T3 | T3 | int} + _ interface{T3 | T4 | bool } + _ interface{T4 | T3 | string } + _ interface{T4 | T4 | float64 } +) + +func _[_ T1 | bool | string | T1 | bool /* ERROR overlapping terms */ ]() {} +func _[_ T1 | bool | string | T2 | ~ /* ERROR overlapping terms */ bool ]() {} +func _[_ T2 | ~bool | ~string | T1 | bool /* ERROR overlapping terms */ ]() {} +func _[_ T2 | ~bool | ~string | T2 | ~ /* ERROR overlapping terms */ bool ]() {} + +func _[_ T3 | T3 | int]() {} +func _[_ T3 | T4 | bool]() {} +func _[_ T4 | T3 | string]() {} +func _[_ T4 | T4 | float64]() {} + +// test cases from issue + +type _ interface { + interface {bool | int} | interface {bool | string} +} + +type _ interface { + interface {bool | int} ; interface {bool | string} +} + +type _ interface { + interface {bool; int} ; interface {bool; string} +} + +type _ interface { + interface {bool; int} | interface {bool; string} +} \ No newline at end of file diff --git a/src/cmd/compile/internal/types2/union.go b/src/cmd/compile/internal/types2/union.go index 57f1a4fe2a..0ed125fb29 100644 --- a/src/cmd/compile/internal/types2/union.go +++ b/src/cmd/compile/internal/types2/union.go @@ -113,14 +113,12 @@ func parseUnion(check *Checker, uexpr syntax.Expr) Type { switch { case tset.NumMethods() != 0: check.errorf(tlist[i], "cannot use %s in union (%s contains methods)", t, t) - continue case t.typ == universeComparable.Type(): check.error(tlist[i], "cannot use comparable in union") - continue case tset.comparable: check.errorf(tlist[i], "cannot use %s in union (%s embeds comparable)", t, t) - continue } + continue // terms with interface types are not subject to the no-overlap rule } // Report overlapping (non-disjoint) terms such as @@ -164,10 +162,16 @@ func parseTilde(check *Checker, tx syntax.Expr) *Term { // overlappingTerm reports the index of the term x in terms which is // overlapping (not disjoint) from y. The result is < 0 if there is no -// such term. +// such term. The type of term y must not be an interface, and terms +// with an interface type are ignored in the terms list. func overlappingTerm(terms []*Term, y *Term) int { + assert(!IsInterface(y.typ)) for i, x := range terms { - // disjoint requires non-nil, non-top arguments + if IsInterface(x.typ) { + continue + } + // disjoint requires non-nil, non-top arguments, + // and non-interface types as term types. if debug { if x == nil || x.typ == nil || y == nil || y.typ == nil { panic("empty or top union term") diff --git a/src/go/types/testdata/examples/constraints.go b/src/go/types/testdata/examples/constraints.go index fb01be56a2..5b144893ce 100644 --- a/src/go/types/testdata/examples/constraints.go +++ b/src/go/types/testdata/examples/constraints.go @@ -24,7 +24,8 @@ type ( _ interface{int|any} _ interface{int|~string|union} _ interface{int|~string|interface{int}} - _ interface{union|union /* ERROR overlapping terms p.union and p.union */ } + _ interface{union|int} // interfaces (here: union) are ignored when checking for overlap + _ interface{union|union} // ditto // For now we do not permit interfaces with methods in unions. _ interface{~ /* ERROR invalid use of ~ */ any} diff --git a/src/go/types/testdata/fixedbugs/issue51607.go b/src/go/types/testdata/fixedbugs/issue51607.go new file mode 100644 index 0000000000..d8df143627 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue51607.go @@ -0,0 +1,65 @@ +// Copyright 2022 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 + +// Interface types must be ignored during overlap test. + +type ( + T1 interface{int} + T2 interface{~int} + T3 interface{T1 | bool | string} + T4 interface{T2 | ~bool | ~string} +) + +type ( + // overlap errors for non-interface terms + // (like the interface terms, but explicitly inlined) + _ interface{int | int /* ERROR overlapping terms int and int */ } + _ interface{int | ~ /* ERROR overlapping terms ~int and int */ int} + _ interface{~int | int /* ERROR overlapping terms int and ~int */ } + _ interface{~int | ~ /* ERROR overlapping terms ~int and ~int */ int} + + _ interface{T1 | bool | string | T1 | bool /* ERROR overlapping terms bool and bool */ | string /* ERROR overlapping terms string and string */ } + _ interface{T1 | bool | string | T2 | ~ /* ERROR overlapping terms ~bool and bool */ bool | ~ /* ERROR overlapping terms ~string and string */ string} + + // no errors for interface terms + _ interface{T1 | T1} + _ interface{T1 | T2} + _ interface{T2 | T1} + _ interface{T2 | T2} + + _ interface{T3 | T3 | int} + _ interface{T3 | T4 | bool } + _ interface{T4 | T3 | string } + _ interface{T4 | T4 | float64 } +) + +func _[_ T1 | bool | string | T1 | bool /* ERROR overlapping terms */ ]() {} +func _[_ T1 | bool | string | T2 | ~ /* ERROR overlapping terms */ bool ]() {} +func _[_ T2 | ~bool | ~string | T1 | bool /* ERROR overlapping terms */ ]() {} +func _[_ T2 | ~bool | ~string | T2 | ~ /* ERROR overlapping terms */ bool ]() {} + +func _[_ T3 | T3 | int]() {} +func _[_ T3 | T4 | bool]() {} +func _[_ T4 | T3 | string]() {} +func _[_ T4 | T4 | float64]() {} + +// test cases from issue + +type _ interface { + interface {bool | int} | interface {bool | string} +} + +type _ interface { + interface {bool | int} ; interface {bool | string} +} + +type _ interface { + interface {bool; int} ; interface {bool; string} +} + +type _ interface { + interface {bool; int} | interface {bool; string} +} \ No newline at end of file diff --git a/src/go/types/union.go b/src/go/types/union.go index b288dfab5c..37a3489558 100644 --- a/src/go/types/union.go +++ b/src/go/types/union.go @@ -116,14 +116,12 @@ func parseUnion(check *Checker, uexpr ast.Expr) Type { switch { case tset.NumMethods() != 0: check.errorf(tlist[i], _InvalidUnion, "cannot use %s in union (%s contains methods)", t, t) - continue case t.typ == universeComparable.Type(): check.error(tlist[i], _InvalidUnion, "cannot use comparable in union") - continue case tset.comparable: check.errorf(tlist[i], _InvalidUnion, "cannot use %s in union (%s embeds comparable)", t, t) - continue } + continue // terms with interface types are not subject to the no-overlap rule } // Report overlapping (non-disjoint) terms such as @@ -167,10 +165,16 @@ func parseTilde(check *Checker, tx ast.Expr) *Term { // overlappingTerm reports the index of the term x in terms which is // overlapping (not disjoint) from y. The result is < 0 if there is no -// such term. +// such term. The type of term y must not be an interface, and terms +// with an interface type are ignored in the terms list. func overlappingTerm(terms []*Term, y *Term) int { + assert(!IsInterface(y.typ)) for i, x := range terms { - // disjoint requires non-nil, non-top arguments + if IsInterface(x.typ) { + continue + } + // disjoint requires non-nil, non-top arguments, + // and non-interface types as term types. if debug { if x == nil || x.typ == nil || y == nil || y.typ == nil { panic("empty or top union term") -- 2.50.0