[clang 19795] derived-to-base error

Nathan Sidwell nathan at acm.org
Fri Feb 6 11:44:07 PST 2015


[forgot to cc list]

Richard,
19795 is about a dynamic cast that is an upcast.  The defect affects any upcast. 
  here's a simpler testcase:

struct X {  };

struct V : public X {};

struct W : private virtual V {};

struct Y : private W, public virtual V {};

X *f (Y *x) {
   return x;
}

9795.cc:10:10: error: cannot cast 'Y' to its private base class 'X'
   return x;
          ^
19795.cc:5:12: note: constrained by private inheritance here
struct W : private virtual V {};
            ^~~~~~~~~~~~~~~~~
as the reporter comments, there's another path to V which is public, so the 
upcast is ok.  Interestingly conversions to V * work fine.  It's only 
conversions to base of virtual that has the problem.

The culprit is Sema::CheckDerivedToBaseConversion in SemaDeclCXX.cpp:
     // Check that the base class can be accessed.
       switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(),
                                    InaccessibleBaseID)) {

notice it's just picking the first path to base and checking that accessibility. 
  Sure enough, swapping the base declarations of Y allows the testcase to pass.

It's not clear to me the best way to fix this, since CheckBaseClassAccess can 
create deferred access checks and the like.  what's needed here is some way to 
check all the base paths and only issue a diagnostic if they all are 
inaccessible.  If one or more is accessible, we're done.  What's the best way of 
achieving that?

nathan



More information about the cfe-commits mailing list