[PATCH] D156063: [Clang] Reject programs declaring namespace std to be inline

PoYao Chang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 23 10:57:43 PDT 2023


rZhBoYao created this revision.
rZhBoYao added reviewers: philnik, clang-language-wg.
Herald added a project: All.
rZhBoYao requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156063

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/CXX/dcl.dcl/basic.namespace/namespace.def/p7.cpp


Index: clang/test/CXX/dcl.dcl/basic.namespace/namespace.def/p7.cpp
===================================================================
--- clang/test/CXX/dcl.dcl/basic.namespace/namespace.def/p7.cpp
+++ clang/test/CXX/dcl.dcl/basic.namespace/namespace.def/p7.cpp
@@ -16,3 +16,6 @@
   inline namespace {} // expected-note {{previous definition}}
   namespace {} // expected-warning {{inline namespace reopened as a non-inline namespace}}
 }
+
+inline namespace std {}
+// expected-error at -1{{namespace 'std' cannot be declared to be inline}}
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -11361,6 +11361,14 @@
 
   NamespaceDecl *PrevNS = nullptr;
   if (II) {
+    // C++ [namespace.std]p7:
+    //   A translation unit shall not declare namespace std to be an inline
+    //   namespace (9.8.2).
+    //
+    // This has to be diagnosed before entering DiagnoseNamespaceInlineMismatch.
+    if (IsInline && II->isStr("std"))
+      Diag(InlineLoc, diag::err_inline_namespace_std)
+          << SourceRange(InlineLoc, InlineLoc.getLocWithOffset(6));
     // C++ [namespace.def]p2:
     //   The identifier in an original-namespace-definition shall not
     //   have been previously defined in the declarative region in
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1579,6 +1579,8 @@
   InGroup<InlineNamespaceReopenedNoninline>;
 def err_inline_namespace_mismatch : Error<
   "non-inline namespace cannot be reopened as inline">;
+def err_inline_namespace_std : Error<
+  "namespace 'std' cannot be declared to be inline">;
 
 def err_unexpected_friend : Error<
   "friends can only be classes or functions">;
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -84,6 +84,7 @@
   directly rather than instantiating the definition from the standard library.
 - Implemented `CWG2518 <https://wg21.link/CWG2518>`_ which allows ``static_assert(false)``
   to not be ill-formed when its condition is evaluated in the context of a template definition.
+- Declaring namespace std to be an inline namespace is now prohibited, `[namespace.std]p7`.
 
 C++20 Feature Support
 ^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156063.543309.patch
Type: text/x-patch
Size: 2526 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230723/cee91107/attachment.bin>


More information about the cfe-commits mailing list