Open Gradi opened 1 week ago
This is indeed a bug related to how try-with in seq gets lowered during compilation into a state machine.
As a workaround, you can accomplish the goal by abstracting the check into an active pattern:
open System
let (|AggCanceled|_|) (exn:Exception) =
match exn with
| :? AggregateException as exc when (exc.InnerException :? OperationCanceledException) -> Some exc
| _ -> None
let seqSample () : int seq =
seq {
try
printfn "Hi"
with
| AggCanceled ac -> yield ac.InnerExceptions.GetHashCode()
}
Will this help in your use case?
@T-Gro This probably can help me, but I have already rewrote code to something different to get job done. But this is still a bug which will be fixed at some point in time, right?
Yes, it is a bug and needs fixing.
Disregard my (now deleted) comment.
In .NET 6 we had the following:
dotnet build
MSBuild version 17.3.2+561848881 for .NET
Determining projects to restore...
Restored /Users/u/code/issues/gh/17930/17930.fsproj (in 134 ms).
/Users/u/code/issues/gh/17930/Program.fs(33,9): error FS0796: 'try'/'with' cannot be used within sequence expressions [/Users/u/code/issues/gh/17930/17930.fsproj]
/Users/u/code/issues/gh/17930/Program.fs(43,9): error FS0796: 'try'/'with' cannot be used within sequence expressions [/Users/u/code/issues/gh/17930/17930.fsproj]
Build FAILED.
/Users/u/code/issues/gh/17930/Program.fs(33,9): error FS0796: 'try'/'with' cannot be used within sequence expressions [/Users/u/code/issues/gh/17930/17930.fsproj]
/Users/u/code/issues/gh/17930/Program.fs(43,9): error FS0796: 'try'/'with' cannot be used within sequence expressions [/Users/u/code/issues/gh/17930/17930.fsproj]
0 Warning(s)
2 Error(s)
Time Elapsed 00:00:01.58
Which has changed in https://github.com/dotnet/fsharp/pull/14540
In rc2 we fail to bind exc
F# code like
fails to be compiled because
exc
is not available in(exc.InnerException :? Some2Exception)
environment.This probably happens somewhere within part of compiler which transforms computational expressions into calls to builder.
Repro steps
Here is minimal source code with bug.
Expected behavior
when
cases oftry with
withinseq
can access exception variable;try with
withinseq
before version 8.0. If my case is not allowed by design, then a good readable warning by compiler;Actual behavior
Compilation fails with
Undefined value 'exc'
.Related information
8.0.403
;12.8.401.0 for F# 8.0
;