Skip to content

Add ILLink coverage for unused virtual method annotations - #130640

Draft
jtschuster with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-ilink-warnings-unused-code
Draft

Add ILLink coverage for unused virtual method annotations#130640
jtschuster with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-ilink-warnings-unused-code

Conversation

Copilot AI commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

ILLink should validate virtual annotation mismatches only for retained implementations, avoiding IL2046/IL2092 warnings from unused code.

  • Coverage
    • Completely unused implementation
    • Type-only reference
    • Used implementation that must warn
    • Used interface with an unused sibling implementation

Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot July 13, 2026 19:44
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jul 13, 2026
Copilot AI changed the title [WIP] Fix ILLink warnings for virtual method annotations on unused code Add ILLink coverage for unused virtual method annotations Jul 13, 2026
Copilot AI requested a review from jtschuster July 13, 2026 19:46
Copilot AI review requested due to automatic review settings July 24, 2026 19:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new ILLink test case under Mono.Linker.Tests.Cases intended to ensure virtual/interface annotation mismatch validation only happens for implementations that are actually retained, avoiding IL2046/IL2092 noise from unused implementations.

Changes:

  • Introduces UnusedVirtualMethodAnnotations test coverage for four scenarios (fully unused impl, type-only reference, used impl must warn, used interface with unused sibling impl).
  • Uses ExpectedWarning to pin IL2046/IL2092 to the retained implementations while suppressing IL2026 at the call sites.
Comments suppressed due to low confidence (2)

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/UnusedVirtualMethodAnnotations.cs:49

  • Same as UnusedImplementation: the Roslyn analyzers validate virtual/interface annotation mismatches independent of trimming reachability, so analyzer-only IL2046/IL2092 expectations are needed here to avoid unexpected warnings under the class-level [ExpectedNoWarnings].
        class TypeOnlyImplementation : ITypeOnly
        {
            public void Method(Type type) { }
        }

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/UnusedVirtualMethodAnnotations.cs:82

  • Because [ExpectedNoWarnings] is applied at the test-case level, any analyzer-emitted mismatches for unused sibling implementations will be treated as unexpected. Add analyzer-only IL2046/IL2092 expectations for this unused implementation so the test can still validate the trimmer-specific reachability behavior without breaking analyzer runs.
        class UnusedImplementationOfPartiallyUsedInterface : IPartiallyUsed
        {
            public void Method(Type type) { }
        }

…unused implementations

Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 24, 2026 20:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because there is no eligible user to bill. To allow Copilot reviews on bot-authored pull requests, enable direct organization billing in your organization's Copilot settings.

Copilot AI requested a review from jtschuster July 24, 2026 20:25
Comment on lines +36 to +37
[UnexpectedWarning("IL2046", Tool.Analyzer, "https://github.com/dotnet/runtime/pull/130640")]
[UnexpectedWarning("IL2092", Tool.Analyzer, "https://github.com/dotnet/runtime/pull/130640")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot Update the issue/reason string for the UnexpectedWarnings to say "Analyzer does not track reachability"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated all 6 UnexpectedWarning reason strings to "Analyzer does not track reachability" in the latest commit.

…eachability'

Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 24, 2026 20:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because there is no eligible user to bill. To allow Copilot reviews on bot-authored pull requests, enable direct organization billing in your organization's Copilot settings.

Copilot AI requested a review from jtschuster July 24, 2026 20:35

class UnusedImplementation : IUnused
{
[UnexpectedWarning("IL2046", Tool.Analyzer, "Analyzer does not track reachability")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot this should be ExpectedWarning for the analyzer warnings

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed all three sites from UnexpectedWarning to ExpectedWarning with Tool.Analyzer.

public void Method(Type type) { }
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do the interfaces need DAM to repro this issue? I would leave this out (or at least add a simpler test without DAM on the interface).


class UnusedImplementationOfPartiallyUsedInterface : IPartiallyUsed
{
[UnexpectedWarning("IL2046", Tool.Analyzer, "Analyzer does not track reachability")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The testcase should demonstrate a scenario where ILLink produces unexpected warnings too

…IL2092 warnings

Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 27, 2026 15:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because there is no eligible user to bill. To allow Copilot reviews on bot-authored pull requests, enable direct organization billing in your organization's Copilot settings.

Copilot AI requested a review from sbomer July 27, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Infrastructure linkable-framework Issues associated with delivering a linker friendly framework

Projects

Status: No status
Status: No status

Development

Successfully merging this pull request may close these issues.

ILLink warns for virtual method annotations on unused code

4 participants