-
Notifications
You must be signed in to change notification settings - Fork 847
Resolve issue with implicit yields requiring Zero #10556
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b25e828
Resolve issue with implicit yields requiring Zero - even when it is n…
laenas 9e56648
Remove dead folder reference from FSPROJ
laenas ac4d547
Add unit tests for Computation Expression Zero requirement behavior
laenas 95729f5
Add comment with guidance on the handling of Zero
laenas 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
95 changes: 95 additions & 0 deletions
95
tests/FSharp.Compiler.ComponentTests/Language/ComputationExpressionTests.fs
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,95 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace FSharp.Compiler.ComponentTests.Language | ||
|
|
||
| open Xunit | ||
| open FSharp.Test.Utilities.Compiler | ||
|
|
||
| module ComputationExpressionTests = | ||
| [<Fact>] | ||
| let ``A CE not using Zero does not require Zero``() = | ||
| FSharp """ | ||
| module ComputationExpressionTests | ||
| type ListBuilder () = | ||
| member __.Combine (a: List<'T>, b) = a @ b | ||
| member __.Yield x = List.singleton x | ||
| member __.Delay expr = expr () : List<'T> | ||
|
|
||
| let lb = ListBuilder () | ||
|
|
||
| let x = lb {1; 2;} | ||
| """ | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> ignore | ||
|
|
||
| [<Fact>] | ||
| let ``A CE explicitly using Zero fails without a defined Zero``() = | ||
| FSharp """ | ||
| module ComputationExpressionTests | ||
| type ListBuilder () = | ||
| member __.Combine (a: List<'T>, b) = a @ b | ||
| member __.Yield x = List.singleton x | ||
| member __.Delay expr = expr () : List<'T> | ||
|
|
||
| let lb = ListBuilder () | ||
|
|
||
| let x = lb {1; 2;()} | ||
| """ | ||
| |> compile | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 39, Line 10, Col 18, Line 10, Col 20, "The type 'ListBuilder' does not define the field, constructor or member 'Zero'.") | ||
| |> ignore | ||
|
|
||
| [<Fact>] | ||
| let ``A CE explicitly using Zero succeeds with a defined Zero``() = | ||
| FSharp """ | ||
| module ComputationExpressionTests | ||
| type ListBuilder () = | ||
| member __.Zero () = [] | ||
| member __.Combine (a: List<'T>, b) = a @ b | ||
| member __.Yield x = List.singleton x | ||
| member __.Delay expr = expr () : List<'T> | ||
|
|
||
| let lb = ListBuilder () | ||
|
|
||
| let x = lb {1; 2;()} | ||
| """ | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> ignore | ||
|
|
||
| [<Fact>] | ||
| let ``A CE with a complete if-then expression does not require Zero``() = | ||
| FSharp """ | ||
| module ComputationExpressionTests | ||
| type ListBuilder () = | ||
| member __.Combine (a: List<'T>, b) = a @ b | ||
| member __.Yield x = List.singleton x | ||
| member __.Delay expr = expr () : List<'T> | ||
|
|
||
| let lb = ListBuilder () | ||
|
|
||
| let x = lb {1; 2; if true then 3 else 4;} | ||
| """ | ||
| |> compile | ||
| |> shouldSucceed | ||
| |> ignore | ||
|
|
||
| [<Fact>] | ||
| let ``A CE with a missing/empty else branch implicitly requires Zero``() = | ||
| FSharp """ | ||
| module ComputationExpressionTests | ||
| type ListBuilder () = | ||
| member __.Combine (a: List<'T>, b) = a @ b | ||
| member __.Yield x = List.singleton x | ||
| member __.Delay expr = expr () : List<'T> | ||
|
|
||
| let lb = ListBuilder () | ||
|
|
||
| let x = lb {1; 2; if true then 3;} | ||
| """ | ||
| |> compile | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 708, Line 10, Col 19, Line 10, Col 31, "This control construct may only be used if the computation expression builder defines a 'Zero' method") | ||
| |> ignore |
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.