Repository navigation
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
@sbomer could you help review this? Also, could we backport it to 10.0.4xx or 10.0.5xx? Thank you! 🙂 |
There was a problem hiding this comment.
🟢 Approval recommended
Pull request overview
This PR updates the PlatformCompatibility analyzer (CA1416/related) to avoid two categories of spurious diagnostics: (1) operations inside members annotated with [SupportedOSPlatformGuard]/[UnsupportedOSPlatformGuard] no longer lose the platform context coming from the containing type/assembly, and (2) references that occur only as the operand of nameof/NameOf are no longer treated as real API accesses.
Changes:
- Skip platform-compatibility analysis for operations that occur under an
INameOfOperation(sonameof(SomePlatformSpecificMember)won’t warn). - When establishing the call-site platform context for an operation, fall back to the containing symbol’s container if the containing member is a guard.
- Add/adjust unit tests to cover the guard-member-context preservation and the
nameofbehavior change (including an obsoleted-platform case).
File summaries
| File | Description |
|---|---|
| src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs | Implements the analyzer behavior changes for guard-member call-site context and skipping operations under nameof. |
| src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.GuardedCallsTests.cs | Adds regression tests ensuring guard-member bodies retain container/assembly platform context and still warn when appropriate. |
| src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.cs | Updates an existing nameof-related expectation and adds new C#/VB tests ensuring nameof does not warn while real access still does. |
| src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.ObsoletedOSPlatformTests.cs | Adds a regression test ensuring nameof does not trigger obsoleted-platform diagnostics. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Lite
Evangelink
left a comment
There was a problem hiding this comment.
Note
This review comment was generated with AI assistance.
Found one analyzer-correctness issue in the guard-member context change.
Members annotated with [SupportedOSPlatformGuard] lost the platform context inherited from their containing type or assembly, so every platform specific API used inside them was reported as reachable on all platforms. Operations under a nameof expression were also treated as real API references, even though nameof only captures the name at compile time. Fixes dotnet#54066 Co-authored-by: Copilot App <[email protected]>
A guard member sheds the platform requirements of its containing type so that it can be referenced from any call site, so its body cannot rely on them either. Falling back to the immediate container meant neither the reference nor the body was diagnosed. Fall back to the assembly instead, whose requirements bind every call site within it. Co-authored-by: Copilot App <[email protected]>
7e51853 to
dc220e3
Compare
Cover both guard attributes in the assembly-context and neutral-call-site tests, add the nested VB NameOf case, and document the paired guard attribute handling. Co-authored-by: Copilot App <[email protected]> Copilot-Session: cfef23d8-6258-4113-8295-d666bd027fe7
|
@tannergooding thank you for the review! I don't have permissions to merge, could you help? 😄 |
|
/backport to release/11.0.1xx |
|
Started backporting to |
Fixes #54066
Two things made CA1416 fire when it shouldn't.
Guard members lost their call site platform context. The analyzer computes the platform attributes of a symbol by walking up the containing chain and merging them, and it clears everything it found when the symbol carries a guard attribute. That is intentional, because referencing a guard member should never warn. The problem is that the same result is also used as the call site context for the symbol whose body is being analyzed, so as soon as a member was annotated with
[SupportedOSPlatformGuard]it also lost the context coming from the assembly level[SupportedOSPlatform]that the SDK generates fromSupportedOSPlatformVersion. Everything inside that member was then reported as reachable on all platforms.The call site lookup now falls back to the assembly when the containing member is a guard. It deliberately stops there rather than using the containing type: a guard sheds its type's requirements so that callers on any platform can reference it, so its body cannot rely on them either. Assembly wide requirements are different, since every call site inside the assembly is bound by them regardless.
nameofwas treated as a real API reference. Anameofoperand is a compile time constant and never touches the member, and it is lowered out of the control flow graph, so platform guards around it did not suppress the diagnostic either. Operations under anameofare now skipped.The second part is a behavior change:
nameof(SomePlatformSpecificProperty)used to report CA1416 and no longer does. That was already inconsistent, since the same expression did not warn when the attribute sat on the accessor rather than on the property, andGetPropertyAccessorsalready documents thatValueUsageInfo.Nameonly uses the name of the property. One existing test line was updated for this.Both cases were reproduced as failing tests first, and each new test was confirmed to fail without the corresponding change. The full
Microsoft.CodeAnalysis.NetAnalyzers.UnitTestssuite passes.