- Notifications
You must be signed in to change notification settings - Fork 10.6k
Add docs for if/switch expr diagnostics #83872
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
Open
owenv wants to merge 2 commits into swiftlang:main Choose a base branch from owenv:owenv/stmt-expr-diags-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Branches have mismatching types | ||
| | ||
| Result values in `if` and `switch` expressions must have the same type. Resolve this error by providing a type annotation or adjusting the values to have matching types. | ||
| | ||
| The following code is invalid because one branch of the `if` expression produces a value of type `String`, and the other branch produces a value of type `Int`. The type of result can be either Int or String, but not both. | ||
| | ||
| ```swift | ||
| let result = if condition { | ||
| "hello, world!" // error: branches have mismatching types 'String' and 'Int' | ||
| } else { | ||
| 42 | ||
| } | ||
| ``` | ||
| | ||
| The same restriction applies to each `case` of a `switch` statement used as an expression: | ||
| | ||
| ```swift | ||
| let result = switch myNumber { | ||
| case 0: "hello, world!" // error: branches have mismatching types 'String' and 'Int' | ||
| case 1: 42 | ||
| default: "hello, again!" | ||
| } | ||
| ``` | ||
| | ||
| These errors can be resolved by adjusting the code so every branch returns a value of the same type. For example, it would be valid for every branch to produce a `String`: | ||
| | ||
| ```swift | ||
| let result = if condition { | ||
| "hello, world!" | ||
| } else { | ||
| "goodbye!" | ||
| } | ||
| ``` | ||
| | ||
| In other situations, it might be appropriate to use an `enum` or `protocol` type to represent the result. Both of these examples would also be valid: | ||
| | ||
| ```swift | ||
| enum MyEnum { | ||
| case message(String) | ||
| case number(Int) | ||
| } | ||
| | ||
| let result = if condition { | ||
| MyEnum.string("hello, world!") | ||
| } else { | ||
| MyEnum.number(42) | ||
| } | ||
| ``` | ||
| | ||
| ```swift | ||
| protocol MyProtocol { ... } | ||
| extension String: MyProtocol { ... } | ||
| extension Int: MyProtocol { ... } | ||
| | ||
| let result = if condition { | ||
| "hello, world!" as (any MyProtocol) | ||
| } else { | ||
| 42 as (any MyProtocol) | ||
| } | ||
| ``` | ||
| | ||
| Because each branch of an `if` or `switch` expression is typechecked independently, the following code is also invalid: | ||
| | ||
| ```swift | ||
| let result = if condition { | ||
| 0 // error: branches have mismatching types 'Int' and 'Double' | ||
| } else { | ||
| 1.0 | ||
| } | ||
| ``` | ||
| | ||
| Even though both `0` and `1.0` could represent a value of type `Double`, because each branch of the expression is typechecked independently `0` is inferred to be of type `Int` and `1.0` is inferred to be of type `Double`, causing a mismatch. In this case, an explicit type annotation can be used to ensure both literals are treated as a `Double`: | ||
| | ||
| ```swift | ||
| let result: Double = if condition { | ||
| 0 | ||
| } else { | ||
| 1.0 | ||
| } | ||
| ``` | ||
| | ||
| ```swift | ||
| let result = if condition { | ||
| 0 as Double | ||
| } else { | ||
| 1.0 | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Statement may only be used as an expression in return, throw, or as the source of an assignment | ||
| | ||
| Using an `if` or `switch` statement as an expression is only valid in certain contexts. Resolve this error by breaking up the expression or rewriting it into multiple statements. | ||
| | ||
| An `if` or `switch` statement is allowed to be used as an expression: | ||
| | ||
| * As the value of a return statement. For example: | ||
| | ||
| ```swift | ||
| func halveIfEven(value: Int) -> Int { | ||
| return if value % 2 == 0 { | ||
| value / 2 | ||
| } else { | ||
| value | ||
| } | ||
| } | ||
| ``` | ||
| | ||
| * As the value of a `throw` statement. For example: | ||
| | ||
| ```swift | ||
| func throwError(for errorCode: Int) throws { | ||
| throw switch errorCode { | ||
| case 1: MyError.foo | ||
| case 2: MyError.bar | ||
| default: MyError.default | ||
| } | ||
| } | ||
| ``` | ||
| | ||
| * As the value assigned to a variable. For example: | ||
| | ||
| ```swift | ||
| let result = if condition { | ||
| "hello" | ||
| } else { | ||
| "goodbye" | ||
| } | ||
| ``` | ||
| | ||
| In any other context, using `if` or `switch` as an expression is not allowed. For example, the following code is invalid because the`if` statement is used as as an operand of a binary operator expression: | ||
| | ||
| ```swift | ||
| // error: 'if' may only be used as expression in return, throw, or as the source of an assignment | ||
| let result = if condition1 { 1 } else { 2 } + 1 | ||
| ``` | ||
| | ||
| This error can be fixed by breaking the code up into multiple statements: | ||
| | ||
| ```swift | ||
| let x = if condition1 { 1 } else { 2 } | ||
| let result = x + 1 | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.