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.
This commit is contained in:
Joseph Myers 2024-11-20 01:37:30 +00:00
parent 357708c653
commit 5afc98fec9
3 changed files with 19 additions and 1 deletions

View File

@ -8936,7 +8936,7 @@ check_constexpr_init (location_t loc, tree type, tree init,
if (!int_const_expr) if (!int_const_expr)
error_at (loc, "%<constexpr%> integer initializer is not an " error_at (loc, "%<constexpr%> integer initializer is not an "
"integer constant expression"); "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 " error_at (loc, "%<constexpr%> initializer not representable in "
"type of object"); "type of object");
return; return;

View File

@ -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" } */

View File

@ -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" } */