tools/go
Daniel Martí 2de7f9bf82 go/analysis/passes/structtag: allow field tag shadowing
In the following piece of code:

	type T1 struct {
		Shadowed string `json:"foo"`
	}
	type T2 struct {
		T1
		Shadowing int `json:"foo"`
	}

encoding/json will encode T2 using T2.Shadowing, ignoring T2.Shadowed
entirely. This can be a useful feature to replace some of T1's fields
when encoding it. Moreover, this feature is already in use in the wild,
even though it's probably never been documented.

This started being a problem, as the structtag pass started walking
through embedded fields a few months ago. To keep it from complaining
about these useful shadowing cases, make it only see duplicate field tag
names if they are at the same embedding level, in which case no
shadowing is happening.

The old code indexed these tags by encoding key and name, using a
[2]string. The new code needs to add a level integer, so start declaring
named types for the map, and use methods to simplify the code further
below. We still use a map pointer, to avoid allocating on every single
struct definition.

Updates golang/go#30846.

Change-Id: Iae53228d4f8bd91584c59dcc982cb1300970bc8f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179360
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2019-06-02 11:28:58 +00:00
..
analysis go/analysis/passes/structtag: allow field tag shadowing 2019-06-02 11:28:58 +00:00
ast go/ast/inspector: fix a small typo 2019-01-10 14:16:07 +00:00
buildutil go/buildutil: get tests to pass when run from a module 2019-02-14 18:55:18 +00:00
callgraph go/...: use recommended issue tracker URLs 2018-12-05 01:41:16 +00:00
cfg go/cfg: a syntactic control-flow graph (CFG) 2018-09-26 00:57:46 +00:00
expect go/expect: rewrite the expectation parser 2018-11-30 19:57:46 +00:00
gccgoexportdata go/gccgoexportdata: correctly handle archive files containing string tables 2016-11-09 21:28:38 +00:00
gcexportdata go/gcexportdata: fix example test for changes in net/rpc 2019-04-16 18:06:57 +00:00
internal go/internal/gccgoimporter: update package to match std lib version 2019-04-25 00:10:55 +00:00
loader go/...: use recommended issue tracker URLs 2018-12-05 01:41:16 +00:00
packages go/packages: update documentation for Package.Types and Package.Syntax 2019-05-31 17:21:33 +00:00
pointer go/...: use recommended issue tracker URLs 2018-12-05 01:41:16 +00:00
ssa go/ssa/interp: make tests fast and robust 2019-03-22 17:29:27 +00:00
types go/analysis/passes/printf: changes for analysis API 2018-10-16 19:44:49 +00:00
vcs go/vcs: ignore "mod" VCS type 2019-05-06 14:53:03 +00:00