Skip to content

Commit

Permalink
Fix wrong [<TailCall>] warning with unit-returning match expression (#…
Browse files Browse the repository at this point in the history
…17637)

* add unit test for issue 17604

* check if the expr type is unit for match expressions, too.

* add release notes entry

---------

Co-authored-by: Petr <psfinaki@users.noreply.github.com>
  • Loading branch information
dawedawe and psfinaki committed Aug 30, 2024
1 parent dcf7db7 commit 138593b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Fixed

* Fix wrong TailCall warning ([Issue #17604](https://github.com/dotnet/fsharp/issues/17604), [PR #17637](https://github.com/dotnet/fsharp/pull/17637))
* Compiler hangs when compiling inline recursive invocation ([Issue #17376](https://github.com/dotnet/fsharp/issues/17376), [PR #17394](https://github.com/dotnet/fsharp/pull/17394))
* Fix reporting IsFromComputationExpression only for CE builder type constructors and let bindings. ([PR #17375](https://github.com/dotnet/fsharp/pull/17375))
* Optimize simple mappings in comprehensions when the body of the mapping has `let`-bindings and/or sequential expressions before a single yield. ([PR #17419](https://github.com/dotnet/fsharp/pull/17419))
Expand Down
8 changes: 8 additions & 0 deletions src/Compiler/Checking/TailCallChecks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ type TailCall =
static member YesFromVal (g: TcGlobals) (v: Val) = TailCall.Yes(TailCall.IsVoidRet g v)

static member YesFromExpr (g: TcGlobals) (expr: Expr) =
let yesFromTType (t: TType) =
if isUnitTy g t then
TailCall.Yes TailCallReturnType.MustReturnVoid
else
TailCall.Yes TailCallReturnType.NonVoid

match expr with
| ValUseAtApp(valRef, _) -> TailCall.Yes(TailCall.IsVoidRet g valRef.Deref)
| Expr.Const(constType = constType) -> yesFromTType constType
| Expr.Match(exprType = exprType) -> yesFromTType exprType
| _ -> TailCall.Yes TailCallReturnType.NonVoid

member x.AtExprLambda =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1722,3 +1722,22 @@ module M =
|> withLangVersion80
|> compile
|> shouldSucceed

[<FSharp.Test.FactForNETCOREAPP>]
let ``Don't warn for tail rec call returning unit`` () =
"""
namespace N
module M =
[<TailCall>]
let rec go (args: string list) =
match args with
| [] -> ()
| "--" :: _ -> ()
| arg :: args -> go args
"""
|> FSharp
|> withLangVersion80
|> compile
|> shouldSucceed

0 comments on commit 138593b

Please sign in to comment.