Skip to content

[LSP] Separate diagnostic reporting from listener abort policy (C1) - #680

Closed
lagergren wants to merge 1 commit into
masterfrom
errs/c1-listener-contract-20261008
Closed

lagergren wants to merge 1 commit into
masterfrom
errs/c1-listener-contract-20261008

Conversation

@lagergren

@lagergren lagergren commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Why?

  • An embedding host needs to receive diagnostics while preserving the compiler's severity, error-budget, and abort decisions.
  • A bare listener callback receives reports but inherits state queries that say no errors occurred, while its default branch stops after one error.
  • The compiler-backed LSP needs a tested listener contract before listener propagation and speculative reporting can be migrated separately.

How?

  • Separate reporting from the decision to stop: report through log, then query isAbortDesired() at the existing control-flow sites.
  • Provide a collecting listener that remembers severity and codes, and forwarding helpers that preserve the listeners' state and abort requests.
  • Introduce explicit diagnostic locations and named reasons for silence, with regressions for branching, budgets, forwarding, and parser/lexer termination.

The lexer change intentionally preserves its existing behavior: after reporting, it still stops when the listener requests an abort. The extra query is required by the proposed void log contract. That contract gives host callbacks one reporting role and keeps the continuation decision in isAbortDesired(), instead of requiring both log and the query to answer the same policy question.

Concrete uses:

  • collecting forwards diagnostics while retaining the severity/code state the compiler queries; a bare callback alone does not maintain that state.
  • tee reports to both sinks and honors either sink's abort request; branches and derived silence preserve the parent's request.
  • The full working adapter independently cancels stale completion work, including work that produced no errors. Its cursor analysis and member lookup compose diagnostic collection with an independently queried cancellation policy. These cancellation APIs and adapter callers are in later slices, not C1.

This is a deliberate API design choice, not a claim that an LSP cannot work with a correctly implemented boolean-returning listener. C1 chooses one explicit stop-policy query and accepts the migration cost below.

Implications

  • Breaking Java API change, planned for 0.5.0: ErrorListener.log and the affected XvmStructure, AstNode, and Token helpers return void. Java clients and custom listener implementations must migrate and recompile; an overload differing only in return type cannot preserve compatibility.
  • RUNTIME reporting records an abort request for ERROR/FATAL instead of throwing. Callers that relied on the exception must query the listener and handle failure explicitly.
  • Default custom-listener branches no longer impose an implicit one-error limit; ErrorList branches preserve their configured budget and observe parent abort requests.
  • Callbacks must remain serialized. These helpers do not make compiler execution concurrent, and this PR does not change the release version.

Changes

  • javatools/src/main/java/org/xvm/asm/ErrorListener.java: add Site.In, Site.At, and Site.None, severity/varargs helpers, collecting, tee, and named silence; separate reporting from abort policy.
  • javatools/src/main/java/org/xvm/asm/ErrorList.java: add named budget policies and a default constructor; preserve branch budgets, parent aborts, and source re-anchoring.
  • javatools/src/main/java/org/xvm/compiler/ast/AstNode.java: adapt the forwarding API to report without returning an abort decision.
  • javatools/src/main/java/org/xvm/compiler/Parser.java: explicitly check the abort policy after reporting; apply the corresponding migration in the lexer and remaining affected callers.
  • javatools/README.md: document the breaking API, callback-versus-collector distinction, budgets, runtime behavior, and migration examples.
  • Five new listener test classes cover collecting versus deduplication, source sites, parent/branch state, named silence, and stop conditions; the launcher regression uses the new reporting signature.

This is C1, rebased onto master at 1d9352b6f, including I1 (#677), I3 (#679), and the subsequent upstream cleanup. It changes 18 files (+1102 / -71); 547 added lines are the five new test classes. The only conflict was master's removal of an old TODO in XvmStructure.log; the resolution retains that removal and C1's void reporting signature. Upstream diagnostic/source changes and test removals are preserved. It has no dependency on I2.

Validation on rebased commit 10f03d17d: XDK distribution build passed; the Java suite executed 410 tests with zero failures/errors and 42 existing skips; all 25 new listener tests executed. The five-test decrease from the preceding run comes from the two I1 regression classes removed on master. The compiler-consumer test passed with zero skips. Root and LSP Spotless checks passed, and the worktree remained clean. Counts were read from JUnit XML after a forced test run.

Validation commands
./gradlew :xdk:installDist --console=plain --quiet
./gradlew :javatools:test :lang:lsp-server:test \
  --tests org.xvm.lsp.adapter.CompilerConsumerTest \
  spotlessCheck :lang:lsp-server:spotlessCheck \
  --rerun-tasks --no-build-cache \
  -PincludeBuildLang=true -PincludeBuildAttachLang=true \
  --console=plain --quiet

The --tests filter applies to the LSP test task; the Java test task ran its complete suite.

Known follow-ups

  • C2: migrate the broader compiler call graph to explicit non-null listeners and explicit reasons for suppression.
  • C3: scope speculative parser diagnostics and restore temporary listeners/contexts on both successful and exceptional exits.
  • Later embedding/LSP work: file/pool listener ownership and richer compilation results remain separate slices; this PR does not deliver the full compiler adapter.

@ggleyzer ggleyzer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see

@@ -2504,7 +2504,8 @@ protected void log(Severity severity, String sCode, Object[] aoParam, long lPosS
* Log an error.
*/
protected void log(Severity severity, String sCode, Object[] aoParam, long lPosStart, long lPosEnd) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unless I miss something, I don't see any value in this change - only a more verbose way to do the same thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right about this individual lexer edit: with the existing ErrorList, it deliberately produces the same result and takes one more line. It is not a lexer bug fix or a claim that verbosity itself improves correctness.

The purpose is to give reporting and continuation policy separate roles throughout the listener API:

errors.log(diagnostic);           // deliver the report
if (errors.isAbortDesired()) {    // consult the current policy
    throw new CompilerException(...);
}

The extra check here is necessary under the proposed void-reporting contract to retain the lexer's existing stop behavior. The benefit is in how the host and wrappers use that contract:

  1. Collecting without losing compiler state. An IDE host supplies a diagnostic consumer. collecting records severity and codes while forwarding reports; compiler queries then see the actual error state. A bare callback still is not a stateful listener. ErrorListenerBranchTest demonstrates that distinction.
  2. Forwarding without losing somebody else's stop request. tee delivers each report to both sinks, then its policy query honors either sink's abort request. Branches and derived silence likewise retain the parent's request. A recorder need not invent a continuation decision just to accept a diagnostic. ErrorListenerSiteTest.testATeeReportsToBothAndKeepsEithersAbort checks both delivery and the preserved budget.
  3. Stopping obsolete IDE work without reporting a fake error. In the full working adapter, a new edit can make a completion request stale even when no diagnostic was emitted. runCursorAnalysis wraps an ErrorList with an independent staleness predicate. receiverMembers uses cancellable(collecting(errors::log), errors::isAbortDesired): it tracks the local lookup's diagnostics while retaining the enclosing operation's stop policy. Existing compiler checkpoints can query that policy independently of reporting. The cancellation wrapper and these adapter callers belong to later slices, not C1.

An implementation could retain boolean-returning log and consistently return isAbortDesired() everywhere. So I am not claiming that the LSP protocol makes a void return unavoidable. C1 deliberately removes that duplicate way of answering the stop question: reporting callbacks deliver diagnostics, and one explicit query supplies the policy. The tradeoff is a mechanical extra query at the few sites that previously consumed log's result, plus a real source/binary API break. That break is documented for 0.5.0.

The C1 parser/lexer migration tests verify that the same error budget still stops parsing/lexing. All 25 new listener cases, 415 executed Java tests, and the compiler-consumer test passed on the submitted C1 head; the suite also recorded 42 existing skips. I have added the usage explanation to the PR description so the design is visible without following this thread.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, but my concern is specifically the change to void log(). As you note, this lexer edit preserves the same behavior, and the listener features could work with a boolean return. What concrete benefit justifies the API break and extra call-site code? If there isn’t one, I’d keep the existing contract.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is a concrete API-safety benefit, although you are right that LSP does not mandate this return type.

I tested this exact callback against the old and new implementations:

var diagnostics = new ArrayList<ErrorInfo>();
ErrorListener listener = diagnostics::add;

With the existing contract, a successful List.add() returns true, which the lexer interprets as an abort request. Given two recoverable bad escapes, it stops after the first—even though listener.isAbortDesired() returns false. With the void contract, the collection result is discarded and both diagnostics are collected.

That illustrates the design problem: reporting and isAbortDesired() can give conflicting continuation decisions. The proposed contract gives each operation one responsibility: reporting delivers the diagnostic; the policy query decides whether compilation should continue. It also allows cancellation to be queried when valid source produces no diagnostics.

A related problem is hiding exception-based control flow inside the reporting implementation. The existing runtime fallback throws IllegalStateException for an error report, so merely choosing a different diagnostic destination can change where compilation unwinds and bypass the caller's intended recovery. That is a poor fit for repeated and speculative IDE compilation: a normal source diagnostic needs to be collected, while the compiler decides at its checkpoints whether to continue, abandon a trial, or stop the request. The proposed separation makes that policy explicit instead of making it an opaque side effect of logging. Changing the return type alone does not prevent a callback from throwing, and unexpected failures or exceptions from a host callback still need to propagate; this is about removing implicit severity-based throwing from ordinary diagnostic reporting, not eliminating exceptions from the compiler.

This does not make a bare callback stateful. The collecting wrapper remains necessary when the compiler needs accumulated severity and error-code queries.

We could preserve the boolean API and enforce consistent behavior through wrappers and implementation rules. Therefore, the return-type change is a deliberate API-safety tradeoff for the planned 0.5.0 boundary, not an unavoidable LSP requirement. My preference is to remove the competing control channel, but I would not let that choice block the underlying requirements: reliable diagnostic state, branch behavior, and propagation of the owner's abort request.

@lagergren
lagergren requested a review from ggleyzer October 9, 2026 10:04
Introduce source sites, collecting listeners, named budgets and silence reasons while preserving branch and merge behavior. Keep null and ambient ownership policy for later slices; document the planned 0.5.0 source and binary break.
@lagergren

Copy link
Copy Markdown
Contributor Author

Superseded by #685, which brings the listener contract together with explicit ownership, scoped reporting and the compilation-result, source-snapshot, cancellation and cached-diagnostic APIs that consume it. The implementation and regression coverage are retained in the combined review. Closing this PR to keep discussion and approval on one complete host-facing change; the existing branch and review history remain available. #683 and #684 remain separate prerequisites.

@lagergren lagergren closed this Oct 9, 2026
@lagergren
lagergren deleted the errs/c1-listener-contract-20261008 branch November 25, 2026 10:16
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