[PATCH] D129531: [clang][C++20] P0960R3: Allow initializing aggregates from a parenthesized list of values

Alan Zhao via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 20 16:41:21 PDT 2022


ayzhao added inline comments.


================
Comment at: clang/lib/Sema/SemaInit.cpp:6052
+    } else if (S.getLangOpts().CPlusPlus20 && !TopLevelOfInitList)
+      TryOrBuildParenListInitialization(S, Entity, Kind, Args, *this,
+                                        /*VerifyOnly=*/true);
----------------
ayzhao wrote:
> ayzhao wrote:
> > Status update:
> > 
> > Right now this is causing the following 2 bugs:
> > 
> > 1. Currently, the below incorrect line of code:
> > 
> > ```
> > int arr[2] = 1;
> > ```
> > 
> > is being incorrectly treated the same as
> > 
> > ```
> > int arr[2](1);
> > ```
> > 
> > 2. The following below correct line of code:
> > ```
> > int arr[2] = static_cast<int[2]>(1);
> > ```
> > 
> > fails with the error:
> > 
> > ```
> > test-static-cast.cc:1:12: error: static_cast from 'int' to 'int[2]' is not allowed
> > int a[2] = static_cast<int[2]>(1);
> >            ^~~~~~~~~~~~~~~~~~~~~~
> > 1 error generated.
> > ```
> small fix: the incorrect error message for the 2nd bug is:
> 
> ```
> test-static-cast.cc:1:12: error: cannot initialize an array element of type 'int' with an rvalue of type 'int[2]'
> int a[2] = static_cast<int[2]>(1);
>            ^~~~~~~~~~~~~~~~~~~~~~
> 1 error generated.
> ```
So I fixed `int arr[2] = 1;` incorrectly being recognized as correct by allowing only `IK_DIRECT`.

I also noticed that my second example is incorrect since the `static_assert` expression is an rvalue, so it can't be assigned to an l-value array with an explicit type declaration. The correct test therefore should be

```
int (&&arr)[2] = static_cast<int[2]>(1);
```

which I've added to the test cases and does indeed pass.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D129531



More information about the cfe-commits mailing list