]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: fix generic type indirection
authorRobert Griesemer <gri@golang.org>
Sun, 11 Jul 2021 22:59:22 +0000 (15:59 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 14 Jul 2021 23:33:51 +0000 (23:33 +0000)
Change-Id: If25ceb2aa403b94608760be331faa2aff11c47cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/333890
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/expr.go
src/cmd/compile/internal/types2/testdata/examples/operations.go2 [new file with mode: 0644]

index d4425a2bfd5315c33f468739627337231fd32c19..008c2446fcdd18bcec1f3cf4397cda8b338b2c9e 100644 (file)
@@ -1479,13 +1479,24 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                                case typexpr:
                                        x.typ = &Pointer{base: x.typ}
                                default:
-                                       if typ := asPointer(x.typ); typ != nil {
-                                               x.mode = variable
-                                               x.typ = typ.base
-                                       } else {
-                                               check.errorf(x, invalidOp+"cannot indirect %s", x)
+                                       var base Type
+                                       if !underIs(x.typ, func(u Type) bool {
+                                               p, _ := u.(*Pointer)
+                                               if p == nil {
+                                                       check.errorf(x, invalidOp+"cannot indirect %s", x)
+                                                       return false
+                                               }
+                                               if base != nil && !Identical(p.base, base) {
+                                                       check.errorf(x, invalidOp+"pointers of %s must have identical base types", x)
+                                                       return false
+                                               }
+                                               base = p.base
+                                               return true
+                                       }) {
                                                goto Error
                                        }
+                                       x.mode = variable
+                                       x.typ = base
                                }
                                break
                        }
diff --git a/src/cmd/compile/internal/types2/testdata/examples/operations.go2 b/src/cmd/compile/internal/types2/testdata/examples/operations.go2
new file mode 100644 (file)
index 0000000..18e4d60
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2021 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
+
+// indirection
+
+func _[P any](p P) {
+        _ = *p // ERROR cannot indirect p
+}
+
+func _[P interface{ int }](p P) {
+        _ = *p // ERROR cannot indirect p
+}
+
+func _[P interface{ *int }](p P) {
+        _ = *p
+}
+
+func _[P interface{ *int | *string }](p P) {
+        _ = *p // ERROR must have identical base types
+}
+
+type intPtr *int
+
+func _[P interface{ *int | intPtr } ](p P) {
+        var _ int = *p
+}