From: Jason Merrill Date: Tue, 6 Apr 2021 19:13:02 +0000 (-0400) Subject: c++: access checking in aggregate initialization [PR96673] X-Git-Tag: basepoints/gcc-12~239 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de03b82f3ca9103eba3699d1dc91b1d0ee1f16cb;p=thirdparty%2Fgcc.git c++: access checking in aggregate initialization [PR96673] We were deferring access checks while parsing B{}, didn't adjust that when we went to instantiate the default member initializer for B::c, deferred access checking for C::C, and then checked it after parsing B{}, back in the main() context which has no access. We need to do the access checks in the class context of the DMI. I tried fixing this in push_to/pop_from_top_level, but that caused several regressions. gcc/cp/ChangeLog: PR c++/96673 * init.c (get_nsdmi): Don't defer access checking. gcc/testsuite/ChangeLog: PR c++/96673 * g++.dg/cpp1y/nsdmi-aggr13.C: New test. --- diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 7d598f6196de..91b45a1a6951 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -591,6 +591,7 @@ get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain) { push_to_top_level (); push_nested_class (ctx); + push_deferring_access_checks (dk_no_deferred); pushed = true; } @@ -616,6 +617,7 @@ get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain) if (pushed) { + pop_deferring_access_checks (); pop_nested_class (); pop_from_top_level (); } diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr13.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr13.C new file mode 100644 index 000000000000..845e26ff5933 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr13.C @@ -0,0 +1,33 @@ +// PR c++/96673 +// { dg-do compile { target c++11 } } + +template +class A {}; + +template +class B; + +template +class C { + private: + + friend class B; + + explicit C(A&) {}; +}; + + +template +class B { + public: + B() = default; + //B() {}; // << This implementation of the constructor makes it work + + A a = {}; + C c = C{a}; +}; + +int main() { + auto b = B{}; + auto &c = b.c; +}