Skip to content

Commit

Permalink
Adding more tests around IndexExpr case
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Oct 2, 2024
1 parent 3077f7f commit 67ec590
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions compiler/internal/analysis/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ func (fi *FuncInfo) visitCallExpr(n *ast.CallExpr) ast.Visitor {
// Register literal function call site in case it is identified as blocking.
fi.literalFuncCallees[f] = append(fi.literalFuncCallees[f], fi.visitorStack.copy())
return nil // No need to walk under this CallExpr, we already did it manually.
case *ast.IndexExpr:
// Collect info about the instantiated type or function.

fmt.Printf(">> %[1]T %#[1]v\n", n) // TODO(gn): Finish implementing!

default:
if astutil.IsTypeExpr(f, fi.pkgInfo.Info) {
// This is a type conversion, not a call. Type assertion itself is not
Expand Down
44 changes: 43 additions & 1 deletion compiler/internal/analysis/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestInstanceBlocking(t *testing.T) {
notBlocking []string
}{
{
name: "blocking instance",
name: `blocking instance`,
src: `package test
func blocking[T any]() {
c := make(chan T)
Expand All @@ -73,6 +73,48 @@ func TestInstanceBlocking(t *testing.T) {
blocking: []string{`blocking`, `bInt`},
notBlocking: []string{`notBlocking`, `nbUint`},
},
{
name: `differentiate indexing`,
// Below calls notBlocking but since the function pointers
// are in the slice they will both be considered as blocking.
// This is just checking that the analysis can tell between
// indexing and instantiation of a generic.
src: `package test
func blocking() {
c := make(chan int)
<-c
}
func notBlocking() {
println()
}
var funcs = []func() { blocking, notBlocking }
func indexer() {
funcs[1]()
}`,
blocking: []string{`blocking`, `indexer`},
notBlocking: []string{`notBlocking`},
},
{
name: `differentiate casting`,
// Below checks that casting to an instance type is treated as a
// cast an not accidentally treated as a function call.
src: `package test
type Foo[T any] interface {
Baz() T
}
type Bar struct {
name string
}
func (b Bar) Baz() string {
return b.name
}
func caster() {
a := Bar{"foo"}
b := Foo[string](a)
println(b.Baz())
}`,
notBlocking: []string{`caster`},
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion internal/srctesting/srctesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (f *Fixture) Check(importPath string, files ...*ast.File) (*types.Info, *ty
}
pkg, err := config.Check(importPath, f.FileSet, files, info)
if err != nil {
f.T.Fatalf("Filed to type check test source: %s", err)
f.T.Fatalf("Failed to type check test source: %s", err)
}
f.Packages[importPath] = pkg
return info, pkg
Expand Down

0 comments on commit 67ec590

Please sign in to comment.