D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3173 - ICE(mtype.c) on wrong code (double to long to int conversion)
Summary: ICE(mtype.c) on wrong code (double to long to int conversion)
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P2 regression
Assignee: No Owner
URL:
Keywords: ice-on-invalid-code, patch
Depends on:
Blocks:
 
Reported: 2009-07-13 13:04 UTC by Sebastien Alaiwan
Modified: 2015-06-09 01:28 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 Sebastien Alaiwan 2009-07-13 13:04:39 UTC
void f1()
{
  double d;
  int c = cast(long)(d);
}

produces : 
dmd: mtype.c:1554: uinteger_t Type::sizemask(): Assertion `0' failed.
Comment 1 Don 2009-08-31 08:16:52 UTC
This is a regression. It worked in 2.022.
Comment 2 Don 2009-08-31 23:56:38 UTC
Cause: default case for getIntRange() assumes it's an integer, which isn't true
if there's casting.
Solution: if not an integer, assume it could contain anything.

PATCH: cast.c, line 1902 (in Walter's private beta of DMD2.032):

IntRange Expression::getIntRange()
{
    IntRange ir;
    ir.imin = 0;
    if (type->isintegral())
	ir.imax = type->sizemask();
    else
	ir.imax = 0xFFFFFFFFFFFFFFFFULL; // assume the worst
    return ir;
}

===========
TEST CASES FOR TEST SUITE
===========

struct S3173{
   double z;
}

int bug3173(int x) {  return x; }

S3173 s3173;
double e3173 = 4;
const double d3173 = 4;
// invalid, but should not ICE

static assert(!is(typeof(bug3173(cast(long)e3173))));
static assert(!is(typeof(bug3173(cast(long)s3173))));
static assert(!is(typeof(bug3173(cast(long)3.256679e30))));
// And two valid cases:
static assert(is(typeof(bug3173(cast(long)d3173))));
static assert(is(typeof(bug3173(cast(long)3.256679e4))));
Comment 3 Don 2009-09-01 04:25:11 UTC
Patch was incomplete, fails for the test case below. Need to also add this code to the end of CastExp::getIntRange()

+   if (type->isintegral()) {
    ir.imin &= type->sizemask();
    ir.imax &= type->sizemask();
+    }
   return ir;
}

====
Test case:

  ubyte b = 6;
  short c5 = cast(int)(b + 6.1);
====
This is arguably an ice-on-valid-code.
Comment 4 Walter Bright 2009-10-06 02:21:38 UTC
Fixed dmd 2.033