Re: [PATCH] c: ICE with nullptr as case expression [PR108424]

2023-01-18 Thread Joseph Myers
On Wed, 18 Jan 2023, Marek Polacek via Gcc-patches wrote:

> In this ICE-on-invalid, we crash on
> 
>   gcc_assert (INTEGRAL_TYPE_P (type));
> 
> in perform_integral_promotions, because a nullptr is an INTEGER_CST,
> but not INTEGRAL_TYPE_P, and check_case_value is only checking the
> former.  In the test I'm testing other "shall be an integral constant
> expression" contexts as well.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.  (INTEGER_CST of pointer type is detected in c_add_case_label.)

-- 
Joseph S. Myers
jos...@codesourcery.com


[PATCH] c: ICE with nullptr as case expression [PR108424]

2023-01-18 Thread Marek Polacek via Gcc-patches
In this ICE-on-invalid, we crash on

  gcc_assert (INTEGRAL_TYPE_P (type));

in perform_integral_promotions, because a nullptr is an INTEGER_CST,
but not INTEGRAL_TYPE_P, and check_case_value is only checking the
former.  In the test I'm testing other "shall be an integral constant
expression" contexts as well.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c/108424

gcc/c-family/ChangeLog:

* c-common.cc (check_case_value): Check INTEGRAL_TYPE_P.

gcc/testsuite/ChangeLog:

* gcc.dg/c2x-nullptr-6.c: New test.
---
 gcc/c-family/c-common.cc |  3 ++-
 gcc/testsuite/gcc.dg/c2x-nullptr-6.c | 33 
 2 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/c2x-nullptr-6.c

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index 76c8abef296..ae92cd5adaf 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -2238,7 +2238,8 @@ check_case_value (location_t loc, tree value)
   if (value == NULL_TREE)
 return value;
 
-  if (TREE_CODE (value) == INTEGER_CST)
+  if (INTEGRAL_TYPE_P (TREE_TYPE (value))
+  && TREE_CODE (value) == INTEGER_CST)
 /* Promote char or short to int.  */
 value = perform_integral_promotions (value);
   else if (value != error_mark_node)
diff --git a/gcc/testsuite/gcc.dg/c2x-nullptr-6.c 
b/gcc/testsuite/gcc.dg/c2x-nullptr-6.c
new file mode 100644
index 000..24e14fa6921
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-nullptr-6.c
@@ -0,0 +1,33 @@
+/* PR c/108424 */
+/* { dg-options "-std=c2x" } */
+
+struct S {
+  int i;
+  int : nullptr; /* { dg-error "not an integer constant" } */
+};
+
+enum E { X = nullptr }; /* { dg-error "not an integer constant" } */
+
+alignas(nullptr) int g; /* { dg-error "not an integer constant" } */
+
+int arr[10] = { [nullptr] = 1 }; /* { dg-error "not of integer type" } */
+
+_Static_assert (nullptr, "nullptr"); /* { dg-error "not an integer" } */
+
+void f (int n)
+{
+  switch (n) {
+  case nullptr: /* { dg-error "an integer constant" } */
+  default:
+  }
+
+  switch (n) {
+  case 1 ... nullptr: /* { dg-error "an integer constant" } */
+  default:
+  }
+
+  switch (n) {
+  case nullptr ... 2: /* { dg-error "an integer constant" } */
+  default:
+  }
+}

base-commit: af7881e07631fc1c61deb307119f7cabdd4094a1
-- 
2.39.0