Skip to content

Commit b664a40

Browse files
committed
[Sema] Type checking forbids typed throw on @objc functions now
Signed-off-by: Peter Rong <PeterRong@meta.com>
1 parent e9253b7 commit b664a40

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,6 +5461,9 @@ WARNING(no_throw_in_do_with_catch,none,
54615461
ERROR(thrown_type_not_error,none,
54625462
"thrown type %0 does not conform to the 'Error' protocol", (Type))
54635463

5464+
ERROR(typed_thrown_in_objc_forbidden,none,
5465+
"typed throw can't be applied to @objc functions", ())
5466+
54645467
//------------------------------------------------------------------------------
54655468
// MARK: Concurrency
54665469
//------------------------------------------------------------------------------

lib/Sema/TypeCheckDecl.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,12 +2569,13 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25692569
ProtocolDecl *errorProto = Context.getErrorDecl();
25702570
if (thrownTy && !thrownTy->hasError() && errorProto) {
25712571
Type thrownTyInContext = AFD->mapTypeIntoContext(thrownTy);
2572-
if (!checkConformance(thrownTyInContext, errorProto)) {
2573-
SourceLoc loc;
2574-
if (auto thrownTypeRepr = AFD->getThrownTypeRepr())
2575-
loc = thrownTypeRepr->getLoc();
2576-
else
2577-
loc = AFD->getLoc();
2572+
auto thrownTypeRepr = AFD->getThrownTypeRepr();
2573+
SourceLoc loc =
2574+
(thrownTypeRepr) ? thrownTypeRepr->getLoc() : AFD->getLoc();
2575+
if (AFD->getAttrs().hasAttribute<ObjCAttr>()) {
2576+
Context.Diags.diagnose(loc, diag::typed_thrown_in_objc_forbidden,
2577+
thrownTy);
2578+
} else if (!checkConformance(thrownTyInContext, errorProto)) {
25782579
Context.Diags.diagnose(loc, diag::thrown_type_not_error, thrownTy);
25792580
}
25802581
}

test/decl/func/typed_throws.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,9 @@ struct NotAnError<T> {}
216216

217217
func badThrowingFunctionType<T>(_: () throws(NotAnError<T>) -> ()) {}
218218
// expected-error@-1 {{thrown type 'NotAnError<T>' does not conform to the 'Error' protocol}}
219+
220+
enum ObjCError: Int, Error {
221+
case Others
222+
}
223+
@objc func objcTypedThrow() throws(ObjCError) -> () {}
224+
// expected-error@-1 {{typed throw can't be applied to @objc functions}}

0 commit comments

Comments
 (0)