Skip to content

Reset sticky diagnostic state after a rejected InstantiateTemplate - #1070

Open
conrade-ctc wants to merge 1 commit into
compiler-research:mainfrom
conrade-ctc:reset-diags-after-rejected-instantiation
Open

conrade-ctc wants to merge 1 commit into
compiler-research:mainfrom
conrade-ctc:reset-diags-after-rejected-instantiation

Conversation

@conrade-ctc

@conrade-ctc conrade-ctc commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A rejected Cpp::InstantiateTemplate (e.g. a type argument supplied for a non-type template parameter) emits its error diagnostic outside any incremental parse, and DiagnosticsEngine's ErrorOccurred flag is sticky. The next IncrementalParser::Parse sees the stale flag after a perfectly good parse, reports "Parsing failed.", and soft-resets the engine — so exactly one subsequent Cpp::Declare/Process fails spuriously. Downstream (cppyy), one rejected template subscript makes the next cppyy.include/cppyy.cppdef report failure while actually loading the code, which can silently corrupt fail-soft feature probes.

Per review, compat::DiagnosticsEngineRAII (from #849) now guards the instantiation. On scope exit, it soft-resets the engine if an error occurred, as IncrementalParser does after a failed parse. The diagnostic text still prints.

Type of change

  • Bug fix

Testing

New ScopeReflection_RejectedInstantiationDoesNotPoisonNextParse: a rejected instantiation followed by two Cpp::Declare calls, both of which must succeed. Fails before the fix (the first Declare reports failure), passes after.

Checklist

  • I have read the contribution guide recently

🤖 Done with the help of Claude Code (Fable 5, human in the loop)

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
// the incremental parser performs on a real parse failure).
clang::DiagnosticsEngine& Diags = S.getDiagnostics();
bool HadErrors = Diags.hasErrorOccurred();
Decl* Result = InstantiateTemplate(TmplD, TemplateArgs, S, instantiate_body);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can can insert a diagnostic trap object -- we have that elsewhere in the codebase.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

indeed, thanks for pointing that out, update forth-coming!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done — the manual snapshot is now a DiagnosticErrorTrap-based RAII (StickyDiagnosticResetRAII): the trap detects errors introduced during the instantiation, and on scope exit the guard restores a clean state with the same Diags.Reset(/*soft=*/true) + client clear() cleanup IncrementalParser performs on a real parse failure. I left InstantiateFunctionDefinition's inline copy of this cleanup untouched — it could migrate to the same guard, but #849 is already touching that spot and I didn't want the two PRs to collide.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you rebase on top of the new master?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Rebased on top of main.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

#849 is in now, so this uses compat::DiagnosticsEngineRAII instead of a new class. Rebased on current main.

@codecov

codecov Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.71%. Comparing base (3ab124a) to head (12a664f).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1070      +/-   ##
==========================================
- Coverage   88.86%   88.71%   -0.15%     
==========================================
  Files          24       24              
  Lines        7014     6674     -340     
==========================================
- Hits         6233     5921     -312     
+ Misses        781      753      -28     
Files with missing lines Coverage Δ
lib/CppInterOp/CppInterOp.cpp 91.39% <100.00%> (+0.16%) ⬆️

... and 19 files with indirect coverage changes

Files with missing lines Coverage Δ
lib/CppInterOp/CppInterOp.cpp 91.39% <100.00%> (+0.16%) ⬆️

... and 19 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@conrade-ctc
conrade-ctc force-pushed the reset-diags-after-rejected-instantiation branch 2 times, most recently from b6fee6d to 4fd6208 Compare July 16, 2026 17:51
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@conrade-ctc
conrade-ctc force-pushed the reset-diags-after-rejected-instantiation branch 2 times, most recently from 52dfd1f to cf958fd Compare August 27, 2026 14:24
conrade-ctc pushed a commit to chicagotrading/CppInterOp that referenced this pull request Aug 27, 2026
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.

Content matches compiler-research#1070.

Co-developed-with-the-help-of: Claude Code (Fable 5, human in the loop)
@conrade-ctc
conrade-ctc force-pushed the reset-diags-after-rejected-instantiation branch 2 times, most recently from ef658ae to 6b74070 Compare September 15, 2026 16:15
conrade-ctc pushed a commit to chicagotrading/CppInterOp that referenced this pull request Oct 2, 2026
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.

Content matches compiler-research#1070.

Co-developed-with-the-help-of: Claude Code (Fable 5, human in the loop)
conrade-ctc pushed a commit to chicagotrading/CppInterOp that referenced this pull request Oct 22, 2026
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.

Content matches compiler-research#1070.

Co-developed-with-the-help-of: Claude Code (Fable 5, human in the loop)
@conrade-ctc
conrade-ctc force-pushed the reset-diags-after-rejected-instantiation branch from 6b74070 to 79b411a Compare November 6, 2026 17:28
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

conrade-ctc pushed a commit to chicagotrading/CppInterOp that referenced this pull request Nov 17, 2026
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.

Content matches compiler-research#1070.

Co-developed-with-the-help-of: Claude Code (Fable 5, human in the loop)
A rejected instantiation emits its error outside any input. The error
flag stays set, and the next input fails. Use
compat::DiagnosticsEngineRAII to soft-reset the engine, as
IncrementalParser does after a failed parse.

Co-developed-with-the-help-of: Claude Code (Fable 5, human in the loop)
@conrade-ctc
conrade-ctc force-pushed the reset-diags-after-rejected-instantiation branch from 79b411a to 12a664f Compare November 30, 2026 22:08
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants