Skip to content

[Sema] Avoid forming nested do expressions in if/switch bindings #70987

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 1 commit into from
Jan 18, 2024
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
7 changes: 6 additions & 1 deletion lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2535,9 +2535,14 @@ SingleValueStmtExpr *SingleValueStmtExpr::createWithWrappedBranches(
if (!IS->isSyntacticallyExhaustive())
continue;
} else if (auto *DCS = dyn_cast<DoCatchStmt>(S)) {
if (!ctx.LangOpts.hasFeature(Feature::DoExpressions))
continue;
if (!DCS->isSyntacticallyExhaustive())
continue;
} else if (!isa<SwitchStmt>(S) && !isa<DoStmt>(S)) {
} else if (isa<DoStmt>(S)) {
if (!ctx.LangOpts.hasFeature(Feature::DoExpressions))
continue;
} else if (!isa<SwitchStmt>(S)) {
continue;
}
} else {
Expand Down
36 changes: 36 additions & 0 deletions test/expr/unary/do_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ func test14() -> Int {
return fn()
}

func test15() -> Int {
let x = if .random() {
do { 0 }
} else {
1
}
return x
}

func test16() -> Int {
let x = if .random() {
1
} else {
do { 2 } catch { 3 }
// expected-warning@-1 {{'catch' block is unreachable because no errors are thrown in 'do' block}}
}
return x
}

func test17() -> Int {
if .random() {
do { 0 }
} else {
1
}
}

func test18() -> Int {
if .random() {
1
} else {
do { 2 } catch { 3 }
// expected-warning@-1 {{'catch' block is unreachable because no errors are thrown in 'do' block}}
}
}

func testEmpty1() {
let _ = do {} // expected-error {{expected expression in branch of 'do' expression}}
}
Expand Down
36 changes: 36 additions & 0 deletions test/expr/unary/do_expr_disabled.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,39 @@ func test12() -> Int {

return fn() // expected-error {{cannot convert return expression of type '()' to return type 'Int'}}
}

func test13() -> Int {
let x = if .random() {
do { 0 } // expected-warning {{integer literal is unused}}
} else { // expected-error {{non-expression branch of 'if' expression may only end with a 'throw'}}
1
}
return x
}

func test14() -> Int {
let x = if .random() {
1
} else {
do { 2 } catch { 3 } // expected-warning 2{{integer literal is unused}}
// expected-warning@-1 {{'catch' block is unreachable because no errors are thrown in 'do' block}}
} // expected-error {{non-expression branch of 'if' expression may only end with a 'throw'}}
return x
}

func test15() -> Int {
if .random() {
do { 0 } // expected-warning {{integer literal is unused}}
} else {
1 // expected-warning {{integer literal is unused}}
}
}

func test16() -> Int {
if .random() {
1 // expected-warning {{integer literal is unused}}
} else {
do { 2 } catch { 3 } // expected-warning 2{{integer literal is unused}}
// expected-warning@-1 {{'catch' block is unreachable because no errors are thrown in 'do' block}}
}
}