1
0
mirror of https://github.com/golang/go synced 2024-11-16 20:44:52 -07:00

cmd/compile: use more lenient type inference for untyped arguments for go1.21

This CL permanently enables the new behavior for -lang=go1.21 and
newer, and keeps the existing behavior if -lang=go1.20 or older.

To be submitted once #58671 is accepted.

For #58671.

Change-Id: I83a1d393f0ce7871be8f38ec35742d393946c55f
Reviewed-on: https://go-review.googlesource.com/c/go/+/496035
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2023-05-17 14:48:51 -07:00 committed by Gopher Robot
parent ad7d1d995f
commit 4088e97fc2
12 changed files with 33 additions and 27 deletions

View File

@ -52,7 +52,6 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
},
Importer: &importer,
Sizes: &gcSizes{},
InferMaxDefaultType: true, // #58671
}
info := &types2.Info{
StoreTypesInSyntax: true,

View File

@ -169,11 +169,6 @@ type Config struct {
// If DisableUnusedImportCheck is set, packages are not checked
// for unused imports.
DisableUnusedImportCheck bool
// If InferMaxDefaultType is set, the minimum (smallest) default
// type that fits all untyped constant arguments for the same type
// parameter is selected in type inference. (go.dev/issue/58671)
InferMaxDefaultType bool
}
func srcimporter_setUsesCgo(conf *Config) {

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.InferMaxDefaultType, "inferMaxDefaultType", false, "")
if err := parseFlags(filenames[0], nil, flags); err != nil {
t.Fatal(err)
}

View File

@ -273,7 +273,19 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
u.tracef("== untyped arguments: %v", untyped)
}
if check.conf.InferMaxDefaultType {
// We need a poser/positioner for check.allowVersion below.
// We should really use pos (argument to infer) but currently
// the generator that generates go/types/infer.go has trouble
// with that. For now, do a little dance to get a position if
// we need one. (If we don't have untyped arguments left, it
// doesn't matter which branch we take below.)
// TODO(gri) adjust infer signature or adjust the rewriter.
var at syntax.Pos
if len(untyped) > 0 {
at = params.At(untyped[0]).pos
}
if check.allowVersion(check.pkg, atPos(at), go1_21) {
// Some generic parameters with untyped arguments may have been given a type by now.
// Collect all remaining parameters that don't have a type yet and determine the
// maximum untyped type for each of those parameters, if possible.

View File

@ -141,7 +141,6 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
conf := Config{
GoVersion: goVersion,
Importer: stdLibImporter,
InferMaxDefaultType: true,
}
_, err = conf.Check(filename, []*syntax.File{file}, nil)
}

View File

@ -170,11 +170,6 @@ type Config struct {
// If DisableUnusedImportCheck is set, packages are not checked
// for unused imports.
DisableUnusedImportCheck bool
// If _InferMaxDefaultType is set, the minimum (smallest) default
// type that fits all untyped constant arguments for the same type
// parameter is selected in type inference. (go.dev/issue/58671)
_InferMaxDefaultType bool
}
func srcimporter_setUsesCgo(conf *Config) {

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, "_InferMaxDefaultType"), "inferMaxDefaultType", false, "")
if err := parseFlags(filenames[0], srcs[0], flags); err != nil {
t.Fatal(err)
}

View File

@ -106,7 +106,6 @@ var filemap = map[string]action{
"infer.go": func(f *ast.File) {
fixTokenPos(f)
fixInferSig(f)
renameIdent(f, "InferMaxDefaultType", "_InferMaxDefaultType")
},
// "initorder.go": fixErrErrorfCall, // disabled for now due to unresolved error_ use implications for gopls
"instantiate.go": func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) },

View File

@ -275,7 +275,19 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
u.tracef("== untyped arguments: %v", untyped)
}
if check.conf._InferMaxDefaultType {
// We need a poser/positioner for check.allowVersion below.
// We should really use pos (argument to infer) but currently
// the generator that generates go/types/infer.go has trouble
// with that. For now, do a little dance to get a position if
// we need one. (If we don't have untyped arguments left, it
// doesn't matter which branch we take below.)
// TODO(gri) adjust infer signature or adjust the rewriter.
var at token.Pos
if len(untyped) > 0 {
at = params.At(untyped[0]).pos
}
if check.allowVersion(check.pkg, atPos(at), go1_21) {
// Some generic parameters with untyped arguments may have been given a type by now.
// Collect all remaining parameters that don't have a type yet and determine the
// maximum untyped type for each of those parameters, if possible.

View File

@ -143,7 +143,6 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
GoVersion: goVersion,
Importer: stdLibImporter,
}
*boolFieldAddr(&conf, "_InferMaxDefaultType") = true
_, err = conf.Check(filename, fset, []*ast.File{file}, nil)
}

View File

@ -307,15 +307,15 @@ var _ int = f7 /* ERROR "cannot use" */ ([]float64{}...)
var _ float64 = f7([]float64{}...)
var _ = f7[float64](1, 2.3)
var _ = f7(float64(1), 2.3)
var _ = f7(1, 2.3 /* ERROR "does not match" */ )
var _ = f7(1.2, 3 /* ERROR "does not match" */ )
var _ = f7(1, 2.3)
var _ = f7(1.2, 3)
func f8[A, B any](A, B, ...B) int { panic(0) }
var _ = f8(1) /* ERROR "not enough arguments" */
var _ = f8(1, 2.3)
var _ = f8(1, 2.3, 3.4, 4.5)
var _ = f8(1, 2.3, 3.4, 4 /* ERROR "does not match" */ )
var _ = f8(1, 2.3, 3.4, 4)
var _ = f8[int, float64](1, 2.3, 3.4, 4)
var _ = f8[int, float64](0, 0, nil...) // test case for #18268

View File

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