[Issue 18146] A case expression of final switch allows to pass wrong enum value

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 21 16:24:10 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=18146

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org

--- Comment #4 from Nick Treleaven <nick at geany.org> ---
The problem in comment #3 seems to be these lines:

class Bezier(BezierOrder TYP){
  enum ubyte N= cast(ubyte)TYP+1u;
  typeof(this)[2] offsetCurve(in double width) @safe const
  do{
    final switch(TYP){
    case BezierOrder.Line:
      static assert(N == 2u);  // compile-time error is generated here
      break;
    case BezierOrder.Quadratic:
      static assert(N == 3u);

*All* the code inside `final switch` will be *compiled* regardless of the value
of `TYP`. `final switch` works at runtime even when given a compile-time
argument. So the compiler will try to compile *both* `static asserts` in the
`final switch`.

> In order to avoid the problem, I tried to use static if statements instead of a final switch statement.

Yes, because `static if` branches are only compiled if the compile-time
condition is true.

--


More information about the Digitalmars-d-bugs mailing list