Skip to content

Commit 394a1ad

Browse files
committed
cmd/compile: allow importing and exporting of ODYANMICDOTTYPE[2]
Fixes #49027 Change-Id: I4520b5c754027bfffbc5cd92c9c27002b248c99a Reviewed-on: https://go-review.googlesource.com/c/go/+/356569 Trust: Keith Randall <[email protected]> Trust: Dan Scales <[email protected]> Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent 4d55072 commit 394a1ad

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,14 @@ func (w *exportWriter) expr(n ir.Node) {
18881888
w.expr(n.X)
18891889
w.typ(n.Type())
18901890

1891+
case ir.ODYNAMICDOTTYPE, ir.ODYNAMICDOTTYPE2:
1892+
n := n.(*ir.DynamicTypeAssertExpr)
1893+
w.op(n.Op())
1894+
w.pos(n.Pos())
1895+
w.expr(n.X)
1896+
w.expr(n.T)
1897+
w.typ(n.Type())
1898+
18911899
case ir.OINDEX, ir.OINDEXMAP:
18921900
n := n.(*ir.IndexExpr)
18931901
if go117ExportTypes {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,11 @@ func (r *importReader) node() ir.Node {
14571457
}
14581458
return n
14591459

1460+
case ir.ODYNAMICDOTTYPE, ir.ODYNAMICDOTTYPE2:
1461+
n := ir.NewDynamicTypeAssertExpr(r.pos(), op, r.expr(), r.expr())
1462+
n.SetType(r.typ())
1463+
return n
1464+
14601465
case ir.OINDEX, ir.OINDEXMAP:
14611466
n := ir.NewIndexExpr(r.pos(), r.expr(), r.expr())
14621467
if go117ExportTypes {

test/typeparam/issue49027.dir/a.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
func Conv(v interface{}) string {
8+
return conv[string](v)
9+
}
10+
11+
func conv[T any](v interface{}) T {
12+
return v.(T)
13+
}
14+
15+
func Conv2(v interface{}) (string, bool) {
16+
return conv2[string](v)
17+
}
18+
func conv2[T any](v interface{}) (T, bool) {
19+
x, ok := v.(T)
20+
return x, ok
21+
}

test/typeparam/issue49027.dir/main.go

Lines changed: 25 additions & 0 deletions
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 main
6+
7+
import (
8+
"a"
9+
"fmt"
10+
)
11+
12+
func main() {
13+
s := "foo"
14+
x := a.Conv(s)
15+
if x != s {
16+
panic(fmt.Sprintf("got %s wanted %s", x, s))
17+
}
18+
y, ok := a.Conv2(s)
19+
if !ok {
20+
panic("conversion failed")
21+
}
22+
if y != s {
23+
panic(fmt.Sprintf("got %s wanted %s", y, s))
24+
}
25+
}

test/typeparam/issue49027.go

Lines changed: 7 additions & 0 deletions
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)