Skip to content

fixes #26694; nimpretty keeps the indentation of lines after a comment inside an expression - #26881

Open
konaito wants to merge 1 commit into
nim-lang:develfrom
konaito:fix-26694-nimpretty-comment
Open

konaito wants to merge 1 commit into
nim-lang:develfrom
konaito:fix-26694-nimpretty-comment

Conversation

@konaito

@konaito konaito commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

fixes #26694

After a comment token, emitTok sets em.lastTok to tkComment. For the next line, emitTok checks whether lastTok is a comma, an open bracket or an operator to decide if the line continues an expression. Because the comment hides the comma (or and/or) before it, the next line is treated as a new statement: its column is pushed on indentStack and the line is written at indentStack.high * indWidth. The pushed column is popped again only at the next dedent, so the statement after the expression (the return in the issue) is written one level too shallow, and finalCheck fails with invalid indentation.

Fix: a comment no longer updates lastTok, so the token after it is laid out in the same context as the token before it.

Test: nimpretty/tests/tcommentinlist.nim, with the example from the issue and the same pattern after and in an if condition.

Verification, on macOS arm64:

  • Before the fix, nim r nimpretty/tester.nim fails on tcommentinlist (invalid indentation); after the fix all files are identical, including the pretty(pretty(x)) runs. The expected output compiles and prints the same as the input.
  • I ran nimpretty before and after the fix over compiler/*.nim, lib/pure/*.nim and lib/std/*.nim (307 files). 23 files failed before, 18 fail after; the 18 fail with the same error at the same position with both versions. The 5 files that now format (compiler/ccgexprs.nim, linter.nim, sem.nim, semexprs.nim, seminst.nim) failed because of this bug: a comment after and/or in an if condition. 6 more files differ, all in lines that follow a comment inside an expression: those lines now keep their original alignment instead of moving left (docgen.nim, extccomp.nim, renderer.nim, semtypinst.nim, parseutils.nim, semmagic.nim). In parseutils.nim the trailing comment # ldexp? is now one space after the code because the line keeps its column; in semmagic.nim the line keeps its column, is then longer than 80 characters and is split.
  • This touches emitTok, not optionalIsGood, so it does not overlap with fixes #26691; fixes #26692; nimpretty keeps the space after a binary operator when it joins lines #26878 or fixes #26693; nimpretty no longer writes an empty line when it splits a joined line #26880. The three branches merge without conflicts and the tester passes on the merged tree.

The error message naming nimpretty_bug.nim, mentioned at the end of the issue, is not changed here.

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.

nimpretty loses the indent of lines after a comment inside a bracket list, then fails with "invalid indentation"

1 participant