]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: use Checker-provided type parameter...
authorRobert Griesemer <gri@golang.org>
Thu, 6 May 2021 23:04:05 +0000 (16:04 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 14 May 2021 22:42:20 +0000 (22:42 +0000)
This is a port of https://golang.org/cl/317472.

For #46003.

Change-Id: Ie7b8880d43d459527b981ed4f60ee4d80a3cd17a
Reviewed-on: https://go-review.googlesource.com/c/go/+/320149
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/api_test.go
src/cmd/compile/internal/types2/check.go
src/cmd/compile/internal/types2/type.go
src/cmd/compile/internal/types2/types_test.go

index 873390c1e9280d6b961ad02592e52e16f39096c3..e1020a12194b0278436475eb9d6e4e52087350c2 100644 (file)
@@ -349,7 +349,6 @@ func TestTypesInfo(t *testing.T) {
        }
 
        for _, test := range tests {
-               ResetId() // avoid renumbering of type parameter ids when adding tests
                info := Info{Types: make(map[syntax.Expr]TypeAndValue)}
                var name string
                if strings.HasPrefix(test.src, brokenPkg) {
index 8d6cd1edab9d4c61cd1f1d5d7a99798def7fdcde..f80a918467e6cf81c8312c07f841e8346053b081 100644 (file)
@@ -83,6 +83,7 @@ type Checker struct {
        pkg  *Package
        *Info
        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
        impMap  map[importKey]*Package      // maps (import path, source directory) to (complete or fake) package
        posMap  map[*Interface][]syntax.Pos // maps interface types to lists of embedded interface positions
index 88dedbad4524c38b64aeb584ec7b42d86e0cbb37..cf119a1b23bdb6feae9b9ecf3dd2c7e48d3ba693 100644 (file)
@@ -732,11 +732,11 @@ 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
+var lastID uint32
 
-// nextId returns a value increasing monotonically by 1 with
+// 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)) }
+func nextID() uint64 { return uint64(atomic.AddUint32(&lastID, 1)) }
 
 // A TypeParam represents a type parameter type.
 type TypeParam struct {
@@ -753,7 +753,13 @@ func (t *TypeParam) Obj() *TypeName { return t.obj }
 // NewTypeParam returns a new TypeParam.
 func (check *Checker) NewTypeParam(obj *TypeName, index int, bound Type) *TypeParam {
        assert(bound != nil)
-       typ := &TypeParam{check: check, id: nextId(), obj: obj, index: index, bound: bound}
+       // Always increment lastID, even if it is not used.
+       id := nextID()
+       if check != nil {
+               check.nextID++
+               id = check.nextID
+       }
+       typ := &TypeParam{check: check, id: id, obj: obj, index: index, bound: bound}
        if obj.typ == nil {
                obj.typ = typ
        }
index 096402148d38ad481815ad916e00664dfd0419c2..11dca0b53de86421a3a34f5ae505d90dca61d3f2 100644 (file)
@@ -4,14 +4,6 @@
 
 package types2
 
-import "sync/atomic"
-
 func init() {
        acceptMethodTypeParams = true
 }
-
-// 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) }