mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[Sema] Improve wording of a diagnostic
Make it clear the issue is the transfer of control flow out of the `if`/`switch` expression. Technically things like `break` and `continue` are allowed, as long as they are not jumping out of the expression.
This commit is contained in:
@@ -1331,7 +1331,7 @@ ERROR(single_value_stmt_branch_must_end_in_result,none,
|
||||
"non-expression branch of '%0' expression may only end with a 'throw'",
|
||||
(StmtKind))
|
||||
ERROR(cannot_jump_in_single_value_stmt,none,
|
||||
"cannot '%0' in '%1' when used as expression",
|
||||
"cannot use '%0' to transfer control out of '%1' expression",
|
||||
(StmtKind, StmtKind))
|
||||
WARNING(effect_marker_on_single_value_stmt,none,
|
||||
"'%0' has no effect on '%1' expression", (StringRef, StmtKind))
|
||||
|
||||
@@ -15,7 +15,7 @@ func testLocalFn() {
|
||||
func testLocalBinding() {
|
||||
bar() {
|
||||
let _ = if .random() { return () } else { 0 }
|
||||
// expected-error@-1 {{cannot 'return' in 'if' when used as expression}}
|
||||
// expected-error@-1 {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
return ()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ func test() {
|
||||
foo {
|
||||
bar(if true { return } else { return })
|
||||
// expected-error@-1 {{'if' may only be used as expression in return, throw, or as the source of an assignment}}
|
||||
// expected-error@-2 2{{cannot 'return' in 'if' when used as expression}}
|
||||
// expected-error@-2 2{{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
}
|
||||
foo {
|
||||
bar(if true { { return } } else { { return } })
|
||||
|
||||
@@ -147,7 +147,7 @@ func testReturn1() -> Int {
|
||||
try throwsError()
|
||||
} catch {
|
||||
if .random() {
|
||||
return 0 // expected-error {{cannot 'return' in 'do-catch' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'do-catch' expression}}
|
||||
}
|
||||
then 0
|
||||
}
|
||||
|
||||
@@ -340,7 +340,7 @@ struct TestFailableInit {
|
||||
let y = if x {
|
||||
0
|
||||
} else {
|
||||
return nil // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return nil // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
}
|
||||
_ = y
|
||||
}
|
||||
@@ -605,16 +605,16 @@ func returnBranches1() -> Int {
|
||||
|
||||
func returnBranchVoid() {
|
||||
return if .random() { return } else { return () }
|
||||
// expected-error@-1 2{{cannot 'return' in 'if' when used as expression}}
|
||||
// expected-error@-1 2{{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
}
|
||||
|
||||
func returnBranchBinding() -> Int {
|
||||
let x = if .random() {
|
||||
// expected-warning@-1 {{constant 'x' inferred to have type 'Void', which may be unexpected}}
|
||||
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
|
||||
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
} else {
|
||||
return 1 // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return 1 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
}
|
||||
return x // expected-error {{cannot convert return expression of type 'Void' to return type 'Int'}}
|
||||
}
|
||||
@@ -648,9 +648,9 @@ func returnBranches5() throws -> Int {
|
||||
let i = if .random() {
|
||||
// expected-warning@-1 {{constant 'i' inferred to have type 'Void', which may be unexpected}}
|
||||
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
|
||||
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
} else {
|
||||
return 1 // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return 1 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
}
|
||||
let j = if .random() {
|
||||
// expected-warning@-1 {{constant 'j' inferred to have type 'Void', which may be unexpected}}
|
||||
@@ -702,7 +702,7 @@ func returnBranches6PoundIf2() -> Int {
|
||||
func returnBranches7() -> Int {
|
||||
let i = if .random() {
|
||||
print("hello")
|
||||
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
} else {
|
||||
1
|
||||
}
|
||||
@@ -710,7 +710,7 @@ func returnBranches7() -> Int {
|
||||
}
|
||||
|
||||
func returnBranches8() -> Int {
|
||||
let i = if .random() { return 1 } else { 0 } // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
let i = if .random() { return 1 } else { 0 } // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
return i
|
||||
}
|
||||
|
||||
@@ -914,7 +914,7 @@ func break1() -> Int {
|
||||
switch true {
|
||||
case true:
|
||||
let j = if .random() {
|
||||
break // expected-error {{cannot 'break' in 'if' when used as expression}}
|
||||
break // expected-error {{cannot use 'break' to transfer control out of 'if' expression}}
|
||||
} else {
|
||||
0
|
||||
}
|
||||
@@ -927,7 +927,7 @@ func break1() -> Int {
|
||||
func continue1() -> Int {
|
||||
for _ in 0 ... 5 {
|
||||
let i = if true { continue } else { 1 }
|
||||
// expected-error@-1 {{cannot 'continue' in 'if' when used as expression}}
|
||||
// expected-error@-1 {{cannot use 'continue' to transfer control out of 'if' expression}}
|
||||
return i
|
||||
}
|
||||
}
|
||||
@@ -941,7 +941,7 @@ func return1() -> Int {
|
||||
while true {
|
||||
switch 0 {
|
||||
default:
|
||||
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,7 +400,7 @@ struct TestFailableInit {
|
||||
case true:
|
||||
0
|
||||
case false:
|
||||
return nil // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return nil // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
}
|
||||
_ = y
|
||||
}
|
||||
@@ -757,7 +757,7 @@ func returnBranches1() -> Int {
|
||||
|
||||
func returnBranchVoid() {
|
||||
return switch Bool.random() { case true: return case false: return () }
|
||||
// expected-error@-1 2{{cannot 'return' in 'switch' when used as expression}}
|
||||
// expected-error@-1 2{{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
}
|
||||
|
||||
func returnBranchBinding() -> Int {
|
||||
@@ -765,9 +765,9 @@ func returnBranchBinding() -> Int {
|
||||
// expected-warning@-1 {{constant 'x' inferred to have type 'Void', which may be unexpected}}
|
||||
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
|
||||
case true:
|
||||
return 0 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
case false:
|
||||
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
}
|
||||
return x // expected-error {{cannot convert return expression of type 'Void' to return type 'Int'}}
|
||||
}
|
||||
@@ -807,9 +807,9 @@ func returnBranches5() -> Int {
|
||||
// expected-warning@-1 {{constant 'i' inferred to have type 'Void', which may be unexpected}}
|
||||
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
|
||||
case true:
|
||||
return 0 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
case false:
|
||||
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
}
|
||||
return i // expected-error {{cannot convert return expression of type 'Void' to return type 'Int'}}
|
||||
}
|
||||
@@ -861,7 +861,7 @@ func returnBranches7() -> Int {
|
||||
let i = switch Bool.random() {
|
||||
case true:
|
||||
print("hello")
|
||||
return 0 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 0 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
case false:
|
||||
1
|
||||
}
|
||||
@@ -871,7 +871,7 @@ func returnBranches7() -> Int {
|
||||
func returnBranches8() -> Int {
|
||||
let i = switch Bool.random() {
|
||||
case true:
|
||||
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
case false:
|
||||
0
|
||||
}
|
||||
@@ -1173,7 +1173,7 @@ func fallthrough2() -> Int {
|
||||
if .random() {
|
||||
fallthrough
|
||||
}
|
||||
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
|
||||
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
|
||||
case false:
|
||||
0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user