Skip to content

Fix crash when doing special member lookup on forward-declared classes (Fixes llvm/llvm-project#144642) #144828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ameya674
Copy link

The compiler tends to crash when encountering a forward-declared incomplete type in Sema::LookupSpecialMember.

Fixed this by adding a check to handle the incomplete types.

The patch compiles and clang triggers the correct error message.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-clang

Author: Ameya Gurjar (Ameya674)

Changes

The compiler tends to crash when encountering a forward-declared incomplete type in Sema::LookupSpecialMember.

Fixed this by adding a check to handle the incomplete types.

The patch compiles and clang triggers the correct error message.


Full diff: https://github.com/llvm/llvm-project/pull/144828.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaLookup.cpp (+23-2)
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 5ad9dd8ed0d3e..bf114e5c9f996 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -3382,8 +3382,29 @@ Sema::SpecialMemberOverloadResult
 Sema::LookupSpecialMember(CXXRecordDecl *RD, CXXSpecialMemberKind SM,
                           bool ConstArg, bool VolatileArg, bool RValueThis,
                           bool ConstThis, bool VolatileThis) {
-  assert(CanDeclareSpecialMemberFunction(RD) &&
-         "doing special member lookup into record that isn't fully complete");
+  
+  if (!CanDeclareSpecialMemberFunction(RD)) {
+
+    llvm::FoldingSetNodeID ID;
+    ID.AddPointer(RD);
+    ID.AddInteger(llvm::to_underlying(SM));
+    ID.AddInteger(ConstArg);
+    ID.AddInteger(VolatileArg);
+    ID.AddInteger(RValueThis);
+    ID.AddInteger(ConstThis);
+    ID.AddInteger(VolatileThis);
+    void *InsertPoint;
+
+    SpecialMemberOverloadResultEntry* Result = BumpAlloc.Allocate<SpecialMemberOverloadResultEntry>();
+    Result = new (Result) SpecialMemberOverloadResultEntry(ID);
+    Result->setMethod(nullptr);
+    Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
+    SpecialMemberCache.InsertNode(Result, InsertPoint);
+    return *Result;
+  }
+  
+  // assert(CanDeclareSpecialMemberFunction(RD) &&
+  //        "doing special member lookup into record that isn't fully complete");
   RD = RD->getDefinition();
   if (RValueThis || ConstThis || VolatileThis)
     assert((SM == CXXSpecialMemberKind::CopyAssignment ||

@Ameya674 Ameya674 changed the title Fix crash when doing special member lookup on forward-declared classes Fix crash when doing special member lookup on forward-declared classes (Fixes llvm/llvm-project#144642) Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants