Skip to content

Commit 4461513

Browse files
committed
[dev.go2go] go/types: add missing instantiation checks for function type arguments
Fixes #40684. Change-Id: If642edbf321973729a8d5b36b4bf61e3fae7ee3a Reviewed-on: https://go-review.googlesource.com/c/go/+/251557 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 93e810a commit 4461513

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

src/go/types/call.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ func (check *Checker) exprOrTypeList(elist []ast.Expr) (xlist []*operand, ok boo
204204
break
205205
}
206206

207+
check.instantiatedOperand(&x)
208+
207209
// exactly one (possibly invalid or comma-ok) value or type
208210
xlist = []*operand{&x}
209211

@@ -220,6 +222,7 @@ func (check *Checker) exprOrTypeList(elist []ast.Expr) (xlist []*operand, ok boo
220222
ntypes = len(xlist) // make 'if' condition fail below (no additional error in this case)
221223
case typexpr:
222224
ntypes++
225+
check.instantiatedOperand(&x)
223226
}
224227
}
225228
if 0 < ntypes && ntypes < len(xlist) {
@@ -521,10 +524,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
521524
goto Error
522525
}
523526

524-
if x.mode == typexpr && isGeneric(x.typ) {
525-
check.errorf(e.Pos(), "cannot use generic type %s without instantiation", x.typ)
526-
goto Error
527-
}
527+
check.instantiatedOperand(x)
528528

529529
obj, index, indirect = check.lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
530530
if obj == nil {
@@ -774,3 +774,11 @@ func (check *Checker) useLHS(arg ...ast.Expr) {
774774
}
775775
}
776776
}
777+
778+
// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
779+
func (check *Checker) instantiatedOperand(x *operand) {
780+
if x.mode == typexpr && isGeneric(x.typ) {
781+
check.errorf(x.pos(), "cannot use generic type %s without instantiation", x.typ)
782+
x.typ = Typ[Invalid]
783+
}
784+
}

src/go/types/expr.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,9 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
13401340
x.mode = typexpr
13411341
}
13421342
return expression
1343-
} else {
1344-
check.errorf(x.pos(), "%s is not a generic type", x.typ)
1345-
goto Error
13461343
}
1344+
check.errorf(x.pos(), "%s is not a generic type", x.typ)
1345+
goto Error
13471346
}
13481347

13491348
if sig := x.typ.Signature(); sig != nil {

src/go/types/fixedbugs/issue40038.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func (A[T]) m(A[T])
1111
func f[P interface{m(P)}]()
1212

1313
func _() {
14-
_ = f[A]
14+
_ = f[A[int]]
1515
}

src/go/types/fixedbugs/issue40684.go2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
type T[_ any] int
8+
9+
func f[_ any]()
10+
func g[_, _ any]()
11+
12+
func _() {
13+
_ = f[T /* ERROR without instantiation */ ]
14+
_ = g[T /* ERROR without instantiation */ , T /* ERROR without instantiation */ ]
15+
}

src/go/types/typexpr.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,7 @@ func (check *Checker) typeOrNil(e ast.Expr) Type {
582582
case novalue:
583583
check.errorf(x.pos(), "%s used as type", &x)
584584
case typexpr:
585-
if isGeneric(x.typ) {
586-
check.errorf(e.Pos(), "cannot use generic type %s without instantiation", x.typ)
587-
return Typ[Invalid]
588-
}
585+
check.instantiatedOperand(&x)
589586
return x.typ
590587
case value:
591588
if x.isNil() {

0 commit comments

Comments
 (0)