On 6/24/25 10:16 AM, Nathaniel Shead wrote:
On Tue, Jun 24, 2025 at 01:03:53PM +0200, Jakub Jelinek wrote:
Hi!
The following patch implements the P3618R0 paper by tweaking pedwarn
condition, adjusting pedwarn wording, adjusting one testcase and adding 4
new ones. The paper was voted in as DR, so it isn't guarded on C++ version.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2025-06-24 Jakub Jelinek <ja...@redhat.com>
PR c++/120773
* decl.cc (grokfndecl): Implement C++26 P3618R0 - Allow attaching
main to the global module. Only pedwarn for current_lang_name
other than lang_name_cplusplus and adjust pedwarn wording.
* g++.dg/parse/linkage5.C: Don't expect error on
extern "C++" int main ();.
* g++.dg/parse/linkage7.C: New test.
* g++.dg/parse/linkage8.C: New test.
* g++.dg/modules/main-2.C: New test.
* g++.dg/modules/main-3.C: New test.
--- gcc/cp/decl.cc.jj 2025-06-19 08:55:04.408676724 +0200
+++ gcc/cp/decl.cc 2025-06-23 17:47:13.942011687 +0200
@@ -11326,9 +11326,9 @@ grokfndecl (tree ctype,
"cannot declare %<::main%> to be %qs", "consteval");
if (!publicp)
error_at (location, "cannot declare %<::main%> to be static");
- if (current_lang_depth () != 0)
+ if (current_lang_name != lang_name_cplusplus)
pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
- " linkage specification");
+ " linkage specification other than %<\"C++\"%>");
if (module_attach_p ())
error_at (location, "cannot attach %<::main%> to a named module");
Maybe it would be nice to add a note/fixit that users can now work
around this error by marking main as 'extern "C++"'? But overall LGTM.
I suppose we could say "other than %<extern \"C++\"%>" to make that a
little clearer. OK with that tweak.
I wouldn't object to a fixup but it sounds more complicated than it's
worth to have different fixups for the extern "C" { int main(); } and
extern "C" int main(); cases.
Jason