[PATCH] D152003: [clang] Fix `static_cast` to array of unknown bound
Aaron Ballman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 5 06:28:12 PDT 2023
aaron.ballman added reviewers: clang-language-wg, cor3ntin.
aaron.ballman added a comment.
Thank you for working on this!
================
Comment at: clang/lib/Sema/SemaCast.cpp:1269-1272
+ ResultType = Self.Context.getConstantArrayType(
+ IAT->getElementType(),
+ llvm::APInt(Self.Context.getTypeSize(Self.Context.getSizeType()), 1),
+ /*SizeExpr=*/nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0);
----------------
This seems incorrect to me -- I think the `CastOperation` should be created with the correct result type to begin with. What you are citing is a note, but the actual wording that specifies that note is: https://eel.is/c++draft/dcl.init#general-16.5 so I think the fix approach is somewhat correct but the code lives in the wrong place, this should be closer to the initialization code I think.
================
Comment at: clang/test/SemaCXX/paren-list-agg-init.cpp:276-285
+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}}
----------------
I'd like to see test coverage for:
```
int (&&arr)[] = (int[])(42);
int (&&arr1)[1] = (int[])(42);
int (&&arrr2)[2] = (int[])(42);
int (&&arr3)[3] = (int[3])(42);
```
where we're using a C-style cast, because: http://eel.is/c++draft/expr.cast#4
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D152003/new/
https://reviews.llvm.org/D152003
More information about the cfe-commits
mailing list