From patchwork Sun Feb 27 08:10:38 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 84679 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 248C4B6F0D for ; Sun, 27 Feb 2011 19:10:57 +1100 (EST) Received: (qmail 19748 invoked by alias); 27 Feb 2011 08:10:54 -0000 Received: (qmail 19740 invoked by uid 22791); 27 Feb 2011 08:10:52 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_CX, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 27 Feb 2011 08:10:40 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p1R8Ad1b014822 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sun, 27 Feb 2011 03:10:39 -0500 Received: from [127.0.0.1] (ovpn-113-65.phx2.redhat.com [10.3.113.65]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p1R8AckU021717 for ; Sun, 27 Feb 2011 03:10:38 -0500 Message-ID: <4D6A06FE.5060905@redhat.com> Date: Sun, 27 Feb 2011 03:10:38 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.7 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/47897 (failure with dependent static const) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org potential_constant_expression needs to allow dependent variables that might have integral type when instantiated. While I was touching that, I factored out the better diagnostics from cxx_eval_constant_expression so that we can have them in potential_constant_expression as well. Tested x86_64-pc-linux-gnu, applied to trunk. commit 1718f6f84c9d3795f9bd29cd2f51c50482f1fd8a Author: Jason Merrill Date: Sat Feb 26 19:49:55 2011 -0500 PR c++/47897 * semantics.c (non_const_var_error): Split out from... (cxx_eval_constant_expression): ...here. (potential_constant_expression_1) [VAR_DECL]: Use it. Allow dependent variables. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 199084a..a33a7ed 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6704,6 +6704,46 @@ cxx_eval_indirect_ref (const constexpr_call *call, tree t, return r; } +/* Complain about R, a VAR_DECL, not being usable in a constant expression. + Shared between potential_constant_expression and + cxx_eval_constant_expression. */ + +static void +non_const_var_error (tree r) +{ + tree type = TREE_TYPE (r); + error ("the value of %qD is not usable in a constant " + "expression", r); + if (DECL_DECLARED_CONSTEXPR_P (r)) + inform (DECL_SOURCE_LOCATION (r), + "%qD used in its own initializer", r); + else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) + { + if (!CP_TYPE_CONST_P (type)) + inform (DECL_SOURCE_LOCATION (r), + "%q#D is not const", r); + else if (CP_TYPE_VOLATILE_P (type)) + inform (DECL_SOURCE_LOCATION (r), + "%q#D is volatile", r); + else if (!DECL_INITIAL (r)) + inform (DECL_SOURCE_LOCATION (r), + "%qD was not initialized with a constant " + "expression", r); + else + gcc_unreachable (); + } + else + { + if (cxx_dialect >= cxx0x && !DECL_DECLARED_CONSTEXPR_P (r)) + inform (DECL_SOURCE_LOCATION (r), + "%qD was not declared %", r); + else + inform (DECL_SOURCE_LOCATION (r), + "%qD does not have integral or enumeration type", + r); + } +} + /* Attempt to reduce the expression T to a constant value. On failure, issue diagnostic and return error_mark_node. */ /* FIXME unify with c_fully_fold */ @@ -6744,39 +6784,7 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, if (DECL_P (r)) { if (!allow_non_constant) - { - tree type = TREE_TYPE (r); - error ("the value of %qD is not usable in a constant " - "expression", r); - if (DECL_DECLARED_CONSTEXPR_P (r)) - inform (DECL_SOURCE_LOCATION (r), - "%qD used in its own initializer", r); - else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) - { - if (!CP_TYPE_CONST_P (type)) - inform (DECL_SOURCE_LOCATION (r), - "%q#D is not const", r); - else if (CP_TYPE_VOLATILE_P (type)) - inform (DECL_SOURCE_LOCATION (r), - "%q#D is volatile", r); - else if (!DECL_INITIAL (r)) - inform (DECL_SOURCE_LOCATION (r), - "%qD was not initialized with a constant " - "expression", r); - else - gcc_unreachable (); - } - else - { - if (cxx_dialect >= cxx0x && !DECL_DECLARED_CONSTEXPR_P (r)) - inform (DECL_SOURCE_LOCATION (r), - "%qD was not declared %", r); - else - inform (DECL_SOURCE_LOCATION (r), - "%qD does not have integral or enumeration type", - r); - } - } + non_const_var_error (r); *non_constant_p = true; } break; @@ -7371,10 +7379,11 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags); case VAR_DECL: - if (want_rval && !decl_constant_var_p (t)) + if (want_rval && !decl_constant_var_p (t) + && !dependent_type_p (TREE_TYPE (t))) { if (flags & tf_error) - error ("variable %qD is not declared constexpr", t); + non_const_var_error (t); return false; } return true; diff --git a/gcc/testsuite/g++.dg/cpp0x/regress/debug-debug7.C b/gcc/testsuite/g++.dg/cpp0x/regress/debug-debug7.C index 8ee8824..ea8f1eb 100644 --- a/gcc/testsuite/g++.dg/cpp0x/regress/debug-debug7.C +++ b/gcc/testsuite/g++.dg/cpp0x/regress/debug-debug7.C @@ -7,8 +7,8 @@ int main() { int a = 4; - int b = 5; - int (*x)[b] = new int[a][b]; // { dg-error "" } + int b = 5; // { dg-message "not const" } + int (*x)[b] = new int[a][b]; // { dg-error "not usable" } x[2][1] = 7; diff --git a/gcc/testsuite/g++.dg/cpp0x/regress/template-const1.C b/gcc/testsuite/g++.dg/cpp0x/regress/template-const1.C new file mode 100644 index 0000000..32db1f8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/regress/template-const1.C @@ -0,0 +1,9 @@ +// PR c++/47897 +// { dg-options -std=c++0x } + +template < typename T, T N > +struct S +{ + static const T value = N; + typedef S< T, value + 1 > next; +}; diff --git a/gcc/testsuite/g++.dg/cpp0x/regress/template-function1.C b/gcc/testsuite/g++.dg/cpp0x/regress/template-function1.C index 028669e..66cbd4b 100644 --- a/gcc/testsuite/g++.dg/cpp0x/regress/template-function1.C +++ b/gcc/testsuite/g++.dg/cpp0x/regress/template-function1.C @@ -1,28 +1,29 @@ // PR c++/38647 // { dg-do compile } // { dg-options "-std=c++0x" } +// { dg-prune-output "note" } template struct A {}; const char func[] = "abc"; -template struct A {}; // { dg-error "cannot appear|is invalid|not a valid|not declared constexpr" } +template struct A {}; // { dg-error "cannot appear|is invalid|not a valid|constant expression" } char a1[1]; A a; template struct B {}; -template struct B<__FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|not declared constexpr" } +template struct B<__FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|constant expression" } char b1[1]; B b; template struct C {}; -template struct C<__PRETTY_FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|not declared constexpr" } +template struct C<__PRETTY_FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|constant expression" } char c1[1]; C c; template struct D {}; -template struct D<__func__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|function scope|not declared constexpr" } +template struct D<__func__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|function scope|constant expression" } char d1[1]; D d; diff --git a/gcc/testsuite/g++.dg/debug/debug7.C b/gcc/testsuite/g++.dg/debug/debug7.C index 31d47ed..8731cf8 100644 --- a/gcc/testsuite/g++.dg/debug/debug7.C +++ b/gcc/testsuite/g++.dg/debug/debug7.C @@ -1,4 +1,5 @@ // { dg-do compile } +// { dg-prune-output "note" } void f (int); diff --git a/gcc/testsuite/g++.dg/template/function1.C b/gcc/testsuite/g++.dg/template/function1.C index 8a112c1..bceed9d 100644 --- a/gcc/testsuite/g++.dg/template/function1.C +++ b/gcc/testsuite/g++.dg/template/function1.C @@ -1,27 +1,28 @@ // PR c++/38647 // { dg-do compile } +// { dg-prune-output "note" } template struct A {}; const char func[] = "abc"; -template struct A {}; // { dg-error "cannot appear|is invalid|not a valid|not declared constexpr" } +template struct A {}; // { dg-error "cannot appear|is invalid|not a valid|constant expression" } char a1[1]; A a; template struct B {}; -template struct B<__FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|not declared constexpr" } +template struct B<__FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|constant expression" } char b1[1]; B b; template struct C {}; -template struct C<__PRETTY_FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|not declared constexpr" } +template struct C<__PRETTY_FUNCTION__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|constant expression" } char c1[1]; C c; template struct D {}; -template struct D<__func__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|function scope|not declared constexpr" } +template struct D<__func__, N> {}; // { dg-error "cannot appear|is invalid|is not a valid|function scope|constant expression" } char d1[1]; D d;