[clang] [Clang] prevent assertion failure by avoiding casts on type declarations that require complete types (PR #101426)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 31 15:30:48 PDT 2024
https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/101426
Fixes #101304
>From c972795aed2357bbceed3a013e12384a70f49489 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 1 Aug 2024 01:27:09 +0300
Subject: [PATCH] [Clang] prevent assertion failure by avoiding casts on type
declarations that require complete types
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaType.cpp | 4 ++--
clang/test/Sema/constexpr.c | 4 ++++
3 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3c2e0282d1c72..c5ec3dfc11bbe 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -156,6 +156,7 @@ Bug Fixes in This Version
- Fixed the definition of ``ATOMIC_FLAG_INIT`` in ``<stdatomic.h>`` so it can
be used in C++.
+- Fixed a failed assertion when casting type declarations that require complete types. (#GH101304).
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 6fa39cdccef2b..75e1b116e2043 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -9267,14 +9267,14 @@ bool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
if (!RT)
return true;
- const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
-
// A partially-defined class type can't be a literal type, because a literal
// class type must have a trivial destructor (which can't be checked until
// the class definition is complete).
if (RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T))
return true;
+ const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
+
// [expr.prim.lambda]p3:
// This class type is [not] a literal type.
if (RD->isLambda() && !getLangOpts().CPlusPlus17) {
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index 8286cd2107d2f..0d2ddabd4c072 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -357,3 +357,7 @@ void infsNaNs() {
constexpr double db5 = LD_SNAN; // expected-error {{constexpr initializer evaluates to nan which is not exactly representable in type 'const double'}}
constexpr double db6 = INF;
}
+
+constexpr struct S9 s9 = { }; // expected-error {{constexpr variable cannot have non-literal type 'const struct S9'}} \
+ // expected-note {{incomplete type 'const struct S9' is not a literal type}} \
+ // expected-note {{forward declaration of 'struct S9'}}
More information about the cfe-commits
mailing list