dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.94k stars 788 forks source link

error FS0192: internal error: Error in pass3 for some F# code. #18066

Closed sylvainouel closed 3 hours ago

sylvainouel commented 5 days ago

Repro steps

Copy/paste the following program in F# interactive:

type A = {
    F: uint64  
}

type B = {
    C: uint64[]
}

let g x =
    let c = 
        [|
            for d in x.C do
                {
                    F = d
                }
        |]
    c

It produces the following error:

error FS0192: internal error: Error in pass3 for type FSI_0003, error: ldelem

Expected behavior

I think it should build fine.

Actual behavior

internal error

Known workarounds changing the uint64 to int64 fixes it.

Related information

It used to work, it stopped working after upgrading to latest VS.

.NET 9

brianrourkeboll commented 5 days ago

This seems like it could be related to some of my changes. I will try to take a look tomorrow.

T-Gro commented 5 days ago

This a regression from NET8, I also suspect the lowering changes being applied here. Even though the previous version was already optimized to Array.map, so it could be a different cause of the regression.

https://sharplab.io/#v2:DYLgZgzgPg9gDgUwHYAIDKBPCAXBBbAWACgBtAHgDkYBRARwFcBDYAS2wwG4qBhGPORgCcWEGEgB8AXWLtEKAIIoAvCgDexFJpQAxECnosk2AGwAWTcQC+xclTpNW7LjF78hIsVJkY5AIWVqGlrcegZGZiTSRNZExMAI2CgA5igAHspBmvGJAMYBmVqaJFAFhYVgMIIoACYohmkAdNw1MKVl7epE7d3d2gHVbT2aMT1QUYU5QA==

brianrourkeboll commented 5 days ago

Here's a more minimal repro:

[|for x in [|1UL|] -> x|]

The same thing applies for unativeint:

[|for x in [|1un|] -> x|]

The problem seems likely to be here

https://github.com/dotnet/fsharp/blob/95fe045af5b105c27ac401cc99a396c755031ae7/src/Compiler/Optimize/LowerComputedCollections.fs#L393-L394

and here

https://github.com/dotnet/fsharp/blob/95fe045af5b105c27ac401cc99a396c755031ae7/src/Compiler/Optimize/LowerComputedCollections.fs#L360-L374

I think we need to be emitting ldelem.i8 and ldelem.i for uint64 and unativeint, respectively. (And maybe likewise for stelem?)

Even though the previous version was already optimized to Array.map

That was an intermediate optimization introduced in #16948, but superseded in #17067 in part to fix debug stepping in the loop body. SharpLab still has a preview version of 9 that doesn't include #17067, #17419, #17711.

brianrourkeboll commented 4 days ago

Known workarounds changing the uint64 to int64 fixes it.

@sylvainouel Another workaround for the time being would be to set <LangVersion>8.0</LangVersion> in your project file.

brianrourkeboll commented 4 days ago

I opened a PR that I believe should fix this: #18081.

sylvainouel commented 2 hours ago

Thank you for this fix!