https://gcc.gnu.org/g:1d5779274ce9807358f9e04f1112b65c6ed6c284

commit r11-11543-g1d5779274ce9807358f9e04f1112b65c6ed6c284
Author: Iain Sandoe <i...@sandoe.co.uk>
Date:   Sat Oct 2 16:15:38 2021 +0100

    coroutines: Fail with a sorry when presented with a VLA [PR 101765].
    
    We do not support this yet.
    
    Signed-off-by: Iain Sandoe <i...@sandoe.co.uk>
    
            PR c++/101765
    
    gcc/cp/ChangeLog:
    
            * coroutines.cc (register_local_var_uses): Emit a sorry if
            we encounter a VLA in the coroutine local variables.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/coroutines/pr101765.C: New test.
    
    (cherry picked from commit fdf0b6ce6c1cfa1c328c0c40473c71ca11fd8303)

Diff:
---
 gcc/cp/coroutines.cc                       | 10 +++++++
 gcc/testsuite/g++.dg/coroutines/pr101765.C | 45 ++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index 04c72ddd48b..406c85c4176 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -3941,6 +3941,16 @@ register_local_var_uses (tree *stmt, int *do_subtree, 
void *d)
          if (local_var.is_static)
            continue;
 
+         poly_uint64 size;
+         if (TREE_CODE (lvtype) == ARRAY_TYPE
+             && !poly_int_tree_p (DECL_SIZE_UNIT (lvar), &size))
+           {
+             sorry_at (local_var.def_loc, "variable length arrays are not"
+                       " yet supported in coroutines");
+             /* Ignore it, this is broken anyway.  */
+             continue;
+           }
+
          lvd->local_var_seen = true;
          /* If this var is a lambda capture proxy, we want to leave it alone,
             and later rewrite the DECL_VALUE_EXPR to indirect through the
diff --git a/gcc/testsuite/g++.dg/coroutines/pr101765.C 
b/gcc/testsuite/g++.dg/coroutines/pr101765.C
new file mode 100644
index 00000000000..49a49d11299
--- /dev/null
+++ b/gcc/testsuite/g++.dg/coroutines/pr101765.C
@@ -0,0 +1,45 @@
+// We cannot compile this yet, much run it - but one day it might be
+// feasible, so do the minimum for now.
+// { dg-additional-options " -fsyntax-only -Wno-vla" }
+
+#include "coro.h"
+
+// boiler-plate for tests of codegen
+#include "coro1-ret-int-yield-int.h"
+
+struct coro1
+foo (int arg) noexcept
+{
+  PRINTF ("foo arg = %d\n", arg);
+  char arr[arg]; /* { dg-message "sorry, unimplemented: variable length arrays 
are not yet supported in coroutines" "" { target *-*-* } } */
+  if (arg < 4)
+    co_return -6174;
+  else
+    for (int i = 0; i < arg; ++i) arr[i] = (char) i;
+  co_yield (int) arr[2];
+  co_return (int) arr[3];
+}
+
+int main ()
+{
+  PRINT ("main: create coro1");
+  struct coro1 x = foo (10);
+  PRINT ("main: got coro1 - resuming");
+  if (x.handle.done())
+    abort();
+  x.handle.resume();
+  PRINT ("main: after resume");
+  int y = x.handle.promise().get_value();
+  if ( y == -6174 )
+    {
+      PRINT ("main: saw -6174");
+      return 1;
+    }
+  else if ( y != 2 )
+    abort;
+  x.handle.resume();
+  y = x.handle.promise().get_value();
+  if ( y != 3 )
+    abort ();
+  return 0;
+}

Reply via email to