Skip to content

Commit 46f352d

Browse files
committed
spec: remove notion of specific types
Specific types were introduced to explain rules for operands of type parameter type. Specific types are really an implementation mechanism to represent (possibly infinite) type sets in the machine; they are not needed in the specification. A specific type is either standing for a single named or unnamed type, or it is the underlying (unnamed) type of an infinite set of types. Each rule that applies to a type T of the set of specific types must also apply to all types T' in the type set for which T is a representative of. Thus, in the spec we can simply refer to the type set directly, infinite or not. Rather then excluding operands with empty type sets in each instance, leave unspecified what happens when such an operand is used. Instead give an implementation some leeway with an implementation restriction. (The implementation restriction also needs to be formulated for types, such as in conversions, which technically are not "operands". Left for another CL.) Minor: Remove the two uses of the word "concrete" to refer to non- interface types; instead just say "non-interface type" for clarity. Change-Id: I67ac89a640c995369c9d421a03820a0c0435835a Reviewed-on: https://go-review.googlesource.com/c/go/+/390694 Trust: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 604140d commit 46f352d

File tree

1 file changed

+38
-100
lines changed

1 file changed

+38
-100
lines changed

doc/go_spec.html

Lines changed: 38 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification - Go 1.18 Draft",
3-
"Subtitle": "Version of March 7, 2022",
3+
"Subtitle": "Version of March 9, 2022",
44
"Path": "/ref/spec"
55
}-->
66

@@ -761,7 +761,7 @@ <h2 id="Variables">Variables</h2>
761761
<code>new</code> call or composite literal, or the type of
762762
an element of a structured variable.
763763
Variables of interface type also have a distinct <i>dynamic type</i>,
764-
which is the concrete type of the value assigned to the variable at run time
764+
which is the (non-interface) type of the value assigned to the variable at run time
765765
(unless the value is the predeclared identifier <code>nil</code>,
766766
which has no type).
767767
The dynamic type may vary during execution but values stored in interface
@@ -1799,70 +1799,6 @@ <h3 id="Core_types">Core types</h3>
17991799
interface{ &lt;-chan int | chan&lt;- int } // directional channels have different directions
18001800
</pre>
18011801

1802-
<h3 id="Specific_types">Specific types</h3>
1803-
1804-
<p><b>
1805-
[The definition of specific types is not quite correct yet.]
1806-
</b></p>
1807-
1808-
<p>
1809-
An interface specification that contains <a href="#Interface_types">type elements</a>
1810-
defines a (possibly empty) set of <i>specific types</i>.
1811-
Loosely speaking, these are the types <code>T</code> that appear in the
1812-
interface definition in terms of the form <code>T</code>, <code>~T</code>,
1813-
or in unions of such terms.
1814-
</p>
1815-
1816-
<p>
1817-
More precisely, for a given interface, the set of specific types corresponds to
1818-
the set 𝑅 of representative types of the interface, if 𝑅 is non-empty and finite.
1819-
Otherwise, if 𝑅 is empty or infinite, the interface has <i>no specific types</i>.
1820-
</p>
1821-
1822-
<p>
1823-
For a given interface, type element or type term, the set 𝑅 of representative types is defined as follows:
1824-
</p>
1825-
1826-
<ul>
1827-
<li>For an interface with no type elements, 𝑅 is the (infinite) set of all types.
1828-
</li>
1829-
1830-
<li>For an interface with type elements,
1831-
𝑅 is the intersection of the representative types of its type elements.
1832-
</li>
1833-
1834-
<li>For a non-interface type term <code>T</code> or a term of the form <code>~T</code>,
1835-
𝑅 is the set consisting of the type <code>T</code>.
1836-
</li>
1837-
1838-
<li>For a <i>union</i> of terms
1839-
<code>t<sub>1</sub>|t<sub>2</sub>|…|t<sub>n</sub></code>,
1840-
𝑅 is the union of the representative types of the terms.
1841-
</li>
1842-
</ul>
1843-
1844-
<p>
1845-
An interface may have specific types even if its <a href="#Interface_types">type set</a>
1846-
is empty.
1847-
</p>
1848-
1849-
<p>
1850-
Examples of interfaces with their specific types:
1851-
</p>
1852-
1853-
<pre>
1854-
interface{} // no specific types
1855-
interface{ int } // int
1856-
interface{ ~string } // string
1857-
interface{ int|~string } // int, string
1858-
interface{ Celsius|Kelvin } // Celsius, Kelvin
1859-
interface{ float64|any } // no specific types (union is all types)
1860-
interface{ int; m() } // int (but type set is empty because int has no method m)
1861-
interface{ ~int; m() } // int (but type set is infinite because many integer types have a method m)
1862-
interface{ int; any } // int
1863-
interface{ int; string } // no specific types (intersection is empty)
1864-
</pre>
1865-
18661802
<h3 id="Type_identity">Type identity</h3>
18671803

18681804
<p>
@@ -2002,25 +1938,24 @@ <h3 id="Assignability">Assignability</h3>
20021938
</ul>
20031939

20041940
<p>
2005-
Additionally, if <code>x</code>'s type <code>V</code> or <code>T</code> are type parameters
2006-
with <a href="#Specific_types">specific types</a>, <code>x</code>
1941+
Additionally, if <code>x</code>'s type <code>V</code> or <code>T</code> are type parameters, <code>x</code>
20071942
is assignable to a variable of type <code>T</code> if one of the following conditions applies:
20081943
</p>
20091944

20101945
<ul>
20111946
<li>
20121947
<code>x</code> is the predeclared identifier <code>nil</code>, <code>T</code> is
2013-
a type parameter, and <code>x</code> is assignable to each specific type of
2014-
<code>T</code>.
1948+
a type parameter, and <code>x</code> is assignable to each type in
1949+
<code>T</code>'s type set.
20151950
</li>
20161951
<li>
20171952
<code>V</code> is not a <a href="#Types">named type</a>, <code>T</code> is
2018-
a type parameter, and <code>x</code> is assignable to each specific type of
2019-
<code>T</code>.
1953+
a type parameter, and <code>x</code> is assignable to each type in
1954+
<code>T</code>'s type set.
20201955
</li>
20211956
<li>
20221957
<code>V</code> is a type parameter and <code>T</code> is not a named type,
2023-
and values of each specific type of <code>V</code> are assignable
1958+
and values of each type in <code>V</code>'s type set are assignable
20241959
to <code>T</code>.
20251960
</li>
20261961
</ul>
@@ -2055,9 +1990,9 @@ <h3 id="Representability">Representability</h3>
20551990
</ul>
20561991

20571992
<p>
2058-
If <code>T</code> is a type parameter with <a href="#Specific_types">specific types</a>,
1993+
If <code>T</code> is a type parameter,
20591994
<code>x</code> is representable by a value of type <code>T</code> if <code>x</code> is representable
2060-
by a value of each specific type of <code>T</code>.
1995+
by a value of each type in <code>T</code>'s type set.
20611996
</p>
20621997

20631998
<pre>
@@ -2705,7 +2640,7 @@ <h4 id="Type_constraints">Type constraints</h4>
27052640
<p>
27062641
The <a href="#Predeclared_identifiers">predeclared</a>
27072642
<a href="#Interface_types">interface type</a> <code>comparable</code>
2708-
denotes the set of all concrete (non-interface) types that are
2643+
denotes the set of all non-interface types that are
27092644
<a href="#Comparison_operators">comparable</a>. Specifically,
27102645
a type <code>T</code> implements <code>comparable</code> if:
27112646
</p>
@@ -3037,6 +2972,14 @@ <h3 id="Operands">Operands</h3>
30372972
operand only on the left-hand side of an <a href="#Assignments">assignment</a>.
30382973
</p>
30392974

2975+
<p>
2976+
Implementation restriction: A compiler need not report an error if an operand's
2977+
type is a <a href="#Type_parameter_lists">type parameter</a> with an empty
2978+
<a href="#Interface_types">type set</a>. Functions with such type parameters
2979+
cannot be <a href="#Instantiations">instantiated</a>; any attempt will lead
2980+
to an error at the instantiation site.
2981+
</p>
2982+
30402983
<h3 id="Qualified_identifiers">Qualified identifiers</h3>
30412984

30422985
<p>
@@ -3819,20 +3762,19 @@ <h3 id="Index_expressions">Index expressions</h3>
38193762
For <code>a</code> of <a href="#Type_parameter_lists">type parameter type</a> <code>P</code>:
38203763
</p>
38213764
<ul>
3822-
<li><code>P</code> must have <a href="#Specific_types">specific types</a>.</li>
38233765
<li>The index expression <code>a[x]</code> must be valid for values
3824-
of all specific types of <code>P</code>.</li>
3825-
<li>The element types of all specific types of <code>P</code> must be identical.
3766+
of all types in <code>P</code>'s type set.</li>
3767+
<li>The element types of all types in <code>P</code>'s type set must be identical.
38263768
In this context, the element type of a string type is <code>byte</code>.</li>
3827-
<li>If there is a map type among the specific types of <code>P</code>,
3828-
all specific types must be map types, and the respective key types
3769+
<li>If there is a map type in the type set of <code>P</code>,
3770+
all types in that type set must be map types, and the respective key types
38293771
must be all identical.</li>
38303772
<li><code>a[x]</code> is the array, slice, or string element at index <code>x</code>,
38313773
or the map element with key <code>x</code> of the type argument
38323774
that <code>P</code> is instantiated with, and the type of <code>a[x]</code> is
38333775
the type of the (identical) element types.</li>
3834-
<li><code>a[x]</code> may not be assigned to if the specific types of <code>P</code>
3835-
include string types.
3776+
<li><code>a[x]</code> may not be assigned to if <code>P</code>'s type set
3777+
includes string types.
38363778
</ul>
38373779

38383780
<p>
@@ -4728,6 +4670,10 @@ <h3 id="Operators">Operators</h3>
47284670
to the type of the other operand.
47294671
</p>
47304672

4673+
<p><b>
4674+
[The rules for shifts need adjustments for type parameters. Issue #51182.]
4675+
</b></p>
4676+
47314677
<p>
47324678
The right operand in a shift expression must have <a href="#Numeric_types">integer type</a>
47334679
or be an untyped constant <a href="#Representability">representable</a> by a
@@ -4832,9 +4778,8 @@ <h3 id="Arithmetic_operators">Arithmetic operators</h3>
48324778
</pre>
48334779

48344780
<p>
4835-
Excluding shifts, if the operand type is a <a href="#Type_parameter_lists">type parameter</a>,
4836-
it must have <a href="#Specific_types">specific types</a>, and the operator must
4837-
apply to each specific type.
4781+
If the operand type is a <a href="#Type_parameter_lists">type parameter</a>,
4782+
the operator must apply to each type in that type set.
48384783
The operands are represented as values of the type argument that the type parameter
48394784
is <a href="#Instantiations">instantiated</a> with, and the operation is computed
48404785
with the precision of that type argument. For example, given the function:
@@ -4857,11 +4802,6 @@ <h3 id="Arithmetic_operators">Arithmetic operators</h3>
48574802
respectively, depending on the type argument for <code>F</code>.
48584803
</p>
48594804

4860-
<p>
4861-
For shifts, the <a href="#Core_types">core type</a> of both operands must be
4862-
an integer.
4863-
</p>
4864-
48654805
<h4 id="Integer_operators">Integer operators</h4>
48664806

48674807
<p>
@@ -5374,23 +5314,23 @@ <h3 id="Conversions">Conversions</h3>
53745314

53755315
<p>
53765316
Additionally, if <code>T</code> or <code>x</code>'s type <code>V</code> are type
5377-
parameters with <a href="#Specific_types">specific types</a>, <code>x</code>
5317+
parameters, <code>x</code>
53785318
can also be converted to type <code>T</code> if one of the following conditions applies:
53795319
</p>
53805320

53815321
<ul>
53825322
<li>
53835323
Both <code>V</code> and <code>T</code> are type parameters and a value of each
5384-
specific type of <code>V</code> can be converted to each specific type
5385-
of <code>T</code>.
5324+
type in <code>V</code>'s type set can be converted to each type in <code>T</code>'s
5325+
type set.
53865326
</li>
53875327
<li>
53885328
Only <code>V</code> is a type parameter and a value of each
5389-
specific type of <code>V</code> can be converted to <code>T</code>.
5329+
type in <code>V</code>'s type set can be converted to <code>T</code>.
53905330
</li>
53915331
<li>
53925332
Only <code>T</code> is a type parameter and <code>x</code> can be converted to each
5393-
specific type of <code>T</code>.
5333+
type in <code>T</code>'s type set.
53945334
</li>
53955335
</ul>
53965336

@@ -7085,9 +7025,8 @@ <h3 id="Length_and_capacity">Length and capacity</h3>
70857025

70867026
<p>
70877027
If the argument type is a <a href="#Type_parameter_lists">type parameter</a> <code>P</code>,
7088-
<code>P</code> must have <a href="#Specific_types">specific types</a>, and
70897028
the call <code>len(e)</code> (or <code>cap(e)</code> respectively) must be valid for
7090-
each specific type of <code>P</code>.
7029+
each type in <code>P</code>'s type set.
70917030
The result is the length (or capacity, respectively) of the argument whose type
70927031
corresponds to the type argument with which <code>P</code> was
70937032
<a href="#Instantiations">instantiated</a>.
@@ -7309,8 +7248,7 @@ <h3 id="Deletion_of_map_elements">Deletion of map elements</h3>
73097248

73107249
<p>
73117250
If the type of <code>m</code> is a <a href="#Type_parameter_lists">type parameter</a>,
7312-
it must have <a href="#Specific_types">specific types</a>, all specific types
7313-
must be maps, and they must all have identical key types.
7251+
all types in that type set must be maps, and they must all have identical key types.
73147252
</p>
73157253

73167254
<p>

0 commit comments

Comments
 (0)