From 8919434dde1e35f7b9ef589ecb80ec0833f021bc Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 12 Oct 2018 16:10:15 -0400 Subject: [PATCH] go/analysis/passes/copylock: add workaround for go1.10 CL 121876 made sync.noCopy implement sync.Locker and added this as an assumption to vet. But now that copylock is no longer in the standard library it cannot assume that it is analyzing a recent standard library in which noCopy has an Unlock method. Change-Id: I5a30b3711ae6cc0855eb246fdd93b1906779bdde Reviewed-on: https://go-review.googlesource.com/c/141683 Reviewed-by: Michael Matloob Run-TryBot: Michael Matloob TryBot-Result: Gobot Gobot --- go/analysis/analysistest/analysistest.go | 3 +++ go/analysis/passes/copylock/copylock.go | 9 +++++++++ go/analysis/passes/copylock/copylock_test.go | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 25062c68..ad979c14 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -263,6 +263,9 @@ func check(t Testing, gopath string, pass *analysis.Pass, diagnostics []analysis // Check the facts match expectations. // Report errors in lexical order for determinism. + // (It's only deterministic within each file, not across files, + // because go/packages does not guarantee file.Pos is ascending + // across the files of a single compilation unit.) var objects []types.Object for obj := range facts { objects = append(objects, obj) diff --git a/go/analysis/passes/copylock/copylock.go b/go/analysis/passes/copylock/copylock.go index e684cd37..c199c2ea 100644 --- a/go/analysis/passes/copylock/copylock.go +++ b/go/analysis/passes/copylock/copylock.go @@ -258,6 +258,15 @@ func lockPath(tpkg *types.Package, typ types.Type) typePath { return []types.Type{typ} } + // In go1.10, sync.noCopy did not implement Locker. + // (The Unlock method was added only in CL 121876.) + // TODO(adonovan): remove workaround when we drop go1.10. + if named, ok := typ.(*types.Named); ok && + named.Obj().Name() == "noCopy" && + named.Obj().Pkg().Path() == "sync" { + return []types.Type{typ} + } + nfields := styp.NumFields() for i := 0; i < nfields; i++ { ftyp := styp.Field(i).Type() diff --git a/go/analysis/passes/copylock/copylock_test.go b/go/analysis/passes/copylock/copylock_test.go index 3e4ca122..acbdb52a 100644 --- a/go/analysis/passes/copylock/copylock_test.go +++ b/go/analysis/passes/copylock/copylock_test.go @@ -7,7 +7,7 @@ import ( "golang.org/x/tools/go/analysis/passes/copylock" ) -func TestFromFileSystem(t *testing.T) { +func Test(t *testing.T) { testdata := analysistest.TestData() analysistest.Run(t, testdata, copylock.Analyzer, "a") }