|
| 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 | +open FSharp.Test.Utilities |
| 9 | + |
| 10 | +module CodeGenRegressions_Exceptions = |
| 11 | + |
| 12 | + let private getActualIL (result: CompilationResult) = |
| 13 | + match result with |
| 14 | + | CompilationResult.Success s -> |
| 15 | + match s.OutputPath with |
| 16 | + | Some p -> |
| 17 | + let (_, _, actualIL) = ILChecker.verifyILAndReturnActual [] p [ "// dummy" ] |
| 18 | + actualIL |
| 19 | + | None -> failwith "No output path" |
| 20 | + | _ -> failwith "Compilation failed" |
| 21 | + |
| 22 | + // https://github.com/dotnet/fsharp/issues/878 |
| 23 | + [<Fact>] |
| 24 | + let ``Issue_878_ExceptionSerialization`` () = |
| 25 | + let source = """ |
| 26 | +module Test |
| 27 | +
|
| 28 | +exception Foo of x:string * y:int |
| 29 | +""" |
| 30 | + let result = |
| 31 | + FSharp source |
| 32 | + |> asLibrary |
| 33 | + |> compile |
| 34 | + |> shouldSucceed |
| 35 | + |
| 36 | + result |
| 37 | + |> verifyIL [ |
| 38 | + ".method public strict virtual instance void GetObjectData(class [runtime]System.Runtime.Serialization.SerializationInfo info, valuetype [runtime]System.Runtime.Serialization.StreamingContext context) cil managed" |
| 39 | + "call instance void [runtime]System.Exception::GetObjectData(class [runtime]System.Runtime.Serialization.SerializationInfo," |
| 40 | + ".method family specialname rtspecialname instance void .ctor(class [runtime]System.Runtime.Serialization.SerializationInfo info, valuetype [runtime]System.Runtime.Serialization.StreamingContext context) cil managed" |
| 41 | + ] |
| 42 | + |> ignore |
| 43 | + |
| 44 | + let actualIL = getActualIL result |
| 45 | + Assert.Contains("AddValue", actualIL) |
| 46 | + |
| 47 | + // https://github.com/dotnet/fsharp/issues/878 |
| 48 | + |
| 49 | + [<Fact>] |
| 50 | + let ``Issue_878_ExceptionSerialization_Roundtrip`` () = |
| 51 | + let source = """ |
| 52 | +module Test |
| 53 | +open System |
| 54 | +open System.Runtime.Serialization |
| 55 | +
|
| 56 | +#nowarn "44" // Serialization types are obsolete but needed for testing ISerializable |
| 57 | +#nowarn "67" |
| 58 | +
|
| 59 | +exception Foo of x:string * y:int |
| 60 | +
|
| 61 | +let roundtrip (e: Exception) = |
| 62 | + let info = SerializationInfo(e.GetType(), FormatterConverter()) |
| 63 | + let ctx = StreamingContext(StreamingContextStates.All) |
| 64 | + e.GetObjectData(info, ctx) |
| 65 | + let ctor = |
| 66 | + e.GetType().GetConstructor( |
| 67 | + System.Reflection.BindingFlags.Instance ||| System.Reflection.BindingFlags.NonPublic ||| System.Reflection.BindingFlags.Public, |
| 68 | + null, |
| 69 | + [| typeof<SerializationInfo>; typeof<StreamingContext> |], |
| 70 | + null) |
| 71 | + if ctor = null then failwith "Deserialization constructor not found" |
| 72 | + ctor.Invoke([| info :> obj; ctx :> obj |]) :?> Exception |
| 73 | +
|
| 74 | +[<EntryPoint>] |
| 75 | +let main _ = |
| 76 | + let original = Foo("value", 42) |
| 77 | + // Check GetObjectData actually writes our fields |
| 78 | + let info = SerializationInfo(original.GetType(), FormatterConverter()) |
| 79 | + let ctx = StreamingContext(StreamingContextStates.All) |
| 80 | + original.GetObjectData(info, ctx) |
| 81 | + let xVal = info.GetString("x") |
| 82 | + let yVal = info.GetInt32("y") |
| 83 | + if xVal <> "value" then failwithf "GetObjectData: Expected x='value', got '%s'" xVal |
| 84 | + if yVal <> 42 then failwithf "GetObjectData: Expected y=42, got %d" yVal |
| 85 | + |
| 86 | + // Check full roundtrip |
| 87 | + let cloned = roundtrip original |
| 88 | + // Access fields via internal backing fields using reflection |
| 89 | + let xField = cloned.GetType().GetField("x@", System.Reflection.BindingFlags.Instance ||| System.Reflection.BindingFlags.NonPublic) |
| 90 | + let yField = cloned.GetType().GetField("y@", System.Reflection.BindingFlags.Instance ||| System.Reflection.BindingFlags.NonPublic) |
| 91 | + if xField = null then failwith "Field x@ not found" |
| 92 | + if yField = null then failwith "Field y@ not found" |
| 93 | + let xCloned = xField.GetValue(cloned) :?> string |
| 94 | + let yCloned = yField.GetValue(cloned) :?> int |
| 95 | + if xCloned <> "value" then failwithf "Roundtrip: Expected x='value', got '%s'" xCloned |
| 96 | + if yCloned <> 42 then failwithf "Roundtrip: Expected y=42, got %d" yCloned |
| 97 | + printfn "SUCCESS: Foo(value, 42) roundtripped correctly" |
| 98 | + 0 |
| 99 | +""" |
| 100 | + FSharp source |
| 101 | + |> asExe |
| 102 | + |> ignoreWarnings |
| 103 | + |> compile |
| 104 | + |> shouldSucceed |
| 105 | + |> run |
| 106 | + |> shouldSucceed |
| 107 | + |> ignore |
0 commit comments