mirror of https://github.com/golang/go.git
cmd/compile: enable reverse type inference
For #59338. Change-Id: I8141d421cdc60e47ee5794fc1ca81246bd8a8a25 Reviewed-on: https://go-review.googlesource.com/c/go/+/491475 Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
ea9097c9f7
commit
1f570787a8
|
|
@ -50,8 +50,9 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
|
||||||
}
|
}
|
||||||
base.ErrorfAt(m.makeXPos(terr.Pos), terr.Code, "%s", msg)
|
base.ErrorfAt(m.makeXPos(terr.Pos), terr.Code, "%s", msg)
|
||||||
},
|
},
|
||||||
Importer: &importer,
|
Importer: &importer,
|
||||||
Sizes: &gcSizes{},
|
Sizes: &gcSizes{},
|
||||||
|
EnableReverseTypeInference: true,
|
||||||
}
|
}
|
||||||
info := &types2.Info{
|
info := &types2.Info{
|
||||||
StoreTypesInSyntax: true,
|
StoreTypesInSyntax: true,
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,11 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
|
||||||
}
|
}
|
||||||
file, err := syntax.ParseFile(filename, nil, nil, 0)
|
file, err := syntax.ParseFile(filename, nil, nil, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
conf := Config{GoVersion: goVersion, Importer: stdLibImporter}
|
conf := Config{
|
||||||
|
GoVersion: goVersion,
|
||||||
|
Importer: stdLibImporter,
|
||||||
|
EnableReverseTypeInference: true,
|
||||||
|
}
|
||||||
_, err = conf.Check(filename, []*syntax.File{file}, nil)
|
_, err = conf.Check(filename, []*syntax.File{file}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -250,8 +254,9 @@ func typecheckFiles(t *testing.T, path string, filenames []string) {
|
||||||
|
|
||||||
// typecheck package files
|
// typecheck package files
|
||||||
conf := Config{
|
conf := Config{
|
||||||
Error: func(err error) { t.Error(err) },
|
Error: func(err error) { t.Error(err) },
|
||||||
Importer: stdLibImporter,
|
Importer: stdLibImporter,
|
||||||
|
EnableReverseTypeInference: true,
|
||||||
}
|
}
|
||||||
info := Info{Uses: make(map[*syntax.Name]Object)}
|
info := Info{Uses: make(map[*syntax.Name]Object)}
|
||||||
conf.Check(path, files, &info)
|
conf.Check(path, files, &info)
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,11 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
|
||||||
// parse and type-check file
|
// parse and type-check file
|
||||||
file, err := parser.ParseFile(fset, filename, nil, 0)
|
file, err := parser.ParseFile(fset, filename, nil, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
conf := Config{GoVersion: goVersion, Importer: stdLibImporter}
|
conf := Config{
|
||||||
|
GoVersion: goVersion,
|
||||||
|
Importer: stdLibImporter,
|
||||||
|
}
|
||||||
|
*boolFieldAddr(&conf, "_EnableReverseTypeInference") = true
|
||||||
_, err = conf.Check(filename, fset, []*ast.File{file}, nil)
|
_, err = conf.Check(filename, fset, []*ast.File{file}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -267,6 +271,7 @@ func typecheckFiles(t *testing.T, path string, filenames []string) {
|
||||||
},
|
},
|
||||||
Importer: stdLibImporter,
|
Importer: stdLibImporter,
|
||||||
}
|
}
|
||||||
|
*boolFieldAddr(&conf, "_EnableReverseTypeInference") = true
|
||||||
info := Info{Uses: make(map[*ast.Ident]Object)}
|
info := Info{Uses: make(map[*ast.Ident]Object)}
|
||||||
conf.Check(path, fset, files, &info)
|
conf.Check(path, fset, files, &info)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
// run
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
// Smoke test for reverse type inference.
|
||||||
|
// The type checker has more expansive tests.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var f1 func(int) int
|
||||||
|
f1 = g1
|
||||||
|
if f1(1) != g1(1) {
|
||||||
|
panic(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
var f2 func(int) string = g2
|
||||||
|
if f2(2) != "" {
|
||||||
|
panic(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disabled for now - requires some noder work
|
||||||
|
// TODO fix this
|
||||||
|
// if g3(g1, 3) != g1(3) {
|
||||||
|
// panic(3)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if g4(g2, 4) != "" {
|
||||||
|
// panic(4)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func g1[P any](x P) P { return x }
|
||||||
|
func g2[P, Q any](x P) Q { var q Q; return q }
|
||||||
|
|
||||||
|
func g3(f1 func(int) int, x int) int { return f1(x) }
|
||||||
|
func g4(f2 func(int) string, x int) string { return f2(x) }
|
||||||
Loading…
Reference in New Issue