]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: use more lenient type inference for untyped arguments for go1.21
authorRobert Griesemer <gri@golang.org>
Wed, 17 May 2023 21:48:51 +0000 (14:48 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 19 May 2023 01:56:58 +0000 (01:56 +0000)
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>
12 files changed:
src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/types2/api.go
src/cmd/compile/internal/types2/check_test.go
src/cmd/compile/internal/types2/infer.go
src/cmd/compile/internal/types2/stdlib_test.go
src/go/types/api.go
src/go/types/check_test.go
src/go/types/generate_test.go
src/go/types/infer.go
src/go/types/stdlib_test.go
src/internal/types/testdata/check/typeparams.go
src/internal/types/testdata/fixedbugs/issue58671.go

index baccd0323b5fc3ca5b775e9b51bde54b134625af..3adf9e5d11d82bd4278d8c5d8ea4a2eb2e99dbd1 100644 (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{},
-               InferMaxDefaultType: true, // #58671
+               Importer: &importer,
+               Sizes:    &gcSizes{},
        }
        info := &types2.Info{
                StoreTypesInSyntax: true,
index 5860c3a92cd2fddeaab268a19e9d3e2aac838737..b798f2c888e8f31ad30dc28a5bb5e0349659b001 100644 (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) {
index 357ca7cf502fa09894e08f3a651d6e791b80ff9e..26bb1aed9e6e8c5eaed271fbc6af2336a1dce87e 100644 (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)
        }
index d8c81820f8884b9df00d77e231dfc489fdfe6cd4..77c594a72238ddd346de75b85853f95fb39f04c7 100644 (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.
index 80a05b749122268b95dc6a3269833062287bb5a5..404e1636aec480bf9f97607903fb441c58beefde 100644 (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,
-                               InferMaxDefaultType: true,
+                               GoVersion: goVersion,
+                               Importer:  stdLibImporter,
                        }
                        _, err = conf.Check(filename, []*syntax.File{file}, nil)
                }
index 3f83a8088ad9be362f9876dbdcdb4953e45c1a4b..08430c9e7a5c85c9ab1c23ccc1c70970a470db59 100644 (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) {
index 9dbcb326cffeedf2b38d380343f510fdd4e36b95..d53aaeadc5e4dc547ec41048cd3bf88f5126239c 100644 (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)
        }
index 73ad2c5b89157e14c0b1eb21148dd33c483425d4..6a8343c615355c6539a3d5fcba6785f7606c2ed6 100644 (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) },
index 9c31d6adf62c8c9512e060f02645e1a3536286f4..b376ce4a4a741d178171312db8f892024981f3cd 100644 (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.
index a7c1ae2ebab579cf191af33779ea5fd64a29ea5c..82f22de8362d7d0b23c69ba9847a2def2be6d379 100644 (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)
                }
 
index 9f06b0888daf2193f0d058fa03b62d743c9a990b..b002377df79e02b054cce0387067fb6d9a8d8465 100644 (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
index 166ffda3d90eeb25fdd33c63f3c42ced413df0d8..fa964aa5fd2e812e5289ac49c927c2878ee111fe 100644 (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.