[C++20] [Modules] Namespace Declaration shouldn't have module linkage
authorChuanqi Xu <yedeng.yd@linux.alibaba.com>
Wed, 8 Dec 2021 05:49:02 +0000 (13:49 +0800)
committerChuanqi Xu <yedeng.yd@linux.alibaba.com>
Wed, 8 Dec 2021 05:54:04 +0000 (13:54 +0800)
According to [basic.namespace.general]/p2, a namespace declaration
shouldn't have a module linkage.
> A namespace is never attached to a named module and never has a name
> with module linkage.

Without this patch, the compiler would crash for the test in assertion
enabled build due to inconsistent linkage for redeclaration for
namespaces.

Reviewed by: rsmith

Differential Revision: https://reviews.llvm.org/D115132

clang/lib/AST/Decl.cpp
clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm [new file with mode: 0644]
clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h [new file with mode: 0644]
clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm [new file with mode: 0644]

index 68dfef2..c033563 100644 (file)
@@ -604,8 +604,14 @@ static LinkageInfo getExternalLinkageFor(const NamedDecl *D) {
   //   - A name declared at namespace scope that does not have internal linkage
   //     by the previous rules and that is introduced by a non-exported
   //     declaration has module linkage.
-  if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit(
-                                  cast<NamedDecl>(D->getCanonicalDecl())))
+  //
+  // [basic.namespace.general]/p2
+  //   A namespace is never attached to a named module and never has a name with
+  //   module linkage.
+  if (isInModulePurview(D) &&
+      !isExportedFromModuleInterfaceUnit(
+          cast<NamedDecl>(D->getCanonicalDecl())) &&
+      !isa<NamespaceDecl>(D))
     return LinkageInfo(ModuleLinkage, DefaultVisibility, false);
 
   return LinkageInfo::external();
diff --git a/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm
new file mode 100644 (file)
index 0000000..4266a4b
--- /dev/null
@@ -0,0 +1,7 @@
+module;
+#include "p2.h"
+export module Y;
+export namespace foo {
+// We need to export something at least
+void print();
+}
diff --git a/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h
new file mode 100644 (file)
index 0000000..c1d86b9
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef P2_H
+#define P2_H
+namespace foo {
+namespace bar {
+}
+} // namespace foo
+#endif
diff --git a/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm
new file mode 100644 (file)
index 0000000..86c6745
--- /dev/null
@@ -0,0 +1,14 @@
+// Check that the compiler wouldn't crash due to inconsistent namesapce linkage
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: %clang -std=c++20 %S/Inputs/p2.cppm --precompile -o %t/Y.pcm
+// RUN: %clang -std=c++20 -fprebuilt-module-path=%t -I%S/Inputs %s -c -Xclang -verify
+// expected-no-diagnostics
+export module X;
+import Y;
+
+export namespace foo {
+namespace bar{
+    void baz();
+}
+}