-
-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function lambda with type annotation breaks code #2295
Comments
Hello, thanks for reporting this. It is an edge case for sure, yet an interesting one on an AST level. The additional For example: let x : int = 1 leads to: SynBinding
(None, Normal, false, false, [],
PreXmlDoc ((5,0), FSharp.Compiler.Xml.XmlDocCollector),
SynValData
(None, SynValInfo ([], SynArgInfo ([], false, None)), None),
Named (SynIdent (x, None), false, None, tmp.fsx (5,4--5,5)),
Some
(SynBindingReturnInfo
(LongIdent (SynLongIdent ([int], [], [None])),
tmp.fsx (5,8--5,11), [])),
Typed
(Const (Int32 1, tmp.fsx (5,14--5,15)),
LongIdent (SynLongIdent ([int], [], [None])),
tmp.fsx (5,14--5,15)), tmp.fsx (5,4--5,5),
Yes tmp.fsx (5,0--5,15),
{ LetKeyword = Some tmp.fsx (5,0--5,3)
EqualsRange = Some tmp.fsx (5,12--5,13) }) I always assumed this was duplicate information from the SynBinding that made things easier in other places from the compiler. So, a bit to my surprise: let x : int = 1 : int gives SynBinding
(None, Normal, false, false, [],
PreXmlDoc ((5,0), FSharp.Compiler.Xml.XmlDocCollector),
SynValData
(None, SynValInfo ([], SynArgInfo ([], false, None)), None),
Named (SynIdent (x, None), false, None, tmp.fsx (5,4--5,5)),
Some
(SynBindingReturnInfo
(LongIdent (SynLongIdent ([int], [], [None])),
tmp.fsx (5,8--5,11), [])),
Typed
(Typed
(Const (Int32 1, tmp.fsx (5,14--5,15)),
LongIdent (SynLongIdent ([int], [], [None])),
tmp.fsx (5,14--5,21)),
LongIdent (SynLongIdent ([int], [], [None])),
tmp.fsx (5,14--5,21)), tmp.fsx (5,4--5,5),
Yes tmp.fsx (5,0--5,21),
{ LetKeyword = Some tmp.fsx (5,0--5,3)
EqualsRange = Some tmp.fsx (5,12--5,13) }) Anyway, I would suggest detecting that you have a fantomas/src/Fantomas.Core/CodePrinter.fs Lines 1210 to 1213 in 25634b2
I wouldn't change it everywhere you find usage of the I hope this helps, are you interested in submitting a PR?
Please open an issue for that here if your |
@nojaf This should ideally be fixed in FCS tree (so it contains the actual tree structure without changing it), but until that is done you can try using this trick to recognize the wrapper expressions: https://github.com/JetBrains/resharper-fsharp/blob/b6faed78754be2001edf81878e2ca53bbbd19191/ReSharper.FSharp/src/FSharp.Psi.Features/src/Parsing/FSharpTreeBuilderBase.fs#L874-L881 |
@auduchinok I'd be in favour of that as well but changing feels like a landmine 😸. |
Yes, it does 😅 I think that wrapping it like this could/should be done just before processing an expression in the type checker. That way it doesn't look that scary and would allow the tree to have the correct structure. 🙂 |
Right, that does seem more doable. A bit similar to the fix of dotnet/fsharp#13131, where the logic was essentially moved from the untyped tree to the typed tree. I might open an issue for this later on in the F# repo. |
Issue created from fantomas-online
Code
Result
Problem description
I encountered this in a slightly more complex scenario, but this was the minimal repro I could come up with. If there's no type annotation on the function definition
f
, Fantomas will reformat and put the type annotation on the function, i.e.:correctly becomes:
However, if there's already a type annotation on the definition, and the lambda has a type annotation, the result of putting the whole thing on the same line breaks, as can be seen on top of this post.
Probably a niche scenario as clearly the type annotation is redundant, but still, running Fantomas shouldn't break code. Solution could be to put the lambda in parentheses, or to drop the type annotation (after all it is redundant, it's already there), or to not format it to one line.
Extra information
warningserrors.Options
Fantomas master branch at 2022-06-09T08:42:49Z - 25634b2
Default Fantomas configuration
Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?
Btw: I do know this, but for some reason, in VS 2022 at least, using
.fantomasignore
does not have any effect.The text was updated successfully, but these errors were encountered: