Skip to content

"try with" within seq fails to capture exception variable #17930

@Gradi

Description

@Gradi

F# code like

seq {
    try
        ...
    with
    | :? SomeException as exc when (exc.InnerException :? Some2Exception)
}

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.

module Program

open System

// This is OK
let sample () =
    try
        printfn "Hi"
    with
    | :? AggregateException as exc when (exc.InnerException :? OperationCanceledException) ->
        ()


// This is OK
let asyncSample () : Async<unit> =
    async {
        try
            printfn "Hi"
        with
        | :? AggregateException as exc when (exc.InnerException :? OperationCanceledException) -> ()
    }

(*
** This fails with
** 0>Program.fs(26,9): Error FS0971 : Undefined value 'exc'
** 0>Program.fs(26,9): Error FS0971 : Undefined value 'exc'
** 0>Program.fs(26,9): Error FS0971 : Undefined value 'exc'
** 0>Program.fs(26,9): Error FS0971 : Undefined value 'exc'
** 0>Program.fs(26,9): Error FS0971 : Undefined value 'exc'
*)
let seqSample () : int seq =
    seq {
        try
            printfn "Hi"
        with
        | :? AggregateException as exc when (exc.InnerException :? OperationCanceledException) -> ()
    }


// This is OK
let seqSample2 () : int seq =
    seq {
        try
            printfn "Hi"
        with
        | :? AggregateException as exc when (true) -> ()
    }

[<EntryPoint>]
let main argv =
    0

Expected behavior

  • Code compiles, when cases of try with within seq can access exception variable;
  • OR As far as I remember, F# didn't have support for try with within seq 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

  • dotnet version 8.0.403;
  • F# version 12.8.401.0 for F# 8.0;
  • Rider and Visual Studio(Ionide) code show no red lines near problematic lines;

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBug

Type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions