cmd/compile: ignore struct tags when converting structs

Implementation of spec change https://golang.org/cl/24190/.

For #16085.

Change-Id: Id71ef29af5031b073e8be163f578d1bb768ff97a
Reviewed-on: https://go-review.googlesource.com/30169
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2016-10-03 11:40:43 -07:00
parent 9abaef93c7
commit 39055700b1
2 changed files with 332 additions and 12 deletions

View file

@ -644,7 +644,12 @@ func cplxsubtype(et EType) EType {
// pointer (t1 == t2), so there's no chance of chasing cycles // pointer (t1 == t2), so there's no chance of chasing cycles
// ad infinitum, so no need for a depth counter. // ad infinitum, so no need for a depth counter.
func eqtype(t1, t2 *Type) bool { func eqtype(t1, t2 *Type) bool {
return eqtype1(t1, t2, nil) return eqtype1(t1, t2, true, nil)
}
// eqtypeIgnoreTags is like eqtype but it ignores struct tags for struct identity.
func eqtypeIgnoreTags(t1, t2 *Type) bool {
return eqtype1(t1, t2, false, nil)
} }
type typePair struct { type typePair struct {
@ -652,7 +657,7 @@ type typePair struct {
t2 *Type t2 *Type
} }
func eqtype1(t1, t2 *Type, assumedEqual map[typePair]struct{}) bool { func eqtype1(t1, t2 *Type, cmpTags bool, assumedEqual map[typePair]struct{}) bool {
if t1 == t2 { if t1 == t2 {
return true return true
} }
@ -684,7 +689,7 @@ func eqtype1(t1, t2 *Type, assumedEqual map[typePair]struct{}) bool {
t1, i1 := iterFields(t1) t1, i1 := iterFields(t1)
t2, i2 := iterFields(t2) t2, i2 := iterFields(t2)
for ; t1 != nil && t2 != nil; t1, t2 = i1.Next(), i2.Next() { for ; t1 != nil && t2 != nil; t1, t2 = i1.Next(), i2.Next() {
if t1.Sym != t2.Sym || t1.Embedded != t2.Embedded || !eqtype1(t1.Type, t2.Type, assumedEqual) || t1.Note != t2.Note { if t1.Sym != t2.Sym || t1.Embedded != t2.Embedded || !eqtype1(t1.Type, t2.Type, cmpTags, assumedEqual) || cmpTags && t1.Note != t2.Note {
return false return false
} }
} }
@ -703,7 +708,7 @@ func eqtype1(t1, t2 *Type, assumedEqual map[typePair]struct{}) bool {
ta, ia := iterFields(f(t1)) ta, ia := iterFields(f(t1))
tb, ib := iterFields(f(t2)) tb, ib := iterFields(f(t2))
for ; ta != nil && tb != nil; ta, tb = ia.Next(), ib.Next() { for ; ta != nil && tb != nil; ta, tb = ia.Next(), ib.Next() {
if ta.Isddd != tb.Isddd || !eqtype1(ta.Type, tb.Type, assumedEqual) { if ta.Isddd != tb.Isddd || !eqtype1(ta.Type, tb.Type, cmpTags, assumedEqual) {
return false return false
} }
} }
@ -724,13 +729,13 @@ func eqtype1(t1, t2 *Type, assumedEqual map[typePair]struct{}) bool {
} }
case TMAP: case TMAP:
if !eqtype1(t1.Key(), t2.Key(), assumedEqual) { if !eqtype1(t1.Key(), t2.Key(), cmpTags, assumedEqual) {
return false return false
} }
return eqtype1(t1.Val(), t2.Val(), assumedEqual) return eqtype1(t1.Val(), t2.Val(), cmpTags, assumedEqual)
} }
return eqtype1(t1.Elem(), t2.Elem(), assumedEqual) return eqtype1(t1.Elem(), t2.Elem(), cmpTags, assumedEqual)
} }
// Are t1 and t2 equal struct types when field names are ignored? // Are t1 and t2 equal struct types when field names are ignored?
@ -906,15 +911,15 @@ func convertop(src *Type, dst *Type, why *string) Op {
*why = "" *why = ""
} }
// 2. src and dst have identical underlying types. // 2. Ignoring struct tags, src and dst have identical underlying types.
if eqtype(src.Orig, dst.Orig) { if eqtypeIgnoreTags(src.Orig, dst.Orig) {
return OCONVNOP return OCONVNOP
} }
// 3. src and dst are unnamed pointer types // 3. src and dst are unnamed pointer types and, ignoring struct tags,
// and their base types have identical underlying types. // their base types have identical underlying types.
if src.IsPtr() && dst.IsPtr() && src.Sym == nil && dst.Sym == nil { if src.IsPtr() && dst.IsPtr() && src.Sym == nil && dst.Sym == nil {
if eqtype(src.Elem().Orig, dst.Elem().Orig) { if eqtypeIgnoreTags(src.Elem().Orig, dst.Elem().Orig) {
return OCONVNOP return OCONVNOP
} }
} }

315
test/convert2.go Normal file
View file

@ -0,0 +1,315 @@
// errorcheck
// Copyright 2016 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.
// Test various valid and invalid struct assignments and conversions.
// Does not compile.
package main
type I interface {
m()
}
// conversions between structs
func _() {
type S struct{}
type T struct{}
var s S
var t T
var u struct{}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u
s = S(s)
s = S(t)
s = S(u)
t = u
t = T(u)
}
func _() {
type S struct{ x int }
type T struct {
x int "foo"
}
var s S
var t T
var u struct {
x int "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u)
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type E struct{ x int }
type S struct{ x E }
type T struct {
x E "foo"
}
var s S
var t T
var u struct {
x E "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u)
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type S struct {
x struct {
x int "foo"
}
}
type T struct {
x struct {
x int "bar"
} "foo"
}
var s S
var t T
var u struct {
x struct {
x int "bar"
} "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u)
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type E1 struct {
x int "foo"
}
type E2 struct {
x int "bar"
}
type S struct{ x E1 }
type T struct {
x E2 "foo"
}
var s S
var t T
var u struct {
x E2 "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t) // ERROR "cannot convert"
s = S(u) // ERROR "cannot convert"
t = u // ERROR "cannot use .* in assignment"
t = T(u)
}
func _() {
type E struct{ x int }
type S struct {
f func(struct {
x int "foo"
})
}
type T struct {
f func(struct {
x int "bar"
})
}
var s S
var t T
var u struct{ f func(E) }
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = S(s)
s = S(t)
s = S(u) // ERROR "cannot convert"
t = u // ERROR "cannot use .* in assignment"
t = T(u) // ERROR "cannot convert"
}
// conversions between pointers to structs
func _() {
type S struct{}
type T struct{}
var s *S
var t *T
var u *struct{}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type S struct{ x int }
type T struct {
x int "foo"
}
var s *S
var t *T
var u *struct {
x int "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type E struct{ x int }
type S struct{ x E }
type T struct {
x E "foo"
}
var s *S
var t *T
var u *struct {
x E "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type S struct {
x struct {
x int "foo"
}
}
type T struct {
x struct {
x int "bar"
} "foo"
}
var s *S
var t *T
var u *struct {
x struct {
x int "bar"
} "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u)
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type E1 struct {
x int "foo"
}
type E2 struct {
x int "bar"
}
type S struct{ x E1 }
type T struct {
x E2 "foo"
}
var s *S
var t *T
var u *struct {
x E2 "bar"
}
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t) // ERROR "cannot convert"
s = (*S)(u) // ERROR "cannot convert"
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u)
}
func _() {
type E struct{ x int }
type S struct {
f func(struct {
x int "foo"
})
}
type T struct {
f func(struct {
x int "bar"
})
}
var s *S
var t *T
var u *struct{ f func(E) }
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u) // ERROR "cannot convert"
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u) // ERROR "cannot convert"
}
func _() {
type E struct{ x int }
type S struct {
f func(*struct {
x int "foo"
})
}
type T struct {
f func(*struct {
x int "bar"
})
}
var s *S
var t *T
var u *struct{ f func(E) }
s = s
s = t // ERROR "cannot use .* in assignment"
s = u // ERROR "cannot use .* in assignment"
s = (*S)(s)
s = (*S)(t)
s = (*S)(u) // ERROR "cannot convert"
t = u // ERROR "cannot use .* in assignment"
t = (*T)(u) // ERROR "cannot convert"
}