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.82k stars 772 forks source link

F# discriminated unions simple use raises AOT and trimming warnings on publish. #17324

Open vzarytovskii opened 1 week ago

vzarytovskii commented 1 week ago

Discussed in https://github.com/dotnet/fsharp/discussions/17323

Originally posted by **@abklearnhere** June 18, 2024 I asked [this question](https://stackoverflow.com/questions/78623078/f-discriminated-unions-simple-use-raises-aot-and-trimming-warnings-on-publish) 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: ```powershell dotnet publish -c Release -r win-x64 --self-contained true -p:PublishAot=true -p:PublishTrimmed=true -o ./publish ``` And the code is: ```fsharp 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 [] 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: ```xml Exe net8.0 true true true ``` When I added `false` 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 `[]` 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 commented 1 week ago

@KevinRansom fyi

charlesroddie commented 1 week ago

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 .

abklearnhere commented 5 days ago

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!