Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Initialization of mutually recursive values can sometimes leave fields uninitialized #847

Closed

Description

@fsoikin

When initializing mutually recursive values, some fields may end up null (i.e. uninitialized). This does not depend on whether the values are not actually recursive or only declared as such.

No warnings are issued by the compiler, and no runtime errors are raised.

Repro steps

Here is a minimal repro:

type A = A of B
and B = B of A | C

// Case 1: non-recursive
let rec a1 = B (A b1)
and b1 = B (A c1)
and c1 = C

// Case 2: true recursive
let rec a2 = A b2
and b2 = B a2
  • Case 1: the values are not actually recursive, but are declared as such. The value a1 ends up under-initialized: a1 = B (A null). However, value b1 initializes correctly: b1 = B (A C).
  • Case 2: the values are actually recursive, and both end up invalid: a2 = A null, b2 = B (A null). Also, b2 itself seems to get initialized correctly as far as its own fields, that is: obj.ReferenceEquals( (fun (B a) -> a) b2, a2 ) = true

Expected behavior

a1 = B (A (B (A C)))
b1 = B (A C)
c1 = C

a2 = A (B a2)
b2 = B (A b2)

Activity

  1. 71 commented on Oct 27, 2018

    @71

    Looks like this problem only appears in modules, that is...

    let rec a = A b
    and b = B a ;;

    does result in

    val a : A = A null
    val b : B = B (A null)

    However, this non-recursive behavior can be avoided by using an inner expression:

    let a, b =
      let rec a = A b
      and b = B a
      in a, b
    ;;

    results in

    val a : A = A (B ...)
    val b : B = B (A ...)
  2. fsoikin commented on Apr 29, 2019

    @fsoikin
    Author
  3. dsyme commented on May 16, 2019

    @dsyme
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions