Skip to content

Conversation

@a-tarasyuk
Copy link
Member

Fixes #147495


This patch addresses the issue of false-positive redeclaration errors that occur for using enum declarations in nested class scopes

struct S {
  enum class E { A };
  using enum E;

  struct S1 {
    using enum E; // no error
  };
};

@a-tarasyuk a-tarasyuk marked this pull request as ready for review July 9, 2025 12:19
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2025

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #147495


This patch addresses the issue of false-positive redeclaration errors that occur for using enum declarations in nested class scopes

struct S {
  enum class E { A };
  using enum E;

  struct S1 {
    using enum E; // no error
  };
};

Full diff: https://github.com/llvm/llvm-project/pull/147711.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3-1)
  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1-1)
  • (modified) clang/test/SemaCXX/cxx20-using-enum.cpp (+14)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 57a94242c9e61..086f0fce4975f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -770,8 +770,10 @@ Bug Fixes in This Version
 - In C23, something like ``[[/*possible attributes*/]];`` is an attribute
   declaration, not a statement. So it is not allowed by the syntax in places
   where a statement is required, specifically as the secondary block of a
-  selection or iteration statement. This differs from C++, since C++ allows 
+  selection or iteration statement. This differs from C++, since C++ allows
   declaration statements. Clang now emits a warning for these patterns. (#GH141659)
+- Fixed false positives for redeclaration errors of using enum in
+  nested scopes. (#GH147495)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index a49a8abb677c5..5cc92ebb0171f 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13308,7 +13308,7 @@ NamedDecl *Sema::BuildUsingEnumDeclaration(Scope *S, AccessSpecifier AS,
     LookupResult Previous(*this, UsingEnumName, LookupUsingDeclName,
                           RedeclarationKind::ForVisibleRedeclaration);
 
-    LookupName(Previous, S);
+    LookupQualifiedName(Previous, CurContext);
 
     for (NamedDecl *D : Previous)
       if (UsingEnumDecl *UED = dyn_cast<UsingEnumDecl>(D))
diff --git a/clang/test/SemaCXX/cxx20-using-enum.cpp b/clang/test/SemaCXX/cxx20-using-enum.cpp
index 14ef4b29925a1..775b65c5a8d8e 100644
--- a/clang/test/SemaCXX/cxx20-using-enum.cpp
+++ b/clang/test/SemaCXX/cxx20-using-enum.cpp
@@ -274,4 +274,18 @@ void f(int a) {
 }
 }
 
+namespace GH147495 {
+struct S {
+  enum class E { A };
+  using enum E;
+
+  struct S1 {
+    using enum E;
+  };
+
+  struct S2 {
+    using E::A;
+  };
+};
+}
 #endif

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@a-tarasyuk a-tarasyuk merged commit 9ef0a88 into llvm:main Jul 10, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] Incorrect "Redeclaration of using-enum declaration"

4 participants