Skip to content

Commit 60c915a

Browse files
forkiKevinRansom
authored andcommitted
Moving ElseBranchHasWrongTypeTests over to NUnit (#7104)
1 parent da432e1 commit 60c915a

13 files changed

Lines changed: 178 additions & 127 deletions
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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 ``Else branch has wrong type`` =
10+
11+
[<Test>]
12+
let ``Else branch is int while if branch is string``() =
13+
CompilerAssert.TypeCheckSingleError
14+
"""
15+
let test = 100
16+
let y =
17+
if test > 10 then "test"
18+
else 123
19+
"""
20+
FSharpErrorSeverity.Error
21+
1
22+
(5, 10, 5, 13)
23+
"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'."
24+
25+
[<Test>]
26+
let ``Else branch is a function that returns int while if branch is string``() =
27+
CompilerAssert.TypeCheckSingleError
28+
"""
29+
let test = 100
30+
let f x = test
31+
let y =
32+
if test > 10 then "test"
33+
else f 10
34+
"""
35+
FSharpErrorSeverity.Error
36+
1
37+
(6, 10, 6, 14)
38+
"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'."
39+
40+
41+
[<Test>]
42+
let ``Else branch is a sequence of expressions that returns int while if branch is string``() =
43+
CompilerAssert.TypeCheckSingleError
44+
"""
45+
let f x = x + 4
46+
47+
let y =
48+
if true then
49+
""
50+
else
51+
"" |> ignore
52+
(f 5)
53+
"""
54+
FSharpErrorSeverity.Error
55+
1
56+
(9, 10, 9, 13)
57+
"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'."
58+
59+
60+
[<Test>]
61+
let ``Else branch is a longer sequence of expressions that returns int while if branch is string``() =
62+
CompilerAssert.TypeCheckSingleError
63+
"""
64+
let f x = x + 4
65+
66+
let y =
67+
if true then
68+
""
69+
else
70+
"" |> ignore
71+
let z = f 4
72+
let a = 3 * z
73+
(f a)
74+
"""
75+
FSharpErrorSeverity.Error
76+
1
77+
(11, 10, 11, 13)
78+
"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'."
79+
80+
81+
[<Test>]
82+
let ``Else branch context doesn't propagate into function application``() =
83+
CompilerAssert.TypeCheckSingleError
84+
"""
85+
let test = 100
86+
let f x : string = x
87+
let y =
88+
if test > 10 then "test"
89+
else
90+
f 123
91+
"""
92+
FSharpErrorSeverity.Error
93+
1
94+
(7, 11, 7, 14)
95+
"This expression was expected to have type\n 'string' \nbut here has type\n 'int' "
96+
97+
[<Test>]
98+
let ``Else branch context doesn't propagate into function application even if not last expr``() =
99+
CompilerAssert.TypeCheckSingleError
100+
"""
101+
let test = 100
102+
let f x = printfn "%s" x
103+
let y =
104+
if test > 10 then "test"
105+
else
106+
f 123
107+
"test"
108+
"""
109+
FSharpErrorSeverity.Error
110+
1
111+
(7, 11, 7, 14)
112+
"This expression was expected to have type\n 'string' \nbut here has type\n 'int' "
113+
114+
[<Test>]
115+
let ``Else branch context doesn't propagate into for loop``() =
116+
CompilerAssert.TypeCheckSingleError
117+
"""
118+
let test = 100
119+
let list = [1..10]
120+
let y =
121+
if test > 10 then "test"
122+
else
123+
for (x:string) in list do
124+
printfn "%s" x
125+
126+
"test"
127+
"""
128+
FSharpErrorSeverity.Error
129+
1
130+
(7, 14, 7, 22)
131+
"This expression was expected to have type\n 'int' \nbut here has type\n 'string' "
132+
133+
[<Test>]
134+
let ``Else branch context doesn't propagate to lines before last line``() =
135+
CompilerAssert.TypeCheckSingleError
136+
"""
137+
let test = 100
138+
let list = [1..10]
139+
let y =
140+
if test > 10 then "test"
141+
else
142+
printfn "%s" 1
143+
144+
"test"
145+
"""
146+
FSharpErrorSeverity.Error
147+
1
148+
(7, 22, 7, 23)
149+
"This expression was expected to have type\n 'string' \nbut here has type\n 'int' "
150+
151+
[<Test>]
152+
let ``Else branch should not have wrong context type``() =
153+
CompilerAssert.TypeCheckWithErrors
154+
"""
155+
let x = 1
156+
let y : bool =
157+
if x = 2 then "A"
158+
else "B"
159+
"""
160+
[| FSharpErrorSeverity.Error, 1, (4, 19, 4, 22), "The 'if' expression needs to have type 'bool' to satisfy context type requirements. It currently has type 'string'."
161+
FSharpErrorSeverity.Error, 1, (5, 10, 5, 13), "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'." |]
162+
163+
164+
[<Test>]
165+
let ``Else branch has wrong type in nested if``() =
166+
CompilerAssert.TypeCheckWithErrors
167+
"""
168+
let x = 1
169+
if x = 1 then true
170+
else
171+
if x = 2 then "A"
172+
else "B"
173+
"""
174+
[| FSharpErrorSeverity.Error, 1, (5, 19, 5, 22), "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'."
175+
FSharpErrorSeverity.Error, 1, (6, 10, 6, 13), "All branches of an 'if' expression must return values of the same type as the first branch, which here is 'bool'. This branch returns a value of type 'string'."
176+
FSharpErrorSeverity.Warning, 20, (3, 1, 6, 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'." |]

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<PropertyGroup>
@@ -32,6 +32,7 @@
3232
<Compile Include="tests.fs" />
3333
<Compile Include="Compiler\ILChecker.fs" />
3434
<Compile Include="Compiler\CompilerAssert.fs" />
35+
<Compile Include="Compiler\ErrorMessages\ElseBranchHasWrongTypeTests.fs" />
3536
<Compile Include="Compiler\SourceTextTests.fs" />
3637
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
3738
<Compile Include="Compiler\Language\SpanOptimizationTests.fs" />

tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateInAppl.fs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateInAppl2.fs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateInForLoop.fs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchContextDoesntPropagateToLinesBeforeLastLine.fs

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchHasWrongContextType.fs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType3.fs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)