Skip to content

Commit 81ae9d6

Browse files
committed
More tests and cleanup
Adding execution and death test to ensure that we crash appropriately when the main function throws an uncaught exception, and that the async main runs correctly. Also switching to doing the CFRunLoopRun lookup with `RTLD_DEFAULT` since `RTLD_SELF` isn't available on Linux. Switching to `try await` since `await try` is no longer the right way to do that. Using exit(0) instead of EXIT_SUCCESS since the C++ importer doesn't mark imported macros with @actorIndependent yet.
1 parent 6b16657 commit 81ae9d6

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

stdlib/public/Concurrency/Task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ void swift::swift_continuation_logFailedCheck(const char *message) {
535535

536536
void swift::swift_task_asyncMainDrainQueue() {
537537
auto runLoop =
538-
reinterpret_cast<void (*)(void)>(dlsym(RTLD_SELF, "CFRunLoopRun"));
538+
reinterpret_cast<void (*)(void)>(dlsym(RTLD_DEFAULT, "CFRunLoopRun"));
539539
if (runLoop)
540540
runLoop();
541541
else

stdlib/public/Concurrency/Task.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ public func _asyncMainDrainQueue() -> Never
329329
public func _runAsyncMain(_ asyncFun: @escaping () async throws -> ()) {
330330
let _ = Task.runDetached {
331331
do {
332-
await try asyncFun()
333-
exit(EXIT_SUCCESS)
332+
try await asyncFun()
333+
exit(0)
334334
} catch {
335335
_errorInMain(error)
336336
}

test/Concurrency/async_main.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
// RUN: %target-swift-frontend -dump-ast -enable-experimental-concurrency -parse-as-library %s | %FileCheck %s --check-prefix=CHECK-AST
2+
// RUN: %target-build-swift -Xfrontend -enable-experimental-concurrency -Xfrontend -parse-as-library %s -o %t_binary
3+
// RUN: %target-run %t_binary | %FileCheck %s --check-prefix=CHECK-EXEC
4+
25
// REQUIRES: concurrency
6+
// REQUIRES: executable_test
37

4-
func asyncFunc() async { }
8+
func asyncFunc() async {
9+
print("Hello World!")
10+
}
511

612
@main struct MyProgram {
713
static func main() async {
814
await asyncFunc()
915
}
1016
}
1117

18+
// CHECK-EXEC: Hello World!
1219

1320
// CHECK-AST-LABEL: "main()" interface
1421
// CHECK-AST: (await_expr type='()'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -Xfrontend -enable-experimental-concurrency -Xfrontend -parse-as-library %s -o %t/main
3+
// RUN: %target-codesign %t/main
4+
// RUN: %target-run %t/main > %t/log 2>&1 || true
5+
// RUN: %FileCheck %s < %t/log
6+
7+
// REQUIRES: concurrency
8+
// REQUIRES: executable_test
9+
10+
enum Err : Error { case noGood }
11+
12+
func asyncFunc() async throws {
13+
throw Err.noGood
14+
}
15+
16+
// CHECK: Fatal error: Error raised at top level: main.Err.noGood
17+
@main struct MyProgram {
18+
static func main() async throws {
19+
try await asyncFunc()
20+
}
21+
}

0 commit comments

Comments
 (0)