]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Fix ICE for integer constexpr initializers of wrong type [PR115515]
authorJoseph Myers <josmyers@redhat.com>
Wed, 20 Nov 2024 01:37:30 +0000 (01:37 +0000)
committerJoseph Myers <josmyers@redhat.com>
Wed, 20 Nov 2024 01:39:06 +0000 (01:39 +0000)
Bug 115515 (plus its duplicate 117139) reports an ICE with constexpr
initializer for an integer type variable that is not of integer type.
Fix this by not calling int_fits_type_p unless the previous check for
an integer constant expression passes.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

PR c/115515

gcc/c/
* c-typeck.cc (check_constexpr_init): Do not call int_fits_type_p
for arguments that are not integer constant expressions.

gcc/testsuite/
* gcc.dg/c23-constexpr-10.c, gcc.dg/gnu23-constexpr-2.c: New
tests.

gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/c23-constexpr-10.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/gnu23-constexpr-2.c [new file with mode: 0644]

index 61145d7e4f0ffe02759c18598ecd166326d95f37..2edf3e941f1007e82b8b7c9d4519b87e75325dc4 100644 (file)
@@ -8936,7 +8936,7 @@ check_constexpr_init (location_t loc, tree type, tree init,
       if (!int_const_expr)
        error_at (loc, "%<constexpr%> integer initializer is not an "
                  "integer constant expression");
-      if (!int_fits_type_p (init, type))
+      else if (!int_fits_type_p (init, type))
        error_at (loc, "%<constexpr%> initializer not representable in "
                  "type of object");
       return;
diff --git a/gcc/testsuite/gcc.dg/c23-constexpr-10.c b/gcc/testsuite/gcc.dg/c23-constexpr-10.c
new file mode 100644 (file)
index 0000000..8ef2fce
--- /dev/null
@@ -0,0 +1,13 @@
+/* Test C23 constexpr.  Invalid types of integer initializers (bug 115515).  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+struct s { float x; };
+const struct s i = { 3.1 };
+constexpr int j = i.x; /* { dg-error "constexpr' integer initializer is not an integer constant expression" } */
+
+constexpr struct s i2 = { 3.25f };
+constexpr int j2 = i2.x; /* { dg-error "constexpr' integer initializer is not an integer constant expression" } */
+
+constexpr int j3 = 2 * 2.5; /* { dg-error "constexpr' integer initializer is not an integer constant expression" } */
+constexpr int j4 = 5.0; /* { dg-error "constexpr' integer initializer is not an integer constant expression" } */
diff --git a/gcc/testsuite/gcc.dg/gnu23-constexpr-2.c b/gcc/testsuite/gcc.dg/gnu23-constexpr-2.c
new file mode 100644 (file)
index 0000000..f570c24
--- /dev/null
@@ -0,0 +1,5 @@
+/* Test C23 constexpr.  Invalid types of integer initializers (bug 115515).  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu23" } */
+
+constexpr int i = 5i; /* { dg-error "constexpr' integer initializer is not an integer constant expression" } */