GCC will not cast a variable of union type named in a typedef to its own type

John Cowan jcowan@reutershealth.com
Fri Apr 27 08:20:00 GMT 2001


/* This file demonstrates that GCC doesn't allow casting a
  * variable of a typedef'ed type that is a union type to
  * its very own type.  It does work if no typedef is involved.
  *
  * On machine 1:
  * uname -a says:
  * Linux skunk.reutershealth.com 2.2.16-22 #1
  * Tue Aug 22 16:49:06 EDT 2000 i686 unknown
  *
  * gcc -v says: Reading specs from
  * /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
  * gcc version 2.96 20000731 (Red Hat Linux 7.0)
  *
  * On machine 2:
  *
  * uname -a says:
  * FreeBSD wc2.cais.net 2.2.5-RELEASE FreeBSD 2.2.5-RELEASE #0:
  * Mon Jan 17 12:21:47 EST 2000
  * bille@drno.cais.net:/usr/src/sys/compile/DOCTOR-NO  i386
  *
  * gcc -v says: gcc version 2.7.2.1
  *
  * On machine 3:
  *
  * uname -a says:
  * SunOS ratanakiri 5.8 Generic sun4m sparc SUNW,SPARCstation-20
  *
  * gcc -v says: Reading specs from
  * /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/specs
  * gcc version 2.95.2 19991024 (release)
  *
  *
  * The preprocessor output is identical to the input, except
  * of course that comments are replaced by blank lines.
  *
  * I have tested this with and without -ansi.  With -ansi -pedantic,
  * all these casts to union types generate warnings.
  *
  * Here's how the bug appears:
  * [cowan@skunk joy]$ gcc -ansi bug.c
  * bug.c: In function `main':
  * bug.c:20: cast to union type from type not present in union
  *
  */

union i_f {int i; double f;};
typedef union i_f I_F;

# define UNITE1(x) ((union i_f) (x))
# define UNITE2(x) ((I_F) (x))

int main() {
         union i_f u1, v1;
         I_F u2, v2;
         u1 = UNITE1(32);
         u1 = UNITE1(32.0);
         v1 = UNITE1(u1);
         u2 = UNITE2(32);
         u2 = UNITE2(32.0);
         v2 = UNITE2(u2);		/* this line fails */
         return 0;
}

-- 
There is / one art             || John Cowan <jcowan@reutershealth.com>
no more / no less              || http://www.reutershealth.com
to do / all things             || http://www.ccil.org/~cowan
with art- / lessness           \\ -- Piet Hein



More information about the Gcc-bugs mailing list