Implement interpolated strings via String.Concat#19971
Conversation
A string-typed interpolated string is lowered to System.String.Concat of its parts rather than the reflection-based printf engine: a string-typed hole is passed through directly, any other plain hole is converted with `string x`, an aligned/formatted hole with `String.Format(InvariantCulture, ...)`, and a printf-specifier hole with `sprintf`. This removes the reflection dependency on the common path, so these interpolations become trim- and NativeAOT-compatible. This generalizes and replaces the language-version-gated String.Concat optimization (dotnet#16556), which only handled all-string holes: the lowering now applies to every string-typed interpolation, ungated. The reflection path is used only for PrintfFormat/FormattableString-typed interpolation. The syntax tree now carries each hole's formatting explicitly, so a printf specifier no longer leaks into an adjacent literal and alignment is no longer a fake tuple: type SynInterpolatedStringPart = | String of value: string * range: range | FillExpr of fillExpr: SynExpr * formatting: SynInterpolationFormatting type SynInterpolationFormatting = | DotNet of alignment: SynExpr option * format: Ident option | Printf of specifier: string * range: range Behavioural change: plain `{x}` holes now render with invariant culture (the F# `string` operator) rather than the current thread culture, matching `string`. Adds a NativeAOT regression test under tests/AheadOfTime/NativeAOT. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
✅ No release notes required |
|
🔍 Tooling Safety Check — Affects-Build-Infra, Affects-Bootstrap, Affects-Compiler-Output, Affects-Test-Tooling
|
e2debcd to
f571df9
Compare
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| // print $"answer = %d{x}" | ||
| // print $"hello %s{name}" |
There was a problem hiding this comment.
%d and %s (unlike %.2f below) serve effectively only as a type annotation. Would it be difficult to identify this, strip the specifiers, and have these 2 lines also be lowered to concat?
There was a problem hiding this comment.
I assume the list is https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/plaintext-formatting, minus %a and %t.
The instructions for clean interpolated string usage could be "avoid format strings (Printf-style specifiers); use dotnet specifiers instead".
This could be modified to add "except you can use this whitelist" by doing what you suggest. It makes the instruction more complex so I wouldn't recommend that.
It could also be modified to "avoid using the %A printf-style format specifier" by working systematically through the list.
If these printf-style usages are important then going the whole way would be best.
There was a problem hiding this comment.
I believe that the current implementation of the feature does already convert $"hello %s{name}" into string.Concat, so changing it so that it doesn't isn't an improvement?
There was a problem hiding this comment.
@Numpsy, it applies only if every single hole is a string.
@charlesroddie, I was wrong in my assumption, it's not always just a type annotation :/
> $"{true}";;
val it: string = "True"
> $"%b{true}";;
val it: string = "true"Guess we could still do it by replicating the run-time logic of printf, but maybe leave it out of this PR.
There was a problem hiding this comment.
it applies only if every single hole is a string
Yes, my point was that the %s{name} case currently compiles into string.concat and doesn't produce any AOT warnings, but the comment here says it will produce warnings.
This PR implements comment, so that interpolated strings, where possible, are implemented via
String.Concat. The benefits are:There are some problems with the current implementation, and this PR takes a moderate approach, resolving some of them but retaining others to keep a broadly backwards-compatible implementation.
stringorPrintfFormat<...>. The latter is an unfortunate addition designed to allow expressions likeprintf $"...", working around the lack ofprintfunctions (fsharp/fslang-suggestions#1092). This should be marked obsolete whenprintfunctions are added.a) The parser handled these only partially: it left the specifier (e.g.
%f) inside the preceding string component, leaving the compiler step to locate and process it. In this PR the specifier is split off and attached to its hole in a parser helper instead. So it's improved but still messy.b) Any hole with these expressions goes through the previous reflection-based route.
stringare culture-independent. This is adjusted to match existing behaviour, using thestringfunction. This is in keeping with similar changes that have moved towardsstringbehaviour. (See the related discussion ofstringvsToStringbehaviour in fsharp/fslang-suggestions#919.)A NativeAOT test
A test under
tests/AheadOfTime/NativeAOT(wired into the Windows trimming CI job) AOT-publishes a program that uses interpolation. Plain and .NET-format holes ({x},{x:F2},{x,6}) are AOT compatible. Printf-format holes (%d{x}) still route throughsprintf, so they remain reflection-based and fail AOT with IL2026/IL2070/IL3050 — see the commented-out examples in the test.This would be a good place to add other currently AOT-incompatible expressions for similar future fixes.
Notes on the LowerInterpolatedStringToConcat feature flag
This work extends and supersedes the work under the LowerInterpolatedStringToConcat flag, an "optimization that lowers string interpolation into a call to concat iff there are at most 4 string parts and all fill expressions are strings".
The reason why this feature was gated is unclear since it's just an optimization, but it's unclear what to do with this gate here. Options:
stringin culture-independence) is a fix to existing behaviour.