reflect: fix test of whether structs are identical

The old code worked with gc, I assume because the linker
unified identical strings, but it failed with gccgo.

R=rsc
CC=gobot, golang-dev
https://golang.org/cl/6826063
This commit is contained in:
Ian Lance Taylor 2012-11-07 11:55:35 -08:00
parent 768ba46cc1
commit f8614a6645
4 changed files with 55 additions and 2 deletions

View file

@ -1257,8 +1257,19 @@ func haveIdenticalUnderlyingType(T, V *commonType) bool {
for i := range t.fields {
tf := &t.fields[i]
vf := &v.fields[i]
if tf.name != vf.name || tf.pkgPath != vf.pkgPath ||
tf.typ != vf.typ || tf.tag != vf.tag || tf.offset != vf.offset {
if tf.name != vf.name && (tf.name == nil || vf.name == nil || *tf.name != *vf.name) {
return false
}
if tf.pkgPath != vf.pkgPath && (tf.pkgPath == nil || vf.pkgPath == nil || *tf.pkgPath != *vf.pkgPath) {
return false
}
if tf.typ != vf.typ {
return false
}
if tf.tag != vf.tag && (tf.tag == nil || vf.tag == nil || *tf.tag != *vf.tag) {
return false
}
if tf.offset != vf.offset {
return false
}
}

View file

@ -0,0 +1,7 @@
// Copyright 2012 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 p1
type S struct { X, Y int }

View file

@ -0,0 +1,25 @@
// Copyright 2012 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 (
"reflect"
"./p1"
)
func main() {
var v1 = p1.S{1, 2}
var v2 = struct { X, Y int }{1, 2}
v1 = v2
t1 := reflect.TypeOf(v1)
t2 := reflect.TypeOf(v2)
if !t1.AssignableTo(t2) {
panic(0)
}
if !t2.AssignableTo(t1) {
panic(1)
}
}

10
test/fixedbugs/bug468.go Normal file
View file

@ -0,0 +1,10 @@
// rundir
// Copyright 2012 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.
// The reflect package was not correctly checking field names
// when checking for struct assignability.
package ignored