Skip to content

Mutually recursive computation expression raises NullReferenceException under some conditions #3783

Closed

Description

@aqtq314

Given the following function definition,

let x () =
    let rec a = seq {
        yield 0
        yield! b () }
    and b () = seq {
        yield 1
        yield! a }
    do Seq.take 10 a |> Seq.iter (printfn "%d")

Invoking x () will print a 0 then raises NullReferenceException. Putting the function body into a type constructor generates a similar behavior when creating an instance of it:

type A () =
    do  let rec a = seq {
            yield 0
            yield! b () }
        and b () = seq {
            yield 1
            yield! a }
        do Seq.take 10 a |> Seq.iter (printfn "%d")

One of the workarounds would be inlining the do-binding in the above type definition, like below:

type A () =
    let rec a = seq {
        yield 0
        yield! b () }
    and b () = seq {
        yield 1
        yield! a }
    do Seq.take 10 a |> Seq.iter (printfn "%d")

Another workaround is to modify a so that it becomes a function that takes in a parameter:

type A () =
    do  let rec a () = seq {
// ...

Environment:

  • VS2017
  • .NET Framework 4.5 or upper
  • FSharp.Core 4.4.0 or upper

This doesn't happen in VS2015 or on www.ideone.com.

Activity

  1. cartermp commented on Oct 20, 2017

    @cartermp
    Contributor

    This looks like a dupe of #3743 - @aqtq314 are you using VS 2017 15.4?

  2. aqtq314 commented on Oct 20, 2017

    @aqtq314
    Author

    I'm on VS Community 2017 15.2 (26430.16), and I cannot reproduce #3743 on my side.
    But it does look like the underlying cause is similar.

  3. aqtq314 commented on Oct 20, 2017

    @aqtq314
    Author

    Oh and release mode doesn't help in this one.

  4. aqtq314 commented on Oct 20, 2017

    @aqtq314
    Author

    clicked the wrong button D:

  5. cartermp commented on Oct 20, 2017

    @cartermp
    Contributor

    I can reproduce this on 15.4.1. Interesting that you can see this on 15.2. @dsyme's PR (#3536) which seems to have caused #3734 was merged long after 15.2 shipped. @dsyme any ideas?

  6. added
    Impact-Medium
    on Oct 24, 2017
  7. dsyme commented on Oct 24, 2017

    @dsyme
    Contributor

    This is a separate bug. It's odd as we've not had any bugs in closure construction for a long time.

    Anyway, it' not a regression but obviously needs to be fixed

  8. added this to the 16.0 milestone on Aug 29, 2018
  9. modified the milestones: 16.0, 16.1 on Feb 21, 2019
  10. removed this from the 16.1 milestone on Apr 23, 2019
  11. 2 remaining items

  12. added this to the Backlog milestone on Apr 30, 2019
  13. added
    Area-Compiler-StateMachines
    and removed on Mar 31, 2022
  14. self-assigned this
    on Mar 31, 2022
  15. github-actions commented on Apr 19, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    This issue appears to be fixed on the current main branch. Tested on commit 9112d21d43f (F# 11.0, FSI version 15.2.101.0).

    Reproduction (both variants from the issue):

    #nowarn "40"
    
    // Variant 1: function definition (original repro)
    let x () =
        let rec a = seq {
            yield 0
            yield! b () }
        and b () = seq {
            yield 1
            yield! a }
        Seq.take 10 a |> Seq.toList
    
    // Variant 2: type constructor (from issue body)
    type A () =
        let test =
            let rec a = seq {
                yield 0
                yield! b () }
            and b () = seq {
                yield 1
                yield! a }
            Seq.take 10 a |> Seq.toList
        member _.Test = test
    
    let result1 = x ()
    printfn "Variant 1: %A" result1   // [0; 1; 0; 1; 0; 1; 0; 1; 0; 1]
    
    let a = A()
    printfn "Variant 2: %A" a.Test    // [0; 1; 0; 1; 0; 1; 0; 1; 0; 1]
    ```
    
    **Output (no exception):**
    ```
    Variant 1: [0; 1; 0; 1; 0; 1; 0; 1; 0; 1]
    Variant 2: [0; 1; 0; 1; 0; 1; 0; 1; 0; 1]

    Both the function-scoped and type-constructor-scoped mutually recursive computation expressions now work correctly without NullReferenceException.

    Generated by 🌈 Repo Assist, see workflow run. Learn more.

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@7c7feb61a52b662eb2089aa2945588b7a200d404
    
  16. added a commit that references this issue on Apr 19, 2026
  17. added a commit that references this issue on Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

AI-thinks-issue-fixedArea-Compiler-StateMachinesBugImpact-Medium

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions