The cvs head of g++ 4.1 miscompiles the following program:

--------------------------------------------------------
extern "C" int printf(...);
struct A
{
  virtual void foo () { printf ("A::foo\n"); }
};
struct B : public A
{
  virtual void foo () { printf ("B::foo\n"); }
};

template <class T>
void f(T a)
{
  ((A*)a)->A::foo();
}

int main()
{
  f(new B);
  return 0;
}
--------------------------------------------------------

I expect this program to print `A::foo'.  But instead, it prints:

$ g++ -o x x.cc
$ ./x
B::foo
$

This worked correctly as of the cvs version from a couple weeks ago.
It also works correctly if f() is made into a non-template function.

Here is the dump of f() from the 02.original tree dump, just to verify
that the problem is upstream of here:

;; Function void f(T) [with T = B*] (_Z1fIP1BEvT_)
;; enabled by -tree-original

<<cleanup_point <<< Unknown tree: expr_stmt
  OBJ_TYPE_REF(*NON_LVALUE_EXPR <(&a->D.1746)->_vptr.A>;&a->D.1746->0)
(&a->D.1746) >>>
>>;

It's doing a virtual function call, rather than calling A::foo directly.


The problem seems to have been introduced by this change:

2005-10-12  Nathan Sidwell  <[EMAIL PROTECTED]>

        PR c++/21592
        * pt.c (build_non_dependent_expr): Don't wrap a COMPONENT_REF
        with already looked up member functions.  Assert we're not
        returning a NON_DEPENDENT_EXPR with unknown type.
        * typeck.c (finish_class_member_access_expr):  We can get
        non-template-id-expr baselinks.  If the lookup finds a baselink,
        remember it even inside templates.

If i revert this change, then the test case is compiled correctly.

Environment:
System: Linux karma 2.6.12.1sss #2 Thu Jul 7 00:28:21 EDT 2005 i686 i686 i386
GNU/Linux
Architecture: i686

        <machine, os, target, libraries (multiple lines)>
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /home/sss/gcc/gcc/configure --prefix=/usr/local/gcc
--enable-threads=posix --enable-long-long --enable-languages=c,c++,f95

How-To-Repeat:

See above.


-- 
           Summary: 4.1: wrong virtual function called in template member
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: snyder at fnal dot gov
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24386

Reply via email to