Bug 21427 – switch skips declaration of variable except it actually doesn't

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-11-26T12:44:15Z
Last change time
2024-12-13T19:13:01Z
Assigned to
No Owner
Creator
JR
Moved to GitHub: dmd#18002 →

Comments

Comment #0 by zorael — 2020-11-26T12:44:15Z
Manjaro/Arch x86_64, dmd v2.094.1. dmd complains about a variable declared in a switch case to have skipped its declaration and compilation fails. Original reduction: https://run.dlang.io/is/ah4XWa void main() { switch (string.init) { case "LS": switch (string.init) { case "sasl": immutable acceptsExternal = true; goto case; version (all) { case "twitch.tv/membership": goto case; } case "znc.in/self-message": break; default: break; } break; default: break; } } > feep[work] | zorael: even shorter https://run.dlang.io/is/nrjBkS void main() { final switch ("1") { case "1": immutable acceptsExternal = true; break; { case "2": break; } } } > feep[work] | zorael: this is the actual problem https://run.dlang.io/is/IOW91m void main() { final switch ("1") { case "1": immutable acceptsExternal = true; break; { case "2": return acceptsExternal; break; } } } > feep[work] | zorael: workaround https://run.dlang.io/is/as7yjf void main() { switch (string.init) { case "LS": switch (string.init) { case "sasl": immutable acceptsExternal = true; goto case; case "THISNEVERHAPPENS": // workaround for weird D switch scoping issue version (all) { case "twitch.tv/membership": goto case; } case "znc.in/self-message": break; default: break; } break; default: break; } } > feep[work] | zorael: alternately, explicitly wrap the acceptsExternal block in a {} [...] > feep[work] | for one, version shouldn't create a scope > feep[work] | zorael: the actual problem is it thinks that the versioned case is in a subscope and so the variable "should remain in scope" > feep[work] | which doesn't work cause you can case jump into the versioned case and skip the immutable variable > feep[work] | so the real issue is that switch thinks version opens a scope (it doesn't even)
Comment #1 by robert.schadek — 2024-12-13T19:13:01Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18002 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB