https://github.com/anondeveg updated 
https://github.com/llvm/llvm-project/pull/218571

>From c5efad391fae90f0cc9b44d58b35c66430c62a01 Mon Sep 17 00:00:00 2001
From: Anondev <[email protected]>
Date: Tue, 25 Aug 2026 04:11:48 +0300
Subject: [PATCH] [clang][Parse] Fix DeclContext reentry to use the template's
 lexical TU, not the current fragment's TU

Fixes 217073
---
 clang/docs/ReleaseNotes.md                    |  1 +
 clang/lib/Parse/ParseTemplate.cpp             | 20 +++++++++++++------
 .../delayed-template-parsing-incremental.cpp  |  7 +++++++
 3 files changed, 22 insertions(+), 6 deletions(-)
 create mode 100644 
clang/test/Interpreter/delayed-template-parsing-incremental.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index ca0dbfa2af229..170dedc5fe5d7 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -532,6 +532,7 @@ features cannot lower the translation-unit ABI level;
   definition of a member of a class template added a default argument to a
   parameter that follows a parameter pack (e.g.
   `template <typename... T> S::S(T..., int = 10) {}`).  (#GH216211)
+- Fixed an assertion failure when instantiating a late-parsed function 
template defined in an earlier translation unit with 
-fdelayed-template-parsing. (#GH217073)
 
 #### Bug Fixes to AST Handling
 
diff --git a/clang/lib/Parse/ParseTemplate.cpp 
b/clang/lib/Parse/ParseTemplate.cpp
index 735a9bd1f9f1c..cba4a29888516 100644
--- a/clang/lib/Parse/ParseTemplate.cpp
+++ b/clang/lib/Parse/ParseTemplate.cpp
@@ -1451,17 +1451,25 @@ void 
Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
   // Track template parameter depth.
   TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
 
-  // To restore the context after late parsing.
-  Sema::ContextRAII GlobalSavedContext(
-      Actions, Actions.Context.getTranslationUnitDecl());
-
   MultiParseScope Scopes(*this);
 
   // Get the list of DeclContexts to reenter.
   SmallVector<DeclContext*, 4> DeclContextsToReenter;
-  for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit();
-       DC = DC->getLexicalParent())
+  DeclContext *LexicalTU = nullptr;
+  for (DeclContext *DC = FunD; DC; DC = DC->getLexicalParent()) {
+    if (DC->isTranslationUnit()) {
+      LexicalTU = DC;
+      break;
+    }
     DeclContextsToReenter.push_back(DC);
+  }
+
+  if (!LexicalTU) {
+    LexicalTU = Actions.Context.getTranslationUnitDecl();
+  }
+
+  // To restore the context after late parsing.
+  Sema::ContextRAII GlobalSavedContext(Actions, LexicalTU);
 
   // Reenter scopes from outermost to innermost.
   for (DeclContext *DC : reverse(DeclContextsToReenter)) {
diff --git a/clang/test/Interpreter/delayed-template-parsing-incremental.cpp 
b/clang/test/Interpreter/delayed-template-parsing-incremental.cpp
new file mode 100644
index 0000000000000..897a62abad094
--- /dev/null
+++ b/clang/test/Interpreter/delayed-template-parsing-incremental.cpp
@@ -0,0 +1,7 @@
+// clang/test/Interpreter/delayed-template-parsing.cpp
+// REQUIRES: host-supports-jit
+// RUN: cat %s | clang-repl -Xcc -fdelayed-template-parsing 2>&1
+// see ISSUE 217073 and PR 218571
+
+namespace GH217073{ template <typename T> int f(T) { return 0; } }
+GH217073::f(1);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to