https://gcc.gnu.org/g:5afc98fec97d84baa0c580809fb3e220444fa20d

commit r15-5481-g5afc98fec97d84baa0c580809fb3e220444fa20d
Author: Joseph Myers <josmy...@redhat.com>
Date:   Wed Nov 20 01:37:30 2024 +0000

    c: Fix ICE for integer constexpr initializers of wrong type [PR115515]
    
    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.

Diff:
---
 gcc/c/c-typeck.cc                        |  2 +-
 gcc/testsuite/gcc.dg/c23-constexpr-10.c  | 13 +++++++++++++
 gcc/testsuite/gcc.dg/gnu23-constexpr-2.c |  5 +++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 61145d7e4f0f..2edf3e941f10 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -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
index 000000000000..8ef2fced421d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-constexpr-10.c
@@ -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
index 000000000000..f570c2424f83
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/gnu23-constexpr-2.c
@@ -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" } */

Reply via email to