]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: omit anonymous field name from StructOf type string
authorIan Lance Taylor <iant@golang.org>
Sun, 3 Mar 2024 03:27:21 +0000 (19:27 -0800)
committerGopher Robot <gobot@golang.org>
Fri, 19 Apr 2024 17:15:47 +0000 (17:15 +0000)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/reflect/all_test.go
src/reflect/type.go

index f9b2ffd4f1de268c953cb5edcfe83e28d63a2e64..5df83bef7a3b0b8afe80d60b0174d7cc84fb1f44 100644 (file)
@@ -6112,6 +6112,20 @@ func TestStructOfTooLarge(t *testing.T) {
        }
 }
 
+func TestStructOfAnonymous(t *testing.T) {
+       var s any = struct{ D1 }{}
+       f := TypeOf(s).Field(0)
+       ds := StructOf([]StructField{f})
+       st := TypeOf(s)
+       dt := New(ds).Elem()
+       if st != dt.Type() {
+               t.Errorf("StructOf returned %s, want %s", dt.Type(), st)
+       }
+
+       // This should not panic.
+       _ = dt.Interface().(struct{ D1 })
+}
+
 func TestChanOf(t *testing.T) {
        // check construction and use of type not in binary
        type T string
index 272f0b87d11a1e9a324351204caf52af02f04411..cfefb4c27c61fdc7780069ea4b79fca69db4d87e 100644 (file)
@@ -2287,8 +2287,9 @@ func StructOf(fields []StructField) Type {
                // Update string and hash
                name := f.Name.Name()
                hash = fnv1(hash, []byte(name)...)
-               repr = append(repr, (" " + name)...)
-               if f.Embedded() {
+               if !f.Embedded() {
+                       repr = append(repr, (" " + name)...)
+               } else {
                        // Embedded field
                        if f.Typ.Kind() == abi.Pointer {
                                // Embedded ** and *interface{} are illegal