* [PATCH] c++: make __is_array return false for T[0] [PR114479]
@ 2024-04-01 17:50 Marek Polacek
2024-04-02 17:15 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2024-04-01 17:50 UTC (permalink / raw)
To: Jason Merrill, GCC Patches
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
When we switched to using the __is_array built-in trait to implement
std::is_array in r14-6623-g7fd9c349e45534, we started saying that
T[0] is an array. There are various opinions as to whether that is
the best answer, but it seems prudent to keep the GCC 13 result.
PR c++/114479
gcc/cp/ChangeLog:
* semantics.cc (trait_expr_value) <case CPTK_IS_ARRAY>: Return false
for zero-sized arrays.
gcc/testsuite/ChangeLog:
* g++.dg/ext/is_array.C: Extend.
---
gcc/cp/semantics.cc | 4 +++-
gcc/testsuite/g++.dg/ext/is_array.C | 12 ++++++++++++
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 9838331d2a9..f561c119dfd 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12439,7 +12439,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
return CP_AGGREGATE_TYPE_P (type1);
case CPTK_IS_ARRAY:
- return type_code1 == ARRAY_TYPE;
+ return (type_code1 == ARRAY_TYPE
+ /* We don't want to report T[0] as being an array type. */
+ && !(TYPE_SIZE (type1) && integer_zerop (TYPE_SIZE (type1))));
case CPTK_IS_ASSIGNABLE:
return is_xible (MODIFY_EXPR, type1, type2);
diff --git a/gcc/testsuite/g++.dg/ext/is_array.C b/gcc/testsuite/g++.dg/ext/is_array.C
index f1a6e08b87a..84993266629 100644
--- a/gcc/testsuite/g++.dg/ext/is_array.C
+++ b/gcc/testsuite/g++.dg/ext/is_array.C
@@ -1,4 +1,5 @@
// { dg-do compile { target c++11 } }
+// { dg-options "" }
#define SA(X) static_assert((X),#X)
@@ -10,18 +11,29 @@
class ClassType { };
+constexpr int sz0 = 0;
+constexpr int sz2 = 2;
+
SA_TEST_CATEGORY(__is_array, int[2], true);
SA_TEST_CATEGORY(__is_array, int[], true);
+SA_TEST_CATEGORY(__is_array, int[0], false);
SA_TEST_CATEGORY(__is_array, int[2][3], true);
SA_TEST_CATEGORY(__is_array, int[][3], true);
+SA_TEST_CATEGORY(__is_array, int[0][3], false);
+SA_TEST_CATEGORY(__is_array, int[3][0], false);
SA_TEST_CATEGORY(__is_array, float*[2], true);
SA_TEST_CATEGORY(__is_array, float*[], true);
SA_TEST_CATEGORY(__is_array, float*[2][3], true);
SA_TEST_CATEGORY(__is_array, float*[][3], true);
SA_TEST_CATEGORY(__is_array, ClassType[2], true);
SA_TEST_CATEGORY(__is_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_array, ClassType[0], false);
SA_TEST_CATEGORY(__is_array, ClassType[2][3], true);
SA_TEST_CATEGORY(__is_array, ClassType[][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[0][3], false);
+SA_TEST_CATEGORY(__is_array, ClassType[2][0], false);
+SA_TEST_CATEGORY(__is_array, int[sz2], true);
+SA_TEST_CATEGORY(__is_array, int[sz0], false);
// Sanity check.
SA_TEST_CATEGORY(__is_array, ClassType, false);
base-commit: bba118db3f63cb1e3953a014aa3ac2ad89908950
--
2.44.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: make __is_array return false for T[0] [PR114479]
2024-04-01 17:50 [PATCH] c++: make __is_array return false for T[0] [PR114479] Marek Polacek
@ 2024-04-02 17:15 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-04-02 17:15 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On 4/1/24 13:50, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
> -- >8 --
> When we switched to using the __is_array built-in trait to implement
> std::is_array in r14-6623-g7fd9c349e45534, we started saying that
> T[0] is an array. There are various opinions as to whether that is
> the best answer, but it seems prudent to keep the GCC 13 result.
>
> PR c++/114479
>
> gcc/cp/ChangeLog:
>
> * semantics.cc (trait_expr_value) <case CPTK_IS_ARRAY>: Return false
> for zero-sized arrays.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/ext/is_array.C: Extend.
> ---
> gcc/cp/semantics.cc | 4 +++-
> gcc/testsuite/g++.dg/ext/is_array.C | 12 ++++++++++++
> 2 files changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 9838331d2a9..f561c119dfd 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -12439,7 +12439,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
> return CP_AGGREGATE_TYPE_P (type1);
>
> case CPTK_IS_ARRAY:
> - return type_code1 == ARRAY_TYPE;
> + return (type_code1 == ARRAY_TYPE
> + /* We don't want to report T[0] as being an array type. */
Please elaborate that this is for compatibility with an implementation
of is_array by template argument deduction, because
compute_array_index_type_loc rejects a zero-size array in SFINAE context.
OK with that adjustment.
> + && !(TYPE_SIZE (type1) && integer_zerop (TYPE_SIZE (type1))));
>
> case CPTK_IS_ASSIGNABLE:
> return is_xible (MODIFY_EXPR, type1, type2);
> diff --git a/gcc/testsuite/g++.dg/ext/is_array.C b/gcc/testsuite/g++.dg/ext/is_array.C
> index f1a6e08b87a..84993266629 100644
> --- a/gcc/testsuite/g++.dg/ext/is_array.C
> +++ b/gcc/testsuite/g++.dg/ext/is_array.C
> @@ -1,4 +1,5 @@
> // { dg-do compile { target c++11 } }
> +// { dg-options "" }
>
> #define SA(X) static_assert((X),#X)
>
> @@ -10,18 +11,29 @@
>
> class ClassType { };
>
> +constexpr int sz0 = 0;
> +constexpr int sz2 = 2;
> +
> SA_TEST_CATEGORY(__is_array, int[2], true);
> SA_TEST_CATEGORY(__is_array, int[], true);
> +SA_TEST_CATEGORY(__is_array, int[0], false);
> SA_TEST_CATEGORY(__is_array, int[2][3], true);
> SA_TEST_CATEGORY(__is_array, int[][3], true);
> +SA_TEST_CATEGORY(__is_array, int[0][3], false);
> +SA_TEST_CATEGORY(__is_array, int[3][0], false);
> SA_TEST_CATEGORY(__is_array, float*[2], true);
> SA_TEST_CATEGORY(__is_array, float*[], true);
> SA_TEST_CATEGORY(__is_array, float*[2][3], true);
> SA_TEST_CATEGORY(__is_array, float*[][3], true);
> SA_TEST_CATEGORY(__is_array, ClassType[2], true);
> SA_TEST_CATEGORY(__is_array, ClassType[], true);
> +SA_TEST_CATEGORY(__is_array, ClassType[0], false);
> SA_TEST_CATEGORY(__is_array, ClassType[2][3], true);
> SA_TEST_CATEGORY(__is_array, ClassType[][3], true);
> +SA_TEST_CATEGORY(__is_array, ClassType[0][3], false);
> +SA_TEST_CATEGORY(__is_array, ClassType[2][0], false);
> +SA_TEST_CATEGORY(__is_array, int[sz2], true);
> +SA_TEST_CATEGORY(__is_array, int[sz0], false);
>
> // Sanity check.
> SA_TEST_CATEGORY(__is_array, ClassType, false);
>
> base-commit: bba118db3f63cb1e3953a014aa3ac2ad89908950
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-02 17:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-01 17:50 [PATCH] c++: make __is_array return false for T[0] [PR114479] Marek Polacek
2024-04-02 17:15 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).