Commit c80e3e02 by Jason Merrill Committed by Jason Merrill

re PR c++/48911 ([C++0x] Error for valid array subscript)

	PR c++/48911
	* semantics.c (cxx_eval_array_reference): Handle implicit
	initializers.

From-SVN: r173510
parent d8f426ec
2011-05-06 Jason Merrill <jason@redhat.com>
PR c++/48911
* semantics.c (cxx_eval_array_reference): Handle implicit
initializers.
2011-05-06 Nathan Froyd <froydnj@codesourcery.com> 2011-05-06 Nathan Froyd <froydnj@codesourcery.com>
* cp-tree.h (type_of_this_parm, class_of_this_parm): New functions. * cp-tree.h (type_of_this_parm, class_of_this_parm): New functions.
......
...@@ -6323,6 +6323,7 @@ cxx_eval_array_reference (const constexpr_call *call, tree t, ...@@ -6323,6 +6323,7 @@ cxx_eval_array_reference (const constexpr_call *call, tree t,
non_constant_p); non_constant_p);
tree index, oldidx; tree index, oldidx;
HOST_WIDE_INT i; HOST_WIDE_INT i;
tree elem_type;
unsigned len, elem_nchars = 1; unsigned len, elem_nchars = 1;
if (*non_constant_p) if (*non_constant_p)
return t; return t;
...@@ -6335,16 +6336,27 @@ cxx_eval_array_reference (const constexpr_call *call, tree t, ...@@ -6335,16 +6336,27 @@ cxx_eval_array_reference (const constexpr_call *call, tree t,
return t; return t;
else if (addr) else if (addr)
return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
elem_type = TREE_TYPE (TREE_TYPE (ary));
if (TREE_CODE (ary) == CONSTRUCTOR) if (TREE_CODE (ary) == CONSTRUCTOR)
len = CONSTRUCTOR_NELTS (ary); len = CONSTRUCTOR_NELTS (ary);
else else
{ {
elem_nchars = (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (ary))) elem_nchars = (TYPE_PRECISION (elem_type)
/ TYPE_PRECISION (char_type_node)); / TYPE_PRECISION (char_type_node));
len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
} }
if (compare_tree_int (index, len) >= 0) if (compare_tree_int (index, len) >= 0)
{ {
if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
{
/* If it's within the array bounds but doesn't have an explicit
initializer, it's value-initialized. */
tree val = build_value_init (elem_type, tf_warning_or_error);
return cxx_eval_constant_expression (call, val,
allow_non_constant, addr,
non_constant_p);
}
if (!allow_non_constant) if (!allow_non_constant)
error ("array subscript out of bound"); error ("array subscript out of bound");
*non_constant_p = true; *non_constant_p = true;
......
2011-05-06 Jason Merrill <jason@redhat.com>
* g++.dg/cpp0x/constexpr-missing.C: New.
2011-05-06 Tobias Burnus <burnus@net-b.de> 2011-05-06 Tobias Burnus <burnus@net-b.de>
PR fortran/18918 PR fortran/18918
......
// PR c++/48911
// { dg-do compile }
// { dg-options "-std=c++0x" }
#define SA(X) static_assert((X),#X)
struct A
{
constexpr A () : a (6) {}
int a;
};
int
main ()
{
constexpr int a[2] = { 42 };
constexpr int i = a[1];
SA(i==0);
constexpr int b[1] = { };
constexpr int j = b[0];
SA(j==0);
constexpr char c[2] = "a";
constexpr char k = c[1];
SA(k==0);
constexpr char d[2] = "";
constexpr char l = d[1];
SA(l==0);
constexpr wchar_t e[2] = L"a";
constexpr wchar_t m = e[1];
SA(m==0);
constexpr wchar_t f[2] = L"";
constexpr wchar_t n = f[1];
SA(n==0);
constexpr A g[2] = { A () };
constexpr A o = g[0];
SA(o.a == 6);
constexpr A p = g[1];
SA(p.a == 6);
}
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