Skip to content

Port ComImportInterfaceAnalyzer to WinRT.SourceGenerator2#2355

Open
Sergio0694 wants to merge 2 commits intostaging/3.0from
port/com-import-interface-analyzer
Open

Port ComImportInterfaceAnalyzer to WinRT.SourceGenerator2#2355
Sergio0694 wants to merge 2 commits intostaging/3.0from
port/com-import-interface-analyzer

Conversation

@Sergio0694
Copy link
Member

Summary

Port the \ComImportInterfaceAnalyzer\ diagnostic analyzer from the legacy \WinRT.SourceGenerator\ project to \WinRT.SourceGenerator2, along with corresponding unit tests.

Changes

Analyzer (\WinRT.SourceGenerator2)

  • Added \CSWINRT2009\ diagnostic descriptor (\Warning\ severity) to \DiagnosticDescriptors.cs, warning when a cast targets a [ComImport]\ interface type that is not compatible with CsWinRT objects in AOT scenarios.
  • Created \ComImportInterfaceAnalyzer.cs\ in \Diagnostics/Analyzers, covering all cast patterns:
    • Explicit casts: (IC)obj\
    • \�s\ expressions: \obj as IC\
    • \is\ type checks: \obj is IC\
    • Declaration patterns: \obj is IC c\
    • Type patterns in list patterns: [IC c], [IC]\
  • Updated \AnalyzerReleases.Shipped.md\ with the new \CSWINRT2009\ rule.

Tests (\SourceGenerator2Test)

  • Added \Test_ComImportInterfaceAnalyzer.cs\ with two tests:
    • \ValidCast_DoesNotWarn: casts to non-[ComImport]\ interfaces produce no diagnostic.
    • \InvalidCast_Warns: casts to [ComImport]\ interfaces produce \CSWINRT2009\ for all supported patterns.

Notes

  • The legacy analyzer gated on \CsWinRTAotOptimizerEnabled\ and \EnableAotAnalyzer\ MSBuild properties. These are not applicable in the new source generator, so the analyzer runs unconditionally.

Sergio0694 and others added 2 commits March 19, 2026 16:10
Port the ComImportInterfaceAnalyzer diagnostic analyzer from the legacy
WinRT.SourceGenerator project to WinRT.SourceGenerator2. This analyzer
warns for casts to [ComImport] interfaces, which are not compatible
with CsWinRT objects in AOT scenarios.

Changes:
- Add CSWINRT2009 diagnostic descriptor to DiagnosticDescriptors.cs
- Add ComImportInterfaceAnalyzer in Diagnostics/Analyzers
- Update AnalyzerReleases.Shipped.md with the new rule

Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com>
Port the ComImportInterfaceAnalyzer unit tests to the SourceGenerator2Test
project, validating that:
- Casts to non-[ComImport] interfaces do not produce warnings
- Casts to [ComImport] interfaces produce CSWINRT2009 warnings for all
  supported patterns (explicit casts, 'as', 'is', declaration patterns,
  type patterns, and list patterns)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 requested a review from manodasanW March 19, 2026 23:16
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.

1 participant