Skip to content

Commit 19457a5

Browse files
committed
cmd/compile: stenciled conversions might be NOPs
A generic conversion might be required for when converting T->interface{}. When stenciled with T=interface{}, then that conversion doesn't need to do anything. Fixes #48276 Change-Id: Ife65d01c99fbd0895cb7eec79df9e93e752b1fa5 Reviewed-on: https://go-review.googlesource.com/c/go/+/348736 Trust: Keith Randall <khr@golang.org> Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org>
1 parent a295b3c commit 19457a5

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/cmd/compile/internal/noder/stencil.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,12 @@ func (subst *subster) node(n ir.Node) ir.Node {
11771177

11781178
case ir.OCONVIFACE:
11791179
x := x.(*ir.ConvExpr)
1180+
if m.Type().IsEmptyInterface() && m.(*ir.ConvExpr).X.Type().IsEmptyInterface() {
1181+
// Was T->interface{}, after stenciling it is now interface{}->interface{}.
1182+
// No longer need the conversion. See issue 48276.
1183+
m.(*ir.ConvExpr).SetOp(ir.OCONVNOP)
1184+
break
1185+
}
11801186
// Note: x's argument is still typed as a type parameter.
11811187
// m's argument now has an instantiated type.
11821188
if x.X.Type().HasTParam() || (x.X.Type().IsInterface() && x.Type().HasTParam()) {

test/typeparam/issue48276a.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run -gcflags=-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 main
8+
9+
import "fmt"
10+
11+
func main() {
12+
IsZero[interface{}]("")
13+
}
14+
15+
func IsZero[T comparable](val T) bool {
16+
var zero T
17+
fmt.Printf("%v:%v\n", zero, val)
18+
return val != zero
19+
}

test/typeparam/issue48276a.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<nil>:

test/typeparam/issue48276b.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run -gcflags=-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 main
8+
9+
func main() {
10+
f[interface{}](nil)
11+
}
12+
13+
func f[T any](x T) {
14+
var _ interface{} = x
15+
}

0 commit comments

Comments
 (0)