Skip to content

Conversation

@eerhardt
Copy link
Member

There are a couple patterns here:

cc @AArnott

FYI - @agocke @MichalStrehovsky - would you mind checking the suppressions I needed to add here? The biggest one is the GetInterfaceMap issue.

…bility.

There are a couple patterns here:

- Use GetMemberWithSameMetadataDefinitionAs to reflect on generic types/methods in a statically verifiable fashion. This method only exists in .NET 6+.
- Suppress warnings when using GetInterfaces when the Type is annotated as preserve 'All'. dotnet/linker#1731
- When looking for a hard-coded interface like IProgress<T>, any preserved Type that implements the interface won't have the interface trimmed.
- MethodNameMap uses GetInterfaceMap, which may not work in all cases. See dotnet/runtime#89157
#pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Using the Json source generator.")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Using the Json source generator.")]
IJsonRpcMessageFormatter CreateFormatter() => new SystemTextJsonFormatter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way we could solve this in JsonSerializer? I get scared about suppressions of library code as it relies on both trimming details and the precise details of the library. Those things can easily drift over time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in order to do this "right" we will need to make some form of SystemTextJsonFormatter (either a whole new class, or a new "trim/aot safe" constructor) that is guaranteed to work consistently if you use the source generator. As it stands, the existing SystemTextJsonFormatter class has methods that will generate IL on the fly, and do other incompatible behaviors.

At least for now the SystemTextJsonFormatter is marked as RUC and RDC, so callers get warnings and can learn about what isn't compatible.

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, I share Andy's concerns in general. I think this is not the first place where we had to suppress warnings about accessing interfaces lists and looking for a generic instantiation of a type we know exists. Maybe another new API candidate so that it can be done without suppressions.

Copy link
Member

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, pending suggestions for updates.

eerhardt added 2 commits June 18, 2025 17:53
- Use for loop to avoid Roslyn analyzer warning.
- Condition the suppression on !NET10.
@eerhardt
Copy link
Member Author

Thanks for the feedback. I believe I've addressed it all, if you want to take a relook.

@AArnott AArnott merged commit e89f197 into microsoft:NativeAOT Jun 19, 2025
1 of 2 checks passed
@eerhardt eerhardt deleted the NativeAOT_eerhardt2 branch June 19, 2025 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants