[clang] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 18:52:48 PDT 2023


================
@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema &S,
       if (T1Quals.hasAddressSpace())
         Sequence.AddQualificationConversionStep(
             cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+      else if (S.getLangOpts().CPlusPlus20 &&
+               isa<IncompleteArrayType>(T1->getUnqualifiedDesugaredType()) &&
+               DestType->isRValueReferenceType()) {
+        // [dcl.init.list] p3.10
+        // unless T is “reference to array of unknown bound of U”, in which case
+        // the type of the prvalue is the type of x in the declaration U x[] H,
+        // where H is the initializer list.
+        Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);
----------------
HerrCai0907 wrote:

I have changed sequence in the latest commit.
The reason to qualification conversion is that in cpp 20, spec. requires support cast from `initializer list` to `reference to array of unknown bound`.

for example: 
```
void foo(int a) {
  auto f = [](int(&&)[]) {};
  f({a});
}
```

`{a}` should be casted from int[1] to int[].


ast will look like:
```
      `-CXXOperatorCallExpr 0x12b904d18 <col:3, col:8> 'void':'void' '()'
        |-ImplicitCastExpr 0x12b904c58 <col:4, col:8> 'void (*)(int (&&)[]) const' <FunctionToPointerDecay>
        | `-DeclRefExpr 0x12b904bd8 <col:4, col:8> 'void (int (&&)[]) const' lvalue CXXMethod 0x12b904008 'operator()' 'void (int (&&)[]) const'
        |-ImplicitCastExpr 0x12b904c70 <col:3> 'const (lambda at ID/test.cpp:2:12)' lvalue <NoOp>
        | `-DeclRefExpr 0x12b904b10 <col:3> '(lambda at ID/test.cpp:2:12)':'(lambda at ID/test.cpp:2:12)' lvalue Var 0x12b8ee210 'f' '(lambda at ID/test.cpp:2:12)':'(lambda at ID/test.cpp:2:12)'
        `-MaterializeTemporaryExpr 0x12b904d00 <col:5, col:7> 'int[]':'int[]' xvalue
          `-ImplicitCastExpr 0x12b904ce8 <col:5, col:7> 'int[]':'int[]' <NoOp>
            `-InitListExpr 0x12b904c88 <col:5, col:7> 'int[1]'
              `-ImplicitCastExpr 0x12b904cc8 <col:6> 'int' <LValueToRValue>
                `-DeclRefExpr 0x12b904b30 <col:6> 'int' lvalue ParmVar 0x12b8edfb8 'a' 'int'
```


https://github.com/llvm/llvm-project/pull/65918


More information about the cfe-commits mailing list