diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs new file mode 100644 index 00000000000..26966e72782 --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/AbbreviationTests.fs @@ -0,0 +1,189 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +module ErrorMessages.AbbreviationTests + +open Xunit +open FSharp.Test.Compiler + +[] +let ``Members 01`` () = + Fsx """ +type StringAlias = string + +type StringAlias with + member x.Length = x.Length + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 17, "Type abbreviations cannot have augmentations") + (Error 895, Line 5, Col 5, Line 5, Col 31, "Type abbreviations cannot have members") + ] + +[] +let ``Members 02 - Interface impl`` () = + Fsx """ +type IntAlias = int + +type IntAlias with + interface System.IComparable with + member x.CompareTo(obj) = 0 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 14, "Type abbreviations cannot have augmentations") + (Error 906, Line 5, Col 15, Line 5, Col 33, "Type abbreviations cannot have interface declarations") + (Error 909, Line 5, Col 15, Line 5, Col 33, "All implemented interfaces should be declared on the initial declaration of the type") + ] + +[] +let ``Members 03 - Members and interface`` () = + Fsx """ +type FloatAlias = float + +type FloatAlias with + member x.IsNaN = System.Double.IsNaN(x) + interface System.IConvertible + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 16, "Type abbreviations cannot have augmentations") + (Error 906, Line 6, Col 15, Line 6, Col 34, "Type abbreviations cannot have interface declarations") + (Error 909, Line 6, Col 15, Line 6, Col 34, "All implemented interfaces should be declared on the initial declaration of the type") + ] + +[] +let ``Multiple types 01`` () = + Fsx """ +type ListAlias = int list +type ArrayAlias = string[] + +type ListAlias with + member x.Head = x.Head + +type ArrayAlias with + member x.Length = x.Length + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 5, Col 6, Line 5, Col 15, "Type abbreviations cannot have augmentations") + (Error 895, Line 6, Col 5, Line 6, Col 27, "Type abbreviations cannot have members") + (Error 964, Line 8, Col 6, Line 8, Col 16, "Type abbreviations cannot have augmentations") + (Error 895, Line 9, Col 5, Line 9, Col 31, "Type abbreviations cannot have members") + ] + +[] +let ``Multiple types 02 - With interface`` () = + Fsx """ +type ArrayAlias = string[] + +type ArrayAlias with + interface System.Collections.IEnumerable with + member x.GetEnumerator() = null + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 16, "Type abbreviations cannot have augmentations") + (Error 906, Line 5, Col 15, Line 5, Col 45, "Type abbreviations cannot have interface declarations") + (Error 909, Line 5, Col 15, Line 5, Col 45, "All implemented interfaces should be declared on the initial declaration of the type") + ] +[] +let ``Nested 01`` () = + Fsx """ +namespace Test + +type Alias1 = int + +module Nested = + type Alias2 = string + + type Alias1 with + member x.Value = x + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 9, Col 10, Line 9, Col 16, "Type abbreviations cannot have augmentations"); + (Error 895, Line 10, Col 9, Line 10, Col 27, "Type abbreviations cannot have members"); + ] + +[] +let ``Nested 02 - Different namespace`` () = + Fsx """ +namespace Test + +module Nested = + type Alias2 = string + + type Alias2 with + interface System.IComparable with + member x.CompareTo(obj) = 0 + +open Nested + +type Alias2 with + member x.ToUpper() = x.ToUpper() + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 7, Col 10, Line 7, Col 16, "Type abbreviations cannot have augmentations"); + (Error 906, Line 8, Col 19, Line 8, Col 37, "Type abbreviations cannot have interface declarations"); + (Error 909, Line 8, Col 19, Line 8, Col 37, "All implemented interfaces should be declared on the initial declaration of the type"); + (Error 964, Line 13, Col 6, Line 13, Col 12, "Type abbreviations cannot have augmentations"); + (Error 895, Line 14, Col 5, Line 14, Col 37, "Type abbreviations cannot have members"); + (Error 644, Line 14, Col 14, Line 14, Col 21, "Namespaces cannot contain extension members except in the same file and namespace declaration group where the type is defined. Consider using a module to hold declarations of extension members."); + ] + +[] +let ``Generic 01`` () = + Fsx """ +type Result<'T> = Result<'T, string> + +type Result<'T> with + member x.IsOk = match x with Ok _ -> true | Error _ -> false + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 12, "Type abbreviations cannot have augmentations"); + (Error 895, Line 5, Col 5, Line 5, Col 65, "Type abbreviations cannot have members"); + ] + +[] +let ``Generic 02 - Interface`` () = + Fsx """ +type MyList<'a> = 'a list + +type MyList<'a> with + interface seq<'a> with + member x.GetEnumerator() = (x :> seq<'a>).GetEnumerator() + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 12, "Type abbreviations cannot have augmentations"); + (Error 906, Line 5, Col 15, Line 5, Col 22, "Type abbreviations cannot have interface declarations"); + (Error 909, Line 5, Col 15, Line 5, Col 22, "All implemented interfaces should be declared on the initial declaration of the type") + ] + +[] +let ``Property getters and setters`` () = + Fsx """ +type IntRef = int ref + +type IntRef with + member x.Value + with get() = !x + and set(v) = x := v + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 964, Line 4, Col 6, Line 4, Col 12, "Type abbreviations cannot have augmentations") + (Error 895, Line 5, Col 5, Line 6, Col 1, "Type abbreviations cannot have members") + ] \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/NamespaceTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/NamespaceTests.fs new file mode 100644 index 00000000000..f989220189e --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/NamespaceTests.fs @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +module ErrorMessages.NamespaceTests + +open Xunit +open FSharp.Test.Compiler + +[] +let ``Value bindings 01`` () = + Fsx """ +namespace TestNamespace + +let x = 1 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 201, Line 4, Col 5, Line 4, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + ] + +[] +let ``Value bindings 02 - Multiple`` () = + Fsx """ +namespace TestNamespace + +let x = 1 +let y = 2 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 201, Line 4, Col 5, Line 4, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + (Error 201, Line 5, Col 5, Line 5, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + ] + +[] +let ``Function bindings`` () = + Fsx """ +namespace TestNamespace + +let add x y = x + y + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 201, Line 4, Col 5, Line 4, Col 12, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + ] + +[] +let ``Multiple namespaces`` () = + Fsx """ +namespace Namespace1 + +let x = 1 + +namespace Namespace2 + +let y = 2 + +namespace Namespace3 + +let z = 3 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 201, Line 4, Col 5, Line 4, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + (Error 201, Line 8, Col 5, Line 8, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + (Error 201, Line 12, Col 5, Line 12, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + ] + +[] +let ``Do expressions`` () = + Fsx """ +namespace TestNamespace + +do printfn "test" + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 201, Line 4, Col 1, Line 4, Col 18, "Namespaces cannot contain values. Consider using a module to hold your value declarations.") + ] \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnitOfMeasureTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnitOfMeasureTests.fs index 36ef76e772d..8e49da35fbd 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnitOfMeasureTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnitOfMeasureTests.fs @@ -6,7 +6,7 @@ open Xunit open FSharp.Test.Compiler [] -let ``Error - Expected unit-of-measure type parameter must be marked with the [] attribute.`` () = +let ``Missing Measure attribute on type parameter`` () = Fsx """ type A<[]'u>(x : int<'u>) = member this.X = x @@ -33,7 +33,7 @@ type FooExt = ] [] -let ``Expected unit-of-measure type parameter must be marked with the [] attribute.`` () = +let ``With Measure attribute on type parameter`` () = Fsx """ type A<[]'u>(x : int<'u>) = member this.X = x @@ -50,3 +50,194 @@ type FooExt = |> typecheck |> shouldSucceed +[] +let ``Instance members 01`` () = + Fsx """ +[] +type kg = + member x.Value = 1.0 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 897, Line 4, Col 5, Line 4, Col 25, "Measure declarations may have only static members") + ] + +[] +let ``Instance members 02 - Multiple`` () = + Fsx """ +[] +type kg = + member x.Value = 1.0 + member x.GetWeight() = 2.0 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 897, Line 4, Col 5, Line 4, Col 25, "Measure declarations may have only static members") + (Error 897, Line 5, Col 5, Line 5, Col 31, "Measure declarations may have only static members") + ] + +[] +let ``Constructors`` () = + Fsx """ +[] +type meter = + new() = { } + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 904, Line 4, Col 5, Line 4, Col 16, "Measure declarations may have only static members: constructors are not available") + ] + +[] +let ``Type parameters`` () = + Fsx """ +[] +type meter<'a> = + class end + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 928, Line 3, Col 6, Line 3, Col 11, "Measure definitions cannot have type parameters"); + ] + +[] +let ``Inherit declarations`` () = + Fsx """ +[] +type Fahrenheit = + inherit Foo() + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 962, Line 4, Col 5, Line 4, Col 18, "This 'inherit' declaration has arguments, but is not in a type with a primary constructor. Consider adding arguments to your type definition, e.g. 'type X(args) = ...'.") + (Error 39, Line 4, Col 13, Line 4, Col 16, "The type 'Foo' is not defined.") + (Error 897, Line 4, Col 5, Line 4, Col 18, "Measure declarations may have only static members") + ] + +[] +let ``Instance let bindings`` () = + Fsx """ +[] +type Celsius = + let instanceValue = 10 + do printfn "init" + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 963, Line 4, Col 5, Line 4, Col 27, "This definition may only be used in a type with a primary constructor. Consider adding arguments to your type definition, e.g. 'type X(args) = ...'.") + (Error 897, Line 4, Col 5, Line 4, Col 27, "Measure declarations may have only static members") + (Error 897, Line 5, Col 5, Line 5, Col 22, "Measure declarations may have only static members") + ] + +[] +let ``Mixed valid and invalid 01`` () = + Fsx """ +[] +type Kelvin = + static member AbsoluteZero = -273.15 // OK + member x.Value = 0.0 // Error: instance member + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 897, Line 5, Col 5, Line 5, Col 25, "Measure declarations may have only static members") + ] + +[] +let ``Mixed valid and invalid 02 - Constructor`` () = + Fsx """ +[] +type Kelvin = + static member AbsoluteZero = -273.15 // OK + new() = { } // Error: constructor + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 904, Line 5, Col 5, Line 5, Col 16, "Measure declarations may have only static members: constructors are not available") + ] +[] +let ``Implicit constructor`` () = + Fsx """ +[] +type newton() = + let force = 10.0 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 897, Line 3, Col 6, Line 3, Col 12, "Measure declarations may have only static members") + (Error 897, Line 4, Col 5, Line 4, Col 21, "Measure declarations may have only static members") + ] + +[] +let ``Augmentations 01`` () = + Fsx """ +[] +type joule + +type joule with + member x.Energy = 1.0 + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 897, Line 6, Col 5, Line 6, Col 26, "Measure declarations may have only static members") + ] + +[] +let ``Augmentations 02 - Multiple errors`` () = + Fsx """ +[] +type joule + +type joule with + member x.Energy = 1.0 + new() = { } + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 897, Line 6, Col 5, Line 6, Col 26, "Measure declarations may have only static members") + (Error 904, Line 7, Col 5, Line 7, Col 16, "Measure declarations may have only static members: constructors are not available") + ] + +[] +let ``Complex with type parameters`` () = + Fsx """ +[] +type pascal<'a> = + new() = { } + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 928, Line 3, Col 6, Line 3, Col 12, "Measure definitions cannot have type parameters") + (Error 904, Line 4, Col 5, Line 4, Col 16, "Measure declarations may have only static members: constructors are not available") + ] + +[] +let ``Let binding order`` () = + Fsx """ +[] +type volt = + static let y = 2 // OK + let z = 3 + member this.Voltage = 0.0 + static member Current = 1.0 // OK + do printfn "hello" + """ + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 960, Line 8, Col 5, Line 8, Col 23, "'let' and 'do' bindings must come before member and interface definitions in type definitions") + (Error 897, Line 5, Col 5, Line 5, Col 14, "Measure declarations may have only static members") + (Error 897, Line 8, Col 5, Line 8, Col 23, "Measure declarations may have only static members") + (Error 897, Line 6, Col 5, Line 6, Col 30, "Measure declarations may have only static members") + ] \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj index 952624f9fcd..f0cb13ea71e 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj +++ b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj @@ -226,6 +226,8 @@ + +