Skip to content

Support required inits in @objcImpl #66262

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
Jun 13, 2023
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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,11 @@ ERROR(objc_implementation_type_mismatch,none,
"header",
(DescriptiveDeclKind, ValueDecl *, Type, Type))

ERROR(objc_implementation_required_attr_mismatch,none,
"%0 %1 %select{should not|should}2 be 'required' to match %0 declared by "
"the header",
(DescriptiveDeclKind, ValueDecl *, bool))

ERROR(objc_implementation_wrong_objc_name,none,
"selector %0 for %1 %2 not found in header; did you mean %3?",
(ObjCSelector, DescriptiveDeclKind, ValueDecl *, ObjCSelector))
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,7 @@ void AttributeChecker::visitRequiredAttr(RequiredAttr *attr) {
// The constructor must be declared within the class itself.
// FIXME: Allow an SDK overlay to add a required initializer to a class
// defined in Objective-C
if (!isa<ClassDecl>(ctor->getDeclContext()) &&
if (!isa<ClassDecl>(ctor->getDeclContext()->getImplementedObjCContext()) &&
!isObjCClassExtensionInOverlay(ctor->getDeclContext())) {
diagnose(ctor, diag::required_initializer_in_extension, parentTy)
.highlight(attr->getLocation());
Expand Down
21 changes: 21 additions & 0 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,7 @@ class ObjCImplementationChecker {
WrongDeclKind,
WrongType,
WrongWritability,
WrongRequiredAttr,

Match,
MatchWithExplicitObjCName,
Expand Down Expand Up @@ -3312,6 +3313,10 @@ class ObjCImplementationChecker {
!cast<AbstractStorageDecl>(cand)->isSettable(nullptr))
return MatchOutcome::WrongWritability;

if (auto reqCtor = dyn_cast<ConstructorDecl>(req))
if (reqCtor->isRequired() != cast<ConstructorDecl>(cand)->isRequired())
return MatchOutcome::WrongRequiredAttr;

// If we got here, everything matched. But at what quality?
if (explicitObjCName)
return MatchOutcome::MatchWithExplicitObjCName;
Expand Down Expand Up @@ -3399,6 +3404,22 @@ class ObjCImplementationChecker {
diagnose(cand, diag::objc_implementation_must_be_settable,
cand->getDescriptiveKind(), cand, req->getDescriptiveKind());
return;

case MatchOutcome::WrongRequiredAttr: {
bool shouldBeRequired = cast<ConstructorDecl>(req)->isRequired();

auto diag =
diagnose(cand, diag::objc_implementation_required_attr_mismatch,
cand->getDescriptiveKind(), cand, shouldBeRequired);

if (shouldBeRequired)
diag.fixItInsert(cand->getAttributeInsertionLoc(/*forModifier=*/true),
"required ");
else
diag.fixItRemove(cand->getAttrs().getAttribute<RequiredAttr>()
->getLocation());
return;
}
}

llvm_unreachable("Unknown MatchOutcome");
Expand Down
11 changes: 10 additions & 1 deletion test/decl/ext/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@

@end

@interface ObjCClass : ObjCBaseClass
@protocol ObjCProto

- (instancetype)initFromProtocol1:(int)param;
- (instancetype)initFromProtocol2:(int)param;

@end

@interface ObjCClass : ObjCBaseClass <ObjCProto>

- (instancetype)initNotFromProtocol:(int)param;

- (void)methodFromHeader1:(int)param;
- (void)methodFromHeader2:(int)param;
Expand Down
15 changes: 15 additions & 0 deletions test/decl/ext/objc_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@
// OK
}

@objc(initFromProtocol1:)
required public init?(fromProtocol1: CInt) {
// OK
}

@objc(initFromProtocol2:)
public init?(fromProtocol2: CInt) {
// expected-error@-1 {{initializer 'init(fromProtocol2:)' should be 'required' to match initializer declared by the header}} {{3-3=required }}
}

@objc(initNotFromProtocol:)
required public init?(notFromProtocol: CInt) {
// expected-error@-1 {{initializer 'init(notFromProtocol:)' should not be 'required' to match initializer declared by the header}} {{3-12=}}
}

class func classMethod1(_: CInt) {
// OK
}
Expand Down
18 changes: 18 additions & 0 deletions test/decl/ext/objc_implementation_conflicts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ import objc_implementation_private
super.init(fromSuperclass2: v)
}

@objc(initFromProtocol1:)
required public init?(fromProtocol1 v: CInt) {
// OK
super.init(fromSuperclass: v)
}

@objc(initFromProtocol2:)
required public init?(fromProtocol2 v: CInt) {
// OK
super.init(fromSuperclass: v)
}

@objc(initNotFromProtocol:)
public init?(notFromProtocol v: CInt) {
// OK
super.init(fromSuperclass: v)
}

class func classMethod1(_: CInt) {}
class func classMethod2(_: CInt) {}
class func classMethod3(_: CInt) {}
Expand Down