Case else-branch should reliably catch undefined value of fixed-size enum

Original Reporter info from Mantis: nanobit @nanobit
  • Reporter name:

Description:

On undefined enum-input, the case-statement
should reliably run the else-block or leave (if "else" is absent).
Case enm of ... else ... end; // e.g. on (byte(enm)=0) and type Tenum = (e1=1, e2)

Current situation: Undefined input may lead to undefined case-behavior (crash)
before reaching user-code. Reason is a functional FPC Pascal limit.
The limit is not widely known and inconvenient compared with other compilers (Delphi, C#, C++).
To reduce the gap, a test-operator for external use has been proposed
(#33603) to prevent undefined behaviors in subsequent operations.

This is not a duplicate of other bug (effect)-reports, but gives a systematic overview:
The four main arguments for allowing undefined input in case-statement:

  1. Enum holders can contain undefined values, thus testing is required.
    This reality (see note #1/2) has to be accepted as the starting point.
  2. Case-statements already are input-test-operations.
  3. Input-test prior to case-statement is inconvenient, which hinders its wide adoption.
  4. obtain compatibility with Delphi case-statement which allows undefined input
    like switch() in C# (enum Tenum: Tint {...};) and C++ (enum class Tenum: Tint {...};)
    Programmers are typically unaware of the FPC else-limit and mostly rely on
    common interpretation of: ALL unlisted values (cases) go to "else" branch.
    In that sense, the limit is a bug, in the better sense it's an unrequired burden
    (always own catching (even for ignore) required and earlier than in builtin versions).

Additional information:

The report objective is a single improvement:
To become compatible with (and reaching convenience of) other compilers:
Case-statement should use else-handling (else-block run, or leave if absent) for ALL unlisted cases.
It should not optimize away else-handling of certain enum-basetype values.

Two important notes (#1/2, #2/2) are the foundation of this report.
They try to answer "all" possible report-questions which stay close
enough to the case-statement input and implementation.

Mantis conversion info:

  • Mantis ID: 36983
  • OS: Windows
  • OS Build: 10
  • Platform: win32
  • Version: 3.3.1