]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix recursive generic interface instantiation
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 31 Jan 2024 07:47:40 +0000 (14:47 +0700)
committerGopher Robot <gobot@golang.org>
Tue, 16 Jul 2024 16:43:30 +0000 (16:43 +0000)
When shapifying recursive instantiated types, the compiler ends up
leaving the type as-is if it already has been a shape type. However, if
both of type arguments are interfaces, and one of them is a recursive
one, it ends up being shaped as-is, while the other is shaped to its
underlying, causing mismatch in function signature.

Fixing this by shapifying an interface type as-is, if it is fully
instantiated and already been a shape type.

Fixes #65362
Fixes #66663

Change-Id: I839d266e0443b41238b1b7362aca09adc0177362
Reviewed-on: https://go-review.googlesource.com/c/go/+/559656
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/noder/reader.go
test/fixedbugs/issue65362.go [new file with mode: 0644]
test/fixedbugs/issue66663.go [new file with mode: 0644]

index 97865bbfb15d4518113983b8d47a0c81ba6a10b6..58fbb72f5d23f826d7dd7e0002c6f837ae2c6d49 100644 (file)
@@ -903,6 +903,11 @@ func shapify(targ *types.Type, basic bool) *types.Type {
                        base.Fatalf("%v is missing its underlying type", targ)
                }
        }
+       // For fully instantiated shape interface type, use it as-is. Otherwise, the instantiation
+       // involved recursive generic interface may cause mismatching in function signature, see issue #65362.
+       if targ.Kind() == types.TINTER && targ.IsFullyInstantiated() && targ.HasShape() {
+               return targ
+       }
 
        // When a pointer type is used to instantiate a type parameter
        // constrained by a basic interface, we know the pointer's element
diff --git a/test/fixedbugs/issue65362.go b/test/fixedbugs/issue65362.go
new file mode 100644 (file)
index 0000000..7c26775
--- /dev/null
@@ -0,0 +1,31 @@
+// compile
+
+// 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 main
+
+type Vector[V any] interface {
+       ReadVector[V]
+}
+
+type ReadVector[V any] interface {
+       Comparisons[ReadVector[V], Vector[V]]
+}
+
+type Comparisons[RV, V any] interface {
+       Diff(RV) V
+}
+
+type VectorImpl[V any] struct{}
+
+func (*VectorImpl[V]) Diff(ReadVector[V]) (_ Vector[V]) {
+       return
+}
+
+func main() {
+       var v1 VectorImpl[int]
+       var v2 Vector[int]
+       _ = v1.Diff(v2)
+}
diff --git a/test/fixedbugs/issue66663.go b/test/fixedbugs/issue66663.go
new file mode 100644 (file)
index 0000000..97a7a4e
--- /dev/null
@@ -0,0 +1,34 @@
+// compile
+
+// 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 Iterator[A any] func() (bool, A)
+
+type Range[A any] interface {
+       Blocks() Iterator[Block[A]]
+}
+
+type Block[A any] interface {
+       Range[A]
+}
+
+type rangeImpl[A any] struct{}
+
+func (r *rangeImpl[A]) Blocks() Iterator[Block[A]] {
+       return func() (bool, Block[A]) {
+               var a Block[A]
+               return false, a
+       }
+}
+
+func NewRange[A any]() Range[A] {
+       return &rangeImpl[A]{}
+}
+
+type AddrImpl struct{}
+
+var _ = NewRange[AddrImpl]()