Skip to content

Commit 9aa69a7

Browse files
committed
[Clang] prevent assertion failure by avoiding required literal type checking in C context
1 parent 74f9579 commit 9aa69a7

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Bug Fixes in This Version
156156

157157
- Fixed the definition of ``ATOMIC_FLAG_INIT`` in ``<stdatomic.h>`` so it can
158158
be used in C++.
159+
- Fixed a failed assertion when checking required literal types in C context. (#GH101304).
159160

160161
Bug Fixes to Compiler Builtins
161162
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8756,7 +8756,8 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
87568756
return;
87578757
}
87588758

8759-
if (NewVD->isConstexpr() && !T->isDependentType() &&
8759+
if (getLangOpts().CPlusPlus && NewVD->isConstexpr() &&
8760+
!T->isDependentType() &&
87608761
RequireLiteralType(NewVD->getLocation(), T,
87618762
diag::err_constexpr_var_non_literal)) {
87628763
NewVD->setInvalidDecl();

clang/lib/Sema/SemaType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9267,14 +9267,14 @@ bool Sema::RequireLiteralType(SourceLocation Loc, QualType T,
92679267
if (!RT)
92689268
return true;
92699269

9270-
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
9271-
92729270
// A partially-defined class type can't be a literal type, because a literal
92739271
// class type must have a trivial destructor (which can't be checked until
92749272
// the class definition is complete).
92759273
if (RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T))
92769274
return true;
92779275

9276+
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
9277+
92789278
// [expr.prim.lambda]p3:
92799279
// This class type is [not] a literal type.
92809280
if (RD->isLambda() && !getLangOpts().CPlusPlus17) {

clang/test/Sema/constexpr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,6 @@ void infsNaNs() {
357357
constexpr double db5 = LD_SNAN; // expected-error {{constexpr initializer evaluates to nan which is not exactly representable in type 'const double'}}
358358
constexpr double db6 = INF;
359359
}
360+
361+
constexpr struct S9 s9 = { }; // expected-error {{variable has incomplete type 'const struct S9'}} \
362+
// expected-note {{forward declaration of 'struct S9'}}

0 commit comments

Comments
 (0)