Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/typesutil: add config.UpdateGoTypesOverload for fixed mixed types.Package #1793

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion x/typesutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type Config struct {

// If IgnoreFuncBodies is set, skip compiling function bodies (optional).
IgnoreFuncBodies bool

// If UpdateGoTypesOverload is set, update go types overload data (optional).
UpdateGoTypesOverload bool
}

// A Checker maintains the state of the type checker.
Expand Down Expand Up @@ -175,13 +178,16 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) (err error)
}
// don't return even if err != nil
}
if len(files) > 0 && p.goInfo != nil {
if len(files) > 0 {
scope := pkgTypes.Scope()
objMap := DeleteObjects(scope, files)
checker := types.NewChecker(conf, fset, pkgTypes, p.goInfo)
err = checker.Files(files)
// TODO: how to process error?
CorrectTypesInfo(scope, objMap, p.gopInfo.Uses)
if opts.UpdateGoTypesOverload {
gox.InitThisGopPkg(pkgTypes)
}
}
return
}
Expand Down
118 changes: 110 additions & 8 deletions x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/goplus/gop/parser"
"github.com/goplus/gop/token"
"github.com/goplus/gop/x/typesutil"
"github.com/goplus/gox"
"github.com/goplus/mod/env"
"github.com/goplus/mod/gopmod"
"github.com/goplus/mod/modfile"
Expand Down Expand Up @@ -61,26 +62,27 @@ func spxParserConf() parser.Config {
}
}

func parseMixedSource(mod *gopmod.Module, fset *token.FileSet, name, src string, goname string, gosrc string, parserConf parser.Config) (*typesutil.Info, *types.Info, error) {
func parseMixedSource(mod *gopmod.Module, fset *token.FileSet, name, src string, goname string, gosrc string, parserConf parser.Config, updateGoTypesOverload bool) (*types.Package, *typesutil.Info, *types.Info, error) {
f, err := parser.ParseEntry(fset, name, src, parserConf)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
var gofiles []*goast.File
if len(gosrc) > 0 {
f, err := goparser.ParseFile(fset, goname, gosrc, goparser.ParseComments)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
gofiles = append(gofiles, f)
}

conf := &types.Config{}
conf.Importer = gop.NewImporter(nil, &env.Gop{Root: "../..", Version: "1.0"}, fset)
chkOpts := &typesutil.Config{
Types: types.NewPackage("main", f.Name.Name),
Fset: fset,
Mod: mod,
Types: types.NewPackage("main", f.Name.Name),
Fset: fset,
Mod: mod,
UpdateGoTypesOverload: updateGoTypesOverload,
}
info := &typesutil.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Expand All @@ -101,7 +103,7 @@ func parseMixedSource(mod *gopmod.Module, fset *token.FileSet, name, src string,
}
check := typesutil.NewChecker(conf, chkOpts, ginfo, info)
err = check.Files(gofiles, []*ast.File{f})
return info, ginfo, err
return chkOpts.Types, info, ginfo, err
}

func parseSource(fset *token.FileSet, filename string, src interface{}, mode parser.Mode) (*types.Package, *typesutil.Info, error) {
Expand Down Expand Up @@ -166,7 +168,7 @@ func testSpxInfo(t *testing.T, name string, src string, expect string) {

func testGopInfoEx(t *testing.T, mod *gopmod.Module, name string, src string, goname string, gosrc string, expect string, parseConf parser.Config) {
fset := token.NewFileSet()
info, _, err := parseMixedSource(mod, fset, name, src, goname, gosrc, parseConf)
_, info, _, err := parseMixedSource(mod, fset, name, src, goname, gosrc, parseConf, false)
if err != nil {
t.Fatal("parserMixedSource error", err)
}
Expand Down Expand Up @@ -1879,3 +1881,103 @@ func _() {
}
`)
}

func TestAddress2(t *testing.T) {
testInfo(t, `package load
import "os"

func _() { _ = os.Stdout }
func _() {
var a int
_ = a
}
func _() {
var p *int
_ = *p
}
func _() {
var s []int
_ = s[0]
}
func _() {
var s struct{f int}
_ = s.f
}
func _() {
var a [10]int
_ = a[0]
}
func _(x int) {
_ = x
}
func _()(x int) {
_ = x
return
}
type T int
func (x T) _() {
_ = x
}

func _() {
var a, b int
_ = a + b
}
func _() {
_ = &[]int{1}
}
func _() {
_ = func(){}
}
func f() { _ = f }
func _() {
var m map[int]int
_ = m[0]
_, _ = m[0]
}
func _() {
var ch chan int
_ = <-ch
_, _ = <-ch
}
`)
}

func TestMixedPackage(t *testing.T) {
fset := token.NewFileSet()
pkg, _, _, err := parseMixedSource(gopmod.Default, fset, "main.gop", `
Test
Test 100
var n N
n.test
n.test 100
`, "main.go", `
package main

func Test__0() {
}
func Test__1(n int) {
}
type N struct {
}
func (p *N) Test__0() {
}
func (p *N) Test__1(n int) {
}`, parser.Config{}, true)
if err != nil {
t.Fatal(err)
}
obj := pkg.Scope().Lookup("N")
named := obj.Type().(*types.Named)
if named.NumMethods() == 2 {
t.Fatal("found overload method failed")
}
ext, ok := gox.CheckFuncEx(named.Method(2).Type().(*types.Signature))
if !ok {
t.Fatal("checkFuncEx failed")
}
m, ok := ext.(*gox.TyOverloadMethod)
if !ok || len(m.Methods) != 2 {
t.Fatal("check TyOverloadMethod failed")
}
}