Skip to content

Conversation

@jtschuster
Copy link
Member

Fixes #2512

Adds checks to ensure that method overrides have the same DAM annotations as the virtual methods / interfaces. Warning on the implicit 'this' meant I had to add annotations to the test class that inherited from Type and update the expected warnings related to that class.

I found that the when there is a mismatch between DAM on a property and on the accessor, the linker will warn on the accessor, but use that DAM annotation instead of the property's. I think it would make more sense to warn on the property, so the analyzer warns there, and I filed #2654 to track the issue with the linker.

Also created a helper to find all the interface members that a type implements, which also takes into account any interfaces an interface recursively implements, and updated the RequiresAnalyzer to use that helper.

Finally, updated the checks for LogContains and LogDoesNotContain in the analyzer tests to support Regex matches and deduplicated some code there.

@vitek-karas
Copy link
Member

I think it would make more sense to warn on the property, so the analyzer warns there, and I filed #2654 to track the issue with the linker.

I agree that it would make more sense, the question is if it's worth the change. If there's an existing suppression, fixing this will break that suppression. I'm not sure the change is important enough to risk a break like that.

Comment on lines 268 to 276
var methodParamHasDam = methodTypeParam.TryGetAttribute (DynamicallyAccessedMembersAttribute, out var methodTypeParamAttribute);
var overriddenParamHasDam = overriddenTypeParam.TryGetAttribute (DynamicallyAccessedMembersAttribute, out var overriddenTypeParamAttribute);
if ((methodParamHasDam && overriddenParamHasDam && !DamAttributesMatch (methodTypeParamAttribute!, overriddenTypeParamAttribute!))
|| (methodParamHasDam ^ overriddenParamHasDam)) {
context.ReportDiagnostic (Diagnostic.Create (
DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.DynamicallyAccessedMembersMismatchOnGenericParameterBetweenOverrides),
methodTypeParam.Locations[0],
methodTypeParam.GetDisplayName (), method.GetDisplayName (), overriddenTypeParam.GetDisplayName (), overriddenMethod.GetDisplayName ()));
}
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to refactor this to share the code between the four places? The condition and warnings are nearly identical.

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 tried to, and for me the slight differences in each case are just enough so that anything I tried ended up adding complexity without reducing repetition.

Copy link
Member

@vitek-karas vitek-karas left a comment

Choose a reason for hiding this comment

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

Some minor comments, but looks good otherwise.

Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

LGTM, only a few nits. Thank you!

@jtschuster jtschuster merged commit ac6cfb3 into dotnet:main Mar 11, 2022
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* Warn on DAM mismatch between overrides

Commit migrated from dotnet/linker@ac6cfb3
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.

[DAM analyzer] Warn about mismatching virtual annotations

3 participants