From a3c4ac0fbca51fc2e3369c93738419fc947bda77 Mon Sep 17 00:00:00 2001 From: zhouguangyuan Date: Fri, 22 Oct 2021 17:00:46 +0800 Subject: [PATCH] reflect: skip duplicate check in StructOf when the name of a field is "_" Fixes #49110 Change-Id: I32c2cb26cca067a4a676ce4bbc3e51f1e0cdb259 Reviewed-on: https://go-review.googlesource.com/c/go/+/357959 Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Reviewed-by: Dan Kortschak Reviewed-by: Sebastien Binet Trust: Cuong Manh Le --- src/reflect/type.go | 2 +- test/fixedbugs/issue49110.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/fixedbugs/issue49110.go diff --git a/src/reflect/type.go b/src/reflect/type.go index 2bb2438381..0896949d7e 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -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 index 0000000000..5e1bde9f00 --- /dev/null +++ b/test/fixedbugs/issue49110.go @@ -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))}, + }) +} -- 2.50.0