Skip to content
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
30 changes: 30 additions & 0 deletions internal/semantic_analysis/call_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,33 @@ func TestLibraryByPath(t *testing.T) {
}
}
}

// TestCallGraph_SamePositionInSiblingFiles: HelpX (types.go) and the method P
// (methods.go, filed under T in types.go) each make one call at the same
// line:col. Each must resolve its own callee; resolving by position over the
// whole package attributed right to HelpX (#32).
func TestCallGraph_SamePositionInSiblingFiles(t *testing.T) {
dir, err := filepath.Abs("../../testdata/callsite_collision")
if err != nil {
t.Fatal(err)
}
b := syntactic_analysis.NewSymbolTableBuilder(dir)
st, err := b.Build(nil, true)
if err != nil {
t.Fatalf("symbol table build failed: %v", err)
}
edges := semantic_analysis.NewCallGraphBuilder(dir, b.Fset(), b.Pkgs()).Build(st)

got := make([]string, 0, len(edges))
for _, e := range edges {
got = append(got, e.Source+" -> "+e.Target)
}
sort.Strings(got)
want := []string{
"example.com/callsitecollision.HelpX -> example.com/callsitecollision.wrong",
"example.com/callsitecollision.T.P -> example.com/callsitecollision.right",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("edges = %q, want %q", got, want)
}
}
3 changes: 3 additions & 0 deletions testdata/callsite_collision/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/callsitecollision

go 1.21
11 changes: 11 additions & 0 deletions testdata/callsite_collision/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package collision

// P's call is at the same line:col as Hlp's in types.go.

func right() {}

func init() {}

func (T) N() {}

func (T) P() { right() }
11 changes: 11 additions & 0 deletions testdata/callsite_collision/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package collision has two calls at the same line:col in sibling files, one in
// a method declared away from its receiver type (#32).
package collision

type T struct{}

func (T) M() {}

func wrong() {}

func HelpX() { wrong() }
Loading