diff options
author | Teddy Wing | 2024-03-09 21:20:32 +0100 |
---|---|---|
committer | Teddy Wing | 2024-03-09 21:20:32 +0100 |
commit | b1b311caf11e9e88ef5735dc4e5512dcd4725059 (patch) | |
tree | d7704a3bd6743863d548c29d611ca37f6a814380 /capturedrefrace.go | |
parent | f923099c4302e49a1563183c5217e1e5cf017d85 (diff) | |
download | gocapturedrefrace-b1b311caf11e9e88ef5735dc4e5512dcd4725059.tar.bz2 |
Fiddle with `pos` in `funcScope.LookupParent` for Go 1.22 breakage
First I tried changing `funcScope.LookupParent` to use the starting
brace ("{") of the function, but that didn't work for redeclarations:
$ go test -v
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:38:6: unexpected diagnostic: captured reference err in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:39:14: unexpected diagnostic: captured reference err in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:47:3: unexpected diagnostic: captured reference err in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:48:6: unexpected diagnostic: captured reference err in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:49:14: unexpected diagnostic: captured reference err in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:66:3: unexpected diagnostic: captured reference err1 in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:67:3: unexpected diagnostic: captured reference err2 in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:68:6: unexpected diagnostic: captured reference err1 in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:68:21: unexpected diagnostic: captured reference err2 in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:69:14: unexpected diagnostic: captured reference err1 in goroutine closure
analysistest.go:522: gocapturedrefrace/testdata/shadow.go:69:20: unexpected diagnostic: captured reference err2 in goroutine closure
--- FAIL: Test (0.43s)
The commit that breaks the analyzer says:
> Previously, its value was unset (NoPos), but the correct
> value is a point after the signature (FuncType.End) and
> before the body.
(https://github.com/golang/go/commit/a27a525d1b4df74989ac9f6ad10394391fe3eb88)
Recalling that, I tried setting the position to `token.NoPos`, which
fixes the analyser and allows the tests to pass.
Diffstat (limited to 'capturedrefrace.go')
-rw-r--r-- | capturedrefrace.go | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/capturedrefrace.go b/capturedrefrace.go index e155470..6b5b1df 100644 --- a/capturedrefrace.go +++ b/capturedrefrace.go @@ -56,7 +56,6 @@ import ( "go/token" "go/types" - "golang.org/x/exp/slices" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" @@ -199,8 +198,12 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { } } + // Idea: If ident is in funcLit signature, then short-circuit. + // Find out whether `ident` was defined in an outer scope. - scope, scopeObj := funcScope.LookupParent(ident.Name, ident.NamePos) + // scope, scopeObj := funcScope.LookupParent(ident.Name, ident.NamePos) + // scope, scopeObj := funcScope.LookupParent(ident.Name, funcLit.Body.Lbrace) + scope, scopeObj := funcScope.LookupParent(ident.Name, token.NoPos) // fmt.Printf("ident: %#v\n\t%#v\n\t%#v\n", ident, scope, scopeObj) // Identifier is local to the closure. @@ -229,11 +232,11 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { fmt.Println() } - za := slices.Index(funcScope.Names(), ident.Name) + // za := slices.Index(funcScope.Names(), ident.Name) // if za == -1 { // return true // } - zb := slices.Index(scope.Names(), ident.Name) + // zb := slices.Index(scope.Names(), ident.Name) // if zb == -1 { // return true // } @@ -242,16 +245,6 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { // Test with golang.org/x/tools@v0.15.0 // Identifier was defined in a different scope. // if funcScope != scope { - if za != -1 && zb != -1 && funcScope.Names()[za] == scope.Names()[zb] { - pass.Reportf( - ident.Pos(), - "captured reference %s in goroutine closure", - ident, - ) - - return true - } - if funcScope != scope { pass.Reportf( ident.Pos(), |