Skip to content

Show the source line of a marked line whose expected diagnostic is missing - #6143

Draft
vlsi wants to merge 2 commits into
google:masterfrom
vlsi:show-missed-diagnostic-line
Draft

vlsi wants to merge 2 commits into
google:masterfrom
vlsi:show-missed-diagnostic-line

Conversation

@vlsi

@vlsi vlsi commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

When a line marked with // BUG: Diagnostic contains: or // BUG: Diagnostic matches: gets no matching diagnostic, CompilationTestHelper reports only the line number and the marker's text, for example Did not see an error on line 4 matching dereferenced expression 'x' is @Nullable. The All errors: list that follows prints each diagnostic with its source line, but no diagnostic exists for this line, so nothing in the report shows the code the marker is on. A regression test usually fails this way: the fix is reverted and the expected diagnostic disappears. Where several markers carry the same text, the reader of a CI log has to open the test source and count lines to find the failing case.

DiagnosticTestHelper now follows each such message with the source line it names, trimmed and indented by four spaces, the way javac prints a diagnostic's line:

Did not see an error on line 4 matching dereferenced expression 'x' is @Nullable.
    x.hashCode();

Caller sees:

  • Did not see an error on line N matching <text>. followed by line N of the source, trimmed, on a line of its own indented by four spaces.
  • Did not see an error on line N containing [<CheckName>]. followed by line N the same way.

Not changed: Saw unexpected error on line N. stays one line, since All errors: already prints the diagnostic on that line with its source.

The four CompilationTestHelperTest tests that compare the whole failure message now expect the source line; each fails on the helper from #6117 and passes with this change.

Depends on #6117, which reports every failing line; its commit is the first on this branch, so review only the second commit.

🤖 Generated with Claude Code

When several lines of a source given to `CompilationTestHelper` failed their expectations, the test failure named only the first of them. `DiagnosticTestHelper.assertHasDiagnosticOnAllMatchingLines` threw at the first mismatched line, so each further mismatch showed up only after the previous one was fixed and the test was run again. For a source with markers on lines 4 and 8 and no diagnostics, the failure was:

```text
Did not see an error on line 4 matching DeadException. There were no errors.
```

Fixes google#6116.

The helper now checks every line of every source and fails once, with one message for each line that fails, followed by the list of all diagnostics:

```text
Saw unexpected error on line 3.
Saw unexpected error on line 6.
All errors:
/Test.java:3: error: [ReturnTreeChecker] Method may return normally.
    return true;
    ^
    (see https://errorprone.info/bugpattern/ReturnTreeChecker)

/Test.java:6: error: [ReturnTreeChecker] Method may return normally.
    return false;
    ^
    (see https://errorprone.info/bugpattern/ReturnTreeChecker)
```

A line's message is the one the helper printed for it before, naming the first expectation the line fails: a marker that names a key missing from `expectErrorMessage`, a missing diagnostic, a diagnostic without the check name, or an unexpected diagnostic. Where the test compiles more than one source, each message starts with the source's name, because a line number no longer says which file it belongs to. `assertHasDiagnosticOnAllMatchingLines` keeps its signature and reports all mismatches in its source the same way.

`CompilationTestHelperTest` has five new tests, which fail on the current master: two marked lines without diagnostics, two unexpected diagnostics, an unexpected diagnostic before a missing one, an unknown key together with another mismatch, and mismatches in two sources. `fileWithSyntaxErrorFails` now also asserts the message for a diagnostic without the check name.

Assisted-by: Claude Code (claude-opus-5)
@google-cla

google-cla Bot commented Sep 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

…ssing

When a line marked with `// BUG: Diagnostic contains:` or `// BUG: Diagnostic matches:` gets no matching diagnostic, `CompilationTestHelper` reports only the line number and the marker's text, for example `Did not see an error on line 4 matching dereferenced expression 'x' is @nullable.` The `All errors:` list that follows prints each diagnostic with its source line, but no diagnostic exists for this line, so nothing in the report shows the code the marker is on. A regression test usually fails this way: the fix is reverted and the expected diagnostic disappears. Where several markers carry the same text, the reader of a CI log has to open the test source and count lines to find the failing case.

`DiagnosticTestHelper` now follows each such message with the source line it names, trimmed and indented by four spaces, the way javac prints a diagnostic's line:

```text
Did not see an error on line 4 matching dereferenced expression 'x' is @nullable.
    x.hashCode();
```

Caller sees:
- `Did not see an error on line N matching <text>.` followed by line N of the source, trimmed, on a line of its own indented by four spaces.
- `Did not see an error on line N containing [<CheckName>].` followed by line N the same way.

Not changed: `Saw unexpected error on line N.` stays one line, since `All errors:` already prints the diagnostic on that line with its source.

Depends on google#6117, which reports every failing line; its commit is the first on this branch.

Assisted-by: Claude Code (claude-opus-5-5)

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.

1 participant