mirror of
https://github.com/golang/go
synced 2024-10-14 03:43:28 +00:00
go/types, types2: disable inference for type instances
Inference for type instances has dependencies on type-checking order that can lead to subtle bugs. As explained in #51527, disable it for 1.18. Fixes #51527 Change-Id: I42795bad30ce53abecfc5a4914599ae5a2041a9e Reviewed-on: https://go-review.googlesource.com/c/go/+/387934 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
20dd9a42fb
commit
28fab5ef21
|
@ -4,22 +4,24 @@
|
||||||
|
|
||||||
package typeInference
|
package typeInference
|
||||||
|
|
||||||
|
// As of issue #51527, type-type inference has been disabled.
|
||||||
|
|
||||||
// basic inference
|
// basic inference
|
||||||
type Tb[P ~*Q, Q any] int
|
type Tb[P ~*Q, Q any] int
|
||||||
func _() {
|
func _() {
|
||||||
var x Tb[*int]
|
var x Tb /* ERROR got 1 arguments */ [*int]
|
||||||
var y Tb[*int, int]
|
var y Tb[*int, int]
|
||||||
x = y
|
x = y /* ERROR cannot use y .* in assignment */
|
||||||
_ = x
|
_ = x
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursive inference
|
// recursive inference
|
||||||
type Tr[A any, B *C, C *D, D *A] int
|
type Tr[A any, B *C, C *D, D *A] int
|
||||||
func _() {
|
func _() {
|
||||||
var x Tr[string]
|
var x Tr /* ERROR got 1 arguments */ [string]
|
||||||
var y Tr[string, ***string, **string, *string]
|
var y Tr[string, ***string, **string, *string]
|
||||||
var z Tr[int, ***int, **int, *int]
|
var z Tr[int, ***int, **int, *int]
|
||||||
x = y
|
x = y /* ERROR cannot use y .* in assignment */
|
||||||
x = z // ERROR cannot use z .* as Tr
|
x = z // ERROR cannot use z .* as Tr
|
||||||
_ = x
|
_ = x
|
||||||
}
|
}
|
||||||
|
@ -31,17 +33,17 @@ type To2[A any, B [][]A] int
|
||||||
type To3[A any, B [3]*A] int
|
type To3[A any, B [3]*A] int
|
||||||
type To4[A any, B any, C struct{a A; b B}] int
|
type To4[A any, B any, C struct{a A; b B}] int
|
||||||
func _() {
|
func _() {
|
||||||
var _ To0[int]
|
var _ To0 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To1[int]
|
var _ To1 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To2[int]
|
var _ To2 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To3[int]
|
var _ To3 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To4[int, string]
|
var _ To4 /* ERROR got 2 arguments */ [int, string]
|
||||||
}
|
}
|
||||||
|
|
||||||
// failed inference
|
// failed inference
|
||||||
type Tf0[A, B any] int
|
type Tf0[A, B any] int
|
||||||
type Tf1[A any, B ~struct{a A; c C}, C any] int
|
type Tf1[A any, B ~struct{a A; c C}, C any] int
|
||||||
func _() {
|
func _() {
|
||||||
var _ Tf0 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
|
var _ Tf0 /* ERROR got 1 arguments but 2 type parameters */ [int]
|
||||||
var _ Tf1 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 3 type parameters */ [int]
|
var _ Tf1 /* ERROR got 1 arguments but 3 type parameters */ [int]
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ type S[A, B any] struct {
|
||||||
|
|
||||||
func (S[A, B]) m() {}
|
func (S[A, B]) m() {}
|
||||||
|
|
||||||
// TODO(gri) We should only report one error below. See issue #50588.
|
// TODO(gri): with type-type inference enabled we should only report one error
|
||||||
|
// below. See issue #50588.
|
||||||
|
|
||||||
func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [A]) {
|
func _[A any](s S /* ERROR got 1 arguments but 2 type parameters */ [A]) {
|
||||||
// we should see no follow-on errors below
|
// we should see no follow-on errors below
|
||||||
s.f = 1
|
s.f = 1
|
||||||
s.m()
|
s.m()
|
||||||
|
@ -21,7 +22,7 @@ func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type
|
||||||
// another test case from the issue
|
// another test case from the issue
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
X(Interface[*F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
|
X(Interface[*F /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func X[Q Qer](fs Interface[Q]) {
|
func X[Q Qer](fs Interface[Q]) {
|
||||||
|
|
|
@ -16,7 +16,7 @@ func G[A, B any](F[A, B]) {
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
// TODO(gri) only report one error below (issue #50932)
|
// TODO(gri) only report one error below (issue #50932)
|
||||||
var x F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
|
var x F /* ERROR got 1 arguments but 2 type parameters */ [int]
|
||||||
G(x /* ERROR does not match */)
|
G(x /* ERROR does not match */)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,9 +46,9 @@ func NSG[G any](c RSC[G]) {
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MMD[Rc RC /* ERROR cannot infer RG */ /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got 2 arguments */ /* ERROR Rc does not match */ [Rc, RG] {
|
func MMD[Rc RC /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got 2 arguments */ [Rc, RG] {
|
||||||
|
|
||||||
var nFn NFn /* ERROR got 2 arguments */ /* ERROR Rc does not match */ [Rc, RG]
|
var nFn NFn /* ERROR got 2 arguments */ [Rc, RG]
|
||||||
|
|
||||||
var empty Rc
|
var empty Rc
|
||||||
switch any(empty).(type) {
|
switch any(empty).(type) {
|
||||||
|
|
|
@ -11,19 +11,20 @@ type RC[RG any] interface {
|
||||||
type Fn[RCT RC[RG], RG any] func(RCT)
|
type Fn[RCT RC[RG], RG any] func(RCT)
|
||||||
|
|
||||||
type F[RCT RC[RG], RG any] interface {
|
type F[RCT RC[RG], RG any] interface {
|
||||||
Fn() Fn[RCT]
|
Fn() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
type concreteF[RCT RC[RG], RG any] struct {
|
type concreteF[RCT RC[RG], RG any] struct {
|
||||||
makeFn func() Fn[RCT]
|
makeFn func() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
|
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
|
||||||
return c.makeFn()
|
return c.makeFn()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F[RCT] {
|
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F /* ERROR got 1 arguments */ [RCT] {
|
||||||
return &concreteF[RCT]{
|
// TODO(rfindley): eliminate the duplicate error below.
|
||||||
|
return & /* ERROR cannot use .* as F\[RCT\] */ concreteF /* ERROR got 1 arguments */ [RCT]{
|
||||||
makeFn: nil,
|
makeFn: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,22 +4,24 @@
|
||||||
|
|
||||||
package p
|
package p
|
||||||
|
|
||||||
|
// As of issue #51527, type-type inference has been disabled.
|
||||||
|
|
||||||
type RC[RG any] interface {
|
type RC[RG any] interface {
|
||||||
~[]RG
|
~[]RG
|
||||||
}
|
}
|
||||||
|
|
||||||
type Fn[RCT RC[RG], RG any] func(RCT)
|
type Fn[RCT RC[RG], RG any] func(RCT)
|
||||||
|
|
||||||
type FFn[RCT RC[RG], RG any] func() Fn[RCT]
|
type FFn[RCT RC[RG], RG any] func() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
|
|
||||||
type F[RCT RC[RG], RG any] interface {
|
type F[RCT RC[RG], RG any] interface {
|
||||||
Fn() Fn[RCT]
|
Fn() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
type concreteF[RCT RC[RG], RG any] struct {
|
type concreteF[RCT RC[RG], RG any] struct {
|
||||||
makeFn FFn[RCT]
|
makeFn FFn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
|
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
|
||||||
return c.makeFn()
|
return c.makeFn()
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,9 @@ package p
|
||||||
type T[P any, B *P] struct{}
|
type T[P any, B *P] struct{}
|
||||||
|
|
||||||
func (T /* ERROR cannot use generic type */ ) m0() {}
|
func (T /* ERROR cannot use generic type */ ) m0() {}
|
||||||
func (T /* ERROR got 1 type parameter, but receiver base type declares 2 */ [_]) m1() {}
|
|
||||||
|
// TODO(rfindley): eliminate the duplicate errors here.
|
||||||
|
func (T /* ERROR got 1 type parameter, but receiver base type declares 2 */ /* ERROR got 1 arguments but 2 type parameters */ [_]) m1() {}
|
||||||
func (T[_, _]) m2() {}
|
func (T[_, _]) m2() {}
|
||||||
// TODO(gri) this error is unfortunate (issue #51343)
|
// TODO(gri) this error is unfortunate (issue #51343)
|
||||||
func (T /* ERROR got 3 arguments but 2 type parameters */ [_, _, _]) m3() {}
|
func (T /* ERROR got 3 arguments but 2 type parameters */ [_, _, _]) m3() {}
|
||||||
|
|
|
@ -430,10 +430,14 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
|
||||||
// evaluate arguments
|
// evaluate arguments
|
||||||
targs := check.typeList(xlist)
|
targs := check.typeList(xlist)
|
||||||
if targs == nil {
|
if targs == nil {
|
||||||
def.setUnderlying(Typ[Invalid]) // avoid later errors due to lazy instantiation
|
def.setUnderlying(Typ[Invalid]) // avoid errors later due to lazy instantiation
|
||||||
return Typ[Invalid]
|
return Typ[Invalid]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// enableTypeTypeInference controls whether to infer missing type arguments
|
||||||
|
// using constraint type inference. See issue #51527.
|
||||||
|
const enableTypeTypeInference = false
|
||||||
|
|
||||||
// create the instance
|
// create the instance
|
||||||
ctxt := check.bestContext(nil)
|
ctxt := check.bestContext(nil)
|
||||||
h := ctxt.instanceHash(orig, targs)
|
h := ctxt.instanceHash(orig, targs)
|
||||||
|
@ -453,14 +457,15 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
|
||||||
def.setUnderlying(inst)
|
def.setUnderlying(inst)
|
||||||
|
|
||||||
inst.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
|
inst.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
|
||||||
tparams := orig.TypeParams().list()
|
tparams := n.orig.TypeParams().list()
|
||||||
|
|
||||||
if len(targs) < len(tparams) {
|
targs := n.targs.list()
|
||||||
|
if enableTypeTypeInference && len(targs) < len(tparams) {
|
||||||
// If inference fails, len(inferred) will be 0, and inst.underlying will
|
// If inference fails, len(inferred) will be 0, and inst.underlying will
|
||||||
// be set to Typ[Invalid] in expandNamed.
|
// be set to Typ[Invalid] in expandNamed.
|
||||||
inferred := check.infer(x.Pos(), tparams, targs, nil, nil)
|
inferred := check.infer(x.Pos(), tparams, targs, nil, nil)
|
||||||
if len(inferred) > len(targs) {
|
if len(inferred) > len(targs) {
|
||||||
inst.targs = newTypeList(inferred)
|
n.targs = newTypeList(inferred)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,10 +478,10 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
|
||||||
// and so it must be resolved during type-checking so that we can report
|
// and so it must be resolved during type-checking so that we can report
|
||||||
// errors.
|
// errors.
|
||||||
inst.resolve(ctxt)
|
inst.resolve(ctxt)
|
||||||
check.recordInstance(x, inst.TypeArgs().list(), inst)
|
|
||||||
// Since check is non-nil, we can still mutate inst. Unpinning the resolver
|
// Since check is non-nil, we can still mutate inst. Unpinning the resolver
|
||||||
// frees some memory.
|
// frees some memory.
|
||||||
inst.resolver = nil
|
inst.resolver = nil
|
||||||
|
check.recordInstance(x, inst.TypeArgs().list(), inst)
|
||||||
|
|
||||||
if check.validateTArgLen(x.Pos(), inst.tparams.Len(), inst.targs.Len()) {
|
if check.validateTArgLen(x.Pos(), inst.tparams.Len(), inst.targs.Len()) {
|
||||||
if i, err := check.verify(x.Pos(), inst.tparams.list(), inst.targs.list()); err != nil {
|
if i, err := check.verify(x.Pos(), inst.tparams.list(), inst.targs.list()); err != nil {
|
||||||
|
|
|
@ -1339,11 +1339,6 @@ const (
|
||||||
// func _() {
|
// func _() {
|
||||||
// f()
|
// f()
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
// Example:
|
|
||||||
// type N[P, Q any] struct{}
|
|
||||||
//
|
|
||||||
// var _ N[int]
|
|
||||||
_CannotInferTypeArgs
|
_CannotInferTypeArgs
|
||||||
|
|
||||||
// _InvalidTypeArg occurs when a type argument does not satisfy its
|
// _InvalidTypeArg occurs when a type argument does not satisfy its
|
||||||
|
|
24
src/go/types/testdata/check/typeinference.go2
vendored
24
src/go/types/testdata/check/typeinference.go2
vendored
|
@ -4,22 +4,24 @@
|
||||||
|
|
||||||
package typeInference
|
package typeInference
|
||||||
|
|
||||||
|
// As of issue #51527, type-type inference has been disabled.
|
||||||
|
|
||||||
// basic inference
|
// basic inference
|
||||||
type Tb[P ~*Q, Q any] int
|
type Tb[P ~*Q, Q any] int
|
||||||
func _() {
|
func _() {
|
||||||
var x Tb[*int]
|
var x Tb /* ERROR got 1 arguments */ [*int]
|
||||||
var y Tb[*int, int]
|
var y Tb[*int, int]
|
||||||
x = y
|
x = y /* ERROR cannot use y .* in assignment */
|
||||||
_ = x
|
_ = x
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursive inference
|
// recursive inference
|
||||||
type Tr[A any, B *C, C *D, D *A] int
|
type Tr[A any, B *C, C *D, D *A] int
|
||||||
func _() {
|
func _() {
|
||||||
var x Tr[string]
|
var x Tr /* ERROR got 1 arguments */ [string]
|
||||||
var y Tr[string, ***string, **string, *string]
|
var y Tr[string, ***string, **string, *string]
|
||||||
var z Tr[int, ***int, **int, *int]
|
var z Tr[int, ***int, **int, *int]
|
||||||
x = y
|
x = y /* ERROR cannot use y .* in assignment */
|
||||||
x = z // ERROR cannot use z .* as Tr
|
x = z // ERROR cannot use z .* as Tr
|
||||||
_ = x
|
_ = x
|
||||||
}
|
}
|
||||||
|
@ -31,17 +33,17 @@ type To2[A any, B [][]A] int
|
||||||
type To3[A any, B [3]*A] int
|
type To3[A any, B [3]*A] int
|
||||||
type To4[A any, B any, C struct{a A; b B}] int
|
type To4[A any, B any, C struct{a A; b B}] int
|
||||||
func _() {
|
func _() {
|
||||||
var _ To0[int]
|
var _ To0 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To1[int]
|
var _ To1 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To2[int]
|
var _ To2 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To3[int]
|
var _ To3 /* ERROR got 1 arguments */ [int]
|
||||||
var _ To4[int, string]
|
var _ To4 /* ERROR got 2 arguments */ [int, string]
|
||||||
}
|
}
|
||||||
|
|
||||||
// failed inference
|
// failed inference
|
||||||
type Tf0[A, B any] int
|
type Tf0[A, B any] int
|
||||||
type Tf1[A any, B ~struct{a A; c C}, C any] int
|
type Tf1[A any, B ~struct{a A; c C}, C any] int
|
||||||
func _() {
|
func _() {
|
||||||
var _ Tf0 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
|
var _ Tf0 /* ERROR got 1 arguments but 2 type parameters */ [int]
|
||||||
var _ Tf1 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 3 type parameters */ [int]
|
var _ Tf1 /* ERROR got 1 arguments but 3 type parameters */ [int]
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ type S[A, B any] struct {
|
||||||
|
|
||||||
func (S[A, B]) m() {}
|
func (S[A, B]) m() {}
|
||||||
|
|
||||||
// TODO(gri) We should only report one error below. See issue #50588.
|
// TODO(gri): with type-type inference enabled we should only report one error
|
||||||
|
// below. See issue #50588.
|
||||||
|
|
||||||
func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [A]) {
|
func _[A any](s S /* ERROR got 1 arguments but 2 type parameters */ [A]) {
|
||||||
// we should see no follow-on errors below
|
// we should see no follow-on errors below
|
||||||
s.f = 1
|
s.f = 1
|
||||||
s.m()
|
s.m()
|
||||||
|
@ -21,7 +22,7 @@ func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type
|
||||||
// another test case from the issue
|
// another test case from the issue
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
X(Interface[*F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
|
X(Interface[*F /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func X[Q Qer](fs Interface[Q]) {
|
func X[Q Qer](fs Interface[Q]) {
|
||||||
|
|
|
@ -16,7 +16,7 @@ func G[A, B any](F[A, B]) {
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
// TODO(gri) only report one error below (issue #50932)
|
// TODO(gri) only report one error below (issue #50932)
|
||||||
var x F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
|
var x F /* ERROR got 1 arguments but 2 type parameters */ [int]
|
||||||
G(x /* ERROR does not match */)
|
G(x /* ERROR does not match */)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,9 +46,9 @@ func NSG[G any](c RSC[G]) {
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MMD[Rc RC /* ERROR cannot infer RG */ /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got 2 arguments */ /* ERROR Rc does not match */ [Rc, RG] {
|
func MMD[Rc RC /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got 2 arguments */ [Rc, RG] {
|
||||||
|
|
||||||
var nFn NFn /* ERROR got 2 arguments */ /* ERROR Rc does not match */ [Rc, RG]
|
var nFn NFn /* ERROR got 2 arguments */ [Rc, RG]
|
||||||
|
|
||||||
var empty Rc
|
var empty Rc
|
||||||
switch any(empty).(type) {
|
switch any(empty).(type) {
|
||||||
|
|
11
src/go/types/testdata/fixedbugs/issue51232.go2
vendored
11
src/go/types/testdata/fixedbugs/issue51232.go2
vendored
|
@ -11,19 +11,20 @@ type RC[RG any] interface {
|
||||||
type Fn[RCT RC[RG], RG any] func(RCT)
|
type Fn[RCT RC[RG], RG any] func(RCT)
|
||||||
|
|
||||||
type F[RCT RC[RG], RG any] interface {
|
type F[RCT RC[RG], RG any] interface {
|
||||||
Fn() Fn[RCT]
|
Fn() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
type concreteF[RCT RC[RG], RG any] struct {
|
type concreteF[RCT RC[RG], RG any] struct {
|
||||||
makeFn func() Fn[RCT]
|
makeFn func() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
|
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
|
||||||
return c.makeFn()
|
return c.makeFn()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F[RCT] {
|
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F /* ERROR got 1 arguments */ [RCT] {
|
||||||
return &concreteF[RCT]{
|
// TODO(rfindley): eliminate the duplicate error below.
|
||||||
|
return & /* ERROR cannot use .* as F\[RCT\] */ concreteF /* ERROR got 1 arguments */ [RCT]{
|
||||||
makeFn: nil,
|
makeFn: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/go/types/testdata/fixedbugs/issue51233.go2
vendored
10
src/go/types/testdata/fixedbugs/issue51233.go2
vendored
|
@ -4,22 +4,24 @@
|
||||||
|
|
||||||
package p
|
package p
|
||||||
|
|
||||||
|
// As of issue #51527, type-type inference has been disabled.
|
||||||
|
|
||||||
type RC[RG any] interface {
|
type RC[RG any] interface {
|
||||||
~[]RG
|
~[]RG
|
||||||
}
|
}
|
||||||
|
|
||||||
type Fn[RCT RC[RG], RG any] func(RCT)
|
type Fn[RCT RC[RG], RG any] func(RCT)
|
||||||
|
|
||||||
type FFn[RCT RC[RG], RG any] func() Fn[RCT]
|
type FFn[RCT RC[RG], RG any] func() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
|
|
||||||
type F[RCT RC[RG], RG any] interface {
|
type F[RCT RC[RG], RG any] interface {
|
||||||
Fn() Fn[RCT]
|
Fn() Fn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
type concreteF[RCT RC[RG], RG any] struct {
|
type concreteF[RCT RC[RG], RG any] struct {
|
||||||
makeFn FFn[RCT]
|
makeFn FFn /* ERROR got 1 arguments */ [RCT]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
|
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
|
||||||
return c.makeFn()
|
return c.makeFn()
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,9 @@ package p
|
||||||
type T[P any, B *P] struct{}
|
type T[P any, B *P] struct{}
|
||||||
|
|
||||||
func (T /* ERROR cannot use generic type */ ) m0() {}
|
func (T /* ERROR cannot use generic type */ ) m0() {}
|
||||||
func (/* ERROR got 1 type parameter, but receiver base type declares 2 */ T[_]) m1() {}
|
|
||||||
|
// TODO(rfindley): eliminate the duplicate errors here.
|
||||||
|
func (/* ERROR got 1 type parameter, but receiver base type declares 2 */ T /* ERROR got 1 arguments but 2 type parameters */ [_]) m1() {}
|
||||||
func (T[_, _]) m2() {}
|
func (T[_, _]) m2() {}
|
||||||
// TODO(gri) this error is unfortunate (issue #51343)
|
// TODO(gri) this error is unfortunate (issue #51343)
|
||||||
func (T /* ERROR got 3 arguments but 2 type parameters */ [_, _, _]) m3() {}
|
func (T /* ERROR got 3 arguments but 2 type parameters */ [_, _, _]) m3() {}
|
||||||
|
|
|
@ -415,10 +415,14 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (re
|
||||||
// evaluate arguments
|
// evaluate arguments
|
||||||
targs := check.typeList(ix.Indices)
|
targs := check.typeList(ix.Indices)
|
||||||
if targs == nil {
|
if targs == nil {
|
||||||
def.setUnderlying(Typ[Invalid]) // avoid later errors due to lazy instantiation
|
def.setUnderlying(Typ[Invalid]) // avoid errors later due to lazy instantiation
|
||||||
return Typ[Invalid]
|
return Typ[Invalid]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// enableTypeTypeInference controls whether to infer missing type arguments
|
||||||
|
// using constraint type inference. See issue #51527.
|
||||||
|
const enableTypeTypeInference = false
|
||||||
|
|
||||||
// create the instance
|
// create the instance
|
||||||
ctxt := check.bestContext(nil)
|
ctxt := check.bestContext(nil)
|
||||||
h := ctxt.instanceHash(orig, targs)
|
h := ctxt.instanceHash(orig, targs)
|
||||||
|
@ -438,14 +442,15 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (re
|
||||||
def.setUnderlying(inst)
|
def.setUnderlying(inst)
|
||||||
|
|
||||||
inst.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
|
inst.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
|
||||||
tparams := orig.TypeParams().list()
|
tparams := n.orig.TypeParams().list()
|
||||||
|
|
||||||
if len(targs) < len(tparams) {
|
targs := n.targs.list()
|
||||||
|
if enableTypeTypeInference && len(targs) < len(tparams) {
|
||||||
// If inference fails, len(inferred) will be 0, and inst.underlying will
|
// If inference fails, len(inferred) will be 0, and inst.underlying will
|
||||||
// be set to Typ[Invalid] in expandNamed.
|
// be set to Typ[Invalid] in expandNamed.
|
||||||
inferred := check.infer(ix.Orig, tparams, targs, nil, nil)
|
inferred := check.infer(ix.Orig, tparams, targs, nil, nil)
|
||||||
if len(inferred) > len(targs) {
|
if len(inferred) > len(targs) {
|
||||||
inst.targs = newTypeList(inferred)
|
n.targs = newTypeList(inferred)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// compile
|
// errorcheck
|
||||||
|
|
||||||
// Copyright 2022 The Go Authors. All rights reserved.
|
// Copyright 2022 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
|
@ -13,19 +13,19 @@ type RC[RG any] interface {
|
||||||
type Fn[RCT RC[RG], RG any] func(RCT)
|
type Fn[RCT RC[RG], RG any] func(RCT)
|
||||||
|
|
||||||
type F[RCT RC[RG], RG any] interface {
|
type F[RCT RC[RG], RG any] interface {
|
||||||
Fn() Fn[RCT]
|
Fn() Fn[RCT] // ERROR "got 1 arguments"
|
||||||
}
|
}
|
||||||
|
|
||||||
type concreteF[RCT RC[RG], RG any] struct {
|
type concreteF[RCT RC[RG], RG any] struct {
|
||||||
makeFn func() Fn[RCT]
|
makeFn func() Fn[RCT] // ERROR "got 1 arguments"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
|
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] { // ERROR "got 1 arguments"
|
||||||
return c.makeFn()
|
return c.makeFn()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F[RCT] {
|
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F[RCT] { // ERROR "got 1 arguments"
|
||||||
return &concreteF[RCT]{
|
return &concreteF[RCT]{ // ERROR "cannot use" "got 1 arguments"
|
||||||
makeFn: nil,
|
makeFn: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,28 @@
|
||||||
// compile
|
// errorcheck
|
||||||
|
|
||||||
// Copyright 2022 The Go Authors. All rights reserved.
|
// Copyright 2022 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
package p
|
package p
|
||||||
|
|
||||||
|
// As of issue #51527, type-type inference has been disabled.
|
||||||
|
|
||||||
type RC[RG any] interface {
|
type RC[RG any] interface {
|
||||||
~[]RG
|
~[]RG
|
||||||
}
|
}
|
||||||
|
|
||||||
type Fn[RCT RC[RG], RG any] func(RCT)
|
type Fn[RCT RC[RG], RG any] func(RCT)
|
||||||
type FFn[RCT RC[RG], RG any] func() Fn[RCT]
|
|
||||||
|
type FFn[RCT RC[RG], RG any] func() Fn[RCT] // ERROR "got 1 arguments"
|
||||||
|
|
||||||
type F[RCT RC[RG], RG any] interface {
|
type F[RCT RC[RG], RG any] interface {
|
||||||
Fn() Fn[RCT]
|
Fn() Fn[RCT] // ERROR "got 1 arguments"
|
||||||
}
|
|
||||||
type concreteF[RCT RC[RG], RG any] struct {
|
|
||||||
makeFn FFn[RCT]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
|
type concreteF[RCT RC[RG], RG any] struct {
|
||||||
|
makeFn FFn[RCT] // ERROR "got 1 arguments"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] { // ERROR "got 1 arguments"
|
||||||
return c.makeFn()
|
return c.makeFn()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue