[clang] [clang] fix infinite recursion (PR #143244)
Zhikai Zeng via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 13 23:43:24 PDT 2025
https://github.com/Backl1ght updated https://github.com/llvm/llvm-project/pull/143244
>From a14625ba505416df42fb785f9205e56b408473b8 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 7 Jun 2025 16:15:04 +0800
Subject: [PATCH 1/4] add visit check
---
clang/lib/Sema/SemaDeclCXX.cpp | 14 ++++++--------
clang/test/SemaCXX/gh102293.cpp | 17 +++++++++++++++++
2 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 31e2834336742..0081fa6622eac 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7159,7 +7159,10 @@ void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) {
// "effectively constexpr" for better compatibility.
// See https://github.com/llvm/llvm-project/issues/102293 for more info.
if (isa<CXXDestructorDecl>(M)) {
- auto Check = [](QualType T, auto &&Check) -> bool {
+ llvm::DenseSet<QualType> Visited;
+ auto Check = [&Visited](QualType T, auto &&Check) -> bool {
+ if (!Visited.insert(T).second)
+ return false;
const CXXRecordDecl *RD =
T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
if (!RD || !RD->isCompleteDefinition())
@@ -7168,16 +7171,11 @@ void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) {
if (!RD->hasConstexprDestructor())
return false;
- QualType CanUnqualT = T.getCanonicalType().getUnqualifiedType();
for (const CXXBaseSpecifier &B : RD->bases())
- if (B.getType().getCanonicalType().getUnqualifiedType() !=
- CanUnqualT &&
- !Check(B.getType(), Check))
+ if (!Check(B.getType(), Check))
return false;
for (const FieldDecl *FD : RD->fields())
- if (FD->getType().getCanonicalType().getUnqualifiedType() !=
- CanUnqualT &&
- !Check(FD->getType(), Check))
+ if (!Check(FD->getType(), Check))
return false;
return true;
};
diff --git a/clang/test/SemaCXX/gh102293.cpp b/clang/test/SemaCXX/gh102293.cpp
index d4218cc13dcec..fe417e697841b 100644
--- a/clang/test/SemaCXX/gh102293.cpp
+++ b/clang/test/SemaCXX/gh102293.cpp
@@ -45,3 +45,20 @@ class quux : quux { // expected-error {{base class has incomplete type}} \
virtual int c();
};
}
+
+// Ensure we don't get infinite recursion from the check, however. See GH141789
+namespace GH141789 {
+template <typename Ty>
+struct S {
+ Ty t; // expected-error {{field has incomplete type 'GH141789::X'}}
+};
+
+struct T {
+ ~T();
+};
+
+struct X { // expected-note {{definition of 'GH141789::X' is not complete until the closing '}'}}
+ S<X> next; // expected-note {{in instantiation of template class 'GH141789::S<GH141789::X>' requested here}}
+ T m;
+};
+}
>From 62a698fcbb50ee0aacae7bd3e0b85a90dd1c77a1 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Tue, 10 Jun 2025 21:42:01 +0800
Subject: [PATCH 2/4] add release note
---
clang/docs/ReleaseNotes.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9ab8031b9ea8c..d0ffece7735d9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -703,6 +703,7 @@ Bug Fixes in This Version
the second clause of a C-style ``for`` loop. (#GH139818)
- Fixed a bug with constexpr evaluation for structs containing unions in case of C++ modules. (#GH143168)
- Fixed incorrect token location when emitting diagnostics for tokens expanded from macros. (#GH143216)
+- Fixed an infinite recursion about defaulted constexpr dtor. (#GH141789)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>From 08e9ffae2c1d1cb2c2a9b01c1105b0132aa96129 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Tue, 10 Jun 2025 22:06:14 +0800
Subject: [PATCH 3/4] use small dense set and store the canonical type
---
clang/lib/Sema/SemaDeclCXX.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 0081fa6622eac..2a909da3c19f9 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7159,9 +7159,9 @@ void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) {
// "effectively constexpr" for better compatibility.
// See https://github.com/llvm/llvm-project/issues/102293 for more info.
if (isa<CXXDestructorDecl>(M)) {
- llvm::DenseSet<QualType> Visited;
+ llvm::SmallDenseSet<QualType> Visited;
auto Check = [&Visited](QualType T, auto &&Check) -> bool {
- if (!Visited.insert(T).second)
+ if (!Visited.insert(T.getCanonicalType().getUnqualifiedType()).second)
return false;
const CXXRecordDecl *RD =
T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
>From 2beddabf9b56956cb2765465db338d974ddf089c Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Wed, 11 Jun 2025 18:50:54 +0800
Subject: [PATCH 4/4] address comments
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/lib/Sema/SemaDeclCXX.cpp | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d0ffece7735d9..33ee8a53b5f37 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -703,7 +703,7 @@ Bug Fixes in This Version
the second clause of a C-style ``for`` loop. (#GH139818)
- Fixed a bug with constexpr evaluation for structs containing unions in case of C++ modules. (#GH143168)
- Fixed incorrect token location when emitting diagnostics for tokens expanded from macros. (#GH143216)
-- Fixed an infinite recursion about defaulted constexpr dtor. (#GH141789)
+- Fixed an infinite recursion when checking constexpr destructors. (#GH141789)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 2a909da3c19f9..6f62c53aaf04d 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7161,7 +7161,7 @@ void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) {
if (isa<CXXDestructorDecl>(M)) {
llvm::SmallDenseSet<QualType> Visited;
auto Check = [&Visited](QualType T, auto &&Check) -> bool {
- if (!Visited.insert(T.getCanonicalType().getUnqualifiedType()).second)
+ if (!Visited.insert(T->getCanonicalTypeUnqualified()).second)
return false;
const CXXRecordDecl *RD =
T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
More information about the cfe-commits
mailing list