Repository navigation
Conversation
Decode OperatingSystem.IsApplePlatform as macOS, MacCatalyst, iOS, or tvOS instead of a fictitious ApplePlatform OS. Preserve existing builtin guard registration and flow-analysis negation. Add C# and VB regressions for allow lists, deny lists, each Apple platform, Windows, watchOS, and negation. Document builtin guard test setup. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ee6e48ad-6898-45d7-98ed-9850afb3bd30
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Restore AGENTS.md to the original baseline as requested, preserving IsApplePlatform analyzer changes and regression tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ee6e48ad-6898-45d7-98ed-9850afb3bd30
5 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Teach the platform compatibility analyzer (CA1416) to recognize
OperatingSystem.IsApplePlatform()as a guard for the union of these four platforms:This supports the public API introduced by dotnet/runtime#135475. The current runtime implementation does not include watchOS, and the analyzer follows that definition.
This change is entirely within the analyzer and its tests. It does not require changes to the runtime implementation, reference API, or platform guard attributes.
Problem
The analyzer already discovers
OperatingSystem.IsApplePlatform()through its existing convention for static, parameterless Boolean methods namedIs*onSystem.OperatingSystem.However, the existing decoder assumes that each such method represents one platform. It removes the
Isprefix and therefore interprets this method as a check for a fictitious platform namedApplePlatform, rather than a check for four actual platforms.This can produce both false positives and false negatives:
IsApplePlatform()guard becauseApplePlatformdoes not match its supported platform annotations.[UnsupportedOSPlatform("ios")]can incorrectly have its warning suppressed because the fictitiousApplePlatformis treated as different fromios, even though the guard can be true on iOS.Calls to APIs annotated only with
[UnsupportedOSPlatform("windows")]can already appear to work with the old decoder. That result is coincidental: it does not demonstrate that the analyzer understands the Apple platform set.Implementation
Add an
IsApplePlatformmethod-name constant and special-case this method in the parameterless branch ofPlatformMethodValue.TryDecode, before the existing single-platform name extraction.The decoder emits four unversioned, non-negated
PlatformMethodValueinstances:The existing operation visitor merges these values with OR semantics. Existing control-flow analysis also handles negation, so neither the operation visitor nor the guard discovery logic needs to change.
The resulting behavior is:
!IsApplePlatform()condition, excludes all four Apple platforms.The production changes are limited to:
PlatformCompatibilityAnalyzer.cs: add the method-name constant.PlatformCompatibilityAnalyzer.Value.cs: decode the aggregate guard into four platform values.FilterPlatformCheckMethodsandPlatformCompatibilityAnalyzer.OperationVisitor.csremain unchanged. No Unix platform-family mapping or OS-version behavior is introduced.Regression test setup
The existing test helpers use .NET 6 reference assemblies, which do not expose
OperatingSystem.IsApplePlatform(). The new tests therefore provide a source stub with the exactSystem.OperatingSystemtype name and a static, parameterless BooleanIsApplePlatformmethod.The stub intentionally has no platform guard attributes. This ensures that the tests exercise the built-in guard discovery and decoding path changed by this PR, rather than the separate custom guard attribute path.
The method body throws
NotImplementedException. Test source is compiled and analyzed, not executed; no runtime platform detection is involved.The tests also explicitly configure:
The target framework metadata enables platform analysis under the existing analyzer rules.
_SupportedPlatformListensures that the unsupported-platform annotations used by the tests are retained for analysis instead of being filtered out beforehand.Including watchOS in this test configuration is intentional: it is a negative control that verifies watchOS is not part of the Apple guard. It does not add watchOS support to the runtime or the decoder.
Each
[|...|]marker identifies a call that must produce CA1416. Calls without markers must remain free of unexpected diagnostics. The tests therefore check both required warnings and required suppression.Test coverage
Three test methods produce six executed test cases: one comprehensive C# case, four parameterized C# cases, and one Visual Basic case.
1. C# positive guard and implicit negation
IsApplePlatform_GuardsOnlyFourApplePlatformsAsyncexercises both branches of:These assertions cover:
else.2. C# explicit negation, independently for every Apple platform
IsApplePlatform_NegatedGuardExcludesOnlyApplePlatformsAsyncruns with four data rows:For each platform, it defines an API unsupported on that platform and checks:
!OperatingSystem.IsApplePlatform()Testing each Apple platform independently verifies that all four values participate in decoding and negation. It also detects omissions from the platform set: a missing platform would no longer be correctly excluded by the negated guard.
Windows is an independent control that verifies the negative guard does not incorrectly exclude non-Apple platforms.
3. Visual Basic positive and negative guards
IsApplePlatform_GuardsApplePlatforms_VisualBasicAsyncverifies the same built-in decoding path through Visual Basic operations and control flow.If OperatingSystem.IsApplePlatform() ThenIf Not OperatingSystem.IsApplePlatform() ThenThis covers both
IfandNotin VB and verifies that the fix is not limited to C# syntax.Validation
The new regressions were exercised against the original decoder and reproduced incorrect analysis involving the fictitious
ApplePlatform. All six new cases pass with the fix.The analyzer solution builds successfully with 0 warnings and 0 errors:
The platform compatibility analyzer test group was run using the repository test driver:
Result: 344 total, 342 passed, 2 existing skipped tests, 0 failed.
Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com