Skip to content

Commit

Permalink
test:info.OverloadOf
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed May 2, 2024
1 parent e610602 commit 967e9bb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 26 deletions.
80 changes: 80 additions & 0 deletions x/typesutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,83 @@ 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)
}
}
for use, o := range info.Uses {
declObj, ovObjs := info.OverloadOf(use)
if declObj != nil && ovObjs != nil {
if info.Overloads[use] == nil {
t.Fatal("bad overload", o)
}
}
}
}
10 changes: 6 additions & 4 deletions x/typesutil/gopinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ 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
if obj := info.Overloads[id]; obj != nil {
if sig, ok := obj.Type().(*types.Signature); ok {
if _, objs := gogen.CheckSigFuncExObjects(sig); len(objs) > 1 {
return obj, objs
}
}
}
return nil, nil
Expand Down Expand Up @@ -265,7 +267,7 @@ func (info gopRecorder) Use(id *ast.Ident, obj types.Object) {
}
if info.Overloads != nil {
if sig, ok := obj.Type().(*types.Signature); ok {
if ext, objs := gogen.CheckSigFuncExObjects(sig); len(objs) > 1 {
if ext, ok := gogen.CheckSigFuncEx(sig); ok {
if debugVerbose {
log.Println("==> Overloads:", id, ext)
}
Expand Down
36 changes: 14 additions & 22 deletions x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ func testGopInfoEx(t *testing.T, mod *gopmod.Module, name string, src string, go
list = append(list, defsList(fset, info.Defs, true)...)
list = append(list, "== uses ==")
list = append(list, usesList(fset, info.Uses)...)
list = append(list, "== overloads ==")
list = append(list, overloadsList(fset, info.Overloads)...)
if len(info.Overloads) > 0 {
list = append(list, "== overloads ==")
list = append(list, overloadsList(fset, info.Overloads)...)
}
result := strings.Join(list, "\n")
t.Log(result)
if result != expect {
Expand Down Expand Up @@ -587,8 +589,7 @@ println a
001: 2: 1 | main | func main.main()
== uses ==
000: 3: 1 | println | func fmt.Println(a ...any) (n int, err error)
001: 3: 9 | a | var a []int
== overloads ==`)
001: 3: 9 | a | var a []int`)
}

func TestForPhrase1(t *testing.T) {
Expand Down Expand Up @@ -627,8 +628,7 @@ println sum
002: 4: 8 | sum | var sum int
003: 4:14 | x | var x int
004: 6: 1 | println | func fmt.Println(a ...any) (n int, err error)
005: 6: 9 | sum | var sum int
== overloads ==`)
005: 6: 9 | sum | var sum int`)
}

func TestForPhrase2(t *testing.T) {
Expand Down Expand Up @@ -675,8 +675,7 @@ println sum
003: 4: 8 | sum | var sum int
004: 4:14 | x | var x int
005: 6: 1 | println | func fmt.Println(a ...any) (n int, err error)
006: 6: 9 | sum | var sum int
== overloads ==`)
006: 6: 9 | sum | var sum int`)
}

func TestMapComprehension(t *testing.T) {
Expand All @@ -703,8 +702,7 @@ println y
000: 2: 7 | x | var x string
001: 2:10 | i | var i int
002: 3: 1 | println | func fmt.Println(a ...any) (n int, err error)
003: 3: 9 | y | var y map[string]int
== overloads ==`)
003: 3: 9 | y | var y map[string]int`)
}

func TestListComprehension(t *testing.T) {
Expand All @@ -730,8 +728,7 @@ _ = b
000: 3: 7 | x | var x float64
001: 3: 9 | x | var x float64
002: 3:20 | a | var a []float64
003: 4: 5 | b | var b []float64
== overloads ==`)
003: 4: 5 | b | var b []float64`)
}

func TestListComprehensionMultiLevel(t *testing.T) {
Expand Down Expand Up @@ -775,8 +772,7 @@ println("x:", x)
005: 3:43 | arr | var arr []float64
006: 3:48 | b | var b float64
007: 4: 1 | println | func fmt.Println(a ...any) (n int, err error)
008: 4:15 | x | var x [][]float64
== overloads ==`)
008: 4:15 | x | var x [][]float64`)
}

func TestFileEnumLines(t *testing.T) {
Expand All @@ -798,8 +794,7 @@ for line <- os.Stdin {
000: 4:13 | os | package os
001: 4:16 | Stdin | var os.Stdin *os.File
002: 5: 2 | println | func fmt.Println(a ...any) (n int, err error)
003: 5:10 | line | var line string
== overloads ==`)
003: 5:10 | line | var line string`)
}

func TestLambdaExpr(t *testing.T) {
Expand Down Expand Up @@ -870,8 +865,7 @@ Map2([1.2, 3.5, 6], x => (x * x, x + x))
011: 11:27 | x | var x float64
012: 11:31 | x | var x float64
013: 11:34 | x | var x float64
014: 11:38 | x | var x float64
== overloads ==`)
014: 11:38 | x | var x float64`)
}

func TestLambdaExpr2(t *testing.T) {
Expand Down Expand Up @@ -950,8 +944,7 @@ Map2([1.2, 3.5, 6], x => {
011: 14: 9 | x | var x float64
012: 14:13 | x | var x float64
013: 14:16 | x | var x float64
014: 14:20 | x | var x float64
== overloads ==`)
014: 14:20 | x | var x float64`)
}

func TestMixedOverload1(t *testing.T) {
Expand Down Expand Up @@ -1661,8 +1654,7 @@ func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] {
004: 4: 6 | Gopx_Var_Cast__0 | func main.Gopx_Var_Cast__0[T main.basetype]() *main.Var__0[T]
005: 4:23 | string | type string
006: 5: 6 | Gopx_Var_Cast__1 | func main.Gopx_Var_Cast__1[T map[string]any]() *main.Var__1[T]
007: 5:23 | M | type main.M = map[string]any
== overloads ==`)
007: 5:23 | M | type main.M = map[string]any`)
}

func TestSpxInfo(t *testing.T) {
Expand Down

0 comments on commit 967e9bb

Please sign in to comment.