Skip to content

Commit 52dfd1f

Browse files
Emery Conradconrade-ctc
authored andcommitted
Reset sticky diagnostic state after a rejected InstantiateTemplate
The rejection (e.g. a type argument for a non-type parameter) emits its error diagnostic outside any incremental parse, and DiagnosticsEngine's ErrorOccurred flag is sticky: the next IncrementalParser::Parse reported the stale flag as its own failure and then soft-reset it, poisoning exactly one parse. Contain the rejection with a DiagnosticErrorTrap-based RAII that restores a clean diagnostic state when the instantiation introduced errors - the same cleanup IncrementalParser performs on a real parse failure and InstantiateFunctionDefinition performs inline. Co-developed-with-the-help-of: Claude Code (Fable 5, human in the loop)
1 parent 1cfe7ee commit 52dfd1f

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎lib/CppInterOp/CppInterOp.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5696,6 +5696,35 @@ std::string ObjToString(const char* TyRef, void* obj) {
56965696
return INTEROP_RETURN(getInterp().toString(TyRef, obj));
56975697
}
56985698

5699+
namespace {
5700+
/// Traps diagnostics emitted by Sema entry points invoked outside any
5701+
/// incremental parse and, when errors were introduced, restores a clean
5702+
/// diagnostic state on scope exit — the same cleanup IncrementalParser
5703+
/// performs on a real parse failure. Without it, DiagnosticsEngine's sticky
5704+
/// ErrorOccurred flag makes the next IncrementalParser::Parse report the
5705+
/// stale flag as its own failure (and then soft-reset it), spuriously
5706+
/// failing exactly one parse.
5707+
class StickyDiagnosticResetRAII {
5708+
clang::DiagnosticsEngine* m_Diags;
5709+
clang::DiagnosticErrorTrap m_Trap;
5710+
5711+
public:
5712+
StickyDiagnosticResetRAII(clang::DiagnosticsEngine& Diags)
5713+
: m_Diags(&Diags), m_Trap(Diags) {}
5714+
StickyDiagnosticResetRAII(const StickyDiagnosticResetRAII&) = delete;
5715+
StickyDiagnosticResetRAII&
5716+
operator=(const StickyDiagnosticResetRAII&) = delete;
5717+
StickyDiagnosticResetRAII(StickyDiagnosticResetRAII&&) = delete;
5718+
StickyDiagnosticResetRAII& operator=(StickyDiagnosticResetRAII&&) = delete;
5719+
~StickyDiagnosticResetRAII() {
5720+
if (m_Trap.hasErrorOccurred()) {
5721+
m_Diags->Reset(/*soft=*/true);
5722+
m_Diags->getClient()->clear();
5723+
}
5724+
}
5725+
};
5726+
} // namespace
5727+
56995728
static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
57005729
TemplateArgumentListInfo& TLI, Sema& S,
57015730
bool instantiate_body) {
@@ -5793,6 +5822,10 @@ DeclRef InstantiateTemplate(compat::Interpreter& I, DeclRef tmpl,
57935822
auto* TmplD = unwrap<TemplateDecl>(tmpl);
57945823
// We will create a new decl, push a transaction.
57955824
compat::SynthesizingCodeRAII RAII(&getInterp());
5825+
// A rejected instantiation emits its error diagnostic outside any
5826+
// incremental parse; contain it so the sticky error state cannot fail the
5827+
// parse that follows.
5828+
StickyDiagnosticResetRAII DiagGuard(S.getDiagnostics());
57965829
return InstantiateTemplate(TmplD, TemplateArgs, S, instantiate_body);
57975830
}
57985831

‎unittests/CppInterOp/ScopeReflectionTest.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,30 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_InstantiateTemplate) {
13111311
EXPECT_TRUE(TA4_1.getAsIntegral() == 3);
13121312
}
13131313

1314+
TYPED_TEST(CPPINTEROP_TEST_MODE,
1315+
ScopeReflection_RejectedInstantiationDoesNotPoisonNextParse) {
1316+
std::vector<Decl*> Decls;
1317+
std::string code = R"(
1318+
struct PoisonTag {};
1319+
template <unsigned N> struct PoisonBuf { int t; };
1320+
)";
1321+
1322+
GetAllTopLevelDecls(code, Decls);
1323+
1324+
// A type argument for the non-type parameter N: Sema rejects the
1325+
// instantiation and emits an error diagnostic outside any incremental
1326+
// parse.
1327+
Cpp::TypeRef TagTy = Cpp::GetTypeFromScope(Decls[0]);
1328+
std::vector<Cpp::TemplateArgInfo> args = {TagTy.data};
1329+
EXPECT_FALSE(Cpp::InstantiateTemplate(Decls[1], args));
1330+
1331+
// The sticky DiagnosticsEngine error state must not fail the parses that
1332+
// follow (the poison was one-shot: only the first parse after the
1333+
// rejection reported failure).
1334+
EXPECT_EQ(Cpp::Declare("int rejected_instantiation_probe_1 = 1;"), 0);
1335+
EXPECT_EQ(Cpp::Declare("int rejected_instantiation_probe_2 = 2;"), 0);
1336+
}
1337+
13141338
TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_GetClassTemplateArgs) {
13151339
std::vector<Decl*> Decls;
13161340
std::string code = R"(

0 commit comments

Comments
 (0)