fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
773 stars 194 forks source link

"Incomplete declaration of a static construct" which the F# compiler accepts #3033

Open Smaug123 opened 9 months ago

Smaug123 commented 9 months ago

Issue created from fantomas-online

Code

type Foo =
    { Mem : unit -> unit }

    static Empty: Foo =
        { Mem = (fun x -> failwith "Unimplemented function") }

Error

Fantomas.Core.ParseException: ParseException
  [{ Severity = Error
     SubCategory = "parse"
     Range = Some (4,4--4,10)
     ErrorNumber = Some 3862
     Message =
      "Incomplete declaration of a static construct. Use 'static let','static do','static member' or 'static val' for declaration." }]
   at Fantomas.Core.CodeFormatterImpl.parse@24.Invoke(Unit unitVar) in /_//src/Fantomas.Core/CodeFormatterImpl.fs:line 29
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2) in D:\a\_work\1\s\src\FSharp.Core\async.fs:line 508
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in D:\a\_work\1\s\src\FSharp.Core\async.fs:line 112

Problem description

The F# compiler accepts this, at least as of net8:

> dotnet fsi

Microsoft (R) F# Interactive version 12.8.0.0 for F# 8.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> type Foo = { Bar : unit -> unit } with static Empty : Foo = { Bar = fun () -> () }
- ;;
type Foo =
  { Bar: (unit -> unit) }
  static member Empty: Foo

Interestingly try.fsharp.org exhibits a similar failure, though.

(There's an obvious workaround, namely to add the keyword member.)

Extra information

Options

Fantomas main branch at 2023-12-29T14:09:11Z - a999b77ca5a024fbc3409955faac797e29b39d27

Default Fantomas configuration

Did you know that you can ignore files when formatting by using a .fantomasignore file? PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

dawedawe commented 9 months ago

I think this is related to https://github.com/dotnet/fsharp/pull/16345 So the net8 F# compiler is behind our parser code and still accepts this but won't anymore with the next bugfix release.

nojaf commented 9 months ago

Yes, this fails in my fsi of my local dotnet/fsharp main branch. This doesn't feel quite right if this code breaks in 8.0.200.