Bug 16711 - [3.4/4.0 regression] Bug in reporting integral constant expressions problems
Summary: [3.4/4.0 regression] Bug in reporting integral constant expressions problems
Status: RESOLVED DUPLICATE of bug 16783
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-07-26 08:03 UTC by Roman
Modified: 2005-07-23 22:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Roman 2004-07-26 08:03:53 UTC
G++ 3.4.1 rejects some integral constant expressions that were allowed in the
earlier versions. The same file compiled with GCC as a C file does not produce
any errors. This report seems to be related to Bugzilla Bug 16489.

The sample file below (sample.cc) contains comments explaining the problem.


How to reproduce the bug:
g++ sample.cc


---------------------------------------------------------------------------

/*
// Bug report for gcc version 3.4.1 (Mandrakelinux (Cooker) 3.4.1-1mdk)

// Recent versions of G++ refuse to accept some 
// constant expressions which contain casts into 
// non integral types, e.g. into pointer types.
// It says a cast to anything
// but integral or enumaration type is not an integral constant
// expression.


// Such expressions are often produced by some
// tools generating C++ code as their output.
// Previously, these expressions were accepted by
// G++.

// Size of array should be an integral constant expression, or?
// But G++ does not complain about this expression
*/
int array [((int)(void*)1)];

enum MyEnum
{
   /*
   // Here we use the same constant_expression,
   // but as a value of the enumerator.
   // G++ doesn't like it for some reason
   */
   ONE = ((int)((void*)1))
};

  
int main(int argc)
{
  switch(argc)
  {
    /*
    // Here we use the same constant_expression
    // as a case value of switch statement
    // G++ does not like it at this place, too.
    */
    case ((int)(void*)1): break;
    case 2: break;
  }

  return 0;
}

/*
    There are three places in this program, that
    are using the same integer constant_expression.
    
    Earlier versions of G++, <= 3.2.X, didn't have
    this problem and accepted all three occurences
    of the constant_expression without any problems.
    More over, all other C++ compilers (Borland C++,
    Visual C++, etc) do not complain about this code.

    If this file is compiled as a C file, no errors
    are reported by GCC.

    It looks like the switch to the new C++
    parser and corresponding changes in the G++
    code introduced this behavior. And it looks
    like it is a bug in G++.
    
    After looking at the Changelog file, it seems
    that the following change could have introduced 
    such a behavior:
    
        2004-03-19  Giovanni Bajo  <giovannibajo@gcc.gnu.org>

	PR c++/14545
	* parser.c (cp_parser_functional_cast): A cast to anything
	but integral or enumaration type is not an integral constant
	expression.
    
    Probably, only the final type of the integral constant expression
    should be integral or enumeration. But in the middle of such
    expressions, it should probably be possible to cast to some other
    types, at least into pointer types, since conversions between
    pointers and integers do not involve any run-time computations.

*/
Comment 1 Wolfgang Bangerth 2004-07-26 13:32:12 UTC
My understanding is that casts are not allowed in integral const 
expressions, but in that case the array size should be disallowed 
as well. 
 
Giovanni, your patch is quoted in this PR, do you have some input? 
 
W. 
Comment 2 Andrew Pinski 2004-07-26 14:26:06 UTC
I do not have the standard in the front of my but IIRC the intermediate casts have to be of integeral 
types too.  In fact the change to reject some of these caused the definition of the macro offsetof to 
change (once again).
Comment 3 Wolfgang Bangerth 2004-07-26 14:28:55 UTC
Yes, I should have been clearer: casts between integral types should 
be ok. Casts to void* and back maybe not. In any case, we're inconsistent 
here. 
 
W. 
Comment 4 Joseph S. Myers 2004-07-26 20:30:24 UTC
Subject: Re:  New: Bug in reporting integral constant expressions
 problems

On Mon, 26 Jul 2004, romixlev at yahoo dot com wrote:

> G++ 3.4.1 rejects some integral constant expressions that were allowed in the
> earlier versions. The same file compiled with GCC as a C file does not produce
> any errors. This report seems to be related to Bugzilla Bug 16489.

Both the C and C++ standards are clear that casts to non-integer types
aren't allowed in integral constant expressions (C90, C++) / integer
constant expressions (C99).  Thus the bug is the nondiagnosis of:

> // Size of array should be an integral constant expression, or?
> // But G++ does not complain about this expression
> */
> int array [((int)(void*)1)];

The constant expressions bugs for C are bugs 456 and 5675.  Addressing
them is on my C standards conformance roadmap
<http://www.srcf.ucam.org/~jsm28/gcc/#stdc>, but after dealing with other
miscellaneous obscure bugs such as 13801, and it is a fair amount of work
to implement properly an obscure area of the standard with little
relevance to real code.  Given the obscurity of the area (and that there
is hardly a great deal of user interest in the finer points of
conformance), I'm surprised that this came up as a bug report, and still
more so that real code is actually using such things that are/were
undocumented extensions to constant expressions.  (The intention will
still be that such undocumented extensions are liable to be removed
without notice, as those in C++ have been with the implementation of the
new parser, but it's interesting that such removal has had *any* impact on
user code.)

Comment 5 Roman 2004-07-27 07:07:21 UTC
Subject: Re:  [3.4/3.5 regression] Bug in reporting integral constant expressions problems

Actually, my bug report comes from the following area.
I'm using PROP, a sort of C++ preprocessor, that was
developed may be 10 years ago, which translates a
superset of C++ into "normal" C++. Among other things
it supports algebraic data types and garbage
collection for C++. For a given algebraic data type
there can be several diffeent constructors (in the
sense of algebraic datatypes, not in C++ sense) with
and without arguments. The constructors with arguments
are dynamically creating the objects on the heap. The
unit constructors (i.e. those without arguments) do
not create objects. Instead they are represented by
some special values like (AlgebraicDataType
*)small_integer.
In principle, they are like enumerations. But these
values should be assignable to a pointer to the
AlgebraicDataType, they are always defined like:
#define UniConstructorName (AlgebraicDataType * )
small_integer

Later this macro is used everywhere in the code
generated by PROP.
  
The funny thing about this is that I'm using it since
6-7 years and have never had problems with it undar
any compiler. So, when I switched to GCC 3.4.1 I was
very surprised to see, that suddenly the automatically
generated code was illigal... 

But now, after some clarifications, I see that the
standard really does not allow such conversions into
non-integral types insdie an integral  constant
expression. So, I simply changed PROP to generate a a
code that it standard compliant. Now it works fine.
BTW, there was a small problem while changing PROP.
Since PROP uses bootstraping (i.e. written in itself),
its code was generated with the old version of it and
of course was using these illegal constructs. I had to
change them by hand (!!!) in several hundred places,
before I could compile a new version of PROP. After
that I regenerated it with the new version and now it
does not use illegal constructs. An G++ option that
would explicitly allow old behavior (at least for some
time, before making this constructs totally obsolette)
would help in such situations. But I guess, I ask too
much, or? :)

--- jsm at polyomino dot org dot uk
<gcc-bugzilla@gcc.gnu.org> wrote:

> 
> ------- Additional Comments From jsm at polyomino
> dot org dot uk  2004-07-26 20:30 -------
> Subject: Re:  New: Bug in reporting integral
> constant expressions
>  problems
> 
> On Mon, 26 Jul 2004, romixlev at yahoo dot com
> wrote:
> 
> > G++ 3.4.1 rejects some integral constant
> expressions that were allowed in the
> > earlier versions. The same file compiled with GCC
> as a C file does not produce
> > any errors. This report seems to be related to
> Bugzilla Bug 16489.
> 
> Both the C and C++ standards are clear that casts to
> non-integer types
> aren't allowed in integral constant expressions
> (C90, C++) / integer
> constant expressions (C99).  Thus the bug is the
> nondiagnosis of:
> 
> > // Size of array should be an integral constant
> expression, or?
> > // But G++ does not complain about this expression
> > */
> > int array [((int)(void*)1)];
> 
> The constant expressions bugs for C are bugs 456 and
> 5675.  Addressing
> them is on my C standards conformance roadmap
> <http://www.srcf.ucam.org/~jsm28/gcc/#stdc>, but
> after dealing with other
> miscellaneous obscure bugs such as 13801, and it is
> a fair amount of work
> to implement properly an obscure area of the
> standard with little
> relevance to real code.  Given the obscurity of the
> area (and that there
> is hardly a great deal of user interest in the finer
> points of
> conformance), I'm surprised that this came up as a
> bug report, and still
> more so that real code is actually using such things
> that are/were
> undocumented extensions to constant expressions. 
> (The intention will
> still be that such undocumented extensions are
> liable to be removed
> without notice, as those in C++ have been with the
> implementation of the
> new parser, but it's interesting that such removal
> has had *any* impact on
> user code.)
> 
> 
> 
> -- 
> 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16711
> 
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
> 



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
Comment 6 Wolfgang Bangerth 2004-07-27 13:21:24 UTC
> But I guess, I ask too much, or? :) 
 
Just as Joseph said, we're actually surprised that someone would use 
a 'feature' like this. You may be the only one who does, so I'd guess 
that chances are slim that we would introduce hacks to preserve 
backwards compatibility. 
 
W. 
Comment 7 Wolfgang Bangerth 2004-07-27 13:25:31 UTC
Since we're now certain what the problem is, I have opened PR 16783 
and will close this PR here as a duplicate. The other one has a more 
concise description of the problem, without the initial uncertainty 
as to what exactly is right and wrong. 
 
W. 

*** This bug has been marked as a duplicate of 16783 ***