diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -652,6 +652,8 @@ (`#63169 _`) - Fix crash when casting an object to an array type. (`#63758 _`) +- Fixed `static_cast` to array of unknown bound. + (`#62863 `_). Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -8853,6 +8853,18 @@ } } } + if (isa(E)) { + QualType DestType = E->getType(); + if (const auto *IAT = 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] + QualType ResultType = Context.getConstantArrayType( + IAT->getElementType(), + llvm::APInt(Context.getTypeSize(Context.getSizeType()), 1), + /*SizeExpr=*/nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0); + E->setType(ResultType); + } + } } QualType Sema::getCompletedType(Expr *E) { diff --git a/clang/test/SemaCXX/paren-list-agg-init.cpp b/clang/test/SemaCXX/paren-list-agg-init.cpp --- a/clang/test/SemaCXX/paren-list-agg-init.cpp +++ b/clang/test/SemaCXX/paren-list-agg-init.cpp @@ -310,3 +310,23 @@ constexpr S s(0); // beforecxx20-warning {{aggregate initialization of type 'const S' from a parenthesized list of values is a C++20 extension}} \ // expected-error {{constexpr variable 's' must be initialized by a constant expression}} } + +namespace gh62863 { +int (&&arr)[] = static_cast(42); +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}} +int (&&arr1)[1] = static_cast(42); +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}} +int (&&arr2)[2] = static_cast(42); // expected-error {{reference to type 'int[2]' could not bind to an rvalue of type 'int[1]'}} +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}} +int (&&arr3)[3] = static_cast(42); +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[3]' from a parenthesized list of values is a C++20 extension}} + +int (&&arr4)[] = (int[])(42); +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}} +int (&&arr5)[1] = (int[])(42); +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}} +int (&&arr6)[2] = (int[])(42); // expected-error {{reference to type 'int[2]' could not bind to an rvalue of type 'int[1]'}} +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[1]' from a parenthesized list of values is a C++20 extension}} +int (&&arr7)[3] = (int[3])(42); +// beforecxx20-warning@-1 {{aggregate initialization of type 'int[3]' from a parenthesized list of values is a C++20 extension}} +}