[PATCH] D152003: [clang] Fix `static_cast` to array of unknown bound

Mariya Podchishchaeva via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 30 03:38:08 PDT 2023


Fznamznon updated this revision to Diff 536172.
Fznamznon added a comment.

Move changes to another place, check destination type


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152003/new/

https://reviews.llvm.org/D152003

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaCast.cpp
  clang/test/SemaCXX/paren-list-agg-init.cpp


Index: clang/test/SemaCXX/paren-list-agg-init.cpp
===================================================================
--- clang/test/SemaCXX/paren-list-agg-init.cpp
+++ clang/test/SemaCXX/paren-list-agg-init.cpp
@@ -272,3 +272,14 @@
 // expected-warning at -1 {{braces around scalar init}}
 // beforecxx20-warning at -2 {{aggregate initialization of type 'A' from a parenthesized list of values is a C++20 extension}}
 }
+
+namespace gh62863 {
+int (&&arr)[] = static_cast<int[]>(42);
+// beforecxx20-warning at -1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}}
+int (&&arr1)[1] = static_cast<int[]>(42);
+// beforecxx20-warning at -1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}}
+int (&&arrr2)[2] = static_cast<int[]>(42); // expected-error {{reference to type 'int[2]' could not bind to an rvalue of type 'int[1]'}}
+// beforecxx20-warning at -1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}}
+int (&&arr3)[3] = static_cast<int[3]>(42);
+// beforecxx20-warning at -1 {{aggregate initialization of type 'int[3]' from a parenthesized list of values is a C++20 extension}}
+}
Index: clang/lib/Sema/SemaCast.cpp
===================================================================
--- clang/lib/Sema/SemaCast.cpp
+++ clang/lib/Sema/SemaCast.cpp
@@ -1262,6 +1262,15 @@
   } else {
     SrcExpr = ExprError();
   }
+
+  if (const auto *IAT = Self.Context.getAsIncompleteArrayType(DestType)) {
+    // C++20 [expr.static.cast]p.4: ... If T is “array of unknown bound of U”,
+    // this direct-initialization defines the type of the expression as U[1]
+    ResultType = Self.Context.getConstantArrayType(
+        IAT->getElementType(),
+        llvm::APInt(Self.Context.getTypeSize(Self.Context.getSizeType()), 1),
+        /*SizeExpr=*/nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0);
+  }
 }
 
 static bool IsAddressSpaceConversion(QualType SrcType, QualType DestType) {
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -561,6 +561,8 @@
   when an immediate invocation appears as a part of an expression that produces
   temporaries.
   (`#60709 <https://github.com/llvm/llvm-project/issues/60709>`_).
+- Fixed `static_cast` to array of unknown bound.
+  (`#62863 <https://github.com/llvm/llvm-project/issues/62863>`_).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152003.536172.patch
Type: text/x-patch
Size: 2562 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230630/498f30ea/attachment-0001.bin>


More information about the cfe-commits mailing list