From 2581fe785cb93e1a07bcd1f746647fa27df68e40 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 23 Dec 2024 17:25:47 +0100 Subject: [PATCH 1/4] Cancellable: add safer APIs to check the token --- src/Compiler/Utilities/Cancellable.fs | 8 ++++++++ src/Compiler/Utilities/Cancellable.fsi | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/Compiler/Utilities/Cancellable.fs b/src/Compiler/Utilities/Cancellable.fs index b3531c74890..3a7762f270a 100644 --- a/src/Compiler/Utilities/Cancellable.fs +++ b/src/Compiler/Utilities/Cancellable.fs @@ -14,6 +14,9 @@ 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) = @@ -28,6 +31,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 diff --git a/src/Compiler/Utilities/Cancellable.fsi b/src/Compiler/Utilities/Cancellable.fsi index 32d1d6e0a04..c5a8d9627ea 100644 --- a/src/Compiler/Utilities/Cancellable.fsi +++ b/src/Compiler/Utilities/Cancellable.fsi @@ -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 From 66c6dd29464cb4cdec9261ef8c396a5eecc61d2c Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 23 Dec 2024 18:46:01 +0100 Subject: [PATCH 2/4] Release notes --- docs/release-notes/.FSharp.Compiler.Service/9.0.200.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md index 740c18edcbc..3faf69c220d 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md +++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.200.md @@ -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 From 657f02ff556e74172b2d1d2ac7ca21d0e98cd651 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 23 Dec 2024 18:52:51 +0100 Subject: [PATCH 3/4] Surface area --- ...FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl | 3 +++ ...harp.Compiler.Service.SurfaceArea.netstandard20.release.bsl | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl b/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl index c3b65b8e378..db4105530c8 100644 --- a/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl +++ b/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl @@ -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]]) diff --git a/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl b/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl index c3b65b8e378..db4105530c8 100644 --- a/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl +++ b/tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl @@ -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]]) From cccc177901605113f7cc191d0edca4b8681e4f41 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 23 Dec 2024 18:52:55 +0100 Subject: [PATCH 4/4] Fantomas --- src/Compiler/Utilities/Cancellable.fs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Compiler/Utilities/Cancellable.fs b/src/Compiler/Utilities/Cancellable.fs index 3a7762f270a..8f14b5ddbfe 100644 --- a/src/Compiler/Utilities/Cancellable.fs +++ b/src/Compiler/Utilities/Cancellable.fs @@ -14,8 +14,7 @@ type Cancellable = tokenHolder.Value |> ValueOption.defaultWith (fun () -> if guard then failwith msg else CancellationToken.None) - static member HasCancellationToken = - tokenHolder.Value.IsSome + static member HasCancellationToken = tokenHolder.Value.IsSome static member Token = ensureToken "Token not available outside of Cancellable computation."