dotnet / dotnet/fsharp

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

Open
#17,324 3 comments 2 reactions 0 assignees View on GitHub
Area-AOT Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

### 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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.