Skip to content

Commit

Permalink
go/analysis/passes/copylock: suppress error in ill-typed code
Browse files Browse the repository at this point in the history
The copylock analyzer is marked RunDespiteErrors. In ill-typed
code such as S{T} where S and T are both types, the compiler will
warn that T is not an expression; the copylocks analyzer should
not additionally report a diagnostic as if T had been an expression
of type T.

The fix feels rather ad hoc. In general, analyzers marked
RunDespiteErrors are unlikely to be able to anticipate the
myriad ways that trees can be ill-formed and avoid spurious
diagnostics.

Also, add a main.go file for copylock.

Fixes golang/go#67787

Change-Id: I07afbed16a4138fe602c22ec42171b4a5e634286
Reviewed-on: https://go-review.googlesource.com/c/tools/+/589895
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
adonovan committed Jun 3, 2024
1 parent 018d3b2 commit 4478db0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go/analysis/passes/copylock/copylock.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ func lockPathRhs(pass *analysis.Pass, x ast.Expr) typePath {
return nil
}
}
return lockPath(pass.Pkg, pass.TypesInfo.Types[x].Type, nil)
if tv, ok := pass.TypesInfo.Types[x]; ok && tv.IsValue() {
return lockPath(pass.Pkg, tv.Type, nil)
}
return nil
}

// lockPath returns a typePath describing the location of a lock value
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/copylock/copylock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import (

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, copylock.Analyzer, "a", "typeparams")
analysistest.Run(t, testdata, copylock.Analyzer, "a", "typeparams", "issue67787")
}
16 changes: 16 additions & 0 deletions go/analysis/passes/copylock/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore

// The copylock command applies the golang.org/x/tools/go/analysis/passes/copylock
// analysis to the specified packages of Go source code.
package main

import (
"golang.org/x/tools/go/analysis/passes/copylock"
"golang.org/x/tools/go/analysis/singlechecker"
)

func main() { singlechecker.Main(copylock.Analyzer) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package issue67787

import "sync"

type T struct{ mu sync.Mutex }
type T1 struct{ t *T }

func NewT1() *T1 { return &T1{T} } // no analyzer diagnostic about T

0 comments on commit 4478db0

Please sign in to comment.