diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md index b23aae2e37a..cb7ea355db2 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md +++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md @@ -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)) diff --git a/src/Compiler/Checking/TailCallChecks.fs b/src/Compiler/Checking/TailCallChecks.fs index b6551160153..d23b50716af 100644 --- a/src/Compiler/Checking/TailCallChecks.fs +++ b/src/Compiler/Checking/TailCallChecks.fs @@ -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 = diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs index e1208481308..4eb2d3b2a14 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs @@ -1722,3 +1722,22 @@ module M = |> withLangVersion80 |> compile |> shouldSucceed + + [] + let ``Don't warn for tail rec call returning unit`` () = + """ +namespace N + +module M = + + [] + let rec go (args: string list) = + match args with + | [] -> () + | "--" :: _ -> () + | arg :: args -> go args + """ + |> FSharp + |> withLangVersion80 + |> compile + |> shouldSucceed