Skip to content

Commit

Permalink
cmd/compile: stenciled conversions might be NOPs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
randall77 committed Sep 9, 2021
1 parent a295b3c commit 19457a5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/cmd/compile/internal/noder/stencil.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,12 @@ func (subst *subster) node(n ir.Node) ir.Node {

case ir.OCONVIFACE:
x := x.(*ir.ConvExpr)
if m.Type().IsEmptyInterface() && m.(*ir.ConvExpr).X.Type().IsEmptyInterface() {
// Was T->interface{}, after stenciling it is now interface{}->interface{}.
// No longer need the conversion. See issue 48276.
m.(*ir.ConvExpr).SetOp(ir.OCONVNOP)
break
}
// Note: x's argument is still typed as a type parameter.
// m's argument now has an instantiated type.
if x.X.Type().HasTParam() || (x.X.Type().IsInterface() && x.Type().HasTParam()) {
Expand Down
19 changes: 19 additions & 0 deletions test/typeparam/issue48276a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run -gcflags=-G=3

// Copyright 2021 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

import "fmt"

func main() {
IsZero[interface{}]("")
}

func IsZero[T comparable](val T) bool {
var zero T
fmt.Printf("%v:%v\n", zero, val)
return val != zero
}
1 change: 1 addition & 0 deletions test/typeparam/issue48276a.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<nil>:
15 changes: 15 additions & 0 deletions test/typeparam/issue48276b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run -gcflags=-G=3

// Copyright 2021 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

func main() {
f[interface{}](nil)
}

func f[T any](x T) {
var _ interface{} = x
}

0 comments on commit 19457a5

Please sign in to comment.