https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100409

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
So I'm looking again at this.  For the simple cases I have at hand we can
fix the C++ FE issue with

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index d035e611be4..81097942449 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -1645,7 +1645,12 @@ convert_to_void (tree expr, impl_conv_void implicit,
tsub
st_flags_t complain)
        }
       expr = build1 (CONVERT_EXPR, void_type_node, expr);
     }
-  if (! TREE_SIDE_EFFECTS (expr))
+  if (! TREE_SIDE_EFFECTS (expr)
+      && ! (CONVERT_EXPR_P (expr)
+           && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
+           && ! TREE_NOTHROW (TREE_OPERAND (expr, 0)))
+      && ! (TREE_CODE (expr) == CALL_EXPR
+           && ! TREE_NOTHROW (expr)))
     expr = void_node;
   return expr;
 }

of course that relies on seeing f() or (void)f(), it already fails with
(void)(1 + f()).  We also diagnose such stmts as having no effect:

t.ii: In function 'int bar(int*, int)':
t.ii:13:10: warning: statement has no effect [-Wunused-value]
   13 |        1 + foo (n);
      |        ~~^~~~~~~~~

there's expr_noexcept_p that seems to work here even for 1 + foo () but
I'm not sure it's the correct tool.  But it would simplify a fix to

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index d035e611be4..4885a7c6584 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -1645,7 +1645,7 @@ convert_to_void (tree expr, impl_conv_void implicit,
tsubst_flags_t complain)
        }
       expr = build1 (CONVERT_EXPR, void_type_node, expr);
     }
-  if (! TREE_SIDE_EFFECTS (expr))
+  if (! TREE_SIDE_EFFECTS (expr) && expr_noexcept_p (expr, 0))
     expr = void_node;
   return expr;
 }

I'm giving that full testing.

Reply via email to