diff options
author | Robert Griesemer <gri@golang.org> | 2021-07-07 21:38:49 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-07-09 17:36:57 +0000 |
commit | 69d945fc6e80475c163f96ba86fe716e77bb0104 (patch) | |
tree | f299a8ca8176a672a539979c846d2951e0c1d1ad /src/cmd/compile/internal/types2/typestring.go | |
parent | 04acb8a7b9fc0212687cc25aa2598d12f6aceb74 (diff) | |
download | go-69d945fc6e80475c163f96ba86fe716e77bb0104.tar.gz go-69d945fc6e80475c163f96ba86fe716e77bb0104.zip |
[dev.typeparams] cmd/compile/internal/types2: use scope numbers to identify local types
Rather than using a local types' position information, use the type
name's scope numbers to uniquely identify the type from others with
the same name.
We use scope numbers rather than indices (with number-1 == index)
to preserve the invariant that the zero value for a scope is a ready
to use empty scope.
Using scope numbers turned out to be fairly simple after all and
provides a reasonably stable identification which will make debugging
simpler. A scope number series may be a bit longer than a unique ID for
each type name but local types should be reasonably rare.
Also did a minor cleanup in universe.go to ensure Named.orig is set up
correctly (there's still an open TODO but with a work-around).
Change-Id: I73935fa9bd960809fd5c95fe8b8a535c313cfc8f
Reviewed-on: https://go-review.googlesource.com/c/go/+/333192
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/types2/typestring.go')
-rw-r--r-- | src/cmd/compile/internal/types2/typestring.go | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/cmd/compile/internal/types2/typestring.go b/src/cmd/compile/internal/types2/typestring.go index f63a23c98c..44099133a0 100644 --- a/src/cmd/compile/internal/types2/typestring.go +++ b/src/cmd/compile/internal/types2/typestring.go @@ -363,22 +363,27 @@ func writeTypeName(buf *bytes.Buffer, obj *TypeName, qf Qualifier) { buf.WriteString(obj.name) if instanceHashing != 0 { - // For local defined types, use the (original!) TypeName's position - // to disambiguate. This is overkill, and could probably instead - // just be the pointer value (if we assume a non-moving GC) or - // a unique ID (like cmd/compile uses). But this works for now, - // and is convenient for debugging. - - // TODO(mdempsky): I still don't fully understand why typ.orig.orig - // can differ from typ.orig, or whether looping more than twice is - // ever necessary. + // For local defined types, use the (original!) TypeName's scope + // numbers to disambiguate. typ := obj.typ.(*Named) + // TODO(gri) Figure out why typ.orig != typ.orig.orig sometimes + // and whether the loop can iterate more than twice. + // (It seems somehow connected to instance types.) for typ.orig != typ { typ = typ.orig } - if orig := typ.obj; orig.pkg != nil && orig.parent != orig.pkg.scope { - fmt.Fprintf(buf, "@%q", orig.pos) - } + writeScopeNumbers(buf, typ.obj.parent) + } +} + +// writeScopeNumbers writes the number sequence for this scope to buf +// in the form ".i.j.k" where i, j, k, etc. stand for scope numbers. +// If a scope is nil or has no parent (such as a package scope), nothing +// is written. +func writeScopeNumbers(buf *bytes.Buffer, s *Scope) { + if s != nil && s.number > 0 { + writeScopeNumbers(buf, s.parent) + fmt.Fprintf(buf, ".%d", s.number) } } |