1
0
mirror of https://github.com/golang/go synced 2024-11-26 08:17:59 -07:00

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 <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Quim Muntal 2021-08-29 22:57:36 +02:00 committed by Robert Griesemer
parent 8f4c020660
commit 86fa510d24
4 changed files with 36 additions and 18 deletions

View File

@ -479,7 +479,9 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
goto Error
}
check.instantiatedOperand(x)
if x.mode == typexpr {
x.typ = check.varType(e.X)
}
obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
if obj == nil {
@ -718,11 +720,3 @@ func (check *Checker) useLHS(arg ...syntax.Expr) {
}
}
}
// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
func (check *Checker) instantiatedOperand(x *operand) {
if x.mode == typexpr && isGeneric(x.typ) {
check.errorf(x, "cannot use generic type %s without instantiation", x.typ)
x.typ = Typ[Invalid]
}
}

View File

@ -0,0 +1,15 @@
// Copyright 2021 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 p
type T[P any] struct{}
func (T[_]) A() {}
var _ = (T[int]).A
var _ = (*T[int]).A
var _ = (T /* ERROR cannot use generic type */).A
var _ = (*T /* ERROR cannot use generic type */).A

View File

@ -470,7 +470,9 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
goto Error
}
check.instantiatedOperand(x)
if x.mode == typexpr {
x.typ = check.varType(e.X)
}
obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
if obj == nil {
@ -745,11 +747,3 @@ func (check *Checker) useLHS(arg ...ast.Expr) {
}
}
}
// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
func (check *Checker) instantiatedOperand(x *operand) {
if x.mode == typexpr && isGeneric(x.typ) {
check.errorf(x, _Todo, "cannot use generic type %s without instantiation", x.typ)
x.typ = Typ[Invalid]
}
}

View File

@ -0,0 +1,15 @@
// Copyright 2021 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 p
type T[P any] struct{}
func (T[_]) A() {}
var _ = (T[int]).A
var _ = (*T[int]).A
var _ = (T /* ERROR cannot use generic type */).A
var _ = (*T /* ERROR cannot use generic type */).A