From patchwork Mon Mar 4 14:18:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1907639 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=A8j2MAUh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TpLR10QGLz23hX for ; Tue, 5 Mar 2024 01:18:53 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E7E703858D33 for ; Mon, 4 Mar 2024 14:18:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 2F8AA3858D28 for ; Mon, 4 Mar 2024 14:18:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F8AA3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2F8AA3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709561909; cv=none; b=Fnc1EBBgwyNCfMadIBtvTNeIHJMhjScRbJJfXD3rCnGiG2Fw8AFmazhchJa4R8sLtr+V05QPcNOhjKvu89fN79FwWoOUQginT3qcPQUTQFxfX78qwvydzW599mePBlEnfKC9gV6w14y4nTVp24hPgyVwGAHlBR1ulPF9xEFLgrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709561909; c=relaxed/simple; bh=DbMxc9qswh51zg3uzK2EfEmZGiCu3CF84s8KwnYl7Ns=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=gnMsRhA5UvHuTLE02k5CSTIEzrjB4FA9kBgpaVJtlefO4fLQS1mHfYoYLwnAuanLRNDwgw4XHChIWd6KLiXOSVv3KCGQEftcvkVkSqGE8XxzLKunQFxDHoFItS3Du73kjYeloQZP94mg1OTkRUMs32AaEJvCXzjPBTr4aeZ1LOU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1dca3951ad9so43094575ad.3 for ; Mon, 04 Mar 2024 06:18:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709561905; x=1710166705; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=kJWxw8cfa0Yxy6qddSc8Kv92Vi+VfB6c4fJXul8QA2Q=; b=A8j2MAUhpR3AKD3O5Cey/saB8Np5HAmkYur56kNZFWvfP4KRZSG/7rMO9AbykIIUSt 6zJAKyi5hAt5fhTDdsCPmo4cx3NQevABGj7DhCum91ZZqe5wGlvKzwC7kejqAIAgDuE8 za1AJ/cpJvK9IzD/VO5Mw6IkRevFhEMrAJu3bz/jsGWja46oZJltUgGMgN4y1/vW86Zd ENcPJlBnycoLnKWn0wdJuRtXnUTjmWwVoxL9H6TSM4uf4j9pld1e5erCFmIEGPf8avh+ UQ7R/xiWHmJqppRBWZgz2onpM9QRc9nfIkQHVixODYxu2Rj7OcYxRqlbu7oMBaII7268 N1Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709561905; x=1710166705; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kJWxw8cfa0Yxy6qddSc8Kv92Vi+VfB6c4fJXul8QA2Q=; b=iVhmohsjDq/Vncm9kiQuRbVkSdvVLqYygNg6/6hBD5J91y6UW5y/pmXUzPE1RqBKF/ dBcxBVKE+NJnTjzfn+SpycA3fL6FUyHVFrdPDriSkglp12unGgNg/Uda5fZomJpV+tVy BfWtUrzGXzwOnCpp9EvxznVe3oVdVBQhz5FZDIy4Zg+PkFMzLcDBBtG3oaz21RB2L0Je ukFninDg6rij/z2UxXodayqglwfv3+OZ4pSLLxNeP8Kao83p9CIYL4d/WzXTfBuRq6mk D94h9zm0182qGsx7pO3EsMlwOsV0nNcTaM0JvnUHYEeoZN/HNjwLdwWuoQDvm2Ct6ak3 a+xg== X-Gm-Message-State: AOJu0Yxx16r89skXSD2oVcx8RRA3d4ly8fciFD1wTNnOq3a+LPu9pUa0 SPVBSePSG+ocUJQEJ/tiWONTPLfWGv40eghmjOC46hEiLGGhy/Y8shzLZElA X-Google-Smtp-Source: AGHT+IEWXGMZmQOiDvyBtcWtVbNysEYeSSOL4CvR0gRMyw/I9pfXYB7SNO7Qs1bWsF0zzoSpmZ7QTg== X-Received: by 2002:a17:903:1111:b0:1db:edfa:7713 with SMTP id n17-20020a170903111100b001dbedfa7713mr8656691plh.18.1709561904898; Mon, 04 Mar 2024 06:18:24 -0800 (PST) Received: from Thaum. (110-175-172-107.tpgi.com.au. [110.175.172.107]) by smtp.gmail.com with ESMTPSA id e7-20020a17090301c700b001db9e12cd62sm8558108plh.10.2024.03.04.06.18.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 06:18:24 -0800 (PST) Message-ID: <65e5d830.170a0220.c79ee.08cb@mx.google.com> X-Google-Original-Message-ID: Date: Tue, 5 Mar 2024 01:18:17 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Implement P2615 'Meaningful Exports' [PR107688] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu. This should probably wait for GCC 15 I suppose, but sending it in now in case there are any comments. -- >8 -- This clarifies which kinds of declarations may and may not be exported in various contexts. The patch additionally fixes up some small issues that were clarified by the paper. Most of the changes are with regards to export-declarations, which are applied for all standards modes that we support '-fmodules-ts' for. However there are also a couple of changes made to linkage specifiers ('extern "C"'); I've applied these as since C++20, to line up with when modules were actually introduced. PR c++/107688 gcc/cp/ChangeLog: * name-lookup.cc (push_namespace): Error when exporting namespace with internal linkage. * parser.h (struct cp_parser): Add new flag 'in_unbraced_export_declaration_p'. * parser.cc (cp_debug_parser): Print the new flag. (cp_parser_new): Initialise the new flag. (cp_parser_module_export): Set the new flag. (cp_parser_class_specifier): Clear and restore the new flag. (cp_parser_import_declaration): Imports can now appear directly in a linkage specification. (cp_parser_declaration): Categorise declarations as "name" or "special"; error on the later in contexts where the former is required. (cp_parser_class_head): Error when exporting a partial specialisation. gcc/testsuite/ChangeLog: * g++.dg/modules/contracts-1_a.C: Avoid now-illegal syntax. * g++.dg/modules/contracts-2_a.C: Likewise. * g++.dg/modules/contracts-3_a.C: Likewise. * g++.dg/modules/contracts-4_a.C: Likewise. * g++.dg/modules/lang-1_c.C: Clarify now-legal syntax. * g++.dg/template/crash71.C: Update error messages. * g++.dg/cpp2a/linkage-spec1.C: New test. * g++.dg/modules/export-3.C: New test. * g++.dg/modules/export-4_a.C: New test. * g++.dg/modules/export-4_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/name-lookup.cc | 10 +- gcc/cp/parser.cc | 105 +++++++++++++++---- gcc/cp/parser.h | 6 +- gcc/testsuite/g++.dg/cpp2a/linkage-spec1.C | 22 ++++ gcc/testsuite/g++.dg/modules/contracts-1_a.C | 2 +- gcc/testsuite/g++.dg/modules/contracts-2_a.C | 2 +- gcc/testsuite/g++.dg/modules/contracts-3_a.C | 2 +- gcc/testsuite/g++.dg/modules/contracts-4_a.C | 2 +- gcc/testsuite/g++.dg/modules/export-3.C | 30 ++++++ gcc/testsuite/g++.dg/modules/export-4_a.C | 23 ++++ gcc/testsuite/g++.dg/modules/export-4_b.C | 13 +++ gcc/testsuite/g++.dg/modules/lang-1_c.C | 2 +- gcc/testsuite/g++.dg/template/crash71.C | 4 +- 13 files changed, 192 insertions(+), 31 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/linkage-spec1.C create mode 100644 gcc/testsuite/g++.dg/modules/export-3.C create mode 100644 gcc/testsuite/g++.dg/modules/export-4_a.C create mode 100644 gcc/testsuite/g++.dg/modules/export-4_b.C diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 6444db3f0eb..743102d8393 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -9053,8 +9053,14 @@ push_namespace (tree name, bool make_inline) { /* A public namespace is exported only if explicitly marked, or it contains exported entities. */ - if (TREE_PUBLIC (ns) && module_exporting_p ()) - DECL_MODULE_EXPORT_P (ns) = true; + if (module_exporting_p ()) + { + if (TREE_PUBLIC (ns)) + DECL_MODULE_EXPORT_P (ns) = true; + else if (!header_module_p ()) + error_at (input_location, + "exporting namespace with internal linkage"); + } if (module_purview_p ()) DECL_MODULE_PURVIEW_P (ns) = true; diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index a310b9e8c07..448392e1bd9 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -560,6 +560,8 @@ cp_debug_parser (FILE *file, cp_parser *parser) & THIS_FORBIDDEN)); cp_debug_print_flag (file, "In unbraced linkage specification", parser->in_unbraced_linkage_specification_p); + cp_debug_print_flag (file, "In unbraced export declaration", + parser->in_unbraced_export_declaration_p); cp_debug_print_flag (file, "Parsing a declarator", parser->in_declarator_p); cp_debug_print_flag (file, "In template argument list", @@ -4425,6 +4427,9 @@ cp_parser_new (cp_lexer *lexer) /* We are not processing an `extern "C"' declaration. */ parser->in_unbraced_linkage_specification_p = false; + /* We aren't parsing an export-declaration. */ + parser->in_unbraced_export_declaration_p = false; + /* We are not processing a declarator. */ parser->in_declarator_p = false; @@ -15249,10 +15254,6 @@ cp_parser_import_declaration (cp_parser *parser, module_parse mp_state, goto skip_eol; cp_parser_require_pragma_eol (parser, token); - if (parser->in_unbraced_linkage_specification_p) - error_at (token->location, "import cannot appear directly in" - " a linkage-specification"); - if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS) { /* Module-purview imports must not be from source inclusion @@ -15273,7 +15274,7 @@ cp_parser_import_declaration (cp_parser *parser, module_parse mp_state, /* export-declaration. - export declaration + export name-declaration export { declaration-seq-opt } */ static void @@ -15315,7 +15316,13 @@ cp_parser_module_export (cp_parser *parser) || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT)) error_at (token->location, "% not part of following" " module-directive"); + + bool saved_in_unbraced_export_declaration_p + = parser->in_unbraced_export_declaration_p; + parser->in_unbraced_export_declaration_p = true; cp_parser_declaration (parser, NULL_TREE); + parser->in_unbraced_export_declaration_p + = saved_in_unbraced_export_declaration_p; } module_kind = mk; @@ -15346,27 +15353,29 @@ cp_parser_declaration_seq_opt (cp_parser* parser) } } -/* Parse a declaration. +/* Parse a declaration. The distinction between name-declaration + and special-declaration is only since C++20. declaration: + name-declaration + special-declaration + + name-declaration: block-declaration + nodeclspec-function-declaration function-definition template-declaration - explicit-instantiation - explicit-specialization + deduction-guide (C++17) linkage-specification namespace-definition + empty-declaration + attribute-declaration + module-import-declaration (modules) - C++17: - deduction-guide - - modules: - (all these are only allowed at the outermost level, check - that semantically, for better diagnostics) - module-declaration - module-export-declaration - module-import-declaration - export-declaration + special-declaration: + explicit-instantiation + explicit-specialization + export-declaration (modules) GNU extension: @@ -15389,6 +15398,13 @@ cp_parser_declaration (cp_parser* parser, tree prefix_attrs) return; } + /* P2615: Determine if we're parsing a name-declaration specifically, + or if special-declarations are OK too. */ + bool require_name_decl_p + = (parser->in_unbraced_export_declaration_p + || (parser->in_unbraced_linkage_specification_p + && cxx_dialect >= cxx20)); + /* Try to figure out what kind of declaration is present. */ cp_token *token1 = cp_lexer_peek_token (parser->lexer); cp_token *token2 = (token1->type == CPP_EOF @@ -15496,13 +15512,30 @@ cp_parser_declaration (cp_parser* parser, tree prefix_attrs) /* `template <>' indicates a template specialization. */ if (token2->type == CPP_LESS && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) - cp_parser_explicit_specialization (parser); + { + if (require_name_decl_p) + { + auto_diagnostic_group d; + cp_token *token3 = cp_lexer_peek_nth_token (parser->lexer, 3); + location_t loc = make_location (token1, token1, token3); + error_at (loc, "explicit specializations are not permitted here"); + if (parser->in_unbraced_export_declaration_p) + inform (loc, "a specialization is always exported alongside " + "its primary template"); + } + cp_parser_explicit_specialization (parser); + } /* `template <' indicates a template declaration. */ else if (token2->type == CPP_LESS) cp_parser_template_declaration (parser, /*member_p=*/false); /* Anything else must be an explicit instantiation. */ else - cp_parser_explicit_instantiation (parser); + { + if (require_name_decl_p) + error_at (token1->location, + "explicit instantiations are not permitted here"); + cp_parser_explicit_instantiation (parser); + } } /* If the next token is `export', it's new-style modules or old-style template. */ @@ -15511,7 +15544,14 @@ cp_parser_declaration (cp_parser* parser, tree prefix_attrs) if (!modules_p ()) cp_parser_template_declaration (parser, /*member_p=*/false); else - cp_parser_module_export (parser); + { + /* We check for nested exports in cp_parser_module_export. */ + if (require_name_decl_p + && !parser->in_unbraced_export_declaration_p) + error_at (token1->location, + "export-declarations are not permitted here"); + cp_parser_module_export (parser); + } } else if (cp_token_is_module_directive (token1)) { @@ -16809,7 +16849,7 @@ cp_parser_function_specifier_opt (cp_parser* parser, linkage-specification: extern string-literal { declaration-seq [opt] } - extern string-literal declaration */ + extern string-literal name-declaration */ static void cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr) @@ -26767,6 +26807,7 @@ cp_parser_class_specifier (cp_parser* parser) unsigned char in_statement; bool in_switch_statement_p; bool saved_in_unbraced_linkage_specification_p; + bool saved_in_unbraced_export_declaration_p; tree old_scope = NULL_TREE; tree scope = NULL_TREE; cp_token *closing_brace; @@ -26818,6 +26859,10 @@ cp_parser_class_specifier (cp_parser* parser) saved_in_unbraced_linkage_specification_p = parser->in_unbraced_linkage_specification_p; parser->in_unbraced_linkage_specification_p = false; + /* Or in an export-declaration. */ + saved_in_unbraced_export_declaration_p + = parser->in_unbraced_export_declaration_p; + parser->in_unbraced_export_declaration_p = false; /* 'this' from an enclosing non-static member function is unavailable. */ tree saved_ccp = current_class_ptr; tree saved_ccr = current_class_ref; @@ -27200,6 +27245,8 @@ cp_parser_class_specifier (cp_parser* parser) = saved_num_template_parameter_lists; parser->in_unbraced_linkage_specification_p = saved_in_unbraced_linkage_specification_p; + parser->in_unbraced_export_declaration_p + = saved_in_unbraced_export_declaration_p; current_class_ptr = saved_ccp; current_class_ref = saved_ccr; @@ -27487,6 +27534,20 @@ cp_parser_class_head (cp_parser* parser, permerror (nested_name_specifier_token_start->location, "extra qualification not allowed"); } + /* The name-declaration of an export-declaration shall not declare + a partial specialization. */ + if (template_id_p + && parser->in_unbraced_export_declaration_p + && !processing_specialization + && !processing_explicit_instantiation) + { + auto_diagnostic_group d; + location_t loc = type_start_token->location; + error_at (loc, "declaration of partial specialization in unbraced " + "export-declaration"); + inform (loc, "a specialization is always exported alongside its " + "primary template"); + } /* An explicit-specialization must be preceded by "template <>". If it is not, try to recover gracefully. */ if (at_namespace_scope_p () diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h index 373e78f3ea4..09b356e5e73 100644 --- a/gcc/cp/parser.h +++ b/gcc/cp/parser.h @@ -312,9 +312,13 @@ struct GTY(()) cp_parser { /* TRUE if the declaration we are parsing is part of a linkage-specification of the form `extern string-literal - declaration'. */ + name-declaration'. */ bool in_unbraced_linkage_specification_p; + /* TRUE if the declaration we are parsing is part of an + export-declaration of the form 'export name-declaration'. */ + bool in_unbraced_export_declaration_p; + /* TRUE if we are presently parsing a declarator, after the direct-declarator. */ bool in_declarator_p; diff --git a/gcc/testsuite/g++.dg/cpp2a/linkage-spec1.C b/gcc/testsuite/g++.dg/cpp2a/linkage-spec1.C new file mode 100644 index 00000000000..9e8d5979059 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/linkage-spec1.C @@ -0,0 +1,22 @@ +// PR c++/107688 +// P2615R1: Meaningful exports: Newly invalid declarations +// { dg-do compile { target c++11 } } + +extern "C++" template struct A {}; + +extern "C++" template struct A {}; + +extern "C++" template <> struct A {}; +// { dg-error "explicit specializations are not permitted here" "" { target c++20 } .-1 } + +extern "C++" template struct A; +// { dg-error "explicit instantiations are not permitted here" "" { target c++20 } .-1 } + + +// These should all still be valid, though +extern "C++" { + template struct B {}; + template struct B {}; + template <> struct B {}; + template struct B; +} diff --git a/gcc/testsuite/g++.dg/modules/contracts-1_a.C b/gcc/testsuite/g++.dg/modules/contracts-1_a.C index f991ef8644e..723726451f4 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-1_a.C +++ b/gcc/testsuite/g++.dg/modules/contracts-1_a.C @@ -8,7 +8,7 @@ export module foo; // { dg-module-cmi foo } export int violation_count{0}; -extern "C++" export void handle_contract_violation(const std::experimental::contract_violation &violation) +export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation) { violation_count++; printf("violation_count: %d\n", violation_count); diff --git a/gcc/testsuite/g++.dg/modules/contracts-2_a.C b/gcc/testsuite/g++.dg/modules/contracts-2_a.C index 828d680d2a0..3d78c412d46 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-2_a.C +++ b/gcc/testsuite/g++.dg/modules/contracts-2_a.C @@ -12,7 +12,7 @@ export module foo; export int violation_count{0}; export int violation_line_sum{0}; -extern "C++" export void handle_contract_violation(const std::experimental::contract_violation &violation) +export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation) { violation_count++; violation_line_sum += violation.line_number () * violation_count; diff --git a/gcc/testsuite/g++.dg/modules/contracts-3_a.C b/gcc/testsuite/g++.dg/modules/contracts-3_a.C index a4f03d35842..b931ec0ddf7 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-3_a.C +++ b/gcc/testsuite/g++.dg/modules/contracts-3_a.C @@ -8,7 +8,7 @@ export module foo; // { dg-module-cmi foo } export int violation_count{0}; -extern "C++" export void handle_contract_violation(const std::experimental::contract_violation &violation) +export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation) { violation_count++; printf("violation_count: %d\n", violation_count); diff --git a/gcc/testsuite/g++.dg/modules/contracts-4_a.C b/gcc/testsuite/g++.dg/modules/contracts-4_a.C index f269e6c2078..a135f50fb7d 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-4_a.C +++ b/gcc/testsuite/g++.dg/modules/contracts-4_a.C @@ -9,7 +9,7 @@ export module foo; // { dg-module-cmi foo } export int violation_count{0}; -extern "C++" export void handle_contract_violation(const std::experimental::contract_violation &violation) +export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation) { violation_count++; printf("violation_count: %d\n", violation_count); diff --git a/gcc/testsuite/g++.dg/modules/export-3.C b/gcc/testsuite/g++.dg/modules/export-3.C new file mode 100644 index 00000000000..76765fd2b2f --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/export-3.C @@ -0,0 +1,30 @@ +// P2615R1 invalid declarations +// PR c++/107688 +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi !bad } + +export module bad; + +extern "C++" export void foo(); // { dg-error "export-declarations are not permitted here" "" { target c++20 } } + +export template struct S {}; + +export template struct S {}; // { dg-error "partial specialization in unbraced export-declaration" } + +export template <> struct S {}; // { dg-error "explicit specializations are not permitted here" } + +export template struct S; // { dg-error "explicit instantiations are not permitted here" } + +template <> export struct S; // { dg-error "expected unqualified-id" } + +export export int x; // { dg-error ".export. may only occur once" } + +export { export int y; } // { dg-error ".export. may only occur once" } + +namespace { + export namespace ns {} // { dg-error "internal linkage" } +} + +export namespace {} // { dg-error "internal linkage" } + +// { dg-prune-output "not writing module" } diff --git a/gcc/testsuite/g++.dg/modules/export-4_a.C b/gcc/testsuite/g++.dg/modules/export-4_a.C new file mode 100644 index 00000000000..5f99577dc99 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/export-4_a.C @@ -0,0 +1,23 @@ +// P2615R1 valid declarations +// PR c++/107688 +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi M } + +export module M; + +export {} +export { static_assert(true); } + +export namespace Empty {} +export using namespace Empty; + +export { + template struct S {}; + template struct S { using a = int; }; + template <> struct S { using b = int; }; + template struct S; +} + +extern "C++" { + export void foo(); +} diff --git a/gcc/testsuite/g++.dg/modules/export-4_b.C b/gcc/testsuite/g++.dg/modules/export-4_b.C new file mode 100644 index 00000000000..4d721119916 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/export-4_b.C @@ -0,0 +1,13 @@ +// PR c++/107688 +// { dg-additional-options "-fmodules-ts" } + +import M; + +using namespace Empty; + +int main() { + S x; + S::b y; + S::a z; + foo(); +} diff --git a/gcc/testsuite/g++.dg/modules/lang-1_c.C b/gcc/testsuite/g++.dg/modules/lang-1_c.C index e3b939df373..b3908ed6188 100644 --- a/gcc/testsuite/g++.dg/modules/lang-1_c.C +++ b/gcc/testsuite/g++.dg/modules/lang-1_c.C @@ -6,7 +6,7 @@ extern "C++" } extern "C" -import "lang-1_a.H"; // { dg-error "cannot appear directly" } + import "lang-1_a.H"; // OK since p2615r1 extern "C" int cfunc (int); // { dg-error "conflicting declaration" } extern "C" int cxxfunc (int); diff --git a/gcc/testsuite/g++.dg/template/crash71.C b/gcc/testsuite/g++.dg/template/crash71.C index 3ac862ed81b..949f46eaf0d 100644 --- a/gcc/testsuite/g++.dg/template/crash71.C +++ b/gcc/testsuite/g++.dg/template/crash71.C @@ -1,3 +1,5 @@ // PR c++/30659 -extern "C" template A foo(); // { dg-error "forbids|static data|expected|template" } +extern "C" template A foo(); +// { dg-error "forbids|static data|expected|template" "" { target c++17_down } .-1 } +// { dg-error "permitted|forbids|static data|expected|template" "" { target c++20 } .-2 }