1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:40:03 -07:00

cmd/compile/internal/types2: remove Config.AcceptMethodTypeParams flag

Type parameters for methods are not part of the accepted language,
but maintaining the code for type-checking them ensures regularity
of the type checker implementation. For now, keep the flag internally,
disabled by default. The flag is set when running tests.

Change-Id: Ic99934bd00bd2608dc1178e4131f46dd1507f0f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/307214
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-04-05 14:30:03 -07:00
parent 7d5c54eee4
commit 1395432f23
7 changed files with 17 additions and 15 deletions

View File

@ -107,9 +107,6 @@ 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

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

View File

@ -31,11 +31,6 @@ const debug = false // leave on during development
//
const forceStrict = false
// If methodTypeParamsOk is set, type parameters are
// permitted in method declarations (in interfaces, too).
// Generalization and experimental feature.
const methodTypeParamsOk = true
// exprInfo stores information about an untyped expression.
type exprInfo struct {
isLhs bool // expression is lhs operand of a shift with delayed type-check

View File

@ -128,7 +128,6 @@ func checkFiles(t *testing.T, filenames []string, goVersion string, colDelta uin
// typecheck and collect typechecker errors
var conf Config
conf.GoVersion = goVersion
conf.AcceptMethodTypeParams = true
// special case for importC.src
if len(filenames) == 1 && strings.HasSuffix(filenames[0], "importC.src") {
conf.FakeImportC = true

View File

@ -426,7 +426,7 @@ func (check *Checker) collectObjects() {
} else {
// method
// d.Recv != nil
if !methodTypeParamsOk && len(d.TParamList) != 0 {
if !acceptMethodTypeParams && len(d.TParamList) != 0 {
//check.error(d.TParamList.Pos(), invalidAST + "method must have no type parameters")
check.error(d, invalidAST+"method must have no type parameters")
}

View File

@ -0,0 +1,9 @@
// 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 types2
func init() {
acceptMethodTypeParams = true
}

View File

@ -15,6 +15,9 @@ import (
"strings"
)
// Disabled by default, but enabled when running tests (via types_test.go).
var acceptMethodTypeParams bool
// ident type-checks identifier e and initializes x with the value or type of e.
// If an error occurred, x.mode is set to invalid.
// For the meaning of def, see Checker.definedType, below.
@ -336,7 +339,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, 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 {
if recvPar != nil && !acceptMethodTypeParams {
check.error(ftyp, "methods cannot have type parameters")
}
}
@ -848,7 +851,7 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
// 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 {
if sig.tparams != nil && !acceptMethodTypeParams {
check.error(f.Type, "methods cannot have type parameters")
}