Fznamznon created this revision.
Herald added a project: All.
Fznamznon requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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 https://github.com/llvm/llvm-project/issues/37257


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158615

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDecl.cpp
  clang/test/SemaCXX/gh37257.cpp


Index: clang/test/SemaCXX/gh37257.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/gh37257.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template<class T>
+T&& create();
+
+template<class T, class... Args>
+void test() {
+  T t(create<Args>()...); // expected-error{{variable has incomplete type 
'int[]'}}
+  (void) t;
+}
+
+struct A;
+
+int main() {
+  test<int[]>(); // expected-note {{in instantiation of function template 
specialization 'test<int[]>' requested here}}
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -13449,6 +13449,21 @@
     IsParenListInit = !InitSeq.steps().empty() &&
                       InitSeq.step_begin()->Kind ==
                           InitializationSequence::SK_ParenthesizedListInit;
+    QualType VDeclType = VDecl->getType();
+    if (!VDeclType->isDependentType() &&
+        Context.getAsIncompleteArrayType(VDeclType)) {
+      if (Init && !Init->getType().isNull() &&
+          !Init->getType()->isDependentType()) {
+        if (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.
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -163,6 +163,9 @@
   ``await_suspend`` could be misoptimized, including accesses to the awaiter
   object itself.
   (`#56301 <https://github.com/llvm/llvm-project/issues/56301>`_)
+- Clang now emits an error if it is not possible to deduce array size for a
+  variable with incomplete array type.
+  (`#37257 <https://github.com/llvm/llvm-project/issues/37257>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Index: clang/test/SemaCXX/gh37257.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/gh37257.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template<class T>
+T&& create();
+
+template<class T, class... Args>
+void test() {
+  T t(create<Args>()...); // expected-error{{variable has incomplete type 'int[]'}}
+  (void) t;
+}
+
+struct A;
+
+int main() {
+  test<int[]>(); // expected-note {{in instantiation of function template specialization 'test<int[]>' requested here}}
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -13449,6 +13449,21 @@
     IsParenListInit = !InitSeq.steps().empty() &&
                       InitSeq.step_begin()->Kind ==
                           InitializationSequence::SK_ParenthesizedListInit;
+    QualType VDeclType = VDecl->getType();
+    if (!VDeclType->isDependentType() &&
+        Context.getAsIncompleteArrayType(VDeclType)) {
+      if (Init && !Init->getType().isNull() &&
+          !Init->getType()->isDependentType()) {
+        if (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.
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -163,6 +163,9 @@
   ``await_suspend`` could be misoptimized, including accesses to the awaiter
   object itself.
   (`#56301 <https://github.com/llvm/llvm-project/issues/56301>`_)
+- Clang now emits an error if it is not possible to deduce array size for a
+  variable with incomplete array type.
+  (`#37257 <https://github.com/llvm/llvm-project/issues/37257>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to