Skip to content

Commit efb50e1

Browse files
committed
Adding implicit try-expr to throwing main
Synthesizing the body of a throwing main didn't take into account the implicit try. We were creating one, then just leaving it off. This patch actually passes the modified AST branch through to the emitted return statement, rather than just keeping the call. As a separate note, I hit an assert saying that a source range must either be completely invalid or completely valid when just passing the invalid SourceLoc to the TryExpr. I've changed it to just take the SourceLoc from the implicit CallExpr, which should be the same as the invalid SourceLoc, but seems to inherit it from another place.
1 parent 7afb399 commit efb50e1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,14 +1952,14 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
19521952

19531953
if (mainFunction->hasThrows()) {
19541954
auto *tryExpr = new (context) TryExpr(
1955-
SourceLoc(), callExpr, context.TheEmptyTupleType, /*implicit=*/true);
1955+
callExpr->getLoc(), callExpr, context.TheEmptyTupleType, /*implicit=*/true);
19561956
returnedExpr = tryExpr;
19571957
} else {
19581958
returnedExpr = callExpr;
19591959
}
19601960

19611961
auto *returnStmt =
1962-
new (context) ReturnStmt(SourceLoc(), callExpr, /*Implicit=*/true);
1962+
new (context) ReturnStmt(SourceLoc(), returnedExpr, /*Implicit=*/true);
19631963

19641964
SmallVector<ASTNode, 1> stmts;
19651965
stmts.push_back(returnStmt);
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s
2+
// RUN: %target-swift-frontend -dump-ast -parse-as-library %s | %FileCheck %s --check-prefix=CHECK-AST
23

34
@main
45
struct MyBase {
5-
static func main() throws {
6+
static func main() throws {
67
}
78
}
89

10+
// CHECK-AST: (func_decl implicit "$main()" interface type='(MyBase.Type) -> () throws -> ()' access=internal type
11+
// CHECK-AST-NEXT: (parameter "self")
12+
// CHECK-AST-NEXT: (parameter_list)
13+
// CHECK-AST-NEXT: (brace_stmt implicit
14+
// CHECK-AST-NEXT: (return_stmt implicit
15+
// CHECK-AST-NEXT: (try_expr implicit
16+
// CHECK-AST-NEXT: (call_expr implicit type='()'

0 commit comments

Comments
 (0)