]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: unalias tilde terms in underIs
authorTim King <taking@google.com>
Thu, 15 Aug 2024 23:48:42 +0000 (16:48 -0700)
committerTim King <taking@google.com>
Tue, 20 Aug 2024 16:48:38 +0000 (16:48 +0000)
Unalias the ~T terms during underIs. Before, if T was an alias
of U, it may pass T to the iteration function. The iterator
function expects an underlying type, under(U), to be passed.
This caused several bugs where underIs is used without
eventually taking the underlying type.

Updates #68935
Fixes #68903

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

index 2577631a53542546bd49638d7cab8342aae9d0d1..01636ed379a090c2cbcf3543c1b7b0d1919acd59 100644 (file)
@@ -131,8 +131,8 @@ func (s *_TypeSet) underIs(f func(Type) bool) bool {
        }
        for _, t := range s.terms {
                assert(t.typ != nil)
-               // x == under(x) for ~x terms
-               u := t.typ
+               // Unalias(x) == under(x) for ~x terms
+               u := Unalias(t.typ)
                if !t.tilde {
                        u = under(u)
                }
index 5981c8ae62b36f1cf86e41ce742c0ed99c5012fc..a94d4fada460587be25b3e2f0e595a05be8a83bb 100644 (file)
@@ -134,8 +134,8 @@ func (s *_TypeSet) underIs(f func(Type) bool) bool {
        }
        for _, t := range s.terms {
                assert(t.typ != nil)
-               // x == under(x) for ~x terms
-               u := t.typ
+               // Unalias(x) == under(x) for ~x terms
+               u := Unalias(t.typ)
                if !t.tilde {
                        u = under(u)
                }
diff --git a/src/internal/types/testdata/fixedbugs/issue68903.go b/src/internal/types/testdata/fixedbugs/issue68903.go
new file mode 100644 (file)
index 0000000..b1369aa
--- /dev/null
@@ -0,0 +1,24 @@
+// 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 = [4]int
+type B = map[string]interface{}
+
+func _[T ~A](x T) {
+       _ = len(x)
+}
+
+func _[U ~A](x U) {
+       _ = cap(x)
+}
+
+func _[V ~A]() {
+       _ = V{}
+}
+
+func _[W ~B](a interface{}) {
+       _ = a.(W)["key"]
+}
diff --git a/src/internal/types/testdata/fixedbugs/issue68935.go b/src/internal/types/testdata/fixedbugs/issue68935.go
new file mode 100644 (file)
index 0000000..2e72468
--- /dev/null
@@ -0,0 +1,26 @@
+// 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 = struct {
+       F string
+       G int
+}
+
+func Make[T ~A]() T {
+       return T{
+               F: "blah",
+               G: 1234,
+       }
+}
+
+type N struct {
+       F string
+       G int
+}
+
+func _() {
+       _ = Make[N]()
+}