Skip to content

Commit

Permalink
test:info.OverloadOf
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 30, 2024
1 parent e610602 commit 248c4de
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
72 changes: 72 additions & 0 deletions x/typesutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,75 @@ func TestBadFile(t *testing.T) {
_ = checker.Files([]*goast.File{{Name: goast.NewIdent("main")}},
[]*ast.File{{Name: ast.NewIdent("main")}})
}

func TestCheckOverload(t *testing.T) {
fset := token.NewFileSet()
info, ginfo, err := checkFiles(fset, "main.gop", `
type foo struct {
}
func (a *foo) mulInt(b int) *foo {
return a
}
func (a *foo) mulFoo(b *foo) *foo {
return a
}
func (foo).mul = (
(foo).mulInt
(foo).mulFoo
)
func addInt0() {
}
func addInt1(i int) {
}
func addInt2(i, j int) {
}
var addInt3 = func(i, j, k int) {
}
func add = (
addInt0
addInt1
addInt2
addInt3
func(a, b string) string {
return a + b
}
)
var a, b *foo
var c = a.mul(100)
var d = a.mul(c)
func init() {
add 100, 200
add 100, 200, 300
add("hello", "world")
}
`, "", "", "", "")
if err != nil || info == nil || ginfo == nil {
t.Fatalf("check failed: %v", err)
}
for use, ovDeclObj := range info.Overloads {
o := info.ObjectOf(use)
declObj, ovObjs := info.OverloadOf(use)
if ovDeclObj != declObj {
t.Fatal("bad overload", o)
}
found := false
for _, ovObj := range ovObjs {
if o == ovObj {
found = true
break
}
}
if !found {
t.Fatal("bad overload", o)
}
}
}
9 changes: 6 additions & 3 deletions x/typesutil/gopinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ func (info *Info) TypeOf(e ast.Expr) types.Type {

// Returns the overloaded function declaration corresponding to the ident and its overloaded function members
func (info *Info) OverloadOf(id *ast.Ident) (types.Object, []types.Object) {
if sig, ok := info.Overloads[id].Type().(*types.Signature); ok {
if _, objs := gogen.CheckSigFuncExObjects(sig); len(objs) > 1 {
return info.Overloads[id], objs
overdecl := info.Overloads[id]
if overdecl != nil {
if sig, ok := overdecl.Type().(*types.Signature); ok {
if _, objs := gogen.CheckSigFuncExObjects(sig); len(objs) > 1 {
return overdecl, objs
}
}
}
return nil, nil

Check warning on line 181 in x/typesutil/gopinfo.go

View check run for this annotation

Codecov / codecov/patch

x/typesutil/gopinfo.go#L181

Added line #L181 was not covered by tests
Expand Down

0 comments on commit 248c4de

Please sign in to comment.