|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace FSharp.Compiler.UnitTests |
| 4 | + |
| 5 | +open NUnit.Framework |
| 6 | +open FSharp.Compiler.SourceCodeServices |
| 7 | + |
| 8 | +[<TestFixture>] |
| 9 | +module ``Type Mismatch`` = |
| 10 | + |
| 11 | + [<Test>] |
| 12 | + let ``return Instead Of return!``() = |
| 13 | + CompilerAssert.TypeCheckSingleError |
| 14 | + """ |
| 15 | +let rec foo() = async { return foo() } |
| 16 | + """ |
| 17 | + FSharpErrorSeverity.Error |
| 18 | + 1 |
| 19 | + (2, 32, 2, 37) |
| 20 | + "Type mismatch. Expecting a\n ''a' \nbut given a\n 'Async<'a>' \nThe types ''a' and 'Async<'a>' cannot be unified. Consider using 'return!' instead of 'return'." |
| 21 | + |
| 22 | + [<Test>] |
| 23 | + let ``yield Instead Of yield!``() = |
| 24 | + CompilerAssert.TypeCheckSingleError |
| 25 | + """ |
| 26 | +type Foo() = |
| 27 | + member this.Yield(x) = [x] |
| 28 | +
|
| 29 | +let rec f () = Foo() { yield f ()} |
| 30 | + """ |
| 31 | + FSharpErrorSeverity.Error |
| 32 | + 1 |
| 33 | + (5, 30, 5, 34) |
| 34 | + "Type mismatch. Expecting a\n ''a' \nbut given a\n ''a list' \nThe types ''a' and ''a list' cannot be unified. Consider using 'yield!' instead of 'yield'." |
| 35 | + |
| 36 | + [<Test>] |
| 37 | + let ``Ref Cell Instead Of Not``() = |
| 38 | + CompilerAssert.TypeCheckSingleError |
| 39 | + """ |
| 40 | +let x = true |
| 41 | +if !x then |
| 42 | + printfn "hello" |
| 43 | + """ |
| 44 | + FSharpErrorSeverity.Error |
| 45 | + 1 |
| 46 | + (3, 5, 3, 6) |
| 47 | + "This expression was expected to have type\n 'bool ref' \nbut here has type\n 'bool' \r\nThe '!' operator is used to dereference a ref cell. Consider using 'not expr' here." |
| 48 | + |
| 49 | + [<Test>] |
| 50 | + let ``Ref Cell Instead Of Not 2``() = |
| 51 | + CompilerAssert.TypeCheckSingleError |
| 52 | + """ |
| 53 | +let x = true |
| 54 | +let y = !x |
| 55 | + """ |
| 56 | + FSharpErrorSeverity.Error |
| 57 | + 1 |
| 58 | + (3, 10, 3, 11) |
| 59 | + "This expression was expected to have type\n ''a ref' \nbut here has type\n 'bool' \r\nThe '!' operator is used to dereference a ref cell. Consider using 'not expr' here." |
| 60 | + |
| 61 | + [<Test>] |
| 62 | + let ``Guard Has Wrong Type``() = |
| 63 | + CompilerAssert.TypeCheckWithErrors |
| 64 | + """ |
| 65 | +let x = 1 |
| 66 | +match x with |
| 67 | +| 1 when "s" -> true |
| 68 | +| _ -> false |
| 69 | + """ |
| 70 | + [| |
| 71 | + FSharpErrorSeverity.Error, 1, (4, 10, 4, 13), "A pattern match guard must be of type 'bool', but this 'when' expression is of type 'string'." |
| 72 | + FSharpErrorSeverity.Warning, 20, (3, 1, 5, 13), "The result of this expression has type 'bool' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'." |
| 73 | + |] |
| 74 | + |
| 75 | + [<Test>] |
| 76 | + let ``Runtime Type Test In Pattern``() = |
| 77 | + CompilerAssert.TypeCheckWithErrors |
| 78 | + """ |
| 79 | +open System.Collections.Generic |
| 80 | +
|
| 81 | +let orig = Dictionary<obj,obj>() |
| 82 | +
|
| 83 | +let c = |
| 84 | + match orig with |
| 85 | + | :? IDictionary<obj,obj> -> "yes" |
| 86 | + | _ -> "no" |
| 87 | + """ |
| 88 | + [| |
| 89 | + FSharpErrorSeverity.Warning, 67, (8, 5, 8, 28), "This type test or downcast will always hold" |
| 90 | + FSharpErrorSeverity.Error, 193, (8, 5, 8, 28), "Type constraint mismatch. The type \n 'IDictionary<obj,obj>' \nis not compatible with type\n 'Dictionary<obj,obj>' \n" |
| 91 | + |] |
| 92 | + |
| 93 | + [<Test>] |
| 94 | + let ``Runtime Type Test In Pattern 2``() = |
| 95 | + CompilerAssert.TypeCheckWithErrors |
| 96 | + """ |
| 97 | +open System.Collections.Generic |
| 98 | +
|
| 99 | +let orig = Dictionary<obj,obj>() |
| 100 | +
|
| 101 | +let c = |
| 102 | + match orig with |
| 103 | + | :? IDictionary<obj,obj> as y -> "yes" + y.ToString() |
| 104 | + | _ -> "no" |
| 105 | + """ |
| 106 | + [| |
| 107 | + FSharpErrorSeverity.Warning, 67, (8, 5, 8, 28), "This type test or downcast will always hold" |
| 108 | + FSharpErrorSeverity.Error, 193, (8, 5, 8, 28), "Type constraint mismatch. The type \n 'IDictionary<obj,obj>' \nis not compatible with type\n 'Dictionary<obj,obj>' \n" |
| 109 | + |] |
| 110 | + |
| 111 | + [<Test>] |
| 112 | + let ``Override Errors``() = |
| 113 | + CompilerAssert.TypeCheckWithErrors |
| 114 | + """ |
| 115 | +type Base() = |
| 116 | + abstract member Member: int * string -> string |
| 117 | + default x.Member (i, s) = s |
| 118 | +
|
| 119 | +type Derived1() = |
| 120 | + inherit Base() |
| 121 | + override x.Member() = 5 |
| 122 | +
|
| 123 | +type Derived2() = |
| 124 | + inherit Base() |
| 125 | + override x.Member (i : int) = "Hello" |
| 126 | +
|
| 127 | +type Derived3() = |
| 128 | + inherit Base() |
| 129 | + override x.Member (s : string, i : int) = sprintf "Hello %s" s |
| 130 | + """ |
| 131 | + [| |
| 132 | + FSharpErrorSeverity.Error, 856, (8, 16, 8, 22), "This override takes a different number of arguments to the corresponding abstract member. The following abstract members were found:\r\n abstract member Base.Member : int * string -> string" |
| 133 | + FSharpErrorSeverity.Error, 856, (12, 16, 12, 22), "This override takes a different number of arguments to the corresponding abstract member. The following abstract members were found:\r\n abstract member Base.Member : int * string -> string" |
| 134 | + FSharpErrorSeverity.Error, 1, (16, 24, 16, 34), "This expression was expected to have type\n 'int' \nbut here has type\n 'string' " |
| 135 | + |] |
0 commit comments