Bug 5607 - No pointer adjustment in covariant return types
Summary: No pointer adjustment in covariant return types
Status: RESOLVED DUPLICATE of bug 3706
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.0
: P3 normal
Target Milestone: ---
Assignee: Nathan Sidwell
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-02-06 02:56 UTC by alexanderrozenman
Modified: 2003-06-12 00:15 UTC (History)
5 users (show)

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


Attachments
Covariant.cpp (289 bytes, application/octet-stream)
2003-05-21 15:16 UTC, alexanderrozenman
Details

Note You need to log in before you can comment on or make changes to this bug.
Description alexanderrozenman 2002-02-06 02:56:01 UTC
When you run the attached program you can see that
pointer to B instance before and after covariant return
are different.

This is copy of my test case (the same as the attached file):

#begin

/**
 * This is test case for covariant pointers 
 * compiler implementation.
 * Numbers in each result line should be
 * the same.
 */

#include <iostream.h>

class A {
public:
  virtual A* getThis() { return this; }
};

class B {
int a;
public:
  virtual B* getThis() { return this; }
};

class AB : public A, public B {
public:
  virtual AB* getThis() { return this; }
};


int main () {

  using namespace std;

  AB* ab = new AB();
  
  A* a = ab;
  B* b = ab;

  cout << a << " " << a->getThis() <<  endl;
  cout << b << " " << b->getThis() <<  endl;

  return 0;
}

#end

Release:
gcc 3.0

Environment:
Solaris 2.6

How-To-Repeat:
Compile the test case, run it. 
In the second line you can see two different 
pointers to the same instance ('B' in the program)
Comment 1 alexanderrozenman 2002-02-06 02:56:01 UTC
Fix:
Adjust the pointer appropriately
Comment 2 Mark Mitchell 2002-04-25 14:56:55 UTC
From: Mark Mitchell <mark@codesourcery.com>
To: Jason Merrill <jason@redhat.com>,
   "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Cc: "gcc-gnats@gcc.gnu.org" <gcc-gnats@gcc.gnu.org>
Subject: Re: c++/5607: No pointer adjustment in covariant return types
 (PATCH)
Date: Thu, 25 Apr 2002 14:56:55 -0700

 > Tested i686-pc-linux-gnu, applied to trunk.  Mark, do you think this
 > should go into 3.1?  It's not a regression, but I don't like silently
 > generating bad code...
 
 Me neither, but it's not the only place.  Let's hold off.
 
 Thanks,
 
 --
 Mark Mitchell                   mark@codesourcery.com
 CodeSourcery, LLC               http://www.codesourcery.com
 

Comment 3 Jason Merrill 2002-04-25 19:02:54 UTC
From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c++/5607: No pointer adjustment in covariant return types
 (PATCH)
Date: Thu, 25 Apr 2002 19:02:54 +0100

 --=-=-=
 
 This is a known unimplimented feature, but we should have rejected your
 testcase with a sorry message.  We failed to do so because we were checking
 the vbase offset before we had calculated it.  This patch causes us to
 check later as well.
 
 Tested i686-pc-linux-gnu, applied to trunk.  Mark, do you think this should
 go into 3.1?  It's not a regression, but I don't like silently generating
 bad code...
 
 2002-04-25  Jason Merrill  <jason@redhat.com>
 
 	PR c++/5607
 	* search.c (check_final_overrider): No longer static.
 	* class.c (update_vtable_entry_for_fn): Call it.
 	* cp-tree.h: Adjust.
 
 
 --=-=-=
 Content-Type: text/x-patch
 Content-Disposition: inline
 
 *** class.c.~1~	Thu Apr 18 14:16:46 2002
 --- class.c	Thu Apr 25 12:24:06 2002
 *************** update_vtable_entry_for_fn (t, binfo, fn
 *** 2555,2560 ****
 --- 2555,2564 ----
     if (overrider == error_mark_node)
       return;
   
 +   /* Check for unsupported covariant returns again now that we've
 +      calculated the base offsets.  */
 +   check_final_overrider (TREE_PURPOSE (overrider), fn);
 + 
     /* Assume that we will produce a thunk that convert all the way to
        the final overrider, and not to an intermediate virtual base.  */
     virtual_base = NULL_TREE;
 *** cp-tree.h.~1~	Thu Apr 25 01:19:30 2002
 --- cp-tree.h	Thu Apr 25 12:21:04 2002
 *************** extern tree lookup_conversions			PARAMS 
 *** 4099,4104 ****
 --- 4099,4105 ----
   extern tree binfo_for_vtable			PARAMS ((tree));
   extern tree binfo_from_vbase			PARAMS ((tree));
   extern tree look_for_overrides_here		PARAMS ((tree, tree));
 + extern int check_final_overrider		PARAMS ((tree, tree));
   extern tree dfs_walk                            PARAMS ((tree,
   						       tree (*) (tree, void *),
   						       tree (*) (tree, void *),
 *** search.c.~1~	Thu Apr 18 14:52:10 2002
 --- search.c	Thu Apr 25 12:20:38 2002
 *************** static tree dfs_push_decls PARAMS ((tree
 *** 100,106 ****
   static tree dfs_unuse_fields PARAMS ((tree, void *));
   static tree add_conversions PARAMS ((tree, void *));
   static int covariant_return_p PARAMS ((tree, tree));
 - static int check_final_overrider PARAMS ((tree, tree));
   static int look_for_overrides_r PARAMS ((tree, tree));
   static struct search_level *push_search_level
   	PARAMS ((struct stack_level *, struct obstack *));
 --- 100,105 ----
 *************** covariant_return_p (brettype, drettype)
 *** 1798,1804 ****
   /* Check that virtual overrider OVERRIDER is acceptable for base function
      BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
   
 ! static int
   check_final_overrider (overrider, basefn)
        tree overrider, basefn;
   {
 --- 1797,1803 ----
   /* Check that virtual overrider OVERRIDER is acceptable for base function
      BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
   
 ! int
   check_final_overrider (overrider, basefn)
        tree overrider, basefn;
   {
 
 --=-=-=--

Comment 4 Jason Merrill 2002-07-01 09:58:40 UTC
From: Jason Merrill <jason@redhat.com>
To: <bronnimann_stephan@isoftel.com>
Cc: "'Huggel Andreas'" <huggel_andreas@isoftel.com>,
	gcc-gnats@gcc.gnu.org
Subject: Re: c++/5607: Covariant return types
Date: Mon, 01 Jul 2002 09:58:40 +0100

 >>>>> "Stephan" == Stephan Broennimann <bronnimann_stephan@isoftel.com> writes:
 
 > For our project, the most important question to you is whether we can expect
 > that the bug will be fixed soon or not:
 
 It's not a bug, it's an unimplemented feature.  I don't think anyone plans
 to implement it soon.
 
 > Firstly: Since PR 3706 (opened for gcc-3.0) things have obviously changed,
 > covariant returns are now accepted. The "problem" is that a warning should
 > be issued because their usage is not safe yet.
 
 Yes, it should give a sorry for cases that don't currently work.  I thought
 I had fixed that.
 
 > Secondly: If in the test case given in PR 5607 AB is derived virtual from
 > either A or B, the compiler still rejects covariant returns.
 > class AB: public virtual A, public B {
 > public:
 >     virtual AB* getThis();
 > }
 > (I don't know if the standard allows for covariant returns when using
 > virtual inheritance, please forgive me should I be wrong).
 
 The standard allows it, but it requires an adjustment, so g++ doesn't
 currently support it.
 
 Jason

Comment 5 Jason Merrill 2002-08-23 12:00:44 UTC
From: jason@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/5607
Date: 23 Aug 2002 12:00:44 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	jason@gcc.gnu.org	2002-08-23 05:00:44
 
 Modified files:
 	gcc            : ChangeLog explow.c expr.c expr.h 
 	                 langhooks-def.h langhooks.c langhooks.h 
 	gcc/cp         : ChangeLog call.c class.c cp-lang.c cp-tree.h 
 	                 search.c typeck.c 
 
 Log message:
 	* langhooks-def.h (LANG_HOOKS_EXPR_SIZE): New macro.
 	* langhooks.c (lhd_expr_size): Define default.
 	* langhooks.h (struct lang_hooks): Add expr_size.
 	* explow.c (expr_size): Call it.
 	(int_expr_size): New fn.
 	* expr.h: Declare it.
 	* expr.c (expand_expr) [CONSTRUCTOR]: Use it to calculate how
 	much to store.
 	cp/
 	PR c++/5607
 	* search.c (check_final_overrider): No longer static.
 	* class.c (update_vtable_entry_for_fn): Call it.
 	* cp-tree.h: Adjust.
 	
 	* cp-lang.c (LANG_HOOKS_EXPR_SIZE): Define.
 	(cp_expr_size): New fn.
 	* call.c (convert_arg_to_ellipsis): Promote non-POD warning to error.
 	* typeck.c (build_modify_expr): Don't use save_expr on an lvalue.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.19&r2=1.13152.2.657.2.20
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/explow.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.91&r2=1.91.6.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/expr.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.423.2.19&r2=1.423.2.19.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/expr.h.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.110.2.3&r2=1.110.2.3.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/langhooks-def.h.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.8.10.2&r2=1.8.10.2.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/langhooks.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.16.2.2&r2=1.16.2.2.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/langhooks.h.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.15.8.2&r2=1.15.8.2.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.2685.2.114.2.4&r2=1.2685.2.114.2.5
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.307.2.6&r2=1.307.2.6.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/class.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.436.2.10.2.2&r2=1.436.2.10.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-lang.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.9.10.1&r2=1.9.10.1.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.681.2.12.2.1&r2=1.681.2.12.2.2
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/search.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.223.2.3&r2=1.223.2.3.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.388.2.10&r2=1.388.2.10.2.1
 
Comment 6 Nathan Sidwell 2002-09-14 15:54:26 UTC
*** This bug has been marked as a duplicate of 3706 ***