Skip to content

Commit b674434

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
spec: clarify prose for embedded struct fields
The spec says that an embedded field must be specified as a type name (or a pointer to a type name). This is explicit in the prose and the FieldDecl syntax. However, the prose on promoted methods required a named type (originally the term used for a "defined type"). Before the introduction of alias types, type names could only refer to named/defined types, so the prose was ok. With the introduction of alias types in Go 1.9, we distinguished between defined types (i.e., types given a name through a type declaration) and type aliases (types given an alternative name), and retired the notion of a named type since any type with a name (alias type and defined type) could be considered a "named type". To make things worse, with Go 1.18 we re-introduced the notion of a named type which now includes predeclared types, defined types, type parameters (and with that type aliases denoting named types). In the process some of the wording on method promotion didn't get updated correctly. At attempt to fix this was made with CL 406054, but while that CL's description correctly explained the intent, the CL changed the prose from "defined type" to "named type" (which had the new meaning after Go 1.18), and thus did not fix the issue. This CL fixes that fix by using the term "type name". This makes the prose consistent for embedded types and in turn clarifies that methods of embedded alias types (defined or not) can be promoted, consistent with the implementation. While at it, also document that the type of an embedded field cannot be a type parameter. This restriction has been in place since the introduction of type parameters with Go 1.18 and is enforced by the compiler. Fixes #66540. For #41687. Change-Id: If9e6a03d7b84d24a3e6a5ceda1d46bda99bdf1f4 Reviewed-on: https://go-review.googlesource.com/c/go/+/603958 Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Axel Wagner <[email protected]>
1 parent 9819ac5 commit b674434

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

doc/go_spec.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Language version go1.23 (June 13, 2024)",
3+
"Subtitle": "Language version go1.23 (Aug 12, 2024)",
44
"Path": "/ref/spec"
55
}-->
66

@@ -1086,7 +1086,7 @@ <h3 id="Struct_types">Struct types</h3>
10861086
An embedded field must be specified as
10871087
a type name <code>T</code> or as a pointer to a non-interface type name <code>*T</code>,
10881088
and <code>T</code> itself may not be
1089-
a pointer type. The unqualified type name acts as the field name.
1089+
a pointer type or type parameter. The unqualified type name acts as the field name.
10901090
</p>
10911091

10921092
<pre>
@@ -1127,7 +1127,7 @@ <h3 id="Struct_types">Struct types</h3>
11271127
</p>
11281128

11291129
<p>
1130-
Given a struct type <code>S</code> and a <a href="#Types">named type</a>
1130+
Given a struct type <code>S</code> and a type name
11311131
<code>T</code>, promoted methods are included in the method set of the struct as follows:
11321132
</p>
11331133
<ul>

0 commit comments

Comments
 (0)