forked from nosami/visualfsharp
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve issue with implicit yields requiring Zero (dotnet#10556)
- Loading branch information
Showing
3 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 |