Skip to content

Commit e785319

Browse files
authored
Merge pull request #17236 from slavapestov/objc-inout-fix
Sema: Explicitly check for 'inout' parameters on @objc functions
2 parents 88742f5 + 0ce6de1 commit e785319

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,16 +3515,16 @@ WARNING(witness_swift3_objc_inference,none,
35153515
(DescriptiveDeclKind, DeclName, Type))
35163516

35173517

3518-
ERROR(objc_invalid_on_func_curried,none,
3519-
"method cannot be %" OBJC_ATTR_SELECT "0 because curried functions "
3520-
"cannot be represented in Objective-C", (unsigned))
35213518
ERROR(objc_observing_accessor, none,
35223519
"observing accessors are not allowed to be marked @objc", ())
35233520
ERROR(objc_addressor, none,
35243521
"addressors are not allowed to be marked @objc", ())
35253522
ERROR(objc_invalid_on_func_variadic,none,
35263523
"method cannot be %" OBJC_ATTR_SELECT "0 because it has a variadic "
35273524
"parameter", (unsigned))
3525+
ERROR(objc_invalid_on_func_inout,none,
3526+
"method cannot be %" OBJC_ATTR_SELECT "0 because inout "
3527+
"parameters cannot be represented in Objective-C", (unsigned))
35283528
ERROR(objc_invalid_on_func_param_type,none,
35293529
"method cannot be %" OBJC_ATTR_SELECT "1 because the type of the "
35303530
"parameter %0 cannot be represented in Objective-C", (unsigned, unsigned))

lib/Sema/TypeCheckType.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,7 +3360,7 @@ static bool isParamListRepresentableInObjC(TypeChecker &TC,
33603360
unsigned NumParams = PL->size();
33613361
for (unsigned ParamIndex = 0; ParamIndex != NumParams; ParamIndex++) {
33623362
auto param = PL->get(ParamIndex);
3363-
3363+
33643364
// Swift Varargs are not representable in Objective-C.
33653365
if (param->isVariadic()) {
33663366
if (Diagnose && shouldDiagnoseObjCReason(Reason, TC.Context)) {
@@ -3369,10 +3369,22 @@ static bool isParamListRepresentableInObjC(TypeChecker &TC,
33693369
.highlight(param->getSourceRange());
33703370
describeObjCReason(TC, AFD, Reason);
33713371
}
3372-
3372+
33733373
return false;
33743374
}
3375-
3375+
3376+
// Swift inout parameters are not representable in Objective-C.
3377+
if (param->isInOut()) {
3378+
if (Diagnose && shouldDiagnoseObjCReason(Reason, TC.Context)) {
3379+
TC.diagnose(param->getStartLoc(), diag::objc_invalid_on_func_inout,
3380+
getObjCDiagnosticAttrKind(Reason))
3381+
.highlight(param->getSourceRange());
3382+
describeObjCReason(TC, AFD, Reason);
3383+
}
3384+
3385+
return false;
3386+
}
3387+
33763388
if (param->getType()->isRepresentableIn(
33773389
ForeignLanguage::ObjectiveC,
33783390
const_cast<AbstractFunctionDecl *>(AFD)))
@@ -3601,20 +3613,6 @@ bool TypeChecker::isRepresentableInObjC(
36013613
llvm_unreachable("bad kind");
36023614
}
36033615

3604-
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
3605-
unsigned ExpectedParamPatterns = 1;
3606-
if (FD->getImplicitSelfDecl())
3607-
ExpectedParamPatterns++;
3608-
if (FD->getParameterLists().size() != ExpectedParamPatterns) {
3609-
if (Diagnose) {
3610-
diagnose(AFD->getLoc(), diag::objc_invalid_on_func_curried,
3611-
getObjCDiagnosticAttrKind(Reason));
3612-
describeObjCReason(*this, AFD, Reason);
3613-
}
3614-
return false;
3615-
}
3616-
}
3617-
36183616
// As a special case, an initializer with a single, named parameter of type
36193617
// '()' is always representable in Objective-C. This allows us to cope with
36203618
// zero-parameter methods with selectors that are longer than "init". For

test/attr/attr_objc.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ class ConcreteContext3 {
354354

355355
typealias NSCodingExistential = NSCoding.Type
356356

357+
@objc func inoutFunc(a: inout Int) {}
358+
// expected-error@-1{{method cannot be marked @objc because inout parameters cannot be represented in Objective-C}}
357359
@objc func metatypeOfExistentialMetatypePram1(a: NSCodingExistential.Protocol) {}
358360
// expected-error@-1{{method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C}}
359361

0 commit comments

Comments
 (0)