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 --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index a701dd090fb8..96e562c49046 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -8934,7 +8934,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" } */ -- Joseph S. Myers josmy...@redhat.com