From: Paolo Carlini Date: Fri, 6 Jun 2014 16:01:37 +0000 (+0000) Subject: re PR c++/60184 (g++ does not allow static members of named unions) X-Git-Tag: releases/gcc-5.1.0~7036 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4dadc66d180efede2b31bc3c45a9602cd03fca84;p=thirdparty%2Fgcc.git re PR c++/60184 (g++ does not allow static members of named unions) /cp 2014-06-06 Paolo Carlini PR c++/60184 * class.c (check_field_decls): In C++11 mode do not reject static data members and reference-type members in unions. /testsuite 2014-06-06 Paolo Carlini PR c++/60184 * g++.dg/cpp0x/constexpr-union6.C: New. * g++.dg/cpp0x/union6.C: Likewise. * g++.dg/init/ref14.C: Adjust. * g++.dg/init/union1.C: Likewise. From-SVN: r211318 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 053155ae4ace..54c7948bb880 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2014-06-06 Paolo Carlini + + PR c++/60184 + * class.c (check_field_decls): In C++11 mode do not reject + static data members and reference-type members in unions. + 2014-06-05 Jason Merrill PR c++/43453 diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 110dbf4f305a..25fc89bc0132 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -3480,22 +3480,25 @@ check_field_decls (tree t, tree *access_decls, /* When this goes into scope, it will be a non-local reference. */ DECL_NONLOCAL (x) = 1; - if (TREE_CODE (t) == UNION_TYPE) + if (TREE_CODE (t) == UNION_TYPE + && cxx_dialect < cxx11) { - /* [class.union] + /* [class.union] (C++98) If a union contains a static data member, or a member of - reference type, the program is ill-formed. */ + reference type, the program is ill-formed. + + In C++11 this limitation doesn't exist anymore. */ if (VAR_P (x)) { - error ("%q+D may not be static because it is a member of a union", x); + error ("in C++98 %q+D may not be static because it is " + "a member of a union", x); continue; } if (TREE_CODE (type) == REFERENCE_TYPE) { - error ("%q+D may not have reference type %qT because" - " it is a member of a union", - x, type); + error ("in C++98 %q+D may not have reference type %qT " + "because it is a member of a union", x, type); continue; } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7141aa3515d8..9f97db486050 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2014-06-06 Paolo Carlini + + PR c++/60184 + * g++.dg/cpp0x/constexpr-union6.C: New. + * g++.dg/cpp0x/union6.C: Likewise. + * g++.dg/init/ref14.C: Adjust. + * g++.dg/init/union1.C: Likewise. + 2014-06-06 Richard Biener PR tree-optimization/59299 diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union6.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union6.C new file mode 100644 index 000000000000..96d48182c3d2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union6.C @@ -0,0 +1,10 @@ +// PR c++/60184 +// { dg-do compile { target c++11 } } + +union Test1 { + static constexpr int kConstant = 10; +}; + +union Test2 { + static constexpr const int& kConstant = Test1::kConstant; +}; diff --git a/gcc/testsuite/g++.dg/cpp0x/union6.C b/gcc/testsuite/g++.dg/cpp0x/union6.C new file mode 100644 index 000000000000..1706da9db59b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/union6.C @@ -0,0 +1,20 @@ +// PR c++/60184 +// { dg-do compile { target c++11 } } + +union Test1 { + static int kConstant; +}; + +union Test2 { + static const int kConstant; +}; + +const int Test2::kConstant = 10; + +union Test3 { + int& kConstant; +}; + +union Test4 { + const int& kConstant = 10; +}; diff --git a/gcc/testsuite/g++.dg/init/ref14.C b/gcc/testsuite/g++.dg/init/ref14.C index 6ac42415b437..2e522c195668 100644 --- a/gcc/testsuite/g++.dg/init/ref14.C +++ b/gcc/testsuite/g++.dg/init/ref14.C @@ -4,7 +4,7 @@ union A { - int &i; // { dg-error "may not have reference type" } + int &i; // { dg-error "may not have reference type" "" { target { ! c++11 } } } }; void foo() diff --git a/gcc/testsuite/g++.dg/init/union1.C b/gcc/testsuite/g++.dg/init/union1.C index 0049f4429166..ea88346ebe88 100644 --- a/gcc/testsuite/g++.dg/init/union1.C +++ b/gcc/testsuite/g++.dg/init/union1.C @@ -1,5 +1,5 @@ // PR c++/14401 union U { - int& i; // { dg-error "" } + int& i; // { dg-error "reference type" "" { target { ! c++11 } } } };