Skip to content

Conversation

@mandel-macaque
Copy link
Contributor

If a class inherits from NSCoding or implements INSCoding we generate the constructor for the 'initWithCoder:' selector.

If the user defined a constructor with the selector 'initWithCoder:' selector, we will skip the user definition and will add our own. Later the analyzer will be used to let the user know that 'initWithCoder:' is automatically added.

Copy link
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 implements automatic generation of the initWithCoder: constructor for classes that inherit from NSCoding or implement INSCoding interfaces. The generator now detects when a class requires NSCoding support and automatically adds the required constructor, while skipping user-defined constructors with the same selector to avoid conflicts.

Key changes:

  • Automatic initWithCoder: constructor generation for NSCoding classes
  • Conflict resolution when user already defines the constructor
  • Test coverage for both scenarios (with and without user-defined constructors)

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

File Description
src/rgen/Microsoft.Macios.Generator/Emitters/ClassEmitter.cs Core logic for detecting NSCoding classes and generating the constructor
src/rgen/Microsoft.Macios.Generator/Emitters/Documentation.cs Documentation template for the generated constructor
tests/rgen/Microsoft.Macios.Generator.Tests/Classes/ClassGenerationTests.cs Test data registration for NSCoding test scenarios
Various test data files Input and expected output files for testing the generation
Comments suppressed due to low confidence (1)

src/rgen/Microsoft.Macios.Generator/Emitters/ClassEmitter.cs:1

  • The selector string has an extra colon. It should be initWithCoder: not initWithCoder::.
// Copyright (c) Microsoft Corporation.

if (IsDirectBinding) {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");
} else {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

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

The selector string has an extra colon. It should be initWithCoder: not initWithCoder::.

Suggested change
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");

Copilot uses AI. Check for mistakes.
if (IsDirectBinding) {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");
} else {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

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

The selector string has an extra colon. It should be initWithCoder: not initWithCoder::.

Suggested change
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");

Copilot uses AI. Check for mistakes.
if (IsDirectBinding) {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");
} else {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

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

The selector string has an extra colon. It should be initWithCoder: not initWithCoder::.

Suggested change
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");

Copilot uses AI. Check for mistakes.
if (IsDirectBinding) {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");
} else {
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

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

The selector string has an extra colon. It should be initWithCoder: not initWithCoder::.

Suggested change
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder::"), coder.Handle), "initWithCoder:");
InitializeHandle (global::ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithCoder:"), coder.Handle), "initWithCoder:");

Copilot uses AI. Check for mistakes.
@vs-mobiletools-engineering-service2

This comment has been minimized.

If a class inherits from NSCoding or implements INSCoding we generate
the constructor for the 'initWithCoder:' selector.

If the user defined a constructor with the selector 'initWithCoder:'
selector, we will skip the user definition and will add our own. Later
the analyzer will be used to let the user know that 'initWithCoder:' is
automatically added.
@mandel-macaque mandel-macaque force-pushed the dev/mandel/add-initWithCoder-class branch from 7941f0f to 9e193b6 Compare September 11, 2025 22:27
@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
Collaborator

✅ [CI Build #65f4add] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: 9e193b6a2b3c1c96c6f775e7f66ff69c2c372a8d [PR build]

@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
Collaborator

✅ [CI Build #65f4add] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

.NET ( No breaking changes )

✅ API diff vs stable

.NET ( No breaking changes )

ℹ️ Generator diff

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

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #2729514] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 27295144c82fd7cde49d97bba82a925bd8d47244 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #9e193b6] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 9e193b6a2b3c1c96c6f775e7f66ff69c2c372a8d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #2729514] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 27295144c82fd7cde49d97bba82a925bd8d47244 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #65f4add] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #9e193b6] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 9e193b6a2b3c1c96c6f775e7f66ff69c2c372a8d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #65f4add] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #2729514] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 27295144c82fd7cde49d97bba82a925bd8d47244 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #9e193b6] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 9e193b6a2b3c1c96c6f775e7f66ff69c2c372a8d [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #65f4add] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #65f4add] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #2729514] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 27295144c82fd7cde49d97bba82a925bd8d47244 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #9e193b6] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 9e193b6a2b3c1c96c6f775e7f66ff69c2c372a8d [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #9e193b6] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 9e193b6a2b3c1c96c6f775e7f66ff69c2c372a8d [PR build]

@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
Collaborator

🔥 [CI Build #65f4add] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

3 tests crashed, 0 tests failed, 68 tests passed.

Failures

❌ interdependent-binding-projects tests

🔥 Failed catastrophically on VSTS: test results - interdependent-binding-projects (no summary found).

Html Report (VSDrops) Download

❌ linker tests

🔥 Failed catastrophically on VSTS: test results - linker (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_macos (no summary found).

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 10 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 10 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 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

Pipeline on Agent
Hash: 65f4add51c490293ddb392a98d8a5dc951c97e9b [PR build]

Copy link
Member

@rolfbjarne rolfbjarne left a comment

Choose a reason for hiding this comment

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

The NSCoding protocol has a constructor:

macios/src/foundation.cs

Lines 3329 to 3332 in fb6a00f

interface NSCoding {
// [Abstract]
[Export ("initWithCoder:")]
NativeHandle Constructor (NSCoder decoder);

So this might be better to implement by just automatically inlining constructors from protocols (which we already do for methods and properties), that way we don't have to special-case NSCoding.

];

/// <summary>
/// Emits the NSCoding constructor (initWithCoder:) for classes that inherit from NSCoding or implement INSCoding.
Copy link
Member

Choose a reason for hiding this comment

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

Inheriting from NSCoding doesn't make sense, we only care for classes that implement INSCoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's coming from the branch we depend. will update accordingly.

Copy link
Member

@dalexsoto dalexsoto left a comment

Choose a reason for hiding this comment

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

after Rolf's 👍

@mandel-macaque
Copy link
Contributor Author

Closing this for a more generic approach.

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.

5 participants