diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index 8b097c46158..b80010e0983 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -166,8 +166,15 @@ genericize_if_stmt (tree *stmt_p) can contain unfolded immediate function calls, we have to discard the then_ block regardless of whether else_ has side-effects or not. */ if (IF_STMT_CONSTEVAL_P (stmt)) - stmt = build3 (COND_EXPR, void_type_node, boolean_false_node, - void_node, else_); + { + if (block_may_fallthru (then_)) + stmt = build3 (COND_EXPR, void_type_node, boolean_false_node, + void_node, else_); + else + stmt = else_; + } + else if (IF_STMT_CONSTEXPR_P (stmt)) + stmt = integer_nonzerop (cond) ? then_ : else_; else stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_); protected_set_expr_location_if_unset (stmt, locus); diff --git a/gcc/cp/cp-objcp-common.c b/gcc/cp/cp-objcp-common.c index fd6fe17e7da..b31c9e3a636 100644 --- a/gcc/cp/cp-objcp-common.c +++ b/gcc/cp/cp-objcp-common.c @@ -313,6 +313,13 @@ cxx_block_may_fallthru (const_tree stmt) return false; case IF_STMT: + if (IF_STMT_CONSTEXPR_P (stmt)) + { + if (integer_nonzerop (IF_COND (stmt))) + return block_may_fallthru (THEN_CLAUSE (stmt)); + if (integer_zerop (IF_COND (stmt))) + return block_may_fallthru (ELSE_CLAUSE (stmt)); + } if (block_may_fallthru (THEN_CLAUSE (stmt))) return true; return block_may_fallthru (ELSE_CLAUSE (stmt)); diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-type-13.C b/gcc/testsuite/g++.dg/warn/Wreturn-type-13.C new file mode 100644 index 00000000000..fa2952b735c --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wreturn-type-13.C @@ -0,0 +1,35 @@ +// PR c++/103991 +// { dg-do compile { target c++17 } } + +struct S { ~S(); }; +int +foo () +{ + S s; + if constexpr (true) + return 0; + else + return 1; +} // { dg-bogus "control reaches end of non-void function" } + +#if __cpp_if_consteval >= 202106L +constexpr int +bar () +{ + S s; + if consteval + { + return 0; + } + else + { + return 1; + } +} // { dg-bogus "control reaches end of non-void function" } + +int +baz () +{ + return bar (); +} +#endif