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

    <tr>
        <th>Summary</th>
        <td>
            Problems with constexpr placement-new implementation
        </td>
    </tr>

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

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

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

<pre>
    This crashes:
```c++
consteval int ok() {
    int i;
    new (&i) int[1]{1};
    return i;
}

static_assert(ok() == 12);
```
```console
clang++: /root/llvm-project/clang/include/clang/AST/Type.h:8674: const clang::ArrayType* clang::Type::castAsArrayTypeUnsafe() const: Assertion `isa<ArrayType>(CanonicalType)' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++2c <source>
1.      <eof> parser at end of file
 #0 0x0000000003b1e5e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b1e5e8)
 #1 0x0000000003b1c2ac llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b1c2ac)
 #2 0x0000000003a64768 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007745eea42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007745eea969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007745eea42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007745eea287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007745eea2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007745eea39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00000000079bc61f CheckEvaluationResult(CheckEvaluationResultKind, (anonymous namespace)::EvalInfo&, clang::SourceLocation, clang::QualType, clang::APValue const&, clang::Expr::ConstantExprKind, clang::FieldDecl const*, llvm::SmallPtrSet<clang::MaterializeTemporaryExpr const*, 8u>&) ExprConstant.cpp:0:0
#10 0x00000000079c6ee7 (anonymous namespace)::ExtractSubobjectHandler::result_type findSubobject<(anonymous namespace)::ExtractSubobjectHandler>((anonymous namespace)::EvalInfo&, clang::Expr const*, (anonymous namespace)::CompleteObject const&, (anonymous namespace)::SubobjectDesignator const&, (anonymous namespace)::ExtractSubobjectHandler&) ExprConstant.cpp:0:0
#11 0x00000000079d4f97 handleLValueToRValueConversion((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::QualType, (anonymous namespace)::LValue const&, clang::APValue&, bool) ExprConstant.cpp:0:0
#12 0x0000000007a114ba (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#13 0x00000000079dd44b clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::IntExprEvaluator, bool>::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#14 0x00000000079ccd6e Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) ExprConstant.cpp:0:0
#15 0x00000000079eff34 EvaluateStmt((anonymous namespace)::StmtResult&, (anonymous namespace)::EvalInfo&, clang::Stmt const*, clang::SwitchCase const*) (.part.0) ExprConstant.cpp:0:0
#16 0x00000000079ee138 EvaluateStmt((anonymous namespace)::StmtResult&, (anonymous namespace)::EvalInfo&, clang::Stmt const*, clang::SwitchCase const*) (.part.0) ExprConstant.cpp:0:0
#17 0x00000000079f41eb HandleFunctionCall(clang::SourceLocation, clang::FunctionDecl const*, (anonymous namespace)::LValue const*, clang::Expr const*, llvm::ArrayRef<clang::Expr const*>, (anonymous namespace)::CallRef, clang::Stmt const*, (anonymous namespace)::EvalInfo&, clang::APValue&, (anonymous namespace)::LValue const*) ExprConstant.cpp:0:0
#18 0x0000000007a36ee7 (anonymous namespace)::IntExprEvaluator::VisitCallExpr(clang::CallExpr const*) ExprConstant.cpp:0:0
#19 0x00000000079dd365 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::IntExprEvaluator, bool>::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
```

And so does this:
```c++
consteval int array1() {
    int i[2];
    new (&i) int[]{1};
    return i[0] + i[1];
}
static_assert(array1() == 3);
```
```console
AST/APValue.h:523: APValue &clang::APValue::getArrayFiller(): Assertion `hasArrayFiller() && "No array filler"' failed.
```

and this:
```c++
consteval int array1() {
    int i[2];
    new (&i) int(0);
    return i[0] + i[1];
}
static_assert(array1() == 3);
```

```console
AST/APValue.h:530: unsigned int clang::APValue::getArrayInitializedElts() const: Assertion `isArray() && "Invalid accessor"' failed
```

All those cases are taken from `cxx2c-constexpr-placement-new.cpp`. The problems only arise when actually reading from `i`.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWV1z27rR_jXwDYYaEvy-0IUsWfNm3rTHjd1z6wHBpYQaBDgAaEv99R2AlCxKOZGVpnOmzTiOCCwWz-6zH-KGGsM3EmCO0nuUru5ob7dKz21FoQat7ypV7-fPW24w09RswaB4gcIVChcoC4cfhsi9-_GrTElj4Y0KzKXF6hWRApESo3zcxxj7HY7ikxUJ79hLZtxJc2lReh-hdIXy-wjlq4mwBttreaLBCQwf_G9jqeXshRoD2iJSfICIVyhe4YggUn4cPphxbpWSRgkYrRJUbkYz4wVGZK2VsoishXhrg06rfwBzj6Pcmksm-hpOVhZPz4isn_cdzLYoXhRZnjhN3l14EIoXKF4stKZ7J4bI4nTdL_lPjBq7MEe5v0tDGxgt9Oqc3oU3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