Skip to content

Commit 2a5864f

Browse files
spenczaradonovan
authored andcommitted
x/tools/go/ssa: Accept struct conversions that ignore tags
This is now allowed in go1.8. Fixes golang/go#19646. Change-Id: Iece4fd2a881144bdbe841e0a26ba4348d6b8828e Reviewed-on: https://go-review.googlesource.com/38452 Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Alan Donovan <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent bc6db94 commit 2a5864f

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

go/ssa/emit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func emitCompare(f *Function, op token.Token, x, y Value, pos token.Pos) Value {
147147
//
148148
func isValuePreserving(ut_src, ut_dst types.Type) bool {
149149
// Identical underlying types?
150-
if types.Identical(ut_dst, ut_src) {
150+
if structTypesIdentical(ut_dst, ut_src) {
151151
return true
152152
}
153153

go/ssa/identical.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build go1.8
2+
3+
package ssa
4+
5+
import "go/types"
6+
7+
var structTypesIdentical = types.IdenticalIgnoreTags

go/ssa/identical_17.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build !go1.8
2+
3+
package ssa
4+
5+
import "go/types"
6+
7+
var structTypesIdentical = types.Identical

go/ssa/identical_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//+build go1.8
2+
3+
package ssa_test
4+
5+
import "testing"
6+
7+
func TestValueForExprStructConv(t *testing.T) {
8+
testValueForExpr(t, "testdata/structconv.go")
9+
}

go/ssa/source_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,16 @@ func checkVarValue(t *testing.T, prog *ssa.Program, pkg *ssa.Package, ref []ast.
194194
// Ensure that, in debug mode, we can determine the ssa.Value
195195
// corresponding to every ast.Expr.
196196
func TestValueForExpr(t *testing.T) {
197+
testValueForExpr(t, "testdata/valueforexpr.go")
198+
}
199+
200+
func testValueForExpr(t *testing.T, testfile string) {
197201
if runtime.GOOS == "android" {
198202
t.Skipf("no testdata dir on %s", runtime.GOOS)
199203
}
200204

201205
conf := loader.Config{ParserMode: parser.ParseComments}
202-
f, err := conf.ParseFile("testdata/valueforexpr.go", nil)
206+
f, err := conf.ParseFile(testfile, nil)
203207
if err != nil {
204208
t.Error(err)
205209
return

go/ssa/testdata/structconv.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//+build ignore
2+
3+
// This file is the input to TestValueForExprStructConv in identical_test.go,
4+
// which uses the same framework as TestValueForExpr does in source_test.go.
5+
//
6+
// In Go 1.8, struct conversions are permitted even when the struct types have
7+
// different tags. This wasn't permitted in earlier versions of Go, so this file
8+
// exists separately from valueforexpr.go to just test this behavior in Go 1.8
9+
// and later.
10+
11+
package main
12+
13+
type t1 struct {
14+
x int
15+
}
16+
type t2 struct {
17+
x int `tag`
18+
}
19+
20+
func main() {
21+
var tv1 t1
22+
var tv2 t2 = /*@ChangeType*/ (t2(tv1))
23+
_ = tv2
24+
}

0 commit comments

Comments
 (0)