|
| 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 ``Classes`` = |
| 10 | + |
| 11 | + [<Test>] |
| 12 | + let ``Tuple In Abstract Method``() = |
| 13 | + CompilerAssert.TypeCheckWithErrors |
| 14 | + """ |
| 15 | +type IInterface = |
| 16 | + abstract Function : (int32 * int32) -> unit |
| 17 | +
|
| 18 | +let x = |
| 19 | + { new IInterface with |
| 20 | + member this.Function (i, j) = () |
| 21 | + } |
| 22 | + """ |
| 23 | + [| |
| 24 | + FSharpErrorSeverity.Error, 768, (7, 16, 7, 36), "The member 'Function' does not accept the correct number of arguments. 1 argument(s) are expected, but 2 were given. The required signature is 'member IInterface.Function : (int32 * int32) -> unit'.\nA tuple type is required for one or more arguments. Consider wrapping the given arguments in additional parentheses or review the definition of the interface." |
| 25 | + FSharpErrorSeverity.Error, 17, (7, 21, 7, 29), "The member 'Function : 'a * 'b -> unit' does not have the correct type to override the corresponding abstract method. The required signature is 'Function : (int32 * int32) -> unit'." |
| 26 | + FSharpErrorSeverity.Error, 783, (6, 9, 6, 19), "At least one override did not correctly implement its corresponding abstract member" |
| 27 | + |] |
| 28 | + |
| 29 | + [<Test>] |
| 30 | + let ``Wrong Arity``() = |
| 31 | + CompilerAssert.TypeCheckSingleError |
| 32 | + """ |
| 33 | +type MyType() = |
| 34 | + static member MyMember(arg1, arg2:int ) = () |
| 35 | + static member MyMember(arg1, arg2:byte) = () |
| 36 | +
|
| 37 | +
|
| 38 | +MyType.MyMember("", 0, 0) |
| 39 | + """ |
| 40 | + FSharpErrorSeverity.Error |
| 41 | + 503 |
| 42 | + (7, 1, 7, 26) |
| 43 | + "A member or object constructor 'MyMember' taking 3 arguments is not accessible from this code location. All accessible versions of method 'MyMember' take 2 arguments." |
| 44 | + |
| 45 | + [<Test>] |
| 46 | + let ``Method Is Not Static``() = |
| 47 | + CompilerAssert.TypeCheckSingleError |
| 48 | + """ |
| 49 | +type Class1() = |
| 50 | + member this.X() = "F#" |
| 51 | +
|
| 52 | +let x = Class1.X() |
| 53 | + """ |
| 54 | + FSharpErrorSeverity.Error |
| 55 | + 3214 |
| 56 | + (5, 9, 5, 17) |
| 57 | + "Method or object constructor 'X' is not static" |
| 58 | + |
| 59 | + [<Test>] |
| 60 | + let ``Matching Method With Same Name Is Not Abstract``() = |
| 61 | + CompilerAssert.TypeCheckWithErrors |
| 62 | + """ |
| 63 | +type Foo(x : int) = |
| 64 | + member v.MyX() = x |
| 65 | +
|
| 66 | +let foo = |
| 67 | + { new Foo(3) |
| 68 | + with |
| 69 | + member v.MyX() = 4 } |
| 70 | + """ |
| 71 | + [| |
| 72 | + FSharpErrorSeverity.Error, 767, (8, 16, 8, 23), "The type Foo contains the member 'MyX' but it is not a virtual or abstract method that is available to override or implement." |
| 73 | + FSharpErrorSeverity.Error, 17, (8, 18, 8, 21), "The member 'MyX : unit -> int' does not have the correct type to override any given virtual method" |
| 74 | + FSharpErrorSeverity.Error, 783, (6, 11, 6, 14), "At least one override did not correctly implement its corresponding abstract member" |
| 75 | + |] |
| 76 | + |
| 77 | + [<Test>] |
| 78 | + let ``No Matching Abstract Method With Same Name``() = |
| 79 | + CompilerAssert.TypeCheckWithErrors |
| 80 | + """ |
| 81 | +type IInterface = |
| 82 | + abstract MyFunction : int32 * int32 -> unit |
| 83 | + abstract SomeOtherFunction : int32 * int32 -> unit |
| 84 | +
|
| 85 | +let x = |
| 86 | + { new IInterface with |
| 87 | + member this.Function (i, j) = () |
| 88 | + } |
| 89 | + """ |
| 90 | + [| |
| 91 | + FSharpErrorSeverity.Error, 767, (8, 14, 8, 34), "The member 'Function' does not correspond to any abstract or virtual method available to override or implement." |
| 92 | + FSharpErrorSeverity.Error, 17, (8, 19, 8, 27), "The member 'Function : 'a * 'b -> unit' does not have the correct type to override any given virtual method" |
| 93 | + FSharpErrorSeverity.Error, 366, (7, 3, 9, 4), "No implementation was given for those members: \r\n\t'abstract member IInterface.MyFunction : int32 * int32 -> unit'\r\n\t'abstract member IInterface.SomeOtherFunction : int32 * int32 -> unit'\r\nNote that all interface members must be implemented and listed under an appropriate 'interface' declaration, e.g. 'interface ... with member ...'." |
| 94 | + FSharpErrorSeverity.Error, 783, (7, 9, 7, 19), "At least one override did not correctly implement its corresponding abstract member" |
| 95 | + |] |
| 96 | + |
| 97 | + [<Test>] |
| 98 | + let ``Member Has Multiple Possible Dispatch Slots``() = |
| 99 | + CompilerAssert.TypeCheckWithErrors |
| 100 | + """ |
| 101 | +type IOverload = |
| 102 | + abstract member Bar : int -> int |
| 103 | + abstract member Bar : double -> int |
| 104 | +
|
| 105 | +type Overload = |
| 106 | + interface IOverload with |
| 107 | + override __.Bar _ = 1 |
| 108 | + """ |
| 109 | + [| |
| 110 | + FSharpErrorSeverity.Error, 366, (7, 15, 7, 24), "No implementation was given for those members: \r\n\t'abstract member IOverload.Bar : double -> int'\r\n\t'abstract member IOverload.Bar : int -> int'\r\nNote that all interface members must be implemented and listed under an appropriate 'interface' declaration, e.g. 'interface ... with member ...'." |
| 111 | + FSharpErrorSeverity.Error, 3213, (8, 21, 8, 24), "The member 'Bar<'a0> : 'a0 -> int' matches multiple overloads of the same method.\nPlease restrict it to one of the following:\r\n Bar : double -> int\r\n Bar : int -> int." |
| 112 | + |] |
| 113 | + |
| 114 | + [<Test>] |
| 115 | + let ``Do Cannot Have Visibility Declarations``() = |
| 116 | + CompilerAssert.ParseWithErrors |
| 117 | + """ |
| 118 | +type X() = |
| 119 | + do () |
| 120 | + private do () |
| 121 | + static member Y() = 1 |
| 122 | + """ |
| 123 | + [| |
| 124 | + FSharpErrorSeverity.Error, 531, (4, 5, 4, 12), "Accessibility modifiers should come immediately prior to the identifier naming a construct" |
| 125 | + FSharpErrorSeverity.Error, 512, (4, 13, 4, 18), "Accessibility modifiers are not permitted on 'do' bindings, but 'Private' was given." |
| 126 | + FSharpErrorSeverity.Error, 222, (2, 1, 3, 1), "Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'. Only the last source file of an application may omit such a declaration." |
| 127 | + |] |
0 commit comments