@@ -12069,15 +12069,15 @@ and GenExnDef cenv mgbuf eenv m (exnc: Tycon) : ILTypeRef option =
1206912069
1207012070 let emitSerializationFieldIL emitPerField =
1207112071 [
12072- for (ilPropName , ilFieldName, ilPropType, _) in fieldNamesAndTypes do
12073- yield! emitPerField ilPropName ilFieldName ilPropType
12072+ for (_ , ilFieldName, ilPropType, _) in fieldNamesAndTypes do
12073+ yield! emitPerField ilFieldName ilPropType
1207412074 ]
1207512075
1207612076 let isILValueType (ty: ILType) =
1207712077 ty.IsNominal && ty.Boxity = ILBoxity.AsValue
1207812078
1207912079 let ilInstrsToRestoreFields =
12080- emitSerializationFieldIL (fun _ilPropName ilFieldName ilPropType ->
12080+ emitSerializationFieldIL (fun ilFieldName ilPropType ->
1208112081 [
1208212082 mkLdarg0
1208312083 mkLdarg 1us
@@ -12110,17 +12110,22 @@ and GenExnDef cenv mgbuf eenv m (exnc: Tycon) : ILTypeRef option =
1211012110 mkNormalStfld (mkILFieldSpecInTy (ilThisTy, ilFieldName, ilPropType))
1211112111 ])
1211212112
12113+ // FSharp.Core is SecurityTransparent and cannot override SecurityCritical
12114+ // Exception.GetObjectData, so field restoration would be unbalanced — skip it.
12115+ let shouldRestoreFields =
12116+ not g.compilingFSharpCore && not fieldNamesAndTypes.IsEmpty
12117+
1211312118 let ilInstrsForSerialization =
1211412119 [
1211512120 mkLdarg0
1211612121 mkLdarg 1us
1211712122 mkLdarg 2us
1211812123 mkNormalCall (mkILCtorMethSpecForTy (g.iltyp_Exception, [ serializationInfoType; streamingContextType ]))
1211912124 ]
12120- @ (if fieldNamesAndTypes.IsEmpty then
12121- []
12125+ @ (if shouldRestoreFields then
12126+ ilInstrsToRestoreFields
1212212127 else
12123- ilInstrsToRestoreFields )
12128+ [] )
1212412129 |> nonBranchingInstrsToCode
1212512130
1212612131 let ilCtorDefForSerialization =
@@ -12134,7 +12139,7 @@ and GenExnDef cenv mgbuf eenv m (exnc: Tycon) : ILTypeRef option =
1213412139 )
1213512140
1213612141 let ilInstrsToSaveFields =
12137- emitSerializationFieldIL (fun _ilPropName ilFieldName ilPropType ->
12142+ emitSerializationFieldIL (fun ilFieldName ilPropType ->
1213812143 [
1213912144 mkLdarg 1us
1214012145 I_ldstr ilFieldName
@@ -12196,28 +12201,7 @@ and GenExnDef cenv mgbuf eenv m (exnc: Tycon) : ILTypeRef option =
1219612201 // the field-restoring deserialization constructor would crash (fields not in
1219712202 // SerializationInfo). So for FSharp.Core: emit only the base-call ctor (status quo).
1219812203 // For user exceptions: emit both GetObjectData and the field-restoring ctor.
12199- if g.compilingFSharpCore then
12200- let ilBaseOnlyCtorInstrs =
12201- [
12202- mkLdarg0
12203- mkLdarg 1us
12204- mkLdarg 2us
12205- mkNormalCall (mkILCtorMethSpecForTy (g.iltyp_Exception, [ serializationInfoType; streamingContextType ]))
12206- ]
12207- |> nonBranchingInstrsToCode
12208-
12209- let ilBaseOnlyCtor =
12210- mkILCtor (
12211- ILMemberAccess.Family,
12212- [
12213- mkILParamNamed ("info", serializationInfoType)
12214- mkILParamNamed ("context", streamingContextType)
12215- ],
12216- mkMethodBody (false, [], 8, ilBaseOnlyCtorInstrs, None, eenv.imports)
12217- )
12218-
12219- [ ilBaseOnlyCtor ]
12220- elif fieldNamesAndTypes.IsEmpty then
12204+ if g.compilingFSharpCore || fieldNamesAndTypes.IsEmpty then
1222112205 [ ilCtorDefForSerialization ]
1222212206 else
1222312207 [ ilCtorDefForSerialization; ilGetObjectDataDef ]
0 commit comments