F# discriminated unions simple use raises AOT and trimming warnings on publish. #17323
Unanswered
abklearnhere
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Thanks @vzarytovskii, @charlesroddie for creating #17324 and suggesting I could see my old+new tests fell in following categories:
Now, I have further questions -
Below is the complete code example: // Test 1: Du simple usage.
type AnyDU = X | Y
// Test 1
let f0 () = sprintf "f0: no fmt specifier."
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
// Test 2: Use augmented DU members.
module augmem =
type DuHasMem =
| A
| B of string
member _.f6 = System.DateTime.Now
member x.f7 () = "simple"
member x.f8 z = if z > 42 then "everything" else "something"
member x.f9 () = match x with A -> "this is A" | B s -> "this is B "+ s
type AnyDU with
member x.f10 = System.DateTime.Now
member x.f11 () = "simple"
member x.f12 z = if z > 42 then "everything" else "something"
member x.f13 () = match x with X -> "this is X" | Y -> "this is B "
let x, b = AnyDU.X, DuHasMem.B "random_string" // creating a DU instance after it's augmentation raises warnings.
let f6_thru_f13 () = augmem.x // function need not be called, it just references DU instance created at module level.
// Test 3: ToString and related.
type B2S =
| Is of bool
static member t = Unchecked.defaultof<B2S>
override x.ToString() = "Hello!"
member x.f14 () = "f14: " + "string const; no ref to DU instance."
member x.f15 () = "f15: " + match x with | Is true -> "B2S.Is true" | _ -> "B2S.Is false"
member x.f16 () = "f16: " + x.ToString()
member x.f17 () = "f17: " + string x
member x.f18 () = "f18: " //+ sprintf "The value is %A" x
member x.f19 () = "f19: " + $"The interpolated value is {x}"
member x.f20 () = "f20: " + "nameof: " + nameof(B2S) + " " + nameof(x) + "." + nameof (B2S.t.f20)
member x.f21 () = "f21: " + System.String.Format("Good old way, value is {0}", x)
member x.f22 () = "f22: " + sprintf "The value is %O" x
let bs = B2S.Is true
let f14 () = bs.f14()
let f15 () = bs.f15()
let f16 () = bs.f16()
let f17 () = bs.f17()
let f18 () = bs.f18()
let f19 () = bs.f19()
let f20 () = bs.f20()
let f21 () = bs.f21()
let f22 () = bs.f22()
// Test 4: lazy, seq-containing-try-with, async-await
let f23 () = let z = lazy (12345) in z.Force()
let f24 () = seq { try ignore (1/0); 111 with e -> 222 }
let f25 () =
let mutable c = 0
let asyncFn = async { c <- c+1 }
async {
do! asyncFn
do! System.Threading.Tasks.Task.Delay 1000 |> Async.AwaitTask
do! asyncFn
return c
} |> Async.RunSynchronously
[<EntryPoint>]
let main _ =
// no warnings regardless of --reflectionfree.
f1 () |> ignore
f2 () |> ignore
f3 () |> ignore
f14() |> System.Console.WriteLine
f15() |> System.Console.WriteLine
f20() |> System.Console.WriteLine
f25() |> System.Console.WriteLine
// warnings removed only if --reflectionfree
f4 () |> ignore
f5 () |> ignore
f6_thru_f13 () |> ignore
f16() |> System.Console.WriteLine
f17() |> System.Console.WriteLine
f21() |> System.Console.WriteLine
// fails to compile if --reflectionfree, else IL3053 and IL2104 warnings.
f18() |> System.Console.WriteLine
// warnings regardless of --reflectionfree, IL3053 and IL2104.
f0 () |> System.Console.WriteLine
f19() |> System.Console.WriteLine
f22() |> System.Console.WriteLine
f23() |> System.Console.WriteLine
f24() |> Seq.head |> System.Console.WriteLine
0
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
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:And the code is:
The
fsproj
file has following settings:When I added
<TrimmerSingleWarn>false</TrimmerSingleWarn>
tofsproj
, 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?
Beta Was this translation helpful? Give feedback.
All reactions