Commit ddbbc9a1 by Jason Merrill Committed by Jason Merrill

semantics.c (cxx_eval_constant_expression): Explain unacceptable use of variable better.

	* semantics.c (cxx_eval_constant_expression): Explain
	unacceptable use of variable better.

From-SVN: r166168
parent fa2200cb
2010-11-01 Jason Merrill <jason@redhat.com>
* semantics.c (cxx_eval_constant_expression): Explain
unacceptable use of variable better.
2010-11-01 Gabriel Dos Reis <gdr@cse.tamu.edu> 2010-11-01 Gabriel Dos Reis <gdr@cse.tamu.edu>
Jason Merrill <jason@redhat.com> Jason Merrill <jason@redhat.com>
......
...@@ -6513,7 +6513,36 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, ...@@ -6513,7 +6513,36 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t,
if (DECL_P (r)) if (DECL_P (r))
{ {
if (!allow_non_constant) 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 %<constexpr%>", r);
else
inform (DECL_SOURCE_LOCATION (r),
"%qD does not have integral or enumeration type",
r);
}
}
*non_constant_p = true; *non_constant_p = true;
} }
break; break;
......
...@@ -55,7 +55,7 @@ constexpr complex I(0, 1); // OK -- literal complex ...@@ -55,7 +55,7 @@ constexpr complex I(0, 1); // OK -- literal complex
// 2 invoked with non-const args // 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 constexpr complex unit(x5, 0); // { dg-error "x5|argument" } error: x5 non-constant
const complex one(x5, 0); // OK, ‘‘ordinary const’’ -- dynamic const complex one(x5, 0); // OK, ‘‘ordinary const’’ -- dynamic
// initialization // initialization
......
...@@ -15,7 +15,7 @@ struct B ...@@ -15,7 +15,7 @@ struct B
constexpr B(T _t): t(_t) { } constexpr B(T _t): t(_t) { }
}; };
B<int> b(1); B<int> b(1); // { dg-message "not declared .constexpr" }
SA(b.t==1); // { dg-error "non-constant condition|'b'" } SA(b.t==1); // { dg-error "non-constant condition|'b'" }
constexpr B<int> b2(1); constexpr B<int> b2(1);
SA(b2.t==1); SA(b2.t==1);
......
...@@ -17,7 +17,7 @@ inline constexpr double ...@@ -17,7 +17,7 @@ inline constexpr double
squared(double x) { return x * x; } squared(double x) { return x * x; }
constexpr int squarei(int 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" } constexpr int area = squarei(side); // { dg-error "side|argument" }
// error: squarei(side) is not a constant expression // error: squarei(side) is not a constant expression
......
...@@ -7,7 +7,7 @@ struct B { ...@@ -7,7 +7,7 @@ struct B {
int i; int i;
}; };
int global; // not constant int global; // { dg-message "not const" }
struct D : B { struct D : B {
constexpr D() : B(global) { } // { dg-error "global|argument" } constexpr D() : B(global) { } // { dg-error "global|argument" }
......
...@@ -16,7 +16,7 @@ void Foo () ...@@ -16,7 +16,7 @@ void Foo ()
Y<I> i; Y<I> i;
static const unsigned J = X<T>::J; static const unsigned J = X<T>::J; // { dg-message "not initialized with a constant expression" }
Y<J> j; // { dg-error "constant" "" } Y<J> j; // { dg-error "constant" "" }
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
template <const int N> struct A { }; template <const int N> struct A { };
template <class T> struct B { template <class T> struct B {
static const int c; static const int c; // { dg-message "not initialized with a constant expression" }
typedef A<B<T>::c> C; // { dg-error "constant expression" } typedef A<B<T>::c> C; // { dg-error "constant expression" }
}; };
template <class T> const int B<T>::c = sizeof (T); template <class T> const int B<T>::c = sizeof (T);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment