Places where gren-format falls short of ideal — a shape where the parser it is
built on accepts more than today’s compiler, or less, a comment-placement
choice forced by a token with no recorded source position, or a limit of the
formatter itself. See the main README for the overview, and
Gren Formatter Rules for what the formatter does when
nothing is going wrong.
when branch patternswhere blockexposing doesn’t sort with the first nameexposing list’s closing paren isn’t recorded
letgren-format is built on compiler-common, which is the parser the next
Gren compiler will use. Today’s released compiler is the Haskell one, and the
two do not accept exactly the same language: compiler-common is deliberately
more permissive about indentation. Where they differ, the formatter follows
compiler-common — so it can hand you a file the compiler you are using right
now refuses to build.
A bracketed pattern is where this shows up. Write one broken across rows with its continuation indented, and today’s compiler is happy:
f z =
let
{ next
, count
} =
z
in
next
gren-format moves the , and the } back to the {’s own column, which is
also the column the binding starts at:
f z =
let
{ next
, count
} =
z
in
next
That is the canonical form, and compiler-common parses it — so the formatter’s
own checks all pass: the AST is preserved and the output is a fixed point. The
Haskell compiler stops at the first continuation row, because it requires every
row inside a binding to be indented past the binding’s start column:
-- UNFINISHED RECORD PATTERN ---------------------------------------- src/M.gren
I was partway through parsing a record pattern, but I got stuck here:
6| { next
^
I was expecting to see a closing curly brace next. Try adding a } here?
Elm rejects it too, with the same message — this is a place where Gren is departing from what it inherited, not a bug in either parser.
The same applies to an array pattern, and in a when branch head as well as a
let binding:
g z =
when z is
[ a
, b
] ->
a
Parameter patterns are unaffected — a pattern in an argument position does not start its line, so its continuation still clears the declaration’s column.
If you are on the released compiler, the workaround is to keep such a
pattern on one line; { next, count } = is accepted by both. This limitation
disappears when the compiler-common-based compiler ships, at which point the
formatted output above compiles as written.
when branch patternsA when branch whose record (or array) pattern is too wide to fit on one line
can wrap in a way the Haskell-based Gren compiler rejects:
-- the formatter may produce:
{ aPopped = Just { first = m1, rest = ms1rest }
, bPopped = Just { first = m2, rest = ms2rest }
} ->
The Haskell-based compiler requires every continuation line of a pattern to be indented deeper than the pattern’s first character, and this layout doesn’t satisfy that. Compiling the formatted file may fail with “I was expecting to see a closing curly brace next.” Rejoin the pattern onto one line by hand until this is resolved.
As described in When the formatter can’t tell what you meant, a comment beside =, :, |, or an import’s as always snaps to one
canonical side. Two different intents produce the same output.
An if’s then is one of these tokens, and it is worth naming because the
comment does not merely snap sideways — it changes branch. A comment written
between the condition’s last token and then comes out inside the then
branch’s body:
-- you write: -- gren-format writes:
if a > 0 && b {- x if a > 0 && b then
y -} then {- x
1 y -}
1
else
0 else
0
then has no recorded position, so “the end of the condition” and “the start of
the body” are the same place. Nothing here distinguishes them.
type alias is another. The alias keyword has no recorded position, so a
comment between type and the name can’t be kept on either side of it. One
written on any row before the name’s row moves above the declaration, whether
you wrote it on the type row or on a row of its own. One on the name’s row
stays in the header before the name:
-- you write: -- gren-format writes:
type {- c -} alias S = {- c -}
Int type alias S =
Int
type
{- c -} {- c -}
alias S = type alias S =
Int Int
elm-format keeps the comment where you wrote it, type {- c -} alias S =.
The clearest case is a -- at a , or a |, because the two spellings that
collapse are the two you are most likely to have meant differently. These:
v = v =
{ rec -- c { rec | -- c
| alpha = 1 alpha = 1
} }
reach the formatter as the same three facts — where rec ends, where the comment
is, where alpha starts — because the | between them has no recorded position.
Both therefore format to the first one:
v =
{ rec -- c
| alpha = 1
}
The same is true of [ 1 -- c ⏎ , 2 ] and [ 1, -- c ⏎ 2 ], and of a union
variant’s |. A comment you wrote on a row of its own is a third, genuinely
distinguishable spelling and is left alone:
v =
{ rec
-- c
| alpha = 1
}
The rule is “a -- keeps the row you wrote it on”, and the row above a
line-leading separator belongs to the item (or the record update’s base) above
it. That is uniform across all three separators, which is why it was chosen — but
it costs elm-format parity on the record update, because elm-format has its own
parser, does not have to collapse anything, and renders each of the two spellings
differently. See divergence #22.
where blockAs described in Comments in an effect module’s header,
a comment’s placement near the where { … } block is determined by proximity
to the handler name. Changing the spacing can change where the comment ends up.
This stays as-is until the parser records positions for the missing tokens.
Concretely, the boundary is a two-column slack past the handler name’s end
(Formatter.Logical.Comments.commentInsideTrailingBracket), so a couple of
extra spaces moves a comment out of the block:
-- inside the block (one space after the name):
effect module MyModule where { command = MyCmd {- note -} } exposing (..)
-- outside it (a few more spaces — same comment, same braces):
effect module MyModule where { command = MyCmd {- note -} } exposing (..)
-- formats to:
effect module MyModule where { command = MyCmd } exposing (..) {- note -}
That is a guess, not a reading of the source, and the section below explains why
it has to be one. It is also the one limitation here that can change the
layout of the header rather than only a comment’s home: a multi-line
{- … -} kept inside the block forces the block open across rows, and the same
comment moved out leaves it on one row.
effect module MyModule where { subscription = MySub {- forces the block to wrap
second row -}
} exposing {- tail -}
( a
)
keeps the block open:
effect module MyModule where { subscription = MySub {- forces the block to wrap
second row -}
} exposing {- tail -}
( a
)
while widening that one gap by three spaces collapses it:
effect module MyModule where { subscription = MySub } exposing
{- forces the block to wrap
second row -} {- tail -}
( a
)
Both outputs are stable — each is its own fixed point and each preserves the
AST — so neither the idempotency checks nor the AST comparison objects. Only
tests/fuzz-whitespace.py --mode stretch can see it, and only if a corpus
fixture carries the shape; none does, because such a fixture would fail that
gate for as long as this limitation stands.
The sharpest form of this: a -- comment written inside the braces, on its
own line, does not stay there. It is moved out of the block, below the module
line:
-- you wrote:
effect module MyModule where { command = MyCmd
-- line note
} exposing (..)
-- formats to (the comment is no longer inside the block; it is a top-level
-- comment now, at column 1):
effect module MyModule where { command = MyCmd } exposing (..)
-- line note
The comment survives — nothing is deleted — but it no longer sits beside the handler name it was written next to.
Written past the two-column slack it does not go to column 1 either — it lands
on the header’s own tail, after exposing (..). A comment the author wrote
after exposing (..) then cannot stay on that row, because a -- takes the
rest of its line, so it detaches to column 1: the place the reparse gives it,
and therefore the only placement that is a fixed point.
-- you wrote:
effect module MyModule where { command = MyCmd
, subscription = MySub -- three
} exposing (..) {- four -}
-- formats to:
effect module MyModule where { command = MyCmd, subscription = MySub } exposing (..) -- three
{- four -}
Pinned by HeaderComments/EffectHeaderLineCommentPushesTrailer.
This one cannot be fixed here. The parser records a position for the handler
name and nothing else in the block: not the where, not the braces, not
exposing. So these two files:
effect module MyModule where { command = MyCmd
-- line note
} exposing (..)
effect module MyModule where { command = MyCmd } exposing (..)
-- line note
hand the formatter byte-identical information — same tree, same single
comment at row 2, column 30. There is no fact available to tell them apart, so
they format the same way. A -- comment inside the braces has to be on a line
of its own (it would otherwise comment out the rest of the header), which is
exactly the case that needs the missing } position to place.
A {- … -} is no better off — it only looks better off because the two-column
slack usually guesses right for it. Line these two up so the comment starts at
the same column in both, and they are byte-identical too:
effect module MyModule where { subscription = MySub {- c -} } exposing (..)
effect module MyModule where { subscription = MySub } {- c -} exposing (..)
--pre-ast on the two files produces the same bytes: the comment is at row 1,
column 49 in both, and the } that separates them is recorded nowhere. Whatever
the formatter answers, it answers for both — which is why the answer has to come
from the comment’s column, and why widening a gap changes it.
The fix used for the module’s own exposing ( … ) list does not transfer.
There, MakeLogical.moduleExposingClose retires the same slack guess by
synthesizing a closing position and marking it elastic
(lpnElasticBracketNode), which says “anything that reaches this container is
inside it”. That works because nothing follows the exposing list inside the
declaration. The where { … } block is not last — exposing ( … ) follows it —
and comments genuinely belong out there. Giving the block an elastic close was
tried and measured: it fixes the whitespace flip above and then swallows
comments that were written past the block, including a -- after
exposing (..), which forces the block open across rows:
-- with an elastic where-block close, this fixture
effect module M where { command = CloseCmd, subscription = CloseSub } exposing (..) -- note
-- became
effect module M where { command = CloseCmd
, subscription = CloseSub -- note
} exposing (..)
Three fixtures moved that way (EffectHeaderCloseRowComment,
EffectModuleOpenLineTrailer, EffectModuleFxWhereComment), so the approach is
recorded here as disproven rather than left as an idea to retry.
Fixing this means the parser recording positions for the block’s own tokens.
exposing doesn’t sort with the first nameAs described in Exposed names sort automatically,
a comment on its own line — attached to the first name in an
exposing ( ... ) list — is a special case: the opening ( has no position
in the AST, so the comment is placed as a header-level comment right after
exposing, not as a child of the first name. It renders in that same spot
every time, regardless of which name ends up first after sorting:
module ExposingListSort exposing
( -- describes zebra
zebra
, Kiwi
, apple
, Mango
)
formats to:
module ExposingListSort exposing
-- describes zebra
( Kiwi
, Mango
, apple
, zebra
)
A comment before any other name in the list (not the first) doesn’t have this issue — it travels with its name normally, as shown in Exposed names sort automatically.
exposing list’s closing paren isn’t recordedThe closing ) of a module header’s exposing ( ... ) list has no position in
the AST — the parser records where each exposed name is, but nothing about the
brackets around them. Mostly that costs nothing: when you wrote the list across
rows, the ) is on its own row below the last name, so a comment after that name
is recognised by its row and stays inside the list (see
When the formatter can’t tell what you meant).
One shape does pay for it.
An import’s list doesn’t pay at all — the parser records where an import ends,
) included, so a comment there stays on whichever side of the ) you wrote it,
however much space you left, and however many comments you stack up.
When you wrote the list flat, everything is on one row and the row tells you
nothing. A comment written inside the brackets and one written past them look
alike. The formatter stops trying to tell them apart: it reads any comment after
the list’s last name as belonging to the list, opens the list up, and pins the
comment above the ). Every way of writing it gives the same result —
module FlatClose exposing (apple, zebra) {- both names -}
module FlatClose exposing (apple, zebra) {- both names -}
module FlatClose exposing (apple, zebra {- both names -})
module FlatClose exposing (zebra, apple) {- both names -}
— all four become:
module FlatClose exposing
( apple
, zebra
{- both names -}
)
That is the point. Neither the spacing you left before the comment nor the order you typed the names in changes the output, and the same module written any of those four ways formats to the same bytes.
What you give up is hanging a comment off the last name of a flat list: it is read as the list’s, not that name’s. A comment on any earlier name is unaffected, because a name follows it and there is nothing to confuse it with:
module Mid exposing (apple {- just apple -}, zebra)
stays exactly as written. And if you do want a comment tied to the last name,
write the list vertically — there the ) has a row of its own, which is enough
to tell the two apart:
module Vert exposing
( apple
, zebra -- just zebra
)
keeps the comment on zebra, through the sort and across reformats.
A chain of comments is treated as one unit and pinned together, in order, each on its own line — including a link that spans rows:
module Chain exposing (zebra, apple) {- first link, and it
spans rows -} {- second link -}
becomes:
module Chain exposing
( apple
, zebra
{- first link, and it
spans rows -}
{- second link -}
)
This also settles what used to be an ambiguity about a vertical list: these two files are handed to the formatter as byte-identical ASTs and byte-identical comment positions, so nothing could ever distinguish them —
module Amb exposing module Amb exposing
( apple ( apple
, zebra , zebra
{- first -} ) {- first -}
{- second -} {- second -}
)
— and both now format to the same thing, with both comments inside the list
above the ). Previously the second comment was pushed out of the brackets and
became a free-floating comment above the declarations.
letThe in keyword of a let ... in is another token with no recorded position:
the parsed let remembers only its bindings and its result expression, never
where in sat. So a comment written in the gap between the last binding and the
result can’t be pinned to one side of in — it might be trailing the binding
above, or introducing the result below, and there’s no fact to tell those apart.
gren-format always treats it as introducing the result, placing it on its own
line just below in:
-- you wrote:
x =
let
y =
1 -- a note
in
y
-- formats to (the note moves below in):
x =
let
y =
1
in
-- a note
y
This is the one placement that stays put every time you reformat and never
misplaces a comment you really did write below in. elm-format, whose parser
records the in position, keeps a trailing-binding comment up with the
bindings instead — a divergence covered in
Comparison with elm-format (point 20), with the
full reasoning for why gren-format can’t follow suit.
A multi-line block comment’s body is re-indented from its own structure: the
least-indented content line is placed a few columns in from the {-, and every
other line keeps its position relative to that. So hand-aligned content — an
ASCII diagram, an aligned table — keeps its shape; the block as a whole is
anchored under the comment’s opener rather than pinned to the exact columns you
typed. Because the indentation is derived only from the body (never from the
whitespace around the comment), two inputs that differ only in that surrounding
whitespace format to the same output — it is whitespace-canonical.
This applies to every multi-line block comment, including the form where {-
sits alone on its first line. (An earlier version of gren-format treated that
opener-alone form as a “keep my exact columns” signal and left the body
verbatim; that was a divergence from elm-format, which re-indents the body the
same way described here, so it was removed.)
Rule C7 says a comment run keeps the rows you wrote it on, and divergence #30 says that holds in every context. It does not hold for a run written just inside an opening bracket, or between a pipeline step’s operator and its operand. There, the whole run is laid out all-or-nothing: if every member can share a line they are all joined onto one, and if any member cannot, every member gets a row of its own.
-- you write: -- gren-format writes:
a = a =
[ {- p -} [ {- p -} {- q -} 1 <- joined a run you split
{- q -} 1 ]
]
b = b =
[ {- p -} -- q [ {- p -} <- split a run you joined
1 -- q
] 1
]
Both happen to be what elm-format does, so this costs nothing in parity — but it is the rule stated elsewhere, applied where the information to apply it is missing.
It is not fixable in the renderer. Which rows the author used inside a
container’s comment run is not recorded anywhere the renderer can read it: rule
C1 makes one gap one attachment, so Comments.gren gives the whole run one role,
derived from each member’s shape (bracketKindRole). [ {- p -} -- q and
[ {- p -} ⏎ -- q therefore arrive with identical roles (RidesInline,
TrailsPrevious), and so do the two spellings of a two-block run. Any rule
written in glueLeadingCommentRun lays both authorings out the same way and so
breaks C7 on one of them; grouping by commentGluesToPrevious the way a binop
chain’s leadingRunRowBoxes does was measured to move the violation from one
spelling to the other rather than remove it.
Honouring C7 here means recording the authored row upstream, in the role — a
change to the comment model rather than to a layout function, and one that
contradicts the “one gap, one role” reading Comments.gren currently documents
at that slot.
This one isn’t a comment-placement or compiler quirk — it’s an implementation
limit in the formatter itself, found by tests/pathological-nesting.py
(geometric-growth-and-bisect stress testing of nesting depth).
A chain of nested lambdas,
x =
\a -> \a -> \a -> \a -> \a -> {- … repeated hundreds of times … -} a
or a chain of nested unary minus,
x =
-(-(-(-(-(-( {- … repeated hundreds of times … -} 1))))))
crashes with RangeError: Maximum call stack size exceeded once nesting
passes roughly 400 levels for lambdas or 300 levels for unary minus.
Rendering a nested expression recurses once per level through a fairly long
call chain (dispatch → flow assembly → per-item classification → the next
level’s dispatch, and so on) — around 10-15 JS stack frames per level of
Gren source nesting — and Node’s default stack budget runs out before the
parser’s own does (the parser tolerates roughly 500-700+ levels of the same
shapes, since its recursive-descent call chain is shorter per level). The
crash is immediate and clean — no hang, no corrupted output — but a file
past the threshold cannot be formatted at all.
This is not expected to matter in practice: no real Gren source this project has ever swept (published packages, its own sources, hundreds of thousands of generated random modules) has come anywhere near this depth, and code with hundreds of directly nested lambdas or unary minuses is not something anyone writes by hand. Fixing it properly means rewriting the renderer’s recursive core to use an explicit stack instead of the JS call stack (a trampoline) — a large, invasive change judged disproportionate to a depth nothing has ever hit. Nor would we consider changing Node’s own stack size; that is out of scope as a solution.
Deep nesting used to be worse than a depth ceiling: several constructs
rendered the same subtree twice per level, which is exponential rather than
linear, and made record literals unusable well before 25 levels. Those were
formatter bugs and are fixed — every construct now renders each subtree once,
and what is left for all of them is the ordinary stack-depth ceiling described
above. The pattern is worth knowing if you touch the renderer: a suspected
blowup here has always turned out to be a second render, not a slow function.
docs/testing.md
has the measurements and the shapes that were timed.