Skip to content

[pigeon] Adds @SwiftClass annotation - #6372

Merged
tarrinneal merged 18 commits into
flutter:mainfrom
tarrinneal:swift-struct-to-class
Mar 26, 2024
Merged

tarrinneal merged 18 commits into
flutter:mainfrom
tarrinneal:swift-struct-to-class

Conversation

@tarrinneal

Copy link
Copy Markdown
Contributor

Adds annotation for @SwiftClass which will cause the swift generator to create a class instead of a struct in the generated Swift code.

This will allow for recursive classes as well as allow for Objc interop, without forcing users to lose the potential benefits of structs if they prefer them instead.

Also creates recursive data class integration test to check for coverage on all languages.

fixes flutter/flutter#145175

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@tarrinneal

Copy link
Copy Markdown
Contributor Author

@LouiseHsu Do you also need the ability to add the @objc attribute to generated classes?

Comment thread packages/pigeon/pigeons/core_tests.dart
@LouiseHsu

LouiseHsu commented Mar 21, 2024 •

Copy link
Copy Markdown
Contributor

@LouiseHsu Do you also need the ability to add the @objc attribute to generated classes?

Yes, that would be helpful! <3

@tarrinneal

Copy link
Copy Markdown
Contributor Author

@LouiseHsu Do you also need the ability to add the @objc attribute to generated classes?

Yes, that would be helpful! <3

For api classes and data classes? or just data classes?

@LouiseHsu

Copy link
Copy Markdown
Contributor

@LouiseHsu Do you also need the ability to add the @objc attribute to generated classes?

Yes, that would be helpful! <3

For api classes and data classes? or just data classes?

I think just data classes will be good for now :)

@tarrinneal

Copy link
Copy Markdown
Contributor Author

Might be best to combine the two annotations (the inheritance being optional within the class one)

@tarrinneal

Copy link
Copy Markdown
Contributor Author

Might be best to combine the two annotations (the inheritance being optional within the class one)

Done.

@stuartmorgan I'm hoping we can push this through in the morning, if you have time to review it.

Comment thread packages/pigeon/README.md Outdated
Comment thread packages/pigeon/pigeons/core_tests.dart
Comment thread packages/pigeon/platform_tests/test_plugin/windows/pigeon/core_tests.gen.h Outdated

@hellohuanlin hellohuanlin left a comment

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.

I like the idea of using annotation, because it can get complicated to detect recursive definition and decide struct vs class for developers.

Comment thread packages/pigeon/lib/ast.dart Outdated
Comment thread packages/pigeon/lib/cpp_generator.dart Outdated
String inheritNSObject = '';
if (classDefinition.isSwiftObjcInteropClass) {
inheritNSObject = ': NSObject';
indent.writeln('@objc');

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.

iirc @objc isn't necessary since it's already a subclass of NSObject, which is already exposed to objc runtime.

@hellohuanlin hellohuanlin Mar 23, 2024 •

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.

Oh sorry i missed it in my previous review - in addition to the above, we probably need to add @objc for all custom members that are not inherited from NSObject (basically all the properties and the init function defined here).

Can you also write some ObjC tests to make sure it works? (requesting changes since this is not tested)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

all properties? even primitives? or only custom types?

@hellohuanlin hellohuanlin Mar 24, 2024 •

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.

All properties and methods that are not inherited from NSObject need to be annotated, in order for them to be accessible by objc runtime. The class definition itself and its members inherited from NSObject are already implicitly annotated.

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.

So yes, even the properties of primitive types.

Comment thread packages/pigeon/README.md Outdated
String inheritNSObject = '';
if (classDefinition.isSwiftObjcInteropClass) {
inheritNSObject = ': NSObject';
indent.writeln('@objc');

@hellohuanlin hellohuanlin Mar 23, 2024 •

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.

Oh sorry i missed it in my previous review - in addition to the above, we probably need to add @objc for all custom members that are not inherited from NSObject (basically all the properties and the init function defined here).

Can you also write some ObjC tests to make sure it works? (requesting changes since this is not tested)

@stuartmorgan-g stuartmorgan-g left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM once @hellohuanlin's concerns are addressed. (Marking now since I'll be OOO.)

@tarrinneal

Copy link
Copy Markdown
Contributor Author

@hellohuanlin I removed the @objc feature for now to unblock this pr. I'll throw up a new pr with just that in a short while.

@tarrinneal
tarrinneal requested a review from hellohuanlin March 26, 2024 00:18

@hellohuanlin hellohuanlin left a comment

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.

As discussed offline, feel free to land this first and create a separate PR for objc interop.

@tarrinneal tarrinneal added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 26, 2024
@tarrinneal

Copy link
Copy Markdown
Contributor Author

Landing to fix the tree

@tarrinneal
tarrinneal merged commit ab1630b into flutter:main Mar 26, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 26, 2024
auto-submit Bot pushed a commit to flutter/flutter that referenced this pull request Mar 26, 2024
flutter/packages@28d126c...ab1630b

2024-03-26 [email protected] [pigeon] Adds @SwiftClass annotation (flutter/packages#6372)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
creatorpiyush pushed a commit to creatorpiyush/packages that referenced this pull request Jun 10, 2026
Adds annotation for @SwiftClass which will cause the swift generator to
create a `class` instead of a `struct` in the generated Swift code.

This will allow for recursive classes as well as allow for Objc interop,
without forcing users to lose the potential benefits of structs if they
prefer them instead.

Also creates recursive data class integration test to check for coverage
on all languages.

fixes flutter/flutter#145175

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [relevant style guides] and ran the
auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages
repo does use `dart format`.)
- [x] I signed the [CLA].
- [x] The title of the PR starts with the name of the package surrounded
by square brackets, e.g. `[shared_preferences]`
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated `pubspec.yaml` with an appropriate new version according
to the [pub versioning philosophy], or this PR is [exempt from version
changes].
- [x] I updated `CHANGELOG.md` to add a description of the change,
[following repository CHANGELOG style].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[relevant style guides]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
[pub versioning philosophy]: https://dart.dev/tools/pub/versioning
[exempt from version changes]:
https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#version-and-changelog-updates
[following repository CHANGELOG style]:
https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changelog-style
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests

---------

Co-authored-by: Stuart Morgan <[email protected]>
bisor0627 pushed a commit to bisor0627/packages that referenced this pull request Jun 19, 2026
Adds annotation for @SwiftClass which will cause the swift generator to
create a `class` instead of a `struct` in the generated Swift code.

This will allow for recursive classes as well as allow for Objc interop,
without forcing users to lose the potential benefits of structs if they
prefer them instead.

Also creates recursive data class integration test to check for coverage
on all languages.

fixes flutter/flutter#145175

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [relevant style guides] and ran the
auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages
repo does use `dart format`.)
- [x] I signed the [CLA].
- [x] The title of the PR starts with the name of the package surrounded
by square brackets, e.g. `[shared_preferences]`
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated `pubspec.yaml` with an appropriate new version according
to the [pub versioning philosophy], or this PR is [exempt from version
changes].
- [x] I updated `CHANGELOG.md` to add a description of the change,
[following repository CHANGELOG style].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[relevant style guides]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
[pub versioning philosophy]: https://dart.dev/tools/pub/versioning
[exempt from version changes]:
https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#version-and-changelog-updates
[following repository CHANGELOG style]:
https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changelog-style
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests

---------

Co-authored-by: Stuart Morgan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[pigeon] Generated swift code throws compile time error on recursive types

4 participants