From: Patrick Palka Date: Fri, 8 Jan 2021 15:02:04 +0000 (-0500) Subject: c++: Fix access checking of scoped non-static member [PR98515] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=98a1fb705ead9258642f2dec0431f11508a9b13c;p=gcc.git c++: Fix access checking of scoped non-static member [PR98515] In the first testcase below, we incorrectly reject the use of the protected non-static member A::var0 from C::g() because check_accessibility_of_qualified_id, at template parse time, determines that the access doesn't go through 'this'. (This happens because the dependent base B of C doesn't have a binfo object, so it appears to DERIVED_FROM_P that A is not an indirect base of C.) From there we create the corresponding deferred access check, which we then perform at instantiation time and which (expectedly) fails. The problem ultimately seems to be that we can't in general determine whether a use of a scoped non-static member goes through 'this' until instantiation time, as the second testcase below illustrates. So this patch makes check_accessibility_of_qualified_id punt in such situations to avoid creating a bogus deferred access check. gcc/cp/ChangeLog: PR c++/98515 * semantics.c (check_accessibility_of_qualified_id): Punt if we're checking access of a scoped non-static member inside a class template. gcc/testsuite/ChangeLog: PR c++/98515 * g++.dg/template/access32.C: New test. * g++.dg/template/access33.C: New test. --- diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index b448efe024a..c6b4c70dc0f 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2107,14 +2107,24 @@ check_accessibility_of_qualified_id (tree decl, /* If the reference is to a non-static member of the current class, treat it as if it were referenced through `this'. */ - tree ct; if (DECL_NONSTATIC_MEMBER_P (decl) - && current_class_ptr - && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ())) - qualifying_type = ct; + && current_class_ptr) + if (tree current = current_nonlambda_class_type ()) + { + if (dependent_type_p (current)) + /* In general we can't know whether this access goes through + `this' until instantiation time. Punt now, or else we might + create a deferred access check that's not relative to `this' + when it ought to be. We'll check this access again after + substitution, e.g. from tsubst_qualified_id. */ + return true; + + if (DERIVED_FROM_P (scope, current)) + qualifying_type = current; + } /* Otherwise, use the type indicated by the nested-name-specifier. */ - else + if (!qualifying_type) qualifying_type = nested_name_specifier; } else diff --git a/gcc/testsuite/g++.dg/template/access32.C b/gcc/testsuite/g++.dg/template/access32.C new file mode 100644 index 00000000000..08faa9f0f97 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/access32.C @@ -0,0 +1,8 @@ +// PR c++/98515 +// { dg-do compile } + +struct A { protected: int var0; }; +template struct B : public A { }; +template struct C : public B { void g(); }; +template void C::g() { A::var0++; } +template class C; diff --git a/gcc/testsuite/g++.dg/template/access33.C b/gcc/testsuite/g++.dg/template/access33.C new file mode 100644 index 00000000000..9fb9b9a1236 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/access33.C @@ -0,0 +1,9 @@ +// PR c++/98515 +// { dg-do compile } + +struct A { protected: int var0; }; +template struct B : public A { }; +template struct C : public B { void g(); }; +template void C::g() { A::var0++; } // { dg-error "protected|invalid" } +template <> struct B { }; +template class C;