diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -189,6 +189,9 @@ (`#64876 `_) - Fixed an assertion if a function has cleanups and fatal erors. (`#48974 `_) +- Clang now emits an error if it is not possible to deduce array size for a + variable with incomplete array type. + (`#37257 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13445,6 +13445,18 @@ IsParenListInit = !InitSeq.steps().empty() && InitSeq.step_begin()->Kind == InitializationSequence::SK_ParenthesizedListInit; + QualType VDeclType = VDecl->getType(); + if (Init && !Init->getType().isNull() && + !Init->getType()->isDependentType() && !VDeclType->isDependentType() && + Context.getAsIncompleteArrayType(VDeclType) && + Context.getAsIncompleteArrayType(Init->getType())) { + // Bail out if it is not possible to deduce array size from the + // initializer. + Diag(VDecl->getLocation(), diag::err_typecheck_decl_incomplete_type) + << VDeclType; + VDecl->setInvalidDecl(); + return; + } } // Check for self-references within variable initializers. diff --git a/clang/test/SemaCXX/gh37257.cpp b/clang/test/SemaCXX/gh37257.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/gh37257.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +template +T&& create(); + +template +void test() { + T t(create()...); // expected-error{{variable has incomplete type 'int[]'}} + (void) t; +} + +struct A; + +int main() { + test(); // expected-note {{in instantiation of function template specialization 'test' requested here}} +}