]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
coroutines: Await expressions are not allowed in handlers [PR 99710].
authorIain Sandoe <iain@sandoe.co.uk>
Sat, 2 Oct 2021 13:43:39 +0000 (14:43 +0100)
committerIain Sandoe <iain@sandoe.co.uk>
Sun, 3 Oct 2021 19:53:45 +0000 (20:53 +0100)
C++20 [expr.await] / 2
An await-expression shall appear only in a potentially-evaluated expression
within the compound-statement of a function-body outside of a handler.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
PR c++/99710

gcc/cp/ChangeLog:

* coroutines.cc (await_statement_walker): Report an error if
an await expression is found in a handler body.

gcc/testsuite/ChangeLog:

* g++.dg/coroutines/pr99710.C: New test.

gcc/cp/coroutines.cc
gcc/testsuite/g++.dg/coroutines/pr99710.C [new file with mode: 0644]

index 1dd9abd1e1f3d57572c565b8800ffaf3449ac325..377b90c777fb9de0f8c94640527d1fab96f88d3c 100644 (file)
@@ -3688,7 +3688,22 @@ await_statement_walker (tree *stmt, int *do_subtree, void *d)
            *do_subtree = 0;
            return res;
          }
-       break;
+         break;
+       case HANDLER:
+         {
+           /* [expr.await] An await-expression shall appear only in a
+              potentially-evaluated expression within the compound-statement
+              of a function-body outside of a handler.  */
+           tree *await_ptr;
+           hash_set<tree> visited;
+           if (!(cp_walk_tree (&HANDLER_BODY (expr), find_any_await,
+                 &await_ptr, &visited)))
+             return NULL_TREE; /* All OK.  */
+           location_t loc = EXPR_LOCATION (*await_ptr);
+           error_at (loc, "await expressions are not permitted in handlers");
+           return NULL_TREE; /* This is going to fail later anyway.  */
+         }
+         break;
       }
   else if (EXPR_P (expr))
     {
diff --git a/gcc/testsuite/g++.dg/coroutines/pr99710.C b/gcc/testsuite/g++.dg/coroutines/pr99710.C
new file mode 100644 (file)
index 0000000..e4f7116
--- /dev/null
@@ -0,0 +1,25 @@
+#include <coroutine>
+
+struct task {
+    struct promise_type {
+        std::suspend_always initial_suspend();
+        std::suspend_always final_suspend() noexcept;
+        task get_return_object();
+        void return_void();
+        void unhandled_exception();
+    };
+};
+
+task
+my_coro ()
+{
+  try
+    { }
+  catch (...)
+    {
+      // [expr.await] An await-expression shall appear only in a potentially-
+      // evaluated expression within the compound-statement of a function-body
+      // outside of a handler 
+      co_await std::suspend_always{}; // { dg-error "await expressions are not permitted in handlers" }
+    }
+}