Skip to content

Sema: Only diagnose 'throws' in 'defer' as part of TypeCheckError [5.2] #29687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions lib/Sema/TypeCheckError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,6 @@ class Context {

Kind TheKind;
bool DiagnoseErrorOnTry = false;
bool isInDefer = false;
DeclContext *RethrowsDC = nullptr;
InterpolatedStringLiteralExpr *InterpolatedString = nullptr;

Expand Down Expand Up @@ -899,9 +898,7 @@ class Context {
}

static Context forDeferBody() {
Context result(Kind::DeferBody);
result.isInDefer = true;
return result;
return Context(Kind::DeferBody);
}

static Context forInitializer(Initializer *init) {
Expand Down Expand Up @@ -979,18 +976,13 @@ class Context {

static void diagnoseThrowInIllegalContext(DiagnosticEngine &Diags,
ASTNode node,
StringRef description,
bool throwInDefer = false) {
if (auto *e = node.dyn_cast<Expr*>())
StringRef description) {
if (auto *e = node.dyn_cast<Expr*>()) {
if (isa<ApplyExpr>(e)) {
Diags.diagnose(e->getLoc(), diag::throwing_call_in_illegal_context,
description);
return;
}

if (throwInDefer) {
// Return because this would've already been diagnosed in TypeCheckStmt.
return;
}

Diags.diagnose(node.getStartLoc(), diag::throw_in_illegal_context,
Expand Down Expand Up @@ -1156,7 +1148,7 @@ class Context {
diagnoseThrowInIllegalContext(Diags, E, "a catch guard expression");
return;
case Kind::DeferBody:
diagnoseThrowInIllegalContext(Diags, E, "a defer body", isInDefer);
diagnoseThrowInIllegalContext(Diags, E, "a defer body");
return;
}
llvm_unreachable("bad context kind");
Expand Down
7 changes: 0 additions & 7 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
}

Stmt *visitThrowStmt(ThrowStmt *TS) {
// If the throw is in a defer, then it isn't valid.
if (isInDefer()) {
getASTContext().Diags.diagnose(TS->getThrowLoc(),
diag::jump_out_of_defer, "throw");
return nullptr;
}

// Coerce the operand to the exception type.
auto E = TS->getSubExpr();

Expand Down
64 changes: 63 additions & 1 deletion test/stmt/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,30 +371,92 @@ enum DeferThrowError: Error {
}

func throwInDefer() {
defer { throw DeferThrowError.someError } // expected-error {{'throw' cannot transfer control out of a defer statement}}
defer { throw DeferThrowError.someError } // expected-error {{errors cannot be thrown out of a defer body}}
print("Foo")
}

func throwInDeferOK1() {
defer {
do {
throw DeferThrowError.someError
} catch {}
}
print("Bar")
}

func throwInDeferOK2() throws {
defer {
do {
throw DeferThrowError.someError
} catch {}
}
print("Bar")
}

func throwingFuncInDefer1() throws {
defer { try throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
print("Bar")
}

func throwingFuncInDefer1a() throws {
defer {
do {
try throwingFunctionCalledInDefer()
} catch {}
}
print("Bar")
}

func throwingFuncInDefer2() throws {
defer { throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
print("Bar")
}

func throwingFuncInDefer2a() throws {
defer {
do {
throwingFunctionCalledInDefer()
// expected-error@-1 {{call can throw but is not marked with 'try'}}
// expected-note@-2 {{did you mean to use 'try'?}}
// expected-note@-3 {{did you mean to handle error as optional value?}}
// expected-note@-4 {{did you mean to disable error propagation?}}
} catch {}
}
print("Bar")
}

func throwingFuncInDefer3() {
defer { try throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
print("Bar")
}

func throwingFuncInDefer3a() {
defer {
do {
try throwingFunctionCalledInDefer()
} catch {}
}
print("Bar")
}

func throwingFuncInDefer4() {
defer { throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
print("Bar")
}

func throwingFuncInDefer4a() {
defer {
do {
throwingFunctionCalledInDefer()
// expected-error@-1 {{call can throw but is not marked with 'try'}}
// expected-note@-2 {{did you mean to use 'try'?}}
// expected-note@-3 {{did you mean to handle error as optional value?}}
// expected-note@-4 {{did you mean to disable error propagation?}}
} catch {}
}
print("Bar")
}

func throwingFunctionCalledInDefer() throws {
throw DeferThrowError.someError
}
Expand Down