From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id C9418385B51F; Wed, 18 Jan 2023 21:50:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9418385B51F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674078614; bh=7fUdc80YjGfF7GU215IIY3E876hOE+08RX0KFQpafBU=; h=From:To:Subject:Date:From; b=VRUrXzEc5eTlQIbOTyZfoXVWJRW4lKsZ/gP+VhXSQ8l4QcZXFGQEK5GJ7gGDWaTn0 BrQv0z6/trumUYy2FjcC7M2AIaYU5vm3f5L+ik5F5JKzUmd9UipLL+pr1EOUa6QVXY btvl+lyNROqiVmGBzWOKB1YW9TDGHfHBHzG6HPfI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5249] c: ICE with nullptr as case expression [PR108424] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 33237e93d59ee7bb215cd5b726880bcbd679bb36 X-Git-Newrev: 0f85ae6591c92b161693073c0931c7ca1d5d0c5a Message-Id: <20230118215014.C9418385B51F@sourceware.org> Date: Wed, 18 Jan 2023 21:50:14 +0000 (GMT) List-Id: https://gcc.gnu.org/g:0f85ae6591c92b161693073c0931c7ca1d5d0c5a commit r13-5249-g0f85ae6591c92b161693073c0931c7ca1d5d0c5a Author: Marek Polacek Date: Wed Jan 18 14:51:59 2023 -0500 c: ICE with nullptr as case expression [PR108424] 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. 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. Diff: --- gcc/c-family/c-common.cc | 3 ++- gcc/testsuite/gcc.dg/c2x-nullptr-6.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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 00000000000..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: + } +}