Skip to content

Commit 673d52b

Browse files
findleyrdmitshur
authored andcommitted
[release-branch.go1.18] 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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> (cherry picked from commit 28fab5e) Reviewed-on: https://go-review.googlesource.com/c/go/+/390576 Trust: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e3f9a4f commit 673d52b

17 files changed

+106
-79
lines changed

src/cmd/compile/internal/types2/testdata/check/typeinference.go2

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
package typeInference
66

7+
// As of issue #51527, type-type inference has been disabled.
8+
79
// basic inference
810
type Tb[P ~*Q, Q any] int
911
func _() {
10-
var x Tb[*int]
12+
var x Tb /* ERROR got 1 arguments */ [*int]
1113
var y Tb[*int, int]
12-
x = y
14+
x = y /* ERROR cannot use y .* in assignment */
1315
_ = x
1416
}
1517

1618
// recursive inference
1719
type Tr[A any, B *C, C *D, D *A] int
1820
func _() {
19-
var x Tr[string]
21+
var x Tr /* ERROR got 1 arguments */ [string]
2022
var y Tr[string, ***string, **string, *string]
2123
var z Tr[int, ***int, **int, *int]
22-
x = y
24+
x = y /* ERROR cannot use y .* in assignment */
2325
x = z // ERROR cannot use z .* as Tr
2426
_ = x
2527
}
@@ -31,17 +33,17 @@ type To2[A any, B [][]A] int
3133
type To3[A any, B [3]*A] int
3234
type To4[A any, B any, C struct{a A; b B}] int
3335
func _() {
34-
var _ To0[int]
35-
var _ To1[int]
36-
var _ To2[int]
37-
var _ To3[int]
38-
var _ To4[int, string]
36+
var _ To0 /* ERROR got 1 arguments */ [int]
37+
var _ To1 /* ERROR got 1 arguments */ [int]
38+
var _ To2 /* ERROR got 1 arguments */ [int]
39+
var _ To3 /* ERROR got 1 arguments */ [int]
40+
var _ To4 /* ERROR got 2 arguments */ [int, string]
3941
}
4042

4143
// failed inference
4244
type Tf0[A, B any] int
4345
type Tf1[A any, B ~struct{a A; c C}, C any] int
4446
func _() {
45-
var _ Tf0 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
46-
var _ Tf1 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 3 type parameters */ [int]
47+
var _ Tf0 /* ERROR got 1 arguments but 2 type parameters */ [int]
48+
var _ Tf1 /* ERROR got 1 arguments but 3 type parameters */ [int]
4749
}

src/cmd/compile/internal/types2/testdata/fixedbugs/issue49541.go2

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ type S[A, B any] struct {
1010

1111
func (S[A, B]) m() {}
1212

13-
// TODO(gri) We should only report one error below. See issue #50588.
13+
// TODO(gri): with type-type inference enabled we should only report one error
14+
// below. See issue #50588.
1415

15-
func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [A]) {
16+
func _[A any](s S /* ERROR got 1 arguments but 2 type parameters */ [A]) {
1617
// we should see no follow-on errors below
1718
s.f = 1
1819
s.m()
@@ -21,7 +22,7 @@ func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type
2122
// another test case from the issue
2223

2324
func _() {
24-
X(Interface[*F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
25+
X(Interface[*F /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
2526
}
2627

2728
func X[Q Qer](fs Interface[Q]) {

src/cmd/compile/internal/types2/testdata/fixedbugs/issue50929.go2

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func G[A, B any](F[A, B]) {
1616

1717
func _() {
1818
// TODO(gri) only report one error below (issue #50932)
19-
var x F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
19+
var x F /* ERROR got 1 arguments but 2 type parameters */ [int]
2020
G(x /* ERROR does not match */)
2121
}
2222

@@ -46,9 +46,9 @@ func NSG[G any](c RSC[G]) {
4646
fmt.Println(c)
4747
}
4848

49-
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] {
49+
func MMD[Rc RC /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got 2 arguments */ [Rc, RG] {
5050

51-
var nFn NFn /* ERROR got 2 arguments */ /* ERROR Rc does not match */ [Rc, RG]
51+
var nFn NFn /* ERROR got 2 arguments */ [Rc, RG]
5252

5353
var empty Rc
5454
switch any(empty).(type) {

src/cmd/compile/internal/types2/testdata/fixedbugs/issue51232.go2

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ type RC[RG any] interface {
1111
type Fn[RCT RC[RG], RG any] func(RCT)
1212

1313
type F[RCT RC[RG], RG any] interface {
14-
Fn() Fn[RCT]
14+
Fn() Fn /* ERROR got 1 arguments */ [RCT]
1515
}
1616

1717
type concreteF[RCT RC[RG], RG any] struct {
18-
makeFn func() Fn[RCT]
18+
makeFn func() Fn /* ERROR got 1 arguments */ [RCT]
1919
}
2020

21-
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
21+
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
2222
return c.makeFn()
2323
}
2424

25-
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F[RCT] {
26-
return &concreteF[RCT]{
25+
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F /* ERROR got 1 arguments */ [RCT] {
26+
// TODO(rfindley): eliminate the duplicate error below.
27+
return & /* ERROR cannot use .* as F\[RCT\] */ concreteF /* ERROR got 1 arguments */ [RCT]{
2728
makeFn: nil,
2829
}
2930
}

src/cmd/compile/internal/types2/testdata/fixedbugs/issue51233.go2

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
package p
66

7+
// As of issue #51527, type-type inference has been disabled.
8+
79
type RC[RG any] interface {
810
~[]RG
911
}
1012

1113
type Fn[RCT RC[RG], RG any] func(RCT)
1214

13-
type FFn[RCT RC[RG], RG any] func() Fn[RCT]
15+
type FFn[RCT RC[RG], RG any] func() Fn /* ERROR got 1 arguments */ [RCT]
1416

1517
type F[RCT RC[RG], RG any] interface {
16-
Fn() Fn[RCT]
18+
Fn() Fn /* ERROR got 1 arguments */ [RCT]
1719
}
1820

1921
type concreteF[RCT RC[RG], RG any] struct {
20-
makeFn FFn[RCT]
22+
makeFn FFn /* ERROR got 1 arguments */ [RCT]
2123
}
2224

23-
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
25+
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
2426
return c.makeFn()
2527
}

src/cmd/compile/internal/types2/testdata/fixedbugs/issue51339.go2

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ package p
1010
type T[P any, B *P] struct{}
1111

1212
func (T /* ERROR cannot use generic type */ ) m0() {}
13-
func (T /* ERROR got 1 type parameter, but receiver base type declares 2 */ [_]) m1() {}
13+
14+
// TODO(rfindley): eliminate the duplicate errors here.
15+
func (T /* ERROR got 1 type parameter, but receiver base type declares 2 */ /* ERROR got 1 arguments but 2 type parameters */ [_]) m1() {}
1416
func (T[_, _]) m2() {}
1517
// TODO(gri) this error is unfortunate (issue #51343)
1618
func (T /* ERROR got 3 arguments but 2 type parameters */ [_, _, _]) m3() {}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,14 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
430430
// evaluate arguments
431431
targs := check.typeList(xlist)
432432
if targs == nil {
433-
def.setUnderlying(Typ[Invalid]) // avoid later errors due to lazy instantiation
433+
def.setUnderlying(Typ[Invalid]) // avoid errors later due to lazy instantiation
434434
return Typ[Invalid]
435435
}
436436

437+
// enableTypeTypeInference controls whether to infer missing type arguments
438+
// using constraint type inference. See issue #51527.
439+
const enableTypeTypeInference = false
440+
437441
// create the instance
438442
ctxt := check.bestContext(nil)
439443
h := ctxt.instanceHash(orig, targs)
@@ -453,14 +457,15 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
453457
def.setUnderlying(inst)
454458

455459
inst.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
456-
tparams := orig.TypeParams().list()
460+
tparams := n.orig.TypeParams().list()
457461

458-
if len(targs) < len(tparams) {
462+
targs := n.targs.list()
463+
if enableTypeTypeInference && len(targs) < len(tparams) {
459464
// If inference fails, len(inferred) will be 0, and inst.underlying will
460465
// be set to Typ[Invalid] in expandNamed.
461466
inferred := check.infer(x.Pos(), tparams, targs, nil, nil)
462467
if len(inferred) > len(targs) {
463-
inst.targs = newTypeList(inferred)
468+
n.targs = newTypeList(inferred)
464469
}
465470
}
466471

@@ -473,10 +478,10 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
473478
// and so it must be resolved during type-checking so that we can report
474479
// errors.
475480
inst.resolve(ctxt)
476-
check.recordInstance(x, inst.TypeArgs().list(), inst)
477481
// Since check is non-nil, we can still mutate inst. Unpinning the resolver
478482
// frees some memory.
479483
inst.resolver = nil
484+
check.recordInstance(x, inst.TypeArgs().list(), inst)
480485

481486
if check.validateTArgLen(x.Pos(), inst.tparams.Len(), inst.targs.Len()) {
482487
if i, err := check.verify(x.Pos(), inst.tparams.list(), inst.targs.list()); err != nil {

src/go/types/errorcodes.go

-5
Original file line numberDiff line numberDiff line change
@@ -1339,11 +1339,6 @@ const (
13391339
// func _() {
13401340
// f()
13411341
// }
1342-
//
1343-
// Example:
1344-
// type N[P, Q any] struct{}
1345-
//
1346-
// var _ N[int]
13471342
_CannotInferTypeArgs
13481343

13491344
// _InvalidTypeArg occurs when a type argument does not satisfy its

src/go/types/testdata/check/typeinference.go2

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
package typeInference
66

7+
// As of issue #51527, type-type inference has been disabled.
8+
79
// basic inference
810
type Tb[P ~*Q, Q any] int
911
func _() {
10-
var x Tb[*int]
12+
var x Tb /* ERROR got 1 arguments */ [*int]
1113
var y Tb[*int, int]
12-
x = y
14+
x = y /* ERROR cannot use y .* in assignment */
1315
_ = x
1416
}
1517

1618
// recursive inference
1719
type Tr[A any, B *C, C *D, D *A] int
1820
func _() {
19-
var x Tr[string]
21+
var x Tr /* ERROR got 1 arguments */ [string]
2022
var y Tr[string, ***string, **string, *string]
2123
var z Tr[int, ***int, **int, *int]
22-
x = y
24+
x = y /* ERROR cannot use y .* in assignment */
2325
x = z // ERROR cannot use z .* as Tr
2426
_ = x
2527
}
@@ -31,17 +33,17 @@ type To2[A any, B [][]A] int
3133
type To3[A any, B [3]*A] int
3234
type To4[A any, B any, C struct{a A; b B}] int
3335
func _() {
34-
var _ To0[int]
35-
var _ To1[int]
36-
var _ To2[int]
37-
var _ To3[int]
38-
var _ To4[int, string]
36+
var _ To0 /* ERROR got 1 arguments */ [int]
37+
var _ To1 /* ERROR got 1 arguments */ [int]
38+
var _ To2 /* ERROR got 1 arguments */ [int]
39+
var _ To3 /* ERROR got 1 arguments */ [int]
40+
var _ To4 /* ERROR got 2 arguments */ [int, string]
3941
}
4042

4143
// failed inference
4244
type Tf0[A, B any] int
4345
type Tf1[A any, B ~struct{a A; c C}, C any] int
4446
func _() {
45-
var _ Tf0 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
46-
var _ Tf1 /* ERROR cannot infer B */ /* ERROR got 1 arguments but 3 type parameters */ [int]
47+
var _ Tf0 /* ERROR got 1 arguments but 2 type parameters */ [int]
48+
var _ Tf1 /* ERROR got 1 arguments but 3 type parameters */ [int]
4749
}

src/go/types/testdata/fixedbugs/issue49541.go2

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ type S[A, B any] struct {
1010

1111
func (S[A, B]) m() {}
1212

13-
// TODO(gri) We should only report one error below. See issue #50588.
13+
// TODO(gri): with type-type inference enabled we should only report one error
14+
// below. See issue #50588.
1415

15-
func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [A]) {
16+
func _[A any](s S /* ERROR got 1 arguments but 2 type parameters */ [A]) {
1617
// we should see no follow-on errors below
1718
s.f = 1
1819
s.m()
@@ -21,7 +22,7 @@ func _[A any](s S /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type
2122
// another test case from the issue
2223

2324
func _() {
24-
X(Interface[*F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
25+
X(Interface[*F /* ERROR got 1 arguments but 2 type parameters */ [string]](Impl{}))
2526
}
2627

2728
func X[Q Qer](fs Interface[Q]) {

src/go/types/testdata/fixedbugs/issue50929.go2

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func G[A, B any](F[A, B]) {
1616

1717
func _() {
1818
// TODO(gri) only report one error below (issue #50932)
19-
var x F /* ERROR cannot infer B */ /* ERROR got 1 arguments but 2 type parameters */ [int]
19+
var x F /* ERROR got 1 arguments but 2 type parameters */ [int]
2020
G(x /* ERROR does not match */)
2121
}
2222

@@ -46,9 +46,9 @@ func NSG[G any](c RSC[G]) {
4646
fmt.Println(c)
4747
}
4848

49-
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] {
49+
func MMD[Rc RC /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got 2 arguments */ [Rc, RG] {
5050

51-
var nFn NFn /* ERROR got 2 arguments */ /* ERROR Rc does not match */ [Rc, RG]
51+
var nFn NFn /* ERROR got 2 arguments */ [Rc, RG]
5252

5353
var empty Rc
5454
switch any(empty).(type) {

src/go/types/testdata/fixedbugs/issue51232.go2

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ type RC[RG any] interface {
1111
type Fn[RCT RC[RG], RG any] func(RCT)
1212

1313
type F[RCT RC[RG], RG any] interface {
14-
Fn() Fn[RCT]
14+
Fn() Fn /* ERROR got 1 arguments */ [RCT]
1515
}
1616

1717
type concreteF[RCT RC[RG], RG any] struct {
18-
makeFn func() Fn[RCT]
18+
makeFn func() Fn /* ERROR got 1 arguments */ [RCT]
1919
}
2020

21-
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
21+
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
2222
return c.makeFn()
2323
}
2424

25-
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F[RCT] {
26-
return &concreteF[RCT]{
25+
func NewConcrete[RCT RC[RG], RG any](Rc RCT) F /* ERROR got 1 arguments */ [RCT] {
26+
// TODO(rfindley): eliminate the duplicate error below.
27+
return & /* ERROR cannot use .* as F\[RCT\] */ concreteF /* ERROR got 1 arguments */ [RCT]{
2728
makeFn: nil,
2829
}
2930
}

src/go/types/testdata/fixedbugs/issue51233.go2

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
package p
66

7+
// As of issue #51527, type-type inference has been disabled.
8+
79
type RC[RG any] interface {
810
~[]RG
911
}
1012

1113
type Fn[RCT RC[RG], RG any] func(RCT)
1214

13-
type FFn[RCT RC[RG], RG any] func() Fn[RCT]
15+
type FFn[RCT RC[RG], RG any] func() Fn /* ERROR got 1 arguments */ [RCT]
1416

1517
type F[RCT RC[RG], RG any] interface {
16-
Fn() Fn[RCT]
18+
Fn() Fn /* ERROR got 1 arguments */ [RCT]
1719
}
1820

1921
type concreteF[RCT RC[RG], RG any] struct {
20-
makeFn FFn[RCT]
22+
makeFn FFn /* ERROR got 1 arguments */ [RCT]
2123
}
2224

23-
func (c *concreteF[RCT, RG]) Fn() Fn[RCT] {
25+
func (c *concreteF[RCT, RG]) Fn() Fn /* ERROR got 1 arguments */ [RCT] {
2426
return c.makeFn()
2527
}

src/go/types/testdata/fixedbugs/issue51339.go2

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ package p
1010
type T[P any, B *P] struct{}
1111

1212
func (T /* ERROR cannot use generic type */ ) m0() {}
13-
func (/* ERROR got 1 type parameter, but receiver base type declares 2 */ T[_]) m1() {}
13+
14+
// TODO(rfindley): eliminate the duplicate errors here.
15+
func (/* ERROR got 1 type parameter, but receiver base type declares 2 */ T /* ERROR got 1 arguments but 2 type parameters */ [_]) m1() {}
1416
func (T[_, _]) m2() {}
1517
// TODO(gri) this error is unfortunate (issue #51343)
1618
func (T /* ERROR got 3 arguments but 2 type parameters */ [_, _, _]) m3() {}

0 commit comments

Comments
 (0)