diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -604,8 +604,14 @@ // - 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(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(D->getCanonicalDecl())) && + !isa(D)) return LinkageInfo(ModuleLinkage, DefaultVisibility, false); return LinkageInfo::external(); 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 --- /dev/null +++ b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h @@ -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/Inputs/p2.cppm b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm new file mode 100644 --- /dev/null +++ b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm @@ -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/p2.cppm b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm new file mode 100644 --- /dev/null +++ b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm @@ -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(); +} +}