https://github.com/ChuanqiXu9 created 
https://github.com/llvm/llvm-project/pull/215184

Close https://github.com/llvm/llvm-project/issues/204632

Note that the error message is already diagnosed. So we don't need to do 
additional thing here.

>From e880f974717468ff72074c24e0076f5a583e3842 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <[email protected]>
Date: Mon, 10 Aug 2026 14:47:56 +0800
Subject: [PATCH] [C++20] [Modules] Don't set clang module as named module for
 module duplication check

Close https://github.com/llvm/llvm-project/issues/204632

Note that the error message is already diagnosed. So we don't need to do
additional thing here.
---
 clang/lib/Sema/SemaModule.cpp    |  5 +++++
 clang/test/Modules/GH204632.cppm | 14 ++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 clang/test/Modules/GH204632.cppm

diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp
index caa61a99a6914..667f36ab737ed 100644
--- a/clang/lib/Sema/SemaModule.cpp
+++ b/clang/lib/Sema/SemaModule.cpp
@@ -389,6 +389,11 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, 
SourceLocation ModuleLoc,
       else if (const ModuleFileName *FileName = M->getASTFileName())
         Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
             << *FileName;
+      // A Clang module or a header unit cannot be used as the current named
+      // module while recovering from it. See clang/test/Modules/GH204632.cppm
+      // for an example.
+      if (!M->isNamedModule())
+        return nullptr;
       Mod = M;
       break;
     }
diff --git a/clang/test/Modules/GH204632.cppm b/clang/test/Modules/GH204632.cppm
new file mode 100644
index 0000000000000..13667fa35e479
--- /dev/null
+++ b/clang/test/Modules/GH204632.cppm
@@ -0,0 +1,14 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: not %clang_cc1 -std=c++20 -fsyntax-only -fmodules \
+// RUN:   -fmodule-map-file=%t/module.modulemap %t/main.cpp 2>&1 | FileCheck %s
+
+// CHECK: main.cpp:1:15: error: redefinition of module 'M'
+// CHECK: module.modulemap:1:8: note: previously defined here
+// CHECK: 1 error generated.
+
+//--- module.modulemap
+module M {}
+
+//--- main.cpp
+export module M;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to