Commit 80fdaad1 by Jason Merrill Committed by Jason Merrill

PR c++/80227 - SFINAE and negative array size.

	* decl.c (compute_array_index_type): Use
	build_converted_constant_expr and valid_constant_size_p.

From-SVN: r258604
parent 929a0122
2018-03-16 Jason Merrill <jason@redhat.com> 2018-03-16 Jason Merrill <jason@redhat.com>
PR c++/80227 - SFINAE and negative array size.
* decl.c (compute_array_index_type): Use
build_converted_constant_expr and valid_constant_size_p.
PR c++/84906 - silent wrong code with ambiguous conversion. PR c++/84906 - silent wrong code with ambiguous conversion.
* call.c (build_user_type_conversion_1): Set need_temporary_p on * call.c (build_user_type_conversion_1): Set need_temporary_p on
ambiguous conversion. ambiguous conversion.
......
...@@ -9514,8 +9514,6 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) ...@@ -9514,8 +9514,6 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
if (!type_dependent_expression_p (size)) if (!type_dependent_expression_p (size))
{ {
tree type = TREE_TYPE (size);
size = mark_rvalue_use (size); size = mark_rvalue_use (size);
if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
...@@ -9525,29 +9523,8 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) ...@@ -9525,29 +9523,8 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
else else
{ {
size = instantiate_non_dependent_expr_sfinae (size, complain); size = instantiate_non_dependent_expr_sfinae (size, complain);
size = build_converted_constant_expr (size_type_node, size, complain);
if (CLASS_TYPE_P (type) size = maybe_constant_value (size);
&& CLASSTYPE_LITERAL_P (type))
{
size = build_expr_type_conversion (WANT_INT, size, true);
if (!size)
{
if (!(complain & tf_error))
return error_mark_node;
if (name)
error ("size of array %qD has non-integral type %qT",
name, type);
else
error ("size of array has non-integral type %qT", type);
size = integer_one_node;
}
if (size == error_mark_node)
return error_mark_node;
type = TREE_TYPE (size);
}
if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
size = maybe_constant_value (size);
if (!TREE_CONSTANT (size)) if (!TREE_CONSTANT (size))
size = osize; size = osize;
...@@ -9557,6 +9534,7 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) ...@@ -9557,6 +9534,7 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
return error_mark_node; return error_mark_node;
/* The array bound must be an integer type. */ /* The array bound must be an integer type. */
tree type = TREE_TYPE (size);
if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
{ {
if (!(complain & tf_error)) if (!(complain & tf_error))
...@@ -9566,7 +9544,6 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) ...@@ -9566,7 +9544,6 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
else else
error ("size of array has non-integral type %qT", type); error ("size of array has non-integral type %qT", type);
size = integer_one_node; size = integer_one_node;
type = TREE_TYPE (size);
} }
} }
...@@ -9604,15 +9581,12 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) ...@@ -9604,15 +9581,12 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
/* Normally, the array-bound will be a constant. */ /* Normally, the array-bound will be a constant. */
if (TREE_CODE (size) == INTEGER_CST) if (TREE_CODE (size) == INTEGER_CST)
{ {
/* Check to see if the array bound overflowed. Make that an
error, no matter how generous we're being. */
constant_expression_error (size);
/* An array must have a positive number of elements. */ /* An array must have a positive number of elements. */
if (tree_int_cst_lt (size, integer_zero_node)) if (!valid_constant_size_p (size))
{ {
if (!(complain & tf_error)) if (!(complain & tf_error))
return error_mark_node; return error_mark_node;
if (name) if (name)
error ("size of array %qD is negative", name); error ("size of array %qD is negative", name);
else else
......
// PR c++/80227
// { dg-do compile { target c++11 } }
template <class T>
int foo (T);
template <class T, class U = T [sizeof (T) - 5]>
int foo (T, U* = 0);
int i = foo (123);
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
void void
foo (float n) foo (float n)
{ {
int A[n][n]; // { dg-error "has non-integral type" } int A[n][n]; // { dg-error "has non-integral type|converted constant expression" }
#pragma omp parallel private(A) #pragma omp parallel private(A)
; ;
} }
...@@ -32,7 +32,7 @@ template <typename T> ...@@ -32,7 +32,7 @@ template <typename T>
void * void *
callnew_fail_3() callnew_fail_3()
{ {
return new T[2][T::n]; // { dg-error "size of array has non-integral type" } return new T[2][T::n]; // { dg-error "size of array has non-integral type|converted constant expression" }
} }
struct T1 { struct T1 {
......
...@@ -2,5 +2,4 @@ ...@@ -2,5 +2,4 @@
/* { dg-do compile } */ /* { dg-do compile } */
/* { dg-options "-fsanitize=undefined" } */ /* { dg-options "-fsanitize=undefined" } */
int a[(long) 4e20]; /* { dg-error "overflow in constant expression" } */ int a[(long) 4e20]; /* { dg-error "size of array .a. is (too large|negative)" } */
/* { dg-error "size of array .a. is too large" "" { target *-*-* } .-1 } */
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