Skip to content

Commit 8408c33

Browse files
authored
Merge pull request #31156 from artemcm/NonThrowDecl
Diagnose non-throwing methods implementing a throwing @objc protocol method
2 parents 0bbf378 + aa3cc67 commit 8408c33

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,9 @@ ERROR(override_throws,none,
24592459
ERROR(override_throws_objc,none,
24602460
"overriding a throwing @objc %select{method|initializer}0 with "
24612461
"a non-throwing %select{method|initializer}0 is not supported", (bool))
2462+
ERROR(satisfy_throws_objc,none,
2463+
"satisfying a throwing @objc %select{method|initializer}0 with "
2464+
"a non-throwing %select{method|initializer}0 is not supported", (bool))
24622465

24632466
ERROR(override_optional_mismatch,none,
24642467
"cannot override %0 %select{parameter|index}1 of type %2 with "

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,10 +1605,17 @@ void markAsObjC(ValueDecl *D, ObjCReason reason,
16051605
}
16061606
}
16071607
}
1608-
1608+
16091609
// Attach the foreign error convention.
16101610
if (inheritedConvention) {
1611-
method->setForeignErrorConvention(*inheritedConvention);
1611+
// Diagnose if this is a method that does not throw
1612+
// but inherits an ObjC error convention.
1613+
if (!method->hasThrows())
1614+
method->diagnose(diag::satisfy_throws_objc,
1615+
isa<ConstructorDecl>(method));
1616+
else
1617+
method->setForeignErrorConvention(*inheritedConvention);
1618+
16121619
} else if (method->hasThrows()) {
16131620
assert(errorConvention && "Missing error convention");
16141621
method->setForeignErrorConvention(*errorConvention);

test/Sema/impl_throw_objc.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-typecheck-verify-swift %s
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
@objc protocol Tracker {
6+
func track(event: String) throws
7+
}
8+
9+
class ApplicationTracker: NSObject, Tracker {
10+
func track(event: String) {} // expected-error {{satisfying a throwing @objc method with a non-throwing method is not supported}}
11+
}

0 commit comments

Comments
 (0)