Skip to content

Add trim-safe ValidationContext constructor #113426

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
merged 6 commits into from
Mar 14, 2025
Merged

Conversation

captainsafia
Copy link
Member

Resolves #113134

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@captainsafia captainsafia requested review from eerhardt and removed request for Copilot March 12, 2025 17:48
@tarekgh
Copy link
Member

tarekgh commented Mar 12, 2025

Don't we need to modify the options source generator with this PR too?

OutLn($"var context = new {StaticValidationContextType}(options);");

OutLn($"[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026:RequiresUnreferencedCode\",");

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

Can you update this line?

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The ctors are marked with RequiresUnreferencedCode.")]
private string? GetDisplayName()

@eerhardt
Copy link
Member

Don't we need to modify the options source generator with this PR too?

It still supports netstandard2.0 right? So we would need to switch based off if the code was targeting net10.0+?

/// This constructor is trim-safe because it does not use reflection to resolve
/// the Type of the <paramref name="instance" /> to support setting the DisplayName.
/// </remarks>
public ValidationContext(object instance, string displayName, IServiceProvider? serviceProvider, IDictionary<object, object?>? items)
Copy link
Member

Choose a reason for hiding this comment

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

Which part of the other constructor forces it to have an RUC annotation? This seems to be replicating most of the statements present in the previous ctor.

Copy link
Member

Choose a reason for hiding this comment

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

This code is the reason why the other 2 constructors are marked RUC:

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The ctors are marked with RequiresUnreferencedCode.")]
private string? GetDisplayName()

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 updated the RUC messages here to be a little bit clearer about the fact that the issue is not providing a display name forces us down the path of resolving the type from the object instance which is not trim-safe.

Copy link
Member

Choose a reason for hiding this comment

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

Pretty wild that getting the display name was the pitfall here. Very cool to overcome that, @captainsafia!

@captainsafia
Copy link
Member Author

captainsafia commented Mar 13, 2025

It still supports netstandard2.0 right? So we would need to switch based off if the code was targeting net10.0+?

I updated the emitter to generate the different invocations behind an #ifdef. Alternatively, we could try to query for the TFM in the source generator itself and change what is emitted there but this felt easier.

Edit: bleh, need to hop on a devbox to update the netfx baselines

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @captainsafia!

@tarekgh tarekgh merged commit 520feeb into main Mar 14, 2025
84 checks passed
@jkotas jkotas deleted the cs/validation-context-api branch March 16, 2025 05:27
@github-actions github-actions bot locked and limited conversation to collaborators Apr 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Provide trim-safe overload for ValidationContext construction
5 participants