Skip to content

Commit 6d57e3b

Browse files
authored
More test for error recovery (#18669)
1 parent e22f251 commit 6d57e3b

File tree

4 files changed

+467
-2
lines changed

4 files changed

+467
-2
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
module ErrorMessages.AbbreviationTests
4+
5+
open Xunit
6+
open FSharp.Test.Compiler
7+
8+
[<Fact>]
9+
let ``Members 01`` () =
10+
Fsx """
11+
type StringAlias = string
12+
13+
type StringAlias with
14+
member x.Length = x.Length
15+
"""
16+
|> typecheck
17+
|> shouldFail
18+
|> withDiagnostics [
19+
(Error 964, Line 4, Col 6, Line 4, Col 17, "Type abbreviations cannot have augmentations")
20+
(Error 895, Line 5, Col 5, Line 5, Col 31, "Type abbreviations cannot have members")
21+
]
22+
23+
[<Fact>]
24+
let ``Members 02 - Interface impl`` () =
25+
Fsx """
26+
type IntAlias = int
27+
28+
type IntAlias with
29+
interface System.IComparable with
30+
member x.CompareTo(obj) = 0
31+
"""
32+
|> typecheck
33+
|> shouldFail
34+
|> withDiagnostics [
35+
(Error 964, Line 4, Col 6, Line 4, Col 14, "Type abbreviations cannot have augmentations")
36+
(Error 906, Line 5, Col 15, Line 5, Col 33, "Type abbreviations cannot have interface declarations")
37+
(Error 909, Line 5, Col 15, Line 5, Col 33, "All implemented interfaces should be declared on the initial declaration of the type")
38+
]
39+
40+
[<Fact>]
41+
let ``Members 03 - Members and interface`` () =
42+
Fsx """
43+
type FloatAlias = float
44+
45+
type FloatAlias with
46+
member x.IsNaN = System.Double.IsNaN(x)
47+
interface System.IConvertible
48+
"""
49+
|> typecheck
50+
|> shouldFail
51+
|> withDiagnostics [
52+
(Error 964, Line 4, Col 6, Line 4, Col 16, "Type abbreviations cannot have augmentations")
53+
(Error 906, Line 6, Col 15, Line 6, Col 34, "Type abbreviations cannot have interface declarations")
54+
(Error 909, Line 6, Col 15, Line 6, Col 34, "All implemented interfaces should be declared on the initial declaration of the type")
55+
]
56+
57+
[<Fact>]
58+
let ``Multiple types 01`` () =
59+
Fsx """
60+
type ListAlias = int list
61+
type ArrayAlias = string[]
62+
63+
type ListAlias with
64+
member x.Head = x.Head
65+
66+
type ArrayAlias with
67+
member x.Length = x.Length
68+
"""
69+
|> typecheck
70+
|> shouldFail
71+
|> withDiagnostics [
72+
(Error 964, Line 5, Col 6, Line 5, Col 15, "Type abbreviations cannot have augmentations")
73+
(Error 895, Line 6, Col 5, Line 6, Col 27, "Type abbreviations cannot have members")
74+
(Error 964, Line 8, Col 6, Line 8, Col 16, "Type abbreviations cannot have augmentations")
75+
(Error 895, Line 9, Col 5, Line 9, Col 31, "Type abbreviations cannot have members")
76+
]
77+
78+
[<Fact>]
79+
let ``Multiple types 02 - With interface`` () =
80+
Fsx """
81+
type ArrayAlias = string[]
82+
83+
type ArrayAlias with
84+
interface System.Collections.IEnumerable with
85+
member x.GetEnumerator() = null
86+
"""
87+
|> typecheck
88+
|> shouldFail
89+
|> withDiagnostics [
90+
(Error 964, Line 4, Col 6, Line 4, Col 16, "Type abbreviations cannot have augmentations")
91+
(Error 906, Line 5, Col 15, Line 5, Col 45, "Type abbreviations cannot have interface declarations")
92+
(Error 909, Line 5, Col 15, Line 5, Col 45, "All implemented interfaces should be declared on the initial declaration of the type")
93+
]
94+
[<Fact>]
95+
let ``Nested 01`` () =
96+
Fsx """
97+
namespace Test
98+
99+
type Alias1 = int
100+
101+
module Nested =
102+
type Alias2 = string
103+
104+
type Alias1 with
105+
member x.Value = x
106+
"""
107+
|> typecheck
108+
|> shouldFail
109+
|> withDiagnostics [
110+
(Error 964, Line 9, Col 10, Line 9, Col 16, "Type abbreviations cannot have augmentations");
111+
(Error 895, Line 10, Col 9, Line 10, Col 27, "Type abbreviations cannot have members");
112+
]
113+
114+
[<Fact>]
115+
let ``Nested 02 - Different namespace`` () =
116+
Fsx """
117+
namespace Test
118+
119+
module Nested =
120+
type Alias2 = string
121+
122+
type Alias2 with
123+
interface System.IComparable with
124+
member x.CompareTo(obj) = 0
125+
126+
open Nested
127+
128+
type Alias2 with
129+
member x.ToUpper() = x.ToUpper()
130+
"""
131+
|> typecheck
132+
|> shouldFail
133+
|> withDiagnostics [
134+
(Error 964, Line 7, Col 10, Line 7, Col 16, "Type abbreviations cannot have augmentations");
135+
(Error 906, Line 8, Col 19, Line 8, Col 37, "Type abbreviations cannot have interface declarations");
136+
(Error 909, Line 8, Col 19, Line 8, Col 37, "All implemented interfaces should be declared on the initial declaration of the type");
137+
(Error 964, Line 13, Col 6, Line 13, Col 12, "Type abbreviations cannot have augmentations");
138+
(Error 895, Line 14, Col 5, Line 14, Col 37, "Type abbreviations cannot have members");
139+
(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.");
140+
]
141+
142+
[<Fact>]
143+
let ``Generic 01`` () =
144+
Fsx """
145+
type Result<'T> = Result<'T, string>
146+
147+
type Result<'T> with
148+
member x.IsOk = match x with Ok _ -> true | Error _ -> false
149+
"""
150+
|> typecheck
151+
|> shouldFail
152+
|> withDiagnostics [
153+
(Error 964, Line 4, Col 6, Line 4, Col 12, "Type abbreviations cannot have augmentations");
154+
(Error 895, Line 5, Col 5, Line 5, Col 65, "Type abbreviations cannot have members");
155+
]
156+
157+
[<Fact>]
158+
let ``Generic 02 - Interface`` () =
159+
Fsx """
160+
type MyList<'a> = 'a list
161+
162+
type MyList<'a> with
163+
interface seq<'a> with
164+
member x.GetEnumerator() = (x :> seq<'a>).GetEnumerator()
165+
"""
166+
|> typecheck
167+
|> shouldFail
168+
|> withDiagnostics [
169+
(Error 964, Line 4, Col 6, Line 4, Col 12, "Type abbreviations cannot have augmentations");
170+
(Error 906, Line 5, Col 15, Line 5, Col 22, "Type abbreviations cannot have interface declarations");
171+
(Error 909, Line 5, Col 15, Line 5, Col 22, "All implemented interfaces should be declared on the initial declaration of the type")
172+
]
173+
174+
[<Fact>]
175+
let ``Property getters and setters`` () =
176+
Fsx """
177+
type IntRef = int ref
178+
179+
type IntRef with
180+
member x.Value
181+
with get() = !x
182+
and set(v) = x := v
183+
"""
184+
|> typecheck
185+
|> shouldFail
186+
|> withDiagnostics [
187+
(Error 964, Line 4, Col 6, Line 4, Col 12, "Type abbreviations cannot have augmentations")
188+
(Error 895, Line 5, Col 5, Line 6, Col 1, "Type abbreviations cannot have members")
189+
]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
module ErrorMessages.NamespaceTests
4+
5+
open Xunit
6+
open FSharp.Test.Compiler
7+
8+
[<Fact>]
9+
let ``Value bindings 01`` () =
10+
Fsx """
11+
namespace TestNamespace
12+
13+
let x = 1
14+
"""
15+
|> typecheck
16+
|> shouldFail
17+
|> withDiagnostics [
18+
(Error 201, Line 4, Col 5, Line 4, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
19+
]
20+
21+
[<Fact>]
22+
let ``Value bindings 02 - Multiple`` () =
23+
Fsx """
24+
namespace TestNamespace
25+
26+
let x = 1
27+
let y = 2
28+
"""
29+
|> typecheck
30+
|> shouldFail
31+
|> withDiagnostics [
32+
(Error 201, Line 4, Col 5, Line 4, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
33+
(Error 201, Line 5, Col 5, Line 5, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
34+
]
35+
36+
[<Fact>]
37+
let ``Function bindings`` () =
38+
Fsx """
39+
namespace TestNamespace
40+
41+
let add x y = x + y
42+
"""
43+
|> typecheck
44+
|> shouldFail
45+
|> withDiagnostics [
46+
(Error 201, Line 4, Col 5, Line 4, Col 12, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
47+
]
48+
49+
[<Fact>]
50+
let ``Multiple namespaces`` () =
51+
Fsx """
52+
namespace Namespace1
53+
54+
let x = 1
55+
56+
namespace Namespace2
57+
58+
let y = 2
59+
60+
namespace Namespace3
61+
62+
let z = 3
63+
"""
64+
|> typecheck
65+
|> shouldFail
66+
|> withDiagnostics [
67+
(Error 201, Line 4, Col 5, Line 4, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
68+
(Error 201, Line 8, Col 5, Line 8, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
69+
(Error 201, Line 12, Col 5, Line 12, Col 6, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
70+
]
71+
72+
[<Fact>]
73+
let ``Do expressions`` () =
74+
Fsx """
75+
namespace TestNamespace
76+
77+
do printfn "test"
78+
"""
79+
|> typecheck
80+
|> shouldFail
81+
|> withDiagnostics [
82+
(Error 201, Line 4, Col 1, Line 4, Col 18, "Namespaces cannot contain values. Consider using a module to hold your value declarations.")
83+
]

0 commit comments

Comments
 (0)