deprecated string case statements without 'else'

This commit is contained in:
Araq 2014-10-02 10:08:41 +02:00
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 covered: BiggestInt = 0
var typ = commonTypeBegin var typ = commonTypeBegin
var hasElse = false var hasElse = false
var notOrdinal = false
case skipTypes(n.sons[0].typ, abstractVarRange-{tyTypeDesc}).kind case skipTypes(n.sons[0].typ, abstractVarRange-{tyTypeDesc}).kind
of tyInt..tyInt64, tyChar, tyEnum, tyUInt..tyUInt32, tyBool: of tyInt..tyInt64, tyChar, tyEnum, tyUInt..tyUInt32, tyBool:
chckCovered = true chckCovered = true
of tyFloat..tyFloat128, tyString, tyError: of tyFloat..tyFloat128, tyString, tyError:
discard notOrdinal = true
else: else:
localError(n.info, errSelectorMustBeOfCertainTypes) localError(n.info, errSelectorMustBeOfCertainTypes)
return return
@ -233,6 +234,9 @@ proc semCase(c: PContext, n: PNode): PNode =
hasElse = true hasElse = true
else: else:
illFormedAst(x) illFormedAst(x)
if notOrdinal and not hasElse:
message(n.info, warnDeprecated,
"use 'else: discard'; non-ordinal case without 'else'")
if chckCovered: if chckCovered:
if covered == toCover(n.sons[0].typ): if covered == toCover(n.sons[0].typ):
hasElse = true 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 part and not all possible values that ``expr`` can hold occur in a
``slicelist``, a static error occurs. This holds only for expressions of ``slicelist``, a static error occurs. This holds only for expressions of
ordinal types. "All possible values" of ``expr`` are determined by ``expr``'s 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 For non ordinal types it is not possible to list every possible value and so
given, control passes after the ``case`` statement. these always require an ``else`` part.
To suppress the static error in the ordinal case an ``else`` part with an
empty ``discard`` statement can be used.
As a special semantic extension, an expression in an ``of`` branch of a case 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 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 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 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: 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 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 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. found under the `nim-code <https://github.com/nimrod-code>`_ organisation.
- Removed the deprecated ``web`` module, the ``httpclient`` module should - Removed the deprecated ``web`` module, the ``httpclient`` module should
be used instead. be used instead.
- String case (or any non-ordinal case) statements
without 'else' are deprecated.
Language Additions Language Additions
------------------ ------------------
- There is a new ``parallel`` statement for safe fork&join parallel computing. - 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 Library Additions