Bug 4342 – branches that are known as not taken at compile time should not be checked

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
Other
OS
Mac OS X
Creation time
2010-06-18T02:46:00Z
Last change time
2012-09-27T00:33:34Z
Assigned to
nobody
Creator
fawzi

Comments

Comment #0 by fawzi — 2010-06-18T02:46:43Z
I see the following code as valid, and thus the present bug as "rejects-valid" {{{ module bug; extern(C) int printf(char*,...); void f(S...)(S args){ if (args[0].length>1 && args[0][1]>='0' && args[0][1]<='9'){ printf("bla,%d\n",args[0][1]-'0'); } } void main(){ f("x"); } }}} while the check in the if is correctly ignored (as it is unreachable because the length of args[0] is 1) the body of the if still checks args[0][1] and finds that it is out of bounds and rejects it. with bug.d(6): Error: array index 1 is out of bounds _param_0[0 .. 1]
Comment #1 by clugdbug — 2012-09-27T00:33:34Z
This if statement is a peculiar mix of compile-time and runtime conditions. It should be rewritten as: void f(S...)(S args) { static if (args[0].length>1) { if (args[0][1]>='0' && args[0][1]<='9') { printf("bla,%d\n",args[0][1]-'0'); } } } The enhancement is basically a request for syntax sugar in these situations: turn "if (false)" into "static if(false)". But it's very bug prone, eg this program would compile: ------ void main(int argc) { byte x = argc; if (x > 1000) { lets_confuse_the_maintenance_programmer() *= undefined_variable; } } ----- Will not be implemented.