Files
swift-mirror/test/expr/unary/do_expr_disabled.swift
Hamish Knight c7ac0aa110 [Sema] Avoid forming nested do expressions in if/switch bindings
In asserts builds this hits an assert that the
feature isn't enabled, and in no-asserts builds
this incorrectly allows `do` expressions to be
used with the feature disabled. Note this only
affects their use when nested in an `if`/`switch`
that is used in a binding, we correctly handled
the other cases.

rdar://121193678
2024-01-18 16:18:49 +00:00

131 lines
4.1 KiB
Swift

// RUN: %target-typecheck-verify-swift
// Currently disabled by default.
func throwsError() throws -> Int { 0 }
func test1() -> Int {
return do { 5 }
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-2 {{non-void function should return a value}}
// expected-warning@-3 {{integer literal is unused}}
}
func test2() -> Int {
return do { try throwsError() } catch { 0 }
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-2 {{non-void function should return a value}}
// expected-warning@-3 {{integer literal is unused}}
// expected-warning@-4 {{result of call to 'throwsError()' is unused}}
}
func test3() -> Int {
return
do { 5 }
// expected-error@-2 {{non-void function should return a value}}
// expected-warning@-2 {{integer literal is unused}}
}
func test4() -> Int {
return
do { try throwsError() } catch { 0 }
// expected-error@-2 {{non-void function should return a value}}
// expected-warning@-2 {{integer literal is unused}}
// expected-warning@-3 {{result of call to 'throwsError()' is unused}}
}
func test5() -> Int {
do { 5 } // expected-warning {{integer literal is unused}}
}
func test6() -> Int {
do { try throwsError() } catch { 0 }
// expected-warning@-1 {{integer literal is unused}}
// expected-warning@-2 {{result of call to 'throwsError()' is unused}}
}
func test7() -> Int {
do { 5 } as Int
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-warning@-2 {{integer literal is unused}}
// expected-error@-3 {{expected expression}}
}
func test8() -> Int {
do { try throwsError() } catch { 0 } as Int
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-warning@-2 {{integer literal is unused}}
// expected-error@-3 {{expected expression}}
// expected-warning@-4 {{result of call to 'throwsError()' is unused}}
}
func test9() -> Int {
let x = do { 5 }
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-2 {{expected initial value after '='}}
// expected-warning@-3 {{integer literal is unused}}
return x
}
func test10() -> Int {
let x = do { try throwsError() } catch { 0 }
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-2 {{expected initial value after '='}}
// expected-warning@-3 {{integer literal is unused}}
// expected-warning@-4 {{result of call to 'throwsError()' is unused}}
return x
}
func test11() -> Int {
let fn = { do { 5 } }
// expected-warning@-1 {{integer literal is unused}}
return fn() // expected-error {{cannot convert return expression of type '()' to return type 'Int'}}
}
func test12() -> Int {
let fn = { do { try throwsError() } catch { 0 } }
// expected-warning@-1 {{integer literal is unused}}
// expected-warning@-2 {{result of call to 'throwsError()' is unused}}
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}}
}
}