Mark backing fields as CompilerGenerated (fixes serialization of fields in FSI multiemit) #13494
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.
This adds CompilerGeneratedAttribute to the backing fields used for records and anon records. These fields should have always been labelled compiler generated: they are not intended for direct use from C# or other languages and have compiler-generated names.
This only really matters when these fields are public, as CompilerGenerated is, for example, an indication to serialization frameworks to ignore the field. But it's reasonable to set it even for fields that are private.
For example this fixes #13493. When F# Interactive multiemit is on, some backing fields need to become public at the IL level because they can be accessed by any number of later assembly fragments. This confuses serialization frameworks which see a public field and serialize it in addition to the public properties. Marking the backing fields as CompilerGenerated has the effect of making the serialization frameworks ignore the field, which is correct.
The tests cover roundtrip serialization/deserialization for records and anonymous records using both Newtownsoft and System.Text.Json. We use the "printing" tests as this is really the only tests where we force multiple fragments into F# Interactive, thus testing multi-emit.