deprecated string case statements without 'else'

This commit is contained in:
Araq
2014-10-02 10:08:41 +02:00
parent 2011805829
commit c99ec16544
4 changed files with 15 additions and 8 deletions

View File

@@ -199,11 +199,12 @@ proc semCase(c: PContext, n: PNode): PNode =
var covered: BiggestInt = 0
var typ = commonTypeBegin
var hasElse = false
var notOrdinal = false
case skipTypes(n.sons[0].typ, abstractVarRange-{tyTypeDesc}).kind
of tyInt..tyInt64, tyChar, tyEnum, tyUInt..tyUInt32, tyBool:
chckCovered = true
of tyFloat..tyFloat128, tyString, tyError:
discard
notOrdinal = true
else:
localError(n.info, errSelectorMustBeOfCertainTypes)
return
@@ -233,6 +234,9 @@ proc semCase(c: PContext, n: PNode): PNode =
hasElse = true
else:
illFormedAst(x)
if notOrdinal and not hasElse:
message(n.info, warnDeprecated,
"use 'else: discard'; non-ordinal case without 'else'")
if chckCovered:
if covered == toCover(n.sons[0].typ):
hasElse = true

View File

@@ -262,13 +262,11 @@ given *slicelist* the ``else`` part is executed. If there is no ``else``
part and not all possible values that ``expr`` can hold occur in a
``slicelist``, a static error occurs. This holds only for expressions of
ordinal types. "All possible values" of ``expr`` are determined by ``expr``'s
type.
type. To suppress the static error an ``else`` part with an
empty ``discard`` statement should be used.
If the expression is not of an ordinal type, and no ``else`` part is
given, control passes after the ``case`` statement.
To suppress the static error in the ordinal case an ``else`` part with an
empty ``discard`` statement can be used.
For non ordinal types it is not possible to list every possible value and so
these always require an ``else`` part.
As a special semantic extension, an expression in an ``of`` branch of a case
statement may evaluate to a set or array constructor; the set or array is then

View File

@@ -312,7 +312,7 @@ the compiler that for every other value nothing should be done:
The empty `discard statement`_ is a *do nothing* statement. The compiler knows
that a case statement with an else part cannot fail and thus the error
disappears. Note that it is impossible to cover all possible string values:
that is why there is no such check for string cases.
that is why string cases always need an ``else`` branch.
In general the case statement is used for subrange types or enumerations where
it is of great help that the compiler checks that you covered any possible

View File

@@ -47,11 +47,16 @@ News
found under the `nim-code <https://github.com/nimrod-code>`_ organisation.
- Removed the deprecated ``web`` module, the ``httpclient`` module should
be used instead.
- String case (or any non-ordinal case) statements
without 'else' are deprecated.
Language Additions
------------------
- There is a new ``parallel`` statement for safe fork&join parallel computing.
- ``guard`` and ``lock`` pragmas have been implemented to support safer
concurrent programming.
Library Additions