Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!princeton!allegra!alice!bs From: bs@alice.UUCP Newsgroups: comp.lang.c++ Subject: bug re unsigned to signed conversion? Message-ID: <6965@alice.UUCP> Date: Mon, 8-Jun-87 14:20:29 EDT Article-I.D.: alice.6965 Posted: Mon Jun 8 14:20:29 1987 Date-Received: Wed, 10-Jun-87 07:18:36 EDT Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 85 Keywords: bugs > From: eppstein@garfield.columbia.edu.UUCP (eppstein @ Columbia University CS Department) > The following program prints both messages. I would expect it to print > neither, but at least the second message shouldn't happen. The > generated C code for the two lines is essentially identical; no doubt > the second line and probably the first should have generated a cast to int. > > #include > > int a = -1; > unsigned b = 2; > > inline int conv() { return b; } > > main() > { > if (a > b) cout << "(int) -1 > (unsigned) 2 ???\n"; > if (a > conv()) cout << "(int) -1 > (int) (unsigned) 2 ????????\n"; > } It is clearly a cfront bug that ``conv()'' is expanded into ``b'' and not ``(int)b''. Sorry. For people with release 1.2 here is a fix (a new ck_cast() for expand.c): int ck_cast(Ptype t1, Ptype t2) /* return a value of type t2 from a function returning a t1 return 1 if cast is needed */ { while (t1->base == TYPE) t1 = Pbase(t1)->b_name->tp; while (t2->base == TYPE) t2 = Pbase(t2)->b_name->tp; if (t1 == t2) return 0; if (t1->base != t2->base) return 1; switch (t1->base) { case CHAR: case SHORT: case INT: case LONG: if (Pbase(t1)->b_unsigned != Pbase(t2)->b_unsigned) return 1; } if (t1->base == COBJ) { Pname nn = Pbase(t1)->b_name; if (Pclass(nn->tp)->csu==UNION ) return 0; if (t2->base==COBJ && nn->tp==Pbase(t2)->b_name->tp) return 0; return 1; } return 0; } I'll leave the question of the correct value of ``a>b'' to your local C compiler until the ANSI C standard finally appears. Interestingly enough my C compiler causes this program main() { int a = -1; unsigned b = 2; if (-1>(unsigned)2) printf("true\n"); else printf("false\n"); if (a>b) printf("true\n"); else printf("false\n"); } so that it produces false true which is clearly a bug.