Skip to content

Commit 5c7a005

Browse files
committed
spec: ignore struct tags when converting structs
This is a backwards-compatible language change. Per the proposal (#16085), the rules for conversions are relaxed such that struct tags in any of the structs involved in the conversion are ignored (recursively). Because this is loosening the existing rules, code that compiled so far will continue to compile. For #16085. Fixes #6858. Change-Id: I0feef651582db5f23046a2331fc3f179ae577c45 Reviewed-on: https://go-review.googlesource.com/24190 Reviewed-by: Russ Cox <[email protected]>
1 parent 8c24bff commit 5c7a005

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

doc/go_spec.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of September 1, 2016",
3+
"Subtitle": "Version of October 4, 2016",
44
"Path": "/ref/spec"
55
}-->
66

@@ -3837,10 +3837,12 @@ <h3 id="Conversions">Conversions</h3>
38373837
to <code>T</code>.
38383838
</li>
38393839
<li>
3840-
<code>x</code>'s type and <code>T</code> have identical
3840+
ignoring struct tags (see below),
3841+
<code>x</code>'s type and <code>T</code> have <a href="#Type_identity">identical</a>
38413842
<a href="#Types">underlying types</a>.
38423843
</li>
38433844
<li>
3845+
ignoring struct tags (see below),
38443846
<code>x</code>'s type and <code>T</code> are unnamed pointer types
38453847
and their pointer base types have identical underlying types.
38463848
</li>
@@ -3860,6 +3862,31 @@ <h3 id="Conversions">Conversions</h3>
38603862
</li>
38613863
</ul>
38623864

3865+
<p>
3866+
<a href="#Struct_types">Struct tags</a> are ignored when comparing struct types
3867+
for identity for the purpose of conversion:
3868+
</p>
3869+
3870+
<pre>
3871+
type Person struct {
3872+
Name string
3873+
Address *struct {
3874+
Street string
3875+
City string
3876+
}
3877+
}
3878+
3879+
var data *struct {
3880+
Name string `json:"name"`
3881+
Address *struct {
3882+
Street string `json:"street"`
3883+
City string `json:"city"`
3884+
} `json:"address"`
3885+
}
3886+
3887+
var person = (*Person)(data) // ignoring tags, the underlying types are identical
3888+
</pre>
3889+
38633890
<p>
38643891
Specific rules apply to (non-constant) conversions between numeric types or
38653892
to and from a string type.

0 commit comments

Comments
 (0)