Skip to content

Report the column of a syntax error where javac puts it - #72

Merged
abashev merged 1 commit into
mainfrom
diagnostic-column
Sep 25, 2026
Merged

abashev merged 1 commit into
mainfrom
diagnostic-column

Conversation

@abashev

@abashev abashev commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Brings over google/google-java-format#1167, a fix from google-java-format 1.24.

The bug

For

class Foo { void f() {
 g() } }

the formatter reported A.java:2:6: error: ';' expected, while javac's caret for the same file stands under column 5. FormatterDiagnostic.toString added one to the column on the way out, on the assumption that it held a 0-based value; but the columns come from javac's line map and diagnostics, which are 1-based already. The comment saying otherwise dates from the time the formatter ran on ecj.

From upstream, with the author kept

  • Fix column numbers in diagnostics google/google-java-format#1167 by Liam Miller-Cushon, commit authored by them: the column is printed as it is, and FormatterDiagnostic.column() is documented as 1-based, which is what it always returned, so the SPI's behaviour does not change. The eleven expectations in DiagnosticTest and MainTest move one column to the left, as upstream's did. Upstream also fixed the caret line of its "problem line as context" output, which this project does not print.

Checked

  • javac's own caret for the two inputs of DiagnosticTest stands at columns 5 and 4, the values the tests now expect.
  • ./gradlew :open-java-format:test on JDK 21: 1497 tests, all green.
  • Formatting is untouched: the JDK corpus check does not apply to an error message.

For "class Foo { void f() {\n g() } }" the formatter reported
"A.java:2:6: error: ';' expected", one column to the right of where
javac puts its caret. FormatterDiagnostic added one to the column on
the way out, on the assumption that it held a 0-based value; but the
columns come from javac's line map and diagnostics, which are 1-based
already. The comment saying otherwise dates from the time the formatter
ran on ecj.

This ports google/google-java-format#1167 by Liam Miller-Cushon: the
column is printed as it is, and column() is documented as 1-based,
which is what it always returned. The eleven expectations in
DiagnosticTest and MainTest move one column to the left; javac's caret
for the two inputs of DiagnosticTest stands at columns 5 and 4, as the
tests now say.
@abashev
abashev merged commit 037a7fd into main Sep 25, 2026
15 checks passed
@abashev
abashev deleted the diagnostic-column branch September 25, 2026 07:56
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