Skip to content
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

Parens: only check for sensitive indentation when really needed #16973

Merged
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
2 changes: 1 addition & 1 deletion docs/release-notes/.FSharp.Compiler.Service/8.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Code generated files with > 64K methods and generated symbols crash when loaded. Use infered sequence points for debugging. ([Issue #16399](https://github.com/dotnet/fsharp/issues/16399), [#PR 16514](https://github.com/dotnet/fsharp/pull/16514))
* `nameof Module` expressions and patterns are processed to link files in `--test:GraphBasedChecking`. ([PR #16550](https://github.com/dotnet/fsharp/pull/16550), [PR #16743](https://github.com/dotnet/fsharp/pull/16743))
* Graph Based Checking doesn't throw on invalid parsed input so it can be used for IDE scenarios ([PR #16575](https://github.com/dotnet/fsharp/pull/16575), [PR #16588](https://github.com/dotnet/fsharp/pull/16588), [PR #16643](https://github.com/dotnet/fsharp/pull/16643))
* Various parenthesization API fixes. ([PR #16578](https://github.com/dotnet/fsharp/pull/16578), [PR #16666](https://github.com/dotnet/fsharp/pull/16666), [PR #16901](https://github.com/dotnet/fsharp/pull/16901))
* Various parenthesization API fixes. ([PR #16578](https://github.com/dotnet/fsharp/pull/16578), [PR #16666](https://github.com/dotnet/fsharp/pull/16666), [PR #16901](https://github.com/dotnet/fsharp/pull/16901), [PR #16973](https://github.com/dotnet/fsharp/pull/16973))
* Keep parens for problematic exprs (`if`, `match`, etc.) in `$"{(…):N0}"`, `$"{(…),-3}"`, etc. ([PR #16578](https://github.com/dotnet/fsharp/pull/16578))
* Fix crash in DOTNET_SYSTEM_GLOBALIZATION_INVARIANT mode [#PR 16471](https://github.com/dotnet/fsharp/pull/16471))
* Fix16572 - Fixed the preview feature enabling Is properties for union case did not work correctly with let .rec and .fsi files ([PR #16657](https://github.com/dotnet/fsharp/pull/16657))
Expand Down
36 changes: 35 additions & 1 deletion src/Compiler/Service/SynExpr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,41 @@ module SynExpr =
// return (
// x
// )
| _, SyntaxNode.SynExpr outer :: _ when containsSensitiveIndentation outer.Range.StartColumn expr.Range -> true
| _, SyntaxNode.SynExpr(SynExpr.YieldOrReturn _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.YieldOrReturnFrom _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Assert _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Lazy _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.App(argExpr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.LetOrUse _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.LetOrUseBang _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.TryWith _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.TryFinally _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.For _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.ForEach _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.IfThenElse _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.New _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Set(rhsExpr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.DotIndexedSet(valueExpr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.DotNamedIndexedPropertySet(rhsExpr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.DotSet(rhsExpr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.LibraryOnlyUnionCaseFieldSet(rhsExpr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.LongIdentSet(expr = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.NamedIndexedPropertySet(expr2 = SynExpr.Paren(expr = Is expr)) as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.InferredUpcast _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.InferredDowncast _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Match _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.MatchBang _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.While _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.WhileBang _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Do _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.DoBang _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Fixed _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.Record _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.AnonRecd _ as outer) :: _
| _, SyntaxNode.SynExpr(SynExpr.InterpolatedString _ as outer) :: _ when
containsSensitiveIndentation outer.Range.StartColumn expr.Range
->
true

// Check for nested matches, e.g.,
//
Expand Down
34 changes: 34 additions & 0 deletions tests/FSharp.Compiler.Service.Tests/SynExprTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,37 @@ let shouldBeParenthesizedInContext (expected: Parenthesization list) src =
| _ -> actual)

CollectionAssert.AreEqual(expected, actual)

[<Theory>]
[<TestCase("9")>]
[<TestCase("9 |> ignore")>]
[<TestCase("
let x =
do ()
9
")>]
[<TestCase("
let x =
do ()
9 |> ignore
")>]
[<TestCase("
for x in 1..10 do
9 |> ignore
")>]
let ``shouldBeParenthesizedInContext handles an unparenthesized hypothetical`` src =
let ast = getParseResults src

let getSourceLineStr =
let lines = src.ReplaceLineEndings().Split '\n'
Line.toZ >> Array.get lines

let expr, path =
(None, ast)
||> ParsedInput.foldWhile (fun acc path node ->
match node with
| SyntaxNode.SynExpr (SynExpr.Const(SynConst.Int32 9, _) as expr) -> Some(Some(expr, path))
| _ -> Some acc)
|> Option.defaultWith (fun () -> invalidOp "Expected a 9 but did not find one.")

Assert.False(SynExpr.shouldBeParenthesizedInContext getSourceLineStr path expr)
Loading