Skip to content

Implement interpolated strings via String.Concat#19971

Open
charlesroddie wants to merge 2 commits into
dotnet:mainfrom
charlesroddie:InterpolatedStringCollector
Open

Implement interpolated strings via String.Concat#19971
charlesroddie wants to merge 2 commits into
dotnet:mainfrom
charlesroddie:InterpolatedStringCollector

Conversation

@charlesroddie

Copy link
Copy Markdown

This PR implements comment, so that interpolated strings, where possible, are implemented via String.Concat. The benefits are:

  • Performance (again comment, with perf measurements)
  • Reflection-free, trimmable code that is NativeAOT compatible

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.

  1. The type of an interpolated string is either string or PrintfFormat<...>. The latter is an unfortunate addition designed to allow expressions like printf $"...", working around the lack of print functions (fsharp/fslang-suggestions#1092). This should be marked obsolete when print functions are added.
  2. Printf format expressions are allowed in holes. This creates:
    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.
  3. The rendering of strings was culture-dependent. This was likely unintentional since F# string functions like string are culture-independent. This is adjusted to match existing behaviour, using the string function. This is in keeping with similar changes that have moved towards string behaviour. (See the related discussion of string vs ToString behaviour in fsharp/fslang-suggestions#919.)
  4. The previous syntax tree was faithful, allowing a hole formatted by both a printf specifier and a .NET alignment/format — which the language does not allow — to be expressed. This is adjusted to the following:
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

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 through sprintf, 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:

  • Apply the behaviour in this PR unconditionally. This is what I would recommend as the behaviour change (matching string in culture-independence) is a fix to existing behaviour.
  • Restore the existing LowerInterpolatedStringToConcat to apply to the current feature. The name is actually closer to the current feature than the previous gated feature. This would require keeping legacy code paths which if you ask me to do, I would re-add to a file clearly marked obsolete to keep current code clean.
  • Add a new gate term. The ultra-bureaucratic solution.

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>
@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

✅ No release notes required

@github-actions github-actions Bot added ⚠️ Affects-Test-Tooling Tooling check: PR touches test framework infrastructure ⚠️ Affects-Compiler-Output Tooling check: PR touches IL emission or codegen ⚠️ Affects-Bootstrap Tooling check: PR touches compiler bootstrap chain ⚠️ Affects-Build-Infra Tooling check: PR touches build infrastructure labels Jun 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔍 Tooling Safety Check — Affects-Build-Infra, Affects-Bootstrap, Affects-Compiler-Output, Affects-Test-Tooling
Affects-Build-Infra: new .fsproj with PublishAot, custom compiler paths, Versions.props import
Affects-Bootstrap: modifies pars.fsy grammar and core compiler checking
Affects-Compiler-Output: changes interpolated string lowering from printf to String.Concat
Affects-Test-Tooling: new check.ps1/check.cmd scripts wired into AheadOfTime CI suite

Generated by PR Tooling Safety Check · opus46 5.8M ·

@charlesroddie charlesroddie force-pushed the InterpolatedStringCollector branch from e2debcd to f571df9 Compare June 18, 2026 21:51
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ Affects-Bootstrap Tooling check: PR touches compiler bootstrap chain ⚠️ Affects-Build-Infra Tooling check: PR touches build infrastructure ⚠️ Affects-Compiler-Output Tooling check: PR touches IL emission or codegen ⚠️ Affects-Test-Tooling Tooling check: PR touches test framework infrastructure

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

1 participant