Skip to content

Commit 5af6e5e

Browse files
committed
Address comments
Signed-off-by: Peter Rong <[email protected]>
1 parent 05644c1 commit 5af6e5e

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,8 +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-
"@objc functions cannot have typed throw", ())
5464+
ERROR(typed_throw_in_objc_forbidden,none,
5465+
"typed 'throws' %kindonly0 cannot be represented in Objective-C",
5466+
(const AbstractFunctionDecl *))
54665467

54675468
//------------------------------------------------------------------------------
54685469
// MARK: Concurrency

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -809,26 +809,23 @@ bool swift::isRepresentableInLanguage(
809809
return false;
810810
}
811811
}
812-
auto isTypedThrow = [&]() {
813-
// With no AST token this should be `throws`
814-
if (!AFD->getThrownTypeRepr())
815-
return false;
816-
// Or it is a `throws(<ErrorType>)`
817-
CanType thrownType = AFD->getThrownInterfaceType()->getCanonicalType();
812+
813+
// Check that @objc functions can't have typed throw.
814+
if (AFD->hasThrows()) {
815+
Type thrownType = AFD->getThrownInterfaceType();
818816
// TODO: only `throws(Error)` is allowed.
819817
// Throwing `any MyError` that confronts `Error` is not implemented yet.
820818
// Shall we allow `any MyError` in the future, we should check against
821-
// `isExistentialType` instead.
822-
if (thrownType->isErrorExistentialType())
819+
// `isExistentialType` instead, and other type checks will make sure it
820+
// confrons to `Error`.
821+
if (thrownType && !thrownType->isErrorExistentialType()) {
822+
softenIfAccessNote(AFD, Reason.getAttr(),
823+
AFD->diagnose(diag::typed_throw_in_objc_forbidden, AFD)
824+
.limitBehavior(behavior));
825+
Reason.describe(AFD);
823826
return false;
824-
softenIfAccessNote(AFD, Reason.getAttr(),
825-
AFD->diagnose(diag::typed_thrown_in_objc_forbidden)
826-
.limitBehavior(behavior));
827-
Reason.describe(AFD);
828-
return true;
829-
};
830-
if (AFD->hasThrows() && isTypedThrow())
831-
return false;
827+
}
828+
}
832829

833830
if (AFD->hasAsync()) {
834831
// Asynchronous functions move all of the result value and thrown error

test/attr/attr_objc.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,10 +2337,12 @@ class ClassThrows1 {
23372337

23382338
@objc func throwsError() throws(Error) {}
23392339
@objc func throwsMyError() throws(MyError) {}
2340-
// expected-error@-1{{@objc functions cannot have typed throw}}
2340+
// expected-error@-1{{typed 'throws' instance method cannot be represented in Objective-C}}
23412341
// expected-error@-2{{thrown type 'any MyError' does not conform to the 'Error' protocol}}
23422342
@objc func throwsObjCError() throws(ObjCError) {}
2343-
// expected-error@-1{{@objc functions cannot have typed throw}}
2343+
// expected-error@-1{{typed 'throws' instance method cannot be represented in Objective-C}}
2344+
@objc static func throwsObjCErrorClass() throws(ObjCError) {}
2345+
// expected-error@-1{{typed 'throws' static method cannot be represented in Objective-C}}
23442346
}
23452347

23462348

test/attr/attr_objc_async.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ actor MyActor {
5656

5757
@objc func objcAsyncThrowsError() async throws(Error) -> Void {}
5858
@objc func objcAsyncThrowsObjCError() async throws(ObjCError) -> Void {}
59-
// expected-error@-1 {{@objc functions cannot have typed throw}}
59+
// expected-error@-1 {{typed 'throws' instance method cannot be represented in Objective-C}}
6060
}
6161

6262
actor class MyActor2 { }

0 commit comments

Comments
 (0)