]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: skip duplicate check in StructOf when the name of a field is "_"
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>
Fri, 22 Oct 2021 09:00:46 +0000 (17:00 +0800)
committerIan Lance Taylor <iant@golang.org>
Wed, 27 Oct 2021 21:35:48 +0000 (21:35 +0000)
Fixes #49110

Change-Id: I32c2cb26cca067a4a676ce4bbc3e51f1e0cdb259
Reviewed-on: https://go-review.googlesource.com/c/go/+/357959
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Dan Kortschak <dan@kortschak.io>
Reviewed-by: Sebastien Binet <s@sbinet.org>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>

src/reflect/type.go
test/fixedbugs/issue49110.go [new file with mode: 0644]

index 2bb2438381514a2e64b9cdbd813dafb0a158aa85..0896949d7e2ac24d494b7561c34b5357228c41a3 100644 (file)
@@ -2606,7 +2606,7 @@ func StructOf(fields []StructField) Type {
                                }
                        }
                }
-               if _, dup := fset[name]; dup {
+               if _, dup := fset[name]; dup && name != "_" {
                        panic("reflect.StructOf: duplicate field " + name)
                }
                fset[name] = struct{}{}
diff --git a/test/fixedbugs/issue49110.go b/test/fixedbugs/issue49110.go
new file mode 100644 (file)
index 0000000..5e1bde9
--- /dev/null
@@ -0,0 +1,16 @@
+// run
+
+// Copyright 2021 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"
+
+func main() {
+       _ = reflect.StructOf([]reflect.StructField{
+               {Name: "_", PkgPath: "main", Type: reflect.TypeOf(int(0))},
+               {Name: "_", PkgPath: "main", Type: reflect.TypeOf(int(0))},
+       })
+}