Skip to content

Array comprehension no longer compiles on rc1 #17708

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

Closed
sayurin opened this issue Sep 12, 2024 · 7 comments · Fixed by #17711
Closed

Array comprehension no longer compiles on rc1 #17708

sayurin opened this issue Sep 12, 2024 · 7 comments · Fixed by #17711

Comments

@sayurin
Copy link

sayurin commented Sep 12, 2024

Please provide a succinct description of the issue.

Repro steps

Following code causes compile error on .NET 9 RC1.

let arr = [| for _ in Array.groupBy id [||] do 0 |]

Expected behavior

.NET 8 can compile it, successfully.

Actual behavior

.NET 9 RC1 fails with following error.

FSC : error FS2014: A problem occurred writing the binary 'obj\Release\net9.0\refint\FsConsoleApp3.dll': duplicate entry 'arr@1' in type index table

Known workarounds

split it.

let arr = [|
    let arr = Array.groupBy id [||]
    for _ in arr do 0
|]

Related information

Provide any related information (optional):

  • Windows 11
  • .NET 9 RC1
  • Visual Studio Professional 2022 Preview 17.12.0 Preview 2.0
@brianrourkeboll
Copy link
Contributor

This must be from one of my PRs. I will look into this.

@vzarytovskii vzarytovskii changed the title F# Compiler regression error Array comprehension no longer compiles on rc1 Sep 12, 2024
@vzarytovskii
Copy link
Member

    <LangVersion>8</LangVersion>

Should fix it

@brianrourkeboll
Copy link
Contributor

brianrourkeboll commented Sep 12, 2024

Known workarounds

split it.

let arr = [|
    let arr = Array.groupBy id [||]
    for _ in arr do 0
|]

It looks like this only happens when the pat in for pat in expr … is the wildcard _. So another workaround (until this is fixed) is to give it a name (prefixed by an underscore if you don't want to use it).

This compiles:

let arr = [| for _group in Array.groupBy id [||] do 0 |]

@vzarytovskii
Copy link
Member

It's this LowerSimpleMappingsInComprehensionsToFastLoops feature which breaks it. I will have a PR in case if we won't have fix til monday (SDK lockdown), so we can quickly insert it last moment and then re-enable in 9.1

@sayurin
Copy link
Author

sayurin commented Sep 12, 2024

@brianrourkeboll wildcard _ is simplified case. pattern matching at for expression also causes FS2014.

// case 1: FS2014
let arr = [| for _ in Array.groupBy id [||] do 0 |]
// case 2: OK
let arr = [| for paired in Array.groupBy id [||] do 0 |]
// case 3: FS2014
let arr = [| for key, grouped in Array.groupBy id [||] do 0 |]

@brianrourkeboll
Copy link
Contributor

...The real problem is actually a very simple one: I neglected to bind srcArray to a compiler-generated local in #17067 🤦‍♂️.

That means that the source array expression is evaluated multiple times (🤦‍♂️🤦‍♂️🤦‍♂️):

let len = mkLdlen g mIn srcArray

and

let body = mkInvisibleLet mBody loopVal (mkAsmExpr ([ldelem], [], [srcArray; i], [loopVal.val_type], mBody)) body

The equivalent optimization for lists does not have this problem, because it was already binding srcList to a compiler-generated local:

let loop =
// let mutable current = enumerableExpr
mkLet spFor m currentVar srcList
// let mutable next = current.TailOrNull
(mkInvisibleLet mFor nextVar tailOrNullExpr
// while nonNull next do
(mkWhile g (spInWhile, WhileLoopForCompiledForEachExprMarker, guardExpr, body, mBody)))

Luckily, this is a very easy problem to fix.

Because I originally thought that this issue was related to pattern-matching and wildcards, I also found and have fixes for two issues related to those.

I'll be updating my PR once I have the baselines updated...

@brianrourkeboll
Copy link
Contributor

Thank you @sayurin for trying out the release candidate today and opening this issue so that I could fix it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants