Bug 56611 - Regression: ArrayTypeMismatchException when running F# script
Summary: Regression: ArrayTypeMismatchException when running F# script
Status: VERIFIED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection (show other bugs)
Version: 5.0 (2017-02)
Hardware: PC Mac OS
: --- normal
Target Milestone: 15.3
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2017-05-18 12:30 UTC by Jason Imison
Modified: 2017-07-18 07:40 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: Yes
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Jason Imison 2017-05-18 12:30:08 UTC
Save the following script as bug.fsx

```
type Observation = { Label:string; Pixels:int[] }

let x = [|""|] |> Array.map (fun s -> { Label = s; Pixels =[|1|]})

printfn "%A" x
```

run `fsharpi bug.fsx`

output from Mono 5.0

System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
  at (wrapper stelemref) System.Object:virt_stelemref_sealed_class (intptr,object)
  at <StartupCode$FSI_0003>.$FSI_0003.main@ () [0x0009f] in <ac8e9934757e4d50a3930165400b2c10>:0
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b697cffb61b74023aa9c587e6c49beb3>:0

output from Mono 4.8


[|{Label = "";
   Pixels = [|1|];}|]
Comment 1 Jason Imison 2017-05-18 12:33:21 UTC
Corresponding ticket in fsharp/fsharp https://github.com/fsharp/fsharp/issues/739
Comment 2 Jason Imison 2017-05-18 12:37:33 UTC
I tested the same version of the compiler with both Mono 4.8 and Mono 5.0 and only 5.0 exhibits this issue.
Comment 3 Marek Safar 2017-05-18 17:55:37 UTC
Jason, 15.2.2 is done and as we don't have a fix yet it won't make it
Comment 4 Dave Thomas 2017-05-19 11:29:43 UTC
Yay for first class F# support.
Comment 5 Aleksey Kliger 2017-05-23 01:55:30 UTC
git blame says this is due to https://github.com/mono/mono/commit/a0ad8154f7cdca706802aa196d95686e0f8d3b7b

I don't entirely understand yet what broke.

Here's a smaller reproduction bug.fsx:

```
type Observation = { Label:string }

let x = [| ({ Label = "" } |]
```
(So it's not type inference misbehaving somehow)
Comment 6 Zoltan Varga 2017-05-23 04:01:58 UTC
This happens because mono_class_setup_fields () is called on the incomplete type Observer from mono_bounded_array_class_get (). So later, when the type is finished, its fields are not initialized again, so the Label field will have offset 0.
Comment 7 Zoltan Varga 2017-05-30 22:20:57 UTC
https://github.com/mono/mono/pull/4952
Comment 8 Zoltan Varga 2017-05-31 17:25:50 UTC
Fixed in master/2017-04.
Comment 9 Jason Imison 2017-06-08 14:40:27 UTC
Tested on Mono 5.2.0.164

Works great, thanks! I can add a regression test into the F# tests.