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# discriminated unions simple use raises AOT and trimming warnings on publish. #17324

Open
vzarytovskii opened this issue Jun 18, 2024 Discussed in #17323 · 3 comments
Open
Labels
Area-AOT Everything related to AOT Feature Improvement
Milestone

Comments

@vzarytovskii
Copy link
Member

vzarytovskii commented Jun 18, 2024

Discussed in #17323

Originally posted by @abklearnhere June 18, 2024
I asked this question on StackOverflow and was suggested that it should be discussed on GitHub in relation to #15980 . Here is the original issue:

I am creating an F#, .NET 8 (SDK 8.0.302) console app and publish as a single file with AOT, trimming. I defined a discriminated union and used it in few trivial functions. But this raises AOT and trimming warnings when I run dotnet publish command. Is this expected or am I missing something obvious? I am running:

dotnet publish -c Release -r win-x64 --self-contained true -p:PublishAot=true -p:PublishTrimmed=true -o ./publish

And the code is:

type AnyDU = X | Y

let f1 () = let unrelated = System.DateTime.Now in ()
let f2 () = let works = X in ()
let f3 () = let works = X in match works with | X -> 0 | _ -> 1
let f4 () = let unrelated = System.DateTime.Now in let fails = X in ()
let f5 () = let fails = X in if fails = X then 1 else 0

[<EntryPoint>]
let main _ =
    f1 () |> ignore // no warnings. unrelated.
    f2 () |> ignore // no warnings.
    f3 () |> ignore // no warnings.
    f4 () |> ignore // IL3053 and IL2104 warnings.
    f5 () |> ignore // IL3053 and IL2104 warnings.
    0

The fsproj file has following settings:

    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <IsAotCompatible>true</IsAotCompatible>
    <PublishAot>true</PublishAot>
    <PublishTrimmed>true</PublishTrimmed>

When I added <TrimmerSingleWarn>false</TrimmerSingleWarn> to fsproj, I got around 50 warnings mostly related to reflection and print formatting with warning codes: IL2055, IL2060, IL2067, IL2070, IL2072, IL2075, IL2080, IL3050.

Also, adding [<Struct>] attribute to the DU didn't help.

The example code is simplified. My real DU structure and usage is fairly complex. So enums instead of DUs is not an option.

So, am I missing something obvious or this is expected? Should I report this as an issue?

@vzarytovskii
Copy link
Member Author

@KevinRansom fyi

@charlesroddie
Copy link
Contributor

charlesroddie commented Jun 18, 2024

Currently you have to use the --reflectionfree switch to make F# trim-safe. This removes generated .ToString() methods. To get useful ToString() methods you then need to override these and manually write out code. This is what we do.

The reason for this is that the generated .ToString() methods are not properly compiled at present. A proposal to do so is in fsharp/fslang-suggestions#919 .

@abonie abonie added Feature Improvement Area-AOT Everything related to AOT and removed Needs-Triage labels Jun 24, 2024
@abklearnhere
Copy link

Thanks @vzarytovskii, @charlesroddie for creating #17324 and suggesting --reflectionfree. It did remove warnings and enabled me to try some more tests. I've shared some interesting findings and follow up queries in the original discussion #17323. Will be great to get a feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-AOT Everything related to AOT Feature Improvement
Projects
Status: New
Development

No branches or pull requests

4 participants