Bug 19087 – `final switch` cannot be used in -betterC

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-07-14T11:35:46Z
Last change time
2023-01-15T04:36:29Z
Keywords
betterC, pull
Assigned to
No Owner
Creator
Mike Franklin
See also
https://issues.dlang.org/show_bug.cgi?id=19172

Comments

Comment #0 by slavo5150 — 2018-07-14T11:35:46Z
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 by greensunny12 — 2018-08-15T23:38:04Z
> 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 by greensunny12 — 2018-08-15T23:44:40Z
Comment #3 by pro.mathias.lang — 2018-08-16T01:09:05Z
Wasn't the plan to make this a compile-time error, or does it needs to be a runtime error ?
Comment #4 by slavo5150 — 2018-08-16T01:59:23Z
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 by slavo5150 — 2018-08-24T08:51:22Z
PR attempting to address this issue: https://github.com/dlang/druntime/pull/2274
Comment #6 by github-bugzilla — 2018-10-22T20:51:17Z
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