aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2023-05-20 16:48:02 +0200
committerTeddy Wing2023-05-20 16:48:02 +0200
commitec33e063b86ae2c5208d856ee1a36259f342d5e0 (patch)
tree2f06a22271d25f688809fdac60e0035dc7513779
parent8ac64e76c32a1f803460a71d29efd235d9be8edd (diff)
downloadgocapturedrefrace-ec33e063b86ae2c5208d856ee1a36259f342d5e0.tar.bz2
Clean up 'function literal in variable' code
* Remove the test code where I was looking for how to get the function literal from the ident specified in the `go` statement. * Clean up `funcLitFromIdent`.
-rw-r--r--capturedrefrace.go54
1 files changed, 7 insertions, 47 deletions
diff --git a/capturedrefrace.go b/capturedrefrace.go
index e9221a9..e7431ac 100644
--- a/capturedrefrace.go
+++ b/capturedrefrace.go
@@ -51,7 +51,6 @@
package capturedrefrace
import (
- "fmt"
"go/ast"
"go/token"
"go/types"
@@ -84,38 +83,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
return
}
- fmt.Printf("func: %#v\n", goStmt.Call.Fun)
- funcIdent, ok := goStmt.Call.Fun.(*ast.Ident)
- if ok {
- fmt.Printf("funcobj: %#v\n", funcIdent.Obj)
- t := pass.TypesInfo.Types[funcIdent]
- fmt.Printf("functype-fromtypesinfo: %#v\n", t)
- fmt.Printf("funcdecl: %#v\n", funcIdent.Obj.Decl)
- obj := pass.TypesInfo.Uses[funcIdent]
- fmt.Printf("funcobj-fromtypesinfo: %#v\n", obj)
- fmt.Printf("funcobj-fromtypesinfo-type: %#v\n", obj.Type())
- def := pass.TypesInfo.Defs[funcIdent]
- fmt.Printf("func def: %#v\n", def)
-
- obj2 := pass.TypesInfo.ObjectOf(funcIdent)
- fmt.Printf("obj2: %#v\n", obj2)
-
- fmt.Printf("obj-parent: %#v\n", obj.Parent())
-
- fmt.Printf("parent lookup: %#v\n", obj.Parent().Lookup(obj.Name()))
- // TODO: How to get the scope of the closure?
-
- das, ok := funcIdent.Obj.Decl.(*ast.AssignStmt)
- if ok {
- for _, expr := range das.Rhs {
- fl, ok := expr.(*ast.FuncLit)
- if ok {
- fmt.Printf("funclit: %#v\n", fl)
- }
- }
- }
- }
-
var funcLit *ast.FuncLit
switch goStmtFunc := goStmt.Call.Fun.(type) {
@@ -153,8 +120,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}
-// TODO: doc
+// funcLitFromIdent takes a variable identifier and looks for a function
+// literal assigned to it, returning the function literal.
func funcLitFromIdent(funcIdent *ast.Ident) (funcLit *ast.FuncLit, ok bool) {
+ // Find the assignment where the function literal was defined.
assignStmt, ok := funcIdent.Obj.Decl.(*ast.AssignStmt)
if !ok {
return nil, ok
@@ -162,6 +131,8 @@ func funcLitFromIdent(funcIdent *ast.Ident) (funcLit *ast.FuncLit, ok bool) {
funcVariableName := funcIdent.Name
+ // Find the index of funcVariableName in assignStmt.Lhs. We need this to
+ // find the closure in the corresponding assignStmt.Rhs list.
assignmentIndex := -1
for i, expr := range assignStmt.Lhs {
lhsIdent, ok := expr.(*ast.Ident)
@@ -175,20 +146,9 @@ func funcLitFromIdent(funcIdent *ast.Ident) (funcLit *ast.FuncLit, ok bool) {
}
}
- if assignmentIndex == -1 {
- return nil, false
- }
-
- // TODO: Get assignStmt.Rhs[position of ident name in assignStmt.Lhs]
- // for _, expr := range assignStmt.Rhs {
- // funcLit, ok := expr.(*ast.FuncLit)
- // if !ok {
- // fmt.Printf("funclit: %#v\n", fl)
- // return nil, ok
- // }
- // }
+ if assignmentIndex == -1 ||
+ assignmentIndex > len(assignStmt.Rhs)-1 {
- if assignmentIndex > len(assignStmt.Rhs)-1 {
return nil, false
}