]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: delayed noexcept in member function template [PR99980]
authorMarek Polacek <polacek@redhat.com>
Wed, 15 Dec 2021 22:41:53 +0000 (17:41 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 16 Dec 2021 22:38:02 +0000 (17:38 -0500)
Some time ago I noticed that we don't properly delay parsing of
noexcept for member function templates.  This patch fixes that.

It didn't work because even though we set CP_PARSER_FLAGS_DELAY_NOEXCEPT
in cp_parser_member_declaration, member template declarations take
a different path: we call cp_parser_template_declaration and return
prior to setting the flag.

PR c++/99980

gcc/cp/ChangeLog:

* parser.c (cp_parser_single_declaration): Maybe pass
CP_PARSER_FLAGS_DELAY_NOEXCEPT down to cp_parser_init_declarator.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/noexcept71.C: New test.

gcc/cp/parser.c
gcc/testsuite/g++.dg/cpp0x/noexcept71.C [new file with mode: 0644]

index 9cf74357ac9e993e9fade3f451011af952146ea5..44eed7ea63877e9a04e423450c14283f8c70ec44 100644 (file)
@@ -31673,8 +31673,13 @@ cp_parser_single_declaration (cp_parser* parser,
       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
          || decl_specifiers.type != error_mark_node))
     {
+      int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
+      /* We don't delay parsing for friends, though CWG 2510 may change
+        that.  */
+      if (member_p && !(friend_p && *friend_p))
+       flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
       decl = cp_parser_init_declarator (parser,
-                                       CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
+                                       flags,
                                        &decl_specifiers,
                                        checks,
                                        /*function_definition_allowed_p=*/true,
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept71.C b/gcc/testsuite/g++.dg/cpp0x/noexcept71.C
new file mode 100644 (file)
index 0000000..361d6ad
--- /dev/null
@@ -0,0 +1,31 @@
+// PR c++/99980
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert(X, #X)
+
+struct S {
+  template<typename T>
+  void f(T) noexcept(B);
+
+  struct N {
+    template<typename T>
+    void f2(T) noexcept(B);
+  };
+
+  static constexpr bool B = true;
+};
+
+S s;
+SA(noexcept(s.f(10)));
+S::N n;
+SA(noexcept(n.f2(10)));
+
+struct Bad {
+  template<typename T>
+  using U = void() noexcept(B); // { dg-error "not declared" }
+
+  template<typename T>
+  friend void friendo() noexcept(B); // { dg-error "not declared" }
+
+  static constexpr bool B = true;
+};