]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: unroll pragma in templates [PR111529]
authorJason Merrill <jason@redhat.com>
Fri, 22 Sep 2023 11:10:11 +0000 (12:10 +0100)
committerJason Merrill <jason@redhat.com>
Fri, 24 May 2024 13:26:35 +0000 (09:26 -0400)
We were failing to handle ANNOTATE_EXPR in tsubst_copy_and_build, leading to
problems with substitution of any wrapped expressions.

Let's also not tell users that lambda templates are available in C++14.

PR c++/111529

gcc/cp/ChangeLog:

* parser.cc (cp_parser_lambda_declarator_opt): Don't suggest
-std=c++14 for lambda templates.
* pt.cc (tsubst_expr): Move ANNOTATE_EXPR handling...
(tsubst_copy_and_build): ...here.

gcc/testsuite/ChangeLog:

* g++.dg/ext/unroll-4.C: New test.

(cherry picked from commit 9c62af101e11e1cce573c2b3d2e18b403412dbc8)

gcc/cp/parser.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/ext/unroll-4.C [new file with mode: 0644]

index 194f9b07d838fd3515cdde56c42d8b17055cae78..9c1bd32cff1cc1133a2417025b4c98315fc46acb 100644 (file)
@@ -11513,11 +11513,8 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
      an opening angle if present.  */
   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
     {
-      if (cxx_dialect < cxx14)
-       pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
-                "lambda templates are only available with "
-                "%<-std=c++14%> or %<-std=gnu++14%>");
-      else if (pedantic && cxx_dialect < cxx20)
+      if (cxx_dialect < cxx20
+         && (pedantic || cxx_dialect < cxx14))
        pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
                 "lambda templates are only available with "
                 "%<-std=c++20%> or %<-std=gnu++20%>");
index ed4dbe06179e17e0cfe881efd4ac08c8ec054ba6..1dcdaad571a7b90b0ff6e2f4b119694d473c2abe 100644 (file)
@@ -19616,13 +19616,6 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
                                    templated_operator_saved_lookups (t),
                                    complain));
 
-    case ANNOTATE_EXPR:
-      tmp = RECUR (TREE_OPERAND (t, 0));
-      RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
-                         TREE_TYPE (tmp), tmp,
-                         RECUR (TREE_OPERAND (t, 1)),
-                         RECUR (TREE_OPERAND (t, 2))));
-
     case PREDICT_EXPR:
       RETURN (add_stmt (copy_node (t)));
 
@@ -21551,6 +21544,13 @@ tsubst_copy_and_build (tree t,
                                          EXPR_LOCATION (t)));
       /* fallthrough.  */
 
+    case ANNOTATE_EXPR:
+      op1 = RECUR (TREE_OPERAND (t, 0));
+      RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
+                         TREE_TYPE (op1), op1,
+                         RECUR (TREE_OPERAND (t, 1)),
+                         RECUR (TREE_OPERAND (t, 2))));
+
     default:
       /* Handle Objective-C++ constructs, if appropriate.  */
       {
diff --git a/gcc/testsuite/g++.dg/ext/unroll-4.C b/gcc/testsuite/g++.dg/ext/unroll-4.C
new file mode 100644 (file)
index 0000000..d488aca
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/111529
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -Wno-c++20-extensions }
+
+template <int>
+void f() {
+  []<int>() {
+    #pragma GCC unroll 9
+    for (int i = 1; i; --i) {
+    }
+  };
+}
+
+int main() {
+  f<0>();
+}