Bug 8433 – Compiler could warn on static if checks which can't branch

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-25T06:38:22Z
Last change time
2019-10-10T11:43:02Z
Assigned to
No Owner
Creator
Andrej Mitrovic

Comments

Comment #0 by andrej.mitrovich — 2012-07-25T06:38:22Z
void main() { enum int x = 1; static if (x == 1) { writeln("1"); } else static if (x == 1) { writeln("2"); } } Only the first write statement will be compiled in. I think the compiler could help in these cases and produce a warning. There could be more complicated cases like: static if (x != 1) { } else static if (x == 2) { } Here again the second branch is never compiled.
Comment #1 by bugzilla — 2012-07-25T10:51:40Z
The trouble with a language feature like this is defining it. You'd have to enumerate the cases that are detected and the cases that are not, which has the side effect of prohibiting improving it. You don't really want a situation where code compiles with one compiler and does not with another, even with both being standard conforming.
Comment #2 by andrej.mitrovich — 2012-07-25T11:12:27Z
I'd like to just keep this in Bugzilla for the time being. I know it's problematic and might even slow down compilation considerably, so maybe it's best to just discuss about it sometime. It could be a nice feature to have if you rely on compile-time branching a lot.
Comment #3 by pro.mathias.lang — 2018-10-19T05:45:17Z
My issue with such a check is that the code is not *clearly* wrong. As a result, it can trigger false positive (very easily, given D's metaprogramming abilities) and those are extremely frustrating to the user and mostly negate the benefits of the warning (because the one benefiting from it are not the ones "paying the price"). It's been 6 years, do you think we should close this ?
Comment #4 by razvan.nitu1305 — 2019-10-10T11:43:02Z
I think that it is safe to close this now.