From: Jakub Jelinek Date: Tue, 31 Jul 2018 14:19:26 +0000 (+0200) Subject: P1008R1 - prohibit aggregates with user-declared constructors X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=686dca7612a8ab5ebebad2fc13bd9ea7793a7e40;p=gcc.git P1008R1 - prohibit aggregates with user-declared constructors P1008R1 - prohibit aggregates with user-declared constructors * class.c (check_bases_and_members): For C++2a set CLASSTYPE_NON_AGGREGATE based on TYPE_HAS_USER_CONSTRUCTOR rather than type_has_user_provided_or_explicit_constructor. * g++.dg/ext/is_aggregate.C: Add tests with deleted or defaulted ctor. * g++.dg/cpp0x/defaulted1.C (main): Ifdef out for C++2a B b = {1};. * g++.dg/cpp0x/deleted2.C: Expect error for C++2a. * g++.dg/cpp2a/aggr1.C: New test. * g++.dg/cpp2a/aggr2.C: New test. From-SVN: r263115 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 128360794e8..bf6ce9761ac 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2018-07-31 Jakub Jelinek + + P1008R1 - prohibit aggregates with user-declared constructors + * class.c (check_bases_and_members): For C++2a set + CLASSTYPE_NON_AGGREGATE based on TYPE_HAS_USER_CONSTRUCTOR rather than + type_has_user_provided_or_explicit_constructor. + 2018-07-31 Martin Liska PR c++/86653 diff --git a/gcc/cp/class.c b/gcc/cp/class.c index d2f78088b94..c03a82b44f8 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -5571,7 +5571,9 @@ check_bases_and_members (tree t) Again, other conditions for being an aggregate are checked elsewhere. */ CLASSTYPE_NON_AGGREGATE (t) - |= (type_has_user_provided_or_explicit_constructor (t) + |= ((cxx_dialect < cxx2a + ? type_has_user_provided_or_explicit_constructor (t) + : TYPE_HAS_USER_CONSTRUCTOR (t)) || TYPE_POLYMORPHIC_P (t)); /* This is the C++98/03 definition of POD; it changed in C++0x, but we retain the old definition internally for ABI reasons. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7aa44c20a6f..90fb9c19a40 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,12 @@ +2018-07-31 Jakub Jelinek + + P1008R1 - prohibit aggregates with user-declared constructors + * g++.dg/ext/is_aggregate.C: Add tests with deleted or defaulted ctor. + * g++.dg/cpp0x/defaulted1.C (main): Ifdef out for C++2a B b = {1};. + * g++.dg/cpp0x/deleted2.C: Expect error for C++2a. + * g++.dg/cpp2a/aggr1.C: New test. + * g++.dg/cpp2a/aggr2.C: New test. + 2018-07-31 Segher Boessenkool PR target/86640 diff --git a/gcc/testsuite/g++.dg/cpp0x/defaulted1.C b/gcc/testsuite/g++.dg/cpp0x/defaulted1.C index 4956e88ed65..0ed81fb26a9 100644 --- a/gcc/testsuite/g++.dg/cpp0x/defaulted1.C +++ b/gcc/testsuite/g++.dg/cpp0x/defaulted1.C @@ -23,7 +23,9 @@ struct B int main() { A a1, a2; +#if __cplusplus <= 201703L B b = {1}; +#endif a1 = a2; } diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted2.C b/gcc/testsuite/g++.dg/cpp0x/deleted2.C index 8590c49f3c5..5576be33bef 100644 --- a/gcc/testsuite/g++.dg/cpp0x/deleted2.C +++ b/gcc/testsuite/g++.dg/cpp0x/deleted2.C @@ -6,4 +6,4 @@ struct A { A() = delete; }; -A a = {1}; +A a = {1}; // { dg-error "could not convert" "" { target c++2a } } diff --git a/gcc/testsuite/g++.dg/cpp2a/aggr1.C b/gcc/testsuite/g++.dg/cpp2a/aggr1.C new file mode 100644 index 00000000000..73a257cead9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/aggr1.C @@ -0,0 +1,15 @@ +// { dg-do compile { target c++11 } } +struct A { + A () = delete; // { dg-message "declared here" "" { target c++2a } } +}; +struct B { + B () = default; + int b = 0; +}; +struct C { + C (C&&) = default; // { dg-message "candidate" "" { target c++2a } } + int c, d; +}; +A a {}; // { dg-error "use of deleted function" "" { target c++2a } } +B b = {1}; // { dg-error "could not convert" "" { target { c++11_only || c++2a } } } +C *c = new C {2, 3}; // { dg-error "no matching function for call to" "" { target c++2a } } diff --git a/gcc/testsuite/g++.dg/cpp2a/aggr2.C b/gcc/testsuite/g++.dg/cpp2a/aggr2.C new file mode 100644 index 00000000000..e774be7ec2f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/aggr2.C @@ -0,0 +1,25 @@ +// { dg-do run { target c++11 } } + +struct A; +struct B { operator A (); }; +struct A { A (const A &) = default; A () = default; B a; }; +A a {B {}}; +bool seen; + +B::operator A () +{ + seen = true; + return A (); +} + +int +main () +{ +#if __cplusplus > 201703L + if (!seen) + __builtin_abort (); +#else + if (seen) + __builtin_abort (); +#endif +} diff --git a/gcc/testsuite/g++.dg/ext/is_aggregate.C b/gcc/testsuite/g++.dg/ext/is_aggregate.C index 8adc6c455f0..bdcc70fa7c2 100644 --- a/gcc/testsuite/g++.dg/ext/is_aggregate.C +++ b/gcc/testsuite/g++.dg/ext/is_aggregate.C @@ -61,6 +61,8 @@ struct K { int a, b; virtual void foo (); }; struct L : virtual public A { int d, e; }; struct M : protected A { int d, e; }; struct N : private A { int d, e; }; +struct O { O () = delete; int a, b, c; }; +struct P { P () = default; int a, b, c; }; typedef int T; typedef float U; typedef int V __attribute__((vector_size (4 * sizeof (int)))); @@ -94,6 +96,13 @@ main () assert (NTEST (L)); assert (NTEST (M)); assert (NTEST (N)); +#if __cplusplus > 201703L + assert (NTEST (O)); + assert (NTEST (P)); +#else + assert (PTEST (O)); + assert (PTEST (P)); +#endif assert (PTEST (int[])); assert (PTEST (double[])); assert (PTEST (T[2])); @@ -114,4 +123,6 @@ main () assert (PTEST (L[])); assert (PTEST (M[6])); assert (PTEST (N[])); + assert (PTEST (O[])); + assert (PTEST (P[])); }