c++: -Wreturn-type with if (true) throw [PR107310]
authorJason Merrill <jason@redhat.com>
Tue, 14 Mar 2023 16:20:51 +0000 (12:20 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 14 Mar 2023 18:45:34 +0000 (14:45 -0400)
I removed this folding in GCC 12 because it was interfering with an
experiment of richi's, but that never went in and the change causes
regressions, so let's put it back.

This reverts commit r12-5638-ga3e75c1491cd2d.

PR c++/107310

gcc/cp/ChangeLog:

* cp-gimplify.cc (genericize_if_stmt): Restore folding
of constant conditions.

gcc/testsuite/ChangeLog:

* c-c++-common/Wimplicit-fallthrough-39.c: Adjust warning.
* g++.dg/warn/Wreturn-6.C: New test.

gcc/cp/cp-gimplify.cc
gcc/testsuite/c-c++-common/Wimplicit-fallthrough-39.c
gcc/testsuite/g++.dg/warn/Wreturn-6.C [new file with mode: 0644]

index b995b4f..4fecd56 100644 (file)
@@ -190,6 +190,12 @@ genericize_if_stmt (tree *stmt_p)
     }
   else if (IF_STMT_CONSTEXPR_P (stmt))
     stmt = integer_nonzerop (cond) ? then_ : else_;
+  /* ??? This optimization doesn't seem to belong here, but removing it
+     causes -Wreturn-type regressions (e.g. 107310).  */
+  else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
+    stmt = then_;
+  else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
+    stmt = else_;
   else
     stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
   protected_set_expr_location_if_unset (stmt, locus);
index da4aef3..d06bc0d 100644 (file)
@@ -37,8 +37,8 @@ fn2 (int n)
   switch (n)
     {
     case 0:
-      if (1)     /* { dg-warning "statement may fall through" "" { target c++ } } */
-               n++;      /* { dg-warning "statement may fall through" "" { target c } } */
+      if (1)
+               n++;      /* { dg-warning "statement may fall through" } */
     case 1:      /* { dg-message "here" } */
       return -1;
     }
diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-6.C b/gcc/testsuite/g++.dg/warn/Wreturn-6.C
new file mode 100644 (file)
index 0000000..85fef0e
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/107310
+
+struct f
+{
+  ~f();
+};
+
+int foo(int t) {
+  f g;
+  switch (t) {
+  case 1:
+    return 1;
+  }
+  if (true)
+    throw 1;
+} // { dg-bogus "control reaches end" }