go/types: add Config flag to enable method type parameters

The type-checker type-checks method type parameters like
function type parameters without any extra work. But the
current design draft does not permit them and the go2go
translator does not expect them.

Add a flag to enable them for type checker tests, but do
not accept them by default. Instead, type-check but report
an error.

Passes all.bash.

Change-Id: Iec02f4a951a3faccd2409d97d48bc4dd4536a750
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/764012
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2020-06-05 12:15:57 -07:00
parent d3a2e0e245
commit 690131b027
4 changed files with 20 additions and 2 deletions

View File

@ -107,6 +107,9 @@ type Config struct {
// type-checked.
IgnoreFuncBodies bool
// If AcceptMethodTypeParams is set, methods may have type parameters.
AcceptMethodTypeParams bool
// If FakeImportC is set, `import "C"` (for packages requiring Cgo)
// declares an empty "C" package and errors are omitted for qualified
// identifiers referring to package C (which won't find an object).

View File

@ -49,8 +49,9 @@ func mayTypecheck(t *testing.T, path, source string, info *Info) (string, error)
t.Fatalf("%s: unable to parse: %s", path, err)
}
conf := Config{
Error: func(err error) {},
Importer: importer.Default(),
AcceptMethodTypeParams: true,
Error: func(err error) {},
Importer: importer.Default(),
}
pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, info)
return pkg.Name(), err

View File

@ -281,6 +281,7 @@ func checkFiles(t *testing.T, testfiles []string) {
// typecheck and collect typechecker errors
var conf Config
conf.AcceptMethodTypeParams = true
// special case for importC.src
if len(testfiles) == 1 && testfiles[0] == "testdata/importC.src" {
conf.FakeImportC = true

View File

@ -311,6 +311,12 @@ func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast
if ftyp.TParams != nil {
sig.tparams = check.collectTypeParams(ftyp.TParams)
// Always type-check method type parameters but complain if they are not enabled.
// (A separate check is needed when type-checking interface method signatures because
// they don't have a receiver specification.)
if recvPar != nil && !check.conf.AcceptMethodTypeParams {
check.errorf(ftyp.TParams.Pos(), "methods cannot have type parameters")
}
}
// Value (non-type) parameters' scope starts in the function body. Use a temporary scope for their
@ -749,6 +755,13 @@ func (check *Checker) interfaceType(ityp *Interface, iface *ast.InterfaceType, d
continue // ignore
}
// Always type-check method type parameters but complain if they are not enabled.
// (This extra check is needed here because interface method signatures don't have
// a receiver specification.)
if sig.tparams != nil && !check.conf.AcceptMethodTypeParams {
check.errorf(f.Type.(*ast.FuncType).TParams.Pos(), "methods cannot have type parameters")
}
// use named receiver type if available (for better error messages)
var recvTyp Type = ityp
if def != nil {