F# discriminated unions simple use raises AOT and trimming warnings on publish.
Discussed in https://github.com/dotnet/fsharp/discussions/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?
@KevinRansom fyi
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 https://github.com/fsharp/fslang-suggestions/issues/919 .