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

[SR-6417] Check if super is called when required. #48967

Open
swift-ci opened this issue Nov 17, 2017 · 1 comment
Open

[SR-6417] Check if super is called when required. #48967

swift-ci opened this issue Nov 17, 2017 · 1 comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers improvement swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6417
Radar rdar://problem/17408107
Original Reporter WWessels (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 5
Component/s Compiler
Labels Improvement, LanguageFeatureRequest, StarterProposal
Assignee mb (JIRA)
Priority Medium

md5: 8ca04a71f5feba734a5bda3679466acc

Issue Description:

When creating a subclass of NSObject, you get an error when you 'forget' to call super in the init() function.

When creating a subclass of UIViewController, and you forget to call super in viewDidLoad, viewWillAppear, etc, you will not get an error or warning.

When creating a subclass of UIView, and you forget to call super in your implementation of awakeFromNib, you will not get an error or warning.

When creating a subclass of UITableViewCell or UICollectionViewCell, and you forget to call super in your implementation of prepareForReuse, you will not get an error or warning.

And there are probably more places where it is required, but not guarded that you call super.

As a freelancer, I produce and see lots of code and this is one of the issues that I often see (and fix). Forgetting to call super in these functions results in unexpected behaviour, even crashes and it can be pretty hard to find the problem.

It would be very helpful if one could declare that an overriding function has to call super. For instance with an attribute in the same manner as @discardableResult:

@callToSuperRequired
func viewDidLoad() {

}

This attribute should be valid for all subclasses that override this function, even subclasses of subclasses.

And then Apple should use this in their declaration of the aforementioned functions.

@belkadan
Copy link
Contributor

We actually have this in Objective-C as the objc_requires_super attribute. Implementing this in Swift means:

  • coming up with a name for it

  • proposing it on swift-evolution (it's probably come up before? can't remember)

  • adding the new attribute - easy

  • adding some checking for it (syntactic? path-sensitive?) - a bit harder

  • making sure it gets imported from Objective-C - easy again, we do the same for other attributes

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added good first issue Good for newcomers type checker Area → compiler: Semantic analysis swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal and removed StarterProposal labels Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers improvement swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants