www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4342] New: branches that are known as not taken at compile time should not be checked

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4342

           Summary: branches that are known as not taken at compile time
                    should not be checked
           Product: D
           Version: D1
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: fawzi gmx.ch



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]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 18 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4342


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug yahoo.com.au
         Resolution|                            |INVALID



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 27 2012