go/types, types2: remove Config.EnableReverseTypeInference flag

Proposal #59338 has been accepted and we expect this feature to
be available starting with Go 1.21. Remove the flag to explicitly
enable it through the API and enable by default.

For now keep an internal constant enableReverseTypeInference to
guard and mark the respective code, so we can disable it for
debugging purposes.

For #59338.

Change-Id: Ia1bf3032483ae603017a0f459417ec73837e2891
Reviewed-on: https://go-review.googlesource.com/c/go/+/491798
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2023-05-03 22:05:27 -07:00 committed by Gopher Robot
parent 13201d5728
commit fc106b016c
22 changed files with 33 additions and 54 deletions

View File

@ -50,9 +50,8 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
}
base.ErrorfAt(m.makeXPos(terr.Pos), terr.Code, "%s", msg)
},
Importer: &importer,
Sizes: &gcSizes{},
EnableReverseTypeInference: true,
Importer: &importer,
Sizes: &gcSizes{},
}
info := &types2.Info{
StoreTypesInSyntax: true,

View File

@ -169,13 +169,6 @@ type Config struct {
// If DisableUnusedImportCheck is set, packages are not checked
// for unused imports.
DisableUnusedImportCheck bool
// If EnableReverseTypeInference is set, uninstantiated and
// partially instantiated generic functions may be assigned
// (incl. returned) to variables of function type and type
// inference will attempt to infer the missing type arguments.
// See proposal go.dev/issue/59338.
EnableReverseTypeInference bool
}
func srcimporter_setUsesCgo(conf *Config) {

View File

@ -594,10 +594,7 @@ type T[P any] []P
for _, test := range tests {
imports := make(testImporter)
conf := Config{
Importer: imports,
EnableReverseTypeInference: true,
}
conf := Config{Importer: imports}
instMap := make(map[*syntax.Name]Instance)
useMap := make(map[*syntax.Name]Object)
makePkg := func(src string) *Package {

View File

@ -496,7 +496,7 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
// collect type parameters from generic function arguments
var genericArgs []int // indices of generic function arguments
if check.conf.EnableReverseTypeInference {
if enableReverseTypeInference {
for i, arg := range args {
// generic arguments cannot have a defined (*Named) type - no need for underlying type below
if asig, _ := arg.typ.(*Signature); asig != nil && asig.TypeParams().Len() > 0 {

View File

@ -133,7 +133,6 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
flags := flag.NewFlagSet("", flag.PanicOnError)
flags.StringVar(&conf.GoVersion, "lang", "", "")
flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
flags.BoolVar(&conf.EnableReverseTypeInference, "reverseTypeInference", false, "")
if err := parseFlags(filenames[0], nil, flags); err != nil {
t.Fatal(err)
}

View File

@ -1291,7 +1291,7 @@ func (check *Checker) nonGeneric(T Type, x *operand) {
}
case *Signature:
if t.tparams != nil {
if check.conf.EnableReverseTypeInference && T != nil {
if enableReverseTypeInference && T != nil {
if tsig, _ := under(T).(*Signature); tsig != nil {
check.funcInst(tsig, x.Pos(), x, nil)
return
@ -1617,7 +1617,7 @@ func (check *Checker) exprInternal(T Type, x *operand, e syntax.Expr, hint Type)
case *syntax.IndexExpr:
if check.indexExpr(x, e) {
var tsig *Signature
if check.conf.EnableReverseTypeInference && T != nil {
if enableReverseTypeInference && T != nil {
tsig, _ = under(T).(*Signature)
}
check.funcInst(tsig, e.Pos(), x, e)

View File

@ -13,6 +13,13 @@ import (
"strings"
)
// If enableReverseTypeInference is set, uninstantiated and
// partially instantiated generic functions may be assigned
// (incl. returned) to variables of function type and type
// inference will attempt to infer the missing type arguments.
// Available with go1.21.
const enableReverseTypeInference = true // disable for debugging
// infer attempts to infer the complete set of type arguments for generic function instantiation/call
// based on the given type parameters tparams, type arguments targs, function parameters params, and
// function arguments args, if any. There must be at least one type parameter, no more type arguments

View File

@ -139,9 +139,8 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
file, err := syntax.ParseFile(filename, nil, nil, 0)
if err == nil {
conf := Config{
GoVersion: goVersion,
Importer: stdLibImporter,
EnableReverseTypeInference: true,
GoVersion: goVersion,
Importer: stdLibImporter,
}
_, err = conf.Check(filename, []*syntax.File{file}, nil)
}
@ -254,9 +253,8 @@ func typecheckFiles(t *testing.T, path string, filenames []string) {
// typecheck package files
conf := Config{
Error: func(err error) { t.Error(err) },
Importer: stdLibImporter,
EnableReverseTypeInference: true,
Error: func(err error) { t.Error(err) },
Importer: stdLibImporter,
}
info := Info{Uses: make(map[*syntax.Name]Object)}
conf.Check(path, files, &info)

View File

@ -170,13 +170,6 @@ type Config struct {
// If DisableUnusedImportCheck is set, packages are not checked
// for unused imports.
DisableUnusedImportCheck bool
// If _EnableReverseTypeInference is set, uninstantiated and
// partially instantiated generic functions may be assigned
// (incl. returned) to variables of function type and type
// inference will attempt to infer the missing type arguments.
// See proposal go.dev/issue/59338.
_EnableReverseTypeInference bool
}
func srcimporter_setUsesCgo(conf *Config) {

View File

@ -594,12 +594,7 @@ type T[P any] []P
for _, test := range tests {
imports := make(testImporter)
conf := Config{
Importer: imports,
// Unexported field: set below with boolFieldAddr
// _EnableReverseTypeInference: true,
}
*boolFieldAddr(&conf, "_EnableReverseTypeInference") = true
conf := Config{Importer: imports}
instMap := make(map[*ast.Ident]Instance)
useMap := make(map[*ast.Ident]Object)
makePkg := func(src string) *Package {

View File

@ -499,7 +499,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
// collect type parameters from generic function arguments
var genericArgs []int // indices of generic function arguments
if check.conf._EnableReverseTypeInference {
if enableReverseTypeInference {
for i, arg := range args {
// generic arguments cannot have a defined (*Named) type - no need for underlying type below
if asig, _ := arg.typ.(*Signature); asig != nil && asig.TypeParams().Len() > 0 {

View File

@ -146,7 +146,6 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
flags := flag.NewFlagSet("", flag.PanicOnError)
flags.StringVar(&conf.GoVersion, "lang", "", "")
flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
flags.BoolVar(boolFieldAddr(&conf, "_EnableReverseTypeInference"), "reverseTypeInference", false, "")
if err := parseFlags(filenames[0], srcs[0], flags); err != nil {
t.Fatal(err)
}

View File

@ -1276,7 +1276,7 @@ func (check *Checker) nonGeneric(T Type, x *operand) {
}
case *Signature:
if t.tparams != nil {
if check.conf._EnableReverseTypeInference && T != nil {
if enableReverseTypeInference && T != nil {
if tsig, _ := under(T).(*Signature); tsig != nil {
check.funcInst(tsig, x.Pos(), x, nil)
return
@ -1600,7 +1600,7 @@ func (check *Checker) exprInternal(T Type, x *operand, e ast.Expr, hint Type) ex
ix := typeparams.UnpackIndexExpr(e)
if check.indexExpr(x, ix) {
var tsig *Signature
if check.conf._EnableReverseTypeInference && T != nil {
if enableReverseTypeInference && T != nil {
tsig, _ = under(T).(*Signature)
}
check.funcInst(tsig, e.Pos(), x, ix)

View File

@ -15,6 +15,13 @@ import (
"strings"
)
// If enableReverseTypeInference is set, uninstantiated and
// partially instantiated generic functions may be assigned
// (incl. returned) to variables of function type and type
// inference will attempt to infer the missing type arguments.
// Available with go1.21.
const enableReverseTypeInference = true // disable for debugging
// infer attempts to infer the complete set of type arguments for generic function instantiation/call
// based on the given type parameters tparams, type arguments targs, function parameters params, and
// function arguments args, if any. There must be at least one type parameter, no more type arguments

View File

@ -143,7 +143,6 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
GoVersion: goVersion,
Importer: stdLibImporter,
}
*boolFieldAddr(&conf, "_EnableReverseTypeInference") = true
_, err = conf.Check(filename, fset, []*ast.File{file}, nil)
}
@ -271,7 +270,6 @@ func typecheckFiles(t *testing.T, path string, filenames []string) {
},
Importer: stdLibImporter,
}
*boolFieldAddr(&conf, "_EnableReverseTypeInference") = true
info := Info{Uses: make(map[*ast.Ident]Object)}
conf.Check(path, fset, files, &info)

View File

@ -1,3 +1,5 @@
// -lang=go1.20
// 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.
@ -154,4 +156,4 @@ func _() {
func f[P any](P) {}
// This must not crash.
var _ func(int) = f // ERROR "cannot use generic function f without instantiation"
var _ func(int) = f // ERROR "implicitly instantiated function in assignment requires go1.21 or later"

View File

@ -1,5 +1,3 @@
// -reverseTypeInference
// Copyright 2023 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.

View File

@ -1,4 +1,4 @@
// -reverseTypeInference -lang=go1.20
// -lang=go1.20
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

View File

@ -1,5 +1,3 @@
// -reverseTypeInference
// Copyright 2023 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.

View File

@ -1,4 +1,4 @@
// -reverseTypeInference -lang=go1.17
// -lang=go1.17
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

View File

@ -1,5 +1,3 @@
// -reverseTypeInference
// Copyright 2023 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.

View File

@ -1,5 +1,3 @@
// -reverseTypeInference
// Copyright 2023 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.