From 1d4f0f3f6097dbe3817387d4889a123ca98bf987 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Thu, 1 Mar 2018 22:44:54 +0000 Subject: [PATCH] re PR c++/84582 (Rejected valid C++ code since r257961) PR c++/84582 * semantics.c (force_paren_expr): Create a PAREN_EXPR when in a template. (maybe_undo_parenthesized_ref): Unwrap PAREN_EXPR. * typeck2.c (store_init_value): Call fold_non_dependent_expr instead of instantiate_non_dependent_expr. * tree.c (lvalue_kind): Handle PAREN_EXPR like NON_DEPENDENT_EXPR. * g++.dg/cpp1y/auto-fn15.C: Extend testing. * g++.dg/cpp1z/static1.C: New test. * g++.dg/template/static37.C: New test. Co-Authored-By: Jason Merrill From-SVN: r258116 --- gcc/cp/ChangeLog | 11 +++++++++++ gcc/cp/semantics.c | 12 ++++++++---- gcc/cp/tree.c | 1 + gcc/cp/typeck2.c | 2 +- gcc/testsuite/ChangeLog | 8 ++++++++ gcc/testsuite/g++.dg/cpp1y/auto-fn15.C | 3 +++ gcc/testsuite/g++.dg/cpp1z/static1.C | 19 +++++++++++++++++++ gcc/testsuite/g++.dg/template/static37.C | 18 ++++++++++++++++++ 8 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/static1.C create mode 100644 gcc/testsuite/g++.dg/template/static37.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 4cb531038b2..d44845b57f3 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,14 @@ +2018-03-01 Marek Polacek + Jason Merrill + + PR c++/84582 + * semantics.c (force_paren_expr): Create a PAREN_EXPR when in + a template. + (maybe_undo_parenthesized_ref): Unwrap PAREN_EXPR. + * typeck2.c (store_init_value): Call fold_non_dependent_expr instead + of instantiate_non_dependent_expr. + * tree.c (lvalue_kind): Handle PAREN_EXPR like NON_DEPENDENT_EXPR. + 2018-03-01 Nathan Sidwell PR c++/84434 diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 87c5c669a55..1ac1d23e761 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -1693,7 +1693,8 @@ force_paren_expr (tree expr) if (TREE_CODE (expr) == COMPONENT_REF || TREE_CODE (expr) == SCOPE_REF) REF_PARENTHESIZED_P (expr) = true; - else if (type_dependent_expression_p (expr)) + else if (type_dependent_expression_p (expr) + || processing_template_decl) expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr); else if (VAR_P (expr) && DECL_HARD_REGISTER (expr)) /* We can't bind a hard register variable to a reference. */; @@ -1724,9 +1725,10 @@ force_paren_expr (tree expr) tree maybe_undo_parenthesized_ref (tree t) { - if (cxx_dialect >= cxx14 - && INDIRECT_REF_P (t) - && REF_PARENTHESIZED_P (t)) + if (cxx_dialect < cxx14) + return t; + + if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t)) { t = TREE_OPERAND (t, 0); while (TREE_CODE (t) == NON_LVALUE_EXPR @@ -1737,6 +1739,8 @@ maybe_undo_parenthesized_ref (tree t) || TREE_CODE (t) == STATIC_CAST_EXPR); t = TREE_OPERAND (t, 0); } + else if (TREE_CODE (t) == PAREN_EXPR) + t = TREE_OPERAND (t, 0); return t; } diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 9b9e36a1173..19f1c0629c9 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -239,6 +239,7 @@ lvalue_kind (const_tree ref) return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref))); case NON_DEPENDENT_EXPR: + case PAREN_EXPR: return lvalue_kind (TREE_OPERAND (ref, 0)); case VIEW_CONVERT_EXPR: diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 153b46cca77..583c65d4d0a 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -822,7 +822,7 @@ store_init_value (tree decl, tree init, vec** cleanups, int flags) if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl)) { bool const_init; - value = instantiate_non_dependent_expr (value); + value = fold_non_dependent_expr (value); if (DECL_DECLARED_CONSTEXPR_P (decl) || (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl))) { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 90b53ae2584..abd409e7df7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2018-03-01 Marek Polacek + Jason Merrill + + PR c++/84582 + * g++.dg/cpp1y/auto-fn15.C: Extend testing. + * g++.dg/cpp1z/static1.C: New test. + * g++.dg/template/static37.C: New test. + 2018-03-01 Nathan Sidwell PR c++/84434 diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn15.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn15.C index ba9f3579f62..0db428f7270 100644 --- a/gcc/testsuite/g++.dg/cpp1y/auto-fn15.C +++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn15.C @@ -22,6 +22,8 @@ template decltype(auto) h5(T t) { return t.i; } template decltype(auto) h6(T t) { return (t.i); } +template +decltype(auto) h7(T t) { return (i); } int main() { @@ -48,4 +50,5 @@ int main() same_type(); same_type(); same_type(); + same_type(); } diff --git a/gcc/testsuite/g++.dg/cpp1z/static1.C b/gcc/testsuite/g++.dg/cpp1z/static1.C new file mode 100644 index 00000000000..cb872997c5a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/static1.C @@ -0,0 +1,19 @@ +// PR c++/84582 +// { dg-options -std=c++17 } + +class C { + static inline const long b = 0; + static inline const unsigned c = (b); +}; +class D { + static inline const long b = 0; + static inline const unsigned c = b; +}; +template class A { + static inline const long b = 0; + static inline const unsigned c = (b); +}; +template class B { + static inline const long b = 0; + static inline const unsigned c = b; +}; diff --git a/gcc/testsuite/g++.dg/template/static37.C b/gcc/testsuite/g++.dg/template/static37.C new file mode 100644 index 00000000000..90bc65d2fbc --- /dev/null +++ b/gcc/testsuite/g++.dg/template/static37.C @@ -0,0 +1,18 @@ +// PR c++/84582 + +class C { + static const long b = 0; + static const unsigned c = (b); +}; +class D { + static const long b = 0; + static const unsigned c = b; +}; +template class A { + static const long b = 0; + static const unsigned c = (b); +}; +template class B { + static const long b = 0; + static const unsigned c = b; +}; -- 2.30.2