Skip to content

Commit 138593b

Browse files
dawedawepsfinaki
andauthored
Fix wrong [<TailCall>] warning with unit-returning match expression (#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 <[email protected]>
1 parent dcf7db7 commit 138593b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.100.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Fixed
22

3+
* Fix wrong TailCall warning ([Issue #17604](https://github.com/dotnet/fsharp/issues/17604), [PR #17637](https://github.com/dotnet/fsharp/pull/17637))
34
* 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))
45
* Fix reporting IsFromComputationExpression only for CE builder type constructors and let bindings. ([PR #17375](https://github.com/dotnet/fsharp/pull/17375))
56
* 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))

src/Compiler/Checking/TailCallChecks.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ type TailCall =
4747
static member YesFromVal (g: TcGlobals) (v: Val) = TailCall.Yes(TailCall.IsVoidRet g v)
4848

4949
static member YesFromExpr (g: TcGlobals) (expr: Expr) =
50+
let yesFromTType (t: TType) =
51+
if isUnitTy g t then
52+
TailCall.Yes TailCallReturnType.MustReturnVoid
53+
else
54+
TailCall.Yes TailCallReturnType.NonVoid
55+
5056
match expr with
5157
| ValUseAtApp(valRef, _) -> TailCall.Yes(TailCall.IsVoidRet g valRef.Deref)
58+
| Expr.Const(constType = constType) -> yesFromTType constType
59+
| Expr.Match(exprType = exprType) -> yesFromTType exprType
5260
| _ -> TailCall.Yes TailCallReturnType.NonVoid
5361

5462
member x.AtExprLambda =

tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,3 +1722,22 @@ module M =
17221722
|> withLangVersion80
17231723
|> compile
17241724
|> shouldSucceed
1725+
1726+
[<FSharp.Test.FactForNETCOREAPP>]
1727+
let ``Don't warn for tail rec call returning unit`` () =
1728+
"""
1729+
namespace N
1730+
1731+
module M =
1732+
1733+
[<TailCall>]
1734+
let rec go (args: string list) =
1735+
match args with
1736+
| [] -> ()
1737+
| "--" :: _ -> ()
1738+
| arg :: args -> go args
1739+
"""
1740+
|> FSharp
1741+
|> withLangVersion80
1742+
|> compile
1743+
|> shouldSucceed

0 commit comments

Comments
 (0)