]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: add missing Unalias calls in type unifier
authorRobert Griesemer <gri@golang.org>
Tue, 11 Jun 2024 15:57:37 +0000 (08:57 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 11 Jun 2024 16:24:39 +0000 (16:24 +0000)
The unification code has "early exits" when the compared
types are pointer-identical.

Because of Alias nodes, we cannot simply compare x == y but we
must compare Unalias(x) == Unalias(y). Still, in the common case
there are no aliases, so as a minor optimization we write:

        x == y || Unalias(x) == Unalias(y)

to test whether x and y are (pointer-) identical.
Add the missing Unalias calls in the place where we forgot them.

Fixes #67872.

Change-Id: Ia26ffe7205b0417fc698287a4aeb1c900d30cc0d
Reviewed-on: https://go-review.googlesource.com/c/go/+/591975
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/unify.go
src/go/types/unify.go
src/internal/types/testdata/fixedbugs/issue67872.go [new file with mode: 0644]

index 8c91294d2b3b6b3420c4ea3327707ed65b739bdb..1c611a3e2a4c948d3e03b92d6398f379329fd1e5 100644 (file)
@@ -344,7 +344,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                // that is a type parameter.
                assert(!isTypeParam(y))
                // x and y may be identical now
-               if x == y {
+               if x == y || Unalias(x) == Unalias(y) {
                        return true
                }
        }
index d8f1b4a5b7b8036c92800a3da9e1b5c1e412664f..e4b50d7d4fecab10060782861e5363a8883653dd 100644 (file)
@@ -347,7 +347,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                // that is a type parameter.
                assert(!isTypeParam(y))
                // x and y may be identical now
-               if x == y {
+               if x == y || Unalias(x) == Unalias(y) {
                        return true
                }
        }
diff --git a/src/internal/types/testdata/fixedbugs/issue67872.go b/src/internal/types/testdata/fixedbugs/issue67872.go
new file mode 100644 (file)
index 0000000..3d96613
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2024 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 p
+
+type A = uint8
+type E uint8
+
+func f[P ~A](P) {}
+
+func g(e E) {
+       f(e)
+}