]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with throw inside concept [PR112437]
authorMarek Polacek <polacek@redhat.com>
Wed, 31 Jan 2024 22:33:26 +0000 (17:33 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 1 Feb 2024 15:49:32 +0000 (10:49 -0500)
We crash in the loop at the end of treat_lvalue_as_rvalue_p for code
like

  template <class T>
  concept Throwable = requires(T x) { throw x; };

because the code assumes that we eventually reach sk_function_parms or
sk_try and bail, but in a concept we're in a sk_namespace.

We're already checking sk_try so we don't crash in a function-try-block,
but I've added a test anyway.

PR c++/112437

gcc/cp/ChangeLog:

* typeck.cc (treat_lvalue_as_rvalue_p): Bail out on sk_namespace in
the move on throw of parms loop.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-throw1.C: New test.
* g++.dg/eh/throw4.C: New test.

(cherry picked from commit 65b105b4f399559685200e1598ead8c7d0935c04)

gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp2a/concepts-throw1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/eh/throw4.C [new file with mode: 0644]

index cc949dab0e78b0477261476f2f6dc64c9f04e449..ff8f0ef0083847e50bb77a786a0dda51f76c2c2a 100644 (file)
@@ -10362,7 +10362,9 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p)
       for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
        if (decl == retval)
          return set_implicit_rvalue_p (move (expr));
-      if (b->kind == sk_function_parms || b->kind == sk_try)
+      if (b->kind == sk_function_parms
+         || b->kind == sk_try
+         || b->kind == sk_namespace)
        return NULL_TREE;
     }
 }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-throw1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-throw1.C
new file mode 100644 (file)
index 0000000..bc3e3b6
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/112437
+// { dg-do compile { target c++20 } }
+
+struct S {};
+template <class T>
+concept Throwable = requires(T x) { throw x; };
+
+bool a = Throwable<S>;
diff --git a/gcc/testsuite/g++.dg/eh/throw4.C b/gcc/testsuite/g++.dg/eh/throw4.C
new file mode 100644 (file)
index 0000000..b474472
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/112437
+// { dg-do compile }
+
+struct S {};
+
+S
+foo (S s)
+try {
+  throw s;
+}
+catch (...) {
+  throw s;
+}