aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2024-03-10 16:26:09 +0100
committerTeddy Wing2024-03-10 16:26:09 +0100
commitb2f90a88f468f35a125a23bb85214c9348ea1481 (patch)
tree1fef8a7fe16402fb21776d1b7b775229f411bbf7
parent9aa2dfef38d08ba5a101a7c1b01fecbb1cec240d (diff)
parent1926d13444dbde1bcd221029ec44bd6bd30396a7 (diff)
downloadgocapturedrefrace-b2f90a88f468f35a125a23bb85214c9348ea1481.tar.bz2
Merge branch 'fix-analysis-for-Go-1.22'
-rw-r--r--capturedrefrace.go34
-rw-r--r--go.mod9
-rw-r--r--go.sum22
3 files changed, 54 insertions, 11 deletions
diff --git a/capturedrefrace.go b/capturedrefrace.go
index c0bc646..66b59e5 100644
--- a/capturedrefrace.go
+++ b/capturedrefrace.go
@@ -196,7 +196,39 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) {
}
// Find out whether `ident` was defined in an outer scope.
- scope, scopeObj := funcScope.LookupParent(ident.Name, ident.NamePos)
+ //
+ // Starting in Go 1.22.0, function scopes in the 'go/types' package
+ // have changed:
+ //
+ // > The start position (Pos) of the lexical environment block
+ // > (Scope) that represents a function body has changed: it used
+ // > to start at the opening curly brace of the function body, but
+ // > now starts at the function's func token.
+ //
+ // (https://go.dev/doc/go1.22#go/types)
+ //
+ // This was caused by the following commit, which corrected
+ // behaviour for gopls:
+ //
+ // https://github.com/golang/go/commit/a27a525d1b4df74989ac9f6ad10394391fe3eb88
+ //
+ // As a result, we can no longer use `ident.NamePos` as the
+ // position for `funcScope.LookupParent`. Doing so causes false
+ // positives for function arguments, incorrectly finding them to be
+ // captured references from the outer scope.
+ //
+ // The commit message of a27a525d1b4df74989ac9f6ad10394391fe3eb88
+ // states:
+ //
+ // > set correct Var.scopePos for parameters/results
+ // >
+ // > Previously, its value was unset (NoPos), but the correct
+ // > value is a point after the signature (FuncType.End) and
+ // > before the body.
+ //
+ // We can restore the previous behaviour by using `token.NoPos`
+ // instead of `ident.NamePos`.
+ scope, scopeObj := funcScope.LookupParent(ident.Name, token.NoPos)
// Identifier is local to the closure.
if scope == nil && scopeObj == nil {
diff --git a/go.mod b/go.mod
index 5d08fa1..3225857 100644
--- a/go.mod
+++ b/go.mod
@@ -2,9 +2,12 @@ module gopkg.teddywing.com/capturedrefrace
go 1.20
-require golang.org/x/tools v0.9.1
+require (
+ golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
+ golang.org/x/tools v0.19.0
+)
require (
- golang.org/x/mod v0.10.0 // indirect
- golang.org/x/sys v0.8.0 // indirect
+ golang.org/x/mod v0.16.0 // indirect
+ golang.org/x/sys v0.18.0 // indirect
)
diff --git a/go.sum b/go.sum
index cf10794..e31f719 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,15 @@
-golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
-golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
-golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
-golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
-golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
-golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
+golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
+golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
+golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
+golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
+golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
+golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
+golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
+golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
+golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
+golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
+golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
+golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=