Skip to content

More misc parens fixes #18350

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

Merged
merged 8 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Fix nullness warning for overrides of generic code with nullable type instance ([Issue #17988](https://github.com/dotnet/fsharp/issues/17988), [PR #18337](https://github.com/dotnet/fsharp/pull/18337))
* Unsafe downcast from `obj` to generic `T` no longer requires `not null` constraint on `T`([Issue #18275](https://github.com/dotnet/fsharp/issues/18275), [PR #18343](https://github.com/dotnet/fsharp/pull/18343))
* Fix for missing parse diagnostics in TransparentCompiler.ParseAndCheckProject ([PR #18366](https://github.com/dotnet/fsharp/pull/18366))
* Miscellanous parentheses analyzer fixes. ([PR #18350](https://github.com/dotnet/fsharp/pull/18350))

### Added
* Added missing type constraints in FCS. ([PR #18241](https://github.com/dotnet/fsharp/pull/18241))
Expand Down
38 changes: 21 additions & 17 deletions src/Compiler/Service/SynExpr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ module SynExpr =
| PrefixApp prec -> ValueSome(prec, Non)
| InfixApp(prec, side) -> ValueSome(prec, side)
| SynExpr.App(argExpr = SynExpr.ComputationExpr _) -> ValueSome(UnaryPrefix, Left)
| SynExpr.App(argExpr = SynExpr.Paren(expr = SynExpr.App _ & Is inner)) -> ValueSome(Apply, Right)
| SynExpr.App(funcExpr = SynExpr.Paren(expr = SynExpr.App _)) -> ValueSome(Apply, Left)
| SynExpr.App(flag = ExprAtomicFlag.Atomic) -> ValueSome(Dot, Non)
| SynExpr.App _ -> ValueSome(Apply, Non)
Expand Down Expand Up @@ -396,6 +397,7 @@ module SynExpr =
| SynExpr.DotIndexedSet _
| SynExpr.DotNamedIndexedPropertySet _
| SynExpr.DotSet _ -> ValueSome Set
| SynExpr.TypeTest _ -> ValueSome TypeTest
| _ -> ValueNone

module Dangling =
Expand Down Expand Up @@ -540,7 +542,7 @@ module SynExpr =

if i >= 0 && i < offsidesCol then
let slice = line.AsSpan(i, min (offsidesCol - i) (line.Length - i))
let j = slice.IndexOfAnyExcept("*/%-+:^@><=!|0$.?".AsSpan())
let j = slice.IndexOfAnyExcept("*/%-+:^@><=!|$.?".AsSpan())

let lo = i + (if j >= 0 && slice[j] = ' ' then j else 0)

Expand Down Expand Up @@ -845,22 +847,6 @@ module SynExpr =
// precedence than regular function application.
| _, SyntaxNode.SynExpr(SynExpr.App _) :: SyntaxNode.SynExpr(PrefixApp High) :: _ -> true

// Parens are never required around suffixed or infixed numeric literals, e.g.,
//
// (1l).ToString()
// (1uy).ToString()
// (0b1).ToString()
// (1e10).ToString()
// (1.0).ToString()
| DotSafeNumericLiteral, _ -> false

// Parens are required around bare decimal ints or doubles ending
// in dots when being dotted into, e.g.,
//
// (1).ToString()
// (1.).ToString()
| SynExpr.Const(constant = SynConst.Int32 _ | SynConst.Double _), SyntaxNode.SynExpr(SynExpr.DotGet _) :: _ -> true

// Parens are required around join conditions:
//
// join … on (… = …)
Expand Down Expand Up @@ -907,6 +893,22 @@ module SynExpr =
->
true

// Parens are never required around suffixed or infixed numeric literals, e.g.,
//
// (1l).ToString()
// (1uy).ToString()
// (0b1).ToString()
// (1e10).ToString()
// (1.0).ToString()
| DotSafeNumericLiteral, _ -> false

// Parens are required around bare decimal ints or doubles ending
// in dots when being dotted into, e.g.,
//
// (1).ToString()
// (1.).ToString()
| SynExpr.Const(constant = SynConst.Int32 _ | SynConst.Double _), SyntaxNode.SynExpr(SynExpr.DotGet _) :: _ -> true

// The :: operator is parsed differently from other symbolic infix operators,
// so we need to give it special treatment.

Expand Down Expand Up @@ -952,6 +954,7 @@ module SynExpr =
let rec loop =
function
| [] -> false
| SynMatchClause(whenExpr = Some(SynExpr.Paren(expr = Dangling.ArrowSensitive _ & Is inner))) :: _ -> true
| SynMatchClause(trivia = trivia) :: clauses ->
trivia.BarRange |> Option.exists (problematic matchOrTryRange)
|| trivia.ArrowRange |> Option.exists (problematic matchOrTryRange)
Expand Down Expand Up @@ -1148,6 +1151,7 @@ module SynExpr =
| _, MulDivMod(Mod, _)
| _, AddSub(Sub, _) -> true
| Relational _, Relational _ -> true
| Apply, Apply -> true
| _ -> false

| c -> c > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ let _ =
",
"
match () with
| () when box x :? int
| () when (box x :? int)
-> ()
| _ -> ()
"
Expand Down Expand Up @@ -1830,6 +1830,8 @@ in x
f ((+) x y) z
"

"(Gen.map f << Gen.map g) (Gen.constant x)", "(Gen.map f << Gen.map g) (Gen.constant x)"

// TypeApp
"id (id<int>)", "id id<int>"

Expand Down Expand Up @@ -1890,6 +1892,7 @@ in x
"""(id "x").Length""", """(id "x").Length"""
"""(3L.ToString("x")).Length""", """(3L.ToString "x").Length"""
"~~TypedResults.Ok<string>(maybe.Value)", "~~TypedResults.Ok<string>(maybe.Value)"
"bg.lighten(0.2).hexa ()", "bg.lighten(0.2).hexa ()"

// DotLambda
"[{| A = x |}] |> List.map (_.A)", "[{| A = x |}] |> List.map _.A"
Expand Down