|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace EmittedIL |
| 4 | + |
| 5 | +open Xunit |
| 6 | +open FSharp.Test |
| 7 | +open FSharp.Test.Compiler |
| 8 | + |
| 9 | +module CodeGenRegressions_TypeDefs = |
| 10 | + |
| 11 | + // https://github.com/dotnet/fsharp/issues/16565 |
| 12 | + [<Fact>] |
| 13 | + let ``Issue_16565_DefaultAugmentationFalseDuplicateEntry`` () = |
| 14 | + let source = """ |
| 15 | +module Test |
| 16 | +
|
| 17 | +open System |
| 18 | +
|
| 19 | +[<DefaultAugmentation(false)>] |
| 20 | +type Option<'T> = |
| 21 | + | Some of Value: 'T |
| 22 | + | None |
| 23 | +
|
| 24 | + member x.Value = |
| 25 | + match x with |
| 26 | + | Some x -> x |
| 27 | + | None -> raise (new InvalidOperationException("Option.Value")) |
| 28 | +
|
| 29 | + static member None : Option<'T> = None |
| 30 | +
|
| 31 | +and 'T option = Option<'T> |
| 32 | +
|
| 33 | +let v = Option.Some 42 |
| 34 | +printfn "Value: %d" v.Value |
| 35 | +let n = Option<int>.None |
| 36 | +printfn "None created successfully" |
| 37 | +""" |
| 38 | + FSharp source |
| 39 | + |> asExe |
| 40 | + |> compile |
| 41 | + |> shouldSucceed |
| 42 | + |> run |
| 43 | + |> shouldSucceed |
| 44 | + |> ignore |
| 45 | + |
| 46 | + // https://github.com/dotnet/fsharp/issues/14321 |
| 47 | + [<Fact>] |
| 48 | + let ``Issue_14321_DuAndIWSAMNames`` () = |
| 49 | + let source = """ |
| 50 | +module Test |
| 51 | +
|
| 52 | +#nowarn "3535" // IWSAM warning |
| 53 | +
|
| 54 | +type EngineError<'e> = |
| 55 | + static abstract Overheated : 'e |
| 56 | + static abstract LowOil : 'e |
| 57 | +
|
| 58 | +type CarError = |
| 59 | + | Overheated |
| 60 | + | LowOil |
| 61 | + | DeviceNotPaired |
| 62 | +
|
| 63 | + interface EngineError<CarError> with |
| 64 | + static member Overheated = Overheated |
| 65 | + static member LowOil = LowOil |
| 66 | +""" |
| 67 | + FSharp source |
| 68 | + |> asLibrary |
| 69 | + |> compile |
| 70 | + |> shouldSucceed |
| 71 | + |> ignore |
| 72 | + |
| 73 | + // https://github.com/dotnet/fsharp/issues/14321 |
| 74 | + // Runtime test: type must load without "duplicate entry in method table" |
| 75 | + [<Fact>] |
| 76 | + let ``Issue_14321_DuAndIWSAMNames_Runtime`` () = |
| 77 | + let source = """ |
| 78 | +module Test |
| 79 | +
|
| 80 | +#nowarn "3535" |
| 81 | +
|
| 82 | +type EngineError<'e> = |
| 83 | + static abstract Overheated : 'e |
| 84 | + static abstract LowOil : 'e |
| 85 | +
|
| 86 | +type CarError = |
| 87 | + | Overheated |
| 88 | + | LowOil |
| 89 | + | DeviceNotPaired |
| 90 | +
|
| 91 | + interface EngineError<CarError> with |
| 92 | + static member Overheated = Overheated |
| 93 | + static member LowOil = LowOil |
| 94 | +
|
| 95 | +[<EntryPoint>] |
| 96 | +let main _ = |
| 97 | + let err = CarError.Overheated |
| 98 | + match err with |
| 99 | + | Overheated -> printfn "Got Overheated" |
| 100 | + | LowOil -> printfn "Got LowOil" |
| 101 | + | DeviceNotPaired -> printfn "Got DeviceNotPaired" |
| 102 | + 0 |
| 103 | +""" |
| 104 | + FSharp source |
| 105 | + |> asExe |
| 106 | + |> compile |
| 107 | + |> shouldSucceed |
| 108 | + |> run |
| 109 | + |> shouldSucceed |
| 110 | + |> ignore |
| 111 | + |
| 112 | + // https://github.com/dotnet/fsharp/issues/5834 |
| 113 | + [<Fact>] |
| 114 | + let ``Issue_5834_EventSpecialname`` () = |
| 115 | + let source = """ |
| 116 | +module Test |
| 117 | +
|
| 118 | +open System |
| 119 | +open System.Reflection |
| 120 | +
|
| 121 | +type IAbstract1 = |
| 122 | + [<CLIEvent>] |
| 123 | + abstract member Event : IEvent<EventHandler, EventArgs> |
| 124 | +
|
| 125 | +type IAbstract2 = |
| 126 | + [<CLIEvent>] |
| 127 | + abstract member Event : IDelegateEvent<EventHandler> |
| 128 | +
|
| 129 | +[<AbstractClass>] |
| 130 | +type Abstract3() = |
| 131 | + [<CLIEvent>] |
| 132 | + abstract member Event : IDelegateEvent<EventHandler> |
| 133 | +
|
| 134 | +type Concrete1() = |
| 135 | + let event = new Event<EventHandler, EventArgs>() |
| 136 | + [<CLIEvent>] |
| 137 | + member this.Event = event.Publish |
| 138 | +
|
| 139 | +type Concrete2() = |
| 140 | + [<CLIEvent>] |
| 141 | + member this.Event = { new IDelegateEvent<EventHandler> with |
| 142 | + member this.AddHandler _ = () |
| 143 | + member this.RemoveHandler _ = () } |
| 144 | +
|
| 145 | +type ConcreteWithObsolete() = |
| 146 | + let evt = new Event<EventHandler, EventArgs>() |
| 147 | + [<Obsolete("deprecated")>] |
| 148 | + [<CLIEvent>] |
| 149 | + member this.MyEvent = evt.Publish |
| 150 | +
|
| 151 | +[<EntryPoint>] |
| 152 | +let main _ = |
| 153 | + let mutable failures = 0 |
| 154 | + let check (t: Type) = |
| 155 | + t.GetMethods(BindingFlags.Public ||| BindingFlags.Instance ||| BindingFlags.DeclaredOnly) |
| 156 | + |> Array.filter (fun m -> m.Name.Contains("Event")) |
| 157 | + |> Array.iter (fun m -> |
| 158 | + if not m.IsSpecialName then |
| 159 | + printfn "FAIL: %s.%s missing specialname" t.Name m.Name |
| 160 | + failures <- failures + 1) |
| 161 | +
|
| 162 | + check typeof<IAbstract1> |
| 163 | + check typeof<IAbstract2> |
| 164 | + check typeof<Abstract3> |
| 165 | + check typeof<Concrete1> |
| 166 | + check typeof<Concrete2> |
| 167 | + check typeof<ConcreteWithObsolete> |
| 168 | +
|
| 169 | + if failures > 0 then |
| 170 | + failwithf "BUG: %d event accessors missing specialname" failures |
| 171 | + printfn "SUCCESS: All event accessors have specialname" |
| 172 | + 0 |
| 173 | +""" |
| 174 | + FSharp source |
| 175 | + |> asExe |
| 176 | + |> compile |
| 177 | + |> shouldSucceed |
| 178 | + |> run |
| 179 | + |> shouldSucceed |
| 180 | + |> ignore |
| 181 | + |
| 182 | + // https://github.com/dotnet/fsharp/issues/5834 |
| 183 | + // IL verification: abstract event accessors must have specialname flag |
| 184 | + [<Fact>] |
| 185 | + let ``Issue_5834_EventSpecialname_IL`` () = |
| 186 | + let source = """ |
| 187 | +module Test |
| 188 | +
|
| 189 | +open System |
| 190 | +
|
| 191 | +type IAbstract1 = |
| 192 | + [<CLIEvent>] |
| 193 | + abstract member Event : IEvent<EventHandler, EventArgs> |
| 194 | +
|
| 195 | +[<AbstractClass>] |
| 196 | +type Abstract2() = |
| 197 | + [<CLIEvent>] |
| 198 | + abstract member Event : IDelegateEvent<EventHandler> |
| 199 | +""" |
| 200 | + FSharp source |
| 201 | + |> asLibrary |
| 202 | + |> compile |
| 203 | + |> shouldSucceed |
| 204 | + |> verifyIL [ |
| 205 | + // Interface abstract event accessors have specialname |
| 206 | + """.method public hidebysig specialname abstract virtual instance void add_Event(class [runtime]System.EventHandler A_1) cil managed""" |
| 207 | + |
| 208 | + """.method public hidebysig specialname abstract virtual instance void remove_Event(class [runtime]System.EventHandler A_1) cil managed""" |
| 209 | + |
| 210 | + // IAbstract1 event references its accessors |
| 211 | + """.addon instance void Test/IAbstract1::add_Event(class [runtime]System.EventHandler)""" |
| 212 | + """.removeon instance void Test/IAbstract1::remove_Event(class [runtime]System.EventHandler)""" |
| 213 | + |
| 214 | + // Abstract2 event also references its specialname accessors |
| 215 | + """.addon instance void Test/Abstract2::add_Event(class [runtime]System.EventHandler)""" |
| 216 | + """.removeon instance void Test/Abstract2::remove_Event(class [runtime]System.EventHandler)""" |
| 217 | + ] |
| 218 | + |> ignore |
| 219 | + |
| 220 | + // https://github.com/dotnet/fsharp/issues/14321 |
| 221 | + // IL verification: DU case properties and IWSAM implementations coexist without duplicates |
| 222 | + [<Fact>] |
| 223 | + let ``Issue_14321_DuAndIWSAMNames_IL`` () = |
| 224 | + let source = """ |
| 225 | +module Test |
| 226 | +
|
| 227 | +#nowarn "3535" |
| 228 | +
|
| 229 | +type EngineError<'e> = |
| 230 | + static abstract Overheated : 'e |
| 231 | + static abstract LowOil : 'e |
| 232 | +
|
| 233 | +type CarError = |
| 234 | + | Overheated |
| 235 | + | LowOil |
| 236 | + | DeviceNotPaired |
| 237 | +
|
| 238 | + interface EngineError<CarError> with |
| 239 | + static member Overheated = Overheated |
| 240 | + static member LowOil = LowOil |
| 241 | +""" |
| 242 | + FSharp source |
| 243 | + |> asLibrary |
| 244 | + |> compile |
| 245 | + |> shouldSucceed |
| 246 | + |> verifyIL [ |
| 247 | + // DU case property getter exists on CarError |
| 248 | + """.method public static class Test/CarError get_Overheated() cil managed""" |
| 249 | + |
| 250 | + // Explicit IWSAM implementation uses mangled name |
| 251 | + """.method public hidebysig specialname static class Test/CarError 'Test.EngineError<Test.CarError>.get_Overheated'() cil managed""" |
| 252 | + |
| 253 | + // Explicit IWSAM for LowOil also present |
| 254 | + """.method public hidebysig specialname static class Test/CarError 'Test.EngineError<Test.CarError>.get_LowOil'() cil managed""" |
| 255 | + ] |
| 256 | + |> ignore |
| 257 | + |
| 258 | + // https://github.com/dotnet/fsharp/issues/16565 |
| 259 | + // IL verification: DefaultAugmentation(false) produces no duplicate method table entries |
| 260 | + [<Fact>] |
| 261 | + let ``Issue_16565_DefaultAugmentationFalseDuplicateEntry_IL`` () = |
| 262 | + let source = """ |
| 263 | +module Test |
| 264 | +
|
| 265 | +[<DefaultAugmentation(false)>] |
| 266 | +type MyOption<'T> = |
| 267 | + | Some of Value: 'T |
| 268 | + | None |
| 269 | +
|
| 270 | + member x.Value = |
| 271 | + match x with |
| 272 | + | Some x -> x |
| 273 | + | None -> failwith "no value" |
| 274 | +
|
| 275 | + static member None : MyOption<'T> = None |
| 276 | +""" |
| 277 | + FSharp source |
| 278 | + |> asLibrary |
| 279 | + |> compile |
| 280 | + |> shouldSucceed |
| 281 | + |> verifyIL [ |
| 282 | + // The user-defined get_Value method exists on the main type with specialname |
| 283 | + """.method public hidebysig specialname instance !T get_Value() cil managed""" |
| 284 | + |
| 285 | + // The static get_None method exists |
| 286 | + """.method public static class Test/MyOption`1<!T> get_None() cil managed""" |
| 287 | + ] |
| 288 | + |> ignore |
| 289 | + |
0 commit comments