Bug 10033 - [3.2 regression] optimization breaks polymorphic references w/ 'typeid' operator
Summary: [3.2 regression] optimization breaks polymorphic references w/ 'typeid' operator
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-03-12 02:36 UTC by jfischer_5809
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

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


Attachments
g++-3.2.2-typeid-bug-demo.tar.gz (174.98 KB, application/x-gzip )
2003-05-21 15:17 UTC, jfischer_5809
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jfischer_5809 2003-03-12 02:36:00 UTC
When any g++ optimization (i.e., other than -O0) is specified, and the lvalue operand of the C++ 'typeid' operator is a C++ reference to an object of polymorphic class type, then the typeid operator incorrectly evaluates to the reference's static type. It should instead evaluate to the referenced object's "most derived" class type. When optimization is not used, the 'typeid' operator correctly evaluates to the referenced object's "most derived" type.

Release:
3.2.2

Environment:
Red Hat 8.0 Linux, i686 (Pentium-III Celeron) PC

How-To-Repeat:
1) Unpack the attached tar.gz file, which contains the C++ source file 'main.cpp'.

2) Example of correct 'typeid' evaluation (NO g++ optimization):

[bash]$ g++ -Wall -Werror main.cpp
[bash]$ ./a.out
    A : 1A
    B : 1B
 aref : 1B  // <-- correct
*aptr : 1B

3) Example of incorrect 'typeid' evaluation (with g++ optimization):

[bash]$ g++ -Wall -Werror -O main.cpp
[bash]$ ./a.out
    A : 1A
    B : 1B
 aref : 1A  // <-- wrong
*aptr : 1B
Comment 1 Wolfgang Bangerth 2003-03-12 14:24:17 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed. Here's a smaller testcase:
    ---------------------
    #include <cstdlib>
    #include <typeinfo>
    
    struct A {
        virtual ~A() { }
    };
    
    struct B : public A { };
    
    int main()
    {
        B bobj;
        A& aref = bobj;
    
        if (typeid(aref) != typeid(B))
          abort();
    
        return 0;
    }
    --------------------------------
    It executes just fine with 3.0, 3.3 and 3.4, but
    aborts with 3.2 when given -O2. So it's a regression
    of 3.2 only.
    
    W.
Comment 2 Joe Buck 2003-04-25 19:53:38 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed for the next release (3.3).