llvm / llvm/llvm-project

[clang] const: Assertion `D->isCompleteDefinition() && "Cannot layout type before complete!"' failed in clang/lib/AST/RecordLayoutBuilder.cpp when using "-std=c++23"

Open
#192,369 4 comments 0 reactions 0 assignees View on GitHub
c++23 clang:frontend crash regression:20
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

We recently discovered our code base will failed to build when enable assertion on ToT clang. We created a simplified reproducer:

Code:

```c++
template
struct PointerToMemberTraits;

template
struct PointerToMemberTraits {
using class_type = Class;
using member_type = T;
};

template ::class_type>
constexpr auto& MemberOf(Class& ptr) {
return ptr.*kMemberPtr;
}

template
constexpr auto ContainerOf(Member* ptr) {
return (typename PointerToMemberTraits::class_type*)nullptr;
}

template
struct CustomFutureList {
using value_type = decltype(*kGetFutureImpl((int*)nullptr));
};

template
using FutureList = CustomFutureList, MemberOf>;

struct BaseChannelFuture {
int core_;
using List = FutureList<&BaseChannelFuture::core_>;
};
```

Compiler options: "-std=c++23"

Failure we got:

Assertion:

```console
llvm: clang/lib/AST/RecordLayoutBuilder.cpp:3390:
const ASTRecordLayout &clang::ASTContext::getASTRecordLayout(const RecordDecl *) const:
Assertion `D->isCompleteDefinition() && "Cannot layout type before complete!"' failed.
```
backtrace:

```console
PLEASE submit a bug report to [https://github.com/llvm/llvm-project/issues/](https://www.google.com/url?sa=D&q=https%3A%2F%2Fgithub.com%2Fllvm%2Fllvm-project%2Fissues%2F) and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /mnt/nvme_crypt/SRC/clang/clang_tot_ci/bin/llvm clang++ -cc1 -std=c++23 channel-21896f.cpp
1. channel-21896f.cpp:31:18: at annotation token
2. channel-21896f.cpp:29:1: parsing struct/union/class body 'BaseChannelFuture'
3. channel-21896f.cpp:12:17: instantiating function definition 'MemberOf<&BaseChannelFuture::core_, BaseChannelFuture>'
#0 0x00005635b1cfacf8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/mnt/nvme_crypt/SRC/clang/clang_tot_ci/bin/llvm+0x9ac6cf8)
./channel-21896f.sh: line 4: 2943618 Aborted (core dumped) "/mnt/nvme_crypt/SRC/clang/clang_tot_ci/bin/llvm" "clang++" "-cc1" "-std=c++23" "channel-21896f.cpp"
```

This was tested on clang revision 9de31c4a3e77ceb1e736d4df3ae9e3f5a1dc8748 and e0f5ad74be2b2f127442f721c70c6753c41e6d8c. Both will fail due to this assertion error.

If we delete the "-std=c++23" from the reproducer, the same code pass the build without issue.

Packed reproducer: [repro_shrink.tar.gz](https://github.com/user-attachments/files/26767915/repro_shrink.tar.gz)

Contributor guide

Open the contributing guide

Research direction

Start with the reproducer and run it using clang++ -cc1 -std=c++23 to confirm the assertion in clang/lib/AST/RecordLayoutBuilder.cpp. Trace the instantiation shown in the stack dump, beginning at MemberOf and the BaseChannelFuture definition. Done means the reproducer builds without the assertion while preserving the existing non-C++23 behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.