}
}
+ // Cycles through type parameter lists are ok (go.dev/issue/68162).
+ // TODO(gri) if we are happy with this this, remove this flag and simplify code.
+ if tparCycle {
+ return true
+ }
+
check.cycleError(cycle, firstInSrc(cycle))
return false
}
}
}
+ // Cycles through type parameter lists are ok (go.dev/issue/68162).
+ // TODO(gri) if we are happy with this this, remove this flag and simplify code.
+ if tparCycle {
+ return true
+ }
+
check.cycleError(cycle, firstInSrc(cycle))
return false
}
package p
-type Builder /* ERROR "invalid recursive type" */ [T interface{ struct{ Builder[T] } }] struct{}
+type Builder[T ~struct{ Builder[T] }] struct{}
type myBuilder struct {
Builder[myBuilder]
}
package p
// test case 1
-type T /* ERROR "invalid recursive type" */ [U interface{ M() T[U] }] int
+type T[U interface{ M() T[U] }] int
type X int
func (X) M() T[X] { return 0 }
// test case 2
-type A /* ERROR "invalid recursive type" */ [T interface{ A[T] }] interface{}
+type A[T interface{ A[T] }] interface{}
// test case 3
-type A2 /* ERROR "invalid recursive type" */ [U interface{ A2[U] }] interface{ M() A2[U] }
+type A2[U interface{ A2[U] }] interface{ M() A2[U] }
type I interface{ A2[I]; M() A2[I] }
package p
// test case 1
-type T /* ERROR "invalid recursive type" */ [U interface{ M() T[U] }] int
+type T[U interface{ M() T[U] }] int
type X int
func (X) M() T[X] { return 0 }
// test case 2
-type A /* ERROR "invalid recursive type" */ [T interface{ A[T] }] interface{}
+type A[T interface{ A[T] }] interface{}
// test case 3
-// TODO(gri) should report error only once
-type A2 /* ERROR "invalid recursive type" */ /* ERROR "invalid recursive type" */ [U interface{ A2[U] }] interface{ M() A2[U] }
+type A2[U interface{ A2[U] }] interface{ M() A2[U] }
type I interface{ A2[I]; M() A2[I] }
// parameterized types with self-recursive constraints
type (
- T1 /* ERROR "invalid recursive type" */ [P T1[P]] interface{}
- T2 /* ERROR "invalid recursive type" */ [P, Q T2[P, Q]] interface{}
+ T1[P T1[P]] interface{}
+ T2[P, Q T2[P, Q]] interface{}
T3[P T2[P, Q], Q interface{ ~string }] interface{}
- T4a /* ERROR "invalid recursive type" */ [P T4a[P]] interface{ ~int }
- T4b /* ERROR "invalid recursive type" */ [P T4b[int]] interface{ ~int }
- T4c /* ERROR "invalid recursive type" */ [P T4c[string]] interface{ ~int }
+ T4a[P T4a[P]] interface{ ~int }
+ T4b[P T4b[int]] interface{ ~int }
+ T4c[P T4c[string /* ERROR "string does not satisfy T4c[string]" */]] interface{ ~int }
// mutually recursive constraints
- T5 /* ERROR "invalid recursive type" */ [P T6[P]] interface{ int }
+ T5[P T6[P]] interface{ int }
T6[P T5[P]] interface{ int }
)
// test case from issue
-type Eq /* ERROR "invalid recursive type" */ [a Eq[a]] interface {
+type Eq[a Eq[a]] interface {
Equal(that a) bool
}
package p
-type T /* ERROR "invalid recursive type" */ [U interface{ M() T[U, int] }] int
+type T[U interface{ M() T /* ERROR "too many type arguments for type T" */ [U, int] }] int
type X int
import "unsafe"
-type T0 /* ERROR "invalid recursive type" */ [P T0[P]] struct{}
+type T0[P T0[P]] struct{}
-type T1 /* ERROR "invalid recursive type" */ [P T2[P]] struct{}
-type T2[P T1[P]] struct{}
+type T1[P T2[P /* ERROR "P does not satisfy T1[P]" */]] struct{}
+type T2[P T1[P /* ERROR "P does not satisfy T2[P]" */]] struct{}
-type T3 /* ERROR "invalid recursive type" */ [P interface{ ~struct{ f T3[int] } }] struct{}
+type T3[P interface{ ~struct{ f T3[int /* ERROR "int does not satisfy" */ ] } }] struct{}
// valid cycle in M
type N[P M[P]] struct{}
-type M[Q any] struct { F *M[Q] }
+type M[Q any] struct{ F *M[Q] }
// "crazy" case
type TC[P [unsafe.Sizeof(func() {
- type T [P [unsafe.Sizeof(func(){})]byte] struct{}
+ type T[P [unsafe.Sizeof(func() {})]byte] struct{}
})]byte] struct{}
// test case from issue
-type X /* ERROR "invalid recursive type" */ [T any, PT X[T]] interface{}
+type X[T any, PT X /* ERROR "not enough type arguments for type X" */ [T]] interface{}
--- /dev/null
+// Copyright 2024 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 main
+
+type N[B N[B]] interface {
+ Add(B) B
+}
+
+func Add[P N[P]](x, y P) P {
+ return x.Add(y)
+}
+
+type MyInt int
+
+func (x MyInt) Add(y MyInt) MyInt {
+ return x + y
+}
+
+func main() {
+ var x, y MyInt = 2, 3
+ println(Add(x, y))
+}
-// errorcheck
+// compile
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
package p
-type T[U interface{ M() T[U] }] int // ERROR "invalid recursive type: T refers to itself"
+type T[U interface{ M() T[U] }] int
type X int
package a
-type T[U interface{ M() int }] int
+type T[U interface{ M() T[U] }] int
type X int
-func (X) M() int { return 0 }
-
-type _ a.T[X]
+func (X) M() a.T[X] { return 0 }
package a
-type I[T any] interface {
+type I[T I[T]] interface {
F() T
}
package a
-type I[T any] interface {
+type I[T I[T]] interface {
F() T
}