diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -463,6 +463,8 @@ - Fix crash when passing a braced initializer list to a parentehsized aggregate initialization expression. (`#63008 `_). +- 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 @@ -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(E)) { + if (auto *SubInit = dyn_cast(Cast->getSubExpr())) { + const Type *InnerType = SubInit->getType().getTypePtr(); + if (const auto *AT = dyn_cast(InnerType); + AT && AT->getSize() == 1) { + Cast->setType(SubInit->getType()); + } + } + } } 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 @@ -272,3 +272,14 @@ // expected-warning@-1 {{braces around scalar init}} // beforecxx20-warning@-2 {{aggregate initialization of type 'A' from a parenthesized list of values is a C++20 extension}} } + +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 (&&arrr2)[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}} +}