Skip to content

Commit 749853e

Browse files
authoredJan 2, 2025··
Cancellable: add safer APIs to check the token (#18175)
1 parent b1659a1 commit 749853e

File tree

5 files changed

+18
-0
lines changed

5 files changed

+18
-0
lines changed
 

‎docs/release-notes/.FSharp.Compiler.Service/9.0.200.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Sink: report SynPat.ArrayOrList type ([PR #18127](https://github.com/dotnet/fsharp/pull/18127))
3333
* Show the default value of compiler options ([PR #18054](https://github.com/dotnet/fsharp/pull/18054))
3434
* Support ValueOption + Struct attribute as optional parameter for methods ([Language suggestion #1136](https://github.com/fsharp/fslang-suggestions/issues/1136), [PR #18098](https://github.com/dotnet/fsharp/pull/18098))
35+
* Cancellable: add safer APIs to check the token ([PR #18175](https://github.com/dotnet/fsharp/pull/18175))
3536

3637
### Changed
3738

‎src/Compiler/Utilities/Cancellable.fs

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type Cancellable =
1414
tokenHolder.Value
1515
|> ValueOption.defaultWith (fun () -> if guard then failwith msg else CancellationToken.None)
1616

17+
static member HasCancellationToken = tokenHolder.Value.IsSome
18+
1719
static member Token = ensureToken "Token not available outside of Cancellable computation."
1820

1921
static member UsingToken(ct) =
@@ -28,6 +30,11 @@ type Cancellable =
2830
let token = ensureToken "CheckAndThrow invoked outside of Cancellable computation."
2931
token.ThrowIfCancellationRequested()
3032

33+
static member TryCheckAndThrow() =
34+
match tokenHolder.Value with
35+
| ValueNone -> ()
36+
| ValueSome token -> token.ThrowIfCancellationRequested()
37+
3138
namespace Internal.Utilities.Library
3239

3340
open System

‎src/Compiler/Utilities/Cancellable.fsi

+4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ open System.Threading
77
type Cancellable =
88
/// For use in testing only. Cancellable.token should be set only by the cancellable computation.
99
static member internal UsingToken: CancellationToken -> IDisposable
10+
11+
static member HasCancellationToken: bool
1012
static member Token: CancellationToken
13+
1114
static member CheckAndThrow: unit -> unit
15+
static member TryCheckAndThrow: unit -> unit
1216

1317
namespace Internal.Utilities.Library
1418

‎tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl

+3
Original file line numberDiff line numberDiff line change
@@ -2011,9 +2011,12 @@ FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryRe
20112011
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+MetadataOnlyFlag
20122012
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+ReduceMemoryFlag
20132013
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+Shim
2014+
FSharp.Compiler.Cancellable: Boolean HasCancellationToken
2015+
FSharp.Compiler.Cancellable: Boolean get_HasCancellationToken()
20142016
FSharp.Compiler.Cancellable: System.Threading.CancellationToken Token
20152017
FSharp.Compiler.Cancellable: System.Threading.CancellationToken get_Token()
20162018
FSharp.Compiler.Cancellable: Void CheckAndThrow()
2019+
FSharp.Compiler.Cancellable: Void TryCheckAndThrow()
20172020
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String OutputFile
20182021
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String get_OutputFile()
20192022
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: Void .ctor(System.String, Microsoft.FSharp.Core.FSharpFunc`2[System.Threading.CancellationToken,Microsoft.FSharp.Core.FSharpOption`1[System.IO.Stream]])

‎tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl

+3
Original file line numberDiff line numberDiff line change
@@ -2011,9 +2011,12 @@ FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryRe
20112011
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+MetadataOnlyFlag
20122012
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+ReduceMemoryFlag
20132013
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+Shim
2014+
FSharp.Compiler.Cancellable: Boolean HasCancellationToken
2015+
FSharp.Compiler.Cancellable: Boolean get_HasCancellationToken()
20142016
FSharp.Compiler.Cancellable: System.Threading.CancellationToken Token
20152017
FSharp.Compiler.Cancellable: System.Threading.CancellationToken get_Token()
20162018
FSharp.Compiler.Cancellable: Void CheckAndThrow()
2019+
FSharp.Compiler.Cancellable: Void TryCheckAndThrow()
20172020
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String OutputFile
20182021
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String get_OutputFile()
20192022
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: Void .ctor(System.String, Microsoft.FSharp.Core.FSharpFunc`2[System.Threading.CancellationToken,Microsoft.FSharp.Core.FSharpOption`1[System.IO.Stream]])

0 commit comments

Comments
 (0)
Please sign in to comment.