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

v1.11.37 #258

Merged
merged 7 commits into from
Aug 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,46 @@ func TestCheckSignature(t *testing.T) {
}
}

func TestCheckSignatures(t *testing.T) {
denoteRecv(&ast.SelectorExpr{Sel: ident("x")})
if CheckSignatures(nil, 0, 0) != nil {
t.Fatal("TestCheckSignatures failed: CheckSignatures(nil) != nil")
}
sig := types.NewSignature(nil, nil, nil, false)
if v := CheckSignatures(sig, 0, 0); len(v) != 1 || v[0] != sig {
t.Fatal("TestCheckSignatures failed: CheckSignatures(sig)[0] != sig")
}
pkg := types.NewPackage("", "foo")
arg := types.NewParam(token.NoPos, pkg, "", sig)
sig2 := types.NewSignature(nil, types.NewTuple(arg, arg), nil, false)
o := types.NewFunc(token.NoPos, pkg, "bar", sig2)
if CheckSignatures(&templateRecvMethodType{fn: o}, 0, 0) == nil {
t.Fatal("TestCheckSignatures failed: CheckSignatures == nil")
}
sig3 := types.NewSignature(nil, types.NewTuple(arg, arg, arg), nil, false)
o2 := types.NewFunc(token.NoPos, pkg, "bar", sig3)
of := NewOverloadFunc(token.NoPos, pkg, "bar", o, o2)
if v := CheckSignatures(of.Type(), 0, 0); len(v) != 2 {
t.Fatal("TestCheckSignatures failed: OverloadFunc CheckSignatures ==", len(v))
}

if HasAutoProperty(of.Type()) {
t.Fatal("func bar has autoprop?")
}

o3 := types.NewFunc(token.NoPos, pkg, "bar2", sig)
of2 := NewOverloadFunc(token.NoPos, pkg, "bar3", o3)
if !HasAutoProperty(of2.Type()) {
t.Fatal("func bar3 has autoprop?")
}

typ := types.NewNamed(types.NewTypeName(token.NoPos, pkg, "t", nil), types.Typ[types.Int], nil)
om := NewOverloadMethod(typ, token.NoPos, pkg, "bar", o, o2)
if CheckSignatures(om.Type(), 0, 1) != nil {
t.Fatal("TestCheckSignatures failed: OverloadMethod CheckSignatures != nil")
}
}

func TestCheckSigParam(t *testing.T) {
if checkSigParam(types.NewPointer(types.Typ[types.Int]), -1) {
t.Fatal("TestCheckSigParam failed: checkSigParam *int should return false")
Expand Down
42 changes: 39 additions & 3 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ func CheckSignature(typ types.Type, idx, nin int) *types.Signature {
switch t := typ.(type) {
case *types.Signature:
if funcs, ok := CheckOverloadMethod(t); ok {
return checkOverloadFuncs(funcs, idx, nin)
return checkOverloadFunc(funcs, idx, nin)
} else {
return t
}
case *overloadFuncType:
return checkOverloadFuncs(t.funcs, idx, nin)
return checkOverloadFunc(t.funcs, idx, nin)
case *templateRecvMethodType:
if sig, ok := t.fn.Type().(*types.Signature); ok {
params := sig.Params()
Expand All @@ -337,7 +337,7 @@ func CheckSignature(typ types.Type, idx, nin int) *types.Signature {
return nil
}

func checkOverloadFuncs(funcs []types.Object, idx, nin int) *types.Signature {
func checkOverloadFunc(funcs []types.Object, idx, nin int) *types.Signature {
for _, v := range funcs {
if sig, ok := v.Type().(*types.Signature); ok {
params := sig.Params()
Expand All @@ -349,6 +349,42 @@ func checkOverloadFuncs(funcs []types.Object, idx, nin int) *types.Signature {
return nil
}

func CheckSignatures(typ types.Type, idx, nin int) []*types.Signature {
switch t := typ.(type) {
case *types.Signature:
if funcs, ok := CheckOverloadMethod(t); ok {
return checkOverloadFuncs(funcs, idx, nin)
} else {
return []*types.Signature{t}
}
case *overloadFuncType:
return checkOverloadFuncs(t.funcs, idx, nin)
case *templateRecvMethodType:
if sig, ok := t.fn.Type().(*types.Signature); ok {
params := sig.Params()
n := params.Len()
mparams := make([]*types.Var, n-1)
for i := range mparams {
mparams[i] = params.At(i + 1)
}
return []*types.Signature{types.NewSignature(nil, types.NewTuple(mparams...), sig.Results(), sig.Variadic())}
}
}
return nil
}

func checkOverloadFuncs(funcs []types.Object, idx, nin int) (sigs []*types.Signature) {
for _, v := range funcs {
if sig, ok := v.Type().(*types.Signature); ok {
params := sig.Params()
if idx < params.Len() && checkSigParam(params.At(idx).Type(), nin) {
sigs = append(sigs, sig)
}
}
}
return
}

func checkSigParam(typ types.Type, nin int) bool {
switch nin {
case -1: // input is CompositeLit
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/goplus/gox

go 1.16

require golang.org/x/tools v0.11.0
require golang.org/x/tools v0.11.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/tools v0.11.1 h1:ojD5zOW8+7dOGzdnNgersm8aPfcDjhMp12UfG93NIMc=
golang.org/x/tools v0.11.1/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2 changes: 1 addition & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func isOverloadFunc(name string) bool {
return n > 3 && name[n-3:n-1] == "__"
}

func initGopPkg(pkg *types.Package) {
func InitGopPkg(pkg *types.Package) {
scope := pkg.Scope()
if scope.Lookup(gopPackage) == nil { // not is a Go+ package
return
Expand Down
2 changes: 1 addition & 1 deletion package.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (p *File) importPkg(this *Package, pkgPath string, src ast.Node) *PkgRef {
}
panic(e)
} else {
initGopPkg(pkgImp)
InitGopPkg(pkgImp)
}
pkgImport = &PkgRef{Types: pkgImp}
p.importPkgs[pkgPath] = pkgImport
Expand Down