diff options
author | Robert Griesemer <gri@golang.org> | 2023-03-07 19:01:38 -0800 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-03-09 20:32:29 +0000 |
commit | 7042ea62da0f9d3f39e902352484ef30a746641b (patch) | |
tree | 739a32976df5620f7a2d906d400dc84d3496fd2d /src/cmd/compile/internal/types2/predicates.go | |
parent | 269bdcd56866d5cd5789164d3f7420a66c524a8a (diff) | |
download | go-7042ea62da0f9d3f39e902352484ef30a746641b.tar.gz go-7042ea62da0f9d3f39e902352484ef30a746641b.zip |
go/types, types2: clean up defined type identity check/unification
Factor out check for identical origin.
Match unification code with type identity check.
Add a test case for #53692.
Change-Id: I1238b28297a5ac549e99261c8a085dd46f3dd65f
Reviewed-on: https://go-review.googlesource.com/c/go/+/474197
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/predicates.go')
-rw-r--r-- | src/cmd/compile/internal/types2/predicates.go | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/src/cmd/compile/internal/types2/predicates.go b/src/cmd/compile/internal/types2/predicates.go index c92c1dc292..4f8441467e 100644 --- a/src/cmd/compile/internal/types2/predicates.go +++ b/src/cmd/compile/internal/types2/predicates.go @@ -433,33 +433,23 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool { case *Named: // Two named types are identical if their type names originate - // in the same type declaration. + // in the same type declaration; if they are instantiated they + // must have identical type argument lists. if y, ok := y.(*Named); ok { + // check type arguments before origins to match unifier + // (for correct source code we need to do all checks so + // order doesn't matter) xargs := x.TypeArgs().list() yargs := y.TypeArgs().list() - if len(xargs) != len(yargs) { return false } - - if len(xargs) > 0 { - // Instances are identical if their original type and type arguments - // are identical. - if !Identical(x.Origin(), y.Origin()) { + for i, xarg := range xargs { + if !Identical(xarg, yargs[i]) { return false } - for i, xa := range xargs { - if !Identical(xa, yargs[i]) { - return false - } - } - return true } - - // TODO(gri) Why is x == y not sufficient? And if it is, - // we can just return false here because x == y - // is caught in the very beginning of this function. - return x.obj == y.obj + return indenticalOrigin(x, y) } case *TypeParam: @@ -475,6 +465,12 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool { return false } +// identicalOrigin reports whether x and y originated in the same declaration. +func indenticalOrigin(x, y *Named) bool { + // TODO(gri) is this correct? + return x.Origin().obj == y.Origin().obj +} + // identicalInstance reports if two type instantiations are identical. // Instantiations are identical if their origin and type arguments are // identical. |