Skip to content

[LSP] Scope parser diagnostics and restore validation state - #682

Closed
lagergren wants to merge 1 commit into
errs/c2-explicit-listeners-20261008from
errs/scoped-reporting-20261009
Closed

lagergren wants to merge 1 commit into
errs/c2-explicit-listeners-20261008from
errs/scoped-reporting-20261009

Conversation

@lagergren

@lagergren lagergren commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This is the first PR that uses the new diagnostics compatible error listeners. We will carefully introduce scoped diagnostics and the restoration of validation state, both which are absolutely necessary and hard requirements to start LSP-speculative compilation and recompilation.

Stacked on #681, following the reporting contract in #680. This PR now targets #681's protected branch, so Files changed shows only these 18 scoped-reporting files. One approving review, the All builds complete check and resolved conversations are required, including for administrators. Once approved, this PR can merge into #681's branch; the combined work then reaches master through #681 after #680. If #681 lands first, refresh this branch and retarget it to master. The incremental comparison remains available.

Why?

  • A compiler used repeatedly by an editor must release each request's diagnostic listener when its callbacks finish, including when a callback throws.
  • Loop and try/finally validation previously cleared temporary context and listener fields only along selected exit paths, leaving stale callback state after failures or early returns.
  • Speculative parsing must keep diagnostics when it keeps the corresponding tokens, and discard both when it abandons a guess.

The README's scoped-reporting section explains the lifetime rules, API migration and regression coverage. #680 establishes the reporting/abort contract; #681 supplies explicit destinations. This change controls how long temporary destinations remain active. An LSP wrapper cannot reliably repair private parser, resolver or AST state left behind inside a failed operation, so these guarantees belong in the compiler itself.

How?

  • Give temporary diagnostic destinations lexical scopes that restore their predecessor through try-with-resources.
  • Buffer speculative parser reports in listener branches; merge accepted attempts and rewind abandoned attempts while retaining the parent's abort policy.
  • Store validation context and listener together, restoring the previous value in finally on normal, early and exceptional exits.
  • Construct evaluation and source-node diagnostic buffers once so their identity and availability do not depend on a later compilation step.

Implications

  • Ordinary Ecstasy source and CLI usage need no new syntax, flags or configuration; the existing compiler algorithms and parser nesting limits remain in place.
  • Java API migration at the planned 0.5.0 boundary: Parser.SafeLookAhead becomes Parser.Attempt, normally opened with parser.attempt(); keepResults() becomes keep(). Java callers using the old nested class must migrate and recompile.
  • NameResolver.getErrorListener() returns null outside an active resolution. During callbacks it exposes the current caller's non-null destination; nested resolution restores the outer caller's destination.
  • These scopes manage lifetime, not concurrency: parser, resolver, AST and listener instances remain confined to their compiler operation. Close reporting scopes in reverse order.
  • Lexer reports still use the lexer's original listener. A caller wanting a silent module-name-only scan must supply an explicit discard listener when constructing the parser.

Changes

The incremental change is 18 files: 11 production files, 6 test files and the README, with 887 additions / 175 deletions. Of the additions, 481 lines are tests.

  • javatools/src/main/java/org/xvm/asm/Reporting.java — new scoped diagnostic destination with restoration on exit.
  • javatools/src/main/java/org/xvm/compiler/Parser.java — buffered attempts, diagnostic commit/discard, token rollback and scoped module-name scanning.
  • javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java — borrow the caller's listener only for the duration of resolve.
  • javatools/src/main/java/org/xvm/compiler/ast/ValidationScope.java and Statement.java — immutable context/listener pairing and guaranteed restoration of the common validation context.
  • ForEachStatement.java, ForStatement.java, WhileStatement.java and TryStatement.java in that AST package — restore state used by lazy label-variable and jump callbacks on every exit.
  • javatools/src/main/java/org/xvm/compiler/EvalCompiler.java and javatools/src/main/java/org/xvm/tool/ModuleInfo.java — final owned diagnostic buffers; successful forwarding drains the node's buffer.
  • Six test files cover nested scopes, rejected null active scopes, accepted/discarded diagnostics, token rollback, parent aborts, throwing host callbacks, resolver reuse, exceptional/early statement exits and buffer draining.

The kept-warning parser test is explicitly a forward-looking guard: the ordinary parser currently reports ERROR or higher. The old lookahead machinery discarded lower-severity reports even when it kept the parse, but this PR does not claim that ordinary parsing already produced such warnings. The exception/restoration tests exercise active scopes and check that the previous state is restored after the failure.

Known follow-ups

  • Explicit structure reporting and cached TypeInfo diagnostic replay remain separate work; this PR does not remove the file/pool ambient-listener APIs or claim complete delivery from cached semantic results.
  • Parser recovery and partial AST features remain separate work; the new attempt lifetime contract supplies a foundation for them without including their functionality here.

Manual verification

Stacked-target CI: Automatic PR runs currently target master or JIT. Verify Commit is running for this head, explicitly dispatched with all publishing inputs disabled. Further head updates while this PR targets the parent branch also require a validation dispatch; All builds complete remains a required merge check.

Verified on the published commit's code after restacking on #681:

  • Rebuilt and installed the XDK with build-cache reuse disabled; root and language-server formatting checks passed.
  • Forced the full Java suite: 488 cases, 446 passed, 42 existing skips, zero failures/errors, verified from JUnit XML. All 21 new regression cases passed without skips; the skips are not counted as successful tests.
  • Forced the real CompilerConsumerTest: 1 passed, zero skips/failures/errors, using compiled modules supplied by Gradle dependencies.
  • Compiled manualTests/src/main/x/loop.x and exceptions.x with the rebuilt XDK and executed TestLoops and TestTry; both exited successfully. Output exercised loop first/last/count variables, TRY.exception, expected caught exceptions, and inner/outer finally execution.
  • git diff --check passed, and a merge simulation against current master found no conflicts. A merge into the protected parent branch is subject to its review and CI requirements; landing the combined work on master still depends on [LSP] Separate diagnostic reporting from listener abort policy (C1) #680.

The build and automated checks can be reproduced from the repository root:

./gradlew :xdk:installDist spotlessCheck --no-build-cache
./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

These results support ordinary compiler behavior and the specific scope-restoration guarantees. They do not imply Java source/binary compatibility with the removed parser API or establish thread safety.

Buffer speculative parser diagnostics until an attempt is kept, scope parser and resolver destinations, and hold validation context and listener together. Restore reporting and validation state on early returns and exceptions; initialize evaluation and source-node buffers once.

Preserve current parser nesting limits and add regression coverage for nested attempts, host callback failures, resolver reuse, statement restoration and diagnostic buffer draining. Document the parser API migration and thread confinement.
@lagergren

Copy link
Copy Markdown
Contributor Author

Superseded by #685. The combined PR retains scoped parser/resolver reporting and validation-state restoration, includes the newer behavioral test rewrites, and preserves the lexical-rollback correction from #684 through the active Reporting/Parser.Attempt scope. It also includes the compilation-result and cancellation APIs that consume these guarantees. Please continue review on #685. This branch and the existing review discussion are preserved; #683 and #684 remain separate prerequisites.

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.

1 participant