Skip to content

release/20.x: [clang-repl] Fix error recovery while PTU cleanup (#127467) #142445

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

Closed
wants to merge 2 commits into from

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented Jun 2, 2025

Backport 3b4c51b

Requested by: @anutosh491

Fixes llvm#123300

What is seen
```
clang-repl> int x = 42;
clang-repl> auto capture = [&]() { return x * 2; };
In file included from <<< inputs >>>:1:
input_line_4:1:17: error: non-local lambda expression cannot have a capture-default
    1 | auto capture = [&]() { return x * 2; };
      |                 ^
zsh: segmentation fault  clang-repl --Xcc="-v"

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
  * frame #0: 0x0000000107b4f8b8 libclang-cpp.19.1.dylib`clang::IncrementalParser::CleanUpPTU(clang::PartialTranslationUnit&) + 988
    frame #1: 0x0000000107b4f1b4 libclang-cpp.19.1.dylib`clang::IncrementalParser::ParseOrWrapTopLevelDecl() + 416
    frame #2: 0x0000000107b4fb94 libclang-cpp.19.1.dylib`clang::IncrementalParser::Parse(llvm::StringRef) + 612
    frame #3: 0x0000000107b52fec libclang-cpp.19.1.dylib`clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) + 180
    frame #4: 0x0000000100003498 clang-repl`main + 3560
    frame #5: 0x000000018d39a0e0 dyld`start + 2360
```

Though the error is justified, we shouldn't be interested in exiting
through a segfault in such cases.

The issue is that empty named decls weren't being taken care of
resulting into this assert

https://github.com/llvm/llvm-project/blob/c1a229252617ed58f943bf3f4698bd8204ee0f04/clang/include/clang/AST/DeclarationName.h#L503

Can also be seen when the example is attempted through xeus-cpp-lite.

![image](https://github.com/user-attachments/assets/9b0e6ead-138e-4b06-9ad9-fcb9f8d5bf6e)

(cherry picked from commit 3b4c51b)
@llvmbot
Copy link
Member Author

llvmbot commented Jun 2, 2025

@vgvassilev What do you think about merging this PR to the release branch?

@llvmbot llvmbot requested a review from vgvassilev June 2, 2025 17:46
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 2, 2025
@llvmbot
Copy link
Member Author

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-clang

Author: None (llvmbot)

Changes

Backport 3b4c51b

Requested by: @anutosh491


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

2 Files Affected:

  • (modified) clang/lib/Interpreter/IncrementalParser.cpp (+1-1)
  • (modified) clang/test/Interpreter/lambda.cpp (+13-2)
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1d223e230669c 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,7 +175,7 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl *MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
     auto *ND = dyn_cast<NamedDecl>(D);
-    if (!ND)
+    if (!ND || ND->getDeclName().isEmpty())
       continue;
     // Check if we need to clean up the IdResolver chain.
     if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
diff --git a/clang/test/Interpreter/lambda.cpp b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..7e5e1057e4c9e 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,14 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error {{non-local lambda expression cannot have a capture-default}}
+auto capture = [&]() { return x * 2; };
+
+// Ensure interpreter continues and x is still valid
+printf("x = %d\n", x);
+// CHECK: x = 42
+
+%quit
\ No newline at end of file

@anutosh491
Copy link
Member

cc @DavidSpickett ,

I executed a cherry pick on my patch to move it into the next release. Haven't had the time to go through the issue you discovered on Windows but is this all we want ? Or should I change something here ?

@DavidSpickett
Copy link
Collaborator

We should cherry pick beffd15 as well so that release builders don't get failures.

I have confirmed it "fixes" the test on Windows on Arm and x64, and it's very unlikely that your code changes are making any difference to the output ordering. It's likely a pre-existing issue.

So this PR does not need to change.

@DavidSpickett
Copy link
Collaborator

DavidSpickett commented Jun 11, 2025

Not sure how to do that given that it relies on this change. I don't think you can push more changes to this PR yourself.

Could you close this and re-do the command with both commit hashes? (https://llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches)

@vgvassilev
Copy link
Contributor

Are you guys saying we should merge this one and merge another one later on?

@DavidSpickett
Copy link
Collaborator

Yes, we need to have the commit in this PR, and then beffd15 to prevent test failures on Windows.

@anutosh491
Copy link
Member

Yes, we need to have the commit in this PR, and then beffd15 to prevent test failures on Windows.

Okay in that case let's do that.
Let's take this in. And then on top (on the pr itself) we can do a cherry pick with the commit pointed above.

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the discussed plan, lgtm!

@github-project-automation github-project-automation bot moved this from Needs Review to Needs Merge in LLVM Release Status Jun 11, 2025
@anutosh491
Copy link
Member

cc @tstellar

I think this should be ready now. Could you help us out here ?

@tstellar
Copy link
Collaborator

Was beffd15 squashed in with this commit?

@anutosh491
Copy link
Member

Was beffd15 squashed in with this commit?

We thought we would take this in and then cherry pick that on top of this. I am not sure if the cherry pick done by the bot here can be updated in any way to also include changes from the other commit. Is that feasible ? In that case maybe we can take both in through the same pr. Not sure if it can be done manually by me though.

Could you help us with the same if that's possible ?

@DavidSpickett
Copy link
Collaborator

#143851 has both.

@anutosh491 anutosh491 closed this Jun 12, 2025
@tstellar tstellar moved this from Needs Merge to Done in LLVM Release Status Jun 12, 2025
@tstellar tstellar moved this from Done to Won't Merge in LLVM Release Status Jun 12, 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
Status: Won't Merge
Development

Successfully merging this pull request may close these issues.

5 participants