Repository navigation
Compiled ToStrings under -reflectionfree for DUs and Records #19976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
abonie
merged 38 commits into
dotnet:main
from
charlesroddie:DUsRecordsCompiledToStrings
Jul 30, 2026
Merged
Changes from 34 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
4e8c86c
Add a compiler intrinsic for the 'string' operator
charlesroddie 1d5769c
Generate a match-based ToString for unions under --reflectionfree
charlesroddie bfb3939
Extract mkStringConcat helper for arity-dispatched String.Concat
charlesroddie e5710fa
Fix generated union ToString for generic unions
charlesroddie d5712c8
Render union ToString fields like option (null -> "null")
charlesroddie 26f0e53
Tidy reflection-free union ToString tests
charlesroddie 87e6870
Add reflection-free ToString to Result and Choice
charlesroddie 956a4b0
Generate a single-line ToString for records under --reflectionfree
charlesroddie a021166
Update FSharp.Core surface-area baselines for Result/Choice ToString
charlesroddie 8eef294
Add release notes
charlesroddie 3754def
Generate a single-line ToString for anonymous records under --reflect…
charlesroddie a5f33ca
Test that a hand-written ToString override is kept under --reflection…
charlesroddie 7ed0d8d
Rename ToString generators for clarity
charlesroddie dd3d75c
Restore tabular layout for string_operator_info in TcGlobals
charlesroddie 1a5065e
Add reflection-free ToString tests for field shapes, structs, anon re…
charlesroddie 2c108ed
Add EmittedIL tests for reflection-free record and union ToString
charlesroddie 6a2199c
Generate reflection-free ToString in the augmentation phase
charlesroddie 474b9f9
Guard generated reflection-free ToString against deep-recursion overflow
charlesroddie e130e50
Test the reflection-free ToString deep-recursion guard
charlesroddie 6e276b1
revert ToString additions to fsharp.core types
charlesroddie e3c1a34
Remove stale FSharp.Core release note for the reverted Result/Choice …
charlesroddie 93ee8ce
Merge remote-tracking branch 'upstream/main' into DUsRecordsCompiledT…
charlesroddie dcb4fc3
Merge branch 'main' into DUsRecordsCompiledToStrings
charlesroddie c4debaf
Merge branch 'main' into DUsRecordsCompiledToStrings
charlesroddie 817e9de
Fix code formatting in IlxGen.fs (dotnet fantomas)
charlesroddie 770601f
Merge branch 'main' into DUsRecordsCompiledToStrings
charlesroddie 0616237
don't use quoted name
charlesroddie 3f710ee
int version of reflectionfree-printing doc
charlesroddie 432b137
doc tweaks
charlesroddie 1d6f442
Link release note to the printing doc and cover anonymous records
charlesroddie 4cae86a
test backticks
charlesroddie 46da7a2
Share the ToString recursion guard with anonymous records
charlesroddie 275b188
Add EmittedIL baselines for struct and anonymous record ToString
charlesroddie 39741d8
Merge remote-tracking branch 'upstream/main' into DUsRecordsCompiledT…
charlesroddie 39ca150
Fix empty anonymous record ToString rendering a doubled space
charlesroddie da3550b
tidy comment
charlesroddie 9ed5cc7
Merge branch 'main' into DUsRecordsCompiledToStrings
charlesroddie 2ccd69d
Merge remote-tracking branch 'upstream/main' into DUsRecordsCompiledT…
charlesroddie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Simple vs Reflection-based DU and Record printing | ||
|
|
||
| This document describes two modes for printing Discriminated Unions (DUs) and Records in F#: a **simple** reflection-free mode that delegates to a `string`-like operator for printing field values, and a `sprintf` mode (`sprintf "%A"`), which uses **reflection** to create output looking like F# code. In this document, the terms *simple* and *reflection* are used to distinguish the two modes. | ||
|
|
||
| Without the `--reflectionfree` flag, the compiler generates a `ToString` for DUs and Records that calls `sprintf "%A"`. With the flag, the compiler generates a `ToString` that uses the simple mode. | ||
|
|
||
| Users can choose between the two modes by 1. use of `--reflectionfree`, and by 2. calling with a `sprintf`-type caller or a `string`-type caller (e.g. the `string` operator, `ToString`, or interpolated strings). | ||
|
|
||
| If `x` is a DU or Record, then output will be simple or reflection-based as follows: | ||
| | | `--reflectionfree` | no `--reflectionfree` | | ||
| |---|---|---| | ||
| | `string x` | simple | reflection | | ||
| | `x.ToString()` | simple | reflection | | ||
| | `$"{x}"` | simple | reflection | | ||
| | `sprintf "%A" x` | disallowed (would be reflection) | reflection | | ||
|
|
||
| As such, the current default reflection `ToString` generation forces reflection formatting on all callers. On the other hand, generating simple `ToString` output means that the records and DUs are printed with simple or reflection formatting depending on whether the caller is of simple or reflection affinity. The `--reflectionfree` flag combines this property with a ban on `sprintf` to prevent the reflection mode from being used. | ||
|
|
||
| In addition to user-defined types, the FSharp.Core `option` type uses simple printing, while other types either have no `ToString` or use some other format. | ||
|
|
||
| ## Behaviour: definitions | ||
|
|
||
| In simple printing, field values are printed with `string`-type formatting, more precisely `anyToStringShowingNull`. No line breaks are inserted. | ||
|
|
||
| - **Record**: `{ Name1 = value1; Name2 = value2 }`. | ||
| - **Anonymous record**: the same, but with `{| ` and ` |}`. | ||
| - **Union**: A case with no fields renders as just its name. A case with fields renders as `CaseName(value1, value2)`. | ||
|
|
||
| `[<Struct>]` records and unions, and struct anonymous records, render identically to their reference-type forms. | ||
|
|
||
| A type that supplies its own `ToString` override keeps it, with no `ToString` generated for it (either simple or reflection). | ||
|
|
||
| Reflection-mode printing is described in [plain text formatting](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/plaintext-formatting). | ||
|
|
||
| ## Behavioural differences | ||
|
|
||
| ### Differences in field rendering | ||
|
|
||
| The following differences between `string` and `sprintf "%A"` carry over directly into differences in field rendering between simple and reflection printing: | ||
|
|
||
| | F# value | simple (`anyToStringShowingNull`) | reflection (`sprintf "%A"`) | | ||
| |---|---|---| | ||
| | string field `"hi"` | `hi` | `"hi"` | | ||
| | char field `'a'` | `a` | `'a'` | | ||
| | float `5.0` | `5` | `5.0` | | ||
| | `250uy` / `42n` / `1.5M` | `250` / `42` / `1.5` | `250uy` / `42n` / `1.5M` | | ||
| | option field `None` | `null` | `None` | | ||
| | array field `[\|1;2;3\|]` | `System.Int32[]` | `[\|1; 2; 3\|]` | | ||
| | unit field `()` | `null` | `()` | | ||
|
|
||
| The overall differences here are: | ||
| - Simple printing converts to strings, while reflection printing is more bi-directional, often generating compilable F# code. | ||
| - F# types that have null representation (`unit`, `option`, and in general types with `AllowNullLiteral` or `UseNullAsTrueValue`) are printed as `null` in simple printing, while reflection printing uses a more F#-like representation. | ||
|
|
||
| ### Other differences | ||
|
|
||
| These differences are in the printing of the record or DU itself rather than of its fields: | ||
|
|
||
| | F# value | simple (`string`) | reflection (`sprintf "%A"`) | | ||
| |---|---|---| | ||
| | `B 5` (single field) | `B(5)` | `B 5` | | ||
| | `C (3, 4)` (two fields) | `C(3, 4)` | `C (3, 4)` | | ||
| | record `{ X = 1; Y = 2 }` | `{ X = 1; Y = 2 }` | `{ X = 1`⏎` Y = 2 }` | | ||
| | `[<StructuredFormatDisplay("Custom<{X}>")>]` | `{ X = 5 }` | `Custom<5>` | | ||
|
|
||
| The overall differences here are: | ||
| - Simple printing always brackets a case's fields and never pads, while reflection printing omits brackets for a single non-tuple field and inserts a space before them otherwise. | ||
| - Simple printing uses a single line (unless a field's own rendering contains breaks), while reflection printing breaks records and nested values across lines with indentation. | ||
| - `StructuredFormatDisplay` is ignored in simple printing and honoured in reflection printing. | ||
|
|
||
| ## Recursion and depth | ||
|
|
||
| Rendering recurses into nested records and unions. Deep nesting is guarded by `RuntimeHelpers.EnsureSufficientExecutionStack`, raising a catchable `InsufficientExecutionStackException` rather than `StackOverflowException`; cycles (which require mutation to construct) still overflow, as `option` and `list` do. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.