Commit 3d81d4ad authored by Robert Griesemer's avatar Robert Griesemer

spec: document that duplicate types are invalid in type switches

Both compilers and also go/types don't permit duplicate types in
type switches; i.e., this spec change is documenting a status quo
that has existed for some time.

Furthermore, duplicate nils are not accepted by gccgo or go/types;
and more recently started causing a compiler error in gc. Permitting
them is inconsistent with the existing status quo.

Rather than making it an implementation restriction (as we have for
expression switches), this is a hard requirement since it was enforced
from the beginning (except for duplicate nils); it is also a well
specified requirement that does not pose a significant burden for
an implementation.

Fixes #15896.

Change-Id: If12db5bafa87598b323ea84418cb05421e657dd8
Reviewed-on: https://go-review.googlesource.com/23584Reviewed-by: default avatarRob Pike <r@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 3659645c
<!--{ <!--{
"Title": "The Go Programming Language Specification", "Title": "The Go Programming Language Specification",
"Subtitle": "Version of April 25, 2016", "Subtitle": "Version of May 31, 2016",
"Path": "/ref/spec" "Path": "/ref/spec"
}--> }-->
...@@ -4672,6 +4672,8 @@ Cases then match actual types <code>T</code> against the dynamic type of the ...@@ -4672,6 +4672,8 @@ Cases then match actual types <code>T</code> against the dynamic type of the
expression <code>x</code>. As with type assertions, <code>x</code> must be of expression <code>x</code>. As with type assertions, <code>x</code> must be of
<a href="#Interface_types">interface type</a>, and each non-interface type <a href="#Interface_types">interface type</a>, and each non-interface type
<code>T</code> listed in a case must implement the type of <code>x</code>. <code>T</code> listed in a case must implement the type of <code>x</code>.
The types listed in the cases of a type switch must all be
<a href="#Type_identity">different</a>.
</p> </p>
<pre class="ebnf"> <pre class="ebnf">
...@@ -4696,6 +4698,7 @@ in the TypeSwitchGuard. ...@@ -4696,6 +4698,7 @@ in the TypeSwitchGuard.
The type in a case may be <a href="#Predeclared_identifiers"><code>nil</code></a>; The type in a case may be <a href="#Predeclared_identifiers"><code>nil</code></a>;
that case is used when the expression in the TypeSwitchGuard that case is used when the expression in the TypeSwitchGuard
is a <code>nil</code> interface value. is a <code>nil</code> interface value.
There may be at most one <code>nil</code> case.
</p> </p>
<p> <p>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment