diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 61145d7e4f0..2edf3e941f1 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, "% 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, "% 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 00000000000..8ef2fced421 --- /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 00000000000..f570c2424f8 --- /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" } */