From 2d9378c7f6dfbbe82d1bbd806093c2dfe57d7e17 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 27 Jul 2015 13:30:16 -0700 Subject: [PATCH] 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 Reviewed-by: Rob Pike --- doc/go_spec.html | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 95406a1687..14fa44c675 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -662,7 +662,7 @@ acts like a variable.

-The static type (or just type) of a variable is the +The static type (or just type) of a variable is the type given in its declaration, the type provided in the new call or composite literal, or the type of an element of a structured variable. @@ -672,8 +672,8 @@ which is the concrete type of the value assigned to the variable at run time which has no type). The dynamic type may vary during execution but values stored in interface variables are always assignable -to the static type of the variable. -

+to the static type of the variable. +

 var x interface{}  // x is nil and has static type interface{}
@@ -4550,6 +4550,7 @@ In an expression switch, the cases contain expressions that are compared
 against the value of the switch expression.
 In a type switch, the cases contain types that are compared against the
 type of a specially annotated switch expression.
+The switch expression is evaluated exactly once in a switch statement.
 

Expression switches

@@ -4576,6 +4577,27 @@ ExprCaseClause = ExprSwitchCase ":" StatementList . ExprSwitchCase = "case" ExpressionList | "default" .
+

+If the switch expression evaluates to an untyped constant, it is first +converted to its default type; +if it is an untyped boolean value, it is first converted to type bool. +The predeclared untyped value nil cannot be used as a switch expression. +

+ +

+If a case expression is untyped, it is first converted +to the type of the switch expression. +For each (possibly converted) case expression x and the value t +of the switch expression, x == t must be a valid comparison. +

+ +

+In other words, the switch expression is treated as if it were used to declare and +initialize a temporary variable t without explicit type; it is that +value of t against which each case expression x is tested +for equality. +

+

In a case or default clause, the last non-empty statement may be a (possibly labeled) @@ -4588,7 +4610,7 @@ but the last clause of an expression switch.

-The expression may be preceded by a simple statement, which +The switch expression may be preceded by a simple statement, which executes before the expression is evaluated.

@@ -4611,6 +4633,13 @@ case x == 4: f3() } +

+Implementation restriction: A compiler may disallow multiple case +expressions evaluating to the same constant. +For instance, the current compilers disallow duplicate integer, +floating point, or string constants in case expressions. +

+

Type switches

-- 2.50.0