Bug 20613 – String switch in -betterC fails for 7+ labels

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2020-02-26T09:25:53Z
Last change time
2022-06-08T17:37:47Z
Keywords
pull
Assigned to
No Owner
Creator
Simen Kjaeraas
See also
https://issues.dlang.org/show_bug.cgi?id=19268

Comments

Comment #0 by simen.kjaras — 2020-02-26T09:25:53Z
First reported here: https://forum.dlang.org/post/[email protected] When compiling with -betterC, switches over strings fail with 7 or more cases, as in this example: extern(C) void main() { auto s = "F"; final switch(s) { case "A": break; case "B": break; case "C": break; case "D": break; case "E": break; case "F": break; case "G": break; } } Which gives this error message: dmd2\windows\bin\..\..\src\druntime\import\object.d(2981): Error: TypeInfo cannot be used with -betterC The reason can be found on this line: https://github.com/dlang/druntime/blob/e018a72084e54ecc7466e97c96e4557b96b7f905/src/core/internal/switch_.d#L67 , which was added in this commit: https://github.com/dlang/druntime/commit/fa665f6618af7dbc09ed5ba1333f385017b7ece8 Testing indicates the whole anonymous lambda dance used there could be replaced with static immutable cases = [caseLabels];
Comment #1 by schveiguy — 2020-02-26T12:33:37Z
The issue is that the betterC restrictions are being applied during CTFE. I would say that this will be fixed as soon as issue 19268 is fixed. I would say this is really a duplicate. But this use case is important.
Comment #2 by schveiguy — 2020-02-26T12:43:09Z
(In reply to Simen Kjaeraas from comment #0) > Testing indicates the whole anonymous lambda dance used there could be > replaced with > static immutable cases = [caseLabels]; Indeed, I would think that a valid solution. The caseLabels are passed at compile time, so I don't see why they would need to be idup'd. I'm not sure why Martin did it the way he did. I'll ask him.
Comment #3 by schveiguy — 2020-02-27T16:19:15Z
(In reply to Steven Schveighoffer from comment #2) > I'm not sure why Martin did it the way he did. I'll ask him. No response from him, but I tried using your suggestion. It doesn't work (std.datetime fails to compile). The issue is that the template is passed in the string literals as the type of the value to switch on. So if you pass in a const(char)[] to test, the literals are passed in as const(char)[]. Which means, even though they are compile-time literals, they don't convert to immutable(char)[]. But I have found an answer that is less complex and just uses a cast. PR imminent.
Comment #4 by dlang-bot — 2020-02-27T16:32:16Z
@schveiguy created dlang/druntime pull request #2962 "Fix 20613 - String switch in -betterC fails for 7+ labels" fixing this issue: - Fix issue 20613. Switch statements of 7 or more strings should not depend on druntime for operation. https://github.com/dlang/druntime/pull/2962
Comment #5 by dlang-bot — 2020-02-27T18:25:32Z
dlang/druntime pull request #2962 "Fix 20613 - String switch in -betterC fails for 7+ labels" was merged into stable: - a836cf4a9ead5ed6edee09f3ba03e52fef4f8e66 by Steven Schveighoffer: Fix issue 20613. Switch statements of 7 or more strings should not depend on druntime for operation. https://github.com/dlang/druntime/pull/2962
Comment #6 by dkorpel — 2022-06-08T17:37:47Z
*** Issue 20573 has been marked as a duplicate of this issue. ***