<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57135>57135</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Assertion failure in case of nested `ArrayInitLoopExpr` using `-std=c++17`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:frontend,
            crash
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          isuckatcs
      </td>
    </tr>
</table>

<pre>
    The following snippet causes an assertion failure when compiled with `-std=c++17`:
```c++
struct S {
  int i;
};

void crash() {
  S s[2][2];

  int res[4];

  res[0] = [s] { return s[0][0].i; }();                                       <-- crash here
}
```

The failure is caused by the nested `ArrayInitLoopExpr`. The related part of the AST looks like this:
```
|-ArrayInitLoopExpr 'S[2][2]'
| |-OpaqueValueExpr 'S[2][2]' lvalue
| | `-DeclRefExpr'S[2][2]' lvalue Var 's' 'S[2][2]'
| `-ArrayInitLoopExpr 'S[2]'                                                   <-- note this
|   |-OpaqueValueExpr 'S[2]' lvalue
|   | `-ArraySubscriptExpr 'S[2]' lvalue
|   |   |-ImplicitCastExpr 'S (*)[2]' <ArrayToPointerDecay>
|   |   | `-OpaqueValueExpr 'S[2][2]' lvalue
|   |   |   `-DeclRefExpr 'S[2][2]' lvalue Var 's' 'S[2][2]'
|   |   `-ArrayInitIndexExpr 'unsigned long'
|   `-CXXConstructExpr 'S' 'void (const S &) noexcept'
```

In `ArrayExprEvaluator::VisitArrayInitLoopExpr()` we loop over the elements of the init loop and
try to evaluate each of them. 

1.) We visit the first init loop with type `S[2][2]`.
2.) We visit the nested one for the **first** time, which has the type `S[2]`. 
3.) We visit the nested one for the **second** time, which has the type `S[2]`. 
4.) `assert(Result.isAbsent() && "local created multiple times");` fails in `CallStackFrame::createLocal()`.

For the stack trace please see [godbolt](https://godbolt.org/z/1jEcqc663).
</pre>
<img width="1px" height="1px" alt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