Issue 19087 - `final switch` cannot be used in -betterC
Summary: `final switch` cannot be used in -betterC
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: betterC, pull
Depends on:
Blocks:
 
Reported: 2018-07-14 11:35 UTC by Mike Franklin
Modified: 2023-01-15 04:36 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mike Franklin 2018-07-14 11:35:46 UTC
void test(int p)
{
 final switch (p)
 {
   case 42:
     break;
 }
}

void main()
{
  test(0);
}

dmd -betterC test.d

/dlang/dmd/linux/bin64/../../src/druntime/import/core/exception.d(584): Error: Cannot use throw statements with -betterC
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(4180): Error: template instance `core.exception.__switch_errorT!()` error instantiating

This is caused by a `throw` statement in the implementation of `__switch_errorT` in druntime.  It should probably be an `assert`.
Comment 1 Seb 2018-08-15 23:38:04 UTC
> It should probably be an `assert`.

Agreed! `final switch` is actually usable in `nothrow` code (see e.g. https://run.dlang.io/is/mjVbPr), s.t. there's no reason to throw an exception.

Also programming errors are typically handled by `assert`.
Comment 2 Seb 2018-08-15 23:44:40 UTC
A start for a discussion: https://github.com/dlang/druntime/pull/2274
Comment 3 Mathias LANG 2018-08-16 01:09:05 UTC
Wasn't the plan to make this a compile-time error, or does it needs to be a runtime error ?
Comment 4 Mike Franklin 2018-08-16 01:59:23 UTC
I'm not sure how to make it a compile-time error.  For example the code could be...

void test(int p)
{
 final switch (p)
 {
   case 42:
     break;
 }
}

void main()
{
    import core.stdc.stdlib;
    test(rand());
}

How can we predict what `rand()` will return.  And I don't think it's practical to enumerate all 2^32 possibilities for `int`.  Though maybe I'm missing something.
Comment 5 Mike Franklin 2018-08-24 08:51:22 UTC
PR attempting to address this issue:  https://github.com/dlang/druntime/pull/2274
Comment 6 github-bugzilla 2018-10-22 20:51:17 UTC
Commits pushed to master at https://github.com/dlang/druntime

https://github.com/dlang/druntime/commit/9cbdab897846781b69bd9dd88d43748b3e5dcc4e
Fix Issue 19087 -  final switch cannot be used in -betterC

https://github.com/dlang/druntime/commit/7f62a68164bdb573bb705b2bed95e2bc7379f296
Merge pull request #2274 from wilzbach/fix-19087

[RFC] Issue 19087 - final switch cannot be used in -betterC