Skip to content

Commit 4967093

Browse files
authored
Make enum cases public (#5002)
* Make Enum have public cases * simplify * add some tests * Test case
1 parent b328b74 commit 4967093

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

src/fsharp/IlxGen.fs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6294,15 +6294,17 @@ and GenTypeDef cenv mgbuf lazyInitInfo eenv m (tycon:Tycon) =
62946294
[ for fspec in tycon.AllFieldsAsList do
62956295

62966296
let useGenuineField = useGenuineField tycon fspec
6297-
6297+
62986298
// The property (or genuine IL field) is hidden in these circumstances:
62996299
// - secret fields apart from "__value" fields for enums
63006300
// - the representation of the type is hidden
63016301
// - the F# field is hidden by a signature or private declaration
6302-
let isPropHidden =
6303-
((fspec.IsCompilerGenerated && not tycon.IsEnumTycon) ||
6304-
hiddenRepr ||
6305-
IsHiddenRecdField eenv.sigToImplRemapInfo (tcref.MakeNestedRecdFieldRef fspec))
6302+
let isPropHidden =
6303+
// Enums always have public cases irrespective of Enum Visibility
6304+
if tycon.IsEnumTycon then false
6305+
else
6306+
(fspec.IsCompilerGenerated || hiddenRepr ||
6307+
IsHiddenRecdField eenv.sigToImplRemapInfo (tcref.MakeNestedRecdFieldRef fspec))
63066308
let ilType = GenType cenv.amap m eenvinner.tyenv fspec.FormalType
63076309
let ilFieldName = ComputeFieldName tycon fspec
63086310

tests/fsharp/core/enum/test.fsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// #Conformance #Regression
2+
#if TESTS_AS_APP
3+
module Core_enum
4+
#endif
5+
6+
open System.Reflection
7+
let failures = ref []
8+
9+
let report_failure (s : string) =
10+
stderr.Write" Failed: "
11+
stderr.WriteLine s
12+
failures := !failures @ [s]
13+
14+
let test (s : string) b =
15+
stderr.Write(s)
16+
if b then stderr.WriteLine " Passed"
17+
else report_failure (s)
18+
19+
let check s b1 b2 = test s (b1 = b2)
20+
21+
type internal internalEnum = Red=0 | Yellow=1 | Blue=2
22+
type public publicEnum = Red=0 | Yellow=1 | Blue=2
23+
24+
module enum =
25+
26+
let AreCasesPublic (t:System.Type) =
27+
let bindingFlags = BindingFlags.Static ||| BindingFlags.Public
28+
try
29+
let red = t.GetField("Red", bindingFlags)
30+
let yellow = t.GetField("Yellow", bindingFlags)
31+
let blue = t.GetField("Blue", bindingFlags)
32+
let value__ = t.GetField("value__", bindingFlags ||| BindingFlags.Instance)
33+
34+
if isNull red || not (red.IsPublic) then failwith (sprintf "Type: %s) - Red is not public. All enum cases should always be public" t.FullName)
35+
if isNull yellow || not (yellow.IsPublic) then failwith (sprintf "Type: %s) - Yellow is not public. All enum cases should always be public" t.FullName)
36+
if isNull blue || not (blue.IsPublic) then failwith (sprintf "Type: %s) - Blue is not public. All enum cases should always be public" t.FullName)
37+
if isNull value__ || not (value__.IsPublic) then failwith (sprintf "Type: %s) - value__ is not public. value__ should always be public" t.FullName)
38+
true
39+
with _ -> false
40+
41+
do check "publicEnum" (AreCasesPublic typeof<publicEnum>) true
42+
do check "internalEnum" (AreCasesPublic typeof<internalEnum>) true
43+
44+
45+
#if TESTS_AS_APP
46+
let RUN() = !failures
47+
#else
48+
let aa =
49+
match !failures with
50+
| [] ->
51+
stdout.WriteLine "Test Passed"
52+
System.IO.File.WriteAllText("test.ok","ok")
53+
exit 0
54+
| _ ->
55+
stdout.WriteLine "Test Failed"
56+
exit 1
57+
#endif
58+

tests/fsharp/tests.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ module CoreTests =
237237
[<Test>]
238238
let csext () = singleTestBuildAndRun "core/csext" FSC_BASIC
239239

240+
241+
[<Test>]
242+
let fscenum () = singleTestBuildAndRun "core/enum" FSC_BASIC
243+
244+
[<Test>]
245+
let fsienum () = singleTestBuildAndRun "core/enum" FSI_BASIC
246+
247+
240248
#if !FSHARP_SUITE_DRIVES_CORECLR_TESTS
241249

242250
[<Test>]

0 commit comments

Comments
 (0)