Skip to content

Commit 4f51f75

Browse files
committed
[PrintAsObjC] Add '__attribute__((return))' for 'Never' return methods
* Add 'SWIFT_NORETURN' macro to the prologue. This macro is evaluated to '__attribute__((noreturn))' where supported. * Apply 'SWIFT_NORETURN' to 'isUninhabited()' methods.
1 parent 47e0079 commit 4f51f75

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,10 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
628628
if (looksLikeInitMethod(AFD->getObjCSelector())) {
629629
os << " SWIFT_METHOD_FAMILY(none)";
630630
}
631-
if (!methodTy->getResult()->isVoid() &&
632-
!methodTy->getResult()->isUninhabited() &&
633-
!AFD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
631+
if (methodTy->getResult()->isUninhabited()) {
632+
os << " SWIFT_NORETURN";
633+
} else if (!methodTy->getResult()->isVoid() &&
634+
!AFD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
634635
os << " SWIFT_WARN_UNUSED_RESULT";
635636
}
636637
}
@@ -2337,6 +2338,11 @@ class ModuleWriter {
23372338
"#else\n"
23382339
"# define SWIFT_WARN_UNUSED_RESULT\n"
23392340
"#endif\n"
2341+
"#if defined(__has_attribute) && __has_attribute(noreturn)\n"
2342+
"# define SWIFT_NORETURN __attribute__((noreturn))\n"
2343+
"#else\n"
2344+
"# define SWIFT_NORETURN\n"
2345+
"#endif\n"
23402346
"#if !defined(SWIFT_CLASS_EXTRA)\n"
23412347
"# define SWIFT_CLASS_EXTRA\n"
23422348
"#endif\n"

test/PrintAsObjC/never.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
}
1313

1414
// CHECK-LABEL: @interface NeverClass
15-
// CHECK: - (void)doesNotReturn;
15+
// CHECK: - (void)doesNotReturn SWIFT_NORETURN;
1616
// CHECK: @end

0 commit comments

Comments
 (0)