D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3617 - CTFE: wrong code for if(x) where x is int or smaller
Summary: CTFE: wrong code for if(x) where x is int or smaller
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2009-12-15 14:12 UTC by Don
Modified: 2014-04-18 09:12 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2009-12-15 14:12:33 UTC
Applies to while(), if(), assert(), etc.
Also happens if you have (say) uint u = 0x8000_0000; u<<=1;
-- since this sets the high bit of the value to 1.

TEST CASE:
----
int foo() {
   int u = cast(int)(0x1_0000_0000L);
   while (u) {
      if (u) { 
         assert(u!=0); 
        }
      assert(u!=0);
   }
   return 2;
}
static assert(foo()==2);
--------------------
PATCH: expression.c, around line 1575.
Need to convert the value to an appropriate sized integer.

int IntegerExp::isBool(int result)
{
+    toInteger();
    return result ? value != 0 : value == 0;
}
Comment 1 Leandro Lucarella 2009-12-15 16:50:30 UTC
http://www.dsource.org/projects/dmd/changeset/296
Comment 2 Walter Bright 2009-12-31 11:17:28 UTC
Fixed dmd 1.054 and 2.038