From 00d0cd1bf27e85e3d3cc8fb32163484150f5c30e Mon Sep 17 00:00:00 2001 From: jason Date: Tue, 2 Nov 2010 01:31:31 +0000 Subject: [PATCH] * semantics.c (cxx_eval_constant_expression): Explain unacceptable use of variable better. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@166168 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/cp/ChangeLog | 5 ++++ gcc/cp/semantics.c | 31 +++++++++++++++++++++- gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C | 2 +- gcc/testsuite/g++.dg/cpp0x/constexpr-ex3.C | 2 +- gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C | 2 +- .../g++.dg/cpp0x/constexpr-non-const-arg.C | 2 +- gcc/testsuite/g++.dg/parse/constant4.C | 2 +- gcc/testsuite/g++.dg/template/qualified-id3.C | 2 +- 8 files changed, 41 insertions(+), 7 deletions(-) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 2e9f535bef2..93f1b7a7e43 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2010-11-01 Jason Merrill + + * semantics.c (cxx_eval_constant_expression): Explain + unacceptable use of variable better. + 2010-11-01 Gabriel Dos Reis Jason Merrill diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 397d383f650..2b8e9e3f4b4 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6513,7 +6513,36 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, if (DECL_P (r)) { if (!allow_non_constant) - error ("%qD cannot appear in a constant expression", r); + { + tree type = TREE_TYPE (r); + error ("the value of %qD is not usable in a constant " + "expression", r); + 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_constant_p = true; } break; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C index f2b53846ecb..c7757f47598 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C @@ -55,7 +55,7 @@ constexpr complex I(0, 1); // OK -- literal complex // 2 invoked with non-const args -double x5 = 1.0; +double x5 = 1.0; // { dg-message "not declared .constexpr" } constexpr complex unit(x5, 0); // { dg-error "x5|argument" } error: x5 non-constant const complex one(x5, 0); // OK, ‘‘ordinary const’’ -- dynamic // initialization diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ex3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ex3.C index 597603c39a4..08552cd7de7 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-ex3.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ex3.C @@ -15,7 +15,7 @@ struct B constexpr B(T _t): t(_t) { } }; -B b(1); +B b(1); // { dg-message "not declared .constexpr" } SA(b.t==1); // { dg-error "non-constant condition|'b'" } constexpr B b2(1); SA(b2.t==1); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C index 5e0c101b2ba..a902e0e226c 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-function2.C @@ -17,7 +17,7 @@ inline constexpr double squared(double x) { return x * x; } constexpr int squarei(int x) { return x * x; } -extern const int side; +extern const int side; // { dg-message "not initialized with a constant expression" } constexpr int area = squarei(side); // { dg-error "side|argument" } // error: squarei(side) is not a constant expression diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-non-const-arg.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-non-const-arg.C index 2c53595833e..7637c0a05bc 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-non-const-arg.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-non-const-arg.C @@ -7,7 +7,7 @@ struct B { int i; }; -int global; // not constant +int global; // { dg-message "not const" } struct D : B { constexpr D() : B(global) { } // { dg-error "global|argument" } diff --git a/gcc/testsuite/g++.dg/parse/constant4.C b/gcc/testsuite/g++.dg/parse/constant4.C index b2c112ce714..4d9814fb485 100644 --- a/gcc/testsuite/g++.dg/parse/constant4.C +++ b/gcc/testsuite/g++.dg/parse/constant4.C @@ -16,7 +16,7 @@ void Foo () Y i; - static const unsigned J = X::J; + static const unsigned J = X::J; // { dg-message "not initialized with a constant expression" } Y j; // { dg-error "constant" "" } } diff --git a/gcc/testsuite/g++.dg/template/qualified-id3.C b/gcc/testsuite/g++.dg/template/qualified-id3.C index 1fc1cc32050..bbfb51e4ce1 100644 --- a/gcc/testsuite/g++.dg/template/qualified-id3.C +++ b/gcc/testsuite/g++.dg/template/qualified-id3.C @@ -2,7 +2,7 @@ template struct A { }; template struct B { - static const int c; + static const int c; // { dg-message "not initialized with a constant expression" } typedef A::c> C; // { dg-error "constant expression" } }; template const int B::c = sizeof (T); -- 2.11.4.GIT