Bug 10990 - [3.3/3.4 regression] Cannot convert with dynamic_cast<> to a private base class from within a member function
Summary: [3.3/3.4 regression] Cannot convert with dynamic_cast<> to a private base cla...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P2 normal
Target Milestone: 3.3.1
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2003-05-27 04:01 UTC by Rich_Sposato
Modified: 2004-01-17 04:22 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-05-27 09:33:50


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rich_Sposato 2003-05-27 04:01:59 UTC
Hello,

Please look into this bug and correct it soon.

Severity: serious: GCC is not working properly; a work-around is possible.
Category: compliance with C++ standard.
Class: rejects-legal: GCC gives an error message for correct code.
Environnment: using MinGW 4.9.8.0 on Win-XP.

Description:
The Junk::AsBase() function shown below should be able to perform a dynamic_cast
on itself and return a pointer to JunkBase.  However, the compiler says the base
class is inaccessible even though class Junk should be able to call any
functions in its base class and also return a pointer to itself as the base class.

Fix:
I can work around this by declaring JunkBase as a public base class.  But that
allows functions provided by JunkBase to be called by any other function, and I
don't want to make those functions accessible to anybody.


This is the Junk.ii file created by the compiler.

# 1 "junk.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "junk.cpp"

class JunkBase
{
public:
    virtual void DoSomething( void ) = 0;
protected:
    virtual ~JunkBase( void ) {};
    JunkBase( void ) {}
};

class Junk : protected JunkBase
{
public:
    Junk( void ) : JunkBase() {}
    virtual ~Junk( void ) {}
protected:
    inline JunkBase * AsBase( void )
    { return dynamic_cast< JunkBase * >( this ); }
    virtual void DoSomething( void ) { }
};




This is the command line I used to expose the bug:
g++.exe -c junk.cpp -o junk.o -I"C:/Dev-Cpp/include/c++" 
-I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/
Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  -I"C:/Projects/Include" 
-save-temps
junk.cpp: In member function `JunkBase* Junk::AsBase()':
junk.cpp:16: `JunkBase' is an inaccessible base of `Junk'
c:\Dev-Cpp\bin\make.exe: *** [junk.o] Error 1




g++ version info:
Reading specs from c:/Dev-Cpp/bin/../lib/gcc-lib/mingw32/3.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as
--host=mingw32 --target=min
gw32 --prefix=/mingw --enable-threads --disable-nls
--enable-languages=f77,c++,objc,ada --disable-wi
n32-registry --disable-shared
Thread model: win32
gcc version 3.2 (mingw special 20020817-1)



Thanks for your attention to this.

Rich
Comment 1 Giovanni Bajo 2003-05-27 09:33:50 UTC
Agreed. The standard says that the base class we're casting to should be "an 
accessible ambigous base class". In the code snippet JunkBase is obviously 
accessible (as base class) from within a member function, so the conversion 
should succeed. This is a regression wrt 2.95.
Comment 2 CVS Commits 2003-06-26 01:03:27 UTC
Subject: Bug 10990

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2003-06-26 00:07:53

Modified files:
	gcc/cp         : ChangeLog search.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.dg/abi: mangle4.C 
	gcc/testsuite/g++.dg/lookup: scoped1.C 
	gcc/testsuite/g++.old-deja/g++.martin: pmf1.C 
Added files:
	gcc/testsuite/g++.dg/rtti: dyncast1.C 

Log message:
	PR c++/10990
	* search.c (lookup_base_r): Rely on accessible_p, rather than
	trying to emulate that logic here.
	
	PR c++/10990
	* g++.dg/rtti/dyncast1.C: New test.
	* g++.dg/abi/mangle4.C: Correct base-specifier access.
	* g++.dg/lookup/scoped1.C: Remove XFAIL.
	* g++.old-deja/g++.martin/pmf1.C: Correct base-specifier access.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3456&r2=1.3457
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/search.c.diff?cvsroot=gcc&r1=1.266&r2=1.267
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2799&r2=1.2800
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/abi/mangle4.C.diff?cvsroot=gcc&r1=1.1&r2=1.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/scoped1.C.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/rtti/dyncast1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.martin/pmf1.C.diff?cvsroot=gcc&r1=1.2&r2=1.3

Comment 3 Mark Mitchell 2003-06-26 01:08:52 UTC
Fixed in GCC 3.3.1, GCC 3.4.
Comment 4 CVS Commits 2003-06-26 04:27:04 UTC
Subject: Bug 10990

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	mmitchel@gcc.gnu.org	2003-06-26 01:05:47

Modified files:
	gcc/cp         : ChangeLog call.c cp-tree.h search.c typeck.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.dg/abi: mangle4.C 
	gcc/testsuite/g++.dg/lookup: scoped1.C 
	gcc/testsuite/g++.old-deja/g++.jason: access8.C 
	gcc/testsuite/g++.old-deja/g++.martin: pmf1.C 
Added files:
	gcc/testsuite/g++.dg/expr: static_cast1.C 
	gcc/testsuite/g++.dg/rtti: dyncast1.C 

Log message:
	PR c++/10990
	* search.c (lookup_base_r): Rely on accessible_p, rather than
	trying to emulate that logic here.
	
	PR c++/10931
	* call.c (convert_like): Pass issue_conversion_warnings.
	(convert_like_with_context): Likewise.
	(convert_like_real): Add issue_conversion_warnings parameter.
	(perform_direct_initialization_if_possible): New function.
	* cp-tree.h (perform_direct_initialization_if_possible): Declare it.
	* typeck.c (check_for_casting_away_constness): New function.
	(build_static_cast): Rewrite.
	
	PR c++/10990
	* g++.dg/rtti/dyncast1.C: New test.
	* g++.dg/abi/mangle4.C: Correct base-specifier access.
	* g++.dg/lookup/scoped1.C: Remove XFAIL.
	* g++.old-deja/g++.martin/pmf1.C: Correct base-specifier access.
	
	PR c++/10931
	* g++.dg/expr/static_cast1.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.163&r2=1.3076.2.164
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.341.2.23&r2=1.341.2.24
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.776.2.23&r2=1.776.2.24
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/search.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.243.2.10&r2=1.243.2.11
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.436.2.8&r2=1.436.2.9
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.202&r2=1.2261.2.203
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/abi/mangle4.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.46.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/expr/static_cast1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/scoped1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.28.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/rtti/dyncast1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.jason/access8.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.4&r2=1.4.20.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.martin/pmf1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.64.1