Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F# program doesn't continue linearly #367

Closed
haf opened this issue Apr 16, 2015 · 4 comments
Closed

F# program doesn't continue linearly #367

haf opened this issue Apr 16, 2015 · 4 comments

Comments

@haf
Copy link

haf commented Apr 16, 2015

When the record-update syntax is called with a null value as the record, the program jumps to a random location in the code-base.

Example:

type B =
 { y : string }
type A =
  { x : B }
let test () : A =
  let n = Unchecked.defaultOf<obj>
  { n with x = { y = "test" } }
test ()

This should probably throw a NullReferenceException?

I get the null from C# interop; but since I'm deserialising to a Record it 'should never be null' according to the static typing rules, but the C# lib (Newtonsoft) deserialises to null if it can't deserialise the record.

@latkin
Copy link
Contributor

latkin commented Apr 16, 2015

I do not see anything resembling "jumps to a random point in the codebase" -- can you provide a complete, self-contained repro that illustrates this?

Since x is the only field on the type, and you are updating its value, the compiled form of the test function actually never even looks at the input.

let doUpdate n = { n with x = { y = "test" } }

Compiles simply to

public static A doUpdate(A n)
{
    return new A(new B("test"));
}

If your record type has multiple properties, then you indeed get a nullref, as expected.

type C = { w : string; z : int }
let doUpdate n = { n with z = 42 }
doUpdate Unchecked.defaultof<C>  // NullReferenceException

Current behavior seems reasonable to me. I guess one could opine that copy and update should always throw if in the input is null, but it seems odd to check for null of an input that is never otherwise used.

@haf
Copy link
Author

haf commented Apr 16, 2015

I tried reproducing it in the small, but wasn't able. I can let someone from your team have access if you want?

@latkin
Copy link
Contributor

latkin commented Apr 16, 2015

You can email private info to [email protected] if needed

@haf
Copy link
Author

haf commented Jul 28, 2015

Let's close this to celebrate F# 4.0 and write it down to a mono bug. =) This was one of the strangest bugs I've seen though :)

@haf haf closed this as completed Jul 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants