]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: set IsShape based on type being in the Shapes pkg
authorDan Scales <danscales@google.com>
Tue, 14 Sep 2021 15:39:08 +0000 (08:39 -0700)
committerDan Scales <danscales@google.com>
Tue, 14 Sep 2021 23:07:15 +0000 (23:07 +0000)
Move ShapePkg to types, and change types.NewNamed to automatically set
IsShape/HasShape if a type is in the shapes pkg. This means that
imported shape types will automatically have the correct
IsShape/HasShape flags, even though we are not explicitly
exporting/importing those flags.

Updates #48337

Change-Id: I8b6131a663205f73f395943c9d0c8bdb2a213401
Reviewed-on: https://go-review.googlesource.com/c/go/+/349869
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>

src/cmd/compile/internal/typecheck/subr.go
src/cmd/compile/internal/types/type.go
test/typeparam/issue48337b.dir/a.go [new file with mode: 0644]
test/typeparam/issue48337b.dir/main.go [new file with mode: 0644]
test/typeparam/issue48337b.go [new file with mode: 0644]

index 5323872eafdf8b10711b70702ed3e6084b6f6e6b..5854e3c458319820e42824eaa0c654198940b542 100644 (file)
@@ -1414,9 +1414,15 @@ func Shapify(t *types.Type) *types.Type {
                return s
        }
 
-       sym := shapePkg.Lookup(u.LinkString())
+       sym := types.ShapePkg.Lookup(u.LinkString())
+       if sym.Def != nil {
+               // Use any existing type with the same name
+               shaped[u] = sym.Def.Type()
+               return shaped[u]
+       }
        name := ir.NewDeclNameAt(u.Pos(), ir.OTYPE, sym)
        s := types.NewNamed(name)
+       sym.Def = name
        s.SetUnderlying(u)
        s.SetIsShape(true)
        s.SetHasShape(true)
@@ -1427,5 +1433,3 @@ func Shapify(t *types.Type) *types.Type {
 }
 
 var shaped = map[*types.Type]*types.Type{}
-
-var shapePkg = types.NewPkg(".shape", ".shape")
index eb70f7b9b452a6ef61f30cf3cc05d596b28e38d1..392c54ba790f6bae8adc3fa0b73acc12ad231b37 100644 (file)
@@ -1706,6 +1706,10 @@ func NewNamed(obj TypeObject) *Type {
        t := newType(TFORW)
        t.sym = obj.Sym()
        t.nod = obj
+       if t.sym.Pkg == ShapePkg {
+               t.SetIsShape(true)
+               t.SetHasShape(true)
+       }
        return t
 }
 
@@ -2182,3 +2186,5 @@ var (
 )
 
 var SimType [NTYPE]Kind
+
+var ShapePkg = NewPkg(".shape", ".shape")
diff --git a/test/typeparam/issue48337b.dir/a.go b/test/typeparam/issue48337b.dir/a.go
new file mode 100644 (file)
index 0000000..a3c2e88
--- /dev/null
@@ -0,0 +1,25 @@
+// 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 a
+
+type Container[T any] struct {
+       X T
+}
+
+func NewContainer[T any](x T) *Container[T] {
+       return &Container[T]{x}
+}
+
+type MetaContainer struct {
+       C *Container[Value]
+}
+
+type Value struct{}
+
+func NewMetaContainer() *MetaContainer {
+       c := NewContainer(Value{})
+       // c := &Container[Value]{Value{}} // <-- this works
+       return &MetaContainer{c}
+}
diff --git a/test/typeparam/issue48337b.dir/main.go b/test/typeparam/issue48337b.dir/main.go
new file mode 100644 (file)
index 0000000..0b2814c
--- /dev/null
@@ -0,0 +1,11 @@
+// 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 main
+
+import "a"
+
+func main() {
+       a.NewMetaContainer()
+}
diff --git a/test/typeparam/issue48337b.go b/test/typeparam/issue48337b.go
new file mode 100644 (file)
index 0000000..76930e5
--- /dev/null
@@ -0,0 +1,7 @@
+// rundir -G=3
+
+// 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 ignored