From 0f1a6c443fbf5ce3bee7418952a3620ddf56ee92 Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Wed, 3 Jul 2019 13:39:25 +0200 Subject: [PATCH] Moving ElseBranchHasWrongTypeTests over to NUnit --- src/fsharp/TypeChecker.fs | 2 +- tests/fsharp/Compiler/CompilerAssert.fs | 9 ++--- .../ElseBranchHasWrongTypeTests.fs | 36 +++++++++++++++++++ tests/fsharp/FSharpSuite.Tests.fsproj | 1 + .../Source/Warnings/ElseBranchHasWrongType.fs | 9 ----- .../Warnings/ElseBranchHasWrongType2.fs | 10 ------ tests/fsharpqa/Source/Warnings/env.lst | 2 -- 7 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs delete mode 100644 tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs delete mode 100644 tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs diff --git a/src/fsharp/TypeChecker.fs b/src/fsharp/TypeChecker.fs index ae464f15758f..4956e5a97898 100644 --- a/src/fsharp/TypeChecker.fs +++ b/src/fsharp/TypeChecker.fs @@ -8604,7 +8604,7 @@ and Propagate cenv overallTy env tpenv (expr: ApplicableExpr) exprty delayed = else exprty - UnifyTypesAndRecover cenv env mExpr overallTy exprty + UnifyTypes cenv env mExpr overallTy exprty | DelayedDot :: _ | DelayedSet _ :: _ diff --git a/tests/fsharp/Compiler/CompilerAssert.fs b/tests/fsharp/Compiler/CompilerAssert.fs index 6a43e562eac8..029d9cf72d86 100644 --- a/tests/fsharp/Compiler/CompilerAssert.fs +++ b/tests/fsharp/Compiler/CompilerAssert.fs @@ -54,25 +54,26 @@ module CompilerAssert = lock lockObj <| fun () -> let parseResults, fileAnswer = checker.ParseAndCheckFileInProject("test.fs", 0, SourceText.ofString source, defaultProjectOptions) |> Async.RunSynchronously - Assert.True(parseResults.Errors.Length = 0, sprintf "Parse errors: %A" parseResults.Errors) + Assert.IsEmpty(parseResults.Errors, sprintf "Parse errors: %A" parseResults.Errors) match fileAnswer with | FSharpCheckFileAnswer.Aborted _ -> Assert.Fail("Type Checker Aborted") | FSharpCheckFileAnswer.Succeeded(typeCheckResults) -> - Assert.True(typeCheckResults.Errors.Length = 0, sprintf "Type Check errors: %A" typeCheckResults.Errors) + Assert.IsEmpty(typeCheckResults.Errors, sprintf "Type Check errors: %A" typeCheckResults.Errors) + let TypeCheckSingleError (source: string) (expectedErrorNumber: int) (expectedErrorRange: int * int * int * int) (expectedErrorMsg: string) = lock lockObj <| fun () -> let parseResults, fileAnswer = checker.ParseAndCheckFileInProject("test.fs", 0, SourceText.ofString source, defaultProjectOptions) |> Async.RunSynchronously - Assert.True(parseResults.Errors.Length = 0, sprintf "Parse errors: %A" parseResults.Errors) + Assert.IsEmpty(parseResults.Errors, sprintf "Parse errors: %A" parseResults.Errors) match fileAnswer with | FSharpCheckFileAnswer.Aborted _ -> Assert.Fail("Type Checker Aborted") | FSharpCheckFileAnswer.Succeeded(typeCheckResults) -> - Assert.True(typeCheckResults.Errors.Length = 1, sprintf "Expected one type check error: %A" typeCheckResults.Errors) + Assert.AreEqual(1, typeCheckResults.Errors.Length, sprintf "Expected one type check error: %A" typeCheckResults.Errors) typeCheckResults.Errors |> Array.iter (fun info -> Assert.AreEqual(FSharpErrorSeverity.Error, info.Severity) diff --git a/tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs b/tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs new file mode 100644 index 000000000000..c8a971c0fa73 --- /dev/null +++ b/tests/fsharp/Compiler/ErrorMessages/ElseBranchHasWrongTypeTests.fs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +namespace FSharp.Compiler.UnitTests + +open NUnit.Framework + +[] +module ElseBranchHasWrongTypeTests = + + [] + let ``Else branch is int while if branch is string``() = + CompilerAssert.TypeCheckSingleError + """ +let test = 100 +let y = + if test > 10 then "test" + else 123 + """ + 1 + (5, 9, 5, 12) + "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'." + + + [] + let ``Else branch is a function that returns int while if branch is string``() = + CompilerAssert.TypeCheckSingleError + """ +let test = 100 +let f x = test +let y = + if test > 10 then "test" + else f 10 + """ + 1 + (6, 9, 6, 13) + "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'." \ No newline at end of file diff --git a/tests/fsharp/FSharpSuite.Tests.fsproj b/tests/fsharp/FSharpSuite.Tests.fsproj index 56240b466b43..43b7183cb588 100644 --- a/tests/fsharp/FSharpSuite.Tests.fsproj +++ b/tests/fsharp/FSharpSuite.Tests.fsproj @@ -31,6 +31,7 @@ + diff --git a/tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs b/tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs deleted file mode 100644 index b14b495304ee..000000000000 --- a/tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs +++ /dev/null @@ -1,9 +0,0 @@ -// #Warnings -//All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'. - -let test = 100 -let y = - if test > 10 then "test" - else 123 - -exit 0 diff --git a/tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs b/tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs deleted file mode 100644 index 06d83f76aa01..000000000000 --- a/tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs +++ /dev/null @@ -1,10 +0,0 @@ -// #Warnings -//All branches of an 'if' expression must return values of the same type as the first branch, which here is 'string'. This branch returns a value of type 'int'. - -let test = 100 -let f x = test -let y = - if test > 10 then "test" - else f 10 - -exit 0 diff --git a/tests/fsharpqa/Source/Warnings/env.lst b/tests/fsharpqa/Source/Warnings/env.lst index 383c291e9069..9b94fa3df197 100644 --- a/tests/fsharpqa/Source/Warnings/env.lst +++ b/tests/fsharpqa/Source/Warnings/env.lst @@ -45,8 +45,6 @@ SOURCE=SuggestDoubleBacktickIdentifiers.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickIdentifiers.fs SOURCE=SuggestDoubleBacktickUnions.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickUnions.fs SOURCE=GuardHasWrongType.fs # GuardHasWrongType.fs - SOURCE=ElseBranchHasWrongType.fs # ElseBranchHasWrongType.fs - SOURCE=ElseBranchHasWrongType2.fs # ElseBranchHasWrongType2.fs SOURCE=ElseBranchHasWrongType3.fs # ElseBranchHasWrongType3.fs SOURCE=ElseBranchHasWrongType4.fs # ElseBranchHasWrongType4.fs SOURCE=NestedElseBranchHasWrongType.fs # NestedElseBranchHasWrongType.fs