Skip to content

Commit 2d9378c

Browse files
committed
spec: document existing expression switch restrictions
The spec didn't specify several aspects of expression switches: - The switch expression is evaluated exactly once. - Switch expressions evaluating to an untyped value are converted to the respective default type before use. - An (untyped) nil value is not permitted as expression switch value. (We could permit it relatively easily, but gc doesn't, and disallowing it is in symmetry with the rules for var decls without explicit type and untyped initializer expressions.) - The comparison x == t between each case expression x and switch expression value t must be valid. - (Some) duplicate constant case expressions are not permitted. This change also clarifies the following issues: 4524: mult. equal int const switch case values should be illegal -> spec issue fixed 6398: switch w/ no value uses bool rather than untyped bool -> spec issue fixed 11578: allows duplicate switch cases -> go/types bug 11667: int overflow in switch expression -> go/types bug 11668: use of untyped nil in switch -> not a gc bug Fixes #4524. Fixes #6398. Fixes #11668. Change-Id: Iae4ab3e714575a5d11c92c9b8fbf027aa706b370 Reviewed-on: https://go-review.googlesource.com/12711 Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent f4c775e commit 2d9378c

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

doc/go_spec.html

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of July 23, 2015",
3+
"Subtitle": "Version of July 30, 2015",
44
"Path": "/ref/spec"
55
}-->
66

@@ -662,7 +662,7 @@ <h2 id="Variables">Variables</h2>
662662
</p>
663663

664664
<p>
665-
The <i>static type</i> (or just <i>type</i>) of a variable is the
665+
The <i>static type</i> (or just <i>type</i>) of a variable is the
666666
type given in its declaration, the type provided in the
667667
<code>new</code> call or composite literal, or the type of
668668
an element of a structured variable.
@@ -672,8 +672,8 @@ <h2 id="Variables">Variables</h2>
672672
which has no type).
673673
The dynamic type may vary during execution but values stored in interface
674674
variables are always <a href="#Assignability">assignable</a>
675-
to the static type of the variable.
676-
</p>
675+
to the static type of the variable.
676+
</p>
677677

678678
<pre>
679679
var x interface{} // x is nil and has static type interface{}
@@ -4550,6 +4550,7 @@ <h3 id="Switch_statements">Switch statements</h3>
45504550
against the value of the switch expression.
45514551
In a type switch, the cases contain types that are compared against the
45524552
type of a specially annotated switch expression.
4553+
The switch expression is evaluated exactly once in a switch statement.
45534554
</p>
45544555

45554556
<h4 id="Expression_switches">Expression switches</h4>
@@ -4576,6 +4577,27 @@ <h4 id="Expression_switches">Expression switches</h4>
45764577
ExprSwitchCase = "case" ExpressionList | "default" .
45774578
</pre>
45784579

4580+
<p>
4581+
If the switch expression evaluates to an untyped constant, it is first
4582+
<a href="#Conversions">converted</a> to its <a href="#Constants">default type</a>;
4583+
if it is an untyped boolean value, it is first converted to type <code>bool</code>.
4584+
The predeclared untyped value <code>nil</code> cannot be used as a switch expression.
4585+
</p>
4586+
4587+
<p>
4588+
If a case expression is untyped, it is first <a href="#Conversions">converted</a>
4589+
to the type of the switch expression.
4590+
For each (possibly converted) case expression <code>x</code> and the value <code>t</code>
4591+
of the switch expression, <code>x == t</code> must be a valid <a href="#Comparison_operators">comparison</a>.
4592+
</p>
4593+
4594+
<p>
4595+
In other words, the switch expression is treated as if it were used to declare and
4596+
initialize a temporary variable <code>t</code> without explicit type; it is that
4597+
value of <code>t</code> against which each case expression <code>x</code> is tested
4598+
for equality.
4599+
</p>
4600+
45794601
<p>
45804602
In a case or default clause, the last non-empty statement
45814603
may be a (possibly <a href="#Labeled_statements">labeled</a>)
@@ -4588,7 +4610,7 @@ <h4 id="Expression_switches">Expression switches</h4>
45884610
</p>
45894611

45904612
<p>
4591-
The expression may be preceded by a simple statement, which
4613+
The switch expression may be preceded by a simple statement, which
45924614
executes before the expression is evaluated.
45934615
</p>
45944616

@@ -4611,6 +4633,13 @@ <h4 id="Expression_switches">Expression switches</h4>
46114633
}
46124634
</pre>
46134635

4636+
<p>
4637+
Implementation restriction: A compiler may disallow multiple case
4638+
expressions evaluating to the same constant.
4639+
For instance, the current compilers disallow duplicate integer,
4640+
floating point, or string constants in case expressions.
4641+
</p>
4642+
46144643
<h4 id="Type_switches">Type switches</h4>
46154644

46164645
<p>

0 commit comments

Comments
 (0)