[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#208981: gcc-3.3: typeof(nonconst+const) is const



Package: gcc-3.3
Version: 1:3.3.2-0pre1
Severity: minor

Try compiling the following function with "gcc-3.3 -c const.c".
GCC gives "assignment of read-only variable" warnings on the
indicated lines.  This shows that __typeof__ treats the
corresponding arguments as const.

void fn(void)
{
  int n;
  const int c;

  { __typeof__(n) a1; a1=0; }
  { __typeof__(c) a2; a2=0; }	/* warning */

  { __typeof__(n+n) b0; b0=0; }
  { __typeof__(n+c) b1; b1=0; }	/* warning */
  { __typeof__(c+n) b2; b2=0; }
  { __typeof__(c+c) b3; b3=0; }	/* warning */

  { __typeof__(1+n) c0; c0=0; }
  { __typeof__(1+c) c1; c1=0; }	/* warning */
  { __typeof__(n+1) c2; c2=0; }
  { __typeof__(c+1) c3; c3=0; }

  { __typeof__(((int)n)+((int)n)) d0; d0=0; }
  { __typeof__(((int)n)+((int)c)) d1; d1=0; } /* warning */
  { __typeof__(((int)c)+((int)n)) d2; d2=0; }
  { __typeof__(((int)c)+((int)c)) d3; d3=0; } /* warning */

  { __typeof__(((const int)n)+((const int)n)) e0; e0=0; }
  { __typeof__(((const int)n)+((const int)c)) e1; e1=0; } /* warning */
  { __typeof__(((const int)c)+((const int)n)) e2; e2=0; }
  { __typeof__(((const int)c)+((const int)c)) e3; e3=0; } /* warning */
}

The behavior at b1 and b2 seems inconsistent.  If n+c is const,
then surely c+n should be as well, given that the addition
operator is commutative in all other respects.

Cases c0 to c3 show that GCC treats an integer constant as a
non-const rvalue.  Cases d0 to d3 and e0 to e3 show that casts
have no effect on the constness of the result; that can't be
right either.

I think it would be cleanest to make the addition operator always
return a non-const rvalue, regardless of whether the operands are
const.  After all, the result is supposed to be freshly computed.

-- System Information:
Debian Release: testing/unstable
Architecture: i386
Kernel: Linux Astalo 2.4.21-kon.astalo.1 #1 la elokuun 23. 01:05:31 EEST 2003 i686
Locale: LANG=fi_FI.UTF-8@euro, LC_CTYPE=fi_FI.UTF-8@euro

Versions of packages gcc-3.3 depends on:
ii  binutils                 2.14.90.0.4-0.1 The GNU assembler, linker and bina
ii  cpp-3.3                  1:3.3.2-0pre1   The GNU C preprocessor
ii  gcc-3.3-base             1:3.3.2-0pre1   The GNU Compiler Collection (base 
ii  libc6                    2.3.2-2         GNU C Library: Shared libraries an
ii  libgcc1                  1:3.3.2-0pre1   GCC support library

-- no debconf information



Reply to: