diff --git a/tests/fsharp/Compiler/ErrorMessages/UpcastDowncastTests.fs b/tests/fsharp/Compiler/ErrorMessages/UpcastDowncastTests.fs new file mode 100644 index 00000000000..739f841bfca --- /dev/null +++ b/tests/fsharp/Compiler/ErrorMessages/UpcastDowncastTests.fs @@ -0,0 +1,51 @@ +// 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 ``Upcast and Downcast`` = + + [] + let ``Downcast Instead Of Upcast``() = + CompilerAssert.TypeCheckSingleError + """ +open System.Collections.Generic + +let orig = Dictionary() :> IDictionary +let c = orig :> Dictionary + """ + FSharpErrorSeverity.Error + 193 + (5, 9, 5, 36) + "Type constraint mismatch. The type \n 'IDictionary' \nis not compatible with type\n 'Dictionary' \n" + + [] + let ``Upcast Instead Of Downcast``() = + CompilerAssert.TypeCheckWithErrors + """ +open System.Collections.Generic + +let orig = Dictionary() +let c = orig :?> IDictionary + """ + [| + FSharpErrorSeverity.Warning, 67, (5, 9, 5, 38), "This type test or downcast will always hold" + FSharpErrorSeverity.Error, 3198, (5, 9, 5, 38), "The conversion from Dictionary to IDictionary is a compile-time safe upcast, not a downcast. Consider using the :> (upcast) operator instead of the :?> (downcast) operator." + |] + + [] + let ``Upcast Function Instead Of Downcast``() = + CompilerAssert.TypeCheckWithErrors + """ +open System.Collections.Generic + +let orig = Dictionary() +let c : IDictionary = downcast orig + """ + [| + FSharpErrorSeverity.Warning, 67, (5, 32, 5, 45), "This type test or downcast will always hold" + FSharpErrorSeverity.Error, 3198, (5, 32, 5, 45), "The conversion from Dictionary to IDictionary is a compile-time safe upcast, not a downcast. Consider using 'upcast' instead of 'downcast'." + |] diff --git a/tests/fsharp/FSharpSuite.Tests.fsproj b/tests/fsharp/FSharpSuite.Tests.fsproj index 5976803c10e..c1781a387c8 100644 --- a/tests/fsharp/FSharpSuite.Tests.fsproj +++ b/tests/fsharp/FSharpSuite.Tests.fsproj @@ -35,6 +35,7 @@ + diff --git a/tests/fsharpqa/Source/Warnings/DowncastInsteadOfUpcast.fs b/tests/fsharpqa/Source/Warnings/DowncastInsteadOfUpcast.fs deleted file mode 100644 index a815425e294..00000000000 --- a/tests/fsharpqa/Source/Warnings/DowncastInsteadOfUpcast.fs +++ /dev/null @@ -1,9 +0,0 @@ -// #Warnings -//Type constraint mismatch. The type - -open System.Collections.Generic - -let orig = Dictionary() :> IDictionary -let c = orig :> Dictionary - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/UpcastFunctionInsteadOfDowncast.fs b/tests/fsharpqa/Source/Warnings/UpcastFunctionInsteadOfDowncast.fs deleted file mode 100644 index 6bc8c54c124..00000000000 --- a/tests/fsharpqa/Source/Warnings/UpcastFunctionInsteadOfDowncast.fs +++ /dev/null @@ -1,9 +0,0 @@ -// #Warnings -//The conversion from Dictionary to IDictionary is a compile-time safe upcast, not a downcast. Consider using 'upcast' instead of 'downcast'. - -open System.Collections.Generic - -let orig = Dictionary() -let c : IDictionary = downcast orig - -exit 0 \ No newline at end of file diff --git a/tests/fsharpqa/Source/Warnings/UpcastInsteadOfDowncast.fs b/tests/fsharpqa/Source/Warnings/UpcastInsteadOfDowncast.fs deleted file mode 100644 index 00962a1521b..00000000000 --- a/tests/fsharpqa/Source/Warnings/UpcastInsteadOfDowncast.fs +++ /dev/null @@ -1,9 +0,0 @@ -// #Warnings -//The conversion from Dictionary to IDictionary is a compile-time safe upcast, not a downcast. - -open System.Collections.Generic - -let orig = Dictionary() -let c = orig :?> IDictionary - -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 14b61a4649a..e82b777bad1 100644 --- a/tests/fsharpqa/Source/Warnings/env.lst +++ b/tests/fsharpqa/Source/Warnings/env.lst @@ -45,9 +45,6 @@ SOURCE=SuggestToUseIndexer.fs # SuggestToUseIndexer.fs SOURCE=RefCellInsteadOfNot.fs # RefCellInsteadOfNot.fs SOURCE=RefCellInsteadOfNot2.fs # RefCellInsteadOfNot2.fs - SOURCE=UpcastInsteadOfDowncast.fs # UpcastInsteadOfDowncast.fs - SOURCE=UpcastFunctionInsteadOfDowncast.fs # UpcastFunctionInsteadOfDowncast.fs - SOURCE=DowncastInsteadOfUpcast.fs # DowncastInsteadOfUpcast.fs SOURCE=RuntimeTypeTestInPattern.fs # RuntimeTypeTestInPattern.fs SOURCE=RuntimeTypeTestInPattern2.fs # RuntimeTypeTestInPattern2.fs SOURCE=WarnIfExpressionResultUnused.fs # WarnIfExpressionResultUnused.fs