]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: make all pointer types have the same shape
authorKeith Randall <khr@golang.org>
Fri, 30 Jul 2021 21:00:27 +0000 (14:00 -0700)
committerKeith Randall <khr@golang.org>
Sat, 31 Jul 2021 17:03:07 +0000 (17:03 +0000)
Except unsafe.Pointer. It has a different Kind, which makes it trickier.

Change-Id: I12582afb6e591bea35da9e43ac8d141ed19532a3
Reviewed-on: https://go-review.googlesource.com/c/go/+/338749
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
src/cmd/compile/internal/typecheck/subr.go
src/cmd/compile/internal/types/identity.go
test/typeparam/shape1.go
test/typeparam/shape1.out

index e2f0a57e7133393984d11d4295bb13984e4abd50..5ee4152e1c97ffe31fc612c1eaed6f18e95f5da7 100644 (file)
@@ -1351,6 +1351,12 @@ func Shapify(t *types.Type) *types.Type {
        // Map all types with the same underlying type to the same shape.
        u := t.Underlying()
 
+       // All pointers have the same shape.
+       // TODO: Make unsafe.Pointer the same shape as normal pointers.
+       if u.Kind() == types.TPTR {
+               u = types.Types[types.TUINT8].PtrTo()
+       }
+
        if s := shaped[u]; s != nil {
                return s //TODO: keep?
        }
index 0a78092f0786c82b7d31d4201c80d20b4e508819..dc39acced866230aa6179d7d8d0f73127142225d 100644 (file)
@@ -31,7 +31,7 @@ func identical(t1, t2 *Type, cmpTags bool, assumedEqual map[typePair]struct{}) b
        if t1.sym != nil || t2.sym != nil {
                if t1.HasShape() || t2.HasShape() {
                        switch t1.kind {
-                       case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR, TCOMPLEX64, TCOMPLEX128, TFLOAT32, TFLOAT64, TBOOL, TSTRING, TUNSAFEPTR:
+                       case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR, TCOMPLEX64, TCOMPLEX128, TFLOAT32, TFLOAT64, TBOOL, TSTRING, TPTR, TUNSAFEPTR:
                                return true
                        }
                        // fall through to unnamed type comparison for complex types.
index 3c9e71ea63dbfc1c09e8a338872ab11d20c6607f..de1ea65ed227faa5da63301aad4c5a4573f43dcb 100644 (file)
@@ -10,7 +10,8 @@ type I interface {
        foo() int
 }
 
-// There should be a single instantiation of f in this program.
+// There should be one instantiation of f for both squarer and doubler.
+// Similarly, there should be one instantiation of f for both *incrementer and *decrementer.
 func f[T I](x T) int {
        return x.foo()
 }
@@ -27,7 +28,23 @@ func (x doubler) foo() int {
        return int(2*x)
 }
 
+type incrementer int16
+
+func (x *incrementer) foo() int {
+       return int(*x+1)
+}
+
+type decrementer int32
+
+func (x *decrementer) foo() int{
+       return int(*x-1)
+}
+
 func main() {
        println(f(squarer(5)))
        println(f(doubler(5)))
+       var i incrementer = 5
+       println(f(&i))
+       var d decrementer = 5
+       println(f(&d))
 }
index 28391fde6677dcd7bed4f1bfdd21ad2aa56a3c8b..da9a12ded55a5e58b371ee3a4e6c00be8eb0cdc7 100644 (file)
@@ -1,2 +1,4 @@
 25
 10
+6
+4