Skip to content

Keep a single anonymous record type argument attached in Stroustrup - #3525

Open
mmabdpr wants to merge 1 commit into
fsprojects:v9.0from
mmabdpr:fix-3069
Open

mmabdpr wants to merge 1 commit into
fsprojects:v9.0from
mmabdpr:fix-3069

Conversation

@mmabdpr

@mmabdpr mmabdpr commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #3069
Fixes #2860

Replaces #3523, narrowed as requested there. With Stroustrup, a type application whose only type argument is an anonymous record (struct allowed) now keeps <{| and |}> attached:

|> convertTo<{|
    kind: string
    properties: {| statisticsEnabled: bool |}
|}>

The fix

A new helper, genApplicationWithTypeArgument, replaces the direct genPrefixApp calls in Expr.TypeApp and the Type.AppPrefix branch of genType.

It uses the attached layout only when all of these hold:

  • the style is Stroustrup
  • there is exactly one type argument, and it is an anonymous record
  • canSafelyUseStroustrup allows it
  • there is no trivia after <
  • identifier<[struct ]{| fits on the current line

It then prints the identifier, <, the record and > in sequence. The existing anonymous-record printer lays out the fields and |}. In every other case it calls the unchanged genPrefixApp.

Behaviour changes

  • Stroustrup, one anonymous-record argument: the record stays attached (<{| ... |}>) instead of being an indented generic-list item with a separate closing >. This restores the layout requested in Regression: Stroustrup bracket style in anonymous record type parameter is not respected #3069.
  • A line comment before >: the comment now follows the attached closing tokens (|}> // close). A line comment cannot sit between |} and an attached > without swallowing it. The comment is preserved on the record’s closing line.

Other argument shapes and styles retain their existing layout rules. One-line output, both fallbacks—a comment after < or an opening that does not fit—and the #2706 goldens are unchanged.

Tests

  • scenarios/TypeAppTests/3069-single-record-type-argument: the reported Regression: Stroustrup bracket style in anonymous record type parameter is not respected #3069 example, pinning the requested pipeline layout.
  • stroustrup/Expr/TypeApp/single-record: covers the expression-side call site in a binding, keeping the record attached rather than printing it as an indented generic-list item.
  • stroustrup/Type/AppPrefix/single-struct-record: covers the separate type-side call site (Box<struct {| ... |}>) and verifies that struct is supported.
  • stroustrup/Expr/TypeApp/trivia/comment-before-greater-than: verifies that the line comment is preserved and follows the attached >.

The two fallbacks were checked against the baseline but have no new snapshot cases because their output is unchanged.

Not covered

#3523 also changed the cases below. They are left out to keep this PR narrow:

  • several or mixed anonymous-record type arguments
  • a record inside parentheses or an array
  • outer attachment in nested generics, such as f<Box<{| ... |}>>; the inner Box<Record> can still change because it independently qualifies
  • moving a closing > solely to satisfy the line-width limit

An attached layout for several anonymous-record arguments needs its own discussion, as requested in the review of #3523. This PR leaves that decision alone.

When fsharp_multiline_bracket_style = stroustrup, keep a single
anonymous record type argument (struct allowed) attached to < and >:

    |> convertTo<{|
        kind: string
    |}>

Only attach when the head and opening fit on the current line and
trivia permits it; otherwise use the unchanged genPrefixApp.

Fixes fsprojects#3069
Fixes fsprojects#2860
@mmabdpr

mmabdpr commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Targeting v9.0 for now, assuming this formatting change is considered breaking and belongs in the next major release; please let me know if main would be more appropriate or feel free to modify the target.

@nojaf nojaf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for coming back with a much smaller PR. This is the scope I asked for, and the tests are exactly the kind I hoped for. I'm happy with the layout itself: <{| ... |}> under Stroustrup is a reasonable thing to restore.

What I'd like to change is how it's wired into CodePrinter. genApplicationWithTypeArgument is a new top-level function that sits in front of genPrefixApp and decides between two layouts. To do that it takes the type arguments twice, once as Type list and once already printed, so from its signature it's hard to tell what it prints. It also writes its own Stroustrup check (IsStroustrupStyle, a match on Type.AnonRecord, canSafelyUseStroustrup). Context.fs already expresses that rule as isStroustrupStyleType plus canSafelyUseStroustrup, and the other places that attach an anonymous record type use those (see autoIndentAndNlnTypeUnlessStroustrup).

Could you try putting the decision where the argument layout is already chosen? colGenericTypeParameters picks between the short and long forms. A single argument for which isStroustrupStyleType and canSafelyUseStroustrup hold could print there with genType directly, without the indent and trailing newline. genPrefixApp would then need to leave out the padding before > in that case. Ideally that removes the new helper altogether, and the Stroustrup rule for type arguments matches the one used everywhere else.

I haven't tried this myself, so if it turns out awkward (the > handling especially), tell me what you ran into before forcing it. I'd rather talk about it than have you work around it. Your tests can stay as they are: if the goldens still pass, the behaviour is the same.

Some smaller things I noticed in the current version. A few of them disappear with the rework, but I'd like to explain them anyway, because they're the kind of thing that makes a change in CodePrinter hard for me to accept:

  • The name. genApplicationWithTypeArgument doesn't tell me it prints Expr.TypeApp and Type.AppPrefix, or that it's about where the record goes. CodePrinter is big, and people mostly find their way around it by function names, so a name has to say what gets printed.
  • let standard. This binding exists only because two branches share the same fallback. When a fallback needs its own name so it can be reached from more than one place, that usually means the conditions belong in one place instead of spread over a when guard and an if. And "standard" doesn't say what the layout is.
  • The prefix is written twice. identifier +> genSingleTextNode lessThan appears once in opening (only to measure it) and again in the real output. Two copies of the same sequence will drift the first time someone changes one of them.
  • It skips the offside padding. genPrefixApp pads with addFixedSpaces (startColumn + 1) so that > stays right of the identifier, which is what keeps it from being read as the comparison operator. The attached layout skips that, and |}> can end up left of the identifier. Your goldens show that this compiles, but it's the least obvious part of the change and it has no comment. That's where a comment should go: why it's safe when > follows |}.
  • The doc comment spends five lines restating the guard conditions, which the code already shows. A comment here is most useful when it explains a reason the code can't show, like the offside point above.

None of this is a criticism of the effort. You did what I asked, and the reason I'm careful is that every special case in CodePrinter gets read by everyone who later touches that node. Thanks again for your patience with this one!

@nojaf
nojaf force-pushed the v9.0 branch 4 times, most recently from 029f9a5 to 9f7699a Compare October 16, 2026 07:38
@mmabdpr

mmabdpr commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

I tried putting the decision in colGenericTypeParameters, but as you predicted the > handling got awkward, so I'd like to check an alternative with you.

colGenericTypeParameters and the padding before > in genPrefixApp both depend on the same check. They also run at different moments: genPrefixApp decides before < is written, and colGenericTypeParameters decides after. They disagree when a comment follows <, and > ends up at the identifier's own column.

Keeping the two in step would need a shared helper and lessThan threaded into colGenericTypeParameters. That brings back the new top-level function you wanted gone.

How about making the decision once in genPrefixApp, which already knows both the arguments and the padding? colGenericTypeParameters would stay untouched. genPrefixApp would take the type parameters as a Type list instead of an already-printed Context -> Context, plus an innerPadding (the space written just inside < and >, which Type.AppPrefix previously wrapped around the arguments itself). The two call sites each pass the list and their padding:

Proposed genPrefixApp
let genPrefixApp
    (identifier: Context -> Context)
    (lessThan: SingleTextNode)
    (typeParameters: Type list)
    (innerPadding: Context -> Context)
    (greaterThan: SingleTextNode)
    (ctx: Context)
    : Context
    =
    let startColumn = ctx.Column

    let genTypeParameters, padGreaterThan =
        match typeParameters with
        | [ t ] when
            isStroustrupStyleType ctx.Config t
            && canSafelyUseStroustrup (Type.Node t) ctx
            && not lessThan.HasAnyContentAfter
            ->
            genType t, sepNone
        | _ -> innerPadding +> colGenericTypeParameters typeParameters +> innerPadding, addFixedSpaces (startColumn + 1)

    (identifier
     +> genSingleTextNode lessThan
     +> genTypeParameters
     +> padGreaterThan
     +> genSingleTextNode greaterThan)
        ctx

It uses the same isStroustrupStyleType and canSafelyUseStroustrup pair, adds no new top-level definition, and the goldens pass unchanged. What do you think?

@nojaf

nojaf commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Hi @mmabdpr, thanks for the proposal, I will circle back to this next week.
In a different head space right now.

nojaf added a commit to nojaf/fantomas that referenced this pull request Oct 21, 2026
With Stroustrup, a type application whose only type argument is an
anonymous record printed it as an indented list item, with the closing
`>` on a line of its own and a space before it. It now stays attached,
`convertTo<{|`, fields, `|}>`, for expressions and types alike, as it
was before 6.3.0.

genPrefixApp makes the choice, since it already holds the padding that
keeps a `>` starting a line right of the identifier, where it cannot be
read as the comparison operator. It takes the type arguments as a list
and uses the same rule as every other place that attaches an anonymous
record type: isStroustrupStyleType and canSafelyUseStroustrup, now in
Context.fsi, decided once `<` is written so a comment after it counts.
With trivia before `>`, the list layout stays, so the `>` always
follows `|}` and needs no padding.

This is the design @mmabdpr proposed in fsprojects#3525, after the first version
there was reviewed.

Closes fsprojects#3069
Closes fsprojects#2860

Co-authored-by: Mohammad Mahdi Abdollahpour <[email protected]>
@nojaf
nojaf force-pushed the v9.0 branch 3 times, most recently from 643ede4 to 19e77bd Compare October 23, 2026 10:58

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.

2 participants