Skip to content

Commit

Permalink
[clangd][AST] Handle uninitialized type constraints
Browse files Browse the repository at this point in the history
The ASTWriter currently assumes template type constraints to be
initialized ((bool)getTypeConstraint() == hasTypeConstraint()).
The attached test case presents a scenario where that is not the case.

This patch removes the assumption and adds another boolean to the
serialization, to explicitly encode whether the type constraint has been
initialized.

Fixes #99036.
Fixes #109354.
  • Loading branch information
falbrechtskirchinger committed Sep 30, 2024
1 parent 5df7d88 commit 8913503
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
20 changes: 20 additions & 0 deletions clang-tools-extra/clangd/unittests/ASTTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,26 @@ TEST(ClangdAST, PreferredIncludeDirective) {
Symbol::IncludeDirective::Import);
}

TEST(ClangdAST, HandleUninitializedTypeConstraints) {
auto TU = TestTU::withHeaderCode(R"cpp(
template<typename T>
concept C; // error-ok
template<C U>
void f();
)cpp");
TU.ExtraArgs.push_back("-std=c++20");

auto AST = TU.build();

const auto &F = llvm::cast<FunctionTemplateDecl>(findDecl(AST, "f"));
const auto *Params = F.getTemplateParameters();
const auto *U = llvm::cast<TemplateTypeParmDecl>(Params->getParam(0));
const auto *TC = U->getTypeConstraint();

EXPECT_TRUE(U->hasTypeConstraint());
EXPECT_FALSE(/*TypeConstraintInitialized=*/TC != nullptr);
}

} // namespace
} // namespace clangd
} // namespace clang
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,7 @@ void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {

D->setDeclaredWithTypename(Record.readInt());

if (D->hasTypeConstraint()) {
if (Record.readBool() && D->hasTypeConstraint()) {
ConceptReference *CR = nullptr;
if (Record.readBool())
CR = Record.readConceptReference();
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Serialization/ASTWriterDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,7 @@ void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Record.push_back(D->wasDeclaredWithTypename());

const TypeConstraint *TC = D->getTypeConstraint();
assert((bool)TC == D->hasTypeConstraint());
Record.push_back(/*TypeConstraintInitialized=*/TC != nullptr);
if (TC) {
auto *CR = TC->getConceptReference();
Record.push_back(CR != nullptr);
Expand All @@ -1917,7 +1917,7 @@ void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
if (OwnsDefaultArg)
Record.AddTemplateArgumentLoc(D->getDefaultArgument());

if (!TC && !OwnsDefaultArg &&
if (!D->hasTypeConstraint() && !OwnsDefaultArg &&
D->getDeclContext() == D->getLexicalDeclContext() &&
!D->isInvalidDecl() && !D->hasAttrs() &&
!D->isTopLevelDeclInObjCContainer() && !D->isImplicit() &&
Expand Down Expand Up @@ -2580,6 +2580,7 @@ void ASTWriter::WriteDeclAbbrevs() {
// TemplateTypeParmDecl
Abv->Add(
BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // wasDeclaredWithTypename
Abv->Add(BitCodeAbbrevOp(0)); // TypeConstraintInitialized
Abv->Add(BitCodeAbbrevOp(0)); // OwnsDefaultArg
DeclTemplateTypeParmAbbrev = Stream.EmitAbbrev(std::move(Abv));

Expand Down

0 comments on commit 8913503

Please sign in to comment.