Skip to content

Allow ObjC superclass to disable init inheritance #61896

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

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,8 @@ InheritsSuperclassInitializersRequest::evaluate(Evaluator &eval,

// If the superclass has known-missing designated initializers, inheriting
// is unsafe.
if (superclassDecl->getModuleContext() != decl->getParentModule() &&
if ((superclassDecl->hasClangNode() ||
superclassDecl->getModuleContext() != decl->getParentModule()) &&
superclassDecl->hasMissingDesignatedInitializers())
return false;

Expand Down
7 changes: 7 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/ObjCParseExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,10 @@ struct TrivialToCopy {
@interface SuperclassWithDesignatedInitInCategory ()
-(instancetype) initWithI:(NSInteger)i __attribute__((objc_designated_initializer));
@end

__attribute__((swift_attr("@_hasMissingDesignatedInitializers")))
@interface NoConvenienceInitInheritanceBase : NSObject
@end

@interface NoConvenienceInitInheritance : NoConvenienceInitInheritanceBase
@end
11 changes: 11 additions & 0 deletions test/ClangImporter/objc_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,14 @@ extension SuperclassWithDesignatedInitInCategory {
func testConvenienceInitInheritance() {
_ = SubclassWithSwiftPrivateDesignatedInit(y: 5)
}

// ...unless the superclass has been marked with
// @_hasMissingDesignatedInitializers to disable this behavior.
extension NoConvenienceInitInheritanceBase {
convenience init(y: Int) { self.init() }
}

func testNoConvenienceInitInheritance() {
_ = NoConvenienceInitInheritanceBase(y: 42)
_ = NoConvenienceInitInheritance(y: 42) // expected-error {{argument passed to call that takes no arguments}}
}