Skip to content

Don't emit DynamicDependency under --reflectionfree - #21012

Open
charlesroddie wants to merge 2 commits into
dotnet:mainfrom
charlesroddie:reflectionfree-no-dynamic-dependency
Open

charlesroddie wants to merge 2 commits into
dotnet:mainfrom
charlesroddie:reflectionfree-no-dynamic-dependency

Conversation

@charlesroddie

@charlesroddie charlesroddie commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

The compiler puts [DynamicDependency] on record, anonymous record and union constructors (#14941, #15077, #15079) so that %A formatting keeps working after trimming. Under --reflectionfree, %A is a compile error and these types get a generated ToString (#19976), so the attributes have no purpose in the compiling assembly.

This PR stops emitting the attribute when --reflectionfree is set. Output without the flag is unchanged.

In a NativeAOT evaluation of a large reflection-free F# application, removing these attributes reduced the app size by 5.7% (1.7MB of 30.2MB).

Behaviour change

Under trimming or NativeAOT, code elsewhere that reflects over these types (%A from another assembly, FSharp.Reflection, reflection-based serializers) may stop working, but 1. this seems clearly correct given the name reflectionfree, and 2. such code is unlikely to be found in trimming/NativeAOT environments.

Testing

CompilerOptions.Fsc.ReflectionFree and EmittedIL.ReflectionFreeToString pass on net11.0 (24 tests). Two are new: the attribute is absent with --reflectionfree and present without it, across records, struct records, unions, nullary unions, struct unions and both kinds of anonymous record.

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated

Record, union and anonymous record constructors carry [DynamicDependency]
so that %A formatting survives trimming. %A is a compile error under
--reflectionfree, so there the attributes only make trimming and NativeAOT
keep the types' fields and properties reflection-visible.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@charlesroddie
charlesroddie marked this pull request as ready for review November 22, 2026 09:24
@charlesroddie
charlesroddie requested a review from a team as a code owner November 22, 2026 09:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

1 participant