Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cancellable: add safer APIs to check the token #18175

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* Sink: report SynPat.ArrayOrList type ([PR #18127](https://github.com/dotnet/fsharp/pull/18127))
* Show the default value of compiler options ([PR #18054](https://github.com/dotnet/fsharp/pull/18054))
* 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))
* Cancellable: add safer APIs to check the token ([PR #18175](https://github.com/dotnet/fsharp/pull/18175))

### Changed

Expand Down
7 changes: 7 additions & 0 deletions src/Compiler/Utilities/Cancellable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Cancellable =
tokenHolder.Value
|> ValueOption.defaultWith (fun () -> if guard then failwith msg else CancellationToken.None)

static member HasCancellationToken = tokenHolder.Value.IsSome

static member Token = ensureToken "Token not available outside of Cancellable computation."

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

static member TryCheckAndThrow() =
match tokenHolder.Value with
| ValueNone -> ()
| ValueSome token -> token.ThrowIfCancellationRequested()

namespace Internal.Utilities.Library

open System
Expand Down
4 changes: 4 additions & 0 deletions src/Compiler/Utilities/Cancellable.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ open System.Threading
type Cancellable =
/// For use in testing only. Cancellable.token should be set only by the cancellable computation.
static member internal UsingToken: CancellationToken -> IDisposable

static member HasCancellationToken: bool
static member Token: CancellationToken

static member CheckAndThrow: unit -> unit
static member TryCheckAndThrow: unit -> unit

namespace Internal.Utilities.Library

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2011,9 +2011,12 @@ FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryRe
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+MetadataOnlyFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+ReduceMemoryFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+Shim
FSharp.Compiler.Cancellable: Boolean HasCancellationToken
FSharp.Compiler.Cancellable: Boolean get_HasCancellationToken()
FSharp.Compiler.Cancellable: System.Threading.CancellationToken Token
FSharp.Compiler.Cancellable: System.Threading.CancellationToken get_Token()
FSharp.Compiler.Cancellable: Void CheckAndThrow()
FSharp.Compiler.Cancellable: Void TryCheckAndThrow()
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String OutputFile
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String get_OutputFile()
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: Void .ctor(System.String, Microsoft.FSharp.Core.FSharpFunc`2[System.Threading.CancellationToken,Microsoft.FSharp.Core.FSharpOption`1[System.IO.Stream]])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2011,9 +2011,12 @@ FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryRe
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+MetadataOnlyFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+ReduceMemoryFlag
FSharp.Compiler.AbstractIL.ILBinaryReader: FSharp.Compiler.AbstractIL.ILBinaryReader+Shim
FSharp.Compiler.Cancellable: Boolean HasCancellationToken
FSharp.Compiler.Cancellable: Boolean get_HasCancellationToken()
FSharp.Compiler.Cancellable: System.Threading.CancellationToken Token
FSharp.Compiler.Cancellable: System.Threading.CancellationToken get_Token()
FSharp.Compiler.Cancellable: Void CheckAndThrow()
FSharp.Compiler.Cancellable: Void TryCheckAndThrow()
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String OutputFile
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: System.String get_OutputFile()
FSharp.Compiler.CodeAnalysis.DelayedILModuleReader: Void .ctor(System.String, Microsoft.FSharp.Core.FSharpFunc`2[System.Threading.CancellationToken,Microsoft.FSharp.Core.FSharpOption`1[System.IO.Stream]])
Expand Down
Loading