Skip to content

Commit cbdb93d

Browse files
committed
[dev.go2go] go/types: a type constraint must be an interface
In particular, it cannot be a type parameter with an operational type that is an interface. Fixes #39723. Change-Id: Ia6b65eaecd472dd71b1c9f7631ce1872f06e5a6d Reviewed-on: https://go-review.googlesource.com/c/go/+/239161 Reviewed-by: Robert Griesemer <[email protected]>
1 parent 815b29d commit cbdb93d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/go/types/check_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var tests = [][]string{
129129
{"fixedbugs/issue39693.go2"},
130130
{"fixedbugs/issue39680.go2"},
131131
{"fixedbugs/issue39711.go2"},
132+
{"fixedbugs/issue39723.go2"},
132133
}
133134

134135
var fset = token.NewFileSet()

src/go/types/decl.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,17 +615,19 @@ func (check *Checker) collectTypeParams(list *ast.FieldList) (tparams []*TypeNam
615615
}
616616

617617
index := 0
618+
var bound Type
618619
for _, f := range list.List {
619620
if f.Type == nil {
620621
goto next
621622
}
622623

623624
// type bound must be an interface
624-
// TODO(gri) We should try to delay the IsInterface check
625-
// as it may expand a possibly incomplete type.
626-
// Worse, it could be a type parameter that is
627-
// not yet instantiated if we accept #39723.
628-
if bound := check.anyType(f.Type); IsInterface(bound) {
625+
// TODO(gri) We should delay the interface check because
626+
// we may not have a complete interface yet:
627+
// type C(type T C) interface {}
628+
// (issue #39724).
629+
bound = check.anyType(f.Type)
630+
if _, ok := bound.Under().(*Interface); ok {
629631
// If the type bound expects exactly one type argument, permit leaving
630632
// it away and use the corresponding type parameter as implicit argument.
631633
// This allows us to write (type p b(p), q b(q), r b(r)) as (type p, q, r b).

src/go/types/fixedbugs/issue39723.go2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
// A constraint must be an interface; it cannot
8+
// be a type parameter, for instance.
9+
func _(type A interface{ type interface{} }, B A /* ERROR not an interface */ )()

0 commit comments

Comments
 (0)