Bug 56423 - FSharp - ArrayTypeMismatchException upon Array.map
Summary: FSharp - ArrayTypeMismatchException upon Array.map
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: 5.0 (2017-02)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-05-14 10:17 UTC by Yves Reynhout
Modified: 2017-07-06 01:50 UTC (History)
3 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Yves Reynhout 2017-05-14 10:17:35 UTC
I get the following runtime exception, executed using fsharp interactive running on top of mono,

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 FSI_0005.Blog.generate (FSI_0005.Blog+BlogSettings settings) [0x0008d] in <9881edeb283347c3b9f6260bc48e127e>:0 
  at FSI_0005.Build+clo@72-5.Invoke (Microsoft.FSharp.Core.Unit unitVar0) [0x00042] in <9881edeb283347c3b9f6260bc48e127e>:0 
  at Fake.TargetHelper+targetFromTemplate@209-1[a].Invoke (Microsoft.FSharp.Core.Unit unitVar0) [0x00001] in <591449acccf1c534a7450383ac491459>:0 
  at Fake.TargetHelper.runSingleTarget (Fake.TargetHelper+TargetTemplate`1[a] target) [0x0004b] in <591449acccf1c534a7450383ac491459>:0 

upon executing the following code (lives in a Blog module)

let generate (settings: BlogSettings) =
    // Generate blogposts
    let allBlogPostFiles = settings.BlogPostsDir.GetFiles("*.md", SearchOption.AllDirectories)

    let blogPosts =
        |> Array.map (fun file -> 
                BlogPost.BaseDirectory = settings.BlogPostsDir
                File = file
                Metadata = readBlogPostMetadata file

Particularly, the Array.map is causing the problem. If I change it to a Seq (IEnumerable) there's no problem. Note this code works as is on mono 4.8 and 4.2 (Ubuntu Xenial) and .NET Fx 4.6 on windows. The GetFiles method is the one from System.IO.DirectoryInfo, which returns an array of System.IO.FileInfo objects. The map transforms those into custom BlogPost objects. Could recent changes in the following area have caused this? http://www.mono-project.com/docs/about-mono/releases/5.0.0/#lazy-array-interfaces

Whether FSharp's Array.map (https://github.com/Microsoft/visualfsharp/blob/6ce962e97de24273d167b6c174344e3017a6e4c7/src/fsharp/FSharp.Core/array.fs#L266) is at fault or the above recent changes in mono is hard to determine for me.
Comment 1 Sam Williams 2017-07-06 01:50:36 UTC
I have also found this problem with the following reproduction:

//file: repoScript.fsx
#r "../packages/FAKE/tools/FakeLib.dll"
open Fake

module Actions =
type AndroidDevice = { Name: string; DeviceSerial: string; Port: string; ExcludeCategory: string; }
let run s xs =

open Actions
let thing = { Name = "Tablet (Portrait)"; DeviceSerial = "93df2b67"; Port = "32021"; ExcludeCategory = "AndroidTests" }

Target "Run-Droid-UI-Online-Tests" (fun _ -> run "online" [|thing|])

RunTargetOrDefault "Run-Droid-UI-Online-Tests"

run with: mono ./packages/FAKE/tools/Fake.exe repoScript.fsx "target=Run-Droid-UI-Online-Tests"

Note You need to log in before you can comment on or make changes to this bug.