Skip to content

parser: parenthesize conversions and conditionals in Expr.ToString - #47

Merged
rameel merged 1 commit into
mainfrom
parser/parenthesize-conversions
Nov 19, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/parenthesize-conversions

Conversation

@rameel

@rameel rameel commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Expr.ToString printed a conversion without parentheses, so 1 + 2 :> long became (1 + 2 :> long). The tree is 1 + (2 :> long), but the text reads as (1 + 2) :> long: the same :> operator in F# binds more loosely than +. A conditional expression had no parentheses either. The parser keeps the parentheses around a conditional operand, but in a tree built by hand, + with the operands a ? b : c and 1 was printed as (a ? b : c + 1), which parses as a ? b : (c + 1).

This PR prints conversions and conditional expressions in parentheses, as Expr.Binary already does for itself: (1 + (2 :> long)) and ((a ? b : c) + 1). A conversion or conditional expression at the top level is now printed in parentheses too, such as (x :> int). In a tree built by hand, a member access on a conversion is now printed as (x :> object).ToString() instead of x :> object.ToString(), which does not parse.

Expr.ToString printed a conversion without parentheses, so
1 + 2 :> long became (1 + 2 :> long). The tree is 1 + (2 :> long),
but the text reads as (1 + 2) :> long, since the same :> operator in
F# binds more loosely than +. A conditional expression had no
parentheses either, so in a tree built by hand, a conditional operand
of + was printed as (a ? b : c + 1), which parses as a ? b : (c + 1).

Print both in parentheses, as Expr.Binary already does for itself:
(1 + (2 :> long)) and ((a ? b : c) + 1).
@rameel
rameel merged commit a1df43b into main Nov 19, 2026
1 check passed
@rameel
rameel deleted the parser/parenthesize-conversions branch November 19, 2026 21:04
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.

1 participant