https://github.com/Kristianerik updated 
https://github.com/llvm/llvm-project/pull/230630

>From 2d09626421418808c0687ca284fb26ffaaed7800 Mon Sep 17 00:00:00 2001
From: Kristianerik <[email protected]>
Date: Thu, 8 Oct 2026 15:21:31 -0700
Subject: [PATCH 1/2] [clang] Fix incorrect 'namespace' in diagnostic for
 out-of-line declarations

---
 .../include/clang/Basic/DiagnosticSemaKinds.td  |  2 +-
 clang/lib/Sema/SemaDecl.cpp                     | 17 +++++++++++++++--
 .../CXX/temp/temp.spec/temp.expl.spec/p2-0x.cpp |  4 ++--
 .../CXX/temp/temp.spec/temp.expl.spec/p2.cpp    |  4 ++--
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 79f10b8240aa47..e36ee773a32ec6 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9063,7 +9063,7 @@ def err_typecheck_deleted_function : Error<
 def err_expected_class_or_namespace : Error<"%0 is not a class"
   "%select{ or namespace|, namespace, or enumeration}1">;
 def err_invalid_declarator_scope : Error<"cannot define or redeclare %0 here "
-  "because namespace %1 does not enclose namespace %2">;
+  "because namespace %1 does not enclose 
%select{namespace|struct|class|union|enum}2 %3">;
 def err_export_non_namespace_scope_name : Error<
   "cannot export %0 as it is not at namespace scope">;
 def err_redeclaration_non_exported : Error <
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1459b71326375a..6e2e75c2687465 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -6456,9 +6456,22 @@ bool Sema::diagnoseQualifiedDeclaration(CXXScopeSpec 
&SS, DeclContext *DC,
         // The cases that DC is not NamespaceDecl should be handled in
         // CheckRedeclarationExported.
         return false;
-    } else
+    } else {
+      unsigned DCKind = 0; // namespace by default
+      if (auto *TD = dyn_cast<TagDecl>(DC)) {
+        if (TD->isStruct())
+          DCKind = 1;
+        else if (TD->isClass())
+          DCKind = 2;
+        else if (TD->isUnion())
+          DCKind = 3;
+        else if (TD->isEnum())
+          DCKind = 4;
+      }
       Diag(Loc, diag::err_invalid_declarator_scope)
-      << Name << cast<NamedDecl>(Cur) << cast<NamedDecl>(DC) << SS.getRange();
+          << Name << cast<NamedDecl>(Cur) << DCKind << cast<NamedDecl>(DC)
+          << SS.getRange();
+    }
 
     return true;
   }
diff --git a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2-0x.cpp 
b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2-0x.cpp
index 80fae707de0c48..8d5ecb8ec970cd 100644
--- a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2-0x.cpp
+++ b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2-0x.cpp
@@ -169,7 +169,7 @@ template<> int N0::X0<int>::member;
 template<> float N0::X0<float>::member = 3.14f;
 
 namespace N1 {
-  template<> double N0::X0<double>::member = 3.14; // expected-error{{does not 
enclose namespace}}
+  template<> double N0::X0<double>::member = 3.14; // expected-error{{does not 
enclose struct}}
 }
 
 //    -- member class of a class template
@@ -267,7 +267,7 @@ void N0::X0<void*>::ft1(void *, float) { }
 
 namespace N1 {
   template<> template<>
-  void N0::X0<void*>::ft1(void *, long) { } // expected-error{{does not 
enclose namespace}}
+  void N0::X0<void*>::ft1(void *, long) { } // expected-error{{does not 
enclose struct}}
 }
 
 
diff --git a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp 
b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp
index 904f950df44332..cd447786066b49 100644
--- a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp
+++ b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp
@@ -134,7 +134,7 @@ template<> int N0::X0<int>::member;
 template<> float N0::X0<float>::member = 3.14f;
 
 namespace N1 {
-  template<> double N0::X0<double>::member = 3.14; // expected-error{{does not 
enclose namespace}}
+  template<> double N0::X0<double>::member = 3.14; // expected-error{{does not 
enclose struct}}
 }
 
 //    -- member class of a class template
@@ -232,7 +232,7 @@ void N0::X0<void*>::ft1(void *, float) { }
 
 namespace N1 {
   template<> template<>
-  void N0::X0<void*>::ft1(void *, long) { } // expected-error{{does not 
enclose namespace}}
+  void N0::X0<void*>::ft1(void *, long) { } // expected-error{{does not 
enclose struct}}
 }
 
 

>From b13b9a994ae6cddf40f91daa36aa2be155e1ce74 Mon Sep 17 00:00:00 2001
From: Kristianerik <[email protected]>
Date: Fri, 9 Oct 2026 16:57:58 -0700
Subject: [PATCH 2/2] fixup: Update thread-safety test to match corrected
 diagnostic wording

---
 clang/test/SemaCXX/warn-thread-safety-parsing.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/warn-thread-safety-parsing.cpp 
b/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
index 8ceacb5a834181..0f1ab8a69c6861 100644
--- a/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
@@ -1681,7 +1681,7 @@ namespace InvalidDeclTest {
 class Foo { };
 namespace {
 void Foo::bar(Mutex* mu) LOCKS_EXCLUDED(mu) { } // \
-   // expected-error   {{cannot define or redeclare 'bar' here because 
namespace '' does not enclose namespace 'Foo'}} \
+   // expected-error   {{cannot define or redeclare 'bar' here because 
namespace '' does not enclose class 'Foo'}} \
    // expected-warning {{attribute locks_excluded ignored, because it is not 
attached to a declaration}}
 }
 

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

Reply via email to