]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: allow importing and exporting of ODYNAMICTYPE
authorKeith Randall <khr@golang.org>
Mon, 18 Oct 2021 17:59:29 +0000 (10:59 -0700)
committerKeith Randall <khr@golang.org>
Tue, 19 Oct 2021 22:47:48 +0000 (22:47 +0000)
Change-Id: I2fca7a801c85ed93c002c23bfcb0cf9593f1bdf4
Reviewed-on: https://go-review.googlesource.com/c/go/+/356571
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/iexport.go
src/cmd/compile/internal/typecheck/iimport.go
test/typeparam/issue49027.dir/a.go
test/typeparam/issue49027.dir/main.go

index e3dd10a56b17ae6ae68abaa721292cb6f9aa1e56..46865ba3fa29e1605d94d065323671b154e4be84 100644 (file)
@@ -1763,6 +1763,19 @@ func (w *exportWriter) expr(n ir.Node) {
                w.op(ir.OTYPE)
                w.typ(n.Type())
 
+       case ir.ODYNAMICTYPE:
+               n := n.(*ir.DynamicType)
+               w.op(ir.ODYNAMICTYPE)
+               w.pos(n.Pos())
+               w.expr(n.X)
+               if n.ITab != nil {
+                       w.bool(true)
+                       w.expr(n.ITab)
+               } else {
+                       w.bool(false)
+               }
+               w.typ(n.Type())
+
        case ir.OTYPESW:
                n := n.(*ir.TypeSwitchGuard)
                w.op(ir.OTYPESW)
index 6351fc37de1e4beea9fcb028220996de76c896fe..cb1e56bf51dbedb7aa3ad7710050dd91ae2216ee 100644 (file)
@@ -1312,6 +1312,14 @@ func (r *importReader) node() ir.Node {
        case ir.OTYPE:
                return ir.TypeNode(r.typ())
 
+       case ir.ODYNAMICTYPE:
+               n := ir.NewDynamicType(r.pos(), r.expr())
+               if r.bool() {
+                       n.ITab = r.expr()
+               }
+               n.SetType(r.typ())
+               return n
+
        case ir.OTYPESW:
                pos := r.pos()
                var tag *ir.Ident
index d3ec27deab5a2c77812a4e5ab6e3ecd00551c1a3..da88297965ec213caba577b318242dcec217fc6f 100644 (file)
@@ -15,7 +15,41 @@ func conv[T any](v interface{}) T {
 func Conv2(v interface{}) (string, bool) {
        return conv2[string](v)
 }
+
 func conv2[T any](v interface{}) (T, bool) {
        x, ok := v.(T)
        return x, ok
 }
+
+func Conv3(v interface{}) string {
+       return conv3[string](v)
+}
+
+func conv3[T any](v interface{}) T {
+       switch v := v.(type) {
+       case T:
+               return v
+       default:
+               var z T
+               return z
+       }
+}
+
+type Mystring string
+
+func (Mystring) Foo() {
+}
+
+func Conv4(v interface{Foo()}) Mystring {
+       return conv4[Mystring](v)
+}
+
+func conv4[T interface{Foo()}](v interface{Foo()}) T {
+       switch v := v.(type) {
+       case T:
+               return v
+       default:
+               var z T
+               return z
+       }
+}
index d0dc33d73430dc23968ce7ccbe0ddb2e9fe147ae..aa20a2fdfbcec6f2b91ec4e6b90d19e79aa9eb7a 100644 (file)
@@ -22,4 +22,12 @@ func main() {
        if y != s {
                panic(fmt.Sprintf("got %s wanted %s", y, s))
        }
+       z := a.Conv3(s)
+       if z != s {
+               panic(fmt.Sprintf("got %s wanted %s", z, s))
+       }
+       w := a.Conv4(a.Mystring(s))
+       if w != a.Mystring(s) {
+               panic(fmt.Sprintf("got %s wanted %s", w, s))
+       }
 }