Skip to content

Commit 6fd6c13

Browse files
authored
Operator methods are not @objc-compatible. (#4562)
https://bugs.swift.org/browse/SR-2419 (cherry picked from commit 4372a30)
1 parent b74ae30 commit 6fd6c13

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,10 @@ ERROR(objc_in_extension_context,none,
27282728
"members of constrained extensions cannot be declared @objc", ())
27292729
ERROR(objc_in_generic_extension,none,
27302730
"@objc is not supported within extensions of generic classes", ())
2731+
ERROR(objc_operator, none,
2732+
"operator methods cannot be declared @objc", ())
2733+
ERROR(objc_operator_proto, none,
2734+
"@objc protocols may not have operator requirements", ())
27312735

27322736
ERROR(objc_for_generic_class,none,
27332737
"generic subclasses of '@objc' classes cannot have an explicit '@objc' "

lib/Sema/TypeCheckDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,8 @@ static Optional<ObjCReason> shouldMarkAsObjC(TypeChecker &TC,
20622062
return ObjCReason::WitnessToObjC;
20632063
else if (VD->isInvalid())
20642064
return None;
2065+
else if (VD->isOperator())
2066+
return None;
20652067
// Implicitly generated declarations are not @objc, except for constructors.
20662068
else if (!allowImplicit && VD->isImplicit())
20672069
return None;
@@ -8119,12 +8121,11 @@ static void validateAttributes(TypeChecker &TC, Decl *D) {
81198121
error = diag::objc_enum_case_req_objc_enum;
81208122
else if (objcAttr->hasName() && EED->getParentCase()->getElements().size() > 1)
81218123
error = diag::objc_enum_case_multi;
8122-
} else if (isa<FuncDecl>(D)) {
8123-
auto func = cast<FuncDecl>(D);
8124+
} else if (auto *func = dyn_cast<FuncDecl>(D)) {
81248125
if (!checkObjCDeclContext(D))
81258126
error = diag::invalid_objc_decl_context;
81268127
else if (func->isOperator())
8127-
error = diag::invalid_objc_decl;
8128+
error = diag::objc_operator;
81288129
else if (func->isAccessor() && !func->isGetterOrSetter())
81298130
error = diag::objc_observing_accessor;
81308131
} else if (isa<ConstructorDecl>(D) ||

lib/Sema/TypeCheckType.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,13 @@ bool TypeChecker::isRepresentableInObjC(
29422942
if (checkObjCInExtensionContext(*this, AFD, Diagnose))
29432943
return false;
29442944

2945+
if (AFD->isOperator()) {
2946+
assert(isa<ProtocolDecl>(AFD->getDeclContext()) &&
2947+
"all other cases should be caught earlier");
2948+
diagnose(AFD, diag::objc_operator_proto);
2949+
return false;
2950+
}
2951+
29452952
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
29462953
if (FD->isAccessor()) {
29472954
// Accessors can only be @objc if the storage declaration is.

test/attr/attr_objc.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,3 +2068,19 @@ class ConformsToProtocolThrowsObjCName2 : ProtocolThrowsObjCName {
20682068
func ==(lhs: ObjC_Class1, rhs: ObjC_Class1) -> Bool {
20692069
return true
20702070
}
2071+
2072+
// CHECK-LABEL: @objc class OperatorInClass
2073+
@objc class OperatorInClass {
2074+
// CHECK: {{^}} static func ==(lhs: OperatorInClass, rhs: OperatorInClass) -> Bool
2075+
static func ==(lhs: OperatorInClass, rhs: OperatorInClass) -> Bool {
2076+
return true
2077+
}
2078+
// CHECK: {{^}} @objc static func +(lhs: OperatorInClass, rhs: OperatorInClass) -> OperatorInClass
2079+
@objc static func +(lhs: OperatorInClass, rhs: OperatorInClass) -> OperatorInClass { // expected-error {{operator methods cannot be declared @objc}}
2080+
return lhs
2081+
}
2082+
} // CHECK: {{^}$}}
2083+
2084+
@objc protocol OperatorInProtocol {
2085+
static func +(lhs: Self, rhs: Self) -> Self // expected-error {{@objc protocols may not have operator requirements}}
2086+
}

0 commit comments

Comments
 (0)