Skip to content

Commit 2ff8934

Browse files
ianlancetaylorgopherbot
authored andcommitted
reflect: omit anonymous field name from StructOf type string
This makes the reflect package match the compiler for StructOf with an embedded field. Fixes #24781 Change-Id: Ice64b167cbe0b9d30a953c5d8e2a86f3ad1158bf Reviewed-on: https://go-review.googlesource.com/c/go/+/567897 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent f0d6ddf commit 2ff8934

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/reflect/all_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6112,6 +6112,20 @@ func TestStructOfTooLarge(t *testing.T) {
61126112
}
61136113
}
61146114

6115+
func TestStructOfAnonymous(t *testing.T) {
6116+
var s any = struct{ D1 }{}
6117+
f := TypeOf(s).Field(0)
6118+
ds := StructOf([]StructField{f})
6119+
st := TypeOf(s)
6120+
dt := New(ds).Elem()
6121+
if st != dt.Type() {
6122+
t.Errorf("StructOf returned %s, want %s", dt.Type(), st)
6123+
}
6124+
6125+
// This should not panic.
6126+
_ = dt.Interface().(struct{ D1 })
6127+
}
6128+
61156129
func TestChanOf(t *testing.T) {
61166130
// check construction and use of type not in binary
61176131
type T string

src/reflect/type.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,8 +2287,9 @@ func StructOf(fields []StructField) Type {
22872287
// Update string and hash
22882288
name := f.Name.Name()
22892289
hash = fnv1(hash, []byte(name)...)
2290-
repr = append(repr, (" " + name)...)
2291-
if f.Embedded() {
2290+
if !f.Embedded() {
2291+
repr = append(repr, (" " + name)...)
2292+
} else {
22922293
// Embedded field
22932294
if f.Typ.Kind() == abi.Pointer {
22942295
// Embedded ** and *interface{} are illegal

0 commit comments

Comments
 (0)