You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect line split when using a custom operator #2107
Yes, that should work as a workaround in this case but the problem does not seem to be related to if..then..else as it also occurs in this somewhat contrived example:
let inline(%%)x y =((x % y)+ y)% y
leta=5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5
which gets formatted to
let inline(%%)x y =((x % y)+ y)% y
leta=5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5%%5
Other operators don't seem to have this problem, I wonder why.
I see the same issue with the modulo operator. The following is not accepted by the F# compiler:
letb=5%5
But replacing the operator with +, it is valid. So it seems there are certain operators which may be put onto a separate line and others may not.
I investigated this issue and confirmed it is reproducible in the current main branch when the infix expression is long enough to require line-breaking.
Root cause:CodePrinter.fs maintains a noBreakInfixOps set of operators that must stay on the same line as their LHS (because they can be parsed as unary prefix operators when they appear at the start of a new line). The set contains %, =, <, > — but was missing %%.
When the expression became too long, Fantomas used genMultilineInfixExpr which always places the operator at the start of the next line:
leta=(if aVeryLongVariableNameThatForceLineBreaking =0then1else-1)%%4// ← invalid: F# parser reads %% at start of line as unary splice operator
The F# compiler then rejects this with error FS0001: This expression was expected to have type 'Quotations.Expr' but here has type 'int' — because %% at the start of a line is the quotation untyped splice operator (~%%).
Fix: Adding "%%" to noBreakInfixOps keeps the operator on the same line as the LHS (matching the existing behaviour for %). I've submitted a draft PR with this fix and a regression test.
As a workaround in the meantime, you can set fsharp_max_infix_operator_expression to a higher value to prevent the expression from being broken across lines.
Activity
nojaf commentedon Feb 20, 2022
Hello @chausner, thank you for reporting this problem.
This one is interesting on a couple of levels:
Other operators don't seem to have this problem, I wonder why.
I'm not sure how to tackle this one, to be honest.
You could increase fsharp_max_if_then_else_short_width to work around the problem.
Sample
chausner commentedon Feb 20, 2022
Yes, that should work as a workaround in this case but the problem does not seem to be related to if..then..else as it also occurs in this somewhat contrived example:
which gets formatted to
I see the same issue with the modulo operator. The following is not accepted by the F# compiler:
But replacing the operator with
+, it is valid. So it seems there are certain operators which may be put onto a separate line and others may not.chausner commentedon Feb 20, 2022
I investigated this a bit further and found that from the following operators:
+-*/%%%**<<=>>=<>=&&||&&&<<<>>>^^^|||~~~only these operators are not accepted on a new line:
%%%<>=For
%and%%, the compiler (incorrectly?) recognizes the operators as(~%)and(~%%), defined in https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-extratopleveloperators.html.For
<,>and=, the compiler fails with a more generic FS0010 error.I wonder if this behavior is by design or a compiler bug. I created dotnet/fsharp#12754 to clarify.
nojaf commentedon Feb 20, 2022
Thank you for following up on this.
Add "%%" to noBreakInfixOps to fix soundness bug with custom %% operator
github-actions commentedon Mar 15, 2026
🤖 This is an automated response from Repo Assist.
I investigated this issue and confirmed it is reproducible in the current
mainbranch when the infix expression is long enough to require line-breaking.Root cause:
CodePrinter.fsmaintains anoBreakInfixOpsset of operators that must stay on the same line as their LHS (because they can be parsed as unary prefix operators when they appear at the start of a new line). The set contains%,=,<,>— but was missing%%.When the expression became too long, Fantomas used
genMultilineInfixExprwhich always places the operator at the start of the next line:The F# compiler then rejects this with
error FS0001: This expression was expected to have type 'Quotations.Expr' but here has type 'int'— because%%at the start of a line is the quotation untyped splice operator(~%%).Fix: Adding
"%%"tonoBreakInfixOpskeeps the operator on the same line as the LHS (matching the existing behaviour for%). I've submitted a draft PR with this fix and a regression test.As a workaround in the meantime, you can set
fsharp_max_infix_operator_expressionto a higher value to prevent the expression from being broken across lines.Add changelog entry for %% infix operator fix (#2107)
Merge pull request #3291 from fsprojects/repo-assist/changelog-issue-…
Bump the dotnet-sidecars group with 6 updates (#257)
Bump the packages group with 5 updates (#106)
Bump the packages group with 6 updates (#84)