Files
Tamir Duberstein c3dc444460 checklocks: fix generics Origin fact lookup
checklocks attaches lock facts to the declaration objects produced by
go/types, but SSA analysis of generic code can observe instantiated
`*types.Var/*types.Func` objects instead. Importing facts from those
instantiated objects fails, which causes spurious inference warnings
("may require checklocks annotation...") and can miss expected
checklocksfail reports.

Fix this by falling back to Origin() when importing lock guard and
function facts, and by normalizing inference observation keys to the
origin object so inference uses the same fact identity as export.

Add regression coverage for generic methods and functions, including a
cross-package generics.
2026-01-06 16:55:41 -05:00

36 lines
1022 B
Go

// Copyright 2022 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package crosspkg is a second package for testing.
package crosspkg
import (
"sync"
)
var (
// +checklocks:FooMu
Foo int
FooMu sync.Mutex
)
// GenericGuard is a generic type with a guarded field. This is used to verify
// that facts exported by this package are correctly imported when another
// package instantiates GenericGuard[T].
type GenericGuard[T any] struct {
Mu sync.Mutex
// +checklocks:Mu
Value T
}