Skip to content

Commit

Permalink
go/types, types2: remove Config.EnableReverseTypeInference flag
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
griesemer authored and gopherbot committed May 4, 2023
1 parent 13201d5 commit fc106b0
Show file tree
Hide file tree
Showing 22 changed files with 33 additions and 54 deletions.
5 changes: 2 additions & 3 deletions src/cmd/compile/internal/noder/irgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions src/cmd/compile/internal/types2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions src/cmd/compile/internal/types2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/cmd/compile/internal/types2/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/types2/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/cmd/compile/internal/types2/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions src/cmd/compile/internal/types2/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions src/go/types/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions src/go/types/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/go/types/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/go/types/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/go/types/infer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/go/types/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion src/internal/types/testdata/examples/inference.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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"
2 changes: 0 additions & 2 deletions src/internal/types/testdata/examples/inference2.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/internal/types/testdata/fixedbugs/issue59338a.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/internal/types/testdata/fixedbugs/issue59338b.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/internal/types/testdata/fixedbugs/issue59639.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/internal/types/testdata/fixedbugs/issue59953.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/internal/types/testdata/fixedbugs/issue59956.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit fc106b0

Please sign in to comment.