}
func (g *irgen) compLit(typ types2.Type, lit *syntax.CompositeLit) ir.Node {
- if ptr, ok := typ.Underlying().(*types2.Pointer); ok {
+ if ptr, ok := types2.StructuralType(typ).(*types2.Pointer); ok {
n := ir.NewAddrExpr(g.pos(lit), g.compLit(ptr.Elem(), lit))
n.SetOp(ir.OPTRLIT)
return typed(g.typ(typ), n)
w.typ(tv.Type)
typ := tv.Type
- // TODO(mdempsky): Use types2.StructuralType here too? See #50833.
- if ptr, ok := typ.Underlying().(*types2.Pointer); ok {
+ if ptr, ok := types2.StructuralType(typ).(*types2.Pointer); ok {
typ = ptr.Elem()
}
str, isStruct := types2.StructuralType(typ).(*types2.Struct)
case hint != nil:
// no composite literal type present - use hint (element type of enclosing type)
typ = hint
- base = typ
- if !isTypeParam(typ) {
- base = under(typ)
- }
- base, _ = deref(base) // *T implies &T{}
+ base, _ = deref(structuralType(typ)) // *T implies &T{}
default:
// TODO(gri) provide better error messages depending on context
--- /dev/null
+// Copyright 2022 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 (
+ S struct{ f int }
+ PS *S
+)
+
+func a() []*S { return []*S{{f: 1}} }
+func b() []PS { return []PS{{f: 1}} }
+
+func c[P *S]() []P { return []P{{f: 1}} }
+func d[P PS]() []P { return []P{{f: 1}} }
case hint != nil:
// no composite literal type present - use hint (element type of enclosing type)
typ = hint
- base = typ
- if !isTypeParam(typ) {
- base = under(typ)
- }
- base, _ = deref(base) // *T implies &T{}
+ base, _ = deref(structuralType(typ)) // *T implies &T{}
default:
// TODO(gri) provide better error messages depending on context
--- /dev/null
+// Copyright 2022 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 (
+ S struct{ f int }
+ PS *S
+)
+
+func a() []*S { return []*S{{f: 1}} }
+func b() []PS { return []PS{{f: 1}} }
+
+func c[P *S]() []P { return []P{{f: 1}} }
+func d[P PS]() []P { return []P{{f: 1}} }
--- /dev/null
+// run -gcflags=-G=3
+
+// Copyright 2022 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 (
+ S struct{ f int }
+ PS *S
+)
+
+func a() []*S { return []*S{{f: 1}} }
+func b() []PS { return []PS{{f: 1}} }
+
+func c[P *S]() []P { return []P{{f: 1}} }
+func d[P PS]() []P { return []P{{f: 1}} }
+
+func main() {
+ c[*S]()
+ d[PS]()
+}