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.89k stars 783 forks source link

`sprintf "%b" bool` causes AOT failure #17691

Open Smaug123 opened 1 month ago

Smaug123 commented 1 month ago

sprintf "%b" true does not work in AOT context; nor does sprintf "%O" true. This affects linux-x64 8.0.401 and v9 preview 1, but not osx-arm64 8.0.300, in my testing.

Repro steps

In an executable with <PublishAot>true</PublishAot>:

let s = sprintf "%O" true // or "%b"
System.Console.WriteLine s

Expected behavior

Program prints "true".

Actual behavior

System.NotSupportedException: 'Microsoft.FSharp.Core.PrintfImpl+Specializations`3[Microsoft.FSharp.Core.Unit,System.String,System.String].CaptureFinal1[System.Boolean](Microsoft.FSharp.Core.PrintfImpl+Step[])' is missing native code.

Known workarounds

(fun s -> $"%O{s}") true works, as does x.ToString () |> sprintf "%s".

Related information

Reproduced on linux-x64 with net8.0.401 and net9 preview 7, but not osx-arm64 8.0.300.

charlesroddie commented 2 weeks ago

Do you have need for sprintf in preference to interpolated strings? It would be better to focus effort on improving interpolated strings (by making them work via a string collector) than working on sprintf which is generally unsalvageable.

Smaug123 commented 2 weeks ago

Yeah, I write source generators and am not really willing to break compatibility for my (completely uncontrolled) consumers. I could of course parameterise the generators or do some work to make available to them the current version of F#, or more practically I could just accept that there are bugs and manually call ToString on everything; that's certainly not the end of the world.

charlesroddie commented 1 week ago

F# 5.0 with string interpolation was released 2020. Can't imagine why anyone would be using previous versions at this point.

Anyway the options are:

majocha commented 1 week ago

F# 5.0 with string interpolation was released 2020. Can't imagine why anyone would be using previous versions at this point.

Partial application is one thing you can't do with interpolated formatting.