-
Notifications
You must be signed in to change notification settings - Fork 3
JavaToLaurelCompiler: fall back to a 1:1 position for unknown line maps #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tautschnig
merged 2 commits into
strata-org:main
from
tautschnig:pr/linemap-unknown-fallback
Jul 1, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
verifier/src/test/java/org/strata/jverify/verifier/UnknownLineMapFallbackTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package org.strata.jverify.verifier; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.strata.jverify.testengine.JVerifyTestEngine; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Locks the JavaToLaurelCompiler behaviour when Strata reports a diagnostic | ||
| * against the synthetic {@code <unknown>} URI (a node with no source in our | ||
| * line map): it must (1) surface as a user-visible Verifier error rather than | ||
| * crashing with "Could not find line map", and (2) be flagged LOUDLY as a | ||
| * synthesis bug rather than silently degrading to a misleading {@code 1:1}. | ||
| * | ||
| * <p>The trigger is a parameter named {@code result}: JVerify renames the | ||
| * postcondition lambda's parameter to Strata's canonical {@code result}, so it | ||
| * collides with Strata's implicit {@code result} <em>return</em> binding and | ||
| * Strata reports "Duplicate definition 'result'". That diagnostic lands on the | ||
| * return binding — a node JVerify does not construct, so it has no source even | ||
| * though JVerify now threads source ranges into everything it <em>does</em> | ||
| * synthesize (parameters, return types, requires/ensures/invariant clauses). | ||
| * Hence this case exercises the residual, now-loud fallback. | ||
| * | ||
| * <p>Note: the underlying collision is removed by #431 (which renames the user | ||
| * parameter), so this test relies on pre-#431 behaviour to reach the | ||
| * synthetic-URI path. | ||
| */ | ||
| public class UnknownLineMapFallbackTest { | ||
|
|
||
| @Test | ||
| public void syntheticUnknownUriSurfacesDiagnosticLoudlyInsteadOfCrashing() throws Exception { | ||
| String source = """ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this test will break when we solve the Laurel/Strata bug, right?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It actually should, yes. |
||
| import static org.strata.jverify.JVerify.*; | ||
| class ResultParam { | ||
| static int identity(int result) { | ||
| postcondition((int r) -> r == result); | ||
| return result; | ||
| } | ||
| } | ||
| """; | ||
| var sourceFile = new SourceFile(Path.of("ResultParam.java"), source); | ||
| var options = JVerifyTestEngine.getVerifierOptions( | ||
| JVerifyTestEngine.makeJVerifyTestAnnotation(0, 0), null); | ||
|
|
||
| // Capture stderr to assert the fallback flags the synthesis bug loudly. | ||
| var captured = new ByteArrayOutputStream(); | ||
| var savedErr = System.err; | ||
| JVerifyResults results; | ||
| try { | ||
| System.setErr(new PrintStream(captured, true, StandardCharsets.UTF_8)); | ||
| // Without the fallback the synthetic <unknown> URI has no line map | ||
| // and this throws "Could not find line map"; with it, the Strata | ||
| // diagnostic threads through instead. | ||
| results = Driver.getDriver(options) | ||
| .verifyJavaFiles(new ArrayList<>(List.of(sourceFile))); | ||
| } finally { | ||
| System.setErr(savedErr); | ||
| } | ||
|
|
||
| assertNotEquals(0, results.exitCode(), | ||
| "the result-parameter collision should surface as a Verifier error"); | ||
| assertFalse(results.diagnostics().isEmpty(), | ||
| "a user-visible diagnostic should be reported, not masked by a crash"); | ||
| assertTrue(captured.toString(StandardCharsets.UTF_8).contains("source-less synthesized node"), | ||
| "the residual synthetic-URI fallback must be flagged loudly as a synthesis bug, " | ||
| + "not silently degraded to a 1:1 location"); | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
verifier/src/test/java/org/strata/jverify/verifier/laurel/LaurelDriverUriTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.strata.jverify.verifier.laurel; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Windows-safety regression (#439): a Strata diagnostic reported against the | ||
| * synthetic {@code <unknown>} source path must not be routed through | ||
| * {@code Paths.get}, which throws {@code InvalidPathException} on Windows | ||
| * ({@code '<'}/{@code '>'} are illegal filename characters). The directly | ||
| * constructed URI must still expose a {@code /<unknown>} path so the | ||
| * JavaToLaurelCompiler line-map fallback (a path-suffix match) triggers. | ||
| */ | ||
| class LaurelDriverUriTest { | ||
|
|
||
| @Test | ||
| void syntheticFileUriForUnknownPathIsConstructibleAndFallbackMatchable() { | ||
| URI uri = LaurelDriver.syntheticFileUri("<unknown>"); | ||
| // getPath() decodes the percent-encoded '<'/'>' back. | ||
| assertEquals("/<unknown>", uri.getPath()); | ||
| assertTrue(uri.getPath().endsWith("/<unknown>"), | ||
| "the line-map fallback matches on the /<unknown> path suffix"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For context, I think that reporting a diagnostic on a generated node indicates a compiler bug. Do you feel the same?
Secondly, I think that despite the above characterization of a bug, that for debugging purposes, it's better to give all synthesis nodes a source location. That source location should originate from the context that is generating the node. Getting a (1,1) diagnostic at any point is quite frustrating for debugging. What do you think?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on both counts.
(1) Yes — a user-facing diagnostic on a source-less generated node is a defect on our side. Here it was mostly that
JavaToLaurelCompilerbuilt parameters, return types, and the requires/ensures/invariant clauses through the no-source builder overloads (SourceRange.NONE) despite the originating Java tree having a real range.(2) Agreed — fixed in 204bd2c: it now threads
toSourceRange(<originating tree>)into everything it synthesizes (parameters, return types, requires/ensures/loop-invariant clauses; the invariant ranges pair with #1361). So a diagnostic on any node jverify constructs now points at the user's code.Honest caveat: this PR's
result-collision trigger isn't reported against a node jverify builds — Strata raises it against its own implicitresultreturn binding, which we never construct, so there's no source to thread. For that residual class I kept the fallback but made it loud (a[jverify] internal: … source-less synthesized node …warning naming it a synthesis bug) rather than a silent 1:1 — surfacing (1) instead of hiding it. (#431 removes this collision anyway -- I'll return to that PR next, trying to address it at the Laurel source as suggested in #431.)