Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling super methods safely #455

Open
madsmtm opened this issue Jun 9, 2023 · 2 comments
Open

Calling super methods safely #455

madsmtm opened this issue Jun 9, 2023 · 2 comments
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request question Further information is requested

Comments

@madsmtm
Copy link
Owner

madsmtm commented Jun 9, 2023

Calling a super method is (with the below restrictions) as safe as calling regular methods.

Since doing so is a common requirement, e.g. in initializers, or when subclassing e.g. NSView, we should find a way so that the user can easily do so.

Requirements that I know of so far:

  • Only safe when declaring a new subclass.
  • Only direct super methods (e.g. calling NSResponder's methods from a subclass of NSView could potentially lead to UB, if NSView was implemented in a certain way.
  • Super method calls can only access methods on the superclass, not on the declared class.
  • Initializer methods only from inside other initializer methods.

The main problem with this is that it effectively doubles the amount of methods that we have to generate, the codegen is different for each. Though maybe with #448, that will be less of a problem?

If we had inherent traits, or something like it, then it would perhaps be easier, since we could make the implementation on a trait instead.

Related: Internally, we can consider getting rid of send_message_super, by providing a struct Super<T: MessageReceiver> { obj: T, cls: &'static Class } that implements MessageReceiver::send_message by calling objc_msgSendSuper instead.

Related:

@madsmtm madsmtm added enhancement New feature or request question Further information is requested A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates A-framework Affects the framework crates and the translator for them labels Jun 9, 2023
@madsmtm madsmtm added this to the icrate v0.2.0 milestone Jun 9, 2023
@madsmtm
Copy link
Owner Author

madsmtm commented Jun 25, 2023

One last requirement that actually ends up making this work:

  • Only need to call super methods when overriding a method (otherwise you'd just call the method normally)

Which means that we can do the super class method generation inside declare_class! instead of extern_class!!

@madsmtm
Copy link
Owner Author

madsmtm commented Jun 25, 2023

It would probably also be nice to require the user to add an #[override] attribute (matches Swift's required override keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant