[clang] [C++20] [Modules] Correct Module Ownership and use canonical decl (PR #246565)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 23:01:00 PST 2026
https://github.com/ChuanqiXu9 created https://github.com/llvm/llvm-project/pull/246565
Close https://github.com/llvm/llvm-project/issues/245967
The root cause of the issue is that we didn't give deduction guide the correct module ownership and we didn't use canonical decl correctly.
>From 05a5f0f41124b87b74009893ce64d05c6f9324fe Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <chuanqi.xcq at alibaba-inc.com>
Date: Fri, 27 Nov 2026 14:52:45 +0800
Subject: [PATCH] [C++20] [Modules] Correct Module Ownership and use canonical
decl
Close https://github.com/llvm/llvm-project/issues/245967
The root cause of the issue is that we didn't give deduction guide
the correct module ownership and we didn't use canonical decl correctly.
---
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 18 +-
clang/test/Modules/pr245967.cppm | 174 ++++++++++++++++++
2 files changed, 189 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Modules/pr245967.cppm
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 9fe91632d41a1f..c50d7b284c53ba 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -280,11 +280,20 @@ buildDeductionGuide(Sema &SemaRef, TemplateDecl *OriginalTemplate,
}
}
+ auto CorrectModuleOnwerModuleShip = [&SemaRef, OriginalTemplate](Decl *D) {
+ if (!SemaRef.getLangOpts().trackLocalOwningModule())
+ return;
+ D->setModuleOwnershipKind(OriginalTemplate->getModuleOwnershipKind());
+ if (D->hasOwningModule())
+ D->setLocalOwningModule(OriginalTemplate->getOwningModule());
+ };
+
auto *Guide = CXXDeductionGuideDecl::Create(
SemaRef.Context, DC, LocStart, ES, Name, GuideType, TInfo, LocEnd, Ctor,
DeductionCandidate::Normal, FunctionTrailingRC);
Guide->setImplicit(IsImplicit);
Guide->setParams(Params);
+ CorrectModuleOnwerModuleShip(Guide);
for (auto *Param : Params)
Param->setDeclContext(Guide);
@@ -302,6 +311,7 @@ buildDeductionGuide(Sema &SemaRef, TemplateDecl *OriginalTemplate,
SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide);
GuideTemplate->setImplicit(IsImplicit);
Guide->setDescribedFunctionTemplate(GuideTemplate);
+ CorrectModuleOnwerModuleShip(GuideTemplate);
if (isa<CXXRecordDecl>(DC))
GuideTemplate->setAccess(AS_public);
@@ -950,7 +960,7 @@ llvm::DenseSet<const NamedDecl *> getSourceDeductionGuides(DeclarationName Name,
assert(GD->getSourceDeductionGuide() &&
"deduction guide for alias template must have a source deduction "
"guide");
- Result.insert(GD->getSourceDeductionGuide());
+ Result.insert(GD->getSourceDeductionGuide()->getCanonicalDecl());
}
}
return Result;
@@ -1496,7 +1506,7 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
for (auto *G : Guides) {
if (auto *DG = dyn_cast<CXXDeductionGuideDecl>(G)) {
- if (SourceDeductionGuides.contains(DG))
+ if (SourceDeductionGuides.contains(DG->getCanonicalDecl()))
continue;
// The deduction guide is a non-template function decl, we just clone it.
auto *FunctionType =
@@ -1543,7 +1553,9 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
continue;
}
FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(G);
- if (!F || SourceDeductionGuides.contains(F->getTemplatedDecl()))
+ if (!F ||
+ SourceDeductionGuides.contains(
+ F->getTemplatedDecl()->getCanonicalDecl()))
continue;
// The **aggregate** deduction guides are handled in a different code path
// (DeclareAggregateDeductionGuideFromInitList), which involves the tricky
diff --git a/clang/test/Modules/pr245967.cppm b/clang/test/Modules/pr245967.cppm
new file mode 100644
index 00000000000000..dfbc5a600767f9
--- /dev/null
+++ b/clang/test/Modules/pr245967.cppm
@@ -0,0 +1,174 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/err.cppm -I%t -emit-module-interface -o %t/err.pcm
+// RUN: %clang_cc1 -std=c++20 %t/use.cpp -I%t -fmodule-file=err=%t/err.pcm \
+// RUN: -fsyntax-only -verify
+// RUN: %clang_cc1 -std=c++20 %t/use_extra.cpp -I%t -fmodule-file=err=%t/err.pcm \
+// RUN: -fsyntax-only -verify
+//
+// Test again with reduced BMI.
+// RUN: %clang_cc1 -std=c++20 %t/err.cppm -I%t -emit-reduced-module-interface \
+// RUN: -o %t/err.pcm
+// RUN: %clang_cc1 -std=c++20 %t/use.cpp -I%t -fmodule-file=err=%t/err.pcm \
+// RUN: -fsyntax-only -verify
+// RUN: %clang_cc1 -std=c++20 %t/use_extra.cpp -I%t -fmodule-file=err=%t/err.pcm \
+// RUN: -fsyntax-only -verify
+//
+// Test header-defined class/alias templates whose deduction guides are
+// synthesized in module purview of multiple modules.
+// RUN: %clang_cc1 -std=c++20 %t/mod_a.cppm -I%t -emit-module-interface -o %t/mod_a.pcm
+// RUN: %clang_cc1 -std=c++20 %t/mod_b.cppm -I%t -emit-module-interface -o %t/mod_b.pcm
+// RUN: %clang_cc1 -std=c++20 %t/use_ab.cpp -I%t -fmodule-file=mod_a=%t/mod_a.pcm \
+// RUN: -fmodule-file=mod_b=%t/mod_b.pcm -fsyntax-only -verify
+// RUN: %clang_cc1 -std=c++20 %t/use_hdr_and_mod.cpp -I%t \
+// RUN: -fmodule-file=mod_a=%t/mod_a.pcm -fsyntax-only -verify
+
+//--- box.hpp
+#ifndef BOX_HPP
+#define BOX_HPP
+
+template <class T>
+struct Box {
+ template <class U>
+ Box(U) {}
+ Box(T, T) {}
+};
+
+template <class T>
+Box(T) -> Box<T>;
+
+template <class T>
+struct ImplicitBox {
+ ImplicitBox(T) {}
+};
+
+template <class T>
+struct NonTemplateGuideBox {
+ NonTemplateGuideBox(T) {}
+};
+
+NonTemplateGuideBox(int) -> NonTemplateGuideBox<int>;
+
+template <class T>
+struct ConstrainedBox {
+ template <class U>
+ ConstrainedBox(U) requires (sizeof(U) > 0) {}
+};
+
+template <class T>
+requires (sizeof(T) > 0)
+ConstrainedBox(T) -> ConstrainedBox<T>;
+
+#endif
+
+//--- err.cppm
+module;
+
+#include "box.hpp"
+
+export module err;
+
+export template <class T>
+using Err = Box<T>;
+
+export template <class T>
+using ImplicitErr = ImplicitBox<T>;
+
+export template <class T>
+using NonTemplateErr = NonTemplateGuideBox<T>;
+
+export template <class T>
+using ConstrainedErr = ConstrainedBox<T>;
+
+auto seed1 = Err(0);
+auto seed2 = ImplicitErr(0);
+auto seed3 = NonTemplateErr(0);
+auto seed4 = ConstrainedErr(0);
+
+//--- use.cpp
+// expected-no-diagnostics
+#include "box.hpp"
+
+auto pre_implicit = ImplicitBox(1);
+
+import err;
+
+auto result1 = Err(42);
+auto result2 = ImplicitErr(42);
+auto result3a = NonTemplateErr(42);
+auto result3b = NonTemplateErr(3.14);
+auto result4 = ConstrainedErr(42);
+
+//--- use_extra.cpp
+// expected-no-diagnostics
+#include "box.hpp"
+import err;
+
+template <class T>
+Box(T, T) -> Box<T>;
+
+auto result1 = Err(42);
+auto result2 = Err(1, 2);
+
+//--- shared.hpp
+#ifndef SHARED_HPP
+#define SHARED_HPP
+
+template <class T>
+struct SharedBox {
+ SharedBox(T) {}
+};
+
+template <class T>
+struct SharedGuidedBox {
+ template <class U>
+ SharedGuidedBox(U) {}
+};
+
+template <class T>
+SharedGuidedBox(T) -> SharedGuidedBox<T>;
+
+template <class T>
+using SharedErr = SharedGuidedBox<T>;
+
+#endif
+
+//--- mod_a.cppm
+module;
+#include "shared.hpp"
+export module mod_a;
+export using ::SharedBox;
+export using ::SharedErr;
+
+auto a1 = SharedBox(0);
+auto a2 = SharedErr(0);
+
+//--- mod_b.cppm
+module;
+#include "shared.hpp"
+export module mod_b;
+
+auto b1 = SharedBox(0);
+auto b2 = SharedErr(0);
+
+//--- use_ab.cpp
+// expected-no-diagnostics
+import mod_a;
+import mod_b;
+
+auto r1 = SharedBox(42);
+auto r2 = SharedErr(42);
+
+//--- use_hdr_and_mod.cpp
+// expected-no-diagnostics
+#include "shared.hpp"
+
+auto before1 = SharedBox(1);
+auto before2 = SharedErr(1);
+
+import mod_a;
+
+auto after1 = SharedBox(42);
+auto after2 = SharedErr(42);
More information about the cfe-commits
mailing list