Skip to content

Commit 137543b

Browse files
committed
cmd/compile: set IsShape based on type being in the Shapes pkg
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 <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Trust: Dan Scales <[email protected]>
1 parent 3a72175 commit 137543b

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/cmd/compile/internal/typecheck/subr.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1414,9 +1414,15 @@ func Shapify(t *types.Type) *types.Type {
14141414
return s
14151415
}
14161416

1417-
sym := shapePkg.Lookup(u.LinkString())
1417+
sym := types.ShapePkg.Lookup(u.LinkString())
1418+
if sym.Def != nil {
1419+
// Use any existing type with the same name
1420+
shaped[u] = sym.Def.Type()
1421+
return shaped[u]
1422+
}
14181423
name := ir.NewDeclNameAt(u.Pos(), ir.OTYPE, sym)
14191424
s := types.NewNamed(name)
1425+
sym.Def = name
14201426
s.SetUnderlying(u)
14211427
s.SetIsShape(true)
14221428
s.SetHasShape(true)
@@ -1427,5 +1433,3 @@ func Shapify(t *types.Type) *types.Type {
14271433
}
14281434

14291435
var shaped = map[*types.Type]*types.Type{}
1430-
1431-
var shapePkg = types.NewPkg(".shape", ".shape")

src/cmd/compile/internal/types/type.go

+6
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,10 @@ func NewNamed(obj TypeObject) *Type {
17061706
t := newType(TFORW)
17071707
t.sym = obj.Sym()
17081708
t.nod = obj
1709+
if t.sym.Pkg == ShapePkg {
1710+
t.SetIsShape(true)
1711+
t.SetHasShape(true)
1712+
}
17091713
return t
17101714
}
17111715

@@ -2182,3 +2186,5 @@ var (
21822186
)
21832187

21842188
var SimType [NTYPE]Kind
2189+
2190+
var ShapePkg = NewPkg(".shape", ".shape")

test/typeparam/issue48337b.dir/a.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type Container[T any] struct {
8+
X T
9+
}
10+
11+
func NewContainer[T any](x T) *Container[T] {
12+
return &Container[T]{x}
13+
}
14+
15+
type MetaContainer struct {
16+
C *Container[Value]
17+
}
18+
19+
type Value struct{}
20+
21+
func NewMetaContainer() *MetaContainer {
22+
c := NewContainer(Value{})
23+
// c := &Container[Value]{Value{}} // <-- this works
24+
return &MetaContainer{c}
25+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import "a"
8+
9+
func main() {
10+
a.NewMetaContainer()
11+
}

test/typeparam/issue48337b.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rundir -G=3
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

0 commit comments

Comments
 (0)