Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -9719,6 +9719,19 @@ SavedPendingLocalImplicitInstantiations; }; + class PerformPendingInstantiationsRAII { + public: + PerformPendingInstantiationsRAII(Sema &S) : S(S) {} + + ~PerformPendingInstantiationsRAII() { + S.PerformPendingInstantiations(); + assert(S.PendingInstantiations.empty() && + "there shouldn't be any pending instantiations"); + } + private: + Sema &S; + }; + /// A helper class for building up ExtParameterInfos. class ExtParameterInfoBuilder { SmallVector Infos; Index: clang/lib/Interpreter/IncrementalParser.cpp =================================================================== --- clang/lib/Interpreter/IncrementalParser.cpp +++ clang/lib/Interpreter/IncrementalParser.cpp @@ -150,6 +150,7 @@ llvm::CrashRecoveryContextCleanupRegistrar CleanupSema(&S); Sema::GlobalEagerInstantiationScope GlobalInstantiations(S, /*Enabled=*/true); Sema::LocalEagerInstantiationScope LocalInstantiations(S); + Sema::PerformPendingInstantiationsRAII PerformPendingInstantiations(S); Index: clang/test/Interpreter/template-recovery.cpp =================================================================== --- /dev/null +++ clang/test/Interpreter/template-recovery.cpp @@ -0,0 +1,17 @@ +// clang-format off +// RUN: clang-repl "template T f() { return T(); };" \ +// RUN: "auto ptu2 = f(); err;" "auto ptu2 = f();" +// REQUIRES: host-supports-jit +// UNSUPPORTED: system-aix +// RUN: cat %s | clang-repl | FileCheck %s +// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s + +extern "C" int printf(const char *, ...); +int i = 10; +auto r1 = printf("i = %d\n", i); +// CHECK: i = 10 + +template T f() { return T(); }; +auto ptu2 = f(); + +%quit