On 6/9/25 4:12 PM, Iain Sandoe wrote:
Hi Jason,
There was some discussion of this in the PR116775 comments. In the
end I have matched what clang does in this circumstance, since that
seems reasonable - and we may ignore the attributes as needed.
tested on x86-64-darwin, powerpc64le-linux, OK for trunk?
thanks
Iain

--- 8< ---

Here we have an expression that is not evaluated but is still seen
as potentially-evaluated.  We handle this by determining if the
operand has side-effects, producing a warning that the assume has
been ignored and eliding it.

gcc/cp/ChangeLog:

        * coroutines.cc (analyze_expression_awaits): Elide assume
        attributes containing await expressions, since these have
        side effects.  Emit a diagnostic that this has been done.

gcc/testsuite/ChangeLog:

        * g++.dg/coroutines/assume.C: New test.
---
  gcc/cp/coroutines.cc                     | 13 ++++++++
  gcc/testsuite/g++.dg/coroutines/assume.C | 40 ++++++++++++++++++++++++
  2 files changed, 53 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/coroutines/assume.C

diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index bbdc33abc0e..21b1d85b02c 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -3587,6 +3587,19 @@ analyze_expression_awaits (tree *stmt, int *do_subtree, 
void *d)
                }
              *do_subtree = 0;
            }
+         else if (!fn && CALL_EXPR_IFN (*stmt) == IFN_ASSUME)
+           {
+             tree expr = CALL_EXPR_ARG (*stmt, 0);
+             if (TREE_SIDE_EFFECTS (expr))
+               {
+                 location_t loc_e = cp_expr_location (expr);
+                 location_t loc_s = cp_expr_location (*stmt);
+                 location_t loc_n = make_location (loc_e, loc_s, loc_s);
+                 warning_at (loc_n, OPT_Wattributes,"assumption ignored"
+                             " because it contains possible side-effects");
+                 *stmt = build_empty_stmt (loc_n);
+               }
+           }

Why is this in coroutines.cc?

We do badly at assume of even trivial calls (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118995) but any warning about that should be emitted by the code that chooses to give up, not in the front end.

Jason

Reply via email to