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.
2025-06-24 Jakub Jelinek <jakub@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.
"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 %<extern \"C++\"%>");
if (module_attach_p ())
error_at (location, "cannot attach %<::main%> to a named module");
inlinep = 0;
--- /dev/null
+// { dg-additional-options "-fmodules" }
+
+export module M;
+extern "C++" int main() {}
--- /dev/null
+// { dg-additional-options "-fmodules" }
+
+export module M;
+extern "C++" {
+ int main() {}
+}
+
// { dg-do compile }
-// The main function shall not be declared with a linkage-specification.
+// The main function shall not be declared with a linkage-specification
+// other than "C++".
extern "C" {
int main(); // { dg-error "linkage" }
extern "C" int main(); // { dg-error "linkage" }
}
-extern "C++" int main(); // { dg-error "linkage" }
+extern "C++" int main();
extern "C" struct S { int main(); }; // OK
--- /dev/null
+// { dg-do compile }
+// The main function shall not be declared with a linkage-specification
+// other than "C++".
+
+extern "C++" {
+ int main();
+}
--- /dev/null
+// { dg-do compile }
+// The main function shall not be declared with a linkage-specification
+// other than "C++".
+
+extern "C" int main(); // { dg-error "linkage" }