Skip to content

[src] Add [Un]SupportedSimulator attributes.#25347

Draft
rolfbjarne wants to merge 6 commits intomainfrom
dev/rolf/supportedsimulatorattribute
Draft

[src] Add [Un]SupportedSimulator attributes.#25347
rolfbjarne wants to merge 6 commits intomainfrom
dev/rolf/supportedsimulatorattribute

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

Add two attributes to specify whether an API is available in the simulator:

  • [UnsupportedSimulator ("")]: API is not available.
  • [SupportedSimulator ("[osversion]")]: API is available, optionally
    only in the specified OS version or not.

If no attribute is found, then the API is available in the simulator.

Unlike the normal availability attributes, specifying simulator availability for
one platform does not mean anything for any other platforms.

Add two attributes to specify whether an API is available in the simulator:

* [UnsupportedSimulator ("<platform>")]: API is not available.
* [SupportedSimulator ("<platform>[osversion]")]: API is available, optionally
  only in the specified OS version or not.

If no attribute is found, then the API is available in the simulator.

Unlike the normal availability attributes, specifying simulator availability for
one platform does not mean anything for any other platforms.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces two new attributes ([SupportedSimulator] / [UnsupportedSimulator]) to describe simulator-only availability for APIs, wires the logic into both the binding generator (bgen) and introspection tests, and replaces a number of simulator-specific skip-lists with attribute-driven behavior (notably for newer Metal/MetalFX/VideoToolbox APIs).

Changes:

  • Added SupportedSimulatorAttribute / UnsupportedSimulatorAttribute to ObjCRuntime, and trimmed them during linking.
  • Updated bgen to emit simulator-availability attributes for iOS/tvOS only, with new bgen unit tests to validate emission behavior.
  • Updated introspection tests to honor simulator-availability attributes, and annotated relevant Metal/MetalFX/VideoToolbox APIs as unsupported in iOS/tvOS simulators.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/introspection/ApiCtorInitTest.cs Removes hardcoded simulator skip cases now handled via simulator-availability attributes.
tests/introspection/ApiBaseTest.cs Extends skip logic to consider simulator availability (IsAvailableInSimulator).
tests/common/PlatformInfo.cs Adds reflection-based evaluation of [SupportedSimulator] / [UnsupportedSimulator] during simulator runs.
tests/bgen/tests/simulator-availability-attributes.cs Adds a dedicated bgen fixture file exercising simulator availability attributes.
tests/bgen/BGenTests.cs Adds tests verifying simulator attributes are emitted for iOS/tvOS and not emitted for macOS/Mac Catalyst.
src/videotoolbox.cs Marks specific new VideoToolbox APIs as unsupported on iOS/tvOS simulators.
src/TrimAttributes.LinkDescription.xml Ensures simulator-availability attributes are removed by the linker (attribute-instance trimming).
src/ObjCRuntime/SupportedSimulatorAttribute.cs Introduces the new public attributes in ObjCRuntime.
src/metalfx.cs Marks MetalFX descriptor APIs as unsupported on iOS/tvOS simulators.
src/Metal/MTLEnums.cs Marks selected Metal enums as unsupported on iOS/tvOS simulators.
src/metal.cs Marks many Metal (incl. Metal 4) APIs as unsupported on iOS/tvOS simulators.
src/frameworks.sources Adds the new attribute source file to the shared core sources list.
src/bgen/Generator.cs Adds emission of simulator-availability attributes (filtered to current platform; ignored on Mac platforms).
src/bgen/Enums.cs Ensures simulator-availability attributes are printed for enum extension generation as well.
src/bgen/bgen.csproj Includes the new attribute definitions in the bgen build.
src/bgen/AttributeManager.cs Teaches attribute loading to resolve the new attribute types.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
Copy link
Copy Markdown
Member Author

/review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

.NET for Apple Platforms PR Reviewer completed successfully!

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Review Summary

This PR introduces two new attributes ([SupportedSimulator] and [UnsupportedSimulator]) to specify whether APIs are available in iOS/tvOS simulators. The implementation is well-structured and follows the repository's patterns.

✅ What's Good

  • Clear architecture: The attributes are properly integrated into bgen, with filtering logic that only emits attributes relevant to the current platform
  • Comprehensive testing: Includes bgen tests, introspection test integration, and proper handling in PlatformInfo.IsAvailableInSimulator()
  • Proper trimming configuration: The attributes are correctly configured for removal during app trimming in TrimAttributes.LinkDescription.xml
  • Consistent usage: Applied systematically to MLCompute, Metal, MetalFX, and VideoToolbox APIs that aren't available in simulators
  • Test cleanup: Removes numerous simulator-specific workarounds from introspection tests now that the framework properly tracks simulator availability

📊 Issues Found

  • ⚠️ 1 warning: Performance issue with string allocations in IsAvailableInSimulator() hot path
  • 💡 4 suggestions: Code duplication, error handling, documentation clarity, and immutability patterns

Verdict

No blocking issues. The warnings and suggestions are optional improvements that would enhance code quality but aren't required for merge.

CI status: Some test failures present (monotouch_tvos, xcframework, interdependent-binding-projects), but these appear unrelated to this change since the core simulator and introspection tests pass.

Generated by .NET for Apple Platforms PR Reviewer for issue #25347 · ● 875K

Comment thread src/bgen/Generator.cs

public void PrintSimulatorAvailabilityAttributes (ICustomAttributeProvider? provider)
{
switch (CurrentPlatform) {
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.

🤖 💡 Code organization — Consider extracting the platform check logic into a helper method.

The switch statement checking CurrentPlatform against iOS/tvOS appears in three places in this file (lines 5592-5601, 5615-5628, 5642-5655). Consider extracting this into a helper method like:

bool IsSimulatorAvailabilityRelevantPlatform()
{
    return CurrentPlatform == PlatformName.iOS || CurrentPlatform == PlatformName.TvOS;
}

This would reduce duplication and make the intent clearer.

Rule: Code duplication

Comment thread tests/common/PlatformInfo.cs Outdated

// There's a SupportedSimulator attribute for the current platform - check version
foreach (var attr in supportedAttrs) {
var versionString = attr.PlatformName.Substring (platformPrefix.Length);
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.

🤖 ⚠️ Performance — Unnecessary string allocation in hot path.

The Substring call allocates a new string on every iteration. Consider using AsSpan() or extract the substring once before the loop:

var versionString = attr.PlatformName.AsSpan(platformPrefix.Length);
if (versionString.IsEmpty)
    return true;
if (Version.TryParse(versionString, out var version) && PlatformInfo.Host.Version >= version)
    return true;

Or cache it once:

foreach (var attr in supportedAttrs) {
    var versionString = attr.PlatformName.Substring (platformPrefix.Length);
    if (string.IsNullOrEmpty (versionString)) {
        return true; // supported, no version constraint
    } else if (Version.TryParse (versionString, out var version) && PlatformInfo.Host.Version >= version) {
        return true;
    }
}

This method is called in test introspection for every API member, so avoiding allocations matters.

Rule: Avoid unnecessary allocations

Comment thread tests/common/PlatformInfo.cs Outdated
var versionString = attr.PlatformName.Substring (platformPrefix.Length);
if (string.IsNullOrEmpty (versionString))
return true; // supported, no version constraint
if (Version.TryParse (versionString, out var version) && PlatformInfo.Host.Version >= version)
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.

🤖 💡 Error handling — Consider handling Version.TryParse failure more explicitly.

If Version.TryParse fails (returns false), the current code silently continues to the next attribute. This means a malformed version string like "ios17.0.invalid" would be ignored rather than causing an error.

Consider logging or failing fast if version parsing fails:

if (!string.IsNullOrEmpty(versionString)) {
    if (!Version.TryParse (versionString, out var version))
        throw new InvalidOperationException ($"Invalid version string in SupportedSimulator attribute: '{attr.PlatformName}'");
    if (PlatformInfo.Host.Version >= version)
        return true;
}

This would catch binding definition errors earlier rather than silently skipping them.

Rule: Include actionable details in exceptions

[AttributeUsage (AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Constructor | AttributeTargets.Field, AllowMultiple = true)]
public sealed class SupportedSimulatorAttribute : Attribute {
/// <summary>Initializes a new <see cref="SupportedSimulatorAttribute" /> attribute.</summary>
/// <param name="platformName">The platform where this API is supported in the simulator. Optionally specifies a platform version.</param>
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.

🤖 💡 Documentation — The XML docs could be clearer about the version format.

The <param> documentation says "The platform where this API is supported in the simulator. Optionally specifies a platform version." but doesn't explain the expected format.

Consider adding an example to clarify:

/// <param name="platformName">The platform where this API is supported in the simulator. 
/// Format: platform name (e.g., "ios", "tvos") optionally followed by a version number (e.g., "ios17.0").</param>

This would help binding authors use the attribute correctly.

Rule: Documentation

Comment thread src/ObjCRuntime/SupportedSimulatorAttribute.cs Outdated
…rove docs

- Extract IsSimulatorAvailabilityRelevantPlatform() helper in Generator.cs
  to reduce duplicated platform switch logic.
- Use AsSpan() instead of Substring() in PlatformInfo.IsAvailableInSimulator
  to avoid string allocations in the hot path.
- Throw InvalidOperationException on malformed version strings instead of
  silently continuing.
- Improve XML doc for platformName param with format examples.
- Make PlatformName properties init-only for immutability.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

rolfbjarne and others added 2 commits May 7, 2026 09:24
Adds ApiSimulatorAvailabilityTest which verifies that:
- P/Invokes and [Field] properties marked with [UnsupportedSimulator]
  truly have unavailable native symbols in the simulator.
- P/Invokes and [Field] properties NOT marked as unsupported don't have
  missing symbols (detecting cases where [UnsupportedSimulator] is needed).

The test only runs in the simulator and is ignored on device/desktop.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #fc0a48a] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: fc0a48a92bc47f50670aa993d758c39b644f6a2d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #fc0a48a] Build passed (Build packages) ✅

Pipeline on Agent
Hash: fc0a48a92bc47f50670aa993d758c39b644f6a2d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: fc0a48a92bc47f50670aa993d758c39b644f6a2d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #fc0a48a] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: fc0a48a92bc47f50670aa993d758c39b644f6a2d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [CI Build #fc0a48a] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 24 tests failed, 145 tests passed.

Failures

❌ interdependent-binding-projects tests

2 tests failed, 2 tests passed.

Failed tests

  • interdependent-binding-projects/iOS - simulator/Debug: LaunchTimedOut
  • interdependent-binding-projects/tvOS - simulator/Debug: LaunchTimedOut

Html Report (VSDrops) Download

❌ linker tests

22 tests failed, 22 tests passed.

Failed tests

  • dont link/iOS - simulator/Debug: LaunchTimedOut
  • dont link/iOS - simulator/Release: LaunchTimedOut
  • dont link/tvOS - simulator/Debug: LaunchTimedOut
  • dont link/tvOS - simulator/Release: LaunchTimedOut
  • link sdk/iOS - simulator/Debug: LaunchTimedOut
  • link sdk/iOS - simulator/Release: LaunchTimedOut
  • link sdk/tvOS - simulator/Debug: LaunchTimedOut
  • link sdk/tvOS - simulator/Release: LaunchTimedOut
  • link all/iOS - simulator/Debug: LaunchTimedOut
  • link all/iOS - simulator/Release: LaunchTimedOut
  • link all/iOS - simulator/Debug (don't bundle original resources): LaunchTimedOut
  • link all/tvOS - simulator/Debug: LaunchTimedOut
  • link all/tvOS - simulator/Release: LaunchTimedOut
  • link all/tvOS - simulator/Debug (don't bundle original resources): LaunchTimedOut
  • trimmode copy/iOS - simulator/Debug: LaunchTimedOut
  • trimmode copy/iOS - simulator/Release: LaunchTimedOut
  • trimmode copy/tvOS - simulator/Debug: LaunchTimedOut
  • trimmode copy/tvOS - simulator/Release: LaunchTimedOut
  • trimmode link/iOS - simulator/Debug: LaunchTimedOut
  • trimmode link/iOS - simulator/Release: LaunchTimedOut
  • trimmode link/tvOS - simulator/Debug: LaunchTimedOut
  • trimmode link/tvOS - simulator/Release: LaunchTimedOut

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 13 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 13 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. (⚠️ Html Report Publish failed ⚠️) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: fc0a48a92bc47f50670aa993d758c39b644f6a2d [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants