Skip to content

[clang] [modules] Add err_main_in_named_module #146247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ashwinbanwari
Copy link

@ashwinbanwari ashwinbanwari commented Jun 28, 2025

Close #146229

As the issue said, main shouldn't be in any modules.

new error output:

/my/code/directory/main.cpp:3:1: error: 'main' cannot be attached to a named module
    3 | int main() {
      | ^
1 error generated.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 28, 2025

@llvm/pr-subscribers-clang

Author: Ashwin Banwari (ashwinbanwari)

Changes

Close #146229

As the issue said, main shouldn't be in any modules.

New Error Output:

/my/code/directory/main.cpp:3:1: error: 'main' cannot be attached to a named module
    3 | int main() {
      | ^
1 error generated.

Full diff: https://github.com/llvm/llvm-project/pull/146247.diff

2 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+8)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 5062505cf3c01..ce9017ded0087 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,6 +1062,8 @@ def err_constexpr_main : Error<
   "'main' is not allowed to be declared %select{constexpr|consteval}0">;
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
+def err_main_in_named_module
+    : Error<"'main' cannot be attached to a named module">;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e1cccf068b5aa..c4ddfda9f447f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12490,6 +12490,14 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
                                 : FixItHint());
       FD->setInvalidDecl(true);
     }
+
+    // In C++ [basic.start.main]p3, it is said a program attaching main to a
+    // named module is ill-formed.
+    if (FD->isInNamedModule()) {
+      Diag(FD->getTypeSpecStartLoc(), diag::err_main_in_named_module)
+          << FixItHint();
+      FD->setInvalidDecl(true);
+    }
   }
 
   // Treat protoless main() as nullary.

@ashwinbanwari ashwinbanwari changed the title [clang] [modules] add err_main_in_named_module [clang] [modules] Add err_main_in_named_module Jun 28, 2025
@Mr-Anyone
Copy link
Contributor

Mr-Anyone commented Jun 29, 2025

You should add a test case and a release note for this patch to be accepted.

@MikailBag
Copy link

MikailBag commented Jun 29, 2025

It seems that clang trunk warns on extern "C++" main, which is now allowed after P3618.

I think it will be helpful for the UX to land this change after that warning is fixed.

(Disclaimer: I'm not clang contributor, just clang user)

@ChuanqiXu9
Copy link
Member

I prefer a warning instead of hard error. There are already users who uses this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] [modules] Clang accepts ill-formed program with main attached to named module
5 participants