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

    <tr>
        <th>Summary</th>
        <td>
            [analyzer] Assertion inside `llvm::cast<>()` fails
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:static analyzer
      </td>
    </tr>

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

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

<pre>
    This snippet causes the static analyzer to fail an assetion.
```c++
struct Aggregate {
    int x;
    ~Aggregate() { int dtor = 0; }
};

int main() {
    int n;

    Aggregate *arr = new Aggregate[n];

    for (int i = 0; i < 4; ++i)
        arr[i].x = i;
}
```

The AST of the new expression looks like this:
```
`-CXXNewExpr 'Aggregate *' array Function 'operator new[]' 'void *(unsigned long)'
   |-ImplicitCastExpr 'unsigned long' <IntegralCast>
   | `-ImplicitCastExpr 'int' <LValueToRValue>
   |   `-DeclRefExpr 'int' lvalue Var 'n' 'int'
   `-CXXConstructExpr 'Aggregate[]' 'void () noexcept'
```
Note the double `ImplicitCastExpr`, where both of them evaluate to `Undefined`.
Later in `ExprEngine::bindReturnValue()` there is `Size.castAs<DefinedOrUnknownSVal>()`,
where `Size` is the `Undefined` value, so the assertion inside `llvm::cast()` fails.

```
decltype(auto) llvm::cast(const From &) [To = clang::ento::DefinedOrUnknownSVal, From = clang::ento::SVal]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"'
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx1VU1v2zAM_TXORWjg2LGTHHxwkxYYMGxAmw27yjLjaFUkQ5LbZr9-pJSvuV3gxLZIPj4-Ukpj2mO13UvHnJZ9D54JPjhwzO-BOc-9FIxrro5_wDJv2I5LhQuMOwdeGj1N0k2S1kmZxksk2T1dYdV5OwjP6q6z0HEPLFmcLAw_Unv2nuQ3K8ni4eKbZMskW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