Skip to content
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

Annotating Regex library for Native Aot apps #67841

Merged
merged 3 commits into from
Apr 12, 2022

Conversation

LakshanF
Copy link
Member

The RequiresDynamicCode attribute target scope change got approved and creating a new PR with this change for Regex library annotation

@ghost
Copy link

ghost commented Apr 11, 2022

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Issue Details

The RequiresDynamicCode attribute target scope change got approved and creating a new PR with this change for Regex library annotation

Author: LakshanF
Assignees: LakshanF
Labels:

area-System.Text.RegularExpressions

Milestone: -

@@ -201,6 +203,7 @@ protected internal static void ValidateMatchTimeout(TimeSpan matchTimeout)
/// Regex constructor, we don't load RegexCompiler and its reflection classes when
/// instantiating a non-compiled regex.
/// </summary>
[RequiresDynamicCode("The native code for Regex compilation might not be available at runtime.")]
Copy link
Member

@stephentoub stephentoub Apr 11, 2022

Choose a reason for hiding this comment

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

Why is this one needed? It's only called from the above internal Regex constructor that's attributed as unconditional suppression. The analyzer doesn't then just ignore everything below that point?

(Same question for all the annotations after this one)

Copy link
Member

Choose a reason for hiding this comment

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

The analyzer doesn't then just ignore everything below that point?

No, the analyzer doesn't do "cross-method" analysis. It just analyzes the current method.

In a perfect world, the UnconditionalSuppressMessage attribute on the internal Regex constructor shouldn't even be needed because it only calls this method behind a check for RuntimeFeature.IsDynamicCodeCompiled. The analyzer should be able to tell that the method that "RequiresDynamicCode" doesn't ever get invoked when dynamic code isn't available.

Copy link
Member

Choose a reason for hiding this comment

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

In a perfect world, the UnconditionalSuppressMessage attribute on the internal Regex constructor shouldn't even be needed because it only calls this method behind a check for RuntimeFeature.IsDynamicCodeCompiled. The analyzer should be able to tell that the method that "RequiresDynamicCode" doesn't ever get invoked when dynamic code isn't available.

Right, we talked about that on the other PRs/issues. I was asking here about, short of recognizing IsDynamicCodeCompiled checks in that manner, it could do the same for UnconditionalSuppressMessage. Both would be valuable, and presumably both require the same level of cross-method analysis (recursively suppressing diagnostics on methods only callable from call sites suppressed via either a guard check or attribute).

Copy link
Member

Choose a reason for hiding this comment

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

and presumably both require the same level of cross-method analysis

The check I'm taking about shouldn't require the same level of cross-method analysis. We would still annotate the Compile method as RequiresDynamicCode. The analyzer wouldn't need any more cross-method analysis than it does today - it just looks at the method being called and looks at its attributes. This would be the same behavior as the SupportedOSPlatform analyzer we have today - it doesn't warn about code inside if (OperatingSystem.IsWindows()) that calls windows-only code.

Copy link
Member

@stephentoub stephentoub Apr 11, 2022

Choose a reason for hiding this comment

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

We would still annotate the Compile method as RequiresDynamicCode

And we don't want to auto-propagate annotations up the call chain because... it's too expensive for the analyzer?

We're pushing onto developers work that the tool could do, it seems we're just choosing not to (presumably for a good reason, like it would be too slow).

Copy link
Member

Choose a reason for hiding this comment

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

I also don't think it's a good UX

That may be. I think the same thing about having to add a bunch of attributes to suppress warnings that, from my perspective, are bogus.

Copy link
Member

Choose a reason for hiding this comment

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

Also, the point of the requires attributes is also to tell developers -- these methods have special requirements. It's an API demand, so it should be visible as part of the API of the method.

Copy link
Member

Choose a reason for hiding this comment

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

It's an API demand, so it should be visible as part of the API of the method.

For exposed API, sure. None of the ones in this PR are.

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 opened an issue to track this, dotnet/linker#2740

Copy link
Member

Choose a reason for hiding this comment

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

Propagating these across methods is problematic because e.g. these annotations need to be consistent across virtuals (if base is not annotated, override cannot be annotated)

It's hard to imagine a good UX around situations when the propagation stops working at some point in the static callgraph. There can be big ripple effects. Also scenarios like adding InternalsVisibleTo to the assembly popping up warnings, etc.

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.

LGTM. Thanks!

…egularExpressions/Regex.cs

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
@LakshanF LakshanF merged commit 623b187 into dotnet:main Apr 12, 2022
@LakshanF LakshanF deleted the AotRegexAnnotation branch April 12, 2022 21:40
@ghost ghost locked as resolved and limited conversation to collaborators May 13, 2022
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.

7 participants