Skip to content

Commit 86fa510

Browse files
qmuntalgriesemer
authored andcommitted
go/types, types2: types in method expressions must be instantiated
Use varType instead of instantiatedOperand to check if the type of a method expressions is instantiated. This removes the last usage of instantiatedOperand, so it can be deleted. Fixes #48048 Change-Id: I2b219dafe2bba3603100bb8f25b8ff4e8ef53841 Reviewed-on: https://go-review.googlesource.com/c/go/+/345970 Trust: Robert Griesemer <[email protected]> Trust: Matthew Dempsky <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 8f4c020 commit 86fa510

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,9 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
479479
goto Error
480480
}
481481

482-
check.instantiatedOperand(x)
482+
if x.mode == typexpr {
483+
x.typ = check.varType(e.X)
484+
}
483485

484486
obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
485487
if obj == nil {
@@ -718,11 +720,3 @@ func (check *Checker) useLHS(arg ...syntax.Expr) {
718720
}
719721
}
720722
}
721-
722-
// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
723-
func (check *Checker) instantiatedOperand(x *operand) {
724-
if x.mode == typexpr && isGeneric(x.typ) {
725-
check.errorf(x, "cannot use generic type %s without instantiation", x.typ)
726-
x.typ = Typ[Invalid]
727-
}
728-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 T[P any] struct{}
8+
9+
func (T[_]) A() {}
10+
11+
var _ = (T[int]).A
12+
var _ = (*T[int]).A
13+
14+
var _ = (T /* ERROR cannot use generic type */).A
15+
var _ = (*T /* ERROR cannot use generic type */).A

src/go/types/call.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
470470
goto Error
471471
}
472472

473-
check.instantiatedOperand(x)
473+
if x.mode == typexpr {
474+
x.typ = check.varType(e.X)
475+
}
474476

475477
obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
476478
if obj == nil {
@@ -745,11 +747,3 @@ func (check *Checker) useLHS(arg ...ast.Expr) {
745747
}
746748
}
747749
}
748-
749-
// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
750-
func (check *Checker) instantiatedOperand(x *operand) {
751-
if x.mode == typexpr && isGeneric(x.typ) {
752-
check.errorf(x, _Todo, "cannot use generic type %s without instantiation", x.typ)
753-
x.typ = Typ[Invalid]
754-
}
755-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 T[P any] struct{}
8+
9+
func (T[_]) A() {}
10+
11+
var _ = (T[int]).A
12+
var _ = (*T[int]).A
13+
14+
var _ = (T /* ERROR cannot use generic type */).A
15+
var _ = (*T /* ERROR cannot use generic type */).A

0 commit comments

Comments
 (0)