Skip to content

Commit 73a062c

Browse files
committed
cmd/compile/internal/types2: handle recursive type parameter constraints
Check type constraints after the respective type parameter list has been associated with a parameterized type so that recursive type parameter constraints "see" a parameterized type. Fixes #45550. Fixes #47796. Change-Id: Iac74610ca017a78013820624230c857395506aff Reviewed-on: https://go-review.googlesource.com/c/go/+/348090 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 9581d89 commit 73a062c

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

src/cmd/compile/internal/types2/decl.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
567567
if tdecl.TParamList != nil {
568568
check.openScope(tdecl, "type parameters")
569569
defer check.closeScope()
570-
named.tparams = check.collectTypeParams(tdecl.TParamList)
570+
check.collectTypeParams(&named.tparams, tdecl.TParamList)
571571
}
572572

573573
// determine underlying type of named
@@ -598,7 +598,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
598598
}
599599
}
600600

601-
func (check *Checker) collectTypeParams(list []*syntax.Field) *TParamList {
601+
func (check *Checker) collectTypeParams(dst **TParamList, list []*syntax.Field) {
602602
tparams := make([]*TypeParam, len(list))
603603

604604
// Declare type parameters up-front.
@@ -608,6 +608,11 @@ func (check *Checker) collectTypeParams(list []*syntax.Field) *TParamList {
608608
tparams[i] = check.declareTypeParam(f.Name)
609609
}
610610

611+
// Set the type parameters before collecting the type constraints because
612+
// the parameterized type may be used by the constraints (issue #47887).
613+
// Example: type T[P T[P]] interface{}
614+
*dst = bindTParams(tparams)
615+
611616
var bound Type
612617
for i, f := range list {
613618
// Optimization: Re-use the previous type bound if it hasn't changed.
@@ -618,13 +623,17 @@ func (check *Checker) collectTypeParams(list []*syntax.Field) *TParamList {
618623
}
619624
tparams[i].bound = bound
620625
}
621-
622-
return bindTParams(tparams)
623626
}
624627

625628
func (check *Checker) declareTypeParam(name *syntax.Name) *TypeParam {
629+
// Use Typ[Invalid] for the type constraint to ensure that a type
630+
// is present even if the actual constraint has not been assigned
631+
// yet.
632+
// TODO(gri) Need to systematically review all uses of type parameter
633+
// constraints to make sure we don't rely on them if they
634+
// are not properly set yet.
626635
tname := NewTypeName(name.Pos(), check.pkg, name.Value, nil)
627-
tpar := check.NewTypeParam(tname, nil) // assigns type to tname as a side-effect
636+
tpar := check.NewTypeParam(tname, Typ[Invalid]) // assigns type to tname as a side-effect
628637
check.declare(check.scope, name, tname, check.scope.pos) // TODO(gri) check scope position
629638
return tpar
630639
}

src/cmd/compile/internal/types2/signature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
157157
}
158158

159159
if tparams != nil {
160-
sig.tparams = check.collectTypeParams(tparams)
160+
check.collectTypeParams(&sig.tparams, tparams)
161161
// Always type-check method type parameters but complain if they are not enabled.
162162
// (A separate check is needed when type-checking interface method signatures because
163163
// they don't have a receiver specification.)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2021 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+
type Builder[T interface{ struct{ Builder[T] } }] struct{}
8+
type myBuilder struct {
9+
Builder[myBuilder /* ERROR myBuilder does not satisfy */]
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 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+
// parameterized types with self-recursive constraints
8+
type (
9+
T1[P T1[P]] interface{}
10+
T2[P, Q T2[P, Q]] interface{}
11+
T3[P T2[P, Q], Q interface{ ~string }] interface{}
12+
13+
T4a[P T4a[P]] interface{ ~int }
14+
T4b[P T4b[int]] interface{ ~int }
15+
T4c[P T4c[string /* ERROR string does not satisfy T4c\[string\] */]] interface{ ~int }
16+
17+
// mutually recursive constraints
18+
T5[P T6[P]] interface{ int }
19+
T6[P T5[P]] interface{ int }
20+
)
21+
22+
// verify that constraints are checked as expected
23+
var (
24+
_ T1[int]
25+
_ T2[int, string]
26+
_ T3[int, string]
27+
)
28+
29+
// test case from issue
30+
31+
type Eq[a Eq[a]] interface {
32+
Equal(that a) bool
33+
}

0 commit comments

Comments
 (0)