Skip to content

Commit bcc8811

Browse files
committed
[clang] Emit an error if variable ends up with incomplete array type
This adds an error if variable with incomplete type has initializer with incomplete type, so it is not possible to deduce array size from initializer. Fixes #37257 Reviewed By: aaron.ballman, shafik Differential Revision: https://reviews.llvm.org/D158615
1 parent a02f9a7 commit bcc8811

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ Bug Fixes in This Version
189189
(`#64876 <https://github.com/llvm/llvm-project/issues/64876>`_)
190190
- Fixed an assertion if a function has cleanups and fatal erors.
191191
(`#48974 <https://github.com/llvm/llvm-project/issues/48974>`_)
192+
- Clang now emits an error if it is not possible to deduce array size for a
193+
variable with incomplete array type.
194+
(`#37257 <https://github.com/llvm/llvm-project/issues/37257>`_)
192195

193196
Bug Fixes to Compiler Builtins
194197
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDecl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13445,6 +13445,18 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
1344513445
IsParenListInit = !InitSeq.steps().empty() &&
1344613446
InitSeq.step_begin()->Kind ==
1344713447
InitializationSequence::SK_ParenthesizedListInit;
13448+
QualType VDeclType = VDecl->getType();
13449+
if (Init && !Init->getType().isNull() &&
13450+
!Init->getType()->isDependentType() && !VDeclType->isDependentType() &&
13451+
Context.getAsIncompleteArrayType(VDeclType) &&
13452+
Context.getAsIncompleteArrayType(Init->getType())) {
13453+
// Bail out if it is not possible to deduce array size from the
13454+
// initializer.
13455+
Diag(VDecl->getLocation(), diag::err_typecheck_decl_incomplete_type)
13456+
<< VDeclType;
13457+
VDecl->setInvalidDecl();
13458+
return;
13459+
}
1344813460
}
1344913461

1345013462
// Check for self-references within variable initializers.

clang/test/SemaCXX/gh37257.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
template<class T>
4+
T&& create();
5+
6+
template<class T, class... Args>
7+
void test() {
8+
T t(create<Args>()...); // expected-error{{variable has incomplete type 'int[]'}}
9+
(void) t;
10+
}
11+
12+
struct A;
13+
14+
int main() {
15+
test<int[]>(); // expected-note {{in instantiation of function template specialization 'test<int[]>' requested here}}
16+
}

0 commit comments

Comments
 (0)