Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 9e1793b

Browse files
KevinRansomnosami
authored andcommitted
interop visibility
1 parent d4724c9 commit 9e1793b

File tree

1 file changed

+66
-94
lines changed

1 file changed

+66
-94
lines changed
Lines changed: 66 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
1+
namespace FSharp.Compiler.ComponentTests.Interop
22

3-
namespace FSharp.Compiler.UnitTests
3+
open Xunit
4+
open FSharp.Test.Utilities.Compiler
45

5-
open FSharp.Compiler.SourceCodeServices
6-
open FSharp.Reflection
7-
open FSharp.Test
8-
open FSharp.Test.Utilities
9-
open FSharp.Test.Utilities.Utilities
10-
open NUnit.Framework
11-
12-
[<TestFixture>]
13-
module ILMemberAccessTests =
6+
module ``Verify visibility of properties`` =
147

158
let csharpBaseClass = """
169
namespace ExhaustiveCombinations
@@ -29,26 +22,23 @@ namespace ExhaustiveCombinations
2922
public string SetPublicGetProtectedInternal { protected internal get; set; }
3023
public string SetPublicGetPrivate { private get; set; }
3124
}
32-
}
33-
"""
25+
}"""
3426

3527
let fsharpBaseClass = """
3628
namespace ExhaustiveCombinations
3729
3830
open System
3931
4032
type FSharpBaseClass () =
41-
4233
member this.GetPublicSetInternal with public get() = "" and internal set (parameter:string) = ignore parameter
4334
member this.GetPublicSetPrivate with public get() = "" and private set (parameter:string) = ignore parameter
4435
member this.SetPublicGetInternal with internal get() = "" and public set (parameter:string) = ignore parameter
45-
member this.SetPublicGetPrivate with private get() = "" and public set (parameter:string) = ignore parameter
46-
47-
"""
36+
member this.SetPublicGetPrivate with private get() = "" and public set (parameter:string) = ignore parameter"""
4837

38+
[<Fact>]
39+
let ``C# class F# derived class - access public`` () =
4940

50-
[<Test>][<Ignore("TODO: This is broken RN, since netcoreapp30 is used for C# and 3.1 for F#, should be fixed as part of https://github.com/dotnet/fsharp/issues/9740")>]
51-
let ``VerifyVisibility of Properties C# class F# derived class -- AccessPublicStuff`` () =
41+
let csharpLib = CSharp csharpBaseClass |> withName "csLib"
5242

5343
let fsharpSource =
5444
fsharpBaseClass + """
@@ -76,24 +66,23 @@ type MyFSharpClass () =
7666
let _ = this.GetPublicSetPrivate // Accessible
7767
()
7868
"""
79-
80-
let csCmpl =
81-
CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30)
82-
|> CompilationReference.Create
83-
84-
let fsCmpl =
85-
Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl])
86-
87-
CompilerAssert.CompileWithErrors(fsCmpl, [|
88-
(FSharpErrorSeverity.Error, 491, (22, 9, 22, 41),
89-
"The member or object constructor 'GetPublicSetInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
90-
(FSharpErrorSeverity.Error, 491, (25, 9, 25, 49),
91-
"The member or object constructor 'GetPublicSetPrivateProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
92-
(FSharpErrorSeverity.Error, 491, (34, 9, 34, 40),
93-
"The member or object constructor 'GetPublicSetPrivate' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")|])
94-
95-
[<Test>][<Ignore("TODO: This is broken RN, since netcoreapp30 is used for C# and 3.1 for F#, should be fixed as part of https://github.com/dotnet/fsharp/issues/9740")>]
96-
let ``VerifyVisibility of Properties C# class F# non-derived class -- AccessPublicStuff`` () =
69+
Fsx fsharpSource
70+
|> withLangVersion50
71+
|> withReferences [csharpLib]
72+
|> compile
73+
|> shouldFail
74+
|> withDiagnostics [
75+
(Error 491, Line 19, Col 9, Line 19, Col 41,
76+
"The member or object constructor 'GetPublicSetInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")
77+
(Error 491, Line 22, Col 9, Line 22, Col 49,
78+
"The member or object constructor 'GetPublicSetPrivateProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")
79+
(Error 491, Line 31, Col 9, Line 31, Col 40,
80+
"The member or object constructor 'GetPublicSetPrivate' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")]
81+
82+
[<Fact>]
83+
let ``C# class F# non-derived class - access public`` () =
84+
85+
let csharpLib = CSharp csharpBaseClass |> withName "csLib"
9786

9887
let fsharpSource =
9988
fsharpBaseClass + """
@@ -122,30 +111,29 @@ type MyFSharpClass () =
122111
()
123112
"""
124113

125-
let csCmpl =
126-
CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30)
127-
|> CompilationReference.Create
128-
129-
let fsCmpl =
130-
Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl])
131-
132-
CompilerAssert.CompileWithErrors(fsCmpl, [|
133-
(FSharpErrorSeverity.Error, 491, (22, 9, 22, 39),
134-
"The member or object constructor 'GetPublicSetInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
135-
(FSharpErrorSeverity.Error, 491, (25, 9, 25, 47),
136-
"The member or object constructor 'GetPublicSetPrivateProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
137-
(FSharpErrorSeverity.Error, 491, (28, 9, 28, 48),
138-
"The member or object constructor 'GetPublicSetProtectedInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
139-
(FSharpErrorSeverity.Error, 491, (31, 9, 31, 40),
140-
"The member or object constructor 'GetPublicSetProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
141-
(FSharpErrorSeverity.Error, 491, (32, 17, 32, 41),
114+
Fsx fsharpSource
115+
|> withLangVersion50
116+
|> withReferences [csharpLib]
117+
|> compile
118+
|> shouldFail
119+
|> withDiagnostics [
120+
(Error 491, Line 19, Col 9, Line 19, Col 39,
121+
"The member or object constructor 'GetPublicSetInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")
122+
(Error 491, Line 22, Col 9, Line 22, Col 47,
123+
"The member or object constructor 'GetPublicSetPrivateProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")
124+
(Error 491, Line 25, Col 9, Line 25, Col 48,
125+
"The member or object constructor 'GetPublicSetProtectedInternal' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")
126+
(Error 491, Line 28, Col 9, Line 28, Col 40,
127+
"The member or object constructor 'GetPublicSetProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")
128+
(Error 491, Line 29, Col 17, Line 29, Col 41,
142129
"The member or object constructor 'SetPublicGetProtected' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.");
143-
(FSharpErrorSeverity.Error, 491, (34, 9, 34, 38),
144-
"The member or object constructor 'GetPublicSetPrivate' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")|])
130+
(Error 491, Line 31, Col 9, Line 31, Col 38,
131+
"The member or object constructor 'GetPublicSetPrivate' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.")]
145132

133+
[<Fact>]
134+
let ``F# base F# derived class - access public`` () =
146135

147-
[<Test>]
148-
let ``VerifyVisibility of Properties F# base F# derived class -- AccessPublicStuff`` () =
136+
let csharpLib = CSharp csharpBaseClass |> withName "csLib"
149137

150138
let fsharpSource =
151139
fsharpBaseClass + """
@@ -154,7 +142,6 @@ open ExhaustiveCombinations
154142
155143
type MyFSharpClass () =
156144
inherit FSharpBaseClass()
157-
158145
member this.AccessPublicStuff() =
159146
160147
this.GetPublicSetInternal <- "1" // Inaccessible
@@ -169,34 +156,27 @@ type MyFSharpClass () =
169156
this.SetPublicGetPrivate <- "1" // Accessible
170157
let _ = this.SetPublicGetPrivate // accessible
171158
172-
()
173-
"""
174-
175-
let csCmpl =
176-
CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30)
177-
|> CompilationReference.Create
178-
179-
let fsCmpl =
180-
Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl])
181-
182-
CompilerAssert.CompileWithErrors(fsCmpl, [|
183-
(FSharpErrorSeverity.Error, 810, (25, 9, 25, 33),
184-
"Property 'GetPublicSetPrivate' cannot be set");
185-
(FSharpErrorSeverity.Error, 807, (32, 17, 32, 41),
186-
"Property 'SetPublicGetPrivate' is not readable")
187-
|])
159+
()"""
160+
Fsx fsharpSource
161+
|> withLangVersion50
162+
|> withReferences [csharpLib]
163+
|> compile
164+
|> shouldFail
165+
|> withDiagnostics [
166+
(Error 810, Line 21, Col 9, Line 21, Col 33, "Property 'GetPublicSetPrivate' cannot be set")
167+
(Error 807, Line 28, Col 17, Line 28, Col 41, "Property 'SetPublicGetPrivate' is not readable")]
188168

169+
[<Fact>]
170+
let ``F# class F# non-derived class - access public`` () =
189171

190-
[<Test>]
191-
let ``VerifyVisibility of Properties F# class F# non-derived class -- AccessPublicStuff`` () =
172+
let csharpLib = CSharp csharpBaseClass |> withName "csLib"
192173

193174
let fsharpSource =
194175
fsharpBaseClass + """
195176
open System
196177
open ExhaustiveCombinations
197178
198179
type MyFSharpClass () =
199-
200180
member _.AccessPublicStuff() =
201181
let bc = new FSharpBaseClass()
202182
@@ -205,19 +185,11 @@ type MyFSharpClass () =
205185
206186
bc.GetPublicSetPrivate <- "1" // Inaccessible
207187
let _ = bc.GetPublicSetPrivate // Accessible
208-
()
209-
"""
210-
211-
let csCmpl =
212-
CompilationUtil.CreateCSharpCompilation(csharpBaseClass, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30)
213-
|> CompilationReference.Create
214-
215-
let fsCmpl =
216-
Compilation.Create(fsharpSource, Fsx, Exe, options = [|"--langversion:preview"|], cmplRefs = [csCmpl])
217-
218-
CompilerAssert.CompileWithErrors(fsCmpl, [|
219-
(FSharpErrorSeverity.Error, 810, (25, 9, 25, 31),
220-
"Property 'GetPublicSetPrivate' cannot be set")|])
221-
222-
223-
188+
()"""
189+
190+
Fsx fsharpSource
191+
|> withLangVersion50
192+
|> withReferences [csharpLib]
193+
|> compile
194+
|> shouldFail
195+
|> withSingleDiagnostic (Error 810, Line 21, Col 9, Line 21, Col 31, "Property 'GetPublicSetPrivate' cannot be set")

0 commit comments

Comments
 (0)