Skip to content

Commit 34fa48f

Browse files
committed
[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.
1 parent 78daa41 commit 34fa48f

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ ERROR(single_value_stmt_branch_must_end_in_result,none,
13311331
"non-expression branch of '%0' expression may only end with a 'throw'",
13321332
(StmtKind))
13331333
ERROR(cannot_jump_in_single_value_stmt,none,
1334-
"cannot '%0' in '%1' when used as expression",
1334+
"cannot use '%0' to transfer control out of '%1' expression",
13351335
(StmtKind, StmtKind))
13361336
WARNING(effect_marker_on_single_value_stmt,none,
13371337
"'%0' has no effect on '%1' expression", (StringRef, StmtKind))

test/Constraints/issue-71273.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func testLocalFn() {
1515
func testLocalBinding() {
1616
bar() {
1717
let _ = if .random() { return () } else { 0 }
18-
// expected-error@-1 {{cannot 'return' in 'if' when used as expression}}
18+
// expected-error@-1 {{cannot use 'return' to transfer control out of 'if' expression}}
1919
return ()
2020
}
2121
}

test/Constraints/rdar114402042.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func test() {
88
foo {
99
bar(if true { return } else { return })
1010
// expected-error@-1 {{'if' may only be used as expression in return, throw, or as the source of an assignment}}
11-
// expected-error@-2 2{{cannot 'return' in 'if' when used as expression}}
11+
// expected-error@-2 2{{cannot use 'return' to transfer control out of 'if' expression}}
1212
}
1313
foo {
1414
bar(if true { { return } } else { { return } })

test/expr/unary/do_expr.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func testReturn1() -> Int {
147147
try throwsError()
148148
} catch {
149149
if .random() {
150-
return 0 // expected-error {{cannot 'return' in 'do-catch' when used as expression}}
150+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'do-catch' expression}}
151151
}
152152
then 0
153153
}

test/expr/unary/if_expr.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ struct TestFailableInit {
340340
let y = if x {
341341
0
342342
} else {
343-
return nil // expected-error {{cannot 'return' in 'if' when used as expression}}
343+
return nil // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
344344
}
345345
_ = y
346346
}
@@ -605,16 +605,16 @@ func returnBranches1() -> Int {
605605

606606
func returnBranchVoid() {
607607
return if .random() { return } else { return () }
608-
// expected-error@-1 2{{cannot 'return' in 'if' when used as expression}}
608+
// expected-error@-1 2{{cannot use 'return' to transfer control out of 'if' expression}}
609609
}
610610

611611
func returnBranchBinding() -> Int {
612612
let x = if .random() {
613613
// expected-warning@-1 {{constant 'x' inferred to have type 'Void', which may be unexpected}}
614614
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
615-
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
615+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
616616
} else {
617-
return 1 // expected-error {{cannot 'return' in 'if' when used as expression}}
617+
return 1 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
618618
}
619619
return x // expected-error {{cannot convert return expression of type 'Void' to return type 'Int'}}
620620
}
@@ -648,9 +648,9 @@ func returnBranches5() throws -> Int {
648648
let i = if .random() {
649649
// expected-warning@-1 {{constant 'i' inferred to have type 'Void', which may be unexpected}}
650650
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
651-
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
651+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
652652
} else {
653-
return 1 // expected-error {{cannot 'return' in 'if' when used as expression}}
653+
return 1 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
654654
}
655655
let j = if .random() {
656656
// expected-warning@-1 {{constant 'j' inferred to have type 'Void', which may be unexpected}}
@@ -702,15 +702,15 @@ func returnBranches6PoundIf2() -> Int {
702702
func returnBranches7() -> Int {
703703
let i = if .random() {
704704
print("hello")
705-
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
705+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
706706
} else {
707707
1
708708
}
709709
return i
710710
}
711711

712712
func returnBranches8() -> Int {
713-
let i = if .random() { return 1 } else { 0 } // expected-error {{cannot 'return' in 'if' when used as expression}}
713+
let i = if .random() { return 1 } else { 0 } // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
714714
return i
715715
}
716716

@@ -914,7 +914,7 @@ func break1() -> Int {
914914
switch true {
915915
case true:
916916
let j = if .random() {
917-
break // expected-error {{cannot 'break' in 'if' when used as expression}}
917+
break // expected-error {{cannot use 'break' to transfer control out of 'if' expression}}
918918
} else {
919919
0
920920
}
@@ -927,7 +927,7 @@ func break1() -> Int {
927927
func continue1() -> Int {
928928
for _ in 0 ... 5 {
929929
let i = if true { continue } else { 1 }
930-
// expected-error@-1 {{cannot 'continue' in 'if' when used as expression}}
930+
// expected-error@-1 {{cannot use 'continue' to transfer control out of 'if' expression}}
931931
return i
932932
}
933933
}
@@ -941,7 +941,7 @@ func return1() -> Int {
941941
while true {
942942
switch 0 {
943943
default:
944-
return 0 // expected-error {{cannot 'return' in 'if' when used as expression}}
944+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'if' expression}}
945945
}
946946
}
947947
}

test/expr/unary/switch_expr.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ struct TestFailableInit {
400400
case true:
401401
0
402402
case false:
403-
return nil // expected-error {{cannot 'return' in 'switch' when used as expression}}
403+
return nil // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
404404
}
405405
_ = y
406406
}
@@ -757,17 +757,17 @@ func returnBranches1() -> Int {
757757

758758
func returnBranchVoid() {
759759
return switch Bool.random() { case true: return case false: return () }
760-
// expected-error@-1 2{{cannot 'return' in 'switch' when used as expression}}
760+
// expected-error@-1 2{{cannot use 'return' to transfer control out of 'switch' expression}}
761761
}
762762

763763
func returnBranchBinding() -> Int {
764764
let x = switch Bool.random() {
765765
// expected-warning@-1 {{constant 'x' inferred to have type 'Void', which may be unexpected}}
766766
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
767767
case true:
768-
return 0 // expected-error {{cannot 'return' in 'switch' when used as expression}}
768+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
769769
case false:
770-
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
770+
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
771771
}
772772
return x // expected-error {{cannot convert return expression of type 'Void' to return type 'Int'}}
773773
}
@@ -807,9 +807,9 @@ func returnBranches5() -> Int {
807807
// expected-warning@-1 {{constant 'i' inferred to have type 'Void', which may be unexpected}}
808808
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
809809
case true:
810-
return 0 // expected-error {{cannot 'return' in 'switch' when used as expression}}
810+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
811811
case false:
812-
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
812+
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
813813
}
814814
return i // expected-error {{cannot convert return expression of type 'Void' to return type 'Int'}}
815815
}
@@ -861,7 +861,7 @@ func returnBranches7() -> Int {
861861
let i = switch Bool.random() {
862862
case true:
863863
print("hello")
864-
return 0 // expected-error {{cannot 'return' in 'switch' when used as expression}}
864+
return 0 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
865865
case false:
866866
1
867867
}
@@ -871,7 +871,7 @@ func returnBranches7() -> Int {
871871
func returnBranches8() -> Int {
872872
let i = switch Bool.random() {
873873
case true:
874-
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
874+
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
875875
case false:
876876
0
877877
}
@@ -1173,7 +1173,7 @@ func fallthrough2() -> Int {
11731173
if .random() {
11741174
fallthrough
11751175
}
1176-
return 1 // expected-error {{cannot 'return' in 'switch' when used as expression}}
1176+
return 1 // expected-error {{cannot use 'return' to transfer control out of 'switch' expression}}
11771177
case false:
11781178
0
11791179
}

0 commit comments

Comments
 (0)