[dmd-internals] bug 259: error on unsigned/signed comparison

Christian Kamm kamm at incasoftware.de
Sun Oct 9 12:05:45 PDT 2011


Hi!

I was briefly looking at
http://d.puremagic.com/issues/show_bug.cgi?id=259
today and wondered whether the fix wouldn't be as easy as:

diff --git a/src/cast.c b/src/cast.c
index c7c78fc..b0c77ee 100644
--- a/src/cast.c
+++ b/src/cast.c
@@ -1629,6 +1629,48 @@ int typeMerge(Scope *sc, Expression *e, Type **pt, 
Expression **pe1, Expression
         TY ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
         TY ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
 
+        // For compares, don't blindly convert. One range must contain the 
other.
+        switch (e->op) {
+        case TOKlt:
+        case TOKle:
+        case TOKgt:
+        case TOKge:
+        case TOKunord:
+        case TOKlg:
+        case TOKleg:
+        case TOKule:
+        case TOKul:
+        case TOKuge:
+        case TOKug:
+        case TOKue: {
+            //printf("\tcompare ranges for comparison\n");
+            IntRange r1 = e1->getIntRange();
+            IntRange r2 = e2->getIntRange();
+            //r1.dump("lhs", e1);
+            //r2.dump("rhs", e2);
+
+            if (IntRange::fromType(t1b).contains(r2)) {
+                //printf("use lhs type\n");
+                t = t1;
+                e2 = e2->castTo(sc, t);
+                goto Lret;
+            }
+            if (IntRange::fromType(t2b).contains(r1)) {
+                //printf("use rhs type\n");
+                t = t2;
+                e1 = e1->castTo(sc, t);
+                goto Lret;
+            }
+
+            //printf("no safe conversion\n");
+            // better error message!
+            goto Lincompatible;
+
+            break;
+        }
+        default: break;
+        }
+
         if (t1b->ty == ty1)     // if no promotions
         {
             if (t1 == t2)

What did I miss? Looking at the first couple of errors when building druntime, 
they all seem genuine.

One thing is probably C code compatibility. With this change 1u < -1 == false.

Regards,
Christian


More information about the dmd-internals mailing list