From bdb2b79e06bcb1b515ba4243103a9ff3cfbb2ea2 Mon Sep 17 00:00:00 2001 From: Sergey Tihon Date: Fri, 19 Jul 2019 21:02:07 +0300 Subject: [PATCH] Moving TypeMismatchTests over to NUnit (#7250) * Moving TypeMismatchTests over to NUnit * ci restart --- .../ErrorMessages/TypeMismatchTests.fs | 135 ++++++++++++++++++ tests/fsharp/FSharpSuite.Tests.fsproj | 1 + .../Source/Warnings/GuardHasWrongType.fs | 9 -- .../Source/Warnings/OverrideErrors.fs | 22 --- .../Source/Warnings/RefCellInsteadOfNot.fs | 9 -- .../Source/Warnings/RefCellInsteadOfNot2.fs | 8 -- .../Warnings/ReturnInsteadOfReturnBang.fs | 10 -- .../Warnings/RuntimeTypeTestInPattern.fs | 13 -- .../Warnings/RuntimeTypeTestInPattern2.fs | 13 -- .../Warnings/YieldInsteadOfYieldBang.fs | 13 -- tests/fsharpqa/Source/Warnings/env.lst | 8 -- 11 files changed, 136 insertions(+), 105 deletions(-) create mode 100644 tests/fsharp/Compiler/ErrorMessages/TypeMismatchTests.fs delete mode 100644 tests/fsharpqa/Source/Warnings/GuardHasWrongType.fs delete mode 100644 tests/fsharpqa/Source/Warnings/OverrideErrors.fs delete mode 100644 tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot.fs delete mode 100644 tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot2.fs delete mode 100644 tests/fsharpqa/Source/Warnings/ReturnInsteadOfReturnBang.fs delete mode 100644 tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern.fs delete mode 100644 tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern2.fs delete mode 100644 tests/fsharpqa/Source/Warnings/YieldInsteadOfYieldBang.fs diff --git a/tests/fsharp/Compiler/ErrorMessages/TypeMismatchTests.fs b/tests/fsharp/Compiler/ErrorMessages/TypeMismatchTests.fs new file mode 100644 index 00000000000..652361045c9 --- /dev/null +++ b/tests/fsharp/Compiler/ErrorMessages/TypeMismatchTests.fs @@ -0,0 +1,135 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +namespace FSharp.Compiler.UnitTests + +open NUnit.Framework +open FSharp.Compiler.SourceCodeServices + +[] +module ``Type Mismatch`` = + + [] + let ``return Instead Of return!``() = + CompilerAssert.TypeCheckSingleError + """ +let rec foo() = async { return foo() } + """ + FSharpErrorSeverity.Error + 1 + (2, 32, 2, 37) + "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'." + + [] + let ``yield Instead Of yield!``() = + CompilerAssert.TypeCheckSingleError + """ +type Foo() = + member this.Yield(x) = [x] + +let rec f () = Foo() { yield f ()} + """ + FSharpErrorSeverity.Error + 1 + (5, 30, 5, 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'." + + [] + let ``Ref Cell Instead Of Not``() = + CompilerAssert.TypeCheckSingleError + """ +let x = true +if !x then + printfn "hello" + """ + FSharpErrorSeverity.Error + 1 + (3, 5, 3, 6) + "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." + + [] + let ``Ref Cell Instead Of Not 2``() = + CompilerAssert.TypeCheckSingleError + """ +let x = true +let y = !x + """ + FSharpErrorSeverity.Error + 1 + (3, 10, 3, 11) + "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." + + [] + let ``Guard Has Wrong Type``() = + CompilerAssert.TypeCheckWithErrors + """ +let x = 1 +match x with +| 1 when "s" -> true +| _ -> false + """ + [| + FSharpErrorSeverity.Error, 1, (4, 10, 4, 13), "A pattern match guard must be of type 'bool', but this 'when' expression is of type 'string'." + 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'." + |] + + [] + let ``Runtime Type Test In Pattern``() = + CompilerAssert.TypeCheckWithErrors + """ +open System.Collections.Generic + +let orig = Dictionary() + +let c = + match orig with + | :? IDictionary -> "yes" + | _ -> "no" + """ + [| + FSharpErrorSeverity.Warning, 67, (8, 5, 8, 28), "This type test or downcast will always hold" + FSharpErrorSeverity.Error, 193, (8, 5, 8, 28), "Type constraint mismatch. The type \n 'IDictionary' \nis not compatible with type\n 'Dictionary' \n" + |] + + [] + let ``Runtime Type Test In Pattern 2``() = + CompilerAssert.TypeCheckWithErrors + """ +open System.Collections.Generic + +let orig = Dictionary() + +let c = + match orig with + | :? IDictionary as y -> "yes" + y.ToString() + | _ -> "no" + """ + [| + FSharpErrorSeverity.Warning, 67, (8, 5, 8, 28), "This type test or downcast will always hold" + FSharpErrorSeverity.Error, 193, (8, 5, 8, 28), "Type constraint mismatch. The type \n 'IDictionary' \nis not compatible with type\n 'Dictionary' \n" + |] + + [] + let ``Override Errors``() = + CompilerAssert.TypeCheckWithErrors + """ +type Base() = + abstract member Member: int * string -> string + default x.Member (i, s) = s + +type Derived1() = + inherit Base() + override x.Member() = 5 + +type Derived2() = + inherit Base() + override x.Member (i : int) = "Hello" + +type Derived3() = + inherit Base() + override x.Member (s : string, i : int) = sprintf "Hello %s" s + """ + [| + 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" + 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" + FSharpErrorSeverity.Error, 1, (16, 24, 16, 34), "This expression was expected to have type\n 'int' \nbut here has type\n 'string' " + |] diff --git a/tests/fsharp/FSharpSuite.Tests.fsproj b/tests/fsharp/FSharpSuite.Tests.fsproj index 0c9578f2192..9d80400c401 100644 --- a/tests/fsharp/FSharpSuite.Tests.fsproj +++ b/tests/fsharp/FSharpSuite.Tests.fsproj @@ -38,6 +38,7 @@ + diff --git a/tests/fsharpqa/Source/Warnings/GuardHasWrongType.fs b/tests/fsharpqa/Source/Warnings/GuardHasWrongType.fs deleted file mode 100644 index ac0c3cb4f3c..00000000000 --- a/tests/fsharpqa/Source/Warnings/GuardHasWrongType.fs +++ /dev/null @@ -1,9 +0,0 @@ -// #Warnings -//A pattern match guard must be of type 'bool' - -let x = 1 -match x with -| 1 when "s" -> true -| _ -> false - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/OverrideErrors.fs b/tests/fsharpqa/Source/Warnings/OverrideErrors.fs deleted file mode 100644 index ff03ba6ed4e..00000000000 --- a/tests/fsharpqa/Source/Warnings/OverrideErrors.fs +++ /dev/null @@ -1,22 +0,0 @@ -// #Warnings -//This override takes a different number of arguments to the corresponding abstract member. The following abstract members were found: -//abstract member Base.Member : int * string -> string -//This expression was expected to have type - -type Base() = - abstract member Member: int * string -> string - default x.Member (i, s) = s - -type Derived1() = - inherit Base() - override x.Member() = 5 - -type Derived2() = - inherit Base() - override x.Member (i : int) = "Hello" - -type Derived3() = - inherit Base() - override x.Member (s : string, i : int) = sprintf "Hello %s" s - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot.fs b/tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot.fs deleted file mode 100644 index 1c6987bfa63..00000000000 --- a/tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot.fs +++ /dev/null @@ -1,9 +0,0 @@ -// #Warnings -//This expression was expected to have type -//The '!' operator is used to dereference a ref cell. Consider using 'not expr' here. - -let x = true -if !x then - printfn "hello" - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot2.fs b/tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot2.fs deleted file mode 100644 index 090934d8f56..00000000000 --- a/tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot2.fs +++ /dev/null @@ -1,8 +0,0 @@ -// #Warnings -//This expression was expected to have type -//The '!' operator is used to dereference a ref cell. Consider using 'not expr' here. - -let x = true -let y = !x - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/ReturnInsteadOfReturnBang.fs b/tests/fsharpqa/Source/Warnings/ReturnInsteadOfReturnBang.fs deleted file mode 100644 index d9f87c521bd..00000000000 --- a/tests/fsharpqa/Source/Warnings/ReturnInsteadOfReturnBang.fs +++ /dev/null @@ -1,10 +0,0 @@ -// #Warnings -//Type mismatch. Expecting a -//''a' -//but given a -//'Async<'a>' -//The types ''a' and 'Async<'a>' cannot be unified. Consider using 'return!' instead of 'return'. - -let rec foo() = async { return foo() } - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern.fs b/tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern.fs deleted file mode 100644 index 67ae796376d..00000000000 --- a/tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern.fs +++ /dev/null @@ -1,13 +0,0 @@ -// #Warnings -//Type constraint mismatch. The type - -open System.Collections.Generic - -let orig = Dictionary() - -let c = - match orig with - | :? IDictionary -> "yes" - | _ -> "no" - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern2.fs b/tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern2.fs deleted file mode 100644 index 098969482c5..00000000000 --- a/tests/fsharpqa/Source/Warnings/RuntimeTypeTestInPattern2.fs +++ /dev/null @@ -1,13 +0,0 @@ -// #Warnings -//Type constraint mismatch. The type - -open System.Collections.Generic - -let orig = Dictionary() - -let c = - match orig with - | :? IDictionary as y -> "yes" + y.ToString() - | _ -> "no" - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/YieldInsteadOfYieldBang.fs b/tests/fsharpqa/Source/Warnings/YieldInsteadOfYieldBang.fs deleted file mode 100644 index bd547fa7b28..00000000000 --- a/tests/fsharpqa/Source/Warnings/YieldInsteadOfYieldBang.fs +++ /dev/null @@ -1,13 +0,0 @@ -// #Warnings -//Type mismatch. Expecting a -//''a' -//but given a -//''a list' -//The types ''a' and ''a list' cannot be unified. Consider using 'yield!' instead of 'yield'. - -type Foo() = - member this.Yield(x) = [x] - -let rec f () = Foo() { yield f ()} - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/env.lst b/tests/fsharpqa/Source/Warnings/env.lst index a3d677bf0fc..18ef811a2a5 100644 --- a/tests/fsharpqa/Source/Warnings/env.lst +++ b/tests/fsharpqa/Source/Warnings/env.lst @@ -1,10 +1,7 @@ SOURCE=WrongNumericLiteral.fs # WrongNumericLiteral.fs - SOURCE=ReturnInsteadOfReturnBang.fs # ReturnInsteadOfReturnBang.fs - SOURCE=YieldInsteadOfYieldBang.fs # YieldInsteadOfYieldBang.fs SOURCE=TupleInAbstractMethod.fs # TupleInAbstractMethod.fs SOURCE=FS0988AtEndOfFile.fs # FS0988AtEndOfFile.fs SOURCE=WrongArity.fs # WrongArity.fs - SOURCE=OverrideErrors.fs # OverrideErrors.fs SOURCE=MethodIsNotStatic.fs # MethodIsNotStatic.fs SOURCE=EqualsInsteadOfInInForLoop.fs # EqualsInsteadOfInInForLoop.fs SOURCE=DontWarnExternalFunctionAsUnused.fs SCFLAGS="--warnon:1182 --warnaserror+" # DontWarnExternalFunctionAsUnused.fs @@ -28,17 +25,12 @@ SOURCE=DontSuggestWhenThingsAreOpen.fs SCFLAGS="--vserrors" # DontSuggestWhenThingsAreOpen.fs SOURCE=SuggestDoubleBacktickIdentifiers.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickIdentifiers.fs SOURCE=SuggestDoubleBacktickUnions.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickUnions.fs - SOURCE=GuardHasWrongType.fs # GuardHasWrongType.fs SOURCE=MatchingMethodWithSameNameIsNotAbstract.fs # MatchingMethodWithSameNameIsNotAbstract.fs SOURCE=NoMatchingAbstractMethodWithSameName.fs # NoMatchingAbstractMethodWithSameName.fs SOURCE=MissingExpressionAfterLet.fs # MissingExpressionAfterLet.fs SOURCE=SuggestFieldsInCtor.fs # SuggestFieldsInCtor.fs SOURCE=FieldSuggestion.fs # FieldSuggestion.fs SOURCE=SuggestToUseIndexer.fs # SuggestToUseIndexer.fs - SOURCE=RefCellInsteadOfNot.fs # RefCellInsteadOfNot.fs - SOURCE=RefCellInsteadOfNot2.fs # RefCellInsteadOfNot2.fs - SOURCE=RuntimeTypeTestInPattern.fs # RuntimeTypeTestInPattern.fs - SOURCE=RuntimeTypeTestInPattern2.fs # RuntimeTypeTestInPattern2.fs SOURCE=MemberHasMultiplePossibleDispatchSlots.fs # MemberHasMultiplePossibleDispatchSlots.fs SOURCE=Repro1548.fs SCFLAGS="-r:Repro1548.dll" # Repro1548.fs SOURCE=DoCannotHaveVisibilityDeclarations.fs