Skip to content

Record with lambda in non-last field collapses to single line producing invalid code #3246

Description

@randrag

Fantomas collapses a multi-line record expression to a single line when it fits within the line length limit. When a non-last field has a fun lambda value, this produces invalid F# code because the fun keyword's body extends to the end of the expression, swallowing subsequent fields.

Issue created from fantomas-online

Code

type Rec = {
    A : int
    B : int -> int
    C : int
}

let test () : Rec =
    {
        A = 1
        B = fun x -> x + 1
        C = 3
    }

Result

type Rec = { A: int; B: int -> int; C: int }

let test () : Rec =
    { A = 1
      B =
        fun x ->
            x + 1
            C = 3 }

Problem description

F# parses fun x -> as extending to the end of the enclosing expression. The semicolons become sequential expressions inside the lambda body:

{ A = 1; B = fun x -> (x + 1; C = 3) }

Suggested fix (I have a PR ready)

Keep the single-line format but wrap the lambda in parentheses to delimit the body:

let test () : Rec = { A = 1; B = (fun x -> x + 1); C = 3 }

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this. I have a (Claude code) PR ready, have reviewed it, will submit as draft.
  • I would like a release if this problem is solved.

Reproduced on version 7.0.3 and 8.0.0-alpha-003.

Options

Fantomas main branch at 1/1/1990

Default Fantomas configuration

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions