[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 2 08:20:46 PDT 2023


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.

Per P1975R0 an expression like `static_cast<U[]>(...)` defines the type
of the expression as U[1].

Fixes https://github.com/llvm/llvm-project/issues/62863


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152003

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaType.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/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -8818,6 +8818,17 @@
       }
     }
   }
+  // 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]
+  if (auto *Cast = dyn_cast<CXXStaticCastExpr>(E)) {
+    if (auto *SubInit = dyn_cast<CXXParenListInitExpr>(Cast->getSubExpr())) {
+      const Type *InnerType = SubInit->getType().getTypePtr();
+      if (const auto *AT = dyn_cast<ConstantArrayType>(InnerType);
+          AT && AT->getSize() == 1) {
+        Cast->setType(SubInit->getType());
+      }
+    }
+  }
 }
 
 QualType Sema::getCompletedType(Expr *E) {
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -463,6 +463,8 @@
 - Fix crash when passing a braced initializer list to a parentehsized aggregate
   initialization expression.
   (`#63008 <https://github.com/llvm/llvm-project/issues/63008>`_).
+- 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.527873.patch
Type: text/x-patch
Size: 2564 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230602/7b5f1a23/attachment.bin>


More information about the cfe-commits mailing list