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

Fix false negative [<TailCall>] warning #18399

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,4 +1,5 @@
### Fixed
* Fix missing TailCall warning in TOp.IntegerForLoop ([PR #18399](https://github.com/dotnet/fsharp/pull/18399))
* Fix classification of `nameof` in `nameof<'T>`, `match … with nameof ident -> …`. ([Issue #10026](https://github.com/dotnet/fsharp/issues/10026), [PR #18300](https://github.com/dotnet/fsharp/pull/18300))
* Fix Realsig+ generates nested closures with incorrect Generic ([Issue #17797](https://github.com/dotnet/fsharp/issues/17797), [PR #17877](https://github.com/dotnet/fsharp/pull/17877))
* Fix optimizer internal error for records with static fields ([Issue #18165](https://github.com/dotnet/fsharp/issues/18165), [PR #18280](https://github.com/dotnet/fsharp/pull/18280))
Expand Down
7 changes: 6 additions & 1 deletion src/Compiler/Checking/TailCallChecks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,12 @@ let CheckModuleBinding cenv (isRec: bool) (TBind _ as bind) =
// warn for recursive calls in TryWith/TryFinally operations
exprs |> Seq.iter (checkTailCall true)
| Expr.Op(args = exprs) -> exprs |> Seq.iter (checkTailCall insideSubBindingOrTry)
| Expr.Sequential(expr2 = expr2) -> checkTailCall insideSubBindingOrTry expr2
| Expr.Sequential(expr1 = expr1; expr2 = expr2) ->
match expr1 with
| Expr.Op(args = exprs; op = TOp.IntegerForLoop _) -> checkTailCall insideSubBindingOrTry expr1
| _ -> ()

checkTailCall insideSubBindingOrTry expr2
| _ -> ()

checkTailCall false bodyExpr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,20 +773,20 @@ namespace N
|> compile
|> shouldFail
|> withResults [
{ Error = Warning 3569
Range = { StartLine = 21
StartColumn = 27
EndLine = 21
EndColumn = 35 }
Message =
"The member or function 'instType' has the 'TailCallAttribute' attribute, but is not being used in a tail recursive way." }
{ Error = Warning 3569
Range = { StartLine = 17
StartColumn = 32
EndLine = 17
EndColumn = 77 }
Message =
"The member or function 'instType' has the 'TailCallAttribute' attribute, but is not being used in a tail recursive way." }
{ Error = Warning 3569
Range = { StartLine = 21
StartColumn = 27
EndLine = 21
EndColumn = 35 }
Message =
"The member or function 'instType' has the 'TailCallAttribute' attribute, but is not being used in a tail recursive way." }
]

[<FSharp.Test.FactForNETCOREAPP>]
Expand Down Expand Up @@ -1769,3 +1769,72 @@ module M =
|> withLangVersion80
|> compile
|> shouldSucceed

[<FSharp.Test.FactForNETCOREAPP>]
let ``Warn successfully in Array-mapped recursive call`` () =
"""
namespace N

module M =

type Value =
{ Code: string }

[<TailCall>]
let rec fooArray (values: Value[], code: string) =
match values with
| [||] -> seq { code }
| values ->
let replicatedValues =
values
|> Array.map (fun value -> fooArray (values, value.Code))
replicatedValues |> Seq.concat
"""
|> FSharp
|> withLangVersion80
|> compile
|> shouldFail
|> withResults [
{ Error = Warning 3569
Range = { StartLine = 16
StartColumn = 20
EndLine = 16
EndColumn = 74 }
Message =
"The member or function 'fooArray' has the 'TailCallAttribute' attribute, but is not being used in a tail recursive way." }
]

[<FSharp.Test.FactForNETCOREAPP>]
let ``Warn successfully in Seq-mapped recursive call`` () =
"""
namespace N

module M =

type Value =
{ Code: string }

[<TailCall>]
let rec fooSeq (values: Value[], code: string) =
match values with
| [||] -> seq { code }
| values ->
let replicatedValues =
values
|> Seq.map (fun value -> fooSeq (values, value.Code))
|> Seq.toArray
replicatedValues |> Seq.concat
"""
|> FSharp
|> withLangVersion80
|> compile
|> shouldFail
|> withResults [
{ Error = Warning 3569
Range = { StartLine = 16
StartColumn = 42
EndLine = 16
EndColumn = 48 }
Message =
"The member or function 'fooSeq' has the 'TailCallAttribute' attribute, but is not being used in a tail recursive way." }
]