Skip to content

Use any Error for caught error type of an exhaustive, non-throwing do..catch #71129

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
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
11 changes: 10 additions & 1 deletion lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3628,8 +3628,17 @@ Type TypeChecker::catchErrorType(DeclContext *dc, DoCatchStmt *stmt) {
stmt->getBody(), EffectKind::Throws);

// If it doesn't throw at all, the type is Never.
if (!classification.hasThrows())
if (!classification.hasThrows()) {
// Source compatibility: if the do..catch was already exhaustive,
// and we aren't doing full typed throws, treat the caught error
// type as 'any Error' to allow pattern-matches to continue to
// type check.
if (!ctx.LangOpts.hasFeature(Feature::FullTypedThrows) &&
stmt->isSyntacticallyExhaustive())
return ctx.getErrorExistentialType();

return ctx.getNeverType();
}

return classification.getThrownError();
}
Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,20 @@ struct One<Two> { // expected-note{{'Two' declared as parameter to type 'One'}}
}
}

enum HomeworkError: Error {
case dogAteIt
case forgot
}

func testOne() {
do {
} catch let error { // expected-warning{{'catch' block is unreachable because no errors are thrown in 'do' block}}
if case One.E.SomeError = error {} // expected-error{{generic parameter 'Two' could not be inferred}}
}
do {
} catch HomeworkError.forgot { // expected-warning{{'catch' block is unreachable because no errors are thrown in 'do' block}}
} catch {
}
}

// https://github.com/apple/swift/issues/50875
Expand Down
6 changes: 3 additions & 3 deletions test/IDE/complete_exception.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ func test006() {
} catch {
#^INSIDE_CATCH1^#
}
// IMPLICIT_ERROR: Decl[LocalVar]/Local: error[#Never#]; name=error
// IMPLICIT_ERROR: Decl[LocalVar]/Local: error[#any Error#]; name=error
}
func test007() {
do {
} catch let e {
#^INSIDE_CATCH2^#
}
// EXPLICIT_ERROR_E: Decl[LocalVar]/Local: e[#Never#]; name=e
// EXPLICIT_ERROR_E: Decl[LocalVar]/Local: e[#any Error#]; name=e
}
func test008() {
do {
Expand Down Expand Up @@ -195,7 +195,7 @@ func test012() {
error.#^INSIDE_CATCH_ERR_DOT1^#
}
}
// ERROR_DOT: Keyword[self]/CurrNominal: self[#Never#]; name=self
// ERROR_DOT: Keyword[self]/CurrNominal: self[#any Error#]; name=self
func test013() {
do {
} catch let e {
Expand Down
6 changes: 2 additions & 4 deletions test/Parse/errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ func one() {
do {

} catch { // expected-warning {{'catch' block is unreachable because no errors are thrown in 'do' block}}
let error2 = error // expected-warning{{constant 'error2' inferred to have type 'Never', which is an enum with no cases}}
// expected-note@-1{{add an explicit type annotation to silence this warning}}
let error2 = error
}

do {
} catch where true { // expected-warning {{'catch' block is unreachable because no errors are thrown in 'do' block}}
let error2 = error // expected-warning{{constant 'error2' inferred to have type 'Never', which is an enum with no cases}}
// expected-note@-1{{add an explicit type annotation to silence this warning}}
let error2 = error
} catch {
}

Expand Down
11 changes: 11 additions & 0 deletions test/stmt/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ func testTryIncompatibleTyped(cond: Bool) throws(HomeworkError) {
}
}

func doSomethingWithoutThrowing() { }

func testDoCatchWithoutThrowing() {
do {
try doSomethingWithoutThrowing() // expected-warning{{no calls to throwing functions occur within 'try' expression}}
} catch HomeworkError.forgot { // expected-warning{{'catch' block is unreachable because no errors are thrown in 'do' block}}
// expected-error@-1{{pattern of type 'HomeworkError' cannot match 'Never'}}
} catch {
}
}

// "Rethrow-like" functions are only allowed to be called from rethrows
// functions as a compatibility hack, which is removed under FullTypedThrows.
func rethrowsLike<E>(_ body: () throws(E) -> Void) throws(E) { }
Expand Down