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

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

>From 7a2075a908f776f2d8ce5f0d23e58ca09c803acf Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <[email protected]>
Date: Tue, 17 Mar 2026 13:53:51 +0800
Subject: [PATCH] [C++20] [Modules] Diagnose for duplicated definition in the
 same module

Close https://github.com/llvm/llvm-project/issues/186603
---
 clang/lib/Sema/SemaDecl.cpp      | 11 ++++++++---
 clang/test/Modules/pr186603.cppm | 22 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Modules/pr186603.cppm

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c5e8046c2b5e5..ac38f32882d9b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -21216,9 +21216,14 @@ bool Sema::shouldIgnoreInHostDeviceCheck(FunctionDecl 
*Callee) {
 bool Sema::isRedefinitionAllowedFor(NamedDecl *D, NamedDecl **Suggested,
                                     bool &Visible) {
   Visible = hasVisibleDefinition(D, Suggested);
+  // Accoding to [basic.def.odr]p16, it is not allowed to have duplicated 
definition
+  // for declaratins which is attached to named modules.
+  // We only did this if the current module is named module as we have better
+  // diagnostics for declarations in global module and named modules.
+  if (getCurrentModule() && getCurrentModule()->isNamedModule() &&
+      D->isInNamedModule())
+    return false;
   // The redefinition of D in the **current** TU is allowed if D is invisible 
or
-  // D is defined in the global module of other module units. We didn't check 
if
-  // it is in global module as, we'll check the redefinition in named module
-  // later with better diagnostic message.
+  // D is defined in the global module of other module units.
   return D->isInAnotherModuleUnit() || !Visible;
 }
diff --git a/clang/test/Modules/pr186603.cppm b/clang/test/Modules/pr186603.cppm
new file mode 100644
index 0000000000000..199efde757ffc
--- /dev/null
+++ b/clang/test/Modules/pr186603.cppm
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// Test that duplicate inline function definitions in different partitions
+// of the same named module are diagnosed as ODR violations.
+// See https://github.com/llvm/llvm-project/issues/186603
+//
+// Case 1: Module interface imports partition and redefines inline function
+// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface 
%t/partition1.cppm -o %t/A-P1.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %t/module1.cppm \
+// RUN:   -fmodule-file=A:P1=%t/A-P1.pcm -o %t/A.pcm -verify
+
+//--- partition1.cppm
+export module A:P1;
+export inline void x() {}
+
+//--- module1.cppm
+export module A;
+import :P1;
+export inline void x() {} // expected-error {{redefinition of 'x'}}
+// [email protected]:* {{previous definition is here}}
\ No newline at end of file

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

Reply via email to