Skip to content

Commit 9f30dcc

Browse files
committed
Don't use Option.getValue in taskOption CE
1 parent d61158b commit 9f30dcc

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

src/FsToolkit.ErrorHandling.TaskResult/TaskOptionCE.fs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,12 @@ type TaskOptionStateMachineData<'T> =
440440
| ValueSome (None) -> true
441441
| ValueSome _ -> false
442442

443+
member this.SetResult() =
444+
match this.Result with
445+
| ValueNone -> this.MethodBuilder.SetResult None
446+
| ValueSome x -> this.MethodBuilder.SetResult x
447+
448+
443449
member this.IsTaskCompleted = this.MethodBuilder.Task.IsCompleted
444450

445451
and AsyncTaskOptionMethodBuilder<'TOverall> = AsyncTaskMethodBuilder<'TOverall option>
@@ -463,7 +469,7 @@ module TaskOptionBuilderBase =
463469
// Set the result now to allow short-circuiting of the rest of the CE.
464470
// Run/RunDynamic will skip setting the result if it's already been set.
465471
// Combine/CombineDynamic will not continue if the result has been set.
466-
sm.Data.MethodBuilder.SetResult sm.Data.Result.Value
472+
sm.Data.SetResult()
467473
true
468474
else
469475
WhileDynamic(&sm, condition, body)
@@ -489,7 +495,7 @@ module TaskOptionBuilderBase =
489495
// Set the result now to allow short-circuiting of the rest of the CE.
490496
// Run/RunDynamic will skip setting the result if it's already been set.
491497
// Combine/CombineDynamic will not continue if the result has been set.
492-
sm.Data.MethodBuilder.SetResult sm.Data.Result.Value
498+
sm.Data.SetResult()
493499
true
494500
else
495501
WhileDynamic(&sm, condition, body)
@@ -603,7 +609,7 @@ type TaskOptionBuilderBase() =
603609
// Set the result now to allow short-circuiting of the rest of the CE.
604610
// Run/RunDynamic will skip setting the result if it's already been set.
605611
// Combine/CombineDynamic will not continue if the result has been set.
606-
sm.Data.MethodBuilder.SetResult sm.Data.Result.Value
612+
sm.Data.SetResult()
607613

608614
__stack_go
609615
//-- RESUMABLE CODE END
@@ -739,7 +745,7 @@ type TaskOptionBuilder() =
739745
if sm.Data.IsTaskCompleted then ()
740746

741747
if step then
742-
sm.Data.MethodBuilder.SetResult(sm.Data.Result.Value)
748+
sm.Data.SetResult()
743749
else
744750
let mutable awaiter =
745751
sm.ResumptionDynamicInfo.ResumptionData :?> ICriticalNotifyCompletion
@@ -775,7 +781,7 @@ type TaskOptionBuilder() =
775781
let __stack_code_fin = code.Invoke(&sm)
776782

777783
if __stack_code_fin && not sm.Data.IsTaskCompleted then
778-
sm.Data.MethodBuilder.SetResult(sm.Data.Result.Value)
784+
sm.Data.SetResult()
779785
with
780786
| exn -> __stack_exn <- exn
781787
// Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567

tests/FsToolkit.ErrorHandling.IcedTasks.Tests/CancellableTaskResultCE.fs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
11
namespace FsToolkit.ErrorHandling.IcedTasks.Tests
22

33
open Expecto
4-
open FsToolkit.ErrorHandling
54
open System.Threading
65
open System.Threading.Tasks
6+
open FsToolkit.ErrorHandling
77
open IcedTasks
88

9+
// module Affected =
10+
// let testFunctionCTR<'Dto>() =
11+
// cancellableTaskResult {
12+
// let dto = Unchecked.defaultof<'Dto>
13+
// System.Console.WriteLine(dto)
14+
// }
15+
// let testFunctionCTR<'Dto>() =
16+
// backgroundCancellableTaskResult {
17+
// let dto = Unchecked.defaultof<'Dto>
18+
// System.Console.WriteLine(dto)
19+
// }
20+
21+
// let testFunctionCanT<'Dto>() =
22+
// cancellableTask {
23+
// let dto = Unchecked.defaultof<'Dto>
24+
// System.Console.WriteLine(dto)
25+
// }
26+
27+
// let testFunctionCoT<'Dto>() =
28+
// coldTask {
29+
// let dto = Unchecked.defaultof<'Dto>
30+
// System.Console.WriteLine(dto)
31+
// }
32+
33+
// let testFunctionBTR<'Dto>() =
34+
// backgroundTaskResult{
35+
// let dto = Unchecked.defaultof<'Dto>
36+
// System.Console.WriteLine(dto)
37+
// }
38+
// let testFunctionBTO<'Dto>() =
39+
// backgroundTaskOption{
40+
// let dto = Unchecked.defaultof<'Dto>
41+
// System.Console.WriteLine(dto)
42+
// }
43+
// module NotAffected =
44+
// let testFunctionTR<'Dto>() =
45+
// taskResult {
46+
// let dto = Unchecked.defaultof<'Dto>
47+
// System.Console.WriteLine(dto)
48+
// }
49+
// let testFunctionTO<'Dto>() =
50+
// taskOption {
51+
// let dto = Unchecked.defaultof<'Dto>
52+
// System.Console.WriteLine(dto)
53+
// }
954
module CancellableTaskResultCE =
1055

1156

@@ -203,7 +248,7 @@ module CancellableTaskResultCE =
203248
task {
204249

205250
let ctr =
206-
cancellableTaskResult { return! fun (ct : CancellationToken) -> Task.CompletedTask }
251+
cancellableTaskResult { return! fun (ct: CancellationToken) -> Task.CompletedTask }
207252

208253
let! actual = ctr CancellationToken.None
209254
Expect.equal actual (Ok()) "Should be able to Return! CancellableTask"
@@ -477,7 +522,7 @@ module CancellableTaskResultCE =
477522

478523
let ctr =
479524
cancellableTaskResult {
480-
let! someValue = fun (ct : CancellationToken) -> Task.CompletedTask
525+
let! someValue = fun (ct: CancellationToken) -> Task.CompletedTask
481526
return someValue
482527
}
483528

@@ -490,13 +535,11 @@ module CancellableTaskResultCE =
490535
let data = ()
491536

492537
let ctr =
493-
cancellableTaskResult {
494-
do! fun (ct : CancellationToken) -> Task.CompletedTask
495-
// return someValue
496-
}
538+
cancellableTaskResult { do! fun (ct: CancellationToken) -> Task.CompletedTask }
497539

498540
let! actual = ctr CancellationToken.None
499541
Expect.equal actual (Ok data) "Should be able to let! CancellableTask"
542+
// return someValue
500543
}
501544
testCaseTask "let! TaskLike"
502545
<| fun () ->
@@ -513,7 +556,6 @@ module CancellableTaskResultCE =
513556
Expect.equal actual (Ok data) "Should be able to let! TaskLike"
514557
}
515558

516-
517559
]
518560
testList
519561
"Zero/Combine/Delay"

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskOption.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ let getFollowersSome = getFollowersSome >> Async.StartAsTask
2626
let allowedToPostOptional =
2727
allowedToPostOptional >> Async.StartAsTask
2828

29+
30+
2931
let mapTests =
3032
testList
3133
"TaskOption.map Tests"

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskOptionCE.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ open System.Threading.Tasks
88
open FSharp.Control.Tasks
99
#endif
1010

11+
module TestFuncs =
12+
let testFunctionTO<'Dto> () =
13+
taskOption {
14+
let dto = Unchecked.defaultof<'Dto>
15+
System.Console.WriteLine(dto)
16+
}
17+
1118
let makeDisposable () =
1219
{ new System.IDisposable with
1320
member this.Dispose() = () }
@@ -420,6 +427,12 @@ let ceTests =
420427
Expect.equal loopCount 2 "Should only loop twice"
421428
Expect.equal actual expected "Should be an error"
422429
Expect.isFalse wasCalled "No additional side effects should occur"
430+
}
431+
testCaseTask "Empty result"
432+
<| fun () ->
433+
task {
434+
let! _ = TestFuncs.testFunctionTO ()
435+
()
423436
} ]
424437

425438
let specialCaseTask returnValue =

0 commit comments

Comments
 (0)