mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
go/types: use a global atomic counter for type parameter ids
This is a 1:1 port of CL 309830 to go/types. Change-Id: Ibf709f8194dd5e93a87145e5f9db674ce93af529 Reviewed-on: https://go-review.googlesource.com/c/go/+/314594 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
168dd4e6aa
commit
07e006dd93
4 changed files with 25 additions and 4 deletions
|
|
@ -353,6 +353,7 @@ func TestTypesInfo(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
ResetId() // avoid renumbering of type parameter ids when adding tests
|
||||||
if strings.HasPrefix(test.src, genericPkg) && !typeparams.Enabled {
|
if strings.HasPrefix(test.src, genericPkg) && !typeparams.Enabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,6 @@ type Checker struct {
|
||||||
pkg *Package
|
pkg *Package
|
||||||
*Info
|
*Info
|
||||||
version version // accepted language version
|
version version // accepted language version
|
||||||
nextId uint64 // unique Id for type parameters (first valid Id is 1)
|
|
||||||
objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
|
objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
|
||||||
impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
|
impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
|
||||||
posMap map[*Interface][]token.Pos // maps interface types to lists of embedded interface positions
|
posMap map[*Interface][]token.Pos // maps interface types to lists of embedded interface positions
|
||||||
|
|
@ -191,7 +190,6 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
|
||||||
pkg: pkg,
|
pkg: pkg,
|
||||||
Info: info,
|
Info: info,
|
||||||
version: version,
|
version: version,
|
||||||
nextId: 1,
|
|
||||||
objMap: make(map[Object]*declInfo),
|
objMap: make(map[Object]*declInfo),
|
||||||
impMap: make(map[importKey]*Package),
|
impMap: make(map[importKey]*Package),
|
||||||
posMap: make(map[*Interface][]token.Pos),
|
posMap: make(map[*Interface][]token.Pos),
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ package types
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Type represents a type of Go.
|
// A Type represents a type of Go.
|
||||||
|
|
@ -715,6 +716,15 @@ func (t *Named) AddMethod(m *Func) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note: This is a uint32 rather than a uint64 because the
|
||||||
|
// respective 64 bit atomic instructions are not available
|
||||||
|
// on all platforms.
|
||||||
|
var lastId uint32
|
||||||
|
|
||||||
|
// nextId returns a value increasing monotonically by 1 with
|
||||||
|
// each call, starting with 1. It may be called concurrently.
|
||||||
|
func nextId() uint64 { return uint64(atomic.AddUint32(&lastId, 1)) }
|
||||||
|
|
||||||
// A _TypeParam represents a type parameter type.
|
// A _TypeParam represents a type parameter type.
|
||||||
type _TypeParam struct {
|
type _TypeParam struct {
|
||||||
check *Checker // for lazy type bound completion
|
check *Checker // for lazy type bound completion
|
||||||
|
|
@ -727,8 +737,7 @@ type _TypeParam struct {
|
||||||
// newTypeParam returns a new TypeParam.
|
// newTypeParam returns a new TypeParam.
|
||||||
func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *_TypeParam {
|
func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *_TypeParam {
|
||||||
assert(bound != nil)
|
assert(bound != nil)
|
||||||
typ := &_TypeParam{check: check, id: check.nextId, obj: obj, index: index, bound: bound}
|
typ := &_TypeParam{check: check, id: nextId(), obj: obj, index: index, bound: bound}
|
||||||
check.nextId++
|
|
||||||
if obj.typ == nil {
|
if obj.typ == nil {
|
||||||
obj.typ = typ
|
obj.typ = typ
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
src/go/types/types_test.go
Normal file
13
src/go/types/types_test.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
// Copyright 2021 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.
|
||||||
|
|
||||||
|
package types
|
||||||
|
|
||||||
|
import "sync/atomic"
|
||||||
|
|
||||||
|
// Upon calling ResetId, nextId starts with 1 again.
|
||||||
|
// It may be called concurrently. This is only needed
|
||||||
|
// for tests where we may want to have a consistent
|
||||||
|
// numbering for each individual test case.
|
||||||
|
func ResetId() { atomic.StoreUint32(&lastId, 0) }
|
||||||
Loading…
Add table
Add a link
Reference in a new issue