From 907e4392f9fe9d3f86a68ea0304866d9ef57357d Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 19 May 2022 22:43:43 +0100 Subject: [PATCH 01/20] split out CheckPatterns.* (#13168) * split out CheckPatterns.* * split out CheckPatterns.* --- src/Compiler/Checking/CheckDeclarations.fs | 23 +- src/Compiler/Checking/CheckExpressions.fs | 1323 +++++-------------- src/Compiler/Checking/CheckExpressions.fsi | 204 ++- src/Compiler/Checking/CheckPatterns.fs | 757 +++++++++++ src/Compiler/Checking/CheckPatterns.fsi | 42 + src/Compiler/FSharp.Compiler.Service.fsproj | 1098 ++++----------- src/Compiler/Service/ServiceLexing.fsi | 1 - 7 files changed, 1606 insertions(+), 1842 deletions(-) create mode 100644 src/Compiler/Checking/CheckPatterns.fs create mode 100644 src/Compiler/Checking/CheckPatterns.fsi diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index 25ef10fedf7..0086fb44d2f 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -11,11 +11,11 @@ open Internal.Utilities.Library.Extras open Internal.Utilities.Library.ResultOrException open FSharp.Compiler open FSharp.Compiler.AbstractIL.IL -open FSharp.Compiler.AbstractIL.Diagnostics open FSharp.Compiler.AccessibilityLogic open FSharp.Compiler.AttributeChecking -open FSharp.Compiler.CheckExpressions open FSharp.Compiler.CheckComputationExpressions +open FSharp.Compiler.CheckExpressions +open FSharp.Compiler.CheckPatterns open FSharp.Compiler.CompilerGlobalState open FSharp.Compiler.ConstraintSolver open FSharp.Compiler.DiagnosticsLogger @@ -758,7 +758,9 @@ module IncrClassChecking = // Type check arguments by processing them as 'simple' patterns // NOTE: if we allow richer patterns here this is where we'd process those patterns - let ctorArgNames, (_, names, _) = TcSimplePatsOfUnknownType cenv true CheckCxs env tpenv (SynSimplePats.SimplePats (spats, m)) + let ctorArgNames, patEnv = TcSimplePatsOfUnknownType cenv true CheckCxs env tpenv (SynSimplePats.SimplePats (spats, m)) + + let (TcPatLinearEnv(_, names, _)) = patEnv // Create the values with the given names let _, vspecs = MakeAndPublishSimpleVals cenv env names @@ -3232,7 +3234,10 @@ module EstablishTypeDefinitionCores = match implicitCtorSynPats with | None -> () | Some spats -> - let ctorArgNames, (_, names, _) = TcSimplePatsOfUnknownType cenv true NoCheckCxs env tpenv spats + let ctorArgNames, patEnv = TcSimplePatsOfUnknownType cenv true NoCheckCxs env tpenv spats + + let (TcPatLinearEnv(_, names, _)) = patEnv + for arg in ctorArgNames do let ty = names[arg].Type let m = names[arg].Ident.idRange @@ -4113,7 +4118,10 @@ module EstablishTypeDefinitionCores = () | Some spats -> if tycon.IsFSharpStructOrEnumTycon then - let ctorArgNames, (_, names, _) = TcSimplePatsOfUnknownType cenv true CheckCxs envinner tpenv spats + let ctorArgNames, patEnv = TcSimplePatsOfUnknownType cenv true CheckCxs envinner tpenv spats + + let (TcPatLinearEnv(_, names, _)) = patEnv + for arg in ctorArgNames do let ty = names[arg].Type let id = names[arg].Ident @@ -5575,7 +5583,6 @@ let rec TcModuleOrNamespaceElementNonMutRec (cenv: cenv) parent typeNames scopem | SynModuleDecl.NamespaceFragment(SynModuleOrNamespace(longId, isRec, kind, defs, xml, attribs, vis, m, _)) -> - if progress then dprintn ("Typecheck implementation " + textOfLid longId) let endm = m.EndRange do for id in longId do @@ -6002,6 +6009,8 @@ let CheckOneImplFile let cenv = cenv.Create (g, isScript, niceNameGen, amap, thisCcu, false, Option.isSome rootSigOpt, conditionalDefines, tcSink, (LightweightTcValForUsingInBuildMethodCall g), isInternalTestSpanStackReferring, + tcPat=TcPat, + tcSimplePats=TcSimplePats, tcSequenceExpressionEntry=TcSequenceExpressionEntry, tcArrayOrListSequenceExpression=TcArrayOrListComputedExpression, tcComputationExpression=TcComputationExpression) @@ -6128,6 +6137,8 @@ let CheckOneSigFile (g, niceNameGen, amap, thisCcu, checkForErrors, conditionalD cenv.Create (g, false, niceNameGen, amap, thisCcu, true, false, conditionalDefines, tcSink, (LightweightTcValForUsingInBuildMethodCall g), isInternalTestSpanStackReferring, + tcPat=TcPat, + tcSimplePats=TcSimplePats, tcSequenceExpressionEntry=TcSequenceExpressionEntry, tcArrayOrListSequenceExpression=TcArrayOrListComputedExpression, tcComputationExpression=TcComputationExpression) diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index cd69237ff78..42710ee52d6 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -199,7 +199,7 @@ type UngeneralizableItem(computeFreeTyvars: unit -> FreeTyvars) = // If WillNeverHaveFreeTypars then we can cache the computation of FreeTraitSolutions, since they are invariant. let mutable cachedFreeTraitSolutions = emptyFreeLocals - member item.GetFreeTyvars() = + member _.GetFreeTyvars() = let fvs = computeFreeTyvars() if fvs.FreeTypars.IsEmpty then willNeverHaveFreeTypars <- true @@ -207,11 +207,11 @@ type UngeneralizableItem(computeFreeTyvars: unit -> FreeTyvars) = cachedFreeTraitSolutions <- fvs.FreeTraitSolutions fvs - member item.WillNeverHaveFreeTypars = willNeverHaveFreeTypars + member _.WillNeverHaveFreeTypars = willNeverHaveFreeTypars - member item.CachedFreeLocalTycons = cachedFreeLocalTycons + member _.CachedFreeLocalTycons = cachedFreeLocalTycons - member item.CachedFreeTraitSolutions = cachedFreeTraitSolutions + member _.CachedFreeTraitSolutions = cachedFreeTraitSolutions /// Represents the type environment at a particular scope. Includes the name /// resolution environment, the ungeneralizable items from earlier in the scope @@ -403,6 +403,236 @@ let TryFindUnscopedTypar name (UnscopedTyparEnv tab) = Map.tryFind name tab let HideUnscopedTypars typars (UnscopedTyparEnv tab) = UnscopedTyparEnv (List.fold (fun acc (tp: Typar) -> Map.remove tp.Name acc) tab typars) +/// Indicates whether constraints should be checked when checking syntactic types +type CheckConstraints = + | CheckCxs + | NoCheckCxs + +type OverridesOK = + | OverridesOK + | WarnOnOverrides + | ErrorOnOverrides + +/// A type to represent information associated with values to indicate what explicit (declared) type parameters +/// are given and what additional type parameters can be inferred, if any. +/// +/// The declared type parameters, e.g. let f<'a> (x:'a) = x, plus an indication +/// of whether additional polymorphism may be inferred, e.g. let f<'a, ..> (x:'a) y = x +type ExplicitTyparInfo = + | ExplicitTyparInfo of + rigidCopyOfDeclaredTypars: Typars * + declaredTypars: Typars * + infer: bool + +let permitInferTypars = ExplicitTyparInfo ([], [], true) +let dontInferTypars = ExplicitTyparInfo ([], [], false) + +type ArgAndRetAttribs = ArgAndRetAttribs of Attribs list list * Attribs +let noArgOrRetAttribs = ArgAndRetAttribs ([], []) + +/// A flag to represent the sort of bindings are we processing. +/// Processing "declaration" and "class" bindings that make up a module (such as "let x = 1 let y = 2") +/// shares the same code paths (e.g. TcLetBinding and TcLetrecBindings) as processing expression bindings (such as "let x = 1 in ...") +/// Member bindings also use this path. +// +// However there are differences in how different bindings get processed, +// i.e. module bindings get published to the implicitly accumulated module type, but expression 'let' bindings don't. +type DeclKind = + | ModuleOrMemberBinding + + /// Extensions to a type within the same assembly + | IntrinsicExtensionBinding + + /// Extensions to a type in a different assembly + | ExtrinsicExtensionBinding + + | ClassLetBinding of isStatic: bool + + | ObjectExpressionOverrideBinding + + | ExpressionBinding + + static member IsModuleOrMemberOrExtensionBinding x = + match x with + | ModuleOrMemberBinding -> true + | IntrinsicExtensionBinding -> true + | ExtrinsicExtensionBinding -> true + | ClassLetBinding _ -> false + | ObjectExpressionOverrideBinding -> false + | ExpressionBinding -> false + + static member MustHaveArity x = DeclKind.IsModuleOrMemberOrExtensionBinding x + + member x.CanBeDllImport = + match x with + | ModuleOrMemberBinding -> true + | IntrinsicExtensionBinding -> true + | ExtrinsicExtensionBinding -> true + | ClassLetBinding _ -> true + | ObjectExpressionOverrideBinding -> false + | ExpressionBinding -> false + + static member IsAccessModifierPermitted x = DeclKind.IsModuleOrMemberOrExtensionBinding x + + static member ImplicitlyStatic x = DeclKind.IsModuleOrMemberOrExtensionBinding x + + static member AllowedAttribTargets (memberFlagsOpt: SynMemberFlags option) x = + match x with + | ModuleOrMemberBinding | ObjectExpressionOverrideBinding -> + match memberFlagsOpt with + | Some flags when flags.MemberKind = SynMemberKind.Constructor -> AttributeTargets.Constructor + | Some flags when flags.MemberKind = SynMemberKind.PropertyGetSet -> AttributeTargets.Event ||| AttributeTargets.Property + | Some flags when flags.MemberKind = SynMemberKind.PropertyGet -> AttributeTargets.Event ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | Some flags when flags.MemberKind = SynMemberKind.PropertySet -> AttributeTargets.Property + | Some _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue + | None -> AttributeTargets.Field ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | IntrinsicExtensionBinding -> AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | ExtrinsicExtensionBinding -> AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | ClassLetBinding _ -> AttributeTargets.Field ||| AttributeTargets.Method ||| AttributeTargets.ReturnValue + | ExpressionBinding -> enum 0 // indicates attributes not allowed on expression 'let' bindings + + // Note: now always true + static member CanGeneralizeConstrainedTypars x = + match x with + | ModuleOrMemberBinding -> true + | IntrinsicExtensionBinding -> true + | ExtrinsicExtensionBinding -> true + | ClassLetBinding _ -> true + | ObjectExpressionOverrideBinding -> true + | ExpressionBinding -> true + + static member ConvertToLinearBindings x = + match x with + | ModuleOrMemberBinding -> true + | IntrinsicExtensionBinding -> true + | ExtrinsicExtensionBinding -> true + | ClassLetBinding _ -> true + | ObjectExpressionOverrideBinding -> true + | ExpressionBinding -> false + + static member CanOverrideOrImplement x = + match x with + | ModuleOrMemberBinding -> OverridesOK + | IntrinsicExtensionBinding -> WarnOnOverrides + | ExtrinsicExtensionBinding -> ErrorOnOverrides + | ClassLetBinding _ -> ErrorOnOverrides + | ObjectExpressionOverrideBinding -> OverridesOK + | ExpressionBinding -> ErrorOnOverrides + +//------------------------------------------------------------------------- +// Data structures that track the gradual accumulation of information +// about values and members during inference. +//------------------------------------------------------------------------- + +/// The ValReprInfo for a value, except the number of typars is not yet inferred +type PrelimValReprInfo = + | PrelimValReprInfo of + curriedArgInfos: ArgReprInfo list list * + returnInfo: ArgReprInfo + +type PrelimMemberInfo = + | PrelimMemberInfo of + memberInfo: ValMemberInfo * + logicalName: string * + compiledName: string + +/// The results of preliminary pass over patterns to extract variables being declared. +// We should make this a record for cleaner code +type PrelimVal1 = + | PrelimVal1 of + id: Ident * + explicitTyparInfo: ExplicitTyparInfo * + prelimType: TType * + prelimValReprInfo: PrelimValReprInfo option * + memberInfoOpt: PrelimMemberInfo option * + isMutable: bool * + inlineFlag: ValInline * + baseOrThisInfo: ValBaseOrThisInfo * + argAttribs: ArgAndRetAttribs * + visibility: SynAccess option * + isCompGen: bool + + member x.Type = let (PrelimVal1(prelimType=ty)) = x in ty + + member x.Ident = let (PrelimVal1(id=id)) = x in id + +/// The results of applying let-style generalization after type checking. +// We should make this a record for cleaner code +type PrelimVal2 = + PrelimVal2 of + id: Ident * + prelimType: GeneralizedType * + prelimValReprInfo: PrelimValReprInfo option * + memberInfoOpt: PrelimMemberInfo option * + isMutable: bool * + inlineFlag: ValInline * + baseOrThisInfo: ValBaseOrThisInfo * + argAttribs: ArgAndRetAttribs * + visibility: SynAccess option * + isCompGen: bool * + hasDeclaredTypars: bool + +/// The results of applying arity inference to PrelimVal2 +type ValScheme = + | ValScheme of + id: Ident * + typeScheme: GeneralizedType * + valReprInfo: ValReprInfo option * + memberInfo: PrelimMemberInfo option * + isMutable: bool * + inlineInfo: ValInline * + baseOrThisInfo: ValBaseOrThisInfo * + visibility: SynAccess option * + isCompGen: bool * + isIncrClass: bool * + isTyFunc: bool * + hasDeclaredTypars: bool + + member x.GeneralizedTypars = let (ValScheme(typeScheme=GeneralizedType(gtps, _))) = x in gtps + + member x.GeneralizedType = let (ValScheme(typeScheme=ts)) = x in ts + + member x.ValReprInfo = let (ValScheme(valReprInfo=valReprInfo)) = x in valReprInfo + +/// Translation of patterns is split into three phases. The first collects names. +/// The second is run after val_specs have been created for those names and inference +/// has been resolved. The second phase is run by applying a function returned by the +/// first phase. The input to the second phase is a List.map that gives the Val and type scheme +/// for each value bound by the pattern. +type TcPatPhase2Input = + | TcPatPhase2Input of NameMap * bool + + // Get an input indicating we are no longer on the left-most path through a disjunctive "or" pattern + member x.WithRightPath() = (let (TcPatPhase2Input(a, _)) = x in TcPatPhase2Input(a, false)) + +/// The first phase of checking and elaborating a binding leaves a goop of information. +/// This is a bit of a mess: much of this information is also carried on a per-value basis by the +/// "NameMap". +type CheckedBindingInfo = + | CheckedBindingInfo of + inlineFlag: ValInline * + valAttribs: Attribs * + xmlDoc: XmlDoc * + tcPatPhase2: (TcPatPhase2Input -> Pattern) * + exlicitTyparInfo: ExplicitTyparInfo * + nameToPrelimValSchemeMap: NameMap * + rhsExprChecked: Expr * + argAndRetAttribs: ArgAndRetAttribs * + overallPatTy: TType * + mBinding: range * + debugPoint: DebugPointAtBinding * + isCompilerGenerated: bool * + literalValue: Const option * + isFixed: bool + + member x.Expr = let (CheckedBindingInfo(rhsExprChecked=expr)) = x in expr + + member x.DebugPoint = let (CheckedBindingInfo(debugPoint=debugPoint)) = x in debugPoint + +type TcPatLinearEnv = TcPatLinearEnv of tpenv: UnscopedTyparEnv * names: NameMap * takenNames: Set + +type TcPatValFlags = TcPatValFlags of inlineFlag: ValInline * explicitTyparInfo: ExplicitTyparInfo * argAndRetAttribs: ArgAndRetAttribs * isMutable: bool * visibility: SynAccess option * isCompilerGenerated: bool + /// Represents the compilation environment for typechecking a single file in an assembly. [] type TcFileState = @@ -462,6 +692,12 @@ type TcFileState = isInternalTestSpanStackReferring: bool + // forward call + TcPat: WarnOnUpperFlag -> TcFileState -> TcEnv -> PrelimValReprInfo option -> TcPatValFlags -> TcPatLinearEnv -> TType -> SynPat -> (TcPatPhase2Input -> Pattern) * TcPatLinearEnv + + // forward call + TcSimplePats: TcFileState -> bool -> CheckConstraints -> TType -> TcEnv -> TcPatLinearEnv -> SynSimplePats -> string list * TcPatLinearEnv + // forward call TcSequenceExpressionEntry: TcFileState -> TcEnv -> OverallTy -> UnscopedTyparEnv -> bool * SynExpr -> range -> Expr * UnscopedTyparEnv @@ -475,7 +711,12 @@ type TcFileState = /// Create a new compilation environment static member Create (g, isScript, niceNameGen, amap, thisCcu, isSig, haveSig, conditionalDefines, tcSink, tcVal, isInternalTestSpanStackReferring, - tcSequenceExpressionEntry, tcArrayOrListSequenceExpression, tcComputationExpression) = + tcPat, + tcSimplePats, + tcSequenceExpressionEntry, + tcArrayOrListSequenceExpression, + tcComputationExpression) = + let infoReader = InfoReader(g, amap) let instantiationGenerator m tpsorig = FreshenTypars m tpsorig let nameResolver = NameResolver(g, amap, infoReader, instantiationGenerator) @@ -498,6 +739,8 @@ type TcFileState = compilingCanonicalFslibModuleType = (isSig || not haveSig) && g.compilingFSharpCore conditionalDefines = conditionalDefines isInternalTestSpanStackReferring = isInternalTestSpanStackReferring + TcPat = tcPat + TcSimplePats = tcSimplePats TcSequenceExpressionEntry = tcSequenceExpressionEntry TcArrayOrListComputedExpression = tcArrayOrListSequenceExpression TcComputationExpression = tcComputationExpression @@ -666,25 +909,6 @@ let ShrinkContext env oldRange newRange = if not (equals m oldRange) then env else { env with eContextInfo = ContextInfo.ElseBranchResult newRange } -/// Optimized unification routine that avoids creating new inference -/// variables unnecessarily -let UnifyRefTupleType contextInfo (cenv: cenv) denv m ty ps = - let g = cenv.g - let ptys = - if isRefTupleTy g ty then - let ptys = destRefTupleTy g ty - if List.length ps = List.length ptys then ptys - else NewInferenceTypes g ps - else NewInferenceTypes g ps - - let contextInfo = - match contextInfo with - | ContextInfo.RecordFields -> ContextInfo.TupleInRecordFields - | _ -> contextInfo - - AddCxTypeEqualsType contextInfo denv cenv.css m ty (TType_tuple (tupInfoRef, ptys)) - ptys - /// Allow the inference of structness from the known type, e.g. /// let (x: struct (int * int)) = (3,4) let UnifyTupleTypeAndInferCharacteristics contextInfo (cenv: cenv) denv m knownTy isExplicitStruct ps = @@ -1020,12 +1244,6 @@ let AdjustValSynInfoInSignature g ty (SynValInfo(argsData, retData) as sigMD) = | _ -> sigMD -/// The ValReprInfo for a value, except the number of typars is not yet inferred -type PrelimValReprInfo = - | PrelimValReprInfo of - curriedArgInfos: ArgReprInfo list list * - returnInfo: ArgReprInfo - let TranslateTopArgSynInfo isArg m tcAttributes (SynArgInfo(Attributes attrs, isOpt, nm)) = // Synthesize an artificial "OptionalArgument" attribute for the parameter let optAttrs = @@ -1077,12 +1295,6 @@ let ComputeLogicalName (id: Ident) (memberFlags: SynMemberFlags) = | SynMemberKind.PropertyGet -> "get_" + id.idText | SynMemberKind.PropertySet -> "set_" + id.idText -type PrelimMemberInfo = - | PrelimMemberInfo of - memberInfo: ValMemberInfo * - logicalName: string * - compiledName: string - /// Make the unique "name" for a member. // // optImplSlotTy = None (for classes) or Some ty (when implementing interface type ty) @@ -1123,233 +1335,24 @@ let MakeMemberDataAndMangledNameForMemberVal(g, tcref, isExtrinsic, attrs, implS failwithf "unexpected: intfSlotTys.Length > 1 (== %i) in MakeMemberDataAndMangledNameForMemberVal for '%s'" intfSlotTys.Length logicalName qualifiedInterfaceImplementationName g intfSlotTys.Head logicalName else - List.foldBack (fun x -> qualifiedMangledNameOfTyconRef (tcrefOfAppTy g x)) intfSlotTys logicalName - - if not isCompGen && IsMangledOpName id.idText && IsMangledInfixOperator id.idText then - let m = id.idRange - let name = DecompileOpName id.idText - // Check symbolic members. Expect valSynData implied arity to be [[2]]. - match SynInfo.AritiesOfArgs valSynData with - | [] | [0] -> warning(Error(FSComp.SR.memberOperatorDefinitionWithNoArguments name, m)) - | n :: otherArgs -> - let opTakesThreeArgs = IsTernaryOperator name - if n<>2 && not opTakesThreeArgs then warning(Error(FSComp.SR.memberOperatorDefinitionWithNonPairArgument(name, n), m)) - if n<>3 && opTakesThreeArgs then warning(Error(FSComp.SR.memberOperatorDefinitionWithNonTripleArgument(name, n), m)) - if not (isNil otherArgs) then warning(Error(FSComp.SR.memberOperatorDefinitionWithCurriedArguments name, m)) - - if isExtrinsic && IsMangledOpName id.idText then - warning(Error(FSComp.SR.tcMemberOperatorDefinitionInExtrinsic(), id.idRange)) - - PrelimMemberInfo(memberInfo, logicalName, compiledName) - -type OverridesOK = - | OverridesOK - | WarnOnOverrides - | ErrorOnOverrides - -/// A type to represent information associated with values to indicate what explicit (declared) type parameters -/// are given and what additional type parameters can be inferred, if any. -/// -/// The declared type parameters, e.g. let f<'a> (x:'a) = x, plus an indication -/// of whether additional polymorphism may be inferred, e.g. let f<'a, ..> (x:'a) y = x -type ExplicitTyparInfo = - | ExplicitTyparInfo of - rigidCopyOfDeclaredTypars: Typars * - declaredTypars: Typars * - infer: bool - -let permitInferTypars = ExplicitTyparInfo ([], [], true) -let dontInferTypars = ExplicitTyparInfo ([], [], false) - -type ArgAndRetAttribs = ArgAndRetAttribs of Attribs list list * Attribs -let noArgOrRetAttribs = ArgAndRetAttribs ([], []) - -/// A flag to represent the sort of bindings are we processing. -/// Processing "declaration" and "class" bindings that make up a module (such as "let x = 1 let y = 2") -/// shares the same code paths (e.g. TcLetBinding and TcLetrecBindings) as processing expression bindings (such as "let x = 1 in ...") -/// Member bindings also use this path. -// -// However there are differences in how different bindings get processed, -// i.e. module bindings get published to the implicitly accumulated module type, but expression 'let' bindings don't. -type DeclKind = - | ModuleOrMemberBinding - - /// Extensions to a type within the same assembly - | IntrinsicExtensionBinding - - /// Extensions to a type in a different assembly - | ExtrinsicExtensionBinding - - | ClassLetBinding of isStatic: bool - - | ObjectExpressionOverrideBinding - - | ExpressionBinding - - static member IsModuleOrMemberOrExtensionBinding x = - match x with - | ModuleOrMemberBinding -> true - | IntrinsicExtensionBinding -> true - | ExtrinsicExtensionBinding -> true - | ClassLetBinding _ -> false - | ObjectExpressionOverrideBinding -> false - | ExpressionBinding -> false - - static member MustHaveArity x = DeclKind.IsModuleOrMemberOrExtensionBinding x - - member x.CanBeDllImport = - match x with - | ModuleOrMemberBinding -> true - | IntrinsicExtensionBinding -> true - | ExtrinsicExtensionBinding -> true - | ClassLetBinding _ -> true - | ObjectExpressionOverrideBinding -> false - | ExpressionBinding -> false - - static member IsAccessModifierPermitted x = DeclKind.IsModuleOrMemberOrExtensionBinding x - - static member ImplicitlyStatic x = DeclKind.IsModuleOrMemberOrExtensionBinding x - - static member AllowedAttribTargets (memberFlagsOpt: SynMemberFlags option) x = - match x with - | ModuleOrMemberBinding | ObjectExpressionOverrideBinding -> - match memberFlagsOpt with - | Some flags when flags.MemberKind = SynMemberKind.Constructor -> AttributeTargets.Constructor - | Some flags when flags.MemberKind = SynMemberKind.PropertyGetSet -> AttributeTargets.Event ||| AttributeTargets.Property - | Some flags when flags.MemberKind = SynMemberKind.PropertyGet -> AttributeTargets.Event ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue - | Some flags when flags.MemberKind = SynMemberKind.PropertySet -> AttributeTargets.Property - | Some _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue - | None -> AttributeTargets.Field ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue - | IntrinsicExtensionBinding -> AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue - | ExtrinsicExtensionBinding -> AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue - | ClassLetBinding _ -> AttributeTargets.Field ||| AttributeTargets.Method ||| AttributeTargets.ReturnValue - | ExpressionBinding -> enum 0 // indicates attributes not allowed on expression 'let' bindings - - // Note: now always true - static member CanGeneralizeConstrainedTypars x = - match x with - | ModuleOrMemberBinding -> true - | IntrinsicExtensionBinding -> true - | ExtrinsicExtensionBinding -> true - | ClassLetBinding _ -> true - | ObjectExpressionOverrideBinding -> true - | ExpressionBinding -> true - - static member ConvertToLinearBindings x = - match x with - | ModuleOrMemberBinding -> true - | IntrinsicExtensionBinding -> true - | ExtrinsicExtensionBinding -> true - | ClassLetBinding _ -> true - | ObjectExpressionOverrideBinding -> true - | ExpressionBinding -> false - - static member CanOverrideOrImplement x = - match x with - | ModuleOrMemberBinding -> OverridesOK - | IntrinsicExtensionBinding -> WarnOnOverrides - | ExtrinsicExtensionBinding -> ErrorOnOverrides - | ClassLetBinding _ -> ErrorOnOverrides - | ObjectExpressionOverrideBinding -> OverridesOK - | ExpressionBinding -> ErrorOnOverrides - -//------------------------------------------------------------------------- -// Data structures that track the gradual accumulation of information -// about values and members during inference. -//------------------------------------------------------------------------- - -/// The results of preliminary pass over patterns to extract variables being declared. -// We should make this a record for cleaner code -type PrelimVal1 = - | PrelimVal1 of - id: Ident * - explicitTyparInfo: ExplicitTyparInfo * - prelimType: TType * - prelimValReprInfo: PrelimValReprInfo option * - memberInfoOpt: PrelimMemberInfo option * - isMutable: bool * - inlineFlag: ValInline * - baseOrThisInfo: ValBaseOrThisInfo * - argAttribs: ArgAndRetAttribs * - visibility: SynAccess option * - isCompGen: bool - - member x.Type = let (PrelimVal1(prelimType=ty)) = x in ty - - member x.Ident = let (PrelimVal1(id=id)) = x in id - -/// The results of applying let-style generalization after type checking. -// We should make this a record for cleaner code -type PrelimVal2 = - PrelimVal2 of - id: Ident * - prelimType: GeneralizedType * - prelimValReprInfo: PrelimValReprInfo option * - memberInfoOpt: PrelimMemberInfo option * - isMutable: bool * - inlineFlag: ValInline * - baseOrThisInfo: ValBaseOrThisInfo * - argAttribs: ArgAndRetAttribs * - visibility: SynAccess option * - isCompGen: bool * - hasDeclaredTypars: bool - -/// The results of applying arity inference to PrelimVal2 -type ValScheme = - | ValScheme of - id: Ident * - typeScheme: GeneralizedType * - valReprInfo: ValReprInfo option * - memberInfo: PrelimMemberInfo option * - isMutable: bool * - inlineInfo: ValInline * - baseOrThisInfo: ValBaseOrThisInfo * - visibility: SynAccess option * - isCompGen: bool * - isIncrClass: bool * - isTyFunc: bool * - hasDeclaredTypars: bool - - member x.GeneralizedTypars = let (ValScheme(typeScheme=GeneralizedType(gtps, _))) = x in gtps - - member x.GeneralizedType = let (ValScheme(typeScheme=ts)) = x in ts - - member x.ValReprInfo = let (ValScheme(valReprInfo=valReprInfo)) = x in valReprInfo - -/// Translation of patterns is split into three phases. The first collects names. -/// The second is run after val_specs have been created for those names and inference -/// has been resolved. The second phase is run by applying a function returned by the -/// first phase. The input to the second phase is a List.map that gives the Val and type scheme -/// for each value bound by the pattern. -type TcPatPhase2Input = - | TcPatPhase2Input of NameMap * bool - - // Get an input indicating we are no longer on the left-most path through a disjunctive "or" pattern - member x.WithRightPath() = (let (TcPatPhase2Input(a, _)) = x in TcPatPhase2Input(a, false)) + List.foldBack (fun x -> qualifiedMangledNameOfTyconRef (tcrefOfAppTy g x)) intfSlotTys logicalName -/// The first phase of checking and elaborating a binding leaves a goop of information. -/// This is a bit of a mess: much of this information is also carried on a per-value basis by the -/// "NameMap". -type CheckedBindingInfo = - | CheckedBindingInfo of - inlineFlag: ValInline * - valAttribs: Attribs * - xmlDoc: XmlDoc * - tcPatPhase2: (TcPatPhase2Input -> Pattern) * - exlicitTyparInfo: ExplicitTyparInfo * - nameToPrelimValSchemeMap: NameMap * - rhsExprChecked: Expr * - argAndRetAttribs: ArgAndRetAttribs * - overallPatTy: TType * - mBinding: range * - debugPoint: DebugPointAtBinding * - isCompilerGenerated: bool * - literalValue: Const option * - isFixed: bool + if not isCompGen && IsMangledOpName id.idText && IsMangledInfixOperator id.idText then + let m = id.idRange + let name = DecompileOpName id.idText + // Check symbolic members. Expect valSynData implied arity to be [[2]]. + match SynInfo.AritiesOfArgs valSynData with + | [] | [0] -> warning(Error(FSComp.SR.memberOperatorDefinitionWithNoArguments name, m)) + | n :: otherArgs -> + let opTakesThreeArgs = IsTernaryOperator name + if n<>2 && not opTakesThreeArgs then warning(Error(FSComp.SR.memberOperatorDefinitionWithNonPairArgument(name, n), m)) + if n<>3 && opTakesThreeArgs then warning(Error(FSComp.SR.memberOperatorDefinitionWithNonTripleArgument(name, n), m)) + if not (isNil otherArgs) then warning(Error(FSComp.SR.memberOperatorDefinitionWithCurriedArguments name, m)) - member x.Expr = let (CheckedBindingInfo(rhsExprChecked=expr)) = x in expr + if isExtrinsic && IsMangledOpName id.idText then + warning(Error(FSComp.SR.tcMemberOperatorDefinitionInExtrinsic(), id.idRange)) - member x.DebugPoint = let (CheckedBindingInfo(debugPoint=debugPoint)) = x in debugPoint + PrelimMemberInfo(memberInfo, logicalName, compiledName) /// Return the generalized type for a type scheme let GeneralizedTypeForTypeScheme typeScheme = @@ -4009,11 +4012,6 @@ type ImplicitlyBoundTyparsAllowed = | NewTyparsOK | NoNewTypars -/// Indicates whether constraints should be checked when checking syntactic types -type CheckConstraints = - | CheckCxs - | NoCheckCxs - /// Represents information about the module or type in which a member or value is declared. type MemberOrValContainerInfo = | MemberOrValContainerInfo of @@ -5071,408 +5069,6 @@ and TcNestedTypeApplication cenv newOk checkConstraints occ env tpenv mWholeType | _ -> error(InternalError("TcNestedTypeApplication: expected type application", mWholeTypeApp)) -and TryAdjustHiddenVarNameToCompGenName cenv env (id: Ident) altNameRefCellOpt = - match altNameRefCellOpt with - | Some ({contents = SynSimplePatAlternativeIdInfo.Undecided altId } as altNameRefCell) -> - match ResolvePatternLongIdent cenv.tcSink cenv.nameResolver AllIdsOK false id.idRange env.eAccessRights env.eNameResEnv TypeNameResolutionInfo.Default [id] with - | Item.NewDef _ -> - // The name is not in scope as a pattern identifier (e.g. union case), so do not use the alternate ID - None - | _ -> - // The name is in scope as a pattern identifier, so use the alternate ID - altNameRefCell.Value <- SynSimplePatAlternativeIdInfo.Decided altId - Some altId - | Some {contents = SynSimplePatAlternativeIdInfo.Decided altId } -> Some altId - | None -> None - -/// Bind the patterns used in a lambda. Not clear why we don't use TcPat. -and TcSimplePat optionalArgsOK checkConstraints cenv ty env (tpenv, names, takenNames) p = - let g = cenv.g - - match p with - | SynSimplePat.Id (id, altNameRefCellOpt, isCompGen, isMemberThis, isOpt, m) -> - - // Check to see if pattern translation decides to use an alternative identifier. - match TryAdjustHiddenVarNameToCompGenName cenv env id altNameRefCellOpt with - | Some altId -> - TcSimplePat optionalArgsOK checkConstraints cenv ty env (tpenv, names, takenNames) (SynSimplePat.Id (altId, None, isCompGen, isMemberThis, isOpt, m) ) - | None -> - if isOpt then - if not optionalArgsOK then - errorR(Error(FSComp.SR.tcOptionalArgsOnlyOnMembers(), m)) - - let tyarg = NewInferenceType g - UnifyTypes cenv env m ty (mkOptionTy g tyarg) - - let _, names, takenNames = TcPatBindingName cenv env id ty isMemberThis None None (ValInline.Optional, permitInferTypars, noArgOrRetAttribs, false, None, isCompGen) (names, takenNames) - id.idText, - (tpenv, names, takenNames) - - | SynSimplePat.Typed (p, cty, m) -> - let ctyR, tpenv = TcTypeAndRecover cenv NewTyparsOK checkConstraints ItemOccurence.UseInType env tpenv cty - match p with - // Optional arguments on members - | SynSimplePat.Id(_, _, _, _, true, _) -> UnifyTypes cenv env m ty (mkOptionTy g ctyR) - | _ -> UnifyTypes cenv env m ty ctyR - - TcSimplePat optionalArgsOK checkConstraints cenv ty env (tpenv, names, takenNames) p - - | SynSimplePat.Attrib (p, _, _) -> - TcSimplePat optionalArgsOK checkConstraints cenv ty env (tpenv, names, takenNames) p - -// raise an error if any optional args precede any non-optional args -and ValidateOptArgOrder (synSimplePats: SynSimplePats) = - - let rec getPats synSimplePats = - match synSimplePats with - | SynSimplePats.SimplePats(p, m) -> p, m - | SynSimplePats.Typed(p, _, _) -> getPats p - - let rec isOptArg pat = - match pat with - | SynSimplePat.Id (_, _, _, _, isOpt, _) -> isOpt - | SynSimplePat.Typed (p, _, _) -> isOptArg p - | SynSimplePat.Attrib (p, _, _) -> isOptArg p - - let pats, m = getPats synSimplePats - - let mutable hitOptArg = false - - List.iter (fun pat -> if isOptArg pat then hitOptArg <- true elif hitOptArg then error(Error(FSComp.SR.tcOptionalArgsMustComeAfterNonOptionalArgs(), m))) pats - - -/// Bind the patterns used in argument position for a function, method or lambda. -and TcSimplePats cenv optionalArgsOK checkConstraints ty env (tpenv, names, takenNames: Set<_>) p = - - let g = cenv.g - - // validate optional argument declaration - ValidateOptArgOrder p - - match p with - | SynSimplePats.SimplePats ([], m) -> - // Unit "()" patterns in argument position become SynSimplePats.SimplePats([], _) in the - // syntactic translation when building bindings. This is done because the - // use of "()" has special significance for arity analysis and argument counting. - // - // Here we give a name to the single argument implied by those patterns. - // This is a little awkward since it would be nice if this was - // uniform with the process where we give names to other (more complex) - // patterns used in argument position, e.g. "let f (D(x)) = ..." - let id = ident("unitVar" + string takenNames.Count, m) - UnifyTypes cenv env m ty g.unit_ty - let _, names, takenNames = TcPatBindingName cenv env id ty false None None (ValInline.Optional, permitInferTypars, noArgOrRetAttribs, false, None, true) (names, takenNames) - [id.idText], (tpenv, names, takenNames) - - | SynSimplePats.SimplePats ([p], _) -> - let v, (tpenv, names, takenNames) = TcSimplePat optionalArgsOK checkConstraints cenv ty env (tpenv, names, takenNames) p - [v], (tpenv, names, takenNames) - - | SynSimplePats.SimplePats (ps, m) -> - let ptys = UnifyRefTupleType env.eContextInfo cenv env.DisplayEnv m ty ps - let ps', (tpenv, names, takenNames) = List.mapFold (fun tpenv (ty, e) -> TcSimplePat optionalArgsOK checkConstraints cenv ty env tpenv e) (tpenv, names, takenNames) (List.zip ptys ps) - ps', (tpenv, names, takenNames) - - | SynSimplePats.Typed (p, cty, m) -> - let ctyR, tpenv = TcTypeAndRecover cenv NewTyparsOK CheckCxs ItemOccurence.UseInType env tpenv cty - - match p with - // Solitary optional arguments on members - | SynSimplePats.SimplePats([SynSimplePat.Id(_, _, _, _, true, _)], _) -> UnifyTypes cenv env m ty (mkOptionTy g ctyR) - | _ -> UnifyTypes cenv env m ty ctyR - - TcSimplePats cenv optionalArgsOK checkConstraints ty env (tpenv, names, takenNames) p - -and TcSimplePatsOfUnknownType cenv optionalArgsOK checkConstraints env tpenv synSimplePats = - let g = cenv.g - let argTy = NewInferenceType g - TcSimplePats cenv optionalArgsOK checkConstraints argTy env (tpenv, NameMap.empty, Set.empty) synSimplePats - -and TcPatBindingName cenv env id ty isMemberThis vis1 valReprInfo (inlineFlag, declaredTypars, argAttribs, isMutable, vis2, isCompGen) (names, takenNames: Set) = - let vis = if Option.isSome vis1 then vis1 else vis2 - - if takenNames.Contains id.idText then errorR (VarBoundTwice id) - - let isCompGen = isCompGen || IsCompilerGeneratedName id.idText - let baseOrThis = if isMemberThis then MemberThisVal else NormalVal - let prelimVal = PrelimVal1(id, declaredTypars, ty, valReprInfo, None, isMutable, inlineFlag, baseOrThis, argAttribs, vis, isCompGen) - let names = Map.add id.idText prelimVal names - let takenNames = Set.add id.idText takenNames - - let phase2 (TcPatPhase2Input (values, isLeftMost)) = - let vspec, typeScheme = - let name = id.idText - match values.TryGetValue name with - | true, value -> - if not (String.IsNullOrEmpty name) && not (String.isLeadingIdentifierCharacterUpperCase name) then - match env.eNameResEnv.ePatItems.TryGetValue name with - | true, Item.Value vref when vref.LiteralValue.IsSome -> - warning(Error(FSComp.SR.checkLowercaseLiteralBindingInPattern name, id.idRange)) - | _ -> () - value - | _ -> error(Error(FSComp.SR.tcNameNotBoundInPattern name, id.idRange)) - - // isLeftMost indicates we are processing the left-most path through a disjunctive or pattern. - // For those binding locations, CallNameResolutionSink is called in MakeAndPublishValue, like all other bindings - // For non-left-most paths, we register the name resolutions here - if not isLeftMost && not vspec.IsCompilerGenerated && not (vspec.LogicalName.StartsWithOrdinal("_")) then - let item = Item.Value(mkLocalValRef vspec) - CallNameResolutionSink cenv.tcSink (id.idRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Binding, env.AccessRights) - - PatternValBinding(vspec, typeScheme) - - phase2, names, takenNames - -and TcPatAndRecover warnOnUpper cenv (env: TcEnv) valReprInfo vFlags (tpenv, names, takenNames) ty (synPat: SynPat) = - try - TcPat warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) ty synPat - with e -> - // Error recovery - return some rubbish expression, but replace/annotate - // the type of the current expression with a type variable that indicates an error - let m = synPat.Range - errorRecovery e m - //SolveTypeAsError cenv env.DisplayEnv m ty - (fun _ -> TPat_error m), (tpenv, names, takenNames) - -/// Typecheck a pattern. Patterns are type-checked in three phases: -/// 1. TcPat builds a List.map from simple variable names to inferred types for -/// those variables. It also returns a function to perform the second phase. -/// 2. The second phase assumes the caller has built the actual value_spec's -/// for the values being defined, and has decided if the types of these -/// variables are to be generalized. The caller hands this information to -/// the second-phase function in terms of a List.map from names to actual -/// value specifications. -and TcPat warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) ty synPat = - let g = cenv.g - let ad = env.AccessRights - - match synPat with - | SynPat.As (_, SynPat.Named _, _) -> () - | SynPat.As (_, _, m) -> checkLanguageFeatureError g.langVersion LanguageFeature.NonVariablePatternsToRightOfAsPatterns m - | _ -> () - - match synPat with - | SynPat.Const (synConst, m) -> - TcConstPat warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty synConst m - - | SynPat.Wild m -> - (fun _ -> TPat_wild m), (tpenv, names, takenNames) - - | SynPat.IsInst (synTargetTy, m) - | SynPat.As (SynPat.IsInst(synTargetTy, m), _, _) -> - TcPatIsInstance warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) ty synPat synTargetTy m - - | SynPat.As (synInnerPat, SynPat.Named (SynIdent(id,_), isMemberThis, vis, m), _) - | SynPat.As (SynPat.Named (SynIdent(id,_), isMemberThis, vis, m), synInnerPat, _) -> - TcPatNamedAs warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) ty synInnerPat id isMemberThis vis m - - | SynPat.As (pat1, pat2, m) -> - TcPatUnnamedAs warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty pat1 pat2 m - - | SynPat.Named (SynIdent(id,_), isMemberThis, vis, m) -> - TcPatNamed warnOnUpper cenv env vFlags (tpenv, names, takenNames) id ty isMemberThis vis valReprInfo m - - | SynPat.OptionalVal (id, m) -> - errorR (Error (FSComp.SR.tcOptionalArgsOnlyOnMembers (), m)) - let bindf, names, takenNames = TcPatBindingName cenv env id ty false None valReprInfo vFlags (names, takenNames) - (fun values -> TPat_as (TPat_wild m, bindf values, m)), (tpenv, names, takenNames) - - | SynPat.Typed (p, cty, m) -> - let ctyR, tpenv = TcTypeAndRecover cenv NewTyparsOK CheckCxs ItemOccurence.UseInType env tpenv cty - UnifyTypes cenv env m ty ctyR - TcPat warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) ty p - - | SynPat.Attrib (innerPat, attrs, _) -> - TcPatAttributed warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty innerPat attrs - - | SynPat.Or (pat1, pat2, m, _) -> - TcPatOr warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty pat1 pat2 m - - | SynPat.Ands (pats, m) -> - TcPatAnds warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty pats m - - | SynPat.LongIdent (longDotId=longDotId; typarDecls=tyargs; argPats=args; accessibility=vis; range=m) -> - TcPatLongIdent warnOnUpper cenv env ad valReprInfo vFlags (tpenv, names, takenNames) ty (longDotId, tyargs, args, vis, m) - - | SynPat.QuoteExpr(_, m) -> - errorR (Error(FSComp.SR.tcInvalidPattern(), m)) - (fun _ -> TPat_error m), (tpenv, names, takenNames) - - | SynPat.Tuple (isExplicitStruct, args, m) -> - TcPatTuple warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty isExplicitStruct args m - - | SynPat.Paren (p, _) -> - TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty p - - | SynPat.ArrayOrList (isArray, args, m) -> - TcPatArrayOrList warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty isArray args m - - | SynPat.Record (flds, m) -> - TcRecordPat warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty flds m - - | SynPat.DeprecatedCharRange (c1, c2, m) -> - errorR(Deprecated(FSComp.SR.tcUseWhenPatternGuard(), m)) - UnifyTypes cenv env m ty g.char_ty - (fun _ -> TPat_range(c1, c2, m)), (tpenv, names, takenNames) - - | SynPat.Null m -> - TcNullPat cenv env (tpenv, names, takenNames) ty m - - | SynPat.InstanceMember (range=m) -> - errorR(Error(FSComp.SR.tcIllegalPattern(), synPat.Range)) - (fun _ -> TPat_wild m), (tpenv, names, takenNames) - - | SynPat.FromParseError (pat, _) -> - suppressErrorReporting (fun () -> TcPatAndRecover warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) (NewErrorType()) pat) - -and TcConstPat warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty synConst m = - let g = cenv.g - match synConst with - | SynConst.Bytes (bytes, _, m) -> - UnifyTypes cenv env m ty (mkByteArrayTy g) - let synReplacementExpr = SynPat.ArrayOrList (true, [ for b in bytes -> SynPat.Const(SynConst.Byte b, m) ], m) - TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty synReplacementExpr - - | SynConst.UserNum _ -> - errorR (Error (FSComp.SR.tcInvalidNonPrimitiveLiteralInPatternMatch (), m)) - (fun _ -> TPat_error m), (tpenv, names, takenNames) - - | _ -> - try - let c = TcConst cenv ty m env synConst - (fun _ -> TPat_const (c, m)), (tpenv, names, takenNames) - with e -> - errorRecovery e m - (fun _ -> TPat_error m), (tpenv, names, takenNames) - -and TcPatNamedAs warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) ty synInnerPat id isMemberThis vis m = - let bindf, names, takenNames = TcPatBindingName cenv env id ty isMemberThis vis valReprInfo vFlags (names, takenNames) - let innerPat, acc = TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty synInnerPat - let phase2 values = TPat_as (innerPat values, bindf values, m) - phase2, acc - -and TcPatUnnamedAs warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty pat1 pat2 m = - let pats = [pat1; pat2] - let patsR, acc = TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) (List.map (fun _ -> ty) pats) pats - let phase2 values = TPat_conjs(List.map (fun f -> f values) patsR, m) - phase2, acc - -and TcPatNamed warnOnUpper cenv env vFlags (tpenv, names, takenNames) id ty isMemberThis vis valReprInfo m = - let bindf, names, takenNames = TcPatBindingName cenv env id ty isMemberThis vis valReprInfo vFlags (names, takenNames) - let pat', acc = TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty (SynPat.Wild m) - let phase2 values = TPat_as (pat' values, bindf values, m) - phase2, acc - -and TcPatIsInstance warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) srcTy synPat synTargetTy m = - let tgtTy, tpenv = TcTypeAndRecover cenv NewTyparsOKButWarnIfNotRigid CheckCxs ItemOccurence.UseInType env tpenv synTargetTy - TcRuntimeTypeTest false true cenv env.DisplayEnv m tgtTy srcTy - match synPat with - | SynPat.IsInst(_, m) -> - (fun _ -> TPat_isinst (srcTy, tgtTy, None, m)), (tpenv, names, takenNames) - | SynPat.As (SynPat.IsInst _, p, m) -> - let pat, acc = TcPat warnOnUpper cenv env valReprInfo vFlags (tpenv, names, takenNames) tgtTy p - (fun values -> TPat_isinst (srcTy, tgtTy, Some (pat values), m)), acc - | _ -> failwith "TcPat" - -and TcPatAttributed warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty innerPat attrs = - errorR (Error (FSComp.SR.tcAttributesInvalidInPatterns (), rangeOfNonNilAttrs attrs)) - for attrList in attrs do - TcAttributes cenv env Unchecked.defaultof<_> attrList.Attributes |> ignore - TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty innerPat - -and TcPatOr warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty pat1 pat2 m = - let pat1R, (tpenv, names1, takenNames1) = TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty pat1 - let pat2R, (tpenv, names2, takenNames2) = TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) ty pat2 - - if not (takenNames1 = takenNames2) then - errorR (UnionPatternsBindDifferentNames m) - - names1 |> Map.iter (fun _ (PrelimVal1 (id=id1; prelimType=ty1)) -> - match names2.TryGetValue id1.idText with - | true, PrelimVal1 (id=id2; prelimType=ty2) -> - try UnifyTypes cenv env id2.idRange ty1 ty2 - with exn -> errorRecovery exn m - | _ -> ()) - - let names = NameMap.layer names1 names2 - let takenNames = Set.union takenNames1 takenNames2 - let phase2 values = TPat_disjs ([pat1R values; pat2R (values.WithRightPath())], m) - phase2, (tpenv, names, takenNames) - -and TcPatAnds warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty pats m = - let patsR, acc = TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) (List.map (fun _ -> ty) pats) pats - let phase2 values = TPat_conjs(List.map (fun f -> f values) patsR, m) - phase2, acc - -and TcPatTuple warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty isExplicitStruct args m = - let g = cenv.g - try - let tupInfo, argTys = UnifyTupleTypeAndInferCharacteristics env.eContextInfo cenv env.DisplayEnv m ty isExplicitStruct args - let argsR, acc = TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) argTys args - let phase2 values = TPat_tuple(tupInfo, List.map (fun f -> f values) argsR, argTys, m) - phase2, acc - with e -> - errorRecovery e m - let _, acc = TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) (NewInferenceTypes g args) args - let phase2 _ = TPat_error m - phase2, acc - -and TcPatArrayOrList warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty isArray args m = - let g = cenv.g - let argTy = NewInferenceType g - UnifyTypes cenv env m ty (if isArray then mkArrayType g argTy else mkListTy g argTy) - let argsR, acc = TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) (List.map (fun _ -> argTy) args) args - let phase2 values = - let argsR = List.map (fun f -> f values) argsR - if isArray then TPat_array(argsR, argTy, m) - else List.foldBack (mkConsListPat g argTy) argsR (mkNilListPat g m argTy) - phase2, acc - -and TcRecordPat warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty fieldPats m = - let fieldPats = fieldPats |> List.map (fun (fieldId, _, fieldPat) -> fieldId, fieldPat) - let tinst, tcref, fldsmap, _fldsList = BuildFieldMap cenv env true ty fieldPats m - let gtyp = mkAppTy tcref tinst - let inst = List.zip (tcref.Typars m) tinst - - UnifyTypes cenv env m ty gtyp - - let fields = tcref.TrueInstanceFieldsAsList - let ftys = fields |> List.map (fun fsp -> actualTyOfRecdField inst fsp, fsp) - - let fieldPats, acc = - ((tpenv, names, takenNames), ftys) ||> List.mapFold (fun s (ty, fsp) -> - match fldsmap.TryGetValue fsp.rfield_id.idText with - | true, v -> TcPat warnOnUpper cenv env None vFlags s ty v - | _ -> (fun _ -> TPat_wild m), s) - - let phase2 values = - TPat_recd (tcref, tinst, List.map (fun f -> f values) fieldPats, m) - - phase2, acc - -and TcNullPat cenv env (tpenv, names, takenNames) ty m = - try - AddCxTypeUseSupportsNull env.DisplayEnv cenv.css m NoTrace ty - with exn -> - errorRecovery exn m - (fun _ -> TPat_null m), (tpenv, names, takenNames) - -and CheckNoArgsForLiteral args m = - match args with - | SynArgPats.Pats [] - | SynArgPats.NamePatPairs ([], _) -> () - | _ -> errorR (Error (FSComp.SR.tcLiteralDoesNotTakeArguments (), m)) - -and GetSynArgPatterns args = - match args with - | SynArgPats.Pats args -> args - | SynArgPats.NamePatPairs (pairs, _) -> List.map (fun (_, _, pat) -> pat) pairs - -and TcArgPats warnOnUpper cenv env vFlags (tpenv, names, takenNames) args = - let g = cenv.g - let args = GetSynArgPatterns args - TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) (NewInferenceTypes g args) args - /// The pattern syntax can also represent active pattern arguments. This routine /// converts from the pattern syntax to the expression syntax. /// @@ -5519,89 +5115,22 @@ and ConvSynPatToSynExpr synPat = | _ -> error(Error(FSComp.SR.tcInvalidArgForParameterizedPattern(), synPat.Range)) -and IsNameOf (cenv: cenv) (env: TcEnv) ad m (id: Ident) = - let g = cenv.g - id.idText = "nameof" && - try - match ResolveExprLongIdent cenv.tcSink cenv.nameResolver m ad env.NameEnv TypeNameResolutionInfo.Default [id] with - | Result (_, Item.Value vref, _) -> valRefEq g vref g.nameof_vref - | _ -> false - with _ -> false - -/// Check a long identifier in a pattern -and TcPatLongIdent warnOnUpper cenv env ad valReprInfo vFlags (tpenv, names, takenNames) ty (longDotId, tyargs, args, vis, m) = - let (SynLongIdent(longId, _, _)) = longDotId - - if tyargs.IsSome then errorR(Error(FSComp.SR.tcInvalidTypeArgumentUsage(), m)) - - let warnOnUpperForId = - match args with - | SynArgPats.Pats [] -> warnOnUpper - | _ -> AllIdsOK - - let lidRange = rangeOfLid longId - - match ResolvePatternLongIdent cenv.tcSink cenv.nameResolver warnOnUpperForId false m ad env.NameEnv TypeNameResolutionInfo.Default longId with - | Item.NewDef id -> - TcPatLongIdentNewDef warnOnUpperForId warnOnUpper cenv env ad valReprInfo vFlags (tpenv, names, takenNames) ty (vis, id, args, m) - - | Item.ActivePatternCase apref as item -> - TcPatLongIdentActivePatternCase warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, item, apref, args, m) - - | Item.UnionCase _ | Item.ExnCase _ as item -> - TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags (tpenv, names, takenNames) ty (lidRange, item, args, m) - - | Item.ILField finfo -> - TcPatLongIdentILField warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, finfo, args, m) - - | Item.RecdField rfinfo -> - TcPatLongIdentRecdField warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, rfinfo, args, m) - - | Item.Value vref -> - TcPatLongIdentLiteral warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, vref, args, m) - - | _ -> error (Error(FSComp.SR.tcRequireVarConstRecogOrLiteral(), m)) - -/// Check a long identifier in a pattern that has been not been resolved to anything else and represents a new value, or nameof -and TcPatLongIdentNewDef warnOnUpperForId warnOnUpper cenv env ad valReprInfo vFlags (tpenv, names, takenNames) ty (vis, id, args, m) = - let g = cenv.g - - match GetSynArgPatterns args with - | [] -> - TcPat warnOnUpperForId cenv env valReprInfo vFlags (tpenv, names, takenNames) ty (mkSynPatVar vis id) - - | [arg] - when g.langVersion.SupportsFeature LanguageFeature.NameOf && IsNameOf cenv env ad m id -> - match TcNameOfExpr cenv env tpenv (ConvSynPatToSynExpr arg) with - | Expr.Const(c, m, _) -> (fun _ -> TPat_const (c, m)), (tpenv, names, takenNames) - | _ -> failwith "Impossible: TcNameOfExpr must return an Expr.Const" - - | _ -> - let _, acc = TcArgPats warnOnUpper cenv env vFlags (tpenv, names, takenNames) args - errorR (UndefinedName (0, FSComp.SR.undefinedNamePatternDiscriminator, id, NoSuggestions)) - (fun _ -> TPat_error m), acc - -/// Check a long identifier 'Case' or 'Case argsR that has been resolved to an active pattern case -and TcPatLongIdentActivePatternCase warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, item, apref, args, m) = +/// Check a long identifier 'Case' or 'Case argsR' that has been resolved to an active pattern case +and TcPatLongIdentActivePatternCase warnOnUpper cenv (env: TcEnv) vFlags patEnv ty (lidRange, item, apref, args, m) = let g = cenv.g + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv let (APElemRef (apinfo, vref, idx, isStructRetTy)) = apref // Report information about the 'active recognizer' occurrence to IDE CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.eAccessRights) - match args with - | SynArgPats.Pats _ -> () - | _ -> errorR (Error (FSComp.SR.tcNamedActivePattern apinfo.ActiveTags[idx], m)) - - let args = GetSynArgPatterns args - // TOTAL/PARTIAL ACTIVE PATTERNS let _, vexp, _, _, tinst, _ = TcVal true cenv env tpenv vref None None m let vexp = MakeApplicableExprWithFlex cenv env vexp let vexpty = vexp.Type - let activePatArgsAsSynPats, patarg = + let activePatArgsAsSynPats, patArg = match args with | [] -> [], SynPat.Const(SynConst.Unit, m) | _ -> @@ -5629,186 +5158,18 @@ and TcPatLongIdentActivePatternCase warnOnUpper cenv env vFlags (tpenv, names, t let activePatExpr, tpenv = PropagateThenTcDelayed cenv (MustEqual activePatType) env tpenv m vexp vexpty ExprAtomicFlag.NonAtomic delayed + let patEnvR = TcPatLinearEnv(tpenv, names, takenNames) + if idx >= activePatResTys.Length then error(Error(FSComp.SR.tcInvalidIndexIntoActivePatternArray(), m)) let argTy = List.item idx activePatResTys - let arg', acc = TcPat warnOnUpper cenv env None vFlags (tpenv, names, takenNames) argTy patarg + let patArgPhase2, acc = cenv.TcPat warnOnUpper cenv env None vFlags patEnvR argTy patArg // The identity of an active pattern consists of its value and the types it is applied to. // If there are any expression args then we've lost identity. let activePatIdentity = if isNil activePatArgsAsSynExprs then Some (vref, tinst) else None - (fun values -> - TPat_query((activePatExpr, activePatResTys, isStructRetTy, activePatIdentity, idx, apinfo), arg' values, m)), acc - -/// Check a long identifier 'Case' or 'Case argsR that has been resolved to a union case or F# exception constructor -and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags (tpenv, names, takenNames) ty (lidRange, item, args, m) = - let g = cenv.g - - // Report information about the case occurrence to IDE - CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.eAccessRights) - - let mkf, argTys, argNames = ApplyUnionCaseOrExnTypesForPat m cenv env ty item - let numArgTys = argTys.Length - - let args, extraPatternsFromNames = - match args with - | SynArgPats.Pats args -> args, [] - | SynArgPats.NamePatPairs (pairs, m) -> - // rewrite patterns from the form (name-N = pat-N; ...) to (..._, pat-N, _...) - // so type T = Case of name: int * value: int - // | Case(value = v) - // will become - // | Case(_, v) - let result = Array.zeroCreate numArgTys - let extraPatterns = List () - - for id, _, pat in pairs do - match argNames |> List.tryFindIndex (fun id2 -> id.idText = id2.idText) with - | None -> - extraPatterns.Add pat - match item with - | Item.UnionCase(uci, _) -> - errorR (Error (FSComp.SR.tcUnionCaseConstructorDoesNotHaveFieldWithGivenName (uci.DisplayName, id.idText), id.idRange)) - | Item.ExnCase tcref -> - errorR (Error (FSComp.SR.tcExceptionConstructorDoesNotHaveFieldWithGivenName (tcref.DisplayName, id.idText), id.idRange)) - | _ -> - errorR (Error (FSComp.SR.tcConstructorDoesNotHaveFieldWithGivenName id.idText, id.idRange)) - - | Some idx -> - let argItem = - match item with - | Item.UnionCase (uci, _) -> Item.UnionCaseField (uci, idx) - | Item.ExnCase tref -> Item.RecdField (RecdFieldInfo ([], RecdFieldRef (tref, id.idText))) - | _ -> failwithf "Expecting union case or exception item, got: %O" item - - CallNameResolutionSink cenv.tcSink (id.idRange, env.NameEnv, argItem, emptyTyparInst, ItemOccurence.Pattern, ad) - - match box result[idx] with - | Null -> result[idx] <- pat - | NonNull _ -> - extraPatterns.Add pat - errorR (Error (FSComp.SR.tcUnionCaseFieldCannotBeUsedMoreThanOnce id.idText, id.idRange)) - - for i = 0 to numArgTys - 1 do - if isNull (box result[i]) then - result[i] <- SynPat.Wild (m.MakeSynthetic()) - - let extraPatterns = List.ofSeq extraPatterns - - let args = List.ofArray result - if result.Length = 1 then args, extraPatterns - else [ SynPat.Tuple(false, args, m) ], extraPatterns - - let args, extraPatterns = - match args with - | [] -> [], [] - - // note: the next will always be parenthesized - | [SynPatErrorSkip(SynPat.Tuple (false, args, _)) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Tuple (false, args, _)), _))] when numArgTys > 1 -> args, [] - - // note: we allow both 'C _' and 'C (_)' regardless of number of argument of the pattern - | [SynPatErrorSkip(SynPat.Wild _ as e) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Wild _ as e), _))] -> List.replicate numArgTys e, [] - - - | args when numArgTys = 0 -> - errorR (Error (FSComp.SR.tcUnionCaseDoesNotTakeArguments (), m)) - [], args - - | arg :: rest when numArgTys = 1 -> - if numArgTys = 1 && not (List.isEmpty rest) then - errorR (Error (FSComp.SR.tcUnionCaseRequiresOneArgument (), m)) - [arg], rest - - | [arg] -> [arg], [] - - | args -> - [], args - - let args, extraPatterns = - let numArgs = args.Length - if numArgs = numArgTys then - args, extraPatterns - elif numArgs < numArgTys then - if numArgTys > 1 then - // Expects tuple without enough args - errorR (Error (FSComp.SR.tcUnionCaseExpectsTupledArguments numArgTys, m)) - else - errorR (UnionCaseWrongArguments (env.DisplayEnv, numArgTys, numArgs, m)) - args @ (List.init (numArgTys - numArgs) (fun _ -> SynPat.Wild (m.MakeSynthetic()))), extraPatterns - else - let args, remaining = args |> List.splitAt numArgTys - for remainingArg in remaining do - errorR (UnionCaseWrongArguments (env.DisplayEnv, numArgTys, numArgs, remainingArg.Range)) - args, extraPatterns @ remaining - - let extraPatterns = extraPatterns @ extraPatternsFromNames - let argsR, acc = TcPatterns warnOnUpper cenv env vFlags (tpenv, names, takenNames) argTys args - let _, acc = TcPatterns warnOnUpper cenv env vFlags acc (NewInferenceTypes g extraPatterns) extraPatterns - (fun values -> mkf m (List.map (fun f -> f values) argsR)), acc - -/// Check a long identifier that has been resolved to an IL field - valid if a literal -and TcPatLongIdentILField warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, finfo, args, m) = - let g = cenv.g - - CheckILFieldInfoAccessible g cenv.amap lidRange env.AccessRights finfo - - if not finfo.IsStatic then - errorR (Error (FSComp.SR.tcFieldIsNotStatic finfo.FieldName, lidRange)) - - CheckILFieldAttributes g finfo m - - match finfo.LiteralValue with - | None -> - error (Error (FSComp.SR.tcFieldNotLiteralCannotBeUsedInPattern (), lidRange)) - | Some lit -> - CheckNoArgsForLiteral args m - let _, acc = TcArgPats warnOnUpper cenv env vFlags (tpenv, names, takenNames) args - - UnifyTypes cenv env m ty (finfo.FieldType (cenv.amap, m)) - let c' = TcFieldInit lidRange lit - let item = Item.ILField finfo - CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.AccessRights) - (fun _ -> TPat_const (c', m)), acc - -/// Check a long identifier that has been resolved to a record field -and TcPatLongIdentRecdField warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, rfinfo, args, m) = - let g = cenv.g - CheckRecdFieldInfoAccessible cenv.amap lidRange env.AccessRights rfinfo - if not rfinfo.IsStatic then errorR (Error (FSComp.SR.tcFieldIsNotStatic(rfinfo.DisplayName), lidRange)) - CheckRecdFieldInfoAttributes g rfinfo lidRange |> CommitOperationResult - match rfinfo.LiteralValue with - | None -> error (Error(FSComp.SR.tcFieldNotLiteralCannotBeUsedInPattern(), lidRange)) - | Some lit -> - CheckNoArgsForLiteral args m - let _, acc = TcArgPats warnOnUpper cenv env vFlags (tpenv, names, takenNames) args - - UnifyTypes cenv env m ty rfinfo.FieldType - let item = Item.RecdField rfinfo - // FUTURE: can we do better than emptyTyparInst here, in order to display instantiations - // of type variables in the quick info provided in the IDE. - CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.AccessRights) - (fun _ -> TPat_const (lit, m)), acc - -/// Check a long identifier that has been resolved to an F# value that is a literal -and TcPatLongIdentLiteral warnOnUpper cenv env vFlags (tpenv, names, takenNames) ty (lidRange, vref, args, m) = - let g = cenv.g - match vref.LiteralValue with - | None -> error (Error(FSComp.SR.tcNonLiteralCannotBeUsedInPattern(), m)) - | Some lit -> - let _, _, _, vexpty, _, _ = TcVal true cenv env tpenv vref None None lidRange - CheckValAccessible lidRange env.AccessRights vref - CheckFSharpAttributes g vref.Attribs lidRange |> CommitOperationResult - CheckNoArgsForLiteral args m - let _, acc = TcArgPats warnOnUpper cenv env vFlags (tpenv, names, takenNames) args - - UnifyTypes cenv env m ty vexpty - let item = Item.Value vref - CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.AccessRights) - (fun _ -> TPat_const (lit, m)), acc - -and TcPatterns warnOnUpper cenv env vFlags s argTys args = - assert (List.length args = List.length argTys) - List.mapFold (fun s (ty, pat) -> TcPat warnOnUpper cenv env None vFlags s ty pat) s (List.zip argTys args) + let phase2 values = TPat_query((activePatExpr, activePatResTys, isStructRetTy, activePatIdentity, idx, apinfo), patArgPhase2 values, m) + phase2, acc and RecordNameAndTypeResolutions cenv env tpenv expr = // This function is motivated by cases like @@ -5948,6 +5309,35 @@ and TryTcStmt cenv env tpenv synExpr = let hasTypeUnit = TryUnifyUnitTypeWithoutWarning cenv env m ty hasTypeUnit, expr, tpenv +and CheckForAdjacentListExpression cenv synExpr hpa isInfix delayed (arg: SynExpr) = + let g = cenv.g + // func (arg)[arg2] gives warning that .[ must be used. + match delayed with + | DelayedApp (hpa2, isSugar2, _, arg2, _) :: _ when not isInfix && (hpa = ExprAtomicFlag.NonAtomic) && isAdjacentListExpr isSugar2 hpa2 (Some synExpr) arg2 -> + let mWarning = unionRanges arg.Range arg2.Range + + match arg with + | SynExpr.Paren _ -> + if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then + warning(Error(FSComp.SR.tcParenThenAdjacentListArgumentNeedsAdjustment(), mWarning)) + elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then + informationalWarning(Error(FSComp.SR.tcParenThenAdjacentListArgumentReserved(), mWarning)) + + | SynExpr.ArrayOrListComputed _ + | SynExpr.ArrayOrList _ -> + if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then + warning(Error(FSComp.SR.tcListThenAdjacentListArgumentNeedsAdjustment(), mWarning)) + elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then + informationalWarning(Error(FSComp.SR.tcListThenAdjacentListArgumentReserved(), mWarning)) + + | _ -> + if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then + warning(Error(FSComp.SR.tcOtherThenAdjacentListArgumentNeedsAdjustment(), mWarning)) + elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then + informationalWarning(Error(FSComp.SR.tcOtherThenAdjacentListArgumentReserved(), mWarning)) + + | _ -> () + /// During checking of expressions of the form (x(y)).z(w1, w2) /// keep a stack of things on the right. This lets us recognize /// method applications and other item-based syntax. @@ -5967,7 +5357,8 @@ and TcExprThen cenv overallTy env tpenv isArg synExpr delayed = match altNameRefCellOpt with | Some {contents = SynSimplePatAlternativeIdInfo.Decided altId} -> TcExprThen cenv overallTy env tpenv isArg (SynExpr.LongIdent (isOpt, SynLongIdent([altId], [], [None]), None, mLongId)) delayed - | _ -> TcLongIdentThen cenv overallTy env tpenv longId delayed + | _ -> + TcLongIdentThen cenv overallTy env tpenv longId delayed // f?x<-v | SynExpr.Set(SynExpr.Dynamic(e1, _, e2, _) , rhsExpr, m) -> @@ -5978,33 +5369,8 @@ and TcExprThen cenv overallTy env tpenv isArg synExpr delayed = // f[x] // hpa=true | SynExpr.App (hpa, isInfix, func, arg, mFuncAndArg) -> TcNonControlFlowExpr env <| fun env -> - - // func (arg)[arg2] gives warning that .[ must be used. - match delayed with - | DelayedApp (hpa2, isSugar2, _, arg2, _) :: _ when not isInfix && (hpa = ExprAtomicFlag.NonAtomic) && isAdjacentListExpr isSugar2 hpa2 (Some synExpr) arg2 -> - let mWarning = unionRanges arg.Range arg2.Range - - match arg with - | SynExpr.Paren _ -> - if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then - warning(Error(FSComp.SR.tcParenThenAdjacentListArgumentNeedsAdjustment(), mWarning)) - elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then - informationalWarning(Error(FSComp.SR.tcParenThenAdjacentListArgumentReserved(), mWarning)) - - | SynExpr.ArrayOrListComputed _ - | SynExpr.ArrayOrList _ -> - if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then - warning(Error(FSComp.SR.tcListThenAdjacentListArgumentNeedsAdjustment(), mWarning)) - elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then - informationalWarning(Error(FSComp.SR.tcListThenAdjacentListArgumentReserved(), mWarning)) - - | _ -> - if g.langVersion.SupportsFeature LanguageFeature.IndexerNotationWithoutDot then - warning(Error(FSComp.SR.tcOtherThenAdjacentListArgumentNeedsAdjustment(), mWarning)) - elif not (g.langVersion.IsExplicitlySpecifiedAs50OrBefore()) then - informationalWarning(Error(FSComp.SR.tcOtherThenAdjacentListArgumentReserved(), mWarning)) - - | _ -> () + + CheckForAdjacentListExpression cenv synExpr hpa isInfix delayed arg TcExprThen cenv overallTy env tpenv false func ((DelayedApp (hpa, isInfix, Some func, arg, mFuncAndArg)) :: delayed) @@ -6876,8 +6242,8 @@ and TcIteratedLambdas cenv isFirst (env: TcEnv) overallTy takenNames tpenv e = let domainTy, resultTy = UnifyFunctionType None cenv env.DisplayEnv m overallTy.Commit - let vs, (tpenv, names, takenNames) = - TcSimplePats cenv isMember CheckCxs domainTy env (tpenv, Map.empty, takenNames) synSimplePats + let vs, (TcPatLinearEnv (tpenv, names, takenNames)) = + cenv.TcSimplePats cenv isMember CheckCxs domainTy env (TcPatLinearEnv (tpenv, Map.empty, takenNames)) synSimplePats let envinner, _, vspecMap = MakeAndPublishSimpleValsForMergedScope cenv env m names let byrefs = vspecMap |> Map.map (fun _ v -> isByrefTy g v.Type, v) @@ -7160,7 +6526,6 @@ and TcIndexingThen cenv env overallTy mWholeExpr mDot tpenv setInfo synLeftExprO // deprecated constrained lookup error(Error(FSComp.SR.tcObjectOfIndeterminateTypeUsedRequireTypeConstraint(), mWholeExpr)) - /// Check a 'new Type(args)' expression, also an 'inheritedTys declaration in an implicit or explicit class /// For 'new Type(args)', mWholeExprOrObjTy is the whole expression /// For 'inherit Type(args)', mWholeExprOrObjTy is the whole expression @@ -10623,7 +9988,7 @@ and TcAndPatternCompileMatchClauses mExpr mMatch actionOnFailure cenv inputExprO and TcMatchPattern cenv inputTy env tpenv (synPat: SynPat) (synWhenExprOpt: SynExpr option) = let g = cenv.g let m = synPat.Range - let patf', (tpenv, names, _) = TcPat WarnOnUpperCase cenv env None (ValInline.Optional, permitInferTypars, noArgOrRetAttribs, false, None, false) (tpenv, Map.empty, Set.empty) inputTy synPat + let patf', (TcPatLinearEnv (tpenv, names, _)) = cenv.TcPat WarnOnUpperCase cenv env None (TcPatValFlags (ValInline.Optional, permitInferTypars, noArgOrRetAttribs, false, None, false)) (TcPatLinearEnv (tpenv, Map.empty, Set.empty)) inputTy synPat let envinner, values, vspecMap = MakeAndPublishSimpleValsForMergedScope cenv env m names let whenExprOpt, tpenv = @@ -10906,8 +10271,8 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt let prelimValReprInfo = TranslateSynValInfo mBinding (TcAttributes cenv env) valSynInfo // Check the pattern of the l.h.s. of the binding - let tcPatPhase2, (tpenv, nameToPrelimValSchemeMap, _) = - TcPat AllIdsOK cenv envinner (Some prelimValReprInfo) (inlineFlag, explicitTyparInfo, argAndRetAttribs, isMutable, vis, isCompGen) (tpenv, NameMap.empty, Set.empty) overallPatTy pat + let tcPatPhase2, (TcPatLinearEnv (tpenv, nameToPrelimValSchemeMap, _)) = + cenv.TcPat AllIdsOK cenv envinner (Some prelimValReprInfo) (TcPatValFlags (inlineFlag, explicitTyparInfo, argAndRetAttribs, isMutable, vis, isCompGen)) (TcPatLinearEnv (tpenv, NameMap.empty, Set.empty)) overallPatTy pat // Add active pattern result names to the environment let apinfoOpt = @@ -11478,7 +10843,7 @@ and ApplyTypesFromArgumentPatterns (cenv, env, optionalArgsOK, ty, m, tpenv, Nor let domainTy, resultTy = UnifyFunctionType None cenv env.DisplayEnv m ty // We apply the type information from the patterns by type checking the // "simple" patterns against 'domainTyR'. They get re-typechecked later. - ignore (TcSimplePats cenv optionalArgsOK CheckCxs domainTy env (tpenv, Map.empty, Set.empty) pushedPat) + ignore (cenv.TcSimplePats cenv optionalArgsOK CheckCxs domainTy env (TcPatLinearEnv (tpenv, Map.empty, Set.empty)) pushedPat) ApplyTypesFromArgumentPatterns (cenv, env, optionalArgsOK, resultTy, m, tpenv, NormalizedBindingRhs (morePushedPats, retInfoOpt, e), memberFlagsOpt) /// Check if the type annotations and inferred type information in a value give a @@ -12542,51 +11907,51 @@ let TcAndPublishValSpec (cenv, env, containerInfo: ContainerInfo, declKind, memF (tpenv, valinfos) ||> List.mapFold (fun tpenv valSpecResult -> - let (ValSpecResult (altActualParent, memberInfoOpt, id, enclosingDeclaredTypars, declaredTypars, ty, prelimValReprInfo, declKind)) = valSpecResult + let (ValSpecResult (altActualParent, memberInfoOpt, id, enclosingDeclaredTypars, declaredTypars, ty, prelimValReprInfo, declKind)) = valSpecResult - let inlineFlag = ComputeInlineFlag (memberInfoOpt |> Option.map (fun (PrelimMemberInfo(memberInfo, _, _)) -> memberInfo.MemberFlags)) isInline mutableFlag m + let inlineFlag = ComputeInlineFlag (memberInfoOpt |> Option.map (fun (PrelimMemberInfo(memberInfo, _, _)) -> memberInfo.MemberFlags)) isInline mutableFlag m - let freeInType = freeInTypeLeftToRight g false ty + let freeInType = freeInTypeLeftToRight g false ty - let allDeclaredTypars = enclosingDeclaredTypars @ declaredTypars + let allDeclaredTypars = enclosingDeclaredTypars @ declaredTypars - let explicitTyparInfo = ExplicitTyparInfo(declaredTypars, declaredTypars, synCanInferTypars) + let explicitTyparInfo = ExplicitTyparInfo(declaredTypars, declaredTypars, synCanInferTypars) - let generalizedTypars = - GeneralizationHelpers.ComputeAndGeneralizeGenericTypars(cenv, denv, id.idRange, - emptyFreeTypars, canInferTypars, CanGeneralizeConstrainedTypars, inlineFlag, - None, allDeclaredTypars, freeInType, ty, false) + let generalizedTypars = + GeneralizationHelpers.ComputeAndGeneralizeGenericTypars(cenv, denv, id.idRange, + emptyFreeTypars, canInferTypars, CanGeneralizeConstrainedTypars, inlineFlag, + None, allDeclaredTypars, freeInType, ty, false) - let valscheme1 = PrelimVal1(id, explicitTyparInfo, ty, Some prelimValReprInfo, memberInfoOpt, mutableFlag, inlineFlag, NormalVal, noArgOrRetAttribs, vis, false) + let valscheme1 = PrelimVal1(id, explicitTyparInfo, ty, Some prelimValReprInfo, memberInfoOpt, mutableFlag, inlineFlag, NormalVal, noArgOrRetAttribs, vis, false) - let valscheme2 = GeneralizeVal cenv denv enclosingDeclaredTypars generalizedTypars valscheme1 + let valscheme2 = GeneralizeVal cenv denv enclosingDeclaredTypars generalizedTypars valscheme1 - let tpenv = HideUnscopedTypars generalizedTypars tpenv + let tpenv = HideUnscopedTypars generalizedTypars tpenv - let valscheme = BuildValScheme declKind (Some prelimValReprInfo) valscheme2 + let valscheme = BuildValScheme declKind (Some prelimValReprInfo) valscheme2 - let literalValue = - match literalExprOpt with - | None -> - let hasLiteralAttr = HasFSharpAttribute g g.attrib_LiteralAttribute attrs - if hasLiteralAttr then - errorR(Error(FSComp.SR.tcLiteralAttributeRequiresConstantValue(), m)) - None + let literalValue = + match literalExprOpt with + | None -> + let hasLiteralAttr = HasFSharpAttribute g g.attrib_LiteralAttribute attrs + if hasLiteralAttr then + errorR(Error(FSComp.SR.tcLiteralAttributeRequiresConstantValue(), m)) + None - | Some e -> - let hasLiteralAttr, literalValue = TcLiteral cenv ty env tpenv (attrs, e) - if not hasLiteralAttr then - errorR(Error(FSComp.SR.tcValueInSignatureRequiresLiteralAttribute(), e.Range)) - literalValue + | Some e -> + let hasLiteralAttr, literalValue = TcLiteral cenv ty env tpenv (attrs, e) + if not hasLiteralAttr then + errorR(Error(FSComp.SR.tcValueInSignatureRequiresLiteralAttribute(), e.Range)) + literalValue - let paramNames = - match valscheme.ValReprInfo with - | None -> None - | Some valReprInfo -> Some valReprInfo.ArgNames + let paramNames = + match valscheme.ValReprInfo with + | None -> None + | Some valReprInfo -> Some valReprInfo.ArgNames - let xmlDoc = xmlDoc.ToXmlDoc(true, paramNames) - let vspec = MakeAndPublishVal cenv env (altActualParent, true, declKind, ValNotInRecScope, valscheme, attrs, xmlDoc, literalValue, false) + let xmlDoc = xmlDoc.ToXmlDoc(true, paramNames) + let vspec = MakeAndPublishVal cenv env (altActualParent, true, declKind, ValNotInRecScope, valscheme, attrs, xmlDoc, literalValue, false) - assert(vspec.InlineInfo = inlineFlag) + assert(vspec.InlineInfo = inlineFlag) - vspec, tpenv) + vspec, tpenv) diff --git a/src/Compiler/Checking/CheckExpressions.fsi b/src/Compiler/Checking/CheckExpressions.fsi index 84a83c5bf65..5ad78d6f5c0 100644 --- a/src/Compiler/Checking/CheckExpressions.fsi +++ b/src/Compiler/Checking/CheckExpressions.fsi @@ -222,6 +222,89 @@ val LightweightTcValForUsingInBuildMethodCall: /// (i.e. are without explicit declaration). type UnscopedTyparEnv +/// A type to represent information associated with values to indicate what explicit (declared) type parameters +/// are given and what additional type parameters can be inferred, if any. +/// +/// The declared type parameters, e.g. let f<'a> (x:'a) = x, plus an indication +/// of whether additional polymorphism may be inferred, e.g. let f<'a, ..> (x:'a) y = x +type ExplicitTyparInfo = ExplicitTyparInfo of rigidCopyOfDeclaredTypars: Typars * declaredTypars: Typars * infer: bool + +val permitInferTypars: ExplicitTyparInfo + +val dontInferTypars: ExplicitTyparInfo + +type ArgAndRetAttribs = ArgAndRetAttribs of Attribs list list * Attribs + +val noArgOrRetAttribs: ArgAndRetAttribs + +/// Indicates whether constraints should be checked when checking syntactic types +type CheckConstraints = + | CheckCxs + | NoCheckCxs + +/// Represents the ValReprInfo for a value, before the typars are fully inferred +type PrelimValReprInfo = PrelimValReprInfo of curriedArgInfos: ArgReprInfo list list * returnInfo: ArgReprInfo + +/// Holds the initial ValMemberInfo and other information before it is fully completed +type PrelimMemberInfo = PrelimMemberInfo of memberInfo: ValMemberInfo * logicalName: string * compiledName: string + +/// Represents the results of the first phase of preparing simple values from a pattern +type PrelimVal1 = + | PrelimVal1 of + id: Ident * + explicitTyparInfo: ExplicitTyparInfo * + prelimType: TType * + prelimValReprInfo: PrelimValReprInfo option * + memberInfoOpt: PrelimMemberInfo option * + isMutable: bool * + inlineFlag: ValInline * + baseOrThisInfo: ValBaseOrThisInfo * + argAttribs: ArgAndRetAttribs * + visibility: SynAccess option * + isCompGen: bool + + member Type: TType + + member Ident: Ident + +/// The results of applying let-style generalization after type checking. +type PrelimVal2 = + | PrelimVal2 of + id: Ident * + prelimType: GeneralizedType * + prelimValReprInfo: PrelimValReprInfo option * + memberInfoOpt: PrelimMemberInfo option * + isMutable: bool * + inlineFlag: ValInline * + baseOrThisInfo: ValBaseOrThisInfo * + argAttribs: ArgAndRetAttribs * + visibility: SynAccess option * + isCompGen: bool * + hasDeclaredTypars: bool + +/// Translation of patterns is split into three phases. The first collects names. +/// The second is run after val_specs have been created for those names and inference +/// has been resolved. The second phase is run by applying a function returned by the +/// first phase. The input to the second phase is a List.map that gives the Val and type scheme +/// for each value bound by the pattern. +type TcPatPhase2Input = + | TcPatPhase2Input of NameMap * bool + + member WithRightPath: unit -> TcPatPhase2Input + +/// Represents the context flowed left-to-right through pattern checking +type TcPatLinearEnv = TcPatLinearEnv of tpenv: UnscopedTyparEnv * names: NameMap * takenNames: Set + +/// Represents the flags passsed to TcPat regarding the binding location +type TcPatValFlags = + | TcPatValFlags of + inlineFlag: ValInline * + explicitTyparInfo: ExplicitTyparInfo * + argAndRetAttribs: ArgAndRetAttribs * + isMutable: bool * + visibility: SynAccess option * + isCompilerGenerated: bool + /// Represents the compilation environment for typechecking a single file in an assembly. [] type TcFileState = @@ -282,6 +365,27 @@ type TcFileState = isInternalTestSpanStackReferring: bool + // forward call + TcPat: WarnOnUpperFlag + -> TcFileState + -> TcEnv + -> PrelimValReprInfo option + -> TcPatValFlags + -> TcPatLinearEnv + -> TType + -> SynPat + -> (TcPatPhase2Input -> Pattern) * TcPatLinearEnv + + // forward call + TcSimplePats: TcFileState + -> bool + -> CheckConstraints + -> TType + -> TcEnv + -> TcPatLinearEnv + -> SynSimplePats + -> string list * TcPatLinearEnv + // forward call TcSequenceExpressionEntry: TcFileState -> TcEnv @@ -321,6 +425,8 @@ type TcFileState = tcSink: TcResultsSink * tcVal: TcValF * isInternalTestSpanStackReferring: bool * + tcPat: (WarnOnUpperFlag -> TcFileState -> TcEnv -> PrelimValReprInfo option -> TcPatValFlags -> TcPatLinearEnv -> TType -> SynPat -> (TcPatPhase2Input -> Pattern) * TcPatLinearEnv) * + tcSimplePats: (TcFileState -> bool -> CheckConstraints -> TType -> TcEnv -> TcPatLinearEnv -> SynSimplePats -> string list * TcPatLinearEnv) * tcSequenceExpressionEntry: (TcFileState -> TcEnv -> OverallTy -> UnscopedTyparEnv -> bool * SynExpr -> range -> Expr * UnscopedTyparEnv) * tcArrayOrListSequenceExpression: (TcFileState -> TcEnv -> OverallTy -> UnscopedTyparEnv -> bool * SynExpr -> range -> Expr * UnscopedTyparEnv) * tcComputationExpression: (TcFileState -> TcEnv -> OverallTy -> UnscopedTyparEnv -> range * Expr * TType * SynExpr -> Expr * UnscopedTyparEnv) -> @@ -400,11 +506,6 @@ type ImplicitlyBoundTyparsAllowed = | NewTyparsOK | NoNewTypars -/// Indicates whether constraints should be checked when checking syntactic types -type CheckConstraints = - | CheckCxs - | NoCheckCxs - /// Indicates if a member binding is an object expression binding type IsObjExprBinding = | ObjExprBinding @@ -418,12 +519,6 @@ type RecDefnBindingInfo = declKind: DeclKind * synBinding: SynBinding -/// Represents the ValReprInfo for a value, before the typars are fully inferred -type PrelimValReprInfo = PrelimValReprInfo of curriedArgInfos: ArgReprInfo list list * returnInfo: ArgReprInfo - -/// Holds the initial ValMemberInfo and other information before it is fully completed -type PrelimMemberInfo = PrelimMemberInfo of memberInfo: ValMemberInfo * logicalName: string * compiledName: string - /// The result of checking a value or member signature type ValSpecResult = | ValSpecResult of @@ -439,13 +534,6 @@ type ValSpecResult = /// An empty environment of type variables with implicit scope val emptyUnscopedTyparEnv: UnscopedTyparEnv -/// A type to represent information associated with values to indicate what explicit (declared) type parameters -/// are given and what additional type parameters can be inferred, if any. -/// -/// The declared type parameters, e.g. let f<'a> (x:'a) = x, plus an indication -/// of whether additional polymorphism may be inferred, e.g. let f<'a, ..> (x:'a) y = x -type ExplicitTyparInfo = ExplicitTyparInfo of rigidCopyOfDeclaredTypars: Typars * declaredTypars: Typars * infer: bool - /// NormalizedBindingRhs records the r.h.s. of a binding after some munging just before type checking. type NormalizedBindingRhs = | NormalizedBindingRhs of @@ -492,12 +580,6 @@ type RecursiveBindingInfo = member EnclosingDeclaredTypars: Typar list member Index: int -/// Represents the results of the first phase of preparing simple values from a pattern -[] -type PrelimVal1 = - member Ident: Ident - member Type: TType - /// Represents the results of the first phase of preparing bindings [] type CheckedBindingInfo @@ -963,6 +1045,9 @@ val TcNewExpr: mWholeExprOrObjTy: range -> Expr * UnscopedTyparEnv +/// Check a 'nameof' expression +val TcNameOfExpr: cenv: TcFileState -> env: TcEnv -> tpenv: UnscopedTyparEnv -> synArg: SynExpr -> Expr + #if !NO_TYPEPROVIDERS /// Check the application of a provided type to static args val TcProvidedTypeAppToStaticConstantArgs: @@ -976,16 +1061,6 @@ val TcProvidedTypeAppToStaticConstantArgs: bool * Tainted * (unit -> unit) #endif -/// Check a set of simple patterns, e.g. the declarations of parameters for an implicit constructor. -val TcSimplePatsOfUnknownType: - cenv: TcFileState -> - optionalArgsOK: bool -> - checkConstraints: CheckConstraints -> - env: TcEnv -> - tpenv: UnscopedTyparEnv -> - synSimplePats: SynSimplePats -> - string list * (UnscopedTyparEnv * NameMap * Set) - /// Check a set of explicitly declared constraints on type parameters val TcTyparConstraints: cenv: TcFileState -> @@ -1064,6 +1139,67 @@ val TranslatePartialValReprInfo: tps: Typar list -> PrelimValReprInfo -> ValRepr /// Constrain two types to be equal within this type checking context val UnifyTypes: cenv: TcFileState -> env: TcEnv -> m: range -> actualTy: TType -> expectedTy: TType -> unit +val TcRuntimeTypeTest: + isCast: bool -> + isOperator: bool -> + cenv: TcFileState -> + denv: DisplayEnv -> + m: range -> + tgtTy: TType -> + srcTy: TType -> + unit + +/// Allow the inference of structness from the known type, e.g. +/// let (x: struct (int * int)) = (3,4) +val UnifyTupleTypeAndInferCharacteristics: + contextInfo: ContextInfo -> + cenv: TcFileState -> + denv: DisplayEnv -> + m: range -> + knownTy: TType -> + isExplicitStruct: bool -> + 'T list -> + TupInfo * TTypes + +/// Helper used to check both record expressions and record patterns +val BuildFieldMap: + cenv: TcFileState -> + env: TcEnv -> + isPartial: bool -> + ty: TType -> + ((Ident list * Ident) * 'T) list -> + m: range -> + TypeInst * TyconRef * Map * (string * 'T) list + +/// Check a long identifier 'Case' or 'Case argsR' that has been resolved to an active pattern case +val TcPatLongIdentActivePatternCase: + warnOnUpper: WarnOnUpperFlag -> + cenv: TcFileState -> + env: TcEnv -> + vFlags: TcPatValFlags -> + patEnv: TcPatLinearEnv -> + ty: TType -> + lidRange: range * item: Item * apref: ActivePatternElemRef * args: SynPat list * m: range -> + (TcPatPhase2Input -> Pattern) * TcPatLinearEnv + +/// The pattern syntax can also represent active pattern arguments. This routine +/// converts from the pattern syntax to the expression syntax. +/// +/// Note we parse arguments to parameterized pattern labels as patterns, not expressions. +/// This means the range of syntactic expression forms that can be used here is limited. +val ConvSynPatToSynExpr: synPat: SynPat -> SynExpr + +val TcVal: + checkAttributes: bool -> + cenv: TcFileState -> + env: TcEnv -> + tpenv: UnscopedTyparEnv -> + vref: ValRef -> + instantiationInfoOpt: (ValUseFlag * (UnscopedTyparEnv -> TyparKind list -> TypeInst * UnscopedTyparEnv)) option -> + optAfterResolution: AfterResolution option -> + m: range -> + Typar list * Expr * bool * TType * TType list * UnscopedTyparEnv + module GeneralizationHelpers = /// Given an environment, compute the set of inference type variables which may not be diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs new file mode 100644 index 00000000000..1314fb2fdfa --- /dev/null +++ b/src/Compiler/Checking/CheckPatterns.fs @@ -0,0 +1,757 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +/// The typechecker. Left-to-right constrained type checking +/// with generalization at appropriate points. +module internal FSharp.Compiler.CheckPatterns + +open System +open System.Collections.Generic + +open Internal.Utilities.Library +open Internal.Utilities.Library.Extras +open FSharp.Compiler.AccessibilityLogic +open FSharp.Compiler.AttributeChecking +open FSharp.Compiler.CheckExpressions +open FSharp.Compiler.ConstraintSolver +open FSharp.Compiler.DiagnosticsLogger +open FSharp.Compiler.Features +open FSharp.Compiler.Infos +open FSharp.Compiler.NameResolution +open FSharp.Compiler.PatternMatchCompilation +open FSharp.Compiler.Syntax +open FSharp.Compiler.Syntax.PrettyNaming +open FSharp.Compiler.SyntaxTreeOps +open FSharp.Compiler.TcGlobals +open FSharp.Compiler.Text.Range +open FSharp.Compiler.TypedTree +open FSharp.Compiler.TypedTreeBasics +open FSharp.Compiler.TypedTreeOps + +type cenv = TcFileState + +//------------------------------------------------------------------------- +// Helpers that should be elsewhere +//------------------------------------------------------------------------- + +let mkNilListPat (g: TcGlobals) m ty = TPat_unioncase(g.nil_ucref, [ty], [], m) + +let mkConsListPat (g: TcGlobals) ty ph pt = TPat_unioncase(g.cons_ucref, [ty], [ph;pt], unionRanges ph.Range pt.Range) + +/// Optimized unification routine that avoids creating new inference +/// variables unnecessarily +let UnifyRefTupleType contextInfo (cenv: cenv) denv m ty ps = + let g = cenv.g + let ptys = + if isRefTupleTy g ty then + let ptys = destRefTupleTy g ty + if List.length ps = List.length ptys then ptys + else NewInferenceTypes g ps + else NewInferenceTypes g ps + + let contextInfo = + match contextInfo with + | ContextInfo.RecordFields -> ContextInfo.TupleInRecordFields + | _ -> contextInfo + + AddCxTypeEqualsType contextInfo denv cenv.css m ty (TType_tuple (tupInfoRef, ptys)) + ptys + +let rec TryAdjustHiddenVarNameToCompGenName cenv env (id: Ident) altNameRefCellOpt = + match altNameRefCellOpt with + | Some ({contents = SynSimplePatAlternativeIdInfo.Undecided altId } as altNameRefCell) -> + match ResolvePatternLongIdent cenv.tcSink cenv.nameResolver AllIdsOK false id.idRange env.eAccessRights env.eNameResEnv TypeNameResolutionInfo.Default [id] with + | Item.NewDef _ -> + // The name is not in scope as a pattern identifier (e.g. union case), so do not use the alternate ID + None + | _ -> + // The name is in scope as a pattern identifier, so use the alternate ID + altNameRefCell.Value <- SynSimplePatAlternativeIdInfo.Decided altId + Some altId + | Some {contents = SynSimplePatAlternativeIdInfo.Decided altId } -> Some altId + | None -> None + +/// Bind the patterns used in a lambda. Not clear why we don't use TcPat. +and TcSimplePat optionalArgsOK checkConstraints (cenv: cenv) ty env patEnv p = + let g = cenv.g + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + + match p with + | SynSimplePat.Id (id, altNameRefCellOpt, isCompGen, isMemberThis, isOpt, m) -> + + // Check to see if pattern translation decides to use an alternative identifier. + match TryAdjustHiddenVarNameToCompGenName cenv env id altNameRefCellOpt with + | Some altId -> + TcSimplePat optionalArgsOK checkConstraints cenv ty env patEnv (SynSimplePat.Id (altId, None, isCompGen, isMemberThis, isOpt, m) ) + | None -> + if isOpt then + if not optionalArgsOK then + errorR(Error(FSComp.SR.tcOptionalArgsOnlyOnMembers(), m)) + + let tyarg = NewInferenceType g + UnifyTypes cenv env m ty (mkOptionTy g tyarg) + + let vFlags = TcPatValFlags (ValInline.Optional, permitInferTypars, noArgOrRetAttribs, false, None, isCompGen) + let _, names, takenNames = TcPatBindingName cenv env id ty isMemberThis None None vFlags (names, takenNames) + let patEnvR = TcPatLinearEnv(tpenv, names, takenNames) + id.idText, patEnvR + + | SynSimplePat.Typed (p, cty, m) -> + let ctyR, tpenv = TcTypeAndRecover cenv NewTyparsOK checkConstraints ItemOccurence.UseInType env tpenv cty + + match p with + // Optional arguments on members + | SynSimplePat.Id(_, _, _, _, true, _) -> UnifyTypes cenv env m ty (mkOptionTy g ctyR) + | _ -> UnifyTypes cenv env m ty ctyR + + let patEnvR = TcPatLinearEnv(tpenv, names, takenNames) + TcSimplePat optionalArgsOK checkConstraints cenv ty env patEnvR p + + | SynSimplePat.Attrib (p, _, _) -> + TcSimplePat optionalArgsOK checkConstraints cenv ty env patEnv p + +// raise an error if any optional args precede any non-optional args +and ValidateOptArgOrder (synSimplePats: SynSimplePats) = + + let rec getPats synSimplePats = + match synSimplePats with + | SynSimplePats.SimplePats(p, m) -> p, m + | SynSimplePats.Typed(p, _, _) -> getPats p + + let rec isOptArg pat = + match pat with + | SynSimplePat.Id (_, _, _, _, isOpt, _) -> isOpt + | SynSimplePat.Typed (p, _, _) -> isOptArg p + | SynSimplePat.Attrib (p, _, _) -> isOptArg p + + let pats, m = getPats synSimplePats + + let mutable hitOptArg = false + + List.iter (fun pat -> if isOptArg pat then hitOptArg <- true elif hitOptArg then error(Error(FSComp.SR.tcOptionalArgsMustComeAfterNonOptionalArgs(), m))) pats + + +/// Bind the patterns used in argument position for a function, method or lambda. +and TcSimplePats (cenv: cenv) optionalArgsOK checkConstraints ty env patEnv synSimplePats = + + let g = cenv.g + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + + // validate optional argument declaration + ValidateOptArgOrder synSimplePats + + match synSimplePats with + | SynSimplePats.SimplePats ([], m) -> + // Unit "()" patterns in argument position become SynSimplePats.SimplePats([], _) in the + // syntactic translation when building bindings. This is done because the + // use of "()" has special significance for arity analysis and argument counting. + // + // Here we give a name to the single argument implied by those patterns. + // This is a little awkward since it would be nice if this was + // uniform with the process where we give names to other (more complex) + // patterns used in argument position, e.g. "let f (D(x)) = ..." + let id = ident("unitVar" + string takenNames.Count, m) + UnifyTypes cenv env m ty g.unit_ty + let vFlags = TcPatValFlags (ValInline.Optional, permitInferTypars, noArgOrRetAttribs, false, None, true) + let _, namesR, takenNamesR = TcPatBindingName cenv env id ty false None None vFlags (names, takenNames) + let patEnvR = TcPatLinearEnv(tpenv, namesR, takenNamesR) + [id.idText], patEnvR + + | SynSimplePats.SimplePats ([synSimplePat], _) -> + let v, patEnv = TcSimplePat optionalArgsOK checkConstraints cenv ty env patEnv synSimplePat + [v], patEnv + + | SynSimplePats.SimplePats (ps, m) -> + let ptys = UnifyRefTupleType env.eContextInfo cenv env.DisplayEnv m ty ps + let ps', patEnvR = (patEnv, List.zip ptys ps) ||> List.mapFold (fun patEnv (ty, pat) -> TcSimplePat optionalArgsOK checkConstraints cenv ty env patEnv pat) + ps', patEnvR + + | SynSimplePats.Typed (p, cty, m) -> + let ctyR, tpenv = TcTypeAndRecover cenv NewTyparsOK CheckCxs ItemOccurence.UseInType env tpenv cty + + match p with + // Solitary optional arguments on members + | SynSimplePats.SimplePats([SynSimplePat.Id(_, _, _, _, true, _)], _) -> UnifyTypes cenv env m ty (mkOptionTy g ctyR) + | _ -> UnifyTypes cenv env m ty ctyR + + let patEnvR = TcPatLinearEnv(tpenv, names, takenNames) + + TcSimplePats cenv optionalArgsOK checkConstraints ty env patEnvR p + +and TcSimplePatsOfUnknownType (cenv: cenv) optionalArgsOK checkConstraints env tpenv synSimplePats = + let g = cenv.g + let argTy = NewInferenceType g + let patEnv = TcPatLinearEnv (tpenv, NameMap.empty, Set.empty) + TcSimplePats cenv optionalArgsOK checkConstraints argTy env patEnv synSimplePats + +and TcPatBindingName cenv env id ty isMemberThis vis1 valReprInfo (vFlags: TcPatValFlags) (names, takenNames: Set) = + let (TcPatValFlags(inlineFlag, declaredTypars, argAttribs, isMutable, vis2, isCompGen)) = vFlags + let vis = if Option.isSome vis1 then vis1 else vis2 + + if takenNames.Contains id.idText then errorR (VarBoundTwice id) + + let isCompGen = isCompGen || IsCompilerGeneratedName id.idText + let baseOrThis = if isMemberThis then MemberThisVal else NormalVal + let prelimVal = PrelimVal1(id, declaredTypars, ty, valReprInfo, None, isMutable, inlineFlag, baseOrThis, argAttribs, vis, isCompGen) + let names = Map.add id.idText prelimVal names + let takenNames = Set.add id.idText takenNames + + let phase2 (TcPatPhase2Input (values, isLeftMost)) = + let vspec, typeScheme = + let name = id.idText + match values.TryGetValue name with + | true, value -> + if not (String.IsNullOrEmpty name) && not (String.isLeadingIdentifierCharacterUpperCase name) then + match env.eNameResEnv.ePatItems.TryGetValue name with + | true, Item.Value vref when vref.LiteralValue.IsSome -> + warning(Error(FSComp.SR.checkLowercaseLiteralBindingInPattern name, id.idRange)) + | _ -> () + value + | _ -> error(Error(FSComp.SR.tcNameNotBoundInPattern name, id.idRange)) + + // isLeftMost indicates we are processing the left-most path through a disjunctive or pattern. + // For those binding locations, CallNameResolutionSink is called in MakeAndPublishValue, like all other bindings + // For non-left-most paths, we register the name resolutions here + if not isLeftMost && not vspec.IsCompilerGenerated && not (vspec.LogicalName.StartsWithOrdinal("_")) then + let item = Item.Value(mkLocalValRef vspec) + CallNameResolutionSink cenv.tcSink (id.idRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Binding, env.AccessRights) + + PatternValBinding(vspec, typeScheme) + + phase2, names, takenNames + +and TcPatAndRecover warnOnUpper cenv (env: TcEnv) valReprInfo (vFlags: TcPatValFlags) patEnv ty (synPat: SynPat) = + try + TcPat warnOnUpper cenv env valReprInfo vFlags patEnv ty synPat + with e -> + // Error recovery - return some rubbish expression, but replace/annotate + // the type of the current expression with a type variable that indicates an error + let m = synPat.Range + errorRecovery e m + //SolveTypeAsError cenv env.DisplayEnv m ty + (fun _ -> TPat_error m), patEnv + +/// Typecheck a pattern. Patterns are type-checked in three phases: +/// 1. TcPat builds a List.map from simple variable names to inferred types for +/// those variables. It also returns a function to perform the second phase. +/// 2. The second phase assumes the caller has built the actual value_spec's +/// for the values being defined, and has decided if the types of these +/// variables are to be generalized. The caller hands this information to +/// the second-phase function in terms of a List.map from names to actual +/// value specifications. +and TcPat warnOnUpper (cenv: cenv) env valReprInfo vFlags (patEnv: TcPatLinearEnv) ty synPat = + let g = cenv.g + let ad = env.AccessRights + + match synPat with + | SynPat.As (_, SynPat.Named _, _) -> () + | SynPat.As (_, _, m) -> checkLanguageFeatureError g.langVersion LanguageFeature.NonVariablePatternsToRightOfAsPatterns m + | _ -> () + + match synPat with + | SynPat.Const (synConst, m) -> + TcConstPat warnOnUpper cenv env vFlags patEnv ty synConst m + + | SynPat.Wild m -> + (fun _ -> TPat_wild m), patEnv + + | SynPat.IsInst (synTargetTy, m) + | SynPat.As (SynPat.IsInst(synTargetTy, m), _, _) -> + TcPatIsInstance warnOnUpper cenv env valReprInfo vFlags patEnv ty synPat synTargetTy m + + | SynPat.As (synInnerPat, SynPat.Named (SynIdent(id,_), isMemberThis, vis, m), _) + | SynPat.As (SynPat.Named (SynIdent(id,_), isMemberThis, vis, m), synInnerPat, _) -> + TcPatNamedAs warnOnUpper cenv env valReprInfo vFlags patEnv ty synInnerPat id isMemberThis vis m + + | SynPat.As (pat1, pat2, m) -> + TcPatUnnamedAs warnOnUpper cenv env vFlags patEnv ty pat1 pat2 m + + | SynPat.Named (SynIdent(id,_), isMemberThis, vis, m) -> + TcPatNamed warnOnUpper cenv env vFlags patEnv id ty isMemberThis vis valReprInfo m + + | SynPat.OptionalVal (id, m) -> + errorR (Error (FSComp.SR.tcOptionalArgsOnlyOnMembers (), m)) + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + let bindf, namesR, takenNamesR = TcPatBindingName cenv env id ty false None valReprInfo vFlags (names, takenNames) + let patEnvR = TcPatLinearEnv(tpenv, namesR, takenNamesR) + (fun values -> TPat_as (TPat_wild m, bindf values, m)), patEnvR + + | SynPat.Typed (p, cty, m) -> + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + let ctyR, tpenvR = TcTypeAndRecover cenv NewTyparsOK CheckCxs ItemOccurence.UseInType env tpenv cty + UnifyTypes cenv env m ty ctyR + let patEnvR = TcPatLinearEnv(tpenvR, names, takenNames) + TcPat warnOnUpper cenv env valReprInfo vFlags patEnvR ty p + + | SynPat.Attrib (innerPat, attrs, _) -> + TcPatAttributed warnOnUpper cenv env vFlags patEnv ty innerPat attrs + + | SynPat.Or (pat1, pat2, m, _) -> + TcPatOr warnOnUpper cenv env vFlags patEnv ty pat1 pat2 m + + | SynPat.Ands (pats, m) -> + TcPatAnds warnOnUpper cenv env vFlags patEnv ty pats m + + | SynPat.LongIdent (longDotId=longDotId; typarDecls=tyargs; argPats=args; accessibility=vis; range=m) -> + TcPatLongIdent warnOnUpper cenv env ad valReprInfo vFlags patEnv ty (longDotId, tyargs, args, vis, m) + + | SynPat.QuoteExpr(_, m) -> + errorR (Error(FSComp.SR.tcInvalidPattern(), m)) + (fun _ -> TPat_error m), patEnv + + | SynPat.Tuple (isExplicitStruct, args, m) -> + TcPatTuple warnOnUpper cenv env vFlags patEnv ty isExplicitStruct args m + + | SynPat.Paren (p, _) -> + TcPat warnOnUpper cenv env None vFlags patEnv ty p + + | SynPat.ArrayOrList (isArray, args, m) -> + TcPatArrayOrList warnOnUpper cenv env vFlags patEnv ty isArray args m + + | SynPat.Record (flds, m) -> + TcRecordPat warnOnUpper cenv env vFlags patEnv ty flds m + + | SynPat.DeprecatedCharRange (c1, c2, m) -> + errorR(Deprecated(FSComp.SR.tcUseWhenPatternGuard(), m)) + UnifyTypes cenv env m ty g.char_ty + (fun _ -> TPat_range(c1, c2, m)), patEnv + + | SynPat.Null m -> + TcNullPat cenv env patEnv ty m + + | SynPat.InstanceMember (range=m) -> + errorR(Error(FSComp.SR.tcIllegalPattern(), synPat.Range)) + (fun _ -> TPat_wild m), patEnv + + | SynPat.FromParseError (pat, _) -> + suppressErrorReporting (fun () -> TcPatAndRecover warnOnUpper cenv env valReprInfo vFlags patEnv (NewErrorType()) pat) + +and TcConstPat warnOnUpper cenv env vFlags patEnv ty synConst m = + let g = cenv.g + match synConst with + | SynConst.Bytes (bytes, _, m) -> + UnifyTypes cenv env m ty (mkByteArrayTy g) + let synReplacementExpr = SynPat.ArrayOrList (true, [ for b in bytes -> SynPat.Const(SynConst.Byte b, m) ], m) + TcPat warnOnUpper cenv env None vFlags patEnv ty synReplacementExpr + + | SynConst.UserNum _ -> + errorR (Error (FSComp.SR.tcInvalidNonPrimitiveLiteralInPatternMatch (), m)) + (fun _ -> TPat_error m), patEnv + + | _ -> + try + let c = TcConst cenv ty m env synConst + (fun _ -> TPat_const (c, m)), patEnv + with e -> + errorRecovery e m + (fun _ -> TPat_error m), patEnv + +and TcPatNamedAs warnOnUpper cenv env valReprInfo vFlags patEnv ty synInnerPat id isMemberThis vis m = + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + let bindf, namesR, takenNamesR = TcPatBindingName cenv env id ty isMemberThis vis valReprInfo vFlags (names, takenNames) + let patEnvR = TcPatLinearEnv(tpenv, namesR, takenNamesR) + let innerPat, acc = TcPat warnOnUpper cenv env None vFlags patEnvR ty synInnerPat + let phase2 values = TPat_as (innerPat values, bindf values, m) + phase2, acc + +and TcPatUnnamedAs warnOnUpper cenv env vFlags patEnv ty pat1 pat2 m = + let pats = [pat1; pat2] + let patsR, patEnvR = TcPatterns warnOnUpper cenv env vFlags patEnv (List.map (fun _ -> ty) pats) pats + let phase2 values = TPat_conjs(List.map (fun f -> f values) patsR, m) + phase2, patEnvR + +and TcPatNamed warnOnUpper cenv env vFlags patEnv id ty isMemberThis vis valReprInfo m = + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + let bindf, namesR, takenNamesR = TcPatBindingName cenv env id ty isMemberThis vis valReprInfo vFlags (names, takenNames) + let patEnvR = TcPatLinearEnv(tpenv, namesR, takenNamesR) + let pat', acc = TcPat warnOnUpper cenv env None vFlags patEnvR ty (SynPat.Wild m) + let phase2 values = TPat_as (pat' values, bindf values, m) + phase2, acc + +and TcPatIsInstance warnOnUpper cenv env valReprInfo vFlags patEnv srcTy synPat synTargetTy m = + let (TcPatLinearEnv(tpenv, names, takenNames)) = patEnv + let tgtTy, tpenv = TcTypeAndRecover cenv NewTyparsOKButWarnIfNotRigid CheckCxs ItemOccurence.UseInType env tpenv synTargetTy + TcRuntimeTypeTest false true cenv env.DisplayEnv m tgtTy srcTy + let patEnv = TcPatLinearEnv(tpenv, names, takenNames) + match synPat with + | SynPat.IsInst(_, m) -> + (fun _ -> TPat_isinst (srcTy, tgtTy, None, m)), patEnv + | SynPat.As (SynPat.IsInst _, p, m) -> + let pat, acc = TcPat warnOnUpper cenv env valReprInfo vFlags patEnv tgtTy p + (fun values -> TPat_isinst (srcTy, tgtTy, Some (pat values), m)), acc + | _ -> failwith "TcPat" + +and TcPatAttributed warnOnUpper cenv env vFlags patEnv ty innerPat attrs = + errorR (Error (FSComp.SR.tcAttributesInvalidInPatterns (), rangeOfNonNilAttrs attrs)) + for attrList in attrs do + TcAttributes cenv env Unchecked.defaultof<_> attrList.Attributes |> ignore + TcPat warnOnUpper cenv env None vFlags patEnv ty innerPat + +and TcPatOr warnOnUpper cenv env vFlags patEnv ty pat1 pat2 m = + let (TcPatLinearEnv(_, names, takenNames)) = patEnv + let pat1R, patEnv1 = TcPat warnOnUpper cenv env None vFlags patEnv ty pat1 + let (TcPatLinearEnv(tpenv, names1, takenNames1)) = patEnv1 + let pat2R, patEnv2 = TcPat warnOnUpper cenv env None vFlags (TcPatLinearEnv(tpenv, names, takenNames)) ty pat2 + let (TcPatLinearEnv(tpenv, names2, takenNames2)) = patEnv2 + + if not (takenNames1 = takenNames2) then + errorR (UnionPatternsBindDifferentNames m) + + names1 |> Map.iter (fun _ (PrelimVal1 (id=id1; prelimType=ty1)) -> + match names2.TryGetValue id1.idText with + | true, PrelimVal1 (id=id2; prelimType=ty2) -> + try UnifyTypes cenv env id2.idRange ty1 ty2 + with exn -> errorRecovery exn m + | _ -> ()) + + let namesR = NameMap.layer names1 names2 + let takenNamesR = Set.union takenNames1 takenNames2 + let patEnvR = TcPatLinearEnv(tpenv, namesR, takenNamesR) + let phase2 values = TPat_disjs ([pat1R values; pat2R (values.WithRightPath())], m) + phase2, patEnvR + +and TcPatAnds warnOnUpper cenv env vFlags patEnv ty pats m = + let patsR, acc = TcPatterns warnOnUpper cenv env vFlags patEnv (List.map (fun _ -> ty) pats) pats + let phase2 values = TPat_conjs(List.map (fun f -> f values) patsR, m) + phase2, acc + +and TcPatTuple warnOnUpper cenv env vFlags patEnv ty isExplicitStruct args m = + let g = cenv.g + try + let tupInfo, argTys = UnifyTupleTypeAndInferCharacteristics env.eContextInfo cenv env.DisplayEnv m ty isExplicitStruct args + let argsR, acc = TcPatterns warnOnUpper cenv env vFlags patEnv argTys args + let phase2 values = TPat_tuple(tupInfo, List.map (fun f -> f values) argsR, argTys, m) + phase2, acc + with e -> + errorRecovery e m + let _, acc = TcPatterns warnOnUpper cenv env vFlags patEnv (NewInferenceTypes g args) args + let phase2 _ = TPat_error m + phase2, acc + +and TcPatArrayOrList warnOnUpper cenv env vFlags patEnv ty isArray args m = + let g = cenv.g + let argTy = NewInferenceType g + UnifyTypes cenv env m ty (if isArray then mkArrayType g argTy else mkListTy g argTy) + let argsR, acc = TcPatterns warnOnUpper cenv env vFlags patEnv (List.map (fun _ -> argTy) args) args + let phase2 values = + let argsR = List.map (fun f -> f values) argsR + if isArray then TPat_array(argsR, argTy, m) + else List.foldBack (mkConsListPat g argTy) argsR (mkNilListPat g m argTy) + phase2, acc + +and TcRecordPat warnOnUpper cenv env vFlags patEnv ty fieldPats m = + let fieldPats = fieldPats |> List.map (fun (fieldId, _, fieldPat) -> fieldId, fieldPat) + let tinst, tcref, fldsmap, _fldsList = BuildFieldMap cenv env true ty fieldPats m + let gtyp = mkAppTy tcref tinst + let inst = List.zip (tcref.Typars m) tinst + + UnifyTypes cenv env m ty gtyp + + let fields = tcref.TrueInstanceFieldsAsList + let ftys = fields |> List.map (fun fsp -> actualTyOfRecdField inst fsp, fsp) + + let fieldPats, patEnvR = + (patEnv, ftys) ||> List.mapFold (fun s (ty, fsp) -> + match fldsmap.TryGetValue fsp.rfield_id.idText with + | true, v -> TcPat warnOnUpper cenv env None vFlags s ty v + | _ -> (fun _ -> TPat_wild m), s) + + let phase2 values = + TPat_recd (tcref, tinst, List.map (fun f -> f values) fieldPats, m) + + phase2, patEnvR + +and TcNullPat cenv env patEnv ty m = + try + AddCxTypeUseSupportsNull env.DisplayEnv cenv.css m NoTrace ty + with exn -> + errorRecovery exn m + (fun _ -> TPat_null m), patEnv + +and CheckNoArgsForLiteral args m = + match args with + | SynArgPats.Pats [] + | SynArgPats.NamePatPairs ([], _) -> () + | _ -> errorR (Error (FSComp.SR.tcLiteralDoesNotTakeArguments (), m)) + +and GetSynArgPatterns args = + match args with + | SynArgPats.Pats args -> args + | SynArgPats.NamePatPairs (pairs, _) -> List.map (fun (_, _, pat) -> pat) pairs + +and TcArgPats warnOnUpper (cenv: cenv) env vFlags patEnv args = + let g = cenv.g + let args = GetSynArgPatterns args + TcPatterns warnOnUpper cenv env vFlags patEnv (NewInferenceTypes g args) args + +and IsNameOf (cenv: cenv) (env: TcEnv) ad m (id: Ident) = + let g = cenv.g + id.idText = "nameof" && + try + match ResolveExprLongIdent cenv.tcSink cenv.nameResolver m ad env.NameEnv TypeNameResolutionInfo.Default [id] with + | Result (_, Item.Value vref, _) -> valRefEq g vref g.nameof_vref + | _ -> false + with _ -> false + +/// Check a long identifier in a pattern +and TcPatLongIdent warnOnUpper cenv env ad valReprInfo vFlags (patEnv: TcPatLinearEnv) ty (longDotId, tyargs, args, vis, m) = + let (SynLongIdent(longId, _, _)) = longDotId + + if tyargs.IsSome then errorR(Error(FSComp.SR.tcInvalidTypeArgumentUsage(), m)) + + let warnOnUpperForId = + match args with + | SynArgPats.Pats [] -> warnOnUpper + | _ -> AllIdsOK + + let lidRange = rangeOfLid longId + + match ResolvePatternLongIdent cenv.tcSink cenv.nameResolver warnOnUpperForId false m ad env.NameEnv TypeNameResolutionInfo.Default longId with + | Item.NewDef id -> + TcPatLongIdentNewDef warnOnUpperForId warnOnUpper cenv env ad valReprInfo vFlags patEnv ty (vis, id, args, m) + + | Item.ActivePatternCase apref as item -> + + let (APElemRef (apinfo, _vref, idx, _isStructRetTy)) = apref + + match args with + | SynArgPats.Pats _ -> () + | _ -> errorR (Error (FSComp.SR.tcNamedActivePattern apinfo.ActiveTags[idx], m)) + + let args = GetSynArgPatterns args + + TcPatLongIdentActivePatternCase warnOnUpper cenv env vFlags patEnv ty (lidRange, item, apref, args, m) + + | Item.UnionCase _ | Item.ExnCase _ as item -> + TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (lidRange, item, args, m) + + | Item.ILField finfo -> + TcPatLongIdentILField warnOnUpper cenv env vFlags patEnv ty (lidRange, finfo, args, m) + + | Item.RecdField rfinfo -> + TcPatLongIdentRecdField warnOnUpper cenv env vFlags patEnv ty (lidRange, rfinfo, args, m) + + | Item.Value vref -> + TcPatLongIdentLiteral warnOnUpper cenv env vFlags patEnv ty (lidRange, vref, args, m) + + | _ -> error (Error(FSComp.SR.tcRequireVarConstRecogOrLiteral(), m)) + +/// Check a long identifier in a pattern that has been not been resolved to anything else and represents a new value, or nameof +and TcPatLongIdentNewDef warnOnUpperForId warnOnUpper (cenv: cenv) env ad valReprInfo vFlags patEnv ty (vis, id, args, m) = + let g = cenv.g + let (TcPatLinearEnv(tpenv, _, _)) = patEnv + + match GetSynArgPatterns args with + | [] -> + TcPat warnOnUpperForId cenv env valReprInfo vFlags patEnv ty (mkSynPatVar vis id) + + | [arg] + when g.langVersion.SupportsFeature LanguageFeature.NameOf && IsNameOf cenv env ad m id -> + match TcNameOfExpr cenv env tpenv (ConvSynPatToSynExpr arg) with + | Expr.Const(c, m, _) -> (fun _ -> TPat_const (c, m)), patEnv + | _ -> failwith "Impossible: TcNameOfExpr must return an Expr.Const" + + | _ -> + let _, acc = TcArgPats warnOnUpper cenv env vFlags patEnv args + errorR (UndefinedName (0, FSComp.SR.undefinedNamePatternDiscriminator, id, NoSuggestions)) + (fun _ -> TPat_error m), acc + +and ApplyUnionCaseOrExn m (cenv: cenv) env overallTy item = + let g = cenv.g + let ad = env.eAccessRights + match item with + | Item.ExnCase ecref -> + CheckEntityAttributes g ecref m |> CommitOperationResult + UnifyTypes cenv env m overallTy g.exn_ty + CheckTyconAccessible cenv.amap m ad ecref |> ignore + let mkf mArgs args = TPat_exnconstr(ecref, args, unionRanges m mArgs) + mkf, recdFieldTysOfExnDefRef ecref, [ for f in (recdFieldsOfExnDefRef ecref) -> f.Id ] + + | Item.UnionCase(ucinfo, showDeprecated) -> + if showDeprecated then + warning(Deprecated(FSComp.SR.nrUnionTypeNeedsQualifiedAccess(ucinfo.DisplayName, ucinfo.Tycon.DisplayName) |> snd, m)) + + let ucref = ucinfo.UnionCaseRef + CheckUnionCaseAttributes g ucref m |> CommitOperationResult + CheckUnionCaseAccessible cenv.amap m ad ucref |> ignore + let gtyp2 = actualResultTyOfUnionCase ucinfo.TypeInst ucref + let inst = mkTyparInst ucref.TyconRef.TyparsNoRange ucinfo.TypeInst + UnifyTypes cenv env m overallTy gtyp2 + let mkf mArgs args = TPat_unioncase(ucref, ucinfo.TypeInst, args, unionRanges m mArgs) + mkf, actualTysOfUnionCaseFields inst ucref, [ for f in ucref.AllFieldsAsList -> f.Id ] + + | _ -> + invalidArg "item" "not a union case or exception reference" + +/// Check a long identifier 'Case' or 'Case argsR that has been resolved to a union case or F# exception constructor +and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (lidRange, item, args, m) = + let g = cenv.g + + // Report information about the case occurrence to IDE + CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.eAccessRights) + + let mkf, argTys, argNames = ApplyUnionCaseOrExn m cenv env ty item + let numArgTys = argTys.Length + + let args, extraPatternsFromNames = + match args with + | SynArgPats.Pats args -> args, [] + | SynArgPats.NamePatPairs (pairs, m) -> + // rewrite patterns from the form (name-N = pat-N; ...) to (..._, pat-N, _...) + // so type T = Case of name: int * value: int + // | Case(value = v) + // will become + // | Case(_, v) + let result = Array.zeroCreate numArgTys + let extraPatterns = List () + + for id, _, pat in pairs do + match argNames |> List.tryFindIndex (fun id2 -> id.idText = id2.idText) with + | None -> + extraPatterns.Add pat + match item with + | Item.UnionCase(uci, _) -> + errorR (Error (FSComp.SR.tcUnionCaseConstructorDoesNotHaveFieldWithGivenName (uci.DisplayName, id.idText), id.idRange)) + | Item.ExnCase tcref -> + errorR (Error (FSComp.SR.tcExceptionConstructorDoesNotHaveFieldWithGivenName (tcref.DisplayName, id.idText), id.idRange)) + | _ -> + errorR (Error (FSComp.SR.tcConstructorDoesNotHaveFieldWithGivenName id.idText, id.idRange)) + + | Some idx -> + let argItem = + match item with + | Item.UnionCase (uci, _) -> Item.UnionCaseField (uci, idx) + | Item.ExnCase tref -> Item.RecdField (RecdFieldInfo ([], RecdFieldRef (tref, id.idText))) + | _ -> failwithf "Expecting union case or exception item, got: %O" item + + CallNameResolutionSink cenv.tcSink (id.idRange, env.NameEnv, argItem, emptyTyparInst, ItemOccurence.Pattern, ad) + + match box result[idx] with + | Null -> result[idx] <- pat + | NonNull _ -> + extraPatterns.Add pat + errorR (Error (FSComp.SR.tcUnionCaseFieldCannotBeUsedMoreThanOnce id.idText, id.idRange)) + + for i = 0 to numArgTys - 1 do + if isNull (box result[i]) then + result[i] <- SynPat.Wild (m.MakeSynthetic()) + + let extraPatterns = List.ofSeq extraPatterns + + let args = List.ofArray result + if result.Length = 1 then args, extraPatterns + else [ SynPat.Tuple(false, args, m) ], extraPatterns + + let args, extraPatterns = + match args with + | [] -> [], [] + + // note: the next will always be parenthesized + | [SynPatErrorSkip(SynPat.Tuple (false, args, _)) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Tuple (false, args, _)), _))] when numArgTys > 1 -> args, [] + + // note: we allow both 'C _' and 'C (_)' regardless of number of argument of the pattern + | [SynPatErrorSkip(SynPat.Wild _ as e) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Wild _ as e), _))] -> List.replicate numArgTys e, [] + + + | args when numArgTys = 0 -> + errorR (Error (FSComp.SR.tcUnionCaseDoesNotTakeArguments (), m)) + [], args + + | arg :: rest when numArgTys = 1 -> + if numArgTys = 1 && not (List.isEmpty rest) then + errorR (Error (FSComp.SR.tcUnionCaseRequiresOneArgument (), m)) + [arg], rest + + | [arg] -> [arg], [] + + | args -> + [], args + + let args, extraPatterns = + let numArgs = args.Length + if numArgs = numArgTys then + args, extraPatterns + elif numArgs < numArgTys then + if numArgTys > 1 then + // Expects tuple without enough args + errorR (Error (FSComp.SR.tcUnionCaseExpectsTupledArguments numArgTys, m)) + else + errorR (UnionCaseWrongArguments (env.DisplayEnv, numArgTys, numArgs, m)) + args @ (List.init (numArgTys - numArgs) (fun _ -> SynPat.Wild (m.MakeSynthetic()))), extraPatterns + else + let args, remaining = args |> List.splitAt numArgTys + for remainingArg in remaining do + errorR (UnionCaseWrongArguments (env.DisplayEnv, numArgTys, numArgs, remainingArg.Range)) + args, extraPatterns @ remaining + + let extraPatterns = extraPatterns @ extraPatternsFromNames + let argsR, acc = TcPatterns warnOnUpper cenv env vFlags patEnv argTys args + let _, acc = TcPatterns warnOnUpper cenv env vFlags acc (NewInferenceTypes g extraPatterns) extraPatterns + (fun values -> mkf m (List.map (fun f -> f values) argsR)), acc + +/// Check a long identifier that has been resolved to an IL field - valid if a literal +and TcPatLongIdentILField warnOnUpper (cenv: cenv) env vFlags patEnv ty (lidRange, finfo, args, m) = + let g = cenv.g + + CheckILFieldInfoAccessible g cenv.amap lidRange env.AccessRights finfo + + if not finfo.IsStatic then + errorR (Error (FSComp.SR.tcFieldIsNotStatic finfo.FieldName, lidRange)) + + CheckILFieldAttributes g finfo m + + match finfo.LiteralValue with + | None -> + error (Error (FSComp.SR.tcFieldNotLiteralCannotBeUsedInPattern (), lidRange)) + | Some lit -> + CheckNoArgsForLiteral args m + let _, acc = TcArgPats warnOnUpper cenv env vFlags patEnv args + + UnifyTypes cenv env m ty (finfo.FieldType (cenv.amap, m)) + let c' = TcFieldInit lidRange lit + let item = Item.ILField finfo + CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.AccessRights) + (fun _ -> TPat_const (c', m)), acc + +/// Check a long identifier that has been resolved to a record field +and TcPatLongIdentRecdField warnOnUpper cenv env vFlags patEnv ty (lidRange, rfinfo, args, m) = + let g = cenv.g + CheckRecdFieldInfoAccessible cenv.amap lidRange env.AccessRights rfinfo + if not rfinfo.IsStatic then errorR (Error (FSComp.SR.tcFieldIsNotStatic(rfinfo.DisplayName), lidRange)) + CheckRecdFieldInfoAttributes g rfinfo lidRange |> CommitOperationResult + + match rfinfo.LiteralValue with + | None -> error (Error(FSComp.SR.tcFieldNotLiteralCannotBeUsedInPattern(), lidRange)) + | Some lit -> + CheckNoArgsForLiteral args m + let _, acc = TcArgPats warnOnUpper cenv env vFlags patEnv args + + UnifyTypes cenv env m ty rfinfo.FieldType + let item = Item.RecdField rfinfo + // FUTURE: can we do better than emptyTyparInst here, in order to display instantiations + // of type variables in the quick info provided in the IDE. + CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.AccessRights) + (fun _ -> TPat_const (lit, m)), acc + +/// Check a long identifier that has been resolved to an F# value that is a literal +and TcPatLongIdentLiteral warnOnUpper (cenv: cenv) env vFlags patEnv ty (lidRange, vref, args, m) = + let g = cenv.g + let (TcPatLinearEnv(tpenv, _, _)) = patEnv + + match vref.LiteralValue with + | None -> error (Error(FSComp.SR.tcNonLiteralCannotBeUsedInPattern(), m)) + | Some lit -> + let _, _, _, vexpty, _, _ = TcVal true cenv env tpenv vref None None lidRange + CheckValAccessible lidRange env.AccessRights vref + CheckFSharpAttributes g vref.Attribs lidRange |> CommitOperationResult + CheckNoArgsForLiteral args m + let _, acc = TcArgPats warnOnUpper cenv env vFlags patEnv args + + UnifyTypes cenv env m ty vexpty + let item = Item.Value vref + CallNameResolutionSink cenv.tcSink (lidRange, env.NameEnv, item, emptyTyparInst, ItemOccurence.Pattern, env.AccessRights) + (fun _ -> TPat_const (lit, m)), acc + +and TcPatterns warnOnUpper cenv env vFlags s argTys args = + assert (List.length args = List.length argTys) + List.mapFold (fun s (ty, pat) -> TcPat warnOnUpper cenv env None vFlags s ty pat) s (List.zip argTys args) + diff --git a/src/Compiler/Checking/CheckPatterns.fsi b/src/Compiler/Checking/CheckPatterns.fsi new file mode 100644 index 00000000000..866012bd0a9 --- /dev/null +++ b/src/Compiler/Checking/CheckPatterns.fsi @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +module internal FSharp.Compiler.CheckPatterns + +open FSharp.Compiler.CheckExpressions +open FSharp.Compiler.NameResolution +open FSharp.Compiler.TypedTree +open FSharp.Compiler.PatternMatchCompilation +open FSharp.Compiler.Syntax + +/// Check a set of simple patterns, e.g. the declarations of parameters for an implicit constructor. +val TcSimplePatsOfUnknownType: + cenv: TcFileState -> + optionalArgsOK: bool -> + checkConstraints: CheckConstraints -> + env: TcEnv -> + tpenv: UnscopedTyparEnv -> + synSimplePats: SynSimplePats -> + string list * TcPatLinearEnv + +// Check a pattern, e.g. for a binding or a match clause +val TcPat: + warnOnUpper: WarnOnUpperFlag -> + cenv: TcFileState -> + env: TcEnv -> + valReprInfo: PrelimValReprInfo option -> + vFlags: TcPatValFlags -> + patEnv: TcPatLinearEnv -> + ty: TType -> + synPat: SynPat -> + (TcPatPhase2Input -> Pattern) * TcPatLinearEnv + +// Check a list of simple patterns, e.g. for the arguments of a function or a class constructor +val TcSimplePats: + cenv: TcFileState -> + optionalArgsOK: bool -> + checkConstraints: CheckConstraints -> + ty: TType -> + env: TcEnv -> + patEnv: TcPatLinearEnv -> + synSimplePats: SynSimplePats -> + string list * TcPatLinearEnv diff --git a/src/Compiler/FSharp.Compiler.Service.fsproj b/src/Compiler/FSharp.Compiler.Service.fsproj index 690cbc3c837..4e80e0956dc 100644 --- a/src/Compiler/FSharp.Compiler.Service.fsproj +++ b/src/Compiler/FSharp.Compiler.Service.fsproj @@ -101,196 +101,70 @@ FSStrings.resx FSStrings.resources - - Utilities\sformat.fsi - - - Utilities\sformat.fs - - - Utilities\sr.fsi - - - Utilities\sr.fs - - - Utilities\ResizeArray.fsi - - - Utilities\ResizeArray.fs - - - Utilities\HashMultiMap.fsi - - - Utilities\HashMultiMap.fs - - - Utilities\EditDistance.fsi - - - Utilities\EditDistance.fs - - - Utilities\TaggedCollections.fsi - - - Utilities\TaggedCollections.fs - - - Utilities\illib.fsi - - - Utilities\illib.fs - - - Utilities\FileSystem.fsi - - - Utilities\FileSystem.fs - - - Utilities\ildiag.fsi - - - Utilities\ildiag.fs - - - Utilities\zmap.fsi - - - Utilities\zmap.fs - - - Utilities\zset.fsi - - - Utilities\zset.fs - - - Utilities\XmlAdapters.fsi - - - Utilities\XmlAdapters.fs - - - Utilities\InternalCollections.fsi - - - Utilities\InternalCollections.fs - - - Utilities\QueueList.fsi - - - Utilities\QueueList.fs - - - Utilities\lib.fsi - - - Utilities\lib.fs - - - Utilities\ImmutableArray.fsi - - - Utilities\ImmutableArray.fs - - - Utilities\rational.fsi - - - Utilities\rational.fs - - - Utilities\PathMap.fsi - - - Utilities\PathMap.fs - - - Utilities\RidHelpers.fs - - - Utilities\range.fsi - - - Utilities\range.fs - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - Facilities\Logger.fsi - - - Facilities\Logger.fs - - - Facilities\LanguageFeatures.fsi - - - Facilities\LanguageFeatures.fs - - - Facilities\DiagnosticOptions.fsi - - - Facilities\DiagnosticOptions.fs - - - Facilities\TextLayoutRender.fsi - - - Facilities\TextLayoutRender.fs - - - Facilities\DiagnosticsLogger.fsi - - - Facilities\DiagnosticsLogger.fs - - - Facilities\DiagnosticResolutionHints.fsi - - - Facilities\DiagnosticResolutionHints.fs - - - Facilities\prim-lexing.fsi - - - Facilities\prim-lexing.fs - - - Facilities\prim-parsing.fsi - - - Facilities\prim-parsing.fs - - - Facilities\ReferenceResolver.fsi - - - Facilities\ReferenceResolver.fs - - - Facilities/SimulatedMSBuildReferenceResolver.fsi - - - Facilities/SimulatedMSBuildReferenceResolver.fs - - - Facilities\CompilerLocation.fsi - - - Facilities\CompilerLocation.fs - - - Facilities\BuildGraph.fsi - - - Facilities\BuildGraph.fs - + + + + + + + + + + + + + + + + + + + + + + + + --unicode --lexlib Internal.Utilities.Text.Lexing AbstractIL\illex.fsl @@ -305,96 +179,40 @@ AbstractIL\FsYacc\ilpars.fsy - - AbstractIL\il.fsi - - - AbstractIL\il.fs - - - AbstractIL\ilx.fsi - - - AbstractIL\ilx.fs - - - AbstractIL\ilascii.fsi - - - AbstractIL\ilascii.fs - + + + + + + AbstractIL\FsYaccOut\ilpars.fs AbstractIL\FsLexOut\illex.fs - - AbstractIL\ilprint.fsi - - - AbstractIL\ilprint.fs - - - AbstractIL\ilmorph.fsi - - - AbstractIL\ilmorph.fs - - - AbstractIL\ilsign.fsi - - - AbstractIL\ilsign.fs - - - AbstractIL\ilnativeres.fsi - - - AbstractIL\ilnativeres.fs - - - AbstractIL\ilsupp.fsi - - - AbstractIL\ilsupp.fs - - - AbstractIL\ilbinary.fsi - - - AbstractIL\ilbinary.fs - - - AbstractIL\ilread.fsi - - - AbstractIL\ilread.fs - - - AbstractIL\ilwritepdb.fsi - - - AbstractIL\ilwritepdb.fs - - - AbstractIL\ilwrite.fsi - - - AbstractIL\ilwrite.fs - - - AbstractIL\ilreflect.fsi - - - AbstractIL\ilreflect.fs - - - SyntaxTree\PrettyNaming.fsi - - - SyntaxTree\PrettyNaming.fs - + + + + + + + + + + + + + + + + + + + + + + --unicode --lexlib Internal.Utilities.Text.Lexing SyntaxTree\pplex.fsl @@ -423,42 +241,18 @@ SyntaxTree\FsYacc\pars.fsy - - SyntaxTree\UnicodeLexing.fsi - - - SyntaxTree\UnicodeLexing.fs - - - SyntaxTree\XmlDoc.fsi - - - SyntaxTree\XmlDoc.fs - - - SyntaxTree\SyntaxTrivia.fsi - - - SyntaxTree\SyntaxTrivia.fs - - - SyntaxTree\SyntaxTree.fsi - - - SyntaxTree\SyntaxTree.fs - - - SyntaxTree\SyntaxTreeOps.fsi - - - SyntaxTree\SyntaxTreeOps.fs - - - SyntaxTree\ParseHelpers.fsi - - - SyntaxTree\ParseHelpers.fs - + + + + + + + + + + + + SyntaxTree\FsYaccOutput\pppars.fs @@ -477,267 +271,95 @@ SyntaxTree\FsLexOutput\lex.fs - - SyntaxTree\LexFilter.fsi - - - SyntaxTree\LexFilter.fs - - - TypedTree\tainted.fsi - - - TypedTree\tainted.fs - - - TypedTree\TypeProviders.fsi - - - TypedTree\TypeProviders.fs - - - TypedTree\QuotationPickler.fsi - - - TypedTree\QuotationPickler.fs - - - TypedTree\CompilerGlobalState.fsi - - - TypedTree\CompilerGlobalState.fs - - - TypedTree\TypedTree.fsi - - - TypedTree\TypedTree.fs - - - TypedTree\TypedTreeBasics.fsi - - - TypedTree\TypedTreeBasics.fs - - - TypedTree\TcGlobals.fs - - - TypedTree\TypedTreeOps.fsi - - - TypedTree\TypedTreeOps.fs - - - TypedTree\TypedTreePickle.fsi - - - TypedTree\TypedTreePickle.fs - - - Checking\import.fsi - - - Checking\import.fs - - - Checking\TypeHierarchy.fsi - - - Checking\TypeHierarchy.fs - - - Checking\infos.fsi - - - Checking\infos.fs - - - Checking\AccessibilityLogic.fsi - - - Checking\AccessibilityLogic.fs - - - Checking\AttributeChecking.fsi - - - Checking\AttributeChecking.fs - - - Checking\TypeRelations.fsi - - - Checking\TypeRelations.fs - - - Checking\InfoReader.fsi - - - Checking\InfoReader.fs - - - Checking\NicePrint.fsi - - - Checking\NicePrint.fs - - - Checking\AugmentWithHashCompare.fsi - - - Checking\AugmentWithHashCompare.fs - - - Checking\NameResolution.fsi - - - Checking\NameResolution.fs - - - Checking\SignatureConformance.fsi - - - Checking\SignatureConformance.fs - - - Checking\MethodOverrides.fsi - - - Checking\MethodOverrides.fs - - - Checking\MethodCalls.fsi - - - Checking\MethodCalls.fs - - - Checking\PatternMatchCompilation.fsi - - - Checking\PatternMatchCompilation.fs - - - Checking\ConstraintSolver.fsi - - - Checking\ConstraintSolver.fs - - - Checking\CheckFormatStrings.fsi - - - Checking\CheckFormatStrings.fs - - - Checking\FindUnsolved.fsi - - - Checking\FindUnsolved.fs - - - Checking\QuotationTranslator.fsi - - - Checking\QuotationTranslator.fs - - - Checking\PostInferenceChecks.fsi - - - Checking\PostInferenceChecks.fs - - - Checking\CheckExpressions.fsi - - - Checking\CheckExpressions.fs - - - Checking\CheckComputationExpressions.fsi - - - Checking\CheckComputationExpressions.fs - - - Checking\CheckDeclarations.fsi - - - Checking\CheckDeclarations.fs - - - Optimize\Optimizer.fsi - - - Optimize\Optimizer.fs - - - Optimize\DetupleArgs.fsi - - - Optimize\DetupleArgs.fs - - - Optimize\InnerLambdasToTopLevelFuncs.fsi - - - Optimize\InnerLambdasToTopLevelFuncs.fs - - - Optimize\LowerCalls.fsi - - - Optimize\LowerCalls.fs - - - Optimize\LowerSequences.fsi - - - Optimize\LowerSequences.fs - - - Optimize\LowerComputedCollections.fsi - - - Optimize\LowerComputedCollections.fs - - - Optimize\LowerStateMachines.fsi - - - Optimize\LowerStateMachines.fs - - - Optimize\LowerLocalMutables.fsi - - - Optimize\LowerLocalMutables.fs - - - CodeGen\EraseClosures.fsi - - - CodeGen\EraseClosures.fs - - - CodeGen\EraseUnions.fsi - - - CodeGen\EraseUnions.fs - - - CodeGen\IlxGen.fsi - - - CodeGen\IlxGen.fs - - - Driver\FxResolver.fsi - - - Driver\FxResolver.fs - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Driver\AssemblyResolveHandler.fsi @@ -756,265 +378,97 @@ Driver\DependencyProvider.fs - - Driver\CompilerConfig.fsi - - - Driver\CompilerConfig.fs - - - Driver\CompilerImports.fsi - - - Driver\CompilerImports.fs - - - Driver\CompilerDiagnostics.fsi - - - Driver\CompilerDiagnostics.fs - - - Driver\ParseAndCheckInputs.fsi - - - Driver\ParseAndCheckInputs.fs - - - Driver\ScriptClosure.fsi - - - Driver\ScriptClosure.fs - - - Driver\CompilerOptions.fsi - - - Driver\CompilerOptions.fs - - - Driver\OptimizeInputs.fsi - - - Driver\OptimizeInputs.fs - - - Driver\XmlDocFileWriter.fsi - - - Driver\XmlDocFileWriter.fs - - - Driver\BinaryResourceFormats.fsi - - - Driver\BinaryResourceFormats.fs - - - Driver\StaticLinking.fsi - - - Driver\StaticLinking.fs - - - Driver\CreateILModule.fsi - - - Driver\CreateILModule.fs - - - Driver\fsc.fsi - - - Driver\fsc.fs - + + + + + + + + + + + + + + + + + + + + + + + + - - Symbols/FSharpDiagnostic.fsi - - - Symbols/FSharpDiagnostic.fs - - - Symbols/SymbolHelpers.fsi - - - Symbols/SymbolHelpers.fs - - - Symbols/Symbols.fsi - - - Symbols/Symbols.fs - - - Symbols/Exprs.fsi - - - Symbols/Exprs.fs - - - Symbols/SymbolPatterns.fsi - - - Symbols/SymbolPatterns.fs - + + + + + + + + + + - - Service/SemanticClassification.fsi - - - Service/SemanticClassification.fs - - - Service/ItemKey.fsi - - - Service/ItemKey.fs - - - Service/SemanticClassificationKey.fsi - - - Service/SemanticClassificationKey.fs - - - Service/FSharpSource.fsi - - - Service/FSharpSource.fs - - - Service/IncrementalBuild.fsi - - - Service/IncrementalBuild.fs - - - Service/ServiceCompilerDiagnostics.fsi - - - Service/ServiceCompilerDiagnostics.fs - - - Service/ServiceConstants.fs - - - Service/ServiceDeclarationLists.fsi - - - Service/ServiceDeclarationLists.fs - - - Service/ServiceLexing.fsi - - - Service/ServiceLexing.fs - - - Service/ServiceParseTreeWalk.fsi - - - Service/ServiceParseTreeWalk.fs - - - Service/ServiceNavigation.fsi - - - Service/ServiceNavigation.fs - - - Service/ServiceParamInfoLocations.fsi - - - Service/ServiceParamInfoLocations.fs - - - Service/FSharpParseFileResults.fsi - - - Service/FSharpParseFileResults.fs - - - Service/ServiceParsedInputOps.fsi - - - Service/ServiceParsedInputOps.fs - - - Service/ServiceAssemblyContent.fsi - - - Service/ServiceAssemblyContent.fs - - - Service/ServiceXmlDocParser.fsi - - - Service/ServiceXmlDocParser.fs - - - Service/ExternalSymbol.fsi - - - Service/ExternalSymbol.fs - - - Service/QuickParse.fsi - - - Service/QuickParse.fs - - - Service/FSharpCheckerResults.fsi - - - Service/FSharpCheckerResults.fs - - - Service/service.fsi - - - Service/service.fs - - - Service/ServiceInterfaceStubGenerator.fsi - - - Service/ServiceInterfaceStubGenerator.fs - - - Service/ServiceStructure.fsi - - - Service/ServiceStructure.fs - - - Service/ServiceAnalysis.fsi - - - Service/ServiceAnalysis.fs - - - Interactive/fsi.fsi - - - Interactive/fsi.fs - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - Legacy/LegacyMSBuildReferenceResolver.fsi - - - Legacy/LegacyMSBuildReferenceResolver.fs - + + - - Legacy/LegacyHostedCompilerForTesting.fs - + diff --git a/src/Compiler/Service/ServiceLexing.fsi b/src/Compiler/Service/ServiceLexing.fsi index df1dfdde294..5e2e5a9732d 100755 --- a/src/Compiler/Service/ServiceLexing.fsi +++ b/src/Compiler/Service/ServiceLexing.fsi @@ -6,7 +6,6 @@ open System open System.Threading open FSharp.Compiler open FSharp.Compiler.Text - #nowarn "57" /// Represents encoded information for the end-of-line continuation of lexing From cccdfb1dbded36fdb8b58cb2185b7021f54217b8 Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 19 May 2022 22:45:51 +0100 Subject: [PATCH 02/20] Formatting: test adjusting line length to 140 (#13170) * adjust line length * apply formatting --- .editorconfig | 1 + src/FSharp.Build/FSharpEmbedResXSource.fs | 4 +- src/FSharp.Build/FSharpEmbedResourceText.fs | 31 +++------------ src/FSharp.Build/MapSourceRoots.fs | 8 +--- .../FSharp.DependencyManager.ProjectFile.fs | 4 +- .../FSharp.DependencyManager.Utilities.fs | 8 +--- .../FSharp.DependencyManager.fs | 39 ++++--------------- src/fsi/console.fs | 4 +- src/fsi/fsimain.fs | 3 +- 9 files changed, 21 insertions(+), 81 deletions(-) diff --git a/.editorconfig b/.editorconfig index 594dcb0ebde..5d68b1d516b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,7 @@ root = true [*.fs] +max_line_length=140 fsharp_newline_between_type_definition_and_members=true fsharp_max_infix_operator_expression=80 fsharp_max_array_or_list_width=80 diff --git a/src/FSharp.Build/FSharpEmbedResXSource.fs b/src/FSharp.Build/FSharpEmbedResXSource.fs index 24665c44f58..4318ff75e46 100644 --- a/src/FSharp.Build/FSharpEmbedResXSource.fs +++ b/src/FSharp.Build/FSharpEmbedResXSource.fs @@ -98,9 +98,7 @@ module internal {1} = XElement(xname "summary", docComment) .ToString() .Split([| "\r\n"; "\r"; "\n" |], StringSplitOptions.None) - |> Array.fold - (fun (sb: StringBuilder) line -> sb.AppendLine(" /// " + line)) - (StringBuilder()) + |> Array.fold (fun (sb: StringBuilder) line -> sb.AppendLine(" /// " + line)) (StringBuilder()) // add the resource let accessorBody = match (generateLegacy, generateLiteral) with diff --git a/src/FSharp.Build/FSharpEmbedResourceText.fs b/src/FSharp.Build/FSharpEmbedResourceText.fs index eb1907187a8..ede5581c02e 100644 --- a/src/FSharp.Build/FSharpEmbedResourceText.fs +++ b/src/FSharp.Build/FSharpEmbedResourceText.fs @@ -175,12 +175,7 @@ type FSharpEmbedResourceText() = | 'f' -> AddHole "System.Double" | 's' -> AddHole "System.String" | '%' -> sb.Append('%') |> ignore - | c -> - Err( - fileName, - lineNum, - sprintf "'%%%c' is not a valid sequence, only %%d %%x %%X %%f %%s or %%%%" c - ) + | c -> Err(fileName, lineNum, sprintf "'%%%c' is not a valid sequence, only %%d %%x %%X %%f %%s or %%%%" c) i <- i + 2 else @@ -219,11 +214,7 @@ type FSharpEmbedResourceText() = i <- i + 1 // parse short identifier if i < txt.Length && not (System.Char.IsLetter(txt.[i])) then - Err( - fileName, - lineNum, - sprintf "The first character in the short identifier should be a letter, but found '%c'" txt.[i] - ) + Err(fileName, lineNum, sprintf "The first character in the short identifier should be a letter, but found '%c'" txt.[i]) while i < txt.Length && System.Char.IsLetterOrDigit txt.[i] do identB.Append txt.[i] |> ignore @@ -240,11 +231,7 @@ type FSharpEmbedResourceText() = i <- i + 1 if i = txt.Length then - Err( - fileName, - lineNum, - sprintf "After the identifier '%s' and comma, there should be the quoted string resource" ident - ) + Err(fileName, lineNum, sprintf "After the identifier '%s' and comma, there should be the quoted string resource" ident) else let str = try @@ -405,13 +392,7 @@ open Printf && (File.GetLastWriteTimeUtc(fileName) <= File.GetLastWriteTimeUtc(outXmlFileName)) if condition5 then - printMessage ( - sprintf - "Skipping generation of %s and %s from %s since up-to-date" - outFileName - outXmlFileName - fileName - ) + printMessage (sprintf "Skipping generation of %s and %s from %s since up-to-date" outFileName outXmlFileName fileName) Some(fileName, outFileName, outXmlFileName) else @@ -543,9 +524,7 @@ open Printf printMessage (sprintf "Generating .resx for %s" outFileName) fprintfn out "" // gen validation method - fprintfn - out - " /// Call this method once to validate that all known resources are valid; throws if not" + fprintfn out " /// Call this method once to validate that all known resources are valid; throws if not" fprintfn out " static member RunStartupValidation() =" diff --git a/src/FSharp.Build/MapSourceRoots.fs b/src/FSharp.Build/MapSourceRoots.fs index eb13aecad0b..c2131209dad 100644 --- a/src/FSharp.Build/MapSourceRoots.fs +++ b/src/FSharp.Build/MapSourceRoots.fs @@ -153,12 +153,8 @@ type MapSourceRoots() = // Since the paths in ItemSpec have backslashes replaced with slashes on non-Windows platforms we need to do the same for ContainingRoot. match topLevelMappedPaths.TryGetValue(Utilities.FixFilePath(containingRoot)) with | true, mappedTopLevelPath -> - root.SetMetadata( - MappedPath, - mappedTopLevelPath + ensureEndsWithSlash (nestedRoot.Replace('\\', '/')) - ) - | false, _ -> - log.LogError(FSBuild.SR.mapSourceRootsNoSuchTopLevelSourceRoot containingRoot) + root.SetMetadata(MappedPath, mappedTopLevelPath + ensureEndsWithSlash (nestedRoot.Replace('\\', '/'))) + | false, _ -> log.LogError(FSBuild.SR.mapSourceRootsNoSuchTopLevelSourceRoot containingRoot) | NullOrEmpty -> log.LogError(FSBuild.SR.mapSourceRootsNoSuchTopLevelSourceRoot "") | NullOrEmpty -> () else diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs index 53b4c3723cb..e0d6660b99b 100644 --- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs +++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs @@ -96,9 +96,7 @@ module internal ProjectFile = let fields = line.Split(',') if fields.Length < 8 then - raise ( - InvalidOperationException(sprintf "Internal error - Invalid resolutions file format '%s'" line) - ) + raise (InvalidOperationException(sprintf "Internal error - Invalid resolutions file format '%s'" line)) else { NugetPackageId = fields[0] diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs index 22e81c56a13..1e0aeebcb7b 100644 --- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs +++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs @@ -144,13 +144,7 @@ module internal Utilities = | None -> -1 let arguments prefix = - sprintf - "%s -restore %s %c%s%c /nologo /t:InteractivePackageManagement" - prefix - binLoggingArguments - '\"' - projectPath - '\"' + sprintf "%s -restore %s %c%s%c /nologo /t:InteractivePackageManagement" prefix binLoggingArguments '\"' projectPath '\"' let workingDir = Path.GetDirectoryName projectPath let dotnetHostPath = getDotnetHostPath () diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs index 908f9cc5aa3..794aa591874 100644 --- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs +++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs @@ -58,20 +58,12 @@ module FSharpDependencyManager = p seq { - match not (String.IsNullOrEmpty(inc)), not (String.IsNullOrEmpty(ver)), not (String.IsNullOrEmpty(script)) - with - | true, true, false -> - yield sprintf @" " inc ver + match not (String.IsNullOrEmpty(inc)), not (String.IsNullOrEmpty(ver)), not (String.IsNullOrEmpty(script)) with + | true, true, false -> yield sprintf @" " inc ver | true, true, true -> - yield - sprintf - @" " - inc - ver - script + yield sprintf @" " inc ver script | true, false, false -> yield sprintf @" " inc - | true, false, true -> - yield sprintf @" " inc script + | true, false, true -> yield sprintf @" " inc script | _ -> () match not (String.IsNullOrEmpty(src)) with @@ -188,8 +180,7 @@ module FSharpDependencyManager = let mutable timeout = None lines - |> List.choose (fun line -> - parsePackageReferenceOption scriptExt (fun p -> binLogPath <- p) (fun t -> timeout <- t) line) + |> List.choose (fun line -> parsePackageReferenceOption scriptExt (fun p -> binLogPath <- p) (fun t -> timeout <- t) line) |> List.distinct |> (fun l -> l, binLogPath, timeout) @@ -202,8 +193,7 @@ module FSharpDependencyManager = match directive with | "i" -> sprintf "RestoreSources=%s" line | _ -> line) - |> List.choose (fun line -> - parsePackageReferenceOption scriptExt (fun p -> binLogPath <- p) (fun t -> timeout <- t) line) + |> List.choose (fun line -> parsePackageReferenceOption scriptExt (fun p -> binLogPath <- p) (fun t -> timeout <- t) line) |> List.distinct |> (fun l -> l, binLogPath, timeout) @@ -391,13 +381,7 @@ type FSharpDependencyManager(outputDirectory: string option) = let directiveLines = Seq.append packageManagerTextLines configIncludes let resolutionResult = - prepareDependencyResolutionFiles ( - scriptExt, - directiveLines, - targetFrameworkMoniker, - runtimeIdentifier, - timeout - ) + prepareDependencyResolutionFiles (scriptExt, directiveLines, targetFrameworkMoniker, runtimeIdentifier, timeout) match resolutionResult.resolutionsFile with | Some file -> @@ -425,13 +409,6 @@ type FSharpDependencyManager(outputDirectory: string option) = | None -> let empty = Seq.empty - ResolveDependenciesResult( - resolutionResult.success, - resolutionResult.stdOut, - resolutionResult.stdErr, - empty, - empty, - empty - ) + ResolveDependenciesResult(resolutionResult.success, resolutionResult.stdOut, resolutionResult.stdErr, empty, empty, empty) generateAndBuildProjectArtifacts :> obj diff --git a/src/fsi/console.fs b/src/fsi/console.fs index 88c45399567..cd84b705b4a 100644 --- a/src/fsi/console.fs +++ b/src/fsi/console.fs @@ -189,9 +189,7 @@ type internal ReadLineConsole() = if (lastDot < 0) then None, name, input.Substring(0, start) else - Some(name.Substring(0, lastDot)), - name.Substring(lastDot + 1), - input.Substring(0, start + lastDot + 1) + Some(name.Substring(0, lastDot)), name.Substring(lastDot + 1), input.Substring(0, start + lastDot + 1) try complete (attr, pref) diff --git a/src/fsi/fsimain.fs b/src/fsi/fsimain.fs index 3cad37941ac..9df8f0cead4 100644 --- a/src/fsi/fsimain.fs +++ b/src/fsi/fsimain.fs @@ -232,8 +232,7 @@ let evaluateSession (argv: string[]) = let fsiTy = fsiAssembly.GetType("FSharp.Compiler.Interactive.Settings") if isNull fsiAssembly then - failwith - "failed to find type FSharp.Compiler.Interactive.Settings in FSharp.Compiler.Interactive.Settings.dll" + failwith "failed to find type FSharp.Compiler.Interactive.Settings in FSharp.Compiler.Interactive.Settings.dll" Some(callStaticMethod fsiTy "get_fsi" []) From e8fc5fb19f16828b997370083bd295c922959e1b Mon Sep 17 00:00:00 2001 From: Don Syme Date: Fri, 20 May 2022 11:24:26 +0100 Subject: [PATCH 03/20] remove whitespace prior to formatting (#13172) --- src/Compiler/Utilities/TaggedCollections.fs | 2002 +++++++++---------- src/Compiler/Utilities/sr.fs | 275 +-- 2 files changed, 1140 insertions(+), 1137 deletions(-) diff --git a/src/Compiler/Utilities/TaggedCollections.fs b/src/Compiler/Utilities/TaggedCollections.fs index 0d99b026a7a..df9167ef792 100644 --- a/src/Compiler/Utilities/TaggedCollections.fs +++ b/src/Compiler/Utilities/TaggedCollections.fs @@ -2,1080 +2,1080 @@ namespace Internal.Utilities.Collections.Tagged - #nowarn "51" - #nowarn "69" // interface implementations in augmentations - #nowarn "60" // override implementations in augmentations - - open Microsoft.FSharp.Core - open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators - open System.Collections.Generic - - [] - [] - type internal SetTree<'T>(k: 'T) = - member _.Key = k - - [] - [] - [] - type internal SetTreeNode<'T>(v:'T, left:SetTree<'T>, right: SetTree<'T>, h: int) = - inherit SetTree<'T>(v) - - member _.Left = left - member _.Right = right - member _.Height = h - - [] - module SetTree = - let empty = null - - let inline isEmpty (t:SetTree<'T>) = isNull t - - let rec countAux (t:SetTree<'T>) acc = - if isEmpty t then - acc - else - match t with - | :? SetTreeNode<'T> as tn -> countAux tn.Left (countAux tn.Right (acc+1)) - | _ -> acc+1 - - let count s = countAux s 0 - - let inline height (t:SetTree<'T>) = - if isEmpty t then 0 - else - match t with - | :? SetTreeNode<'T> as tn -> tn.Height - | _ -> 1 - - [] - let tolerance = 2 - - let mk l k r : SetTree<'T> = - let hl = height l - let hr = height r - let m = if hl < hr then hr else hl - if m = 0 then // m=0 ~ isEmpty l && isEmpty r - SetTree k - else - SetTreeNode (k, l, r, m+1) :> SetTree<'T> - - let inline private asNode(value:SetTree<'T>) : SetTreeNode<'T> = - value :?> SetTreeNode<'T> - - let rebalance t1 v t2 = - let t1h = height t1 - let t2h = height t2 - if t2h > t1h + tolerance then // right is heavier than left - let t2' = asNode(t2) - // one of the nodes must have height > height t1 + 1 - if height t2'.Left > t1h + 1 then // balance left: combination - let t2l = asNode(t2'.Left) - mk (mk t1 v t2l.Left) t2l.Key (mk t2l.Right t2'.Key t2'.Right) - else // rotate left - mk (mk t1 v t2'.Left) t2.Key t2'.Right - else - if t1h > t2h + tolerance then // left is heavier than right - let t1' = asNode(t1) - // one of the nodes must have height > height t2 + 1 - if height t1'.Right > t2h + 1 then - // balance right: combination - let t1r = asNode(t1'.Right) - mk (mk t1'.Left t1.Key t1r.Left) t1r.Key (mk t1r.Right v t2) - else - mk t1'.Left t1'.Key (mk t1'.Right v t2) - else mk t1 v t2 - - let rec add (comparer: IComparer<'T>) k (t:SetTree<'T>) : SetTree<'T> = - if isEmpty t then SetTree k - else - let c = comparer.Compare(k, t.Key) - match t with - | :? SetTreeNode<'T> as tn -> - if c < 0 then rebalance (add comparer k tn.Left) tn.Key tn.Right - elif c = 0 then t - else rebalance tn.Left tn.Key (add comparer k tn.Right) - | _ -> - // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated - let c = comparer.Compare(k, t.Key) - if c < 0 then SetTreeNode (k, empty, t, 2) :> SetTree<'T> - elif c = 0 then t - else SetTreeNode (k, t, empty, 2) :> SetTree<'T> - - let rec balance comparer (t1:SetTree<'T>) k (t2:SetTree<'T>) = - // Given t1 < k < t2 where t1 and t2 are "balanced", - // return a balanced tree for . - // Recall: balance means subtrees heights differ by at most "tolerance" - if isEmpty t1 then add comparer k t2 // drop t1 = empty - elif isEmpty t2 then add comparer k t1 // drop t2 = empty - else - match t1 with - | :? SetTreeNode<'T> as t1n -> - match t2 with - | :? SetTreeNode<'T> as t2n -> - // Have: (t1l < k1 < t1r) < k < (t2l < k2 < t2r) - // Either (a) h1, h2 differ by at most 2 - no rebalance needed. - // (b) h1 too small, i.e. h1+2 < h2 - // (c) h2 too small, i.e. h2+2 < h1 - if t1n.Height + tolerance < t2n.Height then - // case: b, h1 too small - // push t1 into low side of t2, may increase height by 1 so rebalance - rebalance (balance comparer t1 k t2n.Left) t2n.Key t2n.Right - elif t2n.Height + tolerance < t1n.Height then - // case: c, h2 too small - // push t2 into high side of t1, may increase height by 1 so rebalance - rebalance t1n.Left t1n.Key (balance comparer t1n.Right k t2) - else - // case: a, h1 and h2 meet balance requirement - mk t1 k t2 - | _ -> add comparer k (add comparer t2.Key t1) - | _ -> add comparer k (add comparer t1.Key t2) - - let rec split (comparer: IComparer<'T>) pivot (t:SetTree<'T>) = - // Given a pivot and a set t - // Return { x in t s.t. x < pivot }, pivot in t?, { x in t s.t. x > pivot } - if isEmpty t then empty, false, empty - else - match t with - | :? SetTreeNode<'T> as tn -> - let c = comparer.Compare(pivot, tn.Key) - if c < 0 then // pivot t1 - let t11Lo, havePivot, t11Hi = split comparer pivot tn.Left - t11Lo, havePivot, balance comparer t11Hi tn.Key tn.Right - elif c = 0 then // pivot is k1 - tn.Left, true, tn.Right - else // pivot t2 - let t12Lo, havePivot, t12Hi = split comparer pivot tn.Right - balance comparer tn.Left tn.Key t12Lo, havePivot, t12Hi - | _ -> - let c = comparer.Compare(t.Key, pivot) - if c < 0 then t, false, empty // singleton under pivot - elif c = 0 then empty, true, empty // singleton is pivot - else empty, false, t // singleton over pivot - - let rec spliceOutSuccessor (t:SetTree<'T>) = - if isEmpty t then failwith "internal error: Set.spliceOutSuccessor" - else - match t with - | :? SetTreeNode<'T> as tn -> - if isEmpty tn.Left then tn.Key, tn.Right - else let k3, l' = spliceOutSuccessor tn.Left in k3, mk l' tn.Key tn.Right - | _ -> t.Key, empty - - let rec remove (comparer: IComparer<'T>) k (t:SetTree<'T>) = - if isEmpty t then t - else - let c = comparer.Compare(k, t.Key) - match t with - | :? SetTreeNode<'T> as tn -> - if c < 0 then rebalance (remove comparer k tn.Left) tn.Key tn.Right - elif c = 0 then - if isEmpty tn.Left then tn.Right - elif isEmpty tn.Right then tn.Left - else - let sk, r' = spliceOutSuccessor tn.Right - mk tn.Left sk r' - else rebalance tn.Left tn.Key (remove comparer k tn.Right) - | _ -> - if c = 0 then empty - else t - - let rec contains (comparer: IComparer<'T>) k (t:SetTree<'T>) = - if isEmpty t then false - else +#nowarn "51" +#nowarn "69" // interface implementations in augmentations +#nowarn "60" // override implementations in augmentations + +open Microsoft.FSharp.Core +open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators +open System.Collections.Generic + +[] +[] +type internal SetTree<'T>(k: 'T) = + member _.Key = k + +[] +[] +[] +type internal SetTreeNode<'T>(v:'T, left:SetTree<'T>, right: SetTree<'T>, h: int) = + inherit SetTree<'T>(v) + + member _.Left = left + member _.Right = right + member _.Height = h + +[] +module SetTree = + let empty = null + + let inline isEmpty (t:SetTree<'T>) = isNull t + + let rec countAux (t:SetTree<'T>) acc = + if isEmpty t then + acc + else + match t with + | :? SetTreeNode<'T> as tn -> countAux tn.Left (countAux tn.Right (acc+1)) + | _ -> acc+1 + + let count s = countAux s 0 + + let inline height (t:SetTree<'T>) = + if isEmpty t then 0 + else + match t with + | :? SetTreeNode<'T> as tn -> tn.Height + | _ -> 1 + + [] + let tolerance = 2 + + let mk l k r : SetTree<'T> = + let hl = height l + let hr = height r + let m = if hl < hr then hr else hl + if m = 0 then // m=0 ~ isEmpty l && isEmpty r + SetTree k + else + SetTreeNode (k, l, r, m+1) :> SetTree<'T> + + let inline private asNode(value:SetTree<'T>) : SetTreeNode<'T> = + value :?> SetTreeNode<'T> + + let rebalance t1 v t2 = + let t1h = height t1 + let t2h = height t2 + if t2h > t1h + tolerance then // right is heavier than left + let t2' = asNode(t2) + // one of the nodes must have height > height t1 + 1 + if height t2'.Left > t1h + 1 then // balance left: combination + let t2l = asNode(t2'.Left) + mk (mk t1 v t2l.Left) t2l.Key (mk t2l.Right t2'.Key t2'.Right) + else // rotate left + mk (mk t1 v t2'.Left) t2.Key t2'.Right + else + if t1h > t2h + tolerance then // left is heavier than right + let t1' = asNode(t1) + // one of the nodes must have height > height t2 + 1 + if height t1'.Right > t2h + 1 then + // balance right: combination + let t1r = asNode(t1'.Right) + mk (mk t1'.Left t1.Key t1r.Left) t1r.Key (mk t1r.Right v t2) + else + mk t1'.Left t1'.Key (mk t1'.Right v t2) + else mk t1 v t2 + + let rec add (comparer: IComparer<'T>) k (t:SetTree<'T>) : SetTree<'T> = + if isEmpty t then SetTree k + else + let c = comparer.Compare(k, t.Key) + match t with + | :? SetTreeNode<'T> as tn -> + if c < 0 then rebalance (add comparer k tn.Left) tn.Key tn.Right + elif c = 0 then t + else rebalance tn.Left tn.Key (add comparer k tn.Right) + | _ -> + // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated let c = comparer.Compare(k, t.Key) - match t with - | :? SetTreeNode<'T> as tn -> - if c < 0 then contains comparer k tn.Left - elif c = 0 then true - else contains comparer k tn.Right - | _ -> (c = 0) - - let rec iter f (t:SetTree<'T>) = - if isEmpty t then () - else - match t with - | :? SetTreeNode<'T> as tn -> iter f tn.Left; f tn.Key; iter f tn.Right - | _ -> f t.Key - - // Fold, left-to-right. - // - // NOTE: This differs from the behaviour of Map.fold which folds right-to-left. - let rec fold f (t:SetTree<'T>) x = - if isEmpty t then x - else - match t with - | :? SetTreeNode<'T> as tn -> fold f tn.Right (f tn.Key (fold f tn.Left x)) - | _ -> f t.Key x - - let rec forall f (t:SetTree<'T>) = - if isEmpty t then true - else - match t with - | :? SetTreeNode<'T> as tn -> f tn.Key && forall f tn.Left && forall f tn.Right - | _ -> f t.Key - - let rec exists f (t:SetTree<'T>) = - if isEmpty t then false - else - match t with - | :? SetTreeNode<'T> as tn -> f tn.Key || exists f tn.Left || exists f tn.Right - | _ -> f t.Key - - let subset comparer a b = - forall (fun x -> contains comparer x b) a - - let rec filterAux comparer f (t:SetTree<'T>) acc = + if c < 0 then SetTreeNode (k, empty, t, 2) :> SetTree<'T> + elif c = 0 then t + else SetTreeNode (k, t, empty, 2) :> SetTree<'T> + + let rec balance comparer (t1:SetTree<'T>) k (t2:SetTree<'T>) = + // Given t1 < k < t2 where t1 and t2 are "balanced", + // return a balanced tree for . + // Recall: balance means subtrees heights differ by at most "tolerance" + if isEmpty t1 then add comparer k t2 // drop t1 = empty + elif isEmpty t2 then add comparer k t1 // drop t2 = empty + else + match t1 with + | :? SetTreeNode<'T> as t1n -> + match t2 with + | :? SetTreeNode<'T> as t2n -> + // Have: (t1l < k1 < t1r) < k < (t2l < k2 < t2r) + // Either (a) h1, h2 differ by at most 2 - no rebalance needed. + // (b) h1 too small, i.e. h1+2 < h2 + // (c) h2 too small, i.e. h2+2 < h1 + if t1n.Height + tolerance < t2n.Height then + // case: b, h1 too small + // push t1 into low side of t2, may increase height by 1 so rebalance + rebalance (balance comparer t1 k t2n.Left) t2n.Key t2n.Right + elif t2n.Height + tolerance < t1n.Height then + // case: c, h2 too small + // push t2 into high side of t1, may increase height by 1 so rebalance + rebalance t1n.Left t1n.Key (balance comparer t1n.Right k t2) + else + // case: a, h1 and h2 meet balance requirement + mk t1 k t2 + | _ -> add comparer k (add comparer t2.Key t1) + | _ -> add comparer k (add comparer t1.Key t2) + + let rec split (comparer: IComparer<'T>) pivot (t:SetTree<'T>) = + // Given a pivot and a set t + // Return { x in t s.t. x < pivot }, pivot in t?, { x in t s.t. x > pivot } + if isEmpty t then empty, false, empty + else + match t with + | :? SetTreeNode<'T> as tn -> + let c = comparer.Compare(pivot, tn.Key) + if c < 0 then // pivot t1 + let t11Lo, havePivot, t11Hi = split comparer pivot tn.Left + t11Lo, havePivot, balance comparer t11Hi tn.Key tn.Right + elif c = 0 then // pivot is k1 + tn.Left, true, tn.Right + else // pivot t2 + let t12Lo, havePivot, t12Hi = split comparer pivot tn.Right + balance comparer tn.Left tn.Key t12Lo, havePivot, t12Hi + | _ -> + let c = comparer.Compare(t.Key, pivot) + if c < 0 then t, false, empty // singleton under pivot + elif c = 0 then empty, true, empty // singleton is pivot + else empty, false, t // singleton over pivot + + let rec spliceOutSuccessor (t:SetTree<'T>) = + if isEmpty t then failwith "internal error: Set.spliceOutSuccessor" + else + match t with + | :? SetTreeNode<'T> as tn -> + if isEmpty tn.Left then tn.Key, tn.Right + else let k3, l' = spliceOutSuccessor tn.Left in k3, mk l' tn.Key tn.Right + | _ -> t.Key, empty + + let rec remove (comparer: IComparer<'T>) k (t:SetTree<'T>) = + if isEmpty t then t + else + let c = comparer.Compare(k, t.Key) + match t with + | :? SetTreeNode<'T> as tn -> + if c < 0 then rebalance (remove comparer k tn.Left) tn.Key tn.Right + elif c = 0 then + if isEmpty tn.Left then tn.Right + elif isEmpty tn.Right then tn.Left + else + let sk, r' = spliceOutSuccessor tn.Right + mk tn.Left sk r' + else rebalance tn.Left tn.Key (remove comparer k tn.Right) + | _ -> + if c = 0 then empty + else t + + let rec contains (comparer: IComparer<'T>) k (t:SetTree<'T>) = + if isEmpty t then false + else + let c = comparer.Compare(k, t.Key) + match t with + | :? SetTreeNode<'T> as tn -> + if c < 0 then contains comparer k tn.Left + elif c = 0 then true + else contains comparer k tn.Right + | _ -> (c = 0) + + let rec iter f (t:SetTree<'T>) = + if isEmpty t then () + else + match t with + | :? SetTreeNode<'T> as tn -> iter f tn.Left; f tn.Key; iter f tn.Right + | _ -> f t.Key + + // Fold, left-to-right. + // + // NOTE: This differs from the behaviour of Map.fold which folds right-to-left. + let rec fold f (t:SetTree<'T>) x = + if isEmpty t then x + else + match t with + | :? SetTreeNode<'T> as tn -> fold f tn.Right (f tn.Key (fold f tn.Left x)) + | _ -> f t.Key x + + let rec forall f (t:SetTree<'T>) = + if isEmpty t then true + else + match t with + | :? SetTreeNode<'T> as tn -> f tn.Key && forall f tn.Left && forall f tn.Right + | _ -> f t.Key + + let rec exists f (t:SetTree<'T>) = + if isEmpty t then false + else + match t with + | :? SetTreeNode<'T> as tn -> f tn.Key || exists f tn.Left || exists f tn.Right + | _ -> f t.Key + + let subset comparer a b = + forall (fun x -> contains comparer x b) a + + let rec filterAux comparer f (t:SetTree<'T>) acc = + if isEmpty t then acc + else + match t with + | :? SetTreeNode<'T> as tn -> + let acc = if f tn.Key then add comparer tn.Key acc else acc + filterAux comparer f tn.Left (filterAux comparer f tn.Right acc) + | _ -> if f t.Key then add comparer t.Key acc else acc + + let filter comparer f s = filterAux comparer f s empty + + let rec diffAux comparer (t:SetTree<'T>) acc = + if isEmpty acc then acc + else if isEmpty t then acc else match t with - | :? SetTreeNode<'T> as tn -> - let acc = if f tn.Key then add comparer tn.Key acc else acc - filterAux comparer f tn.Left (filterAux comparer f tn.Right acc) - | _ -> if f t.Key then add comparer t.Key acc else acc - - let filter comparer f s = filterAux comparer f s empty - - let rec diffAux comparer (t:SetTree<'T>) acc = - if isEmpty acc then acc - else - if isEmpty t then acc + | :? SetTreeNode<'T> as tn -> diffAux comparer tn.Left (diffAux comparer tn.Right (remove comparer tn.Key acc)) + | _ -> remove comparer t.Key acc + + let diff comparer a b = diffAux comparer b a + + let rec union comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = + // Perf: tried bruteForce for low heights, but nothing significant + if isEmpty t1 then t2 + elif isEmpty t2 then t1 + else + match t1 with + | :? SetTreeNode<'T> as t1n -> + match t2 with + | :? SetTreeNode<'T> as t2n -> // (t1l < k < t1r) AND (t2l < k2 < t2r) + // Divide and Conquer: + // Suppose t1 is largest. + // Split t2 using pivot k1 into lo and hi. + // Union disjoint subproblems and then combine. + if t1n.Height > t2n.Height then + let lo, _, hi = split comparer t1n.Key t2 in + balance comparer (union comparer t1n.Left lo) t1n.Key (union comparer t1n.Right hi) + else + let lo, _, hi = split comparer t2n.Key t1 in + balance comparer (union comparer t2n.Left lo) t2n.Key (union comparer t2n.Right hi) + | _ -> add comparer t2.Key t1 + | _ -> add comparer t1.Key t2 + + let rec intersectionAux comparer b (t:SetTree<'T>) acc = + if isEmpty t then acc + else + match t with + | :? SetTreeNode<'T> as tn -> + let acc = intersectionAux comparer b tn.Right acc + let acc = if contains comparer tn.Key b then add comparer tn.Key acc else acc + intersectionAux comparer b tn.Left acc + | _ -> + if contains comparer t.Key b then add comparer t.Key acc else acc + + let intersection comparer a b = intersectionAux comparer b a empty + + let partition1 comparer f k (acc1, acc2) = if f k then (add comparer k acc1, acc2) else (acc1, add comparer k acc2) + + let rec partitionAux comparer f (t:SetTree<'T>) acc = + if isEmpty t then acc + else + match t with + | :? SetTreeNode<'T> as tn -> + let acc = partitionAux comparer f tn.Right acc + let acc = partition1 comparer f tn.Key acc + partitionAux comparer f tn.Left acc + | _ -> partition1 comparer f t.Key acc + + let partition comparer f s = partitionAux comparer f s (empty, empty) + + let rec minimumElementAux (t:SetTree<'T>) n = + if isEmpty t then n + else + match t with + | :? SetTreeNode<'T> as tn -> minimumElementAux tn.Left tn.Key + | _ -> t.Key + + and minimumElementOpt (t:SetTree<'T>) = + if isEmpty t then None + else + match t with + | :? SetTreeNode<'T> as tn -> Some(minimumElementAux tn.Left tn.Key) + | _ -> Some t.Key + + and maximumElementAux (t:SetTree<'T>) n = + if isEmpty t then n + else + match t with + | :? SetTreeNode<'T> as tn -> maximumElementAux tn.Right tn.Key + | _ -> t.Key + + and maximumElementOpt (t:SetTree<'T>) = + if isEmpty t then None + else + match t with + | :? SetTreeNode<'T> as tn -> Some(maximumElementAux tn.Right tn.Key) + | _ -> Some t.Key + + let minimumElement s = + match minimumElementOpt s with + | Some(k) -> k + | None -> failwith "minimumElement" + + let maximumElement s = + match maximumElementOpt s with + | Some(k) -> k + | None -> failwith "maximumElement" + + //-------------------------------------------------------------------------- + // Imperative left-to-right iterators. + //-------------------------------------------------------------------------- + + type SetIterator<'T>(s:SetTree<'T>) = + + // collapseLHS: + // a) Always returns either [] or a list starting with SetOne. + // b) The "fringe" of the set stack is unchanged. + let rec collapseLHS (stack: SetTree<'T> list) = + match stack with + | [] -> [] + | x :: rest -> + if isEmpty x then collapseLHS rest else - match t with - | :? SetTreeNode<'T> as tn -> diffAux comparer tn.Left (diffAux comparer tn.Right (remove comparer tn.Key acc)) - | _ -> remove comparer t.Key acc - - let diff comparer a b = diffAux comparer b a + match x with + | :? SetTreeNode<'T> as xn-> collapseLHS (xn.Left :: SetTree xn.Key :: xn.Right :: rest) + | _ -> stack - let rec union comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = - // Perf: tried bruteForce for low heights, but nothing significant - if isEmpty t1 then t2 - elif isEmpty t2 then t1 - else - match t1 with - | :? SetTreeNode<'T> as t1n -> - match t2 with - | :? SetTreeNode<'T> as t2n -> // (t1l < k < t1r) AND (t2l < k2 < t2r) - // Divide and Conquer: - // Suppose t1 is largest. - // Split t2 using pivot k1 into lo and hi. - // Union disjoint subproblems and then combine. - if t1n.Height > t2n.Height then - let lo, _, hi = split comparer t1n.Key t2 in - balance comparer (union comparer t1n.Left lo) t1n.Key (union comparer t1n.Right hi) - else - let lo, _, hi = split comparer t2n.Key t1 in - balance comparer (union comparer t2n.Left lo) t2n.Key (union comparer t2n.Right hi) - | _ -> add comparer t2.Key t1 - | _ -> add comparer t1.Key t2 - - let rec intersectionAux comparer b (t:SetTree<'T>) acc = - if isEmpty t then acc - else - match t with - | :? SetTreeNode<'T> as tn -> - let acc = intersectionAux comparer b tn.Right acc - let acc = if contains comparer tn.Key b then add comparer tn.Key acc else acc - intersectionAux comparer b tn.Left acc - | _ -> - if contains comparer t.Key b then add comparer t.Key acc else acc - - let intersection comparer a b = intersectionAux comparer b a empty + // invariant: always collapseLHS result + let mutable stack = collapseLHS [s] + // true when MoveNext has been called + let mutable started = false - let partition1 comparer f k (acc1, acc2) = if f k then (add comparer k acc1, acc2) else (acc1, add comparer k acc2) + let notStarted() = raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) + let alreadyFinished() = raise (System.InvalidOperationException("Enumeration already finished.")) - let rec partitionAux comparer f (t:SetTree<'T>) acc = - if isEmpty t then acc - else - match t with - | :? SetTreeNode<'T> as tn -> - let acc = partitionAux comparer f tn.Right acc - let acc = partition1 comparer f tn.Key acc - partitionAux comparer f tn.Left acc - | _ -> partition1 comparer f t.Key acc - - let partition comparer f s = partitionAux comparer f s (empty, empty) - - let rec minimumElementAux (t:SetTree<'T>) n = - if isEmpty t then n - else - match t with - | :? SetTreeNode<'T> as tn -> minimumElementAux tn.Left tn.Key - | _ -> t.Key - - and minimumElementOpt (t:SetTree<'T>) = - if isEmpty t then None - else - match t with - | :? SetTreeNode<'T> as tn -> Some(minimumElementAux tn.Left tn.Key) - | _ -> Some t.Key - - and maximumElementAux (t:SetTree<'T>) n = - if isEmpty t then n - else - match t with - | :? SetTreeNode<'T> as tn -> maximumElementAux tn.Right tn.Key - | _ -> t.Key - - and maximumElementOpt (t:SetTree<'T>) = - if isEmpty t then None - else - match t with - | :? SetTreeNode<'T> as tn -> Some(maximumElementAux tn.Right tn.Key) - | _ -> Some t.Key - - let minimumElement s = - match minimumElementOpt s with - | Some(k) -> k - | None -> failwith "minimumElement" - - let maximumElement s = - match maximumElementOpt s with - | Some(k) -> k - | None -> failwith "maximumElement" - - //-------------------------------------------------------------------------- - // Imperative left-to-right iterators. - //-------------------------------------------------------------------------- - - type SetIterator<'T>(s:SetTree<'T>) = - - // collapseLHS: - // a) Always returns either [] or a list starting with SetOne. - // b) The "fringe" of the set stack is unchanged. - let rec collapseLHS (stack: SetTree<'T> list) = + member _.Current = + if started then match stack with - | [] -> [] - | x :: rest -> - if isEmpty x then collapseLHS rest - else - match x with - | :? SetTreeNode<'T> as xn-> collapseLHS (xn.Left :: SetTree xn.Key :: xn.Right :: rest) - | _ -> stack - - // invariant: always collapseLHS result - let mutable stack = collapseLHS [s] - // true when MoveNext has been called - let mutable started = false - - let notStarted() = raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) - let alreadyFinished() = raise (System.InvalidOperationException("Enumeration already finished.")) - - member _.Current = - if started then - match stack with - | k :: _ -> k.Key - | [] -> alreadyFinished() - else - notStarted() - - member _.MoveNext() = - if started then - match stack with - | [] -> false - | t :: rest -> - match t with - | :? SetTreeNode<'T> -> failwith "Please report error: Set iterator, unexpected stack for moveNext" - | _ -> - stack <- collapseLHS rest - not stack.IsEmpty - else - started <- true; // The first call to MoveNext "starts" the enumeration. - not stack.IsEmpty - - let toSeq s = - let mutable i = SetIterator s - { new IEnumerator<_> with - member _.Current = i.Current - interface System.Collections.IEnumerator with - member _.Current = box i.Current - member _.MoveNext() = i.MoveNext() - member _.Reset() = i <- SetIterator s - interface System.IDisposable with - member _.Dispose() = () } - - //-------------------------------------------------------------------------- - // Set comparison. This can be expensive. - //-------------------------------------------------------------------------- - - let rec compareStacks (comparer: IComparer<'T>) (l1:SetTree<'T> list) (l2:SetTree<'T> list) : int = - let cont() = - match l1, l2 with - | x1 :: t1, _ when not (isEmpty x1) -> - match x1 with - | :? SetTreeNode<'T> as x1n -> - compareStacks comparer (x1n.Left :: (SetTreeNode (x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2 - | _ -> compareStacks comparer (empty :: SetTree x1.Key :: t1) l2 - | _, x2 :: t2 when not (isEmpty x2) -> - match x2 with - | :? SetTreeNode<'T> as x2n -> - compareStacks comparer l1 (x2n.Left :: (SetTreeNode (x2n.Key, empty, x2n.Right, 0) :> SetTree<'T> ) :: t2) - | _ -> compareStacks comparer l1 (empty :: SetTree x2.Key :: t2) - | _ -> failwith "unexpected state in SetTree.compareStacks" - + | k :: _ -> k.Key + | [] -> alreadyFinished() + else + notStarted() + + member _.MoveNext() = + if started then + match stack with + | [] -> false + | t :: rest -> + match t with + | :? SetTreeNode<'T> -> failwith "Please report error: Set iterator, unexpected stack for moveNext" + | _ -> + stack <- collapseLHS rest + not stack.IsEmpty + else + started <- true; // The first call to MoveNext "starts" the enumeration. + not stack.IsEmpty + + let toSeq s = + let mutable i = SetIterator s + { new IEnumerator<_> with + member _.Current = i.Current + interface System.Collections.IEnumerator with + member _.Current = box i.Current + member _.MoveNext() = i.MoveNext() + member _.Reset() = i <- SetIterator s + interface System.IDisposable with + member _.Dispose() = () } + + //-------------------------------------------------------------------------- + // Set comparison. This can be expensive. + //-------------------------------------------------------------------------- + + let rec compareStacks (comparer: IComparer<'T>) (l1:SetTree<'T> list) (l2:SetTree<'T> list) : int = + let cont() = match l1, l2 with - | [], [] -> 0 - | [], _ -> -1 - | _, [] -> 1 - | x1 :: t1, x2 :: t2 -> - if isEmpty x1 then - if isEmpty x2 then compareStacks comparer t1 t2 - else cont() - elif isEmpty x2 then cont() - else - match x1 with - | :? SetTreeNode<'T> as x1n -> - if isEmpty x1n.Left then - match x2 with - | :? SetTreeNode<'T> as x2n -> - if isEmpty x2n.Left then - let c = comparer.Compare(x1n.Key, x2n.Key) - if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (x2n.Right :: t2) - else cont() - | _ -> - let c = comparer.Compare(x1n.Key, x2.Key) - if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (empty :: t2) - else cont() - | _ -> + | x1 :: t1, _ when not (isEmpty x1) -> + match x1 with + | :? SetTreeNode<'T> as x1n -> + compareStacks comparer (x1n.Left :: (SetTreeNode (x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2 + | _ -> compareStacks comparer (empty :: SetTree x1.Key :: t1) l2 + | _, x2 :: t2 when not (isEmpty x2) -> + match x2 with + | :? SetTreeNode<'T> as x2n -> + compareStacks comparer l1 (x2n.Left :: (SetTreeNode (x2n.Key, empty, x2n.Right, 0) :> SetTree<'T> ) :: t2) + | _ -> compareStacks comparer l1 (empty :: SetTree x2.Key :: t2) + | _ -> failwith "unexpected state in SetTree.compareStacks" + + match l1, l2 with + | [], [] -> 0 + | [], _ -> -1 + | _, [] -> 1 + | x1 :: t1, x2 :: t2 -> + if isEmpty x1 then + if isEmpty x2 then compareStacks comparer t1 t2 + else cont() + elif isEmpty x2 then cont() + else + match x1 with + | :? SetTreeNode<'T> as x1n -> + if isEmpty x1n.Left then match x2 with | :? SetTreeNode<'T> as x2n -> if isEmpty x2n.Left then - let c = comparer.Compare(x1.Key, x2n.Key) - if c <> 0 then c else compareStacks comparer (empty :: t1) (x2n.Right :: t2) + let c = comparer.Compare(x1n.Key, x2n.Key) + if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (x2n.Right :: t2) else cont() | _ -> - let c = comparer.Compare(x1.Key, x2.Key) - if c <> 0 then c else compareStacks comparer t1 t2 - - let compare comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = - if isEmpty t1 then - if isEmpty t2 then 0 - else -1 - else - if isEmpty t2 then 1 - else compareStacks comparer [t1] [t2] - - let choose s = minimumElement s - - let toList (t:SetTree<'T>) = - let rec loop (t':SetTree<'T>) acc = - if isEmpty t' then acc - else - match t' with - | :? SetTreeNode<'T> as tn -> loop tn.Left (tn.Key :: loop tn.Right acc) - | _ -> t'.Key :: acc - loop t [] - - let copyToArray s (arr: _[]) i = - let mutable j = i - iter (fun x -> arr[j] <- x; j <- j + 1) s - - let toArray s = - let n = (count s) - let res = Array.zeroCreate n - copyToArray s res 0; - res - - let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = - if e.MoveNext() then - mkFromEnumerator comparer (add comparer e.Current acc) e - else acc - - let ofSeq comparer (c : IEnumerable<_>) = - use ie = c.GetEnumerator() - mkFromEnumerator comparer empty ie - - let ofArray comparer l = Array.fold (fun acc k -> add comparer k acc) empty l - - - [] - [] - type internal Set<'T,'ComparerTag> when 'ComparerTag :> IComparer<'T>(comparer: IComparer<'T>, tree: SetTree<'T>) = - - static let refresh (s:Set<_,_>) t = Set<_,_>(comparer=s.Comparer, tree=t) - - member s.Tree = tree - member s.Comparer : IComparer<'T> = comparer - - static member Empty(comparer: 'ComparerTag) : Set<'T,'ComparerTag> = - Set<_,_>(comparer=comparer, tree=SetTree.empty) - - - member s.Add(x) : Set<'T,'ComparerTag> = refresh s (SetTree.add comparer x tree) - member s.Remove(x) : Set<'T,'ComparerTag> = refresh s (SetTree.remove comparer x tree) - member s.Count = SetTree.count tree - member s.Contains(x) = SetTree.contains comparer x tree - member s.Iterate(x) = SetTree.iter x tree - member s.Fold f x = SetTree.fold f tree x - member s.IsEmpty = SetTree.isEmpty tree - - member s.Partition predicate : Set<'T,'ComparerTag> * Set<'T,'ComparerTag> = - if SetTree.isEmpty s.Tree then s,s - else - let t1, t2 = SetTree.partition s.Comparer predicate s.Tree - refresh s t1, refresh s t2 - - member s.Filter predicate : Set<'T,'ComparerTag> = - if SetTree.isEmpty s.Tree then s - else - SetTree.filter comparer predicate tree |> refresh s - - member s.Exists predicate = SetTree.exists predicate tree - - member s.ForAll predicate = SetTree.forall predicate tree - - static member (-) (a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = Set<_,_>.Difference(a,b) - - static member (+) (a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = Set<_,_>.Union(a,b) - - static member Intersection(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = - if SetTree.isEmpty b.Tree then b (* A INTER 0 = 0 *) - else - if SetTree.isEmpty a.Tree then a (* 0 INTER B = 0 *) - else SetTree.intersection a.Comparer a.Tree b.Tree |> refresh a - - static member Union(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = - if SetTree.isEmpty b.Tree then a (* A U 0 = A *) - else - if SetTree.isEmpty a.Tree then b (* 0 U B = B *) - else SetTree.union a.Comparer a.Tree b.Tree |> refresh a - - static member Difference(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = - if SetTree.isEmpty a.Tree then a (* 0 - B = 0 *) + let c = comparer.Compare(x1n.Key, x2.Key) + if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (empty :: t2) + else cont() + | _ -> + match x2 with + | :? SetTreeNode<'T> as x2n -> + if isEmpty x2n.Left then + let c = comparer.Compare(x1.Key, x2n.Key) + if c <> 0 then c else compareStacks comparer (empty :: t1) (x2n.Right :: t2) + else cont() + | _ -> + let c = comparer.Compare(x1.Key, x2.Key) + if c <> 0 then c else compareStacks comparer t1 t2 + + let compare comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = + if isEmpty t1 then + if isEmpty t2 then 0 + else -1 + else + if isEmpty t2 then 1 + else compareStacks comparer [t1] [t2] + + let choose s = minimumElement s + + let toList (t:SetTree<'T>) = + let rec loop (t':SetTree<'T>) acc = + if isEmpty t' then acc else - if SetTree.isEmpty b.Tree then a (* A - 0 = A *) - else SetTree.diff a.Comparer a.Tree b.Tree |> refresh a - - static member Equality(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = - (SetTree.compare a.Comparer a.Tree b.Tree = 0) - - static member Compare(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = - SetTree.compare a.Comparer a.Tree b.Tree + match t' with + | :? SetTreeNode<'T> as tn -> loop tn.Left (tn.Key :: loop tn.Right acc) + | _ -> t'.Key :: acc + loop t [] - member s.Choose = SetTree.choose tree + let copyToArray s (arr: _[]) i = + let mutable j = i + iter (fun x -> arr[j] <- x; j <- j + 1) s - member s.MinimumElement = SetTree.minimumElement tree + let toArray s = + let n = (count s) + let res = Array.zeroCreate n + copyToArray s res 0; + res - member s.MaximumElement = SetTree.maximumElement tree + let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = + if e.MoveNext() then + mkFromEnumerator comparer (add comparer e.Current acc) e + else acc + + let ofSeq comparer (c : IEnumerable<_>) = + use ie = c.GetEnumerator() + mkFromEnumerator comparer empty ie - member s.IsSubsetOf((y: Set<'T,'ComparerTag>)) = SetTree.subset comparer tree y.Tree + let ofArray comparer l = Array.fold (fun acc k -> add comparer k acc) empty l - member s.IsSupersetOf((y: Set<'T,'ComparerTag>)) = SetTree.subset comparer y.Tree tree - member s.ToList () = SetTree.toList tree +[] +[] +type internal Set<'T,'ComparerTag> when 'ComparerTag :> IComparer<'T>(comparer: IComparer<'T>, tree: SetTree<'T>) = - member s.ToArray () = SetTree.toArray tree + static let refresh (s:Set<_,_>) t = Set<_,_>(comparer=s.Comparer, tree=t) - override this.Equals(that) = - match that with - // Cast to the exact same type as this, otherwise not equal. - | :? Set<'T,'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) - | _ -> false + member s.Tree = tree + member s.Comparer : IComparer<'T> = comparer - interface System.IComparable with - // Cast s2 to the exact same type as s1, see 4884. - // It is not OK to cast s2 to seq<'T>, since different compares could permute the elements. - member s1.CompareTo(s2: obj) = SetTree.compare s1.Comparer s1.Tree (s2 :?> Set<'T,'ComparerTag>).Tree + static member Empty(comparer: 'ComparerTag) : Set<'T,'ComparerTag> = + Set<_,_>(comparer=comparer, tree=SetTree.empty) - member this.ComputeHashCode() = - let combineHash x y = (x <<< 1) + y + 631 - let mutable res = 0 - for x in this do - res <- combineHash res (Unchecked.hash x) - res - override this.GetHashCode() = this.ComputeHashCode() - - interface ICollection<'T> with - member s.Add _ = raise (System.NotSupportedException("ReadOnlyCollection")) - member s.Clear() = raise (System.NotSupportedException("ReadOnlyCollection")) - member s.Remove _ = raise (System.NotSupportedException("ReadOnlyCollection")) - member s.Contains(x) = SetTree.contains comparer x tree - member s.CopyTo(arr,i) = SetTree.copyToArray tree arr i - member s.IsReadOnly = true - member s.Count = SetTree.count tree + member s.Add(x) : Set<'T,'ComparerTag> = refresh s (SetTree.add comparer x tree) + member s.Remove(x) : Set<'T,'ComparerTag> = refresh s (SetTree.remove comparer x tree) + member s.Count = SetTree.count tree + member s.Contains(x) = SetTree.contains comparer x tree + member s.Iterate(x) = SetTree.iter x tree + member s.Fold f x = SetTree.fold f tree x + member s.IsEmpty = SetTree.isEmpty tree - interface IEnumerable<'T> with - member s.GetEnumerator() = SetTree.toSeq tree + member s.Partition predicate : Set<'T,'ComparerTag> * Set<'T,'ComparerTag> = + if SetTree.isEmpty s.Tree then s,s + else + let t1, t2 = SetTree.partition s.Comparer predicate s.Tree + refresh s t1, refresh s t2 - interface System.Collections.IEnumerable with - override s.GetEnumerator() = (SetTree.toSeq tree :> System.Collections.IEnumerator) + member s.Filter predicate : Set<'T,'ComparerTag> = + if SetTree.isEmpty s.Tree then s + else + SetTree.filter comparer predicate tree |> refresh s - static member Singleton(comparer,x) : Set<'T,'ComparerTag> = - Set<_,_>.Empty(comparer).Add(x) + member s.Exists predicate = SetTree.exists predicate tree - static member Create(comparer : 'ComparerTag,l : seq<'T>) : Set<'T,'ComparerTag> = - Set<_,_>(comparer=comparer, tree=SetTree.ofSeq comparer l) + member s.ForAll predicate = SetTree.forall predicate tree + static member (-) (a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = Set<_,_>.Difference(a,b) - [] - [] - type internal MapTree<'Key, 'Value>(k: 'Key, v: 'Value) = - member _.Key = k - member _.Value = v + static member (+) (a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = Set<_,_>.Union(a,b) - [] - [] - [] - type internal MapTreeNode<'Key, 'Value>(k:'Key, v:'Value, left:MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) = - inherit MapTree<'Key,'Value>(k, v) + static member Intersection(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = + if SetTree.isEmpty b.Tree then b (* A INTER 0 = 0 *) + else + if SetTree.isEmpty a.Tree then a (* 0 INTER B = 0 *) + else SetTree.intersection a.Comparer a.Tree b.Tree |> refresh a + + static member Union(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = + if SetTree.isEmpty b.Tree then a (* A U 0 = A *) + else + if SetTree.isEmpty a.Tree then b (* 0 U B = B *) + else SetTree.union a.Comparer a.Tree b.Tree |> refresh a - member _.Left = left - member _.Right = right - member _.Height = h + static member Difference(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = + if SetTree.isEmpty a.Tree then a (* 0 - B = 0 *) + else + if SetTree.isEmpty b.Tree then a (* A - 0 = A *) + else SetTree.diff a.Comparer a.Tree b.Tree |> refresh a + static member Equality(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = + (SetTree.compare a.Comparer a.Tree b.Tree = 0) - [] - module MapTree = + static member Compare(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = + SetTree.compare a.Comparer a.Tree b.Tree - let empty = null + member s.Choose = SetTree.choose tree - let inline isEmpty (m:MapTree<'Key, 'Value>) = isNull m - - let rec sizeAux acc (m:MapTree<'Key, 'Value>) = - if isEmpty m then - acc - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> sizeAux (sizeAux (acc+1) mn.Left) mn.Right - | _ -> acc + 1 + member s.MinimumElement = SetTree.minimumElement tree - let size x = sizeAux 0 x + member s.MaximumElement = SetTree.maximumElement tree - let inline height (m: MapTree<'Key, 'Value>) = - if isEmpty m then 0 - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> mn.Height - | _ -> 1 - - let mk l k v r : MapTree<'Key, 'Value> = - let hl = height l - let hr = height r - let m = max hl hr - if m = 0 then // m=0 ~ isEmpty l && isEmpty r - MapTree(k,v) - else - MapTreeNode(k,v,l,r,m+1) :> MapTree<'Key, 'Value> - - let inline private asNode(value:MapTree<'Key,'Value>) : MapTreeNode<'Key,'Value> = - value :?> MapTreeNode<'Key,'Value> - - let rebalance t1 (k: 'Key) (v: 'Value) t2 : MapTree<'Key, 'Value> = - let t1h = height t1 - let t2h = height t2 - if t2h > t1h + 2 then (* right is heavier than left *) - let t2' = asNode(t2) - (* one of the nodes must have height > height t1 + 1 *) - if height t2'.Left > t1h + 1 then (* balance left: combination *) - let t2l = asNode(t2'.Left) - mk (mk t1 k v t2l.Left) t2l.Key t2l.Value (mk t2l.Right t2'.Key t2'.Value t2'.Right) - else (* rotate left *) - mk (mk t1 k v t2'.Left) t2'.Key t2'.Value t2'.Right - else - if t1h > t2h + 2 then (* left is heavier than right *) - let t1' = asNode(t1) - (* one of the nodes must have height > height t2 + 1 *) - if height t1'.Right > t2h + 1 then - (* balance right: combination *) - let t1r = asNode(t1'.Right) - mk (mk t1'.Left t1'.Key t1'.Value t1r.Left) t1r.Key t1r.Value (mk t1r.Right k v t2) - else - mk t1'.Left t1'.Key t1'.Value (mk t1'.Right k v t2) - else mk t1 k v t2 + member s.IsSubsetOf((y: Set<'T,'ComparerTag>)) = SetTree.subset comparer tree y.Tree + member s.IsSupersetOf((y: Set<'T,'ComparerTag>)) = SetTree.subset comparer y.Tree tree - let rec add (comparer: IComparer<'Key>) k (v: 'Value) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> = - if isEmpty m then MapTree(k,v) - else - let c = comparer.Compare(k,m.Key) - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - if c < 0 then rebalance (add comparer k v mn.Left) mn.Key mn.Value mn.Right - elif c = 0 then MapTreeNode(k,v,mn.Left,mn.Right,mn.Height) :> MapTree<'Key, 'Value> - else rebalance mn.Left mn.Key mn.Value (add comparer k v mn.Right) - | _ -> - if c < 0 then MapTreeNode (k,v,empty,m,2) :> MapTree<'Key, 'Value> - elif c = 0 then MapTree(k,v) - else MapTreeNode (k,v,m,empty,2) :> MapTree<'Key, 'Value> + member s.ToList () = SetTree.toList tree - let indexNotFound() = raise (KeyNotFoundException("An index satisfying the predicate was not found in the collection")) + member s.ToArray () = SetTree.toArray tree - let rec tryGetValue (comparer: IComparer<'Key>) k (v: byref<'Value>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then false - else - let c = comparer.Compare(k, m.Key) - if c = 0 then v <- m.Value; true - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - tryGetValue comparer k &v (if c < 0 then mn.Left else mn.Right) - | _ -> false - - let find (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = - let mutable v = Unchecked.defaultof<'Value> - if tryGetValue comparer k &v m then - v - else - indexNotFound() + override this.Equals(that) = + match that with + // Cast to the exact same type as this, otherwise not equal. + | :? Set<'T,'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) + | _ -> false - let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = - let mutable v = Unchecked.defaultof<'Value> - if tryGetValue comparer k &v m then - Some v - else - None + interface System.IComparable with + // Cast s2 to the exact same type as s1, see 4884. + // It is not OK to cast s2 to seq<'T>, since different compares could permute the elements. + member s1.CompareTo(s2: obj) = SetTree.compare s1.Comparer s1.Tree (s2 :?> Set<'T,'ComparerTag>).Tree - let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) = - if f.Invoke (k, v) then (add comparer k v acc1, acc2) else (acc1, add comparer k v acc2) + member this.ComputeHashCode() = + let combineHash x y = (x <<< 1) + y + 631 + let mutable res = 0 + for x in this do + res <- combineHash res (Unchecked.hash x) + res - let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = - if isEmpty m then acc - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - let acc = partitionAux comparer f mn.Right acc - let acc = partition1 comparer f mn.Key mn.Value acc - partitionAux comparer f mn.Left acc - | _ -> partition1 comparer f m.Key m.Value acc + override this.GetHashCode() = this.ComputeHashCode() + + interface ICollection<'T> with + member s.Add _ = raise (System.NotSupportedException("ReadOnlyCollection")) + member s.Clear() = raise (System.NotSupportedException("ReadOnlyCollection")) + member s.Remove _ = raise (System.NotSupportedException("ReadOnlyCollection")) + member s.Contains(x) = SetTree.contains comparer x tree + member s.CopyTo(arr,i) = SetTree.copyToArray tree arr i + member s.IsReadOnly = true + member s.Count = SetTree.count tree - let partition (comparer: IComparer<'Key>) f m = - partitionAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m (empty, empty) + interface IEnumerable<'T> with + member s.GetEnumerator() = SetTree.toSeq tree - let filter1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v acc = - if f.Invoke (k, v) then add comparer k v acc else acc + interface System.Collections.IEnumerable with + override s.GetEnumerator() = (SetTree.toSeq tree :> System.Collections.IEnumerator) - let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = - if isEmpty m then acc - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - let acc = filterAux comparer f mn.Left acc - let acc = filter1 comparer f mn.Key mn.Value acc - filterAux comparer f mn.Right acc - | _ -> filter1 comparer f m.Key m.Value acc + static member Singleton(comparer,x) : Set<'T,'ComparerTag> = + Set<_,_>.Empty(comparer).Add(x) - let filter (comparer: IComparer<'Key>) f m = - filterAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m empty + static member Create(comparer : 'ComparerTag,l : seq<'T>) : Set<'T,'ComparerTag> = + Set<_,_>(comparer=comparer, tree=SetTree.ofSeq comparer l) - let rec spliceOutSuccessor (m: MapTree<'Key, 'Value>) = - if isEmpty m then failwith "internal error: Map.spliceOutSuccessor" - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - if isEmpty mn.Left then mn.Key, mn.Value, mn.Right - else let k3, v3, l' = spliceOutSuccessor mn.Left in k3, v3, mk l' mn.Key mn.Value mn.Right - | _ -> m.Key, m.Value, empty - let rec remove (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = - if isEmpty m then empty - else - let c = comparer.Compare(k, m.Key) - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - if c < 0 then rebalance (remove comparer k mn.Left) mn.Key mn.Value mn.Right - elif c = 0 then - if isEmpty mn.Left then mn.Right - elif isEmpty mn.Right then mn.Left - else - let sk, sv, r' = spliceOutSuccessor mn.Right - mk mn.Left sk sv r' - else rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right) - | _ -> - if c = 0 then empty else m +[] +[] +type internal MapTree<'Key, 'Value>(k: 'Key, v: 'Value) = + member _.Key = k + member _.Value = v - let rec mem (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = - if isEmpty m then false - else - let c = comparer.Compare(k, m.Key) - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - if c < 0 then mem comparer k mn.Left - else (c = 0 || mem comparer k mn.Right) - | _ -> c = 0 +[] +[] +[] +type internal MapTreeNode<'Key, 'Value>(k:'Key, v:'Value, left:MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) = + inherit MapTree<'Key,'Value>(k, v) - let rec iterOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then () - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> iterOpt f mn.Left; f.Invoke (mn.Key, mn.Value); iterOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) + member _.Left = left + member _.Right = right + member _.Height = h - let iter f m = - iterOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - let rec tryPickOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then None - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - match tryPickOpt f mn.Left with - | Some _ as res -> res - | None -> - match f.Invoke (mn.Key, mn.Value) with - | Some _ as res -> res - | None -> - tryPickOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) - - let tryPick f m = - tryPickOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - - let rec existsOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then false - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> existsOpt f mn.Left || f.Invoke (mn.Key, mn.Value) || existsOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) +[] +module MapTree = - let exists f m = - existsOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m + let empty = null - let rec forallOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then true - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> forallOpt f mn.Left && f.Invoke (mn.Key, mn.Value) && forallOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) + let inline isEmpty (m:MapTree<'Key, 'Value>) = isNull m - - let forall f m = - forallOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - - let rec map (f:'Value -> 'Result) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Result> = - if isEmpty m then empty - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - let l2 = map f mn.Left - let v2 = f mn.Value - let r2 = map f mn.Right - MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> - | _ -> MapTree (m.Key, f m.Value) - - let rec mapiOpt (f: OptimizedClosures.FSharpFunc<'Key, 'Value, 'Result>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then empty + let rec sizeAux acc (m:MapTree<'Key, 'Value>) = + if isEmpty m then + acc + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> sizeAux (sizeAux (acc+1) mn.Left) mn.Right + | _ -> acc + 1 + + let size x = sizeAux 0 x + + let inline height (m: MapTree<'Key, 'Value>) = + if isEmpty m then 0 + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> mn.Height + | _ -> 1 + + let mk l k v r : MapTree<'Key, 'Value> = + let hl = height l + let hr = height r + let m = max hl hr + if m = 0 then // m=0 ~ isEmpty l && isEmpty r + MapTree(k,v) + else + MapTreeNode(k,v,l,r,m+1) :> MapTree<'Key, 'Value> + + let inline private asNode(value:MapTree<'Key,'Value>) : MapTreeNode<'Key,'Value> = + value :?> MapTreeNode<'Key,'Value> + + let rebalance t1 (k: 'Key) (v: 'Value) t2 : MapTree<'Key, 'Value> = + let t1h = height t1 + let t2h = height t2 + if t2h > t1h + 2 then (* right is heavier than left *) + let t2' = asNode(t2) + (* one of the nodes must have height > height t1 + 1 *) + if height t2'.Left > t1h + 1 then (* balance left: combination *) + let t2l = asNode(t2'.Left) + mk (mk t1 k v t2l.Left) t2l.Key t2l.Value (mk t2l.Right t2'.Key t2'.Value t2'.Right) + else (* rotate left *) + mk (mk t1 k v t2'.Left) t2'.Key t2'.Value t2'.Right + else + if t1h > t2h + 2 then (* left is heavier than right *) + let t1' = asNode(t1) + (* one of the nodes must have height > height t2 + 1 *) + if height t1'.Right > t2h + 1 then + (* balance right: combination *) + let t1r = asNode(t1'.Right) + mk (mk t1'.Left t1'.Key t1'.Value t1r.Left) t1r.Key t1r.Value (mk t1r.Right k v t2) + else + mk t1'.Left t1'.Key t1'.Value (mk t1'.Right k v t2) + else mk t1 k v t2 + + + let rec add (comparer: IComparer<'Key>) k (v: 'Value) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> = + if isEmpty m then MapTree(k,v) + else + let c = comparer.Compare(k,m.Key) + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + if c < 0 then rebalance (add comparer k v mn.Left) mn.Key mn.Value mn.Right + elif c = 0 then MapTreeNode(k,v,mn.Left,mn.Right,mn.Height) :> MapTree<'Key, 'Value> + else rebalance mn.Left mn.Key mn.Value (add comparer k v mn.Right) + | _ -> + if c < 0 then MapTreeNode (k,v,empty,m,2) :> MapTree<'Key, 'Value> + elif c = 0 then MapTree(k,v) + else MapTreeNode (k,v,m,empty,2) :> MapTree<'Key, 'Value> + + let indexNotFound() = raise (KeyNotFoundException("An index satisfying the predicate was not found in the collection")) + + let rec tryGetValue (comparer: IComparer<'Key>) k (v: byref<'Value>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then false + else + let c = comparer.Compare(k, m.Key) + if c = 0 then v <- m.Value; true else match m with | :? MapTreeNode<'Key, 'Value> as mn -> - let l2 = mapiOpt f mn.Left - let v2 = f.Invoke (mn.Key, mn.Value) - let r2 = mapiOpt f mn.Right - MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> - | _ -> MapTree (m.Key, f.Invoke (m.Key, m.Value)) - - let mapi f m = - mapiOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - - // Fold, right-to-left. - // - // NOTE: This differs from the behaviour of Set.fold which folds left-to-right. + tryGetValue comparer k &v (if c < 0 then mn.Left else mn.Right) + | _ -> false + + let find (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + let mutable v = Unchecked.defaultof<'Value> + if tryGetValue comparer k &v m then + v + else + indexNotFound() + + let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + let mutable v = Unchecked.defaultof<'Value> + if tryGetValue comparer k &v m then + Some v + else + None + + let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) = + if f.Invoke (k, v) then (add comparer k v acc1, acc2) else (acc1, add comparer k v acc2) + + let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = + if isEmpty m then acc + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let acc = partitionAux comparer f mn.Right acc + let acc = partition1 comparer f mn.Key mn.Value acc + partitionAux comparer f mn.Left acc + | _ -> partition1 comparer f m.Key m.Value acc + + let partition (comparer: IComparer<'Key>) f m = + partitionAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m (empty, empty) + + let filter1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v acc = + if f.Invoke (k, v) then add comparer k v acc else acc + + let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = + if isEmpty m then acc + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let acc = filterAux comparer f mn.Left acc + let acc = filter1 comparer f mn.Key mn.Value acc + filterAux comparer f mn.Right acc + | _ -> filter1 comparer f m.Key m.Value acc + + let filter (comparer: IComparer<'Key>) f m = + filterAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m empty + + let rec spliceOutSuccessor (m: MapTree<'Key, 'Value>) = + if isEmpty m then failwith "internal error: Map.spliceOutSuccessor" + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + if isEmpty mn.Left then mn.Key, mn.Value, mn.Right + else let k3, v3, l' = spliceOutSuccessor mn.Left in k3, v3, mk l' mn.Key mn.Value mn.Right + | _ -> m.Key, m.Value, empty + + let rec remove (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + if isEmpty m then empty + else + let c = comparer.Compare(k, m.Key) + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + if c < 0 then rebalance (remove comparer k mn.Left) mn.Key mn.Value mn.Right + elif c = 0 then + if isEmpty mn.Left then mn.Right + elif isEmpty mn.Right then mn.Left + else + let sk, sv, r' = spliceOutSuccessor mn.Right + mk mn.Left sk sv r' + else rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right) + | _ -> + if c = 0 then empty else m + + let rec mem (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + if isEmpty m then false + else + let c = comparer.Compare(k, m.Key) + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + if c < 0 then mem comparer k mn.Left + else (c = 0 || mem comparer k mn.Right) + | _ -> c = 0 + + let rec iterOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then () + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> iterOpt f mn.Left; f.Invoke (mn.Key, mn.Value); iterOpt f mn.Right + | _ -> f.Invoke (m.Key, m.Value) + + let iter f m = + iterOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m + + let rec tryPickOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then None + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + match tryPickOpt f mn.Left with + | Some _ as res -> res + | None -> + match f.Invoke (mn.Key, mn.Value) with + | Some _ as res -> res + | None -> + tryPickOpt f mn.Right + | _ -> f.Invoke (m.Key, m.Value) + + let tryPick f m = + tryPickOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m + + let rec existsOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then false + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> existsOpt f mn.Left || f.Invoke (mn.Key, mn.Value) || existsOpt f mn.Right + | _ -> f.Invoke (m.Key, m.Value) + + let exists f m = + existsOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m + + let rec forallOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then true + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> forallOpt f mn.Left && f.Invoke (mn.Key, mn.Value) && forallOpt f mn.Right + | _ -> f.Invoke (m.Key, m.Value) + - let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = + let forall f m = + forallOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m + + let rec map (f:'Value -> 'Result) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Result> = + if isEmpty m then empty + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let l2 = map f mn.Left + let v2 = f mn.Value + let r2 = map f mn.Right + MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> + | _ -> MapTree (m.Key, f m.Value) + + let rec mapiOpt (f: OptimizedClosures.FSharpFunc<'Key, 'Value, 'Result>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then empty + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let l2 = mapiOpt f mn.Left + let v2 = f.Invoke (mn.Key, mn.Value) + let r2 = mapiOpt f mn.Right + MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> + | _ -> MapTree (m.Key, f.Invoke (m.Key, m.Value)) + + let mapi f m = + mapiOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m + + // Fold, right-to-left. + // + // NOTE: This differs from the behaviour of Set.fold which folds left-to-right. + + let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = + if isEmpty m then x + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let x = foldBackOpt f mn.Right x + let x = f.Invoke (mn.Key, mn.Value, x) + foldBackOpt f mn.Left x + | _ -> f.Invoke (m.Key, m.Value, x) + + let foldBack f m x = + foldBackOpt (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x + + let foldSectionOpt (comparer: IComparer<'Key>) lo hi (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = + let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = if isEmpty m then x else match m with | :? MapTreeNode<'Key, 'Value> as mn -> - let x = foldBackOpt f mn.Right x - let x = f.Invoke (mn.Key, mn.Value, x) - foldBackOpt f mn.Left x - | _ -> f.Invoke (m.Key, m.Value, x) - - let foldBack f m x = - foldBackOpt (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x - - let foldSectionOpt (comparer: IComparer<'Key>) lo hi (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = - let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = - if isEmpty m then x + let cLoKey = comparer.Compare(lo, mn.Key) + let cKeyHi = comparer.Compare(mn.Key, hi) + let x = if cLoKey < 0 then foldFromTo f mn.Left x else x + let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (mn.Key, mn.Value, x) else x + let x = if cKeyHi < 0 then foldFromTo f mn.Right x else x + x + | _ -> + let cLoKey = comparer.Compare(lo, m.Key) + let cKeyHi = comparer.Compare(m.Key, hi) + let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (m.Key, m.Value, x) else x + x + + if comparer.Compare(lo, hi) = 1 then x else foldFromTo f m x + + let foldSection (comparer: IComparer<'Key>) lo hi f m x = + foldSectionOpt comparer lo hi (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x + + let rec foldMapOpt (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) z acc = + if isEmpty m then acc,z + else + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let acc,z = foldMapOpt comparer f mn.Right z acc + let v',z = f.Invoke(mn.Key, mn.Value, z) + let acc = add comparer mn.Key v' acc + foldMapOpt comparer f mn.Left z acc + | _ -> + let v',z = f.Invoke(m.Key, m.Value, z) + add comparer m.Key v' acc,z + + let foldMap (comparer: IComparer<'Key>) f (m: MapTree<'Key, 'Value>) z acc = + foldMapOpt comparer (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m z acc + + let toList m = foldBack (fun k v acc -> (k,v) :: acc) m [] + let toArray m = m |> toList |> Array.ofList + let ofList comparer l = List.fold (fun acc (k,v) -> add comparer k v acc) empty l + + let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = + if e.MoveNext() then + let x,y = e.Current + mkFromEnumerator comparer (add comparer x y acc) e + else acc + + let ofSeq comparer (c : seq<_>) = + use ie = c.GetEnumerator() + mkFromEnumerator comparer empty ie + + let copyToArray s (arr: _[]) i = + let mutable j = i + s |> iter (fun x y -> arr[j] <- KeyValuePair(x,y); j <- j + 1) + + + /// Imperative left-to-right iterators. + type MapIterator<'Key,'Value>(s:MapTree<'Key,'Value>) = + // collapseLHS: + // a) Always returns either [] or a list starting with SetOne. + // b) The "fringe" of the set stack is unchanged. + let rec collapseLHS (stack:MapTree<'Key, 'Value> list) = + match stack with + | [] -> [] + | m :: rest -> + if isEmpty m then collapseLHS rest else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - let cLoKey = comparer.Compare(lo, mn.Key) - let cKeyHi = comparer.Compare(mn.Key, hi) - let x = if cLoKey < 0 then foldFromTo f mn.Left x else x - let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (mn.Key, mn.Value, x) else x - let x = if cKeyHi < 0 then foldFromTo f mn.Right x else x - x - | _ -> - let cLoKey = comparer.Compare(lo, m.Key) - let cKeyHi = comparer.Compare(m.Key, hi) - let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (m.Key, m.Value, x) else x - x + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> collapseLHS (mn.Left :: MapTree (mn.Key, mn.Value) :: mn.Right :: rest) + | _ -> stack - if comparer.Compare(lo, hi) = 1 then x else foldFromTo f m x + /// invariant: always collapseLHS result + let mutable stack = collapseLHS [s] + /// true when MoveNext has been called + let mutable started = false - let foldSection (comparer: IComparer<'Key>) lo hi f m x = - foldSectionOpt comparer lo hi (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x + let notStarted() = raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) + let alreadyFinished() = raise (System.InvalidOperationException("Enumeration already finished.")) - let rec foldMapOpt (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) z acc = - if isEmpty m then acc,z + member _.Current = + if started then + match stack with + | [] -> alreadyFinished() + | m :: _ -> + match m with + | :? MapTreeNode<'Key, 'Value> -> failwith "Please report error: Map iterator, unexpected stack for current" + | _ -> KeyValuePair<_, _>(m.Key, m.Value) else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - let acc,z = foldMapOpt comparer f mn.Right z acc - let v',z = f.Invoke(mn.Key, mn.Value, z) - let acc = add comparer mn.Key v' acc - foldMapOpt comparer f mn.Left z acc - | _ -> - let v',z = f.Invoke(m.Key, m.Value, z) - add comparer m.Key v' acc,z - - let foldMap (comparer: IComparer<'Key>) f (m: MapTree<'Key, 'Value>) z acc = - foldMapOpt comparer (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m z acc - - let toList m = foldBack (fun k v acc -> (k,v) :: acc) m [] - let toArray m = m |> toList |> Array.ofList - let ofList comparer l = List.fold (fun acc (k,v) -> add comparer k v acc) empty l - - let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = - if e.MoveNext() then - let x,y = e.Current - mkFromEnumerator comparer (add comparer x y acc) e - else acc - - let ofSeq comparer (c : seq<_>) = - use ie = c.GetEnumerator() - mkFromEnumerator comparer empty ie - - let copyToArray s (arr: _[]) i = - let mutable j = i - s |> iter (fun x y -> arr[j] <- KeyValuePair(x,y); j <- j + 1) - - - /// Imperative left-to-right iterators. - type MapIterator<'Key,'Value>(s:MapTree<'Key,'Value>) = - // collapseLHS: - // a) Always returns either [] or a list starting with SetOne. - // b) The "fringe" of the set stack is unchanged. - let rec collapseLHS (stack:MapTree<'Key, 'Value> list) = + notStarted() + + member _.MoveNext() = + if started then match stack with - | [] -> [] + | [] -> false | m :: rest -> - if isEmpty m then collapseLHS rest - else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> collapseLHS (mn.Left :: MapTree (mn.Key, mn.Value) :: mn.Right :: rest) - | _ -> stack - - /// invariant: always collapseLHS result - let mutable stack = collapseLHS [s] - /// true when MoveNext has been called - let mutable started = false - - let notStarted() = raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) - let alreadyFinished() = raise (System.InvalidOperationException("Enumeration already finished.")) - - member _.Current = - if started then - match stack with - | [] -> alreadyFinished() - | m :: _ -> - match m with - | :? MapTreeNode<'Key, 'Value> -> failwith "Please report error: Map iterator, unexpected stack for current" - | _ -> KeyValuePair<_, _>(m.Key, m.Value) - else - notStarted() - - member _.MoveNext() = - if started then - match stack with - | [] -> false - | m :: rest -> - match m with - | :? MapTreeNode<'Key, 'Value> -> failwith "Please report error: Map iterator, unexpected stack for moveNext" - | _ -> - stack <- collapseLHS rest - not stack.IsEmpty - else - started <- true (* The first call to MoveNext "starts" the enumeration. *) - not stack.IsEmpty - - let toSeq s = - let mutable i = MapIterator(s) - { new IEnumerator<_> with - member self.Current = i.Current - interface System.Collections.IEnumerator with - member self.Current = box i.Current - member self.MoveNext() = i.MoveNext() - member self.Reset() = i <- MapIterator(s) - interface System.IDisposable with - member self.Dispose() = ()} - - - [] - [] - type internal Map<'Key,'T,'ComparerTag> when 'ComparerTag :> IComparer<'Key>( comparer: IComparer<'Key>, tree: MapTree<'Key,'T>) = - - static let refresh (m:Map<_,_,'ComparerTag>) t = - Map<_,_,'ComparerTag>(comparer=m.Comparer, tree=t) - - member s.Tree = tree - member s.Comparer : IComparer<'Key> = comparer - - static member Empty(comparer : 'ComparerTag) = Map<'Key,'T,'ComparerTag>(comparer=comparer, tree=MapTree.empty) - member m.Add(k,v) = refresh m (MapTree.add comparer k v tree) - member m.IsEmpty = MapTree.isEmpty tree - member m.Item with get(k : 'Key) = MapTree.find comparer k tree - member m.First(f) = MapTree.tryPick f tree - member m.Exists(f) = MapTree.exists f tree - member m.Filter(f) = MapTree.filter comparer f tree |> refresh m - member m.ForAll(f) = MapTree.forall f tree - member m.Fold folder acc = MapTree.foldBack folder tree acc - member m.FoldSection lo hi f acc = MapTree.foldSection comparer lo hi f tree acc - member m.FoldAndMap f z = - let tree,z = MapTree.foldMap comparer f tree z MapTree.empty - refresh m tree, z - member m.Iterate action = MapTree.iter action tree - member m.MapRange mapping = refresh m (MapTree.map mapping tree) - member m.Map mapping = refresh m (MapTree.mapi mapping tree) - member m.Partition(f) = - let r1,r2 = MapTree.partition comparer f tree - refresh m r1, refresh m r2 - member m.Count = MapTree.size tree - member m.ContainsKey(k) = MapTree.mem comparer k tree - member m.Remove(k) = refresh m (MapTree.remove comparer k tree) - member m.TryFind(k) = MapTree.tryFind comparer k tree - member m.ToList() = MapTree.toList tree - member m.ToArray() = MapTree.toArray tree - - static member FromList(comparer : 'ComparerTag,l) : Map<'Key,'T,'ComparerTag> = - Map<_,_,_>(comparer=comparer, tree=MapTree.ofList comparer l) - - static member Create(comparer : 'ComparerTag, ie : seq<_>) : Map<'Key,'T,'ComparerTag> = - Map<_,_,_>(comparer=comparer, tree=MapTree.ofSeq comparer ie) - - interface IEnumerable> with - member s.GetEnumerator() = MapTree.toSeq tree - - interface System.Collections.IEnumerable with - override s.GetEnumerator() = (MapTree.toSeq tree :> System.Collections.IEnumerator) - - override this.Equals(that) = - match that with - // Cast to the exact same type as this, otherwise not equal. - | :? Map<'Key,'T,'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) - | _ -> false - - interface System.IComparable with - member m1.CompareTo(m2: obj) = - Seq.compareWith - (fun (kvp1 : KeyValuePair<_,_>) (kvp2 : KeyValuePair<_,_>)-> - let c = m1.Comparer.Compare(kvp1.Key,kvp2.Key) in - if c <> 0 then c else Unchecked.compare kvp1.Value kvp2.Value) - // Cast m2 to the exact same type as m1, see 4884. - // It is not OK to cast m2 to seq>, since different compares could permute the KVPs. - m1 (m2 :?> Map<'Key,'T,'ComparerTag>) - - member this.ComputeHashCode() = - let combineHash x y = (x <<< 1) + y + 631 - let mutable res = 0 - for KeyValue(x,y) in this do - res <- combineHash res (Unchecked.hash x) - res <- combineHash res (Unchecked.hash y) - res - - override this.GetHashCode() = this.ComputeHashCode() - - - type internal Map<'Key,'T> = Map<'Key, 'T, IComparer<'Key>> - type internal Set<'T> = Set<'T, IComparer<'T>> + match m with + | :? MapTreeNode<'Key, 'Value> -> failwith "Please report error: Map iterator, unexpected stack for moveNext" + | _ -> + stack <- collapseLHS rest + not stack.IsEmpty + else + started <- true (* The first call to MoveNext "starts" the enumeration. *) + not stack.IsEmpty + + let toSeq s = + let mutable i = MapIterator(s) + { new IEnumerator<_> with + member self.Current = i.Current + interface System.Collections.IEnumerator with + member self.Current = box i.Current + member self.MoveNext() = i.MoveNext() + member self.Reset() = i <- MapIterator(s) + interface System.IDisposable with + member self.Dispose() = ()} + + +[] +[] +type internal Map<'Key,'T,'ComparerTag> when 'ComparerTag :> IComparer<'Key>( comparer: IComparer<'Key>, tree: MapTree<'Key,'T>) = + + static let refresh (m:Map<_,_,'ComparerTag>) t = + Map<_,_,'ComparerTag>(comparer=m.Comparer, tree=t) + + member s.Tree = tree + member s.Comparer : IComparer<'Key> = comparer + + static member Empty(comparer : 'ComparerTag) = Map<'Key,'T,'ComparerTag>(comparer=comparer, tree=MapTree.empty) + member m.Add(k,v) = refresh m (MapTree.add comparer k v tree) + member m.IsEmpty = MapTree.isEmpty tree + member m.Item with get(k : 'Key) = MapTree.find comparer k tree + member m.First(f) = MapTree.tryPick f tree + member m.Exists(f) = MapTree.exists f tree + member m.Filter(f) = MapTree.filter comparer f tree |> refresh m + member m.ForAll(f) = MapTree.forall f tree + member m.Fold folder acc = MapTree.foldBack folder tree acc + member m.FoldSection lo hi f acc = MapTree.foldSection comparer lo hi f tree acc + member m.FoldAndMap f z = + let tree,z = MapTree.foldMap comparer f tree z MapTree.empty + refresh m tree, z + member m.Iterate action = MapTree.iter action tree + member m.MapRange mapping = refresh m (MapTree.map mapping tree) + member m.Map mapping = refresh m (MapTree.mapi mapping tree) + member m.Partition(f) = + let r1,r2 = MapTree.partition comparer f tree + refresh m r1, refresh m r2 + member m.Count = MapTree.size tree + member m.ContainsKey(k) = MapTree.mem comparer k tree + member m.Remove(k) = refresh m (MapTree.remove comparer k tree) + member m.TryFind(k) = MapTree.tryFind comparer k tree + member m.ToList() = MapTree.toList tree + member m.ToArray() = MapTree.toArray tree + + static member FromList(comparer : 'ComparerTag,l) : Map<'Key,'T,'ComparerTag> = + Map<_,_,_>(comparer=comparer, tree=MapTree.ofList comparer l) + + static member Create(comparer : 'ComparerTag, ie : seq<_>) : Map<'Key,'T,'ComparerTag> = + Map<_,_,_>(comparer=comparer, tree=MapTree.ofSeq comparer ie) + + interface IEnumerable> with + member s.GetEnumerator() = MapTree.toSeq tree + + interface System.Collections.IEnumerable with + override s.GetEnumerator() = (MapTree.toSeq tree :> System.Collections.IEnumerator) + + override this.Equals(that) = + match that with + // Cast to the exact same type as this, otherwise not equal. + | :? Map<'Key,'T,'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) + | _ -> false + + interface System.IComparable with + member m1.CompareTo(m2: obj) = + Seq.compareWith + (fun (kvp1 : KeyValuePair<_,_>) (kvp2 : KeyValuePair<_,_>)-> + let c = m1.Comparer.Compare(kvp1.Key,kvp2.Key) in + if c <> 0 then c else Unchecked.compare kvp1.Value kvp2.Value) + // Cast m2 to the exact same type as m1, see 4884. + // It is not OK to cast m2 to seq>, since different compares could permute the KVPs. + m1 (m2 :?> Map<'Key,'T,'ComparerTag>) + + member this.ComputeHashCode() = + let combineHash x y = (x <<< 1) + y + 631 + let mutable res = 0 + for KeyValue(x,y) in this do + res <- combineHash res (Unchecked.hash x) + res <- combineHash res (Unchecked.hash y) + res + + override this.GetHashCode() = this.ComputeHashCode() + +type internal Map<'Key,'T> = Map<'Key, 'T, IComparer<'Key>> + +type internal Set<'T> = Set<'T, IComparer<'T>> diff --git a/src/Compiler/Utilities/sr.fs b/src/Compiler/Utilities/sr.fs index 43baf2d7a66..dcb5f03e09c 100644 --- a/src/Compiler/Utilities/sr.fs +++ b/src/Compiler/Utilities/sr.fs @@ -1,156 +1,159 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + + namespace FSharp.Compiler - open Microsoft.FSharp.Core - open Microsoft.FSharp.Collections - open Microsoft.FSharp.Reflection - module internal SR = - let private resources = lazy (System.Resources.ResourceManager("fsstrings", System.Reflection.Assembly.GetExecutingAssembly())) +open Microsoft.FSharp.Core +open Microsoft.FSharp.Collections +open Microsoft.FSharp.Reflection - let GetString(name:string) = - let s = resources.Force().GetString(name, System.Globalization.CultureInfo.CurrentUICulture) +module internal SR = + let private resources = lazy (System.Resources.ResourceManager("fsstrings", System.Reflection.Assembly.GetExecutingAssembly())) + + let GetString(name:string) = + let s = resources.Force().GetString(name, System.Globalization.CultureInfo.CurrentUICulture) #if DEBUG - if null = s then - System.Diagnostics.Debug.Assert(false, sprintf "**RESOURCE ERROR**: Resource token %s does not exist!" name) + if null = s then + System.Diagnostics.Debug.Assert(false, sprintf "**RESOURCE ERROR**: Resource token %s does not exist!" name) #endif - s + s + +module internal DiagnosticMessage = - module internal DiagnosticMessage = + open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators - open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators + let mkFunctionValue (tys: System.Type[]) (impl:obj->obj) = + FSharpValue.MakeFunction(FSharpType.MakeFunctionType(tys[0],tys[1]), impl) - let mkFunctionValue (tys: System.Type[]) (impl:obj->obj) = - FSharpValue.MakeFunction(FSharpType.MakeFunctionType(tys[0],tys[1]), impl) + let funTyC = typeof obj>.GetGenericTypeDefinition() + let mkFunTy a b = funTyC.MakeGenericType([| a;b |]) - let funTyC = typeof obj>.GetGenericTypeDefinition() - let mkFunTy a b = funTyC.MakeGenericType([| a;b |]) + let isNamedType(ty:System.Type) = not (ty.IsArray || ty.IsByRef || ty.IsPointer) + let isFunctionType (ty1:System.Type) = + isNamedType(ty1) && ty1.IsGenericType && (ty1.GetGenericTypeDefinition()).Equals(funTyC) - let isNamedType(ty:System.Type) = not (ty.IsArray || ty.IsByRef || ty.IsPointer) - let isFunctionType (ty1:System.Type) = - isNamedType(ty1) && ty1.IsGenericType && (ty1.GetGenericTypeDefinition()).Equals(funTyC) + let rec destFunTy (ty:System.Type) = + if isFunctionType ty then + ty, ty.GetGenericArguments() + else + match ty.BaseType with + | null -> failwith "destFunTy: not a function type" + | b -> destFunTy b - let rec destFunTy (ty:System.Type) = - if isFunctionType ty then - ty, ty.GetGenericArguments() + let buildFunctionForOneArgPat (ty: System.Type) impl = + let _,tys = destFunTy ty + let rty = tys[1] + // PERF: this technique is a bit slow (e.g. in simple cases, like 'sprintf "%x"') + mkFunctionValue tys (fun inp -> impl rty inp) + + let capture1 (fmt:string) i args ty (go : obj list -> System.Type -> int -> obj) : obj = + match fmt[i] with + | '%' -> go args ty (i+1) + | 'd' + | 'f' + | 's' -> buildFunctionForOneArgPat ty (fun rty n -> go (n :: args) rty (i+1)) + | _ -> failwith "bad format specifier" + + // newlines and tabs get converted to strings when read from a resource file + // this will preserve their original intention + let postProcessString (s : string) = + s.Replace("\\n","\n").Replace("\\t","\t") + + let createMessageString (messageString : string) (fmt : Printf.StringFormat<'T>) : 'T = + let fmt = fmt.Value // here, we use the actual error string, as opposed to the one stored as fmt + let len = fmt.Length + + /// Function to capture the arguments and then run. + let rec capture args ty i = + if i >= len || (fmt[i] = '%' && i+1 >= len) then + let b = System.Text.StringBuilder() + b.AppendFormat(messageString, (Array.ofList (List.rev args))) |> ignore + box(b.ToString()) + // REVIEW: For these purposes, this should be a nop, but I'm leaving it + // in case we ever decide to support labels for the error format string + // E.g., "%s%d" + elif System.Char.IsSurrogatePair(fmt,i) then + capture args ty (i+2) else - match ty.BaseType with - | null -> failwith "destFunTy: not a function type" - | b -> destFunTy b - - let buildFunctionForOneArgPat (ty: System.Type) impl = - let _,tys = destFunTy ty - let rty = tys[1] - // PERF: this technique is a bit slow (e.g. in simple cases, like 'sprintf "%x"') - mkFunctionValue tys (fun inp -> impl rty inp) - - let capture1 (fmt:string) i args ty (go : obj list -> System.Type -> int -> obj) : obj = - match fmt[i] with - | '%' -> go args ty (i+1) - | 'd' - | 'f' - | 's' -> buildFunctionForOneArgPat ty (fun rty n -> go (n :: args) rty (i+1)) - | _ -> failwith "bad format specifier" - - // newlines and tabs get converted to strings when read from a resource file - // this will preserve their original intention - let postProcessString (s : string) = - s.Replace("\\n","\n").Replace("\\t","\t") - - let createMessageString (messageString : string) (fmt : Printf.StringFormat<'T>) : 'T = - let fmt = fmt.Value // here, we use the actual error string, as opposed to the one stored as fmt - let len = fmt.Length - - /// Function to capture the arguments and then run. - let rec capture args ty i = - if i >= len || (fmt[i] = '%' && i+1 >= len) then - let b = System.Text.StringBuilder() - b.AppendFormat(messageString, (Array.ofList (List.rev args))) |> ignore - box(b.ToString()) - // REVIEW: For these purposes, this should be a nop, but I'm leaving it - // in case we ever decide to support labels for the error format string - // E.g., "%s%d" - elif System.Char.IsSurrogatePair(fmt,i) then - capture args ty (i+2) - else - match fmt[i] with - | '%' -> - let i = i+1 - capture1 fmt i args ty capture - | _ -> - capture args ty (i+1) - - (unbox (capture [] typeof<'T> 0) : 'T) - - type ResourceString<'T>(fmtString : string, fmt : Printf.StringFormat<'T>) = - member _.Format = - createMessageString fmtString fmt - - let DeclareResourceString (messageID : string,fmt : Printf.StringFormat<'T>) = - let mutable messageString = SR.GetString(messageID) + match fmt[i] with + | '%' -> + let i = i+1 + capture1 fmt i args ty capture + | _ -> + capture args ty (i+1) + + (unbox (capture [] typeof<'T> 0) : 'T) + + type ResourceString<'T>(fmtString : string, fmt : Printf.StringFormat<'T>) = + member _.Format = + createMessageString fmtString fmt + + let DeclareResourceString (messageID : string,fmt : Printf.StringFormat<'T>) = + let mutable messageString = SR.GetString(messageID) #if DEBUG - // validate that the message string exists - let fmtString = fmt.Value + // validate that the message string exists + let fmtString = fmt.Value + + if null = messageString then + System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** String resource %s does not exist" messageID) + messageString <- "" + + // validate the formatting specifiers + let countFormatHoles (s : string) = + // remove escaped format holes + let s = s.Replace("{{","").Replace("}}","") + let len = s.Length - 2 + let mutable pos = 0 + let mutable nHoles = 0 + let mutable order = Set.empty + + while pos < len do + if s[pos] = '{' then + let mutable pos' = pos+1 + while System.Char.IsNumber(s[pos']) do + pos' <- pos' + 1 + if pos' > pos+1 && s[pos'] = '}' then + nHoles <- nHoles + 1 + let ordern = int s[(pos+1) .. (pos'-1)] + order <- order.Add(ordern) + pos <- pos' + pos <- pos + 1 + // the sort should be unnecessary, but better safe than sorry + nHoles,Set.toList order |> List.sortDescending + + let countFormatPlaceholders (s : string) = + // strip any escaped % characters - yes, this will fail if given %%%... + let s = s.Replace("%%","") - if null = messageString then - System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** String resource %s does not exist" messageID) - messageString <- "" - - // validate the formatting specifiers - let countFormatHoles (s : string) = - // remove escaped format holes - let s = s.Replace("{{","").Replace("}}","") - let len = s.Length - 2 + if s = "" then + 0 + else + let len = s.Length - 1 let mutable pos = 0 - let mutable nHoles = 0 - let mutable order = Set.empty - + let mutable nFmt = 0 + while pos < len do - if s[pos] = '{' then - let mutable pos' = pos+1 - while System.Char.IsNumber(s[pos']) do - pos' <- pos' + 1 - if pos' > pos+1 && s[pos'] = '}' then - nHoles <- nHoles + 1 - let ordern = int s[(pos+1) .. (pos'-1)] - order <- order.Add(ordern) - pos <- pos' - pos <- pos + 1 - // the sort should be unnecessary, but better safe than sorry - nHoles,Set.toList order |> List.sortDescending - - let countFormatPlaceholders (s : string) = - // strip any escaped % characters - yes, this will fail if given %%%... - let s = s.Replace("%%","") - - if s = "" then - 0 - else - let len = s.Length - 1 - let mutable pos = 0 - let mutable nFmt = 0 + if s[pos] = '%' && + (s[pos+1] = 'd' || s[pos+1] = 's' || s[pos+1] = 'f') then + nFmt <- nFmt + 1 + pos <- pos + 2 ; + else + pos <- pos + 1 ; + nFmt - while pos < len do - if s[pos] = '%' && - (s[pos+1] = 'd' || s[pos+1] = 's' || s[pos+1] = 'f') then - nFmt <- nFmt + 1 - pos <- pos + 2 ; - else - pos <- pos + 1 ; - nFmt - - let nHoles,holes = countFormatHoles messageString - let nPlaceholders = countFormatPlaceholders fmtString + let nHoles,holes = countFormatHoles messageString + let nPlaceholders = countFormatPlaceholders fmtString + + // first, verify that the number of holes in the message string does not exceed the + // largest hole reference + if holes <> [] && holes[0] > nHoles - 1 then + System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but references hole %d" messageID nHoles holes[0]) - // first, verify that the number of holes in the message string does not exceed the - // largest hole reference - if holes <> [] && holes[0] > nHoles - 1 then - System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but references hole %d" messageID nHoles holes[0]) - - // next, verify that the number of format placeholders is the same as the number of holes - if nHoles <> nPlaceholders then - System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but its format specifier contains %d placeholders" messageID nHoles nPlaceholders) - - #endif - messageString <- postProcessString messageString - new ResourceString<'T>(messageString, fmt) + // next, verify that the number of format placeholders is the same as the number of holes + if nHoles <> nPlaceholders then + System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but its format specifier contains %d placeholders" messageID nHoles nPlaceholders) + +#endif + messageString <- postProcessString messageString + new ResourceString<'T>(messageString, fmt) From 4990f64fa5951b6214c94691845518edad1e6b82 Mon Sep 17 00:00:00 2001 From: Don Syme Date: Fri, 20 May 2022 15:23:50 +0100 Subject: [PATCH 04/20] Apply formatting to "Utilities" (#13171) * change formatting settings * apply formatting --- .fantomasignore | 6 +- src/Compiler/Utilities/EditDistance.fs | 91 +- src/Compiler/Utilities/FileSystem.fs | 559 +++--- src/Compiler/Utilities/HashMultiMap.fs | 133 +- src/Compiler/Utilities/ImmutableArray.fs | 100 +- src/Compiler/Utilities/InternalCollections.fs | 281 +-- src/Compiler/Utilities/PathMap.fs | 31 +- src/Compiler/Utilities/QueueList.fs | 79 +- src/Compiler/Utilities/ResizeArray.fs | 420 +++-- src/Compiler/Utilities/RidHelpers.fs | 14 +- src/Compiler/Utilities/TaggedCollections.fs | 1276 +++++++------ src/Compiler/Utilities/XmlAdapters.fs | 7 +- src/Compiler/Utilities/ildiag.fs | 53 +- src/Compiler/Utilities/illib.fs | 1256 ++++++++----- src/Compiler/Utilities/range.fs | 357 ++-- src/Compiler/Utilities/rational.fs | 89 +- src/Compiler/Utilities/sformat.fs | 1632 ++++++++++------- src/Compiler/Utilities/sr.fs | 163 +- src/Compiler/Utilities/zmap.fs | 62 +- src/Compiler/Utilities/zset.fs | 29 +- 20 files changed, 3960 insertions(+), 2678 deletions(-) diff --git a/.fantomasignore b/.fantomasignore index ae062afa069..0211e8ccbfd 100644 --- a/.fantomasignore +++ b/.fantomasignore @@ -13,7 +13,6 @@ vsintegration/ # Explicitly unformatted implementation files src/FSharp.Core/**/*.fs -src/Compiler/**/*.fs src/Compiler/AbstractIL/**/*.fs src/Compiler/Checking/**/*.fs src/Compiler/CodeGen/**/*.fs @@ -27,9 +26,12 @@ src/Compiler/Service/**/*.fs src/Compiler/Symbols/**/*.fs src/Compiler/SyntaxTree/**/*.fs src/Compiler/TypedTree/**/*.fs -src/Compiler/Utilities/**/*.fs src/Microsoft.FSharp.Compiler/**/*.fs +# Fantomas limitations on implementation files (to investigate) + +src/Compiler/Utilities/lib.fs + # Fantomas limitations on signature files (to investigate) src/Compiler/AbstractIL/ilread.fsi diff --git a/src/Compiler/Utilities/EditDistance.fs b/src/Compiler/Utilities/EditDistance.fs index 1173da83880..8f5a76ae152 100644 --- a/src/Compiler/Utilities/EditDistance.fs +++ b/src/Compiler/Utilities/EditDistance.fs @@ -8,24 +8,28 @@ open System /// Given an offset and a radius from that offset, does mChar exist in that part of str? let inline existsInWin (mChar: char) (str: string) (offset: int) (rad: int) = let startAt = Math.Max(0, offset - rad) - let endAt = Math.Min(offset + rad, str.Length - 1) - if endAt - startAt < 0 then false + let endAt = Math.Min(offset + rad, str.Length - 1) + + if endAt - startAt < 0 then + false else let rec exists index = if str[index] = mChar then true elif index = endAt then false else exists (index + 1) + exists startAt - -let jaro (s1: string) (s2: string) = + +let jaro (s1: string) (s2: string) = // The radius is half of the lesser of the two string lengths rounded up. - let matchRadius = + let matchRadius = let minLen = Math.Min(s1.Length, s2.Length) minLen / 2 + minLen % 2 - let rec nextChar (s1:string) (s2:string) i c = + let rec nextChar (s1: string) (s2: string) i c = if i < s1.Length then let c = s1[i] + if not (existsInWin c s2 i matchRadius) then nextChar s1 s2 (i + 1) c else @@ -33,52 +37,67 @@ let jaro (s1: string) (s2: string) = else struct (i, c) - // The sets of common characters and their lengths as floats + // The sets of common characters and their lengths as floats // The number of transpositions within the sets of common characters. let struct (transpositions, c1length, c2length) = let rec loop i j mismatches c1length c2length = if i < s1.Length && j < s2.Length then let struct (ti, ci) = nextChar s1 s2 i ' ' let struct (tj, cj) = nextChar s2 s1 j ' ' + if ci <> cj then loop (ti + 1) (tj + 1) (mismatches + 1) (c1length + 1) (c2length + 1) else loop (ti + 1) (tj + 1) mismatches (c1length + 1) (c2length + 1) - else struct (i, j, mismatches, c1length, c2length) + else + struct (i, j, mismatches, c1length, c2length) let struct (i, j, mismatches, c1length, c2length) = loop 0 0 0 0 0 - let rec loop (s1:string) (s2:string) i length = + let rec loop (s1: string) (s2: string) i length = if i < s1.Length - 1 then let c = s1[i] - if existsInWin c s2 i matchRadius then + + if existsInWin c s2 i matchRadius then loop s1 s2 (i + 1) (length + 1) else loop s1 s2 (i + 1) length else length + let c1length = loop s1 s2 i c1length |> float let c2length = loop s2 s1 j c2length |> float struct ((float mismatches + abs (c1length - c2length)) / 2.0, c1length, c2length) - + let tLength = Math.Max(c1length, c2length) - + // The jaro distance as given by 1/3 ( m2/|s1| + m1/|s2| + (mc-t)/mc ) - let result = (c1length / float s1.Length + c2length / float s2.Length + (tLength - transpositions) / tLength) / 3.0 - - // This is for cases where |s1|, |s2| or m are zero - if Double.IsNaN result then 0.0 else result + let result = + (c1length / float s1.Length + + c2length / float s2.Length + + (tLength - transpositions) / tLength) + / 3.0 + + // This is for cases where |s1|, |s2| or m are zero + if Double.IsNaN result then + 0.0 + else + result /// Calculates the Jaro-Winkler edit distance between two strings. /// The edit distance is a metric that allows to measure the amount of similarity between two strings. -let JaroWinklerDistance s1 s2 = +let JaroWinklerDistance s1 s2 = let jaroScore = jaro s1 s2 // Accumulate the number of matching initial characters let maxLength = (min s1.Length s2.Length) - 1 + let rec calcL i acc = - if i > maxLength || s1[i] <> s2[i] then acc - else calcL (i + 1) (acc + 1.0) + if i > maxLength || s1[i] <> s2[i] then + acc + else + calcL (i + 1) (acc + 1.0) + let l = min (calcL 0 0.0) 4.0 // Calculate the JW distance let p = 0.1 @@ -88,7 +107,7 @@ let JaroWinklerDistance s1 s2 = /// also known as the "optimal string alignment" distance. /// - read more at https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance /// - Implementation taken from http://www.navision-blog.de/2008/11/01/damerau-levenshtein-distance-in-fsharp-part-ii/ -let private calcDamerauLevenshtein (a:string, b:string) = +let private calcDamerauLevenshtein (a: string, b: string) = let m = b.Length + 1 let mutable lastLine = Array.init m id let mutable lastLastLine = Array.zeroCreate m @@ -96,34 +115,32 @@ let private calcDamerauLevenshtein (a:string, b:string) = for i in 1 .. a.Length do actLine[0] <- i + for j in 1 .. b.Length do - let cost = if a[i-1] = b[j-1] then 0 else 1 + let cost = if a[i - 1] = b[j - 1] then 0 else 1 let deletion = lastLine[j] + 1 - let insertion = actLine[j-1] + 1 - let substitution = lastLine[j-1] + cost - actLine[j] <- - deletion - |> min insertion - |> min substitution - + let insertion = actLine[j - 1] + 1 + let substitution = lastLine[j - 1] + cost + actLine[j] <- deletion |> min insertion |> min substitution + if i > 1 && j > 1 then - if a[i-1] = b[j-2] && a[i-2] = b[j-1] then - let transposition = lastLastLine[j-2] + cost - actLine[j] <- min actLine[j] transposition - + if a[i - 1] = b[j - 2] && a[i - 2] = b[j - 1] then + let transposition = lastLastLine[j - 2] + cost + actLine[j] <- min actLine[j] transposition + // swap lines let temp = lastLastLine lastLastLine <- lastLine lastLine <- actLine actLine <- temp - + lastLine[b.Length] /// Calculates the edit distance between two strings. -/// The edit distance is a metric that allows to measure the amount of difference between two strings +/// The edit distance is a metric that allows to measure the amount of difference between two strings /// and shows how many edit operations (insert, delete, substitution) are needed to transform one string into the other. -let CalculateEditDistance(a:string, b:string) = +let CalculateEditDistance (a: string, b: string) = if a.Length > b.Length then - calcDamerauLevenshtein(a, b) + calcDamerauLevenshtein (a, b) else - calcDamerauLevenshtein(b, a) \ No newline at end of file + calcDamerauLevenshtein (b, a) diff --git a/src/Compiler/Utilities/FileSystem.fs b/src/Compiler/Utilities/FileSystem.fs index 2bb29ff04a2..5296f567cde 100644 --- a/src/Compiler/Utilities/FileSystem.fs +++ b/src/Compiler/Utilities/FileSystem.fs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. namespace FSharp.Compiler.IO + open System open System.IO open System.IO.MemoryMappedFiles @@ -15,35 +16,37 @@ open System.Text exception IllegalFileNameChar of string * char #nowarn "9" + module internal Bytes = - let b0 n = (n &&& 0xFF) + let b0 n = (n &&& 0xFF) - let b1 n = ((n >>> 8) &&& 0xFF) + let b1 n = ((n >>> 8) &&& 0xFF) - let b2 n = ((n >>> 16) &&& 0xFF) + let b2 n = ((n >>> 16) &&& 0xFF) - let b3 n = ((n >>> 24) &&& 0xFF) + let b3 n = ((n >>> 24) &&& 0xFF) let dWw1 n = int32 ((n >>> 32) &&& 0xFFFFFFFFL) let dWw0 n = int32 (n &&& 0xFFFFFFFFL) - let get (b:byte[]) n = int32 (Array.get b n) + let get (b: byte[]) n = int32 (Array.get b n) let zeroCreate n : byte[] = Array.zeroCreate n - let sub ( b:byte[]) s l = Array.sub b s l + let sub (b: byte[]) s l = Array.sub b s l - let blit (a:byte[]) b c d e = Array.blit a b c d e + let blit (a: byte[]) b c d e = Array.blit a b c d e - let ofInt32Array (arr:int[]) = Array.init arr.Length (fun i -> byte arr[i]) + let ofInt32Array (arr: int[]) = + Array.init arr.Length (fun i -> byte arr[i]) - let stringAsUtf8NullTerminated (s:string) = + let stringAsUtf8NullTerminated (s: string) = Array.append (Encoding.UTF8.GetBytes s) (ofInt32Array [| 0x0 |]) - let stringAsUnicodeNullTerminated (s:string) = - Array.append (Encoding.Unicode.GetBytes s) (ofInt32Array [| 0x0;0x0 |]) + let stringAsUnicodeNullTerminated (s: string) = + Array.append (Encoding.Unicode.GetBytes s) (ofInt32Array [| 0x0; 0x0 |]) [] [] @@ -84,10 +87,11 @@ type ByteArrayMemory(bytes: byte[], offset, length) = override _.Length = length - override _.ReadAllBytes () = bytes + override _.ReadAllBytes() = bytes override _.ReadBytes(pos, count) = checkCount count + if count > 0 then Array.sub bytes (offset + pos) count else @@ -95,19 +99,20 @@ type ByteArrayMemory(bytes: byte[], offset, length) = override _.ReadInt32 pos = let finalOffset = offset + pos - (uint32 bytes[finalOffset]) ||| - ((uint32 bytes[finalOffset + 1]) <<< 8) ||| - ((uint32 bytes[finalOffset + 2]) <<< 16) ||| - ((uint32 bytes[finalOffset + 3]) <<< 24) + + (uint32 bytes[finalOffset]) + ||| ((uint32 bytes[finalOffset + 1]) <<< 8) + ||| ((uint32 bytes[finalOffset + 2]) <<< 16) + ||| ((uint32 bytes[finalOffset + 3]) <<< 24) |> int override _.ReadUInt16 pos = let finalOffset = offset + pos - (uint16 bytes[finalOffset]) ||| - ((uint16 bytes[finalOffset + 1]) <<< 8) + (uint16 bytes[finalOffset]) ||| ((uint16 bytes[finalOffset + 1]) <<< 8) override _.ReadUtf8String(pos, count) = checkCount count + if count > 0 then Encoding.UTF8.GetString(bytes, offset + pos, count) else @@ -115,6 +120,7 @@ type ByteArrayMemory(bytes: byte[], offset, length) = override _.Slice(pos, count) = checkCount count + if count > 0 then ByteArrayMemory(bytes, offset + pos, count) :> ByteMemory else @@ -126,6 +132,7 @@ type ByteArrayMemory(bytes: byte[], offset, length) = override _.Copy(srcOffset, dest, destOffset, count) = checkCount count + if count > 0 then Array.blit bytes (offset + srcOffset) dest destOffset count @@ -155,14 +162,14 @@ type SafeUnmanagedMemoryStream = val mutable private holder: obj val mutable private isDisposed: bool - new (addr, length, holder) = + new(addr, length, holder) = { inherit UnmanagedMemoryStream(addr, length) holder = holder isDisposed = false } - new (addr: nativeptr, length: int64, capacity: int64, access: FileAccess, holder) = + new(addr: nativeptr, length: int64, capacity: int64, access: FileAccess, holder) = { inherit UnmanagedMemoryStream(addr, length, capacity, access) holder = holder @@ -173,7 +180,7 @@ type SafeUnmanagedMemoryStream = base.Dispose disposing x.holder <- null // Null out so it can be collected. -type internal MemoryMappedStream(mmf: MemoryMappedFile, length: int64) = +type internal MemoryMappedStream(mmf: MemoryMappedFile, length: int64) = inherit Stream() let viewStream = mmf.CreateViewStream(0L, length, MemoryMappedFileAccess.Read) @@ -183,7 +190,11 @@ type internal MemoryMappedStream(mmf: MemoryMappedFile, length: int64) = override x.CanRead = viewStream.CanRead override x.CanWrite = viewStream.CanWrite override x.CanSeek = viewStream.CanSeek - override x.Position with get() = viewStream.Position and set v = viewStream.Position <- v + + override x.Position + with get () = viewStream.Position + and set v = viewStream.Position <- v + override x.Length = viewStream.Length override x.Flush() = viewStream.Flush() override x.Seek(offset, origin) = viewStream.Seek(offset, origin) @@ -191,8 +202,7 @@ type internal MemoryMappedStream(mmf: MemoryMappedFile, length: int64) = override x.Write(buffer, offset, count) = viewStream.Write(buffer, offset, count) override x.Read(buffer, offset, count) = viewStream.Read(buffer, offset, count) - override x.Finalize() = - x.Dispose() + override x.Finalize() = x.Dispose() interface IDisposable with override x.Dispose() = @@ -200,10 +210,9 @@ type internal MemoryMappedStream(mmf: MemoryMappedFile, length: int64) = mmf.Dispose() viewStream.Dispose() - [] type RawByteMemory(addr: nativeptr, length: int, holder: obj) = - inherit ByteMemory () + inherit ByteMemory() let check i = if i < 0 || i >= length then @@ -220,19 +229,18 @@ type RawByteMemory(addr: nativeptr, length: int, holder: obj) = override _.Item with get i = check i - NativePtr.add addr i - |> NativePtr.read + NativePtr.add addr i |> NativePtr.read and set i v = check i NativePtr.set addr i v override _.Length = length - override this.ReadAllBytes() = - this.ReadBytes(0, length) + override this.ReadAllBytes() = this.ReadBytes(0, length) override _.ReadBytes(pos, count) = checkCount count + if count > 0 then check pos check (pos + count - 1) @@ -246,21 +254,24 @@ type RawByteMemory(addr: nativeptr, length: int, holder: obj) = check pos check (pos + 3) let finalAddr = NativePtr.toNativeInt addr + nativeint pos - uint32(Marshal.ReadByte(finalAddr, 0)) ||| - (uint32(Marshal.ReadByte(finalAddr, 1)) <<< 8) ||| - (uint32(Marshal.ReadByte(finalAddr, 2)) <<< 16) ||| - (uint32(Marshal.ReadByte(finalAddr, 3)) <<< 24) + + uint32 (Marshal.ReadByte(finalAddr, 0)) + ||| (uint32 (Marshal.ReadByte(finalAddr, 1)) <<< 8) + ||| (uint32 (Marshal.ReadByte(finalAddr, 2)) <<< 16) + ||| (uint32 (Marshal.ReadByte(finalAddr, 3)) <<< 24) |> int override _.ReadUInt16 pos = check pos check (pos + 1) let finalAddr = NativePtr.toNativeInt addr + nativeint pos - uint16(Marshal.ReadByte(finalAddr, 0)) ||| - (uint16(Marshal.ReadByte(finalAddr, 1)) <<< 8) + + uint16 (Marshal.ReadByte(finalAddr, 0)) + ||| (uint16 (Marshal.ReadByte(finalAddr, 1)) <<< 8) override _.ReadUtf8String(pos, count) = checkCount count + if count > 0 then check pos check (pos + count - 1) @@ -270,6 +281,7 @@ type RawByteMemory(addr: nativeptr, length: int, holder: obj) = override _.Slice(pos, count) = checkCount count + if count > 0 then check pos check (pos + count - 1) @@ -284,6 +296,7 @@ type RawByteMemory(addr: nativeptr, length: int, holder: obj) = override _.Copy(srcOffset, dest, destOffset, count) = checkCount count + if count > 0 then check srcOffset Marshal.Copy(NativePtr.toNativeInt addr + nativeint srcOffset, dest, destOffset, count) @@ -308,12 +321,13 @@ type RawByteMemory(addr: nativeptr, length: int, holder: obj) = else new MemoryStream([||], 0, 0, false) :> Stream -[] +[] type ReadOnlyByteMemory(bytes: ByteMemory) = - member _.Item with get i = bytes[i] + member _.Item + with get i = bytes[i] - member _.Length with get () = bytes.Length + member _.Length = bytes.Length member _.ReadAllBytes() = bytes.ReadAllBytes() @@ -325,11 +339,13 @@ type ReadOnlyByteMemory(bytes: ByteMemory) = member _.ReadUtf8String(pos, count) = bytes.ReadUtf8String(pos, count) - member _.Slice(pos, count) = bytes.Slice(pos, count) |> ReadOnlyByteMemory + member _.Slice(pos, count) = + bytes.Slice(pos, count) |> ReadOnlyByteMemory member _.CopyTo stream = bytes.CopyTo stream - member _.Copy(srcOffset, dest, destOffset, count) = bytes.Copy(srcOffset, dest, destOffset, count) + member _.Copy(srcOffset, dest, destOffset, count) = + bytes.Copy(srcOffset, dest, destOffset, count) member _.ToArray() = bytes.ToArray() @@ -342,79 +358,97 @@ module MemoryMappedFileExtensions = let private trymmf length copyTo = let length = int64 length + if length = 0L then None + else if runningOnMono then + // mono's MemoryMappedFile implementation throws with null `mapName`, so we use byte arrays instead: https://github.com/mono/mono/issues/1024 + None else - if runningOnMono then - // mono's MemoryMappedFile implementation throws with null `mapName`, so we use byte arrays instead: https://github.com/mono/mono/issues/1024 - None - else - // Try to create a memory mapped file and copy the contents of the given bytes to it. - // If this fails, then we clean up and return None. + // Try to create a memory mapped file and copy the contents of the given bytes to it. + // If this fails, then we clean up and return None. + try + let mmf = + MemoryMappedFile.CreateNew( + null, + length, + MemoryMappedFileAccess.ReadWrite, + MemoryMappedFileOptions.None, + HandleInheritability.None + ) + try - let mmf = MemoryMappedFile.CreateNew(null, length, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, HandleInheritability.None) - try - use stream = mmf.CreateViewStream(0L, length, MemoryMappedFileAccess.ReadWrite) - copyTo stream - Some mmf - with - | _ -> - mmf.Dispose() - None + use stream = mmf.CreateViewStream(0L, length, MemoryMappedFileAccess.ReadWrite) + copyTo stream + Some mmf with | _ -> + mmf.Dispose() None + with + | _ -> None type MemoryMappedFile with + static member TryFromByteMemory(bytes: ReadOnlyByteMemory) = trymmf (int64 bytes.Length) bytes.CopyTo static member TryFromMemory(bytes: ReadOnlyMemory) = let length = int64 bytes.Length - trymmf length - (fun stream -> - let span = Span(stream.PositionPointer |> NativePtr.toVoidPtr, int length) - bytes.Span.CopyTo(span) - stream.Position <- stream.Position + length - ) + + trymmf length (fun stream -> + let span = Span(stream.PositionPointer |> NativePtr.toVoidPtr, int length) + bytes.Span.CopyTo(span) + stream.Position <- stream.Position + length) [] module internal FileSystemUtils = - let checkPathForIllegalChars = + let checkPathForIllegalChars = let chars = System.Collections.Generic.HashSet<_>(Path.GetInvalidPathChars()) - (fun (path:string) -> + + (fun (path: string) -> for c in path do - if chars.Contains c then raise(IllegalFileNameChar(path, c))) + if chars.Contains c then + raise (IllegalFileNameChar(path, c))) let checkSuffix (path: string) (suffix: string) = path.EndsWithOrdinalIgnoreCase(suffix) let hasExtensionWithValidate (validate: bool) (s: string) = - if validate then (checkPathForIllegalChars s) + if validate then + (checkPathForIllegalChars s) + let sLen = s.Length + (sLen >= 1 && s[sLen - 1] = '.' && s <> ".." && s <> ".") || Path.HasExtension(s) let hasExtension (path: string) = hasExtensionWithValidate true path - let chopExtension (path:string) = + let chopExtension (path: string) = checkPathForIllegalChars path - if path = "." then "" else // for OCaml compatibility - if not (hasExtensionWithValidate false path) then - raise (ArgumentException("chopExtension")) // message has to be precisely this, for OCaml compatibility, and no argument name can be set - Path.Combine (Path.GetDirectoryName path, Path.GetFileNameWithoutExtension(path)) + + if path = "." then + "" + else // for OCaml compatibility + if not (hasExtensionWithValidate false path) then + raise (ArgumentException("chopExtension")) // message has to be precisely this, for OCaml compatibility, and no argument name can be set + + Path.Combine(Path.GetDirectoryName path, Path.GetFileNameWithoutExtension(path)) let fileNameOfPath path = checkPathForIllegalChars path Path.GetFileName(path) - let fileNameWithoutExtensionWithValidate (validate:bool) path = - if validate then checkPathForIllegalChars path + let fileNameWithoutExtensionWithValidate (validate: bool) path = + if validate then + checkPathForIllegalChars path + Path.GetFileNameWithoutExtension(path) - let fileNameWithoutExtension path = fileNameWithoutExtensionWithValidate true path + let fileNameWithoutExtension path = + fileNameWithoutExtensionWithValidate true path - let trimQuotes (path: string) = - path.Trim( [|' '; '\"'|] ) + let trimQuotes (path: string) = path.Trim([| ' '; '\"' |]) let isDll fileName = checkSuffix fileName ".dll" @@ -480,14 +514,15 @@ type IFileSystem = abstract IsStableFileHeuristic: fileName: string -> bool - // note: do not add members if you can put generic implementation under StreamExtensions below. +// note: do not add members if you can put generic implementation under StreamExtensions below. [] type DefaultFileSystem() as this = - abstract AssemblyLoader : IAssemblyLoader + abstract AssemblyLoader: IAssemblyLoader default _.AssemblyLoader = DefaultAssemblyLoader() :> IAssemblyLoader - + abstract OpenFileForReadShim: filePath: string * ?useMemoryMappedFile: bool * ?shouldShadowCopy: bool -> Stream + default _.OpenFileForReadShim(filePath: string, ?useMemoryMappedFile: bool, ?shouldShadowCopy: bool) : Stream = let fileMode = FileMode.Open let fileAccess = FileAccess.Read @@ -496,12 +531,12 @@ type DefaultFileSystem() as this = let useMemoryMappedFile = defaultArg useMemoryMappedFile false let fileStream = new FileStream(filePath, fileMode, fileAccess, fileShare) let length = fileStream.Length - + // We want to use mmaped files only when: // - Opening large binary files (no need to use for source or resource files really) // - Running on mono, since its MemoryMappedFile implementation throws when "mapName" is not provided (is null). // (See: https://github.com/mono/mono/issues/10245) - + if runningOnMono || (not useMemoryMappedFile) then fileStream :> Stream else @@ -513,7 +548,9 @@ type DefaultFileSystem() as this = length, MemoryMappedFileAccess.Read, MemoryMappedFileOptions.None, - HandleInheritability.None) + HandleInheritability.None + ) + use stream = mmf.CreateViewStream(0L, length, MemoryMappedFileAccess.Read) fileStream.CopyTo(stream) fileStream.Dispose() @@ -525,16 +562,18 @@ type DefaultFileSystem() as this = length, MemoryMappedFileAccess.Read, HandleInheritability.None, - leaveOpen=false) + leaveOpen = false + ) let stream = new MemoryMappedStream(mmf, length) if not stream.CanRead then invalidOp "Cannot read file" - stream :> Stream + stream :> Stream abstract OpenFileForWriteShim: filePath: string * ?fileMode: FileMode * ?fileAccess: FileAccess * ?fileShare: FileShare -> Stream + default _.OpenFileForWriteShim(filePath: string, ?fileMode: FileMode, ?fileAccess: FileAccess, ?fileShare: FileShare) : Stream = let fileMode = defaultArg fileMode FileMode.OpenOrCreate let fileAccess = defaultArg fileAccess FileAccess.ReadWrite @@ -543,106 +582,130 @@ type DefaultFileSystem() as this = new FileStream(filePath, fileMode, fileAccess, fileShare) :> Stream abstract GetFullPathShim: fileName: string -> string - default _.GetFullPathShim (fileName: string) = Path.GetFullPath fileName + default _.GetFullPathShim(fileName: string) = Path.GetFullPath fileName abstract GetFullFilePathInDirectoryShim: dir: string -> fileName: string -> string + default this.GetFullFilePathInDirectoryShim (dir: string) (fileName: string) = - let p = if (this :> IFileSystem).IsPathRootedShim(fileName) then fileName else Path.Combine(dir, fileName) - try (this :> IFileSystem).GetFullPathShim(p) with + let p = + if (this :> IFileSystem).IsPathRootedShim(fileName) then + fileName + else + Path.Combine(dir, fileName) + + try + (this :> IFileSystem).GetFullPathShim(p) + with | :? ArgumentException | :? ArgumentNullException | :? NotSupportedException | :? PathTooLongException | :? System.Security.SecurityException -> p - + abstract IsPathRootedShim: path: string -> bool - default _.IsPathRootedShim (path: string) = Path.IsPathRooted path + default _.IsPathRootedShim(path: string) = Path.IsPathRooted path abstract NormalizePathShim: path: string -> string - default _.NormalizePathShim (path: string) = + + default _.NormalizePathShim(path: string) = try let ifs = this :> IFileSystem + if ifs.IsPathRootedShim path then ifs.GetFullPathShim path else path - with _ -> path + with + | _ -> path abstract IsInvalidPathShim: path: string -> bool + default _.IsInvalidPathShim(path: string) = - let isInvalidPath(p: string MaybeNull) = + let isInvalidPath (p: string MaybeNull) = match p with - | Null | "" -> true + | Null + | "" -> true | NonNull p -> p.IndexOfAny(Path.GetInvalidPathChars()) <> -1 - let isInvalidFilename(p: string MaybeNull) = + let isInvalidFilename (p: string MaybeNull) = match p with - | Null | "" -> true + | Null + | "" -> true | NonNull p -> p.IndexOfAny(Path.GetInvalidFileNameChars()) <> -1 - let isInvalidDirectory(d: string MaybeNull) = + let isInvalidDirectory (d: string MaybeNull) = match d with | Null -> true | NonNull d -> d.IndexOfAny(Path.GetInvalidPathChars()) <> -1 - isInvalidPath path || - let directory = Path.GetDirectoryName path - let fileName = Path.GetFileName path - isInvalidDirectory directory || isInvalidFilename fileName + isInvalidPath path + || let directory = Path.GetDirectoryName path in + let fileName = Path.GetFileName path in + isInvalidDirectory directory || isInvalidFilename fileName abstract GetTempPathShim: unit -> string default _.GetTempPathShim() = Path.GetTempPath() - abstract GetDirectoryNameShim: path: string -> string - default _.GetDirectoryNameShim(path:string) = + abstract GetDirectoryNameShim: path: string -> string + + default _.GetDirectoryNameShim(path: string) = FileSystemUtils.checkPathForIllegalChars path - if path = "" then "." + + if path = "" then + "." else - match Path.GetDirectoryName(path) with - | null -> if (this :> IFileSystem).IsPathRootedShim(path) then path else "." - | res -> if res = "" then "." else res + match Path.GetDirectoryName(path) with + | null -> + if (this :> IFileSystem).IsPathRootedShim(path) then + path + else + "." + | res -> if res = "" then "." else res abstract GetLastWriteTimeShim: fileName: string -> DateTime - default _.GetLastWriteTimeShim (fileName: string) = File.GetLastWriteTimeUtc fileName + default _.GetLastWriteTimeShim(fileName: string) = File.GetLastWriteTimeUtc fileName abstract GetCreationTimeShim: path: string -> DateTime - default _.GetCreationTimeShim (path: string) = File.GetCreationTimeUtc path + default _.GetCreationTimeShim(path: string) = File.GetCreationTimeUtc path abstract CopyShim: src: string * dest: string * overwrite: bool -> unit - default _.CopyShim (src: string, dest: string, overwrite: bool) = File.Copy(src, dest, overwrite) + default _.CopyShim(src: string, dest: string, overwrite: bool) = File.Copy(src, dest, overwrite) abstract FileExistsShim: fileName: string -> bool - default _.FileExistsShim (fileName: string) = File.Exists fileName + default _.FileExistsShim(fileName: string) = File.Exists fileName abstract FileDeleteShim: fileName: string -> unit - default _.FileDeleteShim (fileName: string) = File.Delete fileName + default _.FileDeleteShim(fileName: string) = File.Delete fileName abstract DirectoryCreateShim: path: string -> string - default _.DirectoryCreateShim (path: string) = + + default _.DirectoryCreateShim(path: string) = let dir = Directory.CreateDirectory path dir.FullName abstract DirectoryExistsShim: path: string -> bool - default _.DirectoryExistsShim (path: string) = Directory.Exists path + default _.DirectoryExistsShim(path: string) = Directory.Exists path abstract DirectoryDeleteShim: path: string -> unit - default _.DirectoryDeleteShim (path: string) = Directory.Delete path + default _.DirectoryDeleteShim(path: string) = Directory.Delete path abstract EnumerateFilesShim: path: string * pattern: string -> string seq default _.EnumerateFilesShim(path: string, pattern: string) = Directory.EnumerateFiles(path, pattern) - abstract EnumerateDirectoriesShim: path: string -> string seq + abstract EnumerateDirectoriesShim: path: string -> string seq default _.EnumerateDirectoriesShim(path: string) = Directory.EnumerateDirectories(path) - + abstract IsStableFileHeuristic: fileName: string -> bool - default _.IsStableFileHeuristic (fileName: string) = + + default _.IsStableFileHeuristic(fileName: string) = let directory = Path.GetDirectoryName fileName - directory.Contains("Reference Assemblies/") || - directory.Contains("Reference Assemblies\\") || - directory.Contains("packages/") || - directory.Contains("packages\\") || - directory.Contains("lib/mono/") - + + directory.Contains("Reference Assemblies/") + || directory.Contains("Reference Assemblies\\") + || directory.Contains("packages/") + || directory.Contains("packages\\") + || directory.Contains("lib/mono/") + interface IFileSystem with member _.AssemblyLoader = this.AssemblyLoader @@ -657,29 +720,34 @@ type DefaultFileSystem() as this = let fileShare = defaultArg fileShare FileShare.Delete ||| FileShare.ReadWrite this.OpenFileForWriteShim(filePath, fileMode, fileAccess, fileShare) - member _.GetFullPathShim (fileName: string) = this.GetFullPathShim fileName - member _.GetFullFilePathInDirectoryShim (dir: string) (fileName: string) = this.GetFullFilePathInDirectoryShim dir fileName - member _.IsPathRootedShim (path: string) = this.IsPathRootedShim path - member _.NormalizePathShim (path: string) = this.NormalizePathShim path + member _.GetFullPathShim(fileName: string) = this.GetFullPathShim fileName + + member _.GetFullFilePathInDirectoryShim (dir: string) (fileName: string) = + this.GetFullFilePathInDirectoryShim dir fileName + + member _.IsPathRootedShim(path: string) = this.IsPathRootedShim path + member _.NormalizePathShim(path: string) = this.NormalizePathShim path member _.IsInvalidPathShim(path: string) = this.IsInvalidPathShim path member _.GetTempPathShim() = this.GetTempPathShim() - member _.GetDirectoryNameShim(s:string) = this.GetDirectoryNameShim s - member _.GetLastWriteTimeShim (fileName: string) = this.GetLastWriteTimeShim fileName - member _.GetCreationTimeShim (path: string) = this.GetCreationTimeShim path - member _.CopyShim (src: string, dest: string, overwrite: bool) = this.CopyShim(src, dest, overwrite) - member _.FileExistsShim (fileName: string) = this.FileExistsShim fileName - member _.FileDeleteShim (fileName: string) = this.FileDeleteShim fileName - member _.DirectoryCreateShim (path: string) = this.DirectoryCreateShim path - member _.DirectoryExistsShim (path: string) = this.DirectoryExistsShim path - member _.DirectoryDeleteShim (path: string) = this.DirectoryDeleteShim path + member _.GetDirectoryNameShim(s: string) = this.GetDirectoryNameShim s + member _.GetLastWriteTimeShim(fileName: string) = this.GetLastWriteTimeShim fileName + member _.GetCreationTimeShim(path: string) = this.GetCreationTimeShim path + member _.CopyShim(src: string, dest: string, overwrite: bool) = this.CopyShim(src, dest, overwrite) + member _.FileExistsShim(fileName: string) = this.FileExistsShim fileName + member _.FileDeleteShim(fileName: string) = this.FileDeleteShim fileName + member _.DirectoryCreateShim(path: string) = this.DirectoryCreateShim path + member _.DirectoryExistsShim(path: string) = this.DirectoryExistsShim path + member _.DirectoryDeleteShim(path: string) = this.DirectoryDeleteShim path member _.EnumerateFilesShim(path: string, pattern: string) = this.EnumerateFilesShim(path, pattern) member _.EnumerateDirectoriesShim(path: string) = this.EnumerateDirectoriesShim path - member _.IsStableFileHeuristic (fileName: string) = this.IsStableFileHeuristic fileName + member _.IsStableFileHeuristic(fileName: string) = this.IsStableFileHeuristic fileName [] module public StreamExtensions = let utf8noBOM = UTF8Encoding(false, true) :> Encoding + type Stream with + member s.GetWriter(?encoding: Encoding) : TextWriter = let encoding = defaultArg encoding utf8noBOM new StreamWriter(s, encoding) :> TextWriter @@ -687,10 +755,11 @@ module public StreamExtensions = member s.WriteAllLines(contents: string seq, ?encoding: Encoding) = let encoding = defaultArg encoding utf8noBOM use writer = s.GetWriter(encoding) + for l in contents do writer.WriteLine(l) - member s.Write (data: 'a) : unit = + member s.Write(data: 'a) : unit = use sw = s.GetWriter() sw.Write(data) @@ -698,42 +767,46 @@ module public StreamExtensions = let retryLocked = defaultArg retryLocked false let retryDelayMilliseconds = 50 let numRetries = 60 + let rec getSource retryNumber = - try - // Use the .NET functionality to auto-detect the unicode encoding - match codePage with - | None -> new StreamReader(s, true) - | Some n -> new StreamReader(s, Encoding.GetEncoding(n)) - with - // We can get here if the file is locked--like when VS is saving a file--we don't have direct - // access to the HRESULT to see that this is EONOACCESS. - | :? IOException as err when retryLocked && err.GetType() = typeof -> - // This second check is to make sure the exception is exactly IOException and none of these for example: - // DirectoryNotFoundException - // EndOfStreamException - // FileNotFoundException - // FileLoadException - // PathTooLongException - if retryNumber < numRetries then - Thread.Sleep retryDelayMilliseconds - getSource (retryNumber + 1) - else - reraise() + try + // Use the .NET functionality to auto-detect the unicode encoding + match codePage with + | None -> new StreamReader(s, true) + | Some n -> new StreamReader(s, Encoding.GetEncoding(n)) + with + // We can get here if the file is locked--like when VS is saving a file--we don't have direct + // access to the HRESULT to see that this is EONOACCESS. + | :? IOException as err when retryLocked && err.GetType() = typeof -> + // This second check is to make sure the exception is exactly IOException and none of these for example: + // DirectoryNotFoundException + // EndOfStreamException + // FileNotFoundException + // FileLoadException + // PathTooLongException + if retryNumber < numRetries then + Thread.Sleep retryDelayMilliseconds + getSource (retryNumber + 1) + else + reraise () + getSource 0 - member s.ReadBytes (start, len) = + member s.ReadBytes(start, len) = s.Seek(int64 start, SeekOrigin.Begin) |> ignore - let buffer = Array.zeroCreate len + let buffer = Array.zeroCreate len let mutable n = 0 - while n < len do - n <- n + s.Read(buffer, n, len-n) + + while n < len do + n <- n + s.Read(buffer, n, len - n) + buffer - + member s.ReadAllBytes() = use reader = new BinaryReader(s) let count = (int s.Length) reader.ReadBytes(count) - + member s.ReadAllText(?encoding: Encoding) = let encoding = defaultArg encoding Encoding.UTF8 use sr = new StreamReader(s, encoding, true) @@ -741,11 +814,14 @@ module public StreamExtensions = member s.ReadLines(?encoding: Encoding) : string seq = let encoding = defaultArg encoding Encoding.UTF8 + seq { use sr = new StreamReader(s, encoding, true) + while not <| sr.EndOfStream do yield sr.ReadLine() } + member s.ReadAllLines(?encoding: Encoding) : string array = let encoding = defaultArg encoding Encoding.UTF8 s.ReadLines(encoding) |> Seq.toArray @@ -760,14 +836,19 @@ module public StreamExtensions = match s with | :? MemoryMappedStream as mmfs -> let length = mmfs.Length - RawByteMemory( - NativePtr.ofNativeInt (mmfs.ViewStream.SafeMemoryMappedViewHandle.DangerousGetHandle()), - int length, - mmfs) :> ByteMemory + + RawByteMemory(NativePtr.ofNativeInt (mmfs.ViewStream.SafeMemoryMappedViewHandle.DangerousGetHandle()), int length, mmfs) + :> ByteMemory | _ -> let bytes = s.ReadAllBytes() - let byteArrayMemory = if bytes.Length = 0 then ByteArrayMemory([||], 0, 0) else ByteArrayMemory(bytes, 0, bytes.Length) + + let byteArrayMemory = + if bytes.Length = 0 then + ByteArrayMemory([||], 0, 0) + else + ByteArrayMemory(bytes, 0, bytes.Length) + byteArrayMemory :> ByteMemory [] @@ -791,98 +872,125 @@ type ByteMemory with static member FromArray(bytes, offset, length) = ByteArrayMemory(bytes, offset, length) :> ByteMemory - static member FromArray (bytes: byte array) = + static member FromArray(bytes: byte array) = if bytes.Length = 0 then ByteMemory.Empty else ByteArrayMemory.FromArray(bytes, 0, bytes.Length) type internal ByteStream = - { bytes: ReadOnlyByteMemory - mutable pos: int - max: int } + { + bytes: ReadOnlyByteMemory + mutable pos: int + max: int + } member b.ReadByte() = - if b.pos >= b.max then failwith "end of stream" + if b.pos >= b.max then + failwith "end of stream" + let res = b.bytes[b.pos] b.pos <- b.pos + 1 res + member b.ReadUtf8String n = - let res = b.bytes.ReadUtf8String(b.pos,n) - b.pos <- b.pos + n; res + let res = b.bytes.ReadUtf8String(b.pos, n) + b.pos <- b.pos + n + res - static member FromBytes (b: ReadOnlyByteMemory,start,length) = - if start < 0 || (start+length) > b.Length then failwith "FromBytes" - { bytes = b; pos = start; max = start+length } + static member FromBytes(b: ReadOnlyByteMemory, start, length) = + if start < 0 || (start + length) > b.Length then + failwith "FromBytes" + + { + bytes = b + pos = start + max = start + length + } + + member b.ReadBytes n = + if b.pos + n > b.max then + failwith "ReadBytes: end of stream" - member b.ReadBytes n = - if b.pos + n > b.max then failwith "ReadBytes: end of stream" let res = b.bytes.Slice(b.pos, n) b.pos <- b.pos + n res member b.Position = b.pos #if LAZY_UNPICKLE - member b.CloneAndSeek = { bytes=b.bytes; pos=pos; max=b.max } + member b.CloneAndSeek = + { + bytes = b.bytes + pos = pos + max = b.max + } + member b.Skip = b.pos <- b.pos + n #endif - type internal ByteBuffer = - { useArrayPool: bool - mutable isDisposed: bool - mutable bbArray: byte[] - mutable bbCurrent: int } + { + useArrayPool: bool + mutable isDisposed: bool + mutable bbArray: byte[] + mutable bbCurrent: int + } member inline private buf.CheckDisposed() = if buf.isDisposed then - raise(ObjectDisposedException(nameof(ByteBuffer))) + raise (ObjectDisposedException(nameof (ByteBuffer))) member private buf.Ensure newSize = let oldBufSize = buf.bbArray.Length + if newSize > oldBufSize then let old = buf.bbArray - buf.bbArray <- + + buf.bbArray <- if buf.useArrayPool then - ArrayPool.Shared.Rent (max newSize (oldBufSize * 2)) + ArrayPool.Shared.Rent(max newSize (oldBufSize * 2)) else Bytes.zeroCreate (max newSize (oldBufSize * 2)) + Bytes.blit old 0 buf.bbArray 0 buf.bbCurrent + if buf.useArrayPool then ArrayPool.Shared.Return old - member buf.AsMemory() = + member buf.AsMemory() = buf.CheckDisposed() ReadOnlyMemory(buf.bbArray, 0, buf.bbCurrent) - member buf.EmitIntAsByte (i:int) = + member buf.EmitIntAsByte(i: int) = buf.CheckDisposed() let newSize = buf.bbCurrent + 1 buf.Ensure newSize - buf.bbArray[buf.bbCurrent] <- byte i + buf.bbArray[ buf.bbCurrent ] <- byte i buf.bbCurrent <- newSize - member buf.EmitByte (b:byte) = + member buf.EmitByte(b: byte) = buf.CheckDisposed() - buf.EmitIntAsByte (int b) + buf.EmitIntAsByte(int b) - member buf.EmitIntsAsBytes (arr:int[]) = + member buf.EmitIntsAsBytes(arr: int[]) = buf.CheckDisposed() let n = arr.Length let newSize = buf.bbCurrent + n buf.Ensure newSize let bbArr = buf.bbArray let bbBase = buf.bbCurrent + for i = 0 to n - 1 do bbArr[bbBase + i] <- byte arr[i] + buf.bbCurrent <- newSize member bb.FixupInt32 pos value = bb.CheckDisposed() - bb.bbArray[pos] <- (Bytes.b0 value |> byte) - bb.bbArray[pos + 1] <- (Bytes.b1 value |> byte) - bb.bbArray[pos + 2] <- (Bytes.b2 value |> byte) - bb.bbArray[pos + 3] <- (Bytes.b3 value |> byte) + bb.bbArray[ pos ] <- (Bytes.b0 value |> byte) + bb.bbArray[ pos + 1 ] <- (Bytes.b1 value |> byte) + bb.bbArray[ pos + 2 ] <- (Bytes.b2 value |> byte) + bb.bbArray[ pos + 3 ] <- (Bytes.b3 value |> byte) member buf.EmitInt32 n = buf.CheckDisposed() @@ -891,7 +999,7 @@ type internal ByteBuffer = buf.FixupInt32 buf.bbCurrent n buf.bbCurrent <- newSize - member buf.EmitBytes (i:byte[]) = + member buf.EmitBytes(i: byte[]) = buf.CheckDisposed() let n = i.Length let newSize = buf.bbCurrent + n @@ -899,7 +1007,7 @@ type internal ByteBuffer = Bytes.blit i 0 buf.bbArray buf.bbCurrent n buf.bbCurrent <- newSize - member buf.EmitMemory (i:ReadOnlyMemory) = + member buf.EmitMemory(i: ReadOnlyMemory) = buf.CheckDisposed() let n = i.Length let newSize = buf.bbCurrent + n @@ -907,7 +1015,7 @@ type internal ByteBuffer = i.CopyTo(Memory(buf.bbArray, buf.bbCurrent, n)) buf.bbCurrent <- newSize - member buf.EmitByteMemory (i:ReadOnlyByteMemory) = + member buf.EmitByteMemory(i: ReadOnlyByteMemory) = buf.CheckDisposed() let n = i.Length let newSize = buf.bbCurrent + n @@ -919,22 +1027,22 @@ type internal ByteBuffer = buf.CheckDisposed() let newSize = buf.bbCurrent + 2 buf.Ensure newSize - buf.bbArray[buf.bbCurrent] <- (Bytes.b0 n |> byte) - buf.bbArray[buf.bbCurrent + 1] <- (Bytes.b1 n |> byte) + buf.bbArray[ buf.bbCurrent ] <- (Bytes.b0 n |> byte) + buf.bbArray[ buf.bbCurrent + 1 ] <- (Bytes.b1 n |> byte) buf.bbCurrent <- newSize - member buf.EmitBoolAsByte (b:bool) = + member buf.EmitBoolAsByte(b: bool) = buf.CheckDisposed() - buf.EmitIntAsByte (if b then 1 else 0) + buf.EmitIntAsByte(if b then 1 else 0) - member buf.EmitUInt16 (x:uint16) = + member buf.EmitUInt16(x: uint16) = buf.CheckDisposed() - buf.EmitInt32AsUInt16 (int32 x) + buf.EmitInt32AsUInt16(int32 x) member buf.EmitInt64 x = buf.CheckDisposed() - buf.EmitInt32 (Bytes.dWw0 x) - buf.EmitInt32 (Bytes.dWw1 x) + buf.EmitInt32(Bytes.dWw0 x) + buf.EmitInt32(Bytes.dWw1 x) member buf.Position = buf.CheckDisposed() @@ -942,16 +1050,24 @@ type internal ByteBuffer = static member Create(capacity, useArrayPool) = let useArrayPool = defaultArg useArrayPool false - { useArrayPool = useArrayPool - isDisposed = false - bbArray = if useArrayPool then ArrayPool.Shared.Rent capacity else Bytes.zeroCreate capacity - bbCurrent = 0 } + + { + useArrayPool = useArrayPool + isDisposed = false + bbArray = + if useArrayPool then + ArrayPool.Shared.Rent capacity + else + Bytes.zeroCreate capacity + bbCurrent = 0 + } interface IDisposable with member this.Dispose() = if not this.isDisposed then this.isDisposed <- true + if this.useArrayPool then ArrayPool.Shared.Return this.bbArray @@ -973,17 +1089,15 @@ type ByteStorage(getByteMemory: unit -> ReadOnlyByteMemory) = | true, byteMemory -> byteMemory.AsReadOnly() | _ -> getAndCache () - static member FromByteArray(bytes: byte []) = + static member FromByteArray(bytes: byte[]) = ByteStorage.FromByteMemory(ByteMemory.FromArray(bytes).AsReadOnly()) - static member FromByteMemory(bytes: ReadOnlyByteMemory) = - ByteStorage(fun () -> bytes) + static member FromByteMemory(bytes: ReadOnlyByteMemory) = ByteStorage(fun () -> bytes) static member FromByteMemoryAndCopy(bytes: ReadOnlyByteMemory, useBackingMemoryMappedFile: bool) = if useBackingMemoryMappedFile then match MemoryMappedFile.TryFromByteMemory(bytes) with - | Some mmf -> - ByteStorage(fun () -> ByteMemory.FromMemoryMappedFile(mmf).AsReadOnly()) + | Some mmf -> ByteStorage(fun () -> ByteMemory.FromMemoryMappedFile(mmf).AsReadOnly()) | _ -> let copiedBytes = ByteMemory.FromArray(bytes.ToArray()).AsReadOnly() ByteStorage.FromByteMemory(copiedBytes) @@ -994,8 +1108,7 @@ type ByteStorage(getByteMemory: unit -> ReadOnlyByteMemory) = static member FromMemoryAndCopy(bytes: ReadOnlyMemory, useBackingMemoryMappedFile: bool) = if useBackingMemoryMappedFile then match MemoryMappedFile.TryFromMemory(bytes) with - | Some mmf -> - ByteStorage(fun () -> ByteMemory.FromMemoryMappedFile(mmf).AsReadOnly()) + | Some mmf -> ByteStorage(fun () -> ByteMemory.FromMemoryMappedFile(mmf).AsReadOnly()) | _ -> let copiedBytes = ByteMemory.FromArray(bytes.ToArray()).AsReadOnly() ByteStorage.FromByteMemory(copiedBytes) @@ -1003,5 +1116,5 @@ type ByteStorage(getByteMemory: unit -> ReadOnlyByteMemory) = let copiedBytes = ByteMemory.FromArray(bytes.ToArray()).AsReadOnly() ByteStorage.FromByteMemory(copiedBytes) - static member FromByteArrayAndCopy(bytes: byte [], useBackingMemoryMappedFile: bool) = + static member FromByteArrayAndCopy(bytes: byte[], useBackingMemoryMappedFile: bool) = ByteStorage.FromByteMemoryAndCopy(ByteMemory.FromArray(bytes).AsReadOnly(), useBackingMemoryMappedFile) diff --git a/src/Compiler/Utilities/HashMultiMap.fs b/src/Compiler/Utilities/HashMultiMap.fs index 0480cfbfee8..b88af5d77eb 100644 --- a/src/Compiler/Utilities/HashMultiMap.fs +++ b/src/Compiler/Utilities/HashMultiMap.fs @@ -3,81 +3,86 @@ namespace Internal.Utilities.Collections open System.Collections.Generic - + // Each entry in the HashMultiMap dictionary has at least one entry. Under normal usage each entry has _only_ // one entry. So use two hash tables: one for the main entries and one for the overflow. [] -type internal HashMultiMap<'Key,'Value>(size: int, comparer: IEqualityComparer<'Key>) = +type internal HashMultiMap<'Key, 'Value>(size: int, comparer: IEqualityComparer<'Key>) = + + let firstEntries = Dictionary<_, _>(size, comparer) - let firstEntries = Dictionary<_,_>(size,comparer) + let rest = Dictionary<_, _>(3, comparer) - let rest = Dictionary<_,_>(3,comparer) - - new (comparer : IEqualityComparer<'Key>) = HashMultiMap<'Key,'Value>(11, comparer) + new(comparer: IEqualityComparer<'Key>) = HashMultiMap<'Key, 'Value>(11, comparer) - new (entries : seq<'Key * 'Value>, comparer : IEqualityComparer<'Key>) as x = - new HashMultiMap<'Key,'Value>(11, comparer) - then entries |> Seq.iter (fun (k,v) -> x.Add(k,v)) + new(entries: seq<'Key * 'Value>, comparer: IEqualityComparer<'Key>) as x = + new HashMultiMap<'Key, 'Value>(11, comparer) + then entries |> Seq.iter (fun (k, v) -> x.Add(k, v)) member x.GetRest(k) = match rest.TryGetValue k with | true, res -> res | _ -> [] - member x.Add(y,z) = + member x.Add(y, z) = match firstEntries.TryGetValue y with - | true, res -> - rest[y] <- res :: x.GetRest(y) + | true, res -> rest[y] <- res :: x.GetRest(y) | _ -> () + firstEntries[y] <- z - member x.Clear() = - firstEntries.Clear() - rest.Clear() + member x.Clear() = + firstEntries.Clear() + rest.Clear() member x.FirstEntries = firstEntries member x.Rest = rest - member x.Copy() = - let res = HashMultiMap<'Key,'Value>(firstEntries.Count,firstEntries.Comparer) - for kvp in firstEntries do - res.FirstEntries.Add(kvp.Key,kvp.Value) + member x.Copy() = + let res = HashMultiMap<'Key, 'Value>(firstEntries.Count, firstEntries.Comparer) + + for kvp in firstEntries do + res.FirstEntries.Add(kvp.Key, kvp.Value) + + for kvp in rest do + res.Rest.Add(kvp.Key, kvp.Value) - for kvp in rest do - res.Rest.Add(kvp.Key,kvp.Value) res - member x.Item - with get(y : 'Key) = + member x.Item + with get (y: 'Key) = match firstEntries.TryGetValue y with | true, res -> res | _ -> raise (KeyNotFoundException("The item was not found in collection")) - and set (y:'Key) (z:'Value) = - x.Replace(y,z) + and set (y: 'Key) (z: 'Value) = x.Replace(y, z) - member x.FindAll(y) = + member x.FindAll(y) = match firstEntries.TryGetValue y with | true, res -> res :: x.GetRest(y) | _ -> [] - member x.Fold f acc = + member x.Fold f acc = let mutable res = acc + for kvp in firstEntries do res <- f kvp.Key kvp.Value res + match x.GetRest(kvp.Key) with | [] -> () - | rest -> + | rest -> for z in rest do res <- f kvp.Key z res + res - member x.Iterate(f) = + member x.Iterate(f) = for kvp in firstEntries do f kvp.Key kvp.Value + match x.GetRest(kvp.Key) with | [] -> () - | rest -> + | rest -> for z in rest do f kvp.Key z @@ -85,28 +90,25 @@ type internal HashMultiMap<'Key,'Value>(size: int, comparer: IEqualityComparer<' member x.ContainsKey(y) = firstEntries.ContainsKey(y) - member x.Remove(y) = + member x.Remove(y) = match firstEntries.TryGetValue y with // NOTE: If not ok then nothing to remove - nop | true, _res -> // We drop the FirstEntry. Here we compute the new FirstEntry and residue MoreEntries match rest.TryGetValue y with | true, res -> - match res with - | [h] -> - firstEntries[y] <- h; + match res with + | [ h ] -> + firstEntries[y] <- h rest.Remove(y) |> ignore - | h :: t -> + | h :: t -> firstEntries[y] <- h rest[y] <- t - | _ -> - () - | _ -> - firstEntries.Remove(y) |> ignore + | _ -> () + | _ -> firstEntries.Remove(y) |> ignore | _ -> () - member x.Replace(y,z) = - firstEntries[y] <- z + member x.Replace(y, z) = firstEntries[y] <- z member x.TryFind(y) = match firstEntries.TryGetValue y with @@ -117,49 +119,60 @@ type internal HashMultiMap<'Key,'Value>(size: int, comparer: IEqualityComparer<' interface IEnumerable> with - member s.GetEnumerator() = + member s.GetEnumerator() = let elems = List<_>(firstEntries.Count + rest.Count) + for kvp in firstEntries do elems.Add(kvp) + for z in s.GetRest(kvp.Key) do - elems.Add(KeyValuePair(kvp.Key, z)) + elems.Add(KeyValuePair(kvp.Key, z)) + (elems.GetEnumerator() :> IEnumerator<_>) interface System.Collections.IEnumerable with - member s.GetEnumerator() = ((s :> seq<_>).GetEnumerator() :> System.Collections.IEnumerator) + member s.GetEnumerator() = + ((s :> seq<_>).GetEnumerator() :> System.Collections.IEnumerator) - interface IDictionary<'Key, 'Value> with + interface IDictionary<'Key, 'Value> with + + member s.Item + with get x = s[x] + and set x v = s[x] <- v - member s.Item - with get x = s[x] - and set x v = s[x] <- v - member s.Keys = ([| for kvp in s -> kvp.Key |] :> ICollection<'Key>) member s.Values = ([| for kvp in s -> kvp.Value |] :> ICollection<'Value>) - member s.Add(k,v) = s[k] <- v + member s.Add(k, v) = s[k] <- v member s.ContainsKey(k) = s.ContainsKey(k) - member s.TryGetValue(k,r) = match s.TryFind k with Some v-> (r <- v; true) | _ -> false + member s.TryGetValue(k, r) = + match s.TryFind k with + | Some v -> + (r <- v + true) + | _ -> false - member s.Remove(k:'Key) = - let res = s.ContainsKey(k) in - s.Remove(k); res + member s.Remove(k: 'Key) = + let res = s.ContainsKey(k) in + s.Remove(k) + res - interface ICollection> with + interface ICollection> with member s.Add(x) = s[x.Key] <- x.Value - member s.Clear() = s.Clear() + member s.Clear() = s.Clear() - member s.Remove(x) = + member s.Remove(x) = match s.TryFind x.Key with - | Some v -> + | Some v -> if Unchecked.equals v x.Value then s.Remove(x.Key) + true | _ -> false @@ -168,9 +181,9 @@ type internal HashMultiMap<'Key,'Value>(size: int, comparer: IEqualityComparer<' | Some v when Unchecked.equals v x.Value -> true | _ -> false - member s.CopyTo(arr,arrIndex) = s |> Seq.iteri (fun j x -> arr[arrIndex+j] <- x) + member s.CopyTo(arr, arrIndex) = + s |> Seq.iteri (fun j x -> arr[arrIndex + j] <- x) member s.IsReadOnly = false member s.Count = s.Count - diff --git a/src/Compiler/Utilities/ImmutableArray.fs b/src/Compiler/Utilities/ImmutableArray.fs index d2c4f424615..e32ce5391b6 100644 --- a/src/Compiler/Utilities/ImmutableArray.fs +++ b/src/Compiler/Utilities/ImmutableArray.fs @@ -5,8 +5,7 @@ open System.Collections.Immutable [] module ImmutableArrayBuilder = - let create size : ImmutableArray<'T>.Builder = - ImmutableArray.CreateBuilder(size) + let create size : ImmutableArray<'T>.Builder = ImmutableArray.CreateBuilder(size) [] module ImmutableArray = @@ -23,8 +22,10 @@ module ImmutableArray = invalidArg "n" "Below zero." let builder = ImmutableArray.CreateBuilder(n) + for i = 0 to n - 1 do builder.Add(f i) + builder.MoveToImmutable() let iter f (arr: ImmutableArray<'T>) = @@ -55,8 +56,10 @@ module ImmutableArray = | 1 -> ImmutableArray.Create(mapper arr[0]) | _ -> let builder = ImmutableArray.CreateBuilder(arr.Length) + for i = 0 to arr.Length - 1 do builder.Add(mapper arr[i]) + builder.MoveToImmutable() let mapi (mapper: int -> 'T -> 'U) (arr: ImmutableArray<'T>) : ImmutableArray<_> = @@ -65,120 +68,155 @@ module ImmutableArray = | 1 -> ImmutableArray.Create(mapper 0 arr[0]) | _ -> let builder = ImmutableArray.CreateBuilder(arr.Length) + for i = 0 to arr.Length - 1 do builder.Add(mapper i arr[i]) + builder.MoveToImmutable() let map2 (mapper: 'T1 -> 'T2 -> 'T) (arr1: ImmutableArray<'T1>) (arr2: ImmutableArray<'T2>) : ImmutableArray<_> = if arr1.Length <> arr2.Length then invalidOp "Block lengths do not match." - + match arr1.Length with | 0 -> ImmutableArray.Empty | 1 -> ImmutableArray.Create(mapper arr1[0] arr2[0]) | n -> let builder = ImmutableArray.CreateBuilder(n) + for i = 0 to n - 1 do builder.Add(mapper arr1[i] arr2[i]) + builder.MoveToImmutable() let mapi2 (mapper: int -> 'T1 -> 'T2 -> 'T) (arr1: ImmutableArray<'T1>) (arr2: ImmutableArray<'T2>) : ImmutableArray<_> = if arr1.Length <> arr2.Length then invalidOp "Block lengths do not match." - + match arr1.Length with | 0 -> ImmutableArray.Empty | 1 -> ImmutableArray.Create(mapper 0 arr1[0] arr2[0]) | n -> let builder = ImmutableArray.CreateBuilder(n) + for i = 0 to n - 1 do builder.Add(mapper i arr1[i] arr2[i]) + builder.MoveToImmutable() let concat (arrs: ImmutableArray>) : ImmutableArray<'T> = match arrs.Length with | 0 -> ImmutableArray.Empty | 1 -> arrs[0] - | 2 -> arrs[0].AddRange(arrs[1]) + | 2 -> arrs[ 0 ].AddRange(arrs[1]) | _ -> - let mutable acc = 0 + let mutable acc = 0 + for h in arrs do acc <- acc + h.Length let builder = ImmutableArray.CreateBuilder(acc) + for i = 0 to arrs.Length - 1 do builder.AddRange(arrs[i]) + builder.MoveToImmutable() let forall predicate (arr: ImmutableArray<'T>) = let len = arr.Length - let rec loop i = i >= len || (predicate arr[i] && loop (i+1)) + + let rec loop i = + i >= len || (predicate arr[i] && loop (i + 1)) + loop 0 let forall2 predicate (arr1: ImmutableArray<'T1>) (arr2: ImmutableArray<'T2>) = if arr1.Length <> arr2.Length then invalidOp "Block lengths do not match." - let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate) + let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (predicate) let len1 = arr1.Length - let rec loop i = i >= len1 || (f.Invoke(arr1[i], arr2[i]) && loop (i+1)) + + let rec loop i = + i >= len1 || (f.Invoke(arr1[i], arr2[i]) && loop (i + 1)) + loop 0 let tryFind predicate (arr: ImmutableArray<'T>) = - let rec loop i = - if i >= arr.Length then None else - if predicate arr[i] then Some arr[i] else loop (i+1) - loop 0 + let rec loop i = + if i >= arr.Length then + None + else if predicate arr[i] then + Some arr[i] + else + loop (i + 1) + + loop 0 let tryFindIndex predicate (arr: ImmutableArray<'T>) = - let len = arr.Length - let rec go n = if n >= len then None elif predicate arr[n] then Some n else go (n+1) - go 0 + let len = arr.Length + + let rec go n = + if n >= len then None + elif predicate arr[n] then Some n + else go (n + 1) + + go 0 let tryPick chooser (arr: ImmutableArray<'T>) = - let rec loop i = - if i >= arr.Length then None else - match chooser arr[i] with - | None -> loop(i+1) - | res -> res - loop 0 + let rec loop i = + if i >= arr.Length then + None + else + match chooser arr[i] with + | None -> loop (i + 1) + | res -> res + + loop 0 - let ofSeq (xs: 'T seq) = - ImmutableArray.CreateRange(xs) + let ofSeq (xs: 'T seq) = ImmutableArray.CreateRange(xs) - let append (arr1: ImmutableArray<'T1>) (arr2: ImmutableArray<'T1>) : ImmutableArray<_> = - arr1.AddRange(arr2) + let append (arr1: ImmutableArray<'T1>) (arr2: ImmutableArray<'T1>) : ImmutableArray<_> = arr1.AddRange(arr2) - let createOne (item: 'T) : ImmutableArray<_> = - ImmutableArray.Create(item) + let createOne (item: 'T) : ImmutableArray<_> = ImmutableArray.Create(item) let filter predicate (arr: ImmutableArray<'T>) : ImmutableArray<'T> = let builder = ImmutableArray.CreateBuilder(arr.Length) + for i = 0 to arr.Length - 1 do if predicate arr[i] then builder.Add(arr[i]) + builder.Capacity <- builder.Count builder.MoveToImmutable() let exists predicate (arr: ImmutableArray<'T>) = let len = arr.Length - let rec loop i = i < len && (predicate arr[i] || loop (i+1)) + + let rec loop i = + i < len && (predicate arr[i] || loop (i + 1)) + len > 0 && loop 0 let choose (chooser: 'T -> 'U option) (arr: ImmutableArray<'T>) : ImmutableArray<'U> = let builder = ImmutableArray.CreateBuilder(arr.Length) + for i = 0 to arr.Length - 1 do let result = chooser arr[i] + if result.IsSome then builder.Add(result.Value) + builder.Capacity <- builder.Count builder.MoveToImmutable() let isEmpty (arr: ImmutableArray<_>) = arr.IsEmpty let fold folder state (arr: ImmutableArray<_>) = - let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(folder) + let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (folder) let mutable state = state - for i = 0 to arr.Length - 1 do + + for i = 0 to arr.Length - 1 do state <- f.Invoke(state, arr[i]) + state diff --git a/src/Compiler/Utilities/InternalCollections.fs b/src/Compiler/Utilities/InternalCollections.fs index a2c3b3ad65a..8ef878530d0 100755 --- a/src/Compiler/Utilities/InternalCollections.fs +++ b/src/Compiler/Utilities/InternalCollections.fs @@ -1,202 +1,241 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. namespace Internal.Utilities.Collections + open System [] -type internal ValueStrength<'T when 'T : not struct> = - | Strong of 'T +type internal ValueStrength<'T when 'T: not struct> = + | Strong of 'T #if FX_NO_GENERIC_WEAKREFERENCE - | Weak of WeakReference + | Weak of WeakReference #else - | Weak of WeakReference<'T> + | Weak of WeakReference<'T> #endif -type internal AgedLookup<'Token, 'Key, 'Value when 'Value : not struct>(keepStrongly:int, areSimilar, ?requiredToKeep, ?keepMax: int) = +type internal AgedLookup<'Token, 'Key, 'Value when 'Value: not struct>(keepStrongly: int, areSimilar, ?requiredToKeep, ?keepMax: int) = /// The list of items stored. Youngest is at the end of the list. /// The choice of order is somewhat arbitrary. If the other way then adding /// items would be O(1) and removing O(N). - let mutable refs:('Key*ValueStrength<'Value>) list = [] + let mutable refs: ('Key * ValueStrength<'Value>) list = [] + let mutable keepStrongly = keepStrongly // The 75 here determines how long the list should be passed the end of strongly held // references. Some operations are O(N) and we don't want to let things get out of // hand. - let keepMax = defaultArg keepMax 75 - let mutable keepMax = max keepStrongly keepMax - let requiredToKeep = defaultArg requiredToKeep (fun _ -> false) - + let keepMax = defaultArg keepMax 75 + let mutable keepMax = max keepStrongly keepMax + let requiredToKeep = defaultArg requiredToKeep (fun _ -> false) + /// Look up a the given key, return None if not found. - let TryPeekKeyValueImpl(data,key) = - let rec Lookup key = function + let TryPeekKeyValueImpl (data, key) = + let rec Lookup key = + function // Treat a list of key-value pairs as a lookup collection. // This function returns true if two keys are the same according to the predicate // function passed in. - | []->None - | (similarKey,value) :: t-> - if areSimilar(key,similarKey) then Some(similarKey,value) - else Lookup key t - Lookup key data - + | [] -> None + | (similarKey, value) :: t -> + if areSimilar (key, similarKey) then + Some(similarKey, value) + else + Lookup key t + + Lookup key data + /// Determines whether a particular key exists. - let Exists(data,key) = TryPeekKeyValueImpl(data,key).IsSome - + let Exists (data, key) = TryPeekKeyValueImpl(data, key).IsSome + /// Set a particular key's value. - let Add(data,key,value) = - data @ [key,value] - + let Add (data, key, value) = data @ [ key, value ] + /// Promote a particular key value. - let Promote (data, key, value) = - (data |> List.filter (fun (similarKey,_)-> not (areSimilar(key,similarKey)))) @ [ (key, value) ] + let Promote (data, key, value) = + (data |> List.filter (fun (similarKey, _) -> not (areSimilar (key, similarKey)))) + @ [ (key, value) ] /// Remove a particular key value. - let RemoveImpl (data, key) = - let keep = data |> List.filter (fun (similarKey,_)-> not (areSimilar(key,similarKey))) + let RemoveImpl (data, key) = + let keep = + data |> List.filter (fun (similarKey, _) -> not (areSimilar (key, similarKey))) + keep - - let TryGetKeyValueImpl(data,key) = - match TryPeekKeyValueImpl(data,key) with - | Some(similarKey, value) as result -> + + let TryGetKeyValueImpl (data, key) = + match TryPeekKeyValueImpl(data, key) with + | Some (similarKey, value) as result -> // If the result existed, move it to the end of the list (more likely to keep it) - result,Promote (data,similarKey,value) - | None -> None,data - + result, Promote(data, similarKey, value) + | None -> None, data + /// Remove weak entries from the list that have been collected. - let FilterAndHold(tok: 'Token) = + let FilterAndHold (tok: 'Token) = ignore tok // reading 'refs' requires a token - [ for key,value in refs do - match value with - | Strong(value) -> yield (key,value) - | Weak(weakReference) -> + + [ + for key, value in refs do + match value with + | Strong (value) -> yield (key, value) + | Weak (weakReference) -> #if FX_NO_GENERIC_WEAKREFERENCE - match weakReference.Target with - | null -> () - | value -> yield key,(value:?>'Value) ] + match weakReference.Target with + | null -> () + | value -> yield key, (value :?> 'Value) + ] #else - match weakReference.TryGetTarget () with - | false, _ -> () - | true, value -> yield key, value ] + match weakReference.TryGetTarget() with + | false, _ -> () + | true, value -> yield key, value + ] #endif - - let AssignWithStrength(tok,newData) = + + let AssignWithStrength (tok, newData) = let actualLength = List.length newData let tossThreshold = max 0 (actualLength - keepMax) // Delete everything less than this threshold let weakThreshold = max 0 (actualLength - keepStrongly) // Weaken everything less than this threshold - - let newData = newData|> List.mapi( fun n kv -> n,kv ) // Place the index. - let newData = newData |> List.filter (fun (n:int,v) -> n >= tossThreshold || requiredToKeep (snd v)) - let newData = - newData - |> List.map( fun (n:int,(k,v)) -> - let handle = - if n List.mapi (fun n kv -> n, kv) // Place the index. + + let newData = + newData + |> List.filter (fun (n: int, v) -> n >= tossThreshold || requiredToKeep (snd v)) + + let newData = + newData + |> List.map (fun (n: int, (k, v)) -> + let handle = + if n < weakThreshold && not (requiredToKeep v) then #if FX_NO_GENERIC_WEAKREFERENCE - Weak(WeakReference(v)) + Weak(WeakReference(v)) #else - Weak(WeakReference<_>(v)) + Weak(WeakReference<_>(v)) #endif - else + else Strong(v) - k,handle ) + + k, handle) + ignore tok // Updating refs requires tok refs <- newData - - member al.TryPeekKeyValue(tok, key) = + + member al.TryPeekKeyValue(tok, key) = // Returns the original key value as well since it may be different depending on equality test. let data = FilterAndHold(tok) - TryPeekKeyValueImpl(data,key) - - member al.TryGetKeyValue(tok, key) = + TryPeekKeyValueImpl(data, key) + + member al.TryGetKeyValue(tok, key) = let data = FilterAndHold(tok) - let result,newData = TryGetKeyValueImpl(data,key) - AssignWithStrength(tok,newData) + let result, newData = TryGetKeyValueImpl(data, key) + AssignWithStrength(tok, newData) result - member al.TryGet(tok, key) = + member al.TryGet(tok, key) = let data = FilterAndHold(tok) - let result,newData = TryGetKeyValueImpl(data,key) - AssignWithStrength(tok,newData) + let result, newData = TryGetKeyValueImpl(data, key) + AssignWithStrength(tok, newData) + match result with - | Some(_,value) -> Some(value) + | Some (_, value) -> Some(value) | None -> None - member al.Put(tok, key,value) = + member al.Put(tok, key, value) = let data = FilterAndHold(tok) - let data = if Exists(data,key) then RemoveImpl (data,key) else data - let data = Add(data,key,value) - AssignWithStrength(tok,data) // This will remove extras - member al.Remove(tok, key) = + let data = + if Exists(data, key) then + RemoveImpl(data, key) + else + data + + let data = Add(data, key, value) + AssignWithStrength(tok, data) // This will remove extras + + member al.Remove(tok, key) = let data = FilterAndHold(tok) - let newData = RemoveImpl (data,key) - AssignWithStrength(tok,newData) + let newData = RemoveImpl(data, key) + AssignWithStrength(tok, newData) member al.Clear(tok) = - let _discards = FilterAndHold(tok) - AssignWithStrength(tok,[]) + let _discards = FilterAndHold(tok) + AssignWithStrength(tok, []) member al.Resize(tok, newKeepStrongly, ?newKeepMax) = - let newKeepMax = defaultArg newKeepMax 75 - keepStrongly <- newKeepStrongly - keepMax <- max newKeepStrongly newKeepMax - let keep = FilterAndHold(tok) - AssignWithStrength(tok,keep) + let newKeepMax = defaultArg newKeepMax 75 + keepStrongly <- newKeepStrongly + keepMax <- max newKeepStrongly newKeepMax + let keep = FilterAndHold(tok) + AssignWithStrength(tok, keep) - +type internal MruCache<'Token, 'Key, 'Value when 'Value: not struct> + ( + keepStrongly, + areSame, + ?isStillValid: 'Key * 'Value -> bool, + ?areSimilar, + ?requiredToKeep, + ?keepMax + ) = -type internal MruCache<'Token, 'Key,'Value when 'Value : not struct>(keepStrongly, areSame, ?isStillValid : 'Key*'Value->bool, ?areSimilar, ?requiredToKeep, ?keepMax) = - /// Default behavior of areSimilar function is areSame. let areSimilar = defaultArg areSimilar areSame - + /// The list of items in the cache. Youngest is at the end of the list. /// The choice of order is somewhat arbitrary. If the other way then adding /// items would be O(1) and removing O(N). - let cache = AgedLookup<'Token, 'Key,'Value>(keepStrongly=keepStrongly,areSimilar=areSimilar,?keepMax=keepMax,?requiredToKeep=requiredToKeep) - + let cache = + AgedLookup<'Token, 'Key, 'Value>( + keepStrongly = keepStrongly, + areSimilar = areSimilar, + ?keepMax = keepMax, + ?requiredToKeep = requiredToKeep + ) + /// Whether or not this result value is still valid. let isStillValid = defaultArg isStillValid (fun _ -> true) - - member bc.ContainsSimilarKey(tok, key) = + + member bc.ContainsSimilarKey(tok, key) = match cache.TryPeekKeyValue(tok, key) with - | Some(_similarKey, _value)-> true + | Some (_similarKey, _value) -> true | None -> false - - member bc.TryGetAny(tok, key) = + + member bc.TryGetAny(tok, key) = match cache.TryPeekKeyValue(tok, key) with - | Some(similarKey, value)-> - if areSame(similarKey,key) then Some(value) - else None + | Some (similarKey, value) -> + if areSame (similarKey, key) then + Some(value) + else + None | None -> None - - member bc.TryGet(tok, key) = + + member bc.TryGet(tok, key) = match cache.TryGetKeyValue(tok, key) with - | Some(similarKey, value) -> - if areSame(similarKey, key) && isStillValid(key,value) then Some value - else None + | Some (similarKey, value) -> + if areSame (similarKey, key) && isStillValid (key, value) then + Some value + else + None | None -> None - member bc.TryGetSimilarAny(tok, key) = + member bc.TryGetSimilarAny(tok, key) = match cache.TryGetKeyValue(tok, key) with - | Some(_, value) -> Some value + | Some (_, value) -> Some value | None -> None - member bc.TryGetSimilar(tok, key) = + member bc.TryGetSimilar(tok, key) = match cache.TryGetKeyValue(tok, key) with - | Some(_, value) -> - if isStillValid(key,value) then Some value - else None + | Some (_, value) -> + if isStillValid (key, value) then + Some value + else + None | None -> None - - member bc.Set(tok, key:'Key,value:'Value) = - cache.Put(tok, key,value) - - member bc.RemoveAnySimilar(tok, key) = - cache.Remove(tok, key) - - member bc.Clear(tok) = - cache.Clear(tok) - + + member bc.Set(tok, key: 'Key, value: 'Value) = cache.Put(tok, key, value) + + member bc.RemoveAnySimilar(tok, key) = cache.Remove(tok, key) + + member bc.Clear(tok) = cache.Clear(tok) + member bc.Resize(tok, newKeepStrongly, ?newKeepMax) = - cache.Resize(tok, newKeepStrongly, ?newKeepMax=newKeepMax) - + cache.Resize(tok, newKeepStrongly, ?newKeepMax = newKeepMax) diff --git a/src/Compiler/Utilities/PathMap.fs b/src/Compiler/Utilities/PathMap.fs index 7d04034a219..f11edef6a82 100644 --- a/src/Compiler/Utilities/PathMap.fs +++ b/src/Compiler/Utilities/PathMap.fs @@ -17,20 +17,22 @@ module internal PathMap = let empty = PathMap Map.empty - let addMapping (src : string) (dst : string) (PathMap map) : PathMap = + let addMapping (src: string) (dst: string) (PathMap map) : PathMap = // Normalise the path let normalSrc = FileSystem.GetFullPathShim src let oldPrefix = - if normalSrc.EndsWith dirSepStr then normalSrc - else normalSrc + dirSepStr + if normalSrc.EndsWith dirSepStr then + normalSrc + else + normalSrc + dirSepStr // Always add a path separator map |> Map.add oldPrefix dst |> PathMap // Map a file path with its replacement. // This logic replicates C#'s PathUtilities.NormalizePathPrefix - let apply (PathMap map) (filePath : string) : string = + let apply (PathMap map) (filePath: string) : string = // Find the first key in the path map that matches a prefix of the // normalized path. We expect the client to use consistent capitalization; // we use ordinal (case-sensitive) comparisons. @@ -40,23 +42,26 @@ module internal PathMap = // to check if it was a partial match // e.g. for the map /goo=/bar and file name /goooo if filePath.StartsWith(oldPrefix, StringComparison.Ordinal) then - let replacement = replacementPrefix + filePath.Substring (oldPrefix.Length - 1) + let replacement = replacementPrefix + filePath.Substring(oldPrefix.Length - 1) // Normalize the path separators if used uniformly in the replacement let hasSlash = replacementPrefix.IndexOf '/' >= 0 let hasBackslash = replacementPrefix.IndexOf '\\' >= 0 - if hasSlash && not hasBackslash then replacement.Replace('\\', '/') - elif hasBackslash && not hasSlash then replacement.Replace('/', '\\') - else replacement + if hasSlash && not hasBackslash then + replacement.Replace('\\', '/') + elif hasBackslash && not hasSlash then + replacement.Replace('/', '\\') + else + replacement |> Some else - None - ) + None) |> Option.defaultValue filePath - let applyDir pathMap (dirName : string) : string = - if dirName.EndsWith dirSepStr then apply pathMap dirName + let applyDir pathMap (dirName: string) : string = + if dirName.EndsWith dirSepStr then + apply pathMap dirName else let mapped = apply pathMap (dirName + dirSepStr) - mapped.TrimEnd (Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + mapped.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) diff --git a/src/Compiler/Utilities/QueueList.fs b/src/Compiler/Utilities/QueueList.fs index 19591b66c8b..2cab0fe0b70 100644 --- a/src/Compiler/Utilities/QueueList.fs +++ b/src/Compiler/Utilities/QueueList.fs @@ -1,81 +1,98 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. -namespace Internal.Utilities.Collections +namespace Internal.Utilities.Collections open System.Collections open System.Collections.Generic /// Iterable functional collection with O(1) append-1 time. Useful for data structures where elements get added at the -/// end but the collection must occasionally be iterated. Iteration is slower and may allocate because +/// end but the collection must occasionally be iterated. Iteration is slower and may allocate because /// a suffix of elements is stored in reverse order. /// /// The type doesn't support structural hashing or comparison. -type internal QueueList<'T>(firstElementsIn:'T list, lastElementsRevIn: 'T list, numLastElementsIn: int) = +type internal QueueList<'T>(firstElementsIn: 'T list, lastElementsRevIn: 'T list, numLastElementsIn: int) = let numFirstElements = List.length firstElementsIn // Push the lastElementsRev onto the firstElements every so often. let push = numLastElementsIn > numFirstElements / 5 - + // Compute the contents after pushing. - let firstElements = if push then List.append firstElementsIn (List.rev lastElementsRevIn) else firstElementsIn + let firstElements = + if push then + List.append firstElementsIn (List.rev lastElementsRevIn) + else + firstElementsIn + let lastElementsRev = if push then [] else lastElementsRevIn let numLastElements = if push then 0 else numLastElementsIn // Compute the last elements on demand. - let lastElements() = if push then [] else List.rev lastElementsRev + let lastElements () = + if push then + [] + else + List.rev lastElementsRev static let empty = QueueList<'T>([], [], 0) - static member Empty : QueueList<'T> = empty + static member Empty: QueueList<'T> = empty - new (xs:'T list) = QueueList(xs,[],0) - - member x.ToList() = if push then firstElements else List.append firstElements (lastElements()) + new(xs: 'T list) = QueueList(xs, [], 0) + + member x.ToList() = + if push then + firstElements + else + List.append firstElements (lastElements ()) member x.FirstElements = firstElements - member x.LastElements = lastElements() + member x.LastElements = lastElements () /// This operation is O(1), unless a push happens, which is rare. - member x.AppendOne(y) = QueueList(firstElements, y :: lastElementsRev, numLastElements+1) + member x.AppendOne(y) = + QueueList(firstElements, y :: lastElementsRev, numLastElements + 1) - member x.Append(ys:seq<_>) = + member x.Append(ys: seq<_>) = let newElements = Seq.toList ys let newLength = List.length newElements let lastElementsRevIn = List.rev newElements @ lastElementsRev QueueList(firstElements, lastElementsRevIn, numLastElementsIn + newLength) - + // This operation is O(n) anyway, so executing ToList() here is OK - interface IEnumerable<'T> with - member x.GetEnumerator() : IEnumerator<'T> = (x.ToList() :> IEnumerable<_>).GetEnumerator() + interface IEnumerable<'T> with + member x.GetEnumerator() : IEnumerator<'T> = + (x.ToList() :> IEnumerable<_>).GetEnumerator() - interface IEnumerable with - member x.GetEnumerator() : IEnumerator = ((x :> IEnumerable<'T>).GetEnumerator() :> IEnumerator) + interface IEnumerable with + member x.GetEnumerator() : IEnumerator = + ((x :> IEnumerable<'T>).GetEnumerator() :> IEnumerator) module internal QueueList = let empty<'T> : QueueList<'T> = QueueList<'T>.Empty - let ofSeq (x:seq<_>) = QueueList(List.ofSeq x) + let ofSeq (x: seq<_>) = QueueList(List.ofSeq x) - let rec iter f (x:QueueList<_>) = Seq.iter f x + let rec iter f (x: QueueList<_>) = Seq.iter f x - let rec map f (x:QueueList<_>) = ofSeq (Seq.map f x) + let rec map f (x: QueueList<_>) = ofSeq (Seq.map f x) - let rec exists f (x:QueueList<_>) = Seq.exists f x + let rec exists f (x: QueueList<_>) = Seq.exists f x - let rec filter f (x:QueueList<_>) = ofSeq (Seq.filter f x) + let rec filter f (x: QueueList<_>) = ofSeq (Seq.filter f x) - let rec foldBack f (x:QueueList<_>) acc = List.foldBack f x.FirstElements (List.foldBack f x.LastElements acc) + let rec foldBack f (x: QueueList<_>) acc = + List.foldBack f x.FirstElements (List.foldBack f x.LastElements acc) - let forall f (x:QueueList<_>) = Seq.forall f x + let forall f (x: QueueList<_>) = Seq.forall f x - let ofList (x:_ list) = QueueList(x) + let ofList (x: _ list) = QueueList(x) - let toList (x:QueueList<_>) = Seq.toList x + let toList (x: QueueList<_>) = Seq.toList x - let tryFind f (x:QueueList<_>) = Seq.tryFind f x + let tryFind f (x: QueueList<_>) = Seq.tryFind f x - let one x = QueueList [x] + let one x = QueueList [ x ] - let appendOne (x:QueueList<_>) y = x.AppendOne(y) + let appendOne (x: QueueList<_>) y = x.AppendOne(y) - let append (x:QueueList<_>) (ys:QueueList<_>) = x.Append(ys) + let append (x: QueueList<_>) (ys: QueueList<_>) = x.Append(ys) diff --git a/src/Compiler/Utilities/ResizeArray.fs b/src/Compiler/Utilities/ResizeArray.fs index e2f865df21a..a54521525f5 100644 --- a/src/Compiler/Utilities/ResizeArray.fs +++ b/src/Compiler/Utilities/ResizeArray.fs @@ -7,175 +7,244 @@ open FSharp.Core.OptimizedClosures [] module internal ResizeArray = - let length (arr: ResizeArray<'T>) = arr.Count + let length (arr: ResizeArray<'T>) = arr.Count - let get (arr: ResizeArray<'T>) (n: int) = arr[n] + let get (arr: ResizeArray<'T>) (n: int) = arr[n] - let set (arr: ResizeArray<'T>) (n: int) (x:'T) = arr[n] <- x + let set (arr: ResizeArray<'T>) (n: int) (x: 'T) = arr[n] <- x - let create (n: int) x = ResizeArray<_>(seq { for _ in 1 .. n -> x }) + let create (n: int) x = + ResizeArray<_>(seq { for _ in 1..n -> x }) - let init (n: int) (f: int -> 'T) = ResizeArray<_>(seq { for i in 0 .. n-1 -> f i }) + let init (n: int) (f: int -> 'T) = + ResizeArray<_>(seq { for i in 0 .. n - 1 -> f i }) let blit (arr1: ResizeArray<'T>) start1 (arr2: ResizeArray<'T>) start2 len = - if start1 < 0 then invalidArg "start1" "index must be positive" - if start2 < 0 then invalidArg "start2" "index must be positive" - if len < 0 then invalidArg "len" "length must be positive" - if start1 + len > length arr1 then invalidArg "start1" "(start1+len) out of range" - if start2 + len > length arr2 then invalidArg "start2" "(start2+len) out of range" - for i = 0 to len - 1 do - arr2[start2+i] <- arr1[start1 + i] + if start1 < 0 then + invalidArg "start1" "index must be positive" - let concat (arrs: ResizeArray<'T> list) = ResizeArray<_>(seq { for arr in arrs do for x in arr do yield x }) + if start2 < 0 then + invalidArg "start2" "index must be positive" - let append (arr1: ResizeArray<'T>) (arr2: ResizeArray<'T>) = concat [arr1; arr2] + if len < 0 then + invalidArg "len" "length must be positive" + + if start1 + len > length arr1 then + invalidArg "start1" "(start1+len) out of range" + + if start2 + len > length arr2 then + invalidArg "start2" "(start2+len) out of range" + + for i = 0 to len - 1 do + arr2[start2 + i] <- arr1[start1 + i] + + let concat (arrs: ResizeArray<'T> list) = + ResizeArray<_>( + seq { + for arr in arrs do + for x in arr do + yield x + } + ) + + let append (arr1: ResizeArray<'T>) (arr2: ResizeArray<'T>) = concat [ arr1; arr2 ] let sub (arr: ResizeArray<'T>) start len = - if start < 0 then invalidArg "start" "index must be positive" - if len < 0 then invalidArg "len" "length must be positive" - if start + len > length arr then invalidArg "len" "length must be positive" - ResizeArray<_>(seq { for i in start .. start+len-1 -> arr[i] }) - - let fill (arr: ResizeArray<'T>) (start: int) (len: int) (x:'T) = - if start < 0 then invalidArg "start" "index must be positive" - if len < 0 then invalidArg "len" "length must be positive" - if start + len > length arr then invalidArg "len" "length must be positive" - for i = start to start + len - 1 do + if start < 0 then + invalidArg "start" "index must be positive" + + if len < 0 then + invalidArg "len" "length must be positive" + + if start + len > length arr then + invalidArg "len" "length must be positive" + + ResizeArray<_>(seq { for i in start .. start + len - 1 -> arr[i] }) + + let fill (arr: ResizeArray<'T>) (start: int) (len: int) (x: 'T) = + if start < 0 then + invalidArg "start" "index must be positive" + + if len < 0 then + invalidArg "len" "length must be positive" + + if start + len > length arr then + invalidArg "len" "length must be positive" + + for i = start to start + len - 1 do arr[i] <- x - let copy (arr: ResizeArray<'T>) = ResizeArray<_>(arr) + let copy (arr: ResizeArray<'T>) = ResizeArray<_>(arr) let toList (arr: ResizeArray<_>) = let mutable res = [] + for i = length arr - 1 downto 0 do res <- arr[i] :: res + res let ofList (l: _ list) = let len = l.Length let res = ResizeArray<_>(len) - let rec add = function - | [] -> () - | e :: l -> res.Add(e); add l + + let rec add = + function + | [] -> () + | e :: l -> + res.Add(e) + add l + add l res - let iter f (arr: ResizeArray<_>) = + let iter f (arr: ResizeArray<_>) = for i = 0 to arr.Count - 1 do f arr[i] let map f (arr: ResizeArray<_>) = let len = length arr let res = ResizeArray<_>(len) + for i = 0 to len - 1 do res.Add(f arr[i]) + res let mapi f (arr: ResizeArray<_>) = - let f = FSharpFunc<_,_,_>.Adapt(f) + let f = FSharpFunc<_, _, _>.Adapt (f) let len = length arr let res = ResizeArray<_>(len) + for i = 0 to len - 1 do res.Add(f.Invoke(i, arr[i])) + res - + let iteri f (arr: ResizeArray<_>) = - let f = FSharpFunc<_,_,_>.Adapt(f) + let f = FSharpFunc<_, _, _>.Adapt (f) + for i = 0 to arr.Count - 1 do f.Invoke(i, arr[i]) let exists (f: 'T -> bool) (arr: ResizeArray<'T>) = - let len = length arr - let rec loop i = i < len && (f arr[i] || loop (i+1)) + let len = length arr + let rec loop i = i < len && (f arr[i] || loop (i + 1)) loop 0 let forall f (arr: ResizeArray<_>) = let len = length arr - let rec loop i = i >= len || (f arr[i] && loop (i+1)) + let rec loop i = i >= len || (f arr[i] && loop (i + 1)) loop 0 - let indexNotFound() = raise (System.Collections.Generic.KeyNotFoundException("An index satisfying the predicate was not found in the collection")) + let indexNotFound () = + raise (System.Collections.Generic.KeyNotFoundException("An index satisfying the predicate was not found in the collection")) - let find f (arr: ResizeArray<_>) = - let rec loop i = - if i >= length arr then indexNotFound() + let find f (arr: ResizeArray<_>) = + let rec loop i = + if i >= length arr then indexNotFound () elif f arr[i] then arr[i] - else loop (i+1) + else loop (i + 1) + loop 0 let tryPick f (arr: ResizeArray<_>) = - let rec loop i = - if i >= length arr then None else - match f arr[i] with - | None -> loop(i+1) - | res -> res + let rec loop i = + if i >= length arr then + None + else + match f arr[i] with + | None -> loop (i + 1) + | res -> res + loop 0 - let tryFind f (arr: ResizeArray<_>) = - let rec loop i = + let tryFind f (arr: ResizeArray<_>) = + let rec loop i = if i >= length arr then None elif f arr[i] then Some arr[i] - else loop (i+1) + else loop (i + 1) + loop 0 - let iter2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = - let f = FSharpFunc<_,_,_>.Adapt(f) + let iter2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = + let f = FSharpFunc<_, _, _>.Adapt (f) let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" - for i = 0 to len1 - 1 do + + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + + for i = 0 to len1 - 1 do f.Invoke(arr1[i], arr2[i]) - let map2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = - let f = FSharpFunc<_,_,_>.Adapt(f) + let map2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = + let f = FSharpFunc<_, _, _>.Adapt (f) let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" + + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + let res = ResizeArray<_>(len1) + for i = 0 to len1 - 1 do res.Add(f.Invoke(arr1[i], arr2[i])) + res - let choose f (arr: ResizeArray<_>) = - let res = ResizeArray<_>() + let choose f (arr: ResizeArray<_>) = + let res = ResizeArray<_>() + for i = 0 to length arr - 1 do - match f arr[i] with + match f arr[i] with | None -> () | Some b -> res.Add(b) + res - let filter f (arr: ResizeArray<_>) = - let res = ResizeArray<_>() - for i = 0 to length arr - 1 do - let x = arr[i] + let filter f (arr: ResizeArray<_>) = + let res = ResizeArray<_>() + + for i = 0 to length arr - 1 do + let x = arr[i] if f x then res.Add(x) + res - let partition f (arr: ResizeArray<_>) = - let res1 = ResizeArray<_>() - let res2 = ResizeArray<_>() - for i = 0 to length arr - 1 do - let x = arr[i] - if f x then res1.Add(x) else res2.Add(x) - res1, res2 - - let rev (arr: ResizeArray<_>) = - let len = length arr - let res = ResizeArray<_>(len) - for i = len - 1 downto 0 do - res.Add(arr[i]) - res - - let foldBack (f : 'T -> 'State -> 'State) (arr: ResizeArray<'T>) (acc: 'State) = - let mutable res = acc - let len = length arr - for i = len - 1 downto 0 do + let partition f (arr: ResizeArray<_>) = + let res1 = ResizeArray<_>() + let res2 = ResizeArray<_>() + + for i = 0 to length arr - 1 do + let x = arr[i] + if f x then res1.Add(x) else res2.Add(x) + + res1, res2 + + let rev (arr: ResizeArray<_>) = + let len = length arr + let res = ResizeArray<_>(len) + + for i = len - 1 downto 0 do + res.Add(arr[i]) + + res + + let foldBack (f: 'T -> 'State -> 'State) (arr: ResizeArray<'T>) (acc: 'State) = + let mutable res = acc + let len = length arr + + for i = len - 1 downto 0 do res <- f (get arr i) res + res - let fold (f : 'State -> 'T -> 'State) (acc: 'State) (arr: ResizeArray<'T>) = - let mutable res = acc - let len = length arr - for i = 0 to len - 1 do + let fold (f: 'State -> 'T -> 'State) (acc: 'State) (arr: ResizeArray<'T>) = + let mutable res = acc + let len = length arr + + for i = 0 to len - 1 do res <- f res (get arr i) + res let toArray (arr: ResizeArray<'T>) = arr.ToArray() @@ -184,109 +253,155 @@ module internal ResizeArray = let toSeq (arr: ResizeArray<'T>) = Seq.readonly arr - let sort f (arr: ResizeArray<'T>) = arr.Sort (System.Comparison(f)) - - let sortBy f (arr: ResizeArray<'T>) = arr.Sort (System.Comparison(fun x y -> compare (f x) (f y))) + let sort f (arr: ResizeArray<'T>) = arr.Sort(System.Comparison(f)) + let sortBy f (arr: ResizeArray<'T>) = + arr.Sort(System.Comparison(fun x y -> compare (f x) (f y))) let exists2 f (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) = let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" - let rec loop i = i < len1 && (f arr1[i] arr2[i] || loop (i+1)) + + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + + let rec loop i = + i < len1 && (f arr1[i] arr2[i] || loop (i + 1)) + loop 0 let findIndex f (arr: ResizeArray<_>) = - let rec go n = if n >= length arr then indexNotFound() elif f arr[n] then n else go (n+1) + let rec go n = + if n >= length arr then indexNotFound () + elif f arr[n] then n + else go (n + 1) + go 0 let findIndexi f (arr: ResizeArray<_>) = - let rec go n = if n >= length arr then indexNotFound() elif f n arr[n] then n else go (n+1) + let rec go n = + if n >= length arr then indexNotFound () + elif f n arr[n] then n + else go (n + 1) + go 0 - let foldSub f acc (arr: ResizeArray<_>) start fin = + let foldSub f acc (arr: ResizeArray<_>) start fin = let mutable res = acc + for i = start to fin do - res <- f res arr[i] + res <- f res arr[i] + res - let foldBackSub f (arr: ResizeArray<_>) start fin acc = - let mutable res = acc + let foldBackSub f (arr: ResizeArray<_>) start fin acc = + let mutable res = acc + for i = fin downto start do res <- f arr[i] res + res - let reduce f (arr : ResizeArray<_>) = + let reduce f (arr: ResizeArray<_>) = let arrn = length arr - if arrn = 0 then invalidArg "arr" "the input array may not be empty" - else foldSub f arr[0] arr 1 (arrn - 1) - - let reduceBack f (arr: ResizeArray<_>) = + + if arrn = 0 then + invalidArg "arr" "the input array may not be empty" + else + foldSub f arr[0] arr 1 (arrn - 1) + + let reduceBack f (arr: ResizeArray<_>) = let arrn = length arr - if arrn = 0 then invalidArg "arr" "the input array may not be empty" - else foldBackSub f arr 0 (arrn - 2) arr[arrn - 1] + + if arrn = 0 then + invalidArg "arr" "the input array may not be empty" + else + foldBackSub f arr 0 (arrn - 2) arr[arrn - 1] let fold2 f (acc: 'T) (arr1: ResizeArray<'T1>) (arr2: ResizeArray<'T2>) = - let f = FSharpFunc<_,_,_,_>.Adapt(f) - let mutable res = acc + let f = FSharpFunc<_, _, _, _>.Adapt (f) + let mutable res = acc let len = length arr1 - if len <> length arr2 then invalidArg "arr2" "the arrays have different lengths" + + if len <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + for i = 0 to len - 1 do - res <- f.Invoke(res,arr1[i],arr2[i]) + res <- f.Invoke(res, arr1[i], arr2[i]) + res let foldBack2 f (arr1: ResizeArray<'T1>) (arr2: ResizeArray<'T2>) (acc: 'b) = - let f = FSharpFunc<_,_,_,_>.Adapt(f) - let mutable res = acc + let f = FSharpFunc<_, _, _, _>.Adapt (f) + let mutable res = acc let len = length arr1 - if len <> length arr2 then invalidArg "arr2" "the arrays have different lengths" - for i = len - 1 downto 0 do - res <- f.Invoke(arr1[i],arr2[i],res) + + if len <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + + for i = len - 1 downto 0 do + res <- f.Invoke(arr1[i], arr2[i], res) + res - let forall2 f (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) = + let forall2 f (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) = let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" - let rec loop i = i >= len1 || (f arr1[i] arr2[i] && loop (i+1)) + + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + + let rec loop i = + i >= len1 || (f arr1[i] arr2[i] && loop (i + 1)) + loop 0 - + let isEmpty (arr: ResizeArray<_>) = length (arr: ResizeArray<_>) = 0 - + let iteri2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = - let f = FSharpFunc<_,_,_,_>.Adapt(f) + let f = FSharpFunc<_, _, _, _>.Adapt (f) let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" - for i = 0 to len1 - 1 do - f.Invoke(i,arr1[i], arr2[i]) - let mapi2 (f: int -> 'T -> 'b -> 'c) (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = - let f = FSharpFunc<_,_,_,_>.Adapt(f) + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + + for i = 0 to len1 - 1 do + f.Invoke(i, arr1[i], arr2[i]) + + let mapi2 (f: int -> 'T -> 'b -> 'c) (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) = + let f = FSharpFunc<_, _, _, _>.Adapt (f) let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" + + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + init len1 (fun i -> f.Invoke(i, arr1[i], arr2[i])) - let scanBackSub f (arr: ResizeArray<'T>) start fin acc = - let f = FSharpFunc<_,_,_>.Adapt(f) + let scanBackSub f (arr: ResizeArray<'T>) start fin acc = + let f = FSharpFunc<_, _, _>.Adapt (f) let mutable state = acc - let res = create (2+fin-start) acc + let res = create (2 + fin - start) acc + for i = fin downto start do state <- f.Invoke(arr[i], state) res[i - start] <- state + res - let scanSub f acc (arr : ResizeArray<'T>) start fin = - let f = FSharpFunc<_,_,_>.Adapt(f) + let scanSub f acc (arr: ResizeArray<'T>) start fin = + let f = FSharpFunc<_, _, _>.Adapt (f) let mutable state = acc - let res = create (fin-start+2) acc + let res = create (fin - start + 2) acc + for i = start to fin do state <- f.Invoke(state, arr[i]) - res[i - start+1] <- state + res[i - start + 1] <- state + res - let scan f acc (arr : ResizeArray<'T>) = + let scan f acc (arr: ResizeArray<'T>) = let arrn = length arr scanSub f acc arr 0 (arrn - 1) - let scanBack f (arr : ResizeArray<'T>) acc = + let scanBack f (arr: ResizeArray<'T>) acc = let arrn = length arr scanBackSub f arr 0 (arrn - 1) acc @@ -295,27 +410,38 @@ module internal ResizeArray = res.Add(x) res - let tryFindIndex f (arr: ResizeArray<'T>) = - let rec go n = if n >= length arr then None elif f arr[n] then Some n else go (n+1) + let tryFindIndex f (arr: ResizeArray<'T>) = + let rec go n = + if n >= length arr then None + elif f arr[n] then Some n + else go (n + 1) + go 0 - - let tryFindIndexi f (arr: ResizeArray<'T>) = - let rec go n = if n >= length arr then None elif f n arr[n] then Some n else go (n+1) + + let tryFindIndexi f (arr: ResizeArray<'T>) = + let rec go n = + if n >= length arr then None + elif f n arr[n] then Some n + else go (n + 1) + go 0 - - let zip (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) = - let len1 = length arr1 - if len1 <> length arr2 then invalidArg "arr2" "the arrays have different lengths" + + let zip (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) = + let len1 = length arr1 + + if len1 <> length arr2 then + invalidArg "arr2" "the arrays have different lengths" + init len1 (fun i -> arr1[i], arr2[i]) - let unzip (arr: ResizeArray<_>) = + let unzip (arr: ResizeArray<_>) = let len = length arr let res1 = ResizeArray<_>(len) let res2 = ResizeArray<_>(len) - for i = 0 to len - 1 do - let x,y = arr[i] + + for i = 0 to len - 1 do + let x, y = arr[i] res1.Add(x) res2.Add(y) - res1,res2 - + res1, res2 diff --git a/src/Compiler/Utilities/RidHelpers.fs b/src/Compiler/Utilities/RidHelpers.fs index 3101bc5d960..16b18917c4e 100644 --- a/src/Compiler/Utilities/RidHelpers.fs +++ b/src/Compiler/Utilities/RidHelpers.fs @@ -10,14 +10,20 @@ module internal RidHelpers = // Where rid is: win, win-x64, win-x86, osx-x64, linux-x64 etc ... let probingRids, baseRid, platformRid = let processArchitecture = RuntimeInformation.ProcessArchitecture + let baseRid = - if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then "win" - elif RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then "osx" - else "linux" + if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then + "win" + elif RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then + "osx" + else + "linux" + let platformRid = match processArchitecture with - | Architecture.X64 -> baseRid + "-x64" + | Architecture.X64 -> baseRid + "-x64" | Architecture.X86 -> baseRid + "-x86" | Architecture.Arm64 -> baseRid + "-arm64" | _ -> baseRid + "-arm" + [| "any"; baseRid; platformRid |], baseRid, platformRid diff --git a/src/Compiler/Utilities/TaggedCollections.fs b/src/Compiler/Utilities/TaggedCollections.fs index df9167ef792..00e0ceb8d1d 100644 --- a/src/Compiler/Utilities/TaggedCollections.fs +++ b/src/Compiler/Utilities/TaggedCollections.fs @@ -18,7 +18,7 @@ type internal SetTree<'T>(k: 'T) = [] [] [] -type internal SetTreeNode<'T>(v:'T, left:SetTree<'T>, right: SetTree<'T>, h: int) = +type internal SetTreeNode<'T>(v: 'T, left: SetTree<'T>, right: SetTree<'T>, h: int) = inherit SetTree<'T>(v) member _.Left = left @@ -26,88 +26,101 @@ type internal SetTreeNode<'T>(v:'T, left:SetTree<'T>, right: SetTree<'T>, h: int member _.Height = h [] -module SetTree = +module SetTree = let empty = null - let inline isEmpty (t:SetTree<'T>) = isNull t + let inline isEmpty (t: SetTree<'T>) = isNull t - let rec countAux (t:SetTree<'T>) acc = + let rec countAux (t: SetTree<'T>) acc = if isEmpty t then acc else - match t with - | :? SetTreeNode<'T> as tn -> countAux tn.Left (countAux tn.Right (acc+1)) - | _ -> acc+1 + match t with + | :? SetTreeNode<'T> as tn -> countAux tn.Left (countAux tn.Right (acc + 1)) + | _ -> acc + 1 let count s = countAux s 0 - let inline height (t:SetTree<'T>) = - if isEmpty t then 0 + let inline height (t: SetTree<'T>) = + if isEmpty t then + 0 else - match t with + match t with | :? SetTreeNode<'T> as tn -> tn.Height | _ -> 1 [] let tolerance = 2 - let mk l k r : SetTree<'T> = - let hl = height l - let hr = height r + let mk l k r : SetTree<'T> = + let hl = height l + let hr = height r let m = if hl < hr then hr else hl + if m = 0 then // m=0 ~ isEmpty l && isEmpty r SetTree k else - SetTreeNode (k, l, r, m+1) :> SetTree<'T> + SetTreeNode(k, l, r, m + 1) :> SetTree<'T> - let inline private asNode(value:SetTree<'T>) : SetTreeNode<'T> = - value :?> SetTreeNode<'T> + let inline private asNode (value: SetTree<'T>) : SetTreeNode<'T> = value :?> SetTreeNode<'T> let rebalance t1 v t2 = - let t1h = height t1 - let t2h = height t2 - if t2h > t1h + tolerance then // right is heavier than left - let t2' = asNode(t2) - // one of the nodes must have height > height t1 + 1 - if height t2'.Left > t1h + 1 then // balance left: combination - let t2l = asNode(t2'.Left) - mk (mk t1 v t2l.Left) t2l.Key (mk t2l.Right t2'.Key t2'.Right) + let t1h = height t1 + let t2h = height t2 + + if t2h > t1h + tolerance then // right is heavier than left + let t2' = asNode (t2) + // one of the nodes must have height > height t1 + 1 + if height t2'.Left > t1h + 1 then // balance left: combination + let t2l = asNode (t2'.Left) + mk (mk t1 v t2l.Left) t2l.Key (mk t2l.Right t2'.Key t2'.Right) else // rotate left mk (mk t1 v t2'.Left) t2.Key t2'.Right + else if t1h > t2h + tolerance then // left is heavier than right + let t1' = asNode (t1) + // one of the nodes must have height > height t2 + 1 + if height t1'.Right > t2h + 1 then + // balance right: combination + let t1r = asNode (t1'.Right) + mk (mk t1'.Left t1.Key t1r.Left) t1r.Key (mk t1r.Right v t2) + else + mk t1'.Left t1'.Key (mk t1'.Right v t2) else - if t1h > t2h + tolerance then // left is heavier than right - let t1' = asNode(t1) - // one of the nodes must have height > height t2 + 1 - if height t1'.Right > t2h + 1 then - // balance right: combination - let t1r = asNode(t1'.Right) - mk (mk t1'.Left t1.Key t1r.Left) t1r.Key (mk t1r.Right v t2) - else - mk t1'.Left t1'.Key (mk t1'.Right v t2) - else mk t1 v t2 + mk t1 v t2 - let rec add (comparer: IComparer<'T>) k (t:SetTree<'T>) : SetTree<'T> = - if isEmpty t then SetTree k + let rec add (comparer: IComparer<'T>) k (t: SetTree<'T>) : SetTree<'T> = + if isEmpty t then + SetTree k else let c = comparer.Compare(k, t.Key) + match t with | :? SetTreeNode<'T> as tn -> - if c < 0 then rebalance (add comparer k tn.Left) tn.Key tn.Right - elif c = 0 then t - else rebalance tn.Left tn.Key (add comparer k tn.Right) - | _ -> - // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated - let c = comparer.Compare(k, t.Key) - if c < 0 then SetTreeNode (k, empty, t, 2) :> SetTree<'T> - elif c = 0 then t - else SetTreeNode (k, t, empty, 2) :> SetTree<'T> - - let rec balance comparer (t1:SetTree<'T>) k (t2:SetTree<'T>) = - // Given t1 < k < t2 where t1 and t2 are "balanced", + if c < 0 then + rebalance (add comparer k tn.Left) tn.Key tn.Right + elif c = 0 then + t + else + rebalance tn.Left tn.Key (add comparer k tn.Right) + | _ -> + // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated + let c = comparer.Compare(k, t.Key) + + if c < 0 then + SetTreeNode(k, empty, t, 2) :> SetTree<'T> + elif c = 0 then + t + else + SetTreeNode(k, t, empty, 2) :> SetTree<'T> + + let rec balance comparer (t1: SetTree<'T>) k (t2: SetTree<'T>) = + // Given t1 < k < t2 where t1 and t2 are "balanced", // return a balanced tree for . // Recall: balance means subtrees heights differ by at most "tolerance" - if isEmpty t1 then add comparer k t2 // drop t1 = empty - elif isEmpty t2 then add comparer k t1 // drop t2 = empty + if isEmpty t1 then + add comparer k t2 // drop t1 = empty + elif isEmpty t2 then + add comparer k t1 // drop t2 = empty else match t1 with | :? SetTreeNode<'T> as t1n -> @@ -116,312 +129,378 @@ module SetTree = // Have: (t1l < k1 < t1r) < k < (t2l < k2 < t2r) // Either (a) h1, h2 differ by at most 2 - no rebalance needed. // (b) h1 too small, i.e. h1+2 < h2 - // (c) h2 too small, i.e. h2+2 < h1 + // (c) h2 too small, i.e. h2+2 < h1 if t1n.Height + tolerance < t2n.Height then - // case: b, h1 too small - // push t1 into low side of t2, may increase height by 1 so rebalance + // case: b, h1 too small + // push t1 into low side of t2, may increase height by 1 so rebalance rebalance (balance comparer t1 k t2n.Left) t2n.Key t2n.Right elif t2n.Height + tolerance < t1n.Height then - // case: c, h2 too small - // push t2 into high side of t1, may increase height by 1 so rebalance + // case: c, h2 too small + // push t2 into high side of t1, may increase height by 1 so rebalance rebalance t1n.Left t1n.Key (balance comparer t1n.Right k t2) else - // case: a, h1 and h2 meet balance requirement + // case: a, h1 and h2 meet balance requirement mk t1 k t2 | _ -> add comparer k (add comparer t2.Key t1) | _ -> add comparer k (add comparer t1.Key t2) - let rec split (comparer: IComparer<'T>) pivot (t:SetTree<'T>) = + let rec split (comparer: IComparer<'T>) pivot (t: SetTree<'T>) = // Given a pivot and a set t - // Return { x in t s.t. x < pivot }, pivot in t?, { x in t s.t. x > pivot } - if isEmpty t then empty, false, empty + // Return { x in t s.t. x < pivot }, pivot in t?, { x in t s.t. x > pivot } + if isEmpty t then + empty, false, empty else match t with | :? SetTreeNode<'T> as tn -> let c = comparer.Compare(pivot, tn.Key) - if c < 0 then // pivot t1 + + if c < 0 then // pivot t1 let t11Lo, havePivot, t11Hi = split comparer pivot tn.Left t11Lo, havePivot, balance comparer t11Hi tn.Key tn.Right - elif c = 0 then // pivot is k1 + elif c = 0 then // pivot is k1 tn.Left, true, tn.Right - else // pivot t2 + else // pivot t2 let t12Lo, havePivot, t12Hi = split comparer pivot tn.Right balance comparer tn.Left tn.Key t12Lo, havePivot, t12Hi | _ -> let c = comparer.Compare(t.Key, pivot) - if c < 0 then t, false, empty // singleton under pivot - elif c = 0 then empty, true, empty // singleton is pivot - else empty, false, t // singleton over pivot - - let rec spliceOutSuccessor (t:SetTree<'T>) = - if isEmpty t then failwith "internal error: Set.spliceOutSuccessor" + + if c < 0 then t, false, empty // singleton under pivot + elif c = 0 then empty, true, empty // singleton is pivot + else empty, false, t // singleton over pivot + + let rec spliceOutSuccessor (t: SetTree<'T>) = + if isEmpty t then + failwith "internal error: Set.spliceOutSuccessor" else - match t with + match t with | :? SetTreeNode<'T> as tn -> - if isEmpty tn.Left then tn.Key, tn.Right - else let k3, l' = spliceOutSuccessor tn.Left in k3, mk l' tn.Key tn.Right + if isEmpty tn.Left then + tn.Key, tn.Right + else + let k3, l' = spliceOutSuccessor tn.Left in k3, mk l' tn.Key tn.Right | _ -> t.Key, empty - let rec remove (comparer: IComparer<'T>) k (t:SetTree<'T>) = - if isEmpty t then t + let rec remove (comparer: IComparer<'T>) k (t: SetTree<'T>) = + if isEmpty t then + t else let c = comparer.Compare(k, t.Key) - match t with + + match t with | :? SetTreeNode<'T> as tn -> - if c < 0 then rebalance (remove comparer k tn.Left) tn.Key tn.Right + if c < 0 then + rebalance (remove comparer k tn.Left) tn.Key tn.Right elif c = 0 then - if isEmpty tn.Left then tn.Right - elif isEmpty tn.Right then tn.Left + if isEmpty tn.Left then + tn.Right + elif isEmpty tn.Right then + tn.Left else - let sk, r' = spliceOutSuccessor tn.Right + let sk, r' = spliceOutSuccessor tn.Right mk tn.Left sk r' - else rebalance tn.Left tn.Key (remove comparer k tn.Right) - | _ -> - if c = 0 then empty - else t + else + rebalance tn.Left tn.Key (remove comparer k tn.Right) + | _ -> if c = 0 then empty else t - let rec contains (comparer: IComparer<'T>) k (t:SetTree<'T>) = - if isEmpty t then false + let rec contains (comparer: IComparer<'T>) k (t: SetTree<'T>) = + if isEmpty t then + false else - let c = comparer.Compare(k, t.Key) - match t with + let c = comparer.Compare(k, t.Key) + + match t with | :? SetTreeNode<'T> as tn -> - if c < 0 then contains comparer k tn.Left - elif c = 0 then true - else contains comparer k tn.Right + if c < 0 then + contains comparer k tn.Left + elif c = 0 then + true + else + contains comparer k tn.Right | _ -> (c = 0) - let rec iter f (t:SetTree<'T>) = - if isEmpty t then () + let rec iter f (t: SetTree<'T>) = + if isEmpty t then + () else - match t with - | :? SetTreeNode<'T> as tn -> iter f tn.Left; f tn.Key; iter f tn.Right - | _ -> f t.Key + match t with + | :? SetTreeNode<'T> as tn -> + iter f tn.Left + f tn.Key + iter f tn.Right + | _ -> f t.Key - // Fold, left-to-right. + // Fold, left-to-right. // // NOTE: This differs from the behaviour of Map.fold which folds right-to-left. - let rec fold f (t:SetTree<'T>) x = - if isEmpty t then x + let rec fold f (t: SetTree<'T>) x = + if isEmpty t then + x else - match t with - | :? SetTreeNode<'T> as tn -> fold f tn.Right (f tn.Key (fold f tn.Left x)) - | _ -> f t.Key x + match t with + | :? SetTreeNode<'T> as tn -> fold f tn.Right (f tn.Key (fold f tn.Left x)) + | _ -> f t.Key x - let rec forall f (t:SetTree<'T>) = - if isEmpty t then true + let rec forall f (t: SetTree<'T>) = + if isEmpty t then + true else - match t with + match t with | :? SetTreeNode<'T> as tn -> f tn.Key && forall f tn.Left && forall f tn.Right - | _ -> f t.Key + | _ -> f t.Key - let rec exists f (t:SetTree<'T>) = - if isEmpty t then false + let rec exists f (t: SetTree<'T>) = + if isEmpty t then + false else - match t with + match t with | :? SetTreeNode<'T> as tn -> f tn.Key || exists f tn.Left || exists f tn.Right - | _ -> f t.Key + | _ -> f t.Key - let subset comparer a b = + let subset comparer a b = forall (fun x -> contains comparer x b) a - let rec filterAux comparer f (t:SetTree<'T>) acc = - if isEmpty t then acc + let rec filterAux comparer f (t: SetTree<'T>) acc = + if isEmpty t then + acc else - match t with + match t with | :? SetTreeNode<'T> as tn -> - let acc = if f tn.Key then add comparer tn.Key acc else acc + let acc = + if f tn.Key then + add comparer tn.Key acc + else + acc + filterAux comparer f tn.Left (filterAux comparer f tn.Right acc) - | _ -> if f t.Key then add comparer t.Key acc else acc + | _ -> + if f t.Key then + add comparer t.Key acc + else + acc let filter comparer f s = filterAux comparer f s empty - let rec diffAux comparer (t:SetTree<'T>) acc = - if isEmpty acc then acc + let rec diffAux comparer (t: SetTree<'T>) acc = + if isEmpty acc then + acc + else if isEmpty t then + acc else - if isEmpty t then acc - else - match t with - | :? SetTreeNode<'T> as tn -> diffAux comparer tn.Left (diffAux comparer tn.Right (remove comparer tn.Key acc)) - | _ -> remove comparer t.Key acc + match t with + | :? SetTreeNode<'T> as tn -> diffAux comparer tn.Left (diffAux comparer tn.Right (remove comparer tn.Key acc)) + | _ -> remove comparer t.Key acc let diff comparer a b = diffAux comparer b a - let rec union comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = - // Perf: tried bruteForce for low heights, but nothing significant - if isEmpty t1 then t2 - elif isEmpty t2 then t1 + let rec union comparer (t1: SetTree<'T>) (t2: SetTree<'T>) = + // Perf: tried bruteForce for low heights, but nothing significant + if isEmpty t1 then + t2 + elif isEmpty t2 then + t1 else match t1 with | :? SetTreeNode<'T> as t1n -> match t2 with - | :? SetTreeNode<'T> as t2n -> // (t1l < k < t1r) AND (t2l < k2 < t2r) + | :? SetTreeNode<'T> as t2n -> // (t1l < k < t1r) AND (t2l < k2 < t2r) // Divide and Conquer: // Suppose t1 is largest. // Split t2 using pivot k1 into lo and hi. - // Union disjoint subproblems and then combine. + // Union disjoint subproblems and then combine. if t1n.Height > t2n.Height then let lo, _, hi = split comparer t1n.Key t2 in + balance comparer (union comparer t1n.Left lo) t1n.Key (union comparer t1n.Right hi) else let lo, _, hi = split comparer t2n.Key t1 in + balance comparer (union comparer t2n.Left lo) t2n.Key (union comparer t2n.Right hi) | _ -> add comparer t2.Key t1 | _ -> add comparer t1.Key t2 - let rec intersectionAux comparer b (t:SetTree<'T>) acc = - if isEmpty t then acc + let rec intersectionAux comparer b (t: SetTree<'T>) acc = + if isEmpty t then + acc else - match t with - | :? SetTreeNode<'T> as tn -> - let acc = intersectionAux comparer b tn.Right acc - let acc = if contains comparer tn.Key b then add comparer tn.Key acc else acc + match t with + | :? SetTreeNode<'T> as tn -> + let acc = intersectionAux comparer b tn.Right acc + + let acc = + if contains comparer tn.Key b then + add comparer tn.Key acc + else + acc + intersectionAux comparer b tn.Left acc - | _ -> - if contains comparer t.Key b then add comparer t.Key acc else acc + | _ -> + if contains comparer t.Key b then + add comparer t.Key acc + else + acc let intersection comparer a b = intersectionAux comparer b a empty - let partition1 comparer f k (acc1, acc2) = if f k then (add comparer k acc1, acc2) else (acc1, add comparer k acc2) + let partition1 comparer f k (acc1, acc2) = + if f k then + (add comparer k acc1, acc2) + else + (acc1, add comparer k acc2) - let rec partitionAux comparer f (t:SetTree<'T>) acc = - if isEmpty t then acc + let rec partitionAux comparer f (t: SetTree<'T>) acc = + if isEmpty t then + acc else - match t with - | :? SetTreeNode<'T> as tn -> - let acc = partitionAux comparer f tn.Right acc + match t with + | :? SetTreeNode<'T> as tn -> + let acc = partitionAux comparer f tn.Right acc let acc = partition1 comparer f tn.Key acc partitionAux comparer f tn.Left acc | _ -> partition1 comparer f t.Key acc - let partition comparer f s = partitionAux comparer f s (empty, empty) - - let rec minimumElementAux (t:SetTree<'T>) n = - if isEmpty t then n + let partition comparer f s = + partitionAux comparer f s (empty, empty) + + let rec minimumElementAux (t: SetTree<'T>) n = + if isEmpty t then + n else - match t with + match t with | :? SetTreeNode<'T> as tn -> minimumElementAux tn.Left tn.Key | _ -> t.Key - and minimumElementOpt (t:SetTree<'T>) = - if isEmpty t then None + and minimumElementOpt (t: SetTree<'T>) = + if isEmpty t then + None else - match t with + match t with | :? SetTreeNode<'T> as tn -> Some(minimumElementAux tn.Left tn.Key) | _ -> Some t.Key - and maximumElementAux (t:SetTree<'T>) n = - if isEmpty t then n + and maximumElementAux (t: SetTree<'T>) n = + if isEmpty t then + n else - match t with + match t with | :? SetTreeNode<'T> as tn -> maximumElementAux tn.Right tn.Key | _ -> t.Key - and maximumElementOpt (t:SetTree<'T>) = - if isEmpty t then None + and maximumElementOpt (t: SetTree<'T>) = + if isEmpty t then + None else - match t with + match t with | :? SetTreeNode<'T> as tn -> Some(maximumElementAux tn.Right tn.Key) | _ -> Some t.Key - let minimumElement s = - match minimumElementOpt s with - | Some(k) -> k - | None -> failwith "minimumElement" + let minimumElement s = + match minimumElementOpt s with + | Some (k) -> k + | None -> failwith "minimumElement" - let maximumElement s = - match maximumElementOpt s with - | Some(k) -> k + let maximumElement s = + match maximumElementOpt s with + | Some (k) -> k | None -> failwith "maximumElement" //-------------------------------------------------------------------------- // Imperative left-to-right iterators. //-------------------------------------------------------------------------- - type SetIterator<'T>(s:SetTree<'T>) = + type SetIterator<'T>(s: SetTree<'T>) = // collapseLHS: // a) Always returns either [] or a list starting with SetOne. // b) The "fringe" of the set stack is unchanged. - let rec collapseLHS (stack: SetTree<'T> list) = + let rec collapseLHS (stack: SetTree<'T> list) = match stack with | [] -> [] | x :: rest -> - if isEmpty x then collapseLHS rest + if isEmpty x then + collapseLHS rest else match x with - | :? SetTreeNode<'T> as xn-> collapseLHS (xn.Left :: SetTree xn.Key :: xn.Right :: rest) + | :? SetTreeNode<'T> as xn -> collapseLHS (xn.Left :: SetTree xn.Key :: xn.Right :: rest) | _ -> stack - // invariant: always collapseLHS result - let mutable stack = collapseLHS [s] - // true when MoveNext has been called - let mutable started = false + // invariant: always collapseLHS result + let mutable stack = collapseLHS [ s ] + // true when MoveNext has been called + let mutable started = false + + let notStarted () = + raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) - let notStarted() = raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) - let alreadyFinished() = raise (System.InvalidOperationException("Enumeration already finished.")) + let alreadyFinished () = + raise (System.InvalidOperationException("Enumeration already finished.")) member _.Current = if started then match stack with | k :: _ -> k.Key - | [] -> alreadyFinished() + | [] -> alreadyFinished () + else + notStarted () + + member _.MoveNext() = + if started then + match stack with + | [] -> false + | t :: rest -> + match t with + | :? SetTreeNode<'T> -> failwith "Please report error: Set iterator, unexpected stack for moveNext" + | _ -> + stack <- collapseLHS rest + not stack.IsEmpty else - notStarted() - - member _.MoveNext() = - if started then - match stack with - | [] -> false - | t :: rest -> - match t with - | :? SetTreeNode<'T> -> failwith "Please report error: Set iterator, unexpected stack for moveNext" - | _ -> - stack <- collapseLHS rest - not stack.IsEmpty - else - started <- true; // The first call to MoveNext "starts" the enumeration. - not stack.IsEmpty - - let toSeq s = + started <- true // The first call to MoveNext "starts" the enumeration. + not stack.IsEmpty + + let toSeq s = let mutable i = SetIterator s - { new IEnumerator<_> with - member _.Current = i.Current - interface System.Collections.IEnumerator with + + { new IEnumerator<_> with + member _.Current = i.Current + interface System.Collections.IEnumerator with member _.Current = box i.Current member _.MoveNext() = i.MoveNext() member _.Reset() = i <- SetIterator s - interface System.IDisposable with - member _.Dispose() = () } + interface System.IDisposable with + member _.Dispose() = () + } //-------------------------------------------------------------------------- // Set comparison. This can be expensive. //-------------------------------------------------------------------------- - let rec compareStacks (comparer: IComparer<'T>) (l1:SetTree<'T> list) (l2:SetTree<'T> list) : int = - let cont() = - match l1, l2 with + let rec compareStacks (comparer: IComparer<'T>) (l1: SetTree<'T> list) (l2: SetTree<'T> list) : int = + let cont () = + match l1, l2 with | x1 :: t1, _ when not (isEmpty x1) -> match x1 with | :? SetTreeNode<'T> as x1n -> - compareStacks comparer (x1n.Left :: (SetTreeNode (x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2 + compareStacks comparer (x1n.Left :: (SetTreeNode(x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2 | _ -> compareStacks comparer (empty :: SetTree x1.Key :: t1) l2 | _, x2 :: t2 when not (isEmpty x2) -> match x2 with | :? SetTreeNode<'T> as x2n -> - compareStacks comparer l1 (x2n.Left :: (SetTreeNode (x2n.Key, empty, x2n.Right, 0) :> SetTree<'T> ) :: t2) + compareStacks comparer l1 (x2n.Left :: (SetTreeNode(x2n.Key, empty, x2n.Right, 0) :> SetTree<'T>) :: t2) | _ -> compareStacks comparer l1 (empty :: SetTree x2.Key :: t2) | _ -> failwith "unexpected state in SetTree.compareStacks" - match l1, l2 with - | [], [] -> 0 - | [], _ -> -1 - | _, [] -> 1 + match l1, l2 with + | [], [] -> 0 + | [], _ -> -1 + | _, [] -> 1 | x1 :: t1, x2 :: t2 -> if isEmpty x1 then - if isEmpty x2 then compareStacks comparer t1 t2 - else cont() - elif isEmpty x2 then cont() + if isEmpty x2 then + compareStacks comparer t1 t2 + else + cont () + elif isEmpty x2 then + cont () else match x1 with | :? SetTreeNode<'T> as x1n -> @@ -429,128 +508,166 @@ module SetTree = match x2 with | :? SetTreeNode<'T> as x2n -> if isEmpty x2n.Left then - let c = comparer.Compare(x1n.Key, x2n.Key) - if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (x2n.Right :: t2) - else cont() + let c = comparer.Compare(x1n.Key, x2n.Key) + + if c <> 0 then + c + else + compareStacks comparer (x1n.Right :: t1) (x2n.Right :: t2) + else + cont () | _ -> - let c = comparer.Compare(x1n.Key, x2.Key) - if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (empty :: t2) - else cont() + let c = comparer.Compare(x1n.Key, x2.Key) + + if c <> 0 then + c + else + compareStacks comparer (x1n.Right :: t1) (empty :: t2) + else + cont () | _ -> match x2 with | :? SetTreeNode<'T> as x2n -> if isEmpty x2n.Left then - let c = comparer.Compare(x1.Key, x2n.Key) - if c <> 0 then c else compareStacks comparer (empty :: t1) (x2n.Right :: t2) - else cont() + let c = comparer.Compare(x1.Key, x2n.Key) + + if c <> 0 then + c + else + compareStacks comparer (empty :: t1) (x2n.Right :: t2) + else + cont () | _ -> - let c = comparer.Compare(x1.Key, x2.Key) - if c <> 0 then c else compareStacks comparer t1 t2 - - let compare comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = + let c = comparer.Compare(x1.Key, x2.Key) + + if c <> 0 then + c + else + compareStacks comparer t1 t2 + + let compare comparer (t1: SetTree<'T>) (t2: SetTree<'T>) = if isEmpty t1 then - if isEmpty t2 then 0 - else -1 + if isEmpty t2 then 0 else -1 + else if isEmpty t2 then + 1 else - if isEmpty t2 then 1 - else compareStacks comparer [t1] [t2] + compareStacks comparer [ t1 ] [ t2 ] let choose s = minimumElement s - let toList (t:SetTree<'T>) = - let rec loop (t':SetTree<'T>) acc = - if isEmpty t' then acc + let toList (t: SetTree<'T>) = + let rec loop (t': SetTree<'T>) acc = + if isEmpty t' then + acc else - match t' with + match t' with | :? SetTreeNode<'T> as tn -> loop tn.Left (tn.Key :: loop tn.Right acc) - | _ -> t'.Key :: acc - loop t [] + | _ -> t'.Key :: acc - let copyToArray s (arr: _[]) i = - let mutable j = i - iter (fun x -> arr[j] <- x; j <- j + 1) s + loop t [] - let toArray s = - let n = (count s) - let res = Array.zeroCreate n - copyToArray s res 0; + let copyToArray s (arr: _[]) i = + let mutable j = i + + iter + (fun x -> + arr[j] <- x + j <- j + 1) + s + + let toArray s = + let n = (count s) + let res = Array.zeroCreate n + copyToArray s res 0 res - let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = - if e.MoveNext() then - mkFromEnumerator comparer (add comparer e.Current acc) e - else acc - - let ofSeq comparer (c : IEnumerable<_>) = - use ie = c.GetEnumerator() - mkFromEnumerator comparer empty ie + let rec mkFromEnumerator comparer acc (e: IEnumerator<_>) = + if e.MoveNext() then + mkFromEnumerator comparer (add comparer e.Current acc) e + else + acc - let ofArray comparer l = Array.fold (fun acc k -> add comparer k acc) empty l + let ofSeq comparer (c: IEnumerable<_>) = + use ie = c.GetEnumerator() + mkFromEnumerator comparer empty ie + let ofArray comparer l = + Array.fold (fun acc k -> add comparer k acc) empty l [] [] -type internal Set<'T,'ComparerTag> when 'ComparerTag :> IComparer<'T>(comparer: IComparer<'T>, tree: SetTree<'T>) = +type internal Set<'T, 'ComparerTag> when 'ComparerTag :> IComparer<'T>(comparer: IComparer<'T>, tree: SetTree<'T>) = - static let refresh (s:Set<_,_>) t = Set<_,_>(comparer=s.Comparer, tree=t) + static let refresh (s: Set<_, _>) t = + Set<_, _>(comparer = s.Comparer, tree = t) member s.Tree = tree - member s.Comparer : IComparer<'T> = comparer + member s.Comparer: IComparer<'T> = comparer - static member Empty(comparer: 'ComparerTag) : Set<'T,'ComparerTag> = - Set<_,_>(comparer=comparer, tree=SetTree.empty) + static member Empty(comparer: 'ComparerTag) : Set<'T, 'ComparerTag> = + Set<_, _>(comparer = comparer, tree = SetTree.empty) + member s.Add(x) : Set<'T, 'ComparerTag> = refresh s (SetTree.add comparer x tree) - member s.Add(x) : Set<'T,'ComparerTag> = refresh s (SetTree.add comparer x tree) - member s.Remove(x) : Set<'T,'ComparerTag> = refresh s (SetTree.remove comparer x tree) - member s.Count = SetTree.count tree - member s.Contains(x) = SetTree.contains comparer x tree - member s.Iterate(x) = SetTree.iter x tree - member s.Fold f x = SetTree.fold f tree x - member s.IsEmpty = SetTree.isEmpty tree + member s.Remove(x) : Set<'T, 'ComparerTag> = + refresh s (SetTree.remove comparer x tree) - member s.Partition predicate : Set<'T,'ComparerTag> * Set<'T,'ComparerTag> = - if SetTree.isEmpty s.Tree then s,s + member s.Count = SetTree.count tree + member s.Contains(x) = SetTree.contains comparer x tree + member s.Iterate(x) = SetTree.iter x tree + member s.Fold f x = SetTree.fold f tree x + member s.IsEmpty = SetTree.isEmpty tree + + member s.Partition predicate : Set<'T, 'ComparerTag> * Set<'T, 'ComparerTag> = + if SetTree.isEmpty s.Tree then + s, s else let t1, t2 = SetTree.partition s.Comparer predicate s.Tree refresh s t1, refresh s t2 - member s.Filter predicate : Set<'T,'ComparerTag> = - if SetTree.isEmpty s.Tree then s - else - SetTree.filter comparer predicate tree |> refresh s + member s.Filter predicate : Set<'T, 'ComparerTag> = + if SetTree.isEmpty s.Tree then + s + else + SetTree.filter comparer predicate tree |> refresh s member s.Exists predicate = SetTree.exists predicate tree member s.ForAll predicate = SetTree.forall predicate tree - static member (-) (a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = Set<_,_>.Difference(a,b) + static member (-)(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) = Set<_, _>.Difference (a, b) - static member (+) (a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = Set<_,_>.Union(a,b) + static member (+)(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) = Set<_, _>.Union (a, b) - static member Intersection(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = - if SetTree.isEmpty b.Tree then b (* A INTER 0 = 0 *) + static member Intersection(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) : Set<'T, 'ComparerTag> = + if SetTree.isEmpty b.Tree then + b (* A INTER 0 = 0 *) + else if SetTree.isEmpty a.Tree then + a (* 0 INTER B = 0 *) else - if SetTree.isEmpty a.Tree then a (* 0 INTER B = 0 *) - else SetTree.intersection a.Comparer a.Tree b.Tree |> refresh a - - static member Union(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = - if SetTree.isEmpty b.Tree then a (* A U 0 = A *) + SetTree.intersection a.Comparer a.Tree b.Tree |> refresh a + + static member Union(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) : Set<'T, 'ComparerTag> = + if SetTree.isEmpty b.Tree then + a (* A U 0 = A *) + else if SetTree.isEmpty a.Tree then + b (* 0 U B = B *) else - if SetTree.isEmpty a.Tree then b (* 0 U B = B *) - else SetTree.union a.Comparer a.Tree b.Tree |> refresh a + SetTree.union a.Comparer a.Tree b.Tree |> refresh a - static member Difference(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) : Set<'T,'ComparerTag> = - if SetTree.isEmpty a.Tree then a (* 0 - B = 0 *) + static member Difference(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) : Set<'T, 'ComparerTag> = + if SetTree.isEmpty a.Tree then + a (* 0 - B = 0 *) + else if SetTree.isEmpty b.Tree then + a (* A - 0 = A *) else - if SetTree.isEmpty b.Tree then a (* A - 0 = A *) - else SetTree.diff a.Comparer a.Tree b.Tree |> refresh a + SetTree.diff a.Comparer a.Tree b.Tree |> refresh a - static member Equality(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = - (SetTree.compare a.Comparer a.Tree b.Tree = 0) + static member Equality(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) = + (SetTree.compare a.Comparer a.Tree b.Tree = 0) - static member Compare(a: Set<'T,'ComparerTag>,b: Set<'T,'ComparerTag>) = - SetTree.compare a.Comparer a.Tree b.Tree + static member Compare(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) = + SetTree.compare a.Comparer a.Tree b.Tree member s.Choose = SetTree.choose tree @@ -558,55 +675,63 @@ type internal Set<'T,'ComparerTag> when 'ComparerTag :> IComparer<'T>(comparer: member s.MaximumElement = SetTree.maximumElement tree - member s.IsSubsetOf((y: Set<'T,'ComparerTag>)) = SetTree.subset comparer tree y.Tree + member s.IsSubsetOf((y: Set<'T, 'ComparerTag>)) = SetTree.subset comparer tree y.Tree - member s.IsSupersetOf((y: Set<'T,'ComparerTag>)) = SetTree.subset comparer y.Tree tree + member s.IsSupersetOf((y: Set<'T, 'ComparerTag>)) = SetTree.subset comparer y.Tree tree - member s.ToList () = SetTree.toList tree + member s.ToList() = SetTree.toList tree - member s.ToArray () = SetTree.toArray tree + member s.ToArray() = SetTree.toArray tree - override this.Equals(that) = + override this.Equals(that) = match that with // Cast to the exact same type as this, otherwise not equal. - | :? Set<'T,'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) + | :? Set<'T, 'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) | _ -> false interface System.IComparable with // Cast s2 to the exact same type as s1, see 4884. // It is not OK to cast s2 to seq<'T>, since different compares could permute the elements. - member s1.CompareTo(s2: obj) = SetTree.compare s1.Comparer s1.Tree (s2 :?> Set<'T,'ComparerTag>).Tree + member s1.CompareTo(s2: obj) = + SetTree.compare s1.Comparer s1.Tree (s2 :?> Set<'T, 'ComparerTag>).Tree + + member this.ComputeHashCode() = + let combineHash x y = (x <<< 1) + y + 631 + let mutable res = 0 + + for x in this do + res <- combineHash res (Unchecked.hash x) - member this.ComputeHashCode() = - let combineHash x y = (x <<< 1) + y + 631 - let mutable res = 0 - for x in this do - res <- combineHash res (Unchecked.hash x) - res + res override this.GetHashCode() = this.ComputeHashCode() - - interface ICollection<'T> with - member s.Add _ = raise (System.NotSupportedException("ReadOnlyCollection")) - member s.Clear() = raise (System.NotSupportedException("ReadOnlyCollection")) - member s.Remove _ = raise (System.NotSupportedException("ReadOnlyCollection")) + + interface ICollection<'T> with + member s.Add _ = + raise (System.NotSupportedException("ReadOnlyCollection")) + + member s.Clear() = + raise (System.NotSupportedException("ReadOnlyCollection")) + + member s.Remove _ = + raise (System.NotSupportedException("ReadOnlyCollection")) + member s.Contains(x) = SetTree.contains comparer x tree - member s.CopyTo(arr,i) = SetTree.copyToArray tree arr i + member s.CopyTo(arr, i) = SetTree.copyToArray tree arr i member s.IsReadOnly = true - member s.Count = SetTree.count tree + member s.Count = SetTree.count tree interface IEnumerable<'T> with member s.GetEnumerator() = SetTree.toSeq tree interface System.Collections.IEnumerable with - override s.GetEnumerator() = (SetTree.toSeq tree :> System.Collections.IEnumerator) - - static member Singleton(comparer,x) : Set<'T,'ComparerTag> = - Set<_,_>.Empty(comparer).Add(x) + override s.GetEnumerator() = + (SetTree.toSeq tree :> System.Collections.IEnumerator) - static member Create(comparer : 'ComparerTag,l : seq<'T>) : Set<'T,'ComparerTag> = - Set<_,_>(comparer=comparer, tree=SetTree.ofSeq comparer l) + static member Singleton(comparer, x) : Set<'T, 'ComparerTag> = Set<_, _>.Empty(comparer).Add(x) + static member Create(comparer: 'ComparerTag, l: seq<'T>) : Set<'T, 'ComparerTag> = + Set<_, _>(comparer = comparer, tree = SetTree.ofSeq comparer l) [] [] @@ -617,124 +742,142 @@ type internal MapTree<'Key, 'Value>(k: 'Key, v: 'Value) = [] [] [] -type internal MapTreeNode<'Key, 'Value>(k:'Key, v:'Value, left:MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) = - inherit MapTree<'Key,'Value>(k, v) +type internal MapTreeNode<'Key, 'Value>(k: 'Key, v: 'Value, left: MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) = + inherit MapTree<'Key, 'Value>(k, v) member _.Left = left member _.Right = right member _.Height = h - [] -module MapTree = +module MapTree = - let empty = null + let empty = null + + let inline isEmpty (m: MapTree<'Key, 'Value>) = isNull m - let inline isEmpty (m:MapTree<'Key, 'Value>) = isNull m - - let rec sizeAux acc (m:MapTree<'Key, 'Value>) = + let rec sizeAux acc (m: MapTree<'Key, 'Value>) = if isEmpty m then acc else match m with - | :? MapTreeNode<'Key, 'Value> as mn -> sizeAux (sizeAux (acc+1) mn.Left) mn.Right + | :? MapTreeNode<'Key, 'Value> as mn -> sizeAux (sizeAux (acc + 1) mn.Left) mn.Right | _ -> acc + 1 let size x = sizeAux 0 x - let inline height (m: MapTree<'Key, 'Value>) = - if isEmpty m then 0 + let inline height (m: MapTree<'Key, 'Value>) = + if isEmpty m then + 0 else match m with | :? MapTreeNode<'Key, 'Value> as mn -> mn.Height | _ -> 1 - let mk l k v r : MapTree<'Key, 'Value> = + let mk l k v r : MapTree<'Key, 'Value> = let hl = height l let hr = height r let m = max hl hr - if m = 0 then // m=0 ~ isEmpty l && isEmpty r - MapTree(k,v) + + if m = 0 then // m=0 ~ isEmpty l && isEmpty r + MapTree(k, v) else - MapTreeNode(k,v,l,r,m+1) :> MapTree<'Key, 'Value> + MapTreeNode(k, v, l, r, m + 1) :> MapTree<'Key, 'Value> + + let inline private asNode (value: MapTree<'Key, 'Value>) : MapTreeNode<'Key, 'Value> = value :?> MapTreeNode<'Key, 'Value> - let inline private asNode(value:MapTree<'Key,'Value>) : MapTreeNode<'Key,'Value> = - value :?> MapTreeNode<'Key,'Value> - let rebalance t1 (k: 'Key) (v: 'Value) t2 : MapTree<'Key, 'Value> = let t1h = height t1 - let t2h = height t2 - if t2h > t1h + 2 then (* right is heavier than left *) - let t2' = asNode(t2) + let t2h = height t2 + + if t2h > t1h + 2 then (* right is heavier than left *) + let t2' = asNode (t2) (* one of the nodes must have height > height t1 + 1 *) - if height t2'.Left > t1h + 1 then (* balance left: combination *) - let t2l = asNode(t2'.Left) + if height t2'.Left > t1h + 1 then (* balance left: combination *) + let t2l = asNode (t2'.Left) mk (mk t1 k v t2l.Left) t2l.Key t2l.Value (mk t2l.Right t2'.Key t2'.Value t2'.Right) else (* rotate left *) mk (mk t1 k v t2'.Left) t2'.Key t2'.Value t2'.Right - else - if t1h > t2h + 2 then (* left is heavier than right *) - let t1' = asNode(t1) - (* one of the nodes must have height > height t2 + 1 *) - if height t1'.Right > t2h + 1 then + else if t1h > t2h + 2 then (* left is heavier than right *) + let t1' = asNode (t1) + (* one of the nodes must have height > height t2 + 1 *) + if height t1'.Right > t2h + 1 then (* balance right: combination *) - let t1r = asNode(t1'.Right) - mk (mk t1'.Left t1'.Key t1'.Value t1r.Left) t1r.Key t1r.Value (mk t1r.Right k v t2) - else - mk t1'.Left t1'.Key t1'.Value (mk t1'.Right k v t2) - else mk t1 k v t2 - + let t1r = asNode (t1'.Right) + mk (mk t1'.Left t1'.Key t1'.Value t1r.Left) t1r.Key t1r.Value (mk t1r.Right k v t2) + else + mk t1'.Left t1'.Key t1'.Value (mk t1'.Right k v t2) + else + mk t1 k v t2 - let rec add (comparer: IComparer<'Key>) k (v: 'Value) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> = - if isEmpty m then MapTree(k,v) + let rec add (comparer: IComparer<'Key>) k (v: 'Value) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> = + if isEmpty m then + MapTree(k, v) else - let c = comparer.Compare(k,m.Key) + let c = comparer.Compare(k, m.Key) + match m with | :? MapTreeNode<'Key, 'Value> as mn -> - if c < 0 then rebalance (add comparer k v mn.Left) mn.Key mn.Value mn.Right - elif c = 0 then MapTreeNode(k,v,mn.Left,mn.Right,mn.Height) :> MapTree<'Key, 'Value> - else rebalance mn.Left mn.Key mn.Value (add comparer k v mn.Right) + if c < 0 then + rebalance (add comparer k v mn.Left) mn.Key mn.Value mn.Right + elif c = 0 then + MapTreeNode(k, v, mn.Left, mn.Right, mn.Height) :> MapTree<'Key, 'Value> + else + rebalance mn.Left mn.Key mn.Value (add comparer k v mn.Right) | _ -> - if c < 0 then MapTreeNode (k,v,empty,m,2) :> MapTree<'Key, 'Value> - elif c = 0 then MapTree(k,v) - else MapTreeNode (k,v,m,empty,2) :> MapTree<'Key, 'Value> + if c < 0 then + MapTreeNode(k, v, empty, m, 2) :> MapTree<'Key, 'Value> + elif c = 0 then + MapTree(k, v) + else + MapTreeNode(k, v, m, empty, 2) :> MapTree<'Key, 'Value> - let indexNotFound() = raise (KeyNotFoundException("An index satisfying the predicate was not found in the collection")) + let indexNotFound () = + raise (KeyNotFoundException("An index satisfying the predicate was not found in the collection")) - let rec tryGetValue (comparer: IComparer<'Key>) k (v: byref<'Value>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then false + let rec tryGetValue (comparer: IComparer<'Key>) k (v: byref<'Value>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then + false else let c = comparer.Compare(k, m.Key) - if c = 0 then v <- m.Value; true + + if c = 0 then + v <- m.Value + true else match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - tryGetValue comparer k &v (if c < 0 then mn.Left else mn.Right) + | :? MapTreeNode<'Key, 'Value> as mn -> tryGetValue comparer k &v (if c < 0 then mn.Left else mn.Right) | _ -> false let find (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = let mutable v = Unchecked.defaultof<'Value> + if tryGetValue comparer k &v m then v else - indexNotFound() + indexNotFound () - let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = let mutable v = Unchecked.defaultof<'Value> + if tryGetValue comparer k &v m then Some v else None - let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) = - if f.Invoke (k, v) then (add comparer k v acc1, acc2) else (acc1, add comparer k v acc2) + let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) = + if f.Invoke(k, v) then + (add comparer k v acc1, acc2) + else + (acc1, add comparer k v acc2) - let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = - if isEmpty m then acc + let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = + if isEmpty m then + acc else - match m with + match m with | :? MapTreeNode<'Key, 'Value> as mn -> - let acc = partitionAux comparer f mn.Right acc + let acc = partitionAux comparer f mn.Right acc let acc = partition1 comparer f mn.Key mn.Value acc partitionAux comparer f mn.Left acc | _ -> partition1 comparer f m.Key m.Value acc @@ -743,12 +886,16 @@ module MapTree = partitionAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m (empty, empty) let filter1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v acc = - if f.Invoke (k, v) then add comparer k v acc else acc + if f.Invoke(k, v) then + add comparer k v acc + else + acc - let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = - if isEmpty m then acc + let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = + if isEmpty m then + acc else - match m with + match m with | :? MapTreeNode<'Key, 'Value> as mn -> let acc = filterAux comparer f mn.Left acc let acc = filter1 comparer f mn.Key mn.Value acc @@ -756,225 +903,284 @@ module MapTree = | _ -> filter1 comparer f m.Key m.Value acc let filter (comparer: IComparer<'Key>) f m = - filterAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m empty + filterAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m empty - let rec spliceOutSuccessor (m: MapTree<'Key, 'Value>) = - if isEmpty m then failwith "internal error: Map.spliceOutSuccessor" + let rec spliceOutSuccessor (m: MapTree<'Key, 'Value>) = + if isEmpty m then + failwith "internal error: Map.spliceOutSuccessor" else match m with | :? MapTreeNode<'Key, 'Value> as mn -> - if isEmpty mn.Left then mn.Key, mn.Value, mn.Right - else let k3, v3, l' = spliceOutSuccessor mn.Left in k3, v3, mk l' mn.Key mn.Value mn.Right + if isEmpty mn.Left then + mn.Key, mn.Value, mn.Right + else + let k3, v3, l' = spliceOutSuccessor mn.Left in k3, v3, mk l' mn.Key mn.Value mn.Right | _ -> m.Key, m.Value, empty - let rec remove (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = - if isEmpty m then empty + let rec remove (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + if isEmpty m then + empty else let c = comparer.Compare(k, m.Key) - match m with + + match m with | :? MapTreeNode<'Key, 'Value> as mn -> - if c < 0 then rebalance (remove comparer k mn.Left) mn.Key mn.Value mn.Right + if c < 0 then + rebalance (remove comparer k mn.Left) mn.Key mn.Value mn.Right elif c = 0 then - if isEmpty mn.Left then mn.Right - elif isEmpty mn.Right then mn.Left + if isEmpty mn.Left then + mn.Right + elif isEmpty mn.Right then + mn.Left else - let sk, sv, r' = spliceOutSuccessor mn.Right + let sk, sv, r' = spliceOutSuccessor mn.Right mk mn.Left sk sv r' - else rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right) - | _ -> - if c = 0 then empty else m + else + rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right) + | _ -> if c = 0 then empty else m - let rec mem (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = - if isEmpty m then false + let rec mem (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = + if isEmpty m then + false else let c = comparer.Compare(k, m.Key) - match m with + + match m with | :? MapTreeNode<'Key, 'Value> as mn -> - if c < 0 then mem comparer k mn.Left - else (c = 0 || mem comparer k mn.Right) + if c < 0 then + mem comparer k mn.Left + else + (c = 0 || mem comparer k mn.Right) | _ -> c = 0 let rec iterOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then () + if isEmpty m then + () else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> iterOpt f mn.Left; f.Invoke (mn.Key, mn.Value); iterOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + iterOpt f mn.Left + f.Invoke(mn.Key, mn.Value) + iterOpt f mn.Right + | _ -> f.Invoke(m.Key, m.Value) let iter f m = iterOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m let rec tryPickOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then None + if isEmpty m then + None else - match m with + match m with | :? MapTreeNode<'Key, 'Value> as mn -> - match tryPickOpt f mn.Left with - | Some _ as res -> res - | None -> - match f.Invoke (mn.Key, mn.Value) with - | Some _ as res -> res - | None -> - tryPickOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) + match tryPickOpt f mn.Left with + | Some _ as res -> res + | None -> + match f.Invoke(mn.Key, mn.Value) with + | Some _ as res -> res + | None -> tryPickOpt f mn.Right + | _ -> f.Invoke(m.Key, m.Value) let tryPick f m = tryPickOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - let rec existsOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then false + let rec existsOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then + false else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> existsOpt f mn.Left || f.Invoke (mn.Key, mn.Value) || existsOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> existsOpt f mn.Left || f.Invoke(mn.Key, mn.Value) || existsOpt f mn.Right + | _ -> f.Invoke(m.Key, m.Value) let exists f m = existsOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - let rec forallOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then true + let rec forallOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then + true else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> forallOpt f mn.Left && f.Invoke (mn.Key, mn.Value) && forallOpt f mn.Right - | _ -> f.Invoke (m.Key, m.Value) - + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> forallOpt f mn.Left && f.Invoke(mn.Key, mn.Value) && forallOpt f mn.Right + | _ -> f.Invoke(m.Key, m.Value) let forall f m = forallOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - let rec map (f:'Value -> 'Result) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Result> = - if isEmpty m then empty + let rec map (f: 'Value -> 'Result) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Result> = + if isEmpty m then + empty else - match m with - | :? MapTreeNode<'Key, 'Value> as mn -> - let l2 = map f mn.Left + match m with + | :? MapTreeNode<'Key, 'Value> as mn -> + let l2 = map f mn.Left let v2 = f mn.Value let r2 = map f mn.Right - MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> - | _ -> MapTree (m.Key, f m.Value) + MapTreeNode(mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> + | _ -> MapTree(m.Key, f m.Value) - let rec mapiOpt (f: OptimizedClosures.FSharpFunc<'Key, 'Value, 'Result>) (m: MapTree<'Key, 'Value>) = - if isEmpty m then empty + let rec mapiOpt (f: OptimizedClosures.FSharpFunc<'Key, 'Value, 'Result>) (m: MapTree<'Key, 'Value>) = + if isEmpty m then + empty else match m with | :? MapTreeNode<'Key, 'Value> as mn -> let l2 = mapiOpt f mn.Left - let v2 = f.Invoke (mn.Key, mn.Value) + let v2 = f.Invoke(mn.Key, mn.Value) let r2 = mapiOpt f mn.Right - MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> - | _ -> MapTree (m.Key, f.Invoke (m.Key, m.Value)) + MapTreeNode(mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result> + | _ -> MapTree(m.Key, f.Invoke(m.Key, m.Value)) let mapi f m = mapiOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m - // Fold, right-to-left. + // Fold, right-to-left. // // NOTE: This differs from the behaviour of Set.fold which folds left-to-right. - let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = - if isEmpty m then x + let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = + if isEmpty m then + x else - match m with + match m with | :? MapTreeNode<'Key, 'Value> as mn -> let x = foldBackOpt f mn.Right x - let x = f.Invoke (mn.Key, mn.Value, x) + let x = f.Invoke(mn.Key, mn.Value, x) foldBackOpt f mn.Left x - | _ -> f.Invoke (m.Key, m.Value, x) + | _ -> f.Invoke(m.Key, m.Value, x) let foldBack f m x = foldBackOpt (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x let foldSectionOpt (comparer: IComparer<'Key>) lo hi (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = - let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = - if isEmpty m then x + let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = + if isEmpty m then + x else - match m with + match m with | :? MapTreeNode<'Key, 'Value> as mn -> let cLoKey = comparer.Compare(lo, mn.Key) let cKeyHi = comparer.Compare(mn.Key, hi) - let x = if cLoKey < 0 then foldFromTo f mn.Left x else x - let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (mn.Key, mn.Value, x) else x - let x = if cKeyHi < 0 then foldFromTo f mn.Right x else x + + let x = + if cLoKey < 0 then + foldFromTo f mn.Left x + else + x + + let x = + if cLoKey <= 0 && cKeyHi <= 0 then + f.Invoke(mn.Key, mn.Value, x) + else + x + + let x = + if cKeyHi < 0 then + foldFromTo f mn.Right x + else + x + x | _ -> let cLoKey = comparer.Compare(lo, m.Key) let cKeyHi = comparer.Compare(m.Key, hi) - let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (m.Key, m.Value, x) else x + + let x = + if cLoKey <= 0 && cKeyHi <= 0 then + f.Invoke(m.Key, m.Value, x) + else + x + x - if comparer.Compare(lo, hi) = 1 then x else foldFromTo f m x + if comparer.Compare(lo, hi) = 1 then + x + else + foldFromTo f m x let foldSection (comparer: IComparer<'Key>) lo hi f m x = foldSectionOpt comparer lo hi (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x - let rec foldMapOpt (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) z acc = - if isEmpty m then acc,z + let rec foldMapOpt (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) z acc = + if isEmpty m then + acc, z else match m with | :? MapTreeNode<'Key, 'Value> as mn -> - let acc,z = foldMapOpt comparer f mn.Right z acc - let v',z = f.Invoke(mn.Key, mn.Value, z) - let acc = add comparer mn.Key v' acc + let acc, z = foldMapOpt comparer f mn.Right z acc + let v', z = f.Invoke(mn.Key, mn.Value, z) + let acc = add comparer mn.Key v' acc foldMapOpt comparer f mn.Left z acc - | _ -> - let v',z = f.Invoke(m.Key, m.Value, z) - add comparer m.Key v' acc,z + | _ -> + let v', z = f.Invoke(m.Key, m.Value, z) + add comparer m.Key v' acc, z let foldMap (comparer: IComparer<'Key>) f (m: MapTree<'Key, 'Value>) z acc = foldMapOpt comparer (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m z acc - let toList m = foldBack (fun k v acc -> (k,v) :: acc) m [] + let toList m = + foldBack (fun k v acc -> (k, v) :: acc) m [] + let toArray m = m |> toList |> Array.ofList - let ofList comparer l = List.fold (fun acc (k,v) -> add comparer k v acc) empty l - let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = - if e.MoveNext() then - let x,y = e.Current + let ofList comparer l = + List.fold (fun acc (k, v) -> add comparer k v acc) empty l + + let rec mkFromEnumerator comparer acc (e: IEnumerator<_>) = + if e.MoveNext() then + let x, y = e.Current mkFromEnumerator comparer (add comparer x y acc) e - else acc - - let ofSeq comparer (c : seq<_>) = + else + acc + + let ofSeq comparer (c: seq<_>) = use ie = c.GetEnumerator() - mkFromEnumerator comparer empty ie - + mkFromEnumerator comparer empty ie + let copyToArray s (arr: _[]) i = - let mutable j = i - s |> iter (fun x y -> arr[j] <- KeyValuePair(x,y); j <- j + 1) + let mutable j = i + s + |> iter (fun x y -> + arr[j] <- KeyValuePair(x, y) + j <- j + 1) /// Imperative left-to-right iterators. - type MapIterator<'Key,'Value>(s:MapTree<'Key,'Value>) = + type MapIterator<'Key, 'Value>(s: MapTree<'Key, 'Value>) = // collapseLHS: // a) Always returns either [] or a list starting with SetOne. - // b) The "fringe" of the set stack is unchanged. - let rec collapseLHS (stack:MapTree<'Key, 'Value> list) = + // b) The "fringe" of the set stack is unchanged. + let rec collapseLHS (stack: MapTree<'Key, 'Value> list) = match stack with | [] -> [] | m :: rest -> - if isEmpty m then collapseLHS rest + if isEmpty m then + collapseLHS rest else match m with - | :? MapTreeNode<'Key, 'Value> as mn -> collapseLHS (mn.Left :: MapTree (mn.Key, mn.Value) :: mn.Right :: rest) + | :? MapTreeNode<'Key, 'Value> as mn -> collapseLHS (mn.Left :: MapTree(mn.Key, mn.Value) :: mn.Right :: rest) | _ -> stack - /// invariant: always collapseLHS result - let mutable stack = collapseLHS [s] - /// true when MoveNext has been called + /// invariant: always collapseLHS result + let mutable stack = collapseLHS [ s ] + /// true when MoveNext has been called let mutable started = false - let notStarted() = raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) - let alreadyFinished() = raise (System.InvalidOperationException("Enumeration already finished.")) + let notStarted () = + raise (System.InvalidOperationException("Enumeration has not started. Call MoveNext.")) + + let alreadyFinished () = + raise (System.InvalidOperationException("Enumeration already finished.")) member _.Current = if started then match stack with - | [] -> alreadyFinished() + | [] -> alreadyFinished () | m :: _ -> match m with | :? MapTreeNode<'Key, 'Value> -> failwith "Please report error: Map iterator, unexpected stack for current" | _ -> KeyValuePair<_, _>(m.Key, m.Value) else - notStarted() + notStarted () member _.MoveNext() = if started then @@ -987,95 +1193,123 @@ module MapTree = stack <- collapseLHS rest not stack.IsEmpty else - started <- true (* The first call to MoveNext "starts" the enumeration. *) + started <- true (* The first call to MoveNext "starts" the enumeration. *) not stack.IsEmpty - let toSeq s = + let toSeq s = let mutable i = MapIterator(s) - { new IEnumerator<_> with - member self.Current = i.Current + + { new IEnumerator<_> with + member self.Current = i.Current interface System.Collections.IEnumerator with member self.Current = box i.Current member self.MoveNext() = i.MoveNext() - member self.Reset() = i <- MapIterator(s) - interface System.IDisposable with - member self.Dispose() = ()} - + member self.Reset() = i <- MapIterator(s) + interface System.IDisposable with + member self.Dispose() = () + } [] [] -type internal Map<'Key,'T,'ComparerTag> when 'ComparerTag :> IComparer<'Key>( comparer: IComparer<'Key>, tree: MapTree<'Key,'T>) = +type internal Map<'Key, 'T, 'ComparerTag> when 'ComparerTag :> IComparer<'Key>(comparer: IComparer<'Key>, tree: MapTree<'Key, 'T>) = - static let refresh (m:Map<_,_,'ComparerTag>) t = - Map<_,_,'ComparerTag>(comparer=m.Comparer, tree=t) + static let refresh (m: Map<_, _, 'ComparerTag>) t = + Map<_, _, 'ComparerTag>(comparer = m.Comparer, tree = t) member s.Tree = tree - member s.Comparer : IComparer<'Key> = comparer + member s.Comparer: IComparer<'Key> = comparer + + static member Empty(comparer: 'ComparerTag) = + Map<'Key, 'T, 'ComparerTag>(comparer = comparer, tree = MapTree.empty) + + member m.Add(k, v) = + refresh m (MapTree.add comparer k v tree) - static member Empty(comparer : 'ComparerTag) = Map<'Key,'T,'ComparerTag>(comparer=comparer, tree=MapTree.empty) - member m.Add(k,v) = refresh m (MapTree.add comparer k v tree) member m.IsEmpty = MapTree.isEmpty tree - member m.Item with get(k : 'Key) = MapTree.find comparer k tree - member m.First(f) = MapTree.tryPick f tree - member m.Exists(f) = MapTree.exists f tree - member m.Filter(f) = MapTree.filter comparer f tree |> refresh m - member m.ForAll(f) = MapTree.forall f tree + + member m.Item + with get (k: 'Key) = MapTree.find comparer k tree + + member m.First(f) = MapTree.tryPick f tree + member m.Exists(f) = MapTree.exists f tree + + member m.Filter(f) = + MapTree.filter comparer f tree |> refresh m + + member m.ForAll(f) = MapTree.forall f tree member m.Fold folder acc = MapTree.foldBack folder tree acc - member m.FoldSection lo hi f acc = MapTree.foldSection comparer lo hi f tree acc - member m.FoldAndMap f z = - let tree,z = MapTree.foldMap comparer f tree z MapTree.empty + + member m.FoldSection lo hi f acc = + MapTree.foldSection comparer lo hi f tree acc + + member m.FoldAndMap f z = + let tree, z = MapTree.foldMap comparer f tree z MapTree.empty refresh m tree, z + member m.Iterate action = MapTree.iter action tree - member m.MapRange mapping = refresh m (MapTree.map mapping tree) - member m.Map mapping = refresh m (MapTree.mapi mapping tree) - member m.Partition(f) = - let r1,r2 = MapTree.partition comparer f tree + member m.MapRange mapping = refresh m (MapTree.map mapping tree) + member m.Map mapping = refresh m (MapTree.mapi mapping tree) + + member m.Partition(f) = + let r1, r2 = MapTree.partition comparer f tree refresh m r1, refresh m r2 + member m.Count = MapTree.size tree member m.ContainsKey(k) = MapTree.mem comparer k tree - member m.Remove(k) = refresh m (MapTree.remove comparer k tree) + + member m.Remove(k) = + refresh m (MapTree.remove comparer k tree) + member m.TryFind(k) = MapTree.tryFind comparer k tree member m.ToList() = MapTree.toList tree member m.ToArray() = MapTree.toArray tree - static member FromList(comparer : 'ComparerTag,l) : Map<'Key,'T,'ComparerTag> = - Map<_,_,_>(comparer=comparer, tree=MapTree.ofList comparer l) + static member FromList(comparer: 'ComparerTag, l) : Map<'Key, 'T, 'ComparerTag> = + Map<_, _, _>(comparer = comparer, tree = MapTree.ofList comparer l) - static member Create(comparer : 'ComparerTag, ie : seq<_>) : Map<'Key,'T,'ComparerTag> = - Map<_,_,_>(comparer=comparer, tree=MapTree.ofSeq comparer ie) + static member Create(comparer: 'ComparerTag, ie: seq<_>) : Map<'Key, 'T, 'ComparerTag> = + Map<_, _, _>(comparer = comparer, tree = MapTree.ofSeq comparer ie) interface IEnumerable> with member s.GetEnumerator() = MapTree.toSeq tree interface System.Collections.IEnumerable with - override s.GetEnumerator() = (MapTree.toSeq tree :> System.Collections.IEnumerator) + override s.GetEnumerator() = + (MapTree.toSeq tree :> System.Collections.IEnumerator) - override this.Equals(that) = + override this.Equals(that) = match that with // Cast to the exact same type as this, otherwise not equal. - | :? Map<'Key,'T,'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) + | :? Map<'Key, 'T, 'ComparerTag> as that -> ((this :> System.IComparable).CompareTo(that) = 0) | _ -> false - interface System.IComparable with - member m1.CompareTo(m2: obj) = - Seq.compareWith - (fun (kvp1 : KeyValuePair<_,_>) (kvp2 : KeyValuePair<_,_>)-> - let c = m1.Comparer.Compare(kvp1.Key,kvp2.Key) in - if c <> 0 then c else Unchecked.compare kvp1.Value kvp2.Value) - // Cast m2 to the exact same type as m1, see 4884. - // It is not OK to cast m2 to seq>, since different compares could permute the KVPs. - m1 (m2 :?> Map<'Key,'T,'ComparerTag>) - - member this.ComputeHashCode() = - let combineHash x y = (x <<< 1) + y + 631 + interface System.IComparable with + member m1.CompareTo(m2: obj) = + Seq.compareWith + (fun (kvp1: KeyValuePair<_, _>) (kvp2: KeyValuePair<_, _>) -> + let c = m1.Comparer.Compare(kvp1.Key, kvp2.Key) in + + if c <> 0 then + c + else + Unchecked.compare kvp1.Value kvp2.Value) + // Cast m2 to the exact same type as m1, see 4884. + // It is not OK to cast m2 to seq>, since different compares could permute the KVPs. + m1 + (m2 :?> Map<'Key, 'T, 'ComparerTag>) + + member this.ComputeHashCode() = + let combineHash x y = (x <<< 1) + y + 631 let mutable res = 0 - for KeyValue(x,y) in this do + + for KeyValue (x, y) in this do res <- combineHash res (Unchecked.hash x) res <- combineHash res (Unchecked.hash y) + res override this.GetHashCode() = this.ComputeHashCode() -type internal Map<'Key,'T> = Map<'Key, 'T, IComparer<'Key>> +type internal Map<'Key, 'T> = Map<'Key, 'T, IComparer<'Key>> type internal Set<'T> = Set<'T, IComparer<'T>> diff --git a/src/Compiler/Utilities/XmlAdapters.fs b/src/Compiler/Utilities/XmlAdapters.fs index f4bde8561b7..f99853d61ea 100644 --- a/src/Compiler/Utilities/XmlAdapters.fs +++ b/src/Compiler/Utilities/XmlAdapters.fs @@ -7,12 +7,11 @@ let s_escapeChars = [| '<'; '>'; '\"'; '\''; '&' |] let getEscapeSequence c = match c with - | '<' -> "<" - | '>' -> ">" + | '<' -> "<" + | '>' -> ">" | '\"' -> """ | '\'' -> "'" - | '&' -> "&" + | '&' -> "&" | _ as ch -> ch.ToString() let escape str = String.collect getEscapeSequence str - diff --git a/src/Compiler/Utilities/ildiag.fs b/src/Compiler/Utilities/ildiag.fs index d43bdf8dca4..e5f3b069bbb 100644 --- a/src/Compiler/Utilities/ildiag.fs +++ b/src/Compiler/Utilities/ildiag.fs @@ -2,21 +2,44 @@ /// Configurable Diagnostics channel for the Abstract IL library -module internal FSharp.Compiler.AbstractIL.Diagnostics - +module internal FSharp.Compiler.AbstractIL.Diagnostics let mutable diagnosticsLog = Some stdout -let setDiagnosticsChannel s = diagnosticsLog <- s - -let dflushn () = match diagnosticsLog with None -> () | Some d -> d.WriteLine(); d.Flush() -let dflush () = match diagnosticsLog with None -> () | Some d -> d.Flush() -let dprintn (s:string) = - match diagnosticsLog with None -> () | Some d -> d.Write s; d.Write "\n"; dflush() - -let dprintf (fmt: Format<_,_,_,_>) = - Printf.kfprintf dflush (match diagnosticsLog with None -> System.IO.TextWriter.Null | Some d -> d) fmt - -let dprintfn (fmt: Format<_,_,_,_>) = - Printf.kfprintf dflushn (match diagnosticsLog with None -> System.IO.TextWriter.Null | Some d -> d) fmt - +let setDiagnosticsChannel s = diagnosticsLog <- s + +let dflushn () = + match diagnosticsLog with + | None -> () + | Some d -> + d.WriteLine() + d.Flush() + +let dflush () = + match diagnosticsLog with + | None -> () + | Some d -> d.Flush() + +let dprintn (s: string) = + match diagnosticsLog with + | None -> () + | Some d -> + d.Write s + d.Write "\n" + dflush () + +let dprintf (fmt: Format<_, _, _, _>) = + Printf.kfprintf + dflush + (match diagnosticsLog with + | None -> System.IO.TextWriter.Null + | Some d -> d) + fmt + +let dprintfn (fmt: Format<_, _, _, _>) = + Printf.kfprintf + dflushn + (match diagnosticsLog with + | None -> System.IO.TextWriter.Null + | Some d -> d) + fmt diff --git a/src/Compiler/Utilities/illib.fs b/src/Compiler/Utilities/illib.fs index ac837cd6a81..f9cfe5ce0e8 100644 --- a/src/Compiler/Utilities/illib.fs +++ b/src/Compiler/Utilities/illib.fs @@ -1,6 +1,6 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. -namespace Internal.Utilities.Library +namespace Internal.Utilities.Library open System open System.Collections.Generic @@ -24,27 +24,39 @@ module internal PervasiveAutoOpens = /// Returns true if the list has less than 2 elements. Otherwise false. let inline isNilOrSingleton l = match l with - | [] - | [_] -> true + | [] + | [ _ ] -> true | _ -> false /// Returns true if the list contains exactly 1 element. Otherwise false. let inline isSingleton l = match l with - | [_] -> true + | [ _ ] -> true | _ -> false - type 'T MaybeNull when 'T : null and 'T: not struct = 'T + type 'T MaybeNull when 'T: null and 'T: not struct = 'T let inline isNotNull (x: 'T) = not (isNull x) - let inline (|NonNullQuick|) (x: 'T MaybeNull) = match x with null -> raise (NullReferenceException()) | v -> v + let inline (|NonNullQuick|) (x: 'T MaybeNull) = + match x with + | null -> raise (NullReferenceException()) + | v -> v - let inline nonNull (x: 'T MaybeNull) = match x with null -> raise (NullReferenceException()) | v -> v + let inline nonNull (x: 'T MaybeNull) = + match x with + | null -> raise (NullReferenceException()) + | v -> v - let inline (|Null|NonNull|) (x: 'T MaybeNull) : Choice = match x with null -> Null | v -> NonNull v + let inline (|Null|NonNull|) (x: 'T MaybeNull) : Choice = + match x with + | null -> Null + | v -> NonNull v - let inline nullArgCheck paramName (x: 'T MaybeNull) = match x with null -> raise (ArgumentNullException(paramName)) | v -> v + let inline nullArgCheck paramName (x: 'T MaybeNull) = + match x with + | null -> raise (ArgumentNullException(paramName)) + | v -> v let inline (===) x y = LanguagePrimitives.PhysicalEquality x y @@ -59,7 +71,8 @@ module internal PervasiveAutoOpens = // "How can I detect if am running in Mono?" section try Type.GetType "Mono.Runtime" <> null - with _ -> + with + | _ -> // Must be robust in the case that someone else has installed a handler into System.AppDomain.OnTypeResolveEvent // that is not reliable. // This is related to bug 5506--the issue is actually a bug in VSTypeResolutionService.EnsurePopulated which is @@ -71,6 +84,7 @@ module internal PervasiveAutoOpens = #endif type String with + member inline x.StartsWithOrdinal value = x.StartsWith(value, StringComparison.Ordinal) @@ -80,62 +94,90 @@ module internal PervasiveAutoOpens = member inline x.EndsWithOrdinalIgnoreCase value = x.EndsWith(value, StringComparison.OrdinalIgnoreCase) - /// Get an initialization hole - let getHole (r: _ ref) = match r.Value with None -> failwith "getHole" | Some x -> x + /// Get an initialization hole + let getHole (r: _ ref) = + match r.Value with + | None -> failwith "getHole" + | Some x -> x let reportTime = - let mutable tFirst =None + let mutable tFirst = None let mutable tPrev = None + fun showTimes descr -> - if showTimes then + if showTimes then let t = Process.GetCurrentProcess().UserProcessorTime.TotalSeconds - let prev = match tPrev with None -> 0.0 | Some t -> t - let first = match tFirst with None -> (tFirst <- Some t; t) | Some t -> t + + let prev = + match tPrev with + | None -> 0.0 + | Some t -> t + + let first = + match tFirst with + | None -> + (tFirst <- Some t + t) + | Some t -> t + printf "ilwrite: TIME %10.3f (total) %10.3f (delta) - %s\n" (t - first) (t - prev) descr tPrev <- Some t let foldOn p f z x = f z (p x) - let notFound() = raise (KeyNotFoundException()) + let notFound () = raise (KeyNotFoundException()) type Async with - static member RunImmediate (computation: Async<'T>, ?cancellationToken ) = + + static member RunImmediate(computation: Async<'T>, ?cancellationToken) = let cancellationToken = defaultArg cancellationToken Async.DefaultCancellationToken let ts = TaskCompletionSource<'T>() let task = ts.Task + Async.StartWithContinuations( computation, (fun k -> ts.SetResult k), (fun exn -> ts.SetException exn), (fun _ -> ts.SetCanceled()), - cancellationToken) + cancellationToken + ) + task.Result /// An efficient lazy for inline storage in a class type. Results in fewer thunks. [] -type InlineDelayInit<'T when 'T : not struct> = - new (f: unit -> 'T) = {store = Unchecked.defaultof<'T>; func = Func<_>(f) } - val mutable store : 'T - val mutable func : Func<'T> - - member x.Value = - match x.func with - | null -> x.store - | _ -> - let res = LazyInitializer.EnsureInitialized(&x.store, x.func) - x.func <- Unchecked.defaultof<_> - res +type InlineDelayInit<'T when 'T: not struct> = + new(f: unit -> 'T) = + { + store = Unchecked.defaultof<'T> + func = Func<_>(f) + } + + val mutable store: 'T + val mutable func: Func<'T> + + member x.Value = + match x.func with + | null -> x.store + | _ -> + let res = LazyInitializer.EnsureInitialized(&x.store, x.func) + x.func <- Unchecked.defaultof<_> + res //------------------------------------------------------------------------- // Library: projections //------------------------------------------------------------------------ -module Order = - let orderBy (p : 'T -> 'U) = - { new IComparer<'T> with member _.Compare(x, xx) = compare (p x) (p xx) } +module Order = + let orderBy (p: 'T -> 'U) = + { new IComparer<'T> with + member _.Compare(x, xx) = compare (p x) (p xx) + } - let orderOn p (pxOrder: IComparer<'U>) = - { new IComparer<'T> with member _.Compare(x, xx) = pxOrder.Compare (p x, p xx) } + let orderOn p (pxOrder: IComparer<'U>) = + { new IComparer<'T> with + member _.Compare(x, xx) = pxOrder.Compare(p x, p xx) + } let toFunction (pxOrder: IComparer<'U>) x y = pxOrder.Compare(x, y) @@ -143,82 +185,100 @@ module Order = // Library: arrays, lists, options, resizearrays //------------------------------------------------------------------------- -module Array = +module Array = let mapq f inp = match inp with - | [| |] -> inp - | _ -> - let res = Array.map f inp - let len = inp.Length + | [||] -> inp + | _ -> + let res = Array.map f inp + let len = inp.Length let mutable eq = true - let mutable i = 0 - while eq && i < len do - if not (inp[i] === res[i]) then eq <- false + let mutable i = 0 + + while eq && i < len do + if not (inp[i] === res[i]) then + eq <- false + i <- i + 1 + if eq then inp else res - let lengthsEqAndForall2 p l1 l2 = - Array.length l1 = Array.length l2 && - Array.forall2 p l1 l2 - - let order (eltOrder: IComparer<'T>) = - { new IComparer> with - member _.Compare(xs, ys) = - let c = compare xs.Length ys.Length - if c <> 0 then c else - let rec loop i = - if i >= xs.Length then 0 else - let c = eltOrder.Compare(xs[i], ys[i]) - if c <> 0 then c else - loop (i+1) - loop 0 } - - let existsOne p l = + let lengthsEqAndForall2 p l1 l2 = + Array.length l1 = Array.length l2 && Array.forall2 p l1 l2 + + let order (eltOrder: IComparer<'T>) = + { new IComparer> with + member _.Compare(xs, ys) = + let c = compare xs.Length ys.Length + + if c <> 0 then + c + else + let rec loop i = + if i >= xs.Length then + 0 + else + let c = eltOrder.Compare(xs[i], ys[i]) + if c <> 0 then c else loop (i + 1) + + loop 0 + } + + let existsOne p l = let rec forallFrom p l n = - (n >= Array.length l) || (p l[n] && forallFrom p l (n+1)) + (n >= Array.length l) || (p l[n] && forallFrom p l (n + 1)) let rec loop p l n = - (n < Array.length l) && - (if p l[n] then forallFrom (fun x -> not (p x)) l (n+1) else loop p l (n+1)) - + (n < Array.length l) + && (if p l[n] then + forallFrom (fun x -> not (p x)) l (n + 1) + else + loop p l (n + 1)) + loop p l 0 - let existsTrue (arr: bool[]) = - let rec loop n = (n < arr.Length) && (arr[n] || loop (n+1)) + let existsTrue (arr: bool[]) = + let rec loop n = + (n < arr.Length) && (arr[n] || loop (n + 1)) + loop 0 - - let findFirstIndexWhereTrue (arr: _[]) p = - let rec look lo hi = + + let findFirstIndexWhereTrue (arr: _[]) p = + let rec look lo hi = assert ((lo >= 0) && (hi >= 0)) assert ((lo <= arr.Length) && (hi <= arr.Length)) - if lo = hi then lo + + if lo = hi then + lo else - let i = (lo+hi)/2 - if p arr[i] then - if i = 0 then i - else - if p arr[i-1] then - look lo i - else - i + let i = (lo + hi) / 2 + + if p arr[i] then + if i = 0 then i + else if p arr[i - 1] then look lo i + else i else // not true here, look after - look (i+1) hi + look (i + 1) hi + look 0 arr.Length - + /// pass an array byref to reverse it in place - let revInPlace (array: 'T []) = - if Array.isEmpty array then () else - let arrLen, revLen = array.Length-1, array.Length/2 - 1 - for idx in 0 .. revLen do - let t1 = array[idx] - let t2 = array[arrLen-idx] - array[idx] <- t2 - array[arrLen-idx] <- t1 + let revInPlace (array: 'T[]) = + if Array.isEmpty array then + () + else + let arrLen, revLen = array.Length - 1, array.Length / 2 - 1 + + for idx in 0..revLen do + let t1 = array[idx] + let t2 = array[arrLen - idx] + array[idx] <- t2 + array[arrLen - idx] <- t1 /// Async implementation of Array.map. - let mapAsync (mapping : 'T -> Async<'U>) (array : 'T[]) : Async<'U[]> = + let mapAsync (mapping: 'T -> Async<'U>) (array: 'T[]) : Async<'U[]> = let len = Array.length array let result = Array.zeroCreate len @@ -230,10 +290,12 @@ module Array = // Return the completed results. return result } - + /// Returns a new array with an element replaced with a given value. - let replace index value (array: _ []) = - if index >= array.Length then raise (IndexOutOfRangeException "index") + let replace index value (array: _[]) = + if index >= array.Length then + raise (IndexOutOfRangeException "index") + let res = Array.copy array res[index] <- value res @@ -241,147 +303,195 @@ module Array = /// Optimized arrays equality. ~100x faster than `array1 = array2` on strings. /// ~2x faster for floats /// ~0.8x slower for ints - let inline areEqual (xs: 'T []) (ys: 'T []) = + let inline areEqual (xs: 'T[]) (ys: 'T[]) = match xs, ys with | null, null -> true | [||], [||] -> true - | null, _ | _, null -> false + | null, _ + | _, null -> false | _ when xs.Length <> ys.Length -> false | _ -> let mutable break' = false let mutable i = 0 let mutable result = true + while i < xs.Length && not break' do - if xs[i] <> ys[i] then + if xs[i] <> ys[i] then break' <- true result <- false + i <- i + 1 + result /// Returns all heads of a given array. /// For [|1;2;3|] it returns [|[|1; 2; 3|]; [|1; 2|]; [|1|]|] - let heads (array: 'T []) = + let heads (array: 'T[]) = let res = Array.zeroCreate<'T[]> array.Length + for i = array.Length - 1 downto 0 do res[i] <- array[0..i] + res - /// check if subArray is found in the wholeArray starting + /// check if subArray is found in the wholeArray starting /// at the provided index - let inline isSubArray (subArray: 'T []) (wholeArray:'T []) index = - if subArray.Length = 0 then true - elif subArray.Length > wholeArray.Length then false - elif subArray.Length = wholeArray.Length then areEqual subArray wholeArray else - let rec loop subidx idx = - if subidx = subArray.Length then true - elif subArray[subidx] = wholeArray[idx] then loop (subidx+1) (idx+1) - else false - loop 0 index - + let inline isSubArray (subArray: 'T[]) (wholeArray: 'T[]) index = + if subArray.Length = 0 then + true + elif subArray.Length > wholeArray.Length then + false + elif subArray.Length = wholeArray.Length then + areEqual subArray wholeArray + else + let rec loop subidx idx = + if subidx = subArray.Length then + true + elif subArray[subidx] = wholeArray[idx] then + loop (subidx + 1) (idx + 1) + else + false + + loop 0 index + /// Returns true if one array has another as its subset from index 0. - let startsWith (prefix: _ []) (whole: _ []) = - isSubArray prefix whole 0 - + let startsWith (prefix: _[]) (whole: _[]) = isSubArray prefix whole 0 + /// Returns true if one array has trailing elements equal to another's. - let endsWith (suffix: _ []) (whole: _ []) = - isSubArray suffix whole (whole.Length-suffix.Length) - -module Option = - - let mapFold f s opt = - match opt with - | None -> None, s - | Some x -> - let x2, s2 = f s x + let endsWith (suffix: _[]) (whole: _[]) = + isSubArray suffix whole (whole.Length - suffix.Length) + +module Option = + + let mapFold f s opt = + match opt with + | None -> None, s + | Some x -> + let x2, s2 = f s x Some x2, s2 - let attempt (f: unit -> 'T) = try Some (f()) with _ -> None - -module List = + let attempt (f: unit -> 'T) = + try + Some(f ()) + with + | _ -> None + +module List = + + let sortWithOrder (c: IComparer<'T>) elements = + List.sortWith (Order.toFunction c) elements + + let splitAfter n l = + let rec split_after_acc n l1 l2 = + if n <= 0 then + List.rev l1, l2 + else + split_after_acc (n - 1) ((List.head l2) :: l1) (List.tail l2) - let sortWithOrder (c: IComparer<'T>) elements = List.sortWith (Order.toFunction c) elements - - let splitAfter n l = - let rec split_after_acc n l1 l2 = if n <= 0 then List.rev l1, l2 else split_after_acc (n-1) ((List.head l2) :: l1) (List.tail l2) split_after_acc n [] l - let existsi f xs = - let rec loop i xs = match xs with [] -> false | h :: t -> f i h || loop (i+1) t - loop 0 xs - - let lengthsEqAndForall2 p l1 l2 = - List.length l1 = List.length l2 && - List.forall2 p l1 l2 + let existsi f xs = + let rec loop i xs = + match xs with + | [] -> false + | h :: t -> f i h || loop (i + 1) t - let rec findi n f l = - match l with + loop 0 xs + + let lengthsEqAndForall2 p l1 l2 = + List.length l1 = List.length l2 && List.forall2 p l1 l2 + + let rec findi n f l = + match l with | [] -> None - | h :: t -> if f h then Some (h, n) else findi (n+1) f t + | h :: t -> + if f h then + Some(h, n) + else + findi (n + 1) f t let splitChoose select l = - let rec ch acc1 acc2 l = - match l with + let rec ch acc1 acc2 l = + match l with | [] -> List.rev acc1, List.rev acc2 - | x :: xs -> + | x :: xs -> match select x with | Choice1Of2 sx -> ch (sx :: acc1) acc2 xs | Choice2Of2 sx -> ch acc1 (sx :: acc2) xs ch [] [] l - let rec checkq l1 l2 = - match l1, l2 with + let rec checkq l1 l2 = + match l1, l2 with | h1 :: t1, h2 :: t2 -> h1 === h2 && checkq t1 t2 | _ -> true let mapq (f: 'T -> 'T) inp = - assert not typeof<'T>.IsValueType + assert not typeof<'T>.IsValueType + match inp with | [] -> inp - | [h1a] -> + | [ h1a ] -> let h2a = f h1a - if h1a === h2a then inp else [h2a] - | [h1a; h1b] -> + if h1a === h2a then inp else [ h2a ] + | [ h1a; h1b ] -> let h2a = f h1a let h2b = f h1b - if h1a === h2a && h1b === h2b then inp else [h2a; h2b] - | [h1a; h1b; h1c] -> + + if h1a === h2a && h1b === h2b then + inp + else + [ h2a; h2b ] + | [ h1a; h1b; h1c ] -> let h2a = f h1a let h2b = f h1b let h2c = f h1c - if h1a === h2a && h1b === h2b && h1c === h2c then inp else [h2a; h2b; h2c] - | _ -> - let res = List.map f inp + + if h1a === h2a && h1b === h2b && h1c === h2c then + inp + else + [ h2a; h2b; h2c ] + | _ -> + let res = List.map f inp if checkq inp res then inp else res - - let frontAndBack l = - let rec loop acc l = + + let frontAndBack l = + let rec loop acc l = match l with - | [] -> + | [] -> Debug.Assert(false, "empty list") - invalidArg "l" "empty list" - | [h] -> List.rev acc, h + invalidArg "l" "empty list" + | [ h ] -> List.rev acc, h | h :: t -> loop (h :: acc) t + loop [] l - let tryRemove f inp = - let rec loop acc l = + let tryRemove f inp = + let rec loop acc l = match l with | [] -> None - | h :: t -> if f h then Some (h, List.rev acc @ t) else loop (h :: acc) t + | h :: t -> + if f h then + Some(h, List.rev acc @ t) + else + loop (h :: acc) t + loop [] inp - let zip4 l1 l2 l3 l4 = - List.zip l1 (List.zip3 l2 l3 l4) |> List.map (fun (x1, (x2, x3, x4)) -> (x1, x2, x3, x4)) + let zip4 l1 l2 l3 l4 = + List.zip l1 (List.zip3 l2 l3 l4) + |> List.map (fun (x1, (x2, x3, x4)) -> (x1, x2, x3, x4)) - let unzip4 l = + let unzip4 l = let a, b, cd = List.unzip3 (List.map (fun (x, y, z, w) -> (x, y, (z, w))) l) let c, d = List.unzip cd a, b, c, d - let rec iter3 f l1 l2 l3 = - match l1, l2, l3 with - | h1 :: t1, h2 :: t2, h3 :: t3 -> f h1 h2 h3; iter3 f t1 t2 t3 + let rec iter3 f l1 l2 l3 = + match l1, l2, l3 with + | h1 :: t1, h2 :: t2, h3 :: t3 -> + f h1 h2 h3 + iter3 f t1 t2 t3 | [], [], [] -> () | _ -> failwith "iter3" @@ -389,65 +499,82 @@ module List = let rec loop acc l = match l with | [] -> List.rev acc, [] - | x :: xs -> if p x then List.rev acc, l else loop (x :: acc) xs + | x :: xs -> + if p x then + List.rev acc, l + else + loop (x :: acc) xs + loop [] l let order (eltOrder: IComparer<'T>) = - { new IComparer<'T list> with - member _.Compare(xs, ys) = - let rec loop xs ys = - match xs, ys with - | [], [] -> 0 - | [], _ -> -1 - | _, [] -> 1 - | x :: xs, y :: ys -> - let cxy = eltOrder.Compare(x, y) - if cxy=0 then loop xs ys else cxy - loop xs ys } - - let indexNotFound() = raise (KeyNotFoundException("An index satisfying the predicate was not found in the collection")) - - let rec assoc x l = - match l with - | [] -> indexNotFound() + { new IComparer<'T list> with + member _.Compare(xs, ys) = + let rec loop xs ys = + match xs, ys with + | [], [] -> 0 + | [], _ -> -1 + | _, [] -> 1 + | x :: xs, y :: ys -> + let cxy = eltOrder.Compare(x, y) + if cxy = 0 then loop xs ys else cxy + + loop xs ys + } + + let indexNotFound () = + raise (KeyNotFoundException("An index satisfying the predicate was not found in the collection")) + + let rec assoc x l = + match l with + | [] -> indexNotFound () | (h, r) :: t -> if x = h then r else assoc x t - let rec memAssoc x l = - match l with + let rec memAssoc x l = + match l with | [] -> false | (h, _) :: t -> x = h || memAssoc x t - let rec memq x l = - match l with - | [] -> false + let rec memq x l = + match l with + | [] -> false | h :: t -> LanguagePrimitives.PhysicalEquality x h || memq x t let mapNth n f xs = - let rec mn i = function - | [] -> [] - | x :: xs -> if i=n then f x :: xs else x :: mn (i+1) xs - + let rec mn i = + function + | [] -> [] + | x :: xs -> + if i = n then + f x :: xs + else + x :: mn (i + 1) xs + mn 0 xs - let count pred xs = List.fold (fun n x -> if pred x then n+1 else n) 0 xs - let headAndTail l = - match l with - | [] -> failwith "headAndTail" - | h::t -> (h,t) + let count pred xs = + List.fold (fun n x -> if pred x then n + 1 else n) 0 xs - // WARNING: not tail-recursive - let mapHeadTail fhead ftail = function - | [] -> [] - | [x] -> [fhead x] - | x :: xs -> fhead x :: List.map ftail xs + let headAndTail l = + match l with + | [] -> failwith "headAndTail" + | h :: t -> (h, t) + + // WARNING: not tail-recursive + let mapHeadTail fhead ftail = + function + | [] -> [] + | [ x ] -> [ fhead x ] + | x :: xs -> fhead x :: List.map ftail xs - let collectFold f s l = - let l, s = List.mapFold f s l - List.concat l, s + let collectFold f s l = + let l, s = List.mapFold f s l + List.concat l, s let collect2 f xs ys = List.concat (List.map2 f xs ys) - let toArraySquared xss = xss |> List.map List.toArray |> List.toArray + let toArraySquared xss = + xss |> List.map List.toArray |> List.toArray let iterSquared f xss = xss |> List.iter (List.iter f) @@ -459,26 +586,29 @@ module List = let forallSquared f xss = xss |> List.forall (List.forall f) - let mapiSquared f xss = xss |> List.mapi (fun i xs -> xs |> List.mapi (fun j x -> f i j x)) + let mapiSquared f xss = + xss |> List.mapi (fun i xs -> xs |> List.mapi (fun j x -> f i j x)) - let existsSquared f xss = xss |> List.exists (fun xs -> xs |> List.exists (fun x -> f x)) + let existsSquared f xss = + xss |> List.exists (fun xs -> xs |> List.exists (fun x -> f x)) - let mapiFoldSquared f z xss = mapFoldSquared f z (xss |> mapiSquared (fun i j x -> (i, j, x))) + let mapiFoldSquared f z xss = + mapFoldSquared f z (xss |> mapiSquared (fun i j x -> (i, j, x))) let duplicates (xs: 'T list) = xs |> List.groupBy id - |> List.filter (fun (_, elems) -> Seq.length elems > 1) - |> List.map fst + |> List.filter (fun (_, elems) -> Seq.length elems > 1) + |> List.map fst let internal allEqual (xs: 'T list) = - match xs with + match xs with | [] -> true - | h::t -> t |> List.forall (fun h2 -> h = h2) + | h :: t -> t |> List.forall (fun h2 -> h = h2) let isSingleton xs = match xs with - | [_] -> true + | [ _ ] -> true | _ -> false module ResizeArray = @@ -489,27 +619,31 @@ module ResizeArray = let chunkBySize chunkSize f (items: ResizeArray<'t>) = // we could use Seq.chunkBySize here, but that would involve many enumerator.MoveNext() calls that we can sidestep with a bit of math let itemCount = items.Count - if itemCount = 0 - then [||] + + if itemCount = 0 then + [||] else let chunksCount = match itemCount / chunkSize with | n when itemCount % chunkSize = 0 -> n | n -> n + 1 // any remainder means we need an additional chunk to store it - [| for index in 0..chunksCount-1 do - let startIndex = index * chunkSize - let takeCount = min (itemCount - startIndex) chunkSize + [| + for index in 0 .. chunksCount - 1 do + let startIndex = index * chunkSize + let takeCount = min (itemCount - startIndex) chunkSize + + let holder = Array.zeroCreate takeCount + // we take a bounds-check hit here on each access. + // other alternatives here include + // * iterating across an IEnumerator (incurs MoveNext penalty) + // * doing a block copy using `List.CopyTo(index, array, index, count)` (requires more copies to do the mapping) + // none are significantly better. + for i in 0 .. takeCount - 1 do + holder[i] <- f items[i] - let holder = Array.zeroCreate takeCount - // we take a bounds-check hit here on each access. - // other alternatives here include - // * iterating across an IEnumerator (incurs MoveNext penalty) - // * doing a block copy using `List.CopyTo(index, array, index, count)` (requires more copies to do the mapping) - // none are significantly better. - for i in 0 .. takeCount - 1 do - holder[i] <- f items[i] - yield holder |] + yield holder + |] /// Split a large ResizeArray into a series of array chunks that are each under the Large Object Heap limit. /// This is done to help prevent a stop-the-world collection of the single large array, instead allowing for a greater @@ -525,9 +659,15 @@ module ResizeArray = module ValueOptionInternal = - let inline ofOption x = match x with Some x -> ValueSome x | None -> ValueNone + let inline ofOption x = + match x with + | Some x -> ValueSome x + | None -> ValueNone - let inline bind f x = match x with ValueSome x -> f x | ValueNone -> ValueNone + let inline bind f x = + match x with + | ValueSome x -> f x + | ValueNone -> ValueNone module String = let make (n: int) (c: char) : string = String(c, n) @@ -539,99 +679,114 @@ module String = let contains (s: string) (c: char) = s.IndexOf c <> -1 let order = LanguagePrimitives.FastGenericComparer - - let lowercase (s: string) = - s.ToLowerInvariant() - let uppercase (s: string) = - s.ToUpperInvariant() + let lowercase (s: string) = s.ToLowerInvariant() + + let uppercase (s: string) = s.ToUpperInvariant() // Scripts that distinguish between upper and lower case (bicameral) DU Discriminators and Active Pattern identifiers are required to start with an upper case character. - // For valid identifiers where the case of the identifier can not be determined because there is no upper and lower case we will allow DU Discriminators and upper case characters - // to be used. This means that developers using unicameral scripts such as hindi, are not required to prefix these identifiers with an Upper case latin character. + // For valid identifiers where the case of the identifier can not be determined because there is no upper and lower case we will allow DU Discriminators and upper case characters + // to be used. This means that developers using unicameral scripts such as hindi, are not required to prefix these identifiers with an Upper case latin character. // - let isLeadingIdentifierCharacterUpperCase (s:string) = + let isLeadingIdentifierCharacterUpperCase (s: string) = let isUpperCaseCharacter c = // if IsUpper and IsLower return the same value, then we can't tell if it's upper or lower case, so ensure it is a letter // otherwise it is bicameral, so must be upper case let isUpper = Char.IsUpper c - if isUpper = Char.IsLower c then Char.IsLetter c - else isUpper + + if isUpper = Char.IsLower c then + Char.IsLetter c + else + isUpper s.Length >= 1 && isUpperCaseCharacter s[0] let capitalize (s: string) = - if s.Length = 0 then s - else uppercase s[0..0] + s[ 1.. s.Length - 1 ] + if s.Length = 0 then + s + else + uppercase s[0..0] + s[1 .. s.Length - 1] let uncapitalize (s: string) = - if s.Length = 0 then s - else lowercase s[0..0] + s[ 1.. s.Length - 1 ] + if s.Length = 0 then + s + else + lowercase s[0..0] + s[1 .. s.Length - 1] - let dropPrefix (s: string) (t: string) = s[t.Length..s.Length - 1] + let dropPrefix (s: string) (t: string) = s[t.Length .. s.Length - 1] - let dropSuffix (s: string) (t: string) = s[0..s.Length - t.Length - 1] + let dropSuffix (s: string) (t: string) = s[0 .. s.Length - t.Length - 1] let inline toCharArray (str: string) = str.ToCharArray() let lowerCaseFirstChar (str: string) = - if String.IsNullOrEmpty str - || Char.IsLower(str, 0) then str else - let strArr = toCharArray str - match Array.tryHead strArr with - | None -> str - | Some c -> - strArr[0] <- Char.ToLower c - String strArr + if String.IsNullOrEmpty str || Char.IsLower(str, 0) then + str + else + let strArr = toCharArray str + + match Array.tryHead strArr with + | None -> str + | Some c -> + strArr[0] <- Char.ToLower c + String strArr let extractTrailingIndex (str: string) = - let charr = str.ToCharArray() + let charr = str.ToCharArray() Array.revInPlace charr let digits = Array.takeWhile Char.IsDigit charr Array.revInPlace digits + String digits |> function | "" -> str, None - | index -> str.Substring (0, str.Length - index.Length), Some (int index) + | index -> str.Substring(0, str.Length - index.Length), Some(int index) /// Splits a string into substrings based on the strings in the array separators - let split options (separator: string []) (value: string) = - value.Split(separator, options) + let split options (separator: string[]) (value: string) = value.Split(separator, options) let (|StartsWith|_|) pattern value = if String.IsNullOrWhiteSpace value then None elif value.StartsWithOrdinal pattern then Some() - else None + else + None let (|Contains|_|) pattern value = if String.IsNullOrWhiteSpace value then None elif value.Contains pattern then Some() - else None + else + None let getLines (str: string) = use reader = new StringReader(str) + [| let mutable line = reader.ReadLine() + while not (isNull line) do yield line line <- reader.ReadLine() + if str.EndsWithOrdinal("\n") then // last trailing space not returned // http://stackoverflow.com/questions/19365404/stringreader-omits-trailing-linebreak yield String.Empty |] -module Dictionary = - let inline newWithSize (size: int) = Dictionary<_, _>(size, HashIdentity.Structural) +module Dictionary = + let inline newWithSize (size: int) = + Dictionary<_, _>(size, HashIdentity.Structural) - let inline ofList (xs: ('Key * 'Value) list) = + let inline ofList (xs: ('Key * 'Value) list) = let t = Dictionary<_, _>(List.length xs, HashIdentity.Structural) - for k,v in xs do - t.Add(k,v) + + for k, v in xs do + t.Add(k, v) + t [] @@ -641,7 +796,7 @@ type DictionaryExtensions() = static member inline BagAdd(dic: Dictionary<'key, 'value list>, key: 'key, value: 'value) = match dic.TryGetValue key with | true, values -> dic[key] <- value :: values - | _ -> dic[key] <- [value] + | _ -> dic[key] <- [ value ] [] static member inline BagExistsValueForKey(dic: Dictionary<'key, 'value list>, key: 'key, f: 'value -> bool) = @@ -649,32 +804,37 @@ type DictionaryExtensions() = | true, values -> values |> List.exists f | _ -> false -module Lazy = +module Lazy = let force (x: Lazy<'T>) = x.Force() //---------------------------------------------------------------------------- // Single threaded execution and mutual exclusion /// Represents a permission active at this point in execution -type ExecutionToken = interface end +type ExecutionToken = + interface + end -/// Represents a token that indicates execution on the compilation thread, i.e. +/// Represents a token that indicates execution on the compilation thread, i.e. /// - we have full access to the (partially mutable) TAST and TcImports data structures /// - compiler execution may result in type provider invocations when resolving types and members /// - we can access various caches in the SourceCodeServices /// /// Like other execution tokens this should be passed via argument passing and not captured/stored beyond -/// the lifetime of stack-based calls. This is not checked, it is a discipline within the compiler code. +/// the lifetime of stack-based calls. This is not checked, it is a discipline within the compiler code. [] -type CompilationThreadToken() = interface ExecutionToken +type CompilationThreadToken() = + interface ExecutionToken /// A base type for various types of tokens that must be passed when a lock is taken. /// Each different static lock should declare a new subtype of this type. -type LockToken = inherit ExecutionToken +type LockToken = + inherit ExecutionToken /// Represents a token that indicates execution on any of several potential user threads calling the F# compiler services. [] -type AnyCallerThreadToken() = interface ExecutionToken +type AnyCallerThreadToken() = + interface ExecutionToken [] module internal LockAutoOpens = @@ -687,151 +847,172 @@ module internal LockAutoOpens = let DoesNotRequireCompilerThreadTokenAndCouldPossiblyBeMadeConcurrent (_ctok: CompilationThreadToken) = () /// Represents a place in the compiler codebase where we assume we are executing on a compilation thread - let AssumeCompilationThreadWithoutEvidence () = Unchecked.defaultof + let AssumeCompilationThreadWithoutEvidence () = + Unchecked.defaultof let AnyCallerThread = Unchecked.defaultof let AssumeLockWithoutEvidence<'LockTokenType when 'LockTokenType :> LockToken> () = Unchecked.defaultof<'LockTokenType> /// Encapsulates a lock associated with a particular token-type representing the acquisition of that lock. -type Lock<'LockTokenType when 'LockTokenType :> LockToken>() = - let lockObj = obj() - member _.AcquireLock f = lock lockObj (fun () -> f (AssumeLockWithoutEvidence<'LockTokenType>())) +type Lock<'LockTokenType when 'LockTokenType :> LockToken>() = + let lockObj = obj () + + member _.AcquireLock f = + lock lockObj (fun () -> f (AssumeLockWithoutEvidence<'LockTokenType>())) //--------------------------------------------------- // Misc -module Map = - let tryFindMulti k map = match Map.tryFind k map with Some res -> res | None -> [] +module Map = + let tryFindMulti k map = + match Map.tryFind k map with + | Some res -> res + | None -> [] [] type ResultOrException<'TResult> = | Result of result: 'TResult | Exception of ``exception``: Exception - -module ResultOrException = + +module ResultOrException = let success a = Result a let raze (b: exn) = Exception b // map - let (|?>) res f = - match res with - | Result x -> Result(f x ) + let (|?>) res f = + match res with + | Result x -> Result(f x) | Exception err -> Exception err - - let ForceRaise res = - match res with + + let ForceRaise res = + match res with | Result x -> x | Exception err -> raise err let otherwise f x = - match x with + match x with | Result x -> success x - | Exception _err -> f() + | Exception _err -> f () -[] +[] type ValueOrCancelled<'TResult> = | Value of result: 'TResult | Cancelled of ``exception``: OperationCanceledException /// Represents a cancellable computation with explicit representation of a cancelled result. /// -/// A cancellable computation is passed may be cancelled via a CancellationToken, which is propagated implicitly. -/// If cancellation occurs, it is propagated as data rather than by raising an OperationCanceledException. +/// A cancellable computation is passed may be cancelled via a CancellationToken, which is propagated implicitly. +/// If cancellation occurs, it is propagated as data rather than by raising an OperationCanceledException. [] type Cancellable<'TResult> = Cancellable of (CancellationToken -> ValueOrCancelled<'TResult>) -module Cancellable = +module Cancellable = /// Run a cancellable computation using the given cancellation token - let run (ct: CancellationToken) (Cancellable oper) = - if ct.IsCancellationRequested then - ValueOrCancelled.Cancelled (OperationCanceledException ct) + let run (ct: CancellationToken) (Cancellable oper) = + if ct.IsCancellationRequested then + ValueOrCancelled.Cancelled(OperationCanceledException ct) else - oper ct + oper ct /// Bind the result of a cancellable computation - let inline bind f comp1 = - Cancellable (fun ct -> - match run ct comp1 with - | ValueOrCancelled.Value v1 -> run ct (f v1) + let inline bind f comp1 = + Cancellable(fun ct -> + match run ct comp1 with + | ValueOrCancelled.Value v1 -> run ct (f v1) | ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1) /// Map the result of a cancellable computation - let inline map f oper = - Cancellable (fun ct -> - match run ct oper with - | ValueOrCancelled.Value res -> ValueOrCancelled.Value (f res) - | ValueOrCancelled.Cancelled err -> ValueOrCancelled.Cancelled err) - + let inline map f oper = + Cancellable(fun ct -> + match run ct oper with + | ValueOrCancelled.Value res -> ValueOrCancelled.Value(f res) + | ValueOrCancelled.Cancelled err -> ValueOrCancelled.Cancelled err) + /// Return a simple value as the result of a cancellable computation - let inline ret x = Cancellable (fun _ -> ValueOrCancelled.Value x) + let inline ret x = + Cancellable(fun _ -> ValueOrCancelled.Value x) /// Fold a cancellable computation along a sequence of inputs - let fold f acc seq = - Cancellable (fun ct -> - (ValueOrCancelled.Value acc, seq) - ||> Seq.fold (fun acc x -> - match acc with - | ValueOrCancelled.Value accv -> run ct (f accv x) - | res -> res)) - + let fold f acc seq = + Cancellable(fun ct -> + (ValueOrCancelled.Value acc, seq) + ||> Seq.fold (fun acc x -> + match acc with + | ValueOrCancelled.Value accv -> run ct (f accv x) + | res -> res)) + /// Iterate a cancellable computation over a collection let inline each f seq = fold (fun acc x -> f x |> map (fun y -> (y :: acc))) [] seq |> map List.rev - + /// Delay a cancellable computation - let inline delay (f: unit -> Cancellable<'T>) = Cancellable (fun ct -> let (Cancellable g) = f() in g ct) + let inline delay (f: unit -> Cancellable<'T>) = + Cancellable(fun ct -> let (Cancellable g) = f () in g ct) - /// Run the computation in a mode where it may not be cancelled. The computation never results in a + /// Run the computation in a mode where it may not be cancelled. The computation never results in a /// ValueOrCancelled.Cancelled. - let runWithoutCancellation comp = - let res = run CancellationToken.None comp - match res with - | ValueOrCancelled.Cancelled _ -> failwith "unexpected cancellation" + let runWithoutCancellation comp = + let res = run CancellationToken.None comp + + match res with + | ValueOrCancelled.Cancelled _ -> failwith "unexpected cancellation" | ValueOrCancelled.Value r -> r let toAsync c = async { let! ct = Async.CancellationToken let res = run ct c - return! Async.FromContinuations (fun (cont, _econt, ccont) -> - match res with - | ValueOrCancelled.Value v -> cont v - | ValueOrCancelled.Cancelled ce -> ccont ce) - } + + return! + Async.FromContinuations(fun (cont, _econt, ccont) -> + match res with + | ValueOrCancelled.Value v -> cont v + | ValueOrCancelled.Cancelled ce -> ccont ce) + } /// Bind the cancellation token associated with the computation - let token () = Cancellable (fun ct -> ValueOrCancelled.Value ct) + let token () = + Cancellable(fun ct -> ValueOrCancelled.Value ct) /// Represents a canceled computation - let canceled() = Cancellable (fun ct -> ValueOrCancelled.Cancelled (OperationCanceledException ct)) + let canceled () = + Cancellable(fun ct -> ValueOrCancelled.Cancelled(OperationCanceledException ct)) /// Catch exceptions in a computation - let inline catch comp = + let inline catch comp = let (Cancellable f) = comp - Cancellable (fun ct -> - try - match f ct with - | ValueOrCancelled.Value res -> ValueOrCancelled.Value (Choice1Of2 res) - | ValueOrCancelled.Cancelled exn -> ValueOrCancelled.Cancelled exn - with err -> - ValueOrCancelled.Value (Choice2Of2 err)) + + Cancellable(fun ct -> + try + match f ct with + | ValueOrCancelled.Value res -> ValueOrCancelled.Value(Choice1Of2 res) + | ValueOrCancelled.Cancelled exn -> ValueOrCancelled.Cancelled exn + with + | err -> ValueOrCancelled.Value(Choice2Of2 err)) /// Implement try/finally for a cancellable computation let inline tryFinally comp compensation = - catch comp |> bind (fun res -> - compensation() - match res with Choice1Of2 r -> ret r | Choice2Of2 err -> raise err) + catch comp + |> bind (fun res -> + compensation () + + match res with + | Choice1Of2 r -> ret r + | Choice2Of2 err -> raise err) /// Implement try/with for a cancellable computation - let inline tryWith comp handler = - catch comp |> bind (fun res -> - match res with Choice1Of2 r -> ret r | Choice2Of2 err -> handler err) - -type CancellableBuilder() = + let inline tryWith comp handler = + catch comp + |> bind (fun res -> + match res with + | Choice1Of2 r -> ret r + | Choice2Of2 err -> handler err) + +type CancellableBuilder() = member inline _.BindReturn(comp, k) = Cancellable.map k comp @@ -839,17 +1020,19 @@ type CancellableBuilder() = member inline _.Return v = Cancellable.ret v - member inline _.ReturnFrom (v: Cancellable<'T>) = v + member inline _.ReturnFrom(v: Cancellable<'T>) = v member inline _.Combine(e1, e2) = e1 |> Cancellable.bind (fun () -> e2) - member inline _.For(es, f) = es |> Cancellable.each f + member inline _.For(es, f) = es |> Cancellable.each f member inline _.TryWith(comp, handler) = Cancellable.tryWith comp handler - member inline _.Using(resource, comp) = Cancellable.tryFinally (comp resource) (fun () -> (resource :> IDisposable).Dispose()) + member inline _.Using(resource, comp) = + Cancellable.tryFinally (comp resource) (fun () -> (resource :> IDisposable).Dispose()) - member inline _.TryFinally(comp, compensation) = Cancellable.tryFinally comp compensation + member inline _.TryFinally(comp, compensation) = + Cancellable.tryFinally comp compensation member inline _.Delay f = Cancellable.delay f @@ -860,10 +1043,11 @@ module CancellableAutoOpens = let cancellable = CancellableBuilder() /// Generates unique stamps -type UniqueStampGenerator<'T when 'T : equality>() = +type UniqueStampGenerator<'T when 'T: equality>() = let gate = obj () let encodeTab = ConcurrentDictionary<'T, int>(HashIdentity.Structural) let mutable nItems = 0 + let encode str = match encodeTab.TryGetValue str with | true, idx -> idx @@ -879,24 +1063,26 @@ type UniqueStampGenerator<'T when 'T : equality>() = member _.Table = encodeTab.Keys /// memoize tables (all entries cached, never collected) -type MemoizationTable<'T, 'U>(compute: 'T -> 'U, keyComparer: IEqualityComparer<'T>, ?canMemoize) = - - let table = new ConcurrentDictionary<'T, 'U>(keyComparer) +type MemoizationTable<'T, 'U>(compute: 'T -> 'U, keyComparer: IEqualityComparer<'T>, ?canMemoize) = - member t.Apply x = - if (match canMemoize with None -> true | Some f -> f x) then + let table = new ConcurrentDictionary<'T, 'U>(keyComparer) + + member t.Apply x = + if (match canMemoize with + | None -> true + | Some f -> f x) then match table.TryGetValue x with | true, res -> res | _ -> - lock table (fun () -> + lock table (fun () -> match table.TryGetValue x with | true, res -> res | _ -> let res = compute x table[x] <- res res) - else compute x - + else + compute x exception UndefinedException @@ -907,71 +1093,88 @@ type LazyWithContextFailure(exn: exn) = member _.Exception = exn static member Undefined = undefined - + /// Just like "Lazy" but EVERY forcer must provide an instance of "ctxt", e.g. to help track errors /// on forcing back to at least one sensible user location [] [] -type LazyWithContext<'T, 'Ctxt> = - { /// This field holds the result of a successful computation. It's initial value is Unchecked.defaultof - mutable value : 'T - - /// This field holds either the function to run or a LazyWithContextFailure object recording the exception raised - /// from running the function. It is null if the thunk has been evaluated successfully. - mutable funcOrException: obj - - /// A helper to ensure we rethrow the "original" exception - findOriginalException : exn -> exn } - - static member Create(f: 'Ctxt->'T, findOriginalException) : LazyWithContext<'T, 'Ctxt> = - { value = Unchecked.defaultof<'T> - funcOrException = box f - findOriginalException = findOriginalException } +type LazyWithContext<'T, 'Ctxt> = + { + /// This field holds the result of a successful computation. It's initial value is Unchecked.defaultof + mutable value: 'T + + /// This field holds either the function to run or a LazyWithContextFailure object recording the exception raised + /// from running the function. It is null if the thunk has been evaluated successfully. + mutable funcOrException: obj + + /// A helper to ensure we rethrow the "original" exception + findOriginalException: exn -> exn + } + + static member Create(f: 'Ctxt -> 'T, findOriginalException) : LazyWithContext<'T, 'Ctxt> = + { + value = Unchecked.defaultof<'T> + funcOrException = box f + findOriginalException = findOriginalException + } - static member NotLazy(x:'T) : LazyWithContext<'T, 'Ctxt> = - { value = x - funcOrException = null - findOriginalException = id } + static member NotLazy(x: 'T) : LazyWithContext<'T, 'Ctxt> = + { + value = x + funcOrException = null + findOriginalException = id + } - member x.IsDelayed = (match x.funcOrException with null -> false | :? LazyWithContextFailure -> false | _ -> true) + member x.IsDelayed = + (match x.funcOrException with + | null -> false + | :? LazyWithContextFailure -> false + | _ -> true) - member x.IsForced = (match x.funcOrException with null -> true | _ -> false) + member x.IsForced = + (match x.funcOrException with + | null -> true + | _ -> false) - member x.Force(ctxt:'Ctxt) = - match x.funcOrException with - | null -> x.value - | _ -> + member x.Force(ctxt: 'Ctxt) = + match x.funcOrException with + | null -> x.value + | _ -> // Enter the lock in case another thread is in the process of evaluating the result - Monitor.Enter x; - try + Monitor.Enter x + + try x.UnsynchronizedForce ctxt finally Monitor.Exit x - member x.UnsynchronizedForce ctxt = - match x.funcOrException with - | null -> x.value - | :? LazyWithContextFailure as res -> - // Re-raise the original exception - raise (x.findOriginalException res.Exception) - | :? ('Ctxt -> 'T) as f -> - x.funcOrException <- box(LazyWithContextFailure.Undefined) - try - let res = f ctxt - x.value <- res - x.funcOrException <- null - res - with exn -> - x.funcOrException <- box(LazyWithContextFailure(exn)) - reraise() - | _ -> - failwith "unreachable" + member x.UnsynchronizedForce ctxt = + match x.funcOrException with + | null -> x.value + | :? LazyWithContextFailure as res -> + // Re-raise the original exception + raise (x.findOriginalException res.Exception) + | :? ('Ctxt -> 'T) as f -> + x.funcOrException <- box (LazyWithContextFailure.Undefined) + + try + let res = f ctxt + x.value <- res + x.funcOrException <- null + res + with + | exn -> + x.funcOrException <- box (LazyWithContextFailure(exn)) + reraise () + | _ -> failwith "unreachable" /// Intern tables to save space. -module Tables = - let memoize f = - let t = ConcurrentDictionary<_, _>(Environment.ProcessorCount, 1000, HashIdentity.Structural) - fun x -> +module Tables = + let memoize f = + let t = + ConcurrentDictionary<_, _>(Environment.ProcessorCount, 1000, HashIdentity.Structural) + + fun x -> match t.TryGetValue x with | true, res -> res | _ -> @@ -980,37 +1183,47 @@ module Tables = res /// Interface that defines methods for comparing objects using partial equality relation -type IPartialEqualityComparer<'T> = +type IPartialEqualityComparer<'T> = inherit IEqualityComparer<'T> /// Can the specified object be tested for equality? - abstract InEqualityRelation : 'T -> bool + abstract InEqualityRelation: 'T -> bool -module IPartialEqualityComparer = +module IPartialEqualityComparer = + + let On f (c: IPartialEqualityComparer<_>) = + { new IPartialEqualityComparer<_> with + member _.InEqualityRelation x = c.InEqualityRelation(f x) + member _.Equals(x, y) = c.Equals(f x, f y) + member _.GetHashCode x = c.GetHashCode(f x) + } - let On f (c: IPartialEqualityComparer<_>) = - { new IPartialEqualityComparer<_> with - member _.InEqualityRelation x = c.InEqualityRelation (f x) - member _.Equals(x, y) = c.Equals(f x, f y) - member _.GetHashCode x = c.GetHashCode(f x) } - // Wrapper type for use by the 'partialDistinctBy' function [] type private WrapType<'T> = Wrap of 'T - + // Like Seq.distinctBy but only filters out duplicates for some of the elements let partialDistinctBy (per: IPartialEqualityComparer<'T>) seq = - let wper = + let wper = { new IPartialEqualityComparer> with - member _.InEqualityRelation (Wrap x) = per.InEqualityRelation x + member _.InEqualityRelation(Wrap x) = per.InEqualityRelation x member _.Equals(Wrap x, Wrap y) = per.Equals(x, y) - member _.GetHashCode (Wrap x) = per.GetHashCode x } + member _.GetHashCode(Wrap x) = per.GetHashCode x + } // Wrap a Wrap _ around all keys in case the key type is itself a type using null as a representation let dict = Dictionary, obj>(wper) - seq |> List.filter (fun v -> + + seq + |> List.filter (fun v -> let key = Wrap v - if (per.InEqualityRelation v) then - if dict.ContainsKey key then false else (dict[key] <- null; true) - else true) + + if (per.InEqualityRelation v) then + if dict.ContainsKey key then + false + else + (dict[key] <- null + true) + else + true) //------------------------------------------------------------------------- // Library: Name maps @@ -1020,21 +1233,25 @@ type NameMap<'T> = Map type NameMultiMap<'T> = NameMap<'T list> -type MultiMap<'T, 'U when 'T : comparison> = Map<'T, 'U list> +type MultiMap<'T, 'U when 'T: comparison> = Map<'T, 'U list> -module NameMap = +module NameMap = let empty = Map.empty - let range m = List.rev (Map.foldBack (fun _ x sofar -> x :: sofar) m []) + let range m = + List.rev (Map.foldBack (fun _ x sofar -> x :: sofar) m []) let foldBack f (m: NameMap<'T>) z = Map.foldBack f m z - let forall f m = Map.foldBack (fun x y sofar -> sofar && f x y) m true + let forall f m = + Map.foldBack (fun x y sofar -> sofar && f x y) m true - let exists f m = Map.foldBack (fun x y sofar -> sofar || f x y) m false + let exists f m = + Map.foldBack (fun x y sofar -> sofar || f x y) m false - let ofKeyedList f l = List.foldBack (fun x acc -> Map.add (f x) x acc) l Map.empty + let ofKeyedList f l = + List.foldBack (fun x acc -> Map.add (f x) x acc) l Map.empty let ofList l : NameMap<'T> = Map.ofList l @@ -1042,120 +1259,169 @@ module NameMap = let toList (l: NameMap<'T>) = Map.toList l - let layer (m1 : NameMap<'T>) m2 = Map.foldBack Map.add m1 m2 + let layer (m1: NameMap<'T>) m2 = Map.foldBack Map.add m1 m2 - /// Not a very useful function - only called in one place - should be changed - let layerAdditive addf m1 m2 = - Map.foldBack (fun x y sofar -> Map.add x (addf (Map.tryFindMulti x sofar) y) sofar) m1 m2 + /// Not a very useful function - only called in one place - should be changed + let layerAdditive addf m1 m2 = + Map.foldBack (fun x y sofar -> Map.add x (addf (Map.tryFindMulti x sofar) y) sofar) m1 m2 /// Union entries by identical key, using the provided function to union sets of values - let union unionf (ms: NameMap<_> seq) = - seq { for m in ms do yield! m } - |> Seq.groupBy (fun (KeyValue(k, _v)) -> k) - |> Seq.map (fun (k, es) -> (k, unionf (Seq.map (fun (KeyValue(_k, v)) -> v) es))) - |> Map.ofSeq + let union unionf (ms: NameMap<_> seq) = + seq { + for m in ms do + yield! m + } + |> Seq.groupBy (fun (KeyValue (k, _v)) -> k) + |> Seq.map (fun (k, es) -> (k, unionf (Seq.map (fun (KeyValue (_k, v)) -> v) es))) + |> Map.ofSeq - /// For every entry in m2 find an entry in m1 and fold + /// For every entry in m2 find an entry in m1 and fold let subfold2 errf f m1 m2 acc = - Map.foldBack (fun n x2 acc -> try f n (Map.find n m1) x2 acc with :? KeyNotFoundException -> errf n x2) m2 acc - - let suball2 errf p m1 m2 = subfold2 errf (fun _ x1 x2 acc -> p x1 x2 && acc) m1 m2 true - - let mapFold f s (l: NameMap<'T>) = + Map.foldBack + (fun n x2 acc -> + try + f n (Map.find n m1) x2 acc + with + | :? KeyNotFoundException -> errf n x2) + m2 + acc + + let suball2 errf p m1 m2 = + subfold2 errf (fun _ x1 x2 acc -> p x1 x2 && acc) m1 m2 true + + let mapFold f s (l: NameMap<'T>) = Map.foldBack (fun x y (l2, sx) -> let y2, sy = f sx x y in Map.add x y2 l2, sy) l (Map.empty, s) - let foldBackRange f (l: NameMap<'T>) acc = Map.foldBack (fun _ y acc -> f y acc) l acc + let foldBackRange f (l: NameMap<'T>) acc = + Map.foldBack (fun _ y acc -> f y acc) l acc - let filterRange f (l: NameMap<'T>) = Map.foldBack (fun x y acc -> if f y then Map.add x y acc else acc) l Map.empty + let filterRange f (l: NameMap<'T>) = + Map.foldBack (fun x y acc -> if f y then Map.add x y acc else acc) l Map.empty - let mapFilter f (l: NameMap<'T>) = Map.foldBack (fun x y acc -> match f y with None -> acc | Some y' -> Map.add x y' acc) l Map.empty + let mapFilter f (l: NameMap<'T>) = + Map.foldBack + (fun x y acc -> + match f y with + | None -> acc + | Some y' -> Map.add x y' acc) + l + Map.empty - let map f (l : NameMap<'T>) = Map.map (fun _ x -> f x) l + let map f (l: NameMap<'T>) = Map.map (fun _ x -> f x) l - let iter f (l : NameMap<'T>) = Map.iter (fun _k v -> f v) l + let iter f (l: NameMap<'T>) = Map.iter (fun _k v -> f v) l - let partition f (l : NameMap<'T>) = Map.filter (fun _ x-> f x) l, Map.filter (fun _ x -> not (f x)) l + let partition f (l: NameMap<'T>) = + Map.filter (fun _ x -> f x) l, Map.filter (fun _ x -> not (f x)) l let mem v (m: NameMap<'T>) = Map.containsKey v m let find v (m: NameMap<'T>) = Map.find v m - let tryFind v (m: NameMap<'T>) = Map.tryFind v m + let tryFind v (m: NameMap<'T>) = Map.tryFind v m let add v x (m: NameMap<'T>) = Map.add v x m let isEmpty (m: NameMap<'T>) = (Map.isEmpty m) - let existsInRange p m = Map.foldBack (fun _ y acc -> acc || p y) m false + let existsInRange p m = + Map.foldBack (fun _ y acc -> acc || p y) m false - let tryFindInRange p m = - Map.foldBack (fun _ y acc -> - match acc with - | None -> if p y then Some y else None - | _ -> acc) m None + let tryFindInRange p m = + Map.foldBack + (fun _ y acc -> + match acc with + | None -> if p y then Some y else None + | _ -> acc) + m + None -module NameMultiMap = +module NameMultiMap = - let existsInRange f (m: NameMultiMap<'T>) = NameMap.exists (fun _ l -> List.exists f l) m + let existsInRange f (m: NameMultiMap<'T>) = + NameMap.exists (fun _ l -> List.exists f l) m - let find v (m: NameMultiMap<'T>) = match m.TryGetValue v with true, r -> r | _ -> [] + let find v (m: NameMultiMap<'T>) = + match m.TryGetValue v with + | true, r -> r + | _ -> [] let add v x (m: NameMultiMap<'T>) = NameMap.add v (x :: find v m) m - let range (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> x @ sofar) m [] + let range (m: NameMultiMap<'T>) = + Map.foldBack (fun _ x sofar -> x @ sofar) m [] - let rangeReversingEachBucket (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> List.rev x @ sofar) m [] - - let chooseRange f (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> List.choose f x @ sofar) m [] + let rangeReversingEachBucket (m: NameMultiMap<'T>) = + Map.foldBack (fun _ x sofar -> List.rev x @ sofar) m [] - let map f (m: NameMultiMap<'T>) = NameMap.map (List.map f) m + let chooseRange f (m: NameMultiMap<'T>) = + Map.foldBack (fun _ x sofar -> List.choose f x @ sofar) m [] - let empty : NameMultiMap<'T> = Map.empty + let map f (m: NameMultiMap<'T>) = NameMap.map (List.map f) m - let initBy f xs : NameMultiMap<'T> = xs |> Seq.groupBy f |> Seq.map (fun (k, v) -> (k, List.ofSeq v)) |> Map.ofSeq + let empty: NameMultiMap<'T> = Map.empty - let ofList (xs: (string * 'T) list) : NameMultiMap<'T> = xs |> Seq.groupBy fst |> Seq.map (fun (k, v) -> (k, List.ofSeq (Seq.map snd v))) |> Map.ofSeq + let initBy f xs : NameMultiMap<'T> = + xs |> Seq.groupBy f |> Seq.map (fun (k, v) -> (k, List.ofSeq v)) |> Map.ofSeq -module MultiMap = + let ofList (xs: (string * 'T) list) : NameMultiMap<'T> = + xs + |> Seq.groupBy fst + |> Seq.map (fun (k, v) -> (k, List.ofSeq (Seq.map snd v))) + |> Map.ofSeq + +module MultiMap = - let existsInRange f (m: MultiMap<_, _>) = Map.exists (fun _ l -> List.exists f l) m + let existsInRange f (m: MultiMap<_, _>) = + Map.exists (fun _ l -> List.exists f l) m - let find v (m: MultiMap<_, _>) = match m.TryGetValue v with true, r -> r | _ -> [] + let find v (m: MultiMap<_, _>) = + match m.TryGetValue v with + | true, r -> r + | _ -> [] let add v x (m: MultiMap<_, _>) = Map.add v (x :: find v m) m - let range (m: MultiMap<_, _>) = Map.foldBack (fun _ x sofar -> x @ sofar) m [] + let range (m: MultiMap<_, _>) = + Map.foldBack (fun _ x sofar -> x @ sofar) m [] - let empty : MultiMap<_, _> = Map.empty + let empty: MultiMap<_, _> = Map.empty - let initBy f xs : MultiMap<_, _> = xs |> Seq.groupBy f |> Seq.map (fun (k, v) -> (k, List.ofSeq v)) |> Map.ofSeq + let initBy f xs : MultiMap<_, _> = + xs |> Seq.groupBy f |> Seq.map (fun (k, v) -> (k, List.ofSeq v)) |> Map.ofSeq -type LayeredMap<'Key, 'Value when 'Key : comparison> = Map<'Key, 'Value> +type LayeredMap<'Key, 'Value when 'Key: comparison> = Map<'Key, 'Value> [] module MapAutoOpens = - type Map<'Key, 'Value when 'Key : comparison> with - - static member Empty : Map<'Key, 'Value> = Map.empty - -#if USE_SHIPPED_FSCORE - member x.Values = [ for KeyValue(_, v) in x -> v ] + type Map<'Key, 'Value when 'Key: comparison> with + + static member Empty: Map<'Key, 'Value> = Map.empty + +#if USE_SHIPPED_FSCORE + member x.Values = [ for KeyValue (_, v) in x -> v ] #endif - member x.AddMany (kvs: _[]) = (x, kvs) ||> Array.fold (fun x (KeyValue(k, v)) -> x.Add(k, v)) + member x.AddMany(kvs: _[]) = + (x, kvs) ||> Array.fold (fun x (KeyValue (k, v)) -> x.Add(k, v)) - member x.AddOrModify (key, f: 'Value option -> 'Value) = x.Add (key, f (x.TryFind key)) + member x.AddOrModify(key, f: 'Value option -> 'Value) = x.Add(key, f (x.TryFind key)) -/// Immutable map collection, with explicit flattening to a backing dictionary +/// Immutable map collection, with explicit flattening to a backing dictionary [] -type LayeredMultiMap<'Key, 'Value when 'Key : equality and 'Key : comparison>(contents : LayeredMap<'Key, 'Value list>) = +type LayeredMultiMap<'Key, 'Value when 'Key: equality and 'Key: comparison>(contents: LayeredMap<'Key, 'Value list>) = - member x.Add (k, v) = LayeredMultiMap(contents.Add(k, v :: x[k])) + member x.Add(k, v) = + LayeredMultiMap(contents.Add(k, v :: x[k])) - member _.Item with get k = match contents.TryGetValue k with true, l -> l | _ -> [] + member _.Item + with get k = + match contents.TryGetValue k with + | true, l -> l + | _ -> [] - member x.AddMany (kvs: _[]) = - (x, kvs) ||> Array.fold (fun x (KeyValue(k, v)) -> x.Add(k, v)) + member x.AddMany(kvs: _[]) = + (x, kvs) ||> Array.fold (fun x (KeyValue (k, v)) -> x.Add(k, v)) member _.TryFind k = contents.TryFind k @@ -1163,4 +1429,4 @@ type LayeredMultiMap<'Key, 'Value when 'Key : equality and 'Key : comparison>(co member _.Values = contents.Values |> List.concat - static member Empty : LayeredMultiMap<'Key, 'Value> = LayeredMultiMap LayeredMap.Empty + static member Empty: LayeredMultiMap<'Key, 'Value> = LayeredMultiMap LayeredMap.Empty diff --git a/src/Compiler/Utilities/range.fs b/src/Compiler/Utilities/range.fs index 34960113224..b8c98f92ada 100755 --- a/src/Compiler/Utilities/range.fs +++ b/src/Compiler/Utilities/range.fs @@ -25,19 +25,22 @@ module PosImpl = let posBitCount = lineBitCount + columnBitCount - let posColumnMask = mask64 0 columnBitCount + let posColumnMask = mask64 0 columnBitCount let lineColumnMask = mask64 columnBitCount lineBitCount [] [] -type Position(code:int64) = +type Position(code: int64) = - new (l, c) = + new(l, c) = let l = max 0 l let c = max 0 c - let p = (int64 c &&& posColumnMask) - ||| ((int64 l <<< columnBitCount) &&& lineColumnMask) + + let p = + (int64 c &&& posColumnMask) + ||| ((int64 l <<< columnBitCount) &&& lineColumnMask) + pos p member p.Line = int32 (uint64 code >>> columnBitCount) @@ -48,9 +51,12 @@ type Position(code:int64) = static member EncodingSize = posBitCount - static member Decode (code:int64) : pos = Position(code) + static member Decode(code: int64) : pos = Position(code) - override p.Equals(obj) = match obj with :? Position as p2 -> code = p2.Encoding | _ -> false + override p.Equals(obj) = + match obj with + | :? Position as p2 -> code = p2.Encoding + | _ -> false override p.GetHashCode() = hash code @@ -98,19 +104,19 @@ module RangeImpl = let debugPointKindBitCount = 4 [] - let fileIndexShift = 0 + let fileIndexShift = 0 [] let startColumnShift = 24 [] - let endColumnShift = 44 + let endColumnShift = 44 [] - let startLineShift = 0 + let startLineShift = 0 [] - let heightShift = 31 + let heightShift = 31 [] let isSyntheticShift = 58 @@ -119,47 +125,61 @@ module RangeImpl = let debugPointKindShift = 59 [] - let fileIndexMask = 0b0000000000000000000000000000000000000000111111111111111111111111L + let fileIndexMask = + 0b0000000000000000000000000000000000000000111111111111111111111111L [] - let startColumnMask = 0b0000000000000000000011111111111111111111000000000000000000000000L + let startColumnMask = + 0b0000000000000000000011111111111111111111000000000000000000000000L [] - let endColumnMask = 0b1111111111111111111100000000000000000000000000000000000000000000L + let endColumnMask = + 0b1111111111111111111100000000000000000000000000000000000000000000L [] - let startLineMask = 0b0000000000000000000000000000000001111111111111111111111111111111L + let startLineMask = + 0b0000000000000000000000000000000001111111111111111111111111111111L [] - let heightMask = 0b0000001111111111111111111111111110000000000000000000000000000000L + let heightMask = 0b0000001111111111111111111111111110000000000000000000000000000000L [] - let isSyntheticMask = 0b0000010000000000000000000000000000000000000000000000000000000000L + let isSyntheticMask = + 0b0000010000000000000000000000000000000000000000000000000000000000L [] - let debugPointKindMask= 0b0111100000000000000000000000000000000000000000000000000000000000L + let debugPointKindMask = + 0b0111100000000000000000000000000000000000000000000000000000000000L - #if DEBUG +#if DEBUG let _ = assert (posBitCount <= 64) let _ = assert (fileIndexBitCount + startColumnBitCount + endColumnBitCount <= 64) - let _ = assert (startLineBitCount + heightBitCount + isSyntheticBitCount + debugPointKindBitCount <= 64) - let _ = assert (startColumnShift = fileIndexShift + fileIndexBitCount) - let _ = assert (endColumnShift = startColumnShift + startColumnBitCount) + let _ = + assert + (startLineBitCount + + heightBitCount + + isSyntheticBitCount + + debugPointKindBitCount + <= 64) + + let _ = assert (startColumnShift = fileIndexShift + fileIndexBitCount) + let _ = assert (endColumnShift = startColumnShift + startColumnBitCount) - let _ = assert (heightShift = startLineShift + startLineBitCount) - let _ = assert (isSyntheticShift = heightShift + heightBitCount) + let _ = assert (heightShift = startLineShift + startLineBitCount) + let _ = assert (isSyntheticShift = heightShift + heightBitCount) let _ = assert (debugPointKindShift = isSyntheticShift + isSyntheticBitCount) - let _ = assert (fileIndexMask = mask64 fileIndexShift fileIndexBitCount) - let _ = assert (startLineMask = mask64 startLineShift startLineBitCount) + let _ = assert (fileIndexMask = mask64 fileIndexShift fileIndexBitCount) + let _ = assert (startLineMask = mask64 startLineShift startLineBitCount) let _ = assert (startColumnMask = mask64 startColumnShift startColumnBitCount) - let _ = assert (heightMask = mask64 heightShift heightBitCount) - let _ = assert (endColumnMask = mask64 endColumnShift endColumnBitCount) + let _ = assert (heightMask = mask64 heightShift heightBitCount) + let _ = assert (endColumnMask = mask64 endColumnShift endColumnBitCount) let _ = assert (isSyntheticMask = mask64 isSyntheticShift isSyntheticBitCount) - let _ = assert (debugPointKindMask = mask64 debugPointKindShift debugPointKindBitCount) - #endif + let _ = + assert (debugPointKindMask = mask64 debugPointKindShift debugPointKindBitCount) +#endif /// A unique-index table for file names. type FileIndexTable() = @@ -177,39 +197,45 @@ type FileIndexTable() = | true, idx -> idx | _ -> - // Try again looking for a normalized entry. - let normalizedFilePath = if normalize then FileSystem.NormalizePathShim filePath else filePath - match fileToIndexTable.TryGetValue normalizedFilePath with - | true, idx -> - // Record the non-normalized entry if necessary - if filePath <> normalizedFilePath then - lock fileToIndexTable (fun () -> - fileToIndexTable[filePath] <- idx) - - // Return the index - idx - - | _ -> - lock fileToIndexTable (fun () -> - // Get the new index - let idx = indexToFileTable.Count - - // Record the normalized entry - indexToFileTable.Add normalizedFilePath - fileToIndexTable[normalizedFilePath] <- idx + // Try again looking for a normalized entry. + let normalizedFilePath = + if normalize then + FileSystem.NormalizePathShim filePath + else + filePath + match fileToIndexTable.TryGetValue normalizedFilePath with + | true, idx -> // Record the non-normalized entry if necessary if filePath <> normalizedFilePath then - fileToIndexTable[filePath] <- idx + lock fileToIndexTable (fun () -> fileToIndexTable[filePath] <- idx) // Return the index - idx) + idx + + | _ -> + lock fileToIndexTable (fun () -> + // Get the new index + let idx = indexToFileTable.Count + + // Record the normalized entry + indexToFileTable.Add normalizedFilePath + fileToIndexTable[normalizedFilePath] <- idx + + // Record the non-normalized entry if necessary + if filePath <> normalizedFilePath then + fileToIndexTable[filePath] <- idx + + // Return the index + idx) member t.IndexToFile n = if n < 0 then failwithf "fileOfFileIndex: negative argument: n = %d\n" n + if n >= indexToFileTable.Count then failwithf "fileOfFileIndex: invalid argument: n = %d\n" n + indexToFileTable[n] [] @@ -221,7 +247,8 @@ module FileIndex = let fileIndexTable = FileIndexTable() // If we exceed the maximum number of files we'll start to report incorrect file names - let fileIndexOfFileAux normalize f = fileIndexTable.FileToIndex normalize f % maxFileIndex + let fileIndexOfFileAux normalize f = + fileIndexTable.FileToIndex normalize f % maxFileIndex let fileIndexOfFile filePath = fileIndexOfFileAux false filePath @@ -233,31 +260,35 @@ module FileIndex = [] [ {DebugCode}")>] -type Range(code1:int64, code2: int64) = - static member Zero = range(0L, 0L) - new (fIdx, bl, bc, el, ec) = - let code1 = ((int64 fIdx) &&& fileIndexMask) - ||| ((int64 bc <<< startColumnShift) &&& startColumnMask) - ||| ((int64 ec <<< endColumnShift) &&& endColumnMask) +type Range(code1: int64, code2: int64) = + static member Zero = range (0L, 0L) + + new(fIdx, bl, bc, el, ec) = + let code1 = + ((int64 fIdx) &&& fileIndexMask) + ||| ((int64 bc <<< startColumnShift) &&& startColumnMask) + ||| ((int64 ec <<< endColumnShift) &&& endColumnMask) + let code2 = - ((int64 bl <<< startLineShift) &&& startLineMask) - ||| ((int64 (el-bl) <<< heightShift) &&& heightMask) - range(code1, code2) + ((int64 bl <<< startLineShift) &&& startLineMask) + ||| ((int64 (el - bl) <<< heightShift) &&& heightMask) + + range (code1, code2) - new (fIdx, b:pos, e:pos) = range(fIdx, b.Line, b.Column, e.Line, e.Column) + new(fIdx, b: pos, e: pos) = range (fIdx, b.Line, b.Column, e.Line, e.Column) - member _.StartLine = int32((code2 &&& startLineMask) >>> startLineShift) + member _.StartLine = int32 ((code2 &&& startLineMask) >>> startLineShift) - member _.StartColumn = int32((code1 &&& startColumnMask) >>> startColumnShift) + member _.StartColumn = int32 ((code1 &&& startColumnMask) >>> startColumnShift) - member m.EndLine = int32((code2 &&& heightMask) >>> heightShift) + m.StartLine + member m.EndLine = int32 ((code2 &&& heightMask) >>> heightShift) + m.StartLine - member _.EndColumn = int32((code1 &&& endColumnMask) >>> endColumnShift) + member _.EndColumn = int32 ((code1 &&& endColumnMask) >>> endColumnShift) - member _.IsSynthetic = int32((code2 &&& isSyntheticMask) >>> isSyntheticShift) <> 0 + member _.IsSynthetic = int32 ((code2 &&& isSyntheticMask) >>> isSyntheticShift) <> 0 - member _.NotedSourceConstruct = - match int32((code2 &&& debugPointKindMask) >>> debugPointKindShift) with + member _.NotedSourceConstruct = + match int32 ((code2 &&& debugPointKindMask) >>> debugPointKindShift) with | 1 -> NotedSourceConstruct.While | 2 -> NotedSourceConstruct.For | 3 -> NotedSourceConstruct.Try @@ -273,7 +304,7 @@ type Range(code1:int64, code2: int64) = member m.End = pos (m.EndLine, m.EndColumn) - member _.FileIndex = int32(code1 &&& fileIndexMask) + member _.FileIndex = int32 (code1 &&& fileIndexMask) member m.StartRange = range (m.FileIndex, m.Start, m.Start) @@ -283,15 +314,16 @@ type Range(code1:int64, code2: int64) = member m.ShortFileName = Path.GetFileName(fileOfFileIndex m.FileIndex) - member _.MakeSynthetic() = range(code1, code2 ||| isSyntheticMask) + member _.MakeSynthetic() = + range (code1, code2 ||| isSyntheticMask) member m.IsAdjacentTo(otherRange: Range) = m.FileIndex = otherRange.FileIndex && m.End.Encoding = otherRange.Start.Encoding - member _.NoteSourceConstruct(kind) = - let code = - match kind with - | NotedSourceConstruct.None -> 0 + member _.NoteSourceConstruct(kind) = + let code = + match kind with + | NotedSourceConstruct.None -> 0 | NotedSourceConstruct.While -> 1 | NotedSourceConstruct.For -> 2 | NotedSourceConstruct.Try -> 3 @@ -301,7 +333,8 @@ type Range(code1:int64, code2: int64) = | NotedSourceConstruct.With -> 7 | NotedSourceConstruct.Combine -> 8 | NotedSourceConstruct.DelayOrQuoteOrRun -> 9 - range(code1, (code2 &&& ~~~debugPointKindMask) ||| (int64 code <<< debugPointKindShift)) + + range (code1, (code2 &&& ~~~debugPointKindMask) ||| (int64 code <<< debugPointKindShift)) member _.Code1 = code1 @@ -309,23 +342,32 @@ type Range(code1:int64, code2: int64) = member m.DebugCode = let name = m.FileName - if name = unknownFileName || name = startupFileName || name = commandLineArgsFileName then name else - try - let endCol = m.EndColumn - 1 - let startCol = m.StartColumn - 1 - if FileSystem.IsInvalidPathShim m.FileName then "path invalid: " + m.FileName - elif not (FileSystem.FileExistsShim m.FileName) then "non existing file: " + m.FileName - else - FileSystem.OpenFileForReadShim(m.FileName).ReadLines() - |> Seq.skip (m.StartLine - 1) - |> Seq.take (m.EndLine - m.StartLine + 1) - |> String.concat "\n" - |> fun s -> s.Substring(startCol + 1, s.LastIndexOf("\n", StringComparison.Ordinal) + 1 - startCol + endCol) - with e -> - e.ToString() + if name = unknownFileName + || name = startupFileName + || name = commandLineArgsFileName then + name + else - member m.ToShortString() = sprintf "(%d,%d--%d,%d)" m.StartLine m.StartColumn m.EndLine m.EndColumn + try + let endCol = m.EndColumn - 1 + let startCol = m.StartColumn - 1 + + if FileSystem.IsInvalidPathShim m.FileName then + "path invalid: " + m.FileName + elif not (FileSystem.FileExistsShim m.FileName) then + "non existing file: " + m.FileName + else + FileSystem.OpenFileForReadShim(m.FileName).ReadLines() + |> Seq.skip (m.StartLine - 1) + |> Seq.take (m.EndLine - m.StartLine + 1) + |> String.concat "\n" + |> fun s -> s.Substring(startCol + 1, s.LastIndexOf("\n", StringComparison.Ordinal) + 1 - startCol + endCol) + with + | e -> e.ToString() + + member m.ToShortString() = + sprintf "(%d,%d--%d,%d)" m.StartLine m.StartColumn m.EndLine m.EndColumn member _.Equals(m2: range) = let code2 = code2 &&& ~~~(debugPointKindMask ||| isSyntheticMask) @@ -341,13 +383,15 @@ type Range(code1:int64, code2: int64) = let code2 = code2 &&& ~~~(debugPointKindMask ||| isSyntheticMask) hash code1 + hash code2 - override r.ToString() = sprintf "%s (%d,%d--%d,%d)" r.FileName r.StartLine r.StartColumn r.EndLine r.EndColumn + override r.ToString() = + sprintf "%s (%d,%d--%d,%d)" r.FileName r.StartLine r.StartColumn r.EndLine r.EndColumn and range = Range #if CHECK_LINE0_TYPES // turn on to check that we correctly transform zero-based line counts to one-based line counts // Visual Studio uses line counts starting at 0, F# uses them starting at 1 -[] type ZeroBasedLineAnnotation +[] +type ZeroBasedLineAnnotation type Line0 = int #else @@ -360,16 +404,17 @@ type Range01 = Position01 * Position01 module Line = - let fromZ (line:Line0) = int line+1 + let fromZ (line: Line0) = int line + 1 - let toZ (line:int) : Line0 = LanguagePrimitives.Int32WithMeasure(line - 1) + let toZ (line: int) : Line0 = + LanguagePrimitives.Int32WithMeasure(line - 1) [] module Position = - let mkPos line column = Position (line, column) + let mkPos line column = Position(line, column) - let outputPos (os:TextWriter) (m:pos) = fprintf os "(%d,%d)" m.Line m.Column + let outputPos (os: TextWriter) (m: pos) = fprintf os "(%d,%d)" m.Line m.Column let posGt (p1: pos) (p2: pos) = let p1Line = p1.Line @@ -382,102 +427,120 @@ module Position = let posLt p1 p2 = posGt p2 p1 - let fromZ (line:Line0) column = mkPos (Line.fromZ line) column + let fromZ (line: Line0) column = mkPos (Line.fromZ line) column - let toZ (p:pos) = (Line.toZ p.Line, p.Column) + let toZ (p: pos) = (Line.toZ p.Line, p.Column) (* For Diagnostics *) - let stringOfPos (pos:pos) = sprintf "(%d,%d)" pos.Line pos.Column + let stringOfPos (pos: pos) = sprintf "(%d,%d)" pos.Line pos.Column let pos0 = mkPos 1 0 module Range = - let mkRange filePath startPos endPos = range (fileIndexOfFileAux true filePath, startPos, endPos) + let mkRange filePath startPos endPos = + range (fileIndexOfFileAux true filePath, startPos, endPos) - let equals (r1: range) (r2: range) = - r1.Equals(r2) + let equals (r1: range) (r2: range) = r1.Equals(r2) let mkFileIndexRange fileIndex startPos endPos = range (fileIndex, startPos, endPos) - let posOrder = Order.orderOn (fun (p:pos) -> p.Line, p.Column) (Pair.order (Int32.order, Int32.order)) + let posOrder = + Order.orderOn (fun (p: pos) -> p.Line, p.Column) (Pair.order (Int32.order, Int32.order)) - let rangeOrder = Order.orderOn (fun (r:range) -> r.FileName, (r.Start, r.End)) (Pair.order (String.order, Pair.order(posOrder, posOrder))) + let rangeOrder = + Order.orderOn (fun (r: range) -> r.FileName, (r.Start, r.End)) (Pair.order (String.order, Pair.order (posOrder, posOrder))) - let outputRange (os:TextWriter) (m:range) = fprintf os "%s%a-%a" m.FileName outputPos m.Start outputPos m.End + let outputRange (os: TextWriter) (m: range) = + fprintf os "%s%a-%a" m.FileName outputPos m.Start outputPos m.End /// This is deliberately written in an allocation-free way, i.e. m1.Start, m1.End etc. are not called - let unionRanges (m1:range) (m2:range) = - if m1.FileIndex <> m2.FileIndex then m2 else - - // If all identical then return m1. This preserves NotedSourceConstruct when no merging takes place - if m1.Code1 = m2.Code1 && m1.Code2 = m2.Code2 then m1 else - - let start = - if (m1.StartLine > m2.StartLine || (m1.StartLine = m2.StartLine && m1.StartColumn > m2.StartColumn)) then m2 - else m1 + let unionRanges (m1: range) (m2: range) = + if m1.FileIndex <> m2.FileIndex then + m2 + else - let finish = - if (m1.EndLine > m2.EndLine || (m1.EndLine = m2.EndLine && m1.EndColumn > m2.EndColumn)) then m1 - else m2 + // If all identical then return m1. This preserves NotedSourceConstruct when no merging takes place + if m1.Code1 = m2.Code1 && m1.Code2 = m2.Code2 then + m1 + else - let m = range (m1.FileIndex, start.StartLine, start.StartColumn, finish.EndLine, finish.EndColumn) - if m1.IsSynthetic || m2.IsSynthetic then m.MakeSynthetic() else m + let start = + if (m1.StartLine > m2.StartLine + || (m1.StartLine = m2.StartLine && m1.StartColumn > m2.StartColumn)) then + m2 + else + m1 + + let finish = + if (m1.EndLine > m2.EndLine + || (m1.EndLine = m2.EndLine && m1.EndColumn > m2.EndColumn)) then + m1 + else + m2 + + let m = + range (m1.FileIndex, start.StartLine, start.StartColumn, finish.EndLine, finish.EndColumn) + + if m1.IsSynthetic || m2.IsSynthetic then + m.MakeSynthetic() + else + m - let rangeContainsRange (m1:range) (m2:range) = - m1.FileIndex = m2.FileIndex && - posGeq m2.Start m1.Start && - posGeq m1.End m2.End + let rangeContainsRange (m1: range) (m2: range) = + m1.FileIndex = m2.FileIndex && posGeq m2.Start m1.Start && posGeq m1.End m2.End - let rangeContainsPos (m1:range) p = - posGeq p m1.Start && - posGeq m1.End p + let rangeContainsPos (m1: range) p = posGeq p m1.Start && posGeq m1.End p - let rangeBeforePos (m1:range) p = - posGeq p m1.End + let rangeBeforePos (m1: range) p = posGeq p m1.End - let rangeN fileName line = mkRange fileName (mkPos line 0) (mkPos line 0) + let rangeN fileName line = + mkRange fileName (mkPos line 0) (mkPos line 0) - let range0 = rangeN unknownFileName 1 + let range0 = rangeN unknownFileName 1 let rangeStartup = rangeN startupFileName 1 let rangeCmdArgs = rangeN commandLineArgsFileName 0 - let trimRangeToLine (r:range) = + let trimRangeToLine (r: range) = let startL, startC = r.StartLine, r.StartColumn - let endL, _endC = r.EndLine, r.EndColumn + let endL, _endC = r.EndLine, r.EndColumn + if endL <= startL then r else // Trim to the start of the next line (we do not know the end of the current line) - let endL, endC = startL+1, 0 + let endL, endC = startL + 1, 0 range (r.FileIndex, startL, startC, endL, endC) - let stringOfRange (r:range) = + let stringOfRange (r: range) = sprintf "%s%s-%s" r.FileName (stringOfPos r.Start) (stringOfPos r.End) - let toZ (m:range) = toZ m.Start, toZ m.End + let toZ (m: range) = toZ m.Start, toZ m.End - let toFileZ (m:range) = m.FileName, toZ m + let toFileZ (m: range) = m.FileName, toZ m let comparer = { new IEqualityComparer with member _.Equals(x1, x2) = equals x1 x2 - member _.GetHashCode o = o.GetHashCode() } + member _.GetHashCode o = o.GetHashCode() + } let mkFirstLineOfFile (file: string) = try let lines = FileSystem.OpenFileForReadShim(file).ReadLines() |> Seq.indexed - let nonWhiteLine = lines |> Seq.tryFind (fun (_,s) -> not (String.IsNullOrWhiteSpace s)) + + let nonWhiteLine = + lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrWhiteSpace s)) match nonWhiteLine with - | Some (i,s) -> mkRange file (mkPos (i+1) 0) (mkPos (i+1) s.Length) + | Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length) | None -> - let nonEmptyLine = lines |> Seq.tryFind (fun (_,s) -> not (String.IsNullOrEmpty s)) + let nonEmptyLine = lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrEmpty s)) - match nonEmptyLine with - | Some (i,s) -> mkRange file (mkPos (i+1) 0) (mkPos (i+1) s.Length) - | None -> mkRange file (mkPos 1 0) (mkPos 1 80) - with _ -> - mkRange file (mkPos 1 0) (mkPos 1 80) + match nonEmptyLine with + | Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length) + | None -> mkRange file (mkPos 1 0) (mkPos 1 80) + with + | _ -> mkRange file (mkPos 1 0) (mkPos 1 80) diff --git a/src/Compiler/Utilities/rational.fs b/src/Compiler/Utilities/rational.fs index 51d7e2f6eb0..563c49cb60c 100644 --- a/src/Compiler/Utilities/rational.fs +++ b/src/Compiler/Utilities/rational.fs @@ -5,62 +5,77 @@ module internal Internal.Utilities.Rational open System.Numerics -type Rational = { - numerator: BigInteger - denominator: BigInteger -} +type Rational = + { + numerator: BigInteger + denominator: BigInteger + } let rec gcd a (b: BigInteger) = - if b = BigInteger.Zero then a else - gcd b (a % b) -let lcm a b = - (a * b) / (gcd a b) + if b = BigInteger.Zero then + a + else + gcd b (a % b) + +let lcm a b = (a * b) / (gcd a b) let mkRational p q = - let p, q = - if q = BigInteger.Zero then raise(System.DivideByZeroException()) - let g = gcd q p in - p/g, q/g - - let p, q = - if q > BigInteger.Zero then p, q else -p, -q - - in - { numerator = p - denominator = q - } + let p, q = + if q = BigInteger.Zero then + raise (System.DivideByZeroException()) + + let g = gcd q p in + p / g, q / g + + let p, q = + if q > BigInteger.Zero then + p, q + else + -p, -q + + in + + { numerator = p; denominator = q } + +let intToRational (p: int) = + mkRational (BigInteger(p)) BigInteger.One -let intToRational (p:int) = mkRational (BigInteger(p)) BigInteger.One let ZeroRational = mkRational BigInteger.Zero BigInteger.One let OneRational = mkRational BigInteger.One BigInteger.One let AddRational m n = - let d = gcd m.denominator n.denominator - let m' = m.denominator / d - let n' = n.denominator / d - mkRational (m.numerator * n' + n.numerator * m') (m.denominator * n') + let d = gcd m.denominator n.denominator + let m' = m.denominator / d + let n' = n.denominator / d + mkRational (m.numerator * n' + n.numerator * m') (m.denominator * n') -let NegRational m = - mkRational (-m.numerator) m.denominator +let NegRational m = mkRational (-m.numerator) m.denominator let MulRational m n = - mkRational (m.numerator * n.numerator) (m.denominator * n.denominator) + mkRational (m.numerator * n.numerator) (m.denominator * n.denominator) let DivRational m n = - mkRational (m.numerator * n.denominator) (m.denominator * n.numerator) + mkRational (m.numerator * n.denominator) (m.denominator * n.numerator) -let AbsRational m = - mkRational (abs m.numerator) m.denominator +let AbsRational m = + mkRational (abs m.numerator) m.denominator let RationalToString m = - if m.denominator = BigInteger.One then m.numerator.ToString() else sprintf "(%A/%A)" m.numerator m.denominator + if m.denominator = BigInteger.One then + m.numerator.ToString() + else + sprintf "(%A/%A)" m.numerator m.denominator -let GcdRational m n = mkRational (gcd m.numerator n.numerator) (lcm m.denominator n.denominator) +let GcdRational m n = + mkRational (gcd m.numerator n.numerator) (lcm m.denominator n.denominator) let GetNumerator p = int p.numerator let GetDenominator p = int p.denominator -let SignRational p = - if p.numerator < BigInteger.Zero then -1 else - if p.numerator > BigInteger.Zero then 1 else 0 - +let SignRational p = + if p.numerator < BigInteger.Zero then + -1 + else if p.numerator > BigInteger.Zero then + 1 + else + 0 diff --git a/src/Compiler/Utilities/sformat.fs b/src/Compiler/Utilities/sformat.fs index 1ee3032b150..d0e0d6fe5ef 100644 --- a/src/Compiler/Utilities/sformat.fs +++ b/src/Compiler/Utilities/sformat.fs @@ -100,7 +100,7 @@ type Layout = | Node (left, _, _) -> left.JuxtapositionLeft | Attr (_, _, subLayout) -> subLayout.JuxtapositionLeft - static member JuxtapositionMiddle (left: Layout, right: Layout) = + static member JuxtapositionMiddle(left: Layout, right: Layout) = left.JuxtapositionRight || right.JuxtapositionLeft member layout.JuxtapositionRight = @@ -111,7 +111,7 @@ type Layout = | Attr (_, _, subLayout) -> subLayout.JuxtapositionRight [] -type IEnvironment = +type IEnvironment = abstract GetLayout: obj -> Layout abstract MaxColumns: int abstract MaxRows: int @@ -144,13 +144,14 @@ module TaggedText = let questionMark = tagPunctuation "?" let leftBracket = tagPunctuation "[" let rightBracket = tagPunctuation "]" - let leftBrace= tagPunctuation "{" + let leftBrace = tagPunctuation "{" let rightBrace = tagPunctuation "}" let space = tagSpace " " let equals = tagOperator "=" #if COMPILER let tagAlias t = mkTag TextTag.Alias t + let keywordFunctions = [ "raise" @@ -182,6 +183,7 @@ module TaggedText = "unativeint" ] |> Set.ofList + let tagDelegate t = mkTag TextTag.Delegate t let tagEnum t = mkTag TextTag.Enum t let tagEvent t = mkTag TextTag.Event t @@ -189,7 +191,13 @@ module TaggedText = let tagLineBreak t = mkTag TextTag.LineBreak t let tagRecord t = mkTag TextTag.Record t let tagModule t = mkTag TextTag.Module t - let tagModuleBinding name = if keywordFunctions.Contains name then mkTag TextTag.Keyword name else mkTag TextTag.ModuleBinding name + + let tagModuleBinding name = + if keywordFunctions.Contains name then + mkTag TextTag.Keyword name + else + mkTag TextTag.ModuleBinding name + let tagFunction t = mkTag TextTag.Function t let tagNamespace t = mkTag TextTag.Namespace t let tagParameter t = mkTag TextTag.Parameter t @@ -236,7 +244,7 @@ module TaggedText = let keywordAbstract = tagKeyword "abstract" let keywordOverride = tagKeyword "override" let keywordEnum = tagKeyword "enum" - let leftBracketBar = tagPunctuation "[|" + let leftBracketBar = tagPunctuation "[|" let rightBracketBar = tagPunctuation "|]" let keywordTypeof = tagKeyword "typeof" let keywordTypedefof = tagKeyword "typedefof" @@ -244,45 +252,45 @@ module TaggedText = let rightBracketAngle = tagPunctuation ">]" let star = tagOperator "*" let keywordNew = tagKeyword "new" -#endif +#endif [] -module Layout = +module Layout = // constructors - let objL (value:obj) = - match value with - | :? string as s -> Leaf (false, mkTag TextTag.Text s, false) - | o -> ObjLeaf (false, o, false) + let objL (value: obj) = + match value with + | :? string as s -> Leaf(false, mkTag TextTag.Text s, false) + | o -> ObjLeaf(false, o, false) - let wordL text = Leaf (false, text, false) + let wordL text = Leaf(false, text, false) - let sepL text = Leaf (true , text, true) + let sepL text = Leaf(true, text, true) - let rightL text = Leaf (true , text, false) + let rightL text = Leaf(true, text, false) - let leftL text = Leaf (false, text, true) + let leftL text = Leaf(false, text, true) - let emptyL = Leaf (true, mkTag TextTag.Text "", true) + let emptyL = Leaf(true, mkTag TextTag.Text "", true) - let isEmptyL layout = - match layout with - | Leaf(true, s, true) -> s.Text = "" + let isEmptyL layout = + match layout with + | Leaf (true, s, true) -> s.Text = "" | _ -> false #if COMPILER - let rec endsWithL (text: string) layout = - match layout with - | Leaf(_, s, _) -> s.Text.EndsWith(text) - | Node(_, r, _) -> endsWithL text r - | Attr(_, _, l) -> endsWithL text l + let rec endsWithL (text: string) layout = + match layout with + | Leaf (_, s, _) -> s.Text.EndsWith(text) + | Node (_, r, _) -> endsWithL text r + | Attr (_, _, l) -> endsWithL text l | ObjLeaf _ -> false #endif let mkNode l r joint = - if isEmptyL l then r else - if isEmptyL r then l else - Node(l, r, joint) + if isEmptyL l then r + else if isEmptyL r then l + else Node(l, r, joint) let aboveL layout1 layout2 = mkNode layout1 layout2 (Broken 0) @@ -290,129 +298,144 @@ module Layout = let apply2 f l r = if isEmptyL l then r - elif isEmptyL r then l + elif isEmptyL r then l else f l r - let (^^) layout1 layout2 = mkNode layout1 layout2 Unbreakable + let (^^) layout1 layout2 = mkNode layout1 layout2 Unbreakable - let (++) layout1 layout2 = mkNode layout1 layout2 (Breakable 0) + let (++) layout1 layout2 = mkNode layout1 layout2 (Breakable 0) - let (--) layout1 layout2 = mkNode layout1 layout2 (Breakable 1) + let (--) layout1 layout2 = mkNode layout1 layout2 (Breakable 1) let (---) layout1 layout2 = mkNode layout1 layout2 (Breakable 2) - let (----) layout1 layout2 = mkNode layout1 layout2 (Breakable 3) + let (----) layout1 layout2 = mkNode layout1 layout2 (Breakable 3) + + let (-----) layout1 layout2 = mkNode layout1 layout2 (Breakable 4) - let (-----) layout1 layout2 = mkNode layout1 layout2 (Breakable 4) + let (@@) layout1 layout2 = + apply2 (fun l r -> mkNode l r (Broken 0)) layout1 layout2 - let (@@) layout1 layout2 = apply2 (fun l r -> mkNode l r (Broken 0)) layout1 layout2 + let (@@-) layout1 layout2 = + apply2 (fun l r -> mkNode l r (Broken 1)) layout1 layout2 - let (@@-) layout1 layout2 = apply2 (fun l r -> mkNode l r (Broken 1)) layout1 layout2 + let (@@--) layout1 layout2 = + apply2 (fun l r -> mkNode l r (Broken 2)) layout1 layout2 - let (@@--) layout1 layout2 = apply2 (fun l r -> mkNode l r (Broken 2)) layout1 layout2 - - let (@@---) layout1 layout2 = apply2 (fun l r -> mkNode l r (Broken 3)) layout1 layout2 - - let (@@----) layout1 layout2 = apply2 (fun l r -> mkNode l r (Broken 4)) layout1 layout2 + let (@@---) layout1 layout2 = + apply2 (fun l r -> mkNode l r (Broken 3)) layout1 layout2 + + let (@@----) layout1 layout2 = + apply2 (fun l r -> mkNode l r (Broken 4)) layout1 layout2 let tagListL tagger els = - match els with + match els with | [] -> emptyL - | [x] -> x + | [ x ] -> x | x :: xs -> let rec process' prefixL yl = match yl with | [] -> prefixL | y :: ys -> process' (tagger prefixL ++ y) ys + process' x xs - - let commaListL layouts = tagListL (fun prefixL -> prefixL ^^ rightL comma) layouts - let semiListL layouts = tagListL (fun prefixL -> prefixL ^^ rightL semicolon) layouts + let commaListL layouts = + tagListL (fun prefixL -> prefixL ^^ rightL comma) layouts + + let semiListL layouts = + tagListL (fun prefixL -> prefixL ^^ rightL semicolon) layouts let spaceListL layouts = tagListL id layouts - let sepListL layout1 layouts = tagListL (fun prefixL -> prefixL ^^ layout1) layouts + let sepListL layout1 layouts = + tagListL (fun prefixL -> prefixL ^^ layout1) layouts - let bracketL layout = leftL leftParen ^^ layout ^^ rightL rightParen + let bracketL layout = + leftL leftParen ^^ layout ^^ rightL rightParen - let tupleL layouts = bracketL (sepListL (sepL comma) layouts) + let tupleL layouts = + bracketL (sepListL (sepL comma) layouts) - let aboveListL layouts = + let aboveListL layouts = match layouts with | [] -> emptyL - | [x] -> x + | [ x ] -> x | x :: ys -> List.fold (fun pre y -> pre @@ y) x ys - let optionL selector value = - match value with + let optionL selector value = + match value with | None -> wordL (tagUnionCase "None") | Some x -> wordL (tagUnionCase "Some") -- (selector x) let listL selector value = - leftL leftBracket ^^ sepListL (sepL semicolon) (List.map selector value) ^^ rightL rightBracket + leftL leftBracket + ^^ sepListL (sepL semicolon) (List.map selector value) ^^ rightL rightBracket let squareBracketL layout = - leftL leftBracket ^^ layout ^^ rightL rightBracket + leftL leftBracket ^^ layout ^^ rightL rightBracket let braceL layout = leftL leftBrace ^^ layout ^^ rightL rightBrace - let boundedUnfoldL - (itemL: 'a -> Layout) - (project: 'z -> ('a * 'z) option) - (stopShort: 'z -> bool) - (z: 'z) - maxLength = + let boundedUnfoldL (itemL: 'a -> Layout) (project: 'z -> ('a * 'z) option) (stopShort: 'z -> bool) (z: 'z) maxLength = let rec consume n z = - if stopShort z then [wordL (tagPunctuation "...")] else - match project z with - | None -> [] // exhausted input - | Some (x, z) -> if n<=0 then [wordL (tagPunctuation "...")] // hit print_length limit - else itemL x :: consume (n-1) z // cons recursive... - consume maxLength z + if stopShort z then + [ wordL (tagPunctuation "...") ] + else + match project z with + | None -> [] // exhausted input + | Some (x, z) -> + if n <= 0 then + [ wordL (tagPunctuation "...") ] // hit print_length limit + else + itemL x :: consume (n - 1) z // cons recursive... + + consume maxLength z let unfoldL selector folder state count = boundedUnfoldL selector folder (fun _ -> false) state count - + /// These are a typical set of options used to control structured formatting. [] type FormatOptions = - { FloatingPointFormat: string - AttributeProcessor: string -> (string * string) list -> bool -> unit + { + FloatingPointFormat: string + AttributeProcessor: string -> (string * string) list -> bool -> unit #if COMPILER // This is the PrintIntercepts extensibility point currently revealed by fsi.exe's AddPrinter - PrintIntercepts: (IEnvironment -> obj -> Layout option) list - StringLimit: int + PrintIntercepts: (IEnvironment -> obj -> Layout option) list + StringLimit: int #endif - FormatProvider: IFormatProvider - BindingFlags: BindingFlags - PrintWidth: int - PrintDepth: int - PrintLength: int - PrintSize: int - ShowProperties: bool - ShowIEnumerable: bool + FormatProvider: IFormatProvider + BindingFlags: BindingFlags + PrintWidth: int + PrintDepth: int + PrintLength: int + PrintSize: int + ShowProperties: bool + ShowIEnumerable: bool } static member Default = - { FormatProvider = (CultureInfo.InvariantCulture :> IFormatProvider) + { + FormatProvider = (CultureInfo.InvariantCulture :> IFormatProvider) #if COMPILER // This is the PrintIntercepts extensibility point currently revealed by fsi.exe's AddPrinter - PrintIntercepts = [] - StringLimit = Int32.MaxValue + PrintIntercepts = [] + StringLimit = Int32.MaxValue #endif - AttributeProcessor= (fun _ _ _ -> ()) - BindingFlags = BindingFlags.Public - FloatingPointFormat = "g10" - PrintWidth = 80 - PrintDepth = 100 - PrintLength = 100 - PrintSize = 10000 - ShowProperties = false - ShowIEnumerable = true + AttributeProcessor = (fun _ _ _ -> ()) + BindingFlags = BindingFlags.Public + FloatingPointFormat = "g10" + PrintWidth = 80 + PrintDepth = 100 + PrintLength = 100 + PrintSize = 10000 + ShowProperties = false + ShowIEnumerable = true } -module ReflectUtils = +module ReflectUtils = [] type TypeInfo = @@ -423,14 +446,16 @@ module ReflectUtils = | UnitType | ObjectType of Type - let isNamedType (ty:Type) = not (ty.IsArray || ty.IsByRef || ty.IsPointer) + let isNamedType (ty: Type) = + not (ty.IsArray || ty.IsByRef || ty.IsPointer) - let equivHeadTypes (ty1:Type) (ty2:Type) = - isNamedType(ty1) && - if ty1.IsGenericType then - ty2.IsGenericType && (ty1.GetGenericTypeDefinition()).Equals(ty2.GetGenericTypeDefinition()) - else - ty1.Equals(ty2) + let equivHeadTypes (ty1: Type) (ty2: Type) = + isNamedType (ty1) + && if ty1.IsGenericType then + ty2.IsGenericType + && (ty1.GetGenericTypeDefinition()).Equals(ty2.GetGenericTypeDefinition()) + else + ty1.Equals(ty2) let option = typedefof @@ -440,10 +465,10 @@ module ReflectUtils = let isUnitType ty = equivHeadTypes ty typeof - let isListType ty = - FSharpType.IsUnion ty && - (let cases = FSharpType.GetUnionCases ty - cases.Length > 0 && equivHeadTypes typedefof<_ list> cases[0].DeclaringType) + let isListType ty = + FSharpType.IsUnion ty + && (let cases = FSharpType.GetUnionCases ty + cases.Length > 0 && equivHeadTypes typedefof<_ list> cases[0].DeclaringType) [] type TupleType = @@ -453,7 +478,7 @@ module ReflectUtils = [] type ValueInfo = | TupleValue of TupleType * (obj * Type)[] - | FunctionClosureValue of Type + | FunctionClosureValue of Type | RecordValue of (string * obj * Type)[] | UnionCaseValue of string * (string * (obj * Type))[] | ExceptionValue of Type * (string * (obj * Type))[] @@ -465,315 +490,359 @@ module ReflectUtils = // Analyze an object to see if it the representation // of an F# value. let GetValueInfoOfObject (bindingFlags: BindingFlags) (obj: obj) = - match obj with + match obj with | null -> NullValue - | _ -> - let reprty = obj.GetType() - - // First a bunch of special rules for tuples - // Because of the way F# currently compiles tuple values - // of size > 7 we can only reliably reflect on sizes up - // to 7. - - if FSharpType.IsTuple reprty then - let tyArgs = FSharpType.GetTupleElements(reprty) - let fields = FSharpValue.GetTupleFields obj |> Array.mapi (fun i v -> (v, tyArgs[i])) - let tupleType = - if reprty.Name.StartsWith "ValueTuple" then TupleType.Value - else TupleType.Reference - TupleValue (tupleType, fields) - - elif FSharpType.IsFunction reprty then - FunctionClosureValue reprty - - // It must be exception, abstract, record or union. - // Either way we assume the only properties defined on - // the type are the actual fields of the type. Again, - // we should be reading attributes here that indicate the - // true structure of the type, e.g. the order of the fields. - elif FSharpType.IsUnion(reprty, bindingFlags) then - let tag, vals = FSharpValue.GetUnionFields (obj, reprty, bindingFlags) - let props = tag.GetFields() - let pvals = (props, vals) ||> Array.map2 (fun prop v -> prop.Name, (v, prop.PropertyType)) - UnionCaseValue(tag.Name, pvals) - - elif FSharpType.IsExceptionRepresentation(reprty, bindingFlags) then - let props = FSharpType.GetExceptionFields(reprty, bindingFlags) - let vals = FSharpValue.GetExceptionFields(obj, bindingFlags) - let pvals = (props, vals) ||> Array.map2 (fun prop v -> prop.Name, (v, prop.PropertyType)) - ExceptionValue(reprty, pvals) - - elif FSharpType.IsRecord(reprty, bindingFlags) then - let props = FSharpType.GetRecordFields(reprty, bindingFlags) - RecordValue(props |> Array.map (fun prop -> prop.Name, prop.GetValue (obj, null), prop.PropertyType)) - else - ObjectValue(obj) + | _ -> + let reprty = obj.GetType() + + // First a bunch of special rules for tuples + // Because of the way F# currently compiles tuple values + // of size > 7 we can only reliably reflect on sizes up + // to 7. + + if FSharpType.IsTuple reprty then + let tyArgs = FSharpType.GetTupleElements(reprty) + + let fields = + FSharpValue.GetTupleFields obj |> Array.mapi (fun i v -> (v, tyArgs[i])) + + let tupleType = + if reprty.Name.StartsWith "ValueTuple" then + TupleType.Value + else + TupleType.Reference + + TupleValue(tupleType, fields) + + elif FSharpType.IsFunction reprty then + FunctionClosureValue reprty + + // It must be exception, abstract, record or union. + // Either way we assume the only properties defined on + // the type are the actual fields of the type. Again, + // we should be reading attributes here that indicate the + // true structure of the type, e.g. the order of the fields. + elif FSharpType.IsUnion(reprty, bindingFlags) then + let tag, vals = FSharpValue.GetUnionFields(obj, reprty, bindingFlags) + let props = tag.GetFields() + + let pvals = + (props, vals) ||> Array.map2 (fun prop v -> prop.Name, (v, prop.PropertyType)) + + UnionCaseValue(tag.Name, pvals) + + elif FSharpType.IsExceptionRepresentation(reprty, bindingFlags) then + let props = FSharpType.GetExceptionFields(reprty, bindingFlags) + let vals = FSharpValue.GetExceptionFields(obj, bindingFlags) + + let pvals = + (props, vals) ||> Array.map2 (fun prop v -> prop.Name, (v, prop.PropertyType)) + + ExceptionValue(reprty, pvals) + + elif FSharpType.IsRecord(reprty, bindingFlags) then + let props = FSharpType.GetRecordFields(reprty, bindingFlags) + + RecordValue( + props + |> Array.map (fun prop -> prop.Name, prop.GetValue(obj, null), prop.PropertyType) + ) + else + ObjectValue(obj) // This one is like the above but can make use of additional // statically-known type information to aid in the - // analysis of null values. + // analysis of null values. - let GetValueInfo bindingFlags (x: 'a, ty: Type) (* x could be null *) = + let GetValueInfo bindingFlags (x: 'a, ty: Type) (* x could be null *) = let obj = (box x) - match obj with + + match obj with | null -> let isNullaryUnion = match ty.GetCustomAttributes(typeof, false) with - | [|:? CompilationRepresentationAttribute as attr|] -> + | [| :? CompilationRepresentationAttribute as attr |] -> (attr.Flags &&& CompilationRepresentationFlags.UseNullAsTrueValue) = CompilationRepresentationFlags.UseNullAsTrueValue | _ -> false + if isNullaryUnion then - let nullaryCase = FSharpType.GetUnionCases ty |> Array.filter (fun uc -> uc.GetFields().Length = 0) |> Array.item 0 - UnionCaseValue(nullaryCase.Name, [| |]) - elif isUnitType ty then UnitValue - else NullValue - | _ -> - GetValueInfoOfObject bindingFlags obj - -module Display = + let nullaryCase = + FSharpType.GetUnionCases ty + |> Array.filter (fun uc -> uc.GetFields().Length = 0) + |> Array.item 0 + + UnionCaseValue(nullaryCase.Name, [||]) + elif isUnitType ty then + UnitValue + else + NullValue + | _ -> GetValueInfoOfObject bindingFlags obj + +module Display = open ReflectUtils - - let string_of_int (i:int) = i.ToString() - let typeUsesSystemObjectToString (ty:Type) = + let string_of_int (i: int) = i.ToString() + + let typeUsesSystemObjectToString (ty: Type) = try - let methInfo = ty.GetMethod("ToString", BindingFlags.Public ||| BindingFlags.Instance, null, [| |], null) + let methInfo = + ty.GetMethod("ToString", BindingFlags.Public ||| BindingFlags.Instance, null, [||], null) + methInfo.DeclaringType = typeof - with _e -> false + with + | _e -> false - let catchExn f = try Choice1Of2 (f ()) with e -> Choice2Of2 e + let catchExn f = + try + Choice1Of2(f ()) + with + | e -> Choice2Of2 e // An implementation of break stack. // Uses mutable state, relying on linear threading of the state. [] - type Breaks = - Breaks of - /// pos of next free slot - nextFreeSlot: int * - /// pos of next possible "outer" break - OR - outer=next if none possible - nextOuterBreak: int * - /// stack of savings, -ve means it has been broken + type Breaks = + | Breaks of + /// pos of next free slot + nextFreeSlot: int * + /// pos of next possible "outer" break - OR - outer=next if none possible + nextOuterBreak: int * + /// stack of savings, -ve means it has been broken savingsStack: int[] - // next is next slot to push into - aka size of current occupied stack. + // next is next slot to push into - aka size of current occupied stack. // outer counts up from 0, and is next slot to break if break forced. // - if all breaks forced, then outer=next. // - popping under these conditions needs to reduce outer and next. - - let chunkN = 400 + + let chunkN = 400 let breaks0 () = Breaks(0, 0, Array.create chunkN 0) - let pushBreak saving (Breaks(next, outer, stack)) = - let stack = + let pushBreak saving (Breaks (next, outer, stack)) = + let stack = if next = stack.Length then - Array.init (next + chunkN) (fun i -> if i < next then stack[i] else 0) // expand if full + Array.init (next + chunkN) (fun i -> if i < next then stack[i] else 0) // expand if full else stack - - stack[next] <- saving; - Breaks(next+1, outer, stack) - - let popBreak (Breaks(next, outer, stack)) = - if next=0 then raise (Failure "popBreak: underflow"); - let topBroke = stack[next-1] < 0 - let outer = if outer=next then outer-1 else outer // if all broken, unwind + + stack[next] <- saving + Breaks(next + 1, outer, stack) + + let popBreak (Breaks (next, outer, stack)) = + if next = 0 then + raise (Failure "popBreak: underflow") + + let topBroke = stack[next - 1] < 0 + + let outer = + if outer = next then + outer - 1 + else + outer // if all broken, unwind + let next = next - 1 Breaks(next, outer, stack), topBroke - let forceBreak (Breaks(next, outer, stack)) = - if outer=next then - // all broken + let forceBreak (Breaks (next, outer, stack)) = + if outer = next then + // all broken None else let saving = stack[outer] - stack[outer] <- -stack[outer]; - let outer = outer+1 - Some (Breaks(next, outer, stack), saving) + stack[outer] <- -stack[outer] + let outer = outer + 1 + Some(Breaks(next, outer, stack), saving) /// fitting let squashToAux (maxWidth, leafFormatter: _ -> TaggedText) layout = let (|ObjToTaggedText|) = leafFormatter - if maxWidth <= 0 then layout else - let rec fit breaks (pos, layout) = - // breaks = break context, can force to get indentation savings. - // pos = current position in line - // layout = to fit - //------ - // returns: - // breaks - // layout - with breaks put in to fit it. - // pos - current pos in line = rightmost position of last line of block. - // offset - width of last line of block - // NOTE: offset <= pos -- depending on tabbing of last block - - let breaks, layout, pos, offset = - match layout with - | Attr (tag, attrs, l) -> - let breaks, layout, pos, offset = fit breaks (pos, l) - let layout = Attr (tag, attrs, layout) - breaks, layout, pos, offset - - | Leaf (jl, text, jr) - | ObjLeaf (jl, ObjToTaggedText text, jr) -> - // save the formatted text from the squash - let layout = Leaf(jl, text, jr) - let textWidth = length text - let rec fitLeaf breaks pos = - if pos + textWidth <= maxWidth then - breaks, layout, pos + textWidth, textWidth // great, it fits - else - match forceBreak breaks with - | None -> - breaks, layout, pos + textWidth, textWidth // tough, no more breaks - | Some (breaks, saving) -> - let pos = pos - saving - fitLeaf breaks pos - - fitLeaf breaks pos - - | Node (l, r, joint) -> - let jm = Layout.JuxtapositionMiddle (l, r) - let mid = if jm then 0 else 1 - match joint with - | Unbreakable -> - let breaks, l, pos, offsetl = fit breaks (pos, l) // fit left - let pos = pos + mid // fit space if juxt says so - let breaks, r, pos, offsetr = fit breaks (pos, r) // fit right - breaks, Node (l, r, Unbreakable), pos, offsetl + mid + offsetr - - | Broken indent -> - let breaks, l, pos, offsetl = fit breaks (pos, l) // fit left - let pos = pos - offsetl + indent // broken so - offset left + ident - let breaks, r, pos, offsetr = fit breaks (pos, r) // fit right - breaks, Node (l, r, Broken indent), pos, indent + offsetr - - | Breakable indent -> - let breaks, l, pos, offsetl = fit breaks (pos, l) // fit left - // have a break possibility, with saving - let saving = offsetl + mid - indent - let pos = pos + mid - if saving>0 then - let breaks = pushBreak saving breaks - let breaks, r, pos, offsetr = fit breaks (pos, r) - let breaks, broken = popBreak breaks - if broken then - breaks, Node (l, r, Broken indent) , pos, indent + offsetr + + if maxWidth <= 0 then + layout + else + let rec fit breaks (pos, layout) = + // breaks = break context, can force to get indentation savings. + // pos = current position in line + // layout = to fit + //------ + // returns: + // breaks + // layout - with breaks put in to fit it. + // pos - current pos in line = rightmost position of last line of block. + // offset - width of last line of block + // NOTE: offset <= pos -- depending on tabbing of last block + + let breaks, layout, pos, offset = + match layout with + | Attr (tag, attrs, l) -> + let breaks, layout, pos, offset = fit breaks (pos, l) + let layout = Attr(tag, attrs, layout) + breaks, layout, pos, offset + + | Leaf (jl, text, jr) + | ObjLeaf (jl, ObjToTaggedText text, jr) -> + // save the formatted text from the squash + let layout = Leaf(jl, text, jr) + let textWidth = length text + + let rec fitLeaf breaks pos = + if pos + textWidth <= maxWidth then + breaks, layout, pos + textWidth, textWidth // great, it fits else - breaks, Node (l, r, Breakable indent), pos, offsetl + mid + offsetr - else - // actually no saving so no break - let breaks, r, pos, offsetr = fit breaks (pos, r) - breaks, Node (l, r, Breakable indent) , pos, offsetl + mid + offsetr - - //printf "\nDone: pos=%d offset=%d" pos offset; - breaks, layout, pos, offset - - let breaks = breaks0 () - let pos = 0 - let _, layout, _, _ = fit breaks (pos, layout) - layout + match forceBreak breaks with + | None -> breaks, layout, pos + textWidth, textWidth // tough, no more breaks + | Some (breaks, saving) -> + let pos = pos - saving + fitLeaf breaks pos + + fitLeaf breaks pos + + | Node (l, r, joint) -> + let jm = Layout.JuxtapositionMiddle(l, r) + let mid = if jm then 0 else 1 + + match joint with + | Unbreakable -> + let breaks, l, pos, offsetl = fit breaks (pos, l) // fit left + let pos = pos + mid // fit space if juxt says so + let breaks, r, pos, offsetr = fit breaks (pos, r) // fit right + breaks, Node(l, r, Unbreakable), pos, offsetl + mid + offsetr + + | Broken indent -> + let breaks, l, pos, offsetl = fit breaks (pos, l) // fit left + let pos = pos - offsetl + indent // broken so - offset left + ident + let breaks, r, pos, offsetr = fit breaks (pos, r) // fit right + breaks, Node(l, r, Broken indent), pos, indent + offsetr + + | Breakable indent -> + let breaks, l, pos, offsetl = fit breaks (pos, l) // fit left + // have a break possibility, with saving + let saving = offsetl + mid - indent + let pos = pos + mid + + if saving > 0 then + let breaks = pushBreak saving breaks + let breaks, r, pos, offsetr = fit breaks (pos, r) + let breaks, broken = popBreak breaks + + if broken then + breaks, Node(l, r, Broken indent), pos, indent + offsetr + else + breaks, Node(l, r, Breakable indent), pos, offsetl + mid + offsetr + else + // actually no saving so no break + let breaks, r, pos, offsetr = fit breaks (pos, r) + breaks, Node(l, r, Breakable indent), pos, offsetl + mid + offsetr + + //printf "\nDone: pos=%d offset=%d" pos offset; + breaks, layout, pos, offset + + let breaks = breaks0 () + let pos = 0 + let _, layout, _, _ = fit breaks (pos, layout) + layout let combine (strs: string list) = String.Concat strs let showL opts leafFormatter layout = let push x rstrs = x :: rstrs let z0 = [], 0 - let addText (rstrs, i) (text:string) = push text rstrs, i + text.Length - let index (_, i) = i - let extract rstrs = combine(List.rev rstrs) - let newLine (rstrs, _) n = // \n then spaces... + let addText (rstrs, i) (text: string) = push text rstrs, i + text.Length + let index (_, i) = i + let extract rstrs = combine (List.rev rstrs) + + let newLine (rstrs, _) n = // \n then spaces... let indent = String(' ', n) - let rstrs = push "\n" rstrs + let rstrs = push "\n" rstrs let rstrs = push indent rstrs rstrs, n - // addL: pos is tab level - let rec addL z pos layout = - match layout with - | ObjLeaf (_, obj, _) -> + // addL: pos is tab level + let rec addL z pos layout = + match layout with + | ObjLeaf (_, obj, _) -> let text = leafFormatter obj addText z text - | Leaf (_, obj, _) -> - addText z obj.Text + | Leaf (_, obj, _) -> addText z obj.Text - | Node (l, r, Broken indent) - // Print width = 0 implies 1D layout, no squash - when not (opts.PrintWidth = 0) -> + | Node (l, r, Broken indent) when not (opts.PrintWidth = 0) -> let z = addL z pos l - let z = newLine z (pos+indent) - let z = addL z (pos+indent) r + let z = newLine z (pos + indent) + let z = addL z (pos + indent) r z | Node (l, r, _) -> - let jm = Layout.JuxtapositionMiddle (l, r) + let jm = Layout.JuxtapositionMiddle(l, r) let z = addL z pos l let z = if jm then z else addText z " " let pos = index z let z = addL z pos r z - | Attr (_, _, l) -> - addL z pos l - + | Attr (_, _, l) -> addL z pos l + let rstrs, _ = addL z0 0 layout extract rstrs let outL outAttribute leafFormatter (chan: TaggedTextWriter) layout = - // write layout to output chan directly + // write layout to output chan directly let write s = chan.Write(s) - // z is just current indent + // z is just current indent let z0 = 0 let index i = i - let addText z text = write text; (z + length text) - let newLine _ n = // \n then spaces... + + let addText z text = + write text + (z + length text) + + let newLine _ n = // \n then spaces... let indent = String(' ', n) - chan.WriteLine(); - write (tagText indent); + chan.WriteLine() + write (tagText indent) n - - // addL: pos is tab level - let rec addL z pos layout = - match layout with - | ObjLeaf (_, obj, _) -> - let text = leafFormatter obj + + // addL: pos is tab level + let rec addL z pos layout = + match layout with + | ObjLeaf (_, obj, _) -> + let text = leafFormatter obj addText z text - | Leaf (_, obj, _) -> - addText z obj - | Node (l, r, Broken indent) -> + | Leaf (_, obj, _) -> addText z obj + | Node (l, r, Broken indent) -> let z = addL z pos l - let z = newLine z (pos+indent) - let z = addL z (pos+indent) r + let z = newLine z (pos + indent) + let z = addL z (pos + indent) r z - | Node (l, r, _) -> - let jm = Layout.JuxtapositionMiddle (l, r) + | Node (l, r, _) -> + let jm = Layout.JuxtapositionMiddle(l, r) let z = addL z pos l let z = if jm then z else addText z space let pos = index z let z = addL z pos r - z + z | Attr (tag, attrs, l) -> let _ = outAttribute tag attrs true let z = addL z pos l let _ = outAttribute tag attrs false z - + let _ = addL z0 0 layout () let unpackCons recd = - match recd with - | [|(_, h);(_, t)|] -> (h, t) + match recd with + | [| (_, h); (_, t) |] -> (h, t) | _ -> failwith "unpackCons" - let getListValueInfo bindingFlags (x:obj, ty:Type) = - match x with - | null -> None - | _ -> + let getListValueInfo bindingFlags (x: obj, ty: Type) = + match x with + | null -> None + | _ -> match Value.GetValueInfo bindingFlags (x, ty) with - | UnionCaseValue ("Cons", recd) -> Some (unpackCons recd) - | UnionCaseValue ("Empty", [| |]) -> None + | UnionCaseValue ("Cons", recd) -> Some(unpackCons recd) + | UnionCaseValue ("Empty", [||]) -> None | _ -> failwith "List value had unexpected ValueInfo" let structL = wordL (tagKeyword "struct") @@ -781,61 +850,79 @@ module Display = let nullL = wordL (tagKeyword "null") let unitL = wordL (tagPunctuation "()") - + let makeRecordL nameXs = let itemL (name, xL) = (wordL name ^^ wordL equals) -- xL - let braceL xs = (wordL leftBrace) ^^ xs ^^ (wordL rightBrace) - - nameXs - |> List.map itemL - |> aboveListL - |> braceL + + let braceL xs = + (wordL leftBrace) ^^ xs ^^ (wordL rightBrace) + + nameXs |> List.map itemL |> aboveListL |> braceL let makePropertiesL nameXs = - let itemL (name, v) = - let labelL = wordL name + let itemL (name, v) = + let labelL = wordL name + (labelL ^^ wordL equals) - ^^ (match v with + ^^ (match v with | None -> wordL questionMark | Some xL -> xL) - ^^ (rightL semicolon) - let braceL xs = (leftL leftBrace) ^^ xs ^^ (rightL rightBrace) + ^^ (rightL semicolon) + + let braceL xs = + (leftL leftBrace) ^^ xs ^^ (rightL rightBrace) + braceL (aboveListL (List.map itemL nameXs)) let makeListL itemLs = - (leftL leftBracket) - ^^ sepListL (rightL semicolon) itemLs - ^^ (rightL rightBracket) + (leftL leftBracket) + ^^ sepListL (rightL semicolon) itemLs ^^ (rightL rightBracket) let makeArrayL xs = - (leftL (tagPunctuation "[|")) - ^^ sepListL (rightL semicolon) xs - ^^ (rightL (tagPunctuation "|]")) + (leftL (tagPunctuation "[|")) + ^^ sepListL (rightL semicolon) xs ^^ (rightL (tagPunctuation "|]")) - let makeArray2L xs = leftL leftBracket ^^ aboveListL xs ^^ rightL rightBracket + let makeArray2L xs = + leftL leftBracket ^^ aboveListL xs ^^ rightL rightBracket let getProperty (ty: Type) (obj: obj) name = - ty.InvokeMember(name, (BindingFlags.GetProperty ||| BindingFlags.Instance ||| BindingFlags.Public ||| BindingFlags.NonPublic), null, obj, [| |],CultureInfo.InvariantCulture) - - let getField obj (fieldInfo: FieldInfo) = - fieldInfo.GetValue(obj) - - let formatChar isChar c = - match c with + ty.InvokeMember( + name, + (BindingFlags.GetProperty + ||| BindingFlags.Instance + ||| BindingFlags.Public + ||| BindingFlags.NonPublic), + null, + obj, + [||], + CultureInfo.InvariantCulture + ) + + let getField obj (fieldInfo: FieldInfo) = fieldInfo.GetValue(obj) + + let formatChar isChar c = + match c with | '\'' when isChar -> "\\\'" | '\"' when not isChar -> "\\\"" | '\\' -> "\\\\" | '\b' -> "\\b" - | _ when Char.IsControl(c) -> - let d1 = (int c / 100) % 10 - let d2 = (int c / 10) % 10 - let d3 = int c % 10 - "\\" + d1.ToString() + d2.ToString() + d3.ToString() + | _ when Char.IsControl(c) -> + let d1 = (int c / 100) % 10 + let d2 = (int c / 10) % 10 + let d3 = int c % 10 + "\\" + d1.ToString() + d2.ToString() + d3.ToString() | _ -> c.ToString() - - let formatString (s:string) = - let rec check i = i < s.Length && not (Char.IsControl(s,i)) && s[i] <> '\"' && check (i+1) - let rec conv i acc = if i = s.Length then combine (List.rev acc) else conv (i+1) (formatChar false s[i] :: acc) + + let formatString (s: string) = + let rec check i = + i < s.Length && not (Char.IsControl(s, i)) && s[i] <> '\"' && check (i + 1) + + let rec conv i acc = + if i = s.Length then + combine (List.rev acc) + else + conv (i + 1) (formatChar false s[i] :: acc) + "\"" + s + "\"" // Return a truncated version of the string, e.g. @@ -847,203 +934,256 @@ module Display = // // The suffix like "+[dd chars]" is 11 chars. // 12345678901 - let formatStringInWidth (width:int) (str:string) = + let formatStringInWidth (width: int) (str: string) = let suffixLength = 11 // turning point suffix length let prefixMinLength = 12 // arbitrary. If print width is reduced, want to print a minimum of information on strings... let prefixLength = max (width - 2 (*quotes*) - suffixLength) prefixMinLength - "\"" + (str.Substring(0,prefixLength)) + "\"" + "+[" + (str.Length - prefixLength).ToString() + " chars]" - - type Precedence = + + "\"" + + (str.Substring(0, prefixLength)) + + "\"" + + "+[" + + (str.Length - prefixLength).ToString() + + " chars]" + + type Precedence = | BracketIfTupleOrNotAtomic = 2 | BracketIfTuple = 3 | NeverBracket = 4 // In fsi.exe, certain objects are not printed for top-level bindings. [] - type ShowMode = - | ShowAll + type ShowMode = + | ShowAll | ShowTopLevelBinding - let isSetOrMapType (ty:Type) = - ty.IsGenericType && - (ty.GetGenericTypeDefinition() = typedefof> - || ty.GetGenericTypeDefinition() = typedefof>) + let isSetOrMapType (ty: Type) = + ty.IsGenericType + && (ty.GetGenericTypeDefinition() = typedefof> + || ty.GetGenericTypeDefinition() = typedefof>) // showMode = ShowTopLevelBinding on the outermost expression when called from fsi.exe, // This allows certain outputs, e.g. objects that would print as to be suppressed, etc. See 4343. // Calls to layout proper sub-objects should pass showMode = ShowAll. // - // Precedences to ensure we add brackets in the right places + // Precedences to ensure we add brackets in the right places type ObjectGraphFormatter(opts: FormatOptions, bindingFlags) = - + // Keep a record of objects encountered along the way - let path = Dictionary(10,HashIdentity.Reference) + let path = Dictionary(10, HashIdentity.Reference) // Roughly count the "nodes" printed, e.g. leaf items and inner nodes, but not every bracket and comma. - let mutable size = opts.PrintSize - let exceededPrintSize() = size<=0 - let countNodes n = if size > 0 then size <- size - n else () // no need to keep decrementing (and avoid wrap around) - let stopShort _ = exceededPrintSize() // for unfoldL + let mutable size = opts.PrintSize + let exceededPrintSize () = size <= 0 + + let countNodes n = + if size > 0 then + size <- size - n + else + () // no need to keep decrementing (and avoid wrap around) + + let stopShort _ = exceededPrintSize () // for unfoldL // Recursive descent - let rec nestedObjL depthLim prec (x:obj, ty:Type) = - objL ShowAll depthLim prec (x, ty) + let rec nestedObjL depthLim prec (x: obj, ty: Type) = objL ShowAll depthLim prec (x, ty) - and objL showMode depthLim prec (x:obj, ty:Type) = + and objL showMode depthLim prec (x: obj, ty: Type) = let info = Value.GetValueInfo bindingFlags (x, ty) + try - if depthLim<=0 || exceededPrintSize() then wordL (tagPunctuation "...") else - match x with - | null -> - reprL showMode (depthLim-1) prec info x - | _ -> - if (path.ContainsKey(x)) then - wordL (tagPunctuation "...") - else - path.Add(x,0) - - let res = - // Lazy values. VS2008 used StructuredFormatDisplayAttribute to show via ToString. Dev10 (no attr) needs a special case. - let ty = x.GetType() - if ty.IsGenericType && ty.GetGenericTypeDefinition() = typedefof> then - Some (wordL (tagText(x.ToString()))) - else - // Try the StructuredFormatDisplayAttribute extensibility attribute - match ty.GetCustomAttributes (typeof, true) with - | null | [| |] -> None - | res -> - structuredFormatObjectL showMode ty depthLim (res[0] :?> StructuredFormatDisplayAttribute) x + if depthLim <= 0 || exceededPrintSize () then + wordL (tagPunctuation "...") + else + match x with + | null -> reprL showMode (depthLim - 1) prec info x + | _ -> + if (path.ContainsKey(x)) then + wordL (tagPunctuation "...") + else + path.Add(x, 0) + + let res = + // Lazy values. VS2008 used StructuredFormatDisplayAttribute to show via ToString. Dev10 (no attr) needs a special case. + let ty = x.GetType() + + if ty.IsGenericType && ty.GetGenericTypeDefinition() = typedefof> then + Some(wordL (tagText (x.ToString()))) + else + // Try the StructuredFormatDisplayAttribute extensibility attribute + match ty.GetCustomAttributes(typeof, true) with + | null + | [||] -> None + | res -> structuredFormatObjectL showMode ty depthLim (res[0] :?> StructuredFormatDisplayAttribute) x #if COMPILER - // This is the PrintIntercepts extensibility point currently revealed by fsi.exe's AddPrinter - let res = - match res with - | Some _ -> res - | None -> - let env = - { new IEnvironment with - member _.GetLayout(y) = nestedObjL (depthLim-1) Precedence.BracketIfTuple (y, y.GetType()) - member _.MaxColumns = opts.PrintLength - member _.MaxRows = opts.PrintLength } - opts.PrintIntercepts |> List.tryPick (fun intercept -> intercept env x) + // This is the PrintIntercepts extensibility point currently revealed by fsi.exe's AddPrinter + let res = + match res with + | Some _ -> res + | None -> + let env = + { new IEnvironment with + member _.GetLayout(y) = + nestedObjL (depthLim - 1) Precedence.BracketIfTuple (y, y.GetType()) + + member _.MaxColumns = opts.PrintLength + member _.MaxRows = opts.PrintLength + } + + opts.PrintIntercepts |> List.tryPick (fun intercept -> intercept env x) #endif - let res = - match res with - | Some res -> res - | None -> reprL showMode (depthLim-1) prec info x + let res = + match res with + | Some res -> res + | None -> reprL showMode (depthLim - 1) prec info x - path.Remove(x) |> ignore - res + path.Remove(x) |> ignore + res with - e -> + | e -> countNodes 1 - wordL (tagText("Error: " + e.Message)) + wordL (tagText ("Error: " + e.Message)) // Format an object that has a layout specified by StructuredFormatAttribute and structuredFormatObjectL showMode ty depthLim (attr: StructuredFormatDisplayAttribute) (obj: obj) = let txt = attr.Value - if isNull txt || txt.Length <= 1 then + + if isNull txt || txt.Length <= 1 then None else - let messageRegexPattern = @"^(?
.*?)(?.*?)(?.*)$"
-            let illFormedBracketPattern = @"(? 1 then Some (spaceListL (List.rev (wordL (tagText(replaceEscapedBrackets(txt))) :: layouts)))
-                    else Some (wordL (tagText(replaceEscapedBrackets(txt))))
-                else
-                    // we have a hit on a property reference
-                    let preText = replaceEscapedBrackets(m.Groups["pre"].Value) // everything before the first opening bracket
-                    let postText = m.Groups["post"].Value // Everything after the closing bracket
-                    let prop = replaceEscapedBrackets(m.Groups["prop"].Value) // Unescape everything between the opening and closing brackets
-
-                    match catchExn (fun () -> getProperty ty obj prop) with
-                    | Choice2Of2 e -> Some (wordL (tagText("")))
-                    | Choice1Of2 alternativeObj ->
-                        try 
-                            let alternativeObjL = 
-                                match alternativeObj with 
-                                // A particular rule is that if the alternative property
-                                // returns a string, we turn off auto-quoting and escaping of
-                                // the string, i.e. just treat the string as display text.
-                                // This allows simple implementations of 
-                                // such as
-                                //
-                                //    []
-                                //    type BigInt(signInt:int, v: BigNat) =
-                                //        member x.StructuredDisplayString = x.ToString()
-                                //
-                                | :? string as s -> sepL (tagText s)
-                                | _ -> 
-                                    // recursing like this can be expensive, so let's throttle it severely
-                                    objL showMode (depthLim/10) Precedence.BracketIfTuple (alternativeObj, alternativeObj.GetType())
-                            countNodes 0 // 0 means we do not count the preText and postText 
-
-                            let postTextMatch = System.Text.RegularExpressions.Regex.Match(postText, messageRegexPattern)
-                            // the postText for this node will be everything up to the next occurrence of an opening brace, if one exists
-                            let currentPostText =
-                                match postTextMatch.Success with
-                                | false -> postText 
-                                | true -> postTextMatch.Groups["pre"].Value
-
-                            let newLayouts = (sepL (tagText preText) ^^ alternativeObjL ^^ sepL (tagText currentPostText)) :: layouts
-                            match postText with
-                            | "" ->
-                                //We are done, build a space-delimited layout from the collection of layouts we've accumulated
-                                Some (spaceListL (List.rev newLayouts))
-
-                            | remainingPropertyText when postTextMatch.Success ->
-                                                      
-                                // look for stray brackets in the text before the next opening bracket
-                                let strayClosingMatch = System.Text.RegularExpressions.Regex.IsMatch(postTextMatch.Groups["pre"].Value, illFormedBracketPattern)
-                                if strayClosingMatch then
-                                    None
-                                else 
-                                    // More to process, keep going, using the postText starting at the next instance of a '{'
-                                    let openingBracketIndex = postTextMatch.Groups["prop"].Index-1
-                                    buildObjMessageL remainingPropertyText[openingBracketIndex..] newLayouts
-
-                            | remaingPropertyText ->
-                                // make sure we don't have any stray brackets
-                                let strayClosingMatch = System.Text.RegularExpressions.Regex.IsMatch(remaingPropertyText, illFormedBracketPattern)
-                                if strayClosingMatch then
-                                    None
-                                else
-                                    // We are done, there's more text but it doesn't contain any more properties, we need to remove escaped brackets now though
-                                    // since that wasn't done when creating currentPostText
-                                    Some (spaceListL (List.rev ((sepL (tagText preText) ^^ alternativeObjL ^^ sepL (tagText(replaceEscapedBrackets(remaingPropertyText)))) :: layouts)))
-                        with _ -> 
-                            None
+                let messageRegexPattern = @"^(?
.*?)(?.*?)(?.*)$"
+                let illFormedBracketPattern = @"(? 1 then
+                            Some(spaceListL (List.rev (wordL (tagText (replaceEscapedBrackets (txt))) :: layouts)))
+                        else
+                            Some(wordL (tagText (replaceEscapedBrackets (txt))))
+                    else
+                        // we have a hit on a property reference
+                        let preText = replaceEscapedBrackets (m.Groups["pre"].Value) // everything before the first opening bracket
+                        let postText = m.Groups["post"].Value // Everything after the closing bracket
+                        let prop = replaceEscapedBrackets (m.Groups["prop"].Value) // Unescape everything between the opening and closing brackets
+
+                        match catchExn (fun () -> getProperty ty obj prop) with
+                        | Choice2Of2 e -> Some(wordL (tagText ("")))
+                        | Choice1Of2 alternativeObj ->
+                            try
+                                let alternativeObjL =
+                                    match alternativeObj with
+                                    // A particular rule is that if the alternative property
+                                    // returns a string, we turn off auto-quoting and escaping of
+                                    // the string, i.e. just treat the string as display text.
+                                    // This allows simple implementations of
+                                    // such as
+                                    //
+                                    //    []
+                                    //    type BigInt(signInt:int, v: BigNat) =
+                                    //        member x.StructuredDisplayString = x.ToString()
+                                    //
+                                    | :? string as s -> sepL (tagText s)
+                                    | _ ->
+                                        // recursing like this can be expensive, so let's throttle it severely
+                                        objL showMode (depthLim / 10) Precedence.BracketIfTuple (alternativeObj, alternativeObj.GetType())
+
+                                countNodes 0 // 0 means we do not count the preText and postText
+
+                                let postTextMatch =
+                                    System.Text.RegularExpressions.Regex.Match(postText, messageRegexPattern)
+                                // the postText for this node will be everything up to the next occurrence of an opening brace, if one exists
+                                let currentPostText =
+                                    match postTextMatch.Success with
+                                    | false -> postText
+                                    | true -> postTextMatch.Groups["pre"].Value
+
+                                let newLayouts =
+                                    (sepL (tagText preText) ^^ alternativeObjL ^^ sepL (tagText currentPostText))
+                                    :: layouts
+
+                                match postText with
+                                | "" ->
+                                    //We are done, build a space-delimited layout from the collection of layouts we've accumulated
+                                    Some(spaceListL (List.rev newLayouts))
+
+                                | remainingPropertyText when postTextMatch.Success ->
+
+                                    // look for stray brackets in the text before the next opening bracket
+                                    let strayClosingMatch =
+                                        System.Text.RegularExpressions.Regex.IsMatch(
+                                            postTextMatch.Groups["pre"].Value,
+                                            illFormedBracketPattern
+                                        )
+
+                                    if strayClosingMatch then
+                                        None
+                                    else
+                                        // More to process, keep going, using the postText starting at the next instance of a '{'
+                                        let openingBracketIndex = postTextMatch.Groups["prop"].Index - 1
+                                        buildObjMessageL remainingPropertyText[openingBracketIndex..] newLayouts
+
+                                | remaingPropertyText ->
+                                    // make sure we don't have any stray brackets
+                                    let strayClosingMatch =
+                                        System.Text.RegularExpressions.Regex.IsMatch(remaingPropertyText, illFormedBracketPattern)
+
+                                    if strayClosingMatch then
+                                        None
+                                    else
+                                        // We are done, there's more text but it doesn't contain any more properties, we need to remove escaped brackets now though
+                                        // since that wasn't done when creating currentPostText
+                                        Some(
+                                            spaceListL (
+                                                List.rev (
+                                                    (sepL (tagText preText)
+                                                     ^^ alternativeObjL ^^ sepL (tagText (replaceEscapedBrackets (remaingPropertyText))))
+                                                    :: layouts
+                                                )
+                                            )
+                                        )
+                            with
+                            | _ -> None
+
+                // Seed with an empty layout with a space to the left for formatting purposes
+                buildObjMessageL txt [ leftL (tagText "") ]
 
         and recdAtomicTupleL depthLim recd =
             // tuples up args to UnionConstruction or ExceptionConstructor. no node count.
-            match recd with 
-            | [(_,x)] -> nestedObjL depthLim Precedence.BracketIfTupleOrNotAtomic x 
-            | txs -> leftL leftParen ^^ commaListL (List.map (snd >> nestedObjL depthLim Precedence.BracketIfTuple) txs) ^^ rightL rightParen
+            match recd with
+            | [ (_, x) ] -> nestedObjL depthLim Precedence.BracketIfTupleOrNotAtomic x
+            | txs ->
+                leftL leftParen
+                ^^ commaListL (List.map (snd >> nestedObjL depthLim Precedence.BracketIfTuple) txs)
+                   ^^ rightL rightParen
 
         and bracketIfL flag basicL =
-            if flag then (leftL leftParen) ^^ basicL ^^ (rightL rightParen) else basicL
+            if flag then
+                (leftL leftParen) ^^ basicL ^^ (rightL rightParen)
+            else
+                basicL
 
         and tupleValueL depthLim prec vals tupleType =
-            let basicL = sepListL (rightL comma) (List.map (nestedObjL depthLim Precedence.BracketIfTuple ) (Array.toList vals))
+            let basicL =
+                sepListL (rightL comma) (List.map (nestedObjL depthLim Precedence.BracketIfTuple) (Array.toList vals))
+
             let fields = bracketIfL (prec <= Precedence.BracketIfTuple) basicL
+
             match tupleType with
             | TupleType.Value -> structL ^^ fields
             | TupleType.Reference -> fields
@@ -1051,15 +1191,20 @@ module Display =
         and recordValueL depthLim items =
             let itemL (name, x, ty) =
                 countNodes 1
-                tagRecordField name,nestedObjL depthLim Precedence.BracketIfTuple (x, ty)
+                tagRecordField name, nestedObjL depthLim Precedence.BracketIfTuple (x, ty)
+
             makeRecordL (List.map itemL items)
 
         and listValueL depthLim constr recd =
-            match constr with 
-            | "Cons" -> 
-                let x,xs = unpackCons recd
+            match constr with
+            | "Cons" ->
+                let x, xs = unpackCons recd
                 let project xs = getListValueInfo bindingFlags xs
-                let itemLs = nestedObjL depthLim Precedence.BracketIfTuple x :: boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) project stopShort xs (opts.PrintLength - 1)
+
+                let itemLs =
+                    nestedObjL depthLim Precedence.BracketIfTuple x
+                    :: boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) project stopShort xs (opts.PrintLength - 1)
+
                 makeListL itemLs
             | _ ->
                 countNodes 1
@@ -1068,170 +1213,281 @@ module Display =
         and unionCaseValueL depthLim prec unionCaseName recd =
             countNodes 1
             let caseName = wordL (tagMethod unionCaseName)
+
             match recd with
             | [] -> caseName
-            | recd -> (caseName --- recdAtomicTupleL depthLim recd) |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
+            | recd ->
+                (caseName --- recdAtomicTupleL depthLim recd)
+                |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
 
         and fsharpExceptionL depthLim prec (exceptionType: Type) recd =
             countNodes 1
-            let name = exceptionType.Name 
+            let name = exceptionType.Name
+
             match recd with
             | [] -> (wordL (tagClass name))
-            | recd -> (wordL (tagClass name) --- recdAtomicTupleL depthLim recd) |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
+            | recd ->
+                (wordL (tagClass name) --- recdAtomicTupleL depthLim recd)
+                |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
 
         and showModeFilter showMode layout =
             match showMode with
             | ShowAll -> layout
-            | ShowTopLevelBinding -> emptyL                                                             
+            | ShowTopLevelBinding -> emptyL
 
         and functionClosureL showMode (closureType: Type) =
-            // Q: should function printing include the ty.Name? It does not convey much useful info to most users, e.g. "clo@0_123".    
+            // Q: should function printing include the ty.Name? It does not convey much useful info to most users, e.g. "clo@0_123".
             countNodes 1
-            wordL (tagText("")) |> showModeFilter showMode
+            wordL (tagText ("")) |> showModeFilter showMode
 
         and stringValueL (s: string) =
             countNodes 1
-#if COMPILER  
-            if s.Length + 2(*quotes*) <= opts.StringLimit then
+#if COMPILER
+            if s.Length + 2 (*quotes*) <= opts.StringLimit then
                 // With the quotes, it fits within the limit.
-                wordL (tagStringLiteral(formatString s))
+                wordL (tagStringLiteral (formatString s))
             else
                 // When a string is considered too long to print, there is a choice: what to print?
                 // a)             -- follows 
                 // b)      -- follows  and gives just the length
                 // c) "abcdefg"+[n chars] -- gives a prefix and the remaining chars
-                wordL (tagStringLiteral(formatStringInWidth opts.StringLimit s))
+                wordL (tagStringLiteral (formatStringInWidth opts.StringLimit s))
 #else
-            wordL (tagStringLiteral (formatString s))  
-#endif                   
+            wordL (tagStringLiteral (formatString s))
+#endif
 
         and arrayValueL depthLim (arr: Array) =
             let ty = arr.GetType().GetElementType()
+
             match arr.Rank with
-            | 1 -> 
+            | 1 ->
                 let n = arr.Length
-                let b1 = arr.GetLowerBound(0) 
-                let project depthLim = if depthLim=(b1+n) then None else Some ((box (arr.GetValue(depthLim)), ty),depthLim+1)
-                let itemLs = boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) project stopShort b1 opts.PrintLength
-                makeArrayL (if b1 = 0 then itemLs else wordL (tagText("bound1="+string_of_int b1)) :: itemLs)
-            | 2 -> 
+                let b1 = arr.GetLowerBound(0)
+
+                let project depthLim =
+                    if depthLim = (b1 + n) then
+                        None
+                    else
+                        Some((box (arr.GetValue(depthLim)), ty), depthLim + 1)
+
+                let itemLs =
+                    boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) project stopShort b1 opts.PrintLength
+
+                makeArrayL (
+                    if b1 = 0 then
+                        itemLs
+                    else
+                        wordL (tagText ("bound1=" + string_of_int b1)) :: itemLs
+                )
+            | 2 ->
                 let n1 = arr.GetLength(0)
                 let n2 = arr.GetLength(1)
-                let b1 = arr.GetLowerBound(0) 
-                let b2 = arr.GetLowerBound(1) 
+                let b1 = arr.GetLowerBound(0)
+                let b2 = arr.GetLowerBound(1)
+
                 let project2 x y =
-                    if x>=(b1+n1) || y>=(b2+n2) then None
-                    else Some ((box (arr.GetValue(x,y)), ty),y+1)
-                let rowL x = boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) (project2 x) stopShort b2 opts.PrintLength |> makeListL
-                let project1 x = if x>=(b1+n1) then None else Some (x,x+1)
+                    if x >= (b1 + n1) || y >= (b2 + n2) then
+                        None
+                    else
+                        Some((box (arr.GetValue(x, y)), ty), y + 1)
+
+                let rowL x =
+                    boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) (project2 x) stopShort b2 opts.PrintLength
+                    |> makeListL
+
+                let project1 x =
+                    if x >= (b1 + n1) then
+                        None
+                    else
+                        Some(x, x + 1)
+
                 let rowsL = boundedUnfoldL rowL project1 stopShort b1 opts.PrintLength
-                makeArray2L (if b1=0 && b2 = 0 then rowsL else wordL (tagText("bound1=" + string_of_int b1)) :: wordL(tagText("bound2=" + string_of_int b2)) :: rowsL)
-            | n -> 
-                makeArrayL [wordL (tagText("rank=" + string_of_int n))]
-                        
+
+                makeArray2L (
+                    if b1 = 0 && b2 = 0 then
+                        rowsL
+                    else
+                        wordL (tagText ("bound1=" + string_of_int b1))
+                        :: wordL (tagText ("bound2=" + string_of_int b2)) :: rowsL
+                )
+            | n -> makeArrayL [ wordL (tagText ("rank=" + string_of_int n)) ]
+
         and mapSetValueL depthLim prec (ty: Type) (obj: obj) =
-            let word = if ty.GetGenericTypeDefinition() = typedefof> then "map" else "set"
-            let possibleKeyValueL v = 
+            let word =
+                if ty.GetGenericTypeDefinition() = typedefof> then
+                    "map"
+                else
+                    "set"
+
+            let possibleKeyValueL v =
                 let tyv = v.GetType()
-                if word = "map" &&
-                    (match v with null -> false | _ -> true) && 
-                    tyv.IsGenericType && 
-                    tyv.GetGenericTypeDefinition() = typedefof> then
-                    nestedObjL depthLim Precedence.BracketIfTuple ((tyv.GetProperty("Key").GetValue(v, [| |]), 
-                                                                    tyv.GetProperty("Value").GetValue(v, [| |])), tyv)
+
+                if word = "map"
+                   && (match v with
+                       | null -> false
+                       | _ -> true)
+                   && tyv.IsGenericType
+                   && tyv.GetGenericTypeDefinition() = typedefof> then
+                    nestedObjL
+                        depthLim
+                        Precedence.BracketIfTuple
+                        ((tyv.GetProperty("Key").GetValue(v, [||]), tyv.GetProperty("Value").GetValue(v, [||])), tyv)
                 else
                     nestedObjL depthLim Precedence.BracketIfTuple (v, tyv)
-            let it = (obj :?>  System.Collections.IEnumerable).GetEnumerator() 
-            try 
-                let itemLs = boundedUnfoldL possibleKeyValueL (fun () -> if it.MoveNext() then Some(it.Current,()) else None) stopShort () (1+opts.PrintLength/12)
-                (wordL (tagClass word) --- makeListL itemLs) |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
-            finally 
-                match it with 
+
+            let it = (obj :?> System.Collections.IEnumerable).GetEnumerator()
+
+            try
+                let itemLs =
+                    boundedUnfoldL
+                        possibleKeyValueL
+                        (fun () ->
+                            if it.MoveNext() then
+                                Some(it.Current, ())
+                            else
+                                None)
+                        stopShort
+                        ()
+                        (1 + opts.PrintLength / 12)
+
+                (wordL (tagClass word) --- makeListL itemLs)
+                |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
+            finally
+                match it with
                 | :? IDisposable as e -> e.Dispose()
                 | _ -> ()
 
         and sequenceValueL showMode depthLim prec (ie: System.Collections.IEnumerable) =
-            let showContent = 
+            let showContent =
                 // do not display content of IQueryable since its execution may take significant time
-                opts.ShowIEnumerable && (ie.GetType().GetInterfaces() |> Array.exists(fun ty -> ty.FullName = "System.Linq.IQueryable") |> not)
+                opts.ShowIEnumerable
+                && (ie.GetType().GetInterfaces()
+                    |> Array.exists (fun ty -> ty.FullName = "System.Linq.IQueryable")
+                    |> not)
 
             if showContent then
                 let word = "seq"
-                let it = ie.GetEnumerator() 
-                let ty = ie.GetType().GetInterfaces() |> Array.filter (fun ty -> ty.IsGenericType && ty.Name = "IEnumerable`1") |> Array.tryItem 0
-                let ty = Option.map (fun (ty:Type) -> ty.GetGenericArguments().[0]) ty
-                try 
-                    let itemLs = boundedUnfoldL (nestedObjL depthLim Precedence.BracketIfTuple) (fun () -> if it.MoveNext() then Some((it.Current, match ty with | None -> it.Current.GetType() | Some ty -> ty),()) else None) stopShort () (1+opts.PrintLength/30)
-                    (wordL (tagClass word) --- makeListL itemLs) |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
-                finally 
-                    match it with 
+                let it = ie.GetEnumerator()
+
+                let ty =
+                    ie.GetType().GetInterfaces()
+                    |> Array.filter (fun ty -> ty.IsGenericType && ty.Name = "IEnumerable`1")
+                    |> Array.tryItem 0
+
+                let ty = Option.map (fun (ty: Type) -> ty.GetGenericArguments().[0]) ty
+
+                try
+                    let itemLs =
+                        boundedUnfoldL
+                            (nestedObjL depthLim Precedence.BracketIfTuple)
+                            (fun () ->
+                                if it.MoveNext() then
+                                    Some(
+                                        (it.Current,
+                                         match ty with
+                                         | None -> it.Current.GetType()
+                                         | Some ty -> ty),
+                                        ()
+                                    )
+                                else
+                                    None)
+                            stopShort
+                            ()
+                            (1 + opts.PrintLength / 30)
+
+                    (wordL (tagClass word) --- makeListL itemLs)
+                    |> bracketIfL (prec <= Precedence.BracketIfTupleOrNotAtomic)
+                finally
+                    match it with
                     | :? IDisposable as e -> e.Dispose()
                     | _ -> ()
-                             
+
             else
                 // Sequence printing is turned off for declared-values, and maybe be disabled to users.
                 // There is choice here, what to print?  or ... or ?
-                // Also, in the declared values case, if the sequence is actually a known non-lazy type (list, array etc etc) we could print it.  
+                // Also, in the declared values case, if the sequence is actually a known non-lazy type (list, array etc etc) we could print it.
                 wordL (tagText "") |> showModeFilter showMode
 
         and objectValueWithPropertiesL depthLim (ty: Type) (obj: obj) =
 
             // This buries an obj in the layout, rendered at squash time via a leafFormatter.
             let basicL = Layout.objL obj
-            let props = ty.GetProperties(BindingFlags.GetField ||| BindingFlags.Instance ||| BindingFlags.Public)
-            let fields = ty.GetFields(BindingFlags.Instance ||| BindingFlags.Public) |> Array.map (fun i -> i :> MemberInfo)
-            let propsAndFields = 
-                props |> Array.map (fun i -> i :> MemberInfo)
-                        |> Array.append fields
-                        |> Array.filter (fun pi ->
-                    // check if property is annotated with System.Diagnostics.DebuggerBrowsable(Never). 
+
+            let props =
+                ty.GetProperties(BindingFlags.GetField ||| BindingFlags.Instance ||| BindingFlags.Public)
+
+            let fields =
+                ty.GetFields(BindingFlags.Instance ||| BindingFlags.Public)
+                |> Array.map (fun i -> i :> MemberInfo)
+
+            let propsAndFields =
+                props
+                |> Array.map (fun i -> i :> MemberInfo)
+                |> Array.append fields
+                |> Array.filter (fun pi ->
+                    // check if property is annotated with System.Diagnostics.DebuggerBrowsable(Never).
                     // Its evaluation may have unexpected side effects and\or block printing.
                     match Seq.toArray (pi.GetCustomAttributes(typeof, false)) with
-                    | [|:? System.Diagnostics.DebuggerBrowsableAttribute as attr |] -> attr.State <> System.Diagnostics.DebuggerBrowsableState.Never
-                    | _ -> true
-                )
+                    | [| :? System.Diagnostics.DebuggerBrowsableAttribute as attr |] ->
+                        attr.State <> System.Diagnostics.DebuggerBrowsableState.Never
+                    | _ -> true)
 
-            // massively reign in deep printing of properties 
-            let nDepth = depthLim/10
+            // massively reign in deep printing of properties
+            let nDepth = depthLim / 10
 #if NETSTANDARD
-            Array.Sort(propsAndFields,{ new IComparer with member this.Compare(p1,p2) = compare p1.Name p2.Name } )
+            Array.Sort(
+                propsAndFields,
+                { new IComparer with
+                    member this.Compare(p1, p2) = compare p1.Name p2.Name
+                }
+            )
 #else
-            Array.Sort((propsAndFields :> Array),{ new System.Collections.IComparer with member this.Compare(p1,p2) = compare ((p1 :?> MemberInfo).Name) ((p2 :?> MemberInfo).Name) } )
+            Array.Sort(
+                (propsAndFields :> Array),
+                { new System.Collections.IComparer with
+                    member this.Compare(p1, p2) =
+                        compare ((p1 :?> MemberInfo).Name) ((p2 :?> MemberInfo).Name)
+                }
+            )
 #endif
 
-            if propsAndFields.Length = 0 || (nDepth <= 0) then basicL 
-            else basicL --- 
-                    (propsAndFields 
-                    |> Array.map 
-                    (fun m -> 
-                        ((if m :? FieldInfo then tagField m.Name else tagProperty m.Name),
-                            (try Some (nestedObjL nDepth Precedence.BracketIfTuple ((getProperty ty obj m.Name), ty)) 
-                                with _ -> 
-                                try Some (nestedObjL nDepth Precedence.BracketIfTuple ((getField obj (m :?> FieldInfo)), ty)) 
-                                with _ -> None)))
-                    |> Array.toList 
-                    |> makePropertiesL)
-
-        and reprL showMode depthLim prec repr x (* x could be null *) =
+            if propsAndFields.Length = 0 || (nDepth <= 0) then
+                basicL
+            else
+                basicL
+                --- (propsAndFields
+                     |> Array.map (fun m ->
+                         ((if m :? FieldInfo then
+                               tagField m.Name
+                           else
+                               tagProperty m.Name),
+                          (try
+                              Some(nestedObjL nDepth Precedence.BracketIfTuple ((getProperty ty obj m.Name), ty))
+                           with
+                           | _ ->
+                               try
+                                   Some(nestedObjL nDepth Precedence.BracketIfTuple ((getField obj (m :?> FieldInfo)), ty))
+                               with
+                               | _ -> None)))
+                     |> Array.toList
+                     |> makePropertiesL)
+
+        and reprL showMode depthLim prec repr x (* x could be null *)  =
             match repr with
-            | TupleValue (tupleType, vals) ->
-                tupleValueL depthLim prec vals tupleType
+            | TupleValue (tupleType, vals) -> tupleValueL depthLim prec vals tupleType
 
-            | RecordValue items -> 
-                recordValueL depthLim (Array.toList items)
+            | RecordValue items -> recordValueL depthLim (Array.toList items)
 
-            | UnionCaseValue (constr,recd) when // x is List. Note: "null" is never a valid list value. 
-                                                    x<>null && isListType (x.GetType()) ->
+            | UnionCaseValue (constr, recd) when // x is List. Note: "null" is never a valid list value.
+                x <> null && isListType (x.GetType())
+                ->
                 listValueL depthLim constr recd
 
-            | UnionCaseValue(unionCaseName, recd) ->
-                unionCaseValueL depthLim prec unionCaseName (Array.toList recd)
+            | UnionCaseValue (unionCaseName, recd) -> unionCaseValueL depthLim prec unionCaseName (Array.toList recd)
 
-            | ExceptionValue(exceptionType, recd) ->
-                fsharpExceptionL depthLim prec exceptionType (Array.toList recd)
+            | ExceptionValue (exceptionType, recd) -> fsharpExceptionL depthLim prec exceptionType (Array.toList recd)
 
-            | FunctionClosureValue closureType ->
-                functionClosureL showMode closureType
+            | FunctionClosureValue closureType -> functionClosureL showMode closureType
 
             | UnitValue ->
                 countNodes 1
@@ -1242,31 +1498,28 @@ module Display =
                 // If this is the root element, wrap the null with angle brackets
                 if depthLim = opts.PrintDepth - 1 then
                     wordL (tagText "")
-                else nullL
+                else
+                    nullL
 
             | ObjectValue obj ->
                 let ty = obj.GetType()
-                match obj with 
-                | :? string as s ->
-                    stringValueL s
 
-                | :? Array as arr ->
-                    arrayValueL depthLim arr
+                match obj with
+                | :? string as s -> stringValueL s
+
+                | :? Array as arr -> arrayValueL depthLim arr
 
-                | _ when isSetOrMapType ty ->
-                    mapSetValueL depthLim prec ty obj
+                | _ when isSetOrMapType ty -> mapSetValueL depthLim prec ty obj
 
-                | :? System.Collections.IEnumerable as ie ->
-                    sequenceValueL showMode depthLim prec ie
+                | :? System.Collections.IEnumerable as ie -> sequenceValueL showMode depthLim prec ie
 
-                | _ when showMode = ShowTopLevelBinding && typeUsesSystemObjectToString ty ->
-                    emptyL 
+                | _ when showMode = ShowTopLevelBinding && typeUsesSystemObjectToString ty -> emptyL
 
                 | :? Enum ->
                     countNodes 1
                     Layout.objL obj
 
-                | _ when opts.ShowProperties -> 
+                | _ when opts.ShowProperties ->
                     countNodes 1
                     objectValueWithPropertiesL depthLim (ty: Type) (obj: obj)
 
@@ -1275,108 +1528,131 @@ module Display =
                     // This buries an obj in the layout, rendered at squash time via a leafFormatter.
                     Layout.objL obj
 
-        member _.Format(showMode, x:'a, xty:Type) =
-            objL showMode opts.PrintDepth  Precedence.BracketIfTuple (x, xty)
+        member _.Format(showMode, x: 'a, xty: Type) =
+            objL showMode opts.PrintDepth Precedence.BracketIfTuple (x, xty)
 
-    let leafFormatter (opts:FormatOptions) (obj :obj) =
-        match obj with 
+    let leafFormatter (opts: FormatOptions) (obj: obj) =
+        match obj with
         | null -> tagKeyword "null"
-        | :? double as d -> 
-            let s = d.ToString(opts.FloatingPointFormat,opts.FormatProvider)
-            let t = 
-                if Double.IsNaN(d) then "nan"
-                elif Double.IsNegativeInfinity(d) then "-infinity"
-                elif Double.IsPositiveInfinity(d) then "infinity"
-                elif opts.FloatingPointFormat[0] = 'g'  && String.forall(fun c -> Char.IsDigit(c) || c = '-')  s
-                then s + ".0" 
-                else s
+        | :? double as d ->
+            let s = d.ToString(opts.FloatingPointFormat, opts.FormatProvider)
+
+            let t =
+                if Double.IsNaN(d) then
+                    "nan"
+                elif Double.IsNegativeInfinity(d) then
+                    "-infinity"
+                elif Double.IsPositiveInfinity(d) then
+                    "infinity"
+                elif opts.FloatingPointFormat[0] = 'g'
+                     && String.forall (fun c -> Char.IsDigit(c) || c = '-') s then
+                    s + ".0"
+                else
+                    s
+
             tagNumericLiteral t
 
-        | :? single as d -> 
+        | :? single as d ->
             let t =
-                (if Single.IsNaN(d) then "nan"
-                    elif Single.IsNegativeInfinity(d) then "-infinity"
-                    elif Single.IsPositiveInfinity(d) then "infinity"
-                    elif opts.FloatingPointFormat.Length >= 1 && opts.FloatingPointFormat[0] = 'g' 
-                    && float32(Int32.MinValue) < d && d < float32(Int32.MaxValue) 
-                    && float32(int32(d)) = d 
-                    then (Convert.ToInt32 d).ToString(opts.FormatProvider) + ".0"
-                    else d.ToString(opts.FloatingPointFormat,opts.FormatProvider)) 
+                (if Single.IsNaN(d) then
+                     "nan"
+                 elif Single.IsNegativeInfinity(d) then
+                     "-infinity"
+                 elif Single.IsPositiveInfinity(d) then
+                     "infinity"
+                 elif opts.FloatingPointFormat.Length >= 1
+                      && opts.FloatingPointFormat[0] = 'g'
+                      && float32 (Int32.MinValue) < d
+                      && d < float32 (Int32.MaxValue)
+                      && float32 (int32 (d)) = d then
+                     (Convert.ToInt32 d).ToString(opts.FormatProvider) + ".0"
+                 else
+                     d.ToString(opts.FloatingPointFormat, opts.FormatProvider))
                 + "f"
+
             tagNumericLiteral t
 
-        | :? decimal as d -> d.ToString("g",opts.FormatProvider) + "M" |> tagNumericLiteral
+        | :? decimal as d -> d.ToString("g", opts.FormatProvider) + "M" |> tagNumericLiteral
         | :? uint64 as d -> d.ToString(opts.FormatProvider) + "UL" |> tagNumericLiteral
-        | :? int64  as d -> d.ToString(opts.FormatProvider) + "L" |> tagNumericLiteral
-        | :? int32  as d -> d.ToString(opts.FormatProvider) |> tagNumericLiteral
+        | :? int64 as d -> d.ToString(opts.FormatProvider) + "L" |> tagNumericLiteral
+        | :? int32 as d -> d.ToString(opts.FormatProvider) |> tagNumericLiteral
         | :? uint32 as d -> d.ToString(opts.FormatProvider) + "u" |> tagNumericLiteral
-        | :? int16  as d -> d.ToString(opts.FormatProvider) + "s" |> tagNumericLiteral
+        | :? int16 as d -> d.ToString(opts.FormatProvider) + "s" |> tagNumericLiteral
         | :? uint16 as d -> d.ToString(opts.FormatProvider) + "us" |> tagNumericLiteral
-        | :? sbyte  as d -> d.ToString(opts.FormatProvider) + "y" |> tagNumericLiteral
-        | :? byte   as d -> d.ToString(opts.FormatProvider) + "uy" |> tagNumericLiteral
+        | :? sbyte as d -> d.ToString(opts.FormatProvider) + "y" |> tagNumericLiteral
+        | :? byte as d -> d.ToString(opts.FormatProvider) + "uy" |> tagNumericLiteral
         | :? nativeint as d -> d.ToString() + "n" |> tagNumericLiteral
-        | :? unativeint  as d -> d.ToString() + "un" |> tagNumericLiteral
-        | :? bool   as b -> (if b then "true" else "false") |> tagKeyword
-        | :? char   as c -> "\'" + formatChar true c + "\'" |> tagStringLiteral
+        | :? unativeint as d -> d.ToString() + "un" |> tagNumericLiteral
+        | :? bool as b -> (if b then "true" else "false") |> tagKeyword
+        | :? char as c -> "\'" + formatChar true c + "\'" |> tagStringLiteral
 
-        | _ -> 
-            let t = 
-                try 
+        | _ ->
+            let t =
+                try
                     let text = obj.ToString()
+
                     match text with
                     | null -> ""
                     | _ -> text
-                with e ->
+                with
+                | e ->
                     // If a .ToString() call throws an exception, catch it and use the message as the result.
                     // This may be informative, e.g. division by zero etc...
-                    "" 
+                    ""
+
             tagText t
 
     let any_to_layout options (value, typValue) =
-        let formatter = ObjectGraphFormatter(options, BindingFlags.Public) 
+        let formatter = ObjectGraphFormatter(options, BindingFlags.Public)
         formatter.Format(ShowAll, value, typValue)
 
-    let squashTo width layout = 
-       layout |> squashToAux (width, leafFormatter FormatOptions.Default)
+    let squashTo width layout =
+        layout |> squashToAux (width, leafFormatter FormatOptions.Default)
 
-    let squash_layout options layout = 
+    let squash_layout options layout =
         // Print width = 0 implies 1D layout, no squash
-        if options.PrintWidth = 0 then 
-            layout 
-        else 
-            layout |> squashToAux (options.PrintWidth,leafFormatter options)
+        if options.PrintWidth = 0 then
+            layout
+        else
+            layout |> squashToAux (options.PrintWidth, leafFormatter options)
 
     let asTaggedTextWriter (writer: TextWriter) =
         { new TaggedTextWriter with
             member _.Write(t) = writer.Write t.Text
-            member _.WriteLine() = writer.WriteLine() }
+            member _.WriteLine() = writer.WriteLine()
+        }
 
-    let output_layout_tagged options writer layout = 
-        layout |> squash_layout options 
-            |> outL options.AttributeProcessor (leafFormatter options) writer
+    let output_layout_tagged options writer layout =
+        layout
+        |> squash_layout options
+        |> outL options.AttributeProcessor (leafFormatter options) writer
 
-    let output_layout options writer layout = 
+    let output_layout options writer layout =
         output_layout_tagged options (asTaggedTextWriter writer) layout
 
-    let layout_to_string options layout = 
-        layout |> squash_layout options 
-            |> showL options ((leafFormatter options) >> toText)
+    let layout_to_string options layout =
+        layout
+        |> squash_layout options
+        |> showL options ((leafFormatter options) >> toText)
 
-    let output_any_ex opts oc x = x |> any_to_layout opts |> output_layout opts oc
+    let output_any_ex opts oc x =
+        x |> any_to_layout opts |> output_layout opts oc
 
-    let output_any writer x = output_any_ex FormatOptions.Default writer x
+    let output_any writer x =
+        output_any_ex FormatOptions.Default writer x
 
-    let layout_as_string options x = x |> any_to_layout options |> layout_to_string options
+    let layout_as_string options x =
+        x |> any_to_layout options |> layout_to_string options
 
-    let any_to_string x = layout_as_string FormatOptions.Default x
+    let any_to_string x =
+        layout_as_string FormatOptions.Default x
 
 #if COMPILER
     let fsi_any_to_layout options (value, typValue) =
-        let formatter = ObjectGraphFormatter(options, BindingFlags.Public) 
-        formatter.Format (ShowTopLevelBinding, value, typValue)
+        let formatter = ObjectGraphFormatter(options, BindingFlags.Public)
+        formatter.Format(ShowTopLevelBinding, value, typValue)
 #else
-    let internal anyToStringForPrintf options (bindingFlags:BindingFlags) (value, typValue) = 
-        let formatter = ObjectGraphFormatter(options, bindingFlags) 
-        formatter.Format (ShowAll, value, typValue) |> layout_to_string options
+    let internal anyToStringForPrintf options (bindingFlags: BindingFlags) (value, typValue) =
+        let formatter = ObjectGraphFormatter(options, bindingFlags)
+        formatter.Format(ShowAll, value, typValue) |> layout_to_string options
 #endif
-
diff --git a/src/Compiler/Utilities/sr.fs b/src/Compiler/Utilities/sr.fs
index dcb5f03e09c..245ed9841c2 100644
--- a/src/Compiler/Utilities/sr.fs
+++ b/src/Compiler/Utilities/sr.fs
@@ -1,18 +1,20 @@
 // Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information.
 
-
-
-namespace FSharp.Compiler 
+namespace FSharp.Compiler
 
 open Microsoft.FSharp.Core
 open Microsoft.FSharp.Collections
 open Microsoft.FSharp.Reflection
 
 module internal SR =
-    let private resources = lazy (System.Resources.ResourceManager("fsstrings", System.Reflection.Assembly.GetExecutingAssembly()))
-
-    let GetString(name:string) =
-        let s = resources.Force().GetString(name, System.Globalization.CultureInfo.CurrentUICulture)
+    let private resources =
+        lazy (System.Resources.ResourceManager("fsstrings", System.Reflection.Assembly.GetExecutingAssembly()))
+
+    let GetString (name: string) =
+        let s =
+            resources
+                .Force()
+                .GetString(name, System.Globalization.CultureInfo.CurrentUICulture)
 #if DEBUG
         if null = s then
             System.Diagnostics.Debug.Assert(false, sprintf "**RESOURCE ERROR**: Resource token %s does not exist!" name)
@@ -23,86 +25,88 @@ module internal DiagnosticMessage =
 
     open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
 
-    let mkFunctionValue (tys: System.Type[]) (impl:obj->obj) = 
-        FSharpValue.MakeFunction(FSharpType.MakeFunctionType(tys[0],tys[1]), impl)
+    let mkFunctionValue (tys: System.Type[]) (impl: obj -> obj) =
+        FSharpValue.MakeFunction(FSharpType.MakeFunctionType(tys[0], tys[1]), impl)
 
-    let funTyC = typeof obj>.GetGenericTypeDefinition()  
-    let mkFunTy a b = funTyC.MakeGenericType([| a;b |])
+    let funTyC = typeof obj>.GetGenericTypeDefinition ()
+    let mkFunTy a b = funTyC.MakeGenericType([| a; b |])
 
-    let isNamedType(ty:System.Type) = not (ty.IsArray ||  ty.IsByRef ||  ty.IsPointer)
-    let isFunctionType (ty1:System.Type)  = 
-        isNamedType(ty1) && ty1.IsGenericType && (ty1.GetGenericTypeDefinition()).Equals(funTyC)
+    let isNamedType (ty: System.Type) =
+        not (ty.IsArray || ty.IsByRef || ty.IsPointer)
 
-    let rec destFunTy (ty:System.Type) =
-        if isFunctionType ty then 
-            ty, ty.GetGenericArguments() 
+    let isFunctionType (ty1: System.Type) =
+        isNamedType (ty1)
+        && ty1.IsGenericType
+        && (ty1.GetGenericTypeDefinition()).Equals(funTyC)
+
+    let rec destFunTy (ty: System.Type) =
+        if isFunctionType ty then
+            ty, ty.GetGenericArguments()
         else
-            match ty.BaseType with 
-            | null -> failwith "destFunTy: not a function type" 
-            | b -> destFunTy b 
+            match ty.BaseType with
+            | null -> failwith "destFunTy: not a function type"
+            | b -> destFunTy b
 
-    let buildFunctionForOneArgPat (ty: System.Type) impl = 
-        let _,tys = destFunTy ty 
+    let buildFunctionForOneArgPat (ty: System.Type) impl =
+        let _, tys = destFunTy ty
         let rty = tys[1]
-        // PERF: this technique is a bit slow (e.g. in simple cases, like 'sprintf "%x"') 
+        // PERF: this technique is a bit slow (e.g. in simple cases, like 'sprintf "%x"')
         mkFunctionValue tys (fun inp -> impl rty inp)
-                
-    let capture1 (fmt:string) i args ty (go : obj list -> System.Type -> int -> obj) : obj = 
+
+    let capture1 (fmt: string) i args ty (go: obj list -> System.Type -> int -> obj) : obj =
         match fmt[i] with
-        | '%' -> go args ty (i+1) 
+        | '%' -> go args ty (i + 1)
         | 'd'
         | 'f'
-        | 's' -> buildFunctionForOneArgPat ty (fun rty n -> go (n :: args) rty (i+1))
+        | 's' -> buildFunctionForOneArgPat ty (fun rty n -> go (n :: args) rty (i + 1))
         | _ -> failwith "bad format specifier"
 
     // newlines and tabs get converted to strings when read from a resource file
-    // this will preserve their original intention    
-    let postProcessString (s : string) =
-        s.Replace("\\n","\n").Replace("\\t","\t")
+    // this will preserve their original intention
+    let postProcessString (s: string) =
+        s.Replace("\\n", "\n").Replace("\\t", "\t")
 
-    let createMessageString (messageString : string) (fmt : Printf.StringFormat<'T>) : 'T = 
+    let createMessageString (messageString: string) (fmt: Printf.StringFormat<'T>) : 'T =
         let fmt = fmt.Value // here, we use the actual error string, as opposed to the one stored as fmt
-        let len = fmt.Length 
+        let len = fmt.Length
 
         /// Function to capture the arguments and then run.
-        let rec capture args ty i = 
-            if i >= len ||  (fmt[i] = '%' && i+1 >= len) then 
-                let b = System.Text.StringBuilder()    
+        let rec capture args ty i =
+            if i >= len || (fmt[i] = '%' && i + 1 >= len) then
+                let b = System.Text.StringBuilder()
                 b.AppendFormat(messageString, (Array.ofList (List.rev args))) |> ignore
-                box(b.ToString())
+                box (b.ToString())
             // REVIEW: For these purposes, this should be a nop, but I'm leaving it
             // in case we ever decide to support labels for the error format string
             // E.g., "%s%d"
-            elif System.Char.IsSurrogatePair(fmt,i) then 
-               capture args ty (i+2)
+            elif System.Char.IsSurrogatePair(fmt, i) then
+                capture args ty (i + 2)
             else
                 match fmt[i] with
                 | '%' ->
-                    let i = i+1 
+                    let i = i + 1
                     capture1 fmt i args ty capture
-                | _ ->
-                    capture args ty (i+1) 
+                | _ -> capture args ty (i + 1)
 
-        (unbox (capture [] typeof<'T> 0) : 'T)
+        (unbox (capture [] typeof<'T> 0): 'T)
 
-    type ResourceString<'T>(fmtString : string, fmt : Printf.StringFormat<'T>) =
-        member _.Format =
-            createMessageString fmtString fmt
+    type ResourceString<'T>(fmtString: string, fmt: Printf.StringFormat<'T>) =
+        member _.Format = createMessageString fmtString fmt
 
-    let DeclareResourceString (messageID : string,fmt : Printf.StringFormat<'T>) =
+    let DeclareResourceString (messageID: string, fmt: Printf.StringFormat<'T>) =
         let mutable messageString = SR.GetString(messageID)
 #if DEBUG
         // validate that the message string exists
         let fmtString = fmt.Value
-        
+
         if null = messageString then
             System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** String resource %s does not exist" messageID)
             messageString <- ""
-        
+
         // validate the formatting specifiers
-        let countFormatHoles (s : string) =
+        let countFormatHoles (s: string) =
             // remove escaped format holes
-            let s = s.Replace("{{","").Replace("}}","")
+            let s = s.Replace("{{", "").Replace("}}", "")
             let len = s.Length - 2
             let mutable pos = 0
             let mutable nHoles = 0
@@ -110,50 +114,63 @@ module internal DiagnosticMessage =
 
             while pos < len do
                 if s[pos] = '{' then
-                    let mutable pos' = pos+1
+                    let mutable pos' = pos + 1
+
                     while System.Char.IsNumber(s[pos']) do
                         pos' <- pos' + 1
-                    if pos' > pos+1 && s[pos'] = '}' then
+
+                    if pos' > pos + 1 && s[pos'] = '}' then
                         nHoles <- nHoles + 1
-                        let ordern = int s[(pos+1) .. (pos'-1)]
+                        let ordern = int s[(pos + 1) .. (pos' - 1)]
                         order <- order.Add(ordern)
                         pos <- pos'
+
                 pos <- pos + 1
             // the sort should be unnecessary, but better safe than sorry
-            nHoles,Set.toList order |> List.sortDescending
+            nHoles, Set.toList order |> List.sortDescending
 
-        let countFormatPlaceholders (s : string) =
+        let countFormatPlaceholders (s: string) =
             // strip any escaped % characters - yes, this will fail if given %%%...
-            let s = s.Replace("%%","")
-            
-            if s = "" then 
+            let s = s.Replace("%%", "")
+
+            if s = "" then
                 0
             else
                 let len = s.Length - 1
                 let mutable pos = 0
                 let mutable nFmt = 0
-            
+
                 while pos < len do
-                    if s[pos] = '%' && 
-                      (s[pos+1] = 'd' || s[pos+1] = 's' || s[pos+1] = 'f') then
+                    if s[pos] = '%' && (s[pos + 1] = 'd' || s[pos + 1] = 's' || s[pos + 1] = 'f') then
                         nFmt <- nFmt + 1
-                        pos <- pos + 2 ;
+                        pos <- pos + 2
                     else
-                        pos <- pos + 1 ;
+                        pos <- pos + 1
+
                 nFmt
-                
-        let nHoles,holes = countFormatHoles messageString
+
+        let nHoles, holes = countFormatHoles messageString
         let nPlaceholders = countFormatPlaceholders fmtString
-        
-        // first, verify that the number of holes in the message string does not exceed the 
+
+        // first, verify that the number of holes in the message string does not exceed the
         // largest hole reference
         if holes <> [] && holes[0] > nHoles - 1 then
-            System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but references hole %d" messageID nHoles holes[0])
-            
+            System.Diagnostics.Debug.Assert(
+                false,
+                sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but references hole %d" messageID nHoles holes[0]
+            )
+
         // next, verify that the number of format placeholders is the same as the number of holes
         if nHoles <> nPlaceholders then
-            System.Diagnostics.Debug.Assert(false, sprintf "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but its format specifier contains %d placeholders" messageID nHoles nPlaceholders)
-            
-#endif            
-        messageString <- postProcessString messageString                            
+            System.Diagnostics.Debug.Assert(
+                false,
+                sprintf
+                    "**DECLARED MESSAGE ERROR** Message string %s contains %d holes, but its format specifier contains %d placeholders"
+                    messageID
+                    nHoles
+                    nPlaceholders
+            )
+
+#endif
+        messageString <- postProcessString messageString
         new ResourceString<'T>(messageString, fmt)
diff --git a/src/Compiler/Utilities/zmap.fs b/src/Compiler/Utilities/zmap.fs
index e1cd0eccd90..03bc085ab8a 100644
--- a/src/Compiler/Utilities/zmap.fs
+++ b/src/Compiler/Utilities/zmap.fs
@@ -6,41 +6,49 @@ open Internal.Utilities.Collections.Tagged
 open System.Collections.Generic
 
 /// Maps with a specific comparison function
-type internal Zmap<'Key,'T> = Internal.Utilities.Collections.Tagged.Map<'Key,'T> 
+type internal Zmap<'Key, 'T> = Internal.Utilities.Collections.Tagged.Map<'Key, 'T>
 
-module internal Zmap = 
+module internal Zmap =
 
-    let empty (ord: IComparer<'T>) = Map<_,_,_>.Empty(ord)
+    let empty (ord: IComparer<'T>) = Map<_, _, _>.Empty (ord)
 
-    let add k v (m: Zmap<_,_>) = m.Add(k,v)
-    let find k (m: Zmap<_,_>) = m[k]
-    let tryFind k (m: Zmap<_,_>) = m.TryFind(k)
-    let remove k (m: Zmap<_,_>) = m.Remove(k)
-    let mem k (m: Zmap<_,_>) = m.ContainsKey(k)
-    let iter action (m: Zmap<_,_>) = m.Iterate(action)
-    let first f (m: Zmap<_,_>) = m.First(fun k v -> if f k v then Some (k,v) else None)
-    let exists f (m: Zmap<_,_>) = m.Exists(f)
-    let forall f (m: Zmap<_,_>) = m.ForAll(f)
-    let map mapping (m: Zmap<_,_>) = m.MapRange(mapping)
-    let mapi mapping (m: Zmap<_,_>) = m.Map(mapping)
-    let fold f (m: Zmap<_,_>) x = m.Fold f x
-    let toList (m: Zmap<_,_>) = m.ToList()
-    let foldSection lo hi f (m: Zmap<_,_>) x = m.FoldSection lo hi f x
+    let add k v (m: Zmap<_, _>) = m.Add(k, v)
+    let find k (m: Zmap<_, _>) = m[k]
+    let tryFind k (m: Zmap<_, _>) = m.TryFind(k)
+    let remove k (m: Zmap<_, _>) = m.Remove(k)
+    let mem k (m: Zmap<_, _>) = m.ContainsKey(k)
+    let iter action (m: Zmap<_, _>) = m.Iterate(action)
 
-    let isEmpty (m: Zmap<_,_>) = m.IsEmpty
+    let first f (m: Zmap<_, _>) =
+        m.First(fun k v -> if f k v then Some(k, v) else None)
 
-    let foldMap f z (m: Zmap<_,_>) =
-      let m,z = m.FoldAndMap (fun k v z -> let z,v' = f z k v in v',z) z in
-      z,m
+    let exists f (m: Zmap<_, _>) = m.Exists(f)
+    let forall f (m: Zmap<_, _>) = m.ForAll(f)
+    let map mapping (m: Zmap<_, _>) = m.MapRange(mapping)
+    let mapi mapping (m: Zmap<_, _>) = m.Map(mapping)
+    let fold f (m: Zmap<_, _>) x = m.Fold f x
+    let toList (m: Zmap<_, _>) = m.ToList()
+    let foldSection lo hi f (m: Zmap<_, _>) x = m.FoldSection lo hi f x
 
-    let choose f  (m: Zmap<_,_>) = m.First(f)
+    let isEmpty (m: Zmap<_, _>) = m.IsEmpty
 
-    let chooseL f  (m: Zmap<_,_>) =
-      m.Fold (fun k v s -> match f k v with None -> s | Some x -> x :: s) []
+    let foldMap f z (m: Zmap<_, _>) =
+        let m, z = m.FoldAndMap (fun k v z -> let z, v' = f z k v in v', z) z in z, m
 
-    let ofList ord xs = Internal.Utilities.Collections.Tagged.Map<_,_>.FromList(ord,xs)
+    let choose f (m: Zmap<_, _>) = m.First(f)
 
-    let keys   (m: Zmap<_,_>) = m.Fold (fun k _ s -> k :: s) []
-    let values (m: Zmap<_,_>) = m.Fold (fun _ v s -> v :: s) []
+    let chooseL f (m: Zmap<_, _>) =
+        m.Fold
+            (fun k v s ->
+                match f k v with
+                | None -> s
+                | Some x -> x :: s)
+            []
+
+    let ofList ord xs =
+        Internal.Utilities.Collections.Tagged.Map<_, _>.FromList (ord, xs)
+
+    let keys (m: Zmap<_, _>) = m.Fold (fun k _ s -> k :: s) []
+    let values (m: Zmap<_, _>) = m.Fold (fun _ v s -> v :: s) []
 
     let memberOf m k = mem k m
diff --git a/src/Compiler/Utilities/zset.fs b/src/Compiler/Utilities/zset.fs
index c8314294108..cc53d4c7006 100644
--- a/src/Compiler/Utilities/zset.fs
+++ b/src/Compiler/Utilities/zset.fs
@@ -8,9 +8,10 @@ open System.Collections.Generic
 /// Sets with a specific comparison function
 type internal Zset<'T> = Internal.Utilities.Collections.Tagged.Set<'T>
 
-module internal Zset = 
+module internal Zset =
 
-    let empty (ord : IComparer<'T>) = Internal.Utilities.Collections.Tagged.Set<_,_>.Empty(ord)
+    let empty (ord: IComparer<'T>) =
+        Internal.Utilities.Collections.Tagged.Set<_, _>.Empty (ord)
 
     let isEmpty (s: Zset<_>) = s.IsEmpty
 
@@ -19,33 +20,37 @@ module internal Zset =
     let add x (s: Zset<_>) = s.Add(x)
 
     let addList xs a = List.fold (fun a x -> add x a) a xs
-        
+
     let singleton ord x = add x (empty ord)
 
     let remove x (s: Zset<_>) = s.Remove(x)
 
-    let fold (f : 'T -> 'b -> 'b) (s: Zset<_>) b = s.Fold f b
+    let fold (f: 'T -> 'b -> 'b) (s: Zset<_>) b = s.Fold f b
 
-    let iter f (s: Zset<_>) = s.Iterate f 
+    let iter f (s: Zset<_>) = s.Iterate f
 
-    let forall predicate (s: Zset<_>) = s.ForAll predicate 
+    let forall predicate (s: Zset<_>) = s.ForAll predicate
 
-    let count  (s: Zset<_>) = s.Count
+    let count (s: Zset<_>) = s.Count
 
     let exists predicate (s: Zset<_>) = s.Exists predicate
 
-    let subset (s1: Zset<_>) (s2: Zset<_>)  = s1.IsSubsetOf s2
+    let subset (s1: Zset<_>) (s2: Zset<_>) = s1.IsSubsetOf s2
 
-    let equal (s1: Zset<_>) (s2: Zset<_>)  = Internal.Utilities.Collections.Tagged.Set<_,_>.Equality(s1,s2)
+    let equal (s1: Zset<_>) (s2: Zset<_>) =
+        Internal.Utilities.Collections.Tagged.Set<_, _>.Equality (s1, s2)
 
     let elements (s: Zset<_>) = s.ToList()
 
     let filter predicate (s: Zset<_>) = s.Filter predicate
 
-    let union (s1: Zset<_>) (s2: Zset<_>)  = Internal.Utilities.Collections.Tagged.Set<_,_>.Union(s1,s2)
+    let union (s1: Zset<_>) (s2: Zset<_>) =
+        Internal.Utilities.Collections.Tagged.Set<_, _>.Union (s1, s2)
 
-    let inter (s1: Zset<_>) (s2: Zset<_>)  = Internal.Utilities.Collections.Tagged.Set<_,_>.Intersection(s1,s2)
+    let inter (s1: Zset<_>) (s2: Zset<_>) =
+        Internal.Utilities.Collections.Tagged.Set<_, _>.Intersection (s1, s2)
 
-    let diff (s1: Zset<_>) (s2: Zset<_>)  = Internal.Utilities.Collections.Tagged.Set<_,_>.Difference(s1,s2)
+    let diff (s1: Zset<_>) (s2: Zset<_>) =
+        Internal.Utilities.Collections.Tagged.Set<_, _>.Difference (s1, s2)
 
     let memberOf m k = contains k m

From 98597e9f3d60960aefbac363e0b1886b74c02959 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 23 May 2022 18:04:41 +0100
Subject: [PATCH 05/20] reactivate test (#13179)

---
 tests/fsharp/tests.fs | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tests/fsharp/tests.fs b/tests/fsharp/tests.fs
index d6cb9b289d6..a5a8ba6d133 100644
--- a/tests/fsharp/tests.fs
+++ b/tests/fsharp/tests.fs
@@ -1587,9 +1587,11 @@ module CoreTests =
     []
     let ``patterns-FSC_OPTIMIZED`` () = singleTestBuildAndRunVersion "core/patterns" FSC_OPTIMIZED "preview"
 
-//BUGBUG: https://github.com/dotnet/fsharp/issues/6601
-//    []
-//    let ``patterns-FSI`` () = singleTestBuildAndRun' "core/patterns" FSI
+// This requires --multiemit on by default, which is not the case for .NET Framework
+#if NETCOREAPP
+    []
+    let ``patterns-FSI`` () = singleTestBuildAndRun "core/patterns" FSI
+#endif
 
     []
     let ``pinvoke-FSC_OPTIMIZED`` () = singleTestBuildAndRun "core/pinvoke" FSC_OPTIMIZED

From 5fb045c7d93cb61f33b7fc9bdfe07f6f41663cc6 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 23 May 2022 18:11:52 +0100
Subject: [PATCH 06/20] simplify diff (#12956)

---
 src/FSharp.Core/prim-types.fs | 91 ++++++++++++++++++++++++-----------
 src/FSharp.Core/printf.fs     | 13 ++++-
 src/FSharp.Core/seqcore.fs    | 29 ++++++++++-
 3 files changed, 101 insertions(+), 32 deletions(-)

diff --git a/src/FSharp.Core/prim-types.fs b/src/FSharp.Core/prim-types.fs
index 907d34488ac..c3df910fafd 100644
--- a/src/FSharp.Core/prim-types.fs
+++ b/src/FSharp.Core/prim-types.fs
@@ -3412,71 +3412,104 @@ namespace Microsoft.FSharp.Core
 
         // Note: this is not made public in the signature, because of conflicts with the Converter overload.
         // The method remains in case someone is calling it via reflection.
-        static member op_Implicit(converter: Func<_,_>) : ('T -> 'Res) =  (fun t -> converter.Invoke(t))
+        static member op_Implicit(converter : Func<_,_>) : ('T -> 'Res) =
+            (fun t -> converter.Invoke(t))
 
         // Note: this is not made public in the signature, because of conflicts with the Converter overload.
         // The method remains in case someone is calling it via reflection.
-        static member op_Implicit(func: ('T -> 'Res) ) =  new Func<'T,'Res>(func)
+        static member op_Implicit(func : ('T -> 'Res) ) =
+            new Func<'T,'Res>(func)
 
-        static member op_Implicit(f: Converter<_,_>) : ('T -> 'Res) =  (fun t -> f.Invoke(t))
+        static member op_Implicit(f : Converter<_,_>) : ('T -> 'Res) =
+            (fun t -> f.Invoke(t))
 
-        static member op_Implicit (func: ('T -> 'Res) ) =  new Converter<'T,'Res>(func)
+        static member op_Implicit (func : ('T -> 'Res) ) =
+            new Converter<'T,'Res>(func)
 
-        static member FromConverter (converter: Converter<_,_>) : ('T -> 'Res) =  (fun t -> converter.Invoke(t))
+        static member FromConverter (converter: Converter<_,_>) : ('T -> 'Res) =
+            (fun t -> converter.Invoke(t))
 
-        static member ToConverter (func: ('T -> 'Res) ) =  new Converter<'T,'Res>(func)
+        static member ToConverter (func: ('T -> 'Res) ) =
+            new Converter<'T,'Res>(func)
 
-        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res)                   = OptimizedClosures.invokeFast2(func, arg1, arg2) 
+        []
+        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res) =
+            OptimizedClosures.invokeFast2(func, arg1, arg2) 
 
-        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res, arg3)             = OptimizedClosures.invokeFast3(func, arg1, arg2, arg3)
+        []
+        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res, arg3) =
+            OptimizedClosures.invokeFast3(func, arg1, arg2, arg3)
 
-        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res, arg3, arg4)       = OptimizedClosures.invokeFast4(func, arg1, arg2, arg3, arg4)
+        []
+        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res, arg3, arg4) =
+            OptimizedClosures.invokeFast4(func, arg1, arg2, arg3, arg4)
 
-        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res, arg3, arg4, arg5) = OptimizedClosures.invokeFast5(func, arg1, arg2, arg3, arg4, arg5)
+        []
+        static member InvokeFast (func:FSharpFunc<_,_>, arg1: 'T, arg2: 'Res, arg3, arg4, arg5) =
+            OptimizedClosures.invokeFast5(func, arg1, arg2, arg3, arg4, arg5)
 
     []
     []
     type FuncConvert = 
 
-        static member  inline ToFSharpFunc (action: Action<_>) = (fun t -> action.Invoke(t))
+        static member inline ToFSharpFunc (action: Action<_>) =
+            (fun t -> action.Invoke(t))
 
-        static member  inline ToFSharpFunc (converter : Converter<_,_>) = (fun t -> converter.Invoke(t))
+        static member inline ToFSharpFunc (converter : Converter<_,_>) =
+            (fun t -> converter.Invoke(t))
 
         // Note: this is not made public in the signature, because of conflicts with the Converter overload.
         // The method remains in case someone is calling it via reflection.
-        static member  inline ToFSharpFunc (converter: Func<_, _>) = (fun t -> converter.Invoke(t))
+        static member inline ToFSharpFunc (converter: Func<_, _>) =
+            (fun t -> converter.Invoke(t))
 
-        static member  inline FromFunc (func: Func<_>) = (fun () -> func.Invoke())
+        static member inline FromFunc (func: Func<_>) =
+            (fun () -> func.Invoke())
 
-        static member  inline FromFunc (func: Func<_, _>) = (fun t -> func.Invoke(t))
+        static member inline FromFunc (func: Func<_, _>) =
+            (fun t -> func.Invoke(t))
 
-        static member  inline FromFunc (func: Func<_, _, _>) = (fun t1 t2 -> func.Invoke(t1,t2))
+        static member inline FromFunc (func: Func<_, _, _>) =
+            (fun t1 t2 -> func.Invoke(t1,t2))
 
-        static member  inline FromFunc (func: Func<_, _, _, _>) = (fun t1 t2 t3 -> func.Invoke(t1,t2,t3))
+        static member inline FromFunc (func: Func<_, _, _, _>) =
+            (fun t1 t2 t3 -> func.Invoke(t1,t2,t3))
 
-        static member  inline FromFunc (func: Func<_, _, _, _, _>) = (fun t1 t2 t3 t4 -> func.Invoke(t1,t2,t3,t4))
+        static member inline FromFunc (func: Func<_, _, _, _, _>) =
+            (fun t1 t2 t3 t4 -> func.Invoke(t1,t2,t3,t4))
 
-        static member  inline FromFunc (func: Func<_, _, _, _, _, _>) = (fun t1 t2 t3 t4 t5 -> func.Invoke(t1,t2,t3,t4,t5))
+        static member inline FromFunc (func: Func<_, _, _, _, _, _>) =
+            (fun t1 t2 t3 t4 t5 -> func.Invoke(t1,t2,t3,t4,t5))
 
-        static member  inline FromAction (action: Action) = (fun () -> action.Invoke())
+        static member inline FromAction (action: Action) =
+            (fun () -> action.Invoke())
 
-        static member  inline FromAction (action: Action<_>) = (fun t -> action.Invoke(t))
+        static member inline FromAction (action: Action<_>) =
+            (fun t -> action.Invoke(t))
 
-        static member  inline FromAction (action: Action<_, _>) = (fun t1 t2 -> action.Invoke(t1,t2))
+        static member inline FromAction (action: Action<_, _>) =
+            (fun t1 t2 -> action.Invoke(t1,t2))
 
-        static member  inline FromAction (action: Action<_, _, _>) = (fun t1 t2 t3 -> action.Invoke(t1,t2,t3))
+        static member inline FromAction (action: Action<_, _, _>) =
+            (fun t1 t2 t3 -> action.Invoke(t1,t2,t3))
 
-        static member  inline FromAction (action: Action<_, _, _, _>) = (fun t1 t2 t3 t4 -> action.Invoke(t1,t2,t3,t4))
+        static member inline FromAction (action: Action<_, _, _, _>) =
+            (fun t1 t2 t3 t4 -> action.Invoke(t1,t2,t3,t4))
 
-        static member  inline FromAction (action: Action<_, _, _, _, _>) = (fun t1 t2 t3 t4 t5 -> action.Invoke(t1,t2,t3,t4,t5))
+        static member inline FromAction (action: Action<_, _, _, _, _>) =
+            (fun t1 t2 t3 t4 t5 -> action.Invoke(t1,t2,t3,t4,t5))
 
-        static member inline FuncFromTupled (func: 'T1 * 'T2 -> 'Res) = (fun a b -> func (a, b))
+        static member inline FuncFromTupled (func: 'T1 * 'T2 -> 'Res) =
+            (fun a b -> func (a, b))
 
-        static member inline FuncFromTupled (func: 'T1 * 'T2 * 'T3 -> 'Res) = (fun a b c -> func (a, b, c))
+        static member inline FuncFromTupled (func: 'T1 * 'T2 * 'T3 -> 'Res) =
+            (fun a b c -> func (a, b, c))
 
-        static member inline FuncFromTupled (func: 'T1 * 'T2 * 'T3 * 'T4 -> 'Res) = (fun a b c d -> func (a, b, c, d))
+        static member inline FuncFromTupled (func: 'T1 * 'T2 * 'T3 * 'T4 -> 'Res) =
+            (fun a b c d -> func (a, b, c, d))
 
-        static member inline FuncFromTupled (func: 'T1 * 'T2 * 'T3 * 'T4 * 'T5 -> 'Res) = (fun a b c d e -> func (a, b, c, d, e))
+        static member inline FuncFromTupled (func: 'T1 * 'T2 * 'T3 * 'T4 * 'T5 -> 'Res) =
+            (fun a b c d e -> func (a, b, c, d, e))
 
     //-------------------------------------------------------------------------
     // Refs
diff --git a/src/FSharp.Core/printf.fs b/src/FSharp.Core/printf.fs
index 9d4d69e6664..58cab425d1f 100644
--- a/src/FSharp.Core/printf.fs
+++ b/src/FSharp.Core/printf.fs
@@ -7,6 +7,7 @@ open System.IO
 open System.Text
 
 open System.Collections.Concurrent
+open System.Diagnostics
 open System.Globalization
 open System.Reflection
 
@@ -16,8 +17,11 @@ open Microsoft.FSharp.Collections
 
 open LanguagePrimitives.IntrinsicOperators
 
-type PrintfFormat<'Printer, 'State, 'Residue, 'Result>(value:string, captures: obj[], captureTys: Type[]) =
+type PrintfFormat<'Printer, 'State, 'Residue, 'Result>
+        []
+        (value:string, captures: obj[], captureTys: Type[]) =
         
+    []
     new (value) = new PrintfFormat<'Printer, 'State, 'Residue, 'Result>(value, null, null) 
 
     member _.Value = value
@@ -28,10 +32,13 @@ type PrintfFormat<'Printer, 'State, 'Residue, 'Result>(value:string, captures: o
 
     override _.ToString() = value
     
-type PrintfFormat<'Printer, 'State, 'Residue, 'Result, 'Tuple>(value:string, captures, captureTys: Type[]) = 
+type PrintfFormat<'Printer, 'State, 'Residue, 'Result, 'Tuple>
+         []
+         (value:string, captures, captureTys: Type[]) = 
 
     inherit PrintfFormat<'Printer, 'State, 'Residue, 'Result>(value, captures, captureTys)
 
+    []
     new (value) = new PrintfFormat<'Printer, 'State, 'Residue, 'Result, 'Tuple>(value, null, null)
 
 type Format<'Printer, 'State, 'Residue, 'Result> = PrintfFormat<'Printer, 'State, 'Residue, 'Result>
@@ -1396,6 +1403,8 @@ module Printf =
     let ksprintf continuation (format: StringFormat<'T, 'Result>) : 'T = 
         gprintf (fun stringCount -> LargeStringPrintfEnv(continuation, stringCount)) format
 
+    // Note: this compiled name is wrong - it should be PrintFormatToString
+    // however for binary compat we do not change this.
     []
     let sprintf (format: StringFormat<'T>) =
         // We inline gprintf by hand here to be sure to remove a few allocations
diff --git a/src/FSharp.Core/seqcore.fs b/src/FSharp.Core/seqcore.fs
index 6a2e81061cc..2b2b3b9e55b 100644
--- a/src/FSharp.Core/seqcore.fs
+++ b/src/FSharp.Core/seqcore.fs
@@ -28,7 +28,10 @@ module internal IEnumerator =
 
           interface IEnumerator with
               member _.Current = unbox<'T> e.Current :> obj
+
+              []
               member _.MoveNext() = e.MoveNext()
+
               member _.Reset() = noReset()
 
           interface System.IDisposable with
@@ -51,6 +54,7 @@ module internal IEnumerator =
                 check started
                 (alreadyFinished() : obj)
 
+            []
             member _.MoveNext() =
                 if not started then started <- true
                 false
@@ -107,6 +111,8 @@ module internal IEnumerator =
 
         interface IEnumerator with
             member _.Current = box (getCurr())
+
+            []
             member _.MoveNext() =
                 start()
                 match state.Value with
@@ -132,7 +138,10 @@ module internal IEnumerator =
 
         interface IEnumerator with
             member _.Current = box v
+
+            []
             member _.MoveNext() = if started then false else (started <- true; true)
+
             member _.Reset() = noReset()
 
         interface System.IDisposable with
@@ -146,7 +155,10 @@ module internal IEnumerator =
 
           interface IEnumerator with
               member _.Current = (e :> IEnumerator).Current
+
+              []
               member _.MoveNext() = e.MoveNext()
+
               member _.Reset() = noReset()
 
           interface System.IDisposable with
@@ -188,6 +200,7 @@ module RuntimeHelpers =
     []
     type internal StructBox<'T when 'T:equality>(value:'T) =
         member x.Value = value
+
         static member Comparer =
             let gcomparer = HashIdentity.Structural<'T>
             { new IEqualityComparer> with
@@ -294,6 +307,7 @@ module RuntimeHelpers =
         interface IEnumerator with
             member x.Current = box (x.GetCurrent())
 
+            []
             member x.MoveNext() =
                if not started then started <- true
                if finished then false
@@ -329,6 +343,8 @@ module RuntimeHelpers =
             member _.Reset() = IEnumerator.noReset()
 
         interface System.IDisposable with
+
+            []
             member x.Dispose() =
                 if not finished then
                     x.Finish()
@@ -353,8 +369,11 @@ module RuntimeHelpers =
            (mkSeq (fun () ->
                 { new IEnumerator<_> with
                       member x.Current = getCurr()
+
                    interface IEnumerator with
                       member x.Current = box (getCurr())
+
+                      []
                       member x.MoveNext() =
                            start()
                            let keepGoing = (try guard() with e -> finish (); reraise ()) in
@@ -362,7 +381,9 @@ module RuntimeHelpers =
                                curr <- Some(source); true
                            else
                                finish(); false
+
                       member x.Reset() = IEnumerator.noReset()
+
                    interface System.IDisposable with
                       member x.Dispose() = () }))
 
@@ -414,9 +435,14 @@ type GeneratedSequenceBase<'T>() =
                  redirectTo <-
                        { new GeneratedSequenceBase<'T>() with
                              member x.GetFreshEnumerator() = e
+
+                             []
                              member x.GenerateNext(_) = if e.MoveNext() then 1 else 0
+
                              member x.Close() = try e.Dispose() finally active.Close()
+
                              member x.CheckClose = true
+
                              member x.LastGenerated = e.Current }
              redirect <- true
              x.MoveNextImpl()
@@ -436,9 +462,10 @@ type GeneratedSequenceBase<'T>() =
         member x.Dispose() = if redirect then redirectTo.Close() else x.Close()
 
     interface IEnumerator with
+
         member x.Current = box (if redirect then redirectTo.LastGenerated else x.LastGenerated)
 
-        //[]
+        []
         member x.MoveNext() = x.MoveNextImpl()
 
         member _.Reset() = raise <| new System.NotSupportedException()

From e9cc848541e0d635fdd622c2b75fe945805decda Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 23 May 2022 18:30:40 +0100
Subject: [PATCH 07/20] error for deprecated feature (#13182)

---
 src/Compiler/Checking/CheckExpressions.fs    | 12 ++++++++++--
 src/Compiler/Checking/CheckPatterns.fs       |  6 +++++-
 src/Compiler/FSComp.txt                      |  1 +
 src/Compiler/Facilities/LanguageFeatures.fs  |  3 +++
 src/Compiler/Facilities/LanguageFeatures.fsi |  1 +
 src/Compiler/xlf/FSComp.txt.cs.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.de.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.es.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.fr.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.it.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.ja.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.ko.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.pl.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.pt-BR.xlf        |  5 +++++
 src/Compiler/xlf/FSComp.txt.ru.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.tr.xlf           |  5 +++++
 src/Compiler/xlf/FSComp.txt.zh-Hans.xlf      |  5 +++++
 src/Compiler/xlf/FSComp.txt.zh-Hant.xlf      |  5 +++++
 18 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs
index 42710ee52d6..1d9d9da70fa 100644
--- a/src/Compiler/Checking/CheckExpressions.fs
+++ b/src/Compiler/Checking/CheckExpressions.fs
@@ -2101,7 +2101,11 @@ let BuildFieldMap (cenv: cenv) env isPartial ty flds m =
                     if Map.containsKey fref2.FieldName fs then
                         errorR (Error(FSComp.SR.tcFieldAppearsTwiceInRecord(fref2.FieldName), m))
                     if showDeprecated then
-                        warning(Deprecated(FSComp.SR.nrRecordTypeNeedsQualifiedAccess(fref2.FieldName, fref2.Tycon.DisplayName) |> snd, m))
+                        let diagnostic = Deprecated(FSComp.SR.nrRecordTypeNeedsQualifiedAccess(fref2.FieldName, fref2.Tycon.DisplayName) |> snd, m)
+                        if g.langVersion.SupportsFeature(LanguageFeature.ErrorOnDeprecatedRequireQualifiedAccess) then
+                            errorR(diagnostic)
+                        else
+                            warning(diagnostic)
 
                     if not (tyconRefEq g tcref fref2.TyconRef) then
                         let _, frefSet1, _ = List.head fldResolutions
@@ -2127,7 +2131,11 @@ let rec ApplyUnionCaseOrExn (makerForUnionCase, makerForExnTag) m (cenv: cenv) e
 
     | Item.UnionCase(ucinfo, showDeprecated) ->
         if showDeprecated then
-            warning(Deprecated(FSComp.SR.nrUnionTypeNeedsQualifiedAccess(ucinfo.DisplayName, ucinfo.Tycon.DisplayName) |> snd, m))
+            let diagnostic = Deprecated(FSComp.SR.nrUnionTypeNeedsQualifiedAccess(ucinfo.DisplayName, ucinfo.Tycon.DisplayName) |> snd, m)
+            if g.langVersion.SupportsFeature(LanguageFeature.ErrorOnDeprecatedRequireQualifiedAccess) then
+                errorR(diagnostic)
+            else
+                warning(diagnostic)
 
         let ucref = ucinfo.UnionCaseRef
         CheckUnionCaseAttributes g ucref m |> CommitOperationResult
diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs
index 1314fb2fdfa..b39d6481517 100644
--- a/src/Compiler/Checking/CheckPatterns.fs
+++ b/src/Compiler/Checking/CheckPatterns.fs
@@ -568,7 +568,11 @@ and ApplyUnionCaseOrExn m (cenv: cenv) env overallTy item =
 
     | Item.UnionCase(ucinfo, showDeprecated) ->
         if showDeprecated then
-            warning(Deprecated(FSComp.SR.nrUnionTypeNeedsQualifiedAccess(ucinfo.DisplayName, ucinfo.Tycon.DisplayName) |> snd, m))
+            let diagnostic = Deprecated(FSComp.SR.nrUnionTypeNeedsQualifiedAccess(ucinfo.DisplayName, ucinfo.Tycon.DisplayName) |> snd, m)
+            if g.langVersion.SupportsFeature(LanguageFeature.ErrorOnDeprecatedRequireQualifiedAccess) then
+                errorR(diagnostic)
+            else
+                warning(diagnostic)
 
         let ucref = ucinfo.UnionCaseRef
         CheckUnionCaseAttributes g ucref m |> CommitOperationResult
diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt
index 2cb1fb84978..bf1989513f0 100644
--- a/src/Compiler/FSComp.txt
+++ b/src/Compiler/FSComp.txt
@@ -1554,6 +1554,7 @@ featureAdditionalImplicitConversions,"additional type-directed conversions"
 featureStructActivePattern,"struct representation for active patterns"
 featureRelaxWhitespace2,"whitespace relaxation v2"
 featureReallyLongList,"list literals of any size"
+featureErrorOnDeprecatedRequireQualifiedAccess,"give error on deprecated access of construct with RequireQualifiedAccess attribute"
 3353,fsiInvalidDirective,"Invalid directive '#%s %s'"
 3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
 3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
diff --git a/src/Compiler/Facilities/LanguageFeatures.fs b/src/Compiler/Facilities/LanguageFeatures.fs
index 6c2cc205593..6a6e57ee896 100644
--- a/src/Compiler/Facilities/LanguageFeatures.fs
+++ b/src/Compiler/Facilities/LanguageFeatures.fs
@@ -48,6 +48,7 @@ type LanguageFeature =
     | BetterExceptionPrinting
     | DelegateTypeNameResolutionFix
     | ReallyLongLists
+    | ErrorOnDeprecatedRequireQualifiedAccess
 
 /// LanguageVersion management
 type LanguageVersion (versionText) =
@@ -106,6 +107,7 @@ type LanguageVersion (versionText) =
             LanguageFeature.MLCompatRevisions,previewVersion
             LanguageFeature.BetterExceptionPrinting,previewVersion
             LanguageFeature.ReallyLongLists, previewVersion
+            LanguageFeature.ErrorOnDeprecatedRequireQualifiedAccess,previewVersion
         ]
 
     static let defaultLanguageVersion = LanguageVersion("default")
@@ -205,6 +207,7 @@ type LanguageVersion (versionText) =
         | LanguageFeature.BetterExceptionPrinting -> FSComp.SR.featureBetterExceptionPrinting()
         | LanguageFeature.DelegateTypeNameResolutionFix -> FSComp.SR.featureDelegateTypeNameResolutionFix()
         | LanguageFeature.ReallyLongLists -> FSComp.SR.featureReallyLongList()
+        | LanguageFeature.ErrorOnDeprecatedRequireQualifiedAccess -> FSComp.SR.featureErrorOnDeprecatedRequireQualifiedAccess()
 
     /// Get a version string associated with the given feature.
     member _.GetFeatureVersionString feature =
diff --git a/src/Compiler/Facilities/LanguageFeatures.fsi b/src/Compiler/Facilities/LanguageFeatures.fsi
index a5f2e8bb1fd..ec884903b49 100644
--- a/src/Compiler/Facilities/LanguageFeatures.fsi
+++ b/src/Compiler/Facilities/LanguageFeatures.fsi
@@ -38,6 +38,7 @@ type LanguageFeature =
     | BetterExceptionPrinting
     | DelegateTypeNameResolutionFix
     | ReallyLongLists
+    | ErrorOnDeprecatedRequireQualifiedAccess
 
 /// LanguageVersion management
 type LanguageVersion =
diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf
index b1fa4e2cf7c..151f341d14c 100644
--- a/src/Compiler/xlf/FSComp.txt.cs.xlf
+++ b/src/Compiler/xlf/FSComp.txt.cs.xlf
@@ -157,6 +157,11 @@
         literál float32 bez tečky
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         více typů podporuje měrné jednotky
diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf
index 4d6f3fc0b08..ade8740fa37 100644
--- a/src/Compiler/xlf/FSComp.txt.de.xlf
+++ b/src/Compiler/xlf/FSComp.txt.de.xlf
@@ -157,6 +157,11 @@
         punktloses float32-Literal
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         Maßeinheitenunterstützung durch weitere Typen
diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf
index 8d9e613caaf..5a6767dc03e 100644
--- a/src/Compiler/xlf/FSComp.txt.es.xlf
+++ b/src/Compiler/xlf/FSComp.txt.es.xlf
@@ -157,6 +157,11 @@
         literal float32 sin punto
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         más tipos admiten las unidades de medida
diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf
index abb9cac58fb..b35af7646d8 100644
--- a/src/Compiler/xlf/FSComp.txt.fr.xlf
+++ b/src/Compiler/xlf/FSComp.txt.fr.xlf
@@ -157,6 +157,11 @@
         littéral float32 sans point
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         d'autres types prennent en charge les unités de mesure
diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf
index ea42495717c..670f488ff43 100644
--- a/src/Compiler/xlf/FSComp.txt.it.xlf
+++ b/src/Compiler/xlf/FSComp.txt.it.xlf
@@ -157,6 +157,11 @@
         valore letterale float32 senza punti
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         più tipi supportano le unità di misura
diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf
index 7aca147dafc..06f422a854f 100644
--- a/src/Compiler/xlf/FSComp.txt.ja.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ja.xlf
@@ -157,6 +157,11 @@
         ドットなしの float32 リテラル
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         単位をサポートするその他の型
diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf
index 756adccd3c3..e7e3a0f922d 100644
--- a/src/Compiler/xlf/FSComp.txt.ko.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ko.xlf
@@ -157,6 +157,11 @@
         점이 없는 float32 리터럴
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         더 많은 형식이 측정 단위를 지원함
diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf
index 2f3076f0c53..1c0829bea4f 100644
--- a/src/Compiler/xlf/FSComp.txt.pl.xlf
+++ b/src/Compiler/xlf/FSComp.txt.pl.xlf
@@ -157,6 +157,11 @@
         bezkropkowy literał float32
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         więcej typów obsługuje jednostki miary
diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
index fe4f138ba45..68e52831e1c 100644
--- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
+++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
@@ -157,6 +157,11 @@
         literal float32 sem ponto
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         mais tipos dão suporte para unidades de medida
diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf
index 9869da25799..9777e20b0ad 100644
--- a/src/Compiler/xlf/FSComp.txt.ru.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ru.xlf
@@ -157,6 +157,11 @@
         литерал float32 без точки
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         другие типы поддерживают единицы измерения
diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf
index 96a4421fcd2..23432c905c6 100644
--- a/src/Compiler/xlf/FSComp.txt.tr.xlf
+++ b/src/Compiler/xlf/FSComp.txt.tr.xlf
@@ -157,6 +157,11 @@
         noktasız float32 sabit değeri
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         tür daha ölçü birimlerini destekler
diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
index 745af28b564..e72481a46d7 100644
--- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
+++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
@@ -157,6 +157,11 @@
         无点 float32 文本
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         更多类型支持度量单位
diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
index 84c81465e8d..d1f619a520a 100644
--- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
+++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
@@ -157,6 +157,11 @@
         無點號的 float32 常值
         
       
+      
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        give error on deprecated access of construct with RequireQualifiedAccess attribute
+        
+      
       
         more types support units of measure
         更多支援測量單位的類型

From c95d6aaa77f79e82b1810a3e306a8d3632262423 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 23 May 2022 18:36:21 +0100
Subject: [PATCH 08/20] Fix for subtype pattern matching  (#13178)

* tentative fix for pattern matching logic

* add test matrix
---
 .../Checking/PatternMatchCompilation.fs       |   8 +-
 tests/fsharp/core/subtype/test.fsx            | 490 ++++++++++++++++++
 2 files changed, 494 insertions(+), 4 deletions(-)

diff --git a/src/Compiler/Checking/PatternMatchCompilation.fs b/src/Compiler/Checking/PatternMatchCompilation.fs
index 521d5ea0e63..90bd293deac 100644
--- a/src/Compiler/Checking/PatternMatchCompilation.fs
+++ b/src/Compiler/Checking/PatternMatchCompilation.fs
@@ -1495,7 +1495,7 @@ let CompilePatternBasic
                         []
 
                 | DecisionTreeTest.IsNull _ ->
-                    match computeWhatSuccessfulTypeTestImpliesAboutNullTest g tgtTy1 with
+                    match computeWhatSuccessfulNullTestImpliesAboutTypeTest g tgtTy1 with
                     | Implication.Succeeds -> [Frontier (i, newActives, valMap)]
                     | Implication.Fails -> []
                     | Implication.Nothing -> [frontier]
@@ -1509,7 +1509,7 @@ let CompilePatternBasic
 
                 match discrim with
                 | DecisionTreeTest.IsInst (_srcTy, tgtTy2) ->
-                    match computeWhatSuccessfulTypeTestImpliesAboutTypeTest g amap m tgtTy1 tgtTy2 with
+                    match computeWhatSuccessfulTypeTestImpliesAboutTypeTest g amap m tgtTy2 tgtTy1 with
                     | Implication.Succeeds -> 
                         match pbindOpt with
                         | Some pbind ->
@@ -1531,7 +1531,7 @@ let CompilePatternBasic
                         [frontier]
 
                 | DecisionTreeTest.IsNull _ ->
-                    match computeWhatSuccessfulTypeTestImpliesAboutNullTest g tgtTy1 with
+                    match computeWhatSuccessfulNullTestImpliesAboutTypeTest g tgtTy1 with
                     | Implication.Succeeds -> [Frontier (i, newActives, valMap)]
                     | Implication.Fails -> []
                     | Implication.Nothing -> [frontier]
@@ -1548,7 +1548,7 @@ let CompilePatternBasic
                 | DecisionTreeTest.IsNull ->
                     [Frontier (i, newActives, valMap)]
                 | DecisionTreeTest.IsInst (_, tgtTy) -> 
-                    match computeWhatSuccessfulNullTestImpliesAboutTypeTest g tgtTy with
+                    match computeWhatSuccessfulTypeTestImpliesAboutNullTest g tgtTy with
                     | Implication.Succeeds -> [Frontier (i, newActives, valMap)]
                     | Implication.Fails -> []
                     | Implication.Nothing -> [frontier]
diff --git a/tests/fsharp/core/subtype/test.fsx b/tests/fsharp/core/subtype/test.fsx
index 9751d03e6e3..8ca98d0f472 100644
--- a/tests/fsharp/core/subtype/test.fsx
+++ b/tests/fsharp/core/subtype/test.fsx
@@ -1968,6 +1968,496 @@ module TestInheritFunc3 =
 
     check "cnwcki4" ((Foo() |> box |> unbox int -> int -> int> ) 5 6 7) 19
 
+module TestSubtypeMatching1 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+
+    let toName (x: obj) =
+        match x with
+        | :? A -> "A"
+        | :? B -> "B"
+        | :? C -> "C"
+        | _ -> "other"
+
+    check "cnwcki4cewweq1" (toName (A())) "A"
+    check "cnwcki4cewweq2" (toName (B())) "A"
+    check "cnwcki4cewweq3" (toName (C())) "A"
+    check "cnwcki4cewweq4" (toName (obj())) "other"
+
+module TestSubtypeMatching2 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+
+    let toName (x: obj) =
+        match x with
+        | :? A when false -> "A fail"
+        | :? B -> "B"
+        | :? C -> "C"
+        | _ -> "other"
+
+    check "cnwcki4cewweq5" (toName (A())) "other"
+    check "cnwcki4cewweq6" (toName (B())) "B"
+    check "cnwcki4cewweq7" (toName (C())) "C"
+    check "cnwcki4cewweq8" (toName (obj())) "other"
+
+
+module TestSubtypeMatching3 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+
+    let toName (x: obj) =
+        match x with
+        | :? A -> "A"
+        | :? B when false -> "B fail"
+        | :? C -> "C"
+        | _ -> "other"
+
+    check "cnwcki4cewweq10" (toName (A())) "A"
+    check "cnwcki4cewweq11" (toName (B())) "A"
+    check "cnwcki4cewweq12" (toName (C())) "A"
+    check "cnwcki4cewweq13" (toName (obj())) "other"
+
+module TestSubtypeMatching4 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+
+    let toName (x: obj) =
+        match x with
+        | :? C -> "C"
+        | :? B when false -> "B fail"
+        | :? A -> "A"
+        | _ -> "other"
+
+    check "cnwcki4cewweq14" (toName (A())) "A"
+    check "cnwcki4cewweq15" (toName (B())) "A"
+    check "cnwcki4cewweq16" (toName (C())) "C"
+    check "cnwcki4cewweq17" (toName (obj())) "other"
+
+// Test interface matching
+module TestSubtypeMatching5 =
+    type IA = interface end
+    type IB = inherit IA
+    type IC = inherit IA
+    type A() =
+        interface IA
+    type B() =
+        interface IB
+    type C() =
+        interface IC
+
+    let toName (x: obj) =
+        match x with
+        | :? IA when false -> "IA fail"
+        | :? IB -> "IB"
+        | :? IC -> "IC"
+        | _ -> "other"
+
+    check "cnwcki4cewweq18" (toName (A())) "other"
+    check "cnwcki4cewweq19" (toName (B())) "IB"
+    check "cnwcki4cewweq20" (toName (C())) "IC"
+    check "cnwcki4cewweq21" (toName (obj())) "other"
+
+// Multi-column with no 'when'
+module TestSubtypeMatching6 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+
+    let toName (x: obj * obj) =
+        match x with
+        | (:? A), :? A -> "AA"
+        | (:? B), :? B -> "BB"
+        | (:? C), :? C -> "CC"
+        | _ -> "other"
+
+    check "cnwcki4ce1" (toName (A(), A())) "AA"
+    check "cnwcki4ce2" (toName (A(), B())) "AA"
+    check "cnwcki4ce3" (toName (A(), C())) "AA"
+    check "cnwcki4ce4" (toName (B(), A())) "AA"
+    check "cnwcki4ce5" (toName (B(), B())) "AA"
+    check "cnwcki4ce6" (toName (B(), C())) "AA"
+    check "cnwcki4ce7" (toName (C(), A())) "AA"
+    check "cnwcki4ce8" (toName (C(), B())) "AA"
+    check "cnwcki4ce9" (toName (C(), C())) "AA"
+    check "cnwcki4ce10" (toName (obj(), obj())) "other"
+
+// Multi-column with failing 'when' and some sealed types
+module TestSubtypeMatching7 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+    []
+    type D() = inherit A()
+    []
+    type E() = inherit A()
+
+    let toName (x: obj * obj) =
+        match x with
+        | (:? A), :? A when false -> "AA"
+        | (:? B), :? B -> "BB"
+        | (:? C), :? C -> "CC"
+        | (:? D), :? D -> "DD"
+        | (:? E), :? E -> "EE"
+        | _ -> "other"
+
+    check "cnwcki4ce11" (toName (obj(), obj())) "other"
+    check "cnwcki4ce12" (toName (obj(), A())) "other"
+    check "cnwcki4ce13" (toName (obj(), B())) "other"
+    check "cnwcki4ce14" (toName (obj(), D())) "other"
+    check "cnwcki4ce15" (toName (obj(), C())) "other"
+    check "cnwcki4ce16" (toName (obj(), E())) "other"
+
+    check "cnwcki4ce17" (toName (A(), obj())) "other"
+    check "cnwcki4ce18" (toName (A(), A())) "other"
+    check "cnwcki4ce19" (toName (A(), B())) "other"
+    check "cnwcki4ce20" (toName (A(), C())) "other"
+    check "cnwcki4ce21" (toName (A(), D())) "other"
+    check "cnwcki4ce22" (toName (A(), E())) "other"
+
+    check "cnwcki4ce23" (toName (B(), obj())) "other"
+    check "cnwcki4ce24" (toName (B(), A())) "other"
+    check "cnwcki4ce25" (toName (B(), B())) "BB"
+    check "cnwcki4ce26" (toName (B(), D())) "other"
+    check "cnwcki4ce27" (toName (B(), C())) "other"
+    check "cnwcki4ce28" (toName (B(), E())) "other"
+
+    check "cnwcki4ce29" (toName (C(), obj())) "other"
+    check "cnwcki4ce30" (toName (C(), A())) "other"
+    check "cnwcki4ce31" (toName (C(), B())) "other"
+    check "cnwcki4ce32" (toName (C(), C())) "CC"
+    check "cnwcki4ce33" (toName (C(), D())) "other"
+    check "cnwcki4ce34" (toName (C(), E())) "other"
+
+    check "cnwcki4ce35" (toName (D(), obj())) "other"
+    check "cnwcki4ce36" (toName (D(), A())) "other"
+    check "cnwcki4ce37" (toName (D(), B())) "other"
+    check "cnwcki4ce38" (toName (D(), C())) "other"
+    check "cnwcki4ce39" (toName (D(), D())) "DD"
+    check "cnwcki4ce40" (toName (D(), E())) "other"
+
+    check "cnwcki4ce41" (toName (E(), obj())) "other"
+    check "cnwcki4ce42" (toName (E(), A())) "other"
+    check "cnwcki4ce43" (toName (E(), B())) "other"
+    check "cnwcki4ce44" (toName (E(), C())) "other"
+    check "cnwcki4ce45" (toName (E(), D())) "other"
+    check "cnwcki4ce46" (toName (E(), E())) "EE"
+
+// Moving the 'when false' clause around shouldn't matter
+module TestSubtypeMatching8 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+    []
+    type D() = inherit A()
+    []
+    type E() = inherit A()
+
+    let toName (x: obj * obj) =
+        match x with
+        | (:? B), :? B -> "BB"
+        | (:? A), :? A when false -> "AA"
+        | (:? C), :? C -> "CC"
+        | (:? D), :? D -> "DD"
+        | (:? E), :? E -> "EE"
+        | _ -> "other"
+
+    check "cnwcki4cf11" (toName (obj(), obj())) "other"
+    check "cnwcki4cf12" (toName (obj(), A())) "other"
+    check "cnwcki4cf13" (toName (obj(), B())) "other"
+    check "cnwcki4cf14" (toName (obj(), D())) "other"
+    check "cnwcki4cf15" (toName (obj(), C())) "other"
+    check "cnwcki4cf16" (toName (obj(), E())) "other"
+
+    check "cnwcki4cf17" (toName (A(), obj())) "other"
+    check "cnwcki4cf18" (toName (A(), A())) "other"
+    check "cnwcki4cf19" (toName (A(), B())) "other"
+    check "cnwcki4cf20" (toName (A(), C())) "other"
+    check "cnwcki4cf21" (toName (A(), D())) "other"
+    check "cnwcki4cf22" (toName (A(), E())) "other"
+
+    check "cnwcki4cf23" (toName (B(), obj())) "other"
+    check "cnwcki4cf24" (toName (B(), A())) "other"
+    check "cnwcki4cf25" (toName (B(), B())) "BB"
+    check "cnwcki4cf26" (toName (B(), D())) "other"
+    check "cnwcki4cf27" (toName (B(), C())) "other"
+    check "cnwcki4cf28" (toName (B(), E())) "other"
+
+    check "cnwcki4cf29" (toName (C(), obj())) "other"
+    check "cnwcki4cf30" (toName (C(), A())) "other"
+    check "cnwcki4cf31" (toName (C(), B())) "other"
+    check "cnwcki4cf32" (toName (C(), C())) "CC"
+    check "cnwcki4cf33" (toName (C(), D())) "other"
+    check "cnwcki4cf34" (toName (C(), E())) "other"
+
+    check "cnwcki4cf35" (toName (D(), obj())) "other"
+    check "cnwcki4cf36" (toName (D(), A())) "other"
+    check "cnwcki4cf37" (toName (D(), B())) "other"
+    check "cnwcki4cf38" (toName (D(), C())) "other"
+    check "cnwcki4cf39" (toName (D(), D())) "DD"
+    check "cnwcki4cf40" (toName (D(), E())) "other"
+
+    check "cnwcki4cf41" (toName (E(), obj())) "other"
+    check "cnwcki4cf42" (toName (E(), A())) "other"
+    check "cnwcki4cf43" (toName (E(), B())) "other"
+    check "cnwcki4cf44" (toName (E(), C())) "other"
+    check "cnwcki4cf45" (toName (E(), D())) "other"
+    check "cnwcki4cf46" (toName (E(), E())) "EE"
+
+// Multi-column in order from most specific to least specific
+module TestSubtypeMatching9 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+    []
+    type D() = inherit A()
+    []
+    type E() = inherit A()
+
+    let toName (x: obj * obj) =
+        match x with
+        | (:? E), :? E -> "EE"
+        | (:? D), :? D -> "DD"
+        | (:? C), :? C -> "CC"
+        | (:? B), :? B -> "BB"
+        | (:? A), :? A -> "AA"
+        | _ -> "other"
+
+    check "cnwcki4cg11" (toName (obj(), obj())) "other"
+    check "cnwcki4cg12" (toName (obj(), A())) "other"
+    check "cnwcki4cg13" (toName (obj(), B())) "other"
+    check "cnwcki4cg14" (toName (obj(), D())) "other"
+    check "cnwcki4cg15" (toName (obj(), C())) "other"
+    check "cnwcki4cg16" (toName (obj(), E())) "other"
+
+    check "cnwcki4cg17" (toName (A(), obj())) "other"
+    check "cnwcki4cg18" (toName (A(), A())) "AA"
+    check "cnwcki4cg19" (toName (A(), B())) "AA"
+    check "cnwcki4cg20" (toName (A(), C())) "AA"
+    check "cnwcki4cg21" (toName (A(), D())) "AA"
+    check "cnwcki4cg22" (toName (A(), E())) "AA"
+
+    check "cnwcki4cg23" (toName (B(), obj())) "other"
+    check "cnwcki4cg24" (toName (B(), A())) "AA"
+    check "cnwcki4cg25" (toName (B(), B())) "BB"
+    check "cnwcki4cg26" (toName (B(), D())) "AA"
+    check "cnwcki4cg27" (toName (B(), C())) "AA"
+    check "cnwcki4cg28" (toName (B(), E())) "AA"
+
+    check "cnwcki4cg29" (toName (C(), obj())) "other"
+    check "cnwcki4cg30" (toName (C(), A())) "AA"
+    check "cnwcki4cg31" (toName (C(), B())) "AA"
+    check "cnwcki4cg32" (toName (C(), C())) "CC"
+    check "cnwcki4cg33" (toName (C(), D())) "AA"
+    check "cnwcki4cg34" (toName (C(), E())) "AA"
+
+    check "cnwcki4cg35" (toName (D(), obj())) "other"
+    check "cnwcki4cg36" (toName (D(), A())) "AA"
+    check "cnwcki4cg37" (toName (D(), B())) "AA"
+    check "cnwcki4cg38" (toName (D(), C())) "AA"
+    check "cnwcki4cg39" (toName (D(), D())) "DD"
+    check "cnwcki4cg40" (toName (D(), E())) "AA"
+
+    check "cnwcki4cg41" (toName (E(), obj())) "other"
+    check "cnwcki4cg42" (toName (E(), A())) "AA"
+    check "cnwcki4cg43" (toName (E(), B())) "AA"
+    check "cnwcki4cg44" (toName (E(), C())) "AA"
+    check "cnwcki4cg45" (toName (E(), D())) "AA"
+    check "cnwcki4cg46" (toName (E(), E())) "EE"
+
+// Multi-column in order from most specific to least specific on second column
+module TestSubtypeMatching10 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+    []
+    type D() = inherit A()
+    []
+    type E() = inherit A()
+
+    let toName (x: obj * obj) =
+        match x with
+        | (:? A), :? E -> "AE"
+        | (:? B), :? D -> "BD"
+        | (:? C), :? C -> "CC"
+        | (:? D), :? B -> "DB"
+        | (:? E), :? A -> "EA"
+        | _ -> "other"
+
+    check "cnwcki4ch11" (toName (obj(), obj())) "other"
+    check "cnwcki4ch12" (toName (obj(), A())) "other"
+    check "cnwcki4ch13" (toName (obj(), B())) "other"
+    check "cnwcki4ch14" (toName (obj(), D())) "other"
+    check "cnwcki4ch15" (toName (obj(), C())) "other"
+    check "cnwcki4ch16" (toName (obj(), E())) "other"
+
+    check "cnwcki4ch17" (toName (A(), obj())) "other"
+    check "cnwcki4ch18" (toName (A(), A())) "other"
+    check "cnwcki4ch19" (toName (A(), B())) "other"
+    check "cnwcki4ch20" (toName (A(), C())) "other"
+    check "cnwcki4ch21" (toName (A(), D())) "other"
+    check "cnwcki4ch22" (toName (A(), E())) "AE"
+
+    check "cnwcki4ch23" (toName (B(), obj())) "other"
+    check "cnwcki4ch24" (toName (B(), A())) "other"
+    check "cnwcki4ch25" (toName (B(), B())) "other"
+    check "cnwcki4ch26" (toName (B(), D())) "BD"
+    check "cnwcki4ch27" (toName (B(), C())) "other"
+    check "cnwcki4ch28" (toName (B(), E())) "AE"
+
+    check "cnwcki4ch29" (toName (C(), obj())) "other"
+    check "cnwcki4ch30" (toName (C(), A())) "other"
+    check "cnwcki4ch31" (toName (C(), B())) "other"
+    check "cnwcki4ch32" (toName (C(), C())) "CC"
+    check "cnwcki4ch33" (toName (C(), D())) "other"
+    check "cnwcki4ch34" (toName (C(), E())) "AE"
+
+    check "cnwcki4ch35" (toName (D(), obj())) "other"
+    check "cnwcki4ch36" (toName (D(), A())) "other"
+    check "cnwcki4ch37" (toName (D(), B())) "DB"
+    check "cnwcki4ch38" (toName (D(), C())) "other"
+    check "cnwcki4ch39" (toName (D(), D())) "other"
+    check "cnwcki4ch40" (toName (D(), E())) "AE"
+
+    check "cnwcki4ch41" (toName (E(), obj())) "other"
+    check "cnwcki4ch42" (toName (E(), A())) "EA"
+    check "cnwcki4ch43" (toName (E(), B())) "EA"
+    check "cnwcki4ch44" (toName (E(), C())) "EA"
+    check "cnwcki4ch45" (toName (E(), D())) "EA"
+    check "cnwcki4ch46" (toName (E(), E())) "AE"
+
+// Add null to the matrix of multi-column (most specific to least specific on second column)
+module TestSubtypeMatching11 =
+    type A() = class end
+    type B() = inherit A()
+    type C() = inherit A()
+    []
+    type D() = inherit A()
+    []
+    type E() = inherit A()
+
+    let toName (x: obj * obj) =
+        match x with
+        | null, :? E -> "0E"
+        | (:? A), :? E -> "AE"
+        | (:? B), :? D -> "BD"
+        | (:? C), :? C -> "CC"
+        | (:? D), :? B -> "DB"
+        | (:? E), :? A -> "EA"
+        | (:? E), null -> "E0"
+        | _ -> "other"
+
+    check "cnwcki4ci11" (toName (null, obj())) "other"
+    check "cnwcki4ci12" (toName (null, A())) "other"
+    check "cnwcki4ci13" (toName (null, B())) "other"
+    check "cnwcki4ci14" (toName (null, D())) "other"
+    check "cnwcki4ci15" (toName (null, C())) "other"
+    check "cnwcki4ci16" (toName (null, E())) "0E"
+    check "cnwcki4ci17" (toName (null, null)) "other"
+
+    check "cnwcki4ci18" (toName (obj(), obj())) "other"
+    check "cnwcki4ci19" (toName (obj(), A())) "other"
+    check "cnwcki4ci20" (toName (obj(), B())) "other"
+    check "cnwcki4ci21" (toName (obj(), D())) "other"
+    check "cnwcki4ci22" (toName (obj(), C())) "other"
+    check "cnwcki4ci23" (toName (obj(), E())) "other"
+    check "cnwcki4ci24" (toName (obj(), null)) "other"
+
+    check "cnwcki4ci25" (toName (A(), obj())) "other"
+    check "cnwcki4ci26" (toName (A(), A())) "other"
+    check "cnwcki4ci27" (toName (A(), B())) "other"
+    check "cnwcki4ci28" (toName (A(), C())) "other"
+    check "cnwcki4ci29" (toName (A(), D())) "other"
+    check "cnwcki4ci30" (toName (A(), E())) "AE"
+    check "cnwcki4ci31" (toName (A(), null)) "other"
+
+    check "cnwcki4ci32" (toName (B(), obj())) "other"
+    check "cnwcki4ci33" (toName (B(), A())) "other"
+    check "cnwcki4ci34" (toName (B(), B())) "other"
+    check "cnwcki4ci35" (toName (B(), D())) "BD"
+    check "cnwcki4ci36" (toName (B(), C())) "other"
+    check "cnwcki4ci37" (toName (B(), E())) "AE"
+    check "cnwcki4ci38" (toName (B(), null)) "other"
+
+    check "cnwcki4ci39" (toName (C(), obj())) "other"
+    check "cnwcki4ci40" (toName (C(), A())) "other"
+    check "cnwcki4ci41" (toName (C(), B())) "other"
+    check "cnwcki4ci42" (toName (C(), C())) "CC"
+    check "cnwcki4ci43" (toName (C(), D())) "other"
+    check "cnwcki4ci44" (toName (C(), E())) "AE"
+    check "cnwcki4ci45" (toName (C(), null)) "other"
+
+    check "cnwcki4ci46" (toName (D(), obj())) "other"
+    check "cnwcki4ci47" (toName (D(), A())) "other"
+    check "cnwcki4ci48" (toName (D(), B())) "DB"
+    check "cnwcki4ci49" (toName (D(), C())) "other"
+    check "cnwcki4ci50" (toName (D(), D())) "other"
+    check "cnwcki4ci51" (toName (D(), E())) "AE"
+    check "cnwcki4ci52" (toName (D(), null)) "other"
+
+    check "cnwcki4ci53" (toName (E(), obj())) "other"
+    check "cnwcki4ci54" (toName (E(), A())) "EA"
+    check "cnwcki4ci55" (toName (E(), B())) "EA"
+    check "cnwcki4ci56" (toName (E(), C())) "EA"
+    check "cnwcki4ci57" (toName (E(), D())) "EA"
+    check "cnwcki4ci58" (toName (E(), E())) "AE"
+    check "cnwcki4ci59" (toName (E(), null)) "E0"
+
+// Test interface matching with 'null'
+module TestSubtypeMatching12 =
+    type IA = interface end
+    type IB = inherit IA
+    type IC = inherit IA
+    type A() =
+        interface IA
+    type B() =
+        interface IB
+    type C() =
+        interface IC
+
+    let toName (x: obj) =
+        match x with
+        | null -> "null"
+        | :? IA when false -> "IA fail"
+        | :? IB -> "IB"
+        | :? IC -> "IC"
+        | _ -> "other"
+
+    check "cnwcki4c0" (toName null) "null"
+    check "cnwcki4c1" (toName (A())) "other"
+    check "cnwcki4c2" (toName (B())) "IB"
+    check "cnwcki4c3" (toName (C())) "IC"
+    check "cnwcki4c4" (toName (obj())) "other"
+
+// Test interface matching with 'null when false'
+module TestSubtypeMatching13 =
+    type IA = interface end
+    type IB = inherit IA
+    type IC = inherit IA
+    type A() =
+        interface IA
+    type B() =
+        interface IB
+    type C() =
+        interface IC
+
+    let toName (x: obj) =
+        match x with
+        | null when false -> "null"
+        | :? IA -> "IA"
+        | :? IB -> "IB"
+        | :? IC -> "IC"
+        | _ -> "other"
+
+    check "cnwcki4d0" (toName null) "other"
+    check "cnwcki4d1" (toName (A())) "IA"
+    check "cnwcki4d2" (toName (B())) "IA"
+    check "cnwcki4d3" (toName (C())) "IA"
+    check "cnwcki4d4" (toName (obj())) "other"
+
 #if !NETCOREAPP
 module TestConverter =
     open System

From 36f01e368fd0a8eb8c8782b0a6f4a723fc112c31 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 23 May 2022 20:13:01 +0100
Subject: [PATCH 09/20] remove experimental attributes for expanded measures
 (#13181)

---
 src/FSharp.Core/prim-types.fs  |  4 ++--
 src/FSharp.Core/prim-types.fsi | 18 ------------------
 2 files changed, 2 insertions(+), 20 deletions(-)

diff --git a/src/FSharp.Core/prim-types.fs b/src/FSharp.Core/prim-types.fs
index c3df910fafd..4f7486c8fe5 100644
--- a/src/FSharp.Core/prim-types.fs
+++ b/src/FSharp.Core/prim-types.fs
@@ -251,10 +251,10 @@ namespace Microsoft.FSharp.Core
 
     module internal ExperimentalAttributeMessages =
         []
-        let RequiresPreview : string = "Experimental library feature, requires '--langversion:preview'"
+        let RequiresPreview: string = "Experimental library feature, requires '--langversion:preview'"
 
         []
-        let NotSupportedYet : string = "This construct is not supported by your version of the F# compiler"
+        let NotSupportedYet: string = "This construct is not supported by your version of the F# compiler"
 
     []
     []
diff --git a/src/FSharp.Core/prim-types.fsi b/src/FSharp.Core/prim-types.fsi
index 3534c76a486..af9b1c1e5a3 100644
--- a/src/FSharp.Core/prim-types.fsi
+++ b/src/FSharp.Core/prim-types.fsi
@@ -1007,7 +1007,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     []
     type nativeint<[] 'Measure> = nativeint
 
@@ -1017,7 +1016,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     []
     type uint<[] 'Measure> = uint
 
@@ -1027,7 +1025,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     []
     type byte<[] 'Measure> = byte
 
@@ -1037,7 +1034,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     []
     type uint16<[] 'Measure> = uint16
 
@@ -1047,7 +1043,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     []
     type uint64<[] 'Measure> = uint64
 
@@ -1057,7 +1052,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     []
     type unativeint<[] 'Measure> = unativeint
 
@@ -1067,7 +1061,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     type double<[] 'Measure> = float<'Measure>
 
     /// The type of single-precision floating point numbers, annotated with a unit of measure.
@@ -1076,7 +1069,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     type single<[] 'Measure> = float32<'Measure>
 
     /// The type of 8-bit signed integer numbers, annotated with a unit of measure.
@@ -1085,7 +1077,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     type int8<[] 'Measure> = sbyte<'Measure>
 
     /// The type of 32-bit signed integer numbers, annotated with a unit of measure.
@@ -1094,7 +1085,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     type int32<[] 'Measure> = int<'Measure>
 
     /// The type of 8-bit unsigned integer numbers, annotated with a unit of measure.
@@ -1103,7 +1093,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     type uint8<[] 'Measure> = byte<'Measure>
 
     /// The type of 32-bit unsigned integer numbers, annotated with a unit of measure.
@@ -1112,7 +1101,6 @@ namespace Microsoft.FSharp.Core
     /// .
     ///
     /// Basic Types with Units of Measure
-    []
     type uint32<[] 'Measure> = uint<'Measure>
 
     /// Represents a managed pointer in F# code.
@@ -1413,7 +1401,6 @@ namespace Microsoft.FSharp.Core
         /// The input nativeint.
         ///
         /// The nativeint with units-of-measure.
-        []
         val inline IntPtrWithMeasure: input: nativeint -> nativeint<'Measure>
 
         /// Creates a uint value with units-of-measure
@@ -1421,7 +1408,6 @@ namespace Microsoft.FSharp.Core
         /// The input uint.
         ///
         /// The uint with units-of-measure.
-        []
         val inline UInt32WithMeasure: input: uint -> uint<'Measure>
         
         /// Creates a uint64 value with units-of-measure
@@ -1429,7 +1415,6 @@ namespace Microsoft.FSharp.Core
         /// The input uint64.
         ///
         /// The uint64 with units-of-measure.
-        []
         val inline UInt64WithMeasure: input: uint64 -> uint64<'Measure>
         
         /// Creates a uint16 value with units-of-measure
@@ -1437,7 +1422,6 @@ namespace Microsoft.FSharp.Core
         /// The input uint16.
         ///
         /// The uint16 with units-of-measure.
-        []
         val inline UInt16WithMeasure: input: uint16 -> uint16<'Measure>
         
         /// Creates a byte value with units-of-measure
@@ -1445,7 +1429,6 @@ namespace Microsoft.FSharp.Core
         /// The input byte.
         ///
         /// The byte with units-of-measure.
-        []
         val inline ByteWithMeasure: input: byte -> byte<'Measure>
         
         /// Creates a unativeint value with units-of-measure
@@ -1453,7 +1436,6 @@ namespace Microsoft.FSharp.Core
         /// The input unativeint.
         ///
         /// The unativeint with units-of-measure.
-        []
         val inline UIntPtrWithMeasure: input: unativeint -> unativeint<'Measure>
 
         /// Parse an int32 according to the rules used by the overloaded 'int32' conversion operator when applied to strings

From 1d1eb4c2ecb98664c4de62c0ddc9f778b52e3adf Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Tue, 24 May 2022 12:43:38 +0100
Subject: [PATCH 10/20] Fix Async.Parallel stack overflow on cancellation of
 ~2000 uncompleted (#13186)

* Fix Async.Parallel stack overflow on cancellation of ~2000 uncompleted computations

* Update AsyncModule.fs

* Update AsyncModule.fs
---
 src/FSharp.Core/async.fs                             | 12 ++++++------
 .../Microsoft.FSharp.Control/AsyncModule.fs          | 12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/FSharp.Core/async.fs b/src/FSharp.Core/async.fs
index 73a229670b3..0b64a6450d1 100644
--- a/src/FSharp.Core/async.fs
+++ b/src/FSharp.Core/async.fs
@@ -1470,21 +1470,21 @@ type Async =
                                 if innerCTS.Token.IsCancellationRequested then
                                     let cexn = OperationCanceledException (innerCTS.Token)
                                     recordFailure (Choice2Of2 cexn) |> unfake
-                                    worker trampolineHolder |> unfake
+                                    worker trampolineHolder
                                 else
                                     let taskCtxt =
                                         AsyncActivation.Create
                                             innerCTS.Token
                                             trampolineHolder
-                                            (fun res -> recordSuccess j res |> unfake; worker trampolineHolder)
-                                            (fun edi -> recordFailure (Choice1Of2 edi) |> unfake; worker trampolineHolder)
-                                            (fun cexn -> recordFailure (Choice2Of2 cexn) |> unfake; worker trampolineHolder)
+                                            (fun res -> recordSuccess j res |> unfake; worker trampolineHolder |> fake)
+                                            (fun edi -> recordFailure (Choice1Of2 edi) |> unfake; worker trampolineHolder |> fake)
+                                            (fun cexn -> recordFailure (Choice2Of2 cexn) |> unfake; worker trampolineHolder |> fake)
                                     computations.[j].Invoke taskCtxt |> unfake
-                        fake()
+
                     for x = 1 to maxDegreeOfParallelism do
                         let trampolineHolder = TrampolineHolder()
                         trampolineHolder.QueueWorkItemWithTrampoline (fun () ->
-                            worker trampolineHolder)
+                            worker trampolineHolder |> fake)
                         |> unfake
 
                 fake()))
diff --git a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs
index 103b636c127..2edf022ac75 100644
--- a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs
+++ b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs
@@ -764,3 +764,15 @@ type AsyncModule() =
                 lock gate <| fun () -> printfn "Unhandled exception: %s" exn.Message
                 lock gate <| fun () -> printfn "Semaphore count available: %i" semaphore.CurrentCount
             Assert.AreEqual(acquiredCount, releaseCount)
+
+    []
+    member _.``Async.Parallel blows stack when cancelling many`` () =
+        let gen (i : int) = async {
+            if i <> 0 then do! Async.Sleep i
+            else return failwith "OK"}
+        let count = 3600
+        let comps = Seq.init count gen
+        let result = Async.Parallel(comps, 16) |> Async.Catch |> Async.RunSynchronously
+        match result with
+        | Choice2Of2 e -> Assert.AreEqual("OK", e.Message)
+        | x -> failwithf "unexpected %A" x

From 40f7eee6fe95a4f910e576e57393e66bfcb7c48b Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Tue, 24 May 2022 12:43:53 +0100
Subject: [PATCH 11/20] make tests show as active (#13180)

---
 tests/fsharp/tests.fs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/fsharp/tests.fs b/tests/fsharp/tests.fs
index a5a8ba6d133..c8e909e1e8b 100644
--- a/tests/fsharp/tests.fs
+++ b/tests/fsharp/tests.fs
@@ -3284,7 +3284,7 @@ open System.Runtime.InteropServices
         fv.LegalTrademarks |> Assert.areEqual "CST \u2122"
 #endif
 
-#if NET472
+#if !NETCOREAPP
 []
 module ProductVersionTest =
 

From 4a7c9234fcddd949fa143b19fb17e118d90b2b15 Mon Sep 17 00:00:00 2001
From: Vlad Zarytovskii 
Date: Tue, 24 May 2022 13:46:26 +0200
Subject: [PATCH 12/20] Moved service solution to root, since we use stable SDK
 for compiler. (#13166)

* Moved service solution to root, since we use stable SDK for compiler overall

* Turned off project graph by default, set default solution to the service

* Set 6.0.200 as 6.0.300 has compiler codegen bug. Fix determinism builds

* Update launch.json
---
 .vscode/launch.json                           | 11 +++++++----
 .vscode/settings.json                         |  8 ++++----
 ...Service.sln => FSharp.Compiler.Service.sln | 19 +++++++++++--------
 ...=> FSharp.Compiler.Service.sln.DotSettings |  0
 azure-pipelines.yml                           |  8 ++++----
 eng/test-determinism.ps1                      |  2 +-
 service/global.json                           |  7 -------
 7 files changed, 27 insertions(+), 28 deletions(-)
 rename service/FSharp.Compiler.Service.sln => FSharp.Compiler.Service.sln (78%)
 rename service/FSharp.Compiler.Service.sln.DotSettings => FSharp.Compiler.Service.sln.DotSettings (100%)
 delete mode 100644 service/global.json

diff --git a/.vscode/launch.json b/.vscode/launch.json
index 652948c7999..d00e32e86bd 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -16,10 +16,12 @@
             "request": "launch",
             // TODO: Shall we assume that it's already been built, or build it every time we debug?
             // "preLaunchTask": "Build (Debug)",
-            // If you have changed target frameworks, make sure to update the program path.
+            // If you have changed target frameworks, make sure to update the program p
             "program": "${workspaceFolder}/artifacts/bin/fsi/Debug/net6.0/fsi.dll",
             "cwd": "${workspaceFolder}/src",
             "console": "integratedTerminal", // This is the default to be able to run in Codespaces.
+            "internalConsoleOptions": "neverOpen",
+            "suppressJITOptimizations": true,
             "stopAtEntry": false,
             "justMyCode": false,
             "enableStepFiltering": true,
@@ -46,8 +48,10 @@
             ],
             "cwd": "${workspaceFolder}",
             "console": "integratedTerminal", // This is the default to be able to run in Codespaces.
+            "internalConsoleOptions": "neverOpen",
+            "suppressJITOptimizations": true,
             "stopAtEntry": false,
-            "justMyCode": false,
+            "justMyCode": true,
             "enableStepFiltering": true,
             "symbolOptions": {
                 "searchMicrosoftSymbolServer": true,
@@ -60,7 +64,7 @@
             },
         },
         {
-            "name": "Attach to .NET process",
+            "name": "Attach to a .NET process",
             "type": "coreclr",
             "request": "attach",
             "processId": "${command:pickProcess}",
@@ -69,7 +73,6 @@
                     "enabled": true
                 }
             },
-            "stopAtEntry": false,
             "justMyCode": false,
             "enableStepFiltering": false,
         }
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 12aeedba022..a6076623164 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -16,9 +16,9 @@
         "*.fs": "${capture}.fsi"
     },
     "FSharp.suggestGitignore": false,
-    "FSharp.enableMSBuildProjectGraph": true,
-    "FSharp.workspacePath": "service/FSharp.Compiler.Service.sln",
-    "FSharp.workspaceModePeekDeepLevel": 2,
+    "FSharp.enableMSBuildProjectGraph": false,
+    "FSharp.workspacePath": "FSharp.Compiler.Service.sln",
+    "FSharp.workspaceModePeekDeepLevel": 1,
     "FSharp.enableBackgroundServices": false,
     "FSharp.excludeProjectDirectories": [
         ".git",
@@ -35,7 +35,7 @@
     "csharp.suppressDotnetRestoreNotification": true,
     "csharp.suppressHiddenDiagnostics": true,
     "omnisharp.autoStart": false,
-    "omnisharp.defaultLaunchSolution": "service/FSharp.Compiler.Service.sln",
+    "omnisharp.defaultLaunchSolution": "FSharp.Compiler.Service.sln",
     "omnisharp.enableMsBuildLoadProjectsOnDemand": true,
     "omnisharp.disableMSBuildDiagnosticWarning": true,
     "omnisharp.enableRoslynAnalyzers": false,
diff --git a/service/FSharp.Compiler.Service.sln b/FSharp.Compiler.Service.sln
similarity index 78%
rename from service/FSharp.Compiler.Service.sln
rename to FSharp.Compiler.Service.sln
index a913c4f2502..2a082c06269 100644
--- a/service/FSharp.Compiler.Service.sln
+++ b/FSharp.Compiler.Service.sln
@@ -1,23 +1,26 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio Version 17
-VisualStudioVersion = 17.1.32228.430
+VisualStudioVersion = 17.1.32113.165
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Compiler.Service", "..\src\Compiler\FSharp.Compiler.Service.fsproj", "{A59DB8AE-8044-41A5-848A-800A7FF31C93}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Compiler.Service", "src\Compiler\FSharp.Compiler.Service.fsproj", "{A59DB8AE-8044-41A5-848A-800A7FF31C93}"
 EndProject
-Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Compiler.Service.Tests", "..\tests\FSharp.Compiler.Service.Tests\FSharp.Compiler.Service.Tests.fsproj", "{8D9C9683-5041-48AB-8FA9-0939D2D27D33}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Compiler.Service.Tests", "tests\FSharp.Compiler.Service.Tests\FSharp.Compiler.Service.Tests.fsproj", "{8D9C9683-5041-48AB-8FA9-0939D2D27D33}"
 EndProject
-Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.DependencyManager.Nuget", "..\src\FSharp.DependencyManager.Nuget\FSharp.DependencyManager.Nuget.fsproj", "{98E7659D-8E0C-489F-B4F5-E12AFC0D1BFA}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.DependencyManager.Nuget", "src\FSharp.DependencyManager.Nuget\FSharp.DependencyManager.Nuget.fsproj", "{98E7659D-8E0C-489F-B4F5-E12AFC0D1BFA}"
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FSharp.Compiler.Service.Tests support", "FSharp.Compiler.Service.Tests support", "{875D91AC-BA4C-4191-AB11-AE461DB9B8DB}"
 EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharp_Analysis", "..\tests\service\data\CSharp_Analysis\CSharp_Analysis.csproj", "{BFE6E6F1-1B73-404F-A3A5-30B57E5E0731}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharp_Analysis", "tests\service\data\CSharp_Analysis\CSharp_Analysis.csproj", "{BFE6E6F1-1B73-404F-A3A5-30B57E5E0731}"
 EndProject
-Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestTP", "..\tests\service\data\TestTP\TestTP.fsproj", "{2EF674B9-8B56-4796-9933-42B2629E52C3}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestTP", "tests\service\data\TestTP\TestTP.fsproj", "{2EF674B9-8B56-4796-9933-42B2629E52C3}"
 EndProject
-Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Test.Utilities", "..\tests\FSharp.Test.Utilities\FSharp.Test.Utilities.fsproj", "{38A23D53-E2BF-4B76-907F-49F41D60C88E}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Test.Utilities", "tests\FSharp.Test.Utilities\FSharp.Test.Utilities.fsproj", "{38A23D53-E2BF-4B76-907F-49F41D60C88E}"
 EndProject
-Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Compiler.ComponentTests", "..\tests\FSharp.Compiler.ComponentTests\FSharp.Compiler.ComponentTests.fsproj", "{2A182B7D-EDA3-4BF2-84B8-C7553BB7A5A7}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Compiler.ComponentTests", "tests\FSharp.Compiler.ComponentTests\FSharp.Compiler.ComponentTests.fsproj", "{2A182B7D-EDA3-4BF2-84B8-C7553BB7A5A7}"
+	ProjectSection(ProjectDependencies) = postProject
+		{38A23D53-E2BF-4B76-907F-49F41D60C88E} = {38A23D53-E2BF-4B76-907F-49F41D60C88E}
+	EndProjectSection
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/service/FSharp.Compiler.Service.sln.DotSettings b/FSharp.Compiler.Service.sln.DotSettings
similarity index 100%
rename from service/FSharp.Compiler.Service.sln.DotSettings
rename to FSharp.Compiler.Service.sln.DotSettings
diff --git a/azure-pipelines.yml b/azure-pipelines.yml
index 3f8d37f692d..6d3e01ed917 100644
--- a/azure-pipelines.yml
+++ b/azure-pipelines.yml
@@ -401,7 +401,7 @@ stages:
               packageType: sdk
               useGlobalJson: true
               includePreviewVersions: false
-              workingDirectory: $(Build.SourcesDirectory)/service
+              workingDirectory: $(Build.SourcesDirectory)
               installationPath: $(Build.SourcesDirectory)/.dotnet
           - script: .\eng\test-determinism.cmd -configuration Debug
             displayName: Determinism tests with Debug configuration
@@ -454,7 +454,7 @@ stages:
               DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
             displayName: Regular rebuild of FSharp.sln
           - script: dotnet build .\FSharp.Compiler.Service.sln /bl:\"artifacts/log/$(_BuildConfig)/ServiceRegularBuild.binlog\"
-            workingDirectory: $(Build.SourcesDirectory)/service
+            workingDirectory: $(Build.SourcesDirectory)
             env:
               DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
             displayName: Regular rebuild of FSharp.Compiler.Service.sln
@@ -486,7 +486,7 @@ stages:
               DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
             displayName: Regular rebuild of FSharp.sln
           - script: dotnet build ./FSharp.Compiler.Service.sln /bl:\"artifacts/log/$(_BuildConfig)/ServiceRegularBuild.binlog\"
-            workingDirectory: $(Build.SourcesDirectory)/service
+            workingDirectory: $(Build.SourcesDirectory)
             env:
               DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
             displayName: Regular rebuild of FSharp.Compiler.Service.sln
@@ -518,7 +518,7 @@ stages:
               DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
             displayName: Regular rebuild of FSharp.sln
           - script: dotnet build ./FSharp.Compiler.Service.sln /bl:\"artifacts/log/$(_BuildConfig)/ServiceRegularBuild.binlog\"
-            workingDirectory: $(Build.SourcesDirectory)/service
+            workingDirectory: $(Build.SourcesDirectory)
             env:
               DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
             displayName: Regular rebuild of FSharp.Compiler.Service.sln
diff --git a/eng/test-determinism.ps1 b/eng/test-determinism.ps1
index 024fe3a605a..de136c12564 100644
--- a/eng/test-determinism.ps1
+++ b/eng/test-determinism.ps1
@@ -39,7 +39,7 @@ function Run-Build([string]$rootDir, [string]$logFileName) {
   $stopWatch.Stop()
   Write-Host "Cleaning took $($stopWatch.Elapsed)"
 
-  $solution = Join-Path $rootDir (Join-Path "service" "FSharp.Compiler.Service.sln")
+  $solution = Join-Path $rootDir "FSharp.Compiler.Service.sln"
 
   if ($logFileName -eq "") {
     $logFileName = [IO.Path]::GetFileNameWithoutExtension($projectFilePath)
diff --git a/service/global.json b/service/global.json
deleted file mode 100644
index 807abcbe7a1..00000000000
--- a/service/global.json
+++ /dev/null
@@ -1,7 +0,0 @@
-{
-    "sdk": {
-        "version": "6.0.100",
-        "allowPrerelease": true,
-        "rollForward": "minor"
-    }
-}
\ No newline at end of file

From 1874b39dfa769bd3a7c9e448ca21ff593f941ed6 Mon Sep 17 00:00:00 2001
From: Adam Boniecki 
Date: Tue, 24 May 2022 13:48:51 +0200
Subject: [PATCH 13/20] Fix anon records duplicate fields (#13142)

* Add unit test for duplicate fields

Related issue: https://github.com/dotnet/fsharp/issues/11743

* Add new error code and message

* Add a fix in TcAnonRecdExpr

* Add another unit test - Update and copy syntax

* Change error message to include field id

* Add unit test for anon record type annotation

* Add check for duplicate labels in type annotations

* Improve error messages

Also switch from errorR to error in SynType case (SynExpr already used
error)

Co-authored-by: Adam 
---
 src/Compiler/Checking/CheckExpressions.fs     | 12 +++++
 src/Compiler/FSComp.txt                       |  2 +
 src/Compiler/xlf/FSComp.txt.cs.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.de.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.es.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.fr.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.it.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.ja.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.ko.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.pl.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.pt-BR.xlf         | 10 ++++
 src/Compiler/xlf/FSComp.txt.ru.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.tr.xlf            | 10 ++++
 src/Compiler/xlf/FSComp.txt.zh-Hans.xlf       | 10 ++++
 src/Compiler/xlf/FSComp.txt.zh-Hant.xlf       | 10 ++++
 .../RecordTypes/AnonymousRecords.fs           | 46 +++++++++++++++++++
 .../FSharp.Compiler.ComponentTests.fsproj     |  1 +
 17 files changed, 191 insertions(+)
 create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/RecordTypes/AnonymousRecords.fs

diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs
index 1d9d9da70fa..773d1a01ba5 100644
--- a/src/Compiler/Checking/CheckExpressions.fs
+++ b/src/Compiler/Checking/CheckExpressions.fs
@@ -4654,6 +4654,12 @@ and TcAnonRecdType cenv newOk checkConstraints occ env tpenv isStruct args m =
     let unsortedFieldIds = args |> List.map fst |> List.toArray
     let anonInfo = AnonRecdTypeInfo.Create(cenv.thisCcu, tupInfo, unsortedFieldIds)
 
+    // Check for duplicate field IDs
+    unsortedFieldIds
+    |> Array.countBy (fun fieldId -> fieldId.idText)
+    |> Array.iter (fun (idText, count) ->
+        if count > 1 then error (Error (FSComp.SR.tcAnonRecdTypeDuplicateFieldId(idText), m)))
+
     // Sort into canonical order
     let sortedFieldTys, sortedCheckedArgTys = List.zip args argsR |> List.indexed |> List.sortBy (fun (i,_) -> unsortedFieldIds[i].idText) |> List.map snd |> List.unzip
 
@@ -7455,6 +7461,12 @@ and TcRecdExpr cenv (overallTy: TType) env tpenv (inherits, withExprOpt, synRecd
 // Check '{| .... |}'
 and TcAnonRecdExpr cenv (overallTy: TType) env tpenv (isStruct, optOrigSynExpr, unsortedFieldIdsAndSynExprsGiven, mWholeExpr) =
 
+    // Check for duplicate field IDs
+    unsortedFieldIdsAndSynExprsGiven
+    |> List.countBy (fun (fId, _, _) -> fId.idText)
+    |> List.iter (fun (label, count) ->
+        if count > 1 then error (Error (FSComp.SR.tcAnonRecdDuplicateFieldId(label), mWholeExpr)))
+
     match optOrigSynExpr with
     | None ->
         TcNewAnonRecdExpr cenv overallTy env tpenv (isStruct, unsortedFieldIdsAndSynExprsGiven, mWholeExpr)
diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt
index bf1989513f0..d59eaf4ff53 100644
--- a/src/Compiler/FSComp.txt
+++ b/src/Compiler/FSComp.txt
@@ -1642,3 +1642,5 @@ reprStateMachineInvalidForm,"The state machine has an unexpected form"
 3519,tcInlineIfLambdaUsedOnNonInlineFunctionOrMethod,"The 'InlineIfLambda' attribute may only be used on parameters of inlined functions of methods whose type is a function or F# delegate type."
 3520,invalidXmlDocPosition,"XML comment is not placed on a valid language element."
 3521,tcInvalidMemberDeclNameMissingOrHasParen,"Invalid member declaration. The name of the member is missing or has parentheses."
+3522,tcAnonRecdDuplicateFieldId,"The field '%s' appears multiple times in this record expression."
+3523,tcAnonRecdTypeDuplicateFieldId,"The field '%s' appears multiple times in this anonymous record type."
\ No newline at end of file
diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf
index 151f341d14c..92860a2902f 100644
--- a/src/Compiler/xlf/FSComp.txt.cs.xlf
+++ b/src/Compiler/xlf/FSComp.txt.cs.xlf
@@ -617,6 +617,11 @@
         Tato funkce se v této verzi jazyka F# nepodporuje. Abyste mohli tuto funkci používat, možná bude nutné přidat /langversion:preview.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Toto je nesprávný anonymní záznam. Měl by mít pole {0}.
@@ -637,6 +642,11 @@
         Neplatná deklarace typu anonymního záznamu
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Atributy nejde použít pro rozšíření typů.
diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf
index ade8740fa37..22bd16c63c3 100644
--- a/src/Compiler/xlf/FSComp.txt.de.xlf
+++ b/src/Compiler/xlf/FSComp.txt.de.xlf
@@ -617,6 +617,11 @@
         Dieses Feature wird in dieser Version von F# nicht unterstützt. Möglicherweise müssen Sie "/langversion:preview" hinzufügen, um dieses Feature zu verwenden.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Dies ist der falsche anonyme Datensatz. Er muss folgende Felder umfassen: {0}.
@@ -637,6 +642,11 @@
         Ungültige Deklaration für anonymen Datensatztyp.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Attribute können nicht auf Typerweiterungen angewendet werden.
diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf
index 5a6767dc03e..4dc904402bf 100644
--- a/src/Compiler/xlf/FSComp.txt.es.xlf
+++ b/src/Compiler/xlf/FSComp.txt.es.xlf
@@ -617,6 +617,11 @@
         Esta versión de F# no admite esta característica. Es posible que tenga que agregar /langversion:preview para usarla.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Este es un registro anónimo incorrecto. Debe tener los campos {0}.
@@ -637,6 +642,11 @@
         Declaración de tipo de registro anónimo no válido.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Los atributos no se pueden aplicar a las extensiones de tipo.
diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf
index b35af7646d8..0c131e6353c 100644
--- a/src/Compiler/xlf/FSComp.txt.fr.xlf
+++ b/src/Compiler/xlf/FSComp.txt.fr.xlf
@@ -617,6 +617,11 @@
         Cette fonctionnalité n'est pas prise en charge dans cette version de F#. Vous devrez peut-être ajouter /langversion:preview pour pouvoir utiliser cette fonctionnalité.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Il s'agit de l'enregistrement anonyme incorrect. Il doit contenir les champs {0}.
@@ -637,6 +642,11 @@
         Déclaration de type d'enregistrement anonyme non valide.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Impossible d'appliquer des attributs aux extensions de type.
diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf
index 670f488ff43..5a6035a7014 100644
--- a/src/Compiler/xlf/FSComp.txt.it.xlf
+++ b/src/Compiler/xlf/FSComp.txt.it.xlf
@@ -617,6 +617,11 @@
         Questa funzionalità non è supportata in questa versione di F#. Per usare questa funzionalità, potrebbe essere necessario aggiungere /langversion:preview.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Si tratta del record anonimo errato. Deve includere i campi {0}.
@@ -637,6 +642,11 @@
         La dichiarazione di tipo Record anonimo non è valida.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Gli attributi non possono essere applicati a estensioni di tipo.
diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf
index 06f422a854f..d8f1be72618 100644
--- a/src/Compiler/xlf/FSComp.txt.ja.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ja.xlf
@@ -617,6 +617,11 @@
         この機能は、このバージョンの F# ではサポートされていません。この機能を使用するには、/langversion:preview の追加が必要な場合があります。
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         この匿名レコードは正しくありません。フィールド {0} を含んでいる必要があります。
@@ -637,6 +642,11 @@
         匿名レコードの型宣言が無効です。
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         属性を型拡張に適用することはできません。
diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf
index e7e3a0f922d..0cd7929d6ff 100644
--- a/src/Compiler/xlf/FSComp.txt.ko.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ko.xlf
@@ -617,6 +617,11 @@
         이 기능은 이 F# 버전에서 지원되지 않습니다. 이 기능을 사용하기 위해 /langversion:preview를 추가해야 할 수도 있습니다.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         잘못된 익명 레코드입니다. {0} 필드가 있어야 합니다.
@@ -637,6 +642,11 @@
         익명 레코드 형식 선언이 잘못되었습니다.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         형식 확장에 특성을 적용할 수 없습니다.
diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf
index 1c0829bea4f..f92eed64abc 100644
--- a/src/Compiler/xlf/FSComp.txt.pl.xlf
+++ b/src/Compiler/xlf/FSComp.txt.pl.xlf
@@ -617,6 +617,11 @@
         Ta funkcja nie jest obsługiwana w tej wersji języka F#. Aby korzystać z tej funkcji, może być konieczne dodanie parametru /langversion:preview.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         To jest nieprawidłowy rekord anonimowy. Powinien zawierać pola {0}.
@@ -637,6 +642,11 @@
         Nieprawidłowa deklaracja typu rekordu anonimowego.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Atrybutów nie można stosować do rozszerzeń typu.
diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
index 68e52831e1c..0c6ceac112a 100644
--- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
+++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
@@ -617,6 +617,11 @@
         Este recurso não tem suporte nesta versão do F#. Talvez seja necessário adicionar /langversion:preview para usar este recurso.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Este é o registro anônimo errado. Ele deve ter os campos {0}.
@@ -637,6 +642,11 @@
         Declaração inválida de tipo de Registro Anônimo.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Os atributos não podem ser aplicados às extensões de tipo.
diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf
index 9777e20b0ad..22b93f16437 100644
--- a/src/Compiler/xlf/FSComp.txt.ru.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ru.xlf
@@ -617,6 +617,11 @@
         Эта функция не поддерживается в данной версии F#. Возможно, потребуется добавить/langversion:preview, чтобы использовать эту функцию.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Неправильная анонимная запись. Она должна содержать поля {0}.
@@ -637,6 +642,11 @@
         Недопустимое объявление типа анонимной записи.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Атрибуты не могут быть применены к расширениям типа.
diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf
index 23432c905c6..3a720bf0f0d 100644
--- a/src/Compiler/xlf/FSComp.txt.tr.xlf
+++ b/src/Compiler/xlf/FSComp.txt.tr.xlf
@@ -617,6 +617,11 @@
         Bu özellik, bu F# sürümünde desteklenmiyor. Bu özelliği kullanabilmeniz için /langversion:preview eklemeniz gerekebilir.
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         Bu anonim kayıt yanlış. Kayıt, {0} alanlarını içermelidir.
@@ -637,6 +642,11 @@
         Anonim Kayıt türü bildirimi geçersiz.
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         Öznitelikler tür uzantılarına uygulanamaz.
diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
index e72481a46d7..f9eea243b93 100644
--- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
+++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
@@ -617,6 +617,11 @@
         此版本的 F# 不支持此功能。你可能需要添加 /langversion:preview 才可使用此功能。
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         此匿名记录不正确。它应具有字段 {0}。
@@ -637,6 +642,11 @@
         匿名记录类型声明无效。
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         属性不可应用于类型扩展。
diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
index d1f619a520a..d05d8005fea 100644
--- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
+++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
@@ -617,6 +617,11 @@
         此版本的 F# 不支援此功能。您可能需要新增 /langversion:preview 才能使用此功能。
         
       
+      
+        The field '{0}' appears multiple times in this record expression.
+        The field '{0}' appears multiple times in this record expression.
+        
+      
       
         This is the wrong anonymous record. It should have the fields {0}.
         此為錯誤的匿名記錄。其應有欄位 {0}。
@@ -637,6 +642,11 @@
         匿名記錄型別宣告無效。
         
       
+      
+        The field '{0}' appears multiple times in this anonymous record type.
+        The field '{0}' appears multiple times in this anonymous record type.
+        
+      
       
         Attributes cannot be applied to type extensions.
         屬性無法套用到類型延伸模組。
diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/RecordTypes/AnonymousRecords.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/RecordTypes/AnonymousRecords.fs
new file mode 100644
index 00000000000..a45c92751a2
--- /dev/null
+++ b/tests/FSharp.Compiler.ComponentTests/Conformance/RecordTypes/AnonymousRecords.fs
@@ -0,0 +1,46 @@
+// Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information.
+
+namespace FSharp.Compiler.ComponentTests.Conformance
+
+open Xunit
+open FSharp.Test
+open FSharp.Test.Compiler
+
+module AnonymousRecord =
+
+    []
+    let ``Anonymous Records with duplicate labels`` () =
+        FSharp """
+namespace FSharpTest
+
+module AnonRecd =
+    let v = {| A = 1; A = 2 |}
+"""
+        |> compile
+        |> shouldFail
+        |> withErrorCode 3522
+        |> withMessage "The field 'A' appears multiple times in this record expression."
+
+    []
+    let ``Anonymous Records with duplicate labels - Copy and update expression`` () =
+        FSharp """
+namespace FSharpTest
+
+module AnonRecd =
+    let v = {| {| y = 3 |} with y = 2; y = 4 |}
+"""
+        |> compile
+        |> shouldFail
+        |> withErrorCode 3522
+
+    []
+    let ``Anonymous Record type annotation with duplicate labels`` () =
+        FSharp """
+namespace FSharpTest
+
+module AnonRecd =
+    let (f : {| A : int; A : string |} option) = None
+"""
+        |> compile
+        |> shouldFail
+        |> withErrorCode 3523
\ No newline at end of file
diff --git a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj
index 6870a3fbf89..a91bb7b00a2 100644
--- a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj
+++ b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj
@@ -83,6 +83,7 @@
     
     
     
+    
     
     
     

From fcffdd2e8fbe438a545b2eba14120efdd6267328 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Wed, 25 May 2022 14:22:34 +0100
Subject: [PATCH 14/20] Formatting: Apply formatting to AbstractIL (#13173)

* adjust format settings

* apply formatting

* adjust code and apply formatting

* add some spacing
---
 .fantomasignore                           |    3 +-
 src/Compiler/AbstractIL/il.fs             | 4107 +++++++++------
 src/Compiler/AbstractIL/ilascii.fs        |  453 +-
 src/Compiler/AbstractIL/ilbinary.fs       | 1743 +++----
 src/Compiler/AbstractIL/ilmorph.fs        |  463 +-
 src/Compiler/AbstractIL/ilnativeres.fs    |  937 ++--
 src/Compiler/AbstractIL/ilprint.fs        | 2174 ++++----
 src/Compiler/AbstractIL/ilread.fs         | 5680 +++++++++++++--------
 src/Compiler/AbstractIL/ilreflect.fs      | 1853 ++++---
 src/Compiler/AbstractIL/ilsign.fs         | 1165 +++--
 src/Compiler/AbstractIL/ilsupp.fs         | 1154 +++--
 src/Compiler/AbstractIL/ilwritepdb.fs     | 1083 ++--
 src/Compiler/AbstractIL/ilx.fs            |  199 +-
 src/Compiler/Checking/CheckExpressions.fs |    4 +
 src/Compiler/Service/ServiceLexing.fsi    |    1 +
 15 files changed, 12720 insertions(+), 8299 deletions(-)

diff --git a/.fantomasignore b/.fantomasignore
index 0211e8ccbfd..1f44854852e 100644
--- a/.fantomasignore
+++ b/.fantomasignore
@@ -9,11 +9,11 @@ service/
 setup/
 tests/
 vsintegration/
+artifacts/
 
 # Explicitly unformatted implementation files
 
 src/FSharp.Core/**/*.fs
-src/Compiler/AbstractIL/**/*.fs
 src/Compiler/Checking/**/*.fs
 src/Compiler/CodeGen/**/*.fs
 src/Compiler/DependencyManager/**/*.fs
@@ -30,6 +30,7 @@ src/Microsoft.FSharp.Compiler/**/*.fs
 
 # Fantomas limitations on implementation files  (to investigate)
 
+src/Compiler/AbstractIL/ilwrite.fs
 src/Compiler/Utilities/lib.fs
 
 # Fantomas limitations on signature files  (to investigate)
diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs
index 0ba00344546..7cd377cad3d 100644
--- a/src/Compiler/AbstractIL/il.fs
+++ b/src/Compiler/AbstractIL/il.fs
@@ -25,7 +25,9 @@ open Internal.Utilities
 
 let logging = false
 
-let _ = if logging then dprintn "* warning: Il.logging is on"
+let _ =
+    if logging then
+        dprintn "* warning: Il.logging is on"
 
 let int_order = LanguagePrimitives.FastGenericComparer
 
@@ -35,7 +37,10 @@ let notlazy v = Lazy<_>.CreateFromValue v
 /// contain lazy values then we don't add laziness. So if the thing to map
 /// is already evaluated then immediately apply the function.
 let lazyMap f (x: Lazy<_>) =
-      if x.IsValueCreated then notlazy (f (x.Force())) else lazy (f (x.Force()))
+    if x.IsValueCreated then
+        notlazy (f (x.Force()))
+    else
+        lazy (f (x.Force()))
 
 []
 type PrimaryAssembly =
@@ -58,41 +63,50 @@ type PrimaryAssembly =
 let memoizeNamespaceTable = ConcurrentDictionary()
 
 //  ++GLOBAL MUTABLE STATE (concurrency-safe)
-let memoizeNamespaceRightTable = ConcurrentDictionary()
+let memoizeNamespaceRightTable =
+    ConcurrentDictionary()
 
 // ++GLOBAL MUTABLE STATE (concurrency-safe)
 let memoizeNamespacePartTable = ConcurrentDictionary()
 
-
 let splitNameAt (nm: string) idx =
-    if idx < 0 then failwith "splitNameAt: idx < 0"
+    if idx < 0 then
+        failwith "splitNameAt: idx < 0"
+
     let last = nm.Length - 1
-    if idx > last then failwith "splitNameAt: idx > last"
-    (nm.Substring (0, idx)),
-    (if idx < last then nm.Substring (idx+1, last - idx) else "")
+
+    if idx > last then
+        failwith "splitNameAt: idx > last"
+
+    (nm.Substring(0, idx)),
+    (if idx < last then
+         nm.Substring(idx + 1, last - idx)
+     else
+         "")
 
 let rec splitNamespaceAux (nm: string) =
     match nm.IndexOf '.' with
-    | -1 -> [nm]
+    | -1 -> [ nm ]
     | idx ->
         let s1, s2 = splitNameAt nm idx
         let s1 = memoizeNamespacePartTable.GetOrAdd(s1, id)
         s1 :: splitNamespaceAux s2
 
-
 let splitNamespace nm =
-    memoizeNamespaceTable.GetOrAdd (nm, splitNamespaceAux)
+    memoizeNamespaceTable.GetOrAdd(nm, splitNamespaceAux)
 
 let splitNamespaceMemoized nm = splitNamespace nm
 
 // ++GLOBAL MUTABLE STATE (concurrency-safe)
-let memoizeNamespaceArrayTable =
-    ConcurrentDictionary()
+let memoizeNamespaceArrayTable = ConcurrentDictionary()
 
 let splitNamespaceToArray nm =
-    memoizeNamespaceArrayTable.GetOrAdd (nm, fun nm ->
-        let x = Array.ofList (splitNamespace nm)
-        x)
+    memoizeNamespaceArrayTable.GetOrAdd(
+        nm,
+        fun nm ->
+            let x = Array.ofList (splitNamespace nm)
+            x
+    )
 
 let splitILTypeName (nm: string) =
     match nm.LastIndexOf '.' with
@@ -115,11 +129,15 @@ let splitILTypeNameWithPossibleStaticArguments (nm: string) =
 
     let nsp, nm =
         match nm.LastIndexOf '.' with
-        | -1 -> [| |], nm
+        | -1 -> [||], nm
         | idx ->
             let s1, s2 = splitNameAt nm idx
             splitNamespaceToArray s1, s2
-    nsp, (match suffix with None -> nm | Some s -> nm + "," + s)
+
+    nsp,
+    (match suffix with
+     | None -> nm
+     | Some s -> nm + "," + s)
 
 (*
 splitILTypeNameWithPossibleStaticArguments "Foo" = ([| |], "Foo")
@@ -137,37 +155,47 @@ let unsplitTypeName (ns, n) =
 
 let splitTypeNameRightAux (nm: string) =
     let idx = nm.LastIndexOf '.'
-    if idx = -1 then None, nm else
-    let s1, s2 = splitNameAt nm idx
-    Some s1, s2
+
+    if idx = -1 then
+        None, nm
+    else
+        let s1, s2 = splitNameAt nm idx
+        Some s1, s2
 
 let splitTypeNameRight nm =
-    memoizeNamespaceRightTable.GetOrAdd (nm, splitTypeNameRightAux)
+    memoizeNamespaceRightTable.GetOrAdd(nm, splitTypeNameRightAux)
 
 // --------------------------------------------------------------------
 // Ordered lists with a lookup table
 // --------------------------------------------------------------------
 
 /// This is used to store event, property and field maps.
-type LazyOrderedMultiMap<'Key, 'Data when 'Key : equality>(keyf : 'Data -> 'Key, lazyItems : Lazy<'Data list>) =
+type LazyOrderedMultiMap<'Key, 'Data when 'Key: equality>(keyf: 'Data -> 'Key, lazyItems: Lazy<'Data list>) =
 
     let quickMap =
-        lazyItems |> lazyMap (fun entries ->
+        lazyItems
+        |> lazyMap (fun entries ->
             let t = Dictionary<_, _>(entries.Length, HashIdentity.Structural)
+
             for y in entries do
                 let key = keyf y
+
                 let v =
                     match t.TryGetValue key with
                     | true, v -> v
                     | _ -> []
+
                 t[key] <- y :: v
+
             t)
 
     member self.Entries() = lazyItems.Force()
 
-    member self.Add y = new LazyOrderedMultiMap<'Key, 'Data>(keyf, lazyItems |> lazyMap (fun x -> y :: x))
+    member self.Add y =
+        new LazyOrderedMultiMap<'Key, 'Data>(keyf, lazyItems |> lazyMap (fun x -> y :: x))
 
-    member self.Filter f = new LazyOrderedMultiMap<'Key, 'Data>(keyf, lazyItems |> lazyMap (List.filter f))
+    member self.Filter f =
+        new LazyOrderedMultiMap<'Key, 'Data>(keyf, lazyItems |> lazyMap (List.filter f))
 
     member self.Item
         with get x =
@@ -175,33 +203,41 @@ type LazyOrderedMultiMap<'Key, 'Data when 'Key : equality>(keyf : 'Data -> 'Key,
             | true, v -> v
             | _ -> []
 
-
 //---------------------------------------------------------------------
 // SHA1 hash-signing algorithm. Used to get the public key token from
 // the public key.
 //---------------------------------------------------------------------
 
-
 let b0 n = (n &&& 0xFF)
 let b1 n = ((n >>> 8) &&& 0xFF)
 let b2 n = ((n >>> 16) &&& 0xFF)
 let b3 n = ((n >>> 24) &&& 0xFF)
 
-
 module SHA1 =
 
     let inline (>>>&) (x: int) (y: int) = int32 (uint32 x >>> y)
 
-    let f(t, b, c, d) =
-        if t < 20 then (b &&& c) ||| ((~~~b) &&& d)
-        elif t < 40 then b ^^^ c ^^^ d
-        elif t < 60 then (b &&& c) ||| (b &&& d) ||| (c &&& d)
-        else b ^^^ c ^^^ d
+    let f (t, b, c, d) =
+        if t < 20 then
+            (b &&& c) ||| ((~~~b) &&& d)
+        elif t < 40 then
+            b ^^^ c ^^^ d
+        elif t < 60 then
+            (b &&& c) ||| (b &&& d) ||| (c &&& d)
+        else
+            b ^^^ c ^^^ d
+
+    []
+    let k0to19 = 0x5A827999
 
-    let [] k0to19 = 0x5A827999
-    let [] k20to39 = 0x6ED9EBA1
-    let [] k40to59 = 0x8F1BBCDC
-    let [] k60to79 = 0xCA62C1D6
+    []
+    let k20to39 = 0x6ED9EBA1
+
+    []
+    let k40to59 = 0x8F1BBCDC
+
+    []
+    let k60to79 = 0xCA62C1D6
 
     let k t =
         if t < 20 then k0to19
@@ -209,36 +245,56 @@ module SHA1 =
         elif t < 60 then k40to59
         else k60to79
 
-
     type SHAStream =
-        { stream: byte[]
-          mutable pos: int
-          mutable eof: bool }
-
-    let rotLeft32 x n = (x <<< n) ||| (x >>>& (32-n))
+        {
+            stream: byte[]
+            mutable pos: int
+            mutable eof: bool
+        }
 
+    let rotLeft32 x n = (x <<< n) ||| (x >>>& (32 - n))
 
     // padding and length (in bits!) recorded at end
     let shaAfterEof sha =
         let n = sha.pos
         let len = sha.stream.Length
-        if n = len then 0x80
+
+        if n = len then
+            0x80
         else
-          let padded_len = (((len + 9 + 63) / 64) * 64) - 8
-          if n < padded_len - 8 then 0x0
-          elif (n &&& 63) = 56 then int32 ((int64 len * int64 8) >>> 56) &&& 0xff
-          elif (n &&& 63) = 57 then int32 ((int64 len * int64 8) >>> 48) &&& 0xff
-          elif (n &&& 63) = 58 then int32 ((int64 len * int64 8) >>> 40) &&& 0xff
-          elif (n &&& 63) = 59 then int32 ((int64 len * int64 8) >>> 32) &&& 0xff
-          elif (n &&& 63) = 60 then int32 ((int64 len * int64 8) >>> 24) &&& 0xff
-          elif (n &&& 63) = 61 then int32 ((int64 len * int64 8) >>> 16) &&& 0xff
-          elif (n &&& 63) = 62 then int32 ((int64 len * int64 8) >>> 8) &&& 0xff
-          elif (n &&& 63) = 63 then (sha.eof <- true; int32 (int64 len * int64 8) &&& 0xff)
-          else 0x0
+            let padded_len = (((len + 9 + 63) / 64) * 64) - 8
+
+            if n < padded_len - 8 then
+                0x0
+            elif (n &&& 63) = 56 then
+                int32 ((int64 len * int64 8) >>> 56) &&& 0xff
+            elif (n &&& 63) = 57 then
+                int32 ((int64 len * int64 8) >>> 48) &&& 0xff
+            elif (n &&& 63) = 58 then
+                int32 ((int64 len * int64 8) >>> 40) &&& 0xff
+            elif (n &&& 63) = 59 then
+                int32 ((int64 len * int64 8) >>> 32) &&& 0xff
+            elif (n &&& 63) = 60 then
+                int32 ((int64 len * int64 8) >>> 24) &&& 0xff
+            elif (n &&& 63) = 61 then
+                int32 ((int64 len * int64 8) >>> 16) &&& 0xff
+            elif (n &&& 63) = 62 then
+                int32 ((int64 len * int64 8) >>> 8) &&& 0xff
+            elif (n &&& 63) = 63 then
+                (sha.eof <- true
+                 int32 (int64 len * int64 8) &&& 0xff)
+            else
+                0x0
 
     let shaRead8 sha =
         let s = sha.stream
-        let b = if sha.pos >= s.Length then shaAfterEof sha else int32 s[sha.pos]
+
+        let b =
+            if sha.pos >= s.Length then
+                shaAfterEof sha
+            else
+                int32 s[sha.pos]
+
         sha.pos <- sha.pos + 1
         b
 
@@ -262,16 +318,20 @@ module SHA1 =
         let mutable d = 0
         let mutable e = 0
         let w = Array.create 80 0x00
+
         while (not sha.eof) do
             for i = 0 to 15 do
                 w[i] <- shaRead32 sha
+
             for t = 16 to 79 do
-                w[t] <- rotLeft32 (w[t-3] ^^^ w[t-8] ^^^ w[t-14] ^^^ w[t-16]) 1
+                w[t] <- rotLeft32 (w[t - 3] ^^^ w[t - 8] ^^^ w[t - 14] ^^^ w[t - 16]) 1
+
             a <- h0
             b <- h1
             c <- h2
             d <- h3
             e <- h4
+
             for t = 0 to 79 do
                 let temp = (rotLeft32 a 5) + f (t, b, c, d) + e + w[t] + k t
                 e <- d
@@ -279,19 +339,21 @@ module SHA1 =
                 c <- rotLeft32 b 30
                 b <- a
                 a <- temp
+
             h0 <- h0 + a
             h1 <- h1 + b
             h2 <- h2 + c
             h3 <- h3 + d
             h4 <- h4 + e
+
         h0, h1, h2, h3, h4
 
     let sha1HashBytes s =
-        let _h0, _h1, _h2, h3, h4 = sha1Hash { stream = s; pos = 0; eof = false }   // the result of the SHA algorithm is stored in registers 3 and 4
-        Array.map byte [| b0 h4; b1 h4; b2 h4; b3 h4; b0 h3; b1 h3; b2 h3; b3 h3; |]
+        let _h0, _h1, _h2, h3, h4 = sha1Hash { stream = s; pos = 0; eof = false } // the result of the SHA algorithm is stored in registers 3 and 4
+        Array.map byte [| b0 h4; b1 h4; b2 h4; b3 h4; b0 h3; b1 h3; b2 h3; b3 h3 |]
 
     let sha1HashInt64 s =
-        let _h0,_h1,_h2,h3,h4 = sha1Hash { stream = s; pos = 0; eof = false }   // the result of the SHA algorithm is stored in registers 3 and 4
+        let _h0, _h1, _h2, h3, h4 = sha1Hash { stream = s; pos = 0; eof = false } // the result of the SHA algorithm is stored in registers 3 and 4
         (int64 h3 <<< 32) ||| int64 h4
 
 let sha1HashBytes s = SHA1.sha1HashBytes s
@@ -309,12 +371,17 @@ type ILVersionInfo =
     val Build: uint16
     val Revision: uint16
 
-    new (major, minor, build, revision) =
-        { Major = major; Minor = minor; Build = build; Revision = revision }
+    new(major, minor, build, revision) =
+        {
+            Major = major
+            Minor = minor
+            Build = build
+            Revision = revision
+        }
 
     /// For debugging
-    override x.ToString() = sprintf "ILVersionInfo: %u %u %u %u" x.Major x.Minor x.Build x.Revision
-
+    override x.ToString() =
+        sprintf "ILVersionInfo: %u %u %u %u" x.Major x.Minor x.Build x.Revision
 
 type Locale = string
 
@@ -325,112 +392,139 @@ type PublicKey =
 
     | PublicKeyToken of byte[]
 
-    member x.IsKey=match x with PublicKey _ -> true | _ -> false
+    member x.IsKey =
+        match x with
+        | PublicKey _ -> true
+        | _ -> false
 
-    member x.IsKeyToken=match x with PublicKeyToken _ -> true | _ -> false
+    member x.IsKeyToken =
+        match x with
+        | PublicKeyToken _ -> true
+        | _ -> false
 
-    member x.Key=match x with PublicKey b -> b | _ -> invalidOp "not a key"
+    member x.Key =
+        match x with
+        | PublicKey b -> b
+        | _ -> invalidOp "not a key"
 
-    member x.KeyToken=match x with PublicKeyToken b -> b | _ -> invalidOp"not a key token"
+    member x.KeyToken =
+        match x with
+        | PublicKeyToken b -> b
+        | _ -> invalidOp "not a key token"
 
     member x.ToToken() =
         match x with
         | PublicKey bytes -> SHA1.sha1HashBytes bytes
         | PublicKeyToken token -> token
 
-    static member KeyAsToken key = PublicKeyToken (PublicKey(key).ToToken())
+    static member KeyAsToken key =
+        PublicKeyToken(PublicKey(key).ToToken())
 
 []
 type AssemblyRefData =
-    { assemRefName: string
-      assemRefHash: byte[] option
-      assemRefPublicKeyInfo: PublicKey option
-      assemRefRetargetable: bool
-      assemRefVersion: ILVersionInfo option
-      assemRefLocale: Locale option }
+    {
+        assemRefName: string
+        assemRefHash: byte[] option
+        assemRefPublicKeyInfo: PublicKey option
+        assemRefRetargetable: bool
+        assemRefVersion: ILVersionInfo option
+        assemRefLocale: Locale option
+    }
 
 /// Global state: table of all assembly references keyed by AssemblyRefData.
 let AssemblyRefUniqueStampGenerator = UniqueStampGenerator()
 
-let isMscorlib data =
-    data.assemRefName = "mscorlib"
+let isMscorlib data = data.assemRefName = "mscorlib"
 
 []
 type ILAssemblyRef(data) =
     let pkToken key =
         match key with
-        | Some (PublicKey bytes) -> Some (PublicKey (SHA1.sha1HashBytes bytes))
-        | Some (PublicKeyToken token) -> Some (PublicKey token)
+        | Some (PublicKey bytes) -> Some(PublicKey(SHA1.sha1HashBytes bytes))
+        | Some (PublicKeyToken token) -> Some(PublicKey token)
         | None -> None
 
     let uniqueStamp =
-        AssemblyRefUniqueStampGenerator.Encode { data with assemRefPublicKeyInfo = pkToken data.assemRefPublicKeyInfo }
+        AssemblyRefUniqueStampGenerator.Encode
+            { data with
+                assemRefPublicKeyInfo = pkToken data.assemRefPublicKeyInfo
+            }
 
     let uniqueIgnoringVersionStamp =
-        AssemblyRefUniqueStampGenerator.Encode { data with assemRefVersion = None; assemRefPublicKeyInfo = pkToken data.assemRefPublicKeyInfo }
+        AssemblyRefUniqueStampGenerator.Encode
+            { data with
+                assemRefVersion = None
+                assemRefPublicKeyInfo = pkToken data.assemRefPublicKeyInfo
+            }
 
-    member x.Name=data.assemRefName
+    member x.Name = data.assemRefName
 
-    member x.Hash=data.assemRefHash
+    member x.Hash = data.assemRefHash
 
-    member x.PublicKey=data.assemRefPublicKeyInfo
+    member x.PublicKey = data.assemRefPublicKeyInfo
 
-    member x.Retargetable=data.assemRefRetargetable
+    member x.Retargetable = data.assemRefRetargetable
 
-    member x.Version=data.assemRefVersion
+    member x.Version = data.assemRefVersion
 
-    member x.Locale=data.assemRefLocale
+    member x.Locale = data.assemRefLocale
 
-    member x.UniqueStamp=uniqueStamp
+    member x.UniqueStamp = uniqueStamp
 
-    member x.UniqueIgnoringVersionStamp=uniqueIgnoringVersionStamp
+    member x.UniqueIgnoringVersionStamp = uniqueIgnoringVersionStamp
 
-    member x.EqualsIgnoringVersion (aref: ILAssemblyRef) =
+    member x.EqualsIgnoringVersion(aref: ILAssemblyRef) =
         aref.UniqueIgnoringVersionStamp = uniqueIgnoringVersionStamp
 
     override x.GetHashCode() = uniqueStamp
 
-    override x.Equals yobj = ((yobj :?> ILAssemblyRef).UniqueStamp = uniqueStamp)
+    override x.Equals yobj =
+        ((yobj :?> ILAssemblyRef).UniqueStamp = uniqueStamp)
 
     interface IComparable with
-        override x.CompareTo yobj = compare (yobj :?> ILAssemblyRef).UniqueStamp uniqueStamp
+        override x.CompareTo yobj =
+            compare (yobj :?> ILAssemblyRef).UniqueStamp uniqueStamp
 
-    static member Create (name, hash, publicKey, retargetable, version, locale) =
+    static member Create(name, hash, publicKey, retargetable, version, locale) =
         ILAssemblyRef
-            { assemRefName=name
-              assemRefHash=hash
-              assemRefPublicKeyInfo=publicKey
-              assemRefRetargetable=retargetable
-              assemRefVersion=version
-              assemRefLocale=locale }
+            {
+                assemRefName = name
+                assemRefHash = hash
+                assemRefPublicKeyInfo = publicKey
+                assemRefRetargetable = retargetable
+                assemRefVersion = version
+                assemRefLocale = locale
+            }
 
-    static member FromAssemblyName (aname: AssemblyName) =
+    static member FromAssemblyName(aname: AssemblyName) =
 
         let locale = None
 
         let publicKey =
-           match aname.GetPublicKey() with
-           | null | [| |] ->
-               match aname.GetPublicKeyToken() with
-               | null | [| |] -> None
-               | bytes -> Some (PublicKeyToken bytes)
-           | bytes ->
-               Some (PublicKey bytes)
+            match aname.GetPublicKey() with
+            | null
+            | [||] ->
+                match aname.GetPublicKeyToken() with
+                | null
+                | [||] -> None
+                | bytes -> Some(PublicKeyToken bytes)
+            | bytes -> Some(PublicKey bytes)
 
         let version =
-           match aname.Version with
-           | null -> None
-           | v -> Some (ILVersionInfo (uint16 v.Major, uint16 v.Minor, uint16 v.Build, uint16 v.Revision))
+            match aname.Version with
+            | null -> None
+            | v -> Some(ILVersionInfo(uint16 v.Major, uint16 v.Minor, uint16 v.Build, uint16 v.Revision))
 
         let retargetable = aname.Flags = AssemblyNameFlags.Retargetable
 
-        ILAssemblyRef.Create (aname.Name, None, publicKey, retargetable, version, locale)
+        ILAssemblyRef.Create(aname.Name, None, publicKey, retargetable, version, locale)
 
     member aref.QualifiedName =
         let b = StringBuilder(100)
         let add (s: string) = b.Append s |> ignore
         let addC (s: char) = b.Append s |> ignore
         add aref.Name
+
         match aref.Version with
         | None -> ()
         | Some version ->
@@ -443,45 +537,57 @@ type ILAssemblyRef(data) =
             add "."
             add (string (int version.Revision))
             add ", Culture="
+
             match aref.Locale with
             | None -> add "neutral"
             | Some b -> add b
+
             add ", PublicKeyToken="
+
             match aref.PublicKey with
             | None -> add "null"
             | Some pki ->
-                  let pkt = pki.ToToken()
-                  let convDigit digit =
-                      let digitc =
-                          if digit < 10
-                          then Convert.ToInt32 '0' + digit
-                          else Convert.ToInt32 'a' + (digit - 10)
-                      Convert.ToChar digitc
-                  for i = 0 to pkt.Length-1 do
-                      let v = pkt[i]
-                      addC (convDigit (int32 v / 16))
-                      addC (convDigit (int32 v % 16))
+                let pkt = pki.ToToken()
+
+                let convDigit digit =
+                    let digitc =
+                        if digit < 10 then
+                            Convert.ToInt32 '0' + digit
+                        else
+                            Convert.ToInt32 'a' + (digit - 10)
+
+                    Convert.ToChar digitc
+
+                for i = 0 to pkt.Length - 1 do
+                    let v = pkt[i]
+                    addC (convDigit (int32 v / 16))
+                    addC (convDigit (int32 v % 16))
             // retargetable can be true only for system assemblies that definitely have Version
             if aref.Retargetable then
                 add ", Retargetable=Yes"
+
         b.ToString()
 
 []
 type ILModuleRef =
-    { name: string
-      hasMetadata: bool
-      hash: byte[] option }
+    {
+        name: string
+        hasMetadata: bool
+        hash: byte[] option
+    }
 
-    static member Create (name, hasMetadata, hash) =
-        { name=name
-          hasMetadata= hasMetadata
-          hash=hash }
+    static member Create(name, hasMetadata, hash) =
+        {
+            name = name
+            hasMetadata = hasMetadata
+            hash = hash
+        }
 
-    member x.Name=x.name
+    member x.Name = x.name
 
-    member x.HasMetadata=x.hasMetadata
+    member x.HasMetadata = x.hasMetadata
 
-    member x.Hash=x.hash
+    member x.Hash = x.hash
 
 []
 []
@@ -491,12 +597,15 @@ type ILScopeRef =
     | Assembly of ILAssemblyRef
     | PrimaryAssembly
 
-    member x.IsLocalRef = match x with ILScopeRef.Local -> true | _ -> false
+    member x.IsLocalRef =
+        match x with
+        | ILScopeRef.Local -> true
+        | _ -> false
 
     member x.QualifiedName =
         match x with
         | ILScopeRef.Local -> ""
-        | ILScopeRef.Module mref -> "module "+mref.Name
+        | ILScopeRef.Module mref -> "module " + mref.Name
         | ILScopeRef.Assembly aref -> aref.QualifiedName
         | ILScopeRef.PrimaryAssembly -> ""
 
@@ -513,12 +622,15 @@ type ILArrayShape =
 
     static member SingleDimensional = ILArrayShapeStatics.SingleDimensional
 
-    static member FromRank n = if n = 1 then ILArrayShape.SingleDimensional else ILArrayShape (List.replicate n (Some 0, None))
-
+    static member FromRank n =
+        if n = 1 then
+            ILArrayShape.SingleDimensional
+        else
+            ILArrayShape(List.replicate n (Some 0, None))
 
 and ILArrayShapeStatics() =
 
-    static let singleDimensional = ILArrayShape [(Some 0, None)]
+    static let singleDimensional = ILArrayShape [ (Some 0, None) ]
 
     static member SingleDimensional = singleDimensional
 
@@ -547,11 +659,20 @@ type ILCallingConv =
 
     member x.BasicConv = let (Callconv (_a, b)) = x in b
 
-    member x.IsInstance = match x.ThisConv with ILThisConvention.Instance -> true | _ -> false
+    member x.IsInstance =
+        match x.ThisConv with
+        | ILThisConvention.Instance -> true
+        | _ -> false
 
-    member x.IsInstanceExplicit = match x.ThisConv with ILThisConvention.InstanceExplicit -> true | _ -> false
+    member x.IsInstanceExplicit =
+        match x.ThisConv with
+        | ILThisConvention.InstanceExplicit -> true
+        | _ -> false
 
-    member x.IsStatic = match x.ThisConv with ILThisConvention.Static -> true | _ -> false
+    member x.IsStatic =
+        match x.ThisConv with
+        | ILThisConvention.Static -> true
+        | _ -> false
 
     static member Instance = ILCallingConvStatics.Instance
 
@@ -560,9 +681,9 @@ type ILCallingConv =
 /// Static storage to amortize the allocation of ILCallingConv.Instance and ILCallingConv.Static.
 and ILCallingConvStatics() =
 
-    static let instanceCallConv = Callconv (ILThisConvention.Instance, ILArgConvention.Default)
+    static let instanceCallConv = Callconv(ILThisConvention.Instance, ILArgConvention.Default)
 
-    static let staticCallConv = Callconv (ILThisConvention.Static, ILArgConvention.Default)
+    static let staticCallConv = Callconv(ILThisConvention.Static, ILArgConvention.Default)
 
     static member Instance = instanceCallConv
 
@@ -575,22 +696,27 @@ type ILBoxity =
 // IL type references have a pre-computed hash code to enable quick lookup tables during binary generation.
 []
 type ILTypeRef =
-    { trefScope: ILScopeRef
-      trefEnclosing: string list
-      trefName: string
-      hashCode: int
-      mutable asBoxedType: ILType }
+    {
+        trefScope: ILScopeRef
+        trefEnclosing: string list
+        trefName: string
+        hashCode: int
+        mutable asBoxedType: ILType
+    }
 
     static member ComputeHash(scope, enclosing, name) =
         hash scope * 17 ^^^ (hash enclosing * 101 <<< 1) ^^^ (hash name * 47 <<< 2)
 
-    static member Create (scope, enclosing, name) =
+    static member Create(scope, enclosing, name) =
         let hashCode = ILTypeRef.ComputeHash(scope, enclosing, name)
-        { trefScope=scope
-          trefEnclosing=enclosing
-          trefName=name
-          hashCode=hashCode
-          asBoxedType = Unchecked.defaultof<_> }
+
+        {
+            trefScope = scope
+            trefEnclosing = enclosing
+            trefName = name
+            hashCode = hashCode
+            asBoxedType = Unchecked.defaultof<_>
+        }
 
     member x.Scope = x.trefScope
 
@@ -600,14 +726,15 @@ type ILTypeRef =
 
     member x.ApproxId = x.hashCode
 
-    member x.AsBoxedType (tspec: ILTypeSpec) =
+    member x.AsBoxedType(tspec: ILTypeSpec) =
         if isNil tspec.tspecInst then
             let v = x.asBoxedType
+
             match box v with
             | null ->
-               let r = ILType.Boxed tspec
-               x.asBoxedType <- r
-               r
+                let r = ILType.Boxed tspec
+                x.asBoxedType <- r
+                r
             | _ -> v
         else
             ILType.Boxed tspec
@@ -616,14 +743,16 @@ type ILTypeRef =
 
     override x.Equals yobj =
         let y = (yobj :?> ILTypeRef)
-        (x.ApproxId = y.ApproxId) &&
-        (x.Scope = y.Scope) &&
-        (x.Name = y.Name) &&
-        (x.Enclosing = y.Enclosing)
 
-    member x.EqualsWithPrimaryScopeRef(primaryScopeRef:ILScopeRef, yobj:obj) =
+        (x.ApproxId = y.ApproxId)
+        && (x.Scope = y.Scope)
+        && (x.Name = y.Name)
+        && (x.Enclosing = y.Enclosing)
+
+    member x.EqualsWithPrimaryScopeRef(primaryScopeRef: ILScopeRef, yobj: obj) =
         let y = (yobj :?> ILTypeRef)
-        let isPrimary (v:ILTypeRef) =
+
+        let isPrimary (v: ILTypeRef) =
             match v.Scope with
             | ILScopeRef.PrimaryAssembly -> true
             | _ -> false
@@ -631,39 +760,71 @@ type ILTypeRef =
         // Since we can remap the scope, we need to recompute hash ... this is not an expensive operation
         let isPrimaryX = isPrimary x
         let isPrimaryY = isPrimary y
-        let xApproxId = if isPrimaryX && not(isPrimaryY) then ILTypeRef.ComputeHash(primaryScopeRef, x.Enclosing, x.Name) else x.ApproxId
-        let yApproxId = if isPrimaryY && not(isPrimaryX) then ILTypeRef.ComputeHash(primaryScopeRef, y.Enclosing, y.Name) else y.ApproxId
-        let xScope = if isPrimaryX then primaryScopeRef else x.Scope
-        let yScope = if isPrimaryY then primaryScopeRef else y.Scope
 
-        (xApproxId = yApproxId) &&
-        (xScope = yScope) &&
-        (x.Name = y.Name) &&
-        (x.Enclosing = y.Enclosing)
+        let xApproxId =
+            if isPrimaryX && not (isPrimaryY) then
+                ILTypeRef.ComputeHash(primaryScopeRef, x.Enclosing, x.Name)
+            else
+                x.ApproxId
+
+        let yApproxId =
+            if isPrimaryY && not (isPrimaryX) then
+                ILTypeRef.ComputeHash(primaryScopeRef, y.Enclosing, y.Name)
+            else
+                y.ApproxId
+
+        let xScope =
+            if isPrimaryX then
+                primaryScopeRef
+            else
+                x.Scope
+
+        let yScope =
+            if isPrimaryY then
+                primaryScopeRef
+            else
+                y.Scope
+
+        (xApproxId = yApproxId)
+        && (xScope = yScope)
+        && (x.Name = y.Name)
+        && (x.Enclosing = y.Enclosing)
 
     interface IComparable with
 
         override x.CompareTo yobj =
             let y = (yobj :?> ILTypeRef)
             let c = compare x.ApproxId y.ApproxId
-            if c <> 0 then c else
-            let c = compare x.Scope y.Scope
-            if c <> 0 then c else
-            let c = compare x.Name y.Name
-            if c <> 0 then c else
-            compare x.Enclosing y.Enclosing
 
-    member tref.FullName = String.concat "." (tref.Enclosing @ [tref.Name])
+            if c <> 0 then
+                c
+            else
+                let c = compare x.Scope y.Scope
+
+                if c <> 0 then
+                    c
+                else
+                    let c = compare x.Name y.Name
+
+                    if c <> 0 then
+                        c
+                    else
+                        compare x.Enclosing y.Enclosing
+
+    member tref.FullName = String.concat "." (tref.Enclosing @ [ tref.Name ])
 
     member tref.BasicQualifiedName =
-        (String.concat "+" (tref.Enclosing @ [ tref.Name ] )).Replace(",", @"\,")
+        (String.concat "+" (tref.Enclosing @ [ tref.Name ])).Replace(",", @"\,")
 
     member tref.AddQualifiedNameExtension basic =
         let sco = tref.Scope.QualifiedName
-        if sco = "" then basic else String.concat ", " [basic;sco]
 
-    member tref.QualifiedName =
-        tref.AddQualifiedNameExtension tref.BasicQualifiedName
+        if sco = "" then
+            basic
+        else
+            String.concat ", " [ basic; sco ]
+
+    member tref.QualifiedName = tref.AddQualifiedNameExtension tref.BasicQualifiedName
 
     /// For debugging
     []
@@ -672,49 +833,63 @@ type ILTypeRef =
     /// For debugging
     override x.ToString() = x.FullName
 
+and [] ILTypeSpec =
+    {
+        tspecTypeRef: ILTypeRef
+        /// The type instantiation if the type is generic.
+        tspecInst: ILGenericArgs
+    }
 
-and []
-    ILTypeSpec =
-    { tspecTypeRef: ILTypeRef
-      /// The type instantiation if the type is generic.
-      tspecInst: ILGenericArgs }
-
-    member x.TypeRef=x.tspecTypeRef
+    member x.TypeRef = x.tspecTypeRef
 
-    member x.Scope=x.TypeRef.Scope
+    member x.Scope = x.TypeRef.Scope
 
-    member x.Enclosing=x.TypeRef.Enclosing
+    member x.Enclosing = x.TypeRef.Enclosing
 
-    member x.Name=x.TypeRef.Name
+    member x.Name = x.TypeRef.Name
 
-    member x.GenericArgs=x.tspecInst
+    member x.GenericArgs = x.tspecInst
 
-    static member Create (typeRef, instantiation) = { tspecTypeRef =typeRef; tspecInst=instantiation }
+    static member Create(typeRef, instantiation) =
+        {
+            tspecTypeRef = typeRef
+            tspecInst = instantiation
+        }
 
     member x.BasicQualifiedName =
         let tc = x.TypeRef.BasicQualifiedName
+
         if isNil x.GenericArgs then
             tc
         else
-            tc + "[" + String.concat "," (x.GenericArgs |> List.map (fun arg -> "[" + arg.QualifiedName + "]")) + "]"
+            tc
+            + "["
+            + String.concat "," (x.GenericArgs |> List.map (fun arg -> "[" + arg.QualifiedName + "]"))
+            + "]"
 
     member x.AddQualifiedNameExtension basic =
         x.TypeRef.AddQualifiedNameExtension basic
 
-    member x.FullName=x.TypeRef.FullName
+    member x.FullName = x.TypeRef.FullName
 
     /// For debugging
     []
     member x.DebugText = x.ToString()
 
-    member x.EqualsWithPrimaryScopeRef(primaryScopeRef:ILScopeRef, yobj:obj) =
+    member x.EqualsWithPrimaryScopeRef(primaryScopeRef: ILScopeRef, yobj: obj) =
         let y = (yobj :?> ILTypeSpec)
-        x.tspecTypeRef.EqualsWithPrimaryScopeRef(primaryScopeRef, y.TypeRef) && (x.GenericArgs = y.GenericArgs)
 
-    override x.ToString() = x.TypeRef.ToString() + if isNil x.GenericArgs then "" else "<...>"
+        x.tspecTypeRef.EqualsWithPrimaryScopeRef(primaryScopeRef, y.TypeRef)
+        && (x.GenericArgs = y.GenericArgs)
 
-and []
-    ILType =
+    override x.ToString() =
+        x.TypeRef.ToString()
+        + if isNil x.GenericArgs then
+              ""
+          else
+              "<...>"
+
+and [] ILType =
     | Void
     | Array of ILArrayShape * ILType
     | Value of ILTypeSpec
@@ -729,8 +904,9 @@ and [ "!" + string n
         | ILType.Modified (_, _ty1, ty2) -> ty2.BasicQualifiedName
-        | ILType.Array (ILArrayShape s, ty) -> ty.BasicQualifiedName + "[" + String(',', s.Length-1) + "]"
-        | ILType.Value tr | ILType.Boxed tr -> tr.BasicQualifiedName
+        | ILType.Array (ILArrayShape s, ty) -> ty.BasicQualifiedName + "[" + String(',', s.Length - 1) + "]"
+        | ILType.Value tr
+        | ILType.Boxed tr -> tr.BasicQualifiedName
         | ILType.Void -> "void"
         | ILType.Ptr _ty -> failwith "unexpected pointer type"
         | ILType.Byref _ty -> failwith "unexpected byref type"
@@ -740,19 +916,20 @@ and [ basic
         | ILType.Modified (_, _ty1, ty2) -> ty2.AddQualifiedNameExtension basic
-        | ILType.Array (ILArrayShape(_s), ty) -> ty.AddQualifiedNameExtension basic
-        | ILType.Value tr | ILType.Boxed tr -> tr.AddQualifiedNameExtension basic
+        | ILType.Array (ILArrayShape (_s), ty) -> ty.AddQualifiedNameExtension basic
+        | ILType.Value tr
+        | ILType.Boxed tr -> tr.AddQualifiedNameExtension basic
         | ILType.Void -> failwith "void"
         | ILType.Ptr _ty -> failwith "unexpected pointer type"
         | ILType.Byref _ty -> failwith "unexpected byref type"
         | ILType.FunctionPointer _mref -> failwith "unexpected function pointer type"
 
-    member x.QualifiedName =
-        x.AddQualifiedNameExtension(x.BasicQualifiedName)
+    member x.QualifiedName = x.AddQualifiedNameExtension(x.BasicQualifiedName)
 
     member x.TypeSpec =
         match x with
-        | ILType.Boxed tr | ILType.Value tr -> tr
+        | ILType.Boxed tr
+        | ILType.Value tr -> tr
         | _ -> invalidOp "not a nominal type"
 
     member x.Boxity =
@@ -763,22 +940,26 @@ and [ tspec.TypeRef
+        | ILType.Boxed tspec
+        | ILType.Value tspec -> tspec.TypeRef
         | _ -> invalidOp "not a nominal type"
 
     member x.IsNominal =
         match x with
-        | ILType.Boxed _ | ILType.Value _ -> true
+        | ILType.Boxed _
+        | ILType.Value _ -> true
         | _ -> false
 
     member x.GenericArgs =
         match x with
-        | ILType.Boxed tspec | ILType.Value tspec -> tspec.GenericArgs
+        | ILType.Boxed tspec
+        | ILType.Value tspec -> tspec.GenericArgs
         | _ -> []
 
     member x.IsTyvar =
         match x with
-        | ILType.TypeVar _ -> true | _ -> false
+        | ILType.TypeVar _ -> true
+        | _ -> false
 
     /// For debugging
     []
@@ -786,28 +967,36 @@ and []
-    ILCallingSignature =
-    { CallingConv: ILCallingConv
-      ArgTypes: ILTypes
-      ReturnType: ILType }
+and [] ILCallingSignature =
+    {
+        CallingConv: ILCallingConv
+        ArgTypes: ILTypes
+        ReturnType: ILType
+    }
 
 and ILGenericArgs = ILType list
 
 and ILTypes = ILType list
 
-let mkILCallSig (cc, args, ret) = { ArgTypes=args; CallingConv=cc; ReturnType=ret}
+let mkILCallSig (cc, args, ret) =
+    {
+        ArgTypes = args
+        CallingConv = cc
+        ReturnType = ret
+    }
 
 let mkILBoxedType (tspec: ILTypeSpec) = tspec.TypeRef.AsBoxedType tspec
 
 []
 type ILMethodRef =
-    { mrefParent: ILTypeRef
-      mrefCallconv: ILCallingConv
-      mrefGenericArity: int
-      mrefName: string
-      mrefArgs: ILTypes
-      mrefReturn: ILType }
+    {
+        mrefParent: ILTypeRef
+        mrefCallconv: ILCallingConv
+        mrefGenericArity: int
+        mrefName: string
+        mrefArgs: ILTypes
+        mrefReturn: ILType
+    }
 
     member x.DeclaringTypeRef = x.mrefParent
 
@@ -825,51 +1014,64 @@ type ILMethodRef =
 
     member x.CallingSignature = mkILCallSig (x.CallingConv, x.ArgTypes, x.ReturnType)
 
-    static member Create (enclosingTypeRef, callingConv, name, genericArity, argTypes, returnType) =
-        { mrefParent=enclosingTypeRef
-          mrefCallconv=callingConv
-          mrefName=name
-          mrefGenericArity=genericArity
-          mrefArgs=argTypes
-          mrefReturn=returnType }
+    static member Create(enclosingTypeRef, callingConv, name, genericArity, argTypes, returnType) =
+        {
+            mrefParent = enclosingTypeRef
+            mrefCallconv = callingConv
+            mrefName = name
+            mrefGenericArity = genericArity
+            mrefArgs = argTypes
+            mrefReturn = returnType
+        }
 
     /// For debugging
     []
     member x.DebugText = x.ToString()
 
-    override x.ToString() = x.DeclaringTypeRef.ToString() + "::" + x.Name + "(...)"
+    override x.ToString() =
+        x.DeclaringTypeRef.ToString() + "::" + x.Name + "(...)"
 
 []
 type ILFieldRef =
-    { DeclaringTypeRef: ILTypeRef
-      Name: string
-      Type: ILType }
+    {
+        DeclaringTypeRef: ILTypeRef
+        Name: string
+        Type: ILType
+    }
 
     /// For debugging
     []
     member x.DebugText = x.ToString()
 
-    override x.ToString() = x.DeclaringTypeRef.ToString() + "::" + x.Name
+    override x.ToString() =
+        x.DeclaringTypeRef.ToString() + "::" + x.Name
 
 []
 type ILMethodSpec =
-    { mspecMethodRef: ILMethodRef
+    {
+        mspecMethodRef: ILMethodRef
 
-      mspecDeclaringType: ILType
+        mspecDeclaringType: ILType
 
-      mspecMethodInst: ILGenericArgs }
+        mspecMethodInst: ILGenericArgs
+    }
 
-    static member Create (a, b, c) = { mspecDeclaringType=a; mspecMethodRef=b; mspecMethodInst=c }
+    static member Create(a, b, c) =
+        {
+            mspecDeclaringType = a
+            mspecMethodRef = b
+            mspecMethodInst = c
+        }
 
     member x.MethodRef = x.mspecMethodRef
 
-    member x.DeclaringType=x.mspecDeclaringType
+    member x.DeclaringType = x.mspecDeclaringType
 
-    member x.GenericArgs=x.mspecMethodInst
+    member x.GenericArgs = x.mspecMethodInst
 
-    member x.Name=x.MethodRef.Name
+    member x.Name = x.MethodRef.Name
 
-    member x.CallingConv=x.MethodRef.CallingConv
+    member x.CallingConv = x.MethodRef.CallingConv
 
     member x.GenericArity = x.MethodRef.GenericArity
 
@@ -885,8 +1087,10 @@ type ILMethodSpec =
 
 []
 type ILFieldSpec =
-    { FieldRef: ILFieldRef
-      DeclaringType: ILType }
+    {
+        FieldRef: ILFieldRef
+        DeclaringType: ILType
+    }
 
     member x.FormalType = x.FieldRef.Type
 
@@ -912,74 +1116,83 @@ type ILPlatform =
     | IA64
 
 type ILSourceDocument =
-    { sourceLanguage: ILGuid option
-      sourceVendor: ILGuid option
-      sourceDocType: ILGuid option
-      sourceFile: string }
+    {
+        sourceLanguage: ILGuid option
+        sourceVendor: ILGuid option
+        sourceDocType: ILGuid option
+        sourceFile: string
+    }
 
-    static member Create (language, vendor, documentType, file) =
-        { sourceLanguage=language
-          sourceVendor=vendor
-          sourceDocType=documentType
-          sourceFile=file }
+    static member Create(language, vendor, documentType, file) =
+        {
+            sourceLanguage = language
+            sourceVendor = vendor
+            sourceDocType = documentType
+            sourceFile = file
+        }
 
-    member x.Language=x.sourceLanguage
+    member x.Language = x.sourceLanguage
 
-    member x.Vendor=x.sourceVendor
+    member x.Vendor = x.sourceVendor
 
-    member x.DocumentType=x.sourceDocType
+    member x.DocumentType = x.sourceDocType
 
-    member x.File=x.sourceFile
+    member x.File = x.sourceFile
 
 []
 type ILDebugPoint =
-    { sourceDocument: ILSourceDocument
-      sourceLine: int
-      sourceColumn: int
-      sourceEndLine: int
-      sourceEndColumn: int }
+    {
+        sourceDocument: ILSourceDocument
+        sourceLine: int
+        sourceColumn: int
+        sourceEndLine: int
+        sourceEndColumn: int
+    }
 
-    static member Create (document, line, column, endLine, endColumn) =
-        { sourceDocument=document
-          sourceLine=line
-          sourceColumn=column
-          sourceEndLine=endLine
-          sourceEndColumn=endColumn }
+    static member Create(document, line, column, endLine, endColumn) =
+        {
+            sourceDocument = document
+            sourceLine = line
+            sourceColumn = column
+            sourceEndLine = endLine
+            sourceEndColumn = endColumn
+        }
 
-    member x.Document=x.sourceDocument
+    member x.Document = x.sourceDocument
 
-    member x.Line=x.sourceLine
+    member x.Line = x.sourceLine
 
-    member x.Column=x.sourceColumn
+    member x.Column = x.sourceColumn
 
-    member x.EndLine=x.sourceEndLine
+    member x.EndLine = x.sourceEndLine
 
-    member x.EndColumn=x.sourceEndColumn
+    member x.EndColumn = x.sourceEndColumn
 
     /// For debugging
     []
     member x.DebugText = x.ToString()
 
-    override x.ToString() = sprintf "(%d, %d)-(%d, %d)" x.Line x.Column x.EndLine x.EndColumn
+    override x.ToString() =
+        sprintf "(%d, %d)-(%d, %d)" x.Line x.Column x.EndLine x.EndColumn
 
 type ILAttribElem =
-  | String of string option
-  | Bool of bool
-  | Char of char
-  | SByte of int8
-  | Int16 of int16
-  | Int32 of int32
-  | Int64 of int64
-  | Byte of uint8
-  | UInt16 of uint16
-  | UInt32 of uint32
-  | UInt64 of uint64
-  | Single of single
-  | Double of double
-  | Null
-  | Type of ILType option
-  | TypeRef of ILTypeRef option
-  | Array of ILType * ILAttribElem list
+    | String of string option
+    | Bool of bool
+    | Char of char
+    | SByte of int8
+    | Int16 of int16
+    | Int32 of int32
+    | Int64 of int64
+    | Byte of uint8
+    | UInt16 of uint16
+    | UInt32 of uint32
+    | UInt64 of uint64
+    | Single of single
+    | Double of double
+    | Null
+    | Type of ILType option
+    | TypeRef of ILTypeRef option
+    | Array of ILType * ILAttribElem list
 
 type ILAttributeNamedArg = string * ILType * bool * ILAttribElem
 
@@ -998,10 +1211,10 @@ type ILAttribute =
         | Encoded (_, _, elements) -> elements
         | Decoded (_, fixedArgs, namedArgs) -> fixedArgs @ (namedArgs |> List.map (fun (_, _, _, e) -> e))
 
-    member x.WithMethod (method: ILMethodSpec) =
+    member x.WithMethod(method: ILMethodSpec) =
         match x with
-        | Encoded (_, data, elements) -> Encoded (method, data, elements)
-        | Decoded (_, fixedArgs, namedArgs) -> Decoded (method, fixedArgs, namedArgs)
+        | Encoded (_, data, elements) -> Encoded(method, data, elements)
+        | Decoded (_, fixedArgs, namedArgs) -> Decoded(method, fixedArgs, namedArgs)
 
     /// For debugging
     []
@@ -1010,7 +1223,7 @@ type ILAttribute =
     override x.ToString() = x.Method.ToString() + "(...)"
 
 []
-type ILAttributes(array : ILAttribute[]) =
+type ILAttributes(array: ILAttribute[]) =
 
     member x.AsArray() = array
 
@@ -1026,25 +1239,30 @@ type ILAttributesStored =
     | Given of ILAttributes
 
     member x.GetCustomAttrs metadataIndex =
-       match x with
-       | Reader f -> ILAttributes (f metadataIndex)
-       | Given attrs -> attrs
+        match x with
+        | Reader f -> ILAttributes(f metadataIndex)
+        | Given attrs -> attrs
 
-let emptyILCustomAttrs = ILAttributes [| |]
+let emptyILCustomAttrs = ILAttributes [||]
 
 let mkILCustomAttrsFromArray (attrs: ILAttribute[]) =
-    if attrs.Length = 0 then emptyILCustomAttrs else ILAttributes attrs
+    if attrs.Length = 0 then
+        emptyILCustomAttrs
+    else
+        ILAttributes attrs
 
 let mkILCustomAttrs l =
     match l with
     | [] -> emptyILCustomAttrs
     | _ -> mkILCustomAttrsFromArray (List.toArray l)
 
-let emptyILCustomAttrsStored =
-    ILAttributesStored.Given emptyILCustomAttrs
+let emptyILCustomAttrsStored = ILAttributesStored.Given emptyILCustomAttrs
 
 let storeILCustomAttrs (attrs: ILAttributes) =
-    if attrs.AsArray().Length = 0 then emptyILCustomAttrsStored else ILAttributesStored.Given attrs
+    if attrs.AsArray().Length = 0 then
+        emptyILCustomAttrsStored
+    else
+        ILAttributesStored.Given attrs
 
 let mkILCustomAttrsReader f = ILAttributesStored.Reader f
 
@@ -1055,69 +1273,68 @@ type ILCodeLabel = int
 // --------------------------------------------------------------------
 
 type ILBasicType =
-  | DT_R
-  | DT_I1
-  | DT_U1
-  | DT_I2
-  | DT_U2
-  | DT_I4
-  | DT_U4
-  | DT_I8
-  | DT_U8
-  | DT_R4
-  | DT_R8
-  | DT_I
-  | DT_U
-  | DT_REF
+    | DT_R
+    | DT_I1
+    | DT_U1
+    | DT_I2
+    | DT_U2
+    | DT_I4
+    | DT_U4
+    | DT_I8
+    | DT_U8
+    | DT_R4
+    | DT_R8
+    | DT_I
+    | DT_U
+    | DT_REF
 
 []
 type ILToken =
-  | ILType of ILType
-  | ILMethod of ILMethodSpec
-  | ILField of ILFieldSpec
+    | ILType of ILType
+    | ILMethod of ILMethodSpec
+    | ILField of ILFieldSpec
 
 []
 type ILConst =
-  | I4 of int32
-  | I8 of int64
-  | R4 of single
-  | R8 of double
+    | I4 of int32
+    | I8 of int64
+    | R4 of single
+    | R8 of double
 
 type ILTailcall =
-  | Tailcall
-  | Normalcall
+    | Tailcall
+    | Normalcall
 
 type ILAlignment =
-  | Aligned
-  | Unaligned1
-  | Unaligned2
-  | Unaligned4
+    | Aligned
+    | Unaligned1
+    | Unaligned2
+    | Unaligned4
 
 type ILVolatility =
-  | Volatile
-  | Nonvolatile
+    | Volatile
+    | Nonvolatile
 
 type ILReadonly =
-  | ReadonlyAddress
-  | NormalAddress
+    | ReadonlyAddress
+    | NormalAddress
 
 type ILVarArgs = ILTypes option
 
 []
 type ILComparisonInstr =
-  | BI_beq
-  | BI_bge
-  | BI_bge_un
-  | BI_bgt
-  | BI_bgt_un
-  | BI_ble
-  | BI_ble_un
-  | BI_blt
-  | BI_blt_un
-  | BI_bne_un
-  | BI_brfalse
-  | BI_brtrue
-
+    | BI_beq
+    | BI_bge
+    | BI_bge_un
+    | BI_bgt
+    | BI_bgt_un
+    | BI_ble
+    | BI_ble_un
+    | BI_blt
+    | BI_blt_un
+    | BI_bne_un
+    | BI_brfalse
+    | BI_brtrue
 
 []
 type ILInstr =
@@ -1230,7 +1447,6 @@ type ILInstr =
     | EI_ilzero of ILType
     | EI_ldlen_multi of int32 * int32
 
-
 []
 type ILExceptionClause =
     | Finally of (ILCodeLabel * ILCodeLabel)
@@ -1240,62 +1456,68 @@ type ILExceptionClause =
 
 []
 type ILExceptionSpec =
-    { Range: ILCodeLabel * ILCodeLabel
-      Clause: ILExceptionClause }
+    {
+        Range: ILCodeLabel * ILCodeLabel
+        Clause: ILExceptionClause
+    }
 
 /// Indicates that a particular local variable has a particular source
 /// language name within a given set of ranges. This does not effect local
 /// variable numbering, which is global over the whole method.
 []
-type ILLocalDebugMapping =
-    { LocalIndex: int
-      LocalName: string }
+type ILLocalDebugMapping = { LocalIndex: int; LocalName: string }
 
 []
 type ILLocalDebugInfo =
-    { Range: ILCodeLabel * ILCodeLabel
-      DebugMappings: ILLocalDebugMapping list }
+    {
+        Range: ILCodeLabel * ILCodeLabel
+        DebugMappings: ILLocalDebugMapping list
+    }
 
 []
 type ILCode =
-    { Labels: Dictionary
-      Instrs: ILInstr[]
-      Exceptions: ILExceptionSpec list
-      Locals: ILLocalDebugInfo list }
+    {
+        Labels: Dictionary
+        Instrs: ILInstr[]
+        Exceptions: ILExceptionSpec list
+        Locals: ILLocalDebugInfo list
+    }
 
 []
 type ILLocal =
-    { Type: ILType
-      IsPinned: bool
-      DebugInfo: (string * int * int) option }
+    {
+        Type: ILType
+        IsPinned: bool
+        DebugInfo: (string * int * int) option
+    }
 
 type ILLocals = ILLocal list
 
 []
 type ILDebugImport =
-    | ImportType of targetType: ILType // * alias: string option 
+    | ImportType of targetType: ILType // * alias: string option
     | ImportNamespace of targetNamespace: string // * assembly: ILAssemblyRef option * alias: string option
 
-    //| ReferenceAlias of string
-    //| OpenXmlNamespace of prefix: string * xmlNamespace: string
+//| ReferenceAlias of string
+//| OpenXmlNamespace of prefix: string * xmlNamespace: string
 
 type ILDebugImports =
     {
-      Parent: ILDebugImports option
-      Imports: ILDebugImport[]
+        Parent: ILDebugImports option
+        Imports: ILDebugImport[]
     }
 
 []
 type ILMethodBody =
-    { 
-      IsZeroInit: bool
-      MaxStack: int32
-      NoInlining: bool
-      AggressiveInlining: bool
-      Locals: ILLocals
-      Code: ILCode
-      DebugRange: ILDebugPoint option
-      DebugImports: ILDebugImports option
+    {
+        IsZeroInit: bool
+        MaxStack: int32
+        NoInlining: bool
+        AggressiveInlining: bool
+        Locals: ILLocals
+        Code: ILCode
+        DebugRange: ILDebugPoint option
+        DebugImports: ILDebugImports option
     }
 
 []
@@ -1375,7 +1597,9 @@ type ILNativeType =
     | UInt16
     | UInt32
     | UInt64
-    | Array of ILNativeType option * (int32 * int32 option) option (* optional idx of parameter giving size plus optional additive i.e. num elems *)
+    | Array of
+        ILNativeType option *
+        (int32 * int32 option) option (* optional idx of parameter giving size plus optional additive i.e. num elems *)
     | Int
     | UInt
     | Method
@@ -1389,9 +1613,7 @@ type ILNativeType =
     | ANSIBSTR
     | VariantBool
 
-and
-  []
-  ILNativeVariant =
+and [] ILNativeVariant =
     | Empty
     | Null
     | Variant
@@ -1459,11 +1681,10 @@ type ILSecurityAction =
     | DemandChoice
 
 []
-type ILSecurityDecl =
-    | ILSecurityDecl of ILSecurityAction * byte[]
+type ILSecurityDecl = ILSecurityDecl of ILSecurityAction * byte[]
 
 []
-type ILSecurityDecls (array : ILSecurityDecl[]) =
+type ILSecurityDecls(array: ILSecurityDecl[]) =
     member x.AsArray() = array
     member x.AsList() = x.AsArray() |> Array.toList
 
@@ -1477,20 +1698,24 @@ type ILSecurityDeclsStored =
     | Given of ILSecurityDecls
 
     member x.GetSecurityDecls metadataIndex =
-       match x with
-       | Reader f -> ILSecurityDecls(f metadataIndex)
-       | Given attrs -> attrs
+        match x with
+        | Reader f -> ILSecurityDecls(f metadataIndex)
+        | Given attrs -> attrs
 
-let emptyILSecurityDecls = ILSecurityDecls [| |]
+let emptyILSecurityDecls = ILSecurityDecls [||]
 
 let emptyILSecurityDeclsStored = ILSecurityDeclsStored.Given emptyILSecurityDecls
 
-let mkILSecurityDecls l = match l with [] -> emptyILSecurityDecls | _ -> ILSecurityDecls (Array.ofList l)
+let mkILSecurityDecls l =
+    match l with
+    | [] -> emptyILSecurityDecls
+    | _ -> ILSecurityDecls(Array.ofList l)
 
 let storeILSecurityDecls (x: ILSecurityDecls) =
     if x.AsArray().Length = 0 then
         emptyILSecurityDeclsStored
-    else ILSecurityDeclsStored.Given x
+    else
+        ILSecurityDeclsStored.Given x
 
 let mkILSecurityDeclsReader f = ILSecurityDeclsStored.Reader f
 
@@ -1524,26 +1749,30 @@ type PInvokeCharEncoding =
 
 []
 type PInvokeMethod =
-    { Where: ILModuleRef
-      Name: string
-      CallingConv: PInvokeCallingConvention
-      CharEncoding: PInvokeCharEncoding
-      NoMangle: bool
-      LastError: bool
-      ThrowOnUnmappableChar: PInvokeThrowOnUnmappableChar
-      CharBestFit: PInvokeCharBestFit }
+    {
+        Where: ILModuleRef
+        Name: string
+        CallingConv: PInvokeCallingConvention
+        CharEncoding: PInvokeCharEncoding
+        NoMangle: bool
+        LastError: bool
+        ThrowOnUnmappableChar: PInvokeThrowOnUnmappableChar
+        CharBestFit: PInvokeCharBestFit
+    }
 
 []
 type ILParameter =
-    { Name: string option
-      Type: ILType
-      Default: ILFieldInit option
-      Marshal: ILNativeType option
-      IsIn: bool
-      IsOut: bool
-      IsOptional: bool
-      CustomAttrsStored: ILAttributesStored
-      MetadataIndex: int32 }
+    {
+        Name: string option
+        Type: ILType
+        Default: ILFieldInit option
+        Marshal: ILNativeType option
+        IsIn: bool
+        IsOut: bool
+        IsOptional: bool
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
+    }
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
 
@@ -1551,14 +1780,19 @@ type ILParameters = ILParameter list
 
 []
 type ILReturn =
-    { Marshal: ILNativeType option
-      Type: ILType
-      CustomAttrsStored: ILAttributesStored
-      MetadataIndex: int32 }
+    {
+        Marshal: ILNativeType option
+        Type: ILType
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
+    }
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
 
-    member x.WithCustomAttrs(customAttrs) = { x with CustomAttrsStored = storeILCustomAttrs customAttrs }
+    member x.WithCustomAttrs(customAttrs) =
+        { x with
+            CustomAttrsStored = storeILCustomAttrs customAttrs
+        }
 
 type ILOverridesSpec =
     | OverridesSpec of ILMethodRef * ILType
@@ -1568,10 +1802,12 @@ type ILOverridesSpec =
     member x.DeclaringType = let (OverridesSpec (_mr, ty)) = x in ty
 
 type ILMethodVirtualInfo =
-    { IsFinal: bool
-      IsNewSlot: bool
-      IsCheckAccessOnOverride: bool
-      IsAbstract: bool }
+    {
+        IsFinal: bool
+        IsNewSlot: bool
+        IsCheckAccessOnOverride: bool
+        IsAbstract: bool
+    }
 
 []
 type MethodBody =
@@ -1597,14 +1833,16 @@ type ILGenericVariance =
 
 []
 type ILGenericParameterDef =
-    { Name: string
-      Constraints: ILTypes
-      Variance: ILGenericVariance
-      HasReferenceTypeConstraint: bool
-      HasNotNullableValueTypeConstraint: bool
-      HasDefaultConstructorConstraint: bool
-      CustomAttrsStored : ILAttributesStored
-      MetadataIndex: int32 }
+    {
+        Name: string
+        Constraints: ILTypes
+        Variance: ILGenericVariance
+        HasReferenceTypeConstraint: bool
+        HasNotNullableValueTypeConstraint: bool
+        HasDefaultConstructorConstraint: bool
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
+    }
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
 
@@ -1618,13 +1856,21 @@ type ILGenericParameterDefs = ILGenericParameterDef list
 
 let memberAccessOfFlags flags =
     let f = (flags &&& 0x00000007)
-    if f = 0x00000001 then ILMemberAccess.Private
-    elif f = 0x00000006 then ILMemberAccess.Public
-    elif f = 0x00000004 then ILMemberAccess.Family
-    elif f = 0x00000002 then ILMemberAccess.FamilyAndAssembly
-    elif f = 0x00000005 then ILMemberAccess.FamilyOrAssembly
-    elif f = 0x00000003 then ILMemberAccess.Assembly
-    else ILMemberAccess.CompilerControlled
+
+    if f = 0x00000001 then
+        ILMemberAccess.Private
+    elif f = 0x00000006 then
+        ILMemberAccess.Public
+    elif f = 0x00000004 then
+        ILMemberAccess.Family
+    elif f = 0x00000002 then
+        ILMemberAccess.FamilyAndAssembly
+    elif f = 0x00000005 then
+        ILMemberAccess.FamilyOrAssembly
+    elif f = 0x00000003 then
+        ILMemberAccess.Assembly
+    else
+        ILMemberAccess.CompilerControlled
 
 let convertMemberAccess (ilMemberAccess: ILMemberAccess) =
     match ilMemberAccess with
@@ -1636,18 +1882,46 @@ let convertMemberAccess (ilMemberAccess: ILMemberAccess) =
     | ILMemberAccess.FamilyOrAssembly -> MethodAttributes.FamORAssem
     | ILMemberAccess.Family -> MethodAttributes.Family
 
-let inline conditionalAdd condition flagToAdd source = if condition then source ||| flagToAdd else source &&& ~~~flagToAdd
+let inline conditionalAdd condition flagToAdd source =
+    if condition then
+        source ||| flagToAdd
+    else
+        source &&& ~~~flagToAdd
 
 let NoMetadataIdx = -1
 
 []
-type ILMethodDef (name: string, attributes: MethodAttributes, implAttributes: MethodImplAttributes, callingConv: ILCallingConv,
-                  parameters: ILParameters, ret: ILReturn, body: Lazy, isEntryPoint: bool, genericParams: ILGenericParameterDefs,
-                  securityDeclsStored: ILSecurityDeclsStored, customAttrsStored: ILAttributesStored, metadataIndex: int32) =
-
-    new (name, attributes, implAttributes, callingConv, parameters, ret, body, isEntryPoint, genericParams, securityDecls, customAttrs) =
-       ILMethodDef (name, attributes, implAttributes, callingConv, parameters, ret, body, isEntryPoint, genericParams,
-                    storeILSecurityDecls securityDecls, storeILCustomAttrs customAttrs, NoMetadataIdx)
+type ILMethodDef
+    (
+        name: string,
+        attributes: MethodAttributes,
+        implAttributes: MethodImplAttributes,
+        callingConv: ILCallingConv,
+        parameters: ILParameters,
+        ret: ILReturn,
+        body: Lazy,
+        isEntryPoint: bool,
+        genericParams: ILGenericParameterDefs,
+        securityDeclsStored: ILSecurityDeclsStored,
+        customAttrsStored: ILAttributesStored,
+        metadataIndex: int32
+    ) =
+
+    new(name, attributes, implAttributes, callingConv, parameters, ret, body, isEntryPoint, genericParams, securityDecls, customAttrs) =
+        ILMethodDef(
+            name,
+            attributes,
+            implAttributes,
+            callingConv,
+            parameters,
+            ret,
+            body,
+            isEntryPoint,
+            genericParams,
+            storeILSecurityDecls securityDecls,
+            storeILCustomAttrs customAttrs,
+            NoMetadataIdx
+        )
 
     member private _.LazyBody = body
 
@@ -1676,22 +1950,40 @@ type ILMethodDef (name: string, attributes: MethodAttributes, implAttributes: Me
 
     member _.MetadataIndex = metadataIndex
 
-    member x.With (?name: string, ?attributes: MethodAttributes, ?implAttributes: MethodImplAttributes,
-                   ?callingConv: ILCallingConv, ?parameters: ILParameters, ?ret: ILReturn,
-                   ?body: Lazy, ?securityDecls: ILSecurityDecls, ?isEntryPoint: bool,
-                   ?genericParams: ILGenericParameterDefs, ?customAttrs: ILAttributes) =
-
-        ILMethodDef (name = defaultArg name x.Name,
-                     attributes = defaultArg attributes x.Attributes,
-                     implAttributes = defaultArg implAttributes x.ImplAttributes,
-                     callingConv = defaultArg callingConv x.CallingConv,
-                     parameters = defaultArg parameters x.Parameters,
-                     ret = defaultArg ret x.Return,
-                     body = defaultArg body x.LazyBody,
-                     securityDecls = (match securityDecls with None -> x.SecurityDecls | Some attrs -> attrs),
-                     isEntryPoint = defaultArg isEntryPoint x.IsEntryPoint,
-                     genericParams = defaultArg genericParams x.GenericParams,
-                     customAttrs=(match customAttrs with None -> x.CustomAttrs | Some attrs -> attrs))
+    member x.With
+        (
+            ?name: string,
+            ?attributes: MethodAttributes,
+            ?implAttributes: MethodImplAttributes,
+            ?callingConv: ILCallingConv,
+            ?parameters: ILParameters,
+            ?ret: ILReturn,
+            ?body: Lazy,
+            ?securityDecls: ILSecurityDecls,
+            ?isEntryPoint: bool,
+            ?genericParams: ILGenericParameterDefs,
+            ?customAttrs: ILAttributes
+        ) =
+
+        ILMethodDef(
+            name = defaultArg name x.Name,
+            attributes = defaultArg attributes x.Attributes,
+            implAttributes = defaultArg implAttributes x.ImplAttributes,
+            callingConv = defaultArg callingConv x.CallingConv,
+            parameters = defaultArg parameters x.Parameters,
+            ret = defaultArg ret x.Return,
+            body = defaultArg body x.LazyBody,
+            securityDecls =
+                (match securityDecls with
+                 | None -> x.SecurityDecls
+                 | Some attrs -> attrs),
+            isEntryPoint = defaultArg isEntryPoint x.IsEntryPoint,
+            genericParams = defaultArg genericParams x.GenericParams,
+            customAttrs =
+                (match customAttrs with
+                 | None -> x.CustomAttrs
+                 | Some attrs -> attrs)
+        )
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs metadataIndex
 
@@ -1700,21 +1992,31 @@ type ILMethodDef (name: string, attributes: MethodAttributes, implAttributes: Me
     member x.ParameterTypes = typesOfILParams x.Parameters
 
     member md.Code =
-          match md.Body with
-          | MethodBody.IL il-> Some il.Value.Code
-          | _ -> None
+        match md.Body with
+        | MethodBody.IL il -> Some il.Value.Code
+        | _ -> None
 
-    member x.IsIL = match x.Body with | MethodBody.IL _ -> true | _ -> false
+    member x.IsIL =
+        match x.Body with
+        | MethodBody.IL _ -> true
+        | _ -> false
 
-    member x.Locals = match x.Body with | MethodBody.IL il -> il.Value.Locals | _ -> []
+    member x.Locals =
+        match x.Body with
+        | MethodBody.IL il -> il.Value.Locals
+        | _ -> []
 
-    member x.MethodBody = match x.Body with MethodBody.IL il -> il.Value | _ -> failwith "not IL"
+    member x.MethodBody =
+        match x.Body with
+        | MethodBody.IL il -> il.Value
+        | _ -> failwith "not IL"
 
     member x.MaxStack = x.MethodBody.MaxStack
 
     member x.IsZeroInit = x.MethodBody.IsZeroInit
 
-    member md.CallingSignature = mkILCallSig (md.CallingConv, md.ParameterTypes, md.Return.Type)
+    member md.CallingSignature =
+        mkILCallSig (md.CallingConv, md.ParameterTypes, md.Return.Type)
 
     member x.IsClassInitializer = x.Name = ".cctor"
 
@@ -1732,7 +2034,8 @@ type ILMethodDef (name: string, attributes: MethodAttributes, implAttributes: Me
 
     member x.IsNewSlot = x.Attributes &&& MethodAttributes.NewSlot <> enum 0
 
-    member x.IsCheckAccessOnOverride= x.Attributes &&& MethodAttributes.CheckAccessOnOverride <> enum 0
+    member x.IsCheckAccessOnOverride =
+        x.Attributes &&& MethodAttributes.CheckAccessOnOverride <> enum 0
 
     member x.IsAbstract = x.Attributes &&& MethodAttributes.Abstract <> enum 0
 
@@ -1740,7 +2043,8 @@ type ILMethodDef (name: string, attributes: MethodAttributes, implAttributes: Me
 
     member x.IsSpecialName = x.Attributes &&& MethodAttributes.SpecialName <> enum 0
 
-    member x.IsUnmanagedExport = x.Attributes &&& MethodAttributes.UnmanagedExport <> enum 0
+    member x.IsUnmanagedExport =
+        x.Attributes &&& MethodAttributes.UnmanagedExport <> enum 0
 
     member x.IsReqSecObj = x.Attributes &&& MethodAttributes.RequireSecObject <> enum 0
 
@@ -1750,119 +2054,191 @@ type ILMethodDef (name: string, attributes: MethodAttributes, implAttributes: Me
 
     member x.IsForwardRef = x.ImplAttributes &&& MethodImplAttributes.ForwardRef <> enum 0
 
-    member x.IsInternalCall = x.ImplAttributes &&& MethodImplAttributes.InternalCall <> enum 0
+    member x.IsInternalCall =
+        x.ImplAttributes &&& MethodImplAttributes.InternalCall <> enum 0
 
-    member x.IsPreserveSig = x.ImplAttributes &&& MethodImplAttributes.PreserveSig <> enum 0
+    member x.IsPreserveSig =
+        x.ImplAttributes &&& MethodImplAttributes.PreserveSig <> enum 0
 
-    member x.IsSynchronized = x.ImplAttributes &&& MethodImplAttributes.Synchronized <> enum 0
+    member x.IsSynchronized =
+        x.ImplAttributes &&& MethodImplAttributes.Synchronized <> enum 0
 
     member x.IsNoInline = x.ImplAttributes &&& MethodImplAttributes.NoInlining <> enum 0
 
-    member x.IsAggressiveInline= x.ImplAttributes &&& MethodImplAttributes.AggressiveInlining <> enum 0
+    member x.IsAggressiveInline =
+        x.ImplAttributes &&& MethodImplAttributes.AggressiveInlining <> enum 0
 
     member x.IsMustRun = x.ImplAttributes &&& MethodImplAttributes.NoOptimization <> enum 0
 
-    member x.WithSpecialName = x.With(attributes = (x.Attributes ||| MethodAttributes.SpecialName))
+    member x.WithSpecialName =
+        x.With(attributes = (x.Attributes ||| MethodAttributes.SpecialName))
 
     member x.WithHideBySig() =
-        x.With(attributes = (
-                if x.IsVirtual then x.Attributes &&& ~~~MethodAttributes.CheckAccessOnOverride ||| MethodAttributes.HideBySig
-                else failwith "WithHideBySig"))
-
-    member x.WithHideBySig(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.HideBySig))
-
-    member x.WithFinal(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.Final))
-
-    member x.WithAbstract(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.Abstract))
-
-    member x.WithAccess(access) = x.With(attributes = (x.Attributes &&& ~~~MethodAttributes.MemberAccessMask ||| convertMemberAccess access))
+        x.With(
+            attributes =
+                (if x.IsVirtual then
+                     x.Attributes &&& ~~~MethodAttributes.CheckAccessOnOverride
+                     ||| MethodAttributes.HideBySig
+                 else
+                     failwith "WithHideBySig")
+        )
+
+    member x.WithHideBySig(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.HideBySig))
+
+    member x.WithFinal(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.Final))
+
+    member x.WithAbstract(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.Abstract))
+
+    member x.WithAccess(access) =
+        x.With(
+            attributes =
+                (x.Attributes &&& ~~~MethodAttributes.MemberAccessMask
+                 ||| convertMemberAccess access)
+        )
 
     member x.WithNewSlot = x.With(attributes = (x.Attributes ||| MethodAttributes.NewSlot))
 
-    member x.WithSecurity(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.HasSecurity))
+    member x.WithSecurity(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.HasSecurity))
 
-    member x.WithPInvoke(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.PinvokeImpl))
+    member x.WithPInvoke(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition MethodAttributes.PinvokeImpl))
 
-    member x.WithPreserveSig(condition) = x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.PreserveSig))
+    member x.WithPreserveSig(condition) =
+        x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.PreserveSig))
 
-    member x.WithSynchronized(condition) = x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.Synchronized))
+    member x.WithSynchronized(condition) =
+        x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.Synchronized))
 
-    member x.WithNoInlining(condition) = x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.NoInlining))
+    member x.WithNoInlining(condition) =
+        x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.NoInlining))
 
-    member x.WithAggressiveInlining(condition) = x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.AggressiveInlining))
+    member x.WithAggressiveInlining(condition) =
+        x.With(
+            implAttributes =
+                (x.ImplAttributes
+                 |> conditionalAdd condition MethodImplAttributes.AggressiveInlining)
+        )
 
-    member x.WithRuntime(condition) = x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.Runtime))
+    member x.WithRuntime(condition) =
+        x.With(implAttributes = (x.ImplAttributes |> conditionalAdd condition MethodImplAttributes.Runtime))
 
 /// Index table by name and arity.
 type MethodDefMap = Map
 
 []
-type ILMethodDefs(f : unit -> ILMethodDef[]) =
+type ILMethodDefs(f: unit -> ILMethodDef[]) =
 
     let mutable array = InlineDelayInit<_>(f)
-    let mutable dict = InlineDelayInit<_>(fun () ->
+
+    let mutable dict =
+        InlineDelayInit<_>(fun () ->
             let arr = array.Value
             let t = Dictionary<_, _>()
+
             for i = arr.Length - 1 downto 0 do
                 let y = arr[i]
                 let key = y.Name
+
                 match t.TryGetValue key with
                 | true, m -> t[key] <- y :: m
-                | _ -> t[key] <- [y]
+                | _ -> t[key] <- [ y ]
+
             t)
 
     interface IEnumerable with
-        member x.GetEnumerator() = ((x :> IEnumerable).GetEnumerator() :> IEnumerator)
+        member x.GetEnumerator() =
+            ((x :> IEnumerable).GetEnumerator() :> IEnumerator)
 
     interface IEnumerable with
-        member x.GetEnumerator() = (array.Value :> IEnumerable).GetEnumerator()
+        member x.GetEnumerator() =
+            (array.Value :> IEnumerable).GetEnumerator()
 
     member x.AsArray() = array.Value
 
-    member x.AsList() = array.Value|> Array.toList
+    member x.AsList() = array.Value |> Array.toList
 
     member x.FindByName nm =
         match dict.Value.TryGetValue nm with
         | true, m -> m
         | _ -> []
 
-    member x.FindByNameAndArity (nm, arity) = x.FindByName nm |> List.filter (fun x -> List.length x.Parameters = arity)
+    member x.FindByNameAndArity(nm, arity) =
+        x.FindByName nm |> List.filter (fun x -> List.length x.Parameters = arity)
 
-    member x.TryFindInstanceByNameAndCallingSignature (nm, callingSig) =
+    member x.TryFindInstanceByNameAndCallingSignature(nm, callingSig) =
         x.FindByName nm
         |> List.tryFind (fun x -> not x.IsStatic && x.CallingSignature = callingSig)
 
 []
-type ILEventDef(eventType: ILType option, name: string, attributes: EventAttributes,
-                addMethod: ILMethodRef, removeMethod: ILMethodRef, fireMethod: ILMethodRef option,
-                otherMethods: ILMethodRef list, customAttrsStored: ILAttributesStored, metadataIndex: int32) =
-
-    new (eventType, name, attributes, addMethod, removeMethod, fireMethod, otherMethods, customAttrs) =
-        ILEventDef(eventType, name, attributes, addMethod, removeMethod, fireMethod, otherMethods, storeILCustomAttrs customAttrs, NoMetadataIdx)
+type ILEventDef
+    (
+        eventType: ILType option,
+        name: string,
+        attributes: EventAttributes,
+        addMethod: ILMethodRef,
+        removeMethod: ILMethodRef,
+        fireMethod: ILMethodRef option,
+        otherMethods: ILMethodRef list,
+        customAttrsStored: ILAttributesStored,
+        metadataIndex: int32
+    ) =
+
+    new(eventType, name, attributes, addMethod, removeMethod, fireMethod, otherMethods, customAttrs) =
+        ILEventDef(
+            eventType,
+            name,
+            attributes,
+            addMethod,
+            removeMethod,
+            fireMethod,
+            otherMethods,
+            storeILCustomAttrs customAttrs,
+            NoMetadataIdx
+        )
 
     member _.EventType = eventType
+
     member _.Name = name
+
     member _.Attributes = attributes
+
     member _.AddMethod = addMethod
+
     member _.RemoveMethod = removeMethod
+
     member _.FireMethod = fireMethod
+
     member _.OtherMethods = otherMethods
+
     member _.CustomAttrsStored = customAttrsStored
+
     member _.MetadataIndex = metadataIndex
+
     member x.CustomAttrs = customAttrsStored.GetCustomAttrs x.MetadataIndex
 
     member x.With(?eventType, ?name, ?attributes, ?addMethod, ?removeMethod, ?fireMethod, ?otherMethods, ?customAttrs) =
-        ILEventDef(eventType= defaultArg eventType x.EventType,
-                   name= defaultArg name x.Name,
-                   attributes= defaultArg attributes x.Attributes,
-                   addMethod=defaultArg addMethod x.AddMethod,
-                   removeMethod=defaultArg removeMethod x.RemoveMethod,
-                   fireMethod= defaultArg fireMethod x.FireMethod,
-                   otherMethods= defaultArg otherMethods x.OtherMethods,
-                   customAttrs=(match customAttrs with None -> x.CustomAttrs | Some attrs -> attrs))
-
-    member x.IsSpecialName = (x.Attributes &&& EventAttributes.SpecialName) <> enum<_>(0)
-    member x.IsRTSpecialName = (x.Attributes &&& EventAttributes.RTSpecialName) <> enum<_>(0)
+        ILEventDef(
+            eventType = defaultArg eventType x.EventType,
+            name = defaultArg name x.Name,
+            attributes = defaultArg attributes x.Attributes,
+            addMethod = defaultArg addMethod x.AddMethod,
+            removeMethod = defaultArg removeMethod x.RemoveMethod,
+            fireMethod = defaultArg fireMethod x.FireMethod,
+            otherMethods = defaultArg otherMethods x.OtherMethods,
+            customAttrs =
+                (match customAttrs with
+                 | None -> x.CustomAttrs
+                 | Some attrs -> attrs)
+        )
+
+    member x.IsSpecialName = (x.Attributes &&& EventAttributes.SpecialName) <> enum<_> (0)
+
+    member x.IsRTSpecialName =
+        (x.Attributes &&& EventAttributes.RTSpecialName) <> enum<_> (0)
 
     /// For debugging
     []
@@ -1879,12 +2255,33 @@ type ILEventDefs =
     member x.LookupByName s = let (ILEvents t) = x in t[s]
 
 []
-type ILPropertyDef(name: string, attributes: PropertyAttributes, setMethod: ILMethodRef option,
-                   getMethod: ILMethodRef option, callingConv: ILThisConvention, propertyType: ILType,
-                   init: ILFieldInit option, args: ILTypes, customAttrsStored: ILAttributesStored, metadataIndex: int32) =
-
-    new (name, attributes, setMethod, getMethod, callingConv, propertyType, init, args, customAttrs) =
-       ILPropertyDef(name, attributes, setMethod, getMethod, callingConv, propertyType, init, args, storeILCustomAttrs customAttrs, NoMetadataIdx)
+type ILPropertyDef
+    (
+        name: string,
+        attributes: PropertyAttributes,
+        setMethod: ILMethodRef option,
+        getMethod: ILMethodRef option,
+        callingConv: ILThisConvention,
+        propertyType: ILType,
+        init: ILFieldInit option,
+        args: ILTypes,
+        customAttrsStored: ILAttributesStored,
+        metadataIndex: int32
+    ) =
+
+    new(name, attributes, setMethod, getMethod, callingConv, propertyType, init, args, customAttrs) =
+        ILPropertyDef(
+            name,
+            attributes,
+            setMethod,
+            getMethod,
+            callingConv,
+            propertyType,
+            init,
+            args,
+            storeILCustomAttrs customAttrs,
+            NoMetadataIdx
+        )
 
     member x.Name = name
     member x.Attributes = attributes
@@ -1899,19 +2296,26 @@ type ILPropertyDef(name: string, attributes: PropertyAttributes, setMethod: ILMe
     member x.MetadataIndex = metadataIndex
 
     member x.With(?name, ?attributes, ?setMethod, ?getMethod, ?callingConv, ?propertyType, ?init, ?args, ?customAttrs) =
-      ILPropertyDef(name=defaultArg name x.Name,
-                    attributes=defaultArg attributes x.Attributes,
-                    setMethod=defaultArg setMethod x.SetMethod,
-                    getMethod=defaultArg getMethod x.GetMethod,
-                    callingConv=defaultArg callingConv x.CallingConv,
-                    propertyType=defaultArg propertyType x.PropertyType,
-                    init=defaultArg init x.Init,
-                    args=defaultArg args x.Args,
-                    customAttrs=(match customAttrs with None -> x.CustomAttrs | Some attrs -> attrs))
-
-
-    member x.IsSpecialName = (x.Attributes &&& PropertyAttributes.SpecialName) <> enum<_>(0)
-    member x.IsRTSpecialName = (x.Attributes &&& PropertyAttributes.RTSpecialName) <> enum<_>(0)
+        ILPropertyDef(
+            name = defaultArg name x.Name,
+            attributes = defaultArg attributes x.Attributes,
+            setMethod = defaultArg setMethod x.SetMethod,
+            getMethod = defaultArg getMethod x.GetMethod,
+            callingConv = defaultArg callingConv x.CallingConv,
+            propertyType = defaultArg propertyType x.PropertyType,
+            init = defaultArg init x.Init,
+            args = defaultArg args x.Args,
+            customAttrs =
+                (match customAttrs with
+                 | None -> x.CustomAttrs
+                 | Some attrs -> attrs)
+        )
+
+    member x.IsSpecialName =
+        (x.Attributes &&& PropertyAttributes.SpecialName) <> enum<_> (0)
+
+    member x.IsRTSpecialName =
+        (x.Attributes &&& PropertyAttributes.RTSpecialName) <> enum<_> (0)
 
     /// For debugging
     []
@@ -1931,7 +2335,7 @@ type ILPropertyDefs =
 let convertFieldAccess (ilMemberAccess: ILMemberAccess) =
     match ilMemberAccess with
     | ILMemberAccess.Assembly -> FieldAttributes.Assembly
-    | ILMemberAccess.CompilerControlled -> enum(0)
+    | ILMemberAccess.CompilerControlled -> enum (0)
     | ILMemberAccess.FamilyAndAssembly -> FieldAttributes.FamANDAssem
     | ILMemberAccess.FamilyOrAssembly -> FieldAttributes.FamORAssem
     | ILMemberAccess.Family -> FieldAttributes.Family
@@ -1939,45 +2343,95 @@ let convertFieldAccess (ilMemberAccess: ILMemberAccess) =
     | ILMemberAccess.Public -> FieldAttributes.Public
 
 []
-type ILFieldDef(name: string, fieldType: ILType, attributes: FieldAttributes, data: byte[] option,
-                literalValue: ILFieldInit option, offset: int32 option, marshal: ILNativeType option,
-                customAttrsStored: ILAttributesStored, metadataIndex: int32) =
-
-    new (name, fieldType, attributes, data, literalValue, offset, marshal, customAttrs) =
+type ILFieldDef
+    (
+        name: string,
+        fieldType: ILType,
+        attributes: FieldAttributes,
+        data: byte[] option,
+        literalValue: ILFieldInit option,
+        offset: int32 option,
+        marshal: ILNativeType option,
+        customAttrsStored: ILAttributesStored,
+        metadataIndex: int32
+    ) =
+
+    new(name, fieldType, attributes, data, literalValue, offset, marshal, customAttrs) =
         ILFieldDef(name, fieldType, attributes, data, literalValue, offset, marshal, storeILCustomAttrs customAttrs, NoMetadataIdx)
-    member _.Name=name
+
+    member _.Name = name
     member _.FieldType = fieldType
-    member _.Attributes=attributes
-    member _.Data=data
-    member _.LiteralValue=literalValue
-    member _.Offset=offset
-    member _.Marshal=marshal
+    member _.Attributes = attributes
+    member _.Data = data
+    member _.LiteralValue = literalValue
+    member _.Offset = offset
+    member _.Marshal = marshal
     member x.CustomAttrsStored = customAttrsStored
     member x.CustomAttrs = customAttrsStored.GetCustomAttrs x.MetadataIndex
     member x.MetadataIndex = metadataIndex
 
-    member x.With(?name: string, ?fieldType: ILType, ?attributes: FieldAttributes, ?data: byte[] option, ?literalValue: ILFieldInit option, ?offset: int32 option, ?marshal: ILNativeType option, ?customAttrs: ILAttributes) =
-        ILFieldDef(name=defaultArg name x.Name,
-                   fieldType=defaultArg fieldType x.FieldType,
-                   attributes=defaultArg attributes x.Attributes,
-                   data=defaultArg data x.Data,
-                   literalValue=defaultArg literalValue x.LiteralValue,
-                   offset=defaultArg offset x.Offset,
-                   marshal=defaultArg marshal x.Marshal,
-                   customAttrs=defaultArg customAttrs x.CustomAttrs)
+    member x.With
+        (
+            ?name: string,
+            ?fieldType: ILType,
+            ?attributes: FieldAttributes,
+            ?data: byte[] option,
+            ?literalValue: ILFieldInit option,
+            ?offset: int32 option,
+            ?marshal: ILNativeType option,
+            ?customAttrs: ILAttributes
+        ) =
+        ILFieldDef(
+            name = defaultArg name x.Name,
+            fieldType = defaultArg fieldType x.FieldType,
+            attributes = defaultArg attributes x.Attributes,
+            data = defaultArg data x.Data,
+            literalValue = defaultArg literalValue x.LiteralValue,
+            offset = defaultArg offset x.Offset,
+            marshal = defaultArg marshal x.Marshal,
+            customAttrs = defaultArg customAttrs x.CustomAttrs
+        )
+
     member x.IsStatic = x.Attributes &&& FieldAttributes.Static <> enum 0
     member x.IsSpecialName = x.Attributes &&& FieldAttributes.SpecialName <> enum 0
     member x.IsLiteral = x.Attributes &&& FieldAttributes.Literal <> enum 0
     member x.NotSerialized = x.Attributes &&& FieldAttributes.NotSerialized <> enum 0
     member x.IsInitOnly = x.Attributes &&& FieldAttributes.InitOnly <> enum 0
     member x.Access = memberAccessOfFlags (int x.Attributes)
-    member x.WithAccess(access) = x.With(attributes = (x.Attributes &&& ~~~FieldAttributes.FieldAccessMask ||| convertFieldAccess access))
-    member x.WithInitOnly(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition FieldAttributes.InitOnly))
-    member x.WithStatic(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition FieldAttributes.Static))
-    member x.WithSpecialName(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition (FieldAttributes.SpecialName ||| FieldAttributes.RTSpecialName)))
-    member x.WithNotSerialized(condition) = x.With(attributes = (x.Attributes |> conditionalAdd condition FieldAttributes.NotSerialized))
-    member x.WithLiteralDefaultValue(literal) = x.With(literalValue = literal, attributes = (x.Attributes |> conditionalAdd literal.IsSome (FieldAttributes.Literal ||| FieldAttributes.HasDefault)))
-    member x.WithFieldMarshal(marshal) = x.With(marshal = marshal, attributes = (x.Attributes |> conditionalAdd marshal.IsSome FieldAttributes.HasFieldMarshal))
+
+    member x.WithAccess(access) =
+        x.With(
+            attributes =
+                (x.Attributes &&& ~~~FieldAttributes.FieldAccessMask
+                 ||| convertFieldAccess access)
+        )
+
+    member x.WithInitOnly(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition FieldAttributes.InitOnly))
+
+    member x.WithStatic(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition FieldAttributes.Static))
+
+    member x.WithSpecialName(condition) =
+        x.With(
+            attributes =
+                (x.Attributes
+                 |> conditionalAdd condition (FieldAttributes.SpecialName ||| FieldAttributes.RTSpecialName))
+        )
+
+    member x.WithNotSerialized(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition FieldAttributes.NotSerialized))
+
+    member x.WithLiteralDefaultValue(literal) =
+        x.With(
+            literalValue = literal,
+            attributes =
+                (x.Attributes
+                 |> conditionalAdd literal.IsSome (FieldAttributes.Literal ||| FieldAttributes.HasDefault))
+        )
+
+    member x.WithFieldMarshal(marshal) =
+        x.With(marshal = marshal, attributes = (x.Attributes |> conditionalAdd marshal.IsSome FieldAttributes.HasFieldMarshal))
 
 // Index table by name. Keep a canonical list to make sure field order is not disturbed for binary manipulation.
 type ILFieldDefs =
@@ -1988,14 +2442,17 @@ type ILFieldDefs =
     member x.LookupByName s = let (ILFields t) = x in t[s]
 
 type ILMethodImplDef =
-    { Overrides: ILOverridesSpec
-      OverrideBy: ILMethodSpec }
+    {
+        Overrides: ILOverridesSpec
+        OverrideBy: ILMethodSpec
+    }
 
 // Index table by name and arity.
 type ILMethodImplDefs =
     | ILMethodImpls of Lazy
 
-    member x.AsList() = let (ILMethodImpls ltab) = x in Map.foldBack (fun _x y r -> y@r) (ltab.Force()) []
+    member x.AsList() =
+        let (ILMethodImpls ltab) = x in Map.foldBack (fun _x y r -> y @ r) (ltab.Force()) []
 
 and MethodImplsMap = Map
 
@@ -2006,8 +2463,10 @@ type ILTypeDefLayout =
     | Explicit of ILTypeDefLayoutInfo (* REVIEW: add field info here *)
 
 and ILTypeDefLayoutInfo =
-    { Size: int32 option
-      Pack: uint16 option }
+    {
+        Size: int32 option
+        Pack: uint16 option
+    }
 
 []
 type ILTypeInit =
@@ -2027,20 +2486,33 @@ type ILTypeDefAccess =
 
 let typeAccessOfFlags flags =
     let f = (flags &&& 0x00000007)
-    if f = 0x00000001 then ILTypeDefAccess.Public
-    elif f = 0x00000002 then ILTypeDefAccess.Nested ILMemberAccess.Public
-    elif f = 0x00000003 then ILTypeDefAccess.Nested ILMemberAccess.Private
-    elif f = 0x00000004 then ILTypeDefAccess.Nested ILMemberAccess.Family
-    elif f = 0x00000006 then ILTypeDefAccess.Nested ILMemberAccess.FamilyAndAssembly
-    elif f = 0x00000007 then ILTypeDefAccess.Nested ILMemberAccess.FamilyOrAssembly
-    elif f = 0x00000005 then ILTypeDefAccess.Nested ILMemberAccess.Assembly
-    else ILTypeDefAccess.Private
+
+    if f = 0x00000001 then
+        ILTypeDefAccess.Public
+    elif f = 0x00000002 then
+        ILTypeDefAccess.Nested ILMemberAccess.Public
+    elif f = 0x00000003 then
+        ILTypeDefAccess.Nested ILMemberAccess.Private
+    elif f = 0x00000004 then
+        ILTypeDefAccess.Nested ILMemberAccess.Family
+    elif f = 0x00000006 then
+        ILTypeDefAccess.Nested ILMemberAccess.FamilyAndAssembly
+    elif f = 0x00000007 then
+        ILTypeDefAccess.Nested ILMemberAccess.FamilyOrAssembly
+    elif f = 0x00000005 then
+        ILTypeDefAccess.Nested ILMemberAccess.Assembly
+    else
+        ILTypeDefAccess.Private
 
 let typeEncodingOfFlags flags =
     let f = (flags &&& 0x00030000)
-    if f = 0x00020000 then ILDefaultPInvokeEncoding.Auto
-    elif f = 0x00010000 then ILDefaultPInvokeEncoding.Unicode
-    else ILDefaultPInvokeEncoding.Ansi
+
+    if f = 0x00020000 then
+        ILDefaultPInvokeEncoding.Auto
+    elif f = 0x00010000 then
+        ILDefaultPInvokeEncoding.Unicode
+    else
+        ILDefaultPInvokeEncoding.Ansi
 
 []
 type ILTypeDefKind =
@@ -2051,16 +2523,23 @@ type ILTypeDefKind =
     | Delegate
 
 let typeKindOfFlags nm (super: ILType option) flags =
-    if (flags &&& 0x00000020) <> 0x0 then ILTypeDefKind.Interface
+    if (flags &&& 0x00000020) <> 0x0 then
+        ILTypeDefKind.Interface
     else
         match super with
         | None -> ILTypeDefKind.Class
         | Some ty ->
             let name = ty.TypeSpec.Name
-            if name = "System.Enum" then ILTypeDefKind.Enum
-            elif (name = "System.Delegate" && nm <> "System.MulticastDelegate") || name = "System.MulticastDelegate" then ILTypeDefKind.Delegate
-            elif name = "System.ValueType" && nm <> "System.Enum" then ILTypeDefKind.ValueType
-            else ILTypeDefKind.Class
+
+            if name = "System.Enum" then
+                ILTypeDefKind.Enum
+            elif (name = "System.Delegate" && nm <> "System.MulticastDelegate")
+                 || name = "System.MulticastDelegate" then
+                ILTypeDefKind.Delegate
+            elif name = "System.ValueType" && nm <> "System.Enum" then
+                ILTypeDefKind.ValueType
+            else
+                ILTypeDefKind.Class
 
 let convertTypeAccessFlags access =
     match access with
@@ -2110,48 +2589,129 @@ let convertInitSemantics (init: ILTypeInit) =
     | ILTypeInit.OnAny -> enum 0
 
 []
-type ILTypeDef(name: string, attributes: TypeAttributes, layout: ILTypeDefLayout, implements: ILTypes, genericParams: ILGenericParameterDefs,
-               extends: ILType option, methods: ILMethodDefs, nestedTypes: ILTypeDefs, fields: ILFieldDefs, methodImpls: ILMethodImplDefs,
-               events: ILEventDefs, properties: ILPropertyDefs, isKnownToBeAttribute: bool, securityDeclsStored: ILSecurityDeclsStored, customAttrsStored: ILAttributesStored, metadataIndex: int32) =
+type ILTypeDef
+    (
+        name: string,
+        attributes: TypeAttributes,
+        layout: ILTypeDefLayout,
+        implements: ILTypes,
+        genericParams: ILGenericParameterDefs,
+        extends: ILType option,
+        methods: ILMethodDefs,
+        nestedTypes: ILTypeDefs,
+        fields: ILFieldDefs,
+        methodImpls: ILMethodImplDefs,
+        events: ILEventDefs,
+        properties: ILPropertyDefs,
+        isKnownToBeAttribute: bool,
+        securityDeclsStored: ILSecurityDeclsStored,
+        customAttrsStored: ILAttributesStored,
+        metadataIndex: int32
+    ) =
 
     let mutable customAttrsStored = customAttrsStored
 
-    new (name, attributes, layout, implements, genericParams, extends, methods, nestedTypes, fields, methodImpls, events, properties, isKnownToBeAttribute, securityDecls, customAttrs) =
-       ILTypeDef (name, attributes, layout, implements, genericParams, extends, methods, nestedTypes, fields, methodImpls, events, properties, isKnownToBeAttribute, storeILSecurityDecls securityDecls, storeILCustomAttrs customAttrs, NoMetadataIdx)
+    new(name,
+        attributes,
+        layout,
+        implements,
+        genericParams,
+        extends,
+        methods,
+        nestedTypes,
+        fields,
+        methodImpls,
+        events,
+        properties,
+        isKnownToBeAttribute,
+        securityDecls,
+        customAttrs) =
+        ILTypeDef(
+            name,
+            attributes,
+            layout,
+            implements,
+            genericParams,
+            extends,
+            methods,
+            nestedTypes,
+            fields,
+            methodImpls,
+            events,
+            properties,
+            isKnownToBeAttribute,
+            storeILSecurityDecls securityDecls,
+            storeILCustomAttrs customAttrs,
+            NoMetadataIdx
+        )
 
     member _.Name = name
+
     member _.Attributes = attributes
+
     member _.GenericParams = genericParams
+
     member _.Layout = layout
+
     member _.NestedTypes = nestedTypes
+
     member _.Implements = implements
+
     member _.Extends = extends
+
     member _.Methods = methods
+
     member _.SecurityDeclsStored = securityDeclsStored
+
     member _.Fields = fields
+
     member _.MethodImpls = methodImpls
+
     member _.Events = events
+
     member _.Properties = properties
+
     member _.IsKnownToBeAttribute = isKnownToBeAttribute
+
     member _.CustomAttrsStored = customAttrsStored
+
     member _.MetadataIndex = metadataIndex
 
-    member x.With(?name, ?attributes, ?layout, ?implements, ?genericParams, ?extends, ?methods, ?nestedTypes, ?fields, ?methodImpls, ?events, ?properties, ?isKnownToBeAttribute, ?customAttrs, ?securityDecls) =
-        ILTypeDef(name=defaultArg name x.Name,
-                  attributes=defaultArg attributes x.Attributes,
-                  layout=defaultArg layout x.Layout,
-                  genericParams = defaultArg genericParams x.GenericParams,
-                  nestedTypes = defaultArg nestedTypes x.NestedTypes,
-                  implements = defaultArg implements x.Implements,
-                  extends = defaultArg extends x.Extends,
-                  methods = defaultArg methods x.Methods,
-                  securityDecls = defaultArg securityDecls x.SecurityDecls,
-                  fields = defaultArg fields x.Fields,
-                  methodImpls = defaultArg methodImpls x.MethodImpls,
-                  events = defaultArg events x.Events,
-                  properties = defaultArg properties x.Properties,
-                  isKnownToBeAttribute = defaultArg isKnownToBeAttribute x.IsKnownToBeAttribute,
-                  customAttrs = defaultArg customAttrs x.CustomAttrs)
+    member x.With
+        (
+            ?name,
+            ?attributes,
+            ?layout,
+            ?implements,
+            ?genericParams,
+            ?extends,
+            ?methods,
+            ?nestedTypes,
+            ?fields,
+            ?methodImpls,
+            ?events,
+            ?properties,
+            ?isKnownToBeAttribute,
+            ?customAttrs,
+            ?securityDecls
+        ) =
+        ILTypeDef(
+            name = defaultArg name x.Name,
+            attributes = defaultArg attributes x.Attributes,
+            layout = defaultArg layout x.Layout,
+            genericParams = defaultArg genericParams x.GenericParams,
+            nestedTypes = defaultArg nestedTypes x.NestedTypes,
+            implements = defaultArg implements x.Implements,
+            extends = defaultArg extends x.Extends,
+            methods = defaultArg methods x.Methods,
+            securityDecls = defaultArg securityDecls x.SecurityDecls,
+            fields = defaultArg fields x.Fields,
+            methodImpls = defaultArg methodImpls x.MethodImpls,
+            events = defaultArg events x.Events,
+            properties = defaultArg properties x.Properties,
+            isKnownToBeAttribute = defaultArg isKnownToBeAttribute x.IsKnownToBeAttribute,
+            customAttrs = defaultArg customAttrs x.CustomAttrs
+        )
 
     member x.CustomAttrs =
         match customAttrsStored with
@@ -2159,56 +2719,113 @@ type ILTypeDef(name: string, attributes: TypeAttributes, layout: ILTypeDefLayout
             let res = ILAttributes(f x.MetadataIndex)
             customAttrsStored <- ILAttributesStored.Given res
             res
-        | ILAttributesStored.Given res ->
-            res
+        | ILAttributesStored.Given res -> res
 
     member x.SecurityDecls = x.SecurityDeclsStored.GetSecurityDecls x.MetadataIndex
 
-    member x.IsClass = (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Class
-    member x.IsStruct = (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.ValueType
-    member x.IsInterface = (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Interface
-    member x.IsEnum = (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Enum
-    member x.IsDelegate = (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Delegate
+    member x.IsClass =
+        (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Class
+
+    member x.IsStruct =
+        (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.ValueType
+
+    member x.IsInterface =
+        (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Interface
+
+    member x.IsEnum =
+        (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Enum
+
+    member x.IsDelegate =
+        (typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Delegate
+
     member x.Access = typeAccessOfFlags (int x.Attributes)
     member x.IsAbstract = x.Attributes &&& TypeAttributes.Abstract <> enum 0
     member x.IsSealed = x.Attributes &&& TypeAttributes.Sealed <> enum 0
     member x.IsSerializable = x.Attributes &&& TypeAttributes.Serializable <> enum 0
-    member x.IsComInterop = x.Attributes &&& TypeAttributes.Import <> enum 0 (* Class or interface generated for COM interop *)
+
+    member x.IsComInterop =
+        x.Attributes &&& TypeAttributes.Import
+        <> enum 0 (* Class or interface generated for COM interop *)
+
     member x.IsSpecialName = x.Attributes &&& TypeAttributes.SpecialName <> enum 0
     member x.HasSecurity = x.Attributes &&& TypeAttributes.HasSecurity <> enum 0
     member x.Encoding = typeEncodingOfFlags (int x.Attributes)
     member x.IsStructOrEnum = x.IsStruct || x.IsEnum
-    member x.WithAccess(access) = x.With(attributes=(x.Attributes &&& ~~~TypeAttributes.VisibilityMask ||| convertTypeAccessFlags access))
-    member x.WithNestedAccess(access) = x.With(attributes=(x.Attributes &&& ~~~TypeAttributes.VisibilityMask ||| convertToNestedTypeAccess access))
-    member x.WithSealed(condition) = x.With(attributes=(x.Attributes |> conditionalAdd condition TypeAttributes.Sealed))
-    member x.WithSerializable(condition) = x.With(attributes=(x.Attributes |> conditionalAdd condition TypeAttributes.Serializable))
-    member x.WithAbstract(condition) = x.With(attributes=(x.Attributes |> conditionalAdd condition TypeAttributes.Abstract))
-    member x.WithImport(condition) = x.With(attributes=(x.Attributes |> conditionalAdd condition TypeAttributes.Import))
-    member x.WithHasSecurity(condition) = x.With(attributes=(x.Attributes |> conditionalAdd condition TypeAttributes.HasSecurity))
-    member x.WithLayout(layout) = x.With(attributes=(x.Attributes ||| convertLayout layout), layout = layout)
-    member x.WithKind(kind) = x.With(attributes=(x.Attributes ||| convertTypeKind kind), extends = match kind with ILTypeDefKind.Interface -> None | _ -> x.Extends)
-    member x.WithEncoding(encoding) = x.With(attributes=(x.Attributes &&& ~~~TypeAttributes.StringFormatMask ||| convertEncoding encoding))
-    member x.WithSpecialName(condition) = x.With(attributes=(x.Attributes |> conditionalAdd condition TypeAttributes.SpecialName))
-    member x.WithInitSemantics(init) = x.With(attributes=(x.Attributes ||| convertInitSemantics init))
-
-and [] ILTypeDefs(f : unit -> ILPreTypeDef[]) =
+
+    member x.WithAccess(access) =
+        x.With(
+            attributes =
+                (x.Attributes &&& ~~~TypeAttributes.VisibilityMask
+                 ||| convertTypeAccessFlags access)
+        )
+
+    member x.WithNestedAccess(access) =
+        x.With(
+            attributes =
+                (x.Attributes &&& ~~~TypeAttributes.VisibilityMask
+                 ||| convertToNestedTypeAccess access)
+        )
+
+    member x.WithSealed(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition TypeAttributes.Sealed))
+
+    member x.WithSerializable(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition TypeAttributes.Serializable))
+
+    member x.WithAbstract(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition TypeAttributes.Abstract))
+
+    member x.WithImport(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition TypeAttributes.Import))
+
+    member x.WithHasSecurity(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition TypeAttributes.HasSecurity))
+
+    member x.WithLayout(layout) =
+        x.With(attributes = (x.Attributes ||| convertLayout layout), layout = layout)
+
+    member x.WithKind(kind) =
+        x.With(
+            attributes = (x.Attributes ||| convertTypeKind kind),
+            extends =
+                match kind with
+                | ILTypeDefKind.Interface -> None
+                | _ -> x.Extends
+        )
+
+    member x.WithEncoding(encoding) =
+        x.With(attributes = (x.Attributes &&& ~~~TypeAttributes.StringFormatMask ||| convertEncoding encoding))
+
+    member x.WithSpecialName(condition) =
+        x.With(attributes = (x.Attributes |> conditionalAdd condition TypeAttributes.SpecialName))
+
+    member x.WithInitSemantics(init) =
+        x.With(attributes = (x.Attributes ||| convertInitSemantics init))
+
+and [] ILTypeDefs(f: unit -> ILPreTypeDef[]) =
 
     let mutable array = InlineDelayInit<_>(f)
 
-    let mutable dict = InlineDelayInit<_>(fun () ->
-        let arr = array.Value
-        let t = Dictionary<_, _>(HashIdentity.Structural)
-        for pre in arr do
-            let key = pre.Namespace, pre.Name
-            t[key] <- pre
-        ReadOnlyDictionary t)
+    let mutable dict =
+        InlineDelayInit<_>(fun () ->
+            let arr = array.Value
+            let t = Dictionary<_, _>(HashIdentity.Structural)
+
+            for pre in arr do
+                let key = pre.Namespace, pre.Name
+                t[key] <- pre
+
+            ReadOnlyDictionary t)
 
-    member x.AsArray() = [| for pre in array.Value -> pre.GetTypeDef() |]
+    member x.AsArray() =
+        [| for pre in array.Value -> pre.GetTypeDef() |]
 
-    member x.AsList() = [ for pre in array.Value -> pre.GetTypeDef() ]
+    member x.AsList() =
+        [ for pre in array.Value -> pre.GetTypeDef() ]
 
     interface IEnumerable with
-        member x.GetEnumerator() = ((x :> IEnumerable).GetEnumerator() :> IEnumerator)
+        member x.GetEnumerator() =
+            ((x :> IEnumerable).GetEnumerator() :> IEnumerator)
 
     interface IEnumerable with
         member x.GetEnumerator() =
@@ -2218,18 +2835,16 @@ and [] ILTypeDefs(f : unit -> ILPreTypeDef[]) =
 
     member x.FindByName nm =
         let ns, n = splitILTypeName nm
-        dict.Value[(ns, n)].GetTypeDef()
-
+        dict.Value[ (ns, n) ].GetTypeDef()
 
 and [] ILPreTypeDef =
     abstract Namespace: string list
     abstract Name: string
     abstract GetTypeDef: unit -> ILTypeDef
 
-
 /// This is a memory-critical class. Very many of these objects get allocated and held to represent the contents of .NET assemblies.
 and [] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIndex: int32, storage: ILTypeDefStored) =
-    let mutable store : ILTypeDef = Unchecked.defaultof<_>
+    let mutable store: ILTypeDef = Unchecked.defaultof<_>
 
     interface ILPreTypeDef with
         member _.Namespace = nameSpace
@@ -2242,10 +2857,8 @@ and [] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIn
                 | ILTypeDefStored.Given td ->
                     store <- td
                     td
-                | ILTypeDefStored.Computed f ->
-                    LazyInitializer.EnsureInitialized(&store, Func<_>(fun () -> f()))
-                | ILTypeDefStored.Reader f ->
-                    LazyInitializer.EnsureInitialized(&store, Func<_>(fun () -> f metadataIndex))
+                | ILTypeDefStored.Computed f -> LazyInitializer.EnsureInitialized(&store, Func<_>(fun () -> f ()))
+                | ILTypeDefStored.Reader f -> LazyInitializer.EnsureInitialized(&store, Func<_>(fun () -> f metadataIndex))
             | _ -> store
 
 and ILTypeDefStored =
@@ -2256,61 +2869,67 @@ and ILTypeDefStored =
 let mkILTypeDefReader f = ILTypeDefStored.Reader f
 
 type ILNestedExportedType =
-    { Name: string
-      Access: ILMemberAccess
-      Nested: ILNestedExportedTypes
-      CustomAttrsStored: ILAttributesStored
-      MetadataIndex: int32 }
+    {
+        Name: string
+        Access: ILMemberAccess
+        Nested: ILNestedExportedTypes
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
+    }
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
 
 and ILNestedExportedTypes =
     | ILNestedExportedTypes of Lazy>
 
-    member x.AsList() = let (ILNestedExportedTypes ltab) = x in Map.foldBack (fun _x y r -> y :: r) (ltab.Force()) []
+    member x.AsList() =
+        let (ILNestedExportedTypes ltab) = x in Map.foldBack (fun _x y r -> y :: r) (ltab.Force()) []
 
-and []
-    ILExportedTypeOrForwarder =
-    { ScopeRef: ILScopeRef
-      Name: string
-      Attributes: TypeAttributes
-      Nested: ILNestedExportedTypes
-      CustomAttrsStored: ILAttributesStored
-      MetadataIndex: int32 }
+and [] ILExportedTypeOrForwarder =
+    {
+        ScopeRef: ILScopeRef
+        Name: string
+        Attributes: TypeAttributes
+        Nested: ILNestedExportedTypes
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
+    }
 
     member x.Access = typeAccessOfFlags (int x.Attributes)
 
-    member x.IsForwarder = x.Attributes &&& enum(0x00200000) <> enum 0
+    member x.IsForwarder = x.Attributes &&& enum (0x00200000) <> enum 0
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
 
 and ILExportedTypesAndForwarders =
     | ILExportedTypesAndForwarders of Lazy>
 
-    member x.AsList() = let (ILExportedTypesAndForwarders ltab) = x in Map.foldBack (fun _x y r -> y :: r) (ltab.Force()) []
+    member x.AsList() =
+        let (ILExportedTypesAndForwarders ltab) = x in Map.foldBack (fun _x y r -> y :: r) (ltab.Force()) []
 
     member x.TryFindByName nm =
         match x with
-        | ILExportedTypesAndForwarders ltab ->
-            ltab.Value.TryFind nm
+        | ILExportedTypesAndForwarders ltab -> ltab.Value.TryFind nm
 
 []
 type ILResourceAccess =
     | Public
     | Private
 
-[]
+[]
 type ILResourceLocation =
     | Local of ByteStorage
     | File of ILModuleRef * int32
     | Assembly of ILAssemblyRef
 
 type ILResource =
-    { Name: string
-      Location: ILResourceLocation
-      Access: ILResourceAccess
-      CustomAttrsStored: ILAttributesStored
-      MetadataIndex: int32 }
+    {
+        Name: string
+        Location: ILResourceLocation
+        Access: ILResourceAccess
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
+    }
 
     /// Read the bytes from a resource local to an assembly
     member r.GetBytes() =
@@ -2340,26 +2959,27 @@ type ILAssemblyLongevity =
     static member Default = Unspecified
 
 type ILAssemblyManifest =
-    { Name: string
-      AuxModuleHashAlgorithm: int32
-      SecurityDeclsStored: ILSecurityDeclsStored
-      PublicKey: byte[] option
-      Version: ILVersionInfo option
-      Locale: Locale option
-      CustomAttrsStored: ILAttributesStored
-
-      AssemblyLongevity: ILAssemblyLongevity
-      DisableJitOptimizations: bool
-      JitTracking: bool
-      IgnoreSymbolStoreSequencePoints: bool
-      Retargetable: bool
-
-      /// Records the types implemented by other modules.
-      ExportedTypes: ILExportedTypesAndForwarders
-
-      /// Records whether the entrypoint resides in another module.
-      EntrypointElsewhere: ILModuleRef option
-      MetadataIndex: int32
+    {
+        Name: string
+        AuxModuleHashAlgorithm: int32
+        SecurityDeclsStored: ILSecurityDeclsStored
+        PublicKey: byte[] option
+        Version: ILVersionInfo option
+        Locale: Locale option
+        CustomAttrsStored: ILAttributesStored
+
+        AssemblyLongevity: ILAssemblyLongevity
+        DisableJitOptimizations: bool
+        JitTracking: bool
+        IgnoreSymbolStoreSequencePoints: bool
+        Retargetable: bool
+
+        /// Records the types implemented by other modules.
+        ExportedTypes: ILExportedTypesAndForwarders
+
+        /// Records whether the entrypoint resides in another module.
+        EntrypointElsewhere: ILModuleRef option
+        MetadataIndex: int32
     }
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
@@ -2372,36 +2992,40 @@ type ILNativeResource =
     | Out of unlinkedResource: byte[]
 
 type ILModuleDef =
-    { Manifest: ILAssemblyManifest option
-      Name: string
-      TypeDefs: ILTypeDefs
-      SubsystemVersion : int * int
-      UseHighEntropyVA : bool
-      SubSystemFlags: int32
-      IsDLL: bool
-      IsILOnly: bool
-      Platform: ILPlatform option
-      StackReserveSize: int32 option
-      Is32Bit: bool
-      Is32BitPreferred: bool
-      Is64Bit: bool
-      VirtualAlignment: int32
-      PhysicalAlignment: int32
-      ImageBase: int32
-      MetadataVersion: string
-      Resources: ILResources
-      /// e.g. win32 resources
-      NativeResources: ILNativeResource list
-      CustomAttrsStored: ILAttributesStored
-      MetadataIndex: int32
+    {
+        Manifest: ILAssemblyManifest option
+        Name: string
+        TypeDefs: ILTypeDefs
+        SubsystemVersion: int * int
+        UseHighEntropyVA: bool
+        SubSystemFlags: int32
+        IsDLL: bool
+        IsILOnly: bool
+        Platform: ILPlatform option
+        StackReserveSize: int32 option
+        Is32Bit: bool
+        Is32BitPreferred: bool
+        Is64Bit: bool
+        VirtualAlignment: int32
+        PhysicalAlignment: int32
+        ImageBase: int32
+        MetadataVersion: string
+        Resources: ILResources
+        /// e.g. win32 resources
+        NativeResources: ILNativeResource list
+        CustomAttrsStored: ILAttributesStored
+        MetadataIndex: int32
     }
+
     member x.ManifestOfAssembly =
         match x.Manifest with
         | Some m -> m
         | None -> failwith "no manifest"
 
     member m.HasManifest =
-        match m.Manifest with None -> false | _ -> true
+        match m.Manifest with
+        | None -> false
+        | _ -> true
 
     member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
 
@@ -2418,24 +3042,26 @@ let emptyILGenericArgsList = ([]: ILType list)
 // Make ILTypeRefs etc.
 // --------------------------------------------------------------------
 
-
-let mkILNestedTyRef (scope, l, nm) = ILTypeRef.Create (scope, l, nm)
+let mkILNestedTyRef (scope, l, nm) = ILTypeRef.Create(scope, l, nm)
 
 let mkILTyRef (scope, nm) = mkILNestedTyRef (scope, [], nm)
 
 type ILGenericArgsList = ILType list
 
-let mkILTySpec (tref, inst) = ILTypeSpec.Create (tref, inst)
+let mkILTySpec (tref, inst) = ILTypeSpec.Create(tref, inst)
 
 let mkILNonGenericTySpec tref = mkILTySpec (tref, [])
 
 let mkILTyRefInTyRef (tref: ILTypeRef, nm) =
-    mkILNestedTyRef (tref.Scope, tref.Enclosing@[tref.Name], nm)
+    mkILNestedTyRef (tref.Scope, tref.Enclosing @ [ tref.Name ], nm)
 
 let mkILTy boxed tspec =
-    match boxed with AsObject -> mkILBoxedType tspec | _ -> ILType.Value tspec
+    match boxed with
+    | AsObject -> mkILBoxedType tspec
+    | _ -> ILType.Value tspec
 
-let mkILNamedTy vc tref tinst = mkILTy vc (ILTypeSpec.Create (tref, tinst))
+let mkILNamedTy vc tref tinst =
+    mkILTy vc (ILTypeSpec.Create(tref, tinst))
 
 let mkILValueTy tref tinst = mkILNamedTy AsValue tref tinst
 
@@ -2446,10 +3072,9 @@ let mkILNonGenericValueTy tref = mkILNamedTy AsValue tref []
 let mkILNonGenericBoxedTy tref = mkILNamedTy AsObject tref []
 
 let mkSimpleAssemblyRef n =
-  ILAssemblyRef.Create (n, None, None, false, None, None)
+    ILAssemblyRef.Create(n, None, None, false, None, None)
 
-let mkSimpleModRef n =
-    ILModuleRef.Create (n, true, None)
+let mkSimpleModRef n = ILModuleRef.Create(n, true, None)
 
 // --------------------------------------------------------------------
 // The toplevel class of a module is called ""
@@ -2457,28 +3082,33 @@ let mkSimpleModRef n =
 
 let typeNameForGlobalFunctions = ""
 
-let mkILTypeForGlobalFunctions scoref = mkILBoxedType (mkILNonGenericTySpec (ILTypeRef.Create (scoref, [], typeNameForGlobalFunctions)))
+let mkILTypeForGlobalFunctions scoref =
+    mkILBoxedType (mkILNonGenericTySpec (ILTypeRef.Create(scoref, [], typeNameForGlobalFunctions)))
 
 let isTypeNameForGlobalFunctions d = (d = typeNameForGlobalFunctions)
 
 let mkILMethRef (tref, callconv, nm, numGenericParams, argTys, retTy) =
-    { mrefParent=tref
-      mrefCallconv=callconv
-      mrefGenericArity=numGenericParams
-      mrefName=nm
-      mrefArgs=argTys
-      mrefReturn=retTy}
+    {
+        mrefParent = tref
+        mrefCallconv = callconv
+        mrefGenericArity = numGenericParams
+        mrefName = nm
+        mrefArgs = argTys
+        mrefReturn = retTy
+    }
 
 let mkILMethSpecForMethRefInTy (mref, ty, methInst) =
-    { mspecMethodRef=mref
-      mspecDeclaringType=ty
-      mspecMethodInst=methInst }
+    {
+        mspecMethodRef = mref
+        mspecDeclaringType = ty
+        mspecMethodInst = methInst
+    }
 
 let mkILMethSpec (mref, vc, tinst, methInst) =
     mkILMethSpecForMethRefInTy (mref, mkILNamedTy vc mref.DeclaringTypeRef tinst, methInst)
 
 let mkILMethSpecInTypeRef (tref, vc, cc, nm, argTys, retTy, tinst, methInst) =
-    mkILMethSpec (mkILMethRef ( tref, cc, nm, List.length methInst, argTys, retTy), vc, tinst, methInst)
+    mkILMethSpec (mkILMethRef (tref, cc, nm, List.length methInst, argTys, retTy), vc, tinst, methInst)
 
 let mkILMethSpecInTy (ty: ILType, cc, nm, argTys, retTy, methInst: ILGenericArgs) =
     mkILMethSpecForMethRefInTy (mkILMethRef (ty.TypeRef, cc, nm, methInst.Length, argTys, retTy), ty, methInst)
@@ -2502,34 +3132,40 @@ let mkILCtorMethSpec (tref, argTys, tinst) =
     mkILMethSpecInTypeRef (tref, AsObject, ILCallingConv.Instance, ".ctor", argTys, ILType.Void, tinst, [])
 
 let mkILCtorMethSpecForTy (ty, args) =
-  mkILMethSpecInTy (ty, ILCallingConv.Instance, ".ctor", args, ILType.Void, [])
+    mkILMethSpecInTy (ty, ILCallingConv.Instance, ".ctor", args, ILType.Void, [])
 
-let mkILNonGenericCtorMethSpec (tref, args) =
-  mkILCtorMethSpec (tref, args, [])
+let mkILNonGenericCtorMethSpec (tref, args) = mkILCtorMethSpec (tref, args, [])
 
 // --------------------------------------------------------------------
 // Make references to fields
 // --------------------------------------------------------------------
 
-let mkILFieldRef (tref, nm, ty) = { DeclaringTypeRef=tref; Name=nm; Type=ty}
+let mkILFieldRef (tref, nm, ty) =
+    {
+        DeclaringTypeRef = tref
+        Name = nm
+        Type = ty
+    }
 
-let mkILFieldSpec (tref, ty) = { FieldRef= tref; DeclaringType=ty }
+let mkILFieldSpec (tref, ty) = { FieldRef = tref; DeclaringType = ty }
 
 let mkILFieldSpecInTy (ty: ILType, nm, fty) =
     mkILFieldSpec (mkILFieldRef (ty.TypeRef, nm, fty), ty)
 
 let andTailness x y =
-  match x with Tailcall when y -> Tailcall | _ -> Normalcall
+    match x with
+    | Tailcall when y -> Tailcall
+    | _ -> Normalcall
 
 // --------------------------------------------------------------------
 // Basic operations on code.
 // --------------------------------------------------------------------
 
-let formatCodeLabel (x: int) = "L"+string x
+let formatCodeLabel (x: int) = "L" + string x
 
 //  ++GLOBAL MUTABLE STATE (concurrency safe)
 let codeLabelCount = ref 0
-let generateCodeLabel() = Interlocked.Increment codeLabelCount
+let generateCodeLabel () = Interlocked.Increment codeLabelCount
 
 let instrIsRet i =
     match i with
@@ -2538,15 +3174,19 @@ let instrIsRet i =
 
 let nonBranchingInstrsToCode instrs : ILCode =
     let instrs = Array.ofList instrs
-    let instrs =
-        if instrs.Length <> 0 && instrIsRet (Array.last instrs) then instrs
-        else Array.append instrs [| I_ret |]
 
-    { Labels = Dictionary()
-      Instrs = instrs
-      Exceptions = []
-      Locals = [] }
+    let instrs =
+        if instrs.Length <> 0 && instrIsRet (Array.last instrs) then
+            instrs
+        else
+            Array.append instrs [| I_ret |]
 
+    {
+        Labels = Dictionary()
+        Instrs = instrs
+        Exceptions = []
+        Locals = []
+    }
 
 // --------------------------------------------------------------------
 //
@@ -2555,14 +3195,16 @@ let nonBranchingInstrsToCode instrs : ILCode =
 let mkILTyvarTy tv = ILType.TypeVar tv
 
 let mkILSimpleTypar nm =
-   { Name=nm
-     Constraints = []
-     Variance=NonVariant
-     HasReferenceTypeConstraint=false
-     HasNotNullableValueTypeConstraint=false
-     HasDefaultConstructorConstraint=false
-     CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
-     MetadataIndex = NoMetadataIdx }
+    {
+        Name = nm
+        Constraints = []
+        Variance = NonVariant
+        HasReferenceTypeConstraint = false
+        HasNotNullableValueTypeConstraint = false
+        HasDefaultConstructorConstraint = false
+        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+        MetadataIndex = NoMetadataIdx
+    }
 
 let genericParamOfGenericActual (_ga: ILType) = mkILSimpleTypar "T"
 
@@ -2571,9 +3213,11 @@ let mkILFormalTypars (x: ILGenericArgsList) = List.map genericParamOfGenericActu
 let mkILFormalGenericArgs numtypars (gparams: ILGenericParameterDefs) =
     List.mapi (fun n _gf -> mkILTyvarTy (uint16 (numtypars + n))) gparams
 
-let mkILFormalBoxedTy tref gparams = mkILBoxedTy tref (mkILFormalGenericArgs 0 gparams)
+let mkILFormalBoxedTy tref gparams =
+    mkILBoxedTy tref (mkILFormalGenericArgs 0 gparams)
 
-let mkILFormalNamedTy bx tref gparams = mkILNamedTy bx tref (mkILFormalGenericArgs 0 gparams)
+let mkILFormalNamedTy bx tref gparams =
+    mkILNamedTy bx tref (mkILFormalGenericArgs 0 gparams)
 
 // --------------------------------------------------------------------
 // Operations on class etc. defs.
@@ -2588,30 +3232,38 @@ let mkRefForNestedILTypeDef scope (enc: ILTypeDef list, td: ILTypeDef) =
 
 let mkILPreTypeDef (td: ILTypeDef) =
     let ns, n = splitILTypeName td.Name
-    ILPreTypeDefImpl (ns, n, NoMetadataIdx, ILTypeDefStored.Given td) :> ILPreTypeDef
+    ILPreTypeDefImpl(ns, n, NoMetadataIdx, ILTypeDefStored.Given td) :> ILPreTypeDef
+
 let mkILPreTypeDefComputed (ns, n, f) =
-    ILPreTypeDefImpl (ns, n, NoMetadataIdx, ILTypeDefStored.Computed f) :> ILPreTypeDef
+    ILPreTypeDefImpl(ns, n, NoMetadataIdx, ILTypeDefStored.Computed f) :> ILPreTypeDef
+
 let mkILPreTypeDefRead (ns, n, idx, f) =
-    ILPreTypeDefImpl (ns, n, idx, f) :> ILPreTypeDef
+    ILPreTypeDefImpl(ns, n, idx, f) :> ILPreTypeDef
 
+let addILTypeDef td (tdefs: ILTypeDefs) =
+    ILTypeDefs(fun () -> [| yield mkILPreTypeDef td; yield! tdefs.AsArrayOfPreTypeDefs() |])
+
+let mkILTypeDefsFromArray (l: ILTypeDef[]) =
+    ILTypeDefs(fun () -> Array.map mkILPreTypeDef l)
 
-let addILTypeDef td (tdefs: ILTypeDefs) = ILTypeDefs (fun () -> [| yield mkILPreTypeDef td; yield! tdefs.AsArrayOfPreTypeDefs() |])
-let mkILTypeDefsFromArray (l: ILTypeDef[]) = ILTypeDefs (fun () -> Array.map mkILPreTypeDef l)
 let mkILTypeDefs l = mkILTypeDefsFromArray (Array.ofList l)
 let mkILTypeDefsComputed f = ILTypeDefs f
-let emptyILTypeDefs = mkILTypeDefsFromArray [| |]
+let emptyILTypeDefs = mkILTypeDefsFromArray [||]
 
 // --------------------------------------------------------------------
 // Operations on method tables.
 // --------------------------------------------------------------------
 
-let mkILMethodsFromArray xs = ILMethodDefs (fun () -> xs)
-let mkILMethods xs = xs |> Array.ofList |> mkILMethodsFromArray
+let mkILMethodsFromArray xs = ILMethodDefs(fun () -> xs)
+
+let mkILMethods xs =
+    xs |> Array.ofList |> mkILMethodsFromArray
+
 let mkILMethodsComputed f = ILMethodDefs f
-let emptyILMethods = mkILMethodsFromArray [| |]
+let emptyILMethods = mkILMethodsFromArray [||]
 
 let filterILMethodDefs f (mdefs: ILMethodDefs) =
-    ILMethodDefs (fun () -> mdefs.AsArray() |> Array.filter f)
+    ILMethodDefs(fun () -> mdefs.AsArray() |> Array.filter f)
 
 // --------------------------------------------------------------------
 // Operations and defaults for modules, assemblies etc.
@@ -2619,18 +3271,26 @@ let filterILMethodDefs f (mdefs: ILMethodDefs) =
 
 let defaultSubSystem = 3 (* this is what comes out of ILDASM on 30/04/2001 *)
 let defaultPhysAlignment = 512 (* this is what comes out of ILDASM on 30/04/2001 *)
-let defaultVirtAlignment = 0x2000 (* this is what comes out of ILDASM on 30/04/2001 *)
-let defaultImageBase = 0x034f0000 (* this is what comes out of ILDASM on 30/04/2001 *)
+
+let defaultVirtAlignment =
+    0x2000 (* this is what comes out of ILDASM on 30/04/2001 *)
+
+let defaultImageBase =
+    0x034f0000 (* this is what comes out of ILDASM on 30/04/2001 *)
 
 // --------------------------------------------------------------------
 // Array types
 // --------------------------------------------------------------------
 
-let mkILArrTy (ty, shape) = ILType.Array (shape, ty)
+let mkILArrTy (ty, shape) = ILType.Array(shape, ty)
 
-let mkILArr1DTy ty = mkILArrTy (ty, ILArrayShape.SingleDimensional)
+let mkILArr1DTy ty =
+    mkILArrTy (ty, ILArrayShape.SingleDimensional)
 
-let isILArrTy ty = match ty with ILType.Array _ -> true| _ -> false
+let isILArrTy ty =
+    match ty with
+    | ILType.Array _ -> true
+    | _ -> false
 
 let destILArrTy ty =
     match ty with
@@ -2699,45 +3359,71 @@ let tname_UIntPtr = "System.UIntPtr"
 let tname_TypedReference = "System.TypedReference"
 
 []
-type ILGlobals(primaryScopeRef: ILScopeRef, assembliesThatForwardToPrimaryAssembly: ILAssemblyRef list, fsharpCoreAssemblyScopeRef: ILScopeRef) =
+type ILGlobals
+    (
+        primaryScopeRef: ILScopeRef,
+        assembliesThatForwardToPrimaryAssembly: ILAssemblyRef list,
+        fsharpCoreAssemblyScopeRef: ILScopeRef
+    ) =
 
-    let assembliesThatForwardToPrimaryAssembly = Array.ofList assembliesThatForwardToPrimaryAssembly
+    let assembliesThatForwardToPrimaryAssembly =
+        Array.ofList assembliesThatForwardToPrimaryAssembly
 
     let mkSysILTypeRef nm = mkILTyRef (primaryScopeRef, nm)
 
     member _.primaryAssemblyScopeRef = primaryScopeRef
+
     member x.primaryAssemblyRef =
         match primaryScopeRef with
         | ILScopeRef.Assembly aref -> aref
         | _ -> failwith "Invalid primary assembly"
+
     member x.primaryAssemblyName = x.primaryAssemblyRef.Name
 
     member val typ_Object = mkILBoxedType (mkILNonGenericTySpec (mkSysILTypeRef tname_Object))
+
     member val typ_String = mkILBoxedType (mkILNonGenericTySpec (mkSysILTypeRef tname_String))
+
     member val typ_Array = mkILBoxedType (mkILNonGenericTySpec (mkSysILTypeRef tname_Array))
+
     member val typ_Type = mkILBoxedType (mkILNonGenericTySpec (mkSysILTypeRef tname_Type))
-    member val typ_SByte = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_SByte))
-    member val typ_Int16 = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Int16))
-    member val typ_Int32 = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Int32))
-    member val typ_Int64 = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Int64))
-    member val typ_Byte = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Byte))
-    member val typ_UInt16 = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_UInt16))
-    member val typ_UInt32 = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_UInt32))
-    member val typ_UInt64 = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_UInt64))
-    member val typ_Single = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Single))
-    member val typ_Double = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Double))
-    member val typ_Bool = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Bool))
-    member val typ_Char = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_Char))
-    member val typ_IntPtr = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_IntPtr))
-    member val typ_UIntPtr = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_UIntPtr))
-    member val typ_TypedReference = ILType.Value (mkILNonGenericTySpec (mkSysILTypeRef tname_TypedReference))
+
+    member val typ_SByte = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_SByte))
+
+    member val typ_Int16 = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Int16))
+
+    member val typ_Int32 = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Int32))
+
+    member val typ_Int64 = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Int64))
+
+    member val typ_Byte = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Byte))
+
+    member val typ_UInt16 = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_UInt16))
+
+    member val typ_UInt32 = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_UInt32))
+
+    member val typ_UInt64 = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_UInt64))
+
+    member val typ_Single = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Single))
+
+    member val typ_Double = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Double))
+
+    member val typ_Bool = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Bool))
+
+    member val typ_Char = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_Char))
+
+    member val typ_IntPtr = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_IntPtr))
+
+    member val typ_UIntPtr = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_UIntPtr))
+
+    member val typ_TypedReference = ILType.Value(mkILNonGenericTySpec (mkSysILTypeRef tname_TypedReference))
 
     member _.fsharpCoreAssemblyScopeRef = fsharpCoreAssemblyScopeRef
 
     member x.IsPossiblePrimaryAssemblyRef(aref: ILAssemblyRef) =
-        aref.EqualsIgnoringVersion x.primaryAssemblyRef ||
-        assembliesThatForwardToPrimaryAssembly
-        |> Array.exists aref.EqualsIgnoringVersion
+        aref.EqualsIgnoringVersion x.primaryAssemblyRef
+        || assembliesThatForwardToPrimaryAssembly
+           |> Array.exists aref.EqualsIgnoringVersion
 
     /// For debugging
     []
@@ -2745,15 +3431,17 @@ type ILGlobals(primaryScopeRef: ILScopeRef, assembliesThatForwardToPrimaryAssemb
 
     override x.ToString() = ""
 
-let mkILGlobals (primaryScopeRef, assembliesThatForwardToPrimaryAssembly, fsharpCoreAssemblyScopeRef) = ILGlobals (primaryScopeRef, assembliesThatForwardToPrimaryAssembly, fsharpCoreAssemblyScopeRef)
+let mkILGlobals (primaryScopeRef, assembliesThatForwardToPrimaryAssembly, fsharpCoreAssemblyScopeRef) =
+    ILGlobals(primaryScopeRef, assembliesThatForwardToPrimaryAssembly, fsharpCoreAssemblyScopeRef)
 
-let mkNormalCall mspec = I_call (Normalcall, mspec, None)
+let mkNormalCall mspec = I_call(Normalcall, mspec, None)
 
-let mkNormalCallvirt mspec = I_callvirt (Normalcall, mspec, None)
+let mkNormalCallvirt mspec = I_callvirt(Normalcall, mspec, None)
 
-let mkNormalCallconstraint (ty, mspec) = I_callconstraint (Normalcall, ty, mspec, None)
+let mkNormalCallconstraint (ty, mspec) =
+    I_callconstraint(Normalcall, ty, mspec, None)
 
-let mkNormalNewobj mspec = I_newobj (mspec, None)
+let mkNormalNewobj mspec = I_newobj(mspec, None)
 
 /// Comment on common object cache sizes:
 /// mkLdArg - I can't imagine any IL method we generate needing more than this
@@ -2761,49 +3449,74 @@ let mkNormalNewobj mspec = I_newobj (mspec, None)
 /// mkStLoc - it should be the same as LdLoc (where there's a LdLoc there must be a StLoc)
 /// mkLdcInt32 - just a guess
 
-let ldargs = [| for i in 0 .. 128 -> I_ldarg (uint16 i) |]
+let ldargs = [| for i in 0..128 -> I_ldarg(uint16 i) |]
 
-let mkLdarg i = if 0us < i && i < uint16 ldargs.Length then ldargs[int i] else I_ldarg i
+let mkLdarg i =
+    if 0us < i && i < uint16 ldargs.Length then
+        ldargs[int i]
+    else
+        I_ldarg i
 
 let mkLdarg0 = mkLdarg 0us
 
-let ldlocs = [| for i in 0 .. 512 -> I_ldloc (uint16 i) |]
+let ldlocs = [| for i in 0..512 -> I_ldloc(uint16 i) |]
 
-let mkLdloc i = if 0us < i && i < uint16 ldlocs.Length then ldlocs[int i] else I_ldloc i
+let mkLdloc i =
+    if 0us < i && i < uint16 ldlocs.Length then
+        ldlocs[int i]
+    else
+        I_ldloc i
 
-let stlocs = [| for i in 0 .. 512 -> I_stloc (uint16 i) |]
+let stlocs = [| for i in 0..512 -> I_stloc(uint16 i) |]
 
-let mkStloc i = if 0us < i && i < uint16 stlocs.Length then stlocs[int i] else I_stloc i
+let mkStloc i =
+    if 0us < i && i < uint16 stlocs.Length then
+        stlocs[int i]
+    else
+        I_stloc i
 
-let ldi32s = [| for i in 0 .. 256 -> AI_ldc (DT_I4, ILConst.I4 i) |]
+let ldi32s = [| for i in 0..256 -> AI_ldc(DT_I4, ILConst.I4 i) |]
 
-let mkLdcInt32 i = if 0 < i && i < ldi32s.Length then ldi32s[i] else AI_ldc (DT_I4, ILConst.I4 i)
+let mkLdcInt32 i =
+    if 0 < i && i < ldi32s.Length then
+        ldi32s[i]
+    else
+        AI_ldc(DT_I4, ILConst.I4 i)
 
-let tname_CompilerGeneratedAttribute = "System.Runtime.CompilerServices.CompilerGeneratedAttribute"
+let tname_CompilerGeneratedAttribute =
+    "System.Runtime.CompilerServices.CompilerGeneratedAttribute"
 
 let tname_DebuggableAttribute = "System.Diagnostics.DebuggableAttribute"
 
 (* NOTE: ecma_ prefix refers to the standard "mscorlib" *)
-let ecmaPublicKey = PublicKeyToken (Bytes.ofInt32Array [|0xde; 0xad; 0xbe; 0xef; 0xca; 0xfe; 0xfa; 0xce |])
+let ecmaPublicKey =
+    PublicKeyToken(Bytes.ofInt32Array [| 0xde; 0xad; 0xbe; 0xef; 0xca; 0xfe; 0xfa; 0xce |])
 
-let isILBoxedTy = function ILType.Boxed _ -> true | _ -> false
+let isILBoxedTy =
+    function
+    | ILType.Boxed _ -> true
+    | _ -> false
 
-let isILValueTy = function ILType.Value _ -> true | _ -> false
+let isILValueTy =
+    function
+    | ILType.Value _ -> true
+    | _ -> false
 
 let rec stripILModifiedFromTy (ty: ILType) =
     match ty with
-    | ILType.Modified(_, _, ty) -> stripILModifiedFromTy ty
+    | ILType.Modified (_, _, ty) -> stripILModifiedFromTy ty
     | _ -> ty
 
 let isBuiltInTySpec (ilg: ILGlobals) (tspec: ILTypeSpec) n =
     let tref = tspec.TypeRef
     let scoref = tref.Scope
-    tref.Name = n &&
-    (match scoref with
-     | ILScopeRef.Local
-     | ILScopeRef.Module _ -> false
-     | ILScopeRef.Assembly aref -> ilg.IsPossiblePrimaryAssemblyRef aref
-     | ILScopeRef.PrimaryAssembly -> true)
+
+    tref.Name = n
+    && (match scoref with
+        | ILScopeRef.Local
+        | ILScopeRef.Module _ -> false
+        | ILScopeRef.Assembly aref -> ilg.IsPossiblePrimaryAssemblyRef aref
+        | ILScopeRef.PrimaryAssembly -> true)
 
 let isILBoxedBuiltInTy ilg (ty: ILType) n =
     isILBoxedTy ty && isBuiltInTySpec ilg ty.TypeSpec n
@@ -2815,7 +3528,8 @@ let isILObjectTy ilg ty = isILBoxedBuiltInTy ilg ty tname_Object
 
 let isILStringTy ilg ty = isILBoxedBuiltInTy ilg ty tname_String
 
-let isILTypedReferenceTy ilg ty = isILValueBuiltInTy ilg ty tname_TypedReference
+let isILTypedReferenceTy ilg ty =
+    isILValueBuiltInTy ilg ty tname_TypedReference
 
 let isILSByteTy ilg ty = isILValueBuiltInTy ilg ty tname_SByte
 
@@ -2860,8 +3574,11 @@ let rescopeILScopeRef scoref scoref1 =
 let rescopeILTypeRef scoref (tref1: ILTypeRef) =
     let scoref1 = tref1.Scope
     let scoref2 = rescopeILScopeRef scoref scoref1
-    if scoref1 === scoref2 then tref1
-    else ILTypeRef.Create (scoref2, tref1.Enclosing, tref1.Name)
+
+    if scoref1 === scoref2 then
+        tref1
+    else
+        ILTypeRef.Create(scoref2, tref1.Enclosing, tref1.Name)
 
 // ORIGINAL IMPLEMENTATION (too many allocations
 //         { tspecTypeRef=rescopeILTypeRef scoref tref
@@ -2873,75 +3590,97 @@ let rec rescopeILTypeSpec scoref (tspec1: ILTypeSpec) =
 
     // avoid reallocation in the common case
     if tref1 === tref2 then
-        if isNil tinst1 then tspec1 else
-        let tinst2 = rescopeILTypes scoref tinst1
-        if tinst1 === tinst2 then tspec1 else
-        ILTypeSpec.Create (tref2, tinst2)
+        if isNil tinst1 then
+            tspec1
+        else
+            let tinst2 = rescopeILTypes scoref tinst1
+
+            if tinst1 === tinst2 then
+                tspec1
+            else
+                ILTypeSpec.Create(tref2, tinst2)
     else
         let tinst2 = rescopeILTypes scoref tinst1
-        ILTypeSpec.Create (tref2, tinst2)
+        ILTypeSpec.Create(tref2, tinst2)
 
 and rescopeILType scoref ty =
     match ty with
-    | ILType.Ptr t -> ILType.Ptr (rescopeILType scoref t)
-    | ILType.FunctionPointer t -> ILType.FunctionPointer (rescopeILCallSig scoref t)
-    | ILType.Byref t -> ILType.Byref (rescopeILType scoref t)
+    | ILType.Ptr t -> ILType.Ptr(rescopeILType scoref t)
+    | ILType.FunctionPointer t -> ILType.FunctionPointer(rescopeILCallSig scoref t)
+    | ILType.Byref t -> ILType.Byref(rescopeILType scoref t)
     | ILType.Boxed cr1 ->
         let cr2 = rescopeILTypeSpec scoref cr1
-        if cr1 === cr2 then ty else
-        mkILBoxedType cr2
+
+        if cr1 === cr2 then
+            ty
+        else
+            mkILBoxedType cr2
     | ILType.Array (s, ety1) ->
         let ety2 = rescopeILType scoref ety1
-        if ety1 === ety2 then ty else
-        ILType.Array (s, ety2)
+
+        if ety1 === ety2 then
+            ty
+        else
+            ILType.Array(s, ety2)
     | ILType.Value cr1 ->
         let cr2 = rescopeILTypeSpec scoref cr1
-        if cr1 === cr2 then ty else
-        ILType.Value cr2
-    | ILType.Modified (b, tref, ty) -> ILType.Modified (b, rescopeILTypeRef scoref tref, rescopeILType scoref ty)
+
+        if cr1 === cr2 then
+            ty
+        else
+            ILType.Value cr2
+    | ILType.Modified (b, tref, ty) -> ILType.Modified(b, rescopeILTypeRef scoref tref, rescopeILType scoref ty)
     | x -> x
 
 and rescopeILTypes scoref i =
-    if isNil i then i
-    else List.mapq (rescopeILType scoref) i
+    if isNil i then
+        i
+    else
+        List.mapq (rescopeILType scoref) i
 
 and rescopeILCallSig scoref csig =
     mkILCallSig (csig.CallingConv, rescopeILTypes scoref csig.ArgTypes, rescopeILType scoref csig.ReturnType)
 
 let rescopeILMethodRef scoref (x: ILMethodRef) =
-    { mrefParent = rescopeILTypeRef scoref x.DeclaringTypeRef
-      mrefCallconv = x.mrefCallconv
-      mrefGenericArity=x.mrefGenericArity
-      mrefName=x.mrefName
-      mrefArgs = rescopeILTypes scoref x.mrefArgs
-      mrefReturn= rescopeILType scoref x.mrefReturn }
+    {
+        mrefParent = rescopeILTypeRef scoref x.DeclaringTypeRef
+        mrefCallconv = x.mrefCallconv
+        mrefGenericArity = x.mrefGenericArity
+        mrefName = x.mrefName
+        mrefArgs = rescopeILTypes scoref x.mrefArgs
+        mrefReturn = rescopeILType scoref x.mrefReturn
+    }
 
 let rescopeILFieldRef scoref x =
-    { DeclaringTypeRef = rescopeILTypeRef scoref x.DeclaringTypeRef
-      Name= x.Name
-      Type= rescopeILType scoref x.Type }
+    {
+        DeclaringTypeRef = rescopeILTypeRef scoref x.DeclaringTypeRef
+        Name = x.Name
+        Type = rescopeILType scoref x.Type
+    }
 
 // --------------------------------------------------------------------
 // Instantiate polymorphism in types
 // --------------------------------------------------------------------
 
 let rec instILTypeSpecAux numFree inst (tspec: ILTypeSpec) =
-    ILTypeSpec.Create (tspec.TypeRef, instILGenericArgsAux numFree inst tspec.GenericArgs)
+    ILTypeSpec.Create(tspec.TypeRef, instILGenericArgsAux numFree inst tspec.GenericArgs)
 
 and instILTypeAux numFree (inst: ILGenericArgs) ty =
     match ty with
-    | ILType.Ptr t -> ILType.Ptr (instILTypeAux numFree inst t)
-    | ILType.FunctionPointer t -> ILType.FunctionPointer (instILCallSigAux numFree inst t)
-    | ILType.Array (a, t) -> ILType.Array (a, instILTypeAux numFree inst t)
-    | ILType.Byref t -> ILType.Byref (instILTypeAux numFree inst t)
+    | ILType.Ptr t -> ILType.Ptr(instILTypeAux numFree inst t)
+    | ILType.FunctionPointer t -> ILType.FunctionPointer(instILCallSigAux numFree inst t)
+    | ILType.Array (a, t) -> ILType.Array(a, instILTypeAux numFree inst t)
+    | ILType.Byref t -> ILType.Byref(instILTypeAux numFree inst t)
     | ILType.Boxed cr -> mkILBoxedType (instILTypeSpecAux numFree inst cr)
-    | ILType.Value cr -> ILType.Value (instILTypeSpecAux numFree inst cr)
+    | ILType.Value cr -> ILType.Value(instILTypeSpecAux numFree inst cr)
     | ILType.TypeVar v ->
         let v = int v
         let top = inst.Length
-        if v < numFree then ty else
-        if v - numFree >= top then
-            ILType.TypeVar (uint16 (v - top))
+
+        if v < numFree then
+            ty
+        else if v - numFree >= top then
+            ILType.TypeVar(uint16 (v - top))
         else
             List.item (v - numFree) inst
     | x -> x
@@ -2949,7 +3688,7 @@ and instILTypeAux numFree (inst: ILGenericArgs) ty =
 and instILGenericArgsAux numFree inst i = List.map (instILTypeAux numFree inst) i
 
 and instILCallSigAux numFree inst csig =
-  mkILCallSig (csig.CallingConv, List.map (instILTypeAux numFree inst) csig.ArgTypes, instILTypeAux numFree inst csig.ReturnType)
+    mkILCallSig (csig.CallingConv, List.map (instILTypeAux numFree inst) csig.ArgTypes, instILTypeAux numFree inst csig.ReturnType)
 
 let instILType i t = instILTypeAux 0 i t
 
@@ -2958,32 +3697,39 @@ let instILType i t = instILTypeAux 0 i t
 // --------------------------------------------------------------------
 
 let mkILParam (name, ty) : ILParameter =
-    { Name=name
-      Default=None
-      Marshal=None
-      IsIn=false
-      IsOut=false
-      IsOptional=false
-      Type=ty
-      CustomAttrsStored=storeILCustomAttrs emptyILCustomAttrs
-      MetadataIndex = NoMetadataIdx }
+    {
+        Name = name
+        Default = None
+        Marshal = None
+        IsIn = false
+        IsOut = false
+        IsOptional = false
+        Type = ty
+        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+        MetadataIndex = NoMetadataIdx
+    }
 
 let mkILParamNamed (s, ty) = mkILParam (Some s, ty)
 
 let mkILParamAnon ty = mkILParam (None, ty)
 
 let mkILReturn ty : ILReturn =
-    { Marshal=None
-      Type=ty
-      CustomAttrsStored=storeILCustomAttrs emptyILCustomAttrs
-      MetadataIndex = NoMetadataIdx }
+    {
+        Marshal = None
+        Type = ty
+        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+        MetadataIndex = NoMetadataIdx
+    }
 
 let mkILLocal ty dbgInfo : ILLocal =
-    { IsPinned=false
-      Type=ty
-      DebugInfo=dbgInfo }
+    {
+        IsPinned = false
+        Type = ty
+        DebugInfo = dbgInfo
+    }
 
 type ILFieldSpec with
+
     member fr.ActualType =
         let env = fr.DeclaringType.GenericArgs
         instILType env fr.FormalType
@@ -2993,18 +3739,20 @@ type ILFieldSpec with
 // --------------------------------------------------------------------
 
 let mkILMethodBody (initlocals, locals, maxstack, code, tag, imports) : ILMethodBody =
-    { IsZeroInit=initlocals
-      MaxStack=maxstack
-      NoInlining=false
-      AggressiveInlining=false
-      Locals= locals
-      Code= code
-      DebugRange=tag
-      DebugImports=imports }
+    {
+        IsZeroInit = initlocals
+        MaxStack = maxstack
+        NoInlining = false
+        AggressiveInlining = false
+        Locals = locals
+        Code = code
+        DebugRange = tag
+        DebugImports = imports
+    }
 
 let mkMethodBody (zeroinit, locals, maxstack, code, tag, imports) =
     let ilCode = mkILMethodBody (zeroinit, locals, maxstack, code, tag, imports)
-    MethodBody.IL (lazy ilCode)
+    MethodBody.IL(lazy ilCode)
 
 // --------------------------------------------------------------------
 // Make a constructor
@@ -3019,40 +3767,45 @@ let methBodyAbstract = notlazy MethodBody.Abstract
 let methBodyNative = notlazy MethodBody.Native
 
 let mkILCtor (access, args, impl) =
-    ILMethodDef(name=".ctor",
-                attributes=(convertMemberAccess access ||| MethodAttributes.SpecialName ||| MethodAttributes.RTSpecialName),
-                implAttributes=MethodImplAttributes.Managed,
-                callingConv=ILCallingConv.Instance,
-                parameters = args,
-                ret= mkILVoidReturn,
-                body= notlazy impl,
-                securityDecls=emptyILSecurityDecls,
-                isEntryPoint=false,
-                genericParams=mkILEmptyGenericParams,
-                customAttrs = emptyILCustomAttrs)
+    ILMethodDef(
+        name = ".ctor",
+        attributes =
+            (convertMemberAccess access
+             ||| MethodAttributes.SpecialName
+             ||| MethodAttributes.RTSpecialName),
+        implAttributes = MethodImplAttributes.Managed,
+        callingConv = ILCallingConv.Instance,
+        parameters = args,
+        ret = mkILVoidReturn,
+        body = notlazy impl,
+        securityDecls = emptyILSecurityDecls,
+        isEntryPoint = false,
+        genericParams = mkILEmptyGenericParams,
+        customAttrs = emptyILCustomAttrs
+    )
 
 // --------------------------------------------------------------------
 // Do-nothing ctor, just pass on to monomorphic superclass
 // --------------------------------------------------------------------
 
 let mkCallBaseConstructor (ty, args: ILType list) =
-    [ mkLdarg0 ] @
-    List.mapi (fun i _ -> mkLdarg (uint16 (i+1))) args @
-    [ mkNormalCall (mkILCtorMethSpecForTy (ty, [])) ]
+    [ mkLdarg0 ]
+    @ List.mapi (fun i _ -> mkLdarg (uint16 (i + 1))) args
+      @ [ mkNormalCall (mkILCtorMethSpecForTy (ty, [])) ]
 
-let mkNormalStfld fspec = I_stfld (Aligned, Nonvolatile, fspec)
+let mkNormalStfld fspec = I_stfld(Aligned, Nonvolatile, fspec)
 
-let mkNormalStsfld fspec = I_stsfld (Nonvolatile, fspec)
+let mkNormalStsfld fspec = I_stsfld(Nonvolatile, fspec)
 
-let mkNormalLdsfld fspec = I_ldsfld (Nonvolatile, fspec)
+let mkNormalLdsfld fspec = I_ldsfld(Nonvolatile, fspec)
 
-let mkNormalLdfld fspec = I_ldfld (Aligned, Nonvolatile, fspec)
+let mkNormalLdfld fspec = I_ldfld(Aligned, Nonvolatile, fspec)
 
 let mkNormalLdflda fspec = I_ldflda fspec
 
-let mkNormalLdobj dt = I_ldobj (Aligned, Nonvolatile, dt)
+let mkNormalLdobj dt = I_ldobj(Aligned, Nonvolatile, dt)
 
-let mkNormalStobj dt = I_stobj (Aligned, Nonvolatile, dt)
+let mkNormalStobj dt = I_stobj(Aligned, Nonvolatile, dt)
 
 let mkILNonGenericEmptyCtor (superTy, tag, imports) =
     let ctor = mkCallBaseConstructor (superTy, [])
@@ -3065,33 +3818,41 @@ let mkILNonGenericEmptyCtor (superTy, tag, imports) =
 // --------------------------------------------------------------------
 
 let mkILStaticMethod (genparams, nm, access, args, ret, impl) =
-    ILMethodDef(genericParams=genparams,
-                name=nm,
-                attributes=(convertMemberAccess access ||| MethodAttributes.Static),
-                implAttributes=MethodImplAttributes.Managed,
-                callingConv = ILCallingConv.Static,
-                parameters = args,
-                ret= ret,
-                securityDecls=emptyILSecurityDecls,
-                isEntryPoint=false,
-                customAttrs = emptyILCustomAttrs,
-                body= notlazy impl)
+    ILMethodDef(
+        genericParams = genparams,
+        name = nm,
+        attributes = (convertMemberAccess access ||| MethodAttributes.Static),
+        implAttributes = MethodImplAttributes.Managed,
+        callingConv = ILCallingConv.Static,
+        parameters = args,
+        ret = ret,
+        securityDecls = emptyILSecurityDecls,
+        isEntryPoint = false,
+        customAttrs = emptyILCustomAttrs,
+        body = notlazy impl
+    )
 
 let mkILNonGenericStaticMethod (nm, access, args, ret, impl) =
     mkILStaticMethod (mkILEmptyGenericParams, nm, access, args, ret, impl)
 
 let mkILClassCtor impl =
-    ILMethodDef(name=".cctor",
-                attributes=(MethodAttributes.Private ||| MethodAttributes.Static ||| MethodAttributes.SpecialName ||| MethodAttributes.RTSpecialName),
-                implAttributes=MethodImplAttributes.Managed,
-                callingConv=ILCallingConv.Static,
-                genericParams=mkILEmptyGenericParams,
-                parameters = [],
-                ret=mkILVoidReturn,
-                isEntryPoint=false,
-                securityDecls=emptyILSecurityDecls,
-                customAttrs=emptyILCustomAttrs,
-                body= notlazy impl)
+    ILMethodDef(
+        name = ".cctor",
+        attributes =
+            (MethodAttributes.Private
+             ||| MethodAttributes.Static
+             ||| MethodAttributes.SpecialName
+             ||| MethodAttributes.RTSpecialName),
+        implAttributes = MethodImplAttributes.Managed,
+        callingConv = ILCallingConv.Static,
+        genericParams = mkILEmptyGenericParams,
+        parameters = [],
+        ret = mkILVoidReturn,
+        isEntryPoint = false,
+        securityDecls = emptyILSecurityDecls,
+        customAttrs = emptyILCustomAttrs,
+        body = notlazy impl
+    )
 
 // --------------------------------------------------------------------
 // Make a virtual method, where the overriding is simply the default
@@ -3099,80 +3860,104 @@ let mkILClassCtor impl =
 // --------------------------------------------------------------------
 
 let mk_ospec (ty: ILType, callconv, nm, genparams, formal_args, formal_ret) =
-  OverridesSpec (mkILMethRef (ty.TypeRef, callconv, nm, genparams, formal_args, formal_ret), ty)
+    OverridesSpec(mkILMethRef (ty.TypeRef, callconv, nm, genparams, formal_args, formal_ret), ty)
 
 let mkILGenericVirtualMethod (nm, access, genparams, actual_args, actual_ret, impl) =
-    ILMethodDef(name=nm,
-                attributes=
-                  (convertMemberAccess access |||
-                   MethodAttributes.CheckAccessOnOverride |||
-                   (match impl with MethodBody.Abstract -> MethodAttributes.Abstract ||| MethodAttributes.Virtual | _ -> MethodAttributes.Virtual)),
-                implAttributes=MethodImplAttributes.Managed,
-                genericParams=genparams,
-                callingConv=ILCallingConv.Instance,
-                parameters=actual_args,
-                ret=actual_ret,
-                isEntryPoint=false,
-                securityDecls=emptyILSecurityDecls,
-                customAttrs = emptyILCustomAttrs,
-                body= notlazy impl)
+    ILMethodDef(
+        name = nm,
+        attributes =
+            (convertMemberAccess access
+             ||| MethodAttributes.CheckAccessOnOverride
+             ||| (match impl with
+                  | MethodBody.Abstract -> MethodAttributes.Abstract ||| MethodAttributes.Virtual
+                  | _ -> MethodAttributes.Virtual)),
+        implAttributes = MethodImplAttributes.Managed,
+        genericParams = genparams,
+        callingConv = ILCallingConv.Instance,
+        parameters = actual_args,
+        ret = actual_ret,
+        isEntryPoint = false,
+        securityDecls = emptyILSecurityDecls,
+        customAttrs = emptyILCustomAttrs,
+        body = notlazy impl
+    )
 
 let mkILNonGenericVirtualMethod (nm, access, args, ret, impl) =
     mkILGenericVirtualMethod (nm, access, mkILEmptyGenericParams, args, ret, impl)
 
 let mkILGenericNonVirtualMethod (nm, access, genparams, actual_args, actual_ret, impl) =
-    ILMethodDef(name=nm,
-                attributes=(convertMemberAccess access ||| MethodAttributes.HideBySig),
-                implAttributes=MethodImplAttributes.Managed,
-                genericParams=genparams,
-                callingConv=ILCallingConv.Instance,
-                parameters=actual_args,
-                ret=actual_ret,
-                isEntryPoint=false,
-                securityDecls=emptyILSecurityDecls,
-                customAttrs = emptyILCustomAttrs,
-                body= notlazy impl)
+    ILMethodDef(
+        name = nm,
+        attributes = (convertMemberAccess access ||| MethodAttributes.HideBySig),
+        implAttributes = MethodImplAttributes.Managed,
+        genericParams = genparams,
+        callingConv = ILCallingConv.Instance,
+        parameters = actual_args,
+        ret = actual_ret,
+        isEntryPoint = false,
+        securityDecls = emptyILSecurityDecls,
+        customAttrs = emptyILCustomAttrs,
+        body = notlazy impl
+    )
 
 let mkILNonGenericInstanceMethod (nm, access, args, ret, impl) =
-  mkILGenericNonVirtualMethod (nm, access, mkILEmptyGenericParams, args, ret, impl)
-
+    mkILGenericNonVirtualMethod (nm, access, mkILEmptyGenericParams, args, ret, impl)
 
 // --------------------------------------------------------------------
 // Add some code to the end of the .cctor for a type. Create a .cctor
 // if one doesn't exist already.
 // --------------------------------------------------------------------
 
-let ilmbody_code2code f (il: ILMethodBody) =
-  {il with Code = f il.Code}
+let ilmbody_code2code f (il: ILMethodBody) = { il with Code = f il.Code }
 
 let mdef_code2code f (md: ILMethodDef) =
     let il =
         match md.Body with
-        | MethodBody.IL il-> il
+        | MethodBody.IL il -> il
         | _ -> failwith "mdef_code2code - method not IL"
+
     let ilCode = ilmbody_code2code f il.Value
-    let b = MethodBody.IL (notlazy ilCode)
+    let b = MethodBody.IL(notlazy ilCode)
     md.With(body = notlazy b)
 
 let prependInstrsToCode (instrs: ILInstr list) (c2: ILCode) =
     let instrs = Array.ofList instrs
     let n = instrs.Length
+
     match c2.Instrs[0] with
     // If there is a sequence point as the first instruction then keep it at the front
     | I_seqpoint _ as i0 ->
         let labels =
             let dict = Dictionary.newWithSize c2.Labels.Count
-            for kvp in c2.Labels do dict.Add (kvp.Key, if kvp.Value = 0 then 0 else kvp.Value + n)
+
+            for kvp in c2.Labels do
+                dict.Add(
+                    kvp.Key,
+                    if kvp.Value = 0 then
+                        0
+                    else
+                        kvp.Value + n
+                )
+
             dict
-        { c2 with Labels = labels
-                  Instrs = Array.concat [| [|i0|] ; instrs ; c2.Instrs[1..] |] }
+
+        { c2 with
+            Labels = labels
+            Instrs = Array.concat [| [| i0 |]; instrs; c2.Instrs[1..] |]
+        }
     | _ ->
         let labels =
             let dict = Dictionary.newWithSize c2.Labels.Count
-            for kvp in c2.Labels do dict.Add (kvp.Key, kvp.Value + n)
+
+            for kvp in c2.Labels do
+                dict.Add(kvp.Key, kvp.Value + n)
+
             dict
-        { c2 with Labels = labels
-                  Instrs = Array.append instrs c2.Instrs }
+
+        { c2 with
+            Labels = labels
+            Instrs = Array.append instrs c2.Instrs
+        }
 
 let prependInstrsToMethod newCode md =
     mdef_code2code (prependInstrsToCode newCode) md
@@ -3180,17 +3965,24 @@ let prependInstrsToMethod newCode md =
 // Creates cctor if needed
 let cdef_cctorCode2CodeOrCreate tag imports f (cd: ILTypeDef) =
     let mdefs = cd.Methods
+
     let cctor =
         match mdefs.FindByName ".cctor" with
-        | [mdef] -> mdef
-        | [] -> 
-            let body = mkMethodBody (false, [], 1, nonBranchingInstrsToCode [ ], tag, imports)
+        | [ mdef ] -> mdef
+        | [] ->
+            let body = mkMethodBody (false, [], 1, nonBranchingInstrsToCode [], tag, imports)
             mkILClassCtor body
         | _ -> failwith "bad method table: more than one .cctor found"
 
-    let methods = ILMethodDefs (fun () -> [| yield f cctor; for md in mdefs do if md.Name <> ".cctor" then yield md |])
-    cd.With(methods = methods)
+    let methods =
+        ILMethodDefs(fun () ->
+            [|
+                yield f cctor
+                for md in mdefs do
+                    if md.Name <> ".cctor" then yield md
+            |])
 
+    cd.With(methods = methods)
 
 let codeOfMethodDef (md: ILMethodDef) =
     match md.Code with
@@ -3200,43 +3992,64 @@ let codeOfMethodDef (md: ILMethodDef) =
 let mkRefToILMethod (tref, md: ILMethodDef) =
     mkILMethRef (tref, md.CallingConv, md.Name, md.GenericParams.Length, md.ParameterTypes, md.Return.Type)
 
-let mkRefToILField (tref, fdef: ILFieldDef) = mkILFieldRef (tref, fdef.Name, fdef.FieldType)
+let mkRefToILField (tref, fdef: ILFieldDef) =
+    mkILFieldRef (tref, fdef.Name, fdef.FieldType)
 
-let mkRefForILMethod scope (tdefs, tdef) mdef = mkRefToILMethod (mkRefForNestedILTypeDef scope (tdefs, tdef), mdef)
+let mkRefForILMethod scope (tdefs, tdef) mdef =
+    mkRefToILMethod (mkRefForNestedILTypeDef scope (tdefs, tdef), mdef)
 
-let mkRefForILField scope (tdefs, tdef) (fdef: ILFieldDef) = mkILFieldRef (mkRefForNestedILTypeDef scope (tdefs, tdef), fdef.Name, fdef.FieldType)
+let mkRefForILField scope (tdefs, tdef) (fdef: ILFieldDef) =
+    mkILFieldRef (mkRefForNestedILTypeDef scope (tdefs, tdef), fdef.Name, fdef.FieldType)
 
 // Creates cctor if needed
 let prependInstrsToClassCtor instrs tag imports cd =
     cdef_cctorCode2CodeOrCreate tag imports (prependInstrsToMethod instrs) cd
 
-let mkILField (isStatic, nm, ty, init: ILFieldInit option, at: byte [] option, access, isLiteral) =
-     ILFieldDef(name=nm,
-                fieldType=ty,
-                attributes=
-                    (convertFieldAccess access |||
-                     (if isStatic then FieldAttributes.Static else enum 0) |||
-                     (if isLiteral then FieldAttributes.Literal else enum 0) |||
-                     (if init.IsSome then FieldAttributes.HasDefault else enum 0) |||
-                     (if at.IsSome then FieldAttributes.HasFieldRVA else enum 0)),
-                literalValue = init,
-                data=at,
-                offset=None,
-                marshal=None,
-                customAttrs=emptyILCustomAttrs)
-
-let mkILInstanceField (nm, ty, init, access) = mkILField (false, nm, ty, init, None, access, false)
-
-let mkILStaticField (nm, ty, init, at, access) = mkILField (true, nm, ty, init, at, access, false)
-
-let mkILLiteralField (nm, ty, init, at, access) = mkILField (true, nm, ty, Some init, at, access, true)
+let mkILField (isStatic, nm, ty, init: ILFieldInit option, at: byte[] option, access, isLiteral) =
+    ILFieldDef(
+        name = nm,
+        fieldType = ty,
+        attributes =
+            (convertFieldAccess access
+             ||| (if isStatic then
+                      FieldAttributes.Static
+                  else
+                      enum 0)
+             ||| (if isLiteral then
+                      FieldAttributes.Literal
+                  else
+                      enum 0)
+             ||| (if init.IsSome then
+                      FieldAttributes.HasDefault
+                  else
+                      enum 0)
+             ||| (if at.IsSome then
+                      FieldAttributes.HasFieldRVA
+                  else
+                      enum 0)),
+        literalValue = init,
+        data = at,
+        offset = None,
+        marshal = None,
+        customAttrs = emptyILCustomAttrs
+    )
+
+let mkILInstanceField (nm, ty, init, access) =
+    mkILField (false, nm, ty, init, None, access, false)
+
+let mkILStaticField (nm, ty, init, at, access) =
+    mkILField (true, nm, ty, init, at, access, false)
+
+let mkILLiteralField (nm, ty, init, at, access) =
+    mkILField (true, nm, ty, Some init, at, access, true)
 
 // --------------------------------------------------------------------
 // Scopes for allocating new temporary variables.
 // --------------------------------------------------------------------
 
-type ILLocalsAllocator (preAlloc: int) =
+type ILLocalsAllocator(preAlloc: int) =
     let newLocals = ResizeArray()
+
     member tmps.AllocLocal loc =
         let locn = uint16 (preAlloc + newLocals.Count)
         newLocals.Add loc
@@ -3244,19 +4057,22 @@ type ILLocalsAllocator (preAlloc: int) =
 
     member tmps.Close() = ResizeArray.toList newLocals
 
-let mkILFieldsLazy l = ILFields (LazyOrderedMultiMap ((fun (fdef: ILFieldDef) -> fdef.Name), l))
+let mkILFieldsLazy l =
+    ILFields(LazyOrderedMultiMap((fun (fdef: ILFieldDef) -> fdef.Name), l))
 
 let mkILFields l = mkILFieldsLazy (notlazy l)
 
 let emptyILFields = mkILFields []
 
-let mkILEventsLazy l = ILEvents (LazyOrderedMultiMap ((fun (edef: ILEventDef) -> edef.Name), l))
+let mkILEventsLazy l =
+    ILEvents(LazyOrderedMultiMap((fun (edef: ILEventDef) -> edef.Name), l))
 
 let mkILEvents l = mkILEventsLazy (notlazy l)
 
 let emptyILEvents = mkILEvents []
 
-let mkILPropertiesLazy l = ILProperties (LazyOrderedMultiMap ((fun (pdef: ILPropertyDef) -> pdef.Name), l) )
+let mkILPropertiesLazy l =
+    ILProperties(LazyOrderedMultiMap((fun (pdef: ILPropertyDef) -> pdef.Name), l))
 
 let mkILProperties l = mkILPropertiesLazy (notlazy l)
 
@@ -3264,26 +4080,29 @@ let emptyILProperties = mkILProperties []
 
 let addExportedTypeToTable (y: ILExportedTypeOrForwarder) tab = Map.add y.Name y tab
 
-let mkILExportedTypes l = ILExportedTypesAndForwarders (notlazy (List.foldBack addExportedTypeToTable l Map.empty))
+let mkILExportedTypes l =
+    ILExportedTypesAndForwarders(notlazy (List.foldBack addExportedTypeToTable l Map.empty))
 
-let mkILExportedTypesLazy (l: Lazy<_>) = ILExportedTypesAndForwarders (lazy (List.foldBack addExportedTypeToTable (l.Force()) Map.empty))
+let mkILExportedTypesLazy (l: Lazy<_>) =
+    ILExportedTypesAndForwarders(lazy (List.foldBack addExportedTypeToTable (l.Force()) Map.empty))
 
-let addNestedExportedTypeToTable (y: ILNestedExportedType) tab =
-    Map.add y.Name y tab
+let addNestedExportedTypeToTable (y: ILNestedExportedType) tab = Map.add y.Name y tab
 
 let mkTypeForwarder scopeRef name nested customAttrs access =
-    { ScopeRef=scopeRef
-      Name=name
-      Attributes=enum(0x00200000) ||| convertTypeAccessFlags access
-      Nested=nested
-      CustomAttrsStored=storeILCustomAttrs customAttrs
-      MetadataIndex = NoMetadataIdx }
+    {
+        ScopeRef = scopeRef
+        Name = name
+        Attributes = enum (0x00200000) ||| convertTypeAccessFlags access
+        Nested = nested
+        CustomAttrsStored = storeILCustomAttrs customAttrs
+        MetadataIndex = NoMetadataIdx
+    }
 
 let mkILNestedExportedTypes l =
-    ILNestedExportedTypes (notlazy (List.foldBack addNestedExportedTypeToTable l Map.empty))
+    ILNestedExportedTypes(notlazy (List.foldBack addNestedExportedTypeToTable l Map.empty))
 
 let mkILNestedExportedTypesLazy (l: Lazy<_>) =
-    ILNestedExportedTypes (lazy (List.foldBack addNestedExportedTypeToTable (l.Force()) Map.empty))
+    ILNestedExportedTypes(lazy (List.foldBack addNestedExportedTypeToTable (l.Force()) Map.empty))
 
 let mkILResources l = ILResources l
 let emptyILResources = ILResources []
@@ -3293,9 +4112,11 @@ let addMethodImplToTable y tab =
     let prev = Map.tryFindMulti key tab
     Map.add key (y :: prev) tab
 
-let mkILMethodImpls l = ILMethodImpls (notlazy (List.foldBack addMethodImplToTable l Map.empty))
+let mkILMethodImpls l =
+    ILMethodImpls(notlazy (List.foldBack addMethodImplToTable l Map.empty))
 
-let mkILMethodImplsLazy l = ILMethodImpls (lazy (List.foldBack addMethodImplToTable (Lazy.force l) Map.empty))
+let mkILMethodImplsLazy l =
+    ILMethodImpls(lazy (List.foldBack addMethodImplToTable (Lazy.force l) Map.empty))
 
 let emptyILMethodImpls = mkILMethodImpls []
 
@@ -3303,27 +4124,26 @@ let emptyILMethodImpls = mkILMethodImpls []
 /// them in fields. preblock is how to call the superclass constructor....
 let mkILStorageCtorWithParamNames (preblock: ILInstr list, ty, extraParams, flds, access, tag, imports) =
     let code =
-        [ match tag with 
-          | Some x -> I_seqpoint x
-          | None -> ()
-          yield! preblock 
-          for (n, (_pnm, nm, fieldTy)) in List.indexed flds do
-              mkLdarg0
-              mkLdarg (uint16 (n+1))
-              mkNormalStfld (mkILFieldSpecInTy (ty, nm, fieldTy)) 
+        [
+            match tag with
+            | Some x -> I_seqpoint x
+            | None -> ()
+            yield! preblock
+            for (n, (_pnm, nm, fieldTy)) in List.indexed flds do
+                mkLdarg0
+                mkLdarg (uint16 (n + 1))
+                mkNormalStfld (mkILFieldSpecInTy (ty, nm, fieldTy))
         ]
+
     let body = mkMethodBody (false, [], 2, nonBranchingInstrsToCode code, tag, imports)
-    mkILCtor(access,
-            (flds |> List.map (fun (pnm, _, ty) -> mkILParamNamed (pnm, ty))) @ extraParams, body
-            )
+    mkILCtor (access, (flds |> List.map (fun (pnm, _, ty) -> mkILParamNamed (pnm, ty))) @ extraParams, body)
 
 let mkILSimpleStorageCtorWithParamNames (baseTySpec, ty, extraParams, flds, access, tag, imports) =
     let preblock =
-      match baseTySpec with
-      | None -> []
-      | Some tspec ->
-          [ mkLdarg0
-            mkNormalCall (mkILCtorMethSpecForTy (mkILBoxedType tspec, [])) ]
+        match baseTySpec with
+        | None -> []
+        | Some tspec -> [ mkLdarg0; mkNormalCall (mkILCtorMethSpecForTy (mkILBoxedType tspec, [])) ]
+
     mkILStorageCtorWithParamNames (preblock, ty, extraParams, flds, access, tag, imports)
 
 let addParamNames flds =
@@ -3337,101 +4157,144 @@ let mkILStorageCtor (preblock, ty, flds, access, tag, imports) =
 
 let mkILGenericClass (nm, access, genparams, extends, impl, methods, fields, nestedTypes, props, events, attrs, init) =
     let attributes =
-        convertTypeAccessFlags access |||
-        TypeAttributes.AutoLayout |||
-        TypeAttributes.Class |||
-        (match init with 
-         | ILTypeInit.BeforeField -> TypeAttributes.BeforeFieldInit
-         | _ -> enum 0) 
+        convertTypeAccessFlags access
+        ||| TypeAttributes.AutoLayout
+        ||| TypeAttributes.Class
+        ||| (match init with
+             | ILTypeInit.BeforeField -> TypeAttributes.BeforeFieldInit
+             | _ -> enum 0)
         ||| TypeAttributes.AnsiClass
 
-    ILTypeDef(name=nm,
-        attributes=attributes,
-        genericParams= genparams,
+    ILTypeDef(
+        name = nm,
+        attributes = attributes,
+        genericParams = genparams,
         implements = impl,
-        layout=ILTypeDefLayout.Auto,
+        layout = ILTypeDefLayout.Auto,
         extends = Some extends,
-        methods= methods,
-        fields= fields,
-        nestedTypes=nestedTypes,
-        customAttrs=attrs,
-        methodImpls=emptyILMethodImpls,
-        properties=props,
-        events=events,
-        isKnownToBeAttribute=false,
-        securityDecls=emptyILSecurityDecls)
+        methods = methods,
+        fields = fields,
+        nestedTypes = nestedTypes,
+        customAttrs = attrs,
+        methodImpls = emptyILMethodImpls,
+        properties = props,
+        events = events,
+        isKnownToBeAttribute = false,
+        securityDecls = emptyILSecurityDecls
+    )
 
 let mkRawDataValueTypeDef (iltyp_ValueType: ILType) (nm, size, pack) =
-    ILTypeDef(name = nm,
-              genericParams= [],
-              attributes = (TypeAttributes.NotPublic ||| TypeAttributes.Sealed ||| TypeAttributes.ExplicitLayout |||
-                            TypeAttributes.BeforeFieldInit ||| TypeAttributes.AnsiClass),
-              implements = [],
-              extends = Some iltyp_ValueType,
-              layout=ILTypeDefLayout.Explicit { Size=Some size; Pack=Some pack },
-              methods= emptyILMethods,
-              fields= emptyILFields,
-              nestedTypes=emptyILTypeDefs,
-              customAttrs=emptyILCustomAttrs,
-              methodImpls=emptyILMethodImpls,
-              properties=emptyILProperties,
-              events=emptyILEvents,
-              isKnownToBeAttribute=false,
-              securityDecls=emptyILSecurityDecls)
-
+    ILTypeDef(
+        name = nm,
+        genericParams = [],
+        attributes =
+            (TypeAttributes.NotPublic
+             ||| TypeAttributes.Sealed
+             ||| TypeAttributes.ExplicitLayout
+             ||| TypeAttributes.BeforeFieldInit
+             ||| TypeAttributes.AnsiClass),
+        implements = [],
+        extends = Some iltyp_ValueType,
+        layout = ILTypeDefLayout.Explicit { Size = Some size; Pack = Some pack },
+        methods = emptyILMethods,
+        fields = emptyILFields,
+        nestedTypes = emptyILTypeDefs,
+        customAttrs = emptyILCustomAttrs,
+        methodImpls = emptyILMethodImpls,
+        properties = emptyILProperties,
+        events = emptyILEvents,
+        isKnownToBeAttribute = false,
+        securityDecls = emptyILSecurityDecls
+    )
 
 let mkILSimpleClass (ilg: ILGlobals) (nm, access, methods, fields, nestedTypes, props, events, attrs, init) =
-  mkILGenericClass (nm, access, mkILEmptyGenericParams, ilg.typ_Object, [], methods, fields, nestedTypes, props, events, attrs, init)
+    mkILGenericClass (nm, access, mkILEmptyGenericParams, ilg.typ_Object, [], methods, fields, nestedTypes, props, events, attrs, init)
 
 let mkILTypeDefForGlobalFunctions ilg (methods, fields) =
-    mkILSimpleClass ilg (typeNameForGlobalFunctions, ILTypeDefAccess.Public, methods, fields, emptyILTypeDefs, emptyILProperties, emptyILEvents, emptyILCustomAttrs, ILTypeInit.BeforeField)
+    mkILSimpleClass
+        ilg
+        (typeNameForGlobalFunctions,
+         ILTypeDefAccess.Public,
+         methods,
+         fields,
+         emptyILTypeDefs,
+         emptyILProperties,
+         emptyILEvents,
+         emptyILCustomAttrs,
+         ILTypeInit.BeforeField)
 
 let destTypeDefsWithGlobalFunctionsFirst ilg (tdefs: ILTypeDefs) =
-  let l = tdefs.AsList()
-  let top, nontop = l |> List.partition (fun td -> td.Name = typeNameForGlobalFunctions)
-  let top2 = if isNil top then [ mkILTypeDefForGlobalFunctions ilg (emptyILMethods, emptyILFields) ] else top
-  top2@nontop
+    let l = tdefs.AsList()
+
+    let top, nontop =
+        l |> List.partition (fun td -> td.Name = typeNameForGlobalFunctions)
 
-let mkILSimpleModule assemblyName moduleName dll subsystemVersion useHighEntropyVA tdefs hashalg locale flags exportedTypes metadataVersion =
+    let top2 =
+        if isNil top then
+            [ mkILTypeDefForGlobalFunctions ilg (emptyILMethods, emptyILFields) ]
+        else
+            top
+
+    top2 @ nontop
+
+let mkILSimpleModule
+    assemblyName
+    moduleName
+    dll
+    subsystemVersion
+    useHighEntropyVA
+    tdefs
+    hashalg
+    locale
+    flags
+    exportedTypes
+    metadataVersion
+    =
     let manifest =
-        { Name=assemblyName
-          AuxModuleHashAlgorithm= match hashalg with | Some alg -> alg | _ -> 0x8004 // SHA1
-          SecurityDeclsStored=emptyILSecurityDeclsStored
-          PublicKey= None
-          Version= None
-          Locale=locale
-          CustomAttrsStored=storeILCustomAttrs emptyILCustomAttrs
-          AssemblyLongevity=ILAssemblyLongevity.Unspecified
-          DisableJitOptimizations = 0 <> (flags &&& 0x4000)
-          JitTracking = (0 <> (flags &&& 0x8000)) // always turn these on
-          IgnoreSymbolStoreSequencePoints = (0 <> (flags &&& 0x2000))
-          Retargetable = (0 <> (flags &&& 0x100))
-          ExportedTypes=exportedTypes
-          EntrypointElsewhere=None
-          MetadataIndex = NoMetadataIdx }
-    { Manifest= Some manifest
-      CustomAttrsStored=storeILCustomAttrs emptyILCustomAttrs
-      Name=moduleName
-      NativeResources=[]
-      TypeDefs=tdefs
-      SubsystemVersion = subsystemVersion
-      UseHighEntropyVA = useHighEntropyVA
-      SubSystemFlags=defaultSubSystem
-      IsDLL=dll
-      IsILOnly=true
-      Platform=None
-      StackReserveSize=None
-      Is32Bit=false
-      Is32BitPreferred=false
-      Is64Bit=false
-      PhysicalAlignment=defaultPhysAlignment
-      VirtualAlignment=defaultVirtAlignment
-      ImageBase=defaultImageBase
-      MetadataVersion=metadataVersion
-      Resources=mkILResources []
-      MetadataIndex = NoMetadataIdx
-    }
+        {
+            Name = assemblyName
+            AuxModuleHashAlgorithm =
+                match hashalg with
+                | Some alg -> alg
+                | _ -> 0x8004 // SHA1
+            SecurityDeclsStored = emptyILSecurityDeclsStored
+            PublicKey = None
+            Version = None
+            Locale = locale
+            CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+            AssemblyLongevity = ILAssemblyLongevity.Unspecified
+            DisableJitOptimizations = 0 <> (flags &&& 0x4000)
+            JitTracking = (0 <> (flags &&& 0x8000)) // always turn these on
+            IgnoreSymbolStoreSequencePoints = (0 <> (flags &&& 0x2000))
+            Retargetable = (0 <> (flags &&& 0x100))
+            ExportedTypes = exportedTypes
+            EntrypointElsewhere = None
+            MetadataIndex = NoMetadataIdx
+        }
 
+    {
+        Manifest = Some manifest
+        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+        Name = moduleName
+        NativeResources = []
+        TypeDefs = tdefs
+        SubsystemVersion = subsystemVersion
+        UseHighEntropyVA = useHighEntropyVA
+        SubSystemFlags = defaultSubSystem
+        IsDLL = dll
+        IsILOnly = true
+        Platform = None
+        StackReserveSize = None
+        Is32Bit = false
+        Is32BitPreferred = false
+        Is64Bit = false
+        PhysicalAlignment = defaultPhysAlignment
+        VirtualAlignment = defaultVirtAlignment
+        ImageBase = defaultImageBase
+        MetadataVersion = metadataVersion
+        Resources = mkILResources []
+        MetadataIndex = NoMetadataIdx
+    }
 
 //-----------------------------------------------------------------------
 // [instructions_to_code] makes the basic block structure of code from
@@ -3441,16 +4304,16 @@ let mkILSimpleModule assemblyName moduleName dll subsystemVersion useHighEntropy
 // [bbstartToCodeLabelMap].
 //-----------------------------------------------------------------------
 
-
 // REVIEW: this function shows up on performance traces. If we eliminated the last ILX->IL rewrites from the
 // F# compiler we could get rid of this structured code representation from Abstract IL altogether and
 // never convert F# code into this form.
 let buildILCode (_methName: string) lab2pc instrs tryspecs localspecs : ILCode =
-    { Labels = lab2pc
-      Instrs = instrs
-      Exceptions = tryspecs
-      Locals = localspecs }
-
+    {
+        Labels = lab2pc
+        Instrs = instrs
+        Exceptions = tryspecs
+        Locals = localspecs
+    }
 
 // --------------------------------------------------------------------
 // Detecting Delegates
@@ -3458,45 +4321,95 @@ let buildILCode (_methName: string) lab2pc instrs tryspecs localspecs : ILCode =
 
 let mkILDelegateMethods access (ilg: ILGlobals) (iltyp_AsyncCallback, iltyp_IAsyncResult) (parms, rtv: ILReturn) =
     let retTy = rtv.Type
+
     let one nm args ret =
-        let mdef = mkILNonGenericVirtualMethod (nm, access, args, mkILReturn ret, MethodBody.Abstract)
+        let mdef =
+            mkILNonGenericVirtualMethod (nm, access, args, mkILReturn ret, MethodBody.Abstract)
+
         mdef.WithAbstract(false).WithHideBySig(true).WithRuntime(true)
-    let ctor = mkILCtor (access, [ mkILParamNamed("object", ilg.typ_Object); mkILParamNamed("method", ilg.typ_IntPtr) ], MethodBody.Abstract)
+
+    let ctor =
+        mkILCtor (
+            access,
+            [
+                mkILParamNamed ("object", ilg.typ_Object)
+                mkILParamNamed ("method", ilg.typ_IntPtr)
+            ],
+            MethodBody.Abstract
+        )
+
     let ctor = ctor.WithRuntime(true).WithHideBySig(true)
-    [ ctor
-      one "Invoke" parms retTy
-      one "BeginInvoke" (parms @ [mkILParamNamed ("callback", iltyp_AsyncCallback); mkILParamNamed ("objects", ilg.typ_Object) ] ) iltyp_IAsyncResult
-      one "EndInvoke" [mkILParamNamed ("result", iltyp_IAsyncResult)] retTy ]
 
+    [
+        ctor
+        one "Invoke" parms retTy
+        one
+            "BeginInvoke"
+            (parms
+             @ [
+                 mkILParamNamed ("callback", iltyp_AsyncCallback)
+                 mkILParamNamed ("objects", ilg.typ_Object)
+             ])
+            iltyp_IAsyncResult
+        one "EndInvoke" [ mkILParamNamed ("result", iltyp_IAsyncResult) ] retTy
+    ]
 
 let mkCtorMethSpecForDelegate (ilg: ILGlobals) (ty: ILType, useUIntPtr) =
     let scoref = ty.TypeRef.Scope
+
     let argTys =
-        [ rescopeILType scoref ilg.typ_Object
-          rescopeILType scoref (if useUIntPtr then ilg.typ_UIntPtr else ilg.typ_IntPtr) ]
+        [
+            rescopeILType scoref ilg.typ_Object
+            rescopeILType
+                scoref
+                (if useUIntPtr then
+                     ilg.typ_UIntPtr
+                 else
+                     ilg.typ_IntPtr)
+        ]
+
     mkILInstanceMethSpecInTy (ty, ".ctor", argTys, ILType.Void, emptyILGenericArgsList)
 
 type ILEnumInfo =
-    { enumValues: (string * ILFieldInit) list
-      enumType: ILType }
+    {
+        enumValues: (string * ILFieldInit) list
+        enumType: ILType
+    }
 
 let getTyOfILEnumInfo info = info.enumType
 
 let computeILEnumInfo (mdName, mdFields: ILFieldDefs) =
     match (List.partition (fun (fd: ILFieldDef) -> fd.IsStatic) (mdFields.AsList())) with
-    | staticFields, [vfd] ->
-        { enumType = vfd.FieldType
-          enumValues = staticFields |> List.map (fun fd -> (fd.Name, match fd.LiteralValue with Some i -> i | None -> failwith ("computeILEnumInfo: badly formed enum "+mdName+": static field does not have an default value"))) }
-    | _, [] -> failwith ("computeILEnumInfo: badly formed enum "+mdName+": no non-static field found")
-    | _, _ -> failwith ("computeILEnumInfo: badly formed enum "+mdName+": more than one non-static field found")
+    | staticFields, [ vfd ] ->
+        {
+            enumType = vfd.FieldType
+            enumValues =
+                staticFields
+                |> List.map (fun fd ->
+                    (fd.Name,
+                     match fd.LiteralValue with
+                     | Some i -> i
+                     | None ->
+                         failwith (
+                             "computeILEnumInfo: badly formed enum "
+                             + mdName
+                             + ": static field does not have an default value"
+                         )))
+        }
+    | _, [] -> failwith ("computeILEnumInfo: badly formed enum " + mdName + ": no non-static field found")
+    | _, _ ->
+        failwith (
+            "computeILEnumInfo: badly formed enum "
+            + mdName
+            + ": more than one non-static field found"
+        )
 
 //---------------------------------------------------------------------
 // Primitives to help read signatures. These do not use the file cursor, but
 // pass around an int index
 //---------------------------------------------------------------------
 
-let sigptr_get_byte bytes sigptr =
-    Bytes.get bytes sigptr, sigptr + 1
+let sigptr_get_byte bytes sigptr = Bytes.get bytes sigptr, sigptr + 1
 
 let sigptr_get_bool bytes sigptr =
     let b0, sigptr = sigptr_get_byte bytes sigptr
@@ -3539,15 +4452,23 @@ let sigptr_get_i64 bytes sigptr =
     let b5, sigptr = sigptr_get_byte bytes sigptr
     let b6, sigptr = sigptr_get_byte bytes sigptr
     let b7, sigptr = sigptr_get_byte bytes sigptr
-    int64 b0 ||| (int64 b1 <<< 8) ||| (int64 b2 <<< 16) ||| (int64 b3 <<< 24) |||
-    (int64 b4 <<< 32) ||| (int64 b5 <<< 40) ||| (int64 b6 <<< 48) ||| (int64 b7 <<< 56),
+
+    int64 b0
+    ||| (int64 b1 <<< 8)
+    ||| (int64 b2 <<< 16)
+    ||| (int64 b3 <<< 24)
+    ||| (int64 b4 <<< 32)
+    ||| (int64 b5 <<< 40)
+    ||| (int64 b6 <<< 48)
+    ||| (int64 b7 <<< 56),
     sigptr
 
 let sigptr_get_u64 bytes sigptr =
     let u, sigptr = sigptr_get_i64 bytes sigptr
     uint64 u, sigptr
 
-let float32OfBits (x: int32) = BitConverter.ToSingle (BitConverter.GetBytes x, 0)
+let float32OfBits (x: int32) =
+    BitConverter.ToSingle(BitConverter.GetBytes x, 0)
 
 let floatOfBits (x: int64) = BitConverter.Int64BitsToDouble x
 
@@ -3561,17 +4482,21 @@ let sigptr_get_ieee64 bytes sigptr =
 
 let sigptr_get_intarray n (bytes: byte[]) sigptr =
     let res = Bytes.zeroCreate n
+
     for i = 0 to n - 1 do
         res[i] <- bytes[sigptr + i]
+
     res, sigptr + n
 
 let sigptr_get_string n bytes sigptr =
     let intarray, sigptr = sigptr_get_intarray n bytes sigptr
-    Encoding.UTF8.GetString (intarray, 0, intarray.Length), sigptr
+    Encoding.UTF8.GetString(intarray, 0, intarray.Length), sigptr
 
 let sigptr_get_z_i32 bytes sigptr =
     let b0, sigptr = sigptr_get_byte bytes sigptr
-    if b0 <= 0x7F then b0, sigptr
+
+    if b0 <= 0x7F then
+        b0, sigptr
     elif b0 <= 0xbf then
         let b0 = b0 &&& 0x7f
         let b1, sigptr = sigptr_get_byte bytes sigptr
@@ -3589,9 +4514,10 @@ let sigptr_get_serstring bytes sigptr =
 
 let sigptr_get_serstring_possibly_null bytes sigptr =
     let b0, new_sigptr = sigptr_get_byte bytes sigptr
+
     if b0 = 0xFF then // null case
         None, new_sigptr
-    else  // throw away new_sigptr, getting length & text advance
+    else // throw away new_sigptr, getting length & text advance
         let len, sigptr = sigptr_get_z_i32 bytes sigptr
         let s, sigptr = sigptr_get_string len bytes sigptr
         Some s, sigptr
@@ -3601,7 +4527,16 @@ let sigptr_get_serstring_possibly_null bytes sigptr =
 //---------------------------------------------------------------------
 
 let mkRefToILAssembly (m: ILAssemblyManifest) =
-    ILAssemblyRef.Create (m.Name, None, (match m.PublicKey with Some k -> Some (PublicKey.KeyAsToken k) | None -> None), m.Retargetable, m.Version, m.Locale)
+    ILAssemblyRef.Create(
+        m.Name,
+        None,
+        (match m.PublicKey with
+         | Some k -> Some(PublicKey.KeyAsToken k)
+         | None -> None),
+        m.Retargetable,
+        m.Version,
+        m.Locale
+    )
 
 let z_unsigned_int_size n =
     if n <= 0x7F then 1
@@ -3609,12 +4544,17 @@ let z_unsigned_int_size n =
     else 3
 
 let z_unsigned_int n =
-    if n >= 0 && n <= 0x7F then [| byte n |]
-    elif n >= 0x80 && n <= 0x3FFF then [| byte (0x80 ||| (n >>>& 8)); byte (n &&& 0xFF) |]
-    else [| byte (0xc0 ||| (n >>>& 24))
+    if n >= 0 && n <= 0x7F then
+        [| byte n |]
+    elif n >= 0x80 && n <= 0x3FFF then
+        [| byte (0x80 ||| (n >>>& 8)); byte (n &&& 0xFF) |]
+    else
+        [|
+            byte (0xc0 ||| (n >>>& 24))
             byte ((n >>>& 16) &&& 0xFF)
             byte ((n >>>& 8) &&& 0xFF)
-            byte (n &&& 0xFF) |]
+            byte (n &&& 0xFF)
+        |]
 
 let string_as_utf8_bytes (s: string) = Encoding.UTF8.GetBytes s
 
@@ -3637,11 +4577,14 @@ let dw0 n = byte (n &&& 0xFFL)
 
 let u8AsBytes (i: byte) = [| i |]
 
-let u16AsBytes x = let n = (int x) in [| byte (b0 n); byte (b1 n) |]
+let u16AsBytes x =
+    let n = (int x) in [| byte (b0 n); byte (b1 n) |]
 
-let i32AsBytes i = [| byte (b0 i); byte (b1 i); byte (b2 i); byte (b3 i) |]
+let i32AsBytes i =
+    [| byte (b0 i); byte (b1 i); byte (b2 i); byte (b3 i) |]
 
-let i64AsBytes i = [| dw0 i; dw1 i; dw2 i; dw3 i; dw4 i; dw5 i; dw6 i; dw7 i |]
+let i64AsBytes i =
+    [| dw0 i; dw1 i; dw2 i; dw3 i; dw4 i; dw5 i; dw6 i; dw7 i |]
 
 let i8AsBytes (i: sbyte) = u8AsBytes (byte i)
 
@@ -3651,7 +4594,8 @@ let u32AsBytes (i: uint32) = i32AsBytes (int32 i)
 
 let u64AsBytes (i: uint64) = i64AsBytes (int64 i)
 
-let bitsOfSingle (x: float32) = BitConverter.ToInt32 (BitConverter.GetBytes x, 0)
+let bitsOfSingle (x: float32) =
+    BitConverter.ToInt32(BitConverter.GetBytes x, 0)
 
 let bitsOfDouble (x: float) = BitConverter.DoubleToInt64Bits x
 
@@ -3691,7 +4635,8 @@ let et_MVAR = 0x1Euy
 let et_CMOD_REQD = 0x1Fuy
 let et_CMOD_OPT = 0x20uy
 
-let formatILVersion (version: ILVersionInfo) = sprintf "%d.%d.%d.%d" (int version.Major) (int version.Minor) (int version.Build) (int version.Revision)
+let formatILVersion (version: ILVersionInfo) =
+    sprintf "%d.%d.%d.%d" (int version.Major) (int version.Minor) (int version.Build) (int version.Revision)
 
 let encodeCustomAttrString s =
     let arr = string_as_utf8_bytes s
@@ -3716,7 +4661,7 @@ let rec encodeCustomAttrElemType x =
     | ILType.Boxed tspec when tspec.Name = tname_Type -> [| 0x50uy |]
     | ILType.Value tspec -> Array.append [| 0x55uy |] (encodeCustomAttrString tspec.TypeRef.QualifiedName)
     | ILType.Array (shape, elemType) when shape = ILArrayShape.SingleDimensional ->
-          Array.append [| et_SZARRAY |] (encodeCustomAttrElemType elemType)
+        Array.append [| et_SZARRAY |] (encodeCustomAttrElemType elemType)
     | _ -> failwith "encodeCustomAttrElemType: unrecognized custom element type"
 
 /// Given a custom attribute element, work out the type of the .NET argument for that element.
@@ -3735,63 +4680,85 @@ let rec encodeCustomAttrElemTypeForObject x =
     | ILAttribElem.UInt64 _ -> [| et_U8 |]
     | ILAttribElem.Type _ -> [| 0x50uy |]
     | ILAttribElem.TypeRef _ -> [| 0x50uy |]
-    | ILAttribElem.Null _ -> [| et_STRING |]// yes, the 0xe prefix is used when passing a "null" to a property or argument of type "object" here
+    | ILAttribElem.Null _ -> [| et_STRING |] // yes, the 0xe prefix is used when passing a "null" to a property or argument of type "object" here
     | ILAttribElem.Single _ -> [| et_R4 |]
     | ILAttribElem.Double _ -> [| et_R8 |]
     | ILAttribElem.Array (elemTy, _) -> [| yield et_SZARRAY; yield! encodeCustomAttrElemType elemTy |]
 
-let tspan = TimeSpan (DateTime.UtcNow.Ticks - DateTime(2000, 1, 1).Ticks)
+let tspan = TimeSpan(DateTime.UtcNow.Ticks - DateTime(2000, 1, 1).Ticks)
 
-let parseILVersion (vstr : string) =
+let parseILVersion (vstr: string) =
     // matches "v1.2.3.4" or "1.2.3.4". Note, if numbers are missing, returns -1 (not 0).
-    let mutable vstr = vstr.TrimStart [|'v'|]
+    let mutable vstr = vstr.TrimStart [| 'v' |]
     // if the version string contains wildcards, replace them
-    let versionComponents = vstr.Split [|'.'|]
+    let versionComponents = vstr.Split [| '.' |]
 
     // account for wildcards
     if versionComponents.Length > 2 then
-      let defaultBuild = uint16 tspan.Days % UInt16.MaxValue - 1us
-      let defaultRevision = uint16 (DateTime.UtcNow.TimeOfDay.TotalSeconds / 2.0) % UInt16.MaxValue - 1us
-      if versionComponents[2] = "*" then
-        if versionComponents.Length > 3 then
-          failwith "Invalid version format"
-        else
-          // set the build number to the number of days since Jan 1, 2000
-          versionComponents[2] <- defaultBuild.ToString()
-          // Set the revision number to number of seconds today / 2
-          vstr <- String.Join (".", versionComponents) + "." + defaultRevision.ToString()
-      elif versionComponents.Length > 3 && versionComponents[3] = "*" then
-        // Set the revision number to number of seconds today / 2
-        versionComponents[3] <- defaultRevision.ToString()
-        vstr <- String.Join (".", versionComponents)
+        let defaultBuild = uint16 tspan.Days % UInt16.MaxValue - 1us
+
+        let defaultRevision =
+            uint16 (DateTime.UtcNow.TimeOfDay.TotalSeconds / 2.0) % UInt16.MaxValue - 1us
+
+        if versionComponents[2] = "*" then
+            if versionComponents.Length > 3 then
+                failwith "Invalid version format"
+            else
+                // set the build number to the number of days since Jan 1, 2000
+                versionComponents[2] <- defaultBuild.ToString()
+                // Set the revision number to number of seconds today / 2
+                vstr <- String.Join(".", versionComponents) + "." + defaultRevision.ToString()
+        elif versionComponents.Length > 3 && versionComponents[3] = "*" then
+            // Set the revision number to number of seconds today / 2
+            versionComponents[3] <- defaultRevision.ToString()
+            vstr <- String.Join(".", versionComponents)
 
     let version = Version vstr
     let zero32 n = if n < 0 then 0us else uint16 n
     // since the minor revision will be -1 if none is specified, we need to truncate to 0 to not break existing code
-    let minorRevision = if version.Revision = -1 then 0us else uint16 version.MinorRevision
-    ILVersionInfo (zero32 version.Major, zero32 version.Minor, zero32 version.Build, minorRevision)
+    let minorRevision =
+        if version.Revision = -1 then
+            0us
+        else
+            uint16 version.MinorRevision
 
-let compareILVersions (version1 : ILVersionInfo) (version2 : ILVersionInfo) =
+    ILVersionInfo(zero32 version.Major, zero32 version.Minor, zero32 version.Build, minorRevision)
+
+let compareILVersions (version1: ILVersionInfo) (version2: ILVersionInfo) =
     let c = compare version1.Major version2.Major
-    if c <> 0 then c else
-    let c = compare version1.Minor version2.Minor
-    if c <> 0 then c else
-    let c = compare version1.Build version2.Build
-    if c <> 0 then c else
-    let c = compare version1.Revision version2.Revision
-    if c <> 0 then c else
-    0
+
+    if c <> 0 then
+        c
+    else
+        let c = compare version1.Minor version2.Minor
+
+        if c <> 0 then
+            c
+        else
+            let c = compare version1.Build version2.Build
+
+            if c <> 0 then
+                c
+            else
+                let c = compare version1.Revision version2.Revision
+                if c <> 0 then c else 0
 
 let DummyFSharpCoreScopeRef =
     let asmRef =
         // The exact public key token and version used here don't actually matter, or shouldn't.
-        ILAssemblyRef.Create("FSharp.Core", None,
-                Some (PublicKeyToken(Bytes.ofInt32Array [| 0xb0; 0x3f; 0x5f; 0x7f; 0x11; 0xd5; 0x0a; 0x3a |])),
-                false,
-                Some (parseILVersion "0.0.0.0"), None)
+        ILAssemblyRef.Create(
+            "FSharp.Core",
+            None,
+            Some(PublicKeyToken(Bytes.ofInt32Array [| 0xb0; 0x3f; 0x5f; 0x7f; 0x11; 0xd5; 0x0a; 0x3a |])),
+            false,
+            Some(parseILVersion "0.0.0.0"),
+            None
+        )
+
     ILScopeRef.Assembly asmRef
 
-let PrimaryAssemblyILGlobals = mkILGlobals (ILScopeRef.PrimaryAssembly, [], DummyFSharpCoreScopeRef)
+let PrimaryAssemblyILGlobals =
+    mkILGlobals (ILScopeRef.PrimaryAssembly, [], DummyFSharpCoreScopeRef)
 
 let rec decodeCustomAttrElemType bytes sigptr x =
     match x with
@@ -3810,13 +4777,12 @@ let rec decodeCustomAttrElemType bytes sigptr x =
     | x when x = et_STRING -> PrimaryAssemblyILGlobals.typ_String, sigptr
     | x when x = et_OBJECT -> PrimaryAssemblyILGlobals.typ_Object, sigptr
     | x when x = et_SZARRAY ->
-         let et, sigptr = sigptr_get_u8 bytes sigptr
-         let elemTy, sigptr = decodeCustomAttrElemType bytes sigptr et
-         mkILArr1DTy elemTy, sigptr
+        let et, sigptr = sigptr_get_u8 bytes sigptr
+        let elemTy, sigptr = decodeCustomAttrElemType bytes sigptr et
+        mkILArr1DTy elemTy, sigptr
     | x when x = 0x50uy -> PrimaryAssemblyILGlobals.typ_Type, sigptr
     | _ -> failwithf "decodeCustomAttrElemType ilg: unrecognized custom element type: %A" x
 
-
 /// Given a custom attribute element, encode it to a binary representation according to the rules in Ecma 335 Partition II.
 let rec encodeCustomAttrPrimValue c =
     match c with
@@ -3840,37 +4806,51 @@ let rec encodeCustomAttrPrimValue c =
     | ILAttribElem.Type (Some ty) -> encodeCustomAttrString ty.QualifiedName
     | ILAttribElem.TypeRef (Some tref) -> encodeCustomAttrString tref.QualifiedName
     | ILAttribElem.Array (_, elems) ->
-         [| yield! i32AsBytes elems.Length; for elem in elems do yield! encodeCustomAttrPrimValue elem |]
+        [|
+            yield! i32AsBytes elems.Length
+            for elem in elems do
+                yield! encodeCustomAttrPrimValue elem
+        |]
 
 and encodeCustomAttrValue ty c =
     match ty, c with
     | ILType.Boxed tspec, _ when tspec.Name = tname_Object ->
-       [| yield! encodeCustomAttrElemTypeForObject c; yield! encodeCustomAttrPrimValue c |]
-    | ILType.Array (shape, _), ILAttribElem.Null when shape = ILArrayShape.SingleDimensional ->
-       [| yield! i32AsBytes 0xFFFFFFFF |]
+        [|
+            yield! encodeCustomAttrElemTypeForObject c
+            yield! encodeCustomAttrPrimValue c
+        |]
+    | ILType.Array (shape, _), ILAttribElem.Null when shape = ILArrayShape.SingleDimensional -> [| yield! i32AsBytes 0xFFFFFFFF |]
     | ILType.Array (shape, elemType), ILAttribElem.Array (_, elems) when shape = ILArrayShape.SingleDimensional ->
-       [| yield! i32AsBytes elems.Length; for elem in elems do yield! encodeCustomAttrValue elemType elem |]
-    | _ ->
-       encodeCustomAttrPrimValue c
+        [|
+            yield! i32AsBytes elems.Length
+            for elem in elems do
+                yield! encodeCustomAttrValue elemType elem
+        |]
+    | _ -> encodeCustomAttrPrimValue c
 
 let encodeCustomAttrNamedArg (nm, ty, prop, elem) =
-   [| yield (if prop then 0x54uy else 0x53uy)
-      yield! encodeCustomAttrElemType ty
-      yield! encodeCustomAttrString nm
-      yield! encodeCustomAttrValue ty elem |]
+    [|
+        yield (if prop then 0x54uy else 0x53uy)
+        yield! encodeCustomAttrElemType ty
+        yield! encodeCustomAttrString nm
+        yield! encodeCustomAttrValue ty elem
+    |]
 
 let encodeCustomAttrArgs (mspec: ILMethodSpec) (fixedArgs: _ list) (namedArgs: _ list) =
     let argTys = mspec.MethodRef.ArgTypes
-    [| yield! [| 0x01uy; 0x00uy; |]
-       for argTy, fixedArg in Seq.zip argTys fixedArgs do
-          yield! encodeCustomAttrValue argTy fixedArg
-       yield! u16AsBytes (uint16 namedArgs.Length)
-       for namedArg in namedArgs do
-           yield! encodeCustomAttrNamedArg namedArg |]
+
+    [|
+        yield! [| 0x01uy; 0x00uy |]
+        for argTy, fixedArg in Seq.zip argTys fixedArgs do
+            yield! encodeCustomAttrValue argTy fixedArg
+        yield! u16AsBytes (uint16 namedArgs.Length)
+        for namedArg in namedArgs do
+            yield! encodeCustomAttrNamedArg namedArg
+    |]
 
 let encodeCustomAttr (mspec: ILMethodSpec, fixedArgs, namedArgs) =
     let args = encodeCustomAttrArgs mspec fixedArgs namedArgs
-    ILAttribute.Encoded (mspec, args, fixedArgs @ (namedArgs |> List.map (fun (_, _, _, e) -> e)))
+    ILAttribute.Encoded(mspec, args, fixedArgs @ (namedArgs |> List.map (fun (_, _, _, e) -> e)))
 
 let mkILCustomAttribMethRef (mspec: ILMethodSpec, fixedArgs, namedArgs) =
     encodeCustomAttr (mspec, fixedArgs, namedArgs)
@@ -3881,8 +4861,7 @@ let mkILCustomAttribute (tref, argTys, argvs, propvs) =
 let getCustomAttrData cattr =
     match cattr with
     | ILAttribute.Encoded (_, data, _) -> data
-    | ILAttribute.Decoded (mspec, fixedArgs, namedArgs) ->
-        encodeCustomAttrArgs mspec fixedArgs namedArgs
+    | ILAttribute.Decoded (mspec, fixedArgs, namedArgs) -> encodeCustomAttrArgs mspec fixedArgs namedArgs
 
 // ILSecurityDecl is a 'blob' having the following format:
 // - A byte containing a period (.).
@@ -3894,43 +4873,73 @@ let getCustomAttrData cattr =
 //      in §23.3, beginning with NumNamed).
 let mkPermissionSet (action, attributes: (ILTypeRef * (string * ILType * ILAttribElem) list) list) =
     let bytes =
-        [| yield (byte '.')
-           yield! z_unsigned_int attributes.Length
-           for tref: ILTypeRef, props in attributes do
-              yield! encodeCustomAttrString tref.QualifiedName
-              let bytes =
-                  [| yield! z_unsigned_int props.Length
-                     for nm, ty, value in props do
-                         yield! encodeCustomAttrNamedArg (nm, ty, true, value)|]
-              yield! z_unsigned_int bytes.Length
-              yield! bytes |]
-
-    ILSecurityDecl.ILSecurityDecl (action, bytes)
+        [|
+            yield (byte '.')
+            yield! z_unsigned_int attributes.Length
+            for tref: ILTypeRef, props in attributes do
+                yield! encodeCustomAttrString tref.QualifiedName
+
+                let bytes =
+                    [|
+                        yield! z_unsigned_int props.Length
+                        for nm, ty, value in props do
+                            yield! encodeCustomAttrNamedArg (nm, ty, true, value)
+                    |]
+
+                yield! z_unsigned_int bytes.Length
+                yield! bytes
+        |]
+
+    ILSecurityDecl.ILSecurityDecl(action, bytes)
 
 // Parse an IL type signature argument within a custom attribute blob
-type ILTypeSigParser (tstring : string) =
+type ILTypeSigParser(tstring: string) =
 
     let mutable startPos = 0
     let mutable currentPos = 0
 
-    let reset() = startPos <- 0 ; currentPos <- 0
+    let reset () =
+        startPos <- 0
+        currentPos <- 0
+
     let nil = '\r' // cannot appear in a type sig
 
     // take a look at the next value, but don't advance
-    let peek() = if currentPos < (tstring.Length-1) then tstring[currentPos+1] else nil
-    let peekN skip = if currentPos < (tstring.Length - skip) then tstring[currentPos+skip] else nil
+    let peek () =
+        if currentPos < (tstring.Length - 1) then
+            tstring[currentPos + 1]
+        else
+            nil
+
+    let peekN skip =
+        if currentPos < (tstring.Length - skip) then
+            tstring[currentPos + skip]
+        else
+            nil
     // take a look at the current value, but don't advance
-    let here() = if currentPos < tstring.Length then tstring[currentPos] else nil
+    let here () =
+        if currentPos < tstring.Length then
+            tstring[currentPos]
+        else
+            nil
     // move on to the next character
-    let step() = currentPos <- currentPos+1
+    let step () = currentPos <- currentPos + 1
     // ignore the current lexeme
-    let skip() = startPos <- currentPos
+    let skip () = startPos <- currentPos
     // ignore the current lexeme, advance
-    let drop() = skip() ; step() ; skip()
+    let drop () =
+        skip ()
+        step ()
+        skip ()
     // return the current lexeme, advance
-    let take() =
-        let s = if currentPos < tstring.Length then tstring[startPos..currentPos] else ""
-        drop()
+    let take () =
+        let s =
+            if currentPos < tstring.Length then
+                tstring[startPos..currentPos]
+            else
+                ""
+
+        drop ()
         s
 
     // The format we accept is
@@ -3950,39 +4959,56 @@ type ILTypeSigParser (tstring : string) =
 
         // Does the type name start with a leading '['? If so, ignore it
         // (if the specialization type is in another module, it will be wrapped in bracket)
-        if here() = '[' then drop()
+        if here () = '[' then drop ()
 
         // 1. Iterate over beginning of type, grabbing the type name and determining if it's generic or an array
         let typeName =
-            while (peek() <> '`') && (peek() <> '[') && (peek() <> ']') && (peek() <> ',') && (peek() <> nil) do step()
-            take()
+            while (peek () <> '`')
+                  && (peek () <> '[')
+                  && (peek () <> ']')
+                  && (peek () <> ',')
+                  && (peek () <> nil) do
+                step ()
+
+            take ()
 
         // 2. Classify the type
 
         // Is the type generic?
         let typeName, specializations =
-            if here() = '`' then
-                drop() // step to the number
+            if here () = '`' then
+                drop () // step to the number
                 // fetch the arity
                 let arity =
-                    while (int(here()) >= (int('0'))) && (int(here()) <= (int('9'))) && (int(peek()) >= (int('0'))) && (int(peek()) <= (int('9'))) do step()
-                    Int32.Parse(take())
+                    while (int (here ()) >= (int ('0')))
+                          && (int (here ()) <= (int ('9')))
+                          && (int (peek ()) >= (int ('0')))
+                          && (int (peek ()) <= (int ('9'))) do
+                        step ()
+
+                    Int32.Parse(take ())
                 // skip the '['
-                drop()
+                drop ()
                 // get the specializations
-                typeName+"`"+(arity.ToString()), Some [for _i in 0..arity-1 do yield x.ParseType()]
+                typeName + "`" + (arity.ToString()),
+                Some
+                    [
+                        for _i in 0 .. arity - 1 do
+                            yield x.ParseType()
+                    ]
             else
                 typeName, None
 
         // Is the type an array?
         let rank =
-            if here() = '[' then
+            if here () = '[' then
                 let mutable rank = 0
 
-                while here() <> ']' do
+                while here () <> ']' do
                     rank <- rank + 1
-                    step()
-                drop()
+                    step ()
+
+                drop ()
 
                 Some(ILArrayShape(List.replicate rank (Some 0, None)))
             else
@@ -3990,35 +5016,43 @@ type ILTypeSigParser (tstring : string) =
 
         // Is there a scope?
         let scope =
-            if (here() = ',' || here() = ' ') && (peek() <> '[' && peekN 2 <> '[') then
-                let grabScopeComponent() =
-                    if here() = ',' then drop() // ditch the ','
-                    if here() = ' ' then drop() // ditch the ' '
+            if (here () = ',' || here () = ' ') && (peek () <> '[' && peekN 2 <> '[') then
+                let grabScopeComponent () =
+                    if here () = ',' then drop () // ditch the ','
+                    if here () = ' ' then drop () // ditch the ' '
 
-                    while (peek() <> ',' && peek() <> ']' && peek() <> nil) do step()
-                    take()
+                    while (peek () <> ',' && peek () <> ']' && peek () <> nil) do
+                        step ()
+
+                    take ()
 
                 let scope =
-                    [ yield grabScopeComponent() // assembly
-                      yield grabScopeComponent() // version
-                      yield grabScopeComponent() // culture
-                      yield grabScopeComponent() // public key token
-                    ] |> String.concat ","
+                    [
+                        yield grabScopeComponent () // assembly
+                        yield grabScopeComponent () // version
+                        yield grabScopeComponent () // culture
+                        yield grabScopeComponent () // public key token
+                    ]
+                    |> String.concat ","
+
                 ILScopeRef.Assembly(ILAssemblyRef.FromAssemblyName(AssemblyName scope))
             else
                 ILScopeRef.Local
 
         // strip any extraneous trailing brackets or commas
-        if (here() = ']') then drop()
-        if (here() = ',') then drop()
+        if (here () = ']') then drop ()
+        if (here () = ',') then drop ()
 
         // build the IL type
         let tref = mkILTyRef (scope, typeName)
+
         let genericArgs =
             match specializations with
             | None -> []
             | Some genericArgs -> genericArgs
-        let tspec = ILTypeSpec.Create (tref, genericArgs)
+
+        let tspec = ILTypeSpec.Create(tref, genericArgs)
+
         let ilTy =
             match tspec.Name with
             | "System.SByte"
@@ -4037,137 +5071,154 @@ type ILTypeSigParser (tstring : string) =
 
         // if it's an array, wrap it - otherwise, just return the IL type
         match rank with
-        | Some r -> ILType.Array (r, ilTy)
+        | Some r -> ILType.Array(r, ilTy)
         | _ -> ilTy
 
     member x.ParseTypeSpec() =
-        reset()
+        reset ()
         let ilTy = x.ParseType()
-        ILAttribElem.Type (Some ilTy)
+        ILAttribElem.Type(Some ilTy)
 
 let decodeILAttribData (ca: ILAttribute) =
     match ca with
     | ILAttribute.Decoded (_, fixedArgs, namedArgs) -> fixedArgs, namedArgs
     | ILAttribute.Encoded (_, bytes, _) ->
 
-    let sigptr = 0
-    let bb0, sigptr = sigptr_get_byte bytes sigptr
-    let bb1, sigptr = sigptr_get_byte bytes sigptr
-    if not (bb0 = 0x01 && bb1 = 0x00) then failwith "decodeILAttribData: invalid data"
-
-    let rec parseVal argTy sigptr =
-      match argTy with
-      | ILType.Value tspec when tspec.Name = "System.SByte" ->
-          let n, sigptr = sigptr_get_i8 bytes sigptr
-          ILAttribElem.SByte n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Byte" ->
-          let n, sigptr = sigptr_get_u8 bytes sigptr
-          ILAttribElem.Byte n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Int16" ->
-          let n, sigptr = sigptr_get_i16 bytes sigptr
-          ILAttribElem.Int16 n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.UInt16" ->
-          let n, sigptr = sigptr_get_u16 bytes sigptr
-          ILAttribElem.UInt16 n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Int32" ->
-          let n, sigptr = sigptr_get_i32 bytes sigptr
-          ILAttribElem.Int32 n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.UInt32" ->
-          let n, sigptr = sigptr_get_u32 bytes sigptr
-          ILAttribElem.UInt32 n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Int64" ->
-          let n, sigptr = sigptr_get_i64 bytes sigptr
-          ILAttribElem.Int64 n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.UInt64" ->
-          let n, sigptr = sigptr_get_u64 bytes sigptr
-          ILAttribElem.UInt64 n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Double" ->
-          let n, sigptr = sigptr_get_ieee64 bytes sigptr
-          ILAttribElem.Double n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Single" ->
-          let n, sigptr = sigptr_get_ieee32 bytes sigptr
-          ILAttribElem.Single n, sigptr
-      | ILType.Value tspec when tspec.Name = "System.Char" ->
-          let n, sigptr = sigptr_get_u16 bytes sigptr
-          ILAttribElem.Char (char (int32 n)), sigptr
-      | ILType.Value tspec when tspec.Name = "System.Boolean" ->
-          let n, sigptr = sigptr_get_byte bytes sigptr
-          ILAttribElem.Bool (not (n = 0)), sigptr
-      | ILType.Boxed tspec when tspec.Name = "System.String" ->
-          let n, sigptr = sigptr_get_serstring_possibly_null bytes sigptr
-          ILAttribElem.String n, sigptr
-      | ILType.Boxed tspec when tspec.Name = "System.Type" ->
-          let nOpt, sigptr = sigptr_get_serstring_possibly_null bytes sigptr
-          match nOpt with
-          | None -> ILAttribElem.TypeRef None, sigptr
-          | Some n ->
-            try
-                let parser = ILTypeSigParser n
-                parser.ParseTypeSpec(), sigptr
-            with exn ->
-                failwith (sprintf "decodeILAttribData: error parsing type in custom attribute blob: %s" exn.Message)
-      | ILType.Boxed tspec when tspec.Name = "System.Object" ->
-          let et, sigptr = sigptr_get_u8 bytes sigptr
-          if et = 0xFFuy then
-              ILAttribElem.Null, sigptr
-          else
-              let ty, sigptr = decodeCustomAttrElemType bytes sigptr et
-              parseVal ty sigptr
-      | ILType.Array (shape, elemTy) when shape = ILArrayShape.SingleDimensional ->
-          let n, sigptr = sigptr_get_i32 bytes sigptr
-          if n = 0xFFFFFFFF then ILAttribElem.Null, sigptr else
-          let rec parseElems acc n sigptr =
-            if n = 0 then List.rev acc, sigptr else
-            let v, sigptr = parseVal elemTy sigptr
-            parseElems (v :: acc) (n-1) sigptr
-          let elems, sigptr = parseElems [] n sigptr
-          ILAttribElem.Array (elemTy, elems), sigptr
-      | ILType.Value _ -> (* assume it is an enumeration *)
-          let n, sigptr = sigptr_get_i32 bytes sigptr
-          ILAttribElem.Int32 n, sigptr
-      | _ -> failwith "decodeILAttribData: attribute data involves an enum or System.Type value"
-
-    let rec parseFixed argTys sigptr =
-        match argTys with
-        | [] -> [], sigptr
-        | h :: t ->
-            let nh, sigptr = parseVal h sigptr
-            let nt, sigptr = parseFixed t sigptr
-            nh :: nt, sigptr
-
-    let fixedArgs, sigptr = parseFixed ca.Method.FormalArgTypes sigptr
-    let nnamed, sigptr = sigptr_get_u16 bytes sigptr
-    let rec parseNamed acc n sigptr =
-      if n = 0 then List.rev acc else
-      let isPropByte, sigptr = sigptr_get_u8 bytes sigptr
-      let isProp = (int isPropByte = 0x54)
-      let et, sigptr = sigptr_get_u8 bytes sigptr
-      // We have a named value
-      let ty, sigptr =
-        if ( (* 0x50 = (int et) || *) 0x55 = (int et)) then
-            let qualified_tname, sigptr = sigptr_get_serstring bytes sigptr
-            let unqualified_tname, rest =
-                let pieces = qualified_tname.Split ','
-                if pieces.Length > 1 then
-                    pieces[0], Some (String.concat "," pieces[1..])
+        let sigptr = 0
+        let bb0, sigptr = sigptr_get_byte bytes sigptr
+        let bb1, sigptr = sigptr_get_byte bytes sigptr
+
+        if not (bb0 = 0x01 && bb1 = 0x00) then
+            failwith "decodeILAttribData: invalid data"
+
+        let rec parseVal argTy sigptr =
+            match argTy with
+            | ILType.Value tspec when tspec.Name = "System.SByte" ->
+                let n, sigptr = sigptr_get_i8 bytes sigptr
+                ILAttribElem.SByte n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Byte" ->
+                let n, sigptr = sigptr_get_u8 bytes sigptr
+                ILAttribElem.Byte n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Int16" ->
+                let n, sigptr = sigptr_get_i16 bytes sigptr
+                ILAttribElem.Int16 n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.UInt16" ->
+                let n, sigptr = sigptr_get_u16 bytes sigptr
+                ILAttribElem.UInt16 n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Int32" ->
+                let n, sigptr = sigptr_get_i32 bytes sigptr
+                ILAttribElem.Int32 n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.UInt32" ->
+                let n, sigptr = sigptr_get_u32 bytes sigptr
+                ILAttribElem.UInt32 n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Int64" ->
+                let n, sigptr = sigptr_get_i64 bytes sigptr
+                ILAttribElem.Int64 n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.UInt64" ->
+                let n, sigptr = sigptr_get_u64 bytes sigptr
+                ILAttribElem.UInt64 n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Double" ->
+                let n, sigptr = sigptr_get_ieee64 bytes sigptr
+                ILAttribElem.Double n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Single" ->
+                let n, sigptr = sigptr_get_ieee32 bytes sigptr
+                ILAttribElem.Single n, sigptr
+            | ILType.Value tspec when tspec.Name = "System.Char" ->
+                let n, sigptr = sigptr_get_u16 bytes sigptr
+                ILAttribElem.Char(char (int32 n)), sigptr
+            | ILType.Value tspec when tspec.Name = "System.Boolean" ->
+                let n, sigptr = sigptr_get_byte bytes sigptr
+                ILAttribElem.Bool(not (n = 0)), sigptr
+            | ILType.Boxed tspec when tspec.Name = "System.String" ->
+                let n, sigptr = sigptr_get_serstring_possibly_null bytes sigptr
+                ILAttribElem.String n, sigptr
+            | ILType.Boxed tspec when tspec.Name = "System.Type" ->
+                let nOpt, sigptr = sigptr_get_serstring_possibly_null bytes sigptr
+
+                match nOpt with
+                | None -> ILAttribElem.TypeRef None, sigptr
+                | Some n ->
+                    try
+                        let parser = ILTypeSigParser n
+                        parser.ParseTypeSpec(), sigptr
+                    with
+                    | exn -> failwith (sprintf "decodeILAttribData: error parsing type in custom attribute blob: %s" exn.Message)
+            | ILType.Boxed tspec when tspec.Name = "System.Object" ->
+                let et, sigptr = sigptr_get_u8 bytes sigptr
+
+                if et = 0xFFuy then
+                    ILAttribElem.Null, sigptr
                 else
-                    pieces[0], None
-            let scoref =
-                match rest with
-                | Some aname -> ILScopeRef.Assembly (ILAssemblyRef.FromAssemblyName (AssemblyName aname))
-                | None -> PrimaryAssemblyILGlobals.primaryAssemblyScopeRef
-
-            let tref = mkILTyRef (scoref, unqualified_tname)
-            let tspec = mkILNonGenericTySpec tref
-            ILType.Value tspec, sigptr
-        else
-            decodeCustomAttrElemType bytes sigptr et
-      let nm, sigptr = sigptr_get_serstring bytes sigptr
-      let v, sigptr = parseVal ty sigptr
-      parseNamed ((nm, ty, isProp, v) :: acc) (n-1) sigptr
-    let named = parseNamed [] (int nnamed) sigptr
-    fixedArgs, named
+                    let ty, sigptr = decodeCustomAttrElemType bytes sigptr et
+                    parseVal ty sigptr
+            | ILType.Array (shape, elemTy) when shape = ILArrayShape.SingleDimensional ->
+                let n, sigptr = sigptr_get_i32 bytes sigptr
 
+                if n = 0xFFFFFFFF then
+                    ILAttribElem.Null, sigptr
+                else
+                    let rec parseElems acc n sigptr =
+                        if n = 0 then
+                            List.rev acc, sigptr
+                        else
+                            let v, sigptr = parseVal elemTy sigptr
+                            parseElems (v :: acc) (n - 1) sigptr
+
+                    let elems, sigptr = parseElems [] n sigptr
+                    ILAttribElem.Array(elemTy, elems), sigptr
+            | ILType.Value _ -> (* assume it is an enumeration *)
+                let n, sigptr = sigptr_get_i32 bytes sigptr
+                ILAttribElem.Int32 n, sigptr
+            | _ -> failwith "decodeILAttribData: attribute data involves an enum or System.Type value"
+
+        let rec parseFixed argTys sigptr =
+            match argTys with
+            | [] -> [], sigptr
+            | h :: t ->
+                let nh, sigptr = parseVal h sigptr
+                let nt, sigptr = parseFixed t sigptr
+                nh :: nt, sigptr
+
+        let fixedArgs, sigptr = parseFixed ca.Method.FormalArgTypes sigptr
+        let nnamed, sigptr = sigptr_get_u16 bytes sigptr
+
+        let rec parseNamed acc n sigptr =
+            if n = 0 then
+                List.rev acc
+            else
+                let isPropByte, sigptr = sigptr_get_u8 bytes sigptr
+                let isProp = (int isPropByte = 0x54)
+                let et, sigptr = sigptr_get_u8 bytes sigptr
+                // We have a named value
+                let ty, sigptr =
+                    if ( (* 0x50 = (int et) || *) 0x55 = (int et)) then
+                        let qualified_tname, sigptr = sigptr_get_serstring bytes sigptr
+
+                        let unqualified_tname, rest =
+                            let pieces = qualified_tname.Split ','
+
+                            if pieces.Length > 1 then
+                                pieces[0], Some(String.concat "," pieces[1..])
+                            else
+                                pieces[0], None
+
+                        let scoref =
+                            match rest with
+                            | Some aname -> ILScopeRef.Assembly(ILAssemblyRef.FromAssemblyName(AssemblyName aname))
+                            | None -> PrimaryAssemblyILGlobals.primaryAssemblyScopeRef
+
+                        let tref = mkILTyRef (scoref, unqualified_tname)
+                        let tspec = mkILNonGenericTySpec tref
+                        ILType.Value tspec, sigptr
+                    else
+                        decodeCustomAttrElemType bytes sigptr et
+
+                let nm, sigptr = sigptr_get_serstring bytes sigptr
+                let v, sigptr = parseVal ty sigptr
+                parseNamed ((nm, ty, isProp, v) :: acc) (n - 1) sigptr
+
+        let named = parseNamed [] (int nnamed) sigptr
+        fixedArgs, named
 
 // --------------------------------------------------------------------
 // Functions to collect up all the references in a full module or
@@ -4176,32 +5227,36 @@ let decodeILAttribData (ca: ILAttribute) =
 // --------------------------------------------------------------------
 
 type ILReferences =
-    { AssemblyReferences: ILAssemblyRef[]
-      ModuleReferences: ILModuleRef[]
-      TypeReferences: ILTypeRef[]
-      MethodReferences: ILMethodRef[]
-      FieldReferences: ILFieldRef[] }
+    {
+        AssemblyReferences: ILAssemblyRef[]
+        ModuleReferences: ILModuleRef[]
+        TypeReferences: ILTypeRef[]
+        MethodReferences: ILMethodRef[]
+        FieldReferences: ILFieldRef[]
+    }
 
 type ILReferencesAccumulator =
-    { ilg: ILGlobals
-      refsA: HashSet
-      refsM: HashSet
-      refsTs: HashSet
-      refsMs: HashSet
-      refsFs: HashSet }
+    {
+        ilg: ILGlobals
+        refsA: HashSet
+        refsM: HashSet
+        refsTs: HashSet
+        refsMs: HashSet
+        refsFs: HashSet
+    }
 
 let emptyILRefs =
-    { AssemblyReferences = [||]
-      ModuleReferences = [||]
-      TypeReferences = [||]
-      MethodReferences = [||]
-      FieldReferences = [||] }
+    {
+        AssemblyReferences = [||]
+        ModuleReferences = [||]
+        TypeReferences = [||]
+        MethodReferences = [||]
+        FieldReferences = [||]
+    }
 
-let refsOfILAssemblyRef (s: ILReferencesAccumulator) x =
-    s.refsA.Add x |> ignore
+let refsOfILAssemblyRef (s: ILReferencesAccumulator) x = s.refsA.Add x |> ignore
 
-let refsOfILModuleRef (s: ILReferencesAccumulator) x =
-    s.refsM.Add x |> ignore
+let refsOfILModuleRef (s: ILReferencesAccumulator) x = s.refsM.Add x |> ignore
 
 let refsOfScopeRef s x =
     match x with
@@ -4214,11 +5269,16 @@ let refsOfILTypeRef s (x: ILTypeRef) = refsOfScopeRef s x.Scope
 
 let rec refsOfILType s x =
     match x with
-    | ILType.Void | ILType.TypeVar _ -> ()
-    | ILType.Modified (_, ty1, ty2) -> refsOfILTypeRef s ty1; refsOfILType s ty2
+    | ILType.Void
+    | ILType.TypeVar _ -> ()
+    | ILType.Modified (_, ty1, ty2) ->
+        refsOfILTypeRef s ty1
+        refsOfILType s ty2
     | ILType.Array (_, ty)
-    | ILType.Ptr ty | ILType.Byref ty -> refsOfILType s ty
-    | ILType.Value tr | ILType.Boxed tr -> refsOfILTypeSpec s tr
+    | ILType.Ptr ty
+    | ILType.Byref ty -> refsOfILType s ty
+    | ILType.Value tr
+    | ILType.Boxed tr -> refsOfILTypeSpec s tr
     | ILType.FunctionPointer mref -> refsOfILCallsig s mref
 
 and refsOfILTypeSpec s (x: ILTypeSpec) =
@@ -4229,11 +5289,9 @@ and refsOfILCallsig s csig =
     refsOfILTypes s csig.ArgTypes
     refsOfILType s csig.ReturnType
 
-and refsOfILGenericParam s x =
-    refsOfILTypes s x.Constraints
+and refsOfILGenericParam s x = refsOfILTypes s x.Constraints
 
-and refsOfILGenericParams s b =
-    List.iter (refsOfILGenericParam s) b
+and refsOfILGenericParams s b = List.iter (refsOfILGenericParam s) b
 
 and refsOfILMethodRef s (x: ILMethodRef) =
     refsOfILTypeRef s x.DeclaringTypeRef
@@ -4272,26 +5330,27 @@ and refsOfILCustomAttrElem s (elem: ILAttribElem) =
     | Type (Some ty) -> refsOfILType s ty
     | TypeRef (Some tref) -> refsOfILTypeRef s tref
     | Array (ty, els) ->
-        refsOfILType s ty 
+        refsOfILType s ty
         refsOfILCustomAttrElems s els
     | _ -> ()
-    
+
 and refsOfILCustomAttrElems s els =
     els |> List.iter (refsOfILCustomAttrElem s)
 
 and refsOfILCustomAttr s (cattr: ILAttribute) =
     refsOfILMethodSpec s cattr.Method
-    refsOfILCustomAttrElems s cattr.Elements 
+    refsOfILCustomAttrElems s cattr.Elements
 
-and refsOfILCustomAttrs s (cas : ILAttributes) =
+and refsOfILCustomAttrs s (cas: ILAttributes) =
     cas.AsArray() |> Array.iter (refsOfILCustomAttr s)
 
-and refsOfILVarArgs s tyso =
-    Option.iter (refsOfILTypes s) tyso
+and refsOfILVarArgs s tyso = Option.iter (refsOfILTypes s) tyso
 
 and refsOfILInstr s x =
     match x with
-    | I_call (_, mr, varargs) | I_newobj (mr, varargs) | I_callvirt (_, mr, varargs) ->
+    | I_call (_, mr, varargs)
+    | I_newobj (mr, varargs)
+    | I_callvirt (_, mr, varargs) ->
         refsOfILMethodSpec s mr
         refsOfILVarArgs s varargs
     | I_callconstraint (_, tr, mr, varargs) ->
@@ -4299,28 +5358,99 @@ and refsOfILInstr s x =
         refsOfILMethodSpec s mr
         refsOfILVarArgs s varargs
     | I_calli (_, callsig, varargs) ->
-        refsOfILCallsig s callsig; refsOfILVarArgs s varargs
-    | I_jmp mr | I_ldftn mr | I_ldvirtftn mr ->
-        refsOfILMethodSpec s mr
-    | I_ldsfld (_, fr) | I_ldfld (_, _, fr) | I_ldsflda fr | I_ldflda fr | I_stsfld (_, fr) | I_stfld (_, _, fr) ->
-        refsOfILFieldSpec s fr
-    | I_isinst ty | I_castclass ty | I_cpobj ty | I_initobj ty | I_ldobj (_, _, ty)
-    | I_stobj (_, _, ty) | I_box ty |I_unbox ty | I_unbox_any ty | I_sizeof ty
-    | I_ldelem_any (_, ty) | I_ldelema (_, _, _, ty) |I_stelem_any (_, ty) | I_newarr (_, ty)
-    | I_mkrefany ty | I_refanyval ty
+        refsOfILCallsig s callsig
+        refsOfILVarArgs s varargs
+    | I_jmp mr
+    | I_ldftn mr
+    | I_ldvirtftn mr -> refsOfILMethodSpec s mr
+    | I_ldsfld (_, fr)
+    | I_ldfld (_, _, fr)
+    | I_ldsflda fr
+    | I_ldflda fr
+    | I_stsfld (_, fr)
+    | I_stfld (_, _, fr) -> refsOfILFieldSpec s fr
+    | I_isinst ty
+    | I_castclass ty
+    | I_cpobj ty
+    | I_initobj ty
+    | I_ldobj (_, _, ty)
+    | I_stobj (_, _, ty)
+    | I_box ty
+    | I_unbox ty
+    | I_unbox_any ty
+    | I_sizeof ty
+    | I_ldelem_any (_, ty)
+    | I_ldelema (_, _, _, ty)
+    | I_stelem_any (_, ty)
+    | I_newarr (_, ty)
+    | I_mkrefany ty
+    | I_refanyval ty
     | EI_ilzero ty -> refsOfILType s ty
     | I_ldtoken token -> refsOfILToken s token
-    | I_stelem _|I_ldelem _|I_ldstr _|I_switch _|I_stloc _|I_stind _
-    | I_starg _|I_ldloca _|I_ldloc _|I_ldind _
-    | I_ldarga _|I_ldarg _|I_leave _|I_br _
-    | I_brcmp _|I_rethrow|I_refanytype|I_ldlen|I_throw|I_initblk _ |I_cpblk _
-    | I_localloc|I_ret |I_endfilter|I_endfinally|I_arglist
+    | I_stelem _
+    | I_ldelem _
+    | I_ldstr _
+    | I_switch _
+    | I_stloc _
+    | I_stind _
+    | I_starg _
+    | I_ldloca _
+    | I_ldloc _
+    | I_ldind _
+    | I_ldarga _
+    | I_ldarg _
+    | I_leave _
+    | I_br _
+    | I_brcmp _
+    | I_rethrow
+    | I_refanytype
+    | I_ldlen
+    | I_throw
+    | I_initblk _
+    | I_cpblk _
+    | I_localloc
+    | I_ret
+    | I_endfilter
+    | I_endfinally
+    | I_arglist
     | I_break
-    | AI_add | AI_add_ovf | AI_add_ovf_un | AI_and | AI_div | AI_div_un | AI_ceq | AI_cgt | AI_cgt_un | AI_clt
-    | AI_clt_un | AI_conv _ | AI_conv_ovf _ | AI_conv_ovf_un _ | AI_mul | AI_mul_ovf | AI_mul_ovf_un | AI_rem | AI_rem_un
-    | AI_shl | AI_shr | AI_shr_un | AI_sub | AI_sub_ovf | AI_sub_ovf_un | AI_xor | AI_or | AI_neg | AI_not
-    | AI_ldnull | AI_dup | AI_pop | AI_ckfinite | AI_nop | AI_ldc _
-    | I_seqpoint _ | EI_ldlen_multi _ -> ()
+    | AI_add
+    | AI_add_ovf
+    | AI_add_ovf_un
+    | AI_and
+    | AI_div
+    | AI_div_un
+    | AI_ceq
+    | AI_cgt
+    | AI_cgt_un
+    | AI_clt
+    | AI_clt_un
+    | AI_conv _
+    | AI_conv_ovf _
+    | AI_conv_ovf_un _
+    | AI_mul
+    | AI_mul_ovf
+    | AI_mul_ovf_un
+    | AI_rem
+    | AI_rem_un
+    | AI_shl
+    | AI_shr
+    | AI_shr_un
+    | AI_sub
+    | AI_sub_ovf
+    | AI_sub_ovf_un
+    | AI_xor
+    | AI_or
+    | AI_neg
+    | AI_not
+    | AI_ldnull
+    | AI_dup
+    | AI_pop
+    | AI_ckfinite
+    | AI_nop
+    | AI_ldc _
+    | I_seqpoint _
+    | EI_ldlen_multi _ -> ()
 
 and refsOfILCode s (c: ILCode) =
     for i in c.Instrs do
@@ -4381,17 +5511,15 @@ and refsOfILFieldDef s (fd: ILFieldDef) =
     refsOfILType s fd.FieldType
     refsOfILCustomAttrs s fd.CustomAttrs
 
-and refsOfILFieldDefs s fields =
-    List.iter (refsOfILFieldDef s) fields
+and refsOfILFieldDefs s fields = List.iter (refsOfILFieldDef s) fields
 
-and refsOfILMethodImpls s mimpls =
-    List.iter (refsOfILMethodImpl s) mimpls
+and refsOfILMethodImpls s mimpls = List.iter (refsOfILMethodImpl s) mimpls
 
 and refsOfILMethodImpl s m =
     refsOfILOverridesSpec s m.Overrides
     refsOfILMethodSpec s m.OverrideBy
 
-and refsOfILTypeDef s (td : ILTypeDef) =
+and refsOfILTypeDef s (td: ILTypeDef) =
     refsOfILTypeDefs s td.NestedTypes
     refsOfILGenericParams s td.GenericParams
     refsOfILTypes s td.Implements
@@ -4405,8 +5533,7 @@ and refsOfILTypeDef s (td : ILTypeDef) =
 
 and refsOfILTypeDefs s (types: ILTypeDefs) = Seq.iter (refsOfILTypeDef s) types
 
-and refsOfILExportedType s (c: ILExportedTypeOrForwarder) =
-    refsOfILCustomAttrs s c.CustomAttrs
+and refsOfILExportedType s (c: ILExportedTypeOrForwarder) = refsOfILCustomAttrs s c.CustomAttrs
 
 and refsOfILExportedTypes s (tab: ILExportedTypesAndForwarders) =
     List.iter (refsOfILExportedType s) (tab.AsList())
@@ -4436,42 +5563,50 @@ and refsOfILManifest s (m: ILAssemblyManifest) =
 
 let computeILRefs ilg modul =
     let s =
-      { ilg = ilg
-        refsA = HashSet<_>(HashIdentity.Structural)
-        refsM = HashSet<_>(HashIdentity.Structural)
-        refsTs = HashSet<_>(HashIdentity.Structural)
-        refsMs = HashSet<_>(HashIdentity.Structural)
-        refsFs = HashSet<_>(HashIdentity.Structural) }
+        {
+            ilg = ilg
+            refsA = HashSet<_>(HashIdentity.Structural)
+            refsM = HashSet<_>(HashIdentity.Structural)
+            refsTs = HashSet<_>(HashIdentity.Structural)
+            refsMs = HashSet<_>(HashIdentity.Structural)
+            refsFs = HashSet<_>(HashIdentity.Structural)
+        }
 
     refsOfILModule s modul
-    { AssemblyReferences = s.refsA.ToArray()
-      ModuleReferences = s.refsM.ToArray()
-      TypeReferences = s.refsTs.ToArray()
-      MethodReferences = s.refsMs.ToArray()
-      FieldReferences = s.refsFs.ToArray() }
 
-let unscopeILTypeRef (x: ILTypeRef) = ILTypeRef.Create (ILScopeRef.Local, x.Enclosing, x.Name)
+    {
+        AssemblyReferences = s.refsA.ToArray()
+        ModuleReferences = s.refsM.ToArray()
+        TypeReferences = s.refsTs.ToArray()
+        MethodReferences = s.refsMs.ToArray()
+        FieldReferences = s.refsFs.ToArray()
+    }
+
+let unscopeILTypeRef (x: ILTypeRef) =
+    ILTypeRef.Create(ILScopeRef.Local, x.Enclosing, x.Name)
 
 let rec unscopeILTypeSpec (tspec: ILTypeSpec) =
     let tref = tspec.TypeRef
     let tinst = tspec.GenericArgs
     let tref = unscopeILTypeRef tref
-    ILTypeSpec.Create (tref, unscopeILTypes tinst)
+    ILTypeSpec.Create(tref, unscopeILTypes tinst)
 
 and unscopeILType ty =
     match ty with
-    | ILType.Ptr t -> ILType.Ptr (unscopeILType t)
-    | ILType.FunctionPointer t -> ILType.FunctionPointer (unscopeILCallSig t)
-    | ILType.Byref t -> ILType.Byref (unscopeILType t)
+    | ILType.Ptr t -> ILType.Ptr(unscopeILType t)
+    | ILType.FunctionPointer t -> ILType.FunctionPointer(unscopeILCallSig t)
+    | ILType.Byref t -> ILType.Byref(unscopeILType t)
     | ILType.Boxed cr -> mkILBoxedType (unscopeILTypeSpec cr)
-    | ILType.Array (s, ty) -> ILType.Array (s, unscopeILType ty)
-    | ILType.Value cr -> ILType.Value (unscopeILTypeSpec cr)
-    | ILType.Modified (b, tref, ty) -> ILType.Modified (b, unscopeILTypeRef tref, unscopeILType ty)
+    | ILType.Array (s, ty) -> ILType.Array(s, unscopeILType ty)
+    | ILType.Value cr -> ILType.Value(unscopeILTypeSpec cr)
+    | ILType.Modified (b, tref, ty) -> ILType.Modified(b, unscopeILTypeRef tref, unscopeILType ty)
     | x -> x
 
 and unscopeILTypes i =
-    if List.isEmpty i then i
-    else List.map unscopeILType i
+    if List.isEmpty i then
+        i
+    else
+        List.map unscopeILType i
 
 and unscopeILCallSig csig =
     mkILCallSig (csig.CallingConv, unscopeILTypes csig.ArgTypes, unscopeILType csig.ReturnType)
@@ -4480,41 +5615,65 @@ let resolveILMethodRefWithRescope r (td: ILTypeDef) (mref: ILMethodRef) =
     let args = mref.ArgTypes
     let nargs = args.Length
     let nm = mref.Name
-    let possibles = td.Methods.FindByNameAndArity (nm, nargs)
-    if isNil possibles then failwith ("no method named " + nm + " found in type " + td.Name)
+    let possibles = td.Methods.FindByNameAndArity(nm, nargs)
+
+    if isNil possibles then
+        failwith ("no method named " + nm + " found in type " + td.Name)
+
     let argTypes = mref.ArgTypes |> List.map r
-    let retType : ILType = r mref.ReturnType
-    match
-      possibles |> List.filter (fun md ->
-          mref.CallingConv = md.CallingConv &&
-          // REVIEW: this uses equality on ILType. For CMOD_OPTIONAL this is not going to be correct
-          (md.Parameters, argTypes) ||> List.lengthsEqAndForall2 (fun p1 p2 -> r p1.Type = p2) &&
-          // REVIEW: this uses equality on ILType. For CMOD_OPTIONAL this is not going to be correct
-          r md.Return.Type = retType) with
-    | [] -> failwith ("no method named "+nm+" with appropriate argument types found in type "+td.Name)
-    | [mdef] -> mdef
-    | _ -> failwith ("multiple methods named "+nm+" appear with identical argument types in type "+td.Name)
-
-let resolveILMethodRef td mref = resolveILMethodRefWithRescope id td mref
-
-let mkRefToILModule m =
-    ILModuleRef.Create (m.Name, true, None)
+    let retType: ILType = r mref.ReturnType
+
+    match possibles
+          |> List.filter (fun md ->
+              mref.CallingConv = md.CallingConv
+              &&
+              // REVIEW: this uses equality on ILType. For CMOD_OPTIONAL this is not going to be correct
+              (md.Parameters, argTypes)
+              ||> List.lengthsEqAndForall2 (fun p1 p2 -> r p1.Type = p2)
+              &&
+              // REVIEW: this uses equality on ILType. For CMOD_OPTIONAL this is not going to be correct
+              r md.Return.Type = retType)
+        with
+    | [] ->
+        failwith (
+            "no method named "
+            + nm
+            + " with appropriate argument types found in type "
+            + td.Name
+        )
+    | [ mdef ] -> mdef
+    | _ ->
+        failwith (
+            "multiple methods named "
+            + nm
+            + " appear with identical argument types in type "
+            + td.Name
+        )
+
+let resolveILMethodRef td mref =
+    resolveILMethodRefWithRescope id td mref
+
+let mkRefToILModule m = ILModuleRef.Create(m.Name, true, None)
 
 type ILEventRef =
-    { erA: ILTypeRef
-      erB: string }
+    {
+        erA: ILTypeRef
+        erB: string
+    }
 
-    static member Create (a, b) = {erA=a;erB=b}
+    static member Create(a, b) = { erA = a; erB = b }
 
     member x.DeclaringTypeRef = x.erA
 
     member x.Name = x.erB
 
 type ILPropertyRef =
-    { prA: ILTypeRef
-      prB: string }
+    {
+        prA: ILTypeRef
+        prB: string
+    }
 
-    static member Create (a, b) = {prA=a;prB=b}
+    static member Create(a, b) = { prA = a; prB = b }
 
     member x.DeclaringTypeRef = x.prA
 
diff --git a/src/Compiler/AbstractIL/ilascii.fs b/src/Compiler/AbstractIL/ilascii.fs
index 495f8fa214d..92c481b0f90 100644
--- a/src/Compiler/AbstractIL/ilascii.fs
+++ b/src/Compiler/AbstractIL/ilascii.fs
@@ -8,263 +8,258 @@ open FSharp.Compiler.AbstractIL.IL
 
 /// Table of parsing and pretty printing data for instructions.
 let noArgInstrs =
-    lazy [
-        ["ldc";"i4";"0"], mkLdcInt32 0
-        ["ldc";"i4";"1"], mkLdcInt32 1
-        ["ldc";"i4";"2"], mkLdcInt32 2
-        ["ldc";"i4";"3"], mkLdcInt32 3
-        ["ldc";"i4";"4"], mkLdcInt32 4
-        ["ldc";"i4";"5"], mkLdcInt32 5
-        ["ldc";"i4";"6"], mkLdcInt32 6
-        ["ldc";"i4";"7"], mkLdcInt32 7
-        ["ldc";"i4";"8"], mkLdcInt32 8
-        ["ldc";"i4";"M1"], mkLdcInt32 -1
-        ["ldc";"i4";"m1"], mkLdcInt32 -1
-        ["stloc";"0"], mkStloc (uint16 0)
-        ["stloc";"1"], mkStloc (uint16 1)
-        ["stloc";"2"], mkStloc (uint16 2)
-        ["stloc";"3"], mkStloc (uint16 3)
-        ["ldloc";"0"], mkLdloc (uint16 0)
-        ["ldloc";"1"], mkLdloc (uint16 1)
-        ["ldloc";"2"], mkLdloc (uint16 2)
-        ["ldloc";"3"], mkLdloc (uint16 3)
-        ["ldarg";"0"], mkLdarg (uint16 0)
-        ["ldarg";"1"], mkLdarg (uint16 1)
-        ["ldarg";"2"], mkLdarg (uint16 2)
-        ["ldarg";"3"], mkLdarg (uint16 3)
-        ["ret"], I_ret
-        ["add"], AI_add
-        ["add";"ovf"], AI_add_ovf
-        ["add";"ovf";"un"], AI_add_ovf_un
-        ["and"], AI_and
-        ["div"], AI_div
-        ["div";"un"], AI_div_un
-        ["ceq"], AI_ceq
-        ["cgt"], AI_cgt
-        ["cgt";"un"], AI_cgt_un
-        ["clt"], AI_clt
-        ["clt";"un"], AI_clt_un
-        ["conv";"i1"], AI_conv DT_I1
-        ["conv";"i2"], AI_conv DT_I2
-        ["conv";"i4"], AI_conv DT_I4
-        ["conv";"i8"], AI_conv DT_I8
-        ["conv";"i"], AI_conv DT_I
-        ["conv";"r4"], AI_conv DT_R4
-        ["conv";"r8"], AI_conv DT_R8
-        ["conv";"u1"], AI_conv DT_U1
-        ["conv";"u2"], AI_conv DT_U2
-        ["conv";"u4"], AI_conv DT_U4
-        ["conv";"u8"], AI_conv DT_U8
-        ["conv";"u"], AI_conv DT_U
-        ["conv";"r"; "un"], AI_conv DT_R
-        ["conv";"ovf";"i1"], AI_conv_ovf DT_I1
-        ["conv";"ovf";"i2"], AI_conv_ovf DT_I2
-        ["conv";"ovf";"i4"], AI_conv_ovf DT_I4
-        ["conv";"ovf";"i8"], AI_conv_ovf DT_I8
-        ["conv";"ovf";"i"], AI_conv_ovf DT_I
-        ["conv";"ovf";"u1"], AI_conv_ovf DT_U1
-        ["conv";"ovf";"u2"], AI_conv_ovf DT_U2
-        ["conv";"ovf";"u4"], AI_conv_ovf DT_U4
-        ["conv";"ovf";"u8"], AI_conv_ovf DT_U8
-        ["conv";"ovf";"u"], AI_conv_ovf DT_U
-        ["conv";"ovf";"i1"; "un"], AI_conv_ovf_un DT_I1
-        ["conv";"ovf";"i2"; "un"], AI_conv_ovf_un DT_I2
-        ["conv";"ovf";"i4"; "un"], AI_conv_ovf_un DT_I4
-        ["conv";"ovf";"i8"; "un"], AI_conv_ovf_un DT_I8
-        ["conv";"ovf";"i"; "un"], AI_conv_ovf_un DT_I
-        ["conv";"ovf";"u1"; "un"], AI_conv_ovf_un DT_U1
-        ["conv";"ovf";"u2"; "un"], AI_conv_ovf_un DT_U2
-        ["conv";"ovf";"u4"; "un"], AI_conv_ovf_un DT_U4
-        ["conv";"ovf";"u8"; "un"], AI_conv_ovf_un DT_U8
-        ["conv";"ovf";"u"; "un"], AI_conv_ovf_un DT_U
-        ["stelem";"i1"], I_stelem DT_I1
-        ["stelem";"i2"], I_stelem DT_I2
-        ["stelem";"i4"], I_stelem DT_I4
-        ["stelem";"i8"], I_stelem DT_I8
-        ["stelem";"r4"], I_stelem DT_R4
-        ["stelem";"r8"], I_stelem DT_R8
-        ["stelem";"i"], I_stelem DT_I
-        ["stelem";"u"], I_stelem DT_I
-        ["stelem";"u8"], I_stelem DT_I8
-        ["stelem";"ref"], I_stelem DT_REF
-        ["ldelem";"i1"], I_ldelem DT_I1
-        ["ldelem";"i2"], I_ldelem DT_I2
-        ["ldelem";"i4"], I_ldelem DT_I4
-        ["ldelem";"i8"], I_ldelem DT_I8
-        ["ldelem";"u8"], I_ldelem DT_I8
-        ["ldelem";"u1"], I_ldelem DT_U1
-        ["ldelem";"u2"], I_ldelem DT_U2
-        ["ldelem";"u4"], I_ldelem DT_U4
-        ["ldelem";"r4"], I_ldelem DT_R4
-        ["ldelem";"r8"], I_ldelem DT_R8
-        ["ldelem";"u"], I_ldelem DT_I // EQUIV
-        ["ldelem";"i"], I_ldelem DT_I
-        ["ldelem";"ref"], I_ldelem DT_REF
-        ["mul"], AI_mul
-        ["mul";"ovf"], AI_mul_ovf
-        ["mul";"ovf";"un"], AI_mul_ovf_un
-        ["rem"], AI_rem
-        ["rem";"un"], AI_rem_un 
-        ["shl"], AI_shl 
-        ["shr"], AI_shr 
-        ["shr";"un"], AI_shr_un
-        ["sub"], AI_sub
-        ["sub";"ovf"], AI_sub_ovf
-        ["sub";"ovf";"un"], AI_sub_ovf_un
-        ["xor"], AI_xor
-        ["or"], AI_or
-        ["neg"], AI_neg
-        ["not"], AI_not
-        ["ldnull"], AI_ldnull
-        ["dup"], AI_dup
-        ["pop"], AI_pop
-        ["ckfinite"], AI_ckfinite
-        ["nop"], AI_nop
-        ["break"], I_break
-        ["arglist"], I_arglist
-        ["endfilter"], I_endfilter
-        ["endfinally"], I_endfinally
-        ["refanytype"], I_refanytype
-        ["localloc"], I_localloc
-        ["throw"], I_throw
-        ["ldlen"], I_ldlen
-        ["rethrow"], I_rethrow
-    ]
+    lazy
+        [
+            [ "ldc"; "i4"; "0" ], mkLdcInt32 0
+            [ "ldc"; "i4"; "1" ], mkLdcInt32 1
+            [ "ldc"; "i4"; "2" ], mkLdcInt32 2
+            [ "ldc"; "i4"; "3" ], mkLdcInt32 3
+            [ "ldc"; "i4"; "4" ], mkLdcInt32 4
+            [ "ldc"; "i4"; "5" ], mkLdcInt32 5
+            [ "ldc"; "i4"; "6" ], mkLdcInt32 6
+            [ "ldc"; "i4"; "7" ], mkLdcInt32 7
+            [ "ldc"; "i4"; "8" ], mkLdcInt32 8
+            [ "ldc"; "i4"; "M1" ], mkLdcInt32 -1
+            [ "ldc"; "i4"; "m1" ], mkLdcInt32 -1
+            [ "stloc"; "0" ], mkStloc (uint16 0)
+            [ "stloc"; "1" ], mkStloc (uint16 1)
+            [ "stloc"; "2" ], mkStloc (uint16 2)
+            [ "stloc"; "3" ], mkStloc (uint16 3)
+            [ "ldloc"; "0" ], mkLdloc (uint16 0)
+            [ "ldloc"; "1" ], mkLdloc (uint16 1)
+            [ "ldloc"; "2" ], mkLdloc (uint16 2)
+            [ "ldloc"; "3" ], mkLdloc (uint16 3)
+            [ "ldarg"; "0" ], mkLdarg (uint16 0)
+            [ "ldarg"; "1" ], mkLdarg (uint16 1)
+            [ "ldarg"; "2" ], mkLdarg (uint16 2)
+            [ "ldarg"; "3" ], mkLdarg (uint16 3)
+            [ "ret" ], I_ret
+            [ "add" ], AI_add
+            [ "add"; "ovf" ], AI_add_ovf
+            [ "add"; "ovf"; "un" ], AI_add_ovf_un
+            [ "and" ], AI_and
+            [ "div" ], AI_div
+            [ "div"; "un" ], AI_div_un
+            [ "ceq" ], AI_ceq
+            [ "cgt" ], AI_cgt
+            [ "cgt"; "un" ], AI_cgt_un
+            [ "clt" ], AI_clt
+            [ "clt"; "un" ], AI_clt_un
+            [ "conv"; "i1" ], AI_conv DT_I1
+            [ "conv"; "i2" ], AI_conv DT_I2
+            [ "conv"; "i4" ], AI_conv DT_I4
+            [ "conv"; "i8" ], AI_conv DT_I8
+            [ "conv"; "i" ], AI_conv DT_I
+            [ "conv"; "r4" ], AI_conv DT_R4
+            [ "conv"; "r8" ], AI_conv DT_R8
+            [ "conv"; "u1" ], AI_conv DT_U1
+            [ "conv"; "u2" ], AI_conv DT_U2
+            [ "conv"; "u4" ], AI_conv DT_U4
+            [ "conv"; "u8" ], AI_conv DT_U8
+            [ "conv"; "u" ], AI_conv DT_U
+            [ "conv"; "r"; "un" ], AI_conv DT_R
+            [ "conv"; "ovf"; "i1" ], AI_conv_ovf DT_I1
+            [ "conv"; "ovf"; "i2" ], AI_conv_ovf DT_I2
+            [ "conv"; "ovf"; "i4" ], AI_conv_ovf DT_I4
+            [ "conv"; "ovf"; "i8" ], AI_conv_ovf DT_I8
+            [ "conv"; "ovf"; "i" ], AI_conv_ovf DT_I
+            [ "conv"; "ovf"; "u1" ], AI_conv_ovf DT_U1
+            [ "conv"; "ovf"; "u2" ], AI_conv_ovf DT_U2
+            [ "conv"; "ovf"; "u4" ], AI_conv_ovf DT_U4
+            [ "conv"; "ovf"; "u8" ], AI_conv_ovf DT_U8
+            [ "conv"; "ovf"; "u" ], AI_conv_ovf DT_U
+            [ "conv"; "ovf"; "i1"; "un" ], AI_conv_ovf_un DT_I1
+            [ "conv"; "ovf"; "i2"; "un" ], AI_conv_ovf_un DT_I2
+            [ "conv"; "ovf"; "i4"; "un" ], AI_conv_ovf_un DT_I4
+            [ "conv"; "ovf"; "i8"; "un" ], AI_conv_ovf_un DT_I8
+            [ "conv"; "ovf"; "i"; "un" ], AI_conv_ovf_un DT_I
+            [ "conv"; "ovf"; "u1"; "un" ], AI_conv_ovf_un DT_U1
+            [ "conv"; "ovf"; "u2"; "un" ], AI_conv_ovf_un DT_U2
+            [ "conv"; "ovf"; "u4"; "un" ], AI_conv_ovf_un DT_U4
+            [ "conv"; "ovf"; "u8"; "un" ], AI_conv_ovf_un DT_U8
+            [ "conv"; "ovf"; "u"; "un" ], AI_conv_ovf_un DT_U
+            [ "stelem"; "i1" ], I_stelem DT_I1
+            [ "stelem"; "i2" ], I_stelem DT_I2
+            [ "stelem"; "i4" ], I_stelem DT_I4
+            [ "stelem"; "i8" ], I_stelem DT_I8
+            [ "stelem"; "r4" ], I_stelem DT_R4
+            [ "stelem"; "r8" ], I_stelem DT_R8
+            [ "stelem"; "i" ], I_stelem DT_I
+            [ "stelem"; "u" ], I_stelem DT_I
+            [ "stelem"; "u8" ], I_stelem DT_I8
+            [ "stelem"; "ref" ], I_stelem DT_REF
+            [ "ldelem"; "i1" ], I_ldelem DT_I1
+            [ "ldelem"; "i2" ], I_ldelem DT_I2
+            [ "ldelem"; "i4" ], I_ldelem DT_I4
+            [ "ldelem"; "i8" ], I_ldelem DT_I8
+            [ "ldelem"; "u8" ], I_ldelem DT_I8
+            [ "ldelem"; "u1" ], I_ldelem DT_U1
+            [ "ldelem"; "u2" ], I_ldelem DT_U2
+            [ "ldelem"; "u4" ], I_ldelem DT_U4
+            [ "ldelem"; "r4" ], I_ldelem DT_R4
+            [ "ldelem"; "r8" ], I_ldelem DT_R8
+            [ "ldelem"; "u" ], I_ldelem DT_I // EQUIV
+            [ "ldelem"; "i" ], I_ldelem DT_I
+            [ "ldelem"; "ref" ], I_ldelem DT_REF
+            [ "mul" ], AI_mul
+            [ "mul"; "ovf" ], AI_mul_ovf
+            [ "mul"; "ovf"; "un" ], AI_mul_ovf_un
+            [ "rem" ], AI_rem
+            [ "rem"; "un" ], AI_rem_un
+            [ "shl" ], AI_shl
+            [ "shr" ], AI_shr
+            [ "shr"; "un" ], AI_shr_un
+            [ "sub" ], AI_sub
+            [ "sub"; "ovf" ], AI_sub_ovf
+            [ "sub"; "ovf"; "un" ], AI_sub_ovf_un
+            [ "xor" ], AI_xor
+            [ "or" ], AI_or
+            [ "neg" ], AI_neg
+            [ "not" ], AI_not
+            [ "ldnull" ], AI_ldnull
+            [ "dup" ], AI_dup
+            [ "pop" ], AI_pop
+            [ "ckfinite" ], AI_ckfinite
+            [ "nop" ], AI_nop
+            [ "break" ], I_break
+            [ "arglist" ], I_arglist
+            [ "endfilter" ], I_endfilter
+            [ "endfinally" ], I_endfinally
+            [ "refanytype" ], I_refanytype
+            [ "localloc" ], I_localloc
+            [ "throw" ], I_throw
+            [ "ldlen" ], I_ldlen
+            [ "rethrow" ], I_rethrow
+        ]
 
 #if DEBUG
 let wordsOfNoArgInstr, isNoArgInstr =
     let t =
-      lazy
-        (let t = HashMultiMap(300, HashIdentity.Structural)
-         noArgInstrs |> Lazy.force |> List.iter (fun (x, mk) -> t.Add(mk, x))
-         t)
-    (fun s -> (Lazy.force t)[s]),
-    (fun s -> (Lazy.force t).ContainsKey s)
+        lazy
+            (let t = HashMultiMap(300, HashIdentity.Structural)
+             noArgInstrs |> Lazy.force |> List.iter (fun (x, mk) -> t.Add(mk, x))
+             t)
+
+    (fun s -> (Lazy.force t)[s]), (fun s -> (Lazy.force t).ContainsKey s)
 #endif
 
-let mk_stind (nm, dt) =  (nm, (fun () -> I_stind(Aligned, Nonvolatile, dt)))
-let mk_ldind (nm, dt) =  (nm, (fun () -> I_ldind(Aligned, Nonvolatile, dt)))
+let mk_stind (nm, dt) =
+    (nm, (fun () -> I_stind(Aligned, Nonvolatile, dt)))
+
+let mk_ldind (nm, dt) =
+    (nm, (fun () -> I_ldind(Aligned, Nonvolatile, dt)))
 
 type NoArgInstr = unit -> ILInstr
-type Int32Instr = int32 ->  ILInstr
-type Int32Int32Instr = int32 * int32 ->  ILInstr
-type Int64Instr = int64 ->  ILInstr
-type DoubleInstr = ILConst ->  ILInstr
-type MethodSpecInstr = ILMethodSpec * ILVarArgs ->  ILInstr
-type TypeInstr = ILType ->  ILInstr
-type IntTypeInstr = int * ILType ->  ILInstr
-type ValueTypeInstr = ILType ->  ILInstr  (* nb. diff. interp of types to TypeInstr *)
-type StringInstr = string ->  ILInstr
-type TokenInstr = ILToken ->  ILInstr
-type SwitchInstr = ILCodeLabel list * ILCodeLabel ->  ILInstr
+type Int32Instr = int32 -> ILInstr
+type Int32Int32Instr = int32 * int32 -> ILInstr
+type Int64Instr = int64 -> ILInstr
+type DoubleInstr = ILConst -> ILInstr
+type MethodSpecInstr = ILMethodSpec * ILVarArgs -> ILInstr
+type TypeInstr = ILType -> ILInstr
+type IntTypeInstr = int * ILType -> ILInstr
+type ValueTypeInstr = ILType -> ILInstr (* nb. diff. interp of types to TypeInstr *)
+type StringInstr = string -> ILInstr
+type TokenInstr = ILToken -> ILInstr
+type SwitchInstr = ILCodeLabel list * ILCodeLabel -> ILInstr
 
 type InstrTable<'T> = (string list * 'T) list
 type LazyInstrTable<'T> = Lazy>
 
 /// Table of parsing and pretty printing data for instructions.
-let NoArgInstrs : Lazy> =
-    lazy [ 
-        for nm, i in noArgInstrs.Force() do  
-             yield (nm, (fun () -> i))
-        yield mk_stind (["stind";"u"], DT_I)
-        yield mk_stind (["stind";"i"], DT_I)
-        yield mk_stind (["stind";"u1"], DT_I1)  
-        yield mk_stind (["stind";"i1"], DT_I1)
-        yield mk_stind (["stind";"u2"], DT_I2)
-        yield mk_stind (["stind";"i2"], DT_I2)
-        yield mk_stind (["stind";"u4"], DT_I4)
-        yield mk_stind (["stind";"i4"], DT_I4)
-        yield mk_stind (["stind";"u8"], DT_I8)
-        yield mk_stind (["stind";"i8"], DT_I8)
-        yield mk_stind (["stind";"r4"], DT_R4)
-        yield mk_stind (["stind";"r8"], DT_R8)
-        yield mk_stind (["stind";"ref"], DT_REF)
-        yield mk_ldind (["ldind";"i"], DT_I)
-        yield mk_ldind (["ldind";"i1"], DT_I1)
-        yield mk_ldind (["ldind";"i2"], DT_I2)
-        yield mk_ldind (["ldind";"i4"], DT_I4)
-        yield mk_ldind (["ldind";"i8"], DT_I8)
-        yield mk_ldind (["ldind";"u1"], DT_U1)
-        yield mk_ldind (["ldind";"u2"], DT_U2)
-        yield mk_ldind (["ldind";"u4"], DT_U4)
-        yield mk_ldind (["ldind";"u8"], DT_I8)
-        yield mk_ldind (["ldind";"r4"], DT_R4)
-        yield mk_ldind (["ldind";"r8"], DT_R8)
-        yield mk_ldind (["ldind";"ref"], DT_REF)
-        yield ["cpblk"], (fun () -> I_cpblk(Aligned, Nonvolatile))
-        yield ["initblk"], (fun () -> I_initblk(Aligned, Nonvolatile))
-    ]
+let NoArgInstrs: Lazy> =
+    lazy
+        [
+            for nm, i in noArgInstrs.Force() do
+                yield (nm, (fun () -> i))
+            yield mk_stind ([ "stind"; "u" ], DT_I)
+            yield mk_stind ([ "stind"; "i" ], DT_I)
+            yield mk_stind ([ "stind"; "u1" ], DT_I1)
+            yield mk_stind ([ "stind"; "i1" ], DT_I1)
+            yield mk_stind ([ "stind"; "u2" ], DT_I2)
+            yield mk_stind ([ "stind"; "i2" ], DT_I2)
+            yield mk_stind ([ "stind"; "u4" ], DT_I4)
+            yield mk_stind ([ "stind"; "i4" ], DT_I4)
+            yield mk_stind ([ "stind"; "u8" ], DT_I8)
+            yield mk_stind ([ "stind"; "i8" ], DT_I8)
+            yield mk_stind ([ "stind"; "r4" ], DT_R4)
+            yield mk_stind ([ "stind"; "r8" ], DT_R8)
+            yield mk_stind ([ "stind"; "ref" ], DT_REF)
+            yield mk_ldind ([ "ldind"; "i" ], DT_I)
+            yield mk_ldind ([ "ldind"; "i1" ], DT_I1)
+            yield mk_ldind ([ "ldind"; "i2" ], DT_I2)
+            yield mk_ldind ([ "ldind"; "i4" ], DT_I4)
+            yield mk_ldind ([ "ldind"; "i8" ], DT_I8)
+            yield mk_ldind ([ "ldind"; "u1" ], DT_U1)
+            yield mk_ldind ([ "ldind"; "u2" ], DT_U2)
+            yield mk_ldind ([ "ldind"; "u4" ], DT_U4)
+            yield mk_ldind ([ "ldind"; "u8" ], DT_I8)
+            yield mk_ldind ([ "ldind"; "r4" ], DT_R4)
+            yield mk_ldind ([ "ldind"; "r8" ], DT_R8)
+            yield mk_ldind ([ "ldind"; "ref" ], DT_REF)
+            yield [ "cpblk" ], (fun () -> I_cpblk(Aligned, Nonvolatile))
+            yield [ "initblk" ], (fun () -> I_initblk(Aligned, Nonvolatile))
+        ]
 
 /// Table of parsing and pretty printing data for instructions.
-let Int64Instrs : Lazy> =
-    lazy [
-        ["ldc";"i8"], (fun x -> AI_ldc (DT_I8, ILConst.I8 x)) 
-    ] 
+let Int64Instrs: Lazy> =
+    lazy [ [ "ldc"; "i8" ], (fun x -> AI_ldc(DT_I8, ILConst.I8 x)) ]
 
 /// Table of parsing and pretty printing data for instructions.
-let Int32Instrs : Lazy> =
-    lazy [ 
-        ["ldc";"i4"], mkLdcInt32
-        ["ldc";"i4";"s"], mkLdcInt32 
-    ] 
+let Int32Instrs: Lazy> =
+    lazy [ [ "ldc"; "i4" ], mkLdcInt32; [ "ldc"; "i4"; "s" ], mkLdcInt32 ]
 
 /// Table of parsing and pretty printing data for instructions.
-let Int32Int32Instrs : Lazy> =
-    lazy [
-        ["ldlen";"multi"], EI_ldlen_multi
-    ]
+let Int32Int32Instrs: Lazy> =
+    lazy [ [ "ldlen"; "multi" ], EI_ldlen_multi ]
 
 /// Table of parsing and pretty printing data for instructions.
-let DoubleInstrs : Lazy> =
-    lazy [
-        ["ldc";"r4"], (fun x -> (AI_ldc (DT_R4, x)))
-        ["ldc";"r8"], (fun x -> (AI_ldc (DT_R8, x))) 
-    ]
+let DoubleInstrs: Lazy> =
+    lazy
+        [
+            [ "ldc"; "r4" ], (fun x -> (AI_ldc(DT_R4, x)))
+            [ "ldc"; "r8" ], (fun x -> (AI_ldc(DT_R8, x)))
+        ]
 
 /// Table of parsing and pretty printing data for instructions.
-let StringInstrs : Lazy> =
-    lazy [
-        ["ldstr"], I_ldstr
-    ] 
+let StringInstrs: Lazy> = lazy [ [ "ldstr" ], I_ldstr ]
 
 /// Table of parsing and pretty printing data for instructions.
-let TokenInstrs : Lazy> =
-    lazy [
-        ["ldtoken"], I_ldtoken
-    ]
+let TokenInstrs: Lazy> = lazy [ [ "ldtoken" ], I_ldtoken ]
 
 /// Table of parsing and pretty printing data for instructions.
-let TypeInstrs : Lazy> =
-    lazy [
-        ["ldelema"], (fun x -> I_ldelema (NormalAddress, false, ILArrayShape.SingleDimensional, x))
-        ["ldelem";"any"], (fun x -> I_ldelem_any (ILArrayShape.SingleDimensional, x))
-        ["stelem";"any"], (fun x -> I_stelem_any (ILArrayShape.SingleDimensional, x))
-        ["newarr"], (fun x -> I_newarr (ILArrayShape.SingleDimensional, x))
-        ["castclass"], I_castclass
-        ["ilzero"], EI_ilzero
-        ["isinst"], I_isinst
-        ["initobj";"any"], I_initobj
-        ["unbox";"any"], I_unbox_any
-    ] 
+let TypeInstrs: Lazy> =
+    lazy
+        [
+            [ "ldelema" ], (fun x -> I_ldelema(NormalAddress, false, ILArrayShape.SingleDimensional, x))
+            [ "ldelem"; "any" ], (fun x -> I_ldelem_any(ILArrayShape.SingleDimensional, x))
+            [ "stelem"; "any" ], (fun x -> I_stelem_any(ILArrayShape.SingleDimensional, x))
+            [ "newarr" ], (fun x -> I_newarr(ILArrayShape.SingleDimensional, x))
+            [ "castclass" ], I_castclass
+            [ "ilzero" ], EI_ilzero
+            [ "isinst" ], I_isinst
+            [ "initobj"; "any" ], I_initobj
+            [ "unbox"; "any" ], I_unbox_any
+        ]
 
 /// Table of parsing and pretty printing data for instructions.
-let IntTypeInstrs : Lazy> =
-    lazy [
-        ["ldelem";"multi"], (fun (x, y) -> (I_ldelem_any (ILArrayShape.FromRank x, y)))
-        ["stelem";"multi"], (fun (x, y) -> (I_stelem_any (ILArrayShape.FromRank x, y)))
-        ["newarr";"multi"], (fun (x, y) -> (I_newarr (ILArrayShape.FromRank x, y)))
-        ["ldelema";"multi"], (fun (x, y) -> (I_ldelema (NormalAddress, false, ILArrayShape.FromRank x, y))) 
-    ] 
+let IntTypeInstrs: Lazy> =
+    lazy
+        [
+            [ "ldelem"; "multi" ], (fun (x, y) -> (I_ldelem_any(ILArrayShape.FromRank x, y)))
+            [ "stelem"; "multi" ], (fun (x, y) -> (I_stelem_any(ILArrayShape.FromRank x, y)))
+            [ "newarr"; "multi" ], (fun (x, y) -> (I_newarr(ILArrayShape.FromRank x, y)))
+            [ "ldelema"; "multi" ], (fun (x, y) -> (I_ldelema(NormalAddress, false, ILArrayShape.FromRank x, y)))
+        ]
 
 /// Table of parsing and pretty printing data for instructions.
-let ValueTypeInstrs : Lazy> =
-    lazy [ 
-        ["cpobj"], I_cpobj
-        ["initobj"], I_initobj
-        ["ldobj"], (fun z -> I_ldobj (Aligned, Nonvolatile, z))
-        ["stobj"], (fun z -> I_stobj (Aligned, Nonvolatile, z))
-        ["sizeof"], I_sizeof
-        ["box"], I_box
-        ["unbox"], I_unbox 
-    ] 
-
+let ValueTypeInstrs: Lazy> =
+    lazy
+        [
+            [ "cpobj" ], I_cpobj
+            [ "initobj" ], I_initobj
+            [ "ldobj" ], (fun z -> I_ldobj(Aligned, Nonvolatile, z))
+            [ "stobj" ], (fun z -> I_stobj(Aligned, Nonvolatile, z))
+            [ "sizeof" ], I_sizeof
+            [ "box" ], I_box
+            [ "unbox" ], I_unbox
+        ]
diff --git a/src/Compiler/AbstractIL/ilbinary.fs b/src/Compiler/AbstractIL/ilbinary.fs
index 18f15bc62ac..f07c714bb37 100644
--- a/src/Compiler/AbstractIL/ilbinary.fs
+++ b/src/Compiler/AbstractIL/ilbinary.fs
@@ -1,283 +1,313 @@
 // Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information.
 
-module internal FSharp.Compiler.AbstractIL.BinaryConstants 
+module internal FSharp.Compiler.AbstractIL.BinaryConstants
 
-open FSharp.Compiler.AbstractIL.IL 
+open FSharp.Compiler.AbstractIL.IL
 open Internal.Utilities.Library
 
 []
-type TableName(idx: int) = 
+type TableName(idx: int) =
     member x.Index = idx
-    static member FromIndex n = TableName n 
-
-module TableNames = 
-    let Module               = TableName 0  
-    let TypeRef              = TableName 1  
-    let TypeDef              = TableName 2  
-    let FieldPtr             = TableName 3  
-    let Field                = TableName 4  
-    let MethodPtr            = TableName 5  
-    let Method               = TableName 6  
-    let ParamPtr             = TableName 7  
-    let Param                = TableName 8  
-    let InterfaceImpl        = TableName 9  
-    let MemberRef            = TableName 10 
-    let Constant             = TableName 11 
-    let CustomAttribute      = TableName 12 
-    let FieldMarshal         = TableName 13 
-    let Permission           = TableName 14 
-    let ClassLayout          = TableName 15 
-    let FieldLayout          = TableName 16 
-    let StandAloneSig        = TableName 17 
-    let EventMap             = TableName 18 
-    let EventPtr             = TableName 19 
-    let Event                = TableName 20 
-    let PropertyMap          = TableName 21 
-    let PropertyPtr          = TableName 22 
-    let Property             = TableName 23 
-    let MethodSemantics      = TableName 24 
-    let MethodImpl           = TableName 25 
-    let ModuleRef            = TableName 26 
-    let TypeSpec             = TableName 27 
-    let ImplMap              = TableName 28 
-    let FieldRVA             = TableName 29 
-    let ENCLog               = TableName 30 
-    let ENCMap               = TableName 31 
-    let Assembly             = TableName 32 
-    let AssemblyProcessor    = TableName 33 
-    let AssemblyOS           = TableName 34 
-    let AssemblyRef          = TableName 35 
-    let AssemblyRefProcessor = TableName 36 
-    let AssemblyRefOS        = TableName 37 
-    let File                 = TableName 38 
-    let ExportedType         = TableName 39 
-    let ManifestResource     = TableName 40 
-    let Nested               = TableName 41 
-    let GenericParam           = TableName 42 
-    let MethodSpec           = TableName 43 
+    static member FromIndex n = TableName n
+
+module TableNames =
+    let Module = TableName 0
+    let TypeRef = TableName 1
+    let TypeDef = TableName 2
+    let FieldPtr = TableName 3
+    let Field = TableName 4
+    let MethodPtr = TableName 5
+    let Method = TableName 6
+    let ParamPtr = TableName 7
+    let Param = TableName 8
+    let InterfaceImpl = TableName 9
+    let MemberRef = TableName 10
+    let Constant = TableName 11
+    let CustomAttribute = TableName 12
+    let FieldMarshal = TableName 13
+    let Permission = TableName 14
+    let ClassLayout = TableName 15
+    let FieldLayout = TableName 16
+    let StandAloneSig = TableName 17
+    let EventMap = TableName 18
+    let EventPtr = TableName 19
+    let Event = TableName 20
+    let PropertyMap = TableName 21
+    let PropertyPtr = TableName 22
+    let Property = TableName 23
+    let MethodSemantics = TableName 24
+    let MethodImpl = TableName 25
+    let ModuleRef = TableName 26
+    let TypeSpec = TableName 27
+    let ImplMap = TableName 28
+    let FieldRVA = TableName 29
+    let ENCLog = TableName 30
+    let ENCMap = TableName 31
+    let Assembly = TableName 32
+    let AssemblyProcessor = TableName 33
+    let AssemblyOS = TableName 34
+    let AssemblyRef = TableName 35
+    let AssemblyRefProcessor = TableName 36
+    let AssemblyRefOS = TableName 37
+    let File = TableName 38
+    let ExportedType = TableName 39
+    let ManifestResource = TableName 40
+    let Nested = TableName 41
+    let GenericParam = TableName 42
+    let MethodSpec = TableName 43
     let GenericParamConstraint = TableName 44
 
-    let UserStrings           = TableName 0x70 (* Special encoding of embedded UserString tokens - See 1.9 Partition III *) 
+    let UserStrings =
+        TableName 0x70 (* Special encoding of embedded UserString tokens - See 1.9 Partition III *)
 
-/// Which tables are sorted and by which column. 
+/// Which tables are sorted and by which column.
 //
-// Sorted bit-vector as stored by CLR V1: 00fa 0133 0002 0000 
-// But what does this mean?  The ECMA spec does not say! 
-// Metainfo -schema reports sorting as shown below. 
-// But some sorting, e.g. EventMap does not seem to show 
-let sortedTableInfo = 
-  [ (TableNames.InterfaceImpl,0) 
-    (TableNames.Constant, 1)
-    (TableNames.CustomAttribute, 0)
-    (TableNames.FieldMarshal, 0)
-    (TableNames.Permission, 1)
-    (TableNames.ClassLayout, 2)
-    (TableNames.FieldLayout, 1)
-    (TableNames.MethodSemantics, 2)
-    (TableNames.MethodImpl, 0)
-    (TableNames.ImplMap, 1)
-    (TableNames.FieldRVA, 1)
-    (TableNames.Nested, 0)
-    (TableNames.GenericParam, 2) 
-    (TableNames.GenericParamConstraint, 0) ]
-    
+// Sorted bit-vector as stored by CLR V1: 00fa 0133 0002 0000
+// But what does this mean?  The ECMA spec does not say!
+// Metainfo -schema reports sorting as shown below.
+// But some sorting, e.g. EventMap does not seem to show
+let sortedTableInfo =
+    [
+        (TableNames.InterfaceImpl, 0)
+        (TableNames.Constant, 1)
+        (TableNames.CustomAttribute, 0)
+        (TableNames.FieldMarshal, 0)
+        (TableNames.Permission, 1)
+        (TableNames.ClassLayout, 2)
+        (TableNames.FieldLayout, 1)
+        (TableNames.MethodSemantics, 2)
+        (TableNames.MethodImpl, 0)
+        (TableNames.ImplMap, 1)
+        (TableNames.FieldRVA, 1)
+        (TableNames.Nested, 0)
+        (TableNames.GenericParam, 2)
+        (TableNames.GenericParamConstraint, 0)
+    ]
+
 []
-type TypeDefOrRefTag(tag: int32) = member x.Tag = tag
+type TypeDefOrRefTag(tag: int32) =
+    member x.Tag = tag
+
 let tdor_TypeDef = TypeDefOrRefTag 0x00
 let tdor_TypeRef = TypeDefOrRefTag 0x01
 let tdor_TypeSpec = TypeDefOrRefTag 0x2
-let mkTypeDefOrRefOrSpecTag x = 
-    match x with 
-    | 0x00 -> tdor_TypeDef // nb. avoid reallocation 
+
+let mkTypeDefOrRefOrSpecTag x =
+    match x with
+    | 0x00 -> tdor_TypeDef // nb. avoid reallocation
     | 0x01 -> tdor_TypeRef
     | 0x02 -> tdor_TypeSpec
     | _ -> invalidArg "x" "mkTypeDefOrRefOrSpecTag"
 
 []
-type HasConstantTag(tag: int32) = member x.Tag = tag
-let hc_FieldDef  = HasConstantTag 0x0
-let hc_ParamDef  = HasConstantTag 0x1
+type HasConstantTag(tag: int32) =
+    member x.Tag = tag
+
+let hc_FieldDef = HasConstantTag 0x0
+let hc_ParamDef = HasConstantTag 0x1
 let hc_Property = HasConstantTag 0x2
 
-let mkHasConstantTag x = 
-    match x with 
+let mkHasConstantTag x =
+    match x with
     | 0x00 -> hc_FieldDef
     | 0x01 -> hc_ParamDef
     | 0x02 -> hc_Property
     | _ -> invalidArg "x" "mkHasConstantTag"
 
 []
-type HasCustomAttributeTag(tag: int32) = member x.Tag = tag
-let hca_MethodDef       = HasCustomAttributeTag 0x0
-let hca_FieldDef        = HasCustomAttributeTag 0x1
-let hca_TypeRef         = HasCustomAttributeTag 0x2
-let hca_TypeDef         = HasCustomAttributeTag 0x3
-let hca_ParamDef        = HasCustomAttributeTag 0x4
-let hca_InterfaceImpl   = HasCustomAttributeTag 0x5
-let hca_MemberRef       = HasCustomAttributeTag 0x6
-let hca_Module          = HasCustomAttributeTag 0x7
-let hca_Permission      = HasCustomAttributeTag 0x8
-let hca_Property        = HasCustomAttributeTag 0x9
-let hca_Event           = HasCustomAttributeTag 0xa
-let hca_StandAloneSig   = HasCustomAttributeTag 0xb
-let hca_ModuleRef       = HasCustomAttributeTag 0xc
-let hca_TypeSpec        = HasCustomAttributeTag 0xd
-let hca_Assembly        = HasCustomAttributeTag 0xe
-let hca_AssemblyRef     = HasCustomAttributeTag 0xf
-let hca_File            = HasCustomAttributeTag 0x10
-let hca_ExportedType    = HasCustomAttributeTag 0x11
-let hca_ManifestResource        = HasCustomAttributeTag 0x12
-let hca_GenericParam            = HasCustomAttributeTag 0x13
-let hca_GenericParamConstraint  = HasCustomAttributeTag 0x14
-let hca_MethodSpec              = HasCustomAttributeTag 0x15
-
-let mkHasCustomAttributeTag x = 
-    match x with 
-    | 0x00 -> hca_MethodDef  
-    | 0x01 -> hca_FieldDef 
-    | 0x02 -> hca_TypeRef  
-    | 0x03 -> hca_TypeDef 
-    | 0x04 -> hca_ParamDef 
-    | 0x05 -> hca_InterfaceImpl 
-    | 0x06 -> hca_MemberRef 
-    | 0x07 -> hca_Module 
-    | 0x08 -> hca_Permission 
-    | 0x09 -> hca_Property 
-    | 0x0a -> hca_Event 
-    | 0x0b -> hca_StandAloneSig 
-    | 0x0c -> hca_ModuleRef 
-    | 0x0d -> hca_TypeSpec 
-    | 0x0e -> hca_Assembly 
-    | 0x0f -> hca_AssemblyRef 
-    | 0x10 -> hca_File 
-    | 0x11 -> hca_ExportedType 
+type HasCustomAttributeTag(tag: int32) =
+    member x.Tag = tag
+
+let hca_MethodDef = HasCustomAttributeTag 0x0
+let hca_FieldDef = HasCustomAttributeTag 0x1
+let hca_TypeRef = HasCustomAttributeTag 0x2
+let hca_TypeDef = HasCustomAttributeTag 0x3
+let hca_ParamDef = HasCustomAttributeTag 0x4
+let hca_InterfaceImpl = HasCustomAttributeTag 0x5
+let hca_MemberRef = HasCustomAttributeTag 0x6
+let hca_Module = HasCustomAttributeTag 0x7
+let hca_Permission = HasCustomAttributeTag 0x8
+let hca_Property = HasCustomAttributeTag 0x9
+let hca_Event = HasCustomAttributeTag 0xa
+let hca_StandAloneSig = HasCustomAttributeTag 0xb
+let hca_ModuleRef = HasCustomAttributeTag 0xc
+let hca_TypeSpec = HasCustomAttributeTag 0xd
+let hca_Assembly = HasCustomAttributeTag 0xe
+let hca_AssemblyRef = HasCustomAttributeTag 0xf
+let hca_File = HasCustomAttributeTag 0x10
+let hca_ExportedType = HasCustomAttributeTag 0x11
+let hca_ManifestResource = HasCustomAttributeTag 0x12
+let hca_GenericParam = HasCustomAttributeTag 0x13
+let hca_GenericParamConstraint = HasCustomAttributeTag 0x14
+let hca_MethodSpec = HasCustomAttributeTag 0x15
+
+let mkHasCustomAttributeTag x =
+    match x with
+    | 0x00 -> hca_MethodDef
+    | 0x01 -> hca_FieldDef
+    | 0x02 -> hca_TypeRef
+    | 0x03 -> hca_TypeDef
+    | 0x04 -> hca_ParamDef
+    | 0x05 -> hca_InterfaceImpl
+    | 0x06 -> hca_MemberRef
+    | 0x07 -> hca_Module
+    | 0x08 -> hca_Permission
+    | 0x09 -> hca_Property
+    | 0x0a -> hca_Event
+    | 0x0b -> hca_StandAloneSig
+    | 0x0c -> hca_ModuleRef
+    | 0x0d -> hca_TypeSpec
+    | 0x0e -> hca_Assembly
+    | 0x0f -> hca_AssemblyRef
+    | 0x10 -> hca_File
+    | 0x11 -> hca_ExportedType
     | 0x12 -> hca_ManifestResource
     | 0x13 -> hca_GenericParam
     | 0x14 -> hca_GenericParamConstraint
-    | 0x15 -> hca_MethodSpec 
+    | 0x15 -> hca_MethodSpec
     | _ -> HasCustomAttributeTag x
 
 []
-type HasFieldMarshalTag(tag: int32) = member x.Tag = tag
-let hfm_FieldDef =  HasFieldMarshalTag 0x00
-let hfm_ParamDef =  HasFieldMarshalTag 0x01
-
-let mkHasFieldMarshalTag x = 
-    match x with 
-    | 0x00 -> hfm_FieldDef 
-    | 0x01 -> hfm_ParamDef 
+type HasFieldMarshalTag(tag: int32) =
+    member x.Tag = tag
+
+let hfm_FieldDef = HasFieldMarshalTag 0x00
+let hfm_ParamDef = HasFieldMarshalTag 0x01
+
+let mkHasFieldMarshalTag x =
+    match x with
+    | 0x00 -> hfm_FieldDef
+    | 0x01 -> hfm_ParamDef
     | _ -> HasFieldMarshalTag x
 
 []
-type HasDeclSecurityTag(tag: int32) = member x.Tag = tag
-let hds_TypeDef =  HasDeclSecurityTag 0x00
-let hds_MethodDef =  HasDeclSecurityTag 0x01
-let hds_Assembly =  HasDeclSecurityTag 0x02
-
-let mkHasDeclSecurityTag x = 
-    match x with 
-    | 0x00 -> hds_TypeDef 
-    | 0x01 -> hds_MethodDef 
-    | 0x02 -> hds_Assembly 
+type HasDeclSecurityTag(tag: int32) =
+    member x.Tag = tag
+
+let hds_TypeDef = HasDeclSecurityTag 0x00
+let hds_MethodDef = HasDeclSecurityTag 0x01
+let hds_Assembly = HasDeclSecurityTag 0x02
+
+let mkHasDeclSecurityTag x =
+    match x with
+    | 0x00 -> hds_TypeDef
+    | 0x01 -> hds_MethodDef
+    | 0x02 -> hds_Assembly
     | _ -> HasDeclSecurityTag x
 
 []
-type MemberRefParentTag(tag: int32) = member x.Tag = tag
+type MemberRefParentTag(tag: int32) =
+    member x.Tag = tag
+
 let mrp_TypeRef = MemberRefParentTag 0x01
 let mrp_ModuleRef = MemberRefParentTag 0x02
 let mrp_MethodDef = MemberRefParentTag 0x03
-let mrp_TypeSpec  = MemberRefParentTag 0x04
-
-let mkMemberRefParentTag x = 
-    match x with 
-    | 0x01 -> mrp_TypeRef 
-    | 0x02 -> mrp_ModuleRef 
-    | 0x03 -> mrp_MethodDef 
-    | 0x04 -> mrp_TypeSpec  
+let mrp_TypeSpec = MemberRefParentTag 0x04
+
+let mkMemberRefParentTag x =
+    match x with
+    | 0x01 -> mrp_TypeRef
+    | 0x02 -> mrp_ModuleRef
+    | 0x03 -> mrp_MethodDef
+    | 0x04 -> mrp_TypeSpec
     | _ -> MemberRefParentTag x
 
 []
-type HasSemanticsTag(tag: int32) = member x.Tag = tag
-let hs_Event =  HasSemanticsTag 0x00
-let hs_Property =  HasSemanticsTag 0x01
-
-let mkHasSemanticsTag x = 
-    match x with 
-    | 0x00 -> hs_Event 
-    | 0x01 -> hs_Property 
+type HasSemanticsTag(tag: int32) =
+    member x.Tag = tag
+
+let hs_Event = HasSemanticsTag 0x00
+let hs_Property = HasSemanticsTag 0x01
+
+let mkHasSemanticsTag x =
+    match x with
+    | 0x00 -> hs_Event
+    | 0x01 -> hs_Property
     | _ -> HasSemanticsTag x
 
 []
-type MethodDefOrRefTag(tag: int32) = member x.Tag = tag
-let mdor_MethodDef =  MethodDefOrRefTag 0x00
-let mdor_MemberRef =  MethodDefOrRefTag 0x01
-let mdor_MethodSpec =  MethodDefOrRefTag 0x02
-
-let mkMethodDefOrRefTag x = 
-    match x with 
-    | 0x00 -> mdor_MethodDef 
-    | 0x01 -> mdor_MemberRef 
-    | 0x02 -> mdor_MethodSpec 
+type MethodDefOrRefTag(tag: int32) =
+    member x.Tag = tag
+
+let mdor_MethodDef = MethodDefOrRefTag 0x00
+let mdor_MemberRef = MethodDefOrRefTag 0x01
+let mdor_MethodSpec = MethodDefOrRefTag 0x02
+
+let mkMethodDefOrRefTag x =
+    match x with
+    | 0x00 -> mdor_MethodDef
+    | 0x01 -> mdor_MemberRef
+    | 0x02 -> mdor_MethodSpec
     | _ -> MethodDefOrRefTag x
 
 []
-type MemberForwardedTag(tag: int32) = member x.Tag = tag
-let mf_FieldDef =  MemberForwardedTag 0x00
-let mf_MethodDef =  MemberForwardedTag 0x01
-
-let mkMemberForwardedTag x = 
-    match x with 
-    | 0x00 -> mf_FieldDef 
-    | 0x01 -> mf_MethodDef 
+type MemberForwardedTag(tag: int32) =
+    member x.Tag = tag
+
+let mf_FieldDef = MemberForwardedTag 0x00
+let mf_MethodDef = MemberForwardedTag 0x01
+
+let mkMemberForwardedTag x =
+    match x with
+    | 0x00 -> mf_FieldDef
+    | 0x01 -> mf_MethodDef
     | _ -> MemberForwardedTag x
 
 []
-type ImplementationTag(tag: int32) = member x.Tag = tag
-let i_File =  ImplementationTag 0x00
-let i_AssemblyRef =  ImplementationTag 0x01
-let i_ExportedType =  ImplementationTag 0x02
-
-let mkImplementationTag x = 
-    match x with 
-    | 0x00 -> i_File 
-    | 0x01 -> i_AssemblyRef 
-    | 0x02 -> i_ExportedType 
+type ImplementationTag(tag: int32) =
+    member x.Tag = tag
+
+let i_File = ImplementationTag 0x00
+let i_AssemblyRef = ImplementationTag 0x01
+let i_ExportedType = ImplementationTag 0x02
+
+let mkImplementationTag x =
+    match x with
+    | 0x00 -> i_File
+    | 0x01 -> i_AssemblyRef
+    | 0x02 -> i_ExportedType
     | _ -> ImplementationTag x
 
 []
-type CustomAttributeTypeTag(tag: int32) = member x.Tag = tag
-let cat_MethodDef =  CustomAttributeTypeTag 0x02
-let cat_MemberRef =  CustomAttributeTypeTag 0x03
-
-let mkILCustomAttributeTypeTag x = 
-    match x with 
-    | 0x02 -> cat_MethodDef 
-    | 0x03 -> cat_MemberRef 
+type CustomAttributeTypeTag(tag: int32) =
+    member x.Tag = tag
+
+let cat_MethodDef = CustomAttributeTypeTag 0x02
+let cat_MemberRef = CustomAttributeTypeTag 0x03
+
+let mkILCustomAttributeTypeTag x =
+    match x with
+    | 0x02 -> cat_MethodDef
+    | 0x03 -> cat_MemberRef
     | _ -> CustomAttributeTypeTag x
 
 []
-type ResolutionScopeTag(tag: int32) = member x.Tag = tag
-let rs_Module =  ResolutionScopeTag 0x00
-let rs_ModuleRef =  ResolutionScopeTag 0x01
-let rs_AssemblyRef  =  ResolutionScopeTag 0x02
-let rs_TypeRef =  ResolutionScopeTag 0x03
-
-let mkResolutionScopeTag x = 
-    match x with 
-    | 0x00 -> rs_Module 
-    | 0x01 -> rs_ModuleRef 
-    | 0x02 -> rs_AssemblyRef  
-    | 0x03 -> rs_TypeRef 
+type ResolutionScopeTag(tag: int32) =
+    member x.Tag = tag
+
+let rs_Module = ResolutionScopeTag 0x00
+let rs_ModuleRef = ResolutionScopeTag 0x01
+let rs_AssemblyRef = ResolutionScopeTag 0x02
+let rs_TypeRef = ResolutionScopeTag 0x03
+
+let mkResolutionScopeTag x =
+    match x with
+    | 0x00 -> rs_Module
+    | 0x01 -> rs_ModuleRef
+    | 0x02 -> rs_AssemblyRef
+    | 0x03 -> rs_TypeRef
     | _ -> ResolutionScopeTag x
 
 []
-type TypeOrMethodDefTag(tag: int32) = member x.Tag = tag
+type TypeOrMethodDefTag(tag: int32) =
+    member x.Tag = tag
+
 let tomd_TypeDef = TypeOrMethodDefTag 0x00
 let tomd_MethodDef = TypeOrMethodDefTag 0x01
 
-let mkTypeOrMethodDefTag x = 
-    match x with 
-    | 0x00 -> tomd_TypeDef 
+let mkTypeOrMethodDefTag x =
+    match x with
+    | 0x00 -> tomd_TypeDef
     | 0x01 -> tomd_MethodDef
     | _ -> TypeOrMethodDefTag x
 
@@ -298,544 +328,546 @@ let et_R8 = 0x0Duy
 let et_STRING = 0x0Euy
 let et_PTR = 0x0Fuy
 let et_BYREF = 0x10uy
-let et_VALUETYPE      = 0x11uy
-let et_CLASS          = 0x12uy
-let et_VAR            = 0x13uy
-let et_ARRAY          = 0x14uy
-let et_WITH           = 0x15uy
-let et_TYPEDBYREF     = 0x16uy
-let et_I              = 0x18uy
-let et_U              = 0x19uy
-let et_FNPTR          = 0x1Buy
-let et_OBJECT         = 0x1Cuy
-let et_SZARRAY        = 0x1Duy
-let et_MVAR           = 0x1euy
-let et_CMOD_REQD      = 0x1Fuy
-let et_CMOD_OPT       = 0x20uy
-
-let et_SENTINEL       = 0x41uy // sentinel for varargs 
-let et_PINNED         = 0x45uy
-
-
-let i_nop           = 0x00 
-let i_break         = 0x01 
-let i_ldarg_0       = 0x02 
-let i_ldarg_1       = 0x03 
-let i_ldarg_2       = 0x04 
-let i_ldarg_3       = 0x05 
-let i_ldloc_0       = 0x06 
-let i_ldloc_1       = 0x07 
-let i_ldloc_2       = 0x08 
-let i_ldloc_3       = 0x09 
-let i_stloc_0       = 0x0a 
-let i_stloc_1       = 0x0b 
-let i_stloc_2       = 0x0c 
-let i_stloc_3       = 0x0d 
-let i_ldarg_s       = 0x0e 
-let i_ldarga_s      = 0x0f 
-let i_starg_s       = 0x10 
-let i_ldloc_s       = 0x11 
-let i_ldloca_s      = 0x12 
-let i_stloc_s       = 0x13 
-let i_ldnull        = 0x14 
-let i_ldc_i4_m1     = 0x15 
-let i_ldc_i4_0      = 0x16 
-let i_ldc_i4_1      = 0x17 
-let i_ldc_i4_2      = 0x18 
-let i_ldc_i4_3      = 0x19 
-let i_ldc_i4_4      = 0x1a 
-let i_ldc_i4_5      = 0x1b 
-let i_ldc_i4_6      = 0x1c 
-let i_ldc_i4_7      = 0x1d 
-let i_ldc_i4_8      = 0x1e 
-let i_ldc_i4_s      = 0x1f 
-let i_ldc_i4        = 0x20 
-let i_ldc_i8        = 0x21 
-let i_ldc_r4        = 0x22 
-let i_ldc_r8        = 0x23 
-let i_dup           = 0x25 
-let i_pop           = 0x26 
-let i_jmp           = 0x27 
-let i_call          = 0x28 
-let i_calli         = 0x29 
-let i_ret           = 0x2a 
-let i_br_s          = 0x2b 
-let i_brfalse_s     = 0x2c 
-let i_brtrue_s      = 0x2d 
-let i_beq_s         = 0x2e 
-let i_bge_s         = 0x2f 
-let i_bgt_s         = 0x30 
-let i_ble_s         = 0x31 
-let i_blt_s         = 0x32 
-let i_bne_un_s      = 0x33 
-let i_bge_un_s      = 0x34 
-let i_bgt_un_s      = 0x35 
-let i_ble_un_s      = 0x36 
-let i_blt_un_s      = 0x37 
-let i_br            = 0x38 
-let i_brfalse       = 0x39 
-let i_brtrue        = 0x3a 
-let i_beq           = 0x3b 
-let i_bge           = 0x3c 
-let i_bgt           = 0x3d 
-let i_ble           = 0x3e 
-let i_blt           = 0x3f 
-let i_bne_un        = 0x40 
-let i_bge_un        = 0x41 
-let i_bgt_un        = 0x42 
-let i_ble_un        = 0x43 
-let i_blt_un        = 0x44 
-let i_switch        = 0x45 
-let i_ldind_i1      = 0x46 
-let i_ldind_u1      = 0x47 
-let i_ldind_i2      = 0x48 
-let i_ldind_u2      = 0x49 
-let i_ldind_i4      = 0x4a 
-let i_ldind_u4      = 0x4b 
-let i_ldind_i8      = 0x4c 
-let i_ldind_i       = 0x4d 
-let i_ldind_r4      = 0x4e 
-let i_ldind_r8      = 0x4f 
-let i_ldind_ref     = 0x50 
-let i_stind_ref     = 0x51 
-let i_stind_i1      = 0x52 
-let i_stind_i2      = 0x53 
-let i_stind_i4      = 0x54 
-let i_stind_i8      = 0x55 
-let i_stind_r4      = 0x56 
-let i_stind_r8      = 0x57 
-let i_add           = 0x58 
-let i_sub           = 0x59 
-let i_mul           = 0x5a 
-let i_div           = 0x5b 
-let i_div_un        = 0x5c 
-let i_rem           = 0x5d 
-let i_rem_un        = 0x5e 
-let i_and           = 0x5f 
-let i_or            = 0x60 
-let i_xor           = 0x61 
-let i_shl           = 0x62 
-let i_shr           = 0x63 
-let i_shr_un        = 0x64 
-let i_neg           = 0x65 
-let i_not           = 0x66 
-let i_conv_i1       = 0x67 
-let i_conv_i2       = 0x68 
-let i_conv_i4       = 0x69 
-let i_conv_i8       = 0x6a 
-let i_conv_r4       = 0x6b 
-let i_conv_r8       = 0x6c 
-let i_conv_u4       = 0x6d 
-let i_conv_u8       = 0x6e 
-let i_callvirt      = 0x6f 
-let i_cpobj         = 0x70 
-let i_ldobj         = 0x71 
-let i_ldstr         = 0x72 
-let i_newobj        = 0x73 
-let i_castclass     = 0x74 
-let i_isinst        = 0x75 
-let i_conv_r_un     = 0x76 
-let i_unbox         = 0x79 
-let i_throw         = 0x7a 
-let i_ldfld         = 0x7b 
-let i_ldflda        = 0x7c 
-let i_stfld         = 0x7d 
-let i_ldsfld        = 0x7e 
-let i_ldsflda       = 0x7f 
-let i_stsfld        = 0x80 
-let i_stobj         = 0x81 
-let i_conv_ovf_i1_un= 0x82 
-let i_conv_ovf_i2_un= 0x83 
-let i_conv_ovf_i4_un= 0x84 
-let i_conv_ovf_i8_un= 0x85 
-let i_conv_ovf_u1_un= 0x86 
-let i_conv_ovf_u2_un= 0x87 
-let i_conv_ovf_u4_un= 0x88 
-let i_conv_ovf_u8_un= 0x89 
-let i_conv_ovf_i_un = 0x8a 
-let i_conv_ovf_u_un = 0x8b 
-let i_box           = 0x8c 
-let i_newarr        = 0x8d 
-let i_ldlen         = 0x8e 
-let i_ldelema       = 0x8f 
-let i_ldelem_i1     = 0x90 
-let i_ldelem_u1     = 0x91 
-let i_ldelem_i2     = 0x92 
-let i_ldelem_u2     = 0x93 
-let i_ldelem_i4     = 0x94 
-let i_ldelem_u4     = 0x95 
-let i_ldelem_i8     = 0x96 
-let i_ldelem_i      = 0x97 
-let i_ldelem_r4     = 0x98 
-let i_ldelem_r8     = 0x99 
-let i_ldelem_ref    = 0x9a 
-let i_stelem_i      = 0x9b 
-let i_stelem_i1     = 0x9c 
-let i_stelem_i2     = 0x9d 
-let i_stelem_i4     = 0x9e 
-let i_stelem_i8     = 0x9f 
-let i_stelem_r4     = 0xa0 
-let i_stelem_r8     = 0xa1 
-let i_stelem_ref    = 0xa2 
-let i_conv_ovf_i1   = 0xb3 
-let i_conv_ovf_u1   = 0xb4 
-let i_conv_ovf_i2   = 0xb5 
-let i_conv_ovf_u2   = 0xb6 
-let i_conv_ovf_i4   = 0xb7 
-let i_conv_ovf_u4   = 0xb8 
-let i_conv_ovf_i8   = 0xb9 
-let i_conv_ovf_u8   = 0xba 
-let i_refanyval     = 0xc2 
-let i_ckfinite      = 0xc3 
-let i_mkrefany      = 0xc6 
-let i_ldtoken       = 0xd0 
-let i_conv_u2       = 0xd1 
-let i_conv_u1       = 0xd2 
-let i_conv_i        = 0xd3 
-let i_conv_ovf_i    = 0xd4 
-let i_conv_ovf_u    = 0xd5 
-let i_add_ovf       = 0xd6 
-let i_add_ovf_un    = 0xd7 
-let i_mul_ovf       = 0xd8 
-let i_mul_ovf_un    = 0xd9 
-let i_sub_ovf       = 0xda 
-let i_sub_ovf_un    = 0xdb 
-let i_endfinally    = 0xdc 
-let i_leave         = 0xdd 
-let i_leave_s       = 0xde 
-let i_stind_i       = 0xdf 
-let i_conv_u        = 0xe0 
-let i_arglist       = 0xfe00
-let i_ceq           = 0xfe01
-let i_cgt           = 0xfe02
-let i_cgt_un        = 0xfe03
-let i_clt           = 0xfe04
-let i_clt_un        = 0xfe05
-let i_ldftn         = 0xfe06 
-let i_ldvirtftn     = 0xfe07 
-let i_ldarg         = 0xfe09 
-let i_ldarga        = 0xfe0a 
-let i_starg         = 0xfe0b 
-let i_ldloc         = 0xfe0c 
-let i_ldloca        = 0xfe0d 
-let i_stloc         = 0xfe0e 
-let i_localloc      = 0xfe0f 
-let i_endfilter     = 0xfe11 
-let i_unaligned     = 0xfe12 
-let i_volatile      = 0xfe13 
-let i_constrained   = 0xfe16
-let i_readonly      = 0xfe1e
-let i_tail          = 0xfe14 
-let i_initobj       = 0xfe15 
-let i_cpblk         = 0xfe17 
-let i_initblk       = 0xfe18 
-let i_rethrow       = 0xfe1a 
-let i_sizeof        = 0xfe1c 
-let i_refanytype    = 0xfe1d 
-let i_ldelem_any    = 0xa3
-let i_stelem_any    = 0xa4
-let i_unbox_any     = 0xa5
+let et_VALUETYPE = 0x11uy
+let et_CLASS = 0x12uy
+let et_VAR = 0x13uy
+let et_ARRAY = 0x14uy
+let et_WITH = 0x15uy
+let et_TYPEDBYREF = 0x16uy
+let et_I = 0x18uy
+let et_U = 0x19uy
+let et_FNPTR = 0x1Buy
+let et_OBJECT = 0x1Cuy
+let et_SZARRAY = 0x1Duy
+let et_MVAR = 0x1euy
+let et_CMOD_REQD = 0x1Fuy
+let et_CMOD_OPT = 0x20uy
+
+let et_SENTINEL = 0x41uy // sentinel for varargs
+let et_PINNED = 0x45uy
+
+let i_nop = 0x00
+let i_break = 0x01
+let i_ldarg_0 = 0x02
+let i_ldarg_1 = 0x03
+let i_ldarg_2 = 0x04
+let i_ldarg_3 = 0x05
+let i_ldloc_0 = 0x06
+let i_ldloc_1 = 0x07
+let i_ldloc_2 = 0x08
+let i_ldloc_3 = 0x09
+let i_stloc_0 = 0x0a
+let i_stloc_1 = 0x0b
+let i_stloc_2 = 0x0c
+let i_stloc_3 = 0x0d
+let i_ldarg_s = 0x0e
+let i_ldarga_s = 0x0f
+let i_starg_s = 0x10
+let i_ldloc_s = 0x11
+let i_ldloca_s = 0x12
+let i_stloc_s = 0x13
+let i_ldnull = 0x14
+let i_ldc_i4_m1 = 0x15
+let i_ldc_i4_0 = 0x16
+let i_ldc_i4_1 = 0x17
+let i_ldc_i4_2 = 0x18
+let i_ldc_i4_3 = 0x19
+let i_ldc_i4_4 = 0x1a
+let i_ldc_i4_5 = 0x1b
+let i_ldc_i4_6 = 0x1c
+let i_ldc_i4_7 = 0x1d
+let i_ldc_i4_8 = 0x1e
+let i_ldc_i4_s = 0x1f
+let i_ldc_i4 = 0x20
+let i_ldc_i8 = 0x21
+let i_ldc_r4 = 0x22
+let i_ldc_r8 = 0x23
+let i_dup = 0x25
+let i_pop = 0x26
+let i_jmp = 0x27
+let i_call = 0x28
+let i_calli = 0x29
+let i_ret = 0x2a
+let i_br_s = 0x2b
+let i_brfalse_s = 0x2c
+let i_brtrue_s = 0x2d
+let i_beq_s = 0x2e
+let i_bge_s = 0x2f
+let i_bgt_s = 0x30
+let i_ble_s = 0x31
+let i_blt_s = 0x32
+let i_bne_un_s = 0x33
+let i_bge_un_s = 0x34
+let i_bgt_un_s = 0x35
+let i_ble_un_s = 0x36
+let i_blt_un_s = 0x37
+let i_br = 0x38
+let i_brfalse = 0x39
+let i_brtrue = 0x3a
+let i_beq = 0x3b
+let i_bge = 0x3c
+let i_bgt = 0x3d
+let i_ble = 0x3e
+let i_blt = 0x3f
+let i_bne_un = 0x40
+let i_bge_un = 0x41
+let i_bgt_un = 0x42
+let i_ble_un = 0x43
+let i_blt_un = 0x44
+let i_switch = 0x45
+let i_ldind_i1 = 0x46
+let i_ldind_u1 = 0x47
+let i_ldind_i2 = 0x48
+let i_ldind_u2 = 0x49
+let i_ldind_i4 = 0x4a
+let i_ldind_u4 = 0x4b
+let i_ldind_i8 = 0x4c
+let i_ldind_i = 0x4d
+let i_ldind_r4 = 0x4e
+let i_ldind_r8 = 0x4f
+let i_ldind_ref = 0x50
+let i_stind_ref = 0x51
+let i_stind_i1 = 0x52
+let i_stind_i2 = 0x53
+let i_stind_i4 = 0x54
+let i_stind_i8 = 0x55
+let i_stind_r4 = 0x56
+let i_stind_r8 = 0x57
+let i_add = 0x58
+let i_sub = 0x59
+let i_mul = 0x5a
+let i_div = 0x5b
+let i_div_un = 0x5c
+let i_rem = 0x5d
+let i_rem_un = 0x5e
+let i_and = 0x5f
+let i_or = 0x60
+let i_xor = 0x61
+let i_shl = 0x62
+let i_shr = 0x63
+let i_shr_un = 0x64
+let i_neg = 0x65
+let i_not = 0x66
+let i_conv_i1 = 0x67
+let i_conv_i2 = 0x68
+let i_conv_i4 = 0x69
+let i_conv_i8 = 0x6a
+let i_conv_r4 = 0x6b
+let i_conv_r8 = 0x6c
+let i_conv_u4 = 0x6d
+let i_conv_u8 = 0x6e
+let i_callvirt = 0x6f
+let i_cpobj = 0x70
+let i_ldobj = 0x71
+let i_ldstr = 0x72
+let i_newobj = 0x73
+let i_castclass = 0x74
+let i_isinst = 0x75
+let i_conv_r_un = 0x76
+let i_unbox = 0x79
+let i_throw = 0x7a
+let i_ldfld = 0x7b
+let i_ldflda = 0x7c
+let i_stfld = 0x7d
+let i_ldsfld = 0x7e
+let i_ldsflda = 0x7f
+let i_stsfld = 0x80
+let i_stobj = 0x81
+let i_conv_ovf_i1_un = 0x82
+let i_conv_ovf_i2_un = 0x83
+let i_conv_ovf_i4_un = 0x84
+let i_conv_ovf_i8_un = 0x85
+let i_conv_ovf_u1_un = 0x86
+let i_conv_ovf_u2_un = 0x87
+let i_conv_ovf_u4_un = 0x88
+let i_conv_ovf_u8_un = 0x89
+let i_conv_ovf_i_un = 0x8a
+let i_conv_ovf_u_un = 0x8b
+let i_box = 0x8c
+let i_newarr = 0x8d
+let i_ldlen = 0x8e
+let i_ldelema = 0x8f
+let i_ldelem_i1 = 0x90
+let i_ldelem_u1 = 0x91
+let i_ldelem_i2 = 0x92
+let i_ldelem_u2 = 0x93
+let i_ldelem_i4 = 0x94
+let i_ldelem_u4 = 0x95
+let i_ldelem_i8 = 0x96
+let i_ldelem_i = 0x97
+let i_ldelem_r4 = 0x98
+let i_ldelem_r8 = 0x99
+let i_ldelem_ref = 0x9a
+let i_stelem_i = 0x9b
+let i_stelem_i1 = 0x9c
+let i_stelem_i2 = 0x9d
+let i_stelem_i4 = 0x9e
+let i_stelem_i8 = 0x9f
+let i_stelem_r4 = 0xa0
+let i_stelem_r8 = 0xa1
+let i_stelem_ref = 0xa2
+let i_conv_ovf_i1 = 0xb3
+let i_conv_ovf_u1 = 0xb4
+let i_conv_ovf_i2 = 0xb5
+let i_conv_ovf_u2 = 0xb6
+let i_conv_ovf_i4 = 0xb7
+let i_conv_ovf_u4 = 0xb8
+let i_conv_ovf_i8 = 0xb9
+let i_conv_ovf_u8 = 0xba
+let i_refanyval = 0xc2
+let i_ckfinite = 0xc3
+let i_mkrefany = 0xc6
+let i_ldtoken = 0xd0
+let i_conv_u2 = 0xd1
+let i_conv_u1 = 0xd2
+let i_conv_i = 0xd3
+let i_conv_ovf_i = 0xd4
+let i_conv_ovf_u = 0xd5
+let i_add_ovf = 0xd6
+let i_add_ovf_un = 0xd7
+let i_mul_ovf = 0xd8
+let i_mul_ovf_un = 0xd9
+let i_sub_ovf = 0xda
+let i_sub_ovf_un = 0xdb
+let i_endfinally = 0xdc
+let i_leave = 0xdd
+let i_leave_s = 0xde
+let i_stind_i = 0xdf
+let i_conv_u = 0xe0
+let i_arglist = 0xfe00
+let i_ceq = 0xfe01
+let i_cgt = 0xfe02
+let i_cgt_un = 0xfe03
+let i_clt = 0xfe04
+let i_clt_un = 0xfe05
+let i_ldftn = 0xfe06
+let i_ldvirtftn = 0xfe07
+let i_ldarg = 0xfe09
+let i_ldarga = 0xfe0a
+let i_starg = 0xfe0b
+let i_ldloc = 0xfe0c
+let i_ldloca = 0xfe0d
+let i_stloc = 0xfe0e
+let i_localloc = 0xfe0f
+let i_endfilter = 0xfe11
+let i_unaligned = 0xfe12
+let i_volatile = 0xfe13
+let i_constrained = 0xfe16
+let i_readonly = 0xfe1e
+let i_tail = 0xfe14
+let i_initobj = 0xfe15
+let i_cpblk = 0xfe17
+let i_initblk = 0xfe18
+let i_rethrow = 0xfe1a
+let i_sizeof = 0xfe1c
+let i_refanytype = 0xfe1d
+let i_ldelem_any = 0xa3
+let i_stelem_any = 0xa4
+let i_unbox_any = 0xa5
 
 let mk_ldc i = mkLdcInt32 i
 
-let noArgInstrs  = 
-   lazy [ i_ldc_i4_0, mk_ldc 0
-          i_ldc_i4_1, mk_ldc 1
-          i_ldc_i4_2, mk_ldc 2
-          i_ldc_i4_3, mk_ldc 3
-          i_ldc_i4_4, mk_ldc 4
-          i_ldc_i4_5, mk_ldc 5
-          i_ldc_i4_6, mk_ldc 6
-          i_ldc_i4_7, mk_ldc 7
-          i_ldc_i4_8, mk_ldc 8
-          i_ldc_i4_m1, mk_ldc -1
-          0x0a, mkStloc 0us
-          0x0b, mkStloc 1us
-          0x0c, mkStloc 2us
-          0x0d, mkStloc 3us
-          0x06, mkLdloc 0us
-          0x07, mkLdloc 1us
-          0x08, mkLdloc 2us
-          0x09, mkLdloc 3us
-          0x02, mkLdarg 0us
-          0x03, mkLdarg 1us
-          0x04, mkLdarg 2us
-          0x05, mkLdarg 3us
-          0x2a, I_ret
-          0x58, AI_add
-          0xd6, AI_add_ovf
-          0xd7, AI_add_ovf_un
-          0x5f, AI_and
-          0x5b, AI_div
-          0x5c, AI_div_un
-          0xfe01, AI_ceq
-          0xfe02, AI_cgt
-          0xfe03, AI_cgt_un
-          0xfe04, AI_clt
-          0xfe05, AI_clt_un
-          0x67, AI_conv DT_I1
-          0x68, AI_conv DT_I2 
-          0x69, AI_conv DT_I4
-          0x6a, AI_conv DT_I8  
-          0xd3, AI_conv DT_I  
-          0x6b, AI_conv DT_R4  
-          0x6c, AI_conv DT_R8  
-          0xd2, AI_conv DT_U1  
-          0xd1, AI_conv DT_U2  
-          0x6d, AI_conv DT_U4  
-          0x6e, AI_conv DT_U8  
-          0xe0, AI_conv DT_U  
-          0x76, AI_conv DT_R  
-          0xb3, AI_conv_ovf DT_I1  
-          0xb5, AI_conv_ovf DT_I2  
-          0xb7, AI_conv_ovf DT_I4  
-          0xb9, AI_conv_ovf DT_I8  
-          0xd4, AI_conv_ovf DT_I  
-          0xb4, AI_conv_ovf DT_U1  
-          0xb6, AI_conv_ovf DT_U2  
-          0xb8, AI_conv_ovf DT_U4  
-          0xba, AI_conv_ovf DT_U8  
-          0xd5, AI_conv_ovf DT_U  
-          0x82, AI_conv_ovf_un DT_I1  
-          0x83, AI_conv_ovf_un DT_I2  
-          0x84, AI_conv_ovf_un DT_I4  
-          0x85, AI_conv_ovf_un DT_I8  
-          0x8a, AI_conv_ovf_un DT_I  
-          0x86, AI_conv_ovf_un DT_U1  
-          0x87, AI_conv_ovf_un DT_U2  
-          0x88, AI_conv_ovf_un DT_U4  
-          0x89, AI_conv_ovf_un DT_U8  
-          0x8b, AI_conv_ovf_un DT_U  
-          0x9c, I_stelem DT_I1  
-          0x9d, I_stelem DT_I2
-          0x9e, I_stelem DT_I4  
-          0x9f, I_stelem DT_I8  
-          0xa0, I_stelem DT_R4  
-          0xa1, I_stelem DT_R8  
-          0x9b, I_stelem DT_I  
-          0xa2, I_stelem DT_REF  
-          0x90, I_ldelem DT_I1
-          0x92, I_ldelem DT_I2  
-          0x94, I_ldelem DT_I4  
-          0x96, I_ldelem DT_I8  
-          0x91, I_ldelem DT_U1  
-          0x93, I_ldelem DT_U2  
-          0x95, I_ldelem DT_U4 
-          0x98, I_ldelem DT_R4  
-          0x99, I_ldelem DT_R8  
-          0x97, I_ldelem DT_I  
-          0x9a, I_ldelem DT_REF  
-          0x5a, AI_mul
-          0xd8, AI_mul_ovf
-          0xd9, AI_mul_ovf_un
-          0x5d, AI_rem
-          0x5e, AI_rem_un
-          0x62, AI_shl 
-          0x63, AI_shr
-          0x64, AI_shr_un
-          0x59, AI_sub
-          0xda, AI_sub_ovf
-          0xdb, AI_sub_ovf_un
-          0x61, AI_xor
-          0x60, AI_or
-          0x65, AI_neg
-          0x66, AI_not
-          i_ldnull, AI_ldnull
-          i_dup, AI_dup
-          i_pop, AI_pop
-          i_ckfinite, AI_ckfinite
-          i_nop, AI_nop
-          i_break, I_break
-          i_arglist, I_arglist
-          i_endfilter, I_endfilter
-          i_endfinally, I_endfinally
-          i_refanytype, I_refanytype
-          i_localloc, I_localloc
-          i_throw, I_throw
-          i_ldlen, I_ldlen
-          i_rethrow, I_rethrow ]
-
-let isNoArgInstr i = 
-  match i with 
-  | AI_ldc (DT_I4, ILConst.I4 n) when -1 <= n && n <= 8 -> true
-  | I_stloc n | I_ldloc n | I_ldarg n when n <= 3us -> true
-  | I_ret
-  | AI_add
-  | AI_add_ovf
-  | AI_add_ovf_un
-  | AI_and  
-  | AI_div 
-  | AI_div_un
-  | AI_ceq  
-  | AI_cgt 
-  | AI_cgt_un
-  | AI_clt
-  | AI_clt_un
-  | AI_conv DT_I1  
-  | AI_conv DT_I2  
-  | AI_conv DT_I4  
-  | AI_conv DT_I8  
-  | AI_conv DT_I  
-  | AI_conv DT_R4  
-  | AI_conv DT_R8  
-  | AI_conv DT_U1  
-  | AI_conv DT_U2  
-  | AI_conv DT_U4  
-  | AI_conv DT_U8  
-  | AI_conv DT_U  
-  | AI_conv DT_R  
-  | AI_conv_ovf DT_I1  
-  | AI_conv_ovf DT_I2  
-  | AI_conv_ovf DT_I4  
-  | AI_conv_ovf DT_I8  
-  | AI_conv_ovf DT_I  
-  | AI_conv_ovf DT_U1  
-  | AI_conv_ovf DT_U2  
-  | AI_conv_ovf DT_U4  
-  | AI_conv_ovf DT_U8  
-  | AI_conv_ovf DT_U  
-  | AI_conv_ovf_un DT_I1  
-  | AI_conv_ovf_un DT_I2  
-  | AI_conv_ovf_un DT_I4  
-  | AI_conv_ovf_un DT_I8  
-  | AI_conv_ovf_un DT_I  
-  | AI_conv_ovf_un DT_U1  
-  | AI_conv_ovf_un DT_U2  
-  | AI_conv_ovf_un DT_U4  
-  | AI_conv_ovf_un DT_U8  
-  | AI_conv_ovf_un DT_U  
-  | I_stelem DT_I1  
-  | I_stelem DT_I2  
-  | I_stelem DT_I4  
-  | I_stelem DT_I8  
-  | I_stelem DT_R4  
-  | I_stelem DT_R8  
-  | I_stelem DT_I  
-  | I_stelem DT_REF  
-  | I_ldelem DT_I1  
-  | I_ldelem DT_I2  
-  | I_ldelem DT_I4  
-  | I_ldelem DT_I8  
-  | I_ldelem DT_U1  
-  | I_ldelem DT_U2  
-  | I_ldelem DT_U4  
-  | I_ldelem DT_R4  
-  | I_ldelem DT_R8  
-  | I_ldelem DT_I  
-  | I_ldelem DT_REF  
-  | AI_mul  
-  | AI_mul_ovf
-  | AI_mul_ovf_un
-  | AI_rem  
-  | AI_rem_un  
-  | AI_shl  
-  | AI_shr  
-  | AI_shr_un
-  | AI_sub  
-  | AI_sub_ovf
-  | AI_sub_ovf_un 
-  | AI_xor  
-  | AI_or     
-  | AI_neg     
-  | AI_not     
-  | AI_ldnull   
-  | AI_dup   
-  | AI_pop
-  | AI_ckfinite
-  | AI_nop
-  | I_break
-  | I_arglist
-  | I_endfilter
-  | I_endfinally
-  | I_refanytype
-  | I_localloc
-  | I_throw
-  | I_ldlen
-  | I_rethrow -> true
-  | _ -> false
-
-let ILCmpInstrMap = 
-    lazy (
-        let dict = Dictionary.newWithSize 12
-        dict.Add (BI_beq, i_beq     )
-        dict.Add (BI_bgt, i_bgt     )
-        dict.Add (BI_bgt_un, i_bgt_un  )
-        dict.Add (BI_bge, i_bge     )
-        dict.Add (BI_bge_un, i_bge_un  )
-        dict.Add (BI_ble, i_ble     )
-        dict.Add (BI_ble_un, i_ble_un  )
-        dict.Add (BI_blt, i_blt     )
-        dict.Add (BI_blt_un, i_blt_un  )
-        dict.Add (BI_bne_un, i_bne_un  )
-        dict.Add (BI_brfalse, i_brfalse )
-        dict.Add (BI_brtrue, i_brtrue  )
-        dict
-    )
-
-let ILCmpInstrRevMap = 
-  lazy (
-      let dict = Dictionary.newWithSize 12
-      dict.Add ( BI_beq, i_beq_s     )
-      dict.Add ( BI_bgt, i_bgt_s     )
-      dict.Add ( BI_bgt_un, i_bgt_un_s  )
-      dict.Add ( BI_bge, i_bge_s     )
-      dict.Add ( BI_bge_un, i_bge_un_s  )
-      dict.Add ( BI_ble, i_ble_s     )
-      dict.Add ( BI_ble_un, i_ble_un_s  )
-      dict.Add ( BI_blt, i_blt_s     )
-      dict.Add ( BI_blt_un, i_blt_un_s  )
-      dict.Add ( BI_bne_un, i_bne_un_s  )
-      dict.Add ( BI_brfalse, i_brfalse_s )
-      dict.Add ( BI_brtrue, i_brtrue_s  )
-      dict
-  )
-
-// From corhdr.h 
-
-let nt_VOID        = 0x1uy
-let nt_BOOLEAN     = 0x2uy
-let nt_I1          = 0x3uy
-let nt_U1          = 0x4uy
-let nt_I2          = 0x5uy
-let nt_U2          = 0x6uy
-let nt_I4          = 0x7uy
-let nt_U4          = 0x8uy
-let nt_I8          = 0x9uy
-let nt_U8          = 0xAuy
-let nt_R4          = 0xBuy
-let nt_R8          = 0xCuy
-let nt_SYSCHAR     = 0xDuy
-let nt_VARIANT     = 0xEuy
-let nt_CURRENCY    = 0xFuy
-let nt_PTR         = 0x10uy
-let nt_DECIMAL     = 0x11uy
-let nt_DATE        = 0x12uy
-let nt_BSTR        = 0x13uy
-let nt_LPSTR       = 0x14uy
-let nt_LPWSTR      = 0x15uy
-let nt_LPTSTR      = 0x16uy
-let nt_FIXEDSYSSTRING  = 0x17uy
-let nt_OBJECTREF   = 0x18uy
-let nt_IUNKNOWN    = 0x19uy
-let nt_IDISPATCH   = 0x1Auy
-let nt_STRUCT      = 0x1Buy
-let nt_INTF        = 0x1Cuy
-let nt_SAFEARRAY   = 0x1Duy
-let nt_FIXEDARRAY  = 0x1Euy
-let nt_INT         = 0x1Fuy
-let nt_UINT        = 0x20uy
-let nt_NESTEDSTRUCT  = 0x21uy
-let nt_BYVALSTR    = 0x22uy
-let nt_ANSIBSTR    = 0x23uy
-let nt_TBSTR       = 0x24uy
+let noArgInstrs =
+    lazy
+        [
+            i_ldc_i4_0, mk_ldc 0
+            i_ldc_i4_1, mk_ldc 1
+            i_ldc_i4_2, mk_ldc 2
+            i_ldc_i4_3, mk_ldc 3
+            i_ldc_i4_4, mk_ldc 4
+            i_ldc_i4_5, mk_ldc 5
+            i_ldc_i4_6, mk_ldc 6
+            i_ldc_i4_7, mk_ldc 7
+            i_ldc_i4_8, mk_ldc 8
+            i_ldc_i4_m1, mk_ldc -1
+            0x0a, mkStloc 0us
+            0x0b, mkStloc 1us
+            0x0c, mkStloc 2us
+            0x0d, mkStloc 3us
+            0x06, mkLdloc 0us
+            0x07, mkLdloc 1us
+            0x08, mkLdloc 2us
+            0x09, mkLdloc 3us
+            0x02, mkLdarg 0us
+            0x03, mkLdarg 1us
+            0x04, mkLdarg 2us
+            0x05, mkLdarg 3us
+            0x2a, I_ret
+            0x58, AI_add
+            0xd6, AI_add_ovf
+            0xd7, AI_add_ovf_un
+            0x5f, AI_and
+            0x5b, AI_div
+            0x5c, AI_div_un
+            0xfe01, AI_ceq
+            0xfe02, AI_cgt
+            0xfe03, AI_cgt_un
+            0xfe04, AI_clt
+            0xfe05, AI_clt_un
+            0x67, AI_conv DT_I1
+            0x68, AI_conv DT_I2
+            0x69, AI_conv DT_I4
+            0x6a, AI_conv DT_I8
+            0xd3, AI_conv DT_I
+            0x6b, AI_conv DT_R4
+            0x6c, AI_conv DT_R8
+            0xd2, AI_conv DT_U1
+            0xd1, AI_conv DT_U2
+            0x6d, AI_conv DT_U4
+            0x6e, AI_conv DT_U8
+            0xe0, AI_conv DT_U
+            0x76, AI_conv DT_R
+            0xb3, AI_conv_ovf DT_I1
+            0xb5, AI_conv_ovf DT_I2
+            0xb7, AI_conv_ovf DT_I4
+            0xb9, AI_conv_ovf DT_I8
+            0xd4, AI_conv_ovf DT_I
+            0xb4, AI_conv_ovf DT_U1
+            0xb6, AI_conv_ovf DT_U2
+            0xb8, AI_conv_ovf DT_U4
+            0xba, AI_conv_ovf DT_U8
+            0xd5, AI_conv_ovf DT_U
+            0x82, AI_conv_ovf_un DT_I1
+            0x83, AI_conv_ovf_un DT_I2
+            0x84, AI_conv_ovf_un DT_I4
+            0x85, AI_conv_ovf_un DT_I8
+            0x8a, AI_conv_ovf_un DT_I
+            0x86, AI_conv_ovf_un DT_U1
+            0x87, AI_conv_ovf_un DT_U2
+            0x88, AI_conv_ovf_un DT_U4
+            0x89, AI_conv_ovf_un DT_U8
+            0x8b, AI_conv_ovf_un DT_U
+            0x9c, I_stelem DT_I1
+            0x9d, I_stelem DT_I2
+            0x9e, I_stelem DT_I4
+            0x9f, I_stelem DT_I8
+            0xa0, I_stelem DT_R4
+            0xa1, I_stelem DT_R8
+            0x9b, I_stelem DT_I
+            0xa2, I_stelem DT_REF
+            0x90, I_ldelem DT_I1
+            0x92, I_ldelem DT_I2
+            0x94, I_ldelem DT_I4
+            0x96, I_ldelem DT_I8
+            0x91, I_ldelem DT_U1
+            0x93, I_ldelem DT_U2
+            0x95, I_ldelem DT_U4
+            0x98, I_ldelem DT_R4
+            0x99, I_ldelem DT_R8
+            0x97, I_ldelem DT_I
+            0x9a, I_ldelem DT_REF
+            0x5a, AI_mul
+            0xd8, AI_mul_ovf
+            0xd9, AI_mul_ovf_un
+            0x5d, AI_rem
+            0x5e, AI_rem_un
+            0x62, AI_shl
+            0x63, AI_shr
+            0x64, AI_shr_un
+            0x59, AI_sub
+            0xda, AI_sub_ovf
+            0xdb, AI_sub_ovf_un
+            0x61, AI_xor
+            0x60, AI_or
+            0x65, AI_neg
+            0x66, AI_not
+            i_ldnull, AI_ldnull
+            i_dup, AI_dup
+            i_pop, AI_pop
+            i_ckfinite, AI_ckfinite
+            i_nop, AI_nop
+            i_break, I_break
+            i_arglist, I_arglist
+            i_endfilter, I_endfilter
+            i_endfinally, I_endfinally
+            i_refanytype, I_refanytype
+            i_localloc, I_localloc
+            i_throw, I_throw
+            i_ldlen, I_ldlen
+            i_rethrow, I_rethrow
+        ]
+
+let isNoArgInstr i =
+    match i with
+    | AI_ldc (DT_I4, ILConst.I4 n) when -1 <= n && n <= 8 -> true
+    | I_stloc n
+    | I_ldloc n
+    | I_ldarg n when n <= 3us -> true
+    | I_ret
+    | AI_add
+    | AI_add_ovf
+    | AI_add_ovf_un
+    | AI_and
+    | AI_div
+    | AI_div_un
+    | AI_ceq
+    | AI_cgt
+    | AI_cgt_un
+    | AI_clt
+    | AI_clt_un
+    | AI_conv DT_I1
+    | AI_conv DT_I2
+    | AI_conv DT_I4
+    | AI_conv DT_I8
+    | AI_conv DT_I
+    | AI_conv DT_R4
+    | AI_conv DT_R8
+    | AI_conv DT_U1
+    | AI_conv DT_U2
+    | AI_conv DT_U4
+    | AI_conv DT_U8
+    | AI_conv DT_U
+    | AI_conv DT_R
+    | AI_conv_ovf DT_I1
+    | AI_conv_ovf DT_I2
+    | AI_conv_ovf DT_I4
+    | AI_conv_ovf DT_I8
+    | AI_conv_ovf DT_I
+    | AI_conv_ovf DT_U1
+    | AI_conv_ovf DT_U2
+    | AI_conv_ovf DT_U4
+    | AI_conv_ovf DT_U8
+    | AI_conv_ovf DT_U
+    | AI_conv_ovf_un DT_I1
+    | AI_conv_ovf_un DT_I2
+    | AI_conv_ovf_un DT_I4
+    | AI_conv_ovf_un DT_I8
+    | AI_conv_ovf_un DT_I
+    | AI_conv_ovf_un DT_U1
+    | AI_conv_ovf_un DT_U2
+    | AI_conv_ovf_un DT_U4
+    | AI_conv_ovf_un DT_U8
+    | AI_conv_ovf_un DT_U
+    | I_stelem DT_I1
+    | I_stelem DT_I2
+    | I_stelem DT_I4
+    | I_stelem DT_I8
+    | I_stelem DT_R4
+    | I_stelem DT_R8
+    | I_stelem DT_I
+    | I_stelem DT_REF
+    | I_ldelem DT_I1
+    | I_ldelem DT_I2
+    | I_ldelem DT_I4
+    | I_ldelem DT_I8
+    | I_ldelem DT_U1
+    | I_ldelem DT_U2
+    | I_ldelem DT_U4
+    | I_ldelem DT_R4
+    | I_ldelem DT_R8
+    | I_ldelem DT_I
+    | I_ldelem DT_REF
+    | AI_mul
+    | AI_mul_ovf
+    | AI_mul_ovf_un
+    | AI_rem
+    | AI_rem_un
+    | AI_shl
+    | AI_shr
+    | AI_shr_un
+    | AI_sub
+    | AI_sub_ovf
+    | AI_sub_ovf_un
+    | AI_xor
+    | AI_or
+    | AI_neg
+    | AI_not
+    | AI_ldnull
+    | AI_dup
+    | AI_pop
+    | AI_ckfinite
+    | AI_nop
+    | I_break
+    | I_arglist
+    | I_endfilter
+    | I_endfinally
+    | I_refanytype
+    | I_localloc
+    | I_throw
+    | I_ldlen
+    | I_rethrow -> true
+    | _ -> false
+
+let ILCmpInstrMap =
+    lazy
+        (let dict = Dictionary.newWithSize 12
+         dict.Add(BI_beq, i_beq)
+         dict.Add(BI_bgt, i_bgt)
+         dict.Add(BI_bgt_un, i_bgt_un)
+         dict.Add(BI_bge, i_bge)
+         dict.Add(BI_bge_un, i_bge_un)
+         dict.Add(BI_ble, i_ble)
+         dict.Add(BI_ble_un, i_ble_un)
+         dict.Add(BI_blt, i_blt)
+         dict.Add(BI_blt_un, i_blt_un)
+         dict.Add(BI_bne_un, i_bne_un)
+         dict.Add(BI_brfalse, i_brfalse)
+         dict.Add(BI_brtrue, i_brtrue)
+         dict)
+
+let ILCmpInstrRevMap =
+    lazy
+        (let dict = Dictionary.newWithSize 12
+         dict.Add(BI_beq, i_beq_s)
+         dict.Add(BI_bgt, i_bgt_s)
+         dict.Add(BI_bgt_un, i_bgt_un_s)
+         dict.Add(BI_bge, i_bge_s)
+         dict.Add(BI_bge_un, i_bge_un_s)
+         dict.Add(BI_ble, i_ble_s)
+         dict.Add(BI_ble_un, i_ble_un_s)
+         dict.Add(BI_blt, i_blt_s)
+         dict.Add(BI_blt_un, i_blt_un_s)
+         dict.Add(BI_bne_un, i_bne_un_s)
+         dict.Add(BI_brfalse, i_brfalse_s)
+         dict.Add(BI_brtrue, i_brtrue_s)
+         dict)
+
+// From corhdr.h
+
+let nt_VOID = 0x1uy
+let nt_BOOLEAN = 0x2uy
+let nt_I1 = 0x3uy
+let nt_U1 = 0x4uy
+let nt_I2 = 0x5uy
+let nt_U2 = 0x6uy
+let nt_I4 = 0x7uy
+let nt_U4 = 0x8uy
+let nt_I8 = 0x9uy
+let nt_U8 = 0xAuy
+let nt_R4 = 0xBuy
+let nt_R8 = 0xCuy
+let nt_SYSCHAR = 0xDuy
+let nt_VARIANT = 0xEuy
+let nt_CURRENCY = 0xFuy
+let nt_PTR = 0x10uy
+let nt_DECIMAL = 0x11uy
+let nt_DATE = 0x12uy
+let nt_BSTR = 0x13uy
+let nt_LPSTR = 0x14uy
+let nt_LPWSTR = 0x15uy
+let nt_LPTSTR = 0x16uy
+let nt_FIXEDSYSSTRING = 0x17uy
+let nt_OBJECTREF = 0x18uy
+let nt_IUNKNOWN = 0x19uy
+let nt_IDISPATCH = 0x1Auy
+let nt_STRUCT = 0x1Buy
+let nt_INTF = 0x1Cuy
+let nt_SAFEARRAY = 0x1Duy
+let nt_FIXEDARRAY = 0x1Euy
+let nt_INT = 0x1Fuy
+let nt_UINT = 0x20uy
+let nt_NESTEDSTRUCT = 0x21uy
+let nt_BYVALSTR = 0x22uy
+let nt_ANSIBSTR = 0x23uy
+let nt_TBSTR = 0x24uy
 let nt_VARIANTBOOL = 0x25uy
-let nt_FUNC        = 0x26uy
-let nt_ASANY       = 0x28uy
-let nt_ARRAY       = 0x2Auy
-let nt_LPSTRUCT    = 0x2Buy
+let nt_FUNC = 0x26uy
+let nt_ASANY = 0x28uy
+let nt_ARRAY = 0x2Auy
+let nt_LPSTRUCT = 0x2Buy
 let nt_CUSTOMMARSHALER = 0x2Cuy
-let nt_ERROR       = 0x2Duy
-let nt_LPUTF8STR   = 0x30uy
+let nt_ERROR = 0x2Duy
+let nt_LPUTF8STR = 0x30uy
 let nt_MAX = 0x50uy
 
 // From c:/clrenv.i386/Crt/Inc/i386/hs.h
@@ -864,7 +896,7 @@ let vt_UI8 = 21
 let vt_INT = 22
 let vt_UINT = 23
 let vt_VOID = 24
-let vt_HRESULT  = 25
+let vt_HRESULT = 25
 let vt_PTR = 26
 let vt_SAFEARRAY = 27
 let vt_CARRAY = 28
@@ -885,110 +917,120 @@ let vt_VECTOR = 0x1000
 let vt_ARRAY = 0x2000
 let vt_BYREF = 0x4000
 
- 
-let ILNativeTypeMap = 
- lazy [ nt_CURRENCY, ILNativeType.Currency
-        nt_BSTR, (* COM interop *) ILNativeType.BSTR
-        nt_LPSTR, ILNativeType.LPSTR
-        nt_LPWSTR, ILNativeType.LPWSTR
-        nt_LPTSTR, ILNativeType.LPTSTR
-        nt_LPUTF8STR, ILNativeType.LPUTF8STR
-        nt_IUNKNOWN, (* COM interop *) ILNativeType.IUnknown
-        nt_IDISPATCH, (* COM interop *) ILNativeType.IDispatch
-        nt_BYVALSTR, ILNativeType.ByValStr
-        nt_TBSTR, ILNativeType.TBSTR
-        nt_LPSTRUCT, ILNativeType.LPSTRUCT
-        nt_INTF, (* COM interop *) ILNativeType.Interface
-        nt_STRUCT, ILNativeType.Struct
-        nt_ERROR, (* COM interop *) ILNativeType.Error               
-        nt_VOID, ILNativeType.Void 
-        nt_BOOLEAN, ILNativeType.Bool
-        nt_I1, ILNativeType.Int8
-        nt_I2, ILNativeType.Int16
-        nt_I4, ILNativeType.Int32
-        nt_I8, ILNativeType.Int64
-        nt_R4, ILNativeType.Single
-        nt_R8, ILNativeType.Double
-        nt_U1, ILNativeType.Byte
-        nt_U2, ILNativeType.UInt16
-        nt_U4, ILNativeType.UInt32
-        nt_U8, ILNativeType.UInt64
-        nt_INT, ILNativeType.Int
-        nt_UINT, ILNativeType.UInt
-        nt_ANSIBSTR, (* COM interop *) ILNativeType.ANSIBSTR
-        nt_VARIANTBOOL, (* COM interop *) ILNativeType.VariantBool
-        nt_FUNC, ILNativeType.Method
-        nt_ASANY, ILNativeType.AsAny ]
-
-let ILNativeTypeRevMap = lazy (List.map (fun (x,y) -> (y,x)) (Lazy.force ILNativeTypeMap))
-    
-let ILVariantTypeMap = 
- lazy [ ILNativeVariant.Empty, vt_EMPTY
-        ILNativeVariant.Null, vt_NULL 
-        ILNativeVariant.Variant, vt_VARIANT       
-        ILNativeVariant.Currency, vt_CY         
-        ILNativeVariant.Decimal, vt_DECIMAL   
-        ILNativeVariant.Date, vt_DATE       
-        ILNativeVariant.BSTR, vt_BSTR       
-        ILNativeVariant.LPSTR, vt_LPSTR     
-        ILNativeVariant.LPWSTR, vt_LPWSTR    
-        ILNativeVariant.IUnknown, vt_UNKNOWN   
-        ILNativeVariant.IDispatch, vt_DISPATCH    
-        ILNativeVariant.SafeArray, vt_SAFEARRAY 
-        ILNativeVariant.Error, vt_ERROR         
-        ILNativeVariant.HRESULT, vt_HRESULT   
-        ILNativeVariant.CArray, vt_CARRAY    
-        ILNativeVariant.UserDefined, vt_USERDEFINED 
-        ILNativeVariant.Record, vt_RECORD
-        ILNativeVariant.FileTime, vt_FILETIME  
-        ILNativeVariant.Blob, vt_BLOB       
-        ILNativeVariant.Stream, vt_STREAM     
-        ILNativeVariant.Storage, vt_STORAGE          
-        ILNativeVariant.StreamedObject, vt_STREAMED_OBJECT
-        ILNativeVariant.StoredObject, vt_STORED_OBJECT 
-        ILNativeVariant.BlobObject, vt_BLOB_OBJECT 
-        ILNativeVariant.CF, vt_CF        
-        ILNativeVariant.CLSID, vt_CLSID                    
-        ILNativeVariant.Void, vt_VOID          
-        ILNativeVariant.Bool, vt_BOOL        
-        ILNativeVariant.Int8, vt_I1            
-        ILNativeVariant.Int16, vt_I2  
-        ILNativeVariant.Int32, vt_I4         
-        ILNativeVariant.Int64, vt_I8        
-        ILNativeVariant.Single, vt_R4         
-        ILNativeVariant.Double, vt_R8         
-        ILNativeVariant.UInt8, vt_UI1       
-        ILNativeVariant.UInt16, vt_UI2       
-        ILNativeVariant.UInt32, vt_UI4       
-        ILNativeVariant.UInt64, vt_UI8       
-        ILNativeVariant.PTR, vt_PTR       
-        ILNativeVariant.Int, vt_INT               
-        ILNativeVariant.UInt, vt_UINT            ]
-
-let ILVariantTypeRevMap = lazy (List.map (fun (x,y) -> (y,x)) (Lazy.force ILVariantTypeMap))
+let ILNativeTypeMap =
+    lazy
+        [
+            nt_CURRENCY, ILNativeType.Currency
+            nt_BSTR (* COM interop *) , ILNativeType.BSTR
+            nt_LPSTR, ILNativeType.LPSTR
+            nt_LPWSTR, ILNativeType.LPWSTR
+            nt_LPTSTR, ILNativeType.LPTSTR
+            nt_LPUTF8STR, ILNativeType.LPUTF8STR
+            nt_IUNKNOWN (* COM interop *) , ILNativeType.IUnknown
+            nt_IDISPATCH (* COM interop *) , ILNativeType.IDispatch
+            nt_BYVALSTR, ILNativeType.ByValStr
+            nt_TBSTR, ILNativeType.TBSTR
+            nt_LPSTRUCT, ILNativeType.LPSTRUCT
+            nt_INTF (* COM interop *) , ILNativeType.Interface
+            nt_STRUCT, ILNativeType.Struct
+            nt_ERROR (* COM interop *) , ILNativeType.Error
+            nt_VOID, ILNativeType.Void
+            nt_BOOLEAN, ILNativeType.Bool
+            nt_I1, ILNativeType.Int8
+            nt_I2, ILNativeType.Int16
+            nt_I4, ILNativeType.Int32
+            nt_I8, ILNativeType.Int64
+            nt_R4, ILNativeType.Single
+            nt_R8, ILNativeType.Double
+            nt_U1, ILNativeType.Byte
+            nt_U2, ILNativeType.UInt16
+            nt_U4, ILNativeType.UInt32
+            nt_U8, ILNativeType.UInt64
+            nt_INT, ILNativeType.Int
+            nt_UINT, ILNativeType.UInt
+            nt_ANSIBSTR (* COM interop *) , ILNativeType.ANSIBSTR
+            nt_VARIANTBOOL (* COM interop *) , ILNativeType.VariantBool
+            nt_FUNC, ILNativeType.Method
+            nt_ASANY, ILNativeType.AsAny
+        ]
+
+let ILNativeTypeRevMap =
+    lazy (List.map (fun (x, y) -> (y, x)) (Lazy.force ILNativeTypeMap))
+
+let ILVariantTypeMap =
+    lazy
+        [
+            ILNativeVariant.Empty, vt_EMPTY
+            ILNativeVariant.Null, vt_NULL
+            ILNativeVariant.Variant, vt_VARIANT
+            ILNativeVariant.Currency, vt_CY
+            ILNativeVariant.Decimal, vt_DECIMAL
+            ILNativeVariant.Date, vt_DATE
+            ILNativeVariant.BSTR, vt_BSTR
+            ILNativeVariant.LPSTR, vt_LPSTR
+            ILNativeVariant.LPWSTR, vt_LPWSTR
+            ILNativeVariant.IUnknown, vt_UNKNOWN
+            ILNativeVariant.IDispatch, vt_DISPATCH
+            ILNativeVariant.SafeArray, vt_SAFEARRAY
+            ILNativeVariant.Error, vt_ERROR
+            ILNativeVariant.HRESULT, vt_HRESULT
+            ILNativeVariant.CArray, vt_CARRAY
+            ILNativeVariant.UserDefined, vt_USERDEFINED
+            ILNativeVariant.Record, vt_RECORD
+            ILNativeVariant.FileTime, vt_FILETIME
+            ILNativeVariant.Blob, vt_BLOB
+            ILNativeVariant.Stream, vt_STREAM
+            ILNativeVariant.Storage, vt_STORAGE
+            ILNativeVariant.StreamedObject, vt_STREAMED_OBJECT
+            ILNativeVariant.StoredObject, vt_STORED_OBJECT
+            ILNativeVariant.BlobObject, vt_BLOB_OBJECT
+            ILNativeVariant.CF, vt_CF
+            ILNativeVariant.CLSID, vt_CLSID
+            ILNativeVariant.Void, vt_VOID
+            ILNativeVariant.Bool, vt_BOOL
+            ILNativeVariant.Int8, vt_I1
+            ILNativeVariant.Int16, vt_I2
+            ILNativeVariant.Int32, vt_I4
+            ILNativeVariant.Int64, vt_I8
+            ILNativeVariant.Single, vt_R4
+            ILNativeVariant.Double, vt_R8
+            ILNativeVariant.UInt8, vt_UI1
+            ILNativeVariant.UInt16, vt_UI2
+            ILNativeVariant.UInt32, vt_UI4
+            ILNativeVariant.UInt64, vt_UI8
+            ILNativeVariant.PTR, vt_PTR
+            ILNativeVariant.Int, vt_INT
+            ILNativeVariant.UInt, vt_UINT
+        ]
+
+let ILVariantTypeRevMap =
+    lazy (List.map (fun (x, y) -> (y, x)) (Lazy.force ILVariantTypeMap))
 
 let ILSecurityActionMap =
-  lazy
-    [ ILSecurityAction.Request, 0x0001
-      ILSecurityAction.Demand, 0x0002
-      ILSecurityAction.Assert, 0x0003
-      ILSecurityAction.Deny, 0x0004
-      ILSecurityAction.PermitOnly, 0x0005
-      ILSecurityAction.LinkCheck, 0x0006
-      ILSecurityAction.InheritCheck, 0x0007
-      ILSecurityAction.ReqMin, 0x0008
-      ILSecurityAction.ReqOpt, 0x0009
-      ILSecurityAction.ReqRefuse, 0x000a
-      ILSecurityAction.PreJitGrant, 0x000b
-      ILSecurityAction.PreJitDeny, 0x000c
-      ILSecurityAction.NonCasDemand, 0x000d
-      ILSecurityAction.NonCasLinkDemand, 0x000e
-      ILSecurityAction.NonCasInheritance, 0x000f
-      ILSecurityAction.LinkDemandChoice, 0x0010
-      ILSecurityAction.InheritanceDemandChoice, 0x0011
-      ILSecurityAction.DemandChoice, 0x0012 ]
-
-let ILSecurityActionRevMap = lazy (List.map (fun (x,y) -> (y,x)) (Lazy.force ILSecurityActionMap))
+    lazy
+        [
+            ILSecurityAction.Request, 0x0001
+            ILSecurityAction.Demand, 0x0002
+            ILSecurityAction.Assert, 0x0003
+            ILSecurityAction.Deny, 0x0004
+            ILSecurityAction.PermitOnly, 0x0005
+            ILSecurityAction.LinkCheck, 0x0006
+            ILSecurityAction.InheritCheck, 0x0007
+            ILSecurityAction.ReqMin, 0x0008
+            ILSecurityAction.ReqOpt, 0x0009
+            ILSecurityAction.ReqRefuse, 0x000a
+            ILSecurityAction.PreJitGrant, 0x000b
+            ILSecurityAction.PreJitDeny, 0x000c
+            ILSecurityAction.NonCasDemand, 0x000d
+            ILSecurityAction.NonCasLinkDemand, 0x000e
+            ILSecurityAction.NonCasInheritance, 0x000f
+            ILSecurityAction.LinkDemandChoice, 0x0010
+            ILSecurityAction.InheritanceDemandChoice, 0x0011
+            ILSecurityAction.DemandChoice, 0x0012
+        ]
+
+let ILSecurityActionRevMap =
+    lazy (List.map (fun (x, y) -> (y, x)) (Lazy.force ILSecurityActionMap))
 
 let e_CorILMethod_TinyFormat = 0x02uy
 let e_CorILMethod_FatFormat = 0x03uy
@@ -996,7 +1038,6 @@ let e_CorILMethod_FormatMask = 0x03uy
 let e_CorILMethod_MoreSects = 0x08uy
 let e_CorILMethod_InitLocals = 0x10uy
 
-
 let e_CorILMethod_Sect_EHTable = 0x1uy
 let e_CorILMethod_Sect_FatFormat = 0x40uy
 let e_CorILMethod_Sect_MoreSects = 0x80uy
@@ -1019,5 +1060,3 @@ let e_IMAGE_CEE_CS_CALLCONV_GENERICINST = 0x0auy
 let e_IMAGE_CEE_CS_CALLCONV_GENERIC = 0x10uy
 let e_IMAGE_CEE_CS_CALLCONV_INSTANCE = 0x20uy
 let e_IMAGE_CEE_CS_CALLCONV_INSTANCE_EXPLICIT = 0x40uy
-
-
diff --git a/src/Compiler/AbstractIL/ilmorph.fs b/src/Compiler/AbstractIL/ilmorph.fs
index 6d50df04c5b..dd60d94d0fc 100644
--- a/src/Compiler/AbstractIL/ilmorph.fs
+++ b/src/Compiler/AbstractIL/ilmorph.fs
@@ -1,226 +1,261 @@
 // Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information.
 
-module internal FSharp.Compiler.AbstractIL.Morphs 
+module internal FSharp.Compiler.AbstractIL.Morphs
 
 open System.Collections.Generic
-open Internal.Utilities.Library 
-open FSharp.Compiler.AbstractIL.IL 
+open Internal.Utilities.Library
+open FSharp.Compiler.AbstractIL.IL
 
 let mutable morphCustomAttributeData = false
 
-let enableMorphCustomAttributeData() = 
-    morphCustomAttributeData <- true
+let enableMorphCustomAttributeData () = morphCustomAttributeData <- true
 
-let disableMorphCustomAttributeData() =
-    morphCustomAttributeData <- false
+let disableMorphCustomAttributeData () = morphCustomAttributeData <- false
 
 let code_instr2instr f (code: ILCode) =
-    { code with Instrs= Array.map f code.Instrs}
+    { code with
+        Instrs = Array.map f code.Instrs
+    }
 
-let code_instr2instrs f (code: ILCode) = 
+let code_instr2instrs f (code: ILCode) =
     let instrs = code.Instrs
     let codebuf = ResizeArray()
     let adjust = Dictionary()
     let mutable old = 0
     let mutable nw = 0
-    for instr in instrs do 
+
+    for instr in instrs do
         adjust[old] <- nw
-        let instrs : _ list = f instr
+        let instrs: _ list = f instr
+
         for instr2 in instrs do
             codebuf.Add instr2
             nw <- nw + 1
+
         old <- old + 1
+
     adjust[old] <- nw
+
     let labels =
         let dict = Dictionary.newWithSize code.Labels.Count
-        for kvp in code.Labels do dict.Add(kvp.Key, adjust[kvp.Value])
+
+        for kvp in code.Labels do
+            dict.Add(kvp.Key, adjust[kvp.Value])
+
         dict
-    { code with 
-         Instrs = codebuf.ToArray()
-         Labels = labels }
 
-let code_instr2instr_ty2ty (finstr,fTy) (code: ILCode) = 
+    { code with
+        Instrs = codebuf.ToArray()
+        Labels = labels
+    }
+
+let code_instr2instr_ty2ty (finstr, fTy) (code: ILCode) =
     let codeR = code_instr2instr finstr code
+
     let exnSpecsR =
-        [ for exnSpec in codeR.Exceptions do
-            let clause =
-                match exnSpec.Clause with
-                | ILExceptionClause.TypeCatch (ilTy, b) -> ILExceptionClause.TypeCatch (fTy ilTy, b)
-                | cl -> cl
-            { exnSpec with Clause = clause } ]
+        [
+            for exnSpec in codeR.Exceptions do
+                let clause =
+                    match exnSpec.Clause with
+                    | ILExceptionClause.TypeCatch (ilTy, b) -> ILExceptionClause.TypeCatch(fTy ilTy, b)
+                    | cl -> cl
+
+                { exnSpec with Clause = clause }
+        ]
+
     { codeR with Exceptions = exnSpecsR }
 
 // --------------------------------------------------------------------
 // Standard morphisms - mapping types etc.
-// -------------------------------------------------------------------- 
+// --------------------------------------------------------------------
 
-let rec morphILTypeRefsInILType f x = 
-    match x with 
-    | ILType.Ptr t -> ILType.Ptr (morphILTypeRefsInILType f t)
-    | ILType.FunctionPointer x -> 
+let rec morphILTypeRefsInILType f x =
+    match x with
+    | ILType.Ptr t -> ILType.Ptr(morphILTypeRefsInILType f t)
+    | ILType.FunctionPointer x ->
         ILType.FunctionPointer
-          { x with 
-                ArgTypes=List.map (morphILTypeRefsInILType f) x.ArgTypes
-                ReturnType=morphILTypeRefsInILType f x.ReturnType}
-    | ILType.Byref t -> ILType.Byref (morphILTypeRefsInILType f t)
+            { x with
+                ArgTypes = List.map (morphILTypeRefsInILType f) x.ArgTypes
+                ReturnType = morphILTypeRefsInILType f x.ReturnType
+            }
+    | ILType.Byref t -> ILType.Byref(morphILTypeRefsInILType f t)
     | ILType.Boxed cr -> mkILBoxedType (tspec_tref2tref f cr)
-    | ILType.Value ir -> ILType.Value (tspec_tref2tref f ir)
-    | ILType.Array (s,ty) -> ILType.Array (s,morphILTypeRefsInILType f ty)
-    | ILType.TypeVar v ->  ILType.TypeVar v 
-    | ILType.Modified (req,tref,ty) ->  ILType.Modified (req, f tref, morphILTypeRefsInILType f ty) 
+    | ILType.Value ir -> ILType.Value(tspec_tref2tref f ir)
+    | ILType.Array (s, ty) -> ILType.Array(s, morphILTypeRefsInILType f ty)
+    | ILType.TypeVar v -> ILType.TypeVar v
+    | ILType.Modified (req, tref, ty) -> ILType.Modified(req, f tref, morphILTypeRefsInILType f ty)
     | ILType.Void -> ILType.Void
 
-and tspec_tref2tref f (tspec: ILTypeSpec) = 
-    mkILTySpec(f tspec.TypeRef, List.map (morphILTypeRefsInILType f) tspec.GenericArgs)
+and tspec_tref2tref f (tspec: ILTypeSpec) =
+    mkILTySpec (f tspec.TypeRef, List.map (morphILTypeRefsInILType f) tspec.GenericArgs)
 
-let rec ty_scoref2scoref_tyvar2ty ((_fscope, fTyvar) as fs) ty = 
-    match ty with 
-    | ILType.Ptr elemTy -> ILType.Ptr (ty_scoref2scoref_tyvar2ty fs elemTy)
-    | ILType.FunctionPointer callsig -> ILType.FunctionPointer (callsig_scoref2scoref_tyvar2ty fs callsig)
-    | ILType.Byref elemTy -> ILType.Byref (ty_scoref2scoref_tyvar2ty fs elemTy)
+let rec ty_scoref2scoref_tyvar2ty ((_fscope, fTyvar) as fs) ty =
+    match ty with
+    | ILType.Ptr elemTy -> ILType.Ptr(ty_scoref2scoref_tyvar2ty fs elemTy)
+    | ILType.FunctionPointer callsig -> ILType.FunctionPointer(callsig_scoref2scoref_tyvar2ty fs callsig)
+    | ILType.Byref elemTy -> ILType.Byref(ty_scoref2scoref_tyvar2ty fs elemTy)
     | ILType.Boxed tspec -> mkILBoxedType (tspec_scoref2scoref_tyvar2ty fs tspec)
-    | ILType.Value tspec -> ILType.Value (tspec_scoref2scoref_tyvar2ty fs tspec)
-    | ILType.Array (shape, elemTy) -> ILType.Array (shape,ty_scoref2scoref_tyvar2ty fs elemTy)
-    | ILType.TypeVar idx ->  fTyvar idx
+    | ILType.Value tspec -> ILType.Value(tspec_scoref2scoref_tyvar2ty fs tspec)
+    | ILType.Array (shape, elemTy) -> ILType.Array(shape, ty_scoref2scoref_tyvar2ty fs elemTy)
+    | ILType.TypeVar idx -> fTyvar idx
     | x -> x
 
-and tspec_scoref2scoref_tyvar2ty fs (x:ILTypeSpec) = 
-    ILTypeSpec.Create(morphILScopeRefsInILTypeRef (fst fs) x.TypeRef,tys_scoref2scoref_tyvar2ty fs x.GenericArgs)
+and tspec_scoref2scoref_tyvar2ty fs (x: ILTypeSpec) =
+    ILTypeSpec.Create(morphILScopeRefsInILTypeRef (fst fs) x.TypeRef, tys_scoref2scoref_tyvar2ty fs x.GenericArgs)
 
-and callsig_scoref2scoref_tyvar2ty f x = 
-    { x with 
-        ArgTypes=List.map (ty_scoref2scoref_tyvar2ty f) x.ArgTypes
-        ReturnType=ty_scoref2scoref_tyvar2ty f x.ReturnType}
+and callsig_scoref2scoref_tyvar2ty f x =
+    { x with
+        ArgTypes = List.map (ty_scoref2scoref_tyvar2ty f) x.ArgTypes
+        ReturnType = ty_scoref2scoref_tyvar2ty f x.ReturnType
+    }
 
-and tys_scoref2scoref_tyvar2ty f i = List.map (ty_scoref2scoref_tyvar2ty f) i
+and tys_scoref2scoref_tyvar2ty f i =
+    List.map (ty_scoref2scoref_tyvar2ty f) i
 
-and gparams_scoref2scoref_tyvar2ty f i = List.map (gparam_scoref2scoref_tyvar2ty f) i
+and gparams_scoref2scoref_tyvar2ty f i =
+    List.map (gparam_scoref2scoref_tyvar2ty f) i
 
 and gparam_scoref2scoref_tyvar2ty _f i = i
 
-and morphILScopeRefsInILTypeRef fscope (tref: ILTypeRef) = 
-    ILTypeRef.Create(scope=fscope tref.Scope, enclosing=tref.Enclosing, name = tref.Name)
+and morphILScopeRefsInILTypeRef fscope (tref: ILTypeRef) =
+    ILTypeRef.Create(scope = fscope tref.Scope, enclosing = tref.Enclosing, name = tref.Name)
 
-let callsig_ty2ty f (callsig: ILCallingSignature) = 
-    { CallingConv = callsig.CallingConv
-      ArgTypes = List.map f callsig.ArgTypes
-      ReturnType = f callsig.ReturnType}
+let callsig_ty2ty f (callsig: ILCallingSignature) =
+    {
+        CallingConv = callsig.CallingConv
+        ArgTypes = List.map f callsig.ArgTypes
+        ReturnType = f callsig.ReturnType
+    }
 
-let gparam_ty2ty f gf = {gf with Constraints = List.map f gf.Constraints}
-let gparams_ty2ty f gfs = List.map (gparam_ty2ty f) gfs
-let tys_ty2ty (f: ILType -> ILType)  x = List.map f x
-let mref_ty2ty (f: ILType -> ILType) (x:ILMethodRef) = 
-    ILMethodRef.Create(enclosingTypeRef= (f (mkILBoxedType (mkILNonGenericTySpec x.DeclaringTypeRef))).TypeRef,
-                       callingConv=x.CallingConv,
-                       name=x.Name,
-                       genericArity=x.GenericArity,
-                       argTypes= List.map f x.ArgTypes,
-                       returnType= f x.ReturnType)
+let gparam_ty2ty f gf =
+    { gf with
+        Constraints = List.map f gf.Constraints
+    }
 
+let gparams_ty2ty f gfs = List.map (gparam_ty2ty f) gfs
+let tys_ty2ty (f: ILType -> ILType) x = List.map f x
+
+let mref_ty2ty (f: ILType -> ILType) (x: ILMethodRef) =
+    ILMethodRef.Create(
+        enclosingTypeRef = (f (mkILBoxedType (mkILNonGenericTySpec x.DeclaringTypeRef))).TypeRef,
+        callingConv = x.CallingConv,
+        name = x.Name,
+        genericArity = x.GenericArity,
+        argTypes = List.map f x.ArgTypes,
+        returnType = f x.ReturnType
+    )
 
 type formal_scopeCtxt = Choice
 
-let mspec_ty2ty ((factualTy : ILType -> ILType, fformalTy: formal_scopeCtxt -> ILType -> ILType)) (x: ILMethodSpec) = 
-    mkILMethSpecForMethRefInTy(mref_ty2ty (fformalTy (Choice1Of2 x)) x.MethodRef,
-                               factualTy x.DeclaringType, 
-                               tys_ty2ty factualTy  x.GenericArgs)
+let mspec_ty2ty ((factualTy: ILType -> ILType, fformalTy: formal_scopeCtxt -> ILType -> ILType)) (x: ILMethodSpec) =
+    mkILMethSpecForMethRefInTy (
+        mref_ty2ty (fformalTy (Choice1Of2 x)) x.MethodRef,
+        factualTy x.DeclaringType,
+        tys_ty2ty factualTy x.GenericArgs
+    )
 
-let fref_ty2ty (f: ILType -> ILType) fref = 
+let fref_ty2ty (f: ILType -> ILType) fref =
     { fref with
         DeclaringTypeRef = (f (mkILBoxedType (mkILNonGenericTySpec fref.DeclaringTypeRef))).TypeRef
-        Type= f fref.Type }
+        Type = f fref.Type
+    }
 
-let fspec_ty2ty ((factualTy,fformalTy : formal_scopeCtxt -> ILType -> ILType)) fspec = 
-    { FieldRef=fref_ty2ty (fformalTy (Choice2Of2 fspec)) fspec.FieldRef
-      DeclaringType= factualTy fspec.DeclaringType }
+let fspec_ty2ty ((factualTy, fformalTy: formal_scopeCtxt -> ILType -> ILType)) fspec =
+    {
+        FieldRef = fref_ty2ty (fformalTy (Choice2Of2 fspec)) fspec.FieldRef
+        DeclaringType = factualTy fspec.DeclaringType
+    }
 
 let rec celem_ty2ty f celem =
     match celem with
-    | ILAttribElem.Type (Some ty)    -> ILAttribElem.Type (Some (f ty))
-    | ILAttribElem.TypeRef (Some tref) -> ILAttribElem.TypeRef (Some (f (mkILBoxedType (mkILNonGenericTySpec tref))).TypeRef)
-    | ILAttribElem.Array (elemTy,elems) -> ILAttribElem.Array (f elemTy, List.map (celem_ty2ty f) elems)
+    | ILAttribElem.Type (Some ty) -> ILAttribElem.Type(Some(f ty))
+    | ILAttribElem.TypeRef (Some tref) -> ILAttribElem.TypeRef(Some (f (mkILBoxedType (mkILNonGenericTySpec tref))).TypeRef)
+    | ILAttribElem.Array (elemTy, elems) -> ILAttribElem.Array(f elemTy, List.map (celem_ty2ty f) elems)
     | _ -> celem
 
-let cnamedarg_ty2ty f ((nm, ty, isProp, elem) : ILAttributeNamedArg)  =
-    (nm, f ty, isProp, celem_ty2ty f elem) 
+let cnamedarg_ty2ty f ((nm, ty, isProp, elem): ILAttributeNamedArg) = (nm, f ty, isProp, celem_ty2ty f elem)
 
 let cattr_ty2ty f (c: ILAttribute) =
     let meth = mspec_ty2ty (f, (fun _ -> f)) c.Method
     // dev11 M3 defensive coding: if anything goes wrong with attribute decoding or encoding, then back out.
     if morphCustomAttributeData then
-        try 
-           let elems,namedArgs = decodeILAttribData c 
-           let elems = elems |> List.map (celem_ty2ty f)
-           let namedArgs = namedArgs |> List.map (cnamedarg_ty2ty f)
-           mkILCustomAttribMethRef (meth, elems, namedArgs)
-        with _ ->
-           c.WithMethod(meth)
+        try
+            let elems, namedArgs = decodeILAttribData c
+            let elems = elems |> List.map (celem_ty2ty f)
+            let namedArgs = namedArgs |> List.map (cnamedarg_ty2ty f)
+            mkILCustomAttribMethRef (meth, elems, namedArgs)
+        with
+        | _ -> c.WithMethod(meth)
     else
         c.WithMethod(meth)
 
-
 let cattrs_ty2ty f (cs: ILAttributes) =
     mkILCustomAttrs (List.map (cattr_ty2ty f) (cs.AsList()))
 
-let fdef_ty2ty fTyInCtxt (fdef: ILFieldDef) = 
-    fdef.With(fieldType=fTyInCtxt fdef.FieldType,
-            customAttrs=cattrs_ty2ty fTyInCtxt fdef.CustomAttrs)
+let fdef_ty2ty fTyInCtxt (fdef: ILFieldDef) =
+    fdef.With(fieldType = fTyInCtxt fdef.FieldType, customAttrs = cattrs_ty2ty fTyInCtxt fdef.CustomAttrs)
 
-let morphILLocal f (l: ILLocal) = {l with Type = f l.Type}
+let morphILLocal f (l: ILLocal) = { l with Type = f l.Type }
 
 let morphILVarArgs f (varargs: ILVarArgs) = Option.map (List.map f) varargs
 
-let morphILTypesInILInstr ((factualTy,fformalTy)) i = 
-    let factualTy = factualTy (Some i) 
-    let conv_fspec fr = fspec_ty2ty (factualTy,fformalTy (Some i)) fr 
-    let conv_mspec mr = mspec_ty2ty (factualTy,fformalTy (Some i)) mr 
-    match i with 
-    | I_calli (a,mref,varargs) ->  I_calli (a,callsig_ty2ty factualTy mref, morphILVarArgs factualTy varargs)
-    | I_call (a,mr,varargs) ->  I_call (a,conv_mspec mr, morphILVarArgs factualTy varargs)
-    | I_callvirt (a,mr,varargs) ->   I_callvirt (a,conv_mspec mr, morphILVarArgs factualTy varargs)
-    | I_callconstraint (a,ty,mr,varargs) ->   I_callconstraint (a,factualTy ty,conv_mspec mr, morphILVarArgs factualTy varargs)
-    | I_newobj (mr,varargs) ->  I_newobj (conv_mspec mr, morphILVarArgs factualTy varargs)
-    | I_ldftn mr ->  I_ldftn (conv_mspec mr)
-    | I_ldvirtftn mr ->  I_ldvirtftn (conv_mspec mr)
-    | I_ldfld (a,b,fr) ->  I_ldfld (a,b,conv_fspec fr)
-    | I_ldsfld (a,fr) ->  I_ldsfld (a,conv_fspec fr)
-    | I_ldsflda fr ->  I_ldsflda (conv_fspec fr)
-    | I_ldflda fr ->  I_ldflda (conv_fspec fr)
-    | I_stfld (a,b,fr) -> I_stfld (a,b,conv_fspec fr)
-    | I_stsfld (a,fr) -> I_stsfld (a,conv_fspec fr)
-    | I_castclass ty -> I_castclass (factualTy ty)
-    | I_isinst ty -> I_isinst (factualTy ty)
-    | I_initobj ty -> I_initobj (factualTy ty)
-    | I_cpobj ty -> I_cpobj (factualTy ty)
-    | I_stobj (al,vol,ty) -> I_stobj (al,vol,factualTy ty)
-    | I_ldobj (al,vol,ty) -> I_ldobj (al,vol,factualTy ty)
-    | I_box ty -> I_box (factualTy ty)
-    | I_unbox ty -> I_unbox (factualTy ty)
-    | I_unbox_any ty -> I_unbox_any (factualTy ty)
-    | I_ldelem_any (shape,ty) ->  I_ldelem_any (shape,factualTy ty)
-    | I_stelem_any (shape,ty) ->  I_stelem_any (shape,factualTy ty)
-    | I_newarr (shape,ty) ->  I_newarr (shape,factualTy ty)
-    | I_ldelema (ro,isNativePtr,shape,ty) ->  I_ldelema (ro,isNativePtr,shape,factualTy ty)
-    | I_sizeof ty ->  I_sizeof (factualTy ty)
-    | I_ldtoken tok -> 
-        match tok with 
-        | ILToken.ILType ty ->   I_ldtoken (ILToken.ILType (factualTy ty))
-        | ILToken.ILMethod mr -> I_ldtoken (ILToken.ILMethod (conv_mspec mr))
-        | ILToken.ILField fr -> I_ldtoken (ILToken.ILField (conv_fspec fr))
+let morphILTypesInILInstr ((factualTy, fformalTy)) i =
+    let factualTy = factualTy (Some i)
+
+    let conv_fspec fr =
+        fspec_ty2ty (factualTy, fformalTy (Some i)) fr
+
+    let conv_mspec mr =
+        mspec_ty2ty (factualTy, fformalTy (Some i)) mr
+
+    match i with
+    | I_calli (a, mref, varargs) -> I_calli(a, callsig_ty2ty factualTy mref, morphILVarArgs factualTy varargs)
+    | I_call (a, mr, varargs) -> I_call(a, conv_mspec mr, morphILVarArgs factualTy varargs)
+    | I_callvirt (a, mr, varargs) -> I_callvirt(a, conv_mspec mr, morphILVarArgs factualTy varargs)
+    | I_callconstraint (a, ty, mr, varargs) -> I_callconstraint(a, factualTy ty, conv_mspec mr, morphILVarArgs factualTy varargs)
+    | I_newobj (mr, varargs) -> I_newobj(conv_mspec mr, morphILVarArgs factualTy varargs)
+    | I_ldftn mr -> I_ldftn(conv_mspec mr)
+    | I_ldvirtftn mr -> I_ldvirtftn(conv_mspec mr)
+    | I_ldfld (a, b, fr) -> I_ldfld(a, b, conv_fspec fr)
+    | I_ldsfld (a, fr) -> I_ldsfld(a, conv_fspec fr)
+    | I_ldsflda fr -> I_ldsflda(conv_fspec fr)
+    | I_ldflda fr -> I_ldflda(conv_fspec fr)
+    | I_stfld (a, b, fr) -> I_stfld(a, b, conv_fspec fr)
+    | I_stsfld (a, fr) -> I_stsfld(a, conv_fspec fr)
+    | I_castclass ty -> I_castclass(factualTy ty)
+    | I_isinst ty -> I_isinst(factualTy ty)
+    | I_initobj ty -> I_initobj(factualTy ty)
+    | I_cpobj ty -> I_cpobj(factualTy ty)
+    | I_stobj (al, vol, ty) -> I_stobj(al, vol, factualTy ty)
+    | I_ldobj (al, vol, ty) -> I_ldobj(al, vol, factualTy ty)
+    | I_box ty -> I_box(factualTy ty)
+    | I_unbox ty -> I_unbox(factualTy ty)
+    | I_unbox_any ty -> I_unbox_any(factualTy ty)
+    | I_ldelem_any (shape, ty) -> I_ldelem_any(shape, factualTy ty)
+    | I_stelem_any (shape, ty) -> I_stelem_any(shape, factualTy ty)
+    | I_newarr (shape, ty) -> I_newarr(shape, factualTy ty)
+    | I_ldelema (ro, isNativePtr, shape, ty) -> I_ldelema(ro, isNativePtr, shape, factualTy ty)
+    | I_sizeof ty -> I_sizeof(factualTy ty)
+    | I_ldtoken tok ->
+        match tok with
+        | ILToken.ILType ty -> I_ldtoken(ILToken.ILType(factualTy ty))
+        | ILToken.ILMethod mr -> I_ldtoken(ILToken.ILMethod(conv_mspec mr))
+        | ILToken.ILField fr -> I_ldtoken(ILToken.ILField(conv_fspec fr))
     | x -> x
 
-let morphILReturn f (r:ILReturn) =
-    {r with
-        Type=f r.Type
-        CustomAttrsStored= storeILCustomAttrs (cattrs_ty2ty f r.CustomAttrs)}
+let morphILReturn f (r: ILReturn) =
+    { r with
+        Type = f r.Type
+        CustomAttrsStored = storeILCustomAttrs (cattrs_ty2ty f r.CustomAttrs)
+    }
 
 let morphILParameter f (p: ILParameter) =
     { p with
-        Type=f p.Type
-        CustomAttrsStored= storeILCustomAttrs (cattrs_ty2ty f p.CustomAttrs)}
+        Type = f p.Type
+        CustomAttrsStored = storeILCustomAttrs (cattrs_ty2ty f p.CustomAttrs)
+    }
 
-let morphILMethodDefs f (m:ILMethodDefs) =
-    mkILMethods (List.map f (m.AsList()))
+let morphILMethodDefs f (m: ILMethodDefs) = mkILMethods (List.map f (m.AsList()))
 
 let morphILFieldDefs f (fdefs: ILFieldDefs) =
     mkILFields (List.map f (fdefs.AsList()))
@@ -228,45 +263,48 @@ let morphILFieldDefs f (fdefs: ILFieldDefs) =
 let morphILTypeDefs f (tdefs: ILTypeDefs) =
     mkILTypeDefsFromArray (Array.map f (tdefs.AsArray()))
 
-let morphILLocals f locals =
-    List.map (morphILLocal f) locals
+let morphILLocals f locals = List.map (morphILLocal f) locals
+
+let ilmbody_instr2instr_ty2ty fs (ilmbody: ILMethodBody) =
+    let finstr, fTyInCtxt = fs
 
-let ilmbody_instr2instr_ty2ty fs (ilmbody: ILMethodBody) = 
-    let finstr, fTyInCtxt = fs 
-    {ilmbody with
-        Code=code_instr2instr_ty2ty (finstr, fTyInCtxt) ilmbody.Code
-        Locals = morphILLocals fTyInCtxt ilmbody.Locals }
+    { ilmbody with
+        Code = code_instr2instr_ty2ty (finstr, fTyInCtxt) ilmbody.Code
+        Locals = morphILLocals fTyInCtxt ilmbody.Locals
+    }
 
-let morphILMethodBody fMethBody (x: MethodBody) = 
+let morphILMethodBody fMethBody (x: MethodBody) =
     match x with
-    | MethodBody.IL il -> 
+    | MethodBody.IL il ->
         let ilCode = fMethBody il.Value // Eager
-        MethodBody.IL (lazy ilCode)
+        MethodBody.IL(lazy ilCode)
     | x -> x
 
-let ospec_ty2ty f (OverridesSpec(mref,ty)) = OverridesSpec(mref_ty2ty f mref, f ty)
+let ospec_ty2ty f (OverridesSpec (mref, ty)) = OverridesSpec(mref_ty2ty f mref, f ty)
+
+let mdef_ty2ty_ilmbody2ilmbody fs (md: ILMethodDef) =
+    let fTyInCtxt, fMethBody = fs
+    let fTyInCtxtR = fTyInCtxt (Some md)
+    let bodyR = morphILMethodBody (fMethBody (Some md)) md.Body
 
-let mdef_ty2ty_ilmbody2ilmbody fs (md: ILMethodDef)  = 
-    let fTyInCtxt,fMethBody = fs 
-    let fTyInCtxtR = fTyInCtxt (Some md) 
-    let bodyR = morphILMethodBody (fMethBody (Some md)) md.Body 
     md.With(
-        genericParams=gparams_ty2ty fTyInCtxtR md.GenericParams,
-        body= notlazy bodyR,
+        genericParams = gparams_ty2ty fTyInCtxtR md.GenericParams,
+        body = notlazy bodyR,
         parameters = List.map (morphILParameter fTyInCtxtR) md.Parameters,
         ret = morphILReturn fTyInCtxtR md.Return,
-        customAttrs=cattrs_ty2ty fTyInCtxtR md.CustomAttrs
+        customAttrs = cattrs_ty2ty fTyInCtxtR md.CustomAttrs
     )
 
-let fdefs_ty2ty f fdefs =
-    morphILFieldDefs (fdef_ty2ty f) fdefs
+let fdefs_ty2ty f fdefs = morphILFieldDefs (fdef_ty2ty f) fdefs
 
 let mdefs_ty2ty_ilmbody2ilmbody fs mdefs =
     morphILMethodDefs (mdef_ty2ty_ilmbody2ilmbody fs) mdefs
 
 let mimpl_ty2ty f mimpl =
-    { Overrides = ospec_ty2ty f mimpl.Overrides
-      OverrideBy = mspec_ty2ty (f,(fun _ -> f)) mimpl.OverrideBy; }
+    {
+        Overrides = ospec_ty2ty f mimpl.Overrides
+        OverrideBy = mspec_ty2ty (f, (fun _ -> f)) mimpl.OverrideBy
+    }
 
 let edef_ty2ty f (edef: ILEventDef) =
     edef.With(
@@ -293,64 +331,77 @@ let pdefs_ty2ty f (pdefs: ILPropertyDefs) =
 let edefs_ty2ty f (edefs: ILEventDefs) =
     mkILEvents (edefs.AsList() |> List.map (edef_ty2ty f))
 
-let mimpls_ty2ty f (mimpls : ILMethodImplDefs) =
+let mimpls_ty2ty f (mimpls: ILMethodImplDefs) =
     mkILMethodImpls (mimpls.AsList() |> List.map (mimpl_ty2ty f))
 
-let rec tdef_ty2ty_ilmbody2ilmbody_mdefs2mdefs enc fs (tdef: ILTypeDef) = 
-   let fTyInCtxt,fMethodDefs = fs 
-   let fTyInCtxtR = fTyInCtxt (Some (enc,tdef)) None 
-   let mdefsR = fMethodDefs (enc, tdef) tdef.Methods 
-   let fdefsR = fdefs_ty2ty fTyInCtxtR tdef.Fields 
-   tdef.With(
-       implements= List.map fTyInCtxtR tdef.Implements,
-       genericParams= gparams_ty2ty fTyInCtxtR tdef.GenericParams,
-       extends = Option.map fTyInCtxtR tdef.Extends,
-       methods=mdefsR,
-       nestedTypes=tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs (enc@[tdef]) fs tdef.NestedTypes,
-       fields=fdefsR,
-       methodImpls = mimpls_ty2ty fTyInCtxtR tdef.MethodImpls,
-       events = edefs_ty2ty fTyInCtxtR tdef.Events,
-       properties = pdefs_ty2ty fTyInCtxtR tdef.Properties,
-       customAttrs = cattrs_ty2ty fTyInCtxtR tdef.CustomAttrs
-   )
-
-and tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs enc fs tdefs = 
-  morphILTypeDefs (tdef_ty2ty_ilmbody2ilmbody_mdefs2mdefs enc fs) tdefs
+let rec tdef_ty2ty_ilmbody2ilmbody_mdefs2mdefs enc fs (tdef: ILTypeDef) =
+    let fTyInCtxt, fMethodDefs = fs
+    let fTyInCtxtR = fTyInCtxt (Some(enc, tdef)) None
+    let mdefsR = fMethodDefs (enc, tdef) tdef.Methods
+    let fdefsR = fdefs_ty2ty fTyInCtxtR tdef.Fields
+
+    tdef.With(
+        implements = List.map fTyInCtxtR tdef.Implements,
+        genericParams = gparams_ty2ty fTyInCtxtR tdef.GenericParams,
+        extends = Option.map fTyInCtxtR tdef.Extends,
+        methods = mdefsR,
+        nestedTypes = tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs (enc @ [ tdef ]) fs tdef.NestedTypes,
+        fields = fdefsR,
+        methodImpls = mimpls_ty2ty fTyInCtxtR tdef.MethodImpls,
+        events = edefs_ty2ty fTyInCtxtR tdef.Events,
+        properties = pdefs_ty2ty fTyInCtxtR tdef.Properties,
+        customAttrs = cattrs_ty2ty fTyInCtxtR tdef.CustomAttrs
+    )
+
+and tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs enc fs tdefs =
+    morphILTypeDefs (tdef_ty2ty_ilmbody2ilmbody_mdefs2mdefs enc fs) tdefs
 
 // --------------------------------------------------------------------
 // Derived versions of the above, e.g. with defaults added
-// -------------------------------------------------------------------- 
+// --------------------------------------------------------------------
 
-let manifest_ty2ty f (m : ILAssemblyManifest) =
-    { m with CustomAttrsStored = storeILCustomAttrs (cattrs_ty2ty f m.CustomAttrs) }
+let manifest_ty2ty f (m: ILAssemblyManifest) =
+    { m with
+        CustomAttrsStored = storeILCustomAttrs (cattrs_ty2ty f m.CustomAttrs)
+    }
 
-let morphILTypeInILModule_ilmbody2ilmbody_mdefs2mdefs (fTyInCtxt: ILModuleDef -> (ILTypeDef list * ILTypeDef) option -> ILMethodDef option -> ILType -> ILType, fMethodDefs) modul = 
+let morphILTypeInILModule_ilmbody2ilmbody_mdefs2mdefs
+    (fTyInCtxt: ILModuleDef -> (ILTypeDef list * ILTypeDef) option -> ILMethodDef option -> ILType -> ILType, fMethodDefs)
+    modul
+    =
 
-    let ftdefs = tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs [] (fTyInCtxt modul, fMethodDefs modul) 
+    let ftdefs =
+        tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs [] (fTyInCtxt modul, fMethodDefs modul)
 
     { modul with
-        TypeDefs=ftdefs modul.TypeDefs
-        CustomAttrsStored= storeILCustomAttrs (cattrs_ty2ty (fTyInCtxt modul None None) modul.CustomAttrs)
-        Manifest=Option.map (manifest_ty2ty (fTyInCtxt modul None None)) modul.Manifest  }
-    
-let morphILInstrsAndILTypesInILModule fs modul = 
-    let fCode, fTyInCtxt = fs 
-    let fMethBody modCtxt tdefCtxt mdefCtxt = ilmbody_instr2instr_ty2ty (fCode modCtxt tdefCtxt mdefCtxt, fTyInCtxt modCtxt (Some tdefCtxt) mdefCtxt) 
-    let fMethodDefs modCtxt tdefCtxt = mdefs_ty2ty_ilmbody2ilmbody (fTyInCtxt modCtxt (Some tdefCtxt), fMethBody modCtxt tdefCtxt) 
-    morphILTypeInILModule_ilmbody2ilmbody_mdefs2mdefs (fTyInCtxt, fMethodDefs) modul 
-
-let morphILInstrsInILCode f ilcode =
-    code_instr2instrs f ilcode
-
-let morphILTypeInILModule fTyInCtxt modul = 
+        TypeDefs = ftdefs modul.TypeDefs
+        CustomAttrsStored = storeILCustomAttrs (cattrs_ty2ty (fTyInCtxt modul None None) modul.CustomAttrs)
+        Manifest = Option.map (manifest_ty2ty (fTyInCtxt modul None None)) modul.Manifest
+    }
+
+let morphILInstrsAndILTypesInILModule fs modul =
+    let fCode, fTyInCtxt = fs
+
+    let fMethBody modCtxt tdefCtxt mdefCtxt =
+        ilmbody_instr2instr_ty2ty (fCode modCtxt tdefCtxt mdefCtxt, fTyInCtxt modCtxt (Some tdefCtxt) mdefCtxt)
+
+    let fMethodDefs modCtxt tdefCtxt =
+        mdefs_ty2ty_ilmbody2ilmbody (fTyInCtxt modCtxt (Some tdefCtxt), fMethBody modCtxt tdefCtxt)
+
+    morphILTypeInILModule_ilmbody2ilmbody_mdefs2mdefs (fTyInCtxt, fMethodDefs) modul
+
+let morphILInstrsInILCode f ilcode = code_instr2instrs f ilcode
+
+let morphILTypeInILModule fTyInCtxt modul =
     let finstr modCtxt tdefCtxt mdefCtxt =
-        let fTy = fTyInCtxt modCtxt (Some tdefCtxt) mdefCtxt 
-        morphILTypesInILInstr ((fun _instrCtxt -> fTy), (fun _instrCtxt _formalCtxt -> fTy)) 
+        let fTy = fTyInCtxt modCtxt (Some tdefCtxt) mdefCtxt
+        morphILTypesInILInstr ((fun _instrCtxt -> fTy), (fun _instrCtxt _formalCtxt -> fTy))
+
     morphILInstrsAndILTypesInILModule (finstr, fTyInCtxt) modul
 
-let morphILTypeRefsInILModuleMemoized f modul = 
+let morphILTypeRefsInILModuleMemoized f modul =
     let fTy = Tables.memoize (morphILTypeRefsInILType f)
     morphILTypeInILModule (fun _ _ _ ty -> fTy ty) modul
 
-let morphILScopeRefsInILModuleMemoized f modul = 
+let morphILScopeRefsInILModuleMemoized f modul =
     morphILTypeRefsInILModuleMemoized (morphILScopeRefsInILTypeRef f) modul
diff --git a/src/Compiler/AbstractIL/ilnativeres.fs b/src/Compiler/AbstractIL/ilnativeres.fs
index fbb790235d3..8bbf79f9afc 100644
--- a/src/Compiler/AbstractIL/ilnativeres.fs
+++ b/src/Compiler/AbstractIL/ilnativeres.fs
@@ -31,13 +31,13 @@ let inline WCHAR s = char s
 let inline BYTE s = byte s
 
 type ResourceException(name: string, ?inner: Exception MaybeNull) =
-    inherit Exception (name, Option.toObj inner)
+    inherit Exception(name, Option.toObj inner)
 
-type RESOURCE_STRING () =
+type RESOURCE_STRING() =
     member val Ordinal = Unchecked.defaultof with get, set
     member val theString = Unchecked.defaultof with get, set
 
-type RESOURCE () =
+type RESOURCE() =
     member val pstringType = Unchecked.defaultof with get, set
     member val pstringName = Unchecked.defaultof with get, set
     member val DataSize = Unchecked.defaultof with get, set
@@ -49,23 +49,27 @@ type RESOURCE () =
     member val Characteristics = Unchecked.defaultof with get, set
     member val data = Unchecked.defaultof with get, set
 
-type CvtResFile () =
+type CvtResFile() =
     static member val private RT_DLGINCLUDE = 17 with get, set
 
-    static member ReadResFile (stream: Stream) = 
-        let mutable reader = new BinaryReader (stream, Encoding.Unicode)
+    static member ReadResFile(stream: Stream) =
+        let mutable reader = new BinaryReader(stream, Encoding.Unicode)
         let mutable resourceNames = List()
 
         // The stream might be empty, so let's check
-        if not (reader.PeekChar () = -1) then
+        if not (reader.PeekChar() = -1) then
             let mutable startPos = stream.Position
-            let mutable initial32Bits = reader.ReadUInt32 ()
+            let mutable initial32Bits = reader.ReadUInt32()
+
             if initial32Bits <> uint32 0 then
-                raise <| ResourceException(FSComp.SR.nativeResourceFormatError())
+                raise <| ResourceException(FSComp.SR.nativeResourceFormatError ())
+
             stream.Position <- startPos
+
             while (stream.Position < stream.Length) do
-                let mutable cbData = reader.ReadUInt32 ()
-                let mutable cbHdr = reader.ReadUInt32 ()
+                let mutable cbData = reader.ReadUInt32()
+                let mutable cbHdr = reader.ReadUInt32()
+
                 if cbHdr < 2u * uint32 sizeof then
                     // TODO:
                     //      Current FSComp.txt converter doesn't yet support %x and %lx so format it as a string
@@ -73,6 +77,7 @@ type CvtResFile () =
                     // The conversion fix flows through to the lkg
                     let msg = String.Format("0x{0:x}", stream.Position - 8L)
                     raise <| ResourceException(FSComp.SR.nativeResourceHeaderMalformed msg)
+
                 if cbData = 0u then
                     stream.Position <- stream.Position + int64 cbHdr - 2L * int64 sizeof
                 else
@@ -81,36 +86,42 @@ type CvtResFile () =
                     pAdditional.DataSize <- cbData
                     pAdditional.pstringType <- CvtResFile.ReadStringOrID reader
                     pAdditional.pstringName <- CvtResFile.ReadStringOrID reader
-                    stream.Position <- stream.Position + 3L &&& ~~~3L
-                    pAdditional.DataVersion <- reader.ReadUInt32 ()
-                    pAdditional.MemoryFlags <- reader.ReadUInt16 ()
-                    pAdditional.LanguageId <- reader.ReadUInt16 ()
-                    pAdditional.Version <- reader.ReadUInt32 ()
-                    pAdditional.Characteristics <- reader.ReadUInt32 ()
+                    stream.Position <- stream.Position + 3L &&& ~~~ 3L
+                    pAdditional.DataVersion <- reader.ReadUInt32()
+                    pAdditional.MemoryFlags <- reader.ReadUInt16()
+                    pAdditional.LanguageId <- reader.ReadUInt16()
+                    pAdditional.Version <- reader.ReadUInt32()
+                    pAdditional.Characteristics <- reader.ReadUInt32()
                     pAdditional.data <- Array.zeroCreate (int pAdditional.DataSize)
-                    reader.Read (pAdditional.data, 0, pAdditional.data.Length) |> ignore
-                    stream.Position <- stream.Position + 3L &&& ~~~3L
-                    if pAdditional.pstringType.theString = Unchecked.defaultof<_> && (pAdditional.pstringType.Ordinal = uint16 CvtResFile.RT_DLGINCLUDE) then
+                    reader.Read(pAdditional.data, 0, pAdditional.data.Length) |> ignore
+                    stream.Position <- stream.Position + 3L &&& ~~~ 3L
+
+                    if pAdditional.pstringType.theString = Unchecked.defaultof<_>
+                       && (pAdditional.pstringType.Ordinal = uint16 CvtResFile.RT_DLGINCLUDE) then
                         () (* ERROR ContinueNotSupported *)
                     else
                         resourceNames.Add pAdditional
+
         resourceNames
 
-    static member private ReadStringOrID (fhIn: BinaryReader) =
-        let mutable (pstring: RESOURCE_STRING) = RESOURCE_STRING ()
-        let mutable (firstWord: WCHAR) = (fhIn.ReadChar ())
+    static member private ReadStringOrID(fhIn: BinaryReader) =
+        let mutable (pstring: RESOURCE_STRING) = RESOURCE_STRING()
+        let mutable (firstWord: WCHAR) = (fhIn.ReadChar())
+
         if int firstWord = 0xFFFF then
-            pstring.Ordinal <- fhIn.ReadUInt16 ()
+            pstring.Ordinal <- fhIn.ReadUInt16()
         else
             pstring.Ordinal <- uint16 0xFFFF
-            let mutable (sb: StringBuilder) = StringBuilder ()
+            let mutable (sb: StringBuilder) = StringBuilder()
             let mutable (curChar: WCHAR) = firstWord
+
             while (curChar <> char 0) do
                 sb.Append(curChar) |> ignore
-                curChar <- fhIn.ReadChar ()
-            pstring.theString <- sb.ToString ()
-        pstring
+                curChar <- fhIn.ReadChar()
+
+            pstring.theString <- sb.ToString()
 
+        pstring
 
 []
 type SectionCharacteristics =
@@ -162,105 +173,146 @@ type SectionCharacteristics =
     | MemWrite = 2147483648u
 
 type ResourceSection() =
-    new(sectionBytes: byte[], relocations: uint32[]) as this = 
-        (ResourceSection ())
+    new(sectionBytes: byte[], relocations: uint32[]) as this =
+        (ResourceSection())
         then
-            Debug.Assert (sectionBytes :> obj <> Unchecked.defaultof<_>)
-            Debug.Assert (relocations :> obj <> Unchecked.defaultof<_>)
+            Debug.Assert(sectionBytes :> obj <> Unchecked.defaultof<_>)
+            Debug.Assert(relocations :> obj <> Unchecked.defaultof<_>)
             this.SectionBytes <- sectionBytes
             this.Relocations <- relocations
 
-    member val SectionBytes = Unchecked.defaultof with get,set
-    member val Relocations = Unchecked.defaultof with get,set
+    member val SectionBytes = Unchecked.defaultof with get, set
+    member val Relocations = Unchecked.defaultof with get, set
 
 []
-type StreamExtensions () =
+type StreamExtensions() =
     []
-    static member TryReadAll (stream: Stream, buffer: byte[], offset: int, count: int) = 
-        Debug.Assert (count > 0)
+    static member TryReadAll(stream: Stream, buffer: byte[], offset: int, count: int) =
+        Debug.Assert(count > 0)
         let mutable (totalBytesRead: int) = Unchecked.defaultof
         let mutable (isFinished: bool) = false
         let mutable (bytesRead: int) = 0
-        do 
+
+        do
             totalBytesRead <- 0
+
             while totalBytesRead < count && not isFinished do
-                bytesRead <- stream.Read (buffer, (offset + totalBytesRead), (count - totalBytesRead))
+                bytesRead <- stream.Read(buffer, (offset + totalBytesRead), (count - totalBytesRead))
+
                 if bytesRead = 0 then
                     isFinished <- true // break;
-                else totalBytesRead <- totalBytesRead + bytesRead
+                else
+                    totalBytesRead <- totalBytesRead + bytesRead
+
         totalBytesRead
 
 type COFFResourceReader() =
-    static member private ConfirmSectionValues (hdr: SectionHeader, fileSize: int64) = 
+    static member private ConfirmSectionValues(hdr: SectionHeader, fileSize: int64) =
         if int64 hdr.PointerToRawData + int64 hdr.SizeOfRawData > fileSize then
             raise <| ResourceException "CoffResourceInvalidSectionSize"
 
-    static member ReadWin32ResourcesFromCOFF (stream: Stream) = 
+    static member ReadWin32ResourcesFromCOFF(stream: Stream) =
         let mutable peHeaders = PEHeaders(stream)
-        let mutable rsrc1 = SectionHeader ()
-        let mutable rsrc2 = SectionHeader ()
+        let mutable rsrc1 = SectionHeader()
+        let mutable rsrc2 = SectionHeader()
         let mutable (foundCount: int) = 0
+
         for sectionHeader in peHeaders.SectionHeaders do
             if sectionHeader.Name = ".rsrc$01" then
                 rsrc1 <- sectionHeader
                 foundCount <- foundCount + 1
-            else 
-                if sectionHeader.Name = ".rsrc$02" then
-                    rsrc2 <- sectionHeader
-                    foundCount <- foundCount + 1
+            else if sectionHeader.Name = ".rsrc$02" then
+                rsrc2 <- sectionHeader
+                foundCount <- foundCount + 1
+
         if foundCount <> 2 then
             raise <| ResourceException "CoffResourceMissingSection"
-        COFFResourceReader.ConfirmSectionValues (rsrc1, stream.Length)
-        COFFResourceReader.ConfirmSectionValues (rsrc2, stream.Length)
-        let mutable imageResourceSectionBytes = Array.zeroCreate (rsrc1.SizeOfRawData + rsrc2.SizeOfRawData)
-        stream.Seek (int64 rsrc1.PointerToRawData, SeekOrigin.Begin) |> ignore
-        stream.TryReadAll (imageResourceSectionBytes, 0, rsrc1.SizeOfRawData) |> ignore
-        stream.Seek (int64 rsrc2.PointerToRawData, SeekOrigin.Begin) |> ignore
-        stream.TryReadAll (imageResourceSectionBytes, rsrc1.SizeOfRawData, rsrc2.SizeOfRawData) |> ignore
+
+        COFFResourceReader.ConfirmSectionValues(rsrc1, stream.Length)
+        COFFResourceReader.ConfirmSectionValues(rsrc2, stream.Length)
+
+        let mutable imageResourceSectionBytes =
+            Array.zeroCreate (rsrc1.SizeOfRawData + rsrc2.SizeOfRawData)
+
+        stream.Seek(int64 rsrc1.PointerToRawData, SeekOrigin.Begin) |> ignore
+
+        stream.TryReadAll(imageResourceSectionBytes, 0, rsrc1.SizeOfRawData)
+        |> ignore
+
+        stream.Seek(int64 rsrc2.PointerToRawData, SeekOrigin.Begin) |> ignore
+
+        stream.TryReadAll(imageResourceSectionBytes, rsrc1.SizeOfRawData, rsrc2.SizeOfRawData)
+        |> ignore
+
         let mutable (SizeOfRelocationEntry: int) = 10
+
         try
-            let mutable relocLastAddress = rsrc1.PointerToRelocations + (int rsrc1.NumberOfRelocations * SizeOfRelocationEntry)
+            let mutable relocLastAddress =
+                rsrc1.PointerToRelocations
+                + (int rsrc1.NumberOfRelocations * SizeOfRelocationEntry)
+
             if int64 relocLastAddress > stream.Length then
                 raise <| ResourceException "CoffResourceInvalidRelocation"
         with
-            :? OverflowException -> (raise <| ResourceException("CoffResourceInvalidRelocation"))
+        | :? OverflowException -> (raise <| ResourceException("CoffResourceInvalidRelocation"))
+
         let mutable relocationOffsets = Array.zeroCreate (int rsrc1.NumberOfRelocations)
-        let mutable relocationSymbolIndices = Array.zeroCreate (int rsrc1.NumberOfRelocations)
-        let mutable reader = new BinaryReader (stream, Encoding.Unicode)
+
+        let mutable relocationSymbolIndices =
+            Array.zeroCreate (int rsrc1.NumberOfRelocations)
+
+        let mutable reader = new BinaryReader(stream, Encoding.Unicode)
         stream.Position <- int64 rsrc1.PointerToRelocations
-        do 
+
+        do
             let mutable (i: int) = 0
+
             while (i < int rsrc1.NumberOfRelocations) do
-                relocationOffsets[i] <- reader.ReadUInt32 ()
-                relocationSymbolIndices[i] <- reader.ReadUInt32 ()
-                reader.ReadUInt16 () |> ignore //we do nothing with the "Type"
+                relocationOffsets[i] <- reader.ReadUInt32()
+                relocationSymbolIndices[i] <- reader.ReadUInt32()
+                reader.ReadUInt16() |> ignore //we do nothing with the "Type"
                 i <- i + 1
+
         stream.Position <- int64 peHeaders.CoffHeader.PointerToSymbolTable
         let mutable (ImageSizeOfSymbol: uint32) = 18u
+
         try
-            let mutable lastSymAddress = int64 peHeaders.CoffHeader.PointerToSymbolTable + int64 peHeaders.CoffHeader.NumberOfSymbols * int64 ImageSizeOfSymbol (* ERROR UnknownNode *)
+            let mutable lastSymAddress =
+                int64 peHeaders.CoffHeader.PointerToSymbolTable
+                + int64 peHeaders.CoffHeader.NumberOfSymbols
+                  * int64 ImageSizeOfSymbol (* ERROR UnknownNode *)
+
             if lastSymAddress > stream.Length then
                 raise <| ResourceException "CoffResourceInvalidSymbol"
         with
-            :? OverflowException -> (raise <| ResourceException("CoffResourceInvalidSymbol"))
-        let mutable outputStream = new MemoryStream (imageResourceSectionBytes)
-        let mutable writer = new BinaryWriter (outputStream)
-        do 
+        | :? OverflowException -> (raise <| ResourceException("CoffResourceInvalidSymbol"))
+
+        let mutable outputStream = new MemoryStream(imageResourceSectionBytes)
+        let mutable writer = new BinaryWriter(outputStream)
+
+        do
             let mutable (i: int) = 0
+
             while (i < relocationSymbolIndices.Length) do
                 if int relocationSymbolIndices[i] > peHeaders.CoffHeader.NumberOfSymbols then
                     raise <| ResourceException "CoffResourceInvalidRelocation"
-                let mutable offsetOfSymbol = int64 peHeaders.CoffHeader.PointerToSymbolTable + int64 relocationSymbolIndices[i] * int64 ImageSizeOfSymbol
+
+                let mutable offsetOfSymbol =
+                    int64 peHeaders.CoffHeader.PointerToSymbolTable
+                    + int64 relocationSymbolIndices[i] * int64 ImageSizeOfSymbol
+
                 stream.Position <- offsetOfSymbol
                 stream.Position <- stream.Position + 8L
-                let mutable symValue = reader.ReadUInt32 ()
-                let mutable symSection = reader.ReadInt16 ()
-                let mutable symType = reader.ReadUInt16 ()
+                let mutable symValue = reader.ReadUInt32()
+                let mutable symSection = reader.ReadInt16()
+                let mutable symType = reader.ReadUInt16()
                 let mutable (IMAGE_SYM_TYPE_NULL: uint16) = uint16 0x0000
+
                 if symType <> IMAGE_SYM_TYPE_NULL || symSection <> 3s then
                     raise <| ResourceException("CoffResourceInvalidSymbol")
+
                 outputStream.Position <- int64 relocationOffsets[i]
-                writer.Write (uint32 (int64 symValue + int64 rsrc1.SizeOfRawData))
+                writer.Write(uint32 (int64 symValue + int64 rsrc1.SizeOfRawData))
                 i <- i + 1
 
         ResourceSection(imageResourceSectionBytes, relocationOffsets)
@@ -285,8 +337,8 @@ type VersionHelper() =
     /// If parsing succeeds, the parsed version. Otherwise a version that represents as much of the input as could be parsed successfully.
     ///
     /// True when parsing succeeds completely (i.e. every character in the string was consumed), false otherwise.
-    static member TryParse(s: string, [] version: byref) = 
-        VersionHelper.TryParse (s, false, UInt16.MaxValue, true, ref version)
+    static member TryParse(s: string, [] version: byref) =
+        VersionHelper.TryParse(s, false, UInt16.MaxValue, true, ref version)
 
     /// 
     /// Parses a version string of the form "major [ '.' minor [ '.' ( '*' | ( build [ '.' ( '*' | revision ) ] ) ) ] ]"
@@ -302,8 +354,8 @@ type VersionHelper() =
     ///
     /// True when parsing succeeds completely (i.e. every character in the string was consumed), false otherwise.
 
-    static member TryParseAssemblyVersion (s: string, allowWildcard: bool, [] version: byref) = 
-        VersionHelper.TryParse (s, allowWildcard, (UInt16.MaxValue - 1us), false, ref version)
+    static member TryParseAssemblyVersion(s: string, allowWildcard: bool, [] version: byref) =
+        VersionHelper.TryParse(s, allowWildcard, (UInt16.MaxValue - 1us), false, ref version)
 
     static member private NullVersion = Version(0, 0, 0, 0)
 
@@ -322,103 +374,133 @@ type VersionHelper() =
     /// 
     ///
     /// True when parsing succeeds completely (i.e. every character in the string was consumed), false otherwise.
-    static member private TryParse(s: string, allowWildcard: bool, maxValue: uint16, allowPartialParse: bool, [] version: byref) = 
-        Debug.Assert (not allowWildcard || maxValue < UInt16.MaxValue)
+    static member private TryParse
+        (
+            s: string,
+            allowWildcard: bool,
+            maxValue: uint16,
+            allowPartialParse: bool,
+            [] version: byref
+        ) =
+        Debug.Assert(not allowWildcard || maxValue < UInt16.MaxValue)
+
         if String.IsNullOrWhiteSpace s then
             version <- VersionHelper.NullVersion
             false
         else
             let mutable (elements: string[]) = s.Split '.'
-            let mutable (hasWildcard: bool) = allowWildcard && elements[(int (elements.Length - 1))] = "*"
+
+            let mutable (hasWildcard: bool) =
+                allowWildcard && elements[(int (elements.Length - 1))] = "*"
+
             if hasWildcard && elements.Length < 3 || elements.Length > 4 then
                 version <- VersionHelper.NullVersion
                 false
             else
                 let mutable (values: uint16[]) = Array.zeroCreate 4
-                let mutable (lastExplicitValue: int) = 
+
+                let mutable (lastExplicitValue: int) =
                     if hasWildcard then
                         elements.Length - 1
-                    else elements.Length
+                    else
+                        elements.Length
+
                 let mutable (parseError: bool) = false
                 let mutable earlyReturn = None
-                do 
+
+                do
                     let mutable (i: int) = 0
                     let mutable breakLoop = false
+
                     while (i < lastExplicitValue) && not breakLoop do
-                        if not (UInt16.TryParse (elements[i], NumberStyles.None, CultureInfo.InvariantCulture, ref values[i])) || values[i] > maxValue then
+                        if
+                            not (UInt16.TryParse(elements[i], NumberStyles.None, CultureInfo.InvariantCulture, ref values[i]))
+                            || values[i] > maxValue
+                        then
                             if not allowPartialParse then
                                 earlyReturn <- Some false
                                 breakLoop <- true
                                 version <- VersionHelper.NullVersion
                             else
                                 parseError <- true
+
                                 if String.IsNullOrWhiteSpace elements[i] then
                                     values[i] <- 0us
                                     breakLoop <- true
+                                else if values[i] > maxValue then
+                                    values[i] <- 0us
+                                    breakLoop <- true
                                 else
-                                    if values[i] > maxValue then
-                                        values[i] <- 0us
-                                        breakLoop <- true
-                                    else
-                                        let mutable (invalidFormat: bool) = false
-                                        //let mutable (number: bigint) = 0I
-                                        do 
-                                            let mutable idx = 0
-                                            let mutable breakLoop = false
-                                            while (idx < elements[i].Length) && not breakLoop do
-                                                if not (Char.IsDigit elements[i].[idx]) then
-                                                    invalidFormat <- true
-                                                    VersionHelper.TryGetValue ((elements[i].Substring (0, idx)), ref values[i]) |> ignore
-                                                    breakLoop <- true
-                                                else
-                                                    idx <- idx + 1
-                                        let mutable doBreak = true
-                                        if not invalidFormat then
-                                            if VersionHelper.TryGetValue (elements[i], ref values[i]) then
-                                                //For this scenario the old compiler would continue processing the remaining version elements
-                                                //so continue processing
-                                                doBreak <- false
-                                                () (* ERROR ContinueNotSupported *)
-                                        (* ERROR BreakNotSupported *)
-                        if not breakLoop then
-                            i <- i + 1
+                                    let mutable (invalidFormat: bool) = false
+                                    //let mutable (number: bigint) = 0I
+                                    do
+                                        let mutable idx = 0
+                                        let mutable breakLoop = false
+
+                                        while (idx < elements[i].Length) && not breakLoop do
+                                            if not (Char.IsDigit elements[i].[idx]) then
+                                                invalidFormat <- true
+
+                                                VersionHelper.TryGetValue((elements[ i ].Substring(0, idx)), ref values[i])
+                                                |> ignore
+
+                                                breakLoop <- true
+                                            else
+                                                idx <- idx + 1
+
+                                    let mutable doBreak = true
+
+                                    if not invalidFormat then
+                                        if VersionHelper.TryGetValue(elements[i], ref values[i]) then
+                                            //For this scenario the old compiler would continue processing the remaining version elements
+                                            //so continue processing
+                                            doBreak <- false
+                                            () (* ERROR ContinueNotSupported *)
+                        (* ERROR BreakNotSupported *)
+                        if not breakLoop then i <- i + 1
+
                 if hasWildcard then
                     let mutable (i: int) = lastExplicitValue
+
                     while (i < values.Length) do
                         values[i] <- UInt16.MaxValue
                         i <- i + 1
+
                 version <- Version(int values[0], int values[1], int values[2], int values[3])
                 not parseError
 
-    static member private TryGetValue(s: string, [] value: byref): bool = 
+    static member private TryGetValue(s: string, [] value: byref) : bool =
         let mutable (number: bigint) = Unchecked.defaultof
-        if bigint.TryParse (s, NumberStyles.None, CultureInfo.InvariantCulture, ref number) then
+
+        if bigint.TryParse(s, NumberStyles.None, CultureInfo.InvariantCulture, ref number) then
             value <- uint16 (number % bigint 65536)
             true
         else
             value <- 0us
             false
 
-    static member GenerateVersionFromPatternAndCurrentTime(time: DateTime, pattern: Version) = 
+    static member GenerateVersionFromPatternAndCurrentTime(time: DateTime, pattern: Version) =
         if pattern = Unchecked.defaultof<_> || pattern.Revision <> int UInt16.MaxValue then
             pattern
         else
             let mutable time = time
-            // MSDN doc on the attribute: 
-            // "The default build number increments daily. The default revision number is the number of seconds since midnight local time 
+            // MSDN doc on the attribute:
+            // "The default build number increments daily. The default revision number is the number of seconds since midnight local time
             // (without taking into account time zone adjustments for daylight saving time), divided by 2."
             if time = Unchecked.defaultof then
                 time <- DateTime.Now
+
             let mutable (revision: int) = int time.TimeOfDay.TotalSeconds / 2
-            Debug.Assert (revision < int UInt16.MaxValue)
+            Debug.Assert(revision < int UInt16.MaxValue)
+
             if pattern.Build = int UInt16.MaxValue then
                 let mutable (days: TimeSpan) = time.Date - DateTime(2000, 1, 1)
-                let mutable (build: int) = Math.Min (int UInt16.MaxValue, (int days.TotalDays))
+                let mutable (build: int) = Math.Min(int UInt16.MaxValue, (int days.TotalDays))
                 Version(pattern.Major, pattern.Minor, int (uint16 build), int (uint16 revision))
             else
                 Version(pattern.Major, pattern.Minor, pattern.Build, int (uint16 revision))
 
-type VersionResourceSerializer () =
+type VersionResourceSerializer() =
     member val private _commentsContents = Unchecked.defaultof with get, set
     member val private _companyNameContents = Unchecked.defaultof with get, set
     member val private _fileDescriptionContents = Unchecked.defaultof with get, set
@@ -452,9 +534,9 @@ type VersionResourceSerializer () =
         originalFileName: string,
         productName: string,
         productVersion: string,
-        assemblyVersion: Version) as this = 
+        assemblyVersion: Version) as this =
 
-        VersionResourceSerializer ()
+        VersionResourceSerializer()
         then
             this._isDll <- isDll
             this._commentsContents <- comments
@@ -468,51 +550,68 @@ type VersionResourceSerializer () =
             this._productNameContents <- productName
             this._productVersionContents <- productVersion
             this._assemblyVersionContents <- assemblyVersion
-            this._langIdAndCodePageKey <- String.Format ("{0:x4}{1:x4}", 0, VersionResourceSerializer.CP_WINUNICODE)
+            this._langIdAndCodePageKey <- String.Format("{0:x4}{1:x4}", 0, VersionResourceSerializer.CP_WINUNICODE)
 
     static member val private VFT_APP = 0x00000001u
     static member val private VFT_DLL = 0x00000002u
 
-    member private this.GetVerStrings() = seq {
-        if this._commentsContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("Comments", this._commentsContents)
-        if this._companyNameContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("CompanyName", this._companyNameContents)
-        if this._fileDescriptionContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("FileDescription", this._fileDescriptionContents)
-        yield KeyValuePair<_,_>("FileVersion", this._fileVersionContents)
-        if this._internalNameContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("InternalName", this._internalNameContents)
-        if this._legalCopyrightContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("LegalCopyright", this._legalCopyrightContents)
-        if this._legalTrademarksContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("LegalTrademarks", this._legalTrademarksContents)
-        if this._originalFileNameContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("OriginalFilename", this._originalFileNameContents)
-        if this._productNameContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("ProductName", this._productNameContents)
-        yield KeyValuePair<_,_>("ProductVersion", this._fileVersionContents)
-        if this._assemblyVersionContents <> Unchecked.defaultof<_> then
-            yield KeyValuePair<_,_>("Assembly Version", this._assemblyVersionContents.ToString())
-    }
-
-    member private this.FileType : uint32 = 
+    member private this.GetVerStrings() =
+        seq {
+            if this._commentsContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("Comments", this._commentsContents)
+
+            if this._companyNameContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("CompanyName", this._companyNameContents)
+
+            if this._fileDescriptionContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("FileDescription", this._fileDescriptionContents)
+
+            yield KeyValuePair<_, _>("FileVersion", this._fileVersionContents)
+
+            if this._internalNameContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("InternalName", this._internalNameContents)
+
+            if this._legalCopyrightContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("LegalCopyright", this._legalCopyrightContents)
+
+            if this._legalTrademarksContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("LegalTrademarks", this._legalTrademarksContents)
+
+            if this._originalFileNameContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("OriginalFilename", this._originalFileNameContents)
+
+            if this._productNameContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("ProductName", this._productNameContents)
+
+            yield KeyValuePair<_, _>("ProductVersion", this._fileVersionContents)
+
+            if this._assemblyVersionContents <> Unchecked.defaultof<_> then
+                yield KeyValuePair<_, _>("Assembly Version", this._assemblyVersionContents.ToString())
+        }
+
+    member private this.FileType: uint32 =
         if this._isDll then
             VersionResourceSerializer.VFT_DLL
         else
-             VersionResourceSerializer.VFT_APP
+            VersionResourceSerializer.VFT_APP
 
-    member private this.WriteVSFixedFileInfo(writer: BinaryWriter) = 
+    member private this.WriteVSFixedFileInfo(writer: BinaryWriter) =
         let mutable (fileVersion: Version) = Unchecked.defaultof
-        VersionHelper.TryParse (this._fileVersionContents, ref fileVersion) |> ignore
+
+        VersionHelper.TryParse(this._fileVersionContents, ref fileVersion)
+        |> ignore
+
         let mutable (productVersion: Version) = Unchecked.defaultof
-        VersionHelper.TryParse (this._productVersionContents, ref productVersion) |> ignore
+
+        VersionHelper.TryParse(this._productVersionContents, ref productVersion)
+        |> ignore
+
         writer.Write 0xFEEF04BDu
         writer.Write 0x00010000u
-        writer.Write ((uint32 fileVersion.Major <<< 16) ||| uint32 fileVersion.Minor)
-        writer.Write ((uint32 fileVersion.Build <<< 16) ||| uint32 fileVersion.Revision)
-        writer.Write ((uint32 productVersion.Major <<< 16) ||| uint32 productVersion.Minor)
-        writer.Write ((uint32 productVersion.Build <<< 16) ||| uint32 productVersion.Revision)
+        writer.Write((uint32 fileVersion.Major <<< 16) ||| uint32 fileVersion.Minor)
+        writer.Write((uint32 fileVersion.Build <<< 16) ||| uint32 fileVersion.Revision)
+        writer.Write((uint32 productVersion.Major <<< 16) ||| uint32 productVersion.Minor)
+        writer.Write((uint32 productVersion.Build <<< 16) ||| uint32 productVersion.Revision)
         writer.Write 0x0000003Fu
         writer.Write 0u
         writer.Write 0x00000004u
@@ -521,173 +620,264 @@ type VersionResourceSerializer () =
         writer.Write 0u
         writer.Write 0u
 
-    static member private PadKeyLen(cb: int) = 
-        VersionResourceSerializer.PadToDword (cb + 3 * sizeof) - 3 * sizeof
+    static member private PadKeyLen(cb: int) =
+        VersionResourceSerializer.PadToDword(cb + 3 * sizeof) - 3 * sizeof
 
-    static member private PadToDword(cb: int) = 
-        cb + 3 &&& ~~~3
+    static member private PadToDword(cb: int) = cb + 3 &&& ~~~ 3
 
     static member val private HDRSIZE = (int (3 * sizeof)) with get, set
 
-    static member private SizeofVerString(lpszKey: string, lpszValue: string) = 
+    static member private SizeofVerString(lpszKey: string, lpszValue: string) =
         let mutable (cbKey: int) = Unchecked.defaultof
         let mutable (cbValue: int) = Unchecked.defaultof
         cbKey <- lpszKey.Length + 1 * 2
         cbValue <- lpszValue.Length + 1 * 2
-        VersionResourceSerializer.PadKeyLen(cbKey) + cbValue + VersionResourceSerializer.HDRSIZE
 
-    static member private WriteVersionString(keyValuePair: KeyValuePair, writer: BinaryWriter) = 
-        Debug.Assert (keyValuePair.Value <> Unchecked.defaultof<_>)
-        let mutable (cbBlock: uint16) = uint16 <| VersionResourceSerializer.SizeofVerString (keyValuePair.Key, keyValuePair.Value)
+        VersionResourceSerializer.PadKeyLen(cbKey)
+        + cbValue
+        + VersionResourceSerializer.HDRSIZE
+
+    static member private WriteVersionString(keyValuePair: KeyValuePair, writer: BinaryWriter) =
+        Debug.Assert(keyValuePair.Value <> Unchecked.defaultof<_>)
+
+        let mutable (cbBlock: uint16) =
+            uint16
+            <| VersionResourceSerializer.SizeofVerString(keyValuePair.Key, keyValuePair.Value)
+
         let mutable (cbKey: int) = keyValuePair.Key.Length + 1 * 2
         //let mutable (cbVal: int) = keyValuePair.Value.Length + 1 * 2
         let mutable startPos = writer.BaseStream.Position
-        Debug.Assert (startPos &&& 3L = 0L)
+        Debug.Assert(startPos &&& 3L = 0L)
         writer.Write cbBlock
-        writer.Write (uint16 (keyValuePair.Value.Length + 1))
+        writer.Write(uint16 (keyValuePair.Value.Length + 1))
         writer.Write 1us
-        writer.Write (keyValuePair.Key.ToCharArray ())
-        writer.Write (uint16 0) //(WORD)'\0'
-        writer.Write (Array.zeroCreate (VersionResourceSerializer.PadKeyLen cbKey - cbKey): byte[])
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
-        writer.Write (keyValuePair.Value.ToCharArray ())
-        writer.Write (uint16 0) // (WORD)'\0'
-        Debug.Assert (int64 cbBlock = writer.BaseStream.Position - startPos)
-
-    static member private KEYSIZE(sz: string) = 
-        VersionResourceSerializer.PadKeyLen (sz.Length + 1 * sizeof) / sizeof
-
-    static member private KEYBYTES(sz: string) = 
+        writer.Write(keyValuePair.Key.ToCharArray())
+        writer.Write(uint16 0) //(WORD)'\0'
+        writer.Write(Array.zeroCreate (VersionResourceSerializer.PadKeyLen cbKey - cbKey): byte[])
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
+        writer.Write(keyValuePair.Value.ToCharArray())
+        writer.Write(uint16 0) // (WORD)'\0'
+        Debug.Assert(int64 cbBlock = writer.BaseStream.Position - startPos)
+
+    static member private KEYSIZE(sz: string) =
+        VersionResourceSerializer.PadKeyLen(sz.Length + 1 * sizeof)
+        / sizeof
+
+    static member private KEYBYTES(sz: string) =
         VersionResourceSerializer.KEYSIZE sz * sizeof
 
-    member private this.GetStringsSize() = 
+    member private this.GetStringsSize() =
         let mutable (sum: int) = 0
-        for verString in this.GetVerStrings () do
-            sum <- sum + 3 &&& ~~~3
-            sum <- sum + VersionResourceSerializer.SizeofVerString (verString.Key, verString.Value)
+
+        for verString in this.GetVerStrings() do
+            sum <- sum + 3 &&& ~~~ 3
+            sum <- sum + VersionResourceSerializer.SizeofVerString(verString.Key, verString.Value)
+
         sum
 
-    member this.GetDataSize () = 
+    member this.GetDataSize() =
         let mutable (sizeEXEVERRESOURCE: int) =
-             sizeof * 3 * 5 + 2 *  sizeof +
-             VersionResourceSerializer.KEYBYTES VersionResourceSerializer.vsVersionInfoKey +
-             VersionResourceSerializer.KEYBYTES VersionResourceSerializer.varFileInfoKey +
-             VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey +
-             VersionResourceSerializer.KEYBYTES VersionResourceSerializer.stringFileInfoKey +
-             VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey +
-             int VersionResourceSerializer.sizeVS_FIXEDFILEINFO
-        this.GetStringsSize () + sizeEXEVERRESOURCE
-
-    member this.WriteVerResource (writer: BinaryWriter) = 
+            sizeof * 3 * 5
+            + 2 * sizeof
+            + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.vsVersionInfoKey
+            + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.varFileInfoKey
+            + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey
+            + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.stringFileInfoKey
+            + VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey
+            + int VersionResourceSerializer.sizeVS_FIXEDFILEINFO
+
+        this.GetStringsSize() + sizeEXEVERRESOURCE
+
+    member this.WriteVerResource(writer: BinaryWriter) =
         let mutable debugPos = writer.BaseStream.Position
-        let mutable dataSize = this.GetDataSize ()
-        writer.Write (WORD dataSize)
-        writer.Write (WORD VersionResourceSerializer.sizeVS_FIXEDFILEINFO)
-        writer.Write (WORD 0us)
-        writer.Write (VersionResourceSerializer.vsVersionInfoKey.ToCharArray ())
-        writer.Write (Array.zeroCreate (VersionResourceSerializer.KEYBYTES VersionResourceSerializer.vsVersionInfoKey - VersionResourceSerializer.vsVersionInfoKey.Length * 2): byte[])
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
+        let mutable dataSize = this.GetDataSize()
+        writer.Write(WORD dataSize)
+        writer.Write(WORD VersionResourceSerializer.sizeVS_FIXEDFILEINFO)
+        writer.Write(WORD 0us)
+        writer.Write(VersionResourceSerializer.vsVersionInfoKey.ToCharArray())
+
+        writer.Write(
+            Array.zeroCreate (
+                VersionResourceSerializer.KEYBYTES VersionResourceSerializer.vsVersionInfoKey
+                - VersionResourceSerializer.vsVersionInfoKey.Length * 2
+            ): byte[]
+        )
+
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
         this.WriteVSFixedFileInfo writer
-        writer.Write (WORD (sizeof * 2 +
-                            2 * VersionResourceSerializer.HDRSIZE +
-                            VersionResourceSerializer.KEYBYTES VersionResourceSerializer.varFileInfoKey +
-                            VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey))
-        writer.Write (WORD 0us)
-        writer.Write (WORD 1us)
-        writer.Write (VersionResourceSerializer.varFileInfoKey.ToCharArray ())
-        writer.Write (Array.zeroCreate (VersionResourceSerializer.KEYBYTES VersionResourceSerializer.varFileInfoKey - VersionResourceSerializer.varFileInfoKey.Length * 2): byte[])
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
-        writer.Write (WORD (sizeof * 2 + VersionResourceSerializer.HDRSIZE + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey))
-        writer.Write (WORD (sizeof * 2))
-        writer.Write (WORD 0us)
-        writer.Write (VersionResourceSerializer.translationKey.ToCharArray ())
-        writer.Write (Array.zeroCreate (VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey - VersionResourceSerializer.translationKey.Length * 2): byte[])
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
+
+        writer.Write(
+            WORD(
+                sizeof * 2
+                + 2 * VersionResourceSerializer.HDRSIZE
+                + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.varFileInfoKey
+                + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey
+            )
+        )
+
+        writer.Write(WORD 0us)
+        writer.Write(WORD 1us)
+        writer.Write(VersionResourceSerializer.varFileInfoKey.ToCharArray())
+
+        writer.Write(
+            Array.zeroCreate (
+                VersionResourceSerializer.KEYBYTES VersionResourceSerializer.varFileInfoKey
+                - VersionResourceSerializer.varFileInfoKey.Length * 2
+            ): byte[]
+        )
+
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
+
+        writer.Write(
+            WORD(
+                sizeof * 2
+                + VersionResourceSerializer.HDRSIZE
+                + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey
+            )
+        )
+
+        writer.Write(WORD(sizeof * 2))
+        writer.Write(WORD 0us)
+        writer.Write(VersionResourceSerializer.translationKey.ToCharArray())
+
+        writer.Write(
+            Array.zeroCreate (
+                VersionResourceSerializer.KEYBYTES VersionResourceSerializer.translationKey
+                - VersionResourceSerializer.translationKey.Length * 2
+            ): byte[]
+        )
+
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
         writer.Write 0us
-        writer.Write (WORD VersionResourceSerializer.CP_WINUNICODE)
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
-        writer.Write (WORD (2 * VersionResourceSerializer.HDRSIZE +
-                            VersionResourceSerializer.KEYBYTES VersionResourceSerializer.stringFileInfoKey +
-                            VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey + this.GetStringsSize ()))
+        writer.Write(WORD VersionResourceSerializer.CP_WINUNICODE)
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
+
+        writer.Write(
+            WORD(
+                2 * VersionResourceSerializer.HDRSIZE
+                + VersionResourceSerializer.KEYBYTES VersionResourceSerializer.stringFileInfoKey
+                + VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey
+                + this.GetStringsSize()
+            )
+        )
+
         writer.Write 0us
         writer.Write 1us
-        writer.Write (VersionResourceSerializer.stringFileInfoKey.ToCharArray ())
-        writer.Write (Array.zeroCreate (VersionResourceSerializer.KEYBYTES VersionResourceSerializer.stringFileInfoKey - VersionResourceSerializer.stringFileInfoKey.Length * 2): byte[])
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
-        writer.Write (WORD (VersionResourceSerializer.HDRSIZE + VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey + this.GetStringsSize ()))
+        writer.Write(VersionResourceSerializer.stringFileInfoKey.ToCharArray())
+
+        writer.Write(
+            Array.zeroCreate (
+                VersionResourceSerializer.KEYBYTES VersionResourceSerializer.stringFileInfoKey
+                - VersionResourceSerializer.stringFileInfoKey.Length * 2
+            ): byte[]
+        )
+
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
+
+        writer.Write(
+            WORD(
+                VersionResourceSerializer.HDRSIZE
+                + VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey
+                + this.GetStringsSize()
+            )
+        )
+
         writer.Write 0us
         writer.Write 1us
-        writer.Write (this._langIdAndCodePageKey.ToCharArray ())
-        writer.Write (Array.zeroCreate (VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey - this._langIdAndCodePageKey.Length * 2): byte[])
-        Debug.Assert (writer.BaseStream.Position &&& 3L = 0L)
-        Debug.Assert (writer.BaseStream.Position - debugPos = int64 dataSize - int64  (this.GetStringsSize ()))
+        writer.Write(this._langIdAndCodePageKey.ToCharArray())
+
+        writer.Write(
+            Array.zeroCreate (
+                VersionResourceSerializer.KEYBYTES this._langIdAndCodePageKey
+                - this._langIdAndCodePageKey.Length * 2
+            ): byte[]
+        )
+
+        Debug.Assert(writer.BaseStream.Position &&& 3L = 0L)
+        Debug.Assert(writer.BaseStream.Position - debugPos = int64 dataSize - int64 (this.GetStringsSize()))
         debugPos <- writer.BaseStream.Position
-        for entry in this.GetVerStrings () do
+
+        for entry in this.GetVerStrings() do
             let mutable writerPos = writer.BaseStream.Position
-            writer.Write (Array.zeroCreate (int ((writerPos + 3L) &&& ~~~3L - writerPos)): byte[])
-            Debug.Assert (entry.Value <> Unchecked.defaultof<_>)
-            VersionResourceSerializer.WriteVersionString (entry, writer)
-        Debug.Assert (writer.BaseStream.Position - debugPos = int64 (this.GetStringsSize ()))
+            writer.Write(Array.zeroCreate (int ((writerPos + 3L) &&& ~~~ 3L - writerPos)): byte[])
+            Debug.Assert(entry.Value <> Unchecked.defaultof<_>)
+            VersionResourceSerializer.WriteVersionString(entry, writer)
+
+        Debug.Assert(writer.BaseStream.Position - debugPos = int64 (this.GetStringsSize()))
 
-type Win32ResourceConversions () =
-    static member AppendIconToResourceStream(resStream: Stream, iconStream: Stream) = 
+type Win32ResourceConversions() =
+    static member AppendIconToResourceStream(resStream: Stream, iconStream: Stream) =
         let mutable iconReader = new BinaryReader(iconStream)
-        let mutable reserved = iconReader.ReadUInt16 ()
+        let mutable reserved = iconReader.ReadUInt16()
+
         if reserved <> 0us then
             raise <| ResourceException("IconStreamUnexpectedFormat")
-        let mutable ``type`` = iconReader.ReadUInt16 ()
+
+        let mutable ``type`` = iconReader.ReadUInt16()
+
         if ``type`` <> 1us then
             raise <| ResourceException("IconStreamUnexpectedFormat")
-        let mutable count = iconReader.ReadUInt16 ()
+
+        let mutable count = iconReader.ReadUInt16()
+
         if count = 0us then
             raise <| ResourceException("IconStreamUnexpectedFormat")
-        let mutable iconDirEntries: ICONDIRENTRY [] = Array.zeroCreate (int count)
-        do 
+
+        let mutable iconDirEntries: ICONDIRENTRY[] = Array.zeroCreate (int count)
+
+        do
             let mutable (i: uint16) = 0us
+
             while (i < count) do
-                iconDirEntries[(int i)].bWidth <- iconReader.ReadByte ()
-                iconDirEntries[(int i)].bHeight <- iconReader.ReadByte ()
-                iconDirEntries[(int i)].bColorCount <- iconReader.ReadByte ()
-                iconDirEntries[(int i)].bReserved <- iconReader.ReadByte ()
-                iconDirEntries[(int i)].wPlanes <- iconReader.ReadUInt16 ()
-                iconDirEntries[(int i)].wBitCount <- iconReader.ReadUInt16 ()
-                iconDirEntries[(int i)].dwBytesInRes <- iconReader.ReadUInt32 ()
-                iconDirEntries[(int i)].dwImageOffset <- iconReader.ReadUInt32 ()
+                iconDirEntries[(int i)].bWidth <- iconReader.ReadByte()
+                iconDirEntries[(int i)].bHeight <- iconReader.ReadByte()
+                iconDirEntries[(int i)].bColorCount <- iconReader.ReadByte()
+                iconDirEntries[(int i)].bReserved <- iconReader.ReadByte()
+                iconDirEntries[(int i)].wPlanes <- iconReader.ReadUInt16()
+                iconDirEntries[(int i)].wBitCount <- iconReader.ReadUInt16()
+                iconDirEntries[(int i)].dwBytesInRes <- iconReader.ReadUInt32()
+                iconDirEntries[(int i)].dwImageOffset <- iconReader.ReadUInt32()
                 i <- i + 1us
+
         do
             let mutable (i: uint16) = 0us
+
             while (i < count) do
                 iconStream.Position <- int64 iconDirEntries[(int i)].dwImageOffset
-                if iconReader.ReadUInt32 () = 40u then 
+
+                if iconReader.ReadUInt32() = 40u then
                     iconStream.Position <- iconStream.Position + 8L
-                    iconDirEntries[(int i)].wPlanes <- iconReader.ReadUInt16 ()
-                    iconDirEntries[(int i)].wBitCount <- iconReader.ReadUInt16 ()
+                    iconDirEntries[(int i)].wPlanes <- iconReader.ReadUInt16()
+                    iconDirEntries[(int i)].wBitCount <- iconReader.ReadUInt16()
+
                 i <- i + 1us
 
         let mutable resWriter = new BinaryWriter(resStream)
         let mutable (RT_ICON: WORD) = 3us
+
         do
             let mutable (i: uint16) = 0us
+
             while (i < count) do
-                resStream.Position <- resStream.Position + 3L &&& ~~~3L
+                resStream.Position <- resStream.Position + 3L &&& ~~~ 3L
                 resWriter.Write iconDirEntries[(int i)].dwBytesInRes
                 resWriter.Write 0x00000020u
                 resWriter.Write 0xFFFFus
                 resWriter.Write RT_ICON
                 resWriter.Write 0xFFFFus
-                resWriter.Write (i + 1us)
+                resWriter.Write(i + 1us)
                 resWriter.Write 0x00000000u
                 resWriter.Write 0x1010us
                 resWriter.Write 0x0000us
                 resWriter.Write 0x00000000u
                 resWriter.Write 0x00000000u
                 iconStream.Position <- int64 iconDirEntries[(int i)].dwImageOffset
-                resWriter.Write (iconReader.ReadBytes (int iconDirEntries[int i].dwBytesInRes))
+                resWriter.Write(iconReader.ReadBytes(int iconDirEntries[int i].dwBytesInRes))
                 i <- i + 1us
 
         let mutable (RT_GROUP_ICON: WORD) = (RT_ICON + 11us)
-        resStream.Position <- resStream.Position + 3L &&& ~~~3L
-        resWriter.Write (uint32 (3 * sizeof + int count * 14))
+        resStream.Position <- resStream.Position + 3L &&& ~~~ 3L
+        resWriter.Write(uint32 (3 * sizeof + int count * 14))
         resWriter.Write 0x00000020u
         resWriter.Write 0xFFFFus
         resWriter.Write RT_GROUP_ICON
@@ -701,8 +891,10 @@ type Win32ResourceConversions () =
         resWriter.Write 0x0000us
         resWriter.Write 0x0001us
         resWriter.Write count
+
         do
             let mutable (i: uint16) = 0us
+
             while (i < count) do
                 resWriter.Write iconDirEntries[(int i)].bWidth
                 resWriter.Write iconDirEntries[(int i)].bHeight
@@ -711,11 +903,14 @@ type Win32ResourceConversions () =
                 resWriter.Write iconDirEntries[(int i)].wPlanes
                 resWriter.Write iconDirEntries[(int i)].wBitCount
                 resWriter.Write iconDirEntries[(int i)].dwBytesInRes
-                resWriter.Write (i + 1us)
+                resWriter.Write(i + 1us)
                 i <- i + 1us
+
         ()
 
-    static member AppendVersionToResourceStream (resStream: Stream, 
+    static member AppendVersionToResourceStream
+        (
+            resStream: Stream,
             isDll: bool,
             fileVersion: string,
             originalFileName: string,
@@ -727,7 +922,8 @@ type Win32ResourceConversions () =
             ?legalTrademarks: string,
             ?productName: string,
             ?comments: string,
-            ?companyName: string) = 
+            ?companyName: string
+        ) =
         let fileDescription = (defaultArg fileDescription) " "
         let legalCopyright = (defaultArg legalCopyright) " "
         let legalTrademarks = (defaultArg legalTrademarks) Unchecked.defaultof<_>
@@ -735,20 +931,32 @@ type Win32ResourceConversions () =
         let comments = (defaultArg comments) Unchecked.defaultof<_>
         let companyName = (defaultArg companyName) Unchecked.defaultof<_>
         let mutable resWriter = new BinaryWriter(resStream, Encoding.Unicode)
-        resStream.Position <- resStream.Position + 3L &&& ~~~3L
+        resStream.Position <- resStream.Position + 3L &&& ~~~ 3L
         let mutable (RT_VERSION: DWORD) = 16u
+
         let mutable ver =
-            VersionResourceSerializer(isDll, comments, companyName,
-                fileDescription, fileVersion, internalName, legalCopyright,
-                legalTrademarks, originalFileName, productName, productVersion,
-                assemblyVersion)
+            VersionResourceSerializer(
+                isDll,
+                comments,
+                companyName,
+                fileDescription,
+                fileVersion,
+                internalName,
+                legalCopyright,
+                legalTrademarks,
+                originalFileName,
+                productName,
+                productVersion,
+                assemblyVersion
+            )
+
         let mutable startPos = resStream.Position
-        let mutable dataSize = ver.GetDataSize ()
+        let mutable dataSize = ver.GetDataSize()
         let mutable (headerSize: int) = 0x20
-        resWriter.Write (uint32 dataSize)
-        resWriter.Write (uint32 headerSize)
+        resWriter.Write(uint32 dataSize)
+        resWriter.Write(uint32 headerSize)
         resWriter.Write 0xFFFFus
-        resWriter.Write (uint16 RT_VERSION)
+        resWriter.Write(uint16 RT_VERSION)
         resWriter.Write 0xFFFFus
         resWriter.Write 0x0001us
         resWriter.Write 0x00000000u
@@ -757,18 +965,18 @@ type Win32ResourceConversions () =
         resWriter.Write 0x00000000u
         resWriter.Write 0x00000000u
         ver.WriteVerResource resWriter
-        Debug.Assert (resStream.Position - startPos = int64 dataSize + int64 headerSize)
+        Debug.Assert(resStream.Position - startPos = int64 dataSize + int64 headerSize)
 
-    static member AppendManifestToResourceStream(resStream: Stream, manifestStream: Stream, isDll: bool) = 
-        resStream.Position <- resStream.Position + 3L &&& ~~~3L (* ERROR UnknownPrefixOperator "~" *)
+    static member AppendManifestToResourceStream(resStream: Stream, manifestStream: Stream, isDll: bool) =
+        resStream.Position <- resStream.Position + 3L &&& ~~~ 3L (* ERROR UnknownPrefixOperator "~" *)
         let mutable (RT_MANIFEST: WORD) = 24us
         let mutable resWriter = new BinaryWriter(resStream)
-        resWriter.Write (uint32 manifestStream.Length)
+        resWriter.Write(uint32 manifestStream.Length)
         resWriter.Write 0x00000020u
         resWriter.Write 0xFFFFus
         resWriter.Write RT_MANIFEST
         resWriter.Write 0xFFFFus
-        resWriter.Write (if isDll then 0x0002us else 0x0001us)
+        resWriter.Write(if isDll then 0x0002us else 0x0001us)
         resWriter.Write 0x00000000u
         resWriter.Write 0x1030us
         resWriter.Write 0x0000us
@@ -776,8 +984,7 @@ type Win32ResourceConversions () =
         resWriter.Write 0x00000000u
         manifestStream.CopyTo resStream
 
-
-type Win32Resource (data: byte[], codePage: DWORD, languageId: DWORD, id: int, name: string, typeId: int, typeName: string) =
+type Win32Resource(data: byte[], codePage: DWORD, languageId: DWORD, id: int, name: string, typeId: int, typeName: string) =
     member val Data = data
     member val CodePage = codePage
     member val LanguageId = languageId
@@ -786,36 +993,39 @@ type Win32Resource (data: byte[], codePage: DWORD, languageId: DWORD, id: int, n
     member val TypeId = typeId
     member val TypeName = typeName
 
-type Directory (name, id) =
+type Directory(name, id) =
     member val Name = name
     member val ID = id
     member val NumberOfNamedEntries = Unchecked.defaultof with get, set
     member val NumberOfIdEntries = Unchecked.defaultof with get, set
     member val Entries = List()
 
-type NativeResourceWriter () =
-    static member private CompareResources (left: Win32Resource) (right: Win32Resource) = 
-        let mutable (result: int) = NativeResourceWriter.CompareResourceIdentifiers (left.TypeId, left.TypeName, right.TypeId, right.TypeName)
+type NativeResourceWriter() =
+    static member private CompareResources (left: Win32Resource) (right: Win32Resource) =
+        let mutable (result: int) =
+            NativeResourceWriter.CompareResourceIdentifiers(left.TypeId, left.TypeName, right.TypeId, right.TypeName)
+
         if result = 0 then
-            NativeResourceWriter.CompareResourceIdentifiers (left.Id, left.Name, right.Id, right.Name)
-        else result
+            NativeResourceWriter.CompareResourceIdentifiers(left.Id, left.Name, right.Id, right.Name)
+        else
+            result
 
-    static member private CompareResourceIdentifiers (xOrdinal: int, xString: string, yOrdinal: int, yString: string) = 
+    static member private CompareResourceIdentifiers(xOrdinal: int, xString: string, yOrdinal: int, yString: string) =
         if xString = Unchecked.defaultof<_> then
             if yString = Unchecked.defaultof<_> then
                 xOrdinal - yOrdinal
             else
                 1
+        else if yString = Unchecked.defaultof<_> then
+            -1
         else
-            if yString = Unchecked.defaultof<_> then
-                -1
-            else
-                String.Compare (xString, yString, StringComparison.OrdinalIgnoreCase)
+            String.Compare(xString, yString, StringComparison.OrdinalIgnoreCase)
 
-    static member SortResources (resources: IEnumerable) =
-        resources.OrderBy ((fun d -> d), Comparer<_>.Create(Comparison<_> NativeResourceWriter.CompareResources)) :> IEnumerable
+    static member SortResources(resources: IEnumerable) =
+        resources.OrderBy((fun d -> d), Comparer<_>.Create (Comparison<_> NativeResourceWriter.CompareResources))
+        :> IEnumerable
 
-    static member SerializeWin32Resources (builder: BlobBuilder, theResources: IEnumerable, resourcesRva: int) = 
+    static member SerializeWin32Resources(builder: BlobBuilder, theResources: IEnumerable, resourcesRva: int) =
         let theResources = NativeResourceWriter.SortResources theResources
         let mutable (typeDirectory: Directory) = Directory(String.Empty, 0)
         let mutable (nameDirectory: Directory) = Unchecked.defaultof<_>
@@ -825,42 +1035,67 @@ type NativeResourceWriter () =
         let mutable (lastID: int) = Int32.MinValue
         let mutable (lastName: string) = Unchecked.defaultof<_>
         let mutable (sizeOfDirectoryTree: uint32) = 16u
+
         for r: Win32Resource in theResources do
-            let mutable (typeDifferent: bool) = r.TypeId < 0 && r.TypeName <> lastTypeName || r.TypeId > lastTypeID
-            if typeDifferent then 
+            let mutable (typeDifferent: bool) =
+                r.TypeId < 0 && r.TypeName <> lastTypeName || r.TypeId > lastTypeID
+
+            if typeDifferent then
                 lastTypeID <- r.TypeId
                 lastTypeName <- r.TypeName
+
                 if lastTypeID < 0 then
-                    Debug.Assert ((typeDirectory.NumberOfIdEntries = 0us), "Not all Win32 resources with types encoded as strings precede those encoded as ints")
+                    Debug.Assert(
+                        (typeDirectory.NumberOfIdEntries = 0us),
+                        "Not all Win32 resources with types encoded as strings precede those encoded as ints"
+                    )
+
                     typeDirectory.NumberOfNamedEntries <- typeDirectory.NumberOfNamedEntries + 1us
-                else 
+                else
                     typeDirectory.NumberOfIdEntries <- typeDirectory.NumberOfIdEntries + 1us
+
                 sizeOfDirectoryTree <- sizeOfDirectoryTree + 24u
                 nameDirectory <- Directory(lastTypeName, lastTypeID)
                 typeDirectory.Entries.Add nameDirectory
-            if typeDifferent || r.Id < 0 && r.Name <> lastName || r.Id > lastID then 
+
+            if typeDifferent || r.Id < 0 && r.Name <> lastName || r.Id > lastID then
                 lastID <- r.Id
                 lastName <- r.Name
+
                 if lastID < 0 then
-                    Debug.Assert ((nameDirectory.NumberOfIdEntries = 0us), "Not all Win32 resources with names encoded as strings precede those encoded as ints")
+                    Debug.Assert(
+                        (nameDirectory.NumberOfIdEntries = 0us),
+                        "Not all Win32 resources with names encoded as strings precede those encoded as ints"
+                    )
+
                     nameDirectory.NumberOfNamedEntries <- nameDirectory.NumberOfNamedEntries + 1us
                 else
                     nameDirectory.NumberOfIdEntries <- nameDirectory.NumberOfIdEntries + 1us
+
                 sizeOfDirectoryTree <- sizeOfDirectoryTree + 24u
                 languageDirectory <- Directory(lastName, lastID)
                 nameDirectory.Entries.Add languageDirectory
+
             languageDirectory.NumberOfIdEntries <- languageDirectory.NumberOfIdEntries + 1us
             sizeOfDirectoryTree <- sizeOfDirectoryTree + 8u
             languageDirectory.Entries.Add r
+
         let mutable dataWriter = BlobBuilder()
-        NativeResourceWriter.WriteDirectory (typeDirectory, builder, 0u, 0u, sizeOfDirectoryTree, resourcesRva, dataWriter)
+        NativeResourceWriter.WriteDirectory(typeDirectory, builder, 0u, 0u, sizeOfDirectoryTree, resourcesRva, dataWriter)
         builder.LinkSuffix dataWriter
         builder.WriteByte 0uy
         builder.Align 4
 
-    static member private WriteDirectory (directory: Directory, writer: BlobBuilder, offset: uint32,
-            level: uint32, sizeOfDirectoryTree: uint32,
-            virtualAddressBase: int, dataWriter: BlobBuilder) = 
+    static member private WriteDirectory
+        (
+            directory: Directory,
+            writer: BlobBuilder,
+            offset: uint32,
+            level: uint32,
+            sizeOfDirectoryTree: uint32,
+            virtualAddressBase: int,
+            dataWriter: BlobBuilder
+        ) =
         writer.WriteUInt32 0u
         writer.WriteUInt32 0u
         writer.WriteUInt32 0u
@@ -868,89 +1103,121 @@ type NativeResourceWriter () =
         writer.WriteUInt16 directory.NumberOfIdEntries
         let mutable (n: uint32) = uint32 directory.Entries.Count
         let mutable (k: uint32) = offset + 16u + n * 8u
-        do 
+
+        do
             let mutable (i: uint32) = 0u
+
             while (i < n) do
                 let mutable (id: int) = Unchecked.defaultof
                 let mutable (name: string) = Unchecked.defaultof
                 let mutable (nameOffset: uint32) = uint32 dataWriter.Count + sizeOfDirectoryTree
                 let mutable (directoryOffset: uint32) = k
+
                 let isDir =
                     match directory.Entries[int i] with
                     | :? Directory as subDir ->
                         id <- subDir.ID
                         name <- subDir.Name
-                        if level = 0u then k <- k + NativeResourceWriter.SizeOfDirectory subDir
-                        else k <- k + 16u + 8u * uint32 subDir.Entries.Count
+
+                        if level = 0u then
+                            k <- k + NativeResourceWriter.SizeOfDirectory subDir
+                        else
+                            k <- k + 16u + 8u * uint32 subDir.Entries.Count
+
                         true
                     | :? Win32Resource as r ->
-                        id <- 
-                            if level = 0u then
-                                r.TypeId
-                            else 
-                                if level = 1u then
-                                    r.Id
-                                else
-                                    int r.LanguageId
-                        name <- 
-                            if level = 0u then
-                                r.TypeName
-                            else 
-                                if level = 1u then
-                                    r.Name
-                                else
-                                    Unchecked.defaultof<_>
-                        dataWriter.WriteUInt32 (uint32 virtualAddressBase + sizeOfDirectoryTree + 16u + uint32 dataWriter.Count)
-                        let mutable (data: byte[]) = (List(r.Data)).ToArray ()
-                        dataWriter.WriteUInt32 (uint32 data.Length)
+                        id <-
+                            if level = 0u then r.TypeId
+                            else if level = 1u then r.Id
+                            else int r.LanguageId
+
+                        name <-
+                            if level = 0u then r.TypeName
+                            else if level = 1u then r.Name
+                            else Unchecked.defaultof<_>
+
+                        dataWriter.WriteUInt32(uint32 virtualAddressBase + sizeOfDirectoryTree + 16u + uint32 dataWriter.Count)
+                        let mutable (data: byte[]) = (List(r.Data)).ToArray()
+                        dataWriter.WriteUInt32(uint32 data.Length)
                         dataWriter.WriteUInt32 r.CodePage
                         dataWriter.WriteUInt32 0u
                         dataWriter.WriteBytes data
+
                         while (dataWriter.Count % 4 <> 0) do
                             dataWriter.WriteByte 0uy
+
                         false
-                    | e -> failwithf "Unknown entry %s" (if isNull e then "" else e.GetType().FullName)
-                if id >= 0 then writer.WriteInt32 id
-                else 
+                    | e ->
+                        failwithf
+                            "Unknown entry %s"
+                            (if isNull e then
+                                 ""
+                             else
+                                 e.GetType().FullName)
+
+                if id >= 0 then
+                    writer.WriteInt32 id
+                else
                     if name = Unchecked.defaultof<_> then
                         name <- String.Empty
-                    writer.WriteUInt32 (nameOffset ||| 0x80000000u)
-                    dataWriter.WriteUInt16 (uint16 name.Length)
+
+                    writer.WriteUInt32(nameOffset ||| 0x80000000u)
+                    dataWriter.WriteUInt16(uint16 name.Length)
                     dataWriter.WriteUTF16 name
-                if isDir then writer.WriteUInt32 (directoryOffset ||| 0x80000000u)
-                else writer.WriteUInt32 nameOffset
+
+                if isDir then
+                    writer.WriteUInt32(directoryOffset ||| 0x80000000u)
+                else
+                    writer.WriteUInt32 nameOffset
+
                 i <- i + 1u
 
         k <- offset + 16u + n * 8u
-        do 
+
+        do
             let mutable (i: int) = 0
+
             while (uint32 i < n) do
                 match directory.Entries[i] with
                 | :? Directory as subDir ->
-                    NativeResourceWriter.WriteDirectory (subDir, writer, k, (level + 1u), sizeOfDirectoryTree, virtualAddressBase, dataWriter)
+                    NativeResourceWriter.WriteDirectory(
+                        subDir,
+                        writer,
+                        k,
+                        (level + 1u),
+                        sizeOfDirectoryTree,
+                        virtualAddressBase,
+                        dataWriter
+                    )
+
                     if level = 0u then
                         k <- k + NativeResourceWriter.SizeOfDirectory subDir
                     else
                         k <- k + 16u + 8u * uint32 subDir.Entries.Count
                 | _ -> ()
+
                 i <- i + 1
+
         ()
 
-    static member private SizeOfDirectory (directory: Directory) =
+    static member private SizeOfDirectory(directory: Directory) =
         let mutable (n: uint32) = uint32 directory.Entries.Count
         let mutable (size: uint32) = 16u + 8u * n
-        do 
+
+        do
             let mutable (i: int) = 0
+
             while (uint32 i < n) do
                 match directory.Entries[i] with
-                | :? Directory as subDir ->
-                    size <- size + 16u + 8u * uint32 subDir.Entries.Count
+                | :? Directory as subDir -> size <- size + 16u + 8u * uint32 subDir.Entries.Count
                 | _ -> ()
+
                 i <- i + 1
+
         size
 
-    (*
-    static member SerializeWin32Resources (builder: BlobBuilder, resourceSections: ResourceSection, resourcesRva: int) = 
+(*
+    static member SerializeWin32Resources (builder: BlobBuilder, resourceSections: ResourceSection, resourcesRva: int) =
         let mutable sectionWriter = new BlobWriter (builder.ReserveBytes (resourceSections.SectionBytes.Length))
         sectionWriter.WriteBytes (resourceSections.SectionBytes)
         let mutable readStream = new MemoryStream (resourceSections.SectionBytes)
@@ -959,4 +1226,4 @@ type NativeResourceWriter () =
             sectionWriter.Offset <- addressToFixup
             reader.BaseStream.Position <- addressToFixup
             sectionWriter.WriteUInt32 (reader.ReadUInt32 () + resourcesRva :> uint32)
-        ()*)
\ No newline at end of file
+        ()*)
diff --git a/src/Compiler/AbstractIL/ilprint.fs b/src/Compiler/AbstractIL/ilprint.fs
index ad0040ffc65..71efc7e0c4b 100644
--- a/src/Compiler/AbstractIL/ilprint.fs
+++ b/src/Compiler/AbstractIL/ilprint.fs
@@ -20,102 +20,143 @@ let pretty () = true
 // --------------------------------------------------------------------
 
 let tyvar_generator =
-  let mutable i = 0
-  fun n ->
-    i <- i + 1
-    n + string i
+    let mutable i = 0
+
+    fun n ->
+        i <- i + 1
+        n + string i
 
 // Carry an environment because the way we print method variables
 // depends on the gparams of the current scope.
 type ppenv =
-    { ilGlobals: ILGlobals
-      ppenvClassFormals: int
-      ppenvMethodFormals: int }
+    {
+        ilGlobals: ILGlobals
+        ppenvClassFormals: int
+        ppenvMethodFormals: int
+    }
 
-let ppenv_enter_method  mgparams env =
-    {env with ppenvMethodFormals=mgparams}
+let ppenv_enter_method mgparams env =
+    { env with
+        ppenvMethodFormals = mgparams
+    }
 
 let ppenv_enter_tdef gparams env =
-    {env with ppenvClassFormals=List.length gparams; ppenvMethodFormals=0}
-
-let mk_ppenv ilg = { ilGlobals = ilg; ppenvClassFormals = 0; ppenvMethodFormals = 0 }
+    { env with
+        ppenvClassFormals = List.length gparams
+        ppenvMethodFormals = 0
+    }
+
+let mk_ppenv ilg =
+    {
+        ilGlobals = ilg
+        ppenvClassFormals = 0
+        ppenvMethodFormals = 0
+    }
 
 let debug_ppenv = mk_ppenv
 
-let ppenv_enter_modul env = { env with  ppenvClassFormals=0; ppenvMethodFormals=0 }
+let ppenv_enter_modul env =
+    { env with
+        ppenvClassFormals = 0
+        ppenvMethodFormals = 0
+    }
 
 // --------------------------------------------------------------------
 // Pretty printing - output streams
 // --------------------------------------------------------------------
 
-let output_string (os: TextWriter) (s:string) = os.Write s
+let output_string (os: TextWriter) (s: string) = os.Write s
 
-let output_char (os: TextWriter) (c:char) = os.Write c
+let output_char (os: TextWriter) (c: char) = os.Write c
 
-let output_int os (i:int) = output_string os (string i)
+let output_int os (i: int) = output_string os (string i)
 
 let output_hex_digit os i =
-  assert (i >= 0 && i < 16)
-  if i > 9 then output_char os (char (int32 'A' + (i-10)))
-  else output_char os (char (int32 '0' + i))
+    assert (i >= 0 && i < 16)
 
-let output_qstring os s =
-  output_char os '"'
-  for i = 0 to String.length s - 1 do
-    let c = String.get s i
-    if (c >= '\000' && c <= '\031') || (c >= '\127' && c <= '\255') then
-      let c' = int32 c
-      output_char os '\\'
-      output_int os (c'/64)
-      output_int os ((c' % 64) / 8)
-      output_int os (c' % 8)
-    else if (c = '"')  then
-      output_char os '\\'; output_char os '"'
-    else if (c = '\\')  then
-      output_char os '\\'; output_char os '\\'
-    else
-      output_char os c
-  done
-  output_char os '"'
-let output_sqstring os s =
-  output_char os '\''
-  for i = 0 to String.length s - 1 do
-    let c = s[i]
-    if (c >= '\000' && c <= '\031') || (c >= '\127' && c <= '\255') then
-      let c' = int32 c
-      output_char os '\\'
-      output_int os (c'/64)
-      output_int os ((c' % 64) / 8)
-      output_int os (c' % 8)
-    else if (c = '\\')  then
-      output_char os '\\'; output_char os '\\'
-    else if (c = '\'')  then
-      output_char os '\\'; output_char os '\''
+    if i > 9 then
+        output_char os (char (int32 'A' + (i - 10)))
     else
-      output_char os c
-  done
-  output_char os '\''
+        output_char os (char (int32 '0' + i))
 
-let output_seq sep f os (a:seq<_>) =
-  use e = a.GetEnumerator()
-  if e.MoveNext() then
-      f os e.Current
-      while e.MoveNext() do
-          output_string os sep
-          f os e.Current
-
-let output_array sep f os (a:_ []) =
-  if not (Array.isEmpty a) then
-      for i in 0..a.Length-2 do
-        f os a[i]
-        output_string os sep
-      f os a[a.Length - 1]
-
-let output_parens f os a = output_string os "("; f os a; output_string os ")"
+let output_qstring os s =
+    output_char os '"'
+
+    for i = 0 to String.length s - 1 do
+        let c = String.get s i
+
+        if (c >= '\000' && c <= '\031') || (c >= '\127' && c <= '\255') then
+            let c' = int32 c
+            output_char os '\\'
+            output_int os (c' / 64)
+            output_int os ((c' % 64) / 8)
+            output_int os (c' % 8)
+        else if (c = '"') then
+            output_char os '\\'
+            output_char os '"'
+        else if (c = '\\') then
+            output_char os '\\'
+            output_char os '\\'
+        else
+            output_char os c
+
+    output_char os '"'
 
-let output_angled f os a = output_string os "<"; f os a; output_string os ">"
+let output_sqstring os s =
+    output_char os '\''
+
+    for i = 0 to String.length s - 1 do
+        let c = s[i]
+
+        if (c >= '\000' && c <= '\031') || (c >= '\127' && c <= '\255') then
+            let c' = int32 c
+            output_char os '\\'
+            output_int os (c' / 64)
+            output_int os ((c' % 64) / 8)
+            output_int os (c' % 8)
+        else if (c = '\\') then
+            output_char os '\\'
+            output_char os '\\'
+        else if (c = '\'') then
+            output_char os '\\'
+            output_char os '\''
+        else
+            output_char os c
+
+    output_char os '\''
+
+let output_seq sep f os (a: seq<_>) =
+    use e = a.GetEnumerator()
+
+    if e.MoveNext() then
+        f os e.Current
+
+        while e.MoveNext() do
+            output_string os sep
+            f os e.Current
+
+let output_array sep f os (a: _[]) =
+    if not (Array.isEmpty a) then
+        for i in 0 .. a.Length - 2 do
+            f os a[i]
+            output_string os sep
+
+        f os a[a.Length - 1]
+
+let output_parens f os a =
+    output_string os "("
+    f os a
+    output_string os ")"
+
+let output_angled f os a =
+    output_string os "<"
+    f os a
+    output_string os ">"
 
-let output_bracks f os a = output_string os "["; f os a; output_string os "]"
+let output_bracks f os a =
+    output_string os "["
+    f os a
+    output_string os "]"
 
 let output_id os n = output_sqstring os n
 
@@ -126,870 +167,1183 @@ let output_lid os lid = output_seq "." output_string os lid
 let string_of_type_name (_, n) = n
 
 let output_byte os i =
-  output_hex_digit os (i / 16)
-  output_hex_digit os (i % 16)
-
-let output_bytes os (bytes:byte[]) =
-  for i = 0 to bytes.Length - 1 do
-    output_byte os (Bytes.get bytes i)
-    output_string os " "
+    output_hex_digit os (i / 16)
+    output_hex_digit os (i % 16)
 
+let output_bytes os (bytes: byte[]) =
+    for i = 0 to bytes.Length - 1 do
+        output_byte os (Bytes.get bytes i)
+        output_string os " "
 
-let bits_of_float32 (x:float32) = System.BitConverter.ToInt32(System.BitConverter.GetBytes(x), 0)
+let bits_of_float32 (x: float32) =
+    System.BitConverter.ToInt32(System.BitConverter.GetBytes(x), 0)
 
-let bits_of_float (x:float) = System.BitConverter.DoubleToInt64Bits(x)
+let bits_of_float (x: float) =
+    System.BitConverter.DoubleToInt64Bits(x)
 
-let output_u8 os (x:byte) = output_string os (string (int x))
+let output_u8 os (x: byte) = output_string os (string (int x))
 
-let output_i8 os (x:sbyte) = output_string os (string (int x))
+let output_i8 os (x: sbyte) = output_string os (string (int x))
 
-let output_u16 os (x:uint16) = output_string os (string (int x))
+let output_u16 os (x: uint16) = output_string os (string (int x))
 
-let output_i16 os (x:int16) = output_string os (string (int x))
+let output_i16 os (x: int16) = output_string os (string (int x))
 
-let output_u32 os (x:uint32) = output_string os (string (int64 x))
+let output_u32 os (x: uint32) = output_string os (string (int64 x))
 
-let output_i32 os (x:int32) = output_string os (string x)
+let output_i32 os (x: int32) = output_string os (string x)
 
-let output_u64 os (x:uint64) = output_string os (string (int64 x))
+let output_u64 os (x: uint64) = output_string os (string (int64 x))
 
-let output_i64 os (x:int64) = output_string os (string x)
+let output_i64 os (x: int64) = output_string os (string x)
 
-let output_ieee32 os (x:float32) = output_string os "float32 ("; output_string os (string (bits_of_float32 x)); output_string os ")"
+let output_ieee32 os (x: float32) =
+    output_string os "float32 ("
+    output_string os (string (bits_of_float32 x))
+    output_string os ")"
 
-let output_ieee64 os (x:float) = output_string os "float64 ("; output_string os (string (bits_of_float x)); output_string os ")"
+let output_ieee64 os (x: float) =
+    output_string os "float64 ("
+    output_string os (string (bits_of_float x))
+    output_string os ")"
 
-let rec goutput_scoref env os = function
-  | ILScopeRef.Local -> ()
-  | ILScopeRef.Assembly aref ->
-      output_string os "["; output_sqstring os aref.Name; output_string os "]"
-  | ILScopeRef.Module mref ->
-      output_string os "[.module "; output_sqstring os mref.Name; output_string os "]"
-  | ILScopeRef.PrimaryAssembly ->
-      output_string os "["; output_sqstring os env.ilGlobals.primaryAssemblyName; output_string os "]"
+let rec goutput_scoref env os =
+    function
+    | ILScopeRef.Local -> ()
+    | ILScopeRef.Assembly aref ->
+        output_string os "["
+        output_sqstring os aref.Name
+        output_string os "]"
+    | ILScopeRef.Module mref ->
+        output_string os "[.module "
+        output_sqstring os mref.Name
+        output_string os "]"
+    | ILScopeRef.PrimaryAssembly ->
+        output_string os "["
+        output_sqstring os env.ilGlobals.primaryAssemblyName
+        output_string os "]"
 
 and goutput_type_name_ref env os (scoref, enc, n) =
-  goutput_scoref env os scoref
-  output_seq "/" output_sqstring os (enc@[n])
-and goutput_tref env os (x:ILTypeRef) =
-  goutput_type_name_ref env os (x.Scope, x.Enclosing, x.Name)
+    goutput_scoref env os scoref
+    output_seq "/" output_sqstring os (enc @ [ n ])
+
+and goutput_tref env os (x: ILTypeRef) =
+    goutput_type_name_ref env os (x.Scope, x.Enclosing, x.Name)
 
 and goutput_typ env os ty =
-  match ty with
-  | ILType.Boxed tr -> goutput_tspec env os tr
-  | ILType.TypeVar tv ->
-      // Special rule to print method type variables in Generic EE preferred form
-      // when an environment is available to help us do this.
-      let cgparams = env.ppenvClassFormals
-      let mgparams = env.ppenvMethodFormals
-      if int tv < cgparams then
-        output_string os "!"
-        output_tyvar os tv
-      elif int tv - cgparams < mgparams then
-        output_string os "!!"
-        output_int os (int tv - cgparams)
-      else
-        output_string os "!"
-        output_tyvar os tv
-        output_int os (int tv)
-
-  | ILType.Byref typ -> goutput_typ env os typ; output_string os "&"
-  | ILType.Ptr typ -> goutput_typ env os typ; output_string   os "*"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_SByte.TypeSpec.Name -> output_string os "int8"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Int16.TypeSpec.Name -> output_string os "int16"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Int32.TypeSpec.Name -> output_string os "int32"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Int64.TypeSpec.Name -> output_string os "int64"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_IntPtr.TypeSpec.Name -> output_string os "native int"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Byte.TypeSpec.Name -> output_string os "unsigned int8"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UInt16.TypeSpec.Name -> output_string os "unsigned int16"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UInt32.TypeSpec.Name -> output_string os "unsigned int32"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UInt64.TypeSpec.Name -> output_string os "unsigned int64"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UIntPtr.TypeSpec.Name -> output_string os "native unsigned int"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Double.TypeSpec.Name -> output_string os "float64"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Single.TypeSpec.Name -> output_string os "float32"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Bool.TypeSpec.Name -> output_string os "bool"
-  | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Char.TypeSpec.Name -> output_string os "char"
-  | ILType.Value tspec ->
-      output_string os "value class "
-      goutput_tref env os tspec.TypeRef
-      output_string os " "
-      goutput_gactuals env os tspec.GenericArgs
-  | ILType.Void -> output_string os "void"
-  | ILType.Array (bounds, ty) ->
-      goutput_typ env os ty
-      output_string os "["
-      output_arr_bounds os bounds
-      output_string os "]"
-  | ILType.FunctionPointer csig ->
-      output_string os "method "
-      goutput_typ env os csig.ReturnType
-      output_string os " *("
-      output_seq ", " (goutput_typ env) os csig.ArgTypes
-      output_string os ")"
-  | _ -> output_string os "NaT"
+    match ty with
+    | ILType.Boxed tr -> goutput_tspec env os tr
+    | ILType.TypeVar tv ->
+        // Special rule to print method type variables in Generic EE preferred form
+        // when an environment is available to help us do this.
+        let cgparams = env.ppenvClassFormals
+        let mgparams = env.ppenvMethodFormals
+
+        if int tv < cgparams then
+            output_string os "!"
+            output_tyvar os tv
+        elif int tv - cgparams < mgparams then
+            output_string os "!!"
+            output_int os (int tv - cgparams)
+        else
+            output_string os "!"
+            output_tyvar os tv
+            output_int os (int tv)
+
+    | ILType.Byref typ ->
+        goutput_typ env os typ
+        output_string os "&"
+    | ILType.Ptr typ ->
+        goutput_typ env os typ
+        output_string os "*"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_SByte.TypeSpec.Name -> output_string os "int8"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Int16.TypeSpec.Name -> output_string os "int16"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Int32.TypeSpec.Name -> output_string os "int32"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Int64.TypeSpec.Name -> output_string os "int64"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_IntPtr.TypeSpec.Name -> output_string os "native int"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Byte.TypeSpec.Name -> output_string os "unsigned int8"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UInt16.TypeSpec.Name -> output_string os "unsigned int16"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UInt32.TypeSpec.Name -> output_string os "unsigned int32"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UInt64.TypeSpec.Name -> output_string os "unsigned int64"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_UIntPtr.TypeSpec.Name -> output_string os "native unsigned int"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Double.TypeSpec.Name -> output_string os "float64"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Single.TypeSpec.Name -> output_string os "float32"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Bool.TypeSpec.Name -> output_string os "bool"
+    | ILType.Value tspec when tspec.Name = PrimaryAssemblyILGlobals.typ_Char.TypeSpec.Name -> output_string os "char"
+    | ILType.Value tspec ->
+        output_string os "value class "
+        goutput_tref env os tspec.TypeRef
+        output_string os " "
+        goutput_gactuals env os tspec.GenericArgs
+    | ILType.Void -> output_string os "void"
+    | ILType.Array (bounds, ty) ->
+        goutput_typ env os ty
+        output_string os "["
+        output_arr_bounds os bounds
+        output_string os "]"
+    | ILType.FunctionPointer csig ->
+        output_string os "method "
+        goutput_typ env os csig.ReturnType
+        output_string os " *("
+        output_seq ", " (goutput_typ env) os csig.ArgTypes
+        output_string os ")"
+    | _ -> output_string os "NaT"
 
 and output_tyvar os d =
-  output_u16 os d; ()
-
-and goutput_ldtoken_info env os = function
-  | ILToken.ILType x -> goutput_typ env os x
-  | ILToken.ILMethod x -> output_string os "method "; goutput_mspec env os x
-  | ILToken.ILField x -> output_string os "field "; goutput_fspec env os x
-
-and goutput_typ_with_shortened_class_syntax env os = function
-    ILType.Boxed tspec when tspec.GenericArgs = [] ->
-      goutput_tref env os tspec.TypeRef
-  | typ2 -> goutput_typ env os typ2
+    output_u16 os d
+    ()
+
+and goutput_ldtoken_info env os =
+    function
+    | ILToken.ILType x -> goutput_typ env os x
+    | ILToken.ILMethod x ->
+        output_string os "method "
+        goutput_mspec env os x
+    | ILToken.ILField x ->
+        output_string os "field "
+        goutput_fspec env os x
+
+and goutput_typ_with_shortened_class_syntax env os =
+    function
+    | ILType.Boxed tspec when tspec.GenericArgs = [] -> goutput_tref env os tspec.TypeRef
+    | typ2 -> goutput_typ env os typ2
 
 and goutput_gactuals env os inst =
-  if not (List.isEmpty inst) then
-    output_string os "<"
-    output_seq ", " (goutput_gactual env)  os inst
-    output_string os ">"
+    if not (List.isEmpty inst) then
+        output_string os "<"
+        output_seq ", " (goutput_gactual env) os inst
+        output_string os ">"
 
 and goutput_gactual env os ty = goutput_typ env os ty
 
 and goutput_tspec env os tspec =
-      output_string os "class "
-      goutput_tref env os tspec.TypeRef
-      output_string os " "
-      goutput_gactuals env os tspec.GenericArgs
-
-and output_arr_bounds os = function
-  | bounds when bounds = ILArrayShape.SingleDimensional -> ()
-  | ILArrayShape l ->
-      output_seq ", "
-          (fun os -> function
-            | None, None  -> output_string os ""
-            | None, Some sz ->
-                output_int os sz
-            | Some lower, None ->
-                output_int os lower
-                output_string os " ... "
-            | Some lower, Some d ->
-                output_int os lower
-                output_string os " ... "
-                output_int os d)
-          os
-          l
+    output_string os "class "
+    goutput_tref env os tspec.TypeRef
+    output_string os " "
+    goutput_gactuals env os tspec.GenericArgs
+
+and output_arr_bounds os =
+    function
+    | bounds when bounds = ILArrayShape.SingleDimensional -> ()
+    | ILArrayShape l ->
+        output_seq
+            ", "
+            (fun os ->
+                function
+                | None, None -> output_string os ""
+                | None, Some sz -> output_int os sz
+                | Some lower, None ->
+                    output_int os lower
+                    output_string os " ... "
+                | Some lower, Some d ->
+                    output_int os lower
+                    output_string os " ... "
+                    output_int os d)
+            os
+            l
 
 and goutput_permission _env os p =
-  let output_security_action os x =
-    output_string os
-      (match x with
-        | ILSecurityAction.Request -> "request"
-        | ILSecurityAction.Demand -> "demand"
-        | ILSecurityAction.Assert-> "assert"
-        | ILSecurityAction.Deny-> "deny"
-        | ILSecurityAction.PermitOnly-> "permitonly"
-        | ILSecurityAction.LinkCheck-> "linkcheck"
-        | ILSecurityAction.InheritCheck-> "inheritcheck"
-        | ILSecurityAction.ReqMin-> "reqmin"
-        | ILSecurityAction.ReqOpt-> "reqopt"
-        | ILSecurityAction.ReqRefuse-> "reqrefuse"
-        | ILSecurityAction.PreJitGrant-> "prejitgrant"
-        | ILSecurityAction.PreJitDeny-> "prejitdeny"
-        | ILSecurityAction.NonCasDemand-> "noncasdemand"
-        | ILSecurityAction.NonCasLinkDemand-> "noncaslinkdemand"
-        | ILSecurityAction.NonCasInheritance-> "noncasinheritance"
-        | ILSecurityAction.LinkDemandChoice -> "linkdemandchoice"
-        | ILSecurityAction.InheritanceDemandChoice -> "inheritancedemandchoice"
-        | ILSecurityAction.DemandChoice -> "demandchoice")
-
-  match p with
-  | ILSecurityDecl (sa, b) ->
-      output_string os " .permissionset "
-      output_security_action os sa
-      output_string os " = ("
-      output_bytes os b
-      output_string os ")"
+    let output_security_action os x =
+        output_string
+            os
+            (match x with
+             | ILSecurityAction.Request -> "request"
+             | ILSecurityAction.Demand -> "demand"
+             | ILSecurityAction.Assert -> "assert"
+             | ILSecurityAction.Deny -> "deny"
+             | ILSecurityAction.PermitOnly -> "permitonly"
+             | ILSecurityAction.LinkCheck -> "linkcheck"
+             | ILSecurityAction.InheritCheck -> "inheritcheck"
+             | ILSecurityAction.ReqMin -> "reqmin"
+             | ILSecurityAction.ReqOpt -> "reqopt"
+             | ILSecurityAction.ReqRefuse -> "reqrefuse"
+             | ILSecurityAction.PreJitGrant -> "prejitgrant"
+             | ILSecurityAction.PreJitDeny -> "prejitdeny"
+             | ILSecurityAction.NonCasDemand -> "noncasdemand"
+             | ILSecurityAction.NonCasLinkDemand -> "noncaslinkdemand"
+             | ILSecurityAction.NonCasInheritance -> "noncasinheritance"
+             | ILSecurityAction.LinkDemandChoice -> "linkdemandchoice"
+             | ILSecurityAction.InheritanceDemandChoice -> "inheritancedemandchoice"
+             | ILSecurityAction.DemandChoice -> "demandchoice")
+
+    match p with
+    | ILSecurityDecl (sa, b) ->
+        output_string os " .permissionset "
+        output_security_action os sa
+        output_string os " = ("
+        output_bytes os b
+        output_string os ")"
 
 and goutput_security_decls env os (ps: ILSecurityDecls) =
-    output_seq " " (goutput_permission env)  os (ps.AsList())
+    output_seq " " (goutput_permission env) os (ps.AsList())
 
 and goutput_gparam env os (gf: ILGenericParameterDef) =
-  output_string os (tyvar_generator gf.Name)
-  output_parens (output_seq ", " (goutput_typ env)) os gf.Constraints
+    output_string os (tyvar_generator gf.Name)
+    output_parens (output_seq ", " (goutput_typ env)) os gf.Constraints
 
 and goutput_gparams env os b =
-  if not (isNil b) then
-     output_string os "<"; output_seq ", " (goutput_gparam env) os b;  output_string os ">"; ()
+    if not (isNil b) then
+        output_string os "<"
+        output_seq ", " (goutput_gparam env) os b
+        output_string os ">"
+        ()
 
 and output_bcc os bcc =
-  output_string os
-    (match bcc with
-    | ILArgConvention.FastCall -> "fastcall "
-    | ILArgConvention.StdCall -> "stdcall "
-    | ILArgConvention.ThisCall -> "thiscall "
-    | ILArgConvention.CDecl -> "cdecl "
-    | ILArgConvention.Default -> " "
-    | ILArgConvention.VarArg -> "vararg ")
+    output_string
+        os
+        (match bcc with
+         | ILArgConvention.FastCall -> "fastcall "
+         | ILArgConvention.StdCall -> "stdcall "
+         | ILArgConvention.ThisCall -> "thiscall "
+         | ILArgConvention.CDecl -> "cdecl "
+         | ILArgConvention.Default -> " "
+         | ILArgConvention.VarArg -> "vararg ")
 
 and output_callconv os (Callconv (hasthis, cc)) =
-  output_string os
-    (match hasthis with
-      ILThisConvention.Instance -> "instance "
-    | ILThisConvention.InstanceExplicit -> "explicit "
-    | ILThisConvention.Static -> "")
-  output_bcc os cc
-
-and goutput_dlocref env os (dref:ILType) =
-  match dref with
-  | dref when
-       dref.IsNominal &&
-       isTypeNameForGlobalFunctions dref.TypeRef.Name &&
-       dref.TypeRef.Scope = ILScopeRef.Local ->
-   ()
-  | dref when
-       dref.IsNominal &&
-       isTypeNameForGlobalFunctions dref.TypeRef.Name ->
-   goutput_scoref env os dref.TypeRef.Scope
-   output_string os "::"
-  | ty ->goutput_typ_with_shortened_class_syntax env os ty;  output_string os "::"
-
-and goutput_callsig env os (csig:ILCallingSignature) =
-  output_callconv os csig.CallingConv
-  output_string os " "
-  goutput_typ env os csig.ReturnType
-  output_parens (output_seq ", " (goutput_typ env)) os csig.ArgTypes
-
-and goutput_mref env os (mref:ILMethodRef) =
-  output_callconv os mref.CallingConv
-  output_string os " "
-  goutput_typ_with_shortened_class_syntax env os mref.ReturnType
-  output_string os " "
-  // no quotes for ".ctor"
-  let name = mref.Name
-  if name = ".ctor" || name = ".cctor" then output_string os name else output_id os name
-  output_parens (output_seq ", " (goutput_typ env)) os mref.ArgTypes
-
-and goutput_mspec env os (mspec:ILMethodSpec) =
-  let fenv =
-    ppenv_enter_method mspec.GenericArity
-      (ppenv_enter_tdef (mkILFormalTypars mspec.DeclaringType.GenericArgs) env)
-  output_callconv os mspec.CallingConv
-  output_string os " "
-  goutput_typ fenv os mspec.FormalReturnType
-  output_string os " "
-  goutput_dlocref env os mspec.DeclaringType
-  output_string os " "
-  let name = mspec.Name
-  if name = ".ctor" || name = ".cctor" then output_string os name else output_id os name
-  goutput_gactuals env os mspec.GenericArgs
-  output_parens (output_seq ", " (goutput_typ fenv)) os mspec.FormalArgTypes
+    output_string
+        os
+        (match hasthis with
+         | ILThisConvention.Instance -> "instance "
+         | ILThisConvention.InstanceExplicit -> "explicit "
+         | ILThisConvention.Static -> "")
+
+    output_bcc os cc
+
+and goutput_dlocref env os (dref: ILType) =
+    match dref with
+    | dref when
+        dref.IsNominal
+        && isTypeNameForGlobalFunctions dref.TypeRef.Name
+        && dref.TypeRef.Scope = ILScopeRef.Local
+        ->
+        ()
+    | dref when dref.IsNominal && isTypeNameForGlobalFunctions dref.TypeRef.Name ->
+        goutput_scoref env os dref.TypeRef.Scope
+        output_string os "::"
+    | ty ->
+        goutput_typ_with_shortened_class_syntax env os ty
+        output_string os "::"
+
+and goutput_callsig env os (csig: ILCallingSignature) =
+    output_callconv os csig.CallingConv
+    output_string os " "
+    goutput_typ env os csig.ReturnType
+    output_parens (output_seq ", " (goutput_typ env)) os csig.ArgTypes
+
+and goutput_mref env os (mref: ILMethodRef) =
+    output_callconv os mref.CallingConv
+    output_string os " "
+    goutput_typ_with_shortened_class_syntax env os mref.ReturnType
+    output_string os " "
+    // no quotes for ".ctor"
+    let name = mref.Name
+
+    if name = ".ctor" || name = ".cctor" then
+        output_string os name
+    else
+        output_id os name
+
+    output_parens (output_seq ", " (goutput_typ env)) os mref.ArgTypes
+
+and goutput_mspec env os (mspec: ILMethodSpec) =
+    let fenv =
+        ppenv_enter_method mspec.GenericArity (ppenv_enter_tdef (mkILFormalTypars mspec.DeclaringType.GenericArgs) env)
+
+    output_callconv os mspec.CallingConv
+    output_string os " "
+    goutput_typ fenv os mspec.FormalReturnType
+    output_string os " "
+    goutput_dlocref env os mspec.DeclaringType
+    output_string os " "
+    let name = mspec.Name
+
+    if name = ".ctor" || name = ".cctor" then
+        output_string os name
+    else
+        output_id os name
+
+    goutput_gactuals env os mspec.GenericArgs
+    output_parens (output_seq ", " (goutput_typ fenv)) os mspec.FormalArgTypes
 
 and goutput_vararg_mspec env os (mspec, varargs) =
-   match varargs with
-   | None -> goutput_mspec env os mspec
-   | Some varargs' ->
-       let fenv =
-         ppenv_enter_method mspec.GenericArity
-           (ppenv_enter_tdef (mkILFormalTypars mspec.DeclaringType.GenericArgs) env)
-       output_callconv os mspec.CallingConv
-       output_string os " "
-       goutput_typ fenv os mspec.FormalReturnType
-       output_string os " "
-       goutput_dlocref env os mspec.DeclaringType
-       let name = mspec.Name
-       if name = ".ctor" || name = ".cctor" then output_string os name else output_id os name
-       goutput_gactuals env os mspec.GenericArgs
-       output_string os "("
-       output_seq ", " (goutput_typ fenv) os mspec.FormalArgTypes
-       output_string os ", ..., "
-       output_seq ", " (goutput_typ fenv) os varargs'
-       output_string os ")"
-
-and goutput_vararg_sig env os (csig:ILCallingSignature, varargs:ILVarArgs) =
-   match varargs with
-   | None -> goutput_callsig env os csig; ()
-   | Some varargs' ->
-       goutput_typ env os csig.ReturnType
-       output_string os " ("
-       let argTys = csig.ArgTypes
-       if argTys.Length <> 0 then
-           output_seq ", " (goutput_typ env)  os argTys
-       output_string os ", ..., "
-       output_seq ", " (goutput_typ env) os varargs'
-       output_string os ")"
-
-and goutput_fspec env os (x:ILFieldSpec) =
-  let fenv = ppenv_enter_tdef (mkILFormalTypars x.DeclaringType.GenericArgs) env
-  goutput_typ fenv os x.FormalType
-  output_string os " "
-  goutput_dlocref env os x.DeclaringType
-  output_id os x.Name
+    match varargs with
+    | None -> goutput_mspec env os mspec
+    | Some varargs' ->
+        let fenv =
+            ppenv_enter_method mspec.GenericArity (ppenv_enter_tdef (mkILFormalTypars mspec.DeclaringType.GenericArgs) env)
+
+        output_callconv os mspec.CallingConv
+        output_string os " "
+        goutput_typ fenv os mspec.FormalReturnType
+        output_string os " "
+        goutput_dlocref env os mspec.DeclaringType
+        let name = mspec.Name
+
+        if name = ".ctor" || name = ".cctor" then
+            output_string os name
+        else
+            output_id os name
+
+        goutput_gactuals env os mspec.GenericArgs
+        output_string os "("
+        output_seq ", " (goutput_typ fenv) os mspec.FormalArgTypes
+        output_string os ", ..., "
+        output_seq ", " (goutput_typ fenv) os varargs'
+        output_string os ")"
+
+and goutput_vararg_sig env os (csig: ILCallingSignature, varargs: ILVarArgs) =
+    match varargs with
+    | None ->
+        goutput_callsig env os csig
+        ()
+    | Some varargs' ->
+        goutput_typ env os csig.ReturnType
+        output_string os " ("
+        let argTys = csig.ArgTypes
+
+        if argTys.Length <> 0 then
+            output_seq ", " (goutput_typ env) os argTys
+
+        output_string os ", ..., "
+        output_seq ", " (goutput_typ env) os varargs'
+        output_string os ")"
+
+and goutput_fspec env os (x: ILFieldSpec) =
+    let fenv = ppenv_enter_tdef (mkILFormalTypars x.DeclaringType.GenericArgs) env
+    goutput_typ fenv os x.FormalType
+    output_string os " "
+    goutput_dlocref env os x.DeclaringType
+    output_id os x.Name
 
 let output_member_access os access =
-  output_string os
-    (match access with
-    | ILMemberAccess.Public -> "public"
-    | ILMemberAccess.Private  -> "private"
-    | ILMemberAccess.Family  -> "family"
-    | ILMemberAccess.CompilerControlled -> "privatescope"
-    | ILMemberAccess.FamilyAndAssembly -> "famandassem"
-    | ILMemberAccess.FamilyOrAssembly -> "famorassem"
-    | ILMemberAccess.Assembly -> "assembly")
+    output_string
+        os
+        (match access with
+         | ILMemberAccess.Public -> "public"
+         | ILMemberAccess.Private -> "private"
+         | ILMemberAccess.Family -> "family"
+         | ILMemberAccess.CompilerControlled -> "privatescope"
+         | ILMemberAccess.FamilyAndAssembly -> "famandassem"
+         | ILMemberAccess.FamilyOrAssembly -> "famorassem"
+         | ILMemberAccess.Assembly -> "assembly")
 
 let output_type_access os access =
-  match access with
-  | ILTypeDefAccess.Public -> output_string os "public"
-  | ILTypeDefAccess.Private  -> output_string os "private"
-  | ILTypeDefAccess.Nested  ilMemberAccess -> output_string os "nested "; output_member_access os ilMemberAccess
+    match access with
+    | ILTypeDefAccess.Public -> output_string os "public"
+    | ILTypeDefAccess.Private -> output_string os "private"
+    | ILTypeDefAccess.Nested ilMemberAccess ->
+        output_string os "nested "
+        output_member_access os ilMemberAccess
 
 let output_encoding os e =
-  match e with
-  | ILDefaultPInvokeEncoding.Ansi -> output_string os " ansi "
-  | ILDefaultPInvokeEncoding.Auto  -> output_string os " autochar "
-  | ILDefaultPInvokeEncoding.Unicode -> output_string os " unicode "
-let output_field_init os = function
-  | ILFieldInit.String s -> output_string os "= "; output_string os s
-  | ILFieldInit.Bool x-> output_string os "= bool"; output_parens output_string os (if x then "true" else "false")
-  | ILFieldInit.Char x-> output_string os "= char"; output_parens output_u16 os x
-  | ILFieldInit.Int8 x-> output_string os "= int8"; output_parens output_i8 os x
-  | ILFieldInit.Int16 x-> output_string os "= int16"; output_parens output_i16 os x
-  | ILFieldInit.Int32 x-> output_string os "= int32"; output_parens output_i32 os x
-  | ILFieldInit.Int64 x-> output_string os "= int64"; output_parens output_i64 os x
-  | ILFieldInit.UInt8 x-> output_string os "= uint8"; output_parens output_u8 os x
-  | ILFieldInit.UInt16 x-> output_string os "= uint16"; output_parens output_u16 os x
-  | ILFieldInit.UInt32 x-> output_string os "= uint32"; output_parens output_u32 os x
-  | ILFieldInit.UInt64 x-> output_string os "= uint64"; output_parens output_u64 os x
-  | ILFieldInit.Single x-> output_string os "= float32"; output_parens output_ieee32 os x
-  | ILFieldInit.Double x-> output_string os "= float64"; output_parens output_ieee64 os x
-  | ILFieldInit.Null-> output_string os "= nullref"
+    match e with
+    | ILDefaultPInvokeEncoding.Ansi -> output_string os " ansi "
+    | ILDefaultPInvokeEncoding.Auto -> output_string os " autochar "
+    | ILDefaultPInvokeEncoding.Unicode -> output_string os " unicode "
+
+let output_field_init os =
+    function
+    | ILFieldInit.String s ->
+        output_string os "= "
+        output_string os s
+    | ILFieldInit.Bool x ->
+        output_string os "= bool"
+        output_parens output_string os (if x then "true" else "false")
+    | ILFieldInit.Char x ->
+        output_string os "= char"
+        output_parens output_u16 os x
+    | ILFieldInit.Int8 x ->
+        output_string os "= int8"
+        output_parens output_i8 os x
+    | ILFieldInit.Int16 x ->
+        output_string os "= int16"
+        output_parens output_i16 os x
+    | ILFieldInit.Int32 x ->
+        output_string os "= int32"
+        output_parens output_i32 os x
+    | ILFieldInit.Int64 x ->
+        output_string os "= int64"
+        output_parens output_i64 os x
+    | ILFieldInit.UInt8 x ->
+        output_string os "= uint8"
+        output_parens output_u8 os x
+    | ILFieldInit.UInt16 x ->
+        output_string os "= uint16"
+        output_parens output_u16 os x
+    | ILFieldInit.UInt32 x ->
+        output_string os "= uint32"
+        output_parens output_u32 os x
+    | ILFieldInit.UInt64 x ->
+        output_string os "= uint64"
+        output_parens output_u64 os x
+    | ILFieldInit.Single x ->
+        output_string os "= float32"
+        output_parens output_ieee32 os x
+    | ILFieldInit.Double x ->
+        output_string os "= float64"
+        output_parens output_ieee64 os x
+    | ILFieldInit.Null -> output_string os "= nullref"
 
 let output_at os b =
-   Printf.fprintf os " at (* no labels for data available, data = %a *)" (output_parens output_bytes) b
+    Printf.fprintf os " at (* no labels for data available, data = %a *)" (output_parens output_bytes) b
 
-let output_option f os = function None -> () | Some x -> f os x
+let output_option f os =
+    function
+    | None -> ()
+    | Some x -> f os x
 
 let goutput_alternative_ref env os (alt: IlxUnionCase) =
-  output_id os alt.Name
-  alt.FieldDefs |> output_parens (output_array ", " (fun os fdef -> goutput_typ env os fdef.Type)) os
+    output_id os alt.Name
+
+    alt.FieldDefs
+    |> output_parens (output_array ", " (fun os fdef -> goutput_typ env os fdef.Type)) os
 
-let goutput_curef env os (IlxUnionRef(_, tref, alts, _, _)) =
-  output_string os " .classunion import "
-  goutput_tref env os tref
-  output_parens (output_array ", " (goutput_alternative_ref env)) os alts
+let goutput_curef env os (IlxUnionRef (_, tref, alts, _, _)) =
+    output_string os " .classunion import "
+    goutput_tref env os tref
+    output_parens (output_array ", " (goutput_alternative_ref env)) os alts
 
-let goutput_cuspec env os (IlxUnionSpec(IlxUnionRef(_, tref, _, _, _), i)) =
-  output_string os "class /* classunion */ "
-  goutput_tref env os  tref
-  goutput_gactuals env os i
+let goutput_cuspec env os (IlxUnionSpec (IlxUnionRef (_, tref, _, _, _), i)) =
+    output_string os "class /* classunion */ "
+    goutput_tref env os tref
+    goutput_gactuals env os i
 
 let output_basic_type os x =
-  output_string os
-    (match x with
-    | DT_I1 -> "i1"
-    | DT_U1 -> "u1"
-    | DT_I2 -> "i2"
-    | DT_U2 -> "u2"
-    | DT_I4 -> "i4"
-    | DT_U4 -> "u4"
-    | DT_I8 -> "i8"
-    | DT_U8 -> "u8"
-    | DT_R4 -> "r4"
-    | DT_R8 -> "r8"
-    | DT_R  -> "r"
-    | DT_I  -> "i"
-    | DT_U  -> "u"
-    | DT_REF  -> "ref")
+    output_string
+        os
+        (match x with
+         | DT_I1 -> "i1"
+         | DT_U1 -> "u1"
+         | DT_I2 -> "i2"
+         | DT_U2 -> "u2"
+         | DT_I4 -> "i4"
+         | DT_U4 -> "u4"
+         | DT_I8 -> "i8"
+         | DT_U8 -> "u8"
+         | DT_R4 -> "r4"
+         | DT_R8 -> "r8"
+         | DT_R -> "r"
+         | DT_I -> "i"
+         | DT_U -> "u"
+         | DT_REF -> "ref")
 
 let output_custom_attr_data os data =
-  output_string os " = "; output_parens output_bytes os data
+    output_string os " = "
+    output_parens output_bytes os data
 
 let goutput_custom_attr env os (attr: ILAttribute) =
-  output_string os " .custom "
-  goutput_mspec env os attr.Method
-  let data = getCustomAttrData attr
-  output_custom_attr_data os data
-
-let goutput_custom_attrs env os (attrs : ILAttributes) =
-  Array.iter (fun attr -> goutput_custom_attr env os attr;  output_string os "\n" ) (attrs.AsArray())
+    output_string os " .custom "
+    goutput_mspec env os attr.Method
+    let data = getCustomAttrData attr
+    output_custom_attr_data os data
+
+let goutput_custom_attrs env os (attrs: ILAttributes) =
+    Array.iter
+        (fun attr ->
+            goutput_custom_attr env os attr
+            output_string os "\n")
+        (attrs.AsArray())
 
 let goutput_fdef _tref env os (fd: ILFieldDef) =
-  output_string os " .field "
-  match fd.Offset with Some i -> output_string os "["; output_i32 os i; output_string os "] " | None -> ()
-  match fd.Marshal with Some _i -> output_string os "// marshal attribute not printed\n"; | None -> ()
-  output_member_access os fd.Access
-  output_string os " "
-  if fd.IsStatic then output_string os " static "
-  if fd.IsLiteral then output_string os " literal "
-  if fd.IsSpecialName then output_string os " specialname rtspecialname "
-  if fd.IsInitOnly then output_string os " initonly "
-  if fd.NotSerialized then output_string os " notserialized "
-  goutput_typ env os fd.FieldType
-  output_string os " "
-  output_id os fd.Name
-  output_option output_at os  fd.Data
-  output_option output_field_init os fd.LiteralValue
-  output_string os "\n"
-  goutput_custom_attrs env os fd.CustomAttrs
-
-let output_alignment os =  function
-    Aligned -> ()
-  | Unaligned1 -> output_string os "unaligned. 1 "
-  | Unaligned2 -> output_string os "unaligned. 2 "
-  | Unaligned4 -> output_string os "unaligned. 4 "
-
-let output_volatility os =  function
-    Nonvolatile -> ()
-  | Volatile -> output_string os "volatile. "
-let output_tailness os =  function
-  | Tailcall -> output_string os "tail. "
-  | _ -> ()
-let output_after_tailcall os =  function
-  | Tailcall  -> output_string os " ret "
-  | _ -> ()
-let rec goutput_apps env os =  function
-  | Apps_tyapp (actual, cs) ->
-      output_angled (goutput_gactual env) os actual
-      output_string os " "
-      output_angled (goutput_gparam env) os (mkILSimpleTypar "T")
-      output_string os " "
-      goutput_apps env os cs
-  | Apps_app(ty, cs) ->
-      output_parens (goutput_typ env) os ty
-      output_string os " "
-      goutput_apps env os cs
-  | Apps_done ty ->
-      output_string os "--> "
-      goutput_typ env os ty
+    output_string os " .field "
+
+    match fd.Offset with
+    | Some i ->
+        output_string os "["
+        output_i32 os i
+        output_string os "] "
+    | None -> ()
+
+    match fd.Marshal with
+    | Some _i -> output_string os "// marshal attribute not printed\n"
+    | None -> ()
+
+    output_member_access os fd.Access
+    output_string os " "
+
+    if fd.IsStatic then
+        output_string os " static "
+
+    if fd.IsLiteral then
+        output_string os " literal "
+
+    if fd.IsSpecialName then
+        output_string os " specialname rtspecialname "
+
+    if fd.IsInitOnly then
+        output_string os " initonly "
+
+    if fd.NotSerialized then
+        output_string os " notserialized "
+
+    goutput_typ env os fd.FieldType
+    output_string os " "
+    output_id os fd.Name
+    output_option output_at os fd.Data
+    output_option output_field_init os fd.LiteralValue
+    output_string os "\n"
+    goutput_custom_attrs env os fd.CustomAttrs
+
+let output_alignment os =
+    function
+    | Aligned -> ()
+    | Unaligned1 -> output_string os "unaligned. 1 "
+    | Unaligned2 -> output_string os "unaligned. 2 "
+    | Unaligned4 -> output_string os "unaligned. 4 "
+
+let output_volatility os =
+    function
+    | Nonvolatile -> ()
+    | Volatile -> output_string os "volatile. "
+
+let output_tailness os =
+    function
+    | Tailcall -> output_string os "tail. "
+    | _ -> ()
+
+let output_after_tailcall os =
+    function
+    | Tailcall -> output_string os " ret "
+    | _ -> ()
+
+let rec goutput_apps env os =
+    function
+    | Apps_tyapp (actual, cs) ->
+        output_angled (goutput_gactual env) os actual
+        output_string os " "
+        output_angled (goutput_gparam env) os (mkILSimpleTypar "T")
+        output_string os " "
+        goutput_apps env os cs
+    | Apps_app (ty, cs) ->
+        output_parens (goutput_typ env) os ty
+        output_string os " "
+        goutput_apps env os cs
+    | Apps_done ty ->
+        output_string os "--> "
+        goutput_typ env os ty
 
 /// Print the short form of instructions
-let output_short_u16 os (x:uint16) =
-     if int x < 256 then (output_string os ".s "; output_u16 os x)
-     else output_string os " "; output_u16 os x
+let output_short_u16 os (x: uint16) =
+    if int x < 256 then
+        (output_string os ".s "
+         output_u16 os x)
+    else
+        output_string os " "
+        output_u16 os x
 
 let output_short_i32 os i32 =
-     if  i32 < 256 && 0 >= i32 then (output_string os ".s "; output_i32 os i32)
-     else output_string os " "; output_i32 os i32
+    if i32 < 256 && 0 >= i32 then
+        (output_string os ".s "
+         output_i32 os i32)
+    else
+        output_string os " "
+        output_i32 os i32
 
-let output_code_label os lab =
-  output_string os (formatCodeLabel lab)
+let output_code_label os lab = output_string os (formatCodeLabel lab)
 
 let goutput_local env os (l: ILLocal) =
-  goutput_typ env os l.Type
-  if l.IsPinned then output_string os " pinned"
+    goutput_typ env os l.Type
+
+    if l.IsPinned then
+        output_string os " pinned"
 
 let goutput_param env os (l: ILParameter) =
-  match l.Name with
-      None -> goutput_typ env os l.Type
-    | Some n -> goutput_typ env os l.Type; output_string os " "; output_sqstring os n
+    match l.Name with
+    | None -> goutput_typ env os l.Type
+    | Some n ->
+        goutput_typ env os l.Type
+        output_string os " "
+        output_sqstring os n
 
 let goutput_params env os ps =
-  output_parens (output_seq ", " (goutput_param env)) os ps
+    output_parens (output_seq ", " (goutput_param env)) os ps
 
 let goutput_freevar env os l =
-  goutput_typ env os l.fvType; output_string os " "; output_sqstring os l.fvName
+    goutput_typ env os l.fvType
+    output_string os " "
+    output_sqstring os l.fvName
 
 let goutput_freevars env os ps =
-  output_parens (output_seq ", " (goutput_freevar env)) os ps
-
-let output_source os (s:ILDebugPoint) =
-  if s.Document.File <> "" then
-    output_string os " .line "
-    output_int os s.Line
-    if s.Column <> -1 then
-      output_string os " : "
-      output_int os s.Column
-    output_string os " /* - "
-    output_int os s.EndLine
-    if s.Column <> -1 then
-      output_string os " : "
-      output_int os s.EndColumn
-    output_string os "*/ "
-    output_sqstring os s.Document.File
+    output_parens (output_seq ", " (goutput_freevar env)) os ps
+
+let output_source os (s: ILDebugPoint) =
+    if s.Document.File <> "" then
+        output_string os " .line "
+        output_int os s.Line
+
+        if s.Column <> -1 then
+            output_string os " : "
+            output_int os s.Column
+
+        output_string os " /* - "
+        output_int os s.EndLine
 
+        if s.Column <> -1 then
+            output_string os " : "
+            output_int os s.EndColumn
+
+        output_string os "*/ "
+        output_sqstring os s.Document.File
 
 let rec goutput_instr env os inst =
-  match inst with
-  | si when isNoArgInstr si ->
-       output_lid os (wordsOfNoArgInstr si)
-  | I_brcmp (cmp, tg1)  ->
-      output_string os
-          (match cmp with
-          | BI_beq -> "beq"
-          | BI_bgt -> "bgt"
-          | BI_bgt_un -> "bgt.un"
-          | BI_bge -> "bge"
-          | BI_bge_un -> "bge.un"
-          | BI_ble -> "ble"
-          | BI_ble_un -> "ble.un"
-          | BI_blt -> "blt"
-          | BI_blt_un -> "blt.un"
-          | BI_bne_un -> "bne.un"
-          | BI_brfalse -> "brfalse"
-          | BI_brtrue -> "brtrue")
-      output_string os " "
-      output_code_label os tg1
-  | I_br  tg -> output_string os "/* br "; output_code_label os tg;  output_string os "*/"
-  | I_leave tg  -> output_string os "leave "; output_code_label os tg
-  | I_call  (tl, mspec, varargs)  ->
-      output_tailness os tl
-      output_string os "call "
-      goutput_vararg_mspec env os (mspec, varargs)
-      output_after_tailcall os tl
-  | I_calli (tl, mref, varargs) ->
-      output_tailness os tl
-      output_string os "calli "
-      goutput_vararg_sig env os (mref, varargs)
-      output_after_tailcall os tl
-  | I_ldarg u16 -> output_string os "ldarg"; output_short_u16 os u16
-  | I_ldarga  u16 -> output_string os "ldarga "; output_u16 os u16
-  | AI_ldc (dt, ILConst.I4 x) ->
-      output_string os "ldc."; output_basic_type os dt; output_short_i32 os x
-  | AI_ldc (dt, ILConst.I8 x) ->
-      output_string os "ldc."; output_basic_type os dt; output_string os " "; output_i64 os x
-  | AI_ldc (dt, ILConst.R4 x) ->
-      output_string os "ldc."; output_basic_type os dt; output_string os " "; output_ieee32 os x
-  | AI_ldc (dt, ILConst.R8 x) ->
-      output_string os "ldc."; output_basic_type os dt; output_string os " "; output_ieee64 os x
-  | I_ldftn mspec -> output_string os "ldftn "; goutput_mspec env os mspec
-  | I_ldvirtftn mspec -> output_string os "ldvirtftn "; goutput_mspec env os mspec
-  | I_ldind (al, vol, dt) ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "ldind."
-      output_basic_type os dt
-  | I_cpblk (al, vol)  ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "cpblk"
-  | I_initblk (al, vol)  ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "initblk"
-  | I_ldloc u16 -> output_string os "ldloc"; output_short_u16 os u16
-  | I_ldloca  u16 -> output_string os "ldloca "; output_u16 os u16
-  | I_starg u16 -> output_string os "starg "; output_u16 os u16
-  | I_stind (al, vol, dt) ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "stind."
-      output_basic_type os dt
-  | I_stloc u16 -> output_string os "stloc"; output_short_u16 os u16
-  | I_switch l -> output_string os "switch "; output_parens (output_seq ", " output_code_label) os l
-  | I_callvirt  (tl, mspec, varargs) ->
-      output_tailness os tl
-      output_string os "callvirt "
-      goutput_vararg_mspec env os (mspec, varargs)
-      output_after_tailcall os tl
-  | I_callconstraint  (tl, ty, mspec, varargs) ->
-      output_tailness os tl
-      output_string os "constraint. "
-      goutput_typ env os ty
-      output_string os " callvirt "
-      goutput_vararg_mspec env os (mspec, varargs)
-      output_after_tailcall os tl
-  | I_castclass ty  -> output_string os "castclass "; goutput_typ env os ty
-  | I_isinst  ty  -> output_string os "isinst "; goutput_typ env os ty
-  | I_ldfld (al, vol, fspec)  ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "ldfld "
-      goutput_fspec env os fspec
-  | I_ldflda  fspec ->
-      output_string os "ldflda "
-      goutput_fspec env os fspec
-  | I_ldsfld  (vol, fspec) ->
-      output_volatility os vol
-      output_string os "ldsfld "
-      goutput_fspec env os fspec
-  | I_ldsflda fspec ->
-      output_string os "ldsflda "
-      goutput_fspec env os fspec
-  | I_stfld (al, vol, fspec)  ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "stfld "
-      goutput_fspec env os fspec
-  | I_stsfld  (vol, fspec) ->
-      output_volatility os vol
-      output_string os "stsfld "
-      goutput_fspec env os fspec
-  | I_ldtoken  tok  -> output_string os "ldtoken ";  goutput_ldtoken_info env os tok
-  | I_refanyval ty  -> output_string os "refanyval "; goutput_typ env os ty
-  | I_refanytype  -> output_string os "refanytype"
-  | I_mkrefany  typ -> output_string os "mkrefany "; goutput_typ env os typ
-  | I_ldstr s ->
-      output_string os "ldstr "
-      output_string os s
-  | I_newobj  (mspec, varargs) ->
-      // newobj: IL has a special rule that the CC is always implicitly "instance" and need
-      // not be mentioned explicitly
-      output_string os "newobj "
-      goutput_vararg_mspec env os (mspec, varargs)
-  | I_stelem    dt      -> output_string os "stelem."; output_basic_type os dt
-  | I_ldelem    dt      -> output_string os "ldelem."; output_basic_type os dt
-
-  | I_newarr    (shape, typ) ->
-      if shape = ILArrayShape.SingleDimensional then
-        output_string os "newarr "
-        goutput_typ_with_shortened_class_syntax env os typ
-      else
-        output_string os "newobj void "
-        goutput_dlocref env os (mkILArrTy(typ, shape))
-        output_string os ".ctor"
-        let rank = shape.Rank
-        output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
-  | I_stelem_any (shape, dt)     ->
-      if shape = ILArrayShape.SingleDimensional then
-        output_string os "stelem.any "; goutput_typ env os dt
-      else
-        output_string os "call instance void "
-        goutput_dlocref env os (mkILArrTy(dt, shape))
-        output_string os "Set"
-        let rank = shape.Rank
-        let arr = Array.create (rank + 1) PrimaryAssemblyILGlobals.typ_Int32
-        arr[rank] <- dt
-        output_parens (output_array ", " (goutput_typ env)) os arr
-  | I_ldelem_any (shape, tok) ->
-      if shape = ILArrayShape.SingleDimensional then
-        output_string os "ldelem.any "; goutput_typ env os tok
-      else
-        output_string os "call instance "
-        goutput_typ env os tok
+    match inst with
+    | si when isNoArgInstr si -> output_lid os (wordsOfNoArgInstr si)
+    | I_brcmp (cmp, tg1) ->
+        output_string
+            os
+            (match cmp with
+             | BI_beq -> "beq"
+             | BI_bgt -> "bgt"
+             | BI_bgt_un -> "bgt.un"
+             | BI_bge -> "bge"
+             | BI_bge_un -> "bge.un"
+             | BI_ble -> "ble"
+             | BI_ble_un -> "ble.un"
+             | BI_blt -> "blt"
+             | BI_blt_un -> "blt.un"
+             | BI_bne_un -> "bne.un"
+             | BI_brfalse -> "brfalse"
+             | BI_brtrue -> "brtrue")
+
         output_string os " "
-        goutput_dlocref env os (mkILArrTy(tok, shape))
-        output_string os "Get"
-        let rank = shape.Rank
-        output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
-  | I_ldelema   (ro, _, shape, tok)  ->
-      if ro = ReadonlyAddress then output_string os "readonly. "
-      if shape = ILArrayShape.SingleDimensional then
-        output_string os "ldelema "; goutput_typ env os tok
-      else
-        output_string os "call instance "
-        goutput_typ env os (ILType.Byref tok)
+        output_code_label os tg1
+    | I_br tg ->
+        output_string os "/* br "
+        output_code_label os tg
+        output_string os "*/"
+    | I_leave tg ->
+        output_string os "leave "
+        output_code_label os tg
+    | I_call (tl, mspec, varargs) ->
+        output_tailness os tl
+        output_string os "call "
+        goutput_vararg_mspec env os (mspec, varargs)
+        output_after_tailcall os tl
+    | I_calli (tl, mref, varargs) ->
+        output_tailness os tl
+        output_string os "calli "
+        goutput_vararg_sig env os (mref, varargs)
+        output_after_tailcall os tl
+    | I_ldarg u16 ->
+        output_string os "ldarg"
+        output_short_u16 os u16
+    | I_ldarga u16 ->
+        output_string os "ldarga "
+        output_u16 os u16
+    | AI_ldc (dt, ILConst.I4 x) ->
+        output_string os "ldc."
+        output_basic_type os dt
+        output_short_i32 os x
+    | AI_ldc (dt, ILConst.I8 x) ->
+        output_string os "ldc."
+        output_basic_type os dt
         output_string os " "
-        goutput_dlocref env os (mkILArrTy(tok, shape))
-        output_string os "Address"
-        let rank = shape.Rank
-        output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
-
-  | I_box       tok     -> output_string os "box "; goutput_typ env os tok
-  | I_unbox     tok     -> output_string os "unbox "; goutput_typ env os tok
-  | I_unbox_any tok     -> output_string os "unbox.any "; goutput_typ env os tok
-  | I_initobj   tok     -> output_string os "initobj "; goutput_typ env os tok
-  | I_ldobj (al, vol, tok)        ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "ldobj "
-      goutput_typ env os tok
-  | I_stobj  (al, vol, tok) ->
-      output_alignment os al
-      output_volatility os vol
-      output_string os "stobj "
-      goutput_typ env os tok
-  | I_cpobj tok -> output_string os "cpobj "; goutput_typ env os tok
-  | I_sizeof  tok -> output_string os "sizeof "; goutput_typ env os tok
-  | I_seqpoint  s -> output_source os s
-  | EI_ilzero ty -> output_string os "ilzero "; goutput_typ env os ty
-  | _ ->
-      output_string os ""
-
+        output_i64 os x
+    | AI_ldc (dt, ILConst.R4 x) ->
+        output_string os "ldc."
+        output_basic_type os dt
+        output_string os " "
+        output_ieee32 os x
+    | AI_ldc (dt, ILConst.R8 x) ->
+        output_string os "ldc."
+        output_basic_type os dt
+        output_string os " "
+        output_ieee64 os x
+    | I_ldftn mspec ->
+        output_string os "ldftn "
+        goutput_mspec env os mspec
+    | I_ldvirtftn mspec ->
+        output_string os "ldvirtftn "
+        goutput_mspec env os mspec
+    | I_ldind (al, vol, dt) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "ldind."
+        output_basic_type os dt
+    | I_cpblk (al, vol) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "cpblk"
+    | I_initblk (al, vol) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "initblk"
+    | I_ldloc u16 ->
+        output_string os "ldloc"
+        output_short_u16 os u16
+    | I_ldloca u16 ->
+        output_string os "ldloca "
+        output_u16 os u16
+    | I_starg u16 ->
+        output_string os "starg "
+        output_u16 os u16
+    | I_stind (al, vol, dt) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "stind."
+        output_basic_type os dt
+    | I_stloc u16 ->
+        output_string os "stloc"
+        output_short_u16 os u16
+    | I_switch l ->
+        output_string os "switch "
+        output_parens (output_seq ", " output_code_label) os l
+    | I_callvirt (tl, mspec, varargs) ->
+        output_tailness os tl
+        output_string os "callvirt "
+        goutput_vararg_mspec env os (mspec, varargs)
+        output_after_tailcall os tl
+    | I_callconstraint (tl, ty, mspec, varargs) ->
+        output_tailness os tl
+        output_string os "constraint. "
+        goutput_typ env os ty
+        output_string os " callvirt "
+        goutput_vararg_mspec env os (mspec, varargs)
+        output_after_tailcall os tl
+    | I_castclass ty ->
+        output_string os "castclass "
+        goutput_typ env os ty
+    | I_isinst ty ->
+        output_string os "isinst "
+        goutput_typ env os ty
+    | I_ldfld (al, vol, fspec) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "ldfld "
+        goutput_fspec env os fspec
+    | I_ldflda fspec ->
+        output_string os "ldflda "
+        goutput_fspec env os fspec
+    | I_ldsfld (vol, fspec) ->
+        output_volatility os vol
+        output_string os "ldsfld "
+        goutput_fspec env os fspec
+    | I_ldsflda fspec ->
+        output_string os "ldsflda "
+        goutput_fspec env os fspec
+    | I_stfld (al, vol, fspec) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "stfld "
+        goutput_fspec env os fspec
+    | I_stsfld (vol, fspec) ->
+        output_volatility os vol
+        output_string os "stsfld "
+        goutput_fspec env os fspec
+    | I_ldtoken tok ->
+        output_string os "ldtoken "
+        goutput_ldtoken_info env os tok
+    | I_refanyval ty ->
+        output_string os "refanyval "
+        goutput_typ env os ty
+    | I_refanytype -> output_string os "refanytype"
+    | I_mkrefany typ ->
+        output_string os "mkrefany "
+        goutput_typ env os typ
+    | I_ldstr s ->
+        output_string os "ldstr "
+        output_string os s
+    | I_newobj (mspec, varargs) ->
+        // newobj: IL has a special rule that the CC is always implicitly "instance" and need
+        // not be mentioned explicitly
+        output_string os "newobj "
+        goutput_vararg_mspec env os (mspec, varargs)
+    | I_stelem dt ->
+        output_string os "stelem."
+        output_basic_type os dt
+    | I_ldelem dt ->
+        output_string os "ldelem."
+        output_basic_type os dt
+
+    | I_newarr (shape, typ) ->
+        if shape = ILArrayShape.SingleDimensional then
+            output_string os "newarr "
+            goutput_typ_with_shortened_class_syntax env os typ
+        else
+            output_string os "newobj void "
+            goutput_dlocref env os (mkILArrTy (typ, shape))
+            output_string os ".ctor"
+            let rank = shape.Rank
+            output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
+    | I_stelem_any (shape, dt) ->
+        if shape = ILArrayShape.SingleDimensional then
+            output_string os "stelem.any "
+            goutput_typ env os dt
+        else
+            output_string os "call instance void "
+            goutput_dlocref env os (mkILArrTy (dt, shape))
+            output_string os "Set"
+            let rank = shape.Rank
+            let arr = Array.create (rank + 1) PrimaryAssemblyILGlobals.typ_Int32
+            arr[rank] <- dt
+            output_parens (output_array ", " (goutput_typ env)) os arr
+    | I_ldelem_any (shape, tok) ->
+        if shape = ILArrayShape.SingleDimensional then
+            output_string os "ldelem.any "
+            goutput_typ env os tok
+        else
+            output_string os "call instance "
+            goutput_typ env os tok
+            output_string os " "
+            goutput_dlocref env os (mkILArrTy (tok, shape))
+            output_string os "Get"
+            let rank = shape.Rank
+            output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
+    | I_ldelema (ro, _, shape, tok) ->
+        if ro = ReadonlyAddress then
+            output_string os "readonly. "
+
+        if shape = ILArrayShape.SingleDimensional then
+            output_string os "ldelema "
+            goutput_typ env os tok
+        else
+            output_string os "call instance "
+            goutput_typ env os (ILType.Byref tok)
+            output_string os " "
+            goutput_dlocref env os (mkILArrTy (tok, shape))
+            output_string os "Address"
+            let rank = shape.Rank
+            output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
+
+    | I_box tok ->
+        output_string os "box "
+        goutput_typ env os tok
+    | I_unbox tok ->
+        output_string os "unbox "
+        goutput_typ env os tok
+    | I_unbox_any tok ->
+        output_string os "unbox.any "
+        goutput_typ env os tok
+    | I_initobj tok ->
+        output_string os "initobj "
+        goutput_typ env os tok
+    | I_ldobj (al, vol, tok) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "ldobj "
+        goutput_typ env os tok
+    | I_stobj (al, vol, tok) ->
+        output_alignment os al
+        output_volatility os vol
+        output_string os "stobj "
+        goutput_typ env os tok
+    | I_cpobj tok ->
+        output_string os "cpobj "
+        goutput_typ env os tok
+    | I_sizeof tok ->
+        output_string os "sizeof "
+        goutput_typ env os tok
+    | I_seqpoint s -> output_source os s
+    | EI_ilzero ty ->
+        output_string os "ilzero "
+        goutput_typ env os ty
+    | _ -> output_string os ""
 
 let goutput_ilmbody env os (il: ILMethodBody) =
-  if il.IsZeroInit then output_string os " .zeroinit\n"
-  output_string os " .maxstack "
-  output_i32 os il.MaxStack
-  output_string os "\n"
-  if il.Locals.Length  <> 0 then
-    output_string os " .locals("
-    output_seq ", \n " (goutput_local env)  os il.Locals
-    output_string os ")\n"
+    if il.IsZeroInit then
+        output_string os " .zeroinit\n"
+
+    output_string os " .maxstack "
+    output_i32 os il.MaxStack
+    output_string os "\n"
+
+    if il.Locals.Length <> 0 then
+        output_string os " .locals("
+        output_seq ", \n " (goutput_local env) os il.Locals
+        output_string os ")\n"
 
 let goutput_mbody is_entrypoint env os (md: ILMethodDef) =
-  if md.ImplAttributes &&& MethodImplAttributes.Native <> enum 0 then output_string os "native "
-  elif md.ImplAttributes &&&  MethodImplAttributes.IL <> enum 0 then output_string os "cil "
-  else output_string os "runtime "
-
-  output_string os (if md.IsInternalCall then "internalcall " else " ")
-  output_string os (if md.IsManaged then "managed " else " ")
-  output_string os (if md.IsForwardRef then "forwardref " else " ")
-  output_string os " \n{ \n"
-  goutput_security_decls env os md.SecurityDecls
-  goutput_custom_attrs env os md.CustomAttrs
-  match md.Body with
+    if md.ImplAttributes &&& MethodImplAttributes.Native <> enum 0 then
+        output_string os "native "
+    elif md.ImplAttributes &&& MethodImplAttributes.IL <> enum 0 then
+        output_string os "cil "
+    else
+        output_string os "runtime "
+
+    output_string
+        os
+        (if md.IsInternalCall then
+             "internalcall "
+         else
+             " ")
+
+    output_string os (if md.IsManaged then "managed " else " ")
+
+    output_string
+        os
+        (if md.IsForwardRef then
+             "forwardref "
+         else
+             " ")
+
+    output_string os " \n{ \n"
+    goutput_security_decls env os md.SecurityDecls
+    goutput_custom_attrs env os md.CustomAttrs
+
+    match md.Body with
     | MethodBody.IL il -> goutput_ilmbody env os il.Value
     | _ -> ()
-  if is_entrypoint then output_string os " .entrypoint"
-  output_string os "\n"
-  output_string os "}\n"
-
-let goutput_mdef env os (md:ILMethodDef) =
-  let attrs =
-      if md.IsVirtual then
-            "virtual " +
-            (if md.IsFinal then "final " else "") +
-            (if md.IsNewSlot then "newslot " else "") +
-            (if md.IsCheckAccessOnOverride then " strict " else "") +
-            (if md.IsAbstract then " abstract " else "") +
-              "  "
-      elif md.IsNonVirtualInstance then     ""
-      elif md.IsConstructor then "rtspecialname"
-      elif md.IsStatic then
-            "static " +
-            (match md.Body with
-              MethodBody.PInvoke attrLazy ->
-                let attr = attrLazy.Value
-                "pinvokeimpl(\"" + attr.Where.Name + "\" as \"" + attr.Name + "\"" +
-                (match attr.CallingConv with
-                | PInvokeCallingConvention.None -> ""
-                | PInvokeCallingConvention.Cdecl -> " cdecl"
-                | PInvokeCallingConvention.Stdcall -> " stdcall"
-                | PInvokeCallingConvention.Thiscall -> " thiscall"
-                | PInvokeCallingConvention.Fastcall -> " fastcall"
-                | PInvokeCallingConvention.WinApi -> " winapi" ) +
-
-                (match attr.CharEncoding with
-                | PInvokeCharEncoding.None -> ""
-                | PInvokeCharEncoding.Ansi -> " ansi"
-                | PInvokeCharEncoding.Unicode -> " unicode"
-                | PInvokeCharEncoding.Auto -> " autochar") +
-
-                (if attr.NoMangle then " nomangle" else "") +
-                (if attr.LastError then " lasterr" else "") +
-                ")"
-              | _ ->
-                  "")
-      elif md.IsClassInitializer then "specialname rtspecialname static"
-      else ""
-  let is_entrypoint = md.IsEntryPoint
-  let menv = ppenv_enter_method (List.length md.GenericParams) env
-  output_string os " .method "
-  if md.IsHideBySig then output_string os "hidebysig "
-  if md.IsReqSecObj then output_string os "reqsecobj "
-  if md.IsSpecialName then output_string os "specialname "
-  if md.IsUnmanagedExport then output_string os "unmanagedexp "
-  output_member_access os md.Access
-  output_string os " "
-  output_string os attrs
-  output_string os " "
-  output_callconv os md.CallingConv
-  output_string os " "
-  (goutput_typ menv) os md.Return.Type
-  output_string os " "
-  output_id os md.Name
-  output_string os " "
-  (goutput_gparams env) os md.GenericParams
-  output_string os " "
-  (goutput_params menv) os md.Parameters
-  output_string os " "
-  if md.IsSynchronized then output_string os "synchronized "
-  if md.IsMustRun then output_string os "/* mustrun */ "
-  if md.IsPreserveSig then output_string os "preservesig "
-  if md.IsNoInline then output_string os "noinlining "
-  if md.IsAggressiveInline then output_string os "aggressiveinlining "
-  (goutput_mbody is_entrypoint menv) os md
-  output_string os "\n"
+
+    if is_entrypoint then
+        output_string os " .entrypoint"
+
+    output_string os "\n"
+    output_string os "}\n"
+
+let goutput_mdef env os (md: ILMethodDef) =
+    let attrs =
+        if md.IsVirtual then
+            "virtual "
+            + (if md.IsFinal then "final " else "")
+            + (if md.IsNewSlot then "newslot " else "")
+            + (if md.IsCheckAccessOnOverride then
+                   " strict "
+               else
+                   "")
+            + (if md.IsAbstract then
+                   " abstract "
+               else
+                   "")
+            + "  "
+        elif md.IsNonVirtualInstance then
+            ""
+        elif md.IsConstructor then
+            "rtspecialname"
+        elif md.IsStatic then
+            "static "
+            + (match md.Body with
+               | MethodBody.PInvoke attrLazy ->
+                   let attr = attrLazy.Value
+
+                   "pinvokeimpl(\""
+                   + attr.Where.Name
+                   + "\" as \""
+                   + attr.Name
+                   + "\""
+                   + (match attr.CallingConv with
+                      | PInvokeCallingConvention.None -> ""
+                      | PInvokeCallingConvention.Cdecl -> " cdecl"
+                      | PInvokeCallingConvention.Stdcall -> " stdcall"
+                      | PInvokeCallingConvention.Thiscall -> " thiscall"
+                      | PInvokeCallingConvention.Fastcall -> " fastcall"
+                      | PInvokeCallingConvention.WinApi -> " winapi")
+                   +
+
+                   (match attr.CharEncoding with
+                    | PInvokeCharEncoding.None -> ""
+                    | PInvokeCharEncoding.Ansi -> " ansi"
+                    | PInvokeCharEncoding.Unicode -> " unicode"
+                    | PInvokeCharEncoding.Auto -> " autochar")
+                   +
+
+                   (if attr.NoMangle then
+                        " nomangle"
+                    else
+                        "")
+                   + (if attr.LastError then
+                          " lasterr"
+                      else
+                          "")
+                   + ")"
+               | _ -> "")
+        elif md.IsClassInitializer then
+            "specialname rtspecialname static"
+        else
+            ""
+
+    let is_entrypoint = md.IsEntryPoint
+    let menv = ppenv_enter_method (List.length md.GenericParams) env
+    output_string os " .method "
+
+    if md.IsHideBySig then
+        output_string os "hidebysig "
+
+    if md.IsReqSecObj then
+        output_string os "reqsecobj "
+
+    if md.IsSpecialName then
+        output_string os "specialname "
+
+    if md.IsUnmanagedExport then
+        output_string os "unmanagedexp "
+
+    output_member_access os md.Access
+    output_string os " "
+    output_string os attrs
+    output_string os " "
+    output_callconv os md.CallingConv
+    output_string os " "
+    (goutput_typ menv) os md.Return.Type
+    output_string os " "
+    output_id os md.Name
+    output_string os " "
+    (goutput_gparams env) os md.GenericParams
+    output_string os " "
+    (goutput_params menv) os md.Parameters
+    output_string os " "
+
+    if md.IsSynchronized then
+        output_string os "synchronized "
+
+    if md.IsMustRun then
+        output_string os "/* mustrun */ "
+
+    if md.IsPreserveSig then
+        output_string os "preservesig "
+
+    if md.IsNoInline then
+        output_string os "noinlining "
+
+    if md.IsAggressiveInline then
+        output_string os "aggressiveinlining "
+
+    (goutput_mbody is_entrypoint menv) os md
+    output_string os "\n"
 
 let goutput_pdef env os (pd: ILPropertyDef) =
-    output_string os  "property\n\tgetter: "
-    (match pd.GetMethod with None -> () | Some mref -> goutput_mref env os mref)
-    output_string os  "\n\tsetter: "
-    (match pd.SetMethod with None -> () | Some mref -> goutput_mref env os mref)
+    output_string os "property\n\tgetter: "
 
-let goutput_superclass env os = function
-    None -> ()
-  | Some typ -> output_string os "extends "; (goutput_typ_with_shortened_class_syntax env) os typ
+    (match pd.GetMethod with
+     | None -> ()
+     | Some mref -> goutput_mref env os mref)
+
+    output_string os "\n\tsetter: "
+
+    (match pd.SetMethod with
+     | None -> ()
+     | Some mref -> goutput_mref env os mref)
+
+let goutput_superclass env os =
+    function
+    | None -> ()
+    | Some typ ->
+        output_string os "extends "
+        (goutput_typ_with_shortened_class_syntax env) os typ
 
 let goutput_superinterfaces env os imp =
-  if not (List.isEmpty imp) then
-      output_string os "implements "
-      output_seq ", " (goutput_typ_with_shortened_class_syntax env) os imp
+    if not (List.isEmpty imp) then
+        output_string os "implements "
+        output_seq ", " (goutput_typ_with_shortened_class_syntax env) os imp
 
-let goutput_implements env os (imp:ILTypes) =
-  if not (List.isEmpty imp) then
-      output_string os "implements "
-      output_seq ", " (goutput_typ_with_shortened_class_syntax env) os imp
+let goutput_implements env os (imp: ILTypes) =
+    if not (List.isEmpty imp) then
+        output_string os "implements "
+        output_seq ", " (goutput_typ_with_shortened_class_syntax env) os imp
 
-let the = function Some x -> x  | None -> failwith "the"
+let the =
+    function
+    | Some x -> x
+    | None -> failwith "the"
 
 let output_type_layout_info os info =
-  if info.Size <> None then (output_string os " .size "; output_i32 os (the info.Size))
-  if info.Pack <> None then (output_string os " .pack "; output_u16 os (the info.Pack))
+    if info.Size <> None then
+        (output_string os " .size "
+         output_i32 os (the info.Size))
+
+    if info.Pack <> None then
+        (output_string os " .pack "
+         output_u16 os (the info.Pack))
 
-let splitTypeLayout = function
-  | ILTypeDefLayout.Auto -> "auto", (fun _os () -> ())
-  | ILTypeDefLayout.Sequential info -> "sequential", (fun os () -> output_type_layout_info os info)
-  | ILTypeDefLayout.Explicit info -> "explicit", (fun os () -> output_type_layout_info os info)
+let splitTypeLayout =
+    function
+    | ILTypeDefLayout.Auto -> "auto", (fun _os () -> ())
+    | ILTypeDefLayout.Sequential info -> "sequential", (fun os () -> output_type_layout_info os info)
+    | ILTypeDefLayout.Explicit info -> "explicit", (fun os () -> output_type_layout_info os info)
 
 let goutput_fdefs tref env os (fdefs: ILFieldDefs) =
-  for f in fdefs.AsList() do
-      goutput_fdef tref env os f
-      output_string os "\n"
+    for f in fdefs.AsList() do
+        goutput_fdef tref env os f
+        output_string os "\n"
 
 let goutput_mdefs env os (mdefs: ILMethodDefs) =
-  for f in mdefs.AsArray() do
-      goutput_mdef env os f
-      output_string os "\n"
+    for f in mdefs.AsArray() do
+        goutput_mdef env os f
+        output_string os "\n"
 
 let goutput_pdefs env os (pdefs: ILPropertyDefs) =
-  for f in pdefs.AsList() do
-      goutput_pdef env os f
-      output_string os "\n"
+    for f in pdefs.AsList() do
+        goutput_pdef env os f
+        output_string os "\n"
 
 let rec goutput_tdef enc env contents os (cd: ILTypeDef) =
-  let env = ppenv_enter_tdef cd.GenericParams env
-  let layout_attr, pp_layout_decls = splitTypeLayout cd.Layout
-  if isTypeNameForGlobalFunctions cd.Name then
-      if contents then
-          let tref = (mkILNestedTyRef (ILScopeRef.Local, enc, cd.Name))
-          goutput_mdefs env os cd.Methods
-          goutput_fdefs tref env os cd.Fields
-          goutput_pdefs env os cd.Properties
-  else
-      output_string os "\n"
-      if cd.IsInterface then output_string os ".class  interface "
-      else output_string os ".class "
-      output_init_semantics os cd.Attributes
-      output_string os " "
-      output_type_access os cd.Access
-      output_string os " "
-      output_encoding os cd.Encoding
-      output_string os " "
-      output_string os layout_attr
-      output_string os " "
-      if cd.IsSealed then  output_string os "sealed "
-      if cd.IsAbstract then  output_string os "abstract "
-      if cd.IsSerializable then  output_string os "serializable "
-      if cd.IsComInterop then  output_string os "import "
-      output_sqstring os cd.Name
-      goutput_gparams env os cd.GenericParams
-      output_string os "\n\t"
-      goutput_superclass env os cd.Extends
-      output_string os "\n\t"
-      goutput_implements env os cd.Implements
-      output_string os "\n{\n "
-      if contents then
-        let tref = (mkILNestedTyRef (ILScopeRef.Local, enc, cd.Name))
-        goutput_custom_attrs env os cd.CustomAttrs
-        goutput_security_decls env os cd.SecurityDecls
-        pp_layout_decls os ()
-        goutput_fdefs tref env os cd.Fields
-        goutput_mdefs env os cd.Methods
-      goutput_tdefs contents  (enc@[cd.Name]) env os cd.NestedTypes
-      output_string os "\n}"
+    let env = ppenv_enter_tdef cd.GenericParams env
+    let layout_attr, pp_layout_decls = splitTypeLayout cd.Layout
+
+    if isTypeNameForGlobalFunctions cd.Name then
+        if contents then
+            let tref = (mkILNestedTyRef (ILScopeRef.Local, enc, cd.Name))
+            goutput_mdefs env os cd.Methods
+            goutput_fdefs tref env os cd.Fields
+            goutput_pdefs env os cd.Properties
+    else
+        output_string os "\n"
+
+        if cd.IsInterface then
+            output_string os ".class  interface "
+        else
+            output_string os ".class "
+
+        output_init_semantics os cd.Attributes
+        output_string os " "
+        output_type_access os cd.Access
+        output_string os " "
+        output_encoding os cd.Encoding
+        output_string os " "
+        output_string os layout_attr
+        output_string os " "
+
+        if cd.IsSealed then
+            output_string os "sealed "
+
+        if cd.IsAbstract then
+            output_string os "abstract "
+
+        if cd.IsSerializable then
+            output_string os "serializable "
+
+        if cd.IsComInterop then
+            output_string os "import "
+
+        output_sqstring os cd.Name
+        goutput_gparams env os cd.GenericParams
+        output_string os "\n\t"
+        goutput_superclass env os cd.Extends
+        output_string os "\n\t"
+        goutput_implements env os cd.Implements
+        output_string os "\n{\n "
+
+        if contents then
+            let tref = (mkILNestedTyRef (ILScopeRef.Local, enc, cd.Name))
+            goutput_custom_attrs env os cd.CustomAttrs
+            goutput_security_decls env os cd.SecurityDecls
+            pp_layout_decls os ()
+            goutput_fdefs tref env os cd.Fields
+            goutput_mdefs env os cd.Methods
+
+        goutput_tdefs contents (enc @ [ cd.Name ]) env os cd.NestedTypes
+        output_string os "\n}"
 
 and output_init_semantics os f =
-  if f &&& TypeAttributes.BeforeFieldInit <> enum 0 then output_string os "beforefieldinit"
+    if f &&& TypeAttributes.BeforeFieldInit <> enum 0 then
+        output_string os "beforefieldinit"
 
 and goutput_lambdas env os lambdas =
-  match lambdas with
-   | Lambdas_forall (gf, l) ->
-       output_angled (goutput_gparam env) os gf
-       output_string os " "
-       (goutput_lambdas env) os l
-   | Lambdas_lambda (ps, l) ->
-       output_parens (goutput_param env) os ps
-       output_string os " "
-       (goutput_lambdas env) os l
-   | Lambdas_return typ -> output_string os "--> "; (goutput_typ env) os typ
+    match lambdas with
+    | Lambdas_forall (gf, l) ->
+        output_angled (goutput_gparam env) os gf
+        output_string os " "
+        (goutput_lambdas env) os l
+    | Lambdas_lambda (ps, l) ->
+        output_parens (goutput_param env) os ps
+        output_string os " "
+        (goutput_lambdas env) os l
+    | Lambdas_return typ ->
+        output_string os "--> "
+        (goutput_typ env) os typ
 
 and goutput_tdefs contents enc env os (tds: ILTypeDefs) =
-  for td in tds.AsList() do
-      goutput_tdef enc env contents os td
+    for td in tds.AsList() do
+        goutput_tdef enc env contents os td
 
 let output_ver os (version: ILVersionInfo) =
     output_string os " .ver "
@@ -1001,71 +1355,98 @@ let output_ver os (version: ILVersionInfo) =
     output_string os " : "
     output_u16 os version.Revision
 
-let output_locale os s = output_string os " .Locale "; output_qstring os s
+let output_locale os s =
+    output_string os " .Locale "
+    output_qstring os s
 
 let output_hash os x =
-    output_string os " .hash = "; output_parens output_bytes os x
+    output_string os " .hash = "
+    output_parens output_bytes os x
+
 let output_publickeytoken os x =
-  output_string os " .publickeytoken = "; output_parens output_bytes os x
+    output_string os " .publickeytoken = "
+    output_parens output_bytes os x
+
 let output_publickey os x =
-  output_string os " .publickey = "; output_parens output_bytes os x
-
-let output_publickeyinfo os = function
-  | PublicKey k -> output_publickey os k
-  | PublicKeyToken k -> output_publickeytoken os k
-
-let output_assemblyRef os (aref:ILAssemblyRef) =
-  output_string os " .assembly extern "
-  output_sqstring os aref.Name
-  if aref.Retargetable then output_string os " retargetable "
-  output_string os " { "
-  output_option output_hash os aref.Hash
-  output_option output_publickeyinfo os aref.PublicKey
-  output_option output_ver os aref.Version
-  output_option output_locale os aref.Locale
-  output_string os " } "
-
-let output_modref os (modref:ILModuleRef) =
-  output_string os (if modref.HasMetadata then " .module extern " else " .file nometadata " )
-  output_sqstring os modref.Name
-  output_option output_hash os modref.Hash
+    output_string os " .publickey = "
+    output_parens output_bytes os x
+
+let output_publickeyinfo os =
+    function
+    | PublicKey k -> output_publickey os k
+    | PublicKeyToken k -> output_publickeytoken os k
+
+let output_assemblyRef os (aref: ILAssemblyRef) =
+    output_string os " .assembly extern "
+    output_sqstring os aref.Name
+
+    if aref.Retargetable then
+        output_string os " retargetable "
+
+    output_string os " { "
+    output_option output_hash os aref.Hash
+    output_option output_publickeyinfo os aref.PublicKey
+    output_option output_ver os aref.Version
+    output_option output_locale os aref.Locale
+    output_string os " } "
+
+let output_modref os (modref: ILModuleRef) =
+    output_string
+        os
+        (if modref.HasMetadata then
+             " .module extern "
+         else
+             " .file nometadata ")
+
+    output_sqstring os modref.Name
+    output_option output_hash os modref.Hash
 
 let goutput_resource env os r =
-  output_string os " .mresource "
-  output_string os (match r.Access with ILResourceAccess.Public -> " public " | ILResourceAccess.Private -> " private ")
-  output_sqstring os r.Name
-  output_string os " { "
-  goutput_custom_attrs env os r.CustomAttrs
-  match r.Location with
-  | ILResourceLocation.Local _ ->
-      output_string os " /* loc nyi */ "
-  | ILResourceLocation.File (mref, off) ->
-      output_string os " .file "
-      output_sqstring os mref.Name
-      output_string os "  at "
-      output_i32 os off
-  | ILResourceLocation.Assembly aref ->
-      output_string os " .assembly extern "
-      output_sqstring os aref.Name
-  output_string os " }\n "
+    output_string os " .mresource "
+
+    output_string
+        os
+        (match r.Access with
+         | ILResourceAccess.Public -> " public "
+         | ILResourceAccess.Private -> " private ")
+
+    output_sqstring os r.Name
+    output_string os " { "
+    goutput_custom_attrs env os r.CustomAttrs
+
+    match r.Location with
+    | ILResourceLocation.Local _ -> output_string os " /* loc nyi */ "
+    | ILResourceLocation.File (mref, off) ->
+        output_string os " .file "
+        output_sqstring os mref.Name
+        output_string os "  at "
+        output_i32 os off
+    | ILResourceLocation.Assembly aref ->
+        output_string os " .assembly extern "
+        output_sqstring os aref.Name
+
+    output_string os " }\n "
 
 let goutput_manifest env os m =
-  output_string os " .assembly "
-  match m.AssemblyLongevity with
-  | ILAssemblyLongevity.Unspecified -> ()
-  | ILAssemblyLongevity.Library -> output_string os "library "
-  | ILAssemblyLongevity.PlatformAppDomain -> output_string os "platformappdomain "
-  | ILAssemblyLongevity.PlatformProcess -> output_string os "platformprocess "
-  | ILAssemblyLongevity.PlatformSystem  -> output_string os "platformmachine "
-  output_sqstring os m.Name
-  output_string os " { \n"
-  output_string os ".hash algorithm "; output_i32 os m.AuxModuleHashAlgorithm; output_string os "\n"
-  goutput_custom_attrs env os m.CustomAttrs
-  output_option output_publickey os m.PublicKey
-  output_option output_ver os m.Version
-  output_option output_locale os m.Locale
-  output_string os " } \n"
-
+    output_string os " .assembly "
+
+    match m.AssemblyLongevity with
+    | ILAssemblyLongevity.Unspecified -> ()
+    | ILAssemblyLongevity.Library -> output_string os "library "
+    | ILAssemblyLongevity.PlatformAppDomain -> output_string os "platformappdomain "
+    | ILAssemblyLongevity.PlatformProcess -> output_string os "platformprocess "
+    | ILAssemblyLongevity.PlatformSystem -> output_string os "platformmachine "
+
+    output_sqstring os m.Name
+    output_string os " { \n"
+    output_string os ".hash algorithm "
+    output_i32 os m.AuxModuleHashAlgorithm
+    output_string os "\n"
+    goutput_custom_attrs env os m.CustomAttrs
+    output_option output_publickey os m.PublicKey
+    output_option output_ver os m.Version
+    output_option output_locale os m.Locale
+    output_string os " } \n"
 
 let output_module_fragment_aux os (ilg: ILGlobals) modul =
     let env = mk_ppenv ilg
@@ -1074,12 +1455,26 @@ let output_module_fragment_aux os (ilg: ILGlobals) modul =
     goutput_tdefs true [] env os modul.TypeDefs
 
 let goutput_module_manifest env os modul =
-    output_string os " .module "; output_sqstring os modul.Name
+    output_string os " .module "
+    output_sqstring os modul.Name
     goutput_custom_attrs env os modul.CustomAttrs
-    output_string os " .imagebase "; output_i32 os modul.ImageBase
-    output_string os " .file alignment "; output_i32 os modul.PhysicalAlignment
-    output_string os " .subsystem "; output_i32 os modul.SubSystemFlags
-    output_string os " .corflags "; output_i32 os ((if modul.IsILOnly then 0x0001 else 0) ||| (if modul.Is32Bit then 0x0002 else 0) ||| (if modul.Is32BitPreferred then 0x00020003 else 0))
+    output_string os " .imagebase "
+    output_i32 os modul.ImageBase
+    output_string os " .file alignment "
+    output_i32 os modul.PhysicalAlignment
+    output_string os " .subsystem "
+    output_i32 os modul.SubSystemFlags
+    output_string os " .corflags "
+
+    output_i32
+        os
+        ((if modul.IsILOnly then 0x0001 else 0)
+         ||| (if modul.Is32Bit then 0x0002 else 0)
+         ||| (if modul.Is32BitPreferred then
+                  0x00020003
+              else
+                  0))
+
     List.iter (fun r -> goutput_resource env os r) (modul.Resources.AsList())
     output_string os "\n"
     output_option (goutput_manifest env) os modul.Manifest
@@ -1090,9 +1485,4 @@ let output_module os (ilg: ILGlobals) modul =
     goutput_module_manifest env os modul
     output_module_fragment_aux os ilg modul
 
-
 #endif
-
-
-
-
diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs
index 9446bd09ad8..6ec7cec7c1c 100644
--- a/src/Compiler/AbstractIL/ilread.fs
+++ b/src/Compiler/AbstractIL/ilread.fs
@@ -33,20 +33,45 @@ open FSharp.NativeInterop
 
 let checking = false
 let logging = false
-let _ = if checking then dprintn "warning: ILBinaryReader.checking is on"
-let noStableFileHeuristic = try (Environment.GetEnvironmentVariable("FSharp_NoStableFileHeuristic") <> null) with _ -> false
-let alwaysMemoryMapFSC = try (Environment.GetEnvironmentVariable("FSharp_AlwaysMemoryMapCommandLineCompiler") <> null) with _ -> false
+
+let _ =
+    if checking then
+        dprintn "warning: ILBinaryReader.checking is on"
+
+let noStableFileHeuristic =
+    try
+        (Environment.GetEnvironmentVariable("FSharp_NoStableFileHeuristic") <> null)
+    with
+    | _ -> false
+
+let alwaysMemoryMapFSC =
+    try
+        (Environment.GetEnvironmentVariable("FSharp_AlwaysMemoryMapCommandLineCompiler")
+         <> null)
+    with
+    | _ -> false
+
 let stronglyHeldReaderCacheSizeDefault = 30
-let stronglyHeldReaderCacheSize = try (match Environment.GetEnvironmentVariable("FSharp_StronglyHeldBinaryReaderCacheSize") with null -> stronglyHeldReaderCacheSizeDefault | s -> int32 s) with _ -> stronglyHeldReaderCacheSizeDefault
 
-let singleOfBits (x: int32) = BitConverter.ToSingle(BitConverter.GetBytes x, 0)
+let stronglyHeldReaderCacheSize =
+    try
+        (match Environment.GetEnvironmentVariable("FSharp_StronglyHeldBinaryReaderCacheSize") with
+         | null -> stronglyHeldReaderCacheSizeDefault
+         | s -> int32 s)
+    with
+    | _ -> stronglyHeldReaderCacheSizeDefault
+
+let singleOfBits (x: int32) =
+    BitConverter.ToSingle(BitConverter.GetBytes x, 0)
+
 let doubleOfBits (x: int64) = BitConverter.Int64BitsToDouble x
 
 //---------------------------------------------------------------------
 // Utilities.
 //---------------------------------------------------------------------
 
-let align alignment n = ((n + alignment - 0x1) / alignment) * alignment
+let align alignment n =
+    ((n + alignment - 0x1) / alignment) * alignment
 
 let uncodedToken (tab: TableName) idx = ((tab.Index <<< 24) ||| idx)
 
@@ -55,56 +80,70 @@ let i32ToUncodedToken tok =
     let tab = tok >>>& 24
     (TableName.FromIndex tab, idx)
 
-
 []
 type TaggedIndex<'T> =
     val tag: 'T
     val index: int32
-    new(tag, index) = { tag=tag; index=index }
+    new(tag, index) = { tag = tag; index = index }
 
 let uncodedTokenToTypeDefOrRefOrSpec (tab, tok) =
     let tag =
-        if tab = TableNames.TypeDef then tdor_TypeDef
-        elif tab = TableNames.TypeRef then tdor_TypeRef
-        elif tab = TableNames.TypeSpec then tdor_TypeSpec
-        else failwith "bad table in uncodedTokenToTypeDefOrRefOrSpec"
+        if tab = TableNames.TypeDef then
+            tdor_TypeDef
+        elif tab = TableNames.TypeRef then
+            tdor_TypeRef
+        elif tab = TableNames.TypeSpec then
+            tdor_TypeSpec
+        else
+            failwith "bad table in uncodedTokenToTypeDefOrRefOrSpec"
+
     TaggedIndex(tag, tok)
 
 let uncodedTokenToMethodDefOrRef (tab, tok) =
     let tag =
-        if tab = TableNames.Method then mdor_MethodDef
-        elif tab = TableNames.MemberRef then mdor_MemberRef
-        else failwith "bad table in uncodedTokenToMethodDefOrRef"
+        if tab = TableNames.Method then
+            mdor_MethodDef
+        elif tab = TableNames.MemberRef then
+            mdor_MemberRef
+        else
+            failwith "bad table in uncodedTokenToMethodDefOrRef"
+
     TaggedIndex(tag, tok)
 
 let (|TaggedIndex|) (x: TaggedIndex<'T>) = x.tag, x.index
+
 let inline tokToTaggedIdx f nbits tok =
     let tagmask =
         if nbits = 1 then 1
         elif nbits = 2 then 3
         elif nbits = 3 then 7
         elif nbits = 4 then 15
-           elif nbits = 5 then 31
-           else failwith "too many nbits"
+        elif nbits = 5 then 31
+        else failwith "too many nbits"
+
     let tag = tok &&& tagmask
     let idx = tok >>>& nbits
     TaggedIndex(f tag, idx)
 
 type Statistics =
-    { mutable rawMemoryFileCount: int
-      mutable memoryMapFileOpenedCount: int
-      mutable memoryMapFileClosedCount: int
-      mutable weakByteFileCount: int
-      mutable byteFileCount: int }
+    {
+        mutable rawMemoryFileCount: int
+        mutable memoryMapFileOpenedCount: int
+        mutable memoryMapFileClosedCount: int
+        mutable weakByteFileCount: int
+        mutable byteFileCount: int
+    }
 
 let stats =
-    { rawMemoryFileCount = 0
-      memoryMapFileOpenedCount = 0
-      memoryMapFileClosedCount = 0
-      weakByteFileCount = 0
-      byteFileCount = 0 }
+    {
+        rawMemoryFileCount = 0
+        memoryMapFileOpenedCount = 0
+        memoryMapFileClosedCount = 0
+        weakByteFileCount = 0
+        byteFileCount = 0
+    }
 
-let GetStatistics() = stats
+let GetStatistics () = stats
 
 type private BinaryView = ReadOnlyByteMemory
 
@@ -120,15 +159,16 @@ type RawMemoryFile =
     val mutable private fileName: string
     val mutable private view: ReadOnlyByteMemory
 
-    new (fileName: string, obj: obj, addr: nativeint, length: int) =
+    new(fileName: string, obj: obj, addr: nativeint, length: int) =
         stats.rawMemoryFileCount <- stats.rawMemoryFileCount + 1
+
         {
             holder = obj
             fileName = fileName
             view = ByteMemory.FromUnsafePointer(addr, length, obj).AsReadOnly()
         }
 
-    new (fileName: string, holder: obj, bmem: ByteMemory) =
+    new(fileName: string, holder: obj, bmem: ByteMemory) =
         {
             holder = holder // gonna be finalized due to how we pass the holder when create RawByteMemory
             fileName = fileName
@@ -144,6 +184,7 @@ type RawMemoryFile =
 /// Gives a view over any ByteMemory, can be stream-based, mmap-ed, or just byte array.
 type ByteMemoryFile(fileName: string, view: ByteMemory) =
     member _.FileName = fileName
+
     interface BinaryFile with
         override _.GetView() = view.AsReadOnly()
 
@@ -153,6 +194,7 @@ type ByteFile(fileName: string, bytes: byte[]) =
     let view = ByteMemory.FromArray(bytes).AsReadOnly()
     do stats.byteFileCount <- stats.byteFileCount + 1
     member _.FileName = fileName
+
     interface BinaryFile with
         override bf.GetView() = view
 
@@ -164,8 +206,7 @@ type PEFile(fileName: string, peReader: PEReader) as this =
 
     member _.FileName = fileName
 
-    override _.Finalize() =
-        peReader.Dispose()
+    override _.Finalize() = peReader.Dispose()
 
     interface BinaryFile with
         override _.GetView() =
@@ -173,7 +214,10 @@ type PEFile(fileName: string, peReader: PEReader) as this =
             | true, m -> m.AsReadOnly()
             | _ ->
                 let block = peReader.GetEntireImage() // it's ok to call this everytime we do GetView as it is cached in the PEReader.
-                let m = ByteMemory.FromUnsafePointer(block.Pointer |> NativePtr.toNativeInt, block.Length, this)
+
+                let m =
+                    ByteMemory.FromUnsafePointer(block.Pointer |> NativePtr.toNativeInt, block.Length, this)
+
                 weakMemory <- WeakReference(m)
                 m.AsReadOnly()
 
@@ -199,15 +243,17 @@ type WeakByteFile(fileName: string, chunk: (int * int) option) =
         override this.GetView() =
             let strongBytes =
                 let mutable tg = null
+
                 if not (weakBytes.TryGetTarget(&tg)) then
                     if FileSystem.GetLastWriteTimeShim fileName <> fileStamp then
-                        error (Error (FSComp.SR.ilreadFileChanged fileName, range0))
+                        error (Error(FSComp.SR.ilreadFileChanged fileName, range0))
 
                     let bytes =
                         use stream = FileSystem.OpenFileForReadShim(fileName)
+
                         match chunk with
                         | None -> stream.ReadAllBytes()
-                        | Some(start, length) -> stream.ReadBytes(start, length)
+                        | Some (start, length) -> stream.ReadBytes(start, length)
 
                     tg <- bytes
 
@@ -217,7 +263,6 @@ type WeakByteFile(fileName: string, chunk: (int * int) option) =
 
             ByteMemory.FromArray(strongBytes).AsReadOnly()
 
-
 let seekReadByte (mdv: BinaryView) addr = mdv[addr]
 let seekReadBytes (mdv: BinaryView) addr len = mdv.ReadBytes(addr, len)
 let seekReadInt32 (mdv: BinaryView) addr = mdv.ReadInt32 addr
@@ -227,31 +272,40 @@ let seekReadByteAsInt32 mdv addr = int32 (seekReadByte mdv addr)
 
 let seekReadInt64 mdv addr =
     let b0 = seekReadByte mdv addr
-    let b1 = seekReadByte mdv (addr+1)
-    let b2 = seekReadByte mdv (addr+2)
-    let b3 = seekReadByte mdv (addr+3)
-    let b4 = seekReadByte mdv (addr+4)
-    let b5 = seekReadByte mdv (addr+5)
-    let b6 = seekReadByte mdv (addr+6)
-    let b7 = seekReadByte mdv (addr+7)
-    int64 b0 ||| (int64 b1 <<< 8) ||| (int64 b2 <<< 16) ||| (int64 b3 <<< 24) |||
-    (int64 b4 <<< 32) ||| (int64 b5 <<< 40) ||| (int64 b6 <<< 48) ||| (int64 b7 <<< 56)
+    let b1 = seekReadByte mdv (addr + 1)
+    let b2 = seekReadByte mdv (addr + 2)
+    let b3 = seekReadByte mdv (addr + 3)
+    let b4 = seekReadByte mdv (addr + 4)
+    let b5 = seekReadByte mdv (addr + 5)
+    let b6 = seekReadByte mdv (addr + 6)
+    let b7 = seekReadByte mdv (addr + 7)
+
+    int64 b0
+    ||| (int64 b1 <<< 8)
+    ||| (int64 b2 <<< 16)
+    ||| (int64 b3 <<< 24)
+    ||| (int64 b4 <<< 32)
+    ||| (int64 b5 <<< 40)
+    ||| (int64 b6 <<< 48)
+    ||| (int64 b7 <<< 56)
 
 let seekReadUInt16AsInt32 mdv addr = int32 (seekReadUInt16 mdv addr)
 
 let seekReadCompressedUInt32 mdv addr =
     let b0 = seekReadByte mdv addr
-    if b0 <= 0x7Fuy then struct (int b0, addr+1)
+
+    if b0 <= 0x7Fuy then
+        struct (int b0, addr + 1)
     elif b0 <= 0xBFuy then
         let b0 = b0 &&& 0x7Fuy
-        let b1 = seekReadByteAsInt32 mdv (addr+1)
-        struct ((int b0 <<< 8) ||| int b1, addr+2)
+        let b1 = seekReadByteAsInt32 mdv (addr + 1)
+        struct ((int b0 <<< 8) ||| int b1, addr + 2)
     else
         let b0 = b0 &&& 0x3Fuy
-        let b1 = seekReadByteAsInt32 mdv (addr+1)
-        let b2 = seekReadByteAsInt32 mdv (addr+2)
-        let b3 = seekReadByteAsInt32 mdv (addr+3)
-        struct ((int b0 <<< 24) ||| (int b1 <<< 16) ||| (int b2 <<< 8) ||| int b3, addr+4)
+        let b1 = seekReadByteAsInt32 mdv (addr + 1)
+        let b2 = seekReadByteAsInt32 mdv (addr + 2)
+        let b3 = seekReadByteAsInt32 mdv (addr + 3)
+        struct ((int b0 <<< 24) ||| (int b1 <<< 16) ||| (int b2 <<< 8) ||| int b3, addr + 4)
 
 let seekReadSByte mdv addr = sbyte (seekReadByte mdv addr)
 let seekReadSingle mdv addr = singleOfBits (seekReadInt32 mdv addr)
@@ -259,12 +313,15 @@ let seekReadDouble mdv addr = doubleOfBits (seekReadInt64 mdv addr)
 
 let rec seekCountUtf8String mdv addr n =
     let c = seekReadByteAsInt32 mdv addr
-    if c = 0 then n
-    else seekCountUtf8String mdv (addr+1) (n+1)
+
+    if c = 0 then
+        n
+    else
+        seekCountUtf8String mdv (addr + 1) (n + 1)
 
 let seekReadUTF8String (mdv: BinaryView) addr =
     let n = seekCountUtf8String mdv addr 0
-    mdv.ReadUtf8String (addr, n)
+    mdv.ReadUtf8String(addr, n)
 
 let seekReadBlob mdv addr =
     let struct (len, addr) = seekReadCompressedUInt32 mdv addr
@@ -280,13 +337,13 @@ let seekReadGuid mdv addr = seekReadBytes mdv addr 0x10
 let seekReadUncodedToken mdv addr =
     i32ToUncodedToken (seekReadInt32 mdv addr)
 
-
 //---------------------------------------------------------------------
 // Primitives to help read signatures. These do not use the file cursor
 //---------------------------------------------------------------------
 
 let sigptrCheck (bytes: byte[]) sigptr =
-    if checking && sigptr >= bytes.Length then failwith "read past end of sig. "
+    if checking && sigptr >= bytes.Length then
+        failwith "read past end of sig. "
 
 // All this code should be moved to use a mutable index into the signature
 //
@@ -318,9 +375,9 @@ let sigptrGetInt16 bytes sigptr =
 let sigptrGetInt32 bytes sigptr =
     sigptrCheck bytes sigptr
     let b0 = bytes[sigptr]
-    let b1 = bytes[sigptr+1]
-    let b2 = bytes[sigptr+2]
-    let b3 = bytes[sigptr+3]
+    let b1 = bytes[sigptr + 1]
+    let b2 = bytes[sigptr + 2]
+    let b3 = bytes[sigptr + 3]
     let res = int b0 ||| (int b1 <<< 8) ||| (int b2 <<< 16) ||| (int b3 <<< 24)
     res, sigptr + 4
 
@@ -347,7 +404,9 @@ let sigptrGetDouble bytes sigptr =
 
 let sigptrGetZInt32 bytes sigptr =
     let b0, sigptr = sigptrGetByte bytes sigptr
-    if b0 <= 0x7Fuy then struct (int b0, sigptr)
+
+    if b0 <= 0x7Fuy then
+        struct (int b0, sigptr)
     elif b0 <= 0xBFuy then
         let b0 = b0 &&& 0x7Fuy
         let b1, sigptr = sigptrGetByte bytes sigptr
@@ -362,20 +421,20 @@ let sigptrGetZInt32 bytes sigptr =
 let rec sigptrFoldAcc f n (bytes: byte[]) (sigptr: int) i acc =
     if i < n then
         let x, sp = f bytes sigptr
-        sigptrFoldAcc f n bytes sp (i+1) (x :: acc)
+        sigptrFoldAcc f n bytes sp (i + 1) (x :: acc)
     else
         List.rev acc, sigptr
 
-let sigptrFold f n (bytes: byte[]) (sigptr: int) =
-    sigptrFoldAcc f n bytes sigptr 0 []
+let sigptrFold f n (bytes: byte[]) (sigptr: int) = sigptrFoldAcc f n bytes sigptr 0 []
 
 let sigptrFoldStruct f n (bytes: byte[]) (sigptr: int) =
     let rec sigptrFoldAcc f n (bytes: byte[]) (sigptr: int) i acc =
         if i < n then
             let struct (x, sp) = f bytes sigptr
-            sigptrFoldAcc f n bytes sp (i+1) (x :: acc)
+            sigptrFoldAcc f n bytes sp (i + 1) (x :: acc)
         else
             struct (List.rev acc, sigptr)
+
     sigptrFoldAcc f n bytes sigptr 0 []
 
 let sigptrGetBytes n (bytes: byte[]) sigptr =
@@ -384,68 +443,116 @@ let sigptrGetBytes n (bytes: byte[]) sigptr =
         Bytes.zeroCreate 0, sigptr
     else
         let res = Bytes.zeroCreate n
+
         for i = 0 to (n - 1) do
             res[i] <- bytes[sigptr + i]
+
         res, sigptr + n
 
 let sigptrGetString n bytes sigptr =
     let bytearray, sigptr = sigptrGetBytes n bytes sigptr
     (Encoding.UTF8.GetString(bytearray, 0, bytearray.Length)), sigptr
 
-
 // --------------------------------------------------------------------
 // Now the tables of instructions
 // --------------------------------------------------------------------
 
 []
 type ILInstrPrefixesRegister =
-   { mutable al: ILAlignment
-     mutable tl: ILTailcall
-     mutable vol: ILVolatility
-     mutable ro: ILReadonly
-     mutable constrained: ILType option}
+    {
+        mutable al: ILAlignment
+        mutable tl: ILTailcall
+        mutable vol: ILVolatility
+        mutable ro: ILReadonly
+        mutable constrained: ILType option
+    }
 
 let noPrefixes mk prefixes =
-    if prefixes.al <> Aligned then failwith "an unaligned prefix is not allowed here"
-    if prefixes.vol <> Nonvolatile then failwith "a volatile prefix is not allowed here"
-    if prefixes.tl <> Normalcall then failwith "a tailcall prefix is not allowed here"
-    if prefixes.ro <> NormalAddress then failwith "a readonly prefix is not allowed here"
-    if prefixes.constrained <> None then failwith "a constrained prefix is not allowed here"
+    if prefixes.al <> Aligned then
+        failwith "an unaligned prefix is not allowed here"
+
+    if prefixes.vol <> Nonvolatile then
+        failwith "a volatile prefix is not allowed here"
+
+    if prefixes.tl <> Normalcall then
+        failwith "a tailcall prefix is not allowed here"
+
+    if prefixes.ro <> NormalAddress then
+        failwith "a readonly prefix is not allowed here"
+
+    if prefixes.constrained <> None then
+        failwith "a constrained prefix is not allowed here"
+
     mk
 
 let volatileOrUnalignedPrefix mk prefixes =
-    if prefixes.tl <> Normalcall then failwith "a tailcall prefix is not allowed here"
-    if prefixes.constrained <> None then failwith "a constrained prefix is not allowed here"
-    if prefixes.ro <> NormalAddress then failwith "a readonly prefix is not allowed here"
+    if prefixes.tl <> Normalcall then
+        failwith "a tailcall prefix is not allowed here"
+
+    if prefixes.constrained <> None then
+        failwith "a constrained prefix is not allowed here"
+
+    if prefixes.ro <> NormalAddress then
+        failwith "a readonly prefix is not allowed here"
+
     mk (prefixes.al, prefixes.vol)
 
 let volatilePrefix mk prefixes =
-    if prefixes.al <> Aligned then failwith "an unaligned prefix is not allowed here"
-    if prefixes.tl <> Normalcall then failwith "a tailcall prefix is not allowed here"
-    if prefixes.constrained <> None then failwith "a constrained prefix is not allowed here"
-    if prefixes.ro <> NormalAddress then failwith "a readonly prefix is not allowed here"
+    if prefixes.al <> Aligned then
+        failwith "an unaligned prefix is not allowed here"
+
+    if prefixes.tl <> Normalcall then
+        failwith "a tailcall prefix is not allowed here"
+
+    if prefixes.constrained <> None then
+        failwith "a constrained prefix is not allowed here"
+
+    if prefixes.ro <> NormalAddress then
+        failwith "a readonly prefix is not allowed here"
+
     mk prefixes.vol
 
 let tailPrefix mk prefixes =
-    if prefixes.al <> Aligned then failwith "an unaligned prefix is not allowed here"
-    if prefixes.vol <> Nonvolatile then failwith "a volatile prefix is not allowed here"
-    if prefixes.constrained <> None then failwith "a constrained prefix is not allowed here"
-    if prefixes.ro <> NormalAddress then failwith "a readonly prefix is not allowed here"
+    if prefixes.al <> Aligned then
+        failwith "an unaligned prefix is not allowed here"
+
+    if prefixes.vol <> Nonvolatile then
+        failwith "a volatile prefix is not allowed here"
+
+    if prefixes.constrained <> None then
+        failwith "a constrained prefix is not allowed here"
+
+    if prefixes.ro <> NormalAddress then
+        failwith "a readonly prefix is not allowed here"
+
     mk prefixes.tl
 
 let constraintOrTailPrefix mk prefixes =
-    if prefixes.al <> Aligned then failwith "an unaligned prefix is not allowed here"
-    if prefixes.vol <> Nonvolatile then failwith "a volatile prefix is not allowed here"
-    if prefixes.ro <> NormalAddress then failwith "a readonly prefix is not allowed here"
-    mk (prefixes.constrained, prefixes.tl )
+    if prefixes.al <> Aligned then
+        failwith "an unaligned prefix is not allowed here"
+
+    if prefixes.vol <> Nonvolatile then
+        failwith "a volatile prefix is not allowed here"
+
+    if prefixes.ro <> NormalAddress then
+        failwith "a readonly prefix is not allowed here"
+
+    mk (prefixes.constrained, prefixes.tl)
 
 let readonlyPrefix mk prefixes =
-    if prefixes.al <> Aligned then failwith "an unaligned prefix is not allowed here"
-    if prefixes.vol <> Nonvolatile then failwith "a volatile prefix is not allowed here"
-    if prefixes.tl <> Normalcall then failwith "a tailcall prefix is not allowed here"
-    if prefixes.constrained <> None then failwith "a constrained prefix is not allowed here"
-    mk prefixes.ro
+    if prefixes.al <> Aligned then
+        failwith "an unaligned prefix is not allowed here"
+
+    if prefixes.vol <> Nonvolatile then
+        failwith "a volatile prefix is not allowed here"
 
+    if prefixes.tl <> Normalcall then
+        failwith "a tailcall prefix is not allowed here"
+
+    if prefixes.constrained <> None then
+        failwith "a constrained prefix is not allowed here"
+
+    mk prefixes.ro
 
 []
 type ILInstrDecoder =
@@ -470,143 +577,166 @@ type ILInstrDecoder =
     | I_type_instr of (ILInstrPrefixesRegister -> ILType -> ILInstr)
     | I_invalid_instr
 
-let mkStind dt = volatileOrUnalignedPrefix (fun (x, y) -> I_stind(x, y, dt))
-let mkLdind dt = volatileOrUnalignedPrefix (fun (x, y) -> I_ldind(x, y, dt))
+let mkStind dt =
+    volatileOrUnalignedPrefix (fun (x, y) -> I_stind(x, y, dt))
+
+let mkLdind dt =
+    volatileOrUnalignedPrefix (fun (x, y) -> I_ldind(x, y, dt))
 
 let instrs () =
- [ i_ldarg_s, I_u16_u8_instr (noPrefixes mkLdarg)
-   i_starg_s, I_u16_u8_instr (noPrefixes I_starg)
-   i_ldarga_s, I_u16_u8_instr (noPrefixes I_ldarga)
-   i_stloc_s, I_u16_u8_instr (noPrefixes mkStloc)
-   i_ldloc_s, I_u16_u8_instr (noPrefixes mkLdloc)
-   i_ldloca_s, I_u16_u8_instr (noPrefixes I_ldloca)
-   i_ldarg, I_u16_u16_instr (noPrefixes mkLdarg)
-   i_starg, I_u16_u16_instr (noPrefixes I_starg)
-   i_ldarga, I_u16_u16_instr (noPrefixes I_ldarga)
-   i_stloc, I_u16_u16_instr (noPrefixes mkStloc)
-   i_ldloc, I_u16_u16_instr (noPrefixes mkLdloc)
-   i_ldloca, I_u16_u16_instr (noPrefixes I_ldloca)
-   i_stind_i, I_none_instr (mkStind DT_I)
-   i_stind_i1, I_none_instr (mkStind DT_I1)
-   i_stind_i2, I_none_instr (mkStind DT_I2)
-   i_stind_i4, I_none_instr (mkStind DT_I4)
-   i_stind_i8, I_none_instr (mkStind DT_I8)
-   i_stind_r4, I_none_instr (mkStind DT_R4)
-   i_stind_r8, I_none_instr (mkStind DT_R8)
-   i_stind_ref, I_none_instr (mkStind DT_REF)
-   i_ldind_i, I_none_instr (mkLdind DT_I)
-   i_ldind_i1, I_none_instr (mkLdind DT_I1)
-   i_ldind_i2, I_none_instr (mkLdind DT_I2)
-   i_ldind_i4, I_none_instr (mkLdind DT_I4)
-   i_ldind_i8, I_none_instr (mkLdind DT_I8)
-   i_ldind_u1, I_none_instr (mkLdind DT_U1)
-   i_ldind_u2, I_none_instr (mkLdind DT_U2)
-   i_ldind_u4, I_none_instr (mkLdind DT_U4)
-   i_ldind_r4, I_none_instr (mkLdind DT_R4)
-   i_ldind_r8, I_none_instr (mkLdind DT_R8)
-   i_ldind_ref, I_none_instr (mkLdind DT_REF)
-   i_cpblk, I_none_instr (volatileOrUnalignedPrefix I_cpblk)
-   i_initblk, I_none_instr (volatileOrUnalignedPrefix I_initblk)
-   i_ldc_i8, I_i64_instr (noPrefixes (fun x ->(AI_ldc (DT_I8, ILConst.I8 x))))
-   i_ldc_i4, I_i32_i32_instr (noPrefixes mkLdcInt32)
-   i_ldc_i4_s, I_i32_i8_instr (noPrefixes mkLdcInt32)
-   i_ldc_r4, I_r4_instr (noPrefixes (fun x -> (AI_ldc (DT_R4, ILConst.R4 x))))
-   i_ldc_r8, I_r8_instr (noPrefixes (fun x -> (AI_ldc (DT_R8, ILConst.R8 x))))
-   i_ldfld, I_field_instr (volatileOrUnalignedPrefix(fun (x, y) fspec -> I_ldfld (x, y, fspec)))
-   i_stfld, I_field_instr (volatileOrUnalignedPrefix(fun (x, y) fspec -> I_stfld (x, y, fspec)))
-   i_ldsfld, I_field_instr (volatilePrefix (fun x fspec -> I_ldsfld (x, fspec)))
-   i_stsfld, I_field_instr (volatilePrefix (fun x fspec -> I_stsfld (x, fspec)))
-   i_ldflda, I_field_instr (noPrefixes I_ldflda)
-   i_ldsflda, I_field_instr (noPrefixes I_ldsflda)
-   i_call, I_method_instr (tailPrefix (fun tl (mspec, y) -> I_call (tl, mspec, y)))
-   i_ldftn, I_method_instr (noPrefixes (fun (mspec, _y) -> I_ldftn mspec))
-   i_ldvirtftn, I_method_instr (noPrefixes (fun (mspec, _y) -> I_ldvirtftn mspec))
-   i_newobj, I_method_instr (noPrefixes I_newobj)
-   i_callvirt, I_method_instr (constraintOrTailPrefix (fun (c, tl) (mspec, y) -> match c with Some ty -> I_callconstraint(tl, ty, mspec, y) | None -> I_callvirt (tl, mspec, y)))
-   i_leave_s, I_unconditional_i8_instr (noPrefixes (fun x -> I_leave x))
-   i_br_s, I_unconditional_i8_instr (noPrefixes I_br)
-   i_leave, I_unconditional_i32_instr (noPrefixes (fun x -> I_leave x))
-   i_br, I_unconditional_i32_instr (noPrefixes I_br)
-   i_brtrue_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_brtrue, x)))
-   i_brfalse_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_brfalse, x)))
-   i_beq_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_beq, x)))
-   i_blt_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_blt, x)))
-   i_blt_un_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_blt_un, x)))
-   i_ble_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_ble, x)))
-   i_ble_un_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_ble_un, x)))
-   i_bgt_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_bgt, x)))
-   i_bgt_un_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_bgt_un, x)))
-   i_bge_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_bge, x)))
-   i_bge_un_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_bge_un, x)))
-   i_bne_un_s, I_conditional_i8_instr (noPrefixes (fun x -> I_brcmp (BI_bne_un, x)))
-   i_brtrue, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_brtrue, x)))
-   i_brfalse, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_brfalse, x)))
-   i_beq, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_beq, x)))
-   i_blt, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_blt, x)))
-   i_blt_un, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_blt_un, x)))
-   i_ble, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_ble, x)))
-   i_ble_un, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_ble_un, x)))
-   i_bgt, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_bgt, x)))
-   i_bgt_un, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_bgt_un, x)))
-   i_bge, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_bge, x)))
-   i_bge_un, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_bge_un, x)))
-   i_bne_un, I_conditional_i32_instr (noPrefixes (fun x -> I_brcmp (BI_bne_un, x)))
-   i_ldstr, I_string_instr (noPrefixes I_ldstr)
-   i_switch, I_switch_instr (noPrefixes I_switch)
-   i_ldtoken, I_tok_instr (noPrefixes I_ldtoken)
-   i_calli, I_sig_instr (tailPrefix (fun tl (x, y) -> I_calli (tl, x, y)))
-   i_mkrefany, I_type_instr (noPrefixes I_mkrefany)
-   i_refanyval, I_type_instr (noPrefixes I_refanyval)
-   i_ldelema, I_type_instr (readonlyPrefix (fun ro x -> I_ldelema (ro, false, ILArrayShape.SingleDimensional, x)))
-   i_ldelem_any, I_type_instr (noPrefixes (fun x -> I_ldelem_any (ILArrayShape.SingleDimensional, x)))
-   i_stelem_any, I_type_instr (noPrefixes (fun x -> I_stelem_any (ILArrayShape.SingleDimensional, x)))
-   i_newarr, I_type_instr (noPrefixes (fun x -> I_newarr (ILArrayShape.SingleDimensional, x)))
-   i_castclass, I_type_instr (noPrefixes I_castclass)
-   i_isinst, I_type_instr (noPrefixes I_isinst)
-   i_unbox_any, I_type_instr (noPrefixes I_unbox_any)
-   i_cpobj, I_type_instr (noPrefixes I_cpobj)
-   i_initobj, I_type_instr (noPrefixes I_initobj)
-   i_ldobj, I_type_instr (volatileOrUnalignedPrefix (fun (x, y) z -> I_ldobj (x, y, z)))
-   i_stobj, I_type_instr (volatileOrUnalignedPrefix (fun (x, y) z -> I_stobj (x, y, z)))
-   i_sizeof, I_type_instr (noPrefixes I_sizeof)
-   i_box, I_type_instr (noPrefixes I_box)
-   i_unbox, I_type_instr (noPrefixes I_unbox) ]
+    [
+        i_ldarg_s, I_u16_u8_instr(noPrefixes mkLdarg)
+        i_starg_s, I_u16_u8_instr(noPrefixes I_starg)
+        i_ldarga_s, I_u16_u8_instr(noPrefixes I_ldarga)
+        i_stloc_s, I_u16_u8_instr(noPrefixes mkStloc)
+        i_ldloc_s, I_u16_u8_instr(noPrefixes mkLdloc)
+        i_ldloca_s, I_u16_u8_instr(noPrefixes I_ldloca)
+        i_ldarg, I_u16_u16_instr(noPrefixes mkLdarg)
+        i_starg, I_u16_u16_instr(noPrefixes I_starg)
+        i_ldarga, I_u16_u16_instr(noPrefixes I_ldarga)
+        i_stloc, I_u16_u16_instr(noPrefixes mkStloc)
+        i_ldloc, I_u16_u16_instr(noPrefixes mkLdloc)
+        i_ldloca, I_u16_u16_instr(noPrefixes I_ldloca)
+        i_stind_i, I_none_instr(mkStind DT_I)
+        i_stind_i1, I_none_instr(mkStind DT_I1)
+        i_stind_i2, I_none_instr(mkStind DT_I2)
+        i_stind_i4, I_none_instr(mkStind DT_I4)
+        i_stind_i8, I_none_instr(mkStind DT_I8)
+        i_stind_r4, I_none_instr(mkStind DT_R4)
+        i_stind_r8, I_none_instr(mkStind DT_R8)
+        i_stind_ref, I_none_instr(mkStind DT_REF)
+        i_ldind_i, I_none_instr(mkLdind DT_I)
+        i_ldind_i1, I_none_instr(mkLdind DT_I1)
+        i_ldind_i2, I_none_instr(mkLdind DT_I2)
+        i_ldind_i4, I_none_instr(mkLdind DT_I4)
+        i_ldind_i8, I_none_instr(mkLdind DT_I8)
+        i_ldind_u1, I_none_instr(mkLdind DT_U1)
+        i_ldind_u2, I_none_instr(mkLdind DT_U2)
+        i_ldind_u4, I_none_instr(mkLdind DT_U4)
+        i_ldind_r4, I_none_instr(mkLdind DT_R4)
+        i_ldind_r8, I_none_instr(mkLdind DT_R8)
+        i_ldind_ref, I_none_instr(mkLdind DT_REF)
+        i_cpblk, I_none_instr(volatileOrUnalignedPrefix I_cpblk)
+        i_initblk, I_none_instr(volatileOrUnalignedPrefix I_initblk)
+        i_ldc_i8, I_i64_instr(noPrefixes (fun x -> (AI_ldc(DT_I8, ILConst.I8 x))))
+        i_ldc_i4, I_i32_i32_instr(noPrefixes mkLdcInt32)
+        i_ldc_i4_s, I_i32_i8_instr(noPrefixes mkLdcInt32)
+        i_ldc_r4, I_r4_instr(noPrefixes (fun x -> (AI_ldc(DT_R4, ILConst.R4 x))))
+        i_ldc_r8, I_r8_instr(noPrefixes (fun x -> (AI_ldc(DT_R8, ILConst.R8 x))))
+        i_ldfld, I_field_instr(volatileOrUnalignedPrefix (fun (x, y) fspec -> I_ldfld(x, y, fspec)))
+        i_stfld, I_field_instr(volatileOrUnalignedPrefix (fun (x, y) fspec -> I_stfld(x, y, fspec)))
+        i_ldsfld, I_field_instr(volatilePrefix (fun x fspec -> I_ldsfld(x, fspec)))
+        i_stsfld, I_field_instr(volatilePrefix (fun x fspec -> I_stsfld(x, fspec)))
+        i_ldflda, I_field_instr(noPrefixes I_ldflda)
+        i_ldsflda, I_field_instr(noPrefixes I_ldsflda)
+        i_call, I_method_instr(tailPrefix (fun tl (mspec, y) -> I_call(tl, mspec, y)))
+        i_ldftn, I_method_instr(noPrefixes (fun (mspec, _y) -> I_ldftn mspec))
+        i_ldvirtftn, I_method_instr(noPrefixes (fun (mspec, _y) -> I_ldvirtftn mspec))
+        i_newobj, I_method_instr(noPrefixes I_newobj)
+        i_callvirt,
+        I_method_instr(
+            constraintOrTailPrefix (fun (c, tl) (mspec, y) ->
+                match c with
+                | Some ty -> I_callconstraint(tl, ty, mspec, y)
+                | None -> I_callvirt(tl, mspec, y))
+        )
+        i_leave_s, I_unconditional_i8_instr(noPrefixes (fun x -> I_leave x))
+        i_br_s, I_unconditional_i8_instr(noPrefixes I_br)
+        i_leave, I_unconditional_i32_instr(noPrefixes (fun x -> I_leave x))
+        i_br, I_unconditional_i32_instr(noPrefixes I_br)
+        i_brtrue_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_brtrue, x)))
+        i_brfalse_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_brfalse, x)))
+        i_beq_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_beq, x)))
+        i_blt_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_blt, x)))
+        i_blt_un_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_blt_un, x)))
+        i_ble_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_ble, x)))
+        i_ble_un_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_ble_un, x)))
+        i_bgt_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_bgt, x)))
+        i_bgt_un_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_bgt_un, x)))
+        i_bge_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_bge, x)))
+        i_bge_un_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_bge_un, x)))
+        i_bne_un_s, I_conditional_i8_instr(noPrefixes (fun x -> I_brcmp(BI_bne_un, x)))
+        i_brtrue, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_brtrue, x)))
+        i_brfalse, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_brfalse, x)))
+        i_beq, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_beq, x)))
+        i_blt, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_blt, x)))
+        i_blt_un, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_blt_un, x)))
+        i_ble, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_ble, x)))
+        i_ble_un, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_ble_un, x)))
+        i_bgt, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_bgt, x)))
+        i_bgt_un, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_bgt_un, x)))
+        i_bge, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_bge, x)))
+        i_bge_un, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_bge_un, x)))
+        i_bne_un, I_conditional_i32_instr(noPrefixes (fun x -> I_brcmp(BI_bne_un, x)))
+        i_ldstr, I_string_instr(noPrefixes I_ldstr)
+        i_switch, I_switch_instr(noPrefixes I_switch)
+        i_ldtoken, I_tok_instr(noPrefixes I_ldtoken)
+        i_calli, I_sig_instr(tailPrefix (fun tl (x, y) -> I_calli(tl, x, y)))
+        i_mkrefany, I_type_instr(noPrefixes I_mkrefany)
+        i_refanyval, I_type_instr(noPrefixes I_refanyval)
+        i_ldelema, I_type_instr(readonlyPrefix (fun ro x -> I_ldelema(ro, false, ILArrayShape.SingleDimensional, x)))
+        i_ldelem_any, I_type_instr(noPrefixes (fun x -> I_ldelem_any(ILArrayShape.SingleDimensional, x)))
+        i_stelem_any, I_type_instr(noPrefixes (fun x -> I_stelem_any(ILArrayShape.SingleDimensional, x)))
+        i_newarr, I_type_instr(noPrefixes (fun x -> I_newarr(ILArrayShape.SingleDimensional, x)))
+        i_castclass, I_type_instr(noPrefixes I_castclass)
+        i_isinst, I_type_instr(noPrefixes I_isinst)
+        i_unbox_any, I_type_instr(noPrefixes I_unbox_any)
+        i_cpobj, I_type_instr(noPrefixes I_cpobj)
+        i_initobj, I_type_instr(noPrefixes I_initobj)
+        i_ldobj, I_type_instr(volatileOrUnalignedPrefix (fun (x, y) z -> I_ldobj(x, y, z)))
+        i_stobj, I_type_instr(volatileOrUnalignedPrefix (fun (x, y) z -> I_stobj(x, y, z)))
+        i_sizeof, I_type_instr(noPrefixes I_sizeof)
+        i_box, I_type_instr(noPrefixes I_box)
+        i_unbox, I_type_instr(noPrefixes I_unbox)
+    ]
 
 // The tables are delayed to avoid building them unnecessarily at startup
 // Many applications of AbsIL (e.g. a compiler) don't need to read instructions.
 let mutable oneByteInstrs = None
 let mutable twoByteInstrs = None
+
 let fillInstrs () =
     let oneByteInstrTable = Array.create 256 I_invalid_instr
     let twoByteInstrTable = Array.create 256 I_invalid_instr
+
     let addInstr (i, f) =
         if i > 0xff then
             assert (i >>>& 8 = 0xfe)
             let i = (i &&& 0xff)
+
             match twoByteInstrTable[i] with
             | I_invalid_instr -> ()
-            | _ -> dprintn ("warning: duplicate decode entries for "+string i)
+            | _ -> dprintn ("warning: duplicate decode entries for " + string i)
+
             twoByteInstrTable[i] <- f
         else
             match oneByteInstrTable[i] with
             | I_invalid_instr -> ()
-            | _ -> dprintn ("warning: duplicate decode entries for "+string i)
+            | _ -> dprintn ("warning: duplicate decode entries for " + string i)
+
             oneByteInstrTable[i] <- f
-    for i in instrs() do
+
+    for i in instrs () do
         addInstr i
+
     for x, mk in noArgInstrs.Force() do
-        addInstr (x, I_none_instr (noPrefixes mk))
+        addInstr (x, I_none_instr(noPrefixes mk))
+
     oneByteInstrs <- Some oneByteInstrTable
     twoByteInstrs <- Some twoByteInstrTable
 
 let rec getOneByteInstr i =
     match oneByteInstrs with
-    | None -> fillInstrs(); getOneByteInstr i
+    | None ->
+        fillInstrs ()
+        getOneByteInstr i
     | Some t -> t[i]
 
 let rec getTwoByteInstr i =
     match twoByteInstrs with
-    | None -> fillInstrs(); getTwoByteInstr i
+    | None ->
+        fillInstrs ()
+        getTwoByteInstr i
     | Some t -> t[i]
 
 //---------------------------------------------------------------------
@@ -615,8 +745,8 @@ let rec getTwoByteInstr i =
 
 type ImageChunk = { size: int32; addr: int32 }
 
-let chunk sz next = ({addr=next; size=sz}, next + sz)
-let nochunk next = ({addr= 0x0;size= 0x0; }, next)
+let chunk sz next = ({ addr = next; size = sz }, next + sz)
+let nochunk next = ({ addr = 0x0; size = 0x0 }, next)
 
 type RowElementKind =
     | UShort
@@ -643,16 +773,37 @@ type RowElementKind =
 
 type RowKind = RowKind of RowElementKind list
 
-let kindAssemblyRef = RowKind [ UShort; UShort; UShort; UShort; ULong; Blob; SString; SString; Blob; ]
+let kindAssemblyRef =
+    RowKind [ UShort; UShort; UShort; UShort; ULong; Blob; SString; SString; Blob ]
+
 let kindModuleRef = RowKind [ SString ]
 let kindFileRef = RowKind [ ULong; SString; Blob ]
 let kindTypeRef = RowKind [ ResolutionScope; SString; SString ]
 let kindTypeSpec = RowKind [ Blob ]
-let kindTypeDef = RowKind [ ULong; SString; SString; TypeDefOrRefOrSpec; SimpleIndex TableNames.Field; SimpleIndex TableNames.Method ]
-let kindPropertyMap = RowKind [ SimpleIndex TableNames.TypeDef; SimpleIndex TableNames.Property ]
-let kindEventMap = RowKind [ SimpleIndex TableNames.TypeDef; SimpleIndex TableNames.Event ]
-let kindInterfaceImpl = RowKind [ SimpleIndex TableNames.TypeDef; TypeDefOrRefOrSpec ]
-let kindNested = RowKind [ SimpleIndex TableNames.TypeDef; SimpleIndex TableNames.TypeDef ]
+
+let kindTypeDef =
+    RowKind
+        [
+            ULong
+            SString
+            SString
+            TypeDefOrRefOrSpec
+            SimpleIndex TableNames.Field
+            SimpleIndex TableNames.Method
+        ]
+
+let kindPropertyMap =
+    RowKind [ SimpleIndex TableNames.TypeDef; SimpleIndex TableNames.Property ]
+
+let kindEventMap =
+    RowKind [ SimpleIndex TableNames.TypeDef; SimpleIndex TableNames.Event ]
+
+let kindInterfaceImpl =
+    RowKind [ SimpleIndex TableNames.TypeDef; TypeDefOrRefOrSpec ]
+
+let kindNested =
+    RowKind [ SimpleIndex TableNames.TypeDef; SimpleIndex TableNames.TypeDef ]
+
 let kindCustomAttribute = RowKind [ HasCustomAttribute; CustomAttributeType; Blob ]
 let kindDeclSecurity = RowKind [ UShort; HasDeclSecurity; Blob ]
 let kindMemberRef = RowKind [ MemberRefParent; SString; Blob ]
@@ -660,25 +811,42 @@ let kindStandAloneSig = RowKind [ Blob ]
 let kindFieldDef = RowKind [ UShort; SString; Blob ]
 let kindFieldRVA = RowKind [ Data; SimpleIndex TableNames.Field ]
 let kindFieldMarshal = RowKind [ HasFieldMarshal; Blob ]
-let kindConstant = RowKind [ UShort;HasConstant; Blob ]
+let kindConstant = RowKind [ UShort; HasConstant; Blob ]
 let kindFieldLayout = RowKind [ ULong; SimpleIndex TableNames.Field ]
 let kindParam = RowKind [ UShort; UShort; SString ]
-let kindMethodDef = RowKind [ ULong; UShort; UShort; SString; Blob; SimpleIndex TableNames.Param ]
-let kindMethodImpl = RowKind [ SimpleIndex TableNames.TypeDef; MethodDefOrRef; MethodDefOrRef ]
-let kindImplMap = RowKind [ UShort; MemberForwarded; SString; SimpleIndex TableNames.ModuleRef ]
-let kindMethodSemantics = RowKind [ UShort; SimpleIndex TableNames.Method; HasSemantics ]
+
+let kindMethodDef =
+    RowKind [ ULong; UShort; UShort; SString; Blob; SimpleIndex TableNames.Param ]
+
+let kindMethodImpl =
+    RowKind [ SimpleIndex TableNames.TypeDef; MethodDefOrRef; MethodDefOrRef ]
+
+let kindImplMap =
+    RowKind [ UShort; MemberForwarded; SString; SimpleIndex TableNames.ModuleRef ]
+
+let kindMethodSemantics =
+    RowKind [ UShort; SimpleIndex TableNames.Method; HasSemantics ]
+
 let kindProperty = RowKind [ UShort; SString; Blob ]
 let kindEvent = RowKind [ UShort; SString; TypeDefOrRefOrSpec ]
 let kindManifestResource = RowKind [ ULong; ULong; SString; Implementation ]
 let kindClassLayout = RowKind [ UShort; ULong; SimpleIndex TableNames.TypeDef ]
 let kindExportedType = RowKind [ ULong; ULong; SString; SString; Implementation ]
-let kindAssembly = RowKind [ ULong; UShort; UShort; UShort; UShort; ULong; Blob; SString; SString ]
-let kindGenericParam_v1_1 = RowKind [ UShort; UShort; TypeOrMethodDef; SString; TypeDefOrRefOrSpec ]
+
+let kindAssembly =
+    RowKind [ ULong; UShort; UShort; UShort; UShort; ULong; Blob; SString; SString ]
+
+let kindGenericParam_v1_1 =
+    RowKind [ UShort; UShort; TypeOrMethodDef; SString; TypeDefOrRefOrSpec ]
+
 let kindGenericParam_v2_0 = RowKind [ UShort; UShort; TypeOrMethodDef; SString ]
 let kindMethodSpec = RowKind [ MethodDefOrRef; Blob ]
-let kindGenericParamConstraint = RowKind [ SimpleIndex TableNames.GenericParam; TypeDefOrRefOrSpec ]
+
+let kindGenericParamConstraint =
+    RowKind [ SimpleIndex TableNames.GenericParam; TypeDefOrRefOrSpec ]
+
 let kindModule = RowKind [ UShort; SString; GGuid; GGuid; GGuid ]
-let kindIllegal = RowKind [ ]
+let kindIllegal = RowKind []
 
 //---------------------------------------------------------------------
 // Used for binary searches of sorted tables. Each function that reads
@@ -688,35 +856,47 @@ let kindIllegal = RowKind [ ]
 // kind of element in that column.
 //---------------------------------------------------------------------
 
-let hcCompare (TaggedIndex(t1: HasConstantTag, idx1: int)) (TaggedIndex(t2: HasConstantTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let hcCompare (TaggedIndex (t1: HasConstantTag, idx1: int)) (TaggedIndex (t2: HasConstantTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let hsCompare (TaggedIndex(t1: HasSemanticsTag, idx1: int)) (TaggedIndex(t2: HasSemanticsTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let hsCompare (TaggedIndex (t1: HasSemanticsTag, idx1: int)) (TaggedIndex (t2: HasSemanticsTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let hcaCompare (TaggedIndex(t1: HasCustomAttributeTag, idx1: int)) (TaggedIndex(t2: HasCustomAttributeTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let hcaCompare (TaggedIndex (t1: HasCustomAttributeTag, idx1: int)) (TaggedIndex (t2: HasCustomAttributeTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let mfCompare (TaggedIndex(t1: MemberForwardedTag, idx1: int)) (TaggedIndex(t2: MemberForwardedTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let mfCompare (TaggedIndex (t1: MemberForwardedTag, idx1: int)) (TaggedIndex (t2: MemberForwardedTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let hdsCompare (TaggedIndex(t1: HasDeclSecurityTag, idx1: int)) (TaggedIndex(t2: HasDeclSecurityTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let hdsCompare (TaggedIndex (t1: HasDeclSecurityTag, idx1: int)) (TaggedIndex (t2: HasDeclSecurityTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let hfmCompare (TaggedIndex(t1: HasFieldMarshalTag, idx1)) (TaggedIndex(t2: HasFieldMarshalTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let hfmCompare (TaggedIndex (t1: HasFieldMarshalTag, idx1)) (TaggedIndex (t2: HasFieldMarshalTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let tomdCompare (TaggedIndex(t1: TypeOrMethodDefTag, idx1)) (TaggedIndex(t2: TypeOrMethodDefTag, idx2)) =
-    if idx1 < idx2 then -1 elif idx1 > idx2 then 1 else compare t1.Tag t2.Tag
+let tomdCompare (TaggedIndex (t1: TypeOrMethodDefTag, idx1)) (TaggedIndex (t2: TypeOrMethodDefTag, idx2)) =
+    if idx1 < idx2 then -1
+    elif idx1 > idx2 then 1
+    else compare t1.Tag t2.Tag
 
-let simpleIndexCompare (idx1: int) (idx2: int) =
-    compare idx1 idx2
+let simpleIndexCompare (idx1: int) (idx2: int) = compare idx1 idx2
 
 //---------------------------------------------------------------------
 // The various keys for the various caches.
 //---------------------------------------------------------------------
 
-
 []
 type TypeDefAsTypIdx = TypeDefAsTypIdx of ILBoxity * ILGenericArgs * int
 
@@ -755,50 +935,60 @@ type GenericParamsIdx = GenericParamsIdx of numTypars: int * TypeOrMethodDefTag
 //---------------------------------------------------------------------
 
 let mkCacheInt32 lowMem _inbase _nm _sz =
-    if lowMem then (fun f x -> f x) else
-    let mutable cache = null
-    let mutable count = 0
+    if lowMem then
+        (fun f x -> f x)
+    else
+        let mutable cache = null
+        let mutable count = 0
 #if STATISTICS
-    addReport (fun oc -> if count <> 0 then oc.WriteLine ((_inbase + string count + " "+ _nm + " cache hits"): string))
+        addReport (fun oc ->
+            if count <> 0 then
+                oc.WriteLine((_inbase + string count + " " + _nm + " cache hits"): string))
 #endif
-    fun f (idx: int32) ->
-        let cache =
-            match cache with
-            | null -> cache <- ConcurrentDictionary(Environment.ProcessorCount, 11)
-            | _ -> ()
-            cache
-        match cache.TryGetValue idx with
-        | true, res ->
-            count <- count + 1
-            res
-        | _ ->
-            let res = f idx
-            cache[idx] <- res
-            res
+        fun f (idx: int32) ->
+            let cache =
+                match cache with
+                | null -> cache <- ConcurrentDictionary(Environment.ProcessorCount, 11)
+                | _ -> ()
+
+                cache
+
+            match cache.TryGetValue idx with
+            | true, res ->
+                count <- count + 1
+                res
+            | _ ->
+                let res = f idx
+                cache[idx] <- res
+                res
 
 let mkCacheGeneric lowMem _inbase _nm _sz =
-    if lowMem then (fun f x -> f x) else
-    let mutable cache = null
-    let mutable count = 0
+    if lowMem then
+        (fun f x -> f x)
+    else
+        let mutable cache = null
+        let mutable count = 0
 #if STATISTICS
-    addReport (fun oc -> if !count <> 0 then oc.WriteLine ((_inbase + string !count + " " + _nm + " cache hits"): string))
+        addReport (fun oc ->
+            if !count <> 0 then
+                oc.WriteLine((_inbase + string !count + " " + _nm + " cache hits"): string))
 #endif
-    fun f (idx :'T) ->
-        let cache =
-            match cache with
-            | null ->
-                cache <- ConcurrentDictionary<_, _>(Environment.ProcessorCount, 11 (* sz: int *) )
-            | _ -> ()
-            cache
-
-        match cache.TryGetValue idx with
-        | true, v ->
-            count <- count + 1
-            v
-        | _ ->
-            let res = f idx
-            cache[idx] <- res
-            res
+        fun f (idx: 'T) ->
+            let cache =
+                match cache with
+                | null -> cache <- ConcurrentDictionary<_, _>(Environment.ProcessorCount, 11 (* sz: int *) )
+                | _ -> ()
+
+                cache
+
+            match cache.TryGetValue idx with
+            | true, v ->
+                count <- count + 1
+                v
+            | _ ->
+                let res = f idx
+                cache[idx] <- res
+                res
 
 //-----------------------------------------------------------------------
 // Polymorphic general helpers for searching for particular rows.
@@ -806,9 +996,13 @@ let mkCacheGeneric lowMem _inbase _nm _sz =
 
 let seekFindRow numRows rowChooser =
     let mutable i = 1
+
     while (i <= numRows && not (rowChooser i)) do
         i <- i + 1
-    if i > numRows then dprintn "warning: seekFindRow: row not found"
+
+    if i > numRows then
+        dprintn "warning: seekFindRow: row not found"
+
     i
 
 // search for rows satisfying predicate
@@ -816,77 +1010,94 @@ let seekReadIndexedRows (numRows, rowReader, keyFunc, keyComparer, binaryChop, r
     if binaryChop then
         let mutable low = 0
         let mutable high = numRows + 1
-        begin
-          let mutable fin = false
-          while not fin do
-              if high - low <= 1 then
-                  fin <- true
-              else
-                  let mid = (low + high) / 2
-                  let midrow = rowReader mid
-                  let c = keyComparer (keyFunc midrow)
-                  if c > 0 then
-                      low <- mid
-                  elif c < 0 then
-                      high <- mid
-                  else
-                      fin <- true
-        end
+
+        (let mutable fin = false
+
+         while not fin do
+             if high - low <= 1 then
+                 fin <- true
+             else
+                 let mid = (low + high) / 2
+                 let midrow = rowReader mid
+                 let c = keyComparer (keyFunc midrow)
+
+                 if c > 0 then low <- mid
+                 elif c < 0 then high <- mid
+                 else fin <- true)
+
         let mutable res = []
+
         if high - low > 1 then
             // now read off rows, forward and backwards
             let mid = (low + high) / 2
             // read forward
             let mutable fin = false
             let mutable curr = mid
+
             while not fin do
                 if curr > numRows then
                     fin <- true
                 else
                     let currrow = rowReader curr
+
                     if keyComparer (keyFunc currrow) = 0 then
                         res <- rowConverter currrow :: res
                     else
                         fin <- true
+
                     curr <- curr + 1
 
             res <- List.rev res
             // read backwards
             let mutable fin = false
             let mutable curr = mid - 1
+
             while not fin do
                 if curr = 0 then
                     fin <- true
                 else
                     let currrow = rowReader curr
+
                     if keyComparer (keyFunc currrow) = 0 then
                         res <- rowConverter currrow :: res
                     else
                         fin <- true
+
                     curr <- curr - 1
         // sanity check
 #if CHECKING
         if checking then
             let res2 =
-                [ for i = 1 to numRows do
-                    let rowinfo = rowReader i
-                    if keyComparer (keyFunc rowinfo) = 0 then
-                      yield rowConverter rowinfo ]
+                [
+                    for i = 1 to numRows do
+                        let rowinfo = rowReader i
+
+                        if keyComparer (keyFunc rowinfo) = 0 then
+                            yield rowConverter rowinfo
+                ]
+
             if (res2 <> res) then
-                failwith ("results of binary search did not match results of linear search: linear search produced "+string res2.Length+", binary search produced "+string res.Length)
+                failwith (
+                    "results of binary search did not match results of linear search: linear search produced "
+                    + string res2.Length
+                    + ", binary search produced "
+                    + string res.Length
+                )
 #endif
 
         res
     else
-        [ for i = 1 to numRows do
-            let rowinfo = rowReader i
-            if keyComparer (keyFunc rowinfo) = 0 then
-                yield rowConverter rowinfo ]
+        [
+            for i = 1 to numRows do
+                let rowinfo = rowReader i
 
+                if keyComparer (keyFunc rowinfo) = 0 then
+                    yield rowConverter rowinfo
+        ]
 
 let seekReadOptionalIndexedRow info =
     match seekReadIndexedRows info with
-    | [k] -> Some k
+    | [ k ] -> Some k
     | [] -> None
     | h :: _ ->
         dprintn "multiple rows found when indexing table"
@@ -903,105 +1114,109 @@ let seekReadIndexedRow info =
 
 type MethodData = MethodData of enclTy: ILType * ILCallingConv * name: string * argTys: ILTypes * retTy: ILType * methInst: ILTypes
 
-type VarArgMethodData = VarArgMethodData of enclTy: ILType * ILCallingConv * name: string * argTys: ILTypes * ILVarArgs * retTy: ILType * methInst: ILTypes
+type VarArgMethodData =
+    | VarArgMethodData of enclTy: ILType * ILCallingConv * name: string * argTys: ILTypes * ILVarArgs * retTy: ILType * methInst: ILTypes
 
 []
 type PEReader =
-  { fileName: string
+    {
+        fileName: string
 #if FX_NO_PDB_READER
-    pdb: obj option
+        pdb: obj option
 #else
-    pdb: (PdbReader * (string -> ILSourceDocument)) option
+        pdb: (PdbReader * (string -> ILSourceDocument)) option
 #endif
-    entryPointToken: TableName * int
-    pefile: BinaryFile
-    textSegmentPhysicalLoc: int32
-    textSegmentPhysicalSize: int32
-    dataSegmentPhysicalLoc: int32
-    dataSegmentPhysicalSize: int32
-    anyV2P: string * int32 -> int32
-    metadataAddr: int32
-    sectionHeaders: (int32 * int32 * int32) list
-    nativeResourcesAddr: int32
-    nativeResourcesSize: int32
-    resourcesAddr: int32
-    strongnameAddr: int32
-    vtableFixupsAddr: int32
-    noFileOnDisk: bool
-}
+        entryPointToken: TableName * int
+        pefile: BinaryFile
+        textSegmentPhysicalLoc: int32
+        textSegmentPhysicalSize: int32
+        dataSegmentPhysicalLoc: int32
+        dataSegmentPhysicalSize: int32
+        anyV2P: string * int32 -> int32
+        metadataAddr: int32
+        sectionHeaders: (int32 * int32 * int32) list
+        nativeResourcesAddr: int32
+        nativeResourcesSize: int32
+        resourcesAddr: int32
+        strongnameAddr: int32
+        vtableFixupsAddr: int32
+        noFileOnDisk: bool
+    }
 
 []
 type ILMetadataReader =
-  { sorted: int64
-    mdfile: BinaryFile
-    pectxtCaptured: PEReader option // only set when reading full PE including code etc. for static linking
-    entryPointToken: TableName * int
-    dataEndPoints: Lazy
-    fileName: string
-    getNumRows: TableName -> int
-    userStringsStreamPhysicalLoc: int32
-    stringsStreamPhysicalLoc: int32
-    blobsStreamPhysicalLoc: int32
-    blobsStreamSize: int32
-    readUserStringHeap: int32 -> string
-    memoizeString: string -> string
-    readStringHeap: int32 -> string
-    readBlobHeap: int32 -> byte[]
-    guidsStreamPhysicalLoc: int32
-    rowAddr: TableName -> int -> int32
-    tableBigness: bool []
-    rsBigness: bool
-    tdorBigness: bool
-    tomdBigness: bool
-    hcBigness: bool
-    hcaBigness: bool
-    hfmBigness: bool
-    hdsBigness: bool
-    mrpBigness: bool
-    hsBigness: bool
-    mdorBigness: bool
-    mfBigness: bool
-    iBigness: bool
-    catBigness: bool
-    stringsBigness: bool
-    guidsBigness: bool
-    blobsBigness: bool
-    seekReadNestedRow: int -> int * int
-    seekReadConstantRow: int -> uint16 * TaggedIndex * int32
-    seekReadMethodSemanticsRow: int -> int32 * int * TaggedIndex
-    seekReadTypeDefRow: int -> int32 * int32 * int32 * TaggedIndex * int * int
-    seekReadAssemblyRef: int -> ILAssemblyRef
-    seekReadMethodSpecAsMethodData: MethodSpecAsMspecIdx -> VarArgMethodData
-    seekReadMemberRefAsMethodData: MemberRefAsMspecIdx -> VarArgMethodData
-    seekReadMemberRefAsFieldSpec: MemberRefAsFspecIdx -> ILFieldSpec
-    seekReadCustomAttr: CustomAttrIdx -> ILAttribute
-    seekReadTypeRef: int ->ILTypeRef
-    seekReadTypeRefAsType: TypeRefAsTypIdx -> ILType
-    readBlobHeapAsPropertySig: BlobAsPropSigIdx -> ILThisConvention * ILType * ILTypes
-    readBlobHeapAsFieldSig: BlobAsFieldSigIdx -> ILType
-    readBlobHeapAsMethodSig: BlobAsMethodSigIdx -> bool * int32 * ILCallingConv * ILType * ILTypes * ILVarArgs
-    readBlobHeapAsLocalsSig: BlobAsLocalSigIdx -> ILLocal list
-    seekReadTypeDefAsType: TypeDefAsTypIdx -> ILType
-    seekReadMethodDefAsMethodData: int -> MethodData
-    seekReadGenericParams: GenericParamsIdx -> ILGenericParameterDef list
-    seekReadFieldDefAsFieldSpec: int -> ILFieldSpec
-    customAttrsReader_Module: ILAttributesStored
-    customAttrsReader_Assembly: ILAttributesStored
-    customAttrsReader_TypeDef: ILAttributesStored
-    customAttrsReader_GenericParam: ILAttributesStored
-    customAttrsReader_FieldDef: ILAttributesStored
-    customAttrsReader_MethodDef: ILAttributesStored
-    customAttrsReader_ParamDef: ILAttributesStored
-    customAttrsReader_Event: ILAttributesStored
-    customAttrsReader_Property: ILAttributesStored
-    customAttrsReader_ManifestResource: ILAttributesStored
-    customAttrsReader_ExportedType: ILAttributesStored
-    securityDeclsReader_TypeDef: ILSecurityDeclsStored
-    securityDeclsReader_MethodDef: ILSecurityDeclsStored
-    securityDeclsReader_Assembly: ILSecurityDeclsStored
-    typeDefReader: ILTypeDefStored }
-
-type ISeekReadIndexedRowReader<'RowT, 'KeyT, 'T when 'RowT : struct> =
+    {
+        sorted: int64
+        mdfile: BinaryFile
+        pectxtCaptured: PEReader option // only set when reading full PE including code etc. for static linking
+        entryPointToken: TableName * int
+        dataEndPoints: Lazy
+        fileName: string
+        getNumRows: TableName -> int
+        userStringsStreamPhysicalLoc: int32
+        stringsStreamPhysicalLoc: int32
+        blobsStreamPhysicalLoc: int32
+        blobsStreamSize: int32
+        readUserStringHeap: int32 -> string
+        memoizeString: string -> string
+        readStringHeap: int32 -> string
+        readBlobHeap: int32 -> byte[]
+        guidsStreamPhysicalLoc: int32
+        rowAddr: TableName -> int -> int32
+        tableBigness: bool[]
+        rsBigness: bool
+        tdorBigness: bool
+        tomdBigness: bool
+        hcBigness: bool
+        hcaBigness: bool
+        hfmBigness: bool
+        hdsBigness: bool
+        mrpBigness: bool
+        hsBigness: bool
+        mdorBigness: bool
+        mfBigness: bool
+        iBigness: bool
+        catBigness: bool
+        stringsBigness: bool
+        guidsBigness: bool
+        blobsBigness: bool
+        seekReadNestedRow: int -> int * int
+        seekReadConstantRow: int -> uint16 * TaggedIndex * int32
+        seekReadMethodSemanticsRow: int -> int32 * int * TaggedIndex
+        seekReadTypeDefRow: int -> int32 * int32 * int32 * TaggedIndex * int * int
+        seekReadAssemblyRef: int -> ILAssemblyRef
+        seekReadMethodSpecAsMethodData: MethodSpecAsMspecIdx -> VarArgMethodData
+        seekReadMemberRefAsMethodData: MemberRefAsMspecIdx -> VarArgMethodData
+        seekReadMemberRefAsFieldSpec: MemberRefAsFspecIdx -> ILFieldSpec
+        seekReadCustomAttr: CustomAttrIdx -> ILAttribute
+        seekReadTypeRef: int -> ILTypeRef
+        seekReadTypeRefAsType: TypeRefAsTypIdx -> ILType
+        readBlobHeapAsPropertySig: BlobAsPropSigIdx -> ILThisConvention * ILType * ILTypes
+        readBlobHeapAsFieldSig: BlobAsFieldSigIdx -> ILType
+        readBlobHeapAsMethodSig: BlobAsMethodSigIdx -> bool * int32 * ILCallingConv * ILType * ILTypes * ILVarArgs
+        readBlobHeapAsLocalsSig: BlobAsLocalSigIdx -> ILLocal list
+        seekReadTypeDefAsType: TypeDefAsTypIdx -> ILType
+        seekReadMethodDefAsMethodData: int -> MethodData
+        seekReadGenericParams: GenericParamsIdx -> ILGenericParameterDef list
+        seekReadFieldDefAsFieldSpec: int -> ILFieldSpec
+        customAttrsReader_Module: ILAttributesStored
+        customAttrsReader_Assembly: ILAttributesStored
+        customAttrsReader_TypeDef: ILAttributesStored
+        customAttrsReader_GenericParam: ILAttributesStored
+        customAttrsReader_FieldDef: ILAttributesStored
+        customAttrsReader_MethodDef: ILAttributesStored
+        customAttrsReader_ParamDef: ILAttributesStored
+        customAttrsReader_Event: ILAttributesStored
+        customAttrsReader_Property: ILAttributesStored
+        customAttrsReader_ManifestResource: ILAttributesStored
+        customAttrsReader_ExportedType: ILAttributesStored
+        securityDeclsReader_TypeDef: ILSecurityDeclsStored
+        securityDeclsReader_MethodDef: ILSecurityDeclsStored
+        securityDeclsReader_Assembly: ILSecurityDeclsStored
+        typeDefReader: ILTypeDefStored
+    }
+
+type ISeekReadIndexedRowReader<'RowT, 'KeyT, 'T when 'RowT: struct> =
     abstract GetRow: int * byref<'RowT> -> unit
     abstract GetKey: byref<'RowT> -> 'KeyT
     abstract CompareKey: 'KeyT -> int
@@ -1009,26 +1224,27 @@ type ISeekReadIndexedRowReader<'RowT, 'KeyT, 'T when 'RowT : struct> =
 
 let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) =
     let mutable row = Unchecked.defaultof<'RowT>
+
     if binaryChop then
         let mutable low = 0
         let mutable high = numRows + 1
 
         let mutable fin = false
+
         while not fin do
-            if high - low <= 1  then
+            if high - low <= 1 then
                 fin <- true
             else
                 let mid = (low + high) / 2
                 reader.GetRow(mid, &row)
                 let c = reader.CompareKey(reader.GetKey(&row))
-                if c > 0 then
-                    low <- mid
-                elif c < 0 then
-                    high <- mid
-                else
-                    fin <- true
+
+                if c > 0 then low <- mid
+                elif c < 0 then high <- mid
+                else fin <- true
 
         let res = ImmutableArray.CreateBuilder()
+
         if high - low > 1 then
             // now read off rows, forward and backwards
             let mid = (low + high) / 2
@@ -1036,15 +1252,18 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR
             // read backwards
             let mutable fin = false
             let mutable curr = mid - 1
+
             while not fin do
                 if curr = 0 then
                     fin <- true
                 else
                     reader.GetRow(curr, &row)
+
                     if reader.CompareKey(reader.GetKey(&row)) = 0 then
                         res.Add(reader.ConvertRow(&row))
                     else
                         fin <- true
+
                 curr <- curr - 1
 
             res.Reverse()
@@ -1052,24 +1271,30 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR
             // read forward
             let mutable fin = false
             let mutable curr = mid
+
             while not fin do
                 if curr > numRows then
                     fin <- true
                 else
                     reader.GetRow(curr, &row)
+
                     if reader.CompareKey(reader.GetKey(&row)) = 0 then
                         res.Add(reader.ConvertRow(&row))
                     else
                         fin <- true
+
                     curr <- curr + 1
 
         res.ToArray()
     else
         let res = ImmutableArray.CreateBuilder()
+
         for i = 1 to numRows do
             reader.GetRow(i, &row)
+
             if reader.CompareKey(reader.GetKey(&row)) = 0 then
-              res.Add(reader.ConvertRow(&row))
+                res.Add(reader.ConvertRow(&row))
+
         res.ToArray()
 
 []
@@ -1085,43 +1310,81 @@ let seekReadUInt16Adv mdv (addr: byref) =
 
 let seekReadInt32Adv mdv (addr: byref) =
     let res = seekReadInt32 mdv addr
-    addr <- addr+4
+    addr <- addr + 4
     res
 
 let seekReadUInt16AsInt32Adv mdv (addr: byref) =
     let res = seekReadUInt16AsInt32 mdv addr
-    addr <- addr+2
+    addr <- addr + 2
     res
 
 let inline seekReadTaggedIdx f nbits big mdv (addr: byref) =
-    let tok = if big then seekReadInt32Adv mdv &addr else seekReadUInt16AsInt32Adv mdv &addr
+    let tok =
+        if big then
+            seekReadInt32Adv mdv &addr
+        else
+            seekReadUInt16AsInt32Adv mdv &addr
+
     tokToTaggedIdx f nbits tok
 
 let seekReadIdx big mdv (addr: byref) =
-    if big then seekReadInt32Adv mdv &addr else seekReadUInt16AsInt32Adv mdv &addr
+    if big then
+        seekReadInt32Adv mdv &addr
+    else
+        seekReadUInt16AsInt32Adv mdv &addr
 
 let seekReadUntaggedIdx (tab: TableName) (ctxt: ILMetadataReader) mdv (addr: byref) =
     seekReadIdx ctxt.tableBigness[tab.Index] mdv &addr
 
-let seekReadResolutionScopeIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkResolutionScopeTag 2 ctxt.rsBigness mdv &addr
-let seekReadTypeDefOrRefOrSpecIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkTypeDefOrRefOrSpecTag 2 ctxt.tdorBigness mdv &addr
-let seekReadTypeOrMethodDefIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkTypeOrMethodDefTag 1 ctxt.tomdBigness mdv &addr
-let seekReadHasConstantIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkHasConstantTag 2 ctxt.hcBigness mdv &addr
-let seekReadHasCustomAttributeIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkHasCustomAttributeTag 5 ctxt.hcaBigness mdv &addr
-let seekReadHasFieldMarshalIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkHasFieldMarshalTag 1 ctxt.hfmBigness mdv &addr
-let seekReadHasDeclSecurityIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkHasDeclSecurityTag 2 ctxt.hdsBigness mdv &addr
-let seekReadMemberRefParentIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkMemberRefParentTag 3 ctxt.mrpBigness mdv &addr
-let seekReadHasSemanticsIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkHasSemanticsTag 1 ctxt.hsBigness mdv &addr
-let seekReadMethodDefOrRefIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkMethodDefOrRefTag 1 ctxt.mdorBigness mdv &addr
-let seekReadMemberForwardedIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkMemberForwardedTag 1 ctxt.mfBigness mdv &addr
-let seekReadImplementationIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkImplementationTag 2 ctxt.iBigness mdv &addr
-let seekReadCustomAttributeTypeIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadTaggedIdx mkILCustomAttributeTypeTag 3 ctxt.catBigness mdv &addr
-let seekReadStringIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadIdx ctxt.stringsBigness mdv &addr
+let seekReadResolutionScopeIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkResolutionScopeTag 2 ctxt.rsBigness mdv &addr
+
+let seekReadTypeDefOrRefOrSpecIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkTypeDefOrRefOrSpecTag 2 ctxt.tdorBigness mdv &addr
+
+let seekReadTypeOrMethodDefIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkTypeOrMethodDefTag 1 ctxt.tomdBigness mdv &addr
+
+let seekReadHasConstantIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkHasConstantTag 2 ctxt.hcBigness mdv &addr
+
+let seekReadHasCustomAttributeIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkHasCustomAttributeTag 5 ctxt.hcaBigness mdv &addr
+
+let seekReadHasFieldMarshalIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkHasFieldMarshalTag 1 ctxt.hfmBigness mdv &addr
+
+let seekReadHasDeclSecurityIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkHasDeclSecurityTag 2 ctxt.hdsBigness mdv &addr
+
+let seekReadMemberRefParentIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkMemberRefParentTag 3 ctxt.mrpBigness mdv &addr
+
+let seekReadHasSemanticsIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkHasSemanticsTag 1 ctxt.hsBigness mdv &addr
+
+let seekReadMethodDefOrRefIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkMethodDefOrRefTag 1 ctxt.mdorBigness mdv &addr
+
+let seekReadMemberForwardedIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkMemberForwardedTag 1 ctxt.mfBigness mdv &addr
+
+let seekReadImplementationIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkImplementationTag 2 ctxt.iBigness mdv &addr
+
+let seekReadCustomAttributeTypeIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadTaggedIdx mkILCustomAttributeTypeTag 3 ctxt.catBigness mdv &addr
+
+let seekReadStringIdx (ctxt: ILMetadataReader) mdv (addr: byref) =
+    seekReadIdx ctxt.stringsBigness mdv &addr
+
 let seekReadGuidIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadIdx ctxt.guidsBigness mdv &addr
 let seekReadBlobIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadIdx ctxt.blobsBigness mdv &addr
 
 let seekReadModuleRow (ctxt: ILMetadataReader) mdv idx =
-    if idx = 0 then failwith "cannot read Module table row 0"
+    if idx = 0 then
+        failwith "cannot read Module table row 0"
+
     let mutable addr = ctxt.rowAddr TableNames.Module idx
     let generation = seekReadUInt16Adv mdv &addr
     let nameIdx = seekReadStringIdx ctxt mdv &addr
@@ -1140,6 +1403,7 @@ let seekReadTypeRefRow (ctxt: ILMetadataReader) mdv idx =
 
 /// Read Table ILTypeDef.
 let seekReadTypeDefRow (ctxt: ILMetadataReader) idx = ctxt.seekReadTypeDefRow idx
+
 let seekReadTypeDefRowUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
@@ -1196,6 +1460,7 @@ let seekReadMemberRefRow (ctxt: ILMetadataReader) mdv idx =
 
 /// Read Table Constant.
 let seekReadConstantRow (ctxt: ILMetadataReader) idx = ctxt.seekReadConstantRow idx
+
 let seekReadConstantRowUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
@@ -1280,6 +1545,7 @@ let seekReadPropertyRow (ctxt: ILMetadataReader) mdv idx =
 
 /// Read Table MethodSemantics.
 let seekReadMethodSemanticsRow (ctxt: ILMetadataReader) idx = ctxt.seekReadMethodSemanticsRow idx
+
 let seekReadMethodSemanticsRowUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
@@ -1382,6 +1648,7 @@ let seekReadManifestResourceRow (ctxt: ILMetadataReader) mdv idx =
 
 /// Read Table Nested.
 let seekReadNestedRow (ctxt: ILMetadataReader) idx = ctxt.seekReadNestedRow idx
+
 let seekReadNestedRowUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
@@ -1413,7 +1680,6 @@ let seekReadMethodSpecRow (ctxt: ILMetadataReader) mdv idx =
     let instIdx = seekReadBlobIdx ctxt mdv &addr
     (mdorIdx, instIdx)
 
-
 let readUserStringHeapUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
@@ -1428,34 +1694,65 @@ let readStringHeapUncached ctxtH idx =
 
 let readStringHeap (ctxt: ILMetadataReader) idx = ctxt.readStringHeap idx
 
-let readStringHeapOption (ctxt: ILMetadataReader) idx = if idx = 0 then None else Some (readStringHeap ctxt idx)
+let readStringHeapOption (ctxt: ILMetadataReader) idx =
+    if idx = 0 then
+        None
+    else
+        Some(readStringHeap ctxt idx)
 
 let readBlobHeapUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
     // valid index lies in range [1..streamSize)
     // NOTE: idx cannot be 0 - Blob\String heap has first empty element that mdv one byte 0
-    if idx <= 0 || idx >= ctxt.blobsStreamSize then [| |]
-    else seekReadBlob mdv (ctxt.blobsStreamPhysicalLoc + idx)
+    if idx <= 0 || idx >= ctxt.blobsStreamSize then
+        [||]
+    else
+        seekReadBlob mdv (ctxt.blobsStreamPhysicalLoc + idx)
 
 let readBlobHeap (ctxt: ILMetadataReader) idx = ctxt.readBlobHeap idx
 
-let readBlobHeapOption ctxt idx = if idx = 0 then None else Some (readBlobHeap ctxt idx)
+let readBlobHeapOption ctxt idx =
+    if idx = 0 then
+        None
+    else
+        Some(readBlobHeap ctxt idx)
 
 //let readGuidHeap ctxt idx = seekReadGuid ctxt.mdv (ctxt.guidsStreamPhysicalLoc + idx)
 
 // read a single value out of a blob heap using the given function
-let readBlobHeapAsBool ctxt vidx = fst (sigptrGetBool (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsSByte ctxt vidx = fst (sigptrGetSByte (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsInt16 ctxt vidx = fst (sigptrGetInt16 (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsInt32 ctxt vidx = fst (sigptrGetInt32 (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsInt64 ctxt vidx = fst (sigptrGetInt64 (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsByte ctxt vidx = fst (sigptrGetByte (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsUInt16 ctxt vidx = fst (sigptrGetUInt16 (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsUInt32 ctxt vidx = fst (sigptrGetUInt32 (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsUInt64 ctxt vidx = fst (sigptrGetUInt64 (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsSingle ctxt vidx = fst (sigptrGetSingle (readBlobHeap ctxt vidx) 0)
-let readBlobHeapAsDouble ctxt vidx = fst (sigptrGetDouble (readBlobHeap ctxt vidx) 0)
+let readBlobHeapAsBool ctxt vidx =
+    fst (sigptrGetBool (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsSByte ctxt vidx =
+    fst (sigptrGetSByte (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsInt16 ctxt vidx =
+    fst (sigptrGetInt16 (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsInt32 ctxt vidx =
+    fst (sigptrGetInt32 (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsInt64 ctxt vidx =
+    fst (sigptrGetInt64 (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsByte ctxt vidx =
+    fst (sigptrGetByte (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsUInt16 ctxt vidx =
+    fst (sigptrGetUInt16 (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsUInt32 ctxt vidx =
+    fst (sigptrGetUInt32 (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsUInt64 ctxt vidx =
+    fst (sigptrGetUInt64 (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsSingle ctxt vidx =
+    fst (sigptrGetSingle (readBlobHeap ctxt vidx) 0)
+
+let readBlobHeapAsDouble ctxt vidx =
+    fst (sigptrGetDouble (readBlobHeap ctxt vidx) 0)
 
 //-----------------------------------------------------------------------
 // Some binaries have raw data embedded their text sections, e.g. mscorlib, for
@@ -1478,198 +1775,301 @@ let readBlobHeapAsDouble ctxt vidx = fst (sigptrGetDouble (readBlobHeap ctxt vid
 // For example the assembly came from a type provider
 // In this case we eagerly read the native resources into memory
 let readNativeResources (pectxt: PEReader) =
-    [ if pectxt.nativeResourcesSize <> 0x0 && pectxt.nativeResourcesAddr <> 0x0 then
-        let start = pectxt.anyV2P (pectxt.fileName + ": native resources", pectxt.nativeResourcesAddr)
-        if pectxt.noFileOnDisk then
-            let unlinkedResource =
-                let linkedResource = seekReadBytes (pectxt.pefile.GetView()) start pectxt.nativeResourcesSize
-                unlinkResource pectxt.nativeResourcesAddr linkedResource
-            yield ILNativeResource.Out unlinkedResource
-        else
-            yield ILNativeResource.In (pectxt.fileName, pectxt.nativeResourcesAddr, start, pectxt.nativeResourcesSize ) ]
+    [
+        if pectxt.nativeResourcesSize <> 0x0 && pectxt.nativeResourcesAddr <> 0x0 then
+            let start =
+                pectxt.anyV2P (pectxt.fileName + ": native resources", pectxt.nativeResourcesAddr)
+
+            if pectxt.noFileOnDisk then
+                let unlinkedResource =
+                    let linkedResource =
+                        seekReadBytes (pectxt.pefile.GetView()) start pectxt.nativeResourcesSize
 
+                    unlinkResource pectxt.nativeResourcesAddr linkedResource
+
+                yield ILNativeResource.Out unlinkedResource
+            else
+                yield ILNativeResource.In(pectxt.fileName, pectxt.nativeResourcesAddr, start, pectxt.nativeResourcesSize)
+    ]
 
 let getDataEndPointsDelayed (pectxt: PEReader) ctxtH =
     lazy
         let (ctxt: ILMetadataReader) = getHole ctxtH
         let mdv = ctxt.mdfile.GetView()
+
         let dataStartPoints =
-            [ for i = 1 to ctxt.getNumRows TableNames.FieldRVA do
-                let rva, _fidx = seekReadFieldRVARow ctxt mdv i
-                ("field", rva)
-              for i = 1 to ctxt.getNumRows TableNames.ManifestResource do
-                let offset, _, _, TaggedIndex(_tag, idx) = seekReadManifestResourceRow ctxt mdv i
-                if idx = 0 then
-                  let rva = pectxt.resourcesAddr + offset
-                  ("manifest resource", rva) ]
-
-        if isNil dataStartPoints then []
+            [
+                for i = 1 to ctxt.getNumRows TableNames.FieldRVA do
+                    let rva, _fidx = seekReadFieldRVARow ctxt mdv i
+                    ("field", rva)
+                for i = 1 to ctxt.getNumRows TableNames.ManifestResource do
+                    let offset, _, _, TaggedIndex (_tag, idx) = seekReadManifestResourceRow ctxt mdv i
+
+                    if idx = 0 then
+                        let rva = pectxt.resourcesAddr + offset
+                        ("manifest resource", rva)
+            ]
+
+        if isNil dataStartPoints then
+            []
         else
-          let methodRVAs =
-              [ for i = 1 to ctxt.getNumRows TableNames.Method do
-                  let rva, _, _, nameIdx, _, _ = seekReadMethodRow ctxt mdv i
-                  if rva <> 0 then
-                     let nm = readStringHeap ctxt nameIdx
-                     (nm, rva) ]
-          ([ pectxt.textSegmentPhysicalLoc + pectxt.textSegmentPhysicalSize
-             pectxt.dataSegmentPhysicalLoc + pectxt.dataSegmentPhysicalSize ]
-           @
-           (List.map pectxt.anyV2P
-              (dataStartPoints
-                @ [for virtAddr, _virtSize, _physLoc in pectxt.sectionHeaders do yield ("section start", virtAddr) done]
-                @ [("md", pectxt.metadataAddr)]
-                @ (if pectxt.nativeResourcesAddr = 0x0 then [] else [("native resources", pectxt.nativeResourcesAddr) ])
-                @ (if pectxt.resourcesAddr = 0x0 then [] else [("managed resources", pectxt.resourcesAddr) ])
-                @ (if pectxt.strongnameAddr = 0x0 then [] else [("managed strongname", pectxt.strongnameAddr) ])
-                @ (if pectxt.vtableFixupsAddr = 0x0 then [] else [("managed vtable_fixups", pectxt.vtableFixupsAddr) ])
-                @ methodRVAs)))
-           |> List.distinct
-           |> List.sort
-
+            let methodRVAs =
+                [
+                    for i = 1 to ctxt.getNumRows TableNames.Method do
+                        let rva, _, _, nameIdx, _, _ = seekReadMethodRow ctxt mdv i
+
+                        if rva <> 0 then
+                            let nm = readStringHeap ctxt nameIdx
+                            (nm, rva)
+                ]
+
+            ([
+                pectxt.textSegmentPhysicalLoc + pectxt.textSegmentPhysicalSize
+                pectxt.dataSegmentPhysicalLoc + pectxt.dataSegmentPhysicalSize
+             ]
+             @ (List.map
+                 pectxt.anyV2P
+                 (dataStartPoints
+                  @ [
+                      for virtAddr, _virtSize, _physLoc in pectxt.sectionHeaders do
+                          yield ("section start", virtAddr)
+                    ]
+                    @ [ ("md", pectxt.metadataAddr) ]
+                      @ (if pectxt.nativeResourcesAddr = 0x0 then
+                             []
+                         else
+                             [ ("native resources", pectxt.nativeResourcesAddr) ])
+                        @ (if pectxt.resourcesAddr = 0x0 then
+                               []
+                           else
+                               [ ("managed resources", pectxt.resourcesAddr) ])
+                          @ (if pectxt.strongnameAddr = 0x0 then
+                                 []
+                             else
+                                 [ ("managed strongname", pectxt.strongnameAddr) ])
+                            @ (if pectxt.vtableFixupsAddr = 0x0 then
+                                   []
+                               else
+                                   [ ("managed vtable_fixups", pectxt.vtableFixupsAddr) ])
+                              @ methodRVAs)))
+            |> List.distinct
+            |> List.sort
 
 let rvaToData (ctxt: ILMetadataReader) (pectxt: PEReader) nm rva =
     if rva = 0x0 then failwith "rva is zero"
     let start = pectxt.anyV2P (nm, rva)
     let endPoints = (Lazy.force ctxt.dataEndPoints)
+
     let rec look l =
         match l with
-        | [] ->
-            failwithf "find_text_data_extent: none found for fileName=%s, name=%s, rva=0x%08x, start=0x%08x" ctxt.fileName nm rva start
+        | [] -> failwithf "find_text_data_extent: none found for fileName=%s, name=%s, rva=0x%08x, start=0x%08x" ctxt.fileName nm rva start
         | e :: t ->
-           if start < e then
-             let pev = pectxt.pefile.GetView()
-             seekReadBytes pev start (e - start)
-           else look t
-    look endPoints
+            if start < e then
+                let pev = pectxt.pefile.GetView()
+                seekReadBytes pev start (e - start)
+            else
+                look t
 
+    look endPoints
 
 //-----------------------------------------------------------------------
 // Read the AbsIL structure (lazily) by reading off the relevant rows.
 // ----------------------------------------------------------------------
 
-let isSorted (ctxt: ILMetadataReader) (tab: TableName) = ((ctxt.sorted &&& (int64 1 <<< tab.Index)) <> int64 0x0)
+let isSorted (ctxt: ILMetadataReader) (tab: TableName) =
+    ((ctxt.sorted &&& (int64 1 <<< tab.Index)) <> int64 0x0)
 
 // Note, pectxtEager and pevEager must not be captured by the results of this function
 let rec seekReadModule (ctxt: ILMetadataReader) canReduceMemory (pectxtEager: PEReader) pevEager peinfo ilMetadataVersion idx =
-    let subsys, subsysversion, useHighEntropyVA, ilOnly, only32, is32bitpreferred, only64, platform, isDll, alignVirt, alignPhys, imageBaseReal = peinfo
+    let (subsys,
+         subsysversion,
+         useHighEntropyVA,
+         ilOnly,
+         only32,
+         is32bitpreferred,
+         only64,
+         platform,
+         isDll,
+         alignVirt,
+         alignPhys,
+         imageBaseReal) =
+        peinfo
+
     let mdv = ctxt.mdfile.GetView()
-    let _generation, nameIdx, _mvidIdx, _encidIdx, _encbaseidIdx = seekReadModuleRow ctxt mdv idx
+
+    let _generation, nameIdx, _mvidIdx, _encidIdx, _encbaseidIdx =
+        seekReadModuleRow ctxt mdv idx
+
     let ilModuleName = readStringHeap ctxt nameIdx
     let nativeResources = readNativeResources pectxtEager
 
-    { Manifest =
-         if ctxt.getNumRows TableNames.Assembly > 0 then Some (seekReadAssemblyManifest ctxt pectxtEager 1)
-         else None
-      CustomAttrsStored = ctxt.customAttrsReader_Module
-      MetadataIndex = idx
-      Name = ilModuleName
-      NativeResources=nativeResources
-      TypeDefs = mkILTypeDefsComputed (fun () -> seekReadTopTypeDefs ctxt)
-      SubSystemFlags = int32 subsys
-      IsILOnly = ilOnly
-      SubsystemVersion = subsysversion
-      UseHighEntropyVA = useHighEntropyVA
-      Platform = platform
-      StackReserveSize = None  // TODO
-      Is32Bit = only32
-      Is32BitPreferred = is32bitpreferred
-      Is64Bit = only64
-      IsDLL=isDll
-      VirtualAlignment = alignVirt
-      PhysicalAlignment = alignPhys
-      ImageBase = imageBaseReal
-      MetadataVersion = ilMetadataVersion
-      Resources = seekReadManifestResources ctxt canReduceMemory mdv pectxtEager pevEager }
+    {
+        Manifest =
+            if ctxt.getNumRows TableNames.Assembly > 0 then
+                Some(seekReadAssemblyManifest ctxt pectxtEager 1)
+            else
+                None
+        CustomAttrsStored = ctxt.customAttrsReader_Module
+        MetadataIndex = idx
+        Name = ilModuleName
+        NativeResources = nativeResources
+        TypeDefs = mkILTypeDefsComputed (fun () -> seekReadTopTypeDefs ctxt)
+        SubSystemFlags = int32 subsys
+        IsILOnly = ilOnly
+        SubsystemVersion = subsysversion
+        UseHighEntropyVA = useHighEntropyVA
+        Platform = platform
+        StackReserveSize = None // TODO
+        Is32Bit = only32
+        Is32BitPreferred = is32bitpreferred
+        Is64Bit = only64
+        IsDLL = isDll
+        VirtualAlignment = alignVirt
+        PhysicalAlignment = alignPhys
+        ImageBase = imageBaseReal
+        MetadataVersion = ilMetadataVersion
+        Resources = seekReadManifestResources ctxt canReduceMemory mdv pectxtEager pevEager
+    }
 
 and seekReadAssemblyManifest (ctxt: ILMetadataReader) pectxt idx =
     let mdview = ctxt.mdfile.GetView()
-    let hash, v1, v2, v3, v4, flags, publicKeyIdx, nameIdx, localeIdx = seekReadAssemblyRow ctxt mdview idx
+
+    let hash, v1, v2, v3, v4, flags, publicKeyIdx, nameIdx, localeIdx =
+        seekReadAssemblyRow ctxt mdview idx
+
     let name = readStringHeap ctxt nameIdx
     let pubkey = readBlobHeapOption ctxt publicKeyIdx
-    { Name= name
-      AuxModuleHashAlgorithm=hash
-      SecurityDeclsStored= ctxt.securityDeclsReader_Assembly
-      PublicKey= pubkey
-      Version= Some (ILVersionInfo (v1, v2, v3, v4))
-      Locale= readStringHeapOption ctxt localeIdx
-      CustomAttrsStored = ctxt.customAttrsReader_Assembly
-      MetadataIndex = idx
-      AssemblyLongevity =
-        let masked = flags &&& 0x000e
-        if masked = 0x0000 then ILAssemblyLongevity.Unspecified
-        elif masked = 0x0002 then ILAssemblyLongevity.Library
-        elif masked = 0x0004 then ILAssemblyLongevity.PlatformAppDomain
-        elif masked = 0x0006 then ILAssemblyLongevity.PlatformProcess
-        elif masked = 0x0008 then ILAssemblyLongevity.PlatformSystem
-        else ILAssemblyLongevity.Unspecified
-      ExportedTypes= seekReadTopExportedTypes ctxt
-      EntrypointElsewhere=
+
+    {
+        Name = name
+        AuxModuleHashAlgorithm = hash
+        SecurityDeclsStored = ctxt.securityDeclsReader_Assembly
+        PublicKey = pubkey
+        Version = Some(ILVersionInfo(v1, v2, v3, v4))
+        Locale = readStringHeapOption ctxt localeIdx
+        CustomAttrsStored = ctxt.customAttrsReader_Assembly
+        MetadataIndex = idx
+        AssemblyLongevity =
+            let masked = flags &&& 0x000e
+
+            if masked = 0x0000 then
+                ILAssemblyLongevity.Unspecified
+            elif masked = 0x0002 then
+                ILAssemblyLongevity.Library
+            elif masked = 0x0004 then
+                ILAssemblyLongevity.PlatformAppDomain
+            elif masked = 0x0006 then
+                ILAssemblyLongevity.PlatformProcess
+            elif masked = 0x0008 then
+                ILAssemblyLongevity.PlatformSystem
+            else
+                ILAssemblyLongevity.Unspecified
+        ExportedTypes = seekReadTopExportedTypes ctxt
+        EntrypointElsewhere =
             let tab, tok = pectxt.entryPointToken
-            if tab = TableNames.File then Some (seekReadFile ctxt mdview tok) else None
-      Retargetable = 0 <> (flags &&& 0x100)
-      DisableJitOptimizations = 0 <> (flags &&& 0x4000)
-      JitTracking = 0 <> (flags &&& 0x8000)
-      IgnoreSymbolStoreSequencePoints = 0 <> (flags &&& 0x2000) }
+
+            if tab = TableNames.File then
+                Some(seekReadFile ctxt mdview tok)
+            else
+                None
+        Retargetable = 0 <> (flags &&& 0x100)
+        DisableJitOptimizations = 0 <> (flags &&& 0x4000)
+        JitTracking = 0 <> (flags &&& 0x8000)
+        IgnoreSymbolStoreSequencePoints = 0 <> (flags &&& 0x2000)
+    }
 
 and seekReadAssemblyRef (ctxt: ILMetadataReader) idx = ctxt.seekReadAssemblyRef idx
+
 and seekReadAssemblyRefUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
-    let v1, v2, v3, v4, flags, publicKeyOrTokenIdx, nameIdx, localeIdx, hashValueIdx = seekReadAssemblyRefRow ctxt mdv idx
+
+    let v1, v2, v3, v4, flags, publicKeyOrTokenIdx, nameIdx, localeIdx, hashValueIdx =
+        seekReadAssemblyRefRow ctxt mdv idx
+
     let nm = readStringHeap ctxt nameIdx
+
     let publicKey =
         match readBlobHeapOption ctxt publicKeyOrTokenIdx with
-          | None -> None
-          | Some blob -> Some (if (flags &&& 0x0001) <> 0x0 then PublicKey blob else PublicKeyToken blob)
-
-    ILAssemblyRef.Create
-        (name = nm,
-         hash = readBlobHeapOption ctxt hashValueIdx,
-         publicKey = publicKey,
-         retargetable = ((flags &&& 0x0100) <> 0x0),
-         version = Some (ILVersionInfo (v1, v2, v3, v4)),
-         locale = readStringHeapOption ctxt localeIdx)
+        | None -> None
+        | Some blob ->
+            Some(
+                if (flags &&& 0x0001) <> 0x0 then
+                    PublicKey blob
+                else
+                    PublicKeyToken blob
+            )
+
+    ILAssemblyRef.Create(
+        name = nm,
+        hash = readBlobHeapOption ctxt hashValueIdx,
+        publicKey = publicKey,
+        retargetable = ((flags &&& 0x0100) <> 0x0),
+        version = Some(ILVersionInfo(v1, v2, v3, v4)),
+        locale = readStringHeapOption ctxt localeIdx
+    )
 
 and seekReadModuleRef (ctxt: ILMetadataReader) mdv idx =
     let nameIdx = seekReadModuleRefRow ctxt mdv idx
-    ILModuleRef.Create(name = readStringHeap ctxt nameIdx, hasMetadata=true, hash=None)
+    ILModuleRef.Create(name = readStringHeap ctxt nameIdx, hasMetadata = true, hash = None)
 
 and seekReadFile (ctxt: ILMetadataReader) mdv idx =
     let flags, nameIdx, hashValueIdx = seekReadFileRow ctxt mdv idx
-    ILModuleRef.Create(name = readStringHeap ctxt nameIdx, hasMetadata= ((flags &&& 0x0001) = 0x0), hash= readBlobHeapOption ctxt hashValueIdx)
+
+    ILModuleRef.Create(
+        name = readStringHeap ctxt nameIdx,
+        hasMetadata = ((flags &&& 0x0001) = 0x0),
+        hash = readBlobHeapOption ctxt hashValueIdx
+    )
 
 and seekReadClassLayout (ctxt: ILMetadataReader) mdv idx =
     let res =
-        seekReadOptionalIndexedRow (ctxt.getNumRows TableNames.ClassLayout,
+        seekReadOptionalIndexedRow (
+            ctxt.getNumRows TableNames.ClassLayout,
             seekReadClassLayoutRow ctxt mdv,
             (fun (_, _, tidx) -> tidx),
             simpleIndexCompare idx,
             isSorted ctxt TableNames.ClassLayout,
-            (fun (pack, size, _) -> pack, size))
-    match res  with
+            (fun (pack, size, _) -> pack, size)
+        )
+
+    match res with
     | None -> { Size = None; Pack = None }
     | Some (pack, size) -> { Size = Some size; Pack = Some pack }
 
 and typeAccessOfFlags flags =
     let f = (flags &&& 0x00000007)
-    if f = 0x00000001 then ILTypeDefAccess.Public
-    elif f = 0x00000002 then ILTypeDefAccess.Nested ILMemberAccess.Public
-    elif f = 0x00000003 then ILTypeDefAccess.Nested ILMemberAccess.Private
-    elif f = 0x00000004 then ILTypeDefAccess.Nested ILMemberAccess.Family
-    elif f = 0x00000006 then ILTypeDefAccess.Nested ILMemberAccess.FamilyAndAssembly
-    elif f = 0x00000007 then ILTypeDefAccess.Nested ILMemberAccess.FamilyOrAssembly
-    elif f = 0x00000005 then ILTypeDefAccess.Nested ILMemberAccess.Assembly
-    else ILTypeDefAccess.Private
+
+    if f = 0x00000001 then
+        ILTypeDefAccess.Public
+    elif f = 0x00000002 then
+        ILTypeDefAccess.Nested ILMemberAccess.Public
+    elif f = 0x00000003 then
+        ILTypeDefAccess.Nested ILMemberAccess.Private
+    elif f = 0x00000004 then
+        ILTypeDefAccess.Nested ILMemberAccess.Family
+    elif f = 0x00000006 then
+        ILTypeDefAccess.Nested ILMemberAccess.FamilyAndAssembly
+    elif f = 0x00000007 then
+        ILTypeDefAccess.Nested ILMemberAccess.FamilyOrAssembly
+    elif f = 0x00000005 then
+        ILTypeDefAccess.Nested ILMemberAccess.Assembly
+    else
+        ILTypeDefAccess.Private
 
 and typeLayoutOfFlags (ctxt: ILMetadataReader) mdv flags tidx =
     let f = (flags &&& 0x00000018)
-    if f = 0x00000008 then ILTypeDefLayout.Sequential (seekReadClassLayout ctxt mdv tidx)
-    elif f = 0x00000010 then ILTypeDefLayout.Explicit (seekReadClassLayout ctxt mdv tidx)
-    else ILTypeDefLayout.Auto
+
+    if f = 0x00000008 then
+        ILTypeDefLayout.Sequential(seekReadClassLayout ctxt mdv tidx)
+    elif f = 0x00000010 then
+        ILTypeDefLayout.Explicit(seekReadClassLayout ctxt mdv tidx)
+    else
+        ILTypeDefLayout.Auto
 
 and isTopTypeDef flags =
-    (typeAccessOfFlags flags = ILTypeDefAccess.Private) ||
-     typeAccessOfFlags flags = ILTypeDefAccess.Public
+    (typeAccessOfFlags flags = ILTypeDefAccess.Private)
+    || typeAccessOfFlags flags = ILTypeDefAccess.Public
 
 and seekIsTopTypeDefOfIdx ctxt idx =
     let flags, _, _, _, _, _ = seekReadTypeDefRow ctxt idx
@@ -1678,6 +2078,7 @@ and seekIsTopTypeDefOfIdx ctxt idx =
 and readBlobHeapAsSplitTypeName ctxt (nameIdx, namespaceIdx) =
     let name = readStringHeap ctxt nameIdx
     let nspace = readStringHeapOption ctxt namespaceIdx
+
     match nspace with
     | Some nspace -> splitNamespace nspace, name
     | None -> [], name
@@ -1685,175 +2086,226 @@ and readBlobHeapAsSplitTypeName ctxt (nameIdx, namespaceIdx) =
 and readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx) =
     let name = readStringHeap ctxt nameIdx
     let nspace = readStringHeapOption ctxt namespaceIdx
+
     match nspace with
     | None -> name
-    | Some ns -> ctxt.memoizeString (ns+"."+name)
+    | Some ns -> ctxt.memoizeString (ns + "." + name)
 
 and seekReadTypeDefRowExtents (ctxt: ILMetadataReader) _info (idx: int) =
     if idx >= ctxt.getNumRows TableNames.TypeDef then
         struct (ctxt.getNumRows TableNames.Field + 1, ctxt.getNumRows TableNames.Method + 1)
     else
         let _, _, _, _, fieldsIdx, methodsIdx = seekReadTypeDefRow ctxt (idx + 1)
-        struct (fieldsIdx, methodsIdx )
+        struct (fieldsIdx, methodsIdx)
 
 and seekReadTypeDefRowWithExtents ctxt (idx: int) =
-    let info= seekReadTypeDefRow ctxt idx
+    let info = seekReadTypeDefRow ctxt idx
     info, seekReadTypeDefRowExtents ctxt info idx
 
 and seekReadPreTypeDef ctxt toponly (idx: int) =
     let flags, nameIdx, namespaceIdx, _, _, _ = seekReadTypeDefRow ctxt idx
-    if toponly && not (isTopTypeDef flags) then None
+
+    if toponly && not (isTopTypeDef flags) then
+        None
     else
-     let ns, n = readBlobHeapAsSplitTypeName ctxt (nameIdx, namespaceIdx)
-     // Return the ILPreTypeDef
-     Some (mkILPreTypeDefRead (ns, n, idx, ctxt.typeDefReader))
-
-and typeDefReader ctxtH: ILTypeDefStored =
-  mkILTypeDefReader
-    (fun idx ->
-           let (ctxt: ILMetadataReader) = getHole ctxtH
-           let mdv = ctxt.mdfile.GetView()
-           // Re-read so as not to save all these in the lazy closure - this suspension ctxt.is the largest
-           // heavily allocated one in all of AbsIL
-
-           let flags, nameIdx, namespaceIdx, extendsIdx, fieldsIdx, methodsIdx as info = seekReadTypeDefRow ctxt idx
-           let nm = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
-           let struct (endFieldsIdx, endMethodsIdx) = seekReadTypeDefRowExtents ctxt info idx
-           let typars = seekReadGenericParams ctxt 0 (tomd_TypeDef, idx)
-           let numTypars = typars.Length
-           let super = seekReadOptionalTypeDefOrRef ctxt numTypars AsObject extendsIdx
-           let layout = typeLayoutOfFlags ctxt mdv flags idx
-           let hasLayout = (match layout with ILTypeDefLayout.Explicit _ -> true | _ -> false)
-           let mdefs = seekReadMethods ctxt numTypars methodsIdx endMethodsIdx
-           let fdefs = seekReadFields ctxt (numTypars, hasLayout) fieldsIdx endFieldsIdx
-           let nested = seekReadNestedTypeDefs ctxt idx
-           let impls = seekReadInterfaceImpls ctxt mdv numTypars idx
-           let mimpls = seekReadMethodImpls ctxt numTypars idx
-           let props = seekReadProperties ctxt numTypars idx
-           let events = seekReadEvents ctxt numTypars idx
-           ILTypeDef(name=nm,
-                     genericParams=typars,
-                     attributes= enum(flags),
-                     layout = layout,
-                     nestedTypes= nested,
-                     implements = impls,
-                     extends = super,
-                     methods = mdefs,
-                     securityDeclsStored = ctxt.securityDeclsReader_TypeDef,
-                     fields=fdefs,
-                     methodImpls=mimpls,
-                     events= events,
-                     properties=props,
-                     isKnownToBeAttribute=false,
-                     customAttrsStored=ctxt.customAttrsReader_TypeDef,
-                     metadataIndex=idx)
-    )
+        let ns, n = readBlobHeapAsSplitTypeName ctxt (nameIdx, namespaceIdx)
+        // Return the ILPreTypeDef
+        Some(mkILPreTypeDefRead (ns, n, idx, ctxt.typeDefReader))
+
+and typeDefReader ctxtH : ILTypeDefStored =
+    mkILTypeDefReader (fun idx ->
+        let (ctxt: ILMetadataReader) = getHole ctxtH
+        let mdv = ctxt.mdfile.GetView()
+        // Re-read so as not to save all these in the lazy closure - this suspension ctxt.is the largest
+        // heavily allocated one in all of AbsIL
+
+        let flags, nameIdx, namespaceIdx, extendsIdx, fieldsIdx, methodsIdx as info =
+            seekReadTypeDefRow ctxt idx
+
+        let nm = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
+        let struct (endFieldsIdx, endMethodsIdx) = seekReadTypeDefRowExtents ctxt info idx
+        let typars = seekReadGenericParams ctxt 0 (tomd_TypeDef, idx)
+        let numTypars = typars.Length
+        let super = seekReadOptionalTypeDefOrRef ctxt numTypars AsObject extendsIdx
+        let layout = typeLayoutOfFlags ctxt mdv flags idx
+
+        let hasLayout =
+            (match layout with
+             | ILTypeDefLayout.Explicit _ -> true
+             | _ -> false)
+
+        let mdefs = seekReadMethods ctxt numTypars methodsIdx endMethodsIdx
+        let fdefs = seekReadFields ctxt (numTypars, hasLayout) fieldsIdx endFieldsIdx
+        let nested = seekReadNestedTypeDefs ctxt idx
+        let impls = seekReadInterfaceImpls ctxt mdv numTypars idx
+        let mimpls = seekReadMethodImpls ctxt numTypars idx
+        let props = seekReadProperties ctxt numTypars idx
+        let events = seekReadEvents ctxt numTypars idx
+
+        ILTypeDef(
+            name = nm,
+            genericParams = typars,
+            attributes = enum (flags),
+            layout = layout,
+            nestedTypes = nested,
+            implements = impls,
+            extends = super,
+            methods = mdefs,
+            securityDeclsStored = ctxt.securityDeclsReader_TypeDef,
+            fields = fdefs,
+            methodImpls = mimpls,
+            events = events,
+            properties = props,
+            isKnownToBeAttribute = false,
+            customAttrsStored = ctxt.customAttrsReader_TypeDef,
+            metadataIndex = idx
+        ))
 
 and seekReadTopTypeDefs (ctxt: ILMetadataReader) =
-    [| for i = 1 to ctxt.getNumRows TableNames.TypeDef do
-          match seekReadPreTypeDef ctxt true i with
-          | None -> ()
-          | Some td -> yield td |]
+    [|
+        for i = 1 to ctxt.getNumRows TableNames.TypeDef do
+            match seekReadPreTypeDef ctxt true i with
+            | None -> ()
+            | Some td -> yield td
+    |]
 
 and seekReadNestedTypeDefs (ctxt: ILMetadataReader) tidx =
     mkILTypeDefsComputed (fun () ->
-           let nestedIdxs = seekReadIndexedRows (ctxt.getNumRows TableNames.Nested, seekReadNestedRow ctxt, snd, simpleIndexCompare tidx, false, fst)
-           [| for i in nestedIdxs do
-                 match seekReadPreTypeDef ctxt false i with
-                 | None -> ()
-                 | Some td -> yield td |])
+        let nestedIdxs =
+            seekReadIndexedRows (ctxt.getNumRows TableNames.Nested, seekReadNestedRow ctxt, snd, simpleIndexCompare tidx, false, fst)
+
+        [|
+            for i in nestedIdxs do
+                match seekReadPreTypeDef ctxt false i with
+                | None -> ()
+                | Some td -> yield td
+        |])
 
 and seekReadInterfaceImpls (ctxt: ILMetadataReader) mdv numTypars tidx =
-    seekReadIndexedRows (ctxt.getNumRows TableNames.InterfaceImpl,
-                         seekReadInterfaceImplRow ctxt mdv,
-                         fst,
-                         simpleIndexCompare tidx,
-                         isSorted ctxt TableNames.InterfaceImpl,
-                         (snd >> seekReadTypeDefOrRef ctxt numTypars AsObject (*ok*) []))
+    seekReadIndexedRows (
+        ctxt.getNumRows TableNames.InterfaceImpl,
+        seekReadInterfaceImplRow ctxt mdv,
+        fst,
+        simpleIndexCompare tidx,
+        isSorted ctxt TableNames.InterfaceImpl,
+        (snd >> seekReadTypeDefOrRef ctxt numTypars AsObject (*ok*) [])
+    )
 
-and seekReadGenericParams ctxt numTypars (a, b): ILGenericParameterDefs =
+and seekReadGenericParams ctxt numTypars (a, b) : ILGenericParameterDefs =
     ctxt.seekReadGenericParams (GenericParamsIdx(numTypars, a, b))
 
-and seekReadGenericParamsUncached ctxtH (GenericParamsIdx(numTypars, a, b)) =
+and seekReadGenericParamsUncached ctxtH (GenericParamsIdx (numTypars, a, b)) =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
+
     let pars =
-        seekReadIndexedRows
-            (ctxt.getNumRows TableNames.GenericParam, seekReadGenericParamRow ctxt mdv,
-             (fun (_, _, _, tomd, _) -> tomd),
-             tomdCompare (TaggedIndex(a, b)),
-             isSorted ctxt TableNames.GenericParam,
-             (fun (gpidx, seq, flags, _, nameIdx) ->
-                 let flags = int32 flags
-                 let variance_flags = flags &&& 0x0003
-                 let variance =
-                     if variance_flags = 0x0000 then NonVariant
-                     elif variance_flags = 0x0001 then CoVariant
-                     elif variance_flags = 0x0002 then ContraVariant
-                     else NonVariant
-                 let constraints = seekReadGenericParamConstraints ctxt mdv numTypars gpidx
-                 seq, {Name=readStringHeap ctxt nameIdx
-                       Constraints = constraints
-                       Variance=variance
-                       CustomAttrsStored = ctxt.customAttrsReader_GenericParam
-                       MetadataIndex=gpidx
-                       HasReferenceTypeConstraint= (flags &&& 0x0004) <> 0
-                       HasNotNullableValueTypeConstraint= (flags &&& 0x0008) <> 0
-                       HasDefaultConstructorConstraint=(flags &&& 0x0010) <> 0 }))
+        seekReadIndexedRows (
+            ctxt.getNumRows TableNames.GenericParam,
+            seekReadGenericParamRow ctxt mdv,
+            (fun (_, _, _, tomd, _) -> tomd),
+            tomdCompare (TaggedIndex(a, b)),
+            isSorted ctxt TableNames.GenericParam,
+            (fun (gpidx, seq, flags, _, nameIdx) ->
+                let flags = int32 flags
+                let variance_flags = flags &&& 0x0003
+
+                let variance =
+                    if variance_flags = 0x0000 then
+                        NonVariant
+                    elif variance_flags = 0x0001 then
+                        CoVariant
+                    elif variance_flags = 0x0002 then
+                        ContraVariant
+                    else
+                        NonVariant
+
+                let constraints = seekReadGenericParamConstraints ctxt mdv numTypars gpidx
+
+                seq,
+                {
+                    Name = readStringHeap ctxt nameIdx
+                    Constraints = constraints
+                    Variance = variance
+                    CustomAttrsStored = ctxt.customAttrsReader_GenericParam
+                    MetadataIndex = gpidx
+                    HasReferenceTypeConstraint = (flags &&& 0x0004) <> 0
+                    HasNotNullableValueTypeConstraint = (flags &&& 0x0008) <> 0
+                    HasDefaultConstructorConstraint = (flags &&& 0x0010) <> 0
+                })
+        )
+
     pars |> List.sortBy fst |> List.map snd
 
 and seekReadGenericParamConstraints (ctxt: ILMetadataReader) mdv numTypars gpidx =
-    seekReadIndexedRows
-        (ctxt.getNumRows TableNames.GenericParamConstraint,
-         seekReadGenericParamConstraintRow ctxt mdv,
-         fst,
-         simpleIndexCompare gpidx,
-         isSorted ctxt TableNames.GenericParamConstraint,
-         (snd >> seekReadTypeDefOrRef ctxt numTypars AsObject (*ok*) List.empty))
+    seekReadIndexedRows (
+        ctxt.getNumRows TableNames.GenericParamConstraint,
+        seekReadGenericParamConstraintRow ctxt mdv,
+        fst,
+        simpleIndexCompare gpidx,
+        isSorted ctxt TableNames.GenericParamConstraint,
+        (snd >> seekReadTypeDefOrRef ctxt numTypars AsObject (*ok*) List.empty)
+    )
 
 and seekReadTypeDefAsType (ctxt: ILMetadataReader) boxity (ginst: ILTypes) idx =
-      ctxt.seekReadTypeDefAsType (TypeDefAsTypIdx (boxity, ginst, idx))
+    ctxt.seekReadTypeDefAsType (TypeDefAsTypIdx(boxity, ginst, idx))
 
 and seekReadTypeDefAsTypeUncached ctxtH (TypeDefAsTypIdx (boxity, ginst, idx)) =
     let ctxt = getHole ctxtH
     mkILTy boxity (ILTypeSpec.Create(seekReadTypeDefAsTypeRef ctxt idx, ginst))
 
 and seekReadTypeDefAsTypeRef (ctxt: ILMetadataReader) idx =
-     let enc =
-       if seekIsTopTypeDefOfIdx ctxt idx then []
-       else
-         let enclIdx = seekReadIndexedRow (ctxt.getNumRows TableNames.Nested, seekReadNestedRow ctxt, fst, simpleIndexCompare idx, isSorted ctxt TableNames.Nested, snd)
-         let tref = seekReadTypeDefAsTypeRef ctxt enclIdx
-         tref.Enclosing@[tref.Name]
-     let _, nameIdx, namespaceIdx, _, _, _ = seekReadTypeDefRow ctxt idx
-     let nm = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
-     ILTypeRef.Create(scope=ILScopeRef.Local, enclosing=enc, name = nm )
+    let enc =
+        if seekIsTopTypeDefOfIdx ctxt idx then
+            []
+        else
+            let enclIdx =
+                seekReadIndexedRow (
+                    ctxt.getNumRows TableNames.Nested,
+                    seekReadNestedRow ctxt,
+                    fst,
+                    simpleIndexCompare idx,
+                    isSorted ctxt TableNames.Nested,
+                    snd
+                )
+
+            let tref = seekReadTypeDefAsTypeRef ctxt enclIdx
+            tref.Enclosing @ [ tref.Name ]
+
+    let _, nameIdx, namespaceIdx, _, _, _ = seekReadTypeDefRow ctxt idx
+    let nm = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
+    ILTypeRef.Create(scope = ILScopeRef.Local, enclosing = enc, name = nm)
 
 and seekReadTypeRef (ctxt: ILMetadataReader) idx = ctxt.seekReadTypeRef idx
+
 and seekReadTypeRefUncached ctxtH idx =
-     let (ctxt: ILMetadataReader) = getHole ctxtH
-     let mdv = ctxt.mdfile.GetView()
-     let scopeIdx, nameIdx, namespaceIdx = seekReadTypeRefRow ctxt mdv idx
-     let scope, enc = seekReadTypeRefScope ctxt mdv scopeIdx
-     let nm = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
-     ILTypeRef.Create(scope=scope, enclosing=enc, name = nm)
-
-and seekReadTypeRefAsType (ctxt: ILMetadataReader) boxity ginst idx = ctxt.seekReadTypeRefAsType (TypeRefAsTypIdx (boxity, ginst, idx))
+    let (ctxt: ILMetadataReader) = getHole ctxtH
+    let mdv = ctxt.mdfile.GetView()
+    let scopeIdx, nameIdx, namespaceIdx = seekReadTypeRefRow ctxt mdv idx
+    let scope, enc = seekReadTypeRefScope ctxt mdv scopeIdx
+    let nm = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
+    ILTypeRef.Create(scope = scope, enclosing = enc, name = nm)
+
+and seekReadTypeRefAsType (ctxt: ILMetadataReader) boxity ginst idx =
+    ctxt.seekReadTypeRefAsType (TypeRefAsTypIdx(boxity, ginst, idx))
+
 and seekReadTypeRefAsTypeUncached ctxtH (TypeRefAsTypIdx (boxity, ginst, idx)) =
-     let ctxt = getHole ctxtH
-     mkILTy boxity (ILTypeSpec.Create(seekReadTypeRef ctxt idx, ginst))
+    let ctxt = getHole ctxtH
+    mkILTy boxity (ILTypeSpec.Create(seekReadTypeRef ctxt idx, ginst))
 
-and seekReadTypeDefOrRef (ctxt: ILMetadataReader) numTypars boxity (ginst: ILTypes) (TaggedIndex(tag, idx) ) =
+and seekReadTypeDefOrRef (ctxt: ILMetadataReader) numTypars boxity (ginst: ILTypes) (TaggedIndex (tag, idx)) =
     let mdv = ctxt.mdfile.GetView()
+
     match tag with
     | tag when tag = tdor_TypeDef -> seekReadTypeDefAsType ctxt boxity ginst idx
     | tag when tag = tdor_TypeRef -> seekReadTypeRefAsType ctxt boxity ginst idx
     | tag when tag = tdor_TypeSpec ->
-        if not (List.isEmpty ginst) then dprintn "type spec used as type constructor for a generic instantiation: ignoring instantiation"
+        if not (List.isEmpty ginst) then
+            dprintn "type spec used as type constructor for a generic instantiation: ignoring instantiation"
+
         readBlobHeapAsType ctxt numTypars (seekReadTypeSpecRow ctxt mdv idx)
     | _ -> failwith "seekReadTypeDefOrRef ctxt"
 
-and seekReadTypeDefOrRefAsTypeRef (ctxt: ILMetadataReader) (TaggedIndex(tag, idx) ) =
+and seekReadTypeDefOrRefAsTypeRef (ctxt: ILMetadataReader) (TaggedIndex (tag, idx)) =
     match tag with
     | tag when tag = tdor_TypeDef -> seekReadTypeDefAsTypeRef ctxt idx
     | tag when tag = tdor_TypeRef -> seekReadTypeRef ctxt idx
@@ -1862,114 +2314,168 @@ and seekReadTypeDefOrRefAsTypeRef (ctxt: ILMetadataReader) (TaggedIndex(tag, idx
         PrimaryAssemblyILGlobals.typ_Object.TypeRef
     | _ -> failwith "seekReadTypeDefOrRefAsTypeRef_readTypeDefOrRefOrSpec"
 
-and seekReadMethodRefParent (ctxt: ILMetadataReader) mdv numTypars (TaggedIndex(tag, idx)) =
+and seekReadMethodRefParent (ctxt: ILMetadataReader) mdv numTypars (TaggedIndex (tag, idx)) =
     match tag with
-    | tag when tag = mrp_TypeRef -> seekReadTypeRefAsType ctxt AsObject (* not ok - no way to tell if a member ref parent is a value type or not *) List.empty idx
-    | tag when tag = mrp_ModuleRef -> mkILTypeForGlobalFunctions (ILScopeRef.Module (seekReadModuleRef ctxt mdv idx))
+    | tag when tag = mrp_TypeRef ->
+        seekReadTypeRefAsType ctxt AsObject (* not ok - no way to tell if a member ref parent is a value type or not *) List.empty idx
+    | tag when tag = mrp_ModuleRef -> mkILTypeForGlobalFunctions (ILScopeRef.Module(seekReadModuleRef ctxt mdv idx))
     | tag when tag = mrp_MethodDef ->
-        let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefAsMethodData ctxt idx
+        let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+            seekReadMethodDefAsMethodData ctxt idx
+
         let mspec = mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
         mspec.DeclaringType
     | tag when tag = mrp_TypeSpec -> readBlobHeapAsType ctxt numTypars (seekReadTypeSpecRow ctxt mdv idx)
     | _ -> failwith "seekReadMethodRefParent"
 
-and seekReadMethodDefOrRef (ctxt: ILMetadataReader) numTypars (TaggedIndex(tag, idx)) =
+and seekReadMethodDefOrRef (ctxt: ILMetadataReader) numTypars (TaggedIndex (tag, idx)) =
     match tag with
     | tag when tag = mdor_MethodDef ->
-        let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefAsMethodData ctxt idx
+        let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+            seekReadMethodDefAsMethodData ctxt idx
+
         VarArgMethodData(enclTy, cc, nm, argTys, None, retTy, methInst)
-    | tag when tag = mdor_MemberRef ->
-        seekReadMemberRefAsMethodData ctxt numTypars idx
+    | tag when tag = mdor_MemberRef -> seekReadMemberRefAsMethodData ctxt numTypars idx
     | _ -> failwith "seekReadMethodDefOrRef"
 
 and seekReadMethodDefOrRefNoVarargs (ctxt: ILMetadataReader) numTypars x =
-     let (VarArgMethodData(enclTy, cc, nm, argTys, varargs, retTy, methInst)) = seekReadMethodDefOrRef ctxt numTypars x
-     if varargs <> None then dprintf "ignoring sentinel and varargs in ILMethodDef token signature"
-     MethodData(enclTy, cc, nm, argTys, retTy, methInst)
+    let (VarArgMethodData (enclTy, cc, nm, argTys, varargs, retTy, methInst)) =
+        seekReadMethodDefOrRef ctxt numTypars x
 
-and seekReadCustomAttrType (ctxt: ILMetadataReader) (TaggedIndex(tag, idx) ) =
+    if varargs <> None then
+        dprintf "ignoring sentinel and varargs in ILMethodDef token signature"
+
+    MethodData(enclTy, cc, nm, argTys, retTy, methInst)
+
+and seekReadCustomAttrType (ctxt: ILMetadataReader) (TaggedIndex (tag, idx)) =
     match tag with
     | tag when tag = cat_MethodDef ->
-        let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefAsMethodData ctxt idx
+        let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+            seekReadMethodDefAsMethodData ctxt idx
+
         mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
     | tag when tag = cat_MemberRef ->
-        let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMemberRefAsMethDataNoVarArgs ctxt 0 idx
+        let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+            seekReadMemberRefAsMethDataNoVarArgs ctxt 0 idx
+
         mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
     | _ -> failwith "seekReadCustomAttrType ctxt"
 
-and seekReadImplAsScopeRef (ctxt: ILMetadataReader) mdv (TaggedIndex(tag, idx) ) =
-     if idx = 0 then ILScopeRef.Local
-     else
-       match tag with
-       | tag when tag = i_File -> ILScopeRef.Module (seekReadFile ctxt mdv idx)
-       | tag when tag = i_AssemblyRef -> ILScopeRef.Assembly (seekReadAssemblyRef ctxt idx)
-       | tag when tag = i_ExportedType -> failwith "seekReadImplAsScopeRef"
-       | _ -> failwith "seekReadImplAsScopeRef"
+and seekReadImplAsScopeRef (ctxt: ILMetadataReader) mdv (TaggedIndex (tag, idx)) =
+    if idx = 0 then
+        ILScopeRef.Local
+    else
+        match tag with
+        | tag when tag = i_File -> ILScopeRef.Module(seekReadFile ctxt mdv idx)
+        | tag when tag = i_AssemblyRef -> ILScopeRef.Assembly(seekReadAssemblyRef ctxt idx)
+        | tag when tag = i_ExportedType -> failwith "seekReadImplAsScopeRef"
+        | _ -> failwith "seekReadImplAsScopeRef"
 
-and seekReadTypeRefScope (ctxt: ILMetadataReader) mdv (TaggedIndex(tag, idx) ) =
+and seekReadTypeRefScope (ctxt: ILMetadataReader) mdv (TaggedIndex (tag, idx)) =
     match tag with
     | tag when tag = rs_Module -> ILScopeRef.Local, []
-    | tag when tag = rs_ModuleRef -> ILScopeRef.Module (seekReadModuleRef ctxt mdv idx), []
-    | tag when tag = rs_AssemblyRef -> ILScopeRef.Assembly (seekReadAssemblyRef ctxt idx), []
+    | tag when tag = rs_ModuleRef -> ILScopeRef.Module(seekReadModuleRef ctxt mdv idx), []
+    | tag when tag = rs_AssemblyRef -> ILScopeRef.Assembly(seekReadAssemblyRef ctxt idx), []
     | tag when tag = rs_TypeRef ->
         let tref = seekReadTypeRef ctxt idx
-        tref.Scope, (tref.Enclosing@[tref.Name])
+        tref.Scope, (tref.Enclosing @ [ tref.Name ])
     | _ -> failwith "seekReadTypeRefScope"
 
 and seekReadOptionalTypeDefOrRef (ctxt: ILMetadataReader) numTypars boxity idx =
-    if idx = TaggedIndex(tdor_TypeDef, 0) then None
-    else Some (seekReadTypeDefOrRef ctxt numTypars boxity List.empty idx)
+    if idx = TaggedIndex(tdor_TypeDef, 0) then
+        None
+    else
+        Some(seekReadTypeDefOrRef ctxt numTypars boxity List.empty idx)
 
 and seekReadField ctxt mdv (numTypars, hasLayout) (idx: int) =
     let flags, nameIdx, typeIdx = seekReadFieldRow ctxt mdv idx
     let nm = readStringHeap ctxt nameIdx
     let isStatic = (flags &&& 0x0010) <> 0
-    ILFieldDef(name = nm,
-               fieldType= readBlobHeapAsFieldSig ctxt numTypars typeIdx,
-               attributes = enum(flags),
-               literalValue = (if (flags &&& 0x8000) = 0 then None else Some (seekReadConstant ctxt (TaggedIndex(hc_FieldDef, idx)))),
-               marshal =
-                   (if (flags &&& 0x1000) = 0 then
-                       None
-                    else
-                       Some (seekReadIndexedRow (ctxt.getNumRows TableNames.FieldMarshal, seekReadFieldMarshalRow ctxt mdv,
-                                                 fst, hfmCompare (TaggedIndex(hfm_FieldDef, idx)),
-                                                 isSorted ctxt TableNames.FieldMarshal,
-                                                 (snd >> readBlobHeapAsNativeType ctxt)))),
-               data =
-                   (if (flags &&& 0x0100) = 0 then
-                       None
-                    else
-                        match ctxt.pectxtCaptured with
-                        | None -> None // indicates metadata only, where Data is not available
-                        | Some pectxt ->
-                            let rva = seekReadIndexedRow (ctxt.getNumRows TableNames.FieldRVA, seekReadFieldRVARow ctxt mdv,
-                                                          snd, simpleIndexCompare idx, isSorted ctxt TableNames.FieldRVA, fst)
-                            Some (rvaToData ctxt pectxt "field" rva)),
-               offset =
-                   (if hasLayout && not isStatic then
-                       Some (seekReadIndexedRow (ctxt.getNumRows TableNames.FieldLayout, seekReadFieldLayoutRow ctxt mdv,
-                                               snd, simpleIndexCompare idx, isSorted ctxt TableNames.FieldLayout, fst)) else None),
-               customAttrsStored=ctxt.customAttrsReader_FieldDef,
-               metadataIndex = idx)
+
+    ILFieldDef(
+        name = nm,
+        fieldType = readBlobHeapAsFieldSig ctxt numTypars typeIdx,
+        attributes = enum (flags),
+        literalValue =
+            (if (flags &&& 0x8000) = 0 then
+                 None
+             else
+                 Some(seekReadConstant ctxt (TaggedIndex(hc_FieldDef, idx)))),
+        marshal =
+            (if (flags &&& 0x1000) = 0 then
+                 None
+             else
+                 Some(
+                     seekReadIndexedRow (
+                         ctxt.getNumRows TableNames.FieldMarshal,
+                         seekReadFieldMarshalRow ctxt mdv,
+                         fst,
+                         hfmCompare (TaggedIndex(hfm_FieldDef, idx)),
+                         isSorted ctxt TableNames.FieldMarshal,
+                         (snd >> readBlobHeapAsNativeType ctxt)
+                     )
+                 )),
+        data =
+            (if (flags &&& 0x0100) = 0 then
+                 None
+             else
+                 match ctxt.pectxtCaptured with
+                 | None -> None // indicates metadata only, where Data is not available
+                 | Some pectxt ->
+                     let rva =
+                         seekReadIndexedRow (
+                             ctxt.getNumRows TableNames.FieldRVA,
+                             seekReadFieldRVARow ctxt mdv,
+                             snd,
+                             simpleIndexCompare idx,
+                             isSorted ctxt TableNames.FieldRVA,
+                             fst
+                         )
+
+                     Some(rvaToData ctxt pectxt "field" rva)),
+        offset =
+            (if hasLayout && not isStatic then
+                 Some(
+                     seekReadIndexedRow (
+                         ctxt.getNumRows TableNames.FieldLayout,
+                         seekReadFieldLayoutRow ctxt mdv,
+                         snd,
+                         simpleIndexCompare idx,
+                         isSorted ctxt TableNames.FieldLayout,
+                         fst
+                     )
+                 )
+             else
+                 None),
+        customAttrsStored = ctxt.customAttrsReader_FieldDef,
+        metadataIndex = idx
+    )
 
 and seekReadFields (ctxt: ILMetadataReader) (numTypars, hasLayout) fidx1 fidx2 =
-    mkILFieldsLazy
-       (lazy
-           let mdv = ctxt.mdfile.GetView()
-           [ if fidx1 > 0 then
-               for i = fidx1 to fidx2 - 1 do
-                   yield seekReadField ctxt mdv (numTypars, hasLayout) i ])
+    mkILFieldsLazy (
+        lazy
+            let mdv = ctxt.mdfile.GetView()
+
+            [
+                if fidx1 > 0 then
+                    for i = fidx1 to fidx2 - 1 do
+                        yield seekReadField ctxt mdv (numTypars, hasLayout) i
+            ]
+    )
 
 and seekReadMethods (ctxt: ILMetadataReader) numTypars midx1 midx2 =
     mkILMethodsComputed (fun () ->
-           let mdv = ctxt.mdfile.GetView()
-           [| if midx1 > 0 then
-                 for i = midx1 to midx2 - 1 do
-                     yield seekReadMethod ctxt mdv numTypars i |])
+        let mdv = ctxt.mdfile.GetView()
+
+        [|
+            if midx1 > 0 then
+                for i = midx1 to midx2 - 1 do
+                    yield seekReadMethod ctxt mdv numTypars i
+        |])
 
 and sigptrGetTypeDefOrRefOrSpecIdx bytes sigptr =
     let struct (n, sigptr) = sigptrGetZInt32 bytes sigptr
+
     if (n &&& 0x01) = 0x0 then (* Type Def *)
         TaggedIndex(tdor_TypeDef, (n >>>& 2)), sigptr
     else (* Type Ref *)
@@ -1977,28 +2483,54 @@ and sigptrGetTypeDefOrRefOrSpecIdx bytes sigptr =
 
 and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
     let b0, sigptr = sigptrGetByte bytes sigptr
-    if b0 = et_OBJECT then PrimaryAssemblyILGlobals.typ_Object, sigptr
-    elif b0 = et_STRING then PrimaryAssemblyILGlobals.typ_String, sigptr
-    elif b0 = et_I1 then PrimaryAssemblyILGlobals.typ_SByte, sigptr
-    elif b0 = et_I2 then PrimaryAssemblyILGlobals.typ_Int16, sigptr
-    elif b0 = et_I4 then PrimaryAssemblyILGlobals.typ_Int32, sigptr
-    elif b0 = et_I8 then PrimaryAssemblyILGlobals.typ_Int64, sigptr
-    elif b0 = et_I then PrimaryAssemblyILGlobals.typ_IntPtr, sigptr
-    elif b0 = et_U1 then PrimaryAssemblyILGlobals.typ_Byte, sigptr
-    elif b0 = et_U2 then PrimaryAssemblyILGlobals.typ_UInt16, sigptr
-    elif b0 = et_U4 then PrimaryAssemblyILGlobals.typ_UInt32, sigptr
-    elif b0 = et_U8 then PrimaryAssemblyILGlobals.typ_UInt64, sigptr
-    elif b0 = et_U then PrimaryAssemblyILGlobals.typ_UIntPtr, sigptr
-    elif b0 = et_R4 then PrimaryAssemblyILGlobals.typ_Single, sigptr
-    elif b0 = et_R8 then PrimaryAssemblyILGlobals.typ_Double, sigptr
-    elif b0 = et_CHAR then PrimaryAssemblyILGlobals.typ_Char, sigptr
-    elif b0 = et_BOOLEAN then PrimaryAssemblyILGlobals.typ_Bool, sigptr
+
+    if b0 = et_OBJECT then
+        PrimaryAssemblyILGlobals.typ_Object, sigptr
+    elif b0 = et_STRING then
+        PrimaryAssemblyILGlobals.typ_String, sigptr
+    elif b0 = et_I1 then
+        PrimaryAssemblyILGlobals.typ_SByte, sigptr
+    elif b0 = et_I2 then
+        PrimaryAssemblyILGlobals.typ_Int16, sigptr
+    elif b0 = et_I4 then
+        PrimaryAssemblyILGlobals.typ_Int32, sigptr
+    elif b0 = et_I8 then
+        PrimaryAssemblyILGlobals.typ_Int64, sigptr
+    elif b0 = et_I then
+        PrimaryAssemblyILGlobals.typ_IntPtr, sigptr
+    elif b0 = et_U1 then
+        PrimaryAssemblyILGlobals.typ_Byte, sigptr
+    elif b0 = et_U2 then
+        PrimaryAssemblyILGlobals.typ_UInt16, sigptr
+    elif b0 = et_U4 then
+        PrimaryAssemblyILGlobals.typ_UInt32, sigptr
+    elif b0 = et_U8 then
+        PrimaryAssemblyILGlobals.typ_UInt64, sigptr
+    elif b0 = et_U then
+        PrimaryAssemblyILGlobals.typ_UIntPtr, sigptr
+    elif b0 = et_R4 then
+        PrimaryAssemblyILGlobals.typ_Single, sigptr
+    elif b0 = et_R8 then
+        PrimaryAssemblyILGlobals.typ_Double, sigptr
+    elif b0 = et_CHAR then
+        PrimaryAssemblyILGlobals.typ_Char, sigptr
+    elif b0 = et_BOOLEAN then
+        PrimaryAssemblyILGlobals.typ_Bool, sigptr
     elif b0 = et_WITH then
         let b0, sigptr = sigptrGetByte bytes sigptr
         let tdorIdx, sigptr = sigptrGetTypeDefOrRefOrSpecIdx bytes sigptr
         let struct (n, sigptr) = sigptrGetZInt32 bytes sigptr
         let argTys, sigptr = sigptrFold (sigptrGetTy ctxt numTypars) n bytes sigptr
-        seekReadTypeDefOrRef ctxt numTypars (if b0 = et_CLASS then AsObject else AsValue) argTys tdorIdx,
+
+        seekReadTypeDefOrRef
+            ctxt
+            numTypars
+            (if b0 = et_CLASS then
+                 AsObject
+             else
+                 AsValue)
+            argTys
+            tdorIdx,
         sigptr
 
     elif b0 = et_CLASS then
@@ -2009,10 +2541,10 @@ and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
         seekReadTypeDefOrRef ctxt numTypars AsValue List.empty tdorIdx, sigptr
     elif b0 = et_VAR then
         let struct (n, sigptr) = sigptrGetZInt32 bytes sigptr
-        ILType.TypeVar (uint16 n), sigptr
+        ILType.TypeVar(uint16 n), sigptr
     elif b0 = et_MVAR then
         let struct (n, sigptr) = sigptrGetZInt32 bytes sigptr
-        ILType.TypeVar (uint16 (n + numTypars)), sigptr
+        ILType.TypeVar(uint16 (n + numTypars)), sigptr
     elif b0 = et_BYREF then
         let ty, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
         ILType.Byref ty, sigptr
@@ -2028,15 +2560,27 @@ and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
         let struct (numSized, sigptr) = sigptrGetZInt32 bytes sigptr
         let struct (sizes, sigptr) = sigptrFoldStruct sigptrGetZInt32 numSized bytes sigptr
         let struct (numLoBounded, sigptr) = sigptrGetZInt32 bytes sigptr
-        let struct (lobounds, sigptr) = sigptrFoldStruct sigptrGetZInt32 numLoBounded bytes sigptr
+
+        let struct (lobounds, sigptr) =
+            sigptrFoldStruct sigptrGetZInt32 numLoBounded bytes sigptr
+
         let shape =
             let dim i =
-              (if i < numLoBounded then Some (List.item i lobounds) else None),
-              (if i < numSized then Some (List.item i sizes) else None)
-            ILArrayShape (List.init rank dim)
+                (if i < numLoBounded then
+                     Some(List.item i lobounds)
+                 else
+                     None),
+                (if i < numSized then
+                     Some(List.item i sizes)
+                 else
+                     None)
+
+            ILArrayShape(List.init rank dim)
+
         mkILArrTy (ty, shape), sigptr
 
-    elif b0 = et_VOID then ILType.Void, sigptr
+    elif b0 = et_VOID then
+        ILType.Void, sigptr
     elif b0 = et_TYPEDBYREF then
         PrimaryAssemblyILGlobals.typ_TypedReference, sigptr
     elif b0 = et_CMOD_REQD || b0 = et_CMOD_OPT then
@@ -2046,46 +2590,66 @@ and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
     elif b0 = et_FNPTR then
         let ccByte, sigptr = sigptrGetByte bytes sigptr
         let generic, cc = byteAsCallConv ccByte
-        if generic then failwith "fptr sig may not be generic"
+
+        if generic then
+            failwith "fptr sig may not be generic"
+
         let struct (numparams, sigptr) = sigptrGetZInt32 bytes sigptr
         let retTy, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
         let argTys, sigptr = sigptrFold (sigptrGetTy ctxt numTypars) numparams bytes sigptr
+
         let typ =
             ILType.FunctionPointer
-              { CallingConv=cc
-                ArgTypes = argTys
-                ReturnType=retTy }
+                {
+                    CallingConv = cc
+                    ArgTypes = argTys
+                    ReturnType = retTy
+                }
+
         typ, sigptr
-    elif b0 = et_SENTINEL then failwith "varargs NYI"
-    else ILType.Void, sigptr
+    elif b0 = et_SENTINEL then
+        failwith "varargs NYI"
+    else
+        ILType.Void, sigptr
 
 and sigptrGetVarArgTys (ctxt: ILMetadataReader) n numTypars bytes sigptr =
     sigptrFold (sigptrGetTy ctxt numTypars) n bytes sigptr
 
 and sigptrGetArgTys (ctxt: ILMetadataReader) n numTypars bytes sigptr acc =
-    if n <= 0 then (List.rev acc, None), sigptr
+    if n <= 0 then
+        (List.rev acc, None), sigptr
     else
-      let b0, sigptr2 = sigptrGetByte bytes sigptr
-      if b0 = et_SENTINEL then
-        let varargs, sigptr = sigptrGetVarArgTys ctxt n numTypars bytes sigptr2
-        (List.rev acc, Some varargs), sigptr
-      else
-        let x, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
-        sigptrGetArgTys ctxt (n-1) numTypars bytes sigptr (x :: acc)
+        let b0, sigptr2 = sigptrGetByte bytes sigptr
+
+        if b0 = et_SENTINEL then
+            let varargs, sigptr = sigptrGetVarArgTys ctxt n numTypars bytes sigptr2
+            (List.rev acc, Some varargs), sigptr
+        else
+            let x, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
+            sigptrGetArgTys ctxt (n - 1) numTypars bytes sigptr (x :: acc)
 
 and sigptrGetLocal (ctxt: ILMetadataReader) numTypars bytes sigptr =
     let pinned, sigptr =
         let b0, sigptr' = sigptrGetByte bytes sigptr
+
         if b0 = et_PINNED then
             true, sigptr'
         else
             false, sigptr
+
     let ty, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
-    let loc: ILLocal = { IsPinned = pinned; Type = ty; DebugInfo = None }
+
+    let loc: ILLocal =
+        {
+            IsPinned = pinned
+            Type = ty
+            DebugInfo = None
+        }
+
     loc, sigptr
 
 and readBlobHeapAsMethodSig (ctxt: ILMetadataReader) numTypars blobIdx =
-    ctxt.readBlobHeapAsMethodSig (BlobAsMethodSigIdx (numTypars, blobIdx))
+    ctxt.readBlobHeapAsMethodSig (BlobAsMethodSigIdx(numTypars, blobIdx))
 
 and readBlobHeapAsMethodSigUncached ctxtH (BlobAsMethodSigIdx (numTypars, blobIdx)) =
     let (ctxt: ILMetadataReader) = getHole ctxtH
@@ -2093,10 +2657,19 @@ and readBlobHeapAsMethodSigUncached ctxtH (BlobAsMethodSigIdx (numTypars, blobId
     let sigptr = 0
     let ccByte, sigptr = sigptrGetByte bytes sigptr
     let generic, cc = byteAsCallConv ccByte
-    let struct (genarity, sigptr) = if generic then sigptrGetZInt32 bytes sigptr else 0x0, sigptr
+
+    let struct (genarity, sigptr) =
+        if generic then
+            sigptrGetZInt32 bytes sigptr
+        else
+            0x0, sigptr
+
     let struct (numparams, sigptr) = sigptrGetZInt32 bytes sigptr
     let retTy, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
-    let (argTys, varargs), _sigptr = sigptrGetArgTys ctxt numparams numTypars bytes sigptr []
+
+    let (argTys, varargs), _sigptr =
+        sigptrGetArgTys ctxt numparams numTypars bytes sigptr []
+
     generic, genarity, cc, retTy, argTys, varargs
 
 and readBlobHeapAsType ctxt numTypars blobIdx =
@@ -2105,20 +2678,22 @@ and readBlobHeapAsType ctxt numTypars blobIdx =
     ty
 
 and readBlobHeapAsFieldSig ctxt numTypars blobIdx =
-    ctxt.readBlobHeapAsFieldSig (BlobAsFieldSigIdx (numTypars, blobIdx))
+    ctxt.readBlobHeapAsFieldSig (BlobAsFieldSigIdx(numTypars, blobIdx))
 
 and readBlobHeapAsFieldSigUncached ctxtH (BlobAsFieldSigIdx (numTypars, blobIdx)) =
     let ctxt = getHole ctxtH
     let bytes = readBlobHeap ctxt blobIdx
     let sigptr = 0
     let ccByte, sigptr = sigptrGetByte bytes sigptr
-    if ccByte <> e_IMAGE_CEE_CS_CALLCONV_FIELD then dprintn "warning: field sig was not CC_FIELD"
+
+    if ccByte <> e_IMAGE_CEE_CS_CALLCONV_FIELD then
+        dprintn "warning: field sig was not CC_FIELD"
+
     let retTy, _sigptr = sigptrGetTy ctxt numTypars bytes sigptr
     retTy
 
-
 and readBlobHeapAsPropertySig (ctxt: ILMetadataReader) numTypars blobIdx =
-    ctxt.readBlobHeapAsPropertySig (BlobAsPropSigIdx (numTypars, blobIdx))
+    ctxt.readBlobHeapAsPropertySig (BlobAsPropSigIdx(numTypars, blobIdx))
 
 and readBlobHeapAsPropertySigUncached ctxtH (BlobAsPropSigIdx (numTypars, blobIdx)) =
     let ctxt = getHole ctxtH
@@ -2127,45 +2702,66 @@ and readBlobHeapAsPropertySigUncached ctxtH (BlobAsPropSigIdx (numTypars, blobId
     let ccByte, sigptr = sigptrGetByte bytes sigptr
     let hasthis = byteAsHasThis ccByte
     let ccMaxked = (ccByte &&& 0x0Fuy)
-    if ccMaxked <> e_IMAGE_CEE_CS_CALLCONV_PROPERTY then dprintn ("warning: property sig was "+string ccMaxked+" instead of CC_PROPERTY")
+
+    if ccMaxked <> e_IMAGE_CEE_CS_CALLCONV_PROPERTY then
+        dprintn ("warning: property sig was " + string ccMaxked + " instead of CC_PROPERTY")
+
     let struct (numparams, sigptr) = sigptrGetZInt32 bytes sigptr
     let retTy, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
     let argTys, _sigptr = sigptrFold (sigptrGetTy ctxt numTypars) numparams bytes sigptr
     hasthis, retTy, argTys
 
 and readBlobHeapAsLocalsSig (ctxt: ILMetadataReader) numTypars blobIdx =
-    ctxt.readBlobHeapAsLocalsSig (BlobAsLocalSigIdx (numTypars, blobIdx))
+    ctxt.readBlobHeapAsLocalsSig (BlobAsLocalSigIdx(numTypars, blobIdx))
 
 and readBlobHeapAsLocalsSigUncached ctxtH (BlobAsLocalSigIdx (numTypars, blobIdx)) =
     let ctxt = getHole ctxtH
     let bytes = readBlobHeap ctxt blobIdx
     let sigptr = 0
     let ccByte, sigptr = sigptrGetByte bytes sigptr
-    if ccByte <> e_IMAGE_CEE_CS_CALLCONV_LOCAL_SIG then dprintn "warning: local sig was not CC_LOCAL"
+
+    if ccByte <> e_IMAGE_CEE_CS_CALLCONV_LOCAL_SIG then
+        dprintn "warning: local sig was not CC_LOCAL"
+
     let struct (numlocals, sigptr) = sigptrGetZInt32 bytes sigptr
-    let localtys, _sigptr = sigptrFold (sigptrGetLocal ctxt numTypars) numlocals bytes sigptr
+
+    let localtys, _sigptr =
+        sigptrFold (sigptrGetLocal ctxt numTypars) numlocals bytes sigptr
+
     localtys
 
 and byteAsHasThis b =
     let hasthis_masked = b &&& 0x60uy
-    if hasthis_masked = e_IMAGE_CEE_CS_CALLCONV_INSTANCE then ILThisConvention.Instance
-    elif hasthis_masked = e_IMAGE_CEE_CS_CALLCONV_INSTANCE_EXPLICIT then ILThisConvention.InstanceExplicit
-    else ILThisConvention.Static
+
+    if hasthis_masked = e_IMAGE_CEE_CS_CALLCONV_INSTANCE then
+        ILThisConvention.Instance
+    elif hasthis_masked = e_IMAGE_CEE_CS_CALLCONV_INSTANCE_EXPLICIT then
+        ILThisConvention.InstanceExplicit
+    else
+        ILThisConvention.Static
 
 and byteAsCallConv b =
     let cc =
         let ccMaxked = b &&& 0x0Fuy
-        if ccMaxked = e_IMAGE_CEE_CS_CALLCONV_FASTCALL then ILArgConvention.FastCall
-        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_STDCALL then ILArgConvention.StdCall
-        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_THISCALL then ILArgConvention.ThisCall
-        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_CDECL then ILArgConvention.CDecl
-        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_VARARG then ILArgConvention.VarArg
-        else ILArgConvention.Default
+
+        if ccMaxked = e_IMAGE_CEE_CS_CALLCONV_FASTCALL then
+            ILArgConvention.FastCall
+        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_STDCALL then
+            ILArgConvention.StdCall
+        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_THISCALL then
+            ILArgConvention.ThisCall
+        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_CDECL then
+            ILArgConvention.CDecl
+        elif ccMaxked = e_IMAGE_CEE_CS_CALLCONV_VARARG then
+            ILArgConvention.VarArg
+        else
+            ILArgConvention.Default
+
     let generic = (b &&& e_IMAGE_CEE_CS_CALLCONV_GENERIC) <> 0x0uy
-    generic, Callconv (byteAsHasThis b, cc)
+    generic, Callconv(byteAsHasThis b, cc)
 
-and seekReadMemberRefAsMethodData ctxt numTypars idx: VarArgMethodData =
-    ctxt.seekReadMemberRefAsMethodData (MemberRefAsMspecIdx (numTypars, idx))
+and seekReadMemberRefAsMethodData ctxt numTypars idx : VarArgMethodData =
+    ctxt.seekReadMemberRefAsMethodData (MemberRefAsMspecIdx(numTypars, idx))
 
 and seekReadMemberRefAsMethodDataUncached ctxtH (MemberRefAsMspecIdx (numTypars, idx)) =
     let (ctxt: ILMetadataReader) = getHole ctxtH
@@ -2173,44 +2769,62 @@ and seekReadMemberRefAsMethodDataUncached ctxtH (MemberRefAsMspecIdx (numTypars,
     let mrpIdx, nameIdx, typeIdx = seekReadMemberRefRow ctxt mdv idx
     let nm = readStringHeap ctxt nameIdx
     let enclTy = seekReadMethodRefParent ctxt mdv numTypars mrpIdx
-    let _generic, genarity, cc, retTy, argTys, varargs = readBlobHeapAsMethodSig ctxt enclTy.GenericArgs.Length typeIdx
-    let methInst = List.init genarity (fun n -> mkILTyvarTy (uint16 (numTypars+n)))
+
+    let _generic, genarity, cc, retTy, argTys, varargs =
+        readBlobHeapAsMethodSig ctxt enclTy.GenericArgs.Length typeIdx
+
+    let methInst = List.init genarity (fun n -> mkILTyvarTy (uint16 (numTypars + n)))
     (VarArgMethodData(enclTy, cc, nm, argTys, varargs, retTy, methInst))
 
-and seekReadMemberRefAsMethDataNoVarArgs ctxt numTypars idx: MethodData =
-   let (VarArgMethodData(enclTy, cc, nm, argTys, varargs, retTy, methInst)) = seekReadMemberRefAsMethodData ctxt numTypars idx
-   if Option.isSome varargs then dprintf "ignoring sentinel and varargs in ILMethodDef token signature"
-   (MethodData(enclTy, cc, nm, argTys, retTy, methInst))
+and seekReadMemberRefAsMethDataNoVarArgs ctxt numTypars idx : MethodData =
+    let (VarArgMethodData (enclTy, cc, nm, argTys, varargs, retTy, methInst)) =
+        seekReadMemberRefAsMethodData ctxt numTypars idx
+
+    if Option.isSome varargs then
+        dprintf "ignoring sentinel and varargs in ILMethodDef token signature"
+
+    (MethodData(enclTy, cc, nm, argTys, retTy, methInst))
 
 and seekReadMethodSpecAsMethodData (ctxt: ILMetadataReader) numTypars idx =
-    ctxt.seekReadMethodSpecAsMethodData (MethodSpecAsMspecIdx (numTypars, idx))
+    ctxt.seekReadMethodSpecAsMethodData (MethodSpecAsMspecIdx(numTypars, idx))
 
 and seekReadMethodSpecAsMethodDataUncached ctxtH (MethodSpecAsMspecIdx (numTypars, idx)) =
     let (ctxt: ILMetadataReader) = getHole ctxtH
     let mdv = ctxt.mdfile.GetView()
     let mdorIdx, instIdx = seekReadMethodSpecRow ctxt mdv idx
-    let (VarArgMethodData(enclTy, cc, nm, argTys, varargs, retTy, _)) = seekReadMethodDefOrRef ctxt numTypars mdorIdx
+
+    let (VarArgMethodData (enclTy, cc, nm, argTys, varargs, retTy, _)) =
+        seekReadMethodDefOrRef ctxt numTypars mdorIdx
+
     let methInst =
         let bytes = readBlobHeap ctxt instIdx
         let sigptr = 0
         let ccByte, sigptr = sigptrGetByte bytes sigptr
-        if ccByte <> e_IMAGE_CEE_CS_CALLCONV_GENERICINST then dprintn ("warning: method inst ILCallingConv was "+string ccByte+" instead of CC_GENERICINST")
+
+        if ccByte <> e_IMAGE_CEE_CS_CALLCONV_GENERICINST then
+            dprintn (
+                "warning: method inst ILCallingConv was "
+                + string ccByte
+                + " instead of CC_GENERICINST"
+            )
+
         let struct (numgpars, sigptr) = sigptrGetZInt32 bytes sigptr
         let argTys, _sigptr = sigptrFold (sigptrGetTy ctxt numTypars) numgpars bytes sigptr
         argTys
+
     VarArgMethodData(enclTy, cc, nm, argTys, varargs, retTy, methInst)
 
 and seekReadMemberRefAsFieldSpec (ctxt: ILMetadataReader) numTypars idx =
-   ctxt.seekReadMemberRefAsFieldSpec (MemberRefAsFspecIdx (numTypars, idx))
+    ctxt.seekReadMemberRefAsFieldSpec (MemberRefAsFspecIdx(numTypars, idx))
 
 and seekReadMemberRefAsFieldSpecUncached ctxtH (MemberRefAsFspecIdx (numTypars, idx)) =
-   let (ctxt: ILMetadataReader) = getHole ctxtH
-   let mdv = ctxt.mdfile.GetView()
-   let mrpIdx, nameIdx, typeIdx = seekReadMemberRefRow ctxt mdv idx
-   let nm = readStringHeap ctxt nameIdx
-   let enclTy = seekReadMethodRefParent ctxt mdv numTypars mrpIdx
-   let retTy = readBlobHeapAsFieldSig ctxt numTypars typeIdx
-   mkILFieldSpecInTy(enclTy, nm, retTy)
+    let (ctxt: ILMetadataReader) = getHole ctxtH
+    let mdv = ctxt.mdfile.GetView()
+    let mrpIdx, nameIdx, typeIdx = seekReadMemberRefRow ctxt mdv idx
+    let nm = readStringHeap ctxt nameIdx
+    let enclTy = seekReadMethodRefParent ctxt mdv numTypars mrpIdx
+    let retTy = readBlobHeapAsFieldSig ctxt numTypars typeIdx
+    mkILFieldSpecInTy (enclTy, nm, retTy)
 
 // One extremely annoying aspect of the MD format is that given a
 // ILMethodDef token it is non-trivial to find which ILTypeDef it belongs
@@ -2218,192 +2832,264 @@ and seekReadMemberRefAsFieldSpecUncached ctxtH (MemberRefAsFspecIdx (numTypars,
 // looking for which ILTypeDef has the ILMethodDef within its range.
 // Although the ILTypeDef table is not "sorted", it is effectively sorted by
 // method-range and field-range start/finish indexes
-and seekReadMethodDefAsMethodData ctxt idx =
-   ctxt.seekReadMethodDefAsMethodData idx
+and seekReadMethodDefAsMethodData ctxt idx = ctxt.seekReadMethodDefAsMethodData idx
 
 and seekReadMethodDefAsMethodDataUncached ctxtH idx =
-   let (ctxt: ILMetadataReader) = getHole ctxtH
-   let mdv = ctxt.mdfile.GetView()
-   // Look for the method def parent.
-   let tidx =
-     seekReadIndexedRow (ctxt.getNumRows TableNames.TypeDef,
-                            (fun i -> i, seekReadTypeDefRowWithExtents ctxt i),
-                            (fun r -> r),
-                            (fun (_, ((_, _, _, _, _, methodsIdx),
-                                      (_, endMethodsIdx))) ->
-                                        if endMethodsIdx <= idx then 1
-                                        elif methodsIdx <= idx && idx < endMethodsIdx then 0
-                                        else -1),
-                            true, fst)
-   // Create a formal instantiation if needed
-   let typeGenericArgs = seekReadGenericParams ctxt 0 (tomd_TypeDef, tidx)
-   let typeGenericArgsCount = typeGenericArgs.Length
+    let (ctxt: ILMetadataReader) = getHole ctxtH
+    let mdv = ctxt.mdfile.GetView()
+    // Look for the method def parent.
+    let tidx =
+        seekReadIndexedRow (
+            ctxt.getNumRows TableNames.TypeDef,
+            (fun i -> i, seekReadTypeDefRowWithExtents ctxt i),
+            (fun r -> r),
+            (fun (_, ((_, _, _, _, _, methodsIdx), (_, endMethodsIdx))) ->
+                if endMethodsIdx <= idx then
+                    1
+                elif methodsIdx <= idx && idx < endMethodsIdx then
+                    0
+                else
+                    -1),
+            true,
+            fst
+        )
+    // Create a formal instantiation if needed
+    let typeGenericArgs = seekReadGenericParams ctxt 0 (tomd_TypeDef, tidx)
+    let typeGenericArgsCount = typeGenericArgs.Length
 
-   let methodGenericArgs = seekReadGenericParams ctxt typeGenericArgsCount (tomd_MethodDef, idx)
+    let methodGenericArgs =
+        seekReadGenericParams ctxt typeGenericArgsCount (tomd_MethodDef, idx)
 
-   let finst = mkILFormalGenericArgs 0 typeGenericArgs
-   let methInst = mkILFormalGenericArgs typeGenericArgsCount methodGenericArgs
+    let finst = mkILFormalGenericArgs 0 typeGenericArgs
+    let methInst = mkILFormalGenericArgs typeGenericArgsCount methodGenericArgs
 
-   // Read the method def parent.
-   let enclTy = seekReadTypeDefAsType ctxt AsObject (* not ok: see note *) finst tidx
+    // Read the method def parent.
+    let enclTy = seekReadTypeDefAsType ctxt AsObject (* not ok: see note *) finst tidx
 
-   // Return the constituent parts: put it together at the place where this is called.
-   let _code_rva, _implflags, _flags, nameIdx, typeIdx, _paramIdx = seekReadMethodRow ctxt mdv idx
-   let nm = readStringHeap ctxt nameIdx
+    // Return the constituent parts: put it together at the place where this is called.
+    let _code_rva, _implflags, _flags, nameIdx, typeIdx, _paramIdx =
+        seekReadMethodRow ctxt mdv idx
+
+    let nm = readStringHeap ctxt nameIdx
 
-   // Read the method def signature.
-   let _generic, _genarity, cc, retTy, argTys, varargs = readBlobHeapAsMethodSig ctxt typeGenericArgsCount typeIdx
-   if varargs <> None then dprintf "ignoring sentinel and varargs in ILMethodDef token signature"
+    // Read the method def signature.
+    let _generic, _genarity, cc, retTy, argTys, varargs =
+        readBlobHeapAsMethodSig ctxt typeGenericArgsCount typeIdx
 
-   MethodData(enclTy, cc, nm, argTys, retTy, methInst)
+    if varargs <> None then
+        dprintf "ignoring sentinel and varargs in ILMethodDef token signature"
 
+    MethodData(enclTy, cc, nm, argTys, retTy, methInst)
 
-and seekReadFieldDefAsFieldSpec (ctxt: ILMetadataReader) idx =
-   ctxt.seekReadFieldDefAsFieldSpec idx
+and seekReadFieldDefAsFieldSpec (ctxt: ILMetadataReader) idx = ctxt.seekReadFieldDefAsFieldSpec idx
 
 and seekReadFieldDefAsFieldSpecUncached ctxtH idx =
-   let (ctxt: ILMetadataReader) = getHole ctxtH
-   let mdv = ctxt.mdfile.GetView()
-   let _flags, nameIdx, typeIdx = seekReadFieldRow ctxt mdv idx
-   let nm = readStringHeap ctxt nameIdx
-   (* Look for the field def parent. *)
-   let tidx =
-     seekReadIndexedRow (ctxt.getNumRows TableNames.TypeDef,
-                            (fun i -> i, seekReadTypeDefRowWithExtents ctxt i),
-                            (fun r -> r),
-                            (fun (_, ((_, _, _, _, fieldsIdx, _), (endFieldsIdx, _))) ->
-                                if endFieldsIdx <= idx then 1
-                                elif fieldsIdx <= idx && idx < endFieldsIdx then 0
-                                else -1),
-                            true, fst)
-   // Read the field signature.
-   let retTy = readBlobHeapAsFieldSig ctxt 0 typeIdx
-
-   // Create a formal instantiation if needed
-   let finst = mkILFormalGenericArgs 0 (seekReadGenericParams ctxt 0 (tomd_TypeDef, tidx))
-
-   // Read the field def parent.
-   let enclTy = seekReadTypeDefAsType ctxt AsObject (* not ok: see note *) finst tidx
-
-   // Put it together.
-   mkILFieldSpecInTy(enclTy, nm, retTy)
+    let (ctxt: ILMetadataReader) = getHole ctxtH
+    let mdv = ctxt.mdfile.GetView()
+    let _flags, nameIdx, typeIdx = seekReadFieldRow ctxt mdv idx
+    let nm = readStringHeap ctxt nameIdx
+    (* Look for the field def parent. *)
+    let tidx =
+        seekReadIndexedRow (
+            ctxt.getNumRows TableNames.TypeDef,
+            (fun i -> i, seekReadTypeDefRowWithExtents ctxt i),
+            (fun r -> r),
+            (fun (_, ((_, _, _, _, fieldsIdx, _), (endFieldsIdx, _))) ->
+                if endFieldsIdx <= idx then
+                    1
+                elif fieldsIdx <= idx && idx < endFieldsIdx then
+                    0
+                else
+                    -1),
+            true,
+            fst
+        )
+    // Read the field signature.
+    let retTy = readBlobHeapAsFieldSig ctxt 0 typeIdx
+
+    // Create a formal instantiation if needed
+    let finst =
+        mkILFormalGenericArgs 0 (seekReadGenericParams ctxt 0 (tomd_TypeDef, tidx))
+
+    // Read the field def parent.
+    let enclTy = seekReadTypeDefAsType ctxt AsObject (* not ok: see note *) finst tidx
+
+    // Put it together.
+    mkILFieldSpecInTy (enclTy, nm, retTy)
 
 and seekReadMethod (ctxt: ILMetadataReader) mdv numTypars (idx: int) =
-     let codeRVA, implflags, flags, nameIdx, typeIdx, paramIdx = seekReadMethodRow ctxt mdv idx
-     let nm = readStringHeap ctxt nameIdx
-     let abstr = (flags &&& 0x0400) <> 0x0
-     let pinvoke = (flags &&& 0x2000) <> 0x0
-     let codetype = implflags &&& 0x0003
-     let unmanaged = (implflags &&& 0x0004) <> 0x0
-     let internalcall = (implflags &&& 0x1000) <> 0x0
-     let noinline = (implflags &&& 0x0008) <> 0x0
-     let aggressiveinline = (implflags &&& 0x0100) <> 0x0
-     let _generic, _genarity, cc, retTy, argTys, varargs = readBlobHeapAsMethodSig ctxt numTypars typeIdx
-     if varargs <> None then dprintf "ignoring sentinel and varargs in ILMethodDef signature"
-
-     let endParamIdx =
-       if idx >= ctxt.getNumRows TableNames.Method then
-         ctxt.getNumRows TableNames.Param + 1
-       else
-         let _, _, _, _, _, paramIdx = seekReadMethodRow ctxt mdv (idx + 1)
-         paramIdx
-
-     let ret, ilParams = seekReadParams ctxt mdv (retTy, argTys) paramIdx endParamIdx
-
-     let isEntryPoint =
-         let tab, tok = ctxt.entryPointToken
-         (tab = TableNames.Method && tok = idx)
-
-     let body =
-         if (codetype = 0x01) && pinvoke then
-             methBodyNative
-         elif pinvoke then
-             seekReadImplMap ctxt nm idx
-         elif internalcall || abstr || unmanaged || (codetype <> 0x00) then
-             methBodyAbstract
-         else
-             match ctxt.pectxtCaptured with
-             | None -> methBodyNotAvailable
-             | Some pectxt -> seekReadMethodRVA pectxt ctxt (idx, nm, internalcall, noinline, aggressiveinline, numTypars) codeRVA
-
-     ILMethodDef(name=nm,
-                 attributes = enum(flags),
-                 implAttributes= enum(implflags),
-                 securityDeclsStored=ctxt.securityDeclsReader_MethodDef,
-                 isEntryPoint=isEntryPoint,
-                 genericParams=seekReadGenericParams ctxt numTypars (tomd_MethodDef, idx),
-                 parameters= ilParams,
-                 callingConv=cc,
-                 ret=ret,
-                 body=body,
-                 customAttrsStored=ctxt.customAttrsReader_MethodDef,
-                 metadataIndex=idx)
+    let codeRVA, implflags, flags, nameIdx, typeIdx, paramIdx =
+        seekReadMethodRow ctxt mdv idx
 
+    let nm = readStringHeap ctxt nameIdx
+    let abstr = (flags &&& 0x0400) <> 0x0
+    let pinvoke = (flags &&& 0x2000) <> 0x0
+    let codetype = implflags &&& 0x0003
+    let unmanaged = (implflags &&& 0x0004) <> 0x0
+    let internalcall = (implflags &&& 0x1000) <> 0x0
+    let noinline = (implflags &&& 0x0008) <> 0x0
+    let aggressiveinline = (implflags &&& 0x0100) <> 0x0
+
+    let _generic, _genarity, cc, retTy, argTys, varargs =
+        readBlobHeapAsMethodSig ctxt numTypars typeIdx
+
+    if varargs <> None then
+        dprintf "ignoring sentinel and varargs in ILMethodDef signature"
+
+    let endParamIdx =
+        if idx >= ctxt.getNumRows TableNames.Method then
+            ctxt.getNumRows TableNames.Param + 1
+        else
+            let _, _, _, _, _, paramIdx = seekReadMethodRow ctxt mdv (idx + 1)
+            paramIdx
+
+    let ret, ilParams = seekReadParams ctxt mdv (retTy, argTys) paramIdx endParamIdx
+
+    let isEntryPoint =
+        let tab, tok = ctxt.entryPointToken
+        (tab = TableNames.Method && tok = idx)
+
+    let body =
+        if (codetype = 0x01) && pinvoke then
+            methBodyNative
+        elif pinvoke then
+            seekReadImplMap ctxt nm idx
+        elif internalcall || abstr || unmanaged || (codetype <> 0x00) then
+            methBodyAbstract
+        else
+            match ctxt.pectxtCaptured with
+            | None -> methBodyNotAvailable
+            | Some pectxt -> seekReadMethodRVA pectxt ctxt (idx, nm, internalcall, noinline, aggressiveinline, numTypars) codeRVA
+
+    ILMethodDef(
+        name = nm,
+        attributes = enum (flags),
+        implAttributes = enum (implflags),
+        securityDeclsStored = ctxt.securityDeclsReader_MethodDef,
+        isEntryPoint = isEntryPoint,
+        genericParams = seekReadGenericParams ctxt numTypars (tomd_MethodDef, idx),
+        parameters = ilParams,
+        callingConv = cc,
+        ret = ret,
+        body = body,
+        customAttrsStored = ctxt.customAttrsReader_MethodDef,
+        metadataIndex = idx
+    )
 
 and seekReadParams (ctxt: ILMetadataReader) mdv (retTy, argTys) pidx1 pidx2 =
     let mutable retRes = mkILReturn retTy
     let paramsRes = argTys |> List.toArray |> Array.map mkILParamAnon
+
     for i = pidx1 to pidx2 - 1 do
         seekReadParamExtras ctxt mdv (&retRes, paramsRes) i
+
     retRes, List.ofArray paramsRes
 
 and seekReadParamExtras (ctxt: ILMetadataReader) mdv (retRes: byref, paramsRes) (idx: int) =
-   let flags, seq, nameIdx = seekReadParamRow ctxt mdv idx
-   let inOutMasked = (flags &&& 0x00FF)
-   let hasMarshal = (flags &&& 0x2000) <> 0x0
-   let hasDefault = (flags &&& 0x1000) <> 0x0
-   let fmReader idx = seekReadIndexedRow (ctxt.getNumRows TableNames.FieldMarshal, seekReadFieldMarshalRow ctxt mdv, fst, hfmCompare idx, isSorted ctxt TableNames.FieldMarshal, (snd >> readBlobHeapAsNativeType ctxt))
-   if seq = 0 then
-       retRes <- { retRes with
-                        Marshal=(if hasMarshal then Some (fmReader (TaggedIndex(hfm_ParamDef, idx))) else None)
-                        CustomAttrsStored = ctxt.customAttrsReader_ParamDef
-                        MetadataIndex = idx}
-   elif seq > Array.length paramsRes then dprintn "bad seq num. for param"
-   else
-       paramsRes[seq - 1] <-
-          { paramsRes[seq - 1] with
-               Marshal=(if hasMarshal then Some (fmReader (TaggedIndex(hfm_ParamDef, idx))) else None)
-               Default = (if hasDefault then Some (seekReadConstant ctxt (TaggedIndex(hc_ParamDef, idx))) else None)
-               Name = readStringHeapOption ctxt nameIdx
-               IsIn = ((inOutMasked &&& 0x0001) <> 0x0)
-               IsOut = ((inOutMasked &&& 0x0002) <> 0x0)
-               IsOptional = ((inOutMasked &&& 0x0010) <> 0x0)
-               CustomAttrsStored = ctxt.customAttrsReader_ParamDef
-               MetadataIndex = idx }
+    let flags, seq, nameIdx = seekReadParamRow ctxt mdv idx
+    let inOutMasked = (flags &&& 0x00FF)
+    let hasMarshal = (flags &&& 0x2000) <> 0x0
+    let hasDefault = (flags &&& 0x1000) <> 0x0
+
+    let fmReader idx =
+        seekReadIndexedRow (
+            ctxt.getNumRows TableNames.FieldMarshal,
+            seekReadFieldMarshalRow ctxt mdv,
+            fst,
+            hfmCompare idx,
+            isSorted ctxt TableNames.FieldMarshal,
+            (snd >> readBlobHeapAsNativeType ctxt)
+        )
+
+    if seq = 0 then
+        retRes <-
+            { retRes with
+                Marshal =
+                    (if hasMarshal then
+                         Some(fmReader (TaggedIndex(hfm_ParamDef, idx)))
+                     else
+                         None)
+                CustomAttrsStored = ctxt.customAttrsReader_ParamDef
+                MetadataIndex = idx
+            }
+    elif seq > Array.length paramsRes then
+        dprintn "bad seq num. for param"
+    else
+        paramsRes[seq - 1] <-
+            { paramsRes[seq - 1] with
+                Marshal =
+                    (if hasMarshal then
+                         Some(fmReader (TaggedIndex(hfm_ParamDef, idx)))
+                     else
+                         None)
+                Default =
+                    (if hasDefault then
+                         Some(seekReadConstant ctxt (TaggedIndex(hc_ParamDef, idx)))
+                     else
+                         None)
+                Name = readStringHeapOption ctxt nameIdx
+                IsIn = ((inOutMasked &&& 0x0001) <> 0x0)
+                IsOut = ((inOutMasked &&& 0x0002) <> 0x0)
+                IsOptional = ((inOutMasked &&& 0x0010) <> 0x0)
+                CustomAttrsStored = ctxt.customAttrsReader_ParamDef
+                MetadataIndex = idx
+            }
 
 and seekReadMethodImpls (ctxt: ILMetadataReader) numTypars tidx =
-   mkILMethodImplsLazy
-      (lazy
-          let mdv = ctxt.mdfile.GetView()
-          let mimpls = seekReadIndexedRows (ctxt.getNumRows TableNames.MethodImpl, seekReadMethodImplRow ctxt mdv, (fun (a, _, _) -> a), simpleIndexCompare tidx, isSorted ctxt TableNames.MethodImpl, (fun (_, b, c) -> b, c))
-          mimpls |> List.map (fun (b, c) ->
-              { OverrideBy=
-                  let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefOrRefNoVarargs ctxt numTypars b
-                  mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
-                Overrides=
-                  let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefOrRefNoVarargs ctxt numTypars c
-                  let mspec = mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
-                  OverridesSpec(mspec.MethodRef, mspec.DeclaringType) }))
+    mkILMethodImplsLazy (
+        lazy
+            let mdv = ctxt.mdfile.GetView()
+
+            let mimpls =
+                seekReadIndexedRows (
+                    ctxt.getNumRows TableNames.MethodImpl,
+                    seekReadMethodImplRow ctxt mdv,
+                    (fun (a, _, _) -> a),
+                    simpleIndexCompare tidx,
+                    isSorted ctxt TableNames.MethodImpl,
+                    (fun (_, b, c) -> b, c)
+                )
+
+            mimpls
+            |> List.map (fun (b, c) ->
+                {
+                    OverrideBy =
+                        let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+                            seekReadMethodDefOrRefNoVarargs ctxt numTypars b
+
+                        mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
+                    Overrides =
+                        let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+                            seekReadMethodDefOrRefNoVarargs ctxt numTypars c
+
+                        let mspec = mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
+                        OverridesSpec(mspec.MethodRef, mspec.DeclaringType)
+                })
+    )
 
 and seekReadMultipleMethodSemantics (ctxt: ILMetadataReader) (flags, id) =
-    seekReadIndexedRows
-      (ctxt.getNumRows TableNames.MethodSemantics,
-       seekReadMethodSemanticsRow ctxt,
-       (fun (_flags, _, c) -> c),
-       hsCompare id,
-       isSorted ctxt TableNames.MethodSemantics,
-       (fun (a, b, _c) ->
-           let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefAsMethodData ctxt b
-           a, (mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)).MethodRef))
+    seekReadIndexedRows (
+        ctxt.getNumRows TableNames.MethodSemantics,
+        seekReadMethodSemanticsRow ctxt,
+        (fun (_flags, _, c) -> c),
+        hsCompare id,
+        isSorted ctxt TableNames.MethodSemantics,
+        (fun (a, b, _c) ->
+            let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+                seekReadMethodDefAsMethodData ctxt b
+
+            a, (mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)).MethodRef)
+    )
     |> List.filter (fun (flags2, _) -> flags = flags2)
     |> List.map snd
 
-
 and seekReadOptionalMethodSemantics ctxt id =
     match seekReadMultipleMethodSemantics ctxt id with
     | [] -> None
-    | [h] -> Some h
-    | h :: _ -> dprintn "multiple method semantics found"; Some h
+    | [ h ] -> Some h
+    | h :: _ ->
+        dprintn "multiple method semantics found"
+        Some h
 
 and seekReadMethodSemantics ctxt id =
     match seekReadOptionalMethodSemantics ctxt id with
@@ -2411,691 +3097,916 @@ and seekReadMethodSemantics ctxt id =
     | Some x -> x
 
 and seekReadEvent ctxt mdv numTypars idx =
-   let flags, nameIdx, typIdx = seekReadEventRow ctxt mdv idx
-   ILEventDef(eventType = seekReadOptionalTypeDefOrRef ctxt numTypars AsObject typIdx,
-              name = readStringHeap ctxt nameIdx,
-              attributes = enum(flags),
-              addMethod= seekReadMethodSemantics ctxt (0x0008, TaggedIndex(hs_Event, idx)),
-              removeMethod=seekReadMethodSemantics ctxt (0x0010, TaggedIndex(hs_Event, idx)),
-              fireMethod=seekReadOptionalMethodSemantics ctxt (0x0020, TaggedIndex(hs_Event, idx)),
-              otherMethods = seekReadMultipleMethodSemantics ctxt (0x0004, TaggedIndex(hs_Event, idx)),
-              customAttrsStored=ctxt.customAttrsReader_Event,
-              metadataIndex = idx )
-
-  (* REVIEW: can substantially reduce numbers of EventMap and PropertyMap reads by first checking if the whole table mdv sorted according to ILTypeDef tokens and then doing a binary chop *)
+    let flags, nameIdx, typIdx = seekReadEventRow ctxt mdv idx
+
+    ILEventDef(
+        eventType = seekReadOptionalTypeDefOrRef ctxt numTypars AsObject typIdx,
+        name = readStringHeap ctxt nameIdx,
+        attributes = enum (flags),
+        addMethod = seekReadMethodSemantics ctxt (0x0008, TaggedIndex(hs_Event, idx)),
+        removeMethod = seekReadMethodSemantics ctxt (0x0010, TaggedIndex(hs_Event, idx)),
+        fireMethod = seekReadOptionalMethodSemantics ctxt (0x0020, TaggedIndex(hs_Event, idx)),
+        otherMethods = seekReadMultipleMethodSemantics ctxt (0x0004, TaggedIndex(hs_Event, idx)),
+        customAttrsStored = ctxt.customAttrsReader_Event,
+        metadataIndex = idx
+    )
+
+(* REVIEW: can substantially reduce numbers of EventMap and PropertyMap reads by first checking if the whole table mdv sorted according to ILTypeDef tokens and then doing a binary chop *)
 and seekReadEvents (ctxt: ILMetadataReader) numTypars tidx =
-   mkILEventsLazy
-      (lazy
-           let mdv = ctxt.mdfile.GetView()
-           match seekReadOptionalIndexedRow (ctxt.getNumRows TableNames.EventMap, (fun i -> i, seekReadEventMapRow ctxt mdv i), (fun (_, row) -> fst row), compare tidx, false, (fun (i, row) -> (i, snd row))) with
-           | None -> []
-           | Some (rowNum, beginEventIdx) ->
-               let endEventIdx =
-                   if rowNum >= ctxt.getNumRows TableNames.EventMap then
-                       ctxt.getNumRows TableNames.Event + 1
-                   else
-                       let _, endEventIdx = seekReadEventMapRow ctxt mdv (rowNum + 1)
-                       endEventIdx
-
-               [ if beginEventIdx > 0 then
-                   for i in beginEventIdx .. endEventIdx - 1 do
-                     yield seekReadEvent ctxt mdv numTypars i ])
+    mkILEventsLazy (
+        lazy
+            let mdv = ctxt.mdfile.GetView()
+
+            match
+                seekReadOptionalIndexedRow
+                    (
+                        ctxt.getNumRows TableNames.EventMap,
+                        (fun i -> i, seekReadEventMapRow ctxt mdv i),
+                        (fun (_, row) -> fst row),
+                        compare tidx,
+                        false,
+                        (fun (i, row) -> (i, snd row))
+                    )
+                with
+            | None -> []
+            | Some (rowNum, beginEventIdx) ->
+                let endEventIdx =
+                    if rowNum >= ctxt.getNumRows TableNames.EventMap then
+                        ctxt.getNumRows TableNames.Event + 1
+                    else
+                        let _, endEventIdx = seekReadEventMapRow ctxt mdv (rowNum + 1)
+                        endEventIdx
+
+                [
+                    if beginEventIdx > 0 then
+                        for i in beginEventIdx .. endEventIdx - 1 do
+                            yield seekReadEvent ctxt mdv numTypars i
+                ]
+    )
 
 and seekReadProperty ctxt mdv numTypars idx =
-   let flags, nameIdx, typIdx = seekReadPropertyRow ctxt mdv idx
-   let cc, retTy, argTys = readBlobHeapAsPropertySig ctxt numTypars typIdx
-   let setter= seekReadOptionalMethodSemantics ctxt (0x0001, TaggedIndex(hs_Property, idx))
-   let getter = seekReadOptionalMethodSemantics ctxt (0x0002, TaggedIndex(hs_Property, idx))
-(* NOTE: the "ThisConv" value on the property is not reliable: better to look on the getter/setter *)
-(* NOTE: e.g. tlbimp on Office msword.olb seems to set this incorrectly *)
-   let cc2 =
-       match getter with
-       | Some mref -> mref.CallingConv.ThisConv
-       | None ->
-           match setter with
-           | Some mref -> mref.CallingConv .ThisConv
-           | None -> cc
-
-   ILPropertyDef(name=readStringHeap ctxt nameIdx,
-                 callingConv = cc2,
-                 attributes = enum(flags),
-                 setMethod=setter,
-                 getMethod=getter,
-                 propertyType=retTy,
-                 init= (if (flags &&& 0x1000) = 0 then None else Some (seekReadConstant ctxt (TaggedIndex(hc_Property, idx)))),
-                 args=argTys,
-                 customAttrsStored=ctxt.customAttrsReader_Property,
-                 metadataIndex = idx )
+    let flags, nameIdx, typIdx = seekReadPropertyRow ctxt mdv idx
+    let cc, retTy, argTys = readBlobHeapAsPropertySig ctxt numTypars typIdx
+
+    let setter =
+        seekReadOptionalMethodSemantics ctxt (0x0001, TaggedIndex(hs_Property, idx))
+
+    let getter =
+        seekReadOptionalMethodSemantics ctxt (0x0002, TaggedIndex(hs_Property, idx))
+    (* NOTE: the "ThisConv" value on the property is not reliable: better to look on the getter/setter *)
+    (* NOTE: e.g. tlbimp on Office msword.olb seems to set this incorrectly *)
+    let cc2 =
+        match getter with
+        | Some mref -> mref.CallingConv.ThisConv
+        | None ->
+            match setter with
+            | Some mref -> mref.CallingConv.ThisConv
+            | None -> cc
+
+    ILPropertyDef(
+        name = readStringHeap ctxt nameIdx,
+        callingConv = cc2,
+        attributes = enum (flags),
+        setMethod = setter,
+        getMethod = getter,
+        propertyType = retTy,
+        init =
+            (if (flags &&& 0x1000) = 0 then
+                 None
+             else
+                 Some(seekReadConstant ctxt (TaggedIndex(hc_Property, idx)))),
+        args = argTys,
+        customAttrsStored = ctxt.customAttrsReader_Property,
+        metadataIndex = idx
+    )
 
 and seekReadProperties (ctxt: ILMetadataReader) numTypars tidx =
-   mkILPropertiesLazy
-      (lazy
-           let mdv = ctxt.mdfile.GetView()
-           match seekReadOptionalIndexedRow (ctxt.getNumRows TableNames.PropertyMap, (fun i -> i, seekReadPropertyMapRow ctxt mdv i), (fun (_, row) -> fst row), compare tidx, false, (fun (i, row) -> (i, snd row))) with
-           | None -> []
-           | Some (rowNum, beginPropIdx) ->
-               let endPropIdx =
-                   if rowNum >= ctxt.getNumRows TableNames.PropertyMap then
-                       ctxt.getNumRows TableNames.Property + 1
-                   else
-                       let _, endPropIdx = seekReadPropertyMapRow ctxt mdv (rowNum + 1)
-                       endPropIdx
-               [ if beginPropIdx > 0 then
-                   for i in beginPropIdx .. endPropIdx - 1 do
-                     yield seekReadProperty ctxt mdv numTypars i ])
-
-
-and customAttrsReader ctxtH tag: ILAttributesStored =
-    mkILCustomAttrsReader
-        (fun idx ->
-            let (ctxt: ILMetadataReader) = getHole ctxtH
+    mkILPropertiesLazy (
+        lazy
             let mdv = ctxt.mdfile.GetView()
-            let reader =
-                { new ISeekReadIndexedRowReader, ILAttribute> with
-                    member _.GetRow(i, row) = seekReadCustomAttributeRow ctxt mdv i &row
-                    member _.GetKey(attrRow) = attrRow.parentIndex
-                    member _.CompareKey(key) = hcaCompare (TaggedIndex(tag, idx)) key
-                    member _.ConvertRow(attrRow) = seekReadCustomAttr ctxt (attrRow.typeIndex, attrRow.valueIndex)
-                }
-            seekReadIndexedRowsByInterface (ctxt.getNumRows TableNames.CustomAttribute) (isSorted ctxt TableNames.CustomAttribute) reader)
 
-and seekReadCustomAttr ctxt (TaggedIndex(cat, idx), b) =
-    ctxt.seekReadCustomAttr (CustomAttrIdx (cat, idx, b))
+            match
+                seekReadOptionalIndexedRow
+                    (
+                        ctxt.getNumRows TableNames.PropertyMap,
+                        (fun i -> i, seekReadPropertyMapRow ctxt mdv i),
+                        (fun (_, row) -> fst row),
+                        compare tidx,
+                        false,
+                        (fun (i, row) -> (i, snd row))
+                    )
+                with
+            | None -> []
+            | Some (rowNum, beginPropIdx) ->
+                let endPropIdx =
+                    if rowNum >= ctxt.getNumRows TableNames.PropertyMap then
+                        ctxt.getNumRows TableNames.Property + 1
+                    else
+                        let _, endPropIdx = seekReadPropertyMapRow ctxt mdv (rowNum + 1)
+                        endPropIdx
+
+                [
+                    if beginPropIdx > 0 then
+                        for i in beginPropIdx .. endPropIdx - 1 do
+                            yield seekReadProperty ctxt mdv numTypars i
+                ]
+    )
+
+and customAttrsReader ctxtH tag : ILAttributesStored =
+    mkILCustomAttrsReader (fun idx ->
+        let (ctxt: ILMetadataReader) = getHole ctxtH
+        let mdv = ctxt.mdfile.GetView()
+
+        let reader =
+            { new ISeekReadIndexedRowReader, ILAttribute> with
+                member _.GetRow(i, row) =
+                    seekReadCustomAttributeRow ctxt mdv i &row
+
+                member _.GetKey(attrRow) = attrRow.parentIndex
+
+                member _.CompareKey(key) = hcaCompare (TaggedIndex(tag, idx)) key
+
+                member _.ConvertRow(attrRow) =
+                    seekReadCustomAttr ctxt (attrRow.typeIndex, attrRow.valueIndex)
+            }
+
+        seekReadIndexedRowsByInterface (ctxt.getNumRows TableNames.CustomAttribute) (isSorted ctxt TableNames.CustomAttribute) reader)
+
+and seekReadCustomAttr ctxt (TaggedIndex (cat, idx), b) =
+    ctxt.seekReadCustomAttr (CustomAttrIdx(cat, idx, b))
 
 and seekReadCustomAttrUncached ctxtH (CustomAttrIdx (cat, idx, valIdx)) =
     let ctxt = getHole ctxtH
     let method = seekReadCustomAttrType ctxt (TaggedIndex(cat, idx))
+
     let data =
         match readBlobHeapOption ctxt valIdx with
         | Some bytes -> bytes
-        | None -> Bytes.ofInt32Array [| |]
+        | None -> Bytes.ofInt32Array [||]
+
     let elements = []
-    ILAttribute.Encoded (method, data, elements)
+    ILAttribute.Encoded(method, data, elements)
 
 and securityDeclsReader ctxtH tag =
-    mkILSecurityDeclsReader
-      (fun idx ->
-         let (ctxt: ILMetadataReader) = getHole ctxtH
-         let mdv = ctxt.mdfile.GetView()
-         seekReadIndexedRows (ctxt.getNumRows TableNames.Permission,
-                                 seekReadPermissionRow ctxt mdv,
-                                 (fun (_, par, _) -> par),
-                                 hdsCompare (TaggedIndex(tag,idx)),
-                                 isSorted ctxt TableNames.Permission,
-                                 (fun (act, _, ty) -> seekReadSecurityDecl ctxt (act, ty)))
-          |> List.toArray)
+    mkILSecurityDeclsReader (fun idx ->
+        let (ctxt: ILMetadataReader) = getHole ctxtH
+        let mdv = ctxt.mdfile.GetView()
+
+        seekReadIndexedRows (
+            ctxt.getNumRows TableNames.Permission,
+            seekReadPermissionRow ctxt mdv,
+            (fun (_, par, _) -> par),
+            hdsCompare (TaggedIndex(tag, idx)),
+            isSorted ctxt TableNames.Permission,
+            (fun (act, _, ty) -> seekReadSecurityDecl ctxt (act, ty))
+        )
+        |> List.toArray)
 
 and seekReadSecurityDecl ctxt (act, ty) =
-    ILSecurityDecl ((if List.memAssoc (int act) (Lazy.force ILSecurityActionRevMap) then List.assoc (int act) (Lazy.force ILSecurityActionRevMap) else failwith "unknown security action"),
-                    readBlobHeap ctxt ty)
+    ILSecurityDecl(
+        (if List.memAssoc (int act) (Lazy.force ILSecurityActionRevMap) then
+             List.assoc (int act) (Lazy.force ILSecurityActionRevMap)
+         else
+             failwith "unknown security action"),
+        readBlobHeap ctxt ty
+    )
 
 and seekReadConstant (ctxt: ILMetadataReader) idx =
-  let kind, vidx = seekReadIndexedRow (ctxt.getNumRows TableNames.Constant,
-                                      seekReadConstantRow ctxt,
-                                      (fun (_, key, _) -> key),
-                                      hcCompare idx, isSorted ctxt TableNames.Constant, (fun (kind, _, v) -> kind, v))
-  match kind with
-  | x when x = uint16 et_STRING ->
-    let blobHeap = readBlobHeap ctxt vidx
-    let s = Encoding.Unicode.GetString(blobHeap, 0, blobHeap.Length)
-    ILFieldInit.String s
-  | x when x = uint16 et_BOOLEAN -> ILFieldInit.Bool (readBlobHeapAsBool ctxt vidx)
-  | x when x = uint16 et_CHAR -> ILFieldInit.Char (readBlobHeapAsUInt16 ctxt vidx)
-  | x when x = uint16 et_I1 -> ILFieldInit.Int8 (readBlobHeapAsSByte ctxt vidx)
-  | x when x = uint16 et_I2 -> ILFieldInit.Int16 (readBlobHeapAsInt16 ctxt vidx)
-  | x when x = uint16 et_I4 -> ILFieldInit.Int32 (readBlobHeapAsInt32 ctxt vidx)
-  | x when x = uint16 et_I8 -> ILFieldInit.Int64 (readBlobHeapAsInt64 ctxt vidx)
-  | x when x = uint16 et_U1 -> ILFieldInit.UInt8 (readBlobHeapAsByte ctxt vidx)
-  | x when x = uint16 et_U2 -> ILFieldInit.UInt16 (readBlobHeapAsUInt16 ctxt vidx)
-  | x when x = uint16 et_U4 -> ILFieldInit.UInt32 (readBlobHeapAsUInt32 ctxt vidx)
-  | x when x = uint16 et_U8 -> ILFieldInit.UInt64 (readBlobHeapAsUInt64 ctxt vidx)
-  | x when x = uint16 et_R4 -> ILFieldInit.Single (readBlobHeapAsSingle ctxt vidx)
-  | x when x = uint16 et_R8 -> ILFieldInit.Double (readBlobHeapAsDouble ctxt vidx)
-  | x when x = uint16 et_CLASS || x = uint16 et_OBJECT -> ILFieldInit.Null
-  | _ -> ILFieldInit.Null
+    let kind, vidx =
+        seekReadIndexedRow (
+            ctxt.getNumRows TableNames.Constant,
+            seekReadConstantRow ctxt,
+            (fun (_, key, _) -> key),
+            hcCompare idx,
+            isSorted ctxt TableNames.Constant,
+            (fun (kind, _, v) -> kind, v)
+        )
+
+    match kind with
+    | x when x = uint16 et_STRING ->
+        let blobHeap = readBlobHeap ctxt vidx
+        let s = Encoding.Unicode.GetString(blobHeap, 0, blobHeap.Length)
+        ILFieldInit.String s
+    | x when x = uint16 et_BOOLEAN -> ILFieldInit.Bool(readBlobHeapAsBool ctxt vidx)
+    | x when x = uint16 et_CHAR -> ILFieldInit.Char(readBlobHeapAsUInt16 ctxt vidx)
+    | x when x = uint16 et_I1 -> ILFieldInit.Int8(readBlobHeapAsSByte ctxt vidx)
+    | x when x = uint16 et_I2 -> ILFieldInit.Int16(readBlobHeapAsInt16 ctxt vidx)
+    | x when x = uint16 et_I4 -> ILFieldInit.Int32(readBlobHeapAsInt32 ctxt vidx)
+    | x when x = uint16 et_I8 -> ILFieldInit.Int64(readBlobHeapAsInt64 ctxt vidx)
+    | x when x = uint16 et_U1 -> ILFieldInit.UInt8(readBlobHeapAsByte ctxt vidx)
+    | x when x = uint16 et_U2 -> ILFieldInit.UInt16(readBlobHeapAsUInt16 ctxt vidx)
+    | x when x = uint16 et_U4 -> ILFieldInit.UInt32(readBlobHeapAsUInt32 ctxt vidx)
+    | x when x = uint16 et_U8 -> ILFieldInit.UInt64(readBlobHeapAsUInt64 ctxt vidx)
+    | x when x = uint16 et_R4 -> ILFieldInit.Single(readBlobHeapAsSingle ctxt vidx)
+    | x when x = uint16 et_R8 -> ILFieldInit.Double(readBlobHeapAsDouble ctxt vidx)
+    | x when x = uint16 et_CLASS || x = uint16 et_OBJECT -> ILFieldInit.Null
+    | _ -> ILFieldInit.Null
 
 and seekReadImplMap (ctxt: ILMetadataReader) nm midx =
- lazy
-   MethodBody.PInvoke
-      (lazy
-            let mdv = ctxt.mdfile.GetView()
-            let flags, nameIdx, scopeIdx = seekReadIndexedRow (ctxt.getNumRows TableNames.ImplMap,
-                                                                seekReadImplMapRow ctxt mdv,
-                                                                (fun (_, m, _, _) -> m),
-                                                                mfCompare (TaggedIndex(mf_MethodDef, midx)),
-                                                                isSorted ctxt TableNames.ImplMap,
-                                                                (fun (a, _, c, d) -> a, c, d))
-            let cc =
-                let masked = flags &&& 0x0700
-                if masked = 0x0000 then PInvokeCallingConvention.None
-                elif masked = 0x0200 then PInvokeCallingConvention.Cdecl
-                elif masked = 0x0300 then PInvokeCallingConvention.Stdcall
-                elif masked = 0x0400 then PInvokeCallingConvention.Thiscall
-                elif masked = 0x0500 then PInvokeCallingConvention.Fastcall
-                elif masked = 0x0100 then PInvokeCallingConvention.WinApi
-                else (dprintn "strange CallingConv"; PInvokeCallingConvention.None)
-
-            let enc =
-                let masked = flags &&& 0x0006
-                if masked = 0x0000 then PInvokeCharEncoding.None
-                elif masked = 0x0002 then PInvokeCharEncoding.Ansi
-                elif masked = 0x0004 then PInvokeCharEncoding.Unicode
-                elif masked = 0x0006 then PInvokeCharEncoding.Auto
-                else (dprintn "strange CharEncoding"; PInvokeCharEncoding.None)
-
-            let bestfit =
-                let masked = flags &&& 0x0030
-                if masked = 0x0000 then PInvokeCharBestFit.UseAssembly
-                elif masked = 0x0010 then PInvokeCharBestFit.Enabled
-                elif masked = 0x0020 then PInvokeCharBestFit.Disabled
-                else (dprintn "strange CharBestFit"; PInvokeCharBestFit.UseAssembly)
-
-            let unmap =
-                let masked = flags &&& 0x3000
-                if masked = 0x0000 then PInvokeThrowOnUnmappableChar.UseAssembly
-                elif masked = 0x1000 then PInvokeThrowOnUnmappableChar.Enabled
-                elif masked = 0x2000 then PInvokeThrowOnUnmappableChar.Disabled
-                else (dprintn "strange ThrowOnUnmappableChar"; PInvokeThrowOnUnmappableChar.UseAssembly)
-
-            { CallingConv = cc
-              CharEncoding = enc
-              CharBestFit=bestfit
-              ThrowOnUnmappableChar=unmap
-              NoMangle = (flags &&& 0x0001) <> 0x0
-              LastError = (flags &&& 0x0040) <> 0x0
-              Name =
-                  (match readStringHeapOption ctxt nameIdx with
-                   | None -> nm
-                   | Some nm2 -> nm2)
-              Where = seekReadModuleRef ctxt mdv scopeIdx })
+    lazy
+        MethodBody.PInvoke(
+            lazy
+                let mdv = ctxt.mdfile.GetView()
+
+                let flags, nameIdx, scopeIdx =
+                    seekReadIndexedRow (
+                        ctxt.getNumRows TableNames.ImplMap,
+                        seekReadImplMapRow ctxt mdv,
+                        (fun (_, m, _, _) -> m),
+                        mfCompare (TaggedIndex(mf_MethodDef, midx)),
+                        isSorted ctxt TableNames.ImplMap,
+                        (fun (a, _, c, d) -> a, c, d)
+                    )
+
+                let cc =
+                    let masked = flags &&& 0x0700
+
+                    if masked = 0x0000 then
+                        PInvokeCallingConvention.None
+                    elif masked = 0x0200 then
+                        PInvokeCallingConvention.Cdecl
+                    elif masked = 0x0300 then
+                        PInvokeCallingConvention.Stdcall
+                    elif masked = 0x0400 then
+                        PInvokeCallingConvention.Thiscall
+                    elif masked = 0x0500 then
+                        PInvokeCallingConvention.Fastcall
+                    elif masked = 0x0100 then
+                        PInvokeCallingConvention.WinApi
+                    else
+                        (dprintn "strange CallingConv"
+                         PInvokeCallingConvention.None)
+
+                let enc =
+                    let masked = flags &&& 0x0006
+
+                    if masked = 0x0000 then
+                        PInvokeCharEncoding.None
+                    elif masked = 0x0002 then
+                        PInvokeCharEncoding.Ansi
+                    elif masked = 0x0004 then
+                        PInvokeCharEncoding.Unicode
+                    elif masked = 0x0006 then
+                        PInvokeCharEncoding.Auto
+                    else
+                        (dprintn "strange CharEncoding"
+                         PInvokeCharEncoding.None)
+
+                let bestfit =
+                    let masked = flags &&& 0x0030
+
+                    if masked = 0x0000 then
+                        PInvokeCharBestFit.UseAssembly
+                    elif masked = 0x0010 then
+                        PInvokeCharBestFit.Enabled
+                    elif masked = 0x0020 then
+                        PInvokeCharBestFit.Disabled
+                    else
+                        (dprintn "strange CharBestFit"
+                         PInvokeCharBestFit.UseAssembly)
+
+                let unmap =
+                    let masked = flags &&& 0x3000
+
+                    if masked = 0x0000 then
+                        PInvokeThrowOnUnmappableChar.UseAssembly
+                    elif masked = 0x1000 then
+                        PInvokeThrowOnUnmappableChar.Enabled
+                    elif masked = 0x2000 then
+                        PInvokeThrowOnUnmappableChar.Disabled
+                    else
+                        (dprintn "strange ThrowOnUnmappableChar"
+                         PInvokeThrowOnUnmappableChar.UseAssembly)
+
+                {
+                    CallingConv = cc
+                    CharEncoding = enc
+                    CharBestFit = bestfit
+                    ThrowOnUnmappableChar = unmap
+                    NoMangle = (flags &&& 0x0001) <> 0x0
+                    LastError = (flags &&& 0x0040) <> 0x0
+                    Name =
+                        (match readStringHeapOption ctxt nameIdx with
+                         | None -> nm
+                         | Some nm2 -> nm2)
+                    Where = seekReadModuleRef ctxt mdv scopeIdx
+                }
+        )
 
 and seekReadTopCode (ctxt: ILMetadataReader) pev mdv numTypars (sz: int) start seqpoints =
-   let labelsOfRawOffsets = Dictionary<_, _>(sz/2)
-   let ilOffsetsOfLabels = Dictionary<_, _>(sz/2)
-
-   let rawToLabel rawOffset =
-       match labelsOfRawOffsets.TryGetValue rawOffset with
-       | true, l -> l
-       | _ ->
-           let lab = generateCodeLabel()
-           labelsOfRawOffsets[rawOffset] <- lab
-           lab
-
-   let markAsInstructionStart rawOffset ilOffset =
-       let lab = rawToLabel rawOffset
-       ilOffsetsOfLabels[lab] <- ilOffset
-
-   let ibuf = ResizeArray<_>(sz/2)
-   let mutable curr = 0
-   let prefixes = { al=Aligned; tl= Normalcall; vol= Nonvolatile;ro=NormalAddress;constrained=None }
-   let mutable lastb = 0x0
-   let mutable lastb2 = 0x0
-   let mutable b = 0x0
-   let get () =
-       lastb <- seekReadByteAsInt32 pev (start + curr)
-       curr <- curr + 1
-       b <-
-         if lastb = 0xfe && curr < sz then
-           lastb2 <- seekReadByteAsInt32 pev (start + curr)
-           curr <- curr + 1
-           lastb2
-         else
-           lastb
-
-   let mutable seqPointsRemaining = seqpoints
-
-   while curr < sz do
-     // registering "+string !curr+" as start of an instruction")
-     markAsInstructionStart curr ibuf.Count
-
-     // Insert any sequence points into the instruction sequence
-     while
-         (match seqPointsRemaining with
-          | (i, _tag) :: _rest when i <= curr -> true
-          | _ -> false)
-        do
-         // Emitting one sequence point
-         let _, tag = List.head seqPointsRemaining
-         seqPointsRemaining <- List.tail seqPointsRemaining
-         ibuf.Add (I_seqpoint tag)
-
-     // Read the prefixes. Leave lastb and lastb2 holding the instruction byte(s)
-     begin
-       prefixes.al <- Aligned
-       prefixes.tl <- Normalcall
-       prefixes.vol <- Nonvolatile
-       prefixes.ro<-NormalAddress
-       prefixes.constrained<-None
-       get ()
-       while curr < sz &&
-         lastb = 0xfe &&
-         (b = (i_constrained &&& 0xff) ||
-          b = (i_readonly &&& 0xff) ||
-          b = (i_unaligned &&& 0xff) ||
-          b = (i_volatile &&& 0xff) ||
-          b = (i_tail &&& 0xff)) do
-         begin
-             if b = (i_unaligned &&& 0xff) then
-               let unal = seekReadByteAsInt32 pev (start + curr)
-               curr <- curr + 1
-               prefixes.al <-
-                  if unal = 0x1 then Unaligned1
-                  elif unal = 0x2 then Unaligned2
-                  elif unal = 0x4 then Unaligned4
-                  else (dprintn "bad alignment for unaligned"; Aligned)
-             elif b = (i_volatile &&& 0xff) then prefixes.vol <- Volatile
-             elif b = (i_readonly &&& 0xff) then prefixes.ro <- ReadonlyAddress
-             elif b = (i_constrained &&& 0xff) then
-                 let uncoded = seekReadUncodedToken pev (start + curr)
-                 curr <- curr + 4
-                 let ty = seekReadTypeDefOrRef ctxt numTypars AsObject [] (uncodedTokenToTypeDefOrRefOrSpec uncoded)
-                 prefixes.constrained <- Some ty
-             else prefixes.tl <- Tailcall
-         end
+    let labelsOfRawOffsets = Dictionary<_, _>(sz / 2)
+    let ilOffsetsOfLabels = Dictionary<_, _>(sz / 2)
+
+    let rawToLabel rawOffset =
+        match labelsOfRawOffsets.TryGetValue rawOffset with
+        | true, l -> l
+        | _ ->
+            let lab = generateCodeLabel ()
+            labelsOfRawOffsets[rawOffset] <- lab
+            lab
+
+    let markAsInstructionStart rawOffset ilOffset =
+        let lab = rawToLabel rawOffset
+        ilOffsetsOfLabels[lab] <- ilOffset
+
+    let ibuf = ResizeArray<_>(sz / 2)
+    let mutable curr = 0
+
+    let prefixes =
+        {
+            al = Aligned
+            tl = Normalcall
+            vol = Nonvolatile
+            ro = NormalAddress
+            constrained = None
+        }
+
+    let mutable lastb = 0x0
+    let mutable lastb2 = 0x0
+    let mutable b = 0x0
+
+    let get () =
+        lastb <- seekReadByteAsInt32 pev (start + curr)
+        curr <- curr + 1
+
+        b <-
+            if lastb = 0xfe && curr < sz then
+                lastb2 <- seekReadByteAsInt32 pev (start + curr)
+                curr <- curr + 1
+                lastb2
+            else
+                lastb
+
+    let mutable seqPointsRemaining = seqpoints
+
+    while curr < sz do
+        // registering "+string !curr+" as start of an instruction")
+        markAsInstructionStart curr ibuf.Count
+
+        // Insert any sequence points into the instruction sequence
+        while (match seqPointsRemaining with
+               | (i, _tag) :: _rest when i <= curr -> true
+               | _ -> false) do
+            // Emitting one sequence point
+            let _, tag = List.head seqPointsRemaining
+            seqPointsRemaining <- List.tail seqPointsRemaining
+            ibuf.Add(I_seqpoint tag)
+
+        // Read the prefixes. Leave lastb and lastb2 holding the instruction byte(s)
+        (prefixes.al <- Aligned
+         prefixes.tl <- Normalcall
+         prefixes.vol <- Nonvolatile
+         prefixes.ro <- NormalAddress
+         prefixes.constrained <- None
          get ()
-     end
-
-     // data for instruction begins at "+string !curr
-     // Read and decode the instruction
-     if (curr <= sz) then
-       let idecoder =
-           if lastb = 0xfe then getTwoByteInstr lastb2
-           else getOneByteInstr lastb
-       let instr =
-         match idecoder with
-         | I_u16_u8_instr f ->
-             let x = seekReadByte pev (start + curr) |> uint16
-             curr <- curr + 1
-             f prefixes x
-         | I_u16_u16_instr f ->
-             let x = seekReadUInt16 pev (start + curr)
-             curr <- curr + 2
-             f prefixes x
-         | I_none_instr f ->
-             f prefixes
-         | I_i64_instr f ->
-             let x = seekReadInt64 pev (start + curr)
-             curr <- curr + 8
-             f prefixes x
-         | I_i32_i8_instr f ->
-             let x = seekReadSByte pev (start + curr) |> int32
-             curr <- curr + 1
-             f prefixes x
-         | I_i32_i32_instr f ->
-             let x = seekReadInt32 pev (start + curr)
-             curr <- curr + 4
-             f prefixes x
-         | I_r4_instr f ->
-             let x = seekReadSingle pev (start + curr)
-             curr <- curr + 4
-             f prefixes x
-         | I_r8_instr f ->
-             let x = seekReadDouble pev (start + curr)
-             curr <- curr + 8
-             f prefixes x
-         | I_field_instr f ->
-             let tab, tok = seekReadUncodedToken pev (start + curr)
-             curr <- curr + 4
-             let fspec =
-               if tab = TableNames.Field then
-                 seekReadFieldDefAsFieldSpec ctxt tok
-               elif tab = TableNames.MemberRef then
-                 seekReadMemberRefAsFieldSpec ctxt numTypars tok
-               else failwith "bad table in FieldDefOrRef"
-             f prefixes fspec
-         | I_method_instr f ->
-             // method instruction, curr = "+string !curr
-
-             let tab, idx = seekReadUncodedToken pev (start + curr)
-             curr <- curr + 4
-             let (VarArgMethodData(enclTy, cc, nm, argTys, varargs, retTy, methInst)) =
-               if tab = TableNames.Method then
-                 seekReadMethodDefOrRef ctxt numTypars (TaggedIndex(mdor_MethodDef, idx))
-               elif tab = TableNames.MemberRef then
-                 seekReadMethodDefOrRef ctxt numTypars (TaggedIndex(mdor_MemberRef, idx))
-               elif tab = TableNames.MethodSpec then
-                 seekReadMethodSpecAsMethodData ctxt numTypars idx
-               else failwith "bad table in MethodDefOrRefOrSpec"
-             match enclTy with
-             | ILType.Array (shape, ty) ->
-               match nm with
-               | "Get" -> I_ldelem_any(shape, ty)
-               | "Set" -> I_stelem_any(shape, ty)
-               | "Address" -> I_ldelema(prefixes.ro, false, shape, ty)
-               | ".ctor" -> I_newarr(shape, ty)
-               | _ -> failwith "bad method on array type"
-             | _ ->
-               let mspec = mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
-               f prefixes (mspec, varargs)
-         | I_type_instr f ->
-             let uncoded = seekReadUncodedToken pev (start + curr)
-             curr <- curr + 4
-             let ty = seekReadTypeDefOrRef ctxt numTypars AsObject [] (uncodedTokenToTypeDefOrRefOrSpec uncoded)
-             f prefixes ty
-         | I_string_instr f ->
-             let tab, idx = seekReadUncodedToken pev (start + curr)
-             curr <- curr + 4
-             if tab <> TableNames.UserStrings then dprintn "warning: bad table in user string for ldstr"
-             f prefixes (readUserStringHeap ctxt idx)
-
-         | I_conditional_i32_instr f ->
-             let offsDest = (seekReadInt32 pev (start + curr))
-             curr <- curr + 4
-             let dest = curr + offsDest
-             f prefixes (rawToLabel dest)
-         | I_conditional_i8_instr f ->
-             let offsDest = int (seekReadSByte pev (start + curr))
-             curr <- curr + 1
-             let dest = curr + offsDest
-             f prefixes (rawToLabel dest)
-         | I_unconditional_i32_instr f ->
-             let offsDest = (seekReadInt32 pev (start + curr))
-             curr <- curr + 4
-             let dest = curr + offsDest
-             f prefixes (rawToLabel dest)
-         | I_unconditional_i8_instr f ->
-             let offsDest = int (seekReadSByte pev (start + curr))
-             curr <- curr + 1
-             let dest = curr + offsDest
-             f prefixes (rawToLabel dest)
-         | I_invalid_instr ->
-             dprintn ("invalid instruction: "+string lastb+ (if lastb = 0xfe then ", "+string lastb2 else ""))
-             I_ret
-         | I_tok_instr f ->
-             let tab, idx = seekReadUncodedToken pev (start + curr)
-             curr <- curr + 4
-             (* REVIEW: this incorrectly labels all MemberRef tokens as ILMethod's: we should go look at the MemberRef sig to determine if it is a field or method *)
-             let token_info =
-               if tab = TableNames.Method || tab = TableNames.MemberRef (* REVIEW: generics or tab = TableNames.MethodSpec *) then
-                 let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) = seekReadMethodDefOrRefNoVarargs ctxt numTypars (uncodedTokenToMethodDefOrRef (tab, idx))
-                 ILToken.ILMethod (mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst))
-               elif tab = TableNames.Field then
-                 ILToken.ILField (seekReadFieldDefAsFieldSpec ctxt idx)
-               elif tab = TableNames.TypeDef || tab = TableNames.TypeRef || tab = TableNames.TypeSpec then
-                 ILToken.ILType (seekReadTypeDefOrRef ctxt numTypars AsObject [] (uncodedTokenToTypeDefOrRefOrSpec (tab, idx)))
-               else failwith "bad token for ldtoken"
-             f prefixes token_info
-         | I_sig_instr f ->
-             let tab, idx = seekReadUncodedToken pev (start + curr)
-             curr <- curr + 4
-             if tab <> TableNames.StandAloneSig then dprintn "strange table for callsig token"
-             let generic, _genarity, cc, retTy, argTys, varargs = readBlobHeapAsMethodSig ctxt numTypars (seekReadStandAloneSigRow ctxt mdv idx)
-             if generic then failwith "bad image: a generic method signature is begin used at a calli instruction"
-             f prefixes (mkILCallSig (cc, argTys, retTy), varargs)
-         | I_switch_instr f ->
-             let n = (seekReadInt32 pev (start + curr))
-             curr <- curr + 4
-             let offsets =
-               List.init n (fun _ ->
-                   let i = (seekReadInt32 pev (start + curr))
-                   curr <- curr + 4
-                   i)
-             let dests = List.map (fun offs -> rawToLabel (curr + offs)) offsets
-             f prefixes dests
-       ibuf.Add instr
-   done
-   // Finished reading instructions - mark the end of the instruction stream in case the PDB information refers to it.
-   markAsInstructionStart curr ibuf.Count
-   // Build the function that maps from raw labels (offsets into the bytecode stream) to indexes in the AbsIL instruction stream
-   let lab2pc = ilOffsetsOfLabels
-
-   // Some offsets used in debug info refer to the end of an instruction, rather than the
-   // start of the subsequent instruction. But all labels refer to instruction starts,
-   // apart from a final label which refers to the end of the method. This function finds
-   // the start of the next instruction referred to by the raw offset.
-   let raw2nextLab rawOffset =
-       let isInstrStart x =
-           match labelsOfRawOffsets.TryGetValue x with
-           | true, lab -> ilOffsetsOfLabels.ContainsKey lab
-           | _ -> false
-       if isInstrStart rawOffset then rawToLabel rawOffset
-       elif isInstrStart (rawOffset+1) then rawToLabel (rawOffset+1)
-       else failwith ("the bytecode raw offset "+string rawOffset+" did not refer either to the start or end of an instruction")
-   let instrs = ibuf.ToArray()
-   instrs, rawToLabel, lab2pc, raw2nextLab
+
+         while curr < sz
+               && lastb = 0xfe
+               && (b = (i_constrained &&& 0xff)
+                   || b = (i_readonly &&& 0xff)
+                   || b = (i_unaligned &&& 0xff)
+                   || b = (i_volatile &&& 0xff)
+                   || b = (i_tail &&& 0xff)) do
+             (if b = (i_unaligned &&& 0xff) then
+                  let unal = seekReadByteAsInt32 pev (start + curr)
+                  curr <- curr + 1
+
+                  prefixes.al <-
+                      if unal = 0x1 then
+                          Unaligned1
+                      elif unal = 0x2 then
+                          Unaligned2
+                      elif unal = 0x4 then
+                          Unaligned4
+                      else
+                          (dprintn "bad alignment for unaligned"
+                           Aligned)
+              elif b = (i_volatile &&& 0xff) then
+                  prefixes.vol <- Volatile
+              elif b = (i_readonly &&& 0xff) then
+                  prefixes.ro <- ReadonlyAddress
+              elif b = (i_constrained &&& 0xff) then
+                  let uncoded = seekReadUncodedToken pev (start + curr)
+                  curr <- curr + 4
+
+                  let ty =
+                      seekReadTypeDefOrRef ctxt numTypars AsObject [] (uncodedTokenToTypeDefOrRefOrSpec uncoded)
+
+                  prefixes.constrained <- Some ty
+              else
+                  prefixes.tl <- Tailcall)
+
+             get ())
+
+        // data for instruction begins at "+string !curr
+        // Read and decode the instruction
+        if (curr <= sz) then
+            let idecoder =
+                if lastb = 0xfe then
+                    getTwoByteInstr lastb2
+                else
+                    getOneByteInstr lastb
+
+            let instr =
+                match idecoder with
+                | I_u16_u8_instr f ->
+                    let x = seekReadByte pev (start + curr) |> uint16
+                    curr <- curr + 1
+                    f prefixes x
+                | I_u16_u16_instr f ->
+                    let x = seekReadUInt16 pev (start + curr)
+                    curr <- curr + 2
+                    f prefixes x
+                | I_none_instr f -> f prefixes
+                | I_i64_instr f ->
+                    let x = seekReadInt64 pev (start + curr)
+                    curr <- curr + 8
+                    f prefixes x
+                | I_i32_i8_instr f ->
+                    let x = seekReadSByte pev (start + curr) |> int32
+                    curr <- curr + 1
+                    f prefixes x
+                | I_i32_i32_instr f ->
+                    let x = seekReadInt32 pev (start + curr)
+                    curr <- curr + 4
+                    f prefixes x
+                | I_r4_instr f ->
+                    let x = seekReadSingle pev (start + curr)
+                    curr <- curr + 4
+                    f prefixes x
+                | I_r8_instr f ->
+                    let x = seekReadDouble pev (start + curr)
+                    curr <- curr + 8
+                    f prefixes x
+                | I_field_instr f ->
+                    let tab, tok = seekReadUncodedToken pev (start + curr)
+                    curr <- curr + 4
+
+                    let fspec =
+                        if tab = TableNames.Field then
+                            seekReadFieldDefAsFieldSpec ctxt tok
+                        elif tab = TableNames.MemberRef then
+                            seekReadMemberRefAsFieldSpec ctxt numTypars tok
+                        else
+                            failwith "bad table in FieldDefOrRef"
+
+                    f prefixes fspec
+                | I_method_instr f ->
+                    // method instruction, curr = "+string !curr
+
+                    let tab, idx = seekReadUncodedToken pev (start + curr)
+                    curr <- curr + 4
+
+                    let (VarArgMethodData (enclTy, cc, nm, argTys, varargs, retTy, methInst)) =
+                        if tab = TableNames.Method then
+                            seekReadMethodDefOrRef ctxt numTypars (TaggedIndex(mdor_MethodDef, idx))
+                        elif tab = TableNames.MemberRef then
+                            seekReadMethodDefOrRef ctxt numTypars (TaggedIndex(mdor_MemberRef, idx))
+                        elif tab = TableNames.MethodSpec then
+                            seekReadMethodSpecAsMethodData ctxt numTypars idx
+                        else
+                            failwith "bad table in MethodDefOrRefOrSpec"
+
+                    match enclTy with
+                    | ILType.Array (shape, ty) ->
+                        match nm with
+                        | "Get" -> I_ldelem_any(shape, ty)
+                        | "Set" -> I_stelem_any(shape, ty)
+                        | "Address" -> I_ldelema(prefixes.ro, false, shape, ty)
+                        | ".ctor" -> I_newarr(shape, ty)
+                        | _ -> failwith "bad method on array type"
+                    | _ ->
+                        let mspec = mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst)
+                        f prefixes (mspec, varargs)
+                | I_type_instr f ->
+                    let uncoded = seekReadUncodedToken pev (start + curr)
+                    curr <- curr + 4
+
+                    let ty =
+                        seekReadTypeDefOrRef ctxt numTypars AsObject [] (uncodedTokenToTypeDefOrRefOrSpec uncoded)
+
+                    f prefixes ty
+                | I_string_instr f ->
+                    let tab, idx = seekReadUncodedToken pev (start + curr)
+                    curr <- curr + 4
+
+                    if tab <> TableNames.UserStrings then
+                        dprintn "warning: bad table in user string for ldstr"
+
+                    f prefixes (readUserStringHeap ctxt idx)
+
+                | I_conditional_i32_instr f ->
+                    let offsDest = (seekReadInt32 pev (start + curr))
+                    curr <- curr + 4
+                    let dest = curr + offsDest
+                    f prefixes (rawToLabel dest)
+                | I_conditional_i8_instr f ->
+                    let offsDest = int (seekReadSByte pev (start + curr))
+                    curr <- curr + 1
+                    let dest = curr + offsDest
+                    f prefixes (rawToLabel dest)
+                | I_unconditional_i32_instr f ->
+                    let offsDest = (seekReadInt32 pev (start + curr))
+                    curr <- curr + 4
+                    let dest = curr + offsDest
+                    f prefixes (rawToLabel dest)
+                | I_unconditional_i8_instr f ->
+                    let offsDest = int (seekReadSByte pev (start + curr))
+                    curr <- curr + 1
+                    let dest = curr + offsDest
+                    f prefixes (rawToLabel dest)
+                | I_invalid_instr ->
+                    dprintn (
+                        "invalid instruction: "
+                        + string lastb
+                        + (if lastb = 0xfe then
+                               ", " + string lastb2
+                           else
+                               "")
+                    )
+
+                    I_ret
+                | I_tok_instr f ->
+                    let tab, idx = seekReadUncodedToken pev (start + curr)
+                    curr <- curr + 4
+                    (* REVIEW: this incorrectly labels all MemberRef tokens as ILMethod's: we should go look at the MemberRef sig to determine if it is a field or method *)
+                    let token_info =
+                        if tab = TableNames.Method
+                           || tab = TableNames.MemberRef (* REVIEW: generics or tab = TableNames.MethodSpec *) then
+                            let (MethodData (enclTy, cc, nm, argTys, retTy, methInst)) =
+                                seekReadMethodDefOrRefNoVarargs ctxt numTypars (uncodedTokenToMethodDefOrRef (tab, idx))
+
+                            ILToken.ILMethod(mkILMethSpecInTy (enclTy, cc, nm, argTys, retTy, methInst))
+                        elif tab = TableNames.Field then
+                            ILToken.ILField(seekReadFieldDefAsFieldSpec ctxt idx)
+                        elif tab = TableNames.TypeDef
+                             || tab = TableNames.TypeRef
+                             || tab = TableNames.TypeSpec then
+                            ILToken.ILType(seekReadTypeDefOrRef ctxt numTypars AsObject [] (uncodedTokenToTypeDefOrRefOrSpec (tab, idx)))
+                        else
+                            failwith "bad token for ldtoken"
+
+                    f prefixes token_info
+                | I_sig_instr f ->
+                    let tab, idx = seekReadUncodedToken pev (start + curr)
+                    curr <- curr + 4
+
+                    if tab <> TableNames.StandAloneSig then
+                        dprintn "strange table for callsig token"
+
+                    let generic, _genarity, cc, retTy, argTys, varargs =
+                        readBlobHeapAsMethodSig ctxt numTypars (seekReadStandAloneSigRow ctxt mdv idx)
+
+                    if generic then
+                        failwith "bad image: a generic method signature is begin used at a calli instruction"
+
+                    f prefixes (mkILCallSig (cc, argTys, retTy), varargs)
+                | I_switch_instr f ->
+                    let n = (seekReadInt32 pev (start + curr))
+                    curr <- curr + 4
+
+                    let offsets =
+                        List.init n (fun _ ->
+                            let i = (seekReadInt32 pev (start + curr))
+                            curr <- curr + 4
+                            i)
+
+                    let dests = List.map (fun offs -> rawToLabel (curr + offs)) offsets
+                    f prefixes dests
+
+            ibuf.Add instr
+    // Finished reading instructions - mark the end of the instruction stream in case the PDB information refers to it.
+    markAsInstructionStart curr ibuf.Count
+    // Build the function that maps from raw labels (offsets into the bytecode stream) to indexes in the AbsIL instruction stream
+    let lab2pc = ilOffsetsOfLabels
+
+    // Some offsets used in debug info refer to the end of an instruction, rather than the
+    // start of the subsequent instruction. But all labels refer to instruction starts,
+    // apart from a final label which refers to the end of the method. This function finds
+    // the start of the next instruction referred to by the raw offset.
+    let raw2nextLab rawOffset =
+        let isInstrStart x =
+            match labelsOfRawOffsets.TryGetValue x with
+            | true, lab -> ilOffsetsOfLabels.ContainsKey lab
+            | _ -> false
+
+        if isInstrStart rawOffset then
+            rawToLabel rawOffset
+        elif isInstrStart (rawOffset + 1) then
+            rawToLabel (rawOffset + 1)
+        else
+            failwith (
+                "the bytecode raw offset "
+                + string rawOffset
+                + " did not refer either to the start or end of an instruction"
+            )
+
+    let instrs = ibuf.ToArray()
+    instrs, rawToLabel, lab2pc, raw2nextLab
 
 #if FX_NO_PDB_READER
 and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (_idx, nm, _internalcall, noinline, aggressiveinline, numTypars) rva =
 #else
 and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (idx, nm, _internalcall, noinline, aggressiveinline, numTypars) rva =
 #endif
- lazy
-  let pev = pectxt.pefile.GetView()
-  let baseRVA = pectxt.anyV2P("method rva", rva)
-  // ": reading body of method "+nm+" at rva "+string rva+", phys "+string baseRVA
-  let b = seekReadByte pev baseRVA
-
-  let isTinyFormat = (b &&& e_CorILMethod_FormatMask) = e_CorILMethod_TinyFormat
-  let isFatFormat = (b &&& e_CorILMethod_FormatMask) = e_CorILMethod_FatFormat
-
-  if not isTinyFormat && not isFatFormat then
-    if logging then failwith "unknown format"
-    MethodBody.Abstract
-  else
-
-  MethodBody.IL
-   (lazy
-       let pev = pectxt.pefile.GetView()
-       let mdv = ctxt.mdfile.GetView()
-
-       // Read any debug information for this method into temporary data structures
-       //    -- a list of locals, marked with the raw offsets (actually closures which accept the resolution function that maps raw offsets to labels)
-       //    -- an overall range for the method
-       //    -- the sequence points for the method
-       let localPdbInfos, methRangePdbInfo, seqpoints =
+    lazy
+        let pev = pectxt.pefile.GetView()
+        let baseRVA = pectxt.anyV2P ("method rva", rva)
+        // ": reading body of method "+nm+" at rva "+string rva+", phys "+string baseRVA
+        let b = seekReadByte pev baseRVA
+
+        let isTinyFormat = (b &&& e_CorILMethod_FormatMask) = e_CorILMethod_TinyFormat
+        let isFatFormat = (b &&& e_CorILMethod_FormatMask) = e_CorILMethod_FatFormat
+
+        if not isTinyFormat && not isFatFormat then
+            if logging then
+                failwith "unknown format"
+
+            MethodBody.Abstract
+        else
+
+            MethodBody.IL(
+                lazy
+                    let pev = pectxt.pefile.GetView()
+                    let mdv = ctxt.mdfile.GetView()
+
+                    // Read any debug information for this method into temporary data structures
+                    //    -- a list of locals, marked with the raw offsets (actually closures which accept the resolution function that maps raw offsets to labels)
+                    //    -- an overall range for the method
+                    //    -- the sequence points for the method
+                    let localPdbInfos, methRangePdbInfo, seqpoints =
 #if FX_NO_PDB_READER
-           [], None, []
+                        [], None, []
 #else
-           match pectxt.pdb with
-           | None ->
-               [], None, []
-           | Some (pdbr, get_doc) ->
-               try
-
-                 let pdbm = pdbReaderGetMethod pdbr (uncodedToken TableNames.Method idx)
-                 let sps = pdbMethodGetDebugPoints pdbm
-                 (* let roota, rootb = pdbScopeGetOffsets rootScope in *)
-                 let seqpoints =
-                    let arr =
-                       sps |> Array.map (fun sp ->
-                           // It is VERY annoying to have to call GetURL for the document for
-                           // each sequence point. This appears to be a short coming of the PDB
-                           // reader API. They should return an index into the array of documents for the reader
-                           let sourcedoc = get_doc (pdbDocumentGetURL sp.pdbSeqPointDocument)
-                           let source =
-                               ILDebugPoint.Create(document = sourcedoc,
-                                   line = sp.pdbSeqPointLine,
-                                   column = sp.pdbSeqPointColumn,
-                                   endLine = sp.pdbSeqPointEndLine,
-                                   endColumn = sp.pdbSeqPointEndColumn)
-                           (sp.pdbSeqPointOffset, source))
-
-                    Array.sortInPlaceBy fst arr
-
-                    Array.toList arr
-
-                 let rec scopes scp =
-                       let a, b = pdbScopeGetOffsets scp
-                       let lvs = pdbScopeGetLocals scp
-                       let ilvs =
-                         lvs
-                         |> Array.toList
-                         |> List.filter (fun l ->
-                             let k, _idx = pdbVariableGetAddressAttributes l
-                             k = 1 (* ADDR_IL_OFFSET *))
-                       let ilinfos: ILLocalDebugMapping list =
-                         ilvs |> List.map (fun ilv ->
-                             let _k, idx = pdbVariableGetAddressAttributes ilv
-                             let n = pdbVariableGetName ilv
-                             { LocalIndex= idx
-                               LocalName=n})
-
-                       let thisOne =
-                         (fun raw2nextLab ->
-                           { Range= (raw2nextLab a, raw2nextLab b)
-                             DebugMappings = ilinfos }: ILLocalDebugInfo )
-                       let others = List.foldBack (scopes >> (@)) (Array.toList (pdbScopeGetChildren scp)) []
-                       thisOne :: others
-                 let localPdbInfos = [] (*  scopes fail for mscorlib  scopes rootScope *)
-                 // REVIEW: look through sps to get ranges? Use GetRanges?? Change AbsIL??
-                 (localPdbInfos, None, seqpoints)
-               with e ->
-                   // "* Warning: PDB info for method "+nm+" could not be read and will be ignored: "+e.Message
-                   [], None, []
+                        match pectxt.pdb with
+                        | None -> [], None, []
+                        | Some (pdbr, get_doc) ->
+                            try
+
+                                let pdbm = pdbReaderGetMethod pdbr (uncodedToken TableNames.Method idx)
+                                let sps = pdbMethodGetDebugPoints pdbm
+                                (* let roota, rootb = pdbScopeGetOffsets rootScope in *)
+                                let seqpoints =
+                                    let arr =
+                                        sps
+                                        |> Array.map (fun sp ->
+                                            // It is VERY annoying to have to call GetURL for the document for
+                                            // each sequence point. This appears to be a short coming of the PDB
+                                            // reader API. They should return an index into the array of documents for the reader
+                                            let sourcedoc = get_doc (pdbDocumentGetURL sp.pdbSeqPointDocument)
+
+                                            let source =
+                                                ILDebugPoint.Create(
+                                                    document = sourcedoc,
+                                                    line = sp.pdbSeqPointLine,
+                                                    column = sp.pdbSeqPointColumn,
+                                                    endLine = sp.pdbSeqPointEndLine,
+                                                    endColumn = sp.pdbSeqPointEndColumn
+                                                )
+
+                                            (sp.pdbSeqPointOffset, source))
+
+                                    Array.sortInPlaceBy fst arr
+
+                                    Array.toList arr
+
+                                let rec scopes scp =
+                                    let a, b = pdbScopeGetOffsets scp
+                                    let lvs = pdbScopeGetLocals scp
+
+                                    let ilvs =
+                                        lvs
+                                        |> Array.toList
+                                        |> List.filter (fun l ->
+                                            let k, _idx = pdbVariableGetAddressAttributes l
+                                            k = 1 (* ADDR_IL_OFFSET *) )
+
+                                    let ilinfos: ILLocalDebugMapping list =
+                                        ilvs
+                                        |> List.map (fun ilv ->
+                                            let _k, idx = pdbVariableGetAddressAttributes ilv
+                                            let n = pdbVariableGetName ilv
+                                            { LocalIndex = idx; LocalName = n })
+
+                                    let thisOne =
+                                        (fun raw2nextLab ->
+                                            {
+                                                Range = (raw2nextLab a, raw2nextLab b)
+                                                DebugMappings = ilinfos
+                                            }: ILLocalDebugInfo)
+
+                                    let others =
+                                        List.foldBack (scopes >> (@)) (Array.toList (pdbScopeGetChildren scp)) []
+
+                                    thisOne :: others
+
+                                let localPdbInfos =
+                                    [] (*  scopes fail for mscorlib  scopes rootScope *)
+                                // REVIEW: look through sps to get ranges? Use GetRanges?? Change AbsIL??
+                                (localPdbInfos, None, seqpoints)
+                            with
+                            | e ->
+                                // "* Warning: PDB info for method "+nm+" could not be read and will be ignored: "+e.Message
+                                [], None, []
 #endif
 
-       if isTinyFormat then
-           let codeBase = baseRVA + 1
-           let codeSize = (int32 b >>>& 2)
-           // tiny format for "+nm+", code size = " + string codeSize)
-           let instrs, _, lab2pc, raw2nextLab = seekReadTopCode ctxt pev mdv numTypars codeSize codeBase seqpoints
-
-           // Convert the linear code format to the nested code format
-           let localPdbInfos2 = List.map (fun f -> f raw2nextLab) localPdbInfos
-           let code = buildILCode nm lab2pc instrs [] localPdbInfos2
-
-           { 
-             IsZeroInit=false
-             MaxStack= 8
-             NoInlining=noinline
-             AggressiveInlining=aggressiveinline
-             Locals=List.empty
-             Code=code
-             DebugRange=methRangePdbInfo
-             DebugImports=None
-           }
-
-       else
-           let hasMoreSections = (b &&& e_CorILMethod_MoreSects) <> 0x0uy
-           let initlocals = (b &&& e_CorILMethod_InitLocals) <> 0x0uy
-           let maxstack = seekReadUInt16AsInt32 pev (baseRVA + 2)
-           let codeSize = seekReadInt32 pev (baseRVA + 4)
-           let localsTab, localToken = seekReadUncodedToken pev (baseRVA + 8)
-           let codeBase = baseRVA + 12
-           let locals =
-             if localToken = 0x0 then []
-             else
-               if localsTab <> TableNames.StandAloneSig then dprintn "strange table for locals token"
-               readBlobHeapAsLocalsSig ctxt numTypars (seekReadStandAloneSigRow ctxt pev localToken)
-
-           // fat format for "+nm+", code size = " + string codeSize+", hasMoreSections = "+(if hasMoreSections then "true" else "false")+", b = "+string b)
-
-           // Read the method body
-           let instrs, rawToLabel, lab2pc, raw2nextLab = seekReadTopCode ctxt pev mdv numTypars codeSize codeBase seqpoints
-
-           // Read all the sections that follow the method body.
-           // These contain the exception clauses.
-           let mutable nextSectionBase = align 4 (codeBase + codeSize)
-           let mutable moreSections = hasMoreSections
-           let mutable seh = []
-           while moreSections do
-             let sectionBase = nextSectionBase
-             let sectionFlag = seekReadByte pev sectionBase
-             // fat format for "+nm+", sectionFlag = " + string sectionFlag)
-             let sectionSize, clauses =
-               if (sectionFlag &&& e_CorILMethod_Sect_FatFormat) <> 0x0uy then
-                   let bigSize = (seekReadInt32 pev sectionBase) >>>& 8
-                   // bigSize = "+string bigSize)
-                   let clauses =
-                       if (sectionFlag &&& e_CorILMethod_Sect_EHTable) <> 0x0uy then
-                           // WORKAROUND: The ECMA spec says this should be
-                           // let numClauses = ((bigSize - 4)  / 24) in
-                           // but the CCI IL generator generates multiples of 24
-                           let numClauses = (bigSize  / 24)
-
-                           List.init numClauses (fun i ->
-                               let clauseBase = sectionBase + 4 + (i * 24)
-                               let kind = seekReadInt32 pev (clauseBase + 0)
-                               let st1 = seekReadInt32 pev (clauseBase + 4)
-                               let sz1 = seekReadInt32 pev (clauseBase + 8)
-                               let st2 = seekReadInt32 pev (clauseBase + 12)
-                               let sz2 = seekReadInt32 pev (clauseBase + 16)
-                               let extra = seekReadInt32 pev (clauseBase + 20)
-                               (kind, st1, sz1, st2, sz2, extra))
-                       else []
-                   bigSize, clauses
-               else
-                 let smallSize = seekReadByteAsInt32 pev (sectionBase + 0x01)
-                 let clauses =
-                   if (sectionFlag &&& e_CorILMethod_Sect_EHTable) <> 0x0uy then
-                       // WORKAROUND: The ECMA spec says this should be
-                       // let numClauses = ((smallSize - 4)  / 12) in
-                       // but the C# compiler (or some IL generator) generates multiples of 12
-                       let numClauses = (smallSize  / 12)
-                       // dprintn (nm+" has " + string numClauses + " tiny seh clauses")
-                       List.init numClauses (fun i ->
-                           let clauseBase = sectionBase + 4 + (i * 12)
-                           let kind = seekReadUInt16AsInt32 pev (clauseBase + 0)
-                           if logging then dprintn ("One tiny SEH clause, kind = "+string kind)
-                           let st1 = seekReadUInt16AsInt32 pev (clauseBase + 2)
-                           let sz1 = seekReadByteAsInt32 pev (clauseBase + 4)
-                           let st2 = seekReadUInt16AsInt32 pev (clauseBase + 5)
-                           let sz2 = seekReadByteAsInt32 pev (clauseBase + 7)
-                           let extra = seekReadInt32 pev (clauseBase + 8)
-                           (kind, st1, sz1, st2, sz2, extra))
-                   else
-                       []
-                 smallSize, clauses
-
-             // Morph together clauses that cover the same range
-             let sehClauses =
-                let sehMap = Dictionary<_, _>(clauses.Length, HashIdentity.Structural)
-
-                for (kind, st1, sz1, st2, sz2, extra) in clauses do
-                    let tryStart = rawToLabel st1
-                    let tryFinish = rawToLabel (st1 + sz1)
-                    let handlerStart = rawToLabel st2
-                    let handlerFinish = rawToLabel (st2 + sz2)
-                    let clause =
-                      if kind = e_COR_ILEXCEPTION_CLAUSE_EXCEPTION then
-                        ILExceptionClause.TypeCatch(seekReadTypeDefOrRef ctxt numTypars AsObject List.empty (uncodedTokenToTypeDefOrRefOrSpec (i32ToUncodedToken extra)), (handlerStart, handlerFinish) )
-                      elif kind = e_COR_ILEXCEPTION_CLAUSE_FILTER then
-                        let filterStart = rawToLabel extra
-                        let filterFinish = handlerStart
-                        ILExceptionClause.FilterCatch((filterStart, filterFinish), (handlerStart, handlerFinish))
-                      elif kind = e_COR_ILEXCEPTION_CLAUSE_FINALLY then
-                        ILExceptionClause.Finally(handlerStart, handlerFinish)
-                      elif kind = e_COR_ILEXCEPTION_CLAUSE_FAULT then
-                        ILExceptionClause.Fault(handlerStart, handlerFinish)
-                      else begin
-                        dprintn (ctxt.fileName + ": unknown exception handler kind: "+string kind)
-                        ILExceptionClause.Finally(handlerStart, handlerFinish)
-                      end
-
-                    let key = (tryStart, tryFinish)
-                    match sehMap.TryGetValue key with
-                    | true, prev -> sehMap[key] <- prev @ [clause]
-                    | _ -> sehMap[key] <- [clause]
-                  
-                ([], sehMap) ||> Seq.fold (fun acc (KeyValue(key, bs)) -> [ for b in bs -> {Range=key; Clause=b}: ILExceptionSpec ] @ acc)
-             seh <- sehClauses
-             moreSections <- (sectionFlag &&& e_CorILMethod_Sect_MoreSects) <> 0x0uy
-             nextSectionBase <- sectionBase + sectionSize
-
-           // Convert the linear code format to the nested code format 
-           if logging then dprintn "doing localPdbInfos2"
-           let localPdbInfos2 = List.map (fun f -> f raw2nextLab) localPdbInfos
-           if logging then dprintn "done localPdbInfos2, checking code..."
-           let code = buildILCode nm lab2pc instrs seh localPdbInfos2
-           if logging then dprintn "done checking code."
-           { 
-             IsZeroInit=initlocals
-             MaxStack= maxstack
-             NoInlining=noinline
-             AggressiveInlining=aggressiveinline
-             Locals = locals
-             Code=code
-             DebugRange=methRangePdbInfo
-             DebugImports = None
-           })
+                    if isTinyFormat then
+                        let codeBase = baseRVA + 1
+                        let codeSize = (int32 b >>>& 2)
+                        // tiny format for "+nm+", code size = " + string codeSize)
+                        let instrs, _, lab2pc, raw2nextLab =
+                            seekReadTopCode ctxt pev mdv numTypars codeSize codeBase seqpoints
+
+                        // Convert the linear code format to the nested code format
+                        let localPdbInfos2 = List.map (fun f -> f raw2nextLab) localPdbInfos
+                        let code = buildILCode nm lab2pc instrs [] localPdbInfos2
+
+                        {
+                            IsZeroInit = false
+                            MaxStack = 8
+                            NoInlining = noinline
+                            AggressiveInlining = aggressiveinline
+                            Locals = List.empty
+                            Code = code
+                            DebugRange = methRangePdbInfo
+                            DebugImports = None
+                        }
+
+                    else
+                        let hasMoreSections = (b &&& e_CorILMethod_MoreSects) <> 0x0uy
+                        let initlocals = (b &&& e_CorILMethod_InitLocals) <> 0x0uy
+                        let maxstack = seekReadUInt16AsInt32 pev (baseRVA + 2)
+                        let codeSize = seekReadInt32 pev (baseRVA + 4)
+                        let localsTab, localToken = seekReadUncodedToken pev (baseRVA + 8)
+                        let codeBase = baseRVA + 12
+
+                        let locals =
+                            if localToken = 0x0 then
+                                []
+                            else
+                                if localsTab <> TableNames.StandAloneSig then
+                                    dprintn "strange table for locals token"
+
+                                readBlobHeapAsLocalsSig ctxt numTypars (seekReadStandAloneSigRow ctxt pev localToken)
+
+                        // fat format for "+nm+", code size = " + string codeSize+", hasMoreSections = "+(if hasMoreSections then "true" else "false")+", b = "+string b)
+
+                        // Read the method body
+                        let instrs, rawToLabel, lab2pc, raw2nextLab =
+                            seekReadTopCode ctxt pev mdv numTypars codeSize codeBase seqpoints
+
+                        // Read all the sections that follow the method body.
+                        // These contain the exception clauses.
+                        let mutable nextSectionBase = align 4 (codeBase + codeSize)
+                        let mutable moreSections = hasMoreSections
+                        let mutable seh = []
+
+                        while moreSections do
+                            let sectionBase = nextSectionBase
+                            let sectionFlag = seekReadByte pev sectionBase
+                            // fat format for "+nm+", sectionFlag = " + string sectionFlag)
+                            let sectionSize, clauses =
+                                if (sectionFlag &&& e_CorILMethod_Sect_FatFormat) <> 0x0uy then
+                                    let bigSize = (seekReadInt32 pev sectionBase) >>>& 8
+                                    // bigSize = "+string bigSize)
+                                    let clauses =
+                                        if (sectionFlag &&& e_CorILMethod_Sect_EHTable) <> 0x0uy then
+                                            // WORKAROUND: The ECMA spec says this should be
+                                            // let numClauses = ((bigSize - 4)  / 24) in
+                                            // but the CCI IL generator generates multiples of 24
+                                            let numClauses = (bigSize / 24)
+
+                                            List.init numClauses (fun i ->
+                                                let clauseBase = sectionBase + 4 + (i * 24)
+                                                let kind = seekReadInt32 pev (clauseBase + 0)
+                                                let st1 = seekReadInt32 pev (clauseBase + 4)
+                                                let sz1 = seekReadInt32 pev (clauseBase + 8)
+                                                let st2 = seekReadInt32 pev (clauseBase + 12)
+                                                let sz2 = seekReadInt32 pev (clauseBase + 16)
+                                                let extra = seekReadInt32 pev (clauseBase + 20)
+                                                (kind, st1, sz1, st2, sz2, extra))
+                                        else
+                                            []
+
+                                    bigSize, clauses
+                                else
+                                    let smallSize = seekReadByteAsInt32 pev (sectionBase + 0x01)
+
+                                    let clauses =
+                                        if (sectionFlag &&& e_CorILMethod_Sect_EHTable) <> 0x0uy then
+                                            // WORKAROUND: The ECMA spec says this should be
+                                            // let numClauses = ((smallSize - 4)  / 12) in
+                                            // but the C# compiler (or some IL generator) generates multiples of 12
+                                            let numClauses = (smallSize / 12)
+                                            // dprintn (nm+" has " + string numClauses + " tiny seh clauses")
+                                            List.init numClauses (fun i ->
+                                                let clauseBase = sectionBase + 4 + (i * 12)
+                                                let kind = seekReadUInt16AsInt32 pev (clauseBase + 0)
+
+                                                if logging then
+                                                    dprintn ("One tiny SEH clause, kind = " + string kind)
+
+                                                let st1 = seekReadUInt16AsInt32 pev (clauseBase + 2)
+                                                let sz1 = seekReadByteAsInt32 pev (clauseBase + 4)
+                                                let st2 = seekReadUInt16AsInt32 pev (clauseBase + 5)
+                                                let sz2 = seekReadByteAsInt32 pev (clauseBase + 7)
+                                                let extra = seekReadInt32 pev (clauseBase + 8)
+                                                (kind, st1, sz1, st2, sz2, extra))
+                                        else
+                                            []
+
+                                    smallSize, clauses
+
+                            // Morph together clauses that cover the same range
+                            let sehClauses =
+                                let sehMap = Dictionary<_, _>(clauses.Length, HashIdentity.Structural)
+
+                                for (kind, st1, sz1, st2, sz2, extra) in clauses do
+                                    let tryStart = rawToLabel st1
+                                    let tryFinish = rawToLabel (st1 + sz1)
+                                    let handlerStart = rawToLabel st2
+                                    let handlerFinish = rawToLabel (st2 + sz2)
+
+                                    let clause =
+                                        if kind = e_COR_ILEXCEPTION_CLAUSE_EXCEPTION then
+                                            ILExceptionClause.TypeCatch(
+                                                seekReadTypeDefOrRef
+                                                    ctxt
+                                                    numTypars
+                                                    AsObject
+                                                    List.empty
+                                                    (uncodedTokenToTypeDefOrRefOrSpec (i32ToUncodedToken extra)),
+                                                (handlerStart, handlerFinish)
+                                            )
+                                        elif kind = e_COR_ILEXCEPTION_CLAUSE_FILTER then
+                                            let filterStart = rawToLabel extra
+                                            let filterFinish = handlerStart
+                                            ILExceptionClause.FilterCatch((filterStart, filterFinish), (handlerStart, handlerFinish))
+                                        elif kind = e_COR_ILEXCEPTION_CLAUSE_FINALLY then
+                                            ILExceptionClause.Finally(handlerStart, handlerFinish)
+                                        elif kind = e_COR_ILEXCEPTION_CLAUSE_FAULT then
+                                            ILExceptionClause.Fault(handlerStart, handlerFinish)
+                                        else
+                                            (dprintn (ctxt.fileName + ": unknown exception handler kind: " + string kind)
+                                             ILExceptionClause.Finally(handlerStart, handlerFinish))
+
+                                    let key = (tryStart, tryFinish)
+
+                                    match sehMap.TryGetValue key with
+                                    | true, prev -> sehMap[key] <- prev @ [ clause ]
+                                    | _ -> sehMap[key] <- [ clause ]
+
+                                ([], sehMap)
+                                ||> Seq.fold (fun acc (KeyValue (key, bs)) ->
+                                    [ for b in bs -> { Range = key; Clause = b }: ILExceptionSpec ] @ acc)
+
+                            seh <- sehClauses
+                            moreSections <- (sectionFlag &&& e_CorILMethod_Sect_MoreSects) <> 0x0uy
+                            nextSectionBase <- sectionBase + sectionSize
+
+                        // Convert the linear code format to the nested code format
+                        if logging then
+                            dprintn "doing localPdbInfos2"
+
+                        let localPdbInfos2 = List.map (fun f -> f raw2nextLab) localPdbInfos
+
+                        if logging then
+                            dprintn "done localPdbInfos2, checking code..."
+
+                        let code = buildILCode nm lab2pc instrs seh localPdbInfos2
+
+                        if logging then
+                            dprintn "done checking code."
+
+                        {
+                            IsZeroInit = initlocals
+                            MaxStack = maxstack
+                            NoInlining = noinline
+                            AggressiveInlining = aggressiveinline
+                            Locals = locals
+                            Code = code
+                            DebugRange = methRangePdbInfo
+                            DebugImports = None
+                        }
+            )
 
 and int32AsILVariantType (ctxt: ILMetadataReader) (n: int32) =
     if List.memAssoc n (Lazy.force ILVariantTypeRevMap) then
-      List.assoc n (Lazy.force ILVariantTypeRevMap)
-    elif (n &&& vt_ARRAY) <> 0x0 then ILNativeVariant.Array (int32AsILVariantType ctxt (n &&& (~~~ vt_ARRAY)))
-    elif (n &&& vt_VECTOR) <> 0x0 then ILNativeVariant.Vector (int32AsILVariantType ctxt (n &&& (~~~ vt_VECTOR)))
-    elif (n &&& vt_BYREF) <> 0x0 then ILNativeVariant.Byref (int32AsILVariantType ctxt (n &&& (~~~ vt_BYREF)))
-    else (dprintn (ctxt.fileName + ": int32AsILVariantType ctxt: unexpected variant type, n = "+string n) ; ILNativeVariant.Empty)
+        List.assoc n (Lazy.force ILVariantTypeRevMap)
+    elif (n &&& vt_ARRAY) <> 0x0 then
+        ILNativeVariant.Array(int32AsILVariantType ctxt (n &&& (~~~vt_ARRAY)))
+    elif (n &&& vt_VECTOR) <> 0x0 then
+        ILNativeVariant.Vector(int32AsILVariantType ctxt (n &&& (~~~vt_VECTOR)))
+    elif (n &&& vt_BYREF) <> 0x0 then
+        ILNativeVariant.Byref(int32AsILVariantType ctxt (n &&& (~~~vt_BYREF)))
+    else
+        (dprintn (
+            ctxt.fileName
+            + ": int32AsILVariantType ctxt: unexpected variant type, n = "
+            + string n
+         )
+
+         ILNativeVariant.Empty)
 
 and readBlobHeapAsNativeType ctxt blobIdx =
     // reading native type blob "+string blobIdx)
@@ -3106,9 +4017,11 @@ and readBlobHeapAsNativeType ctxt blobIdx =
 and sigptrGetILNativeType ctxt bytes sigptr : ILNativeType * int =
     // reading native type blob, sigptr= "+string sigptr)
     let ntbyte, sigptr = sigptrGetByte bytes sigptr
+
     if List.memAssoc ntbyte (Lazy.force ILNativeTypeMap) then
         List.assoc ntbyte (Lazy.force ILNativeTypeMap), sigptr
-    elif ntbyte = 0x0uy then ILNativeType.Empty, sigptr
+    elif ntbyte = 0x0uy then
+        ILNativeType.Empty, sigptr
     elif ntbyte = nt_CUSTOMMARSHALER then
         // reading native type blob CM1, sigptr= "+string sigptr+ ", bytes.Length = "+string bytes.Length)
         let struct (guidLen, sigptr) = sigptrGetZInt32 bytes sigptr
@@ -3128,189 +4041,256 @@ and sigptrGetILNativeType ctxt bytes sigptr : ILNativeType * int =
         // reading native type blob CM8, sigptr= "+string sigptr+", cookieStringLen = "+string ( cookieStringLen))
         let cookieString, sigptr = sigptrGetBytes cookieStringLen bytes sigptr
         // reading native type blob CM9, sigptr= "+string sigptr)
-        ILNativeType.Custom (guid, nativeTypeName, custMarshallerName, cookieString), sigptr
+        ILNativeType.Custom(guid, nativeTypeName, custMarshallerName, cookieString), sigptr
     elif ntbyte = nt_FIXEDSYSSTRING then
-      let struct (i, sigptr) = sigptrGetZInt32 bytes sigptr
-      ILNativeType.FixedSysString i, sigptr
+        let struct (i, sigptr) = sigptrGetZInt32 bytes sigptr
+        ILNativeType.FixedSysString i, sigptr
     elif ntbyte = nt_FIXEDARRAY then
-      let struct (i, sigptr) = sigptrGetZInt32 bytes sigptr
-      ILNativeType.FixedArray i, sigptr
+        let struct (i, sigptr) = sigptrGetZInt32 bytes sigptr
+        ILNativeType.FixedArray i, sigptr
     elif ntbyte = nt_SAFEARRAY then
-      (if sigptr >= bytes.Length then
-         ILNativeType.SafeArray(ILNativeVariant.Empty, None), sigptr
-       else
-         let struct (i, sigptr) = sigptrGetZInt32 bytes sigptr
-         if sigptr >= bytes.Length then
-           ILNativeType.SafeArray (int32AsILVariantType ctxt i, None), sigptr
+        (if sigptr >= bytes.Length then
+             ILNativeType.SafeArray(ILNativeVariant.Empty, None), sigptr
          else
-           let struct (len, sigptr) = sigptrGetZInt32 bytes sigptr
-           let s, sigptr = sigptrGetString len bytes sigptr
-           ILNativeType.SafeArray (int32AsILVariantType ctxt i, Some s), sigptr)
+             let struct (i, sigptr) = sigptrGetZInt32 bytes sigptr
+
+             if sigptr >= bytes.Length then
+                 ILNativeType.SafeArray(int32AsILVariantType ctxt i, None), sigptr
+             else
+                 let struct (len, sigptr) = sigptrGetZInt32 bytes sigptr
+                 let s, sigptr = sigptrGetString len bytes sigptr
+                 ILNativeType.SafeArray(int32AsILVariantType ctxt i, Some s), sigptr)
     elif ntbyte = nt_ARRAY then
-       if sigptr >= bytes.Length then
-         ILNativeType.Array(None, None), sigptr
-       else
-         let nt, sigptr =
-           let struct (u, sigptr') = sigptrGetZInt32 bytes sigptr
-           if (u = int nt_MAX) then
-             ILNativeType.Empty, sigptr'
-           else
-             // NOTE: go back to start and read native type
-             sigptrGetILNativeType ctxt bytes sigptr
-         if sigptr >= bytes.Length then
-           ILNativeType.Array (Some nt, None), sigptr
-         else
-           let struct (pnum, sigptr) = sigptrGetZInt32 bytes sigptr
-           if sigptr >= bytes.Length then
-             ILNativeType.Array (Some nt, Some(pnum, None)), sigptr
-           else
-             let struct (additive, sigptr) =
-               if sigptr >= bytes.Length then 0, sigptr
-               else sigptrGetZInt32 bytes sigptr
-             ILNativeType.Array (Some nt, Some(pnum, Some additive)), sigptr
-    else (ILNativeType.Empty, sigptr)
+        if sigptr >= bytes.Length then
+            ILNativeType.Array(None, None), sigptr
+        else
+            let nt, sigptr =
+                let struct (u, sigptr') = sigptrGetZInt32 bytes sigptr
+
+                if (u = int nt_MAX) then
+                    ILNativeType.Empty, sigptr'
+                else
+                    // NOTE: go back to start and read native type
+                    sigptrGetILNativeType ctxt bytes sigptr
+
+            if sigptr >= bytes.Length then
+                ILNativeType.Array(Some nt, None), sigptr
+            else
+                let struct (pnum, sigptr) = sigptrGetZInt32 bytes sigptr
+
+                if sigptr >= bytes.Length then
+                    ILNativeType.Array(Some nt, Some(pnum, None)), sigptr
+                else
+                    let struct (additive, sigptr) =
+                        if sigptr >= bytes.Length then
+                            0, sigptr
+                        else
+                            sigptrGetZInt32 bytes sigptr
+
+                    ILNativeType.Array(Some nt, Some(pnum, Some additive)), sigptr
+    else
+        (ILNativeType.Empty, sigptr)
 
 // Note, pectxtEager and pevEager must not be captured by the results of this function
 // As a result, reading the resource offsets in the physical file is done eagerly to avoid holding on to any resources
 and seekReadManifestResources (ctxt: ILMetadataReader) canReduceMemory (mdv: BinaryView) (pectxtEager: PEReader) (pevEager: BinaryView) =
     mkILResources
-        [ for i = 1 to ctxt.getNumRows TableNames.ManifestResource do
-             let offset, flags, nameIdx, implIdx = seekReadManifestResourceRow ctxt mdv i
-
-             let scoref = seekReadImplAsScopeRef ctxt mdv implIdx
-
-             let location =
-                match scoref with
-                | ILScopeRef.Local ->
-                    let start = pectxtEager.anyV2P ("resource", offset + pectxtEager.resourcesAddr)
-                    let resourceLength = seekReadInt32 pevEager start
-                    let offsetOfBytesFromStartOfPhysicalPEFile = start + 4
-                    let byteStorage =
-                        let bytes = pevEager.Slice(offsetOfBytesFromStartOfPhysicalPEFile, resourceLength)
-                        ByteStorage.FromByteMemoryAndCopy(bytes, useBackingMemoryMappedFile = canReduceMemory)
-                    ILResourceLocation.Local(byteStorage)
-
-                | ILScopeRef.Module mref -> ILResourceLocation.File (mref, offset)
-                | ILScopeRef.Assembly aref -> ILResourceLocation.Assembly aref
-                | _ -> failwith "seekReadManifestResources: Invalid ILScopeRef"
-
-             let r =
-               { Name= readStringHeap ctxt nameIdx
-                 Location = location
-                 Access = (if (flags &&& 0x01) <> 0x0 then ILResourceAccess.Public else ILResourceAccess.Private)
-                 CustomAttrsStored = ctxt.customAttrsReader_ManifestResource
-                 MetadataIndex = i }
-             yield r ]
-
-and seekReadNestedExportedTypes ctxt (exported: _ []) (nested: Lazy<_ []>) parentIdx =
-    mkILNestedExportedTypesLazy
-      (lazy
-            nested.Force().[parentIdx-1]
+        [
+            for i = 1 to ctxt.getNumRows TableNames.ManifestResource do
+                let offset, flags, nameIdx, implIdx = seekReadManifestResourceRow ctxt mdv i
+
+                let scoref = seekReadImplAsScopeRef ctxt mdv implIdx
+
+                let location =
+                    match scoref with
+                    | ILScopeRef.Local ->
+                        let start = pectxtEager.anyV2P ("resource", offset + pectxtEager.resourcesAddr)
+                        let resourceLength = seekReadInt32 pevEager start
+                        let offsetOfBytesFromStartOfPhysicalPEFile = start + 4
+
+                        let byteStorage =
+                            let bytes = pevEager.Slice(offsetOfBytesFromStartOfPhysicalPEFile, resourceLength)
+                            ByteStorage.FromByteMemoryAndCopy(bytes, useBackingMemoryMappedFile = canReduceMemory)
+
+                        ILResourceLocation.Local(byteStorage)
+
+                    | ILScopeRef.Module mref -> ILResourceLocation.File(mref, offset)
+                    | ILScopeRef.Assembly aref -> ILResourceLocation.Assembly aref
+                    | _ -> failwith "seekReadManifestResources: Invalid ILScopeRef"
+
+                let r =
+                    {
+                        Name = readStringHeap ctxt nameIdx
+                        Location = location
+                        Access =
+                            (if (flags &&& 0x01) <> 0x0 then
+                                 ILResourceAccess.Public
+                             else
+                                 ILResourceAccess.Private)
+                        CustomAttrsStored = ctxt.customAttrsReader_ManifestResource
+                        MetadataIndex = i
+                    }
+
+                yield r
+        ]
+
+and seekReadNestedExportedTypes ctxt (exported: _[]) (nested: Lazy<_[]>) parentIdx =
+    mkILNestedExportedTypesLazy (
+        lazy
+            nested.Force().[parentIdx - 1]
             |> List.map (fun i ->
-                let flags, _tok, nameIdx, namespaceIdx, _implIdx = exported[i-1]
-                { Name = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
-                  Access = (match typeAccessOfFlags flags with
-                            | ILTypeDefAccess.Nested n -> n
-                            | _ -> failwith "non-nested access for a nested type described as being in an auxiliary module")
-                  Nested = seekReadNestedExportedTypes ctxt exported nested i
-                  CustomAttrsStored = ctxt.customAttrsReader_ExportedType
-                  MetadataIndex = i }
-            ))
+                let flags, _tok, nameIdx, namespaceIdx, _implIdx = exported[i - 1]
+
+                {
+                    Name = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
+                    Access =
+                        (match typeAccessOfFlags flags with
+                         | ILTypeDefAccess.Nested n -> n
+                         | _ -> failwith "non-nested access for a nested type described as being in an auxiliary module")
+                    Nested = seekReadNestedExportedTypes ctxt exported nested i
+                    CustomAttrsStored = ctxt.customAttrsReader_ExportedType
+                    MetadataIndex = i
+                })
+    )
 
 and seekReadTopExportedTypes (ctxt: ILMetadataReader) =
-    mkILExportedTypesLazy
-      (lazy
+    mkILExportedTypesLazy (
+        lazy
             let mdv = ctxt.mdfile.GetView()
             let numRows = ctxt.getNumRows TableNames.ExportedType
             let exported = [| for i in 1..numRows -> seekReadExportedTypeRow ctxt mdv i |]
 
             // add each nested type id to their parent's children list
-            let nested = lazy (
-                let nested = [| for _i in 1..numRows -> [] |]
-                for i = 1 to numRows do
-                    let flags,_,_,_,TaggedIndex(tag, idx) = exported[i-1]
-                    if not (isTopTypeDef flags) && (tag = i_ExportedType) then
-                        nested[idx-1] <- i :: nested[idx-1]
-                nested)
+            let nested =
+                lazy
+                    (let nested = [| for _i in 1..numRows -> [] |]
+
+                     for i = 1 to numRows do
+                         let flags, _, _, _, TaggedIndex (tag, idx) = exported[i - 1]
+
+                         if not (isTopTypeDef flags) && (tag = i_ExportedType) then
+                             nested[idx - 1] <- i :: nested[idx - 1]
+
+                     nested)
 
             // return top exported types
-            [ for i = 1 to numRows do
-                let flags, _tok, nameIdx, namespaceIdx, implIdx = exported[i-1]
-                let (TaggedIndex(tag, _idx)) = implIdx
-
-                // if not a nested type
-                if (isTopTypeDef flags) && (tag <> i_ExportedType) then
-                    yield
-                      { ScopeRef = seekReadImplAsScopeRef ctxt mdv implIdx
-                        Name = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
-                        Attributes = enum(flags)
-                        Nested = seekReadNestedExportedTypes ctxt exported nested i
-                        CustomAttrsStored = ctxt.customAttrsReader_ExportedType
-                        MetadataIndex = i }
-            ])
+            [
+                for i = 1 to numRows do
+                    let flags, _tok, nameIdx, namespaceIdx, implIdx = exported[i - 1]
+                    let (TaggedIndex (tag, _idx)) = implIdx
+
+                    // if not a nested type
+                    if (isTopTypeDef flags) && (tag <> i_ExportedType) then
+                        yield
+                            {
+                                ScopeRef = seekReadImplAsScopeRef ctxt mdv implIdx
+                                Name = readBlobHeapAsTypeName ctxt (nameIdx, namespaceIdx)
+                                Attributes = enum (flags)
+                                Nested = seekReadNestedExportedTypes ctxt exported nested i
+                                CustomAttrsStored = ctxt.customAttrsReader_ExportedType
+                                MetadataIndex = i
+                            }
+            ]
+    )
 
 #if !FX_NO_PDB_READER
 let getPdbReader pdbDirPath fileName =
     match pdbDirPath with
     | None -> None
     | Some pdbpath ->
-         try
-              let pdbr = pdbReadOpen fileName pdbpath
-              let pdbdocs = pdbReaderGetDocuments pdbr
-
-              let tab = new Dictionary<_, _>(Array.length pdbdocs)
-              pdbdocs |> Array.iter (fun pdbdoc ->
-                  let url = pdbDocumentGetURL pdbdoc
-                  tab.[url] <-
-                      ILSourceDocument.Create(language=Some (pdbDocumentGetLanguage pdbdoc),
-                                            vendor = Some (pdbDocumentGetLanguageVendor pdbdoc),
-                                            documentType = Some (pdbDocumentGetType pdbdoc),
-                                            file = url))
-
-              let docfun url =
-                  match tab.TryGetValue url with
-                  | true, doc -> doc
-                  | _ -> failwith ("Document with URL " + url + " not found in list of documents in the PDB file")
-              Some (pdbr, docfun)
-          with e -> dprintn ("* Warning: PDB file could not be read and will be ignored: "+e.Message); None
+        try
+            let pdbr = pdbReadOpen fileName pdbpath
+            let pdbdocs = pdbReaderGetDocuments pdbr
+
+            let tab = new Dictionary<_, _>(Array.length pdbdocs)
+
+            pdbdocs
+            |> Array.iter (fun pdbdoc ->
+                let url = pdbDocumentGetURL pdbdoc
+
+                tab.[url] <-
+                    ILSourceDocument.Create(
+                        language = Some(pdbDocumentGetLanguage pdbdoc),
+                        vendor = Some(pdbDocumentGetLanguageVendor pdbdoc),
+                        documentType = Some(pdbDocumentGetType pdbdoc),
+                        file = url
+                    ))
+
+            let docfun url =
+                match tab.TryGetValue url with
+                | true, doc -> doc
+                | _ -> failwith ("Document with URL " + url + " not found in list of documents in the PDB file")
+
+            Some(pdbr, docfun)
+        with
+        | e ->
+            dprintn ("* Warning: PDB file could not be read and will be ignored: " + e.Message)
+            None
 #endif
 
 // Note, pectxtEager and pevEager must not be captured by the results of this function
-let openMetadataReader (fileName, mdfile: BinaryFile, metadataPhysLoc, peinfo, pectxtEager: PEReader, pevEager, pectxtCaptured, reduceMemoryUsage) =
+let openMetadataReader
+    (
+        fileName,
+        mdfile: BinaryFile,
+        metadataPhysLoc,
+        peinfo,
+        pectxtEager: PEReader,
+        pevEager,
+        pectxtCaptured,
+        reduceMemoryUsage
+    ) =
     let mdv = mdfile.GetView()
     let magic = seekReadUInt16AsInt32 mdv metadataPhysLoc
-    if magic <> 0x5342 then failwith (fileName + ": bad metadata magic number: " + string magic)
+
+    if magic <> 0x5342 then
+        failwith (fileName + ": bad metadata magic number: " + string magic)
+
     let magic2 = seekReadUInt16AsInt32 mdv (metadataPhysLoc + 2)
-    if magic2 <> 0x424a then failwith "bad metadata magic number"
+
+    if magic2 <> 0x424a then
+        failwith "bad metadata magic number"
+
     let _majorMetadataVersion = seekReadUInt16 mdv (metadataPhysLoc + 4)
     let _minorMetadataVersion = seekReadUInt16 mdv (metadataPhysLoc + 6)
 
     let versionLength = seekReadInt32 mdv (metadataPhysLoc + 12)
-    let ilMetadataVersion = seekReadBytes mdv (metadataPhysLoc + 16) versionLength |> Array.filter (fun b -> b <> 0uy)
+
+    let ilMetadataVersion =
+        seekReadBytes mdv (metadataPhysLoc + 16) versionLength
+        |> Array.filter (fun b -> b <> 0uy)
+
     let x = align 0x04 (16 + versionLength)
     let numStreams = seekReadUInt16AsInt32 mdv (metadataPhysLoc + x + 2)
     let streamHeadersStart = (metadataPhysLoc + x + 4)
 
     let tryFindStream name =
-      let rec look i pos =
-        if i >= numStreams then None
-        else
-          let offset = seekReadInt32 mdv (pos + 0)
-          let length = seekReadInt32 mdv (pos + 4)
-          let mutable res = true
-          let mutable fin = false
-          let mutable n = 0
-          // read and compare the stream name byte by byte
-          while not fin do
-              let c= seekReadByteAsInt32 mdv (pos + 8 + n)
-              if c = 0 then
-                  fin <- true
-              elif n >= Array.length name || c <> name[n] then
-                  res <- false
-              n <- n + 1
-          if res then Some(offset + metadataPhysLoc, length)
-          else look (i+1) (align 0x04 (pos + 8 + n))
-      look 0 streamHeadersStart
+        let rec look i pos =
+            if i >= numStreams then
+                None
+            else
+                let offset = seekReadInt32 mdv (pos + 0)
+                let length = seekReadInt32 mdv (pos + 4)
+                let mutable res = true
+                let mutable fin = false
+                let mutable n = 0
+                // read and compare the stream name byte by byte
+                while not fin do
+                    let c = seekReadByteAsInt32 mdv (pos + 8 + n)
+
+                    if c = 0 then
+                        fin <- true
+                    elif n >= Array.length name || c <> name[n] then
+                        res <- false
+
+                    n <- n + 1
+
+                if res then
+                    Some(offset + metadataPhysLoc, length)
+                else
+                    look (i + 1) (align 0x04 (pos + 8 + n))
+
+        look 0 streamHeadersStart
 
     let findStream name =
         match tryFindStream name with
@@ -3318,100 +4298,111 @@ let openMetadataReader (fileName, mdfile: BinaryFile, metadataPhysLoc, peinfo, p
         | Some positions -> positions
 
     let tablesStreamPhysLoc, _tablesStreamSize =
-      match tryFindStream [| 0x23; 0x7e |] (* #~ *) with
-      | Some res -> res
-      | None ->
-        match tryFindStream [| 0x23; 0x2d |] (* #-: at least one DLL I've seen uses this! *) with
+        match tryFindStream [| 0x23; 0x7e |] (* #~ *)  with
         | Some res -> res
         | None ->
-         let firstStreamOffset = seekReadInt32 mdv (streamHeadersStart + 0)
-         let firstStreamLength = seekReadInt32 mdv (streamHeadersStart + 4)
-         firstStreamOffset, firstStreamLength
+            match tryFindStream [| 0x23; 0x2d |] (* #-: at least one DLL I've seen uses this! *)  with
+            | Some res -> res
+            | None ->
+                let firstStreamOffset = seekReadInt32 mdv (streamHeadersStart + 0)
+                let firstStreamLength = seekReadInt32 mdv (streamHeadersStart + 4)
+                firstStreamOffset, firstStreamLength
 
-    let stringsStreamPhysicalLoc, stringsStreamSize = findStream [| 0x23; 0x53; 0x74; 0x72; 0x69; 0x6e; 0x67; 0x73; |] (* #Strings *)
-    let userStringsStreamPhysicalLoc, userStringsStreamSize = findStream [| 0x23; 0x55; 0x53; |] (* #US *)
-    let guidsStreamPhysicalLoc, _guidsStreamSize = findStream [| 0x23; 0x47; 0x55; 0x49; 0x44; |] (* #GUID *)
-    let blobsStreamPhysicalLoc, blobsStreamSize = findStream [| 0x23; 0x42; 0x6c; 0x6f; 0x62; |] (* #Blob *)
+    let stringsStreamPhysicalLoc, stringsStreamSize =
+        findStream [| 0x23; 0x53; 0x74; 0x72; 0x69; 0x6e; 0x67; 0x73 |] (* #Strings *)
+
+    let userStringsStreamPhysicalLoc, userStringsStreamSize =
+        findStream [| 0x23; 0x55; 0x53 |] (* #US *)
+
+    let guidsStreamPhysicalLoc, _guidsStreamSize =
+        findStream [| 0x23; 0x47; 0x55; 0x49; 0x44 |] (* #GUID *)
+
+    let blobsStreamPhysicalLoc, blobsStreamSize =
+        findStream [| 0x23; 0x42; 0x6c; 0x6f; 0x62 |] (* #Blob *)
 
     let tableKinds =
-        [|kindModule (* Table 0 *)
-          kindTypeRef (* Table 1 *)
-          kindTypeDef (* Table 2 *)
-          kindIllegal (* kindFieldPtr *) (* Table 3 *)
-          kindFieldDef (* Table 4 *)
-          kindIllegal (* kindMethodPtr *) (* Table 5 *)
-          kindMethodDef (* Table 6 *)
-          kindIllegal (* kindParamPtr *) (* Table 7 *)
-          kindParam (* Table 8 *)
-          kindInterfaceImpl (* Table 9 *)
-          kindMemberRef (* Table 10 *)
-          kindConstant (* Table 11 *)
-          kindCustomAttribute (* Table 12 *)
-          kindFieldMarshal (* Table 13 *)
-          kindDeclSecurity (* Table 14 *)
-          kindClassLayout (* Table 15 *)
-          kindFieldLayout (* Table 16 *)
-          kindStandAloneSig (* Table 17 *)
-          kindEventMap (* Table 18 *)
-          kindIllegal (* kindEventPtr *) (* Table 19 *)
-          kindEvent (* Table 20 *)
-          kindPropertyMap (* Table 21 *)
-          kindIllegal (* kindPropertyPtr *) (* Table 22 *)
-          kindProperty (* Table 23 *)
-          kindMethodSemantics (* Table 24 *)
-          kindMethodImpl (* Table 25 *)
-          kindModuleRef (* Table 26 *)
-          kindTypeSpec (* Table 27 *)
-          kindImplMap (* Table 28 *)
-          kindFieldRVA (* Table 29 *)
-          kindIllegal (* kindENCLog *) (* Table 30 *)
-          kindIllegal (* kindENCMap *) (* Table 31 *)
-          kindAssembly (* Table 32 *)
-          kindIllegal (* kindAssemblyProcessor *) (* Table 33 *)
-          kindIllegal (* kindAssemblyOS *) (* Table 34 *)
-          kindAssemblyRef (* Table 35 *)
-          kindIllegal (* kindAssemblyRefProcessor *) (* Table 36 *)
-          kindIllegal (* kindAssemblyRefOS *) (* Table 37 *)
-          kindFileRef (* Table 38 *)
-          kindExportedType (* Table 39 *)
-          kindManifestResource (* Table 40 *)
-          kindNested (* Table 41 *)
-          kindGenericParam_v2_0 (* Table 42 *)
-          kindMethodSpec (* Table 43 *)
-          kindGenericParamConstraint (* Table 44 *)
-          kindIllegal (* Table 45 *)
-          kindIllegal (* Table 46 *)
-          kindIllegal (* Table 47 *)
-          kindIllegal (* Table 48 *)
-          kindIllegal (* Table 49 *)
-          kindIllegal (* Table 50 *)
-          kindIllegal (* Table 51 *)
-          kindIllegal (* Table 52 *)
-          kindIllegal (* Table 53 *)
-          kindIllegal (* Table 54 *)
-          kindIllegal (* Table 55 *)
-          kindIllegal (* Table 56 *)
-          kindIllegal (* Table 57 *)
-          kindIllegal (* Table 58 *)
-          kindIllegal (* Table 59 *)
-          kindIllegal (* Table 60 *)
-          kindIllegal (* Table 61 *)
-          kindIllegal (* Table 62 *)
-          kindIllegal (* Table 63 *)
+        [|
+            kindModule (* Table 0 *)
+            kindTypeRef (* Table 1 *)
+            kindTypeDef (* Table 2 *)
+            kindIllegal (* kindFieldPtr *) (* Table 3 *)
+            kindFieldDef (* Table 4 *)
+            kindIllegal (* kindMethodPtr *) (* Table 5 *)
+            kindMethodDef (* Table 6 *)
+            kindIllegal (* kindParamPtr *) (* Table 7 *)
+            kindParam (* Table 8 *)
+            kindInterfaceImpl (* Table 9 *)
+            kindMemberRef (* Table 10 *)
+            kindConstant (* Table 11 *)
+            kindCustomAttribute (* Table 12 *)
+            kindFieldMarshal (* Table 13 *)
+            kindDeclSecurity (* Table 14 *)
+            kindClassLayout (* Table 15 *)
+            kindFieldLayout (* Table 16 *)
+            kindStandAloneSig (* Table 17 *)
+            kindEventMap (* Table 18 *)
+            kindIllegal (* kindEventPtr *) (* Table 19 *)
+            kindEvent (* Table 20 *)
+            kindPropertyMap (* Table 21 *)
+            kindIllegal (* kindPropertyPtr *) (* Table 22 *)
+            kindProperty (* Table 23 *)
+            kindMethodSemantics (* Table 24 *)
+            kindMethodImpl (* Table 25 *)
+            kindModuleRef (* Table 26 *)
+            kindTypeSpec (* Table 27 *)
+            kindImplMap (* Table 28 *)
+            kindFieldRVA (* Table 29 *)
+            kindIllegal (* kindENCLog *) (* Table 30 *)
+            kindIllegal (* kindENCMap *) (* Table 31 *)
+            kindAssembly (* Table 32 *)
+            kindIllegal (* kindAssemblyProcessor *) (* Table 33 *)
+            kindIllegal (* kindAssemblyOS *) (* Table 34 *)
+            kindAssemblyRef (* Table 35 *)
+            kindIllegal (* kindAssemblyRefProcessor *) (* Table 36 *)
+            kindIllegal (* kindAssemblyRefOS *) (* Table 37 *)
+            kindFileRef (* Table 38 *)
+            kindExportedType (* Table 39 *)
+            kindManifestResource (* Table 40 *)
+            kindNested (* Table 41 *)
+            kindGenericParam_v2_0 (* Table 42 *)
+            kindMethodSpec (* Table 43 *)
+            kindGenericParamConstraint (* Table 44 *)
+            kindIllegal (* Table 45 *)
+            kindIllegal (* Table 46 *)
+            kindIllegal (* Table 47 *)
+            kindIllegal (* Table 48 *)
+            kindIllegal (* Table 49 *)
+            kindIllegal (* Table 50 *)
+            kindIllegal (* Table 51 *)
+            kindIllegal (* Table 52 *)
+            kindIllegal (* Table 53 *)
+            kindIllegal (* Table 54 *)
+            kindIllegal (* Table 55 *)
+            kindIllegal (* Table 56 *)
+            kindIllegal (* Table 57 *)
+            kindIllegal (* Table 58 *)
+            kindIllegal (* Table 59 *)
+            kindIllegal (* Table 60 *)
+            kindIllegal (* Table 61 *)
+            kindIllegal (* Table 62 *)
+            kindIllegal (* Table 63 *)
         |]
 
     let heapSizes = seekReadByteAsInt32 mdv (tablesStreamPhysLoc + 6)
     let valid = seekReadInt64 mdv (tablesStreamPhysLoc + 8)
     let sorted = seekReadInt64 mdv (tablesStreamPhysLoc + 16)
+
     let tablesPresent, tableRowCount, startOfTables =
         let mutable present = []
         let numRows = Array.create 64 0
         let mutable prevNumRowIdx = tablesStreamPhysLoc + 24
+
         for i = 0 to 63 do
             if (valid &&& (int64 1 <<< i)) <> int64 0 then
                 present <- i :: present
                 numRows[i] <- (seekReadInt32 mdv prevNumRowIdx)
                 prevNumRowIdx <- prevNumRowIdx + 4
+
         List.rev present, numRows, prevNumRowIdx
 
     let getNumRows (tab: TableName) = tableRowCount[tab.Index]
@@ -3420,100 +4411,97 @@ let openMetadataReader (fileName, mdfile: BinaryFile, metadataPhysLoc, peinfo, p
     let guidsBigness = (heapSizes &&& 2) <> 0
     let blobsBigness = (heapSizes &&& 4) <> 0
 
-    if logging then dprintn (fileName + ": numTables = "+string numTables)
-    if logging && stringsBigness then dprintn (fileName + ": strings are big")
-    if logging && blobsBigness then dprintn (fileName + ": blobs are big")
+    if logging then
+        dprintn (fileName + ": numTables = " + string numTables)
+
+    if logging && stringsBigness then
+        dprintn (fileName + ": strings are big")
+
+    if logging && blobsBigness then
+        dprintn (fileName + ": blobs are big")
 
     let tableBigness = Array.map (fun n -> n >= 0x10000) tableRowCount
 
     let codedBigness nbits tab =
-      let rows = getNumRows tab
-      rows >= (0x10000 >>>& nbits)
+        let rows = getNumRows tab
+        rows >= (0x10000 >>>& nbits)
 
     let tdorBigness =
-      codedBigness 2 TableNames.TypeDef ||
-      codedBigness 2 TableNames.TypeRef ||
-      codedBigness 2 TableNames.TypeSpec
+        codedBigness 2 TableNames.TypeDef
+        || codedBigness 2 TableNames.TypeRef
+        || codedBigness 2 TableNames.TypeSpec
 
     let tomdBigness =
-      codedBigness 1 TableNames.TypeDef ||
-      codedBigness 1 TableNames.Method
+        codedBigness 1 TableNames.TypeDef || codedBigness 1 TableNames.Method
 
     let hcBigness =
-      codedBigness 2 TableNames.Field ||
-      codedBigness 2 TableNames.Param ||
-      codedBigness 2 TableNames.Property
+        codedBigness 2 TableNames.Field
+        || codedBigness 2 TableNames.Param
+        || codedBigness 2 TableNames.Property
 
     let hcaBigness =
-      codedBigness 5 TableNames.Method ||
-      codedBigness 5 TableNames.Field ||
-      codedBigness 5 TableNames.TypeRef ||
-      codedBigness 5 TableNames.TypeDef ||
-      codedBigness 5 TableNames.Param ||
-      codedBigness 5 TableNames.InterfaceImpl ||
-      codedBigness 5 TableNames.MemberRef ||
-      codedBigness 5 TableNames.Module ||
-      codedBigness 5 TableNames.Permission ||
-      codedBigness 5 TableNames.Property ||
-      codedBigness 5 TableNames.Event ||
-      codedBigness 5 TableNames.StandAloneSig ||
-      codedBigness 5 TableNames.ModuleRef ||
-      codedBigness 5 TableNames.TypeSpec ||
-      codedBigness 5 TableNames.Assembly ||
-      codedBigness 5 TableNames.AssemblyRef ||
-      codedBigness 5 TableNames.File ||
-      codedBigness 5 TableNames.ExportedType ||
-      codedBigness 5 TableNames.ManifestResource ||
-      codedBigness 5 TableNames.GenericParam ||
-      codedBigness 5 TableNames.GenericParamConstraint ||
-      codedBigness 5 TableNames.MethodSpec
-
-
-    let hfmBigness =
-      codedBigness 1 TableNames.Field ||
-      codedBigness 1 TableNames.Param
+        codedBigness 5 TableNames.Method
+        || codedBigness 5 TableNames.Field
+        || codedBigness 5 TableNames.TypeRef
+        || codedBigness 5 TableNames.TypeDef
+        || codedBigness 5 TableNames.Param
+        || codedBigness 5 TableNames.InterfaceImpl
+        || codedBigness 5 TableNames.MemberRef
+        || codedBigness 5 TableNames.Module
+        || codedBigness 5 TableNames.Permission
+        || codedBigness 5 TableNames.Property
+        || codedBigness 5 TableNames.Event
+        || codedBigness 5 TableNames.StandAloneSig
+        || codedBigness 5 TableNames.ModuleRef
+        || codedBigness 5 TableNames.TypeSpec
+        || codedBigness 5 TableNames.Assembly
+        || codedBigness 5 TableNames.AssemblyRef
+        || codedBigness 5 TableNames.File
+        || codedBigness 5 TableNames.ExportedType
+        || codedBigness 5 TableNames.ManifestResource
+        || codedBigness 5 TableNames.GenericParam
+        || codedBigness 5 TableNames.GenericParamConstraint
+        || codedBigness 5 TableNames.MethodSpec
+
+    let hfmBigness = codedBigness 1 TableNames.Field || codedBigness 1 TableNames.Param
 
     let hdsBigness =
-      codedBigness 2 TableNames.TypeDef ||
-      codedBigness 2 TableNames.Method ||
-      codedBigness 2 TableNames.Assembly
+        codedBigness 2 TableNames.TypeDef
+        || codedBigness 2 TableNames.Method
+        || codedBigness 2 TableNames.Assembly
 
     let mrpBigness =
-      codedBigness 3 TableNames.TypeDef ||
-      codedBigness 3 TableNames.TypeRef ||
-      codedBigness 3 TableNames.ModuleRef ||
-      codedBigness 3 TableNames.Method ||
-      codedBigness 3 TableNames.TypeSpec
+        codedBigness 3 TableNames.TypeDef
+        || codedBigness 3 TableNames.TypeRef
+        || codedBigness 3 TableNames.ModuleRef
+        || codedBigness 3 TableNames.Method
+        || codedBigness 3 TableNames.TypeSpec
 
     let hsBigness =
-      codedBigness 1 TableNames.Event ||
-      codedBigness 1 TableNames.Property
+        codedBigness 1 TableNames.Event || codedBigness 1 TableNames.Property
 
     let mdorBigness =
-      codedBigness 1 TableNames.Method ||
-      codedBigness 1 TableNames.MemberRef
+        codedBigness 1 TableNames.Method || codedBigness 1 TableNames.MemberRef
 
-    let mfBigness =
-      codedBigness 1 TableNames.Field ||
-      codedBigness 1 TableNames.Method
+    let mfBigness = codedBigness 1 TableNames.Field || codedBigness 1 TableNames.Method
 
     let iBigness =
-      codedBigness 2 TableNames.File ||
-      codedBigness 2 TableNames.AssemblyRef ||
-      codedBigness 2 TableNames.ExportedType
+        codedBigness 2 TableNames.File
+        || codedBigness 2 TableNames.AssemblyRef
+        || codedBigness 2 TableNames.ExportedType
 
     let catBigness =
-      codedBigness 3 TableNames.Method ||
-      codedBigness 3 TableNames.MemberRef
+        codedBigness 3 TableNames.Method || codedBigness 3 TableNames.MemberRef
 
     let rsBigness =
-      codedBigness 2 TableNames.Module ||
-      codedBigness 2 TableNames.ModuleRef ||
-      codedBigness 2 TableNames.AssemblyRef ||
-      codedBigness 2 TableNames.TypeRef
+        codedBigness 2 TableNames.Module
+        || codedBigness 2 TableNames.ModuleRef
+        || codedBigness 2 TableNames.AssemblyRef
+        || codedBigness 2 TableNames.TypeRef
 
     let rowKindSize (RowKind kinds) =
-      kinds |> List.sumBy (fun x ->
+        kinds
+        |> List.sumBy (fun x ->
             match x with
             | UShort -> 2
             | ULong -> 4
@@ -3540,121 +4528,182 @@ let openMetadataReader (fileName, mdfile: BinaryFile, metadataPhysLoc, peinfo, p
     let tableRowSizes = tableKinds |> Array.map rowKindSize
 
     let tablePhysLocations =
-         let res = Array.create 64 0x0
-         let mutable prevTablePhysLoc = startOfTables
-         for i = 0 to 63 do
-             res[i] <- prevTablePhysLoc
-             prevTablePhysLoc <- prevTablePhysLoc + (tableRowCount[i] * tableRowSizes[i])
-         res
+        let res = Array.create 64 0x0
+        let mutable prevTablePhysLoc = startOfTables
+
+        for i = 0 to 63 do
+            res[i] <- prevTablePhysLoc
+            prevTablePhysLoc <- prevTablePhysLoc + (tableRowCount[i] * tableRowSizes[i])
+
+        res
 
     let inbase = FileSystemUtils.fileNameOfPath fileName + ": "
 
     // All the caches. The sizes are guesstimates for the rough sharing-density of the assembly
-    let cacheAssemblyRef = mkCacheInt32 false inbase "ILAssemblyRef" (getNumRows TableNames.AssemblyRef)
-    let cacheMethodSpecAsMethodData = mkCacheGeneric reduceMemoryUsage inbase "MethodSpecAsMethodData" (getNumRows TableNames.MethodSpec / 20 + 1)
-    let cacheMemberRefAsMemberData = mkCacheGeneric reduceMemoryUsage inbase "MemberRefAsMemberData" (getNumRows TableNames.MemberRef / 20 + 1)
-    let cacheCustomAttr = mkCacheGeneric reduceMemoryUsage inbase "CustomAttr" (getNumRows TableNames.CustomAttribute / 50 + 1)
-    let cacheTypeRef = mkCacheInt32 false inbase "ILTypeRef" (getNumRows TableNames.TypeRef / 20 + 1)
-    let cacheTypeRefAsType = mkCacheGeneric reduceMemoryUsage inbase "TypeRefAsType" (getNumRows TableNames.TypeRef / 20 + 1)
-    let cacheBlobHeapAsPropertySig = mkCacheGeneric reduceMemoryUsage inbase "BlobHeapAsPropertySig" (getNumRows TableNames.Property / 20 + 1)
-    let cacheBlobHeapAsFieldSig = mkCacheGeneric reduceMemoryUsage inbase "BlobHeapAsFieldSig" (getNumRows TableNames.Field / 20 + 1)
-    let cacheBlobHeapAsMethodSig = mkCacheGeneric reduceMemoryUsage inbase "BlobHeapAsMethodSig" (getNumRows TableNames.Method / 20 + 1)
-    let cacheTypeDefAsType = mkCacheGeneric reduceMemoryUsage inbase "TypeDefAsType" (getNumRows TableNames.TypeDef / 20 + 1)
-    let cacheMethodDefAsMethodData = mkCacheInt32 reduceMemoryUsage inbase "MethodDefAsMethodData" (getNumRows TableNames.Method / 20 + 1)
-    let cacheGenericParams = mkCacheGeneric reduceMemoryUsage inbase "GenericParams" (getNumRows TableNames.GenericParam / 20 + 1)
-    let cacheFieldDefAsFieldSpec = mkCacheInt32 reduceMemoryUsage inbase "FieldDefAsFieldSpec" (getNumRows TableNames.Field / 20 + 1)
-    let cacheUserStringHeap = mkCacheInt32 reduceMemoryUsage inbase "UserStringHeap" ( userStringsStreamSize / 20 + 1)
+    let cacheAssemblyRef =
+        mkCacheInt32 false inbase "ILAssemblyRef" (getNumRows TableNames.AssemblyRef)
+
+    let cacheMethodSpecAsMethodData =
+        mkCacheGeneric reduceMemoryUsage inbase "MethodSpecAsMethodData" (getNumRows TableNames.MethodSpec / 20 + 1)
+
+    let cacheMemberRefAsMemberData =
+        mkCacheGeneric reduceMemoryUsage inbase "MemberRefAsMemberData" (getNumRows TableNames.MemberRef / 20 + 1)
+
+    let cacheCustomAttr =
+        mkCacheGeneric reduceMemoryUsage inbase "CustomAttr" (getNumRows TableNames.CustomAttribute / 50 + 1)
+
+    let cacheTypeRef =
+        mkCacheInt32 false inbase "ILTypeRef" (getNumRows TableNames.TypeRef / 20 + 1)
+
+    let cacheTypeRefAsType =
+        mkCacheGeneric reduceMemoryUsage inbase "TypeRefAsType" (getNumRows TableNames.TypeRef / 20 + 1)
+
+    let cacheBlobHeapAsPropertySig =
+        mkCacheGeneric reduceMemoryUsage inbase "BlobHeapAsPropertySig" (getNumRows TableNames.Property / 20 + 1)
+
+    let cacheBlobHeapAsFieldSig =
+        mkCacheGeneric reduceMemoryUsage inbase "BlobHeapAsFieldSig" (getNumRows TableNames.Field / 20 + 1)
+
+    let cacheBlobHeapAsMethodSig =
+        mkCacheGeneric reduceMemoryUsage inbase "BlobHeapAsMethodSig" (getNumRows TableNames.Method / 20 + 1)
+
+    let cacheTypeDefAsType =
+        mkCacheGeneric reduceMemoryUsage inbase "TypeDefAsType" (getNumRows TableNames.TypeDef / 20 + 1)
+
+    let cacheMethodDefAsMethodData =
+        mkCacheInt32 reduceMemoryUsage inbase "MethodDefAsMethodData" (getNumRows TableNames.Method / 20 + 1)
+
+    let cacheGenericParams =
+        mkCacheGeneric reduceMemoryUsage inbase "GenericParams" (getNumRows TableNames.GenericParam / 20 + 1)
+
+    let cacheFieldDefAsFieldSpec =
+        mkCacheInt32 reduceMemoryUsage inbase "FieldDefAsFieldSpec" (getNumRows TableNames.Field / 20 + 1)
+
+    let cacheUserStringHeap =
+        mkCacheInt32 reduceMemoryUsage inbase "UserStringHeap" (userStringsStreamSize / 20 + 1)
     // nb. Lots and lots of cache hits on this cache, hence never optimize cache away
-    let cacheStringHeap = mkCacheInt32 false inbase "string heap" ( stringsStreamSize / 50 + 1)
-    let cacheBlobHeap = mkCacheInt32 reduceMemoryUsage inbase "blob heap" ( blobsStreamSize / 50 + 1)
+    let cacheStringHeap =
+        mkCacheInt32 false inbase "string heap" (stringsStreamSize / 50 + 1)
+
+    let cacheBlobHeap =
+        mkCacheInt32 reduceMemoryUsage inbase "blob heap" (blobsStreamSize / 50 + 1)
+
+    // These tables are not required to enforce sharing fo the final data
+    // structure, but are very useful as searching these tables gives rise to many reads
+    // in standard applications.
 
-     // These tables are not required to enforce sharing fo the final data
-     // structure, but are very useful as searching these tables gives rise to many reads
-     // in standard applications.
+    let cacheNestedRow =
+        mkCacheInt32 reduceMemoryUsage inbase "Nested Table Rows" (getNumRows TableNames.Nested / 20 + 1)
 
-    let cacheNestedRow = mkCacheInt32 reduceMemoryUsage inbase "Nested Table Rows" (getNumRows TableNames.Nested / 20 + 1)
-    let cacheConstantRow = mkCacheInt32 reduceMemoryUsage inbase "Constant Rows" (getNumRows TableNames.Constant / 20 + 1)
-    let cacheMethodSemanticsRow = mkCacheInt32 reduceMemoryUsage inbase "MethodSemantics Rows" (getNumRows TableNames.MethodSemantics / 20 + 1)
-    let cacheTypeDefRow = mkCacheInt32 reduceMemoryUsage inbase "ILTypeDef Rows" (getNumRows TableNames.TypeDef / 20 + 1)
+    let cacheConstantRow =
+        mkCacheInt32 reduceMemoryUsage inbase "Constant Rows" (getNumRows TableNames.Constant / 20 + 1)
 
-    let rowAddr (tab: TableName) idx = tablePhysLocations[tab.Index] + (idx - 1) * tableRowSizes[tab.Index]
+    let cacheMethodSemanticsRow =
+        mkCacheInt32 reduceMemoryUsage inbase "MethodSemantics Rows" (getNumRows TableNames.MethodSemantics / 20 + 1)
+
+    let cacheTypeDefRow =
+        mkCacheInt32 reduceMemoryUsage inbase "ILTypeDef Rows" (getNumRows TableNames.TypeDef / 20 + 1)
+
+    let rowAddr (tab: TableName) idx =
+        tablePhysLocations[tab.Index] + (idx - 1) * tableRowSizes[tab.Index]
 
     // Build the reader context
     // Use an initialization hole
     let ctxtH = ref None
+
     let ctxt: ILMetadataReader =
-        { sorted=sorted
-          getNumRows=getNumRows
-          mdfile=mdfile
-          dataEndPoints = match pectxtCaptured with None -> notlazy [] | Some pectxt -> getDataEndPointsDelayed pectxt ctxtH
-          pectxtCaptured=pectxtCaptured
-          entryPointToken=pectxtEager.entryPointToken
-          fileName=fileName
-          userStringsStreamPhysicalLoc = userStringsStreamPhysicalLoc
-          stringsStreamPhysicalLoc = stringsStreamPhysicalLoc
-          blobsStreamPhysicalLoc = blobsStreamPhysicalLoc
-          blobsStreamSize = blobsStreamSize
-          memoizeString = Tables.memoize id
-          readUserStringHeap = cacheUserStringHeap (readUserStringHeapUncached ctxtH)
-          readStringHeap = cacheStringHeap (readStringHeapUncached ctxtH)
-          readBlobHeap = cacheBlobHeap (readBlobHeapUncached ctxtH)
-          seekReadNestedRow = cacheNestedRow (seekReadNestedRowUncached ctxtH)
-          seekReadConstantRow = cacheConstantRow (seekReadConstantRowUncached ctxtH)
-          seekReadMethodSemanticsRow = cacheMethodSemanticsRow (seekReadMethodSemanticsRowUncached ctxtH)
-          seekReadTypeDefRow = cacheTypeDefRow (seekReadTypeDefRowUncached ctxtH)
-          seekReadAssemblyRef = cacheAssemblyRef (seekReadAssemblyRefUncached ctxtH)
-          seekReadMethodSpecAsMethodData = cacheMethodSpecAsMethodData (seekReadMethodSpecAsMethodDataUncached ctxtH)
-          seekReadMemberRefAsMethodData = cacheMemberRefAsMemberData (seekReadMemberRefAsMethodDataUncached ctxtH)
-          seekReadMemberRefAsFieldSpec = seekReadMemberRefAsFieldSpecUncached ctxtH
-          seekReadCustomAttr = cacheCustomAttr (seekReadCustomAttrUncached ctxtH)
-          seekReadTypeRef = cacheTypeRef (seekReadTypeRefUncached ctxtH)
-          readBlobHeapAsPropertySig = cacheBlobHeapAsPropertySig (readBlobHeapAsPropertySigUncached ctxtH)
-          readBlobHeapAsFieldSig = cacheBlobHeapAsFieldSig (readBlobHeapAsFieldSigUncached ctxtH)
-          readBlobHeapAsMethodSig = cacheBlobHeapAsMethodSig (readBlobHeapAsMethodSigUncached ctxtH)
-          readBlobHeapAsLocalsSig = readBlobHeapAsLocalsSigUncached ctxtH
-          seekReadTypeDefAsType = cacheTypeDefAsType (seekReadTypeDefAsTypeUncached ctxtH)
-          seekReadTypeRefAsType = cacheTypeRefAsType (seekReadTypeRefAsTypeUncached ctxtH)
-          seekReadMethodDefAsMethodData = cacheMethodDefAsMethodData (seekReadMethodDefAsMethodDataUncached ctxtH)
-          seekReadGenericParams = cacheGenericParams (seekReadGenericParamsUncached ctxtH)
-          seekReadFieldDefAsFieldSpec = cacheFieldDefAsFieldSpec (seekReadFieldDefAsFieldSpecUncached ctxtH)
-          customAttrsReader_Module = customAttrsReader ctxtH hca_Module
-          customAttrsReader_Assembly = customAttrsReader ctxtH hca_Assembly
-          customAttrsReader_TypeDef = customAttrsReader ctxtH hca_TypeDef
-          customAttrsReader_GenericParam= customAttrsReader ctxtH hca_GenericParam
-          customAttrsReader_FieldDef= customAttrsReader ctxtH hca_FieldDef
-          customAttrsReader_MethodDef= customAttrsReader ctxtH hca_MethodDef
-          customAttrsReader_ParamDef= customAttrsReader ctxtH hca_ParamDef
-          customAttrsReader_Event= customAttrsReader ctxtH hca_Event
-          customAttrsReader_Property= customAttrsReader ctxtH hca_Property
-          customAttrsReader_ManifestResource= customAttrsReader ctxtH hca_ManifestResource
-          customAttrsReader_ExportedType= customAttrsReader ctxtH hca_ExportedType
-          securityDeclsReader_TypeDef = securityDeclsReader ctxtH hds_TypeDef
-          securityDeclsReader_MethodDef = securityDeclsReader ctxtH hds_MethodDef
-          securityDeclsReader_Assembly = securityDeclsReader ctxtH hds_Assembly
-          typeDefReader = typeDefReader ctxtH
-          guidsStreamPhysicalLoc = guidsStreamPhysicalLoc
-          rowAddr=rowAddr
-          rsBigness=rsBigness
-          tdorBigness=tdorBigness
-          tomdBigness=tomdBigness
-          hcBigness=hcBigness
-          hcaBigness=hcaBigness
-          hfmBigness=hfmBigness
-          hdsBigness=hdsBigness
-          mrpBigness=mrpBigness
-          hsBigness=hsBigness
-          mdorBigness=mdorBigness
-          mfBigness=mfBigness
-          iBigness=iBigness
-          catBigness=catBigness
-          stringsBigness=stringsBigness
-          guidsBigness=guidsBigness
-          blobsBigness=blobsBigness
-          tableBigness=tableBigness }
+        {
+            sorted = sorted
+            getNumRows = getNumRows
+            mdfile = mdfile
+            dataEndPoints =
+                match pectxtCaptured with
+                | None -> notlazy []
+                | Some pectxt -> getDataEndPointsDelayed pectxt ctxtH
+            pectxtCaptured = pectxtCaptured
+            entryPointToken = pectxtEager.entryPointToken
+            fileName = fileName
+            userStringsStreamPhysicalLoc = userStringsStreamPhysicalLoc
+            stringsStreamPhysicalLoc = stringsStreamPhysicalLoc
+            blobsStreamPhysicalLoc = blobsStreamPhysicalLoc
+            blobsStreamSize = blobsStreamSize
+            memoizeString = Tables.memoize id
+            readUserStringHeap = cacheUserStringHeap (readUserStringHeapUncached ctxtH)
+            readStringHeap = cacheStringHeap (readStringHeapUncached ctxtH)
+            readBlobHeap = cacheBlobHeap (readBlobHeapUncached ctxtH)
+            seekReadNestedRow = cacheNestedRow (seekReadNestedRowUncached ctxtH)
+            seekReadConstantRow = cacheConstantRow (seekReadConstantRowUncached ctxtH)
+            seekReadMethodSemanticsRow = cacheMethodSemanticsRow (seekReadMethodSemanticsRowUncached ctxtH)
+            seekReadTypeDefRow = cacheTypeDefRow (seekReadTypeDefRowUncached ctxtH)
+            seekReadAssemblyRef = cacheAssemblyRef (seekReadAssemblyRefUncached ctxtH)
+            seekReadMethodSpecAsMethodData = cacheMethodSpecAsMethodData (seekReadMethodSpecAsMethodDataUncached ctxtH)
+            seekReadMemberRefAsMethodData = cacheMemberRefAsMemberData (seekReadMemberRefAsMethodDataUncached ctxtH)
+            seekReadMemberRefAsFieldSpec = seekReadMemberRefAsFieldSpecUncached ctxtH
+            seekReadCustomAttr = cacheCustomAttr (seekReadCustomAttrUncached ctxtH)
+            seekReadTypeRef = cacheTypeRef (seekReadTypeRefUncached ctxtH)
+            readBlobHeapAsPropertySig = cacheBlobHeapAsPropertySig (readBlobHeapAsPropertySigUncached ctxtH)
+            readBlobHeapAsFieldSig = cacheBlobHeapAsFieldSig (readBlobHeapAsFieldSigUncached ctxtH)
+            readBlobHeapAsMethodSig = cacheBlobHeapAsMethodSig (readBlobHeapAsMethodSigUncached ctxtH)
+            readBlobHeapAsLocalsSig = readBlobHeapAsLocalsSigUncached ctxtH
+            seekReadTypeDefAsType = cacheTypeDefAsType (seekReadTypeDefAsTypeUncached ctxtH)
+            seekReadTypeRefAsType = cacheTypeRefAsType (seekReadTypeRefAsTypeUncached ctxtH)
+            seekReadMethodDefAsMethodData = cacheMethodDefAsMethodData (seekReadMethodDefAsMethodDataUncached ctxtH)
+            seekReadGenericParams = cacheGenericParams (seekReadGenericParamsUncached ctxtH)
+            seekReadFieldDefAsFieldSpec = cacheFieldDefAsFieldSpec (seekReadFieldDefAsFieldSpecUncached ctxtH)
+            customAttrsReader_Module = customAttrsReader ctxtH hca_Module
+            customAttrsReader_Assembly = customAttrsReader ctxtH hca_Assembly
+            customAttrsReader_TypeDef = customAttrsReader ctxtH hca_TypeDef
+            customAttrsReader_GenericParam = customAttrsReader ctxtH hca_GenericParam
+            customAttrsReader_FieldDef = customAttrsReader ctxtH hca_FieldDef
+            customAttrsReader_MethodDef = customAttrsReader ctxtH hca_MethodDef
+            customAttrsReader_ParamDef = customAttrsReader ctxtH hca_ParamDef
+            customAttrsReader_Event = customAttrsReader ctxtH hca_Event
+            customAttrsReader_Property = customAttrsReader ctxtH hca_Property
+            customAttrsReader_ManifestResource = customAttrsReader ctxtH hca_ManifestResource
+            customAttrsReader_ExportedType = customAttrsReader ctxtH hca_ExportedType
+            securityDeclsReader_TypeDef = securityDeclsReader ctxtH hds_TypeDef
+            securityDeclsReader_MethodDef = securityDeclsReader ctxtH hds_MethodDef
+            securityDeclsReader_Assembly = securityDeclsReader ctxtH hds_Assembly
+            typeDefReader = typeDefReader ctxtH
+            guidsStreamPhysicalLoc = guidsStreamPhysicalLoc
+            rowAddr = rowAddr
+            rsBigness = rsBigness
+            tdorBigness = tdorBigness
+            tomdBigness = tomdBigness
+            hcBigness = hcBigness
+            hcaBigness = hcaBigness
+            hfmBigness = hfmBigness
+            hdsBigness = hdsBigness
+            mrpBigness = mrpBigness
+            hsBigness = hsBigness
+            mdorBigness = mdorBigness
+            mfBigness = mfBigness
+            iBigness = iBigness
+            catBigness = catBigness
+            stringsBigness = stringsBigness
+            guidsBigness = guidsBigness
+            blobsBigness = blobsBigness
+            tableBigness = tableBigness
+        }
+
     ctxtH.Value <- Some ctxt
 
-    let ilModule = seekReadModule ctxt reduceMemoryUsage pectxtEager pevEager peinfo (Encoding.UTF8.GetString (ilMetadataVersion, 0, ilMetadataVersion.Length)) 1
-    let ilAssemblyRefs = lazy [ for i in 1 .. getNumRows TableNames.AssemblyRef do yield seekReadAssemblyRef ctxt i ]
+    let ilModule =
+        seekReadModule
+            ctxt
+            reduceMemoryUsage
+            pectxtEager
+            pevEager
+            peinfo
+            (Encoding.UTF8.GetString(ilMetadataVersion, 0, ilMetadataVersion.Length))
+            1
+
+    let ilAssemblyRefs =
+        lazy
+            [
+                for i in 1 .. getNumRows TableNames.AssemblyRef do
+                    yield seekReadAssemblyRef ctxt i
+            ]
 
     ilModule, ilAssemblyRefs
 
@@ -3672,130 +4721,222 @@ let openPEFileReader (fileName, pefile: BinaryFile, pdbDirPath, noFileOnDisk) =
     let peFileHeaderPhysLoc = peSignaturePhysLoc + 0x04
     let peOptionalHeaderPhysLoc = peFileHeaderPhysLoc + 0x14
     let peSignature = seekReadInt32 pev (peSignaturePhysLoc + 0)
-    if peSignature <> 0x4550 then failwithf "not a PE file - bad magic PE number 0x%08x, is = %A" peSignature pev
 
+    if peSignature <> 0x4550 then
+        failwithf "not a PE file - bad magic PE number 0x%08x, is = %A" peSignature pev
 
     (* PE SIGNATURE *)
     let machine = seekReadUInt16AsInt32 pev (peFileHeaderPhysLoc + 0)
     let numSections = seekReadUInt16AsInt32 pev (peFileHeaderPhysLoc + 2)
     let headerSizeOpt = seekReadUInt16AsInt32 pev (peFileHeaderPhysLoc + 16)
-    if headerSizeOpt <> 0xe0 &&
-       headerSizeOpt <> 0xf0 then failwith "not a PE file - bad optional header size"
+
+    if headerSizeOpt <> 0xe0 && headerSizeOpt <> 0xf0 then
+        failwith "not a PE file - bad optional header size"
+
     let x64adjust = headerSizeOpt - 0xe0
-    let only64 = (headerSizeOpt = 0xf0) (* May want to read in the optional header Magic number and check that as well... *)
-    let platform = match machine with | 0x8664 -> Some AMD64 | 0x200 -> Some IA64 | _ -> Some X86
+
+    let only64 =
+        (headerSizeOpt = 0xf0) (* May want to read in the optional header Magic number and check that as well... *)
+
+    let platform =
+        match machine with
+        | 0x8664 -> Some AMD64
+        | 0x200 -> Some IA64
+        | _ -> Some X86
+
     let sectionHeadersStartPhysLoc = peOptionalHeaderPhysLoc + headerSizeOpt
 
     let flags = seekReadUInt16AsInt32 pev (peFileHeaderPhysLoc + 18)
     let isDll = (flags &&& 0x2000) <> 0x0
 
-   (* OPTIONAL PE HEADER *)
-    let _textPhysSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 4) (* Size of the code (text) section, or the sum of all code sections if there are multiple sections. *)
-     (* x86: 000000a0 *)
-    let _initdataPhysSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 8) (* Size of the initialized data section, or the sum of all such sections if there are multiple data sections. *)
-    let _uninitdataPhysSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 12) (* Size of the uninitialized data section, or the sum of all such sections if there are multiple data sections. *)
-    let _entrypointAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 16) (* RVA of entry point, needs to point to bytes 0xFF 0x25 followed by the RVA+!0x4000000 in a section marked execute/read for EXEs or 0 for DLLs e.g. 0x0000b57e *)
-    let _textAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 20) (* e.g. 0x0002000 *)
-     (* x86: 000000b0 *)
-    let dataSegmentAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 24) (* e.g. 0x0000c000 *)
+    (* OPTIONAL PE HEADER *)
+    let _textPhysSize =
+        seekReadInt32
+            pev
+            (peOptionalHeaderPhysLoc + 4) (* Size of the code (text) section, or the sum of all code sections if there are multiple sections. *)
+    (* x86: 000000a0 *)
+    let _initdataPhysSize =
+        seekReadInt32
+            pev
+            (peOptionalHeaderPhysLoc + 8) (* Size of the initialized data section, or the sum of all such sections if there are multiple data sections. *)
+
+    let _uninitdataPhysSize =
+        seekReadInt32
+            pev
+            (peOptionalHeaderPhysLoc + 12) (* Size of the uninitialized data section, or the sum of all such sections if there are multiple data sections. *)
+
+    let _entrypointAddr =
+        seekReadInt32
+            pev
+            (peOptionalHeaderPhysLoc + 16) (* RVA of entry point, needs to point to bytes 0xFF 0x25 followed by the RVA+!0x4000000 in a section marked execute/read for EXEs or 0 for DLLs e.g. 0x0000b57e *)
+
+    let _textAddr =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 20) (* e.g. 0x0002000 *)
+    (* x86: 000000b0 *)
+    let dataSegmentAddr =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 24) (* e.g. 0x0000c000 *)
     (* REVIEW: For now, we'll use the DWORD at offset 24 for x64. This currently ok since fsc doesn't support true 64-bit image bases,
         but we'll have to fix this up when such support is added. *)
-    let imageBaseReal = if only64 then dataSegmentAddr else seekReadInt32 pev (peOptionalHeaderPhysLoc + 28)  // Image Base Always 0x400000 (see Section 23.1).
-    let alignVirt = seekReadInt32 pev (peOptionalHeaderPhysLoc + 32)   //  Section Alignment Always 0x2000 (see Section 23.1).
-    let alignPhys = seekReadInt32 pev (peOptionalHeaderPhysLoc + 36)  // File Alignment Either 0x200 or 0x1000.
-     (* x86: 000000c0 *)
-    let _osMajor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 40)   //  OS Major Always 4 (see Section 23.1).
-    let _osMinor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 42)   // OS Minor Always 0 (see Section 23.1).
-    let _userMajor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 44)   // User Major Always 0 (see Section 23.1).
-    let _userMinor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 46)   // User Minor Always 0 (see Section 23.1).
-    let subsysMajor = seekReadUInt16AsInt32 pev (peOptionalHeaderPhysLoc + 48)   // SubSys Major Always 4 (see Section 23.1).
-    let subsysMinor = seekReadUInt16AsInt32 pev (peOptionalHeaderPhysLoc + 50)   // SubSys Minor Always 0 (see Section 23.1).
-     (* x86: 000000d0 *)
-    let _imageEndAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 56)  // Image Size: Size, in bytes, of image, including all headers and padding
-    let _headerPhysSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 60)  // Header Size Combined size of MS-DOS Header, PE Header, PE Optional Header and padding
-    let subsys = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 68)   // SubSystem Subsystem required to run this image.
+    let imageBaseReal =
+        if only64 then
+            dataSegmentAddr
+        else
+            seekReadInt32 pev (peOptionalHeaderPhysLoc + 28) // Image Base Always 0x400000 (see Section 23.1).
+
+    let alignVirt = seekReadInt32 pev (peOptionalHeaderPhysLoc + 32) //  Section Alignment Always 0x2000 (see Section 23.1).
+    let alignPhys = seekReadInt32 pev (peOptionalHeaderPhysLoc + 36) // File Alignment Either 0x200 or 0x1000.
+    (* x86: 000000c0 *)
+    let _osMajor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 40) //  OS Major Always 4 (see Section 23.1).
+    let _osMinor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 42) // OS Minor Always 0 (see Section 23.1).
+    let _userMajor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 44) // User Major Always 0 (see Section 23.1).
+    let _userMinor = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 46) // User Minor Always 0 (see Section 23.1).
+    let subsysMajor = seekReadUInt16AsInt32 pev (peOptionalHeaderPhysLoc + 48) // SubSys Major Always 4 (see Section 23.1).
+    let subsysMinor = seekReadUInt16AsInt32 pev (peOptionalHeaderPhysLoc + 50) // SubSys Minor Always 0 (see Section 23.1).
+    (* x86: 000000d0 *)
+    let _imageEndAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 56) // Image Size: Size, in bytes, of image, including all headers and padding
+    let _headerPhysSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 60) // Header Size Combined size of MS-DOS Header, PE Header, PE Optional Header and padding
+    let subsys = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 68) // SubSystem Subsystem required to run this image.
+
     let useHighEnthropyVA =
         let n = seekReadUInt16 pev (peOptionalHeaderPhysLoc + 70)
         let highEnthropyVA = 0x20us
         (n &&& highEnthropyVA) = highEnthropyVA
 
-     (* x86: 000000e0 *)
+    (* x86: 000000e0 *)
 
     (* WARNING: THESE ARE 64 bit ON x64/ia64 *)
     (* REVIEW: If we ever decide that we need these values for x64, we'll have to read them in as 64bit and fix up the rest of the offsets.
         Then again, it should suffice to just use the defaults, and still not bother... *)
-    (* let stackReserve = seekReadInt32 is (peOptionalHeaderPhysLoc + 72) in *) (* Stack Reserve Size Always 0x100000 (1Mb) (see Section 23.1). *)
-    (* let stackCommit = seekReadInt32 is (peOptionalHeaderPhysLoc + 76) in *) (* Stack Commit Size Always 0x1000 (4Kb) (see Section 23.1). *)
-    (* let heapReserve = seekReadInt32 is (peOptionalHeaderPhysLoc + 80) in *) (* Heap Reserve Size Always 0x100000 (1Mb) (see Section 23.1). *)
-    (* let heapCommit = seekReadInt32 is (peOptionalHeaderPhysLoc + 84) in *) (* Heap Commit Size Always 0x1000 (4Kb) (see Section 23.1). *)
-
-     (* x86: 000000f0, x64: 00000100 *)
-    let _numDataDirectories = seekReadInt32 pev (peOptionalHeaderPhysLoc + 92 + x64adjust) (* Number of Data Directories: Always 0x10 (see Section 23.1). *)
-     (* 00000100 - these addresses are for x86 - for the x64 location, add x64adjust (0x10) *)
-    let _importTableAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 104 + x64adjust) (* Import Table RVA of Import Table, (see clause 24.3.1). e.g. 0000b530 *)
-    let _importTableSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 108 + x64adjust) (* Size of Import Table, (see clause 24.3.1). *)
-    let nativeResourcesAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 112 + x64adjust)
-    let nativeResourcesSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 116 + x64adjust)
-     (* 00000110 *)
-     (* 00000120 *)
-  (* let base_relocTableNames.addr = seekReadInt32 is (peOptionalHeaderPhysLoc + 136)
+    (* let stackReserve = seekReadInt32 is (peOptionalHeaderPhysLoc + 72) in *)
+    (* Stack Reserve Size Always 0x100000 (1Mb) (see Section 23.1). *)
+    (* let stackCommit = seekReadInt32 is (peOptionalHeaderPhysLoc + 76) in *)
+    (* Stack Commit Size Always 0x1000 (4Kb) (see Section 23.1). *)
+    (* let heapReserve = seekReadInt32 is (peOptionalHeaderPhysLoc + 80) in *)
+    (* Heap Reserve Size Always 0x100000 (1Mb) (see Section 23.1). *)
+    (* let heapCommit = seekReadInt32 is (peOptionalHeaderPhysLoc + 84) in *)
+    (* Heap Commit Size Always 0x1000 (4Kb) (see Section 23.1). *)
+
+    (* x86: 000000f0, x64: 00000100 *)
+    let _numDataDirectories =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 92 + x64adjust) (* Number of Data Directories: Always 0x10 (see Section 23.1). *)
+    (* 00000100 - these addresses are for x86 - for the x64 location, add x64adjust (0x10) *)
+    let _importTableAddr =
+        seekReadInt32
+            pev
+            (peOptionalHeaderPhysLoc + 104 + x64adjust) (* Import Table RVA of Import Table, (see clause 24.3.1). e.g. 0000b530 *)
+
+    let _importTableSize =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 108 + x64adjust) (* Size of Import Table, (see clause 24.3.1). *)
+
+    let nativeResourcesAddr =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 112 + x64adjust)
+
+    let nativeResourcesSize =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 116 + x64adjust)
+    (* 00000110 *)
+    (* 00000120 *)
+    (* let base_relocTableNames.addr = seekReadInt32 is (peOptionalHeaderPhysLoc + 136)
     let base_relocTableNames.size = seekReadInt32 is (peOptionalHeaderPhysLoc + 140) in *)
-     (* 00000130 *)
-     (* 00000140 *)
-     (* 00000150 *)
-    let _importAddrTableAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 192 + x64adjust) (* RVA of Import Addr Table, (see clause 24.3.1). e.g. 0x00002000 *)
-    let _importAddrTableSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 196 + x64adjust) (* Size of Import Addr Table, (see clause 24.3.1). e.g. 0x00002000 *)
-     (* 00000160 *)
+    (* 00000130 *)
+    (* 00000140 *)
+    (* 00000150 *)
+    let _importAddrTableAddr =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 192 + x64adjust) (* RVA of Import Addr Table, (see clause 24.3.1). e.g. 0x00002000 *)
+
+    let _importAddrTableSize =
+        seekReadInt32 pev (peOptionalHeaderPhysLoc + 196 + x64adjust) (* Size of Import Addr Table, (see clause 24.3.1). e.g. 0x00002000 *)
+    (* 00000160 *)
     let cliHeaderAddr = seekReadInt32 pev (peOptionalHeaderPhysLoc + 208 + x64adjust)
     let _cliHeaderSize = seekReadInt32 pev (peOptionalHeaderPhysLoc + 212 + x64adjust)
-     (* 00000170 *)
-
+    (* 00000170 *)
 
     (* Crack section headers *)
 
     let sectionHeaders =
-      [ for i in 0 .. numSections-1 do
-          let pos = sectionHeadersStartPhysLoc + i * 0x28
-          let virtSize = seekReadInt32 pev (pos + 8)
-          let virtAddr = seekReadInt32 pev (pos + 12)
-          let physLoc = seekReadInt32 pev (pos + 20)
-          yield (virtAddr, virtSize, physLoc) ]
+        [
+            for i in 0 .. numSections - 1 do
+                let pos = sectionHeadersStartPhysLoc + i * 0x28
+                let virtSize = seekReadInt32 pev (pos + 8)
+                let virtAddr = seekReadInt32 pev (pos + 12)
+                let physLoc = seekReadInt32 pev (pos + 20)
+                yield (virtAddr, virtSize, physLoc)
+        ]
 
     let findSectionHeader addr =
-      let rec look i pos =
-        if i >= numSections then 0x0
-        else
-          let virtSize = seekReadInt32 pev (pos + 8)
-          let virtAddr = seekReadInt32 pev (pos + 12)
-          if (addr >= virtAddr && addr < virtAddr + virtSize) then pos
-          else look (i+1) (pos + 0x28)
-      look 0 sectionHeadersStartPhysLoc
+        let rec look i pos =
+            if i >= numSections then
+                0x0
+            else
+                let virtSize = seekReadInt32 pev (pos + 8)
+                let virtAddr = seekReadInt32 pev (pos + 12)
+
+                if (addr >= virtAddr && addr < virtAddr + virtSize) then
+                    pos
+                else
+                    look (i + 1) (pos + 0x28)
+
+        look 0 sectionHeadersStartPhysLoc
 
     let textHeaderStart = findSectionHeader cliHeaderAddr
     let dataHeaderStart = findSectionHeader dataSegmentAddr
-  (* let relocHeaderStart = findSectionHeader base_relocTableNames.addr in *)
+    (* let relocHeaderStart = findSectionHeader base_relocTableNames.addr in *)
+
+    let _textSize =
+        if textHeaderStart = 0x0 then
+            0x0
+        else
+            seekReadInt32 pev (textHeaderStart + 8)
 
-    let _textSize = if textHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (textHeaderStart + 8)
-    let _textAddr = if textHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (textHeaderStart + 12)
-    let textSegmentPhysicalSize = if textHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (textHeaderStart + 16)
-    let textSegmentPhysicalLoc = if textHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (textHeaderStart + 20)
+    let _textAddr =
+        if textHeaderStart = 0x0 then
+            0x0
+        else
+            seekReadInt32 pev (textHeaderStart + 12)
+
+    let textSegmentPhysicalSize =
+        if textHeaderStart = 0x0 then
+            0x0
+        else
+            seekReadInt32 pev (textHeaderStart + 16)
+
+    let textSegmentPhysicalLoc =
+        if textHeaderStart = 0x0 then
+            0x0
+        else
+            seekReadInt32 pev (textHeaderStart + 20)
 
     //let dataSegmentSize = if dataHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (dataHeaderStart + 8)
     //let dataSegmentAddr = if dataHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (dataHeaderStart + 12)
-    let dataSegmentPhysicalSize = if dataHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (dataHeaderStart + 16)
-    let dataSegmentPhysicalLoc = if dataHeaderStart = 0x0 then 0x0 else seekReadInt32 pev (dataHeaderStart + 20)
+    let dataSegmentPhysicalSize =
+        if dataHeaderStart = 0x0 then
+            0x0
+        else
+            seekReadInt32 pev (dataHeaderStart + 16)
 
-    let anyV2P (n, v) =
-      let pev = pefile.GetView()
-      let rec look i pos =
-        if i >= numSections then (failwith (fileName + ": bad "+n+", rva "+string v); 0x0)
+    let dataSegmentPhysicalLoc =
+        if dataHeaderStart = 0x0 then
+            0x0
         else
-          let virtSize = seekReadInt32 pev (pos + 8)
-          let virtAddr = seekReadInt32 pev (pos + 12)
-          let physLoc = seekReadInt32 pev (pos + 20)
-          if (v >= virtAddr && (v < virtAddr + virtSize)) then (v - virtAddr) + physLoc
-          else look (i+1) (pos + 0x28)
-      look 0 sectionHeadersStartPhysLoc
+            seekReadInt32 pev (dataHeaderStart + 20)
+
+    let anyV2P (n, v) =
+        let pev = pefile.GetView()
+
+        let rec look i pos =
+            if i >= numSections then
+                (failwith (fileName + ": bad " + n + ", rva " + string v)
+                 0x0)
+            else
+                let virtSize = seekReadInt32 pev (pos + 8)
+                let virtAddr = seekReadInt32 pev (pos + 12)
+                let physLoc = seekReadInt32 pev (pos + 20)
+
+                if (v >= virtAddr && (v < virtAddr + virtSize)) then
+                    (v - virtAddr) + physLoc
+                else
+                    look (i + 1) (pos + 0x28)
+
+        look 0 sectionHeadersStartPhysLoc
 
     let cliHeaderPhysLoc = anyV2P ("cli header", cliHeaderAddr)
 
@@ -3819,18 +4960,29 @@ let openPEFileReader (fileName, pefile: BinaryFile, pdbDirPath, noFileOnDisk) =
     let vtableFixupsAddr = seekReadInt32 pev (cliHeaderPhysLoc + 40)
     let _vtableFixupsSize = seekReadInt32 pev (cliHeaderPhysLoc + 44)
 
-    if logging then dprintn (fileName + ": metadataAddr = "+string metadataAddr)
-    if logging then dprintn (fileName + ": resourcesAddr = "+string resourcesAddr)
-    if logging then dprintn (fileName + ": resourcesSize = "+string resourcesSize)
-    if logging then dprintn (fileName + ": nativeResourcesAddr = "+string nativeResourcesAddr)
-    if logging then dprintn (fileName + ": nativeResourcesSize = "+string nativeResourcesSize)
+    if logging then
+        dprintn (fileName + ": metadataAddr = " + string metadataAddr)
+
+    if logging then
+        dprintn (fileName + ": resourcesAddr = " + string resourcesAddr)
+
+    if logging then
+        dprintn (fileName + ": resourcesSize = " + string resourcesSize)
+
+    if logging then
+        dprintn (fileName + ": nativeResourcesAddr = " + string nativeResourcesAddr)
+
+    if logging then
+        dprintn (fileName + ": nativeResourcesSize = " + string nativeResourcesSize)
 
     let metadataPhysLoc = anyV2P ("metadata", metadataAddr)
-   //-----------------------------------------------------------------------
-   // Set up the PDB reader so we can read debug info for methods.
-   // ----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
+    // Set up the PDB reader so we can read debug info for methods.
+    // ----------------------------------------------------------------------
 #if FX_NO_PDB_READER
-    let pdb = ignore pdbDirPath; None
+    let pdb =
+        ignore pdbDirPath
+        None
 #else
     let pdb =
         if runningOnMono then
@@ -3840,30 +4992,49 @@ let openPEFileReader (fileName, pefile: BinaryFile, pdbDirPath, noFileOnDisk) =
 #endif
 
     let pectxt: PEReader =
-        { pdb=pdb
-          textSegmentPhysicalLoc=textSegmentPhysicalLoc
-          textSegmentPhysicalSize=textSegmentPhysicalSize
-          dataSegmentPhysicalLoc=dataSegmentPhysicalLoc
-          dataSegmentPhysicalSize=dataSegmentPhysicalSize
-          anyV2P=anyV2P
-          metadataAddr=metadataAddr
-          sectionHeaders=sectionHeaders
-          nativeResourcesAddr=nativeResourcesAddr
-          nativeResourcesSize=nativeResourcesSize
-          resourcesAddr=resourcesAddr
-          strongnameAddr=strongnameAddr
-          vtableFixupsAddr=vtableFixupsAddr
-          pefile=pefile
-          fileName=fileName
-          entryPointToken=entryPointToken
-          noFileOnDisk=noFileOnDisk
+        {
+            pdb = pdb
+            textSegmentPhysicalLoc = textSegmentPhysicalLoc
+            textSegmentPhysicalSize = textSegmentPhysicalSize
+            dataSegmentPhysicalLoc = dataSegmentPhysicalLoc
+            dataSegmentPhysicalSize = dataSegmentPhysicalSize
+            anyV2P = anyV2P
+            metadataAddr = metadataAddr
+            sectionHeaders = sectionHeaders
+            nativeResourcesAddr = nativeResourcesAddr
+            nativeResourcesSize = nativeResourcesSize
+            resourcesAddr = resourcesAddr
+            strongnameAddr = strongnameAddr
+            vtableFixupsAddr = vtableFixupsAddr
+            pefile = pefile
+            fileName = fileName
+            entryPointToken = entryPointToken
+            noFileOnDisk = noFileOnDisk
         }
-    let peinfo = (subsys, (subsysMajor, subsysMinor), useHighEnthropyVA, ilOnly, only32, is32bitpreferred, only64, platform, isDll, alignVirt, alignPhys, imageBaseReal)
+
+    let peinfo =
+        (subsys,
+         (subsysMajor, subsysMinor),
+         useHighEnthropyVA,
+         ilOnly,
+         only32,
+         is32bitpreferred,
+         only64,
+         platform,
+         isDll,
+         alignVirt,
+         alignPhys,
+         imageBaseReal)
+
     (metadataPhysLoc, metadataSize, peinfo, pectxt, pev, pdb)
 
 let openPE (fileName, pefile, pdbDirPath, reduceMemoryUsage, noFileOnDisk) =
-    let metadataPhysLoc, _metadataSize, peinfo, pectxt, pev, pdb = openPEFileReader (fileName, pefile, pdbDirPath, noFileOnDisk)
-    let ilModule, ilAssemblyRefs = openMetadataReader (fileName, pefile, metadataPhysLoc, peinfo, pectxt, pev, Some pectxt, reduceMemoryUsage)
+    let metadataPhysLoc, _metadataSize, peinfo, pectxt, pev, pdb =
+        openPEFileReader (fileName, pefile, pdbDirPath, noFileOnDisk)
+
+    let ilModule, ilAssemblyRefs =
+        openMetadataReader (fileName, pefile, metadataPhysLoc, peinfo, pectxt, pev, Some pectxt, reduceMemoryUsage)
+
     ilModule, ilAssemblyRefs, pdb
 
 let openPEMetadataOnly (fileName, peinfo, pectxtEager, pevEager, mdfile: BinaryFile, reduceMemoryUsage) =
@@ -3880,19 +5051,25 @@ let ClosePdbReader pdb =
 #endif
 
 type ILReaderMetadataSnapshot = obj * nativeint * int
-type ILReaderTryGetMetadataSnapshot = (* path: *) string * (* snapshotTimeStamp: *) DateTime -> ILReaderMetadataSnapshot option
+type ILReaderTryGetMetadataSnapshot = (* path: *) string (* snapshotTimeStamp: *)  * DateTime -> ILReaderMetadataSnapshot option
 
 []
-type MetadataOnlyFlag = Yes | No
+type MetadataOnlyFlag =
+    | Yes
+    | No
 
 []
-type ReduceMemoryFlag = Yes | No
+type ReduceMemoryFlag =
+    | Yes
+    | No
 
 type ILReaderOptions =
-    { pdbDirPath: string option
-      reduceMemoryUsage: ReduceMemoryFlag
-      metadataOnly: MetadataOnlyFlag
-      tryGetMetadataSnapshot: ILReaderTryGetMetadataSnapshot }
+    {
+        pdbDirPath: string option
+        reduceMemoryUsage: ReduceMemoryFlag
+        metadataOnly: MetadataOnlyFlag
+        tryGetMetadataSnapshot: ILReaderTryGetMetadataSnapshot
+    }
 
 type ILModuleReader =
     abstract ILModuleDef: ILModuleDef
@@ -3906,53 +5083,66 @@ type ILModuleReaderImpl(ilModule: ILModuleDef, ilAssemblyRefs: Lazy
-       (stronglyHeldReaderCacheSize,
-        keepMax=stronglyHeldReaderCacheSize, // only strong entries
-        areSimilar=(fun (x, y) -> x = y))
+    AgedLookup(
+        stronglyHeldReaderCacheSize,
+        keepMax = stronglyHeldReaderCacheSize, // only strong entries
+        areSimilar = (fun (x, y) -> x = y)
+    )
+
 let ilModuleReaderCache1Lock = Lock()
 
 // // Cache to reuse readers that have already been created and are not yet GC'd
-let ilModuleReaderCache2 = ConcurrentDictionary>(HashIdentity.Structural)
+let ilModuleReaderCache2 =
+    ConcurrentDictionary>(HashIdentity.Structural)
 
 let stableFileHeuristicApplies fileName =
-    not noStableFileHeuristic && try FileSystem.IsStableFileHeuristic fileName with _ -> false
+    not noStableFileHeuristic
+    && try
+        FileSystem.IsStableFileHeuristic fileName
+       with
+       | _ -> false
 
 let createByteFileChunk opts fileName chunk =
     // If we're trying to reduce memory usage then we are willing to go back and re-read the binary, so we can use
     // a weakly-held handle to an array of bytes.
-    if opts.reduceMemoryUsage = ReduceMemoryFlag.Yes && stableFileHeuristicApplies fileName then
+    if opts.reduceMemoryUsage = ReduceMemoryFlag.Yes
+       && stableFileHeuristicApplies fileName then
         WeakByteFile(fileName, chunk) :> BinaryFile
     else
         let bytes =
             use stream = FileSystem.OpenFileForReadShim(fileName)
+
             match chunk with
             | None -> stream.ReadAllBytes()
-            | Some(start, length) -> stream.ReadBytes(start, length)
+            | Some (start, length) -> stream.ReadBytes(start, length)
 
         ByteFile(fileName, bytes) :> BinaryFile
 
 let getBinaryFile fileName useMemoryMappedFile =
-    let stream = FileSystem.OpenFileForReadShim(fileName, useMemoryMappedFile = useMemoryMappedFile)
+    let stream =
+        FileSystem.OpenFileForReadShim(fileName, useMemoryMappedFile = useMemoryMappedFile)
+
     let byteMem = stream.AsByteMemory()
 
     let safeHolder =
         { new obj() with
-            override x.Finalize() =
-                (x :?> IDisposable).Dispose()
+            override x.Finalize() = (x :?> IDisposable).Dispose()
           interface IDisposable with
-            member x.Dispose() =
-                GC.SuppressFinalize x
-                stream.Dispose()
-                stats.memoryMapFileClosedCount <- stats.memoryMapFileClosedCount + 1 }
+              member x.Dispose() =
+                  GC.SuppressFinalize x
+                  stream.Dispose()
+                  stats.memoryMapFileClosedCount <- stats.memoryMapFileClosedCount + 1
+        }
 
     stats.memoryMapFileOpenedCount <- stats.memoryMapFileOpenedCount + 1
 
@@ -3960,36 +5150,57 @@ let getBinaryFile fileName useMemoryMappedFile =
 
 let OpenILModuleReaderFromBytes fileName assemblyContents options =
     let pefile = ByteFile(fileName, assemblyContents) :> BinaryFile
-    let ilModule, ilAssemblyRefs, pdb = openPE (fileName, pefile, options.pdbDirPath, (options.reduceMemoryUsage = ReduceMemoryFlag.Yes), true)
+
+    let ilModule, ilAssemblyRefs, pdb =
+        openPE (fileName, pefile, options.pdbDirPath, (options.reduceMemoryUsage = ReduceMemoryFlag.Yes), true)
+
     new ILModuleReaderImpl(ilModule, ilAssemblyRefs, (fun () -> ClosePdbReader pdb)) :> ILModuleReader
 
 let OpenILModuleReaderFromStream fileName (peStream: Stream) options =
-    let peReader = new System.Reflection.PortableExecutable.PEReader(peStream, PEStreamOptions.PrefetchEntireImage)
+    let peReader =
+        new System.Reflection.PortableExecutable.PEReader(peStream, PEStreamOptions.PrefetchEntireImage)
+
     let pefile = PEFile(fileName, peReader) :> BinaryFile
-    let ilModule, ilAssemblyRefs, pdb = openPE (fileName, pefile, options.pdbDirPath, (options.reduceMemoryUsage = ReduceMemoryFlag.Yes), true)
+
+    let ilModule, ilAssemblyRefs, pdb =
+        openPE (fileName, pefile, options.pdbDirPath, (options.reduceMemoryUsage = ReduceMemoryFlag.Yes), true)
+
     new ILModuleReaderImpl(ilModule, ilAssemblyRefs, (fun () -> ClosePdbReader pdb)) :> ILModuleReader
 
-let ClearAllILModuleReaderCache() =
+let ClearAllILModuleReaderCache () =
     ilModuleReaderCache1.Clear(ILModuleReaderCache1LockToken())
     ilModuleReaderCache2.Clear()
 
 let OpenILModuleReader fileName opts =
     // Pseudo-normalize the paths.
-    let ILModuleReaderCacheKey (fullPath,writeStamp,_,_,_) as key, keyOk =
+    let ILModuleReaderCacheKey (fullPath, writeStamp, _, _, _) as key, keyOk =
         try
-           let fullPath = FileSystem.GetFullPathShim fileName
-           let writeTime = FileSystem.GetLastWriteTimeShim fileName
-           let key = ILModuleReaderCacheKey (fullPath, writeTime, opts.pdbDirPath.IsSome, opts.reduceMemoryUsage, opts.metadataOnly)
-           key, true
-        with exn ->
-            Debug.Assert(false, sprintf "Failed to compute key in OpenILModuleReader cache for '%s'. Falling back to uncached. Error = %s" fileName (exn.ToString()))
-            let fakeKey = ILModuleReaderCacheKey(fileName, DateTime.UtcNow, false, ReduceMemoryFlag.Yes, MetadataOnlyFlag.Yes)
+            let fullPath = FileSystem.GetFullPathShim fileName
+            let writeTime = FileSystem.GetLastWriteTimeShim fileName
+
+            let key =
+                ILModuleReaderCacheKey(fullPath, writeTime, opts.pdbDirPath.IsSome, opts.reduceMemoryUsage, opts.metadataOnly)
+
+            key, true
+        with
+        | exn ->
+            Debug.Assert(
+                false,
+                sprintf
+                    "Failed to compute key in OpenILModuleReader cache for '%s'. Falling back to uncached. Error = %s"
+                    fileName
+                    (exn.ToString())
+            )
+
+            let fakeKey =
+                ILModuleReaderCacheKey(fileName, DateTime.UtcNow, false, ReduceMemoryFlag.Yes, MetadataOnlyFlag.Yes)
+
             fakeKey, false
 
     let cacheResult1 =
         // can't used a cached entry when reading PDBs, since it makes the returned object IDisposable
         if keyOk && opts.pdbDirPath.IsNone then
-            ilModuleReaderCache1Lock.AcquireLock (fun ltok -> ilModuleReaderCache1.TryGet(ltok, key))
+            ilModuleReaderCache1Lock.AcquireLock(fun ltok -> ilModuleReaderCache1.TryGet(ltok, key))
         else
             None
 
@@ -3997,92 +5208,105 @@ let OpenILModuleReader fileName opts =
     | Some ilModuleReader -> ilModuleReader
     | None ->
 
-    let cacheResult2 =
-        // can't used a cached entry when reading PDBs, since it makes the returned object IDisposable
-        if keyOk && opts.pdbDirPath.IsNone then
-            ilModuleReaderCache2.TryGetValue key
-        else
-            false, Unchecked.defaultof<_>
-
-    let mutable res = Unchecked.defaultof<_>
-    match cacheResult2 with
-    | true, weak when weak.TryGetTarget(&res) -> res
-    | _ ->
-
-    let reduceMemoryUsage = (opts.reduceMemoryUsage = ReduceMemoryFlag.Yes)
-    let metadataOnly = (opts.metadataOnly = MetadataOnlyFlag.Yes)
-
-    if reduceMemoryUsage && opts.pdbDirPath.IsNone then
-
-        // This case is used in FCS applications, devenv.exe and fsi.exe
-        //
-        let ilModuleReader =
-            // Check if we are doing metadataOnly reading (the most common case in both the compiler and IDE)
-            if not runningOnMono && metadataOnly then
-
-                // See if tryGetMetadata gives us a BinaryFile for the metadata section alone.
-                let mdfileOpt =
-                    match opts.tryGetMetadataSnapshot (fullPath, writeStamp) with
-                    | Some (obj, start, len) -> Some (RawMemoryFile(fullPath, obj, start, len) :> BinaryFile)
-                    | None -> None
-
-                // For metadata-only, always use a temporary, short-lived PE file reader, preferably over a memory mapped file.
-                // Then use the metadata blob as the long-lived memory resource.
-                let disposer, pefileEager = getBinaryFile fullPath false
-                use _disposer = disposer
-                let metadataPhysLoc, metadataSize, peinfo, pectxtEager, pevEager, _pdb = openPEFileReader (fullPath, pefileEager, None, false)
-                let mdfile =
-                    match mdfileOpt with
-                    | Some mdfile -> mdfile
-                    | None ->
-                        // If tryGetMetadata doesn't give anything, then just read the metadata chunk out of the binary
-                        createByteFileChunk opts fullPath (Some (metadataPhysLoc, metadataSize))
-
-                let ilModule, ilAssemblyRefs = openPEMetadataOnly (fullPath, peinfo, pectxtEager, pevEager, mdfile, reduceMemoryUsage)
-                new ILModuleReaderImpl(ilModule, ilAssemblyRefs, ignore)
+        let cacheResult2 =
+            // can't used a cached entry when reading PDBs, since it makes the returned object IDisposable
+            if keyOk && opts.pdbDirPath.IsNone then
+                ilModuleReaderCache2.TryGetValue key
             else
-                // If we are not doing metadata-only, then just go ahead and read all the bytes and hold them either strongly or weakly
-                // depending on the heuristic
-                let pefile = createByteFileChunk opts fullPath None
-                let ilModule, ilAssemblyRefs, _pdb = openPE (fullPath, pefile, None, reduceMemoryUsage, false)
-                new ILModuleReaderImpl(ilModule, ilAssemblyRefs, ignore)
+                false, Unchecked.defaultof<_>
+
+        let mutable res = Unchecked.defaultof<_>
 
-        let ilModuleReader = ilModuleReader :> ILModuleReader
-        if keyOk then
-            ilModuleReaderCache1Lock.AcquireLock (fun ltok -> ilModuleReaderCache1.Put(ltok, key, ilModuleReader))
-            ilModuleReaderCache2[key] <- System.WeakReference<_>(ilModuleReader)
-        ilModuleReader
+        match cacheResult2 with
+        | true, weak when weak.TryGetTarget(&res) -> res
+        | _ ->
 
+            let reduceMemoryUsage = (opts.reduceMemoryUsage = ReduceMemoryFlag.Yes)
+            let metadataOnly = (opts.metadataOnly = MetadataOnlyFlag.Yes)
+
+            if reduceMemoryUsage && opts.pdbDirPath.IsNone then
+
+                // This case is used in FCS applications, devenv.exe and fsi.exe
+                //
+                let ilModuleReader =
+                    // Check if we are doing metadataOnly reading (the most common case in both the compiler and IDE)
+                    if not runningOnMono && metadataOnly then
+
+                        // See if tryGetMetadata gives us a BinaryFile for the metadata section alone.
+                        let mdfileOpt =
+                            match opts.tryGetMetadataSnapshot (fullPath, writeStamp) with
+                            | Some (obj, start, len) -> Some(RawMemoryFile(fullPath, obj, start, len) :> BinaryFile)
+                            | None -> None
+
+                        // For metadata-only, always use a temporary, short-lived PE file reader, preferably over a memory mapped file.
+                        // Then use the metadata blob as the long-lived memory resource.
+                        let disposer, pefileEager = getBinaryFile fullPath false
+                        use _disposer = disposer
+
+                        let metadataPhysLoc, metadataSize, peinfo, pectxtEager, pevEager, _pdb =
+                            openPEFileReader (fullPath, pefileEager, None, false)
+
+                        let mdfile =
+                            match mdfileOpt with
+                            | Some mdfile -> mdfile
+                            | None ->
+                                // If tryGetMetadata doesn't give anything, then just read the metadata chunk out of the binary
+                                createByteFileChunk opts fullPath (Some(metadataPhysLoc, metadataSize))
+
+                        let ilModule, ilAssemblyRefs =
+                            openPEMetadataOnly (fullPath, peinfo, pectxtEager, pevEager, mdfile, reduceMemoryUsage)
+
+                        new ILModuleReaderImpl(ilModule, ilAssemblyRefs, ignore)
+                    else
+                        // If we are not doing metadata-only, then just go ahead and read all the bytes and hold them either strongly or weakly
+                        // depending on the heuristic
+                        let pefile = createByteFileChunk opts fullPath None
+
+                        let ilModule, ilAssemblyRefs, _pdb =
+                            openPE (fullPath, pefile, None, reduceMemoryUsage, false)
+
+                        new ILModuleReaderImpl(ilModule, ilAssemblyRefs, ignore)
+
+                let ilModuleReader = ilModuleReader :> ILModuleReader
+
+                if keyOk then
+                    ilModuleReaderCache1Lock.AcquireLock(fun ltok -> ilModuleReaderCache1.Put(ltok, key, ilModuleReader))
+                    ilModuleReaderCache2[key] <- System.WeakReference<_>(ilModuleReader)
+
+                ilModuleReader
 
-    else
-        // This case is primarily used in fsc.exe.
-        //
-        // In fsc.exe, we're not trying to reduce memory usage, nor do we really care if we leak memory.
-        //
-        // Note we ignore the "metadata only" flag as it's generally OK to read in the
-        // whole binary for the command-line compiler: address space is rarely an issue.
-        //
-        // We do however care about avoiding locks on files that prevent their deletion during a
-        // multi-proc build. So use memory mapping, but only for stable files. Other files
-        // still use an in-memory ByteFile
-        let pefile =
-            if not runningOnMono && (alwaysMemoryMapFSC || stableFileHeuristicApplies fullPath) then
-                let _, pefile = getBinaryFile fullPath false
-                pefile
             else
-                createByteFileChunk opts fullPath None
+                // This case is primarily used in fsc.exe.
+                //
+                // In fsc.exe, we're not trying to reduce memory usage, nor do we really care if we leak memory.
+                //
+                // Note we ignore the "metadata only" flag as it's generally OK to read in the
+                // whole binary for the command-line compiler: address space is rarely an issue.
+                //
+                // We do however care about avoiding locks on files that prevent their deletion during a
+                // multi-proc build. So use memory mapping, but only for stable files. Other files
+                // still use an in-memory ByteFile
+                let pefile =
+                    if not runningOnMono && (alwaysMemoryMapFSC || stableFileHeuristicApplies fullPath) then
+                        let _, pefile = getBinaryFile fullPath false
+                        pefile
+                    else
+                        createByteFileChunk opts fullPath None
 
-        let ilModule, ilAssemblyRefs, pdb = openPE (fullPath, pefile, opts.pdbDirPath, reduceMemoryUsage, false)
-        let ilModuleReader = new ILModuleReaderImpl(ilModule, ilAssemblyRefs, (fun () -> ClosePdbReader pdb))
+                let ilModule, ilAssemblyRefs, pdb =
+                    openPE (fullPath, pefile, opts.pdbDirPath, reduceMemoryUsage, false)
 
-        let ilModuleReader = ilModuleReader :> ILModuleReader
+                let ilModuleReader =
+                    new ILModuleReaderImpl(ilModule, ilAssemblyRefs, (fun () -> ClosePdbReader pdb))
 
-        // Readers with PDB reader disposal logic don't go in the cache. Note the PDB reader is only used in static linking.
-        if keyOk && opts.pdbDirPath.IsNone then
-            ilModuleReaderCache1Lock.AcquireLock (fun ltok -> ilModuleReaderCache1.Put(ltok, key, ilModuleReader))
-            ilModuleReaderCache2[key] <- WeakReference<_>(ilModuleReader)
+                let ilModuleReader = ilModuleReader :> ILModuleReader
+
+                // Readers with PDB reader disposal logic don't go in the cache. Note the PDB reader is only used in static linking.
+                if keyOk && opts.pdbDirPath.IsNone then
+                    ilModuleReaderCache1Lock.AcquireLock(fun ltok -> ilModuleReaderCache1.Put(ltok, key, ilModuleReader))
+                    ilModuleReaderCache2[key] <- WeakReference<_>(ilModuleReader)
 
-        ilModuleReader
+                ilModuleReader
 
 []
 module Shim =
diff --git a/src/Compiler/AbstractIL/ilreflect.fs b/src/Compiler/AbstractIL/ilreflect.fs
index 35339c31abf..574075ea6b8 100644
--- a/src/Compiler/AbstractIL/ilreflect.fs
+++ b/src/Compiler/AbstractIL/ilreflect.fs
@@ -21,8 +21,7 @@ open FSharp.Core.Printf
 let codeLabelOrder = ComparisonIdentity.Structural
 
 // Convert the output of convCustomAttr
-let wrapCustomAttr setCustomAttr (cinfo, bytes) =
-    setCustomAttr(cinfo, bytes)
+let wrapCustomAttr setCustomAttr (cinfo, bytes) = setCustomAttr (cinfo, bytes)
 
 //----------------------------------------------------------------------------
 // logging to enable debugging
@@ -31,241 +30,440 @@ let wrapCustomAttr setCustomAttr (cinfo, bytes) =
 let logRefEmitCalls = false
 
 type AssemblyBuilder with
-    member asmB.DefineDynamicModuleAndLog (a, b, c) =
+
+    member asmB.DefineDynamicModuleAndLog(a, b, c) =
 #if FX_RESHAPED_REFEMIT
         ignore b
         ignore c
         let modB = asmB.DefineDynamicModule a
 #else
         let modB = asmB.DefineDynamicModule(a, b, c)
-        if logRefEmitCalls then printfn "let moduleBuilder%d = assemblyBuilder%d.DefineDynamicModule(%A, %A, %A)" (abs <| hash modB) (abs <| hash asmB) a b c
+
+        if logRefEmitCalls then
+            printfn "let moduleBuilder%d = assemblyBuilder%d.DefineDynamicModule(%A, %A, %A)" (abs <| hash modB) (abs <| hash asmB) a b c
 #endif
         modB
 
-    member asmB.SetCustomAttributeAndLog (cinfo, bytes) =
-        if logRefEmitCalls then printfn "assemblyBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash asmB) cinfo bytes
+    member asmB.SetCustomAttributeAndLog(cinfo, bytes) =
+        if logRefEmitCalls then
+            printfn "assemblyBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash asmB) cinfo bytes
+
         wrapCustomAttr asmB.SetCustomAttribute (cinfo, bytes)
 
 #if !FX_RESHAPED_REFEMIT
-    member asmB.AddResourceFileAndLog (nm1, nm2, attrs) =
-        if logRefEmitCalls then printfn "assemblyBuilder%d.AddResourceFile(%A, %A, enum %d)" (abs <| hash asmB) nm1 nm2 (LanguagePrimitives.EnumToValue attrs)
+    member asmB.AddResourceFileAndLog(nm1, nm2, attrs) =
+        if logRefEmitCalls then
+            printfn "assemblyBuilder%d.AddResourceFile(%A, %A, enum %d)" (abs <| hash asmB) nm1 nm2 (LanguagePrimitives.EnumToValue attrs)
+
         asmB.AddResourceFile(nm1, nm2, attrs)
 #endif
     member asmB.SetCustomAttributeAndLog cab =
-        if logRefEmitCalls then printfn "assemblyBuilder%d.SetCustomAttribute(%A)" (abs <| hash asmB) cab
-        asmB.SetCustomAttribute cab
+        if logRefEmitCalls then
+            printfn "assemblyBuilder%d.SetCustomAttribute(%A)" (abs <| hash asmB) cab
 
+        asmB.SetCustomAttribute cab
 
 type ModuleBuilder with
-    member modB.GetArrayMethodAndLog (arrayTy, nm, flags, retTy, argTys) =
-        if logRefEmitCalls then printfn "moduleBuilder%d.GetArrayMethod(%A, %A, %A, %A, %A)" (abs <| hash modB) arrayTy nm flags retTy argTys
+
+    member modB.GetArrayMethodAndLog(arrayTy, nm, flags, retTy, argTys) =
+        if logRefEmitCalls then
+            printfn "moduleBuilder%d.GetArrayMethod(%A, %A, %A, %A, %A)" (abs <| hash modB) arrayTy nm flags retTy argTys
+
         modB.GetArrayMethod(arrayTy, nm, flags, retTy, argTys)
 
 #if !FX_RESHAPED_REFEMIT
-    member modB.DefineDocumentAndLog (file, lang, vendor, doctype) =
+    member modB.DefineDocumentAndLog(file, lang, vendor, doctype) =
         let symDoc = modB.DefineDocument(file, lang, vendor, doctype)
-        if logRefEmitCalls then printfn "let docWriter%d = moduleBuilder%d.DefineDocument(@%A, System.Guid(\"%A\"), System.Guid(\"%A\"), System.Guid(\"%A\"))" (abs <| hash symDoc) (abs <| hash modB) file lang vendor doctype
+
+        if logRefEmitCalls then
+            printfn
+                "let docWriter%d = moduleBuilder%d.DefineDocument(@%A, System.Guid(\"%A\"), System.Guid(\"%A\"), System.Guid(\"%A\"))"
+                (abs <| hash symDoc)
+                (abs <| hash modB)
+                file
+                lang
+                vendor
+                doctype
+
         symDoc
 #endif
-    member modB.GetTypeAndLog (nameInModule, flag1, flag2) =
-        if logRefEmitCalls then printfn "moduleBuilder%d.GetType(%A, %A, %A) |> ignore" (abs <| hash modB) nameInModule flag1 flag2
+    member modB.GetTypeAndLog(nameInModule, flag1, flag2) =
+        if logRefEmitCalls then
+            printfn "moduleBuilder%d.GetType(%A, %A, %A) |> ignore" (abs <| hash modB) nameInModule flag1 flag2
+
         modB.GetType(nameInModule, flag1, flag2)
 
-    member modB.DefineTypeAndLog (name, attrs) =
+    member modB.DefineTypeAndLog(name, attrs) =
         let typB = modB.DefineType(name, attrs)
-        if logRefEmitCalls then printfn "let typeBuilder%d = moduleBuilder%d.DefineType(%A, enum %d)" (abs <| hash typB) (abs <| hash modB) name (LanguagePrimitives.EnumToValue attrs)
+
+        if logRefEmitCalls then
+            printfn
+                "let typeBuilder%d = moduleBuilder%d.DefineType(%A, enum %d)"
+                (abs <| hash typB)
+                (abs <| hash modB)
+                name
+                (LanguagePrimitives.EnumToValue attrs)
+
         typB
 
 #if !FX_RESHAPED_REFEMIT
-    member modB.DefineManifestResourceAndLog (name, stream, attrs) =
-        if logRefEmitCalls then printfn "moduleBuilder%d.DefineManifestResource(%A, %A, enum %d)" (abs <| hash modB) name stream (LanguagePrimitives.EnumToValue attrs)
+    member modB.DefineManifestResourceAndLog(name, stream, attrs) =
+        if logRefEmitCalls then
+            printfn
+                "moduleBuilder%d.DefineManifestResource(%A, %A, enum %d)"
+                (abs <| hash modB)
+                name
+                stream
+                (LanguagePrimitives.EnumToValue attrs)
+
         modB.DefineManifestResource(name, stream, attrs)
 #endif
-    member modB.SetCustomAttributeAndLog (cinfo, bytes) =
-        if logRefEmitCalls then printfn "moduleBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash modB) cinfo bytes
-        wrapCustomAttr modB.SetCustomAttribute (cinfo, bytes)
+    member modB.SetCustomAttributeAndLog(cinfo, bytes) =
+        if logRefEmitCalls then
+            printfn "moduleBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash modB) cinfo bytes
 
+        wrapCustomAttr modB.SetCustomAttribute (cinfo, bytes)
 
 type ConstructorBuilder with
+
     member consB.SetImplementationFlagsAndLog attrs =
-        if logRefEmitCalls then printfn "constructorBuilder%d.SetImplementationFlags(enum %d)" (abs <| hash consB) (LanguagePrimitives.EnumToValue attrs)
+        if logRefEmitCalls then
+            printfn "constructorBuilder%d.SetImplementationFlags(enum %d)" (abs <| hash consB) (LanguagePrimitives.EnumToValue attrs)
+
         consB.SetImplementationFlags attrs
 
-    member consB.DefineParameterAndLog (n, attr, nm) =
-        if logRefEmitCalls then printfn "constructorBuilder%d.DefineParameter(%d, enum %d, %A)" (abs <| hash consB) n (LanguagePrimitives.EnumToValue attr) nm
+    member consB.DefineParameterAndLog(n, attr, nm) =
+        if logRefEmitCalls then
+            printfn "constructorBuilder%d.DefineParameter(%d, enum %d, %A)" (abs <| hash consB) n (LanguagePrimitives.EnumToValue attr) nm
+
         consB.DefineParameter(n, attr, nm)
 
-    member consB.GetILGeneratorAndLog () =
+    member consB.GetILGeneratorAndLog() =
         let ilG = consB.GetILGenerator()
-        if logRefEmitCalls then printfn "let ilg%d = constructorBuilder%d.GetILGenerator()" (abs <| hash ilG) (abs <| hash consB)
+
+        if logRefEmitCalls then
+            printfn "let ilg%d = constructorBuilder%d.GetILGenerator()" (abs <| hash ilG) (abs <| hash consB)
+
         ilG
 
 type MethodBuilder with
+
     member methB.SetImplementationFlagsAndLog attrs =
-        if logRefEmitCalls then printfn "methodBuilder%d.SetImplementationFlags(enum %d)" (abs <| hash methB) (LanguagePrimitives.EnumToValue attrs)
+        if logRefEmitCalls then
+            printfn "methodBuilder%d.SetImplementationFlags(enum %d)" (abs <| hash methB) (LanguagePrimitives.EnumToValue attrs)
+
         methB.SetImplementationFlags attrs
 
-    member methB.SetSignatureAndLog (returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers,parameterTypeOptionalCustomModifiers) =
-        if logRefEmitCalls then printfn "methodBuilder%d.SetSignature(...)" (abs <| hash methB)
-        methB.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers,parameterTypeOptionalCustomModifiers)
+    member methB.SetSignatureAndLog
+        (
+            returnType,
+            returnTypeRequiredCustomModifiers,
+            returnTypeOptionalCustomModifiers,
+            parameterTypes,
+            parameterTypeRequiredCustomModifiers,
+            parameterTypeOptionalCustomModifiers
+        ) =
+        if logRefEmitCalls then
+            printfn "methodBuilder%d.SetSignature(...)" (abs <| hash methB)
+
+        methB.SetSignature(
+            returnType,
+            returnTypeRequiredCustomModifiers,
+            returnTypeOptionalCustomModifiers,
+            parameterTypes,
+            parameterTypeRequiredCustomModifiers,
+            parameterTypeOptionalCustomModifiers
+        )
+
+    member methB.DefineParameterAndLog(n, attr, nm) =
+        if logRefEmitCalls then
+            printfn "methodBuilder%d.DefineParameter(%d, enum %d, %A)" (abs <| hash methB) n (LanguagePrimitives.EnumToValue attr) nm
 
-    member methB.DefineParameterAndLog (n, attr, nm) =
-        if logRefEmitCalls then printfn "methodBuilder%d.DefineParameter(%d, enum %d, %A)" (abs <| hash methB) n (LanguagePrimitives.EnumToValue attr) nm
         methB.DefineParameter(n, attr, nm)
 
     member methB.DefineGenericParametersAndLog gps =
-        if logRefEmitCalls then printfn "let gps%d = methodBuilder%d.DefineGenericParameters(%A)" (abs <| hash methB) (abs <| hash methB) gps
+        if logRefEmitCalls then
+            printfn "let gps%d = methodBuilder%d.DefineGenericParameters(%A)" (abs <| hash methB) (abs <| hash methB) gps
+
         methB.DefineGenericParameters gps
 
-    member methB.GetILGeneratorAndLog () =
+    member methB.GetILGeneratorAndLog() =
         let ilG = methB.GetILGenerator()
-        if logRefEmitCalls then printfn "let ilg%d = methodBuilder%d.GetILGenerator()" (abs <| hash ilG) (abs <| hash methB)
+
+        if logRefEmitCalls then
+            printfn "let ilg%d = methodBuilder%d.GetILGenerator()" (abs <| hash ilG) (abs <| hash methB)
+
         ilG
 
-    member methB.SetCustomAttributeAndLog (cinfo, bytes) =
-        if logRefEmitCalls then printfn "methodBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash methB) cinfo bytes
+    member methB.SetCustomAttributeAndLog(cinfo, bytes) =
+        if logRefEmitCalls then
+            printfn "methodBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash methB) cinfo bytes
+
         wrapCustomAttr methB.SetCustomAttribute (cinfo, bytes)
 
 type TypeBuilder with
-    member typB.CreateTypeAndLog () =
-        if logRefEmitCalls then printfn "typeBuilder%d.CreateType()" (abs <| hash typB)
+
+    member typB.CreateTypeAndLog() =
+        if logRefEmitCalls then
+            printfn "typeBuilder%d.CreateType()" (abs <| hash typB)
 #if FX_RESHAPED_REFEMIT
         typB.CreateTypeInfo().AsType()
 #else
         typB.CreateType()
 #endif
-    member typB.DefineNestedTypeAndLog (name, attrs) =
+    member typB.DefineNestedTypeAndLog(name, attrs) =
         let res = typB.DefineNestedType(name, attrs)
-        if logRefEmitCalls then printfn "let typeBuilder%d = typeBuilder%d.DefineNestedType(\"%s\", enum %d)" (abs <| hash res) (abs <| hash typB) name (LanguagePrimitives.EnumToValue attrs)
+
+        if logRefEmitCalls then
+            printfn
+                "let typeBuilder%d = typeBuilder%d.DefineNestedType(\"%s\", enum %d)"
+                (abs <| hash res)
+                (abs <| hash typB)
+                name
+                (LanguagePrimitives.EnumToValue attrs)
+
         res
 
-    member typB.DefineMethodAndLog (name, attrs, cconv) =
+    member typB.DefineMethodAndLog(name, attrs, cconv) =
         let methB = typB.DefineMethod(name, attrs, cconv)
-        if logRefEmitCalls then printfn "let methodBuilder%d = typeBuilder%d.DefineMethod(\"%s\", enum %d, enum %d)" (abs <| hash methB) (abs <| hash typB) name (LanguagePrimitives.EnumToValue attrs) (LanguagePrimitives.EnumToValue cconv)
+
+        if logRefEmitCalls then
+            printfn
+                "let methodBuilder%d = typeBuilder%d.DefineMethod(\"%s\", enum %d, enum %d)"
+                (abs <| hash methB)
+                (abs <| hash typB)
+                name
+                (LanguagePrimitives.EnumToValue attrs)
+                (LanguagePrimitives.EnumToValue cconv)
+
         methB
 
     member typB.DefineGenericParametersAndLog gps =
-        if logRefEmitCalls then printfn "typeBuilder%d.DefineGenericParameters(%A)" (abs <| hash typB) gps
+        if logRefEmitCalls then
+            printfn "typeBuilder%d.DefineGenericParameters(%A)" (abs <| hash typB) gps
+
         typB.DefineGenericParameters gps
 
-    member typB.DefineConstructorAndLog (attrs, cconv, parms) =
+    member typB.DefineConstructorAndLog(attrs, cconv, parms) =
         let consB = typB.DefineConstructor(attrs, cconv, parms)
-        if logRefEmitCalls then printfn "let constructorBuilder%d = typeBuilder%d.DefineConstructor(enum %d, CallingConventions.%A, %A)" (abs <| hash consB) (abs <| hash typB) (LanguagePrimitives.EnumToValue attrs) cconv parms
+
+        if logRefEmitCalls then
+            printfn
+                "let constructorBuilder%d = typeBuilder%d.DefineConstructor(enum %d, CallingConventions.%A, %A)"
+                (abs <| hash consB)
+                (abs <| hash typB)
+                (LanguagePrimitives.EnumToValue attrs)
+                cconv
+                parms
+
         consB
 
-    member typB.DefineFieldAndLog (nm, ty: Type, attrs) =
+    member typB.DefineFieldAndLog(nm, ty: Type, attrs) =
         let fieldB = typB.DefineField(nm, ty, attrs)
-        if logRefEmitCalls then printfn "let fieldBuilder%d = typeBuilder%d.DefineField(\"%s\", typeof<%s>, enum %d)" (abs <| hash fieldB) (abs <| hash typB) nm ty.FullName (LanguagePrimitives.EnumToValue attrs)
+
+        if logRefEmitCalls then
+            printfn
+                "let fieldBuilder%d = typeBuilder%d.DefineField(\"%s\", typeof<%s>, enum %d)"
+                (abs <| hash fieldB)
+                (abs <| hash typB)
+                nm
+                ty.FullName
+                (LanguagePrimitives.EnumToValue attrs)
+
         fieldB
 
-    member typB.DefinePropertyAndLog (nm, attrs, ty: Type, args) =
-        if logRefEmitCalls then printfn "typeBuilder%d.DefineProperty(\"%A\", enum %d, typeof<%s>, %A)" (abs <| hash typB) nm (LanguagePrimitives.EnumToValue attrs) ty.FullName args
+    member typB.DefinePropertyAndLog(nm, attrs, ty: Type, args) =
+        if logRefEmitCalls then
+            printfn
+                "typeBuilder%d.DefineProperty(\"%A\", enum %d, typeof<%s>, %A)"
+                (abs <| hash typB)
+                nm
+                (LanguagePrimitives.EnumToValue attrs)
+                ty.FullName
+                args
+
         typB.DefineProperty(nm, attrs, ty, args)
 
-    member typB.DefineEventAndLog (nm, attrs, ty: Type) =
-        if logRefEmitCalls then printfn "typeBuilder%d.DefineEvent(\"%A\", enum %d, typeof<%A>)" (abs <| hash typB) nm (LanguagePrimitives.EnumToValue attrs) ty.FullName
+    member typB.DefineEventAndLog(nm, attrs, ty: Type) =
+        if logRefEmitCalls then
+            printfn
+                "typeBuilder%d.DefineEvent(\"%A\", enum %d, typeof<%A>)"
+                (abs <| hash typB)
+                nm
+                (LanguagePrimitives.EnumToValue attrs)
+                ty.FullName
+
         typB.DefineEvent(nm, attrs, ty)
 
-    member typB.SetParentAndLog (ty: Type) =
-        if logRefEmitCalls then printfn "typeBuilder%d.SetParent(typeof<%s>)" (abs <| hash typB) ty.FullName
+    member typB.SetParentAndLog(ty: Type) =
+        if logRefEmitCalls then
+            printfn "typeBuilder%d.SetParent(typeof<%s>)" (abs <| hash typB) ty.FullName
+
         typB.SetParent ty
 
     member typB.AddInterfaceImplementationAndLog ty =
-        if logRefEmitCalls then printfn "typeBuilder%d.AddInterfaceImplementation(%A)" (abs <| hash typB) ty
+        if logRefEmitCalls then
+            printfn "typeBuilder%d.AddInterfaceImplementation(%A)" (abs <| hash typB) ty
+
         typB.AddInterfaceImplementation ty
 
-    member typB.InvokeMemberAndLog (nm, _flags, args) =
+    member typB.InvokeMemberAndLog(nm, _flags, args) =
 #if FX_RESHAPED_REFEMIT
-        let t = typB.CreateTypeAndLog ()
+        let t = typB.CreateTypeAndLog()
+
         let m =
-            if t <> null then t.GetMethod(nm, (args |> Seq.map(fun x -> x.GetType()) |> Seq.toArray))
-            else null
-        if m <> null then m.Invoke(null, args)
-        else raise (MissingMethodException nm)
+            if t <> null then
+                t.GetMethod(nm, (args |> Seq.map (fun x -> x.GetType()) |> Seq.toArray))
+            else
+                null
+
+        if m <> null then
+            m.Invoke(null, args)
+        else
+            raise (MissingMethodException nm)
 #else
-        if logRefEmitCalls then printfn "typeBuilder%d.InvokeMember(\"%s\", enum %d, null, null, %A, Globalization.CultureInfo.InvariantCulture)" (abs <| hash typB) nm (LanguagePrimitives.EnumToValue _flags) args
+        if logRefEmitCalls then
+            printfn
+                "typeBuilder%d.InvokeMember(\"%s\", enum %d, null, null, %A, Globalization.CultureInfo.InvariantCulture)"
+                (abs <| hash typB)
+                nm
+                (LanguagePrimitives.EnumToValue _flags)
+                args
+
         typB.InvokeMember(nm, _flags, null, null, args, Globalization.CultureInfo.InvariantCulture)
 #endif
 
-    member typB.SetCustomAttributeAndLog (cinfo, bytes) =
-        if logRefEmitCalls then printfn "typeBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash typB) cinfo bytes
-        wrapCustomAttr typB.SetCustomAttribute (cinfo, bytes)
+    member typB.SetCustomAttributeAndLog(cinfo, bytes) =
+        if logRefEmitCalls then
+            printfn "typeBuilder%d.SetCustomAttribute(%A, %A)" (abs <| hash typB) cinfo bytes
 
+        wrapCustomAttr typB.SetCustomAttribute (cinfo, bytes)
 
 type OpCode with
-    member opcode.RefEmitName = (string (Char.ToUpper(opcode.Name[0])) + opcode.Name[1..]).Replace(".", "_").Replace("_i4", "_I4")
+
+    member opcode.RefEmitName =
+        (string (Char.ToUpper(opcode.Name[0])) + opcode.Name[1..])
+            .Replace(".", "_")
+            .Replace("_i4", "_I4")
 
 type ILGenerator with
-    member ilG.DeclareLocalAndLog (ty: Type, isPinned) =
-        if logRefEmitCalls then printfn "ilg%d.DeclareLocal(typeof<%s>, %b)" (abs <| hash ilG) ty.FullName isPinned
+
+    member ilG.DeclareLocalAndLog(ty: Type, isPinned) =
+        if logRefEmitCalls then
+            printfn "ilg%d.DeclareLocal(typeof<%s>, %b)" (abs <| hash ilG) ty.FullName isPinned
+
         ilG.DeclareLocal(ty, isPinned)
 
     member ilG.MarkLabelAndLog lab =
-        if logRefEmitCalls then printfn "ilg%d.MarkLabel(label%d_%d)" (abs <| hash ilG) (abs <| hash ilG) (abs <| hash lab)
+        if logRefEmitCalls then
+            printfn "ilg%d.MarkLabel(label%d_%d)" (abs <| hash ilG) (abs <| hash ilG) (abs <| hash lab)
+
         ilG.MarkLabel lab
 
 #if !FX_RESHAPED_REFEMIT
-    member ilG.MarkSequencePointAndLog (symDoc, l1, c1, l2, c2) =
-        if logRefEmitCalls then printfn "ilg%d.MarkSequencePoint(docWriter%d, %A, %A, %A, %A)" (abs <| hash ilG) (abs <| hash symDoc) l1 c1 l2 c2
+    member ilG.MarkSequencePointAndLog(symDoc, l1, c1, l2, c2) =
+        if logRefEmitCalls then
+            printfn "ilg%d.MarkSequencePoint(docWriter%d, %A, %A, %A, %A)" (abs <| hash ilG) (abs <| hash symDoc) l1 c1 l2 c2
+
         ilG.MarkSequencePoint(symDoc, l1, c1, l2, c2)
 #endif
-    member ilG.BeginExceptionBlockAndLog () =
-        if logRefEmitCalls then printfn "ilg%d.BeginExceptionBlock()" (abs <| hash ilG)
+    member ilG.BeginExceptionBlockAndLog() =
+        if logRefEmitCalls then
+            printfn "ilg%d.BeginExceptionBlock()" (abs <| hash ilG)
+
         ilG.BeginExceptionBlock()
 
-    member ilG.EndExceptionBlockAndLog () =
-        if logRefEmitCalls then printfn "ilg%d.EndExceptionBlock()" (abs <| hash ilG)
+    member ilG.EndExceptionBlockAndLog() =
+        if logRefEmitCalls then
+            printfn "ilg%d.EndExceptionBlock()" (abs <| hash ilG)
+
         ilG.EndExceptionBlock()
 
-    member ilG.BeginFinallyBlockAndLog () =
-        if logRefEmitCalls then printfn "ilg%d.BeginFinallyBlock()" (abs <| hash ilG)
+    member ilG.BeginFinallyBlockAndLog() =
+        if logRefEmitCalls then
+            printfn "ilg%d.BeginFinallyBlock()" (abs <| hash ilG)
+
         ilG.BeginFinallyBlock()
 
     member ilG.BeginCatchBlockAndLog ty =
-        if logRefEmitCalls then printfn "ilg%d.BeginCatchBlock(%A)" (abs <| hash ilG) ty
+        if logRefEmitCalls then
+            printfn "ilg%d.BeginCatchBlock(%A)" (abs <| hash ilG) ty
+
         ilG.BeginCatchBlock ty
 
-    member ilG.BeginExceptFilterBlockAndLog () =
-        if logRefEmitCalls then printfn "ilg%d.BeginExceptFilterBlock()" (abs <| hash ilG)
+    member ilG.BeginExceptFilterBlockAndLog() =
+        if logRefEmitCalls then
+            printfn "ilg%d.BeginExceptFilterBlock()" (abs <| hash ilG)
+
         ilG.BeginExceptFilterBlock()
 
-    member ilG.BeginFaultBlockAndLog () =
-        if logRefEmitCalls then printfn "ilg%d.BeginFaultBlock()" (abs <| hash ilG)
+    member ilG.BeginFaultBlockAndLog() =
+        if logRefEmitCalls then
+            printfn "ilg%d.BeginFaultBlock()" (abs <| hash ilG)
+
         ilG.BeginFaultBlock()
 
-    member ilG.DefineLabelAndLog () =
+    member ilG.DefineLabelAndLog() =
         let lab = ilG.DefineLabel()
-        if logRefEmitCalls then printfn "let label%d_%d = ilg%d.DefineLabel()" (abs <| hash ilG) (abs <| hash lab) (abs <| hash ilG)
+
+        if logRefEmitCalls then
+            printfn "let label%d_%d = ilg%d.DefineLabel()" (abs <| hash ilG) (abs <| hash lab) (abs <| hash ilG)
+
         lab
 
-    member x.EmitAndLog (op: OpCode) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s)" (abs <| hash x) op.RefEmitName
+    member x.EmitAndLog(op: OpCode) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s)" (abs <| hash x) op.RefEmitName
+
         x.Emit op
-    member x.EmitAndLog (op: OpCode, v: Label) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, label%d_%d)" (abs <| hash x) op.RefEmitName (abs <| hash x) (abs <| hash v)
-        x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: int16) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, int16 %d)" (abs <| hash x) op.RefEmitName v
+
+    member x.EmitAndLog(op: OpCode, v: Label) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, label%d_%d)" (abs <| hash x) op.RefEmitName (abs <| hash x) (abs <| hash v)
+
         x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: int32) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, %d)" (abs <| hash x) op.RefEmitName v
+
+    member x.EmitAndLog(op: OpCode, v: int16) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, int16 %d)" (abs <| hash x) op.RefEmitName v
+
         x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: MethodInfo) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, methodBuilder%d) // method %s" (abs <| hash x) op.RefEmitName (abs <| hash v) v.Name
+
+    member x.EmitAndLog(op: OpCode, v: int32) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, %d)" (abs <| hash x) op.RefEmitName v
+
         x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: string) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, \"@%s\")" (abs <| hash x) op.RefEmitName v
+
+    member x.EmitAndLog(op: OpCode, v: MethodInfo) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, methodBuilder%d) // method %s" (abs <| hash x) op.RefEmitName (abs <| hash v) v.Name
+
         x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: Type) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, typeof<%s>)" (abs <| hash x) op.RefEmitName v.FullName
+
+    member x.EmitAndLog(op: OpCode, v: string) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, \"@%s\")" (abs <| hash x) op.RefEmitName v
+
         x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: FieldInfo) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, fieldBuilder%d) // field %s" (abs <| hash x) op.RefEmitName (abs <| hash v) v.Name
+
+    member x.EmitAndLog(op: OpCode, v: Type) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, typeof<%s>)" (abs <| hash x) op.RefEmitName v.FullName
+
         x.Emit(op, v)
-    member x.EmitAndLog (op: OpCode, v: ConstructorInfo) =
-        if logRefEmitCalls then printfn "ilg%d.Emit(OpCodes.%s, constructor_%s)" (abs <| hash x) op.RefEmitName v.DeclaringType.Name
+
+    member x.EmitAndLog(op: OpCode, v: FieldInfo) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, fieldBuilder%d) // field %s" (abs <| hash x) op.RefEmitName (abs <| hash v) v.Name
+
         x.Emit(op, v)
 
+    member x.EmitAndLog(op: OpCode, v: ConstructorInfo) =
+        if logRefEmitCalls then
+            printfn "ilg%d.Emit(OpCodes.%s, constructor_%s)" (abs <| hash x) op.RefEmitName v.DeclaringType.Name
+
+        x.Emit(op, v)
 
 //----------------------------------------------------------------------------
 // misc
@@ -274,19 +472,36 @@ type ILGenerator with
 let inline flagsIf b x = if b then x else enum 0
 
 module Zmap =
-    let force x m str = match Zmap.tryFind x m with Some y -> y | None -> failwithf "Zmap.force: %s: x = %+A" str x
+    let force x m str =
+        match Zmap.tryFind x m with
+        | Some y -> y
+        | None -> failwithf "Zmap.force: %s: x = %+A" str x
 
 let equalTypes (s: Type) (t: Type) = s.Equals t
-let equalTypeLists ss tt = List.lengthsEqAndForall2 equalTypes ss tt
-let equalTypeArrays ss tt = Array.lengthsEqAndForall2 equalTypes ss tt
+
+let equalTypeLists ss tt =
+    List.lengthsEqAndForall2 equalTypes ss tt
+
+let equalTypeArrays ss tt =
+    Array.lengthsEqAndForall2 equalTypes ss tt
 
 let getGenericArgumentsOfType (typT: Type) =
-    if typT.IsGenericType then typT.GetGenericArguments() else [| |]
+    if typT.IsGenericType then
+        typT.GetGenericArguments()
+    else
+        [||]
+
 let getGenericArgumentsOfMethod (methI: MethodInfo) =
-    if methI.IsGenericMethod then methI.GetGenericArguments() else [| |]
+    if methI.IsGenericMethod then
+        methI.GetGenericArguments()
+    else
+        [||]
 
 let getTypeConstructor (ty: Type) =
-    if ty.IsGenericType then ty.GetGenericTypeDefinition() else ty
+    if ty.IsGenericType then
+        ty.GetGenericTypeDefinition()
+    else
+        ty
 
 //----------------------------------------------------------------------------
 // convAssemblyRef
@@ -295,12 +510,15 @@ let getTypeConstructor (ty: Type) =
 let convAssemblyRef (aref: ILAssemblyRef) =
     let asmName = AssemblyName()
     asmName.Name <- aref.Name
+
     (match aref.PublicKey with
      | None -> ()
      | Some (PublicKey bytes) -> asmName.SetPublicKey bytes
      | Some (PublicKeyToken bytes) -> asmName.SetPublicKeyToken bytes)
+
     let setVersion (version: ILVersionInfo) =
-       asmName.Version <- Version (int32 version.Major, int32 version.Minor, int32 version.Build, int32 version.Revision)
+        asmName.Version <- Version(int32 version.Major, int32 version.Minor, int32 version.Build, int32 version.Revision)
+
     Option.iter setVersion aref.Version
     //  asmName.ProcessorArchitecture <- System.Reflection.ProcessorArchitecture.MSIL
     //Option.iter (fun name -> asmName.CultureInfo <- System.Globalization.CultureInfo.CreateSpecificCulture name) aref.Locale
@@ -309,11 +527,13 @@ let convAssemblyRef (aref: ILAssemblyRef) =
 
 /// The global environment.
 type cenv =
-    { ilg: ILGlobals
-      emitTailcalls: bool
-      tryFindSysILTypeRef: string -> ILTypeRef option
-      generatePdb: bool
-      resolveAssemblyRef: ILAssemblyRef -> Choice option }
+    {
+        ilg: ILGlobals
+        emitTailcalls: bool
+        tryFindSysILTypeRef: string -> ILTypeRef option
+        generatePdb: bool
+        resolveAssemblyRef: ILAssemblyRef -> Choice option
+    }
 
     override x.ToString() = ""
 
@@ -325,14 +545,15 @@ let convResolveAssemblyRef (cenv: cenv) (asmref: ILAssemblyRef) qualifiedName =
             let asmName = AssemblyName.GetAssemblyName(path)
             asmName.CodeBase <- path
             FileSystem.AssemblyLoader.AssemblyLoad asmName
-        | Some (Choice2Of2 assembly) ->
-            assembly
+        | Some (Choice2Of2 assembly) -> assembly
         | None ->
             let asmName = convAssemblyRef asmref
             FileSystem.AssemblyLoader.AssemblyLoad asmName
+
     let typT = assembly.GetType qualifiedName
+
     match typT with
-    | null -> error(Error(FSComp.SR.itemNotFoundDuringDynamicCodeGen ("type", qualifiedName, asmref.QualifiedName), range0))
+    | null -> error (Error(FSComp.SR.itemNotFoundDuringDynamicCodeGen ("type", qualifiedName, asmref.QualifiedName), range0))
     | res -> res
 
 /// Convert an Abstract IL type reference to Reflection.Emit System.Type value.
@@ -343,33 +564,36 @@ let convResolveAssemblyRef (cenv: cenv) (asmref: ILAssemblyRef) qualifiedName =
 // [ns]            , name -> ns+name
 // [ns;typeA;typeB], name -> ns+typeA+typeB+name
 let convTypeRefAux (cenv: cenv) (tref: ILTypeRef) =
-    let qualifiedName = (String.concat "+" (tref.Enclosing @ [ tref.Name ])).Replace(",", @"\,")
+    let qualifiedName =
+        (String.concat "+" (tref.Enclosing @ [ tref.Name ])).Replace(",", @"\,")
+
     match tref.Scope with
-    | ILScopeRef.Assembly asmref ->
-        convResolveAssemblyRef cenv asmref qualifiedName
+    | ILScopeRef.Assembly asmref -> convResolveAssemblyRef cenv asmref qualifiedName
     | ILScopeRef.Module _
     | ILScopeRef.Local _ ->
         let typT = Type.GetType qualifiedName
+
         match typT with
-        | null -> error(Error(FSComp.SR.itemNotFoundDuringDynamicCodeGen ("type", qualifiedName, ""), range0))
+        | null -> error (Error(FSComp.SR.itemNotFoundDuringDynamicCodeGen ("type", qualifiedName, ""), range0))
         | res -> res
-    | ILScopeRef.PrimaryAssembly ->
-        convResolveAssemblyRef cenv cenv.ilg.primaryAssemblyRef qualifiedName
+    | ILScopeRef.PrimaryAssembly -> convResolveAssemblyRef cenv cenv.ilg.primaryAssemblyRef qualifiedName
 
 /// The (local) emitter env (state). Some of these fields are effectively global accumulators
 /// and could be placed as hash tables in the global environment.
 []
 type ILDynamicAssemblyEmitEnv =
-    { emTypMap: Zmap
-      emConsMap: Zmap
-      emMethMap: Zmap
-      emFieldMap: Zmap
-      emPropMap: Zmap
-      emLocals: LocalBuilder[]
-      emLabels: Zmap
-      emTyvars: Type[] list; // stack
-      emEntryPts: (TypeBuilder * string) list
-      delayedFieldInits: (unit -> unit) list}
+    {
+        emTypMap: Zmap
+        emConsMap: Zmap
+        emMethMap: Zmap
+        emFieldMap: Zmap
+        emPropMap: Zmap
+        emLocals: LocalBuilder[]
+        emLabels: Zmap
+        emTyvars: Type[] list // stack
+        emEntryPts: (TypeBuilder * string) list
+        delayedFieldInits: (unit -> unit) list
+    }
 
 let orderILTypeRef = ComparisonIdentity.Structural
 let orderILMethodRef = ComparisonIdentity.Structural
@@ -377,29 +601,36 @@ let orderILFieldRef = ComparisonIdentity.Structural
 let orderILPropertyRef = ComparisonIdentity.Structural
 
 let emEnv0 =
-    { emTypMap = Zmap.empty orderILTypeRef
-      emConsMap = Zmap.empty orderILMethodRef
-      emMethMap = Zmap.empty orderILMethodRef
-      emFieldMap = Zmap.empty orderILFieldRef
-      emPropMap = Zmap.empty orderILPropertyRef
-      emLocals = [| |]
-      emLabels = Zmap.empty codeLabelOrder
-      emTyvars = []
-      emEntryPts = []
-      delayedFieldInits = [] }
+    {
+        emTypMap = Zmap.empty orderILTypeRef
+        emConsMap = Zmap.empty orderILMethodRef
+        emMethMap = Zmap.empty orderILMethodRef
+        emFieldMap = Zmap.empty orderILFieldRef
+        emPropMap = Zmap.empty orderILPropertyRef
+        emLocals = [||]
+        emLabels = Zmap.empty codeLabelOrder
+        emTyvars = []
+        emEntryPts = []
+        delayedFieldInits = []
+    }
 
 let envBindTypeRef emEnv (tref: ILTypeRef) (typT, typB, typeDef) =
     match typT with
     | null -> failwithf "binding null type in envBindTypeRef: %s\n" tref.Name
-    | _ -> {emEnv with emTypMap = Zmap.add tref (typT, typB, typeDef, None) emEnv.emTypMap}
+    | _ ->
+        { emEnv with
+            emTypMap = Zmap.add tref (typT, typB, typeDef, None) emEnv.emTypMap
+        }
 
 let envUpdateCreatedTypeRef emEnv (tref: ILTypeRef) =
     // The tref's TypeBuilder has been created, so we have a Type proper.
     // Update the tables to include this created type (the typT held prior to this is (i think) actually (TypeBuilder :> Type).
     // The (TypeBuilder :> Type) does not implement all the methods that a Type proper does.
-    let typT, typB, typeDef, _createdTypOpt = Zmap.force tref emEnv.emTypMap "envGetTypeDef: failed"
+    let typT, typB, typeDef, _createdTypOpt =
+        Zmap.force tref emEnv.emTypMap "envGetTypeDef: failed"
+
     if typB.IsCreated() then
-        let ty = typB.CreateTypeAndLog ()
+        let ty = typB.CreateTypeAndLog()
 #if ENABLE_MONO_SUPPORT
         // Mono has a bug where executing code that includes an array type
         // match "match x with :? C[] -> ..." before the full loading of an object of type
@@ -407,12 +638,20 @@ let envUpdateCreatedTypeRef emEnv (tref: ILTypeRef) =
         // of objects. We use System.Runtime.Serialization.FormatterServices.GetUninitializedObject to do
         // the fake allocation - this creates an "empty" object, even if the object doesn't have
         // a constructor. It is not usable in partial trust code.
-        if runningOnMono && ty.IsClass && not ty.IsAbstract && not ty.IsGenericType && not ty.IsGenericTypeDefinition then
+        if runningOnMono
+           && ty.IsClass
+           && not ty.IsAbstract
+           && not ty.IsGenericType
+           && not ty.IsGenericTypeDefinition then
             try
-              System.Runtime.Serialization.FormatterServices.GetUninitializedObject ty |> ignore
-            with _ -> ()
+                System.Runtime.Serialization.FormatterServices.GetUninitializedObject ty
+                |> ignore
+            with
+            | _ -> ()
 #endif
-        {emEnv with emTypMap = Zmap.add tref (typT, typB, typeDef, Some ty) emEnv.emTypMap}
+        { emEnv with
+            emTypMap = Zmap.add tref (typT, typB, typeDef, Some ty) emEnv.emTypMap
+        }
     else
 #if DEBUG
         printf "envUpdateCreatedTypeRef: expected type to be created\n"
@@ -426,25 +665,33 @@ let convTypeRef cenv emEnv preferCreated (tref: ILTypeRef) =
     | None -> convTypeRefAux cenv tref
 
 let envBindConsRef emEnv (mref: ILMethodRef) consB =
-    {emEnv with emConsMap = Zmap.add mref consB emEnv.emConsMap}
+    { emEnv with
+        emConsMap = Zmap.add mref consB emEnv.emConsMap
+    }
 
 let envGetConsB emEnv (mref: ILMethodRef) =
     Zmap.force mref emEnv.emConsMap "envGetConsB: failed"
 
 let envBindMethodRef emEnv (mref: ILMethodRef) methB =
-    {emEnv with emMethMap = Zmap.add mref methB emEnv.emMethMap}
+    { emEnv with
+        emMethMap = Zmap.add mref methB emEnv.emMethMap
+    }
 
 let envGetMethB emEnv (mref: ILMethodRef) =
     Zmap.force mref emEnv.emMethMap "envGetMethB: failed"
 
 let envBindFieldRef emEnv fref fieldB =
-    {emEnv with emFieldMap = Zmap.add fref fieldB emEnv.emFieldMap}
+    { emEnv with
+        emFieldMap = Zmap.add fref fieldB emEnv.emFieldMap
+    }
 
 let envGetFieldB emEnv fref =
     Zmap.force fref emEnv.emFieldMap "- envGetMethB: failed"
 
 let envBindPropRef emEnv (pref: ILPropertyRef) propB =
-    {emEnv with emPropMap = Zmap.add pref propB emEnv.emPropMap}
+    { emEnv with
+        emPropMap = Zmap.add pref propB emEnv.emPropMap
+    }
 
 let envGetPropB emEnv pref =
     Zmap.force pref emEnv.emPropMap "- envGetPropB: failed"
@@ -457,36 +704,51 @@ let envGetTypeDef emEnv (tref: ILTypeRef) =
     Zmap.force tref emEnv.emTypMap "envGetTypeDef: failed"
     |> (fun (_typT, _typB, typeDef, _createdTypOpt) -> typeDef)
 
-let envSetLocals emEnv locs = assert (emEnv.emLocals.Length = 0); // check "locals" is not yet set (scopes once only)
-                              {emEnv with emLocals = locs}
-let envGetLocal emEnv i = emEnv.emLocals[i] // implicit bounds checking
+let envSetLocals emEnv locs =
+    assert (emEnv.emLocals.Length = 0) // check "locals" is not yet set (scopes once only)
+    { emEnv with emLocals = locs }
+
+let envGetLocal emEnv i = emEnv.emLocals[i]
 
 let envSetLabel emEnv name lab =
     assert (not (Zmap.mem name emEnv.emLabels))
-    {emEnv with emLabels = Zmap.add name lab emEnv.emLabels}
 
-let envGetLabel emEnv name =
-    Zmap.find name emEnv.emLabels
+    { emEnv with
+        emLabels = Zmap.add name lab emEnv.emLabels
+    }
+
+let envGetLabel emEnv name = Zmap.find name emEnv.emLabels
 
-let envPushTyvars emEnv tys = {emEnv with emTyvars = tys :: emEnv.emTyvars}
+let envPushTyvars emEnv tys =
+    { emEnv with
+        emTyvars = tys :: emEnv.emTyvars
+    }
 
-let envPopTyvars emEnv = {emEnv with emTyvars = List.tail emEnv.emTyvars}
+let envPopTyvars emEnv =
+    { emEnv with
+        emTyvars = List.tail emEnv.emTyvars
+    }
 
 let envGetTyvar emEnv u16 =
     match emEnv.emTyvars with
     | [] -> failwith "envGetTyvar: not scope of type vars"
     | tvs :: _ ->
         let i = int32 u16
-        if i<0 || i>= Array.length tvs then
+
+        if i < 0 || i >= Array.length tvs then
             failwith (sprintf "want tyvar #%d, but only had %d tyvars" i (Array.length tvs))
         else
             tvs[i]
 
 let isEmittedTypeRef emEnv tref = Zmap.mem tref emEnv.emTypMap
 
-let envAddEntryPt emEnv mref = {emEnv with emEntryPts = mref :: emEnv.emEntryPts}
+let envAddEntryPt emEnv mref =
+    { emEnv with
+        emEntryPts = mref :: emEnv.emEntryPts
+    }
 
-let envPopEntryPts emEnv = {emEnv with emEntryPts = []}, emEnv.emEntryPts
+let envPopEntryPts emEnv =
+    { emEnv with emEntryPts = [] }, emEnv.emEntryPts
 
 //----------------------------------------------------------------------------
 // convCallConv
@@ -510,7 +772,6 @@ let convCallConv (Callconv (hasThis, basic)) =
 
     ccA ||| ccB
 
-
 //----------------------------------------------------------------------------
 // convType
 //----------------------------------------------------------------------------
@@ -518,13 +779,16 @@ let convCallConv (Callconv (hasThis, basic)) =
 let rec convTypeSpec cenv emEnv preferCreated (tspec: ILTypeSpec) =
     let typT = convTypeRef cenv emEnv preferCreated tspec.TypeRef
     let tyargs = List.map (convTypeAux cenv emEnv preferCreated) tspec.GenericArgs
+
     let res =
         match isNil tyargs, typT.IsGenericType with
         | _, true -> typT.MakeGenericType(List.toArray tyargs)
         | true, false -> typT
         | _, false -> null
+
     match res with
-    | Null -> error(Error(FSComp.SR.itemNotFoundDuringDynamicCodeGen ("type", tspec.TypeRef.QualifiedName, tspec.Scope.QualifiedName), range0))
+    | Null ->
+        error (Error(FSComp.SR.itemNotFoundDuringDynamicCodeGen ("type", tspec.TypeRef.QualifiedName, tspec.Scope.QualifiedName), range0))
     | NonNull res -> res
 
 and convTypeAux cenv emEnv preferCreated ty =
@@ -538,9 +802,10 @@ and convTypeAux cenv emEnv preferCreated ty =
         // MakeArrayType(2) returns "eltType[, ]"
         // MakeArrayType(3) returns "eltType[, , ]"
         // All non-equal.
-        if nDims=1
-        then baseT.MakeArrayType()
-        else baseT.MakeArrayType shape.Rank
+        if nDims = 1 then
+            baseT.MakeArrayType()
+        else
+            baseT.MakeArrayType shape.Rank
     | ILType.Value tspec -> convTypeSpec cenv emEnv preferCreated tspec
     | ILType.Boxed tspec -> convTypeSpec cenv emEnv preferCreated tspec
     | ILType.Ptr eltType ->
@@ -584,22 +849,35 @@ let convTypeOrTypeDef cenv emEnv ty =
 
 let convTypes cenv emEnv (tys: ILTypes) = List.map (convType cenv emEnv) tys
 
-let convTypesToArray cenv emEnv (tys: ILTypes) = convTypes cenv emEnv tys |> List.toArray
+let convTypesToArray cenv emEnv (tys: ILTypes) =
+    convTypes cenv emEnv tys |> List.toArray
 
 /// Uses the .CreateType() for emitted type if available.
 let convCreatedType cenv emEnv ty = convTypeAux cenv emEnv true ty
 let convCreatedTypeRef cenv emEnv ty = convTypeRef cenv emEnv true ty
 
 let rec convParamModifiersOfType cenv emEnv (paramTy: ILType) =
-    [| match paramTy with
+    [|
+        match paramTy with
         | ILType.Modified (modreq, ty, modifiedTy) ->
             yield (modreq, convTypeRef cenv emEnv false ty)
             yield! convParamModifiersOfType cenv emEnv modifiedTy
-        | _ -> () |]
+        | _ -> ()
+    |]
 
 let splitModifiers mods =
-    let reqd = mods |> Array.choose (function true, ty -> Some ty | _ -> None)
-    let optional = mods |> Array.choose (function false, ty -> Some ty | _ -> None)
+    let reqd =
+        mods
+        |> Array.choose (function
+            | true, ty -> Some ty
+            | _ -> None)
+
+    let optional =
+        mods
+        |> Array.choose (function
+            | false, ty -> Some ty
+            | _ -> None)
+
     reqd, optional
 
 let convParamModifiers cenv emEnv (p: ILParameter) =
@@ -622,12 +900,13 @@ let TypeBuilderInstantiationT =
 #if ENABLE_MONO_SUPPORT
         if runningOnMono then
             let ty = Type.GetType("System.Reflection.MonoGenericClass")
+
             match ty with
             | null -> Type.GetType("System.Reflection.Emit.TypeBuilderInstantiation")
             | _ -> ty
         else
 #endif
-            Type.GetType("System.Reflection.Emit.TypeBuilderInstantiation")
+        Type.GetType("System.Reflection.Emit.TypeBuilderInstantiation")
 
     assert (not (isNull ty))
     ty
@@ -636,145 +915,228 @@ let typeIsNotQueryable (ty: Type) =
     (ty :? TypeBuilder) || ((ty.GetType()).Equals(TypeBuilderInstantiationT))
 
 let queryableTypeGetField _emEnv (parentT: Type) (fref: ILFieldRef) =
-    let res = parentT.GetField(fref.Name, BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance ||| BindingFlags.Static )
+    let res =
+        parentT.GetField(
+            fref.Name,
+            BindingFlags.Public
+            ||| BindingFlags.NonPublic
+            ||| BindingFlags.Instance
+            ||| BindingFlags.Static
+        )
+
     match res with
-    | Null -> error(Error(FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("field", fref.Name, fref.DeclaringTypeRef.FullName, fref.DeclaringTypeRef.Scope.QualifiedName), range0))
+    | Null ->
+        error (
+            Error(
+                FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen (
+                    "field",
+                    fref.Name,
+                    fref.DeclaringTypeRef.FullName,
+                    fref.DeclaringTypeRef.Scope.QualifiedName
+                ),
+                range0
+            )
+        )
     | NonNull res -> res
 
 let nonQueryableTypeGetField (parentTI: Type) (fieldInfo: FieldInfo) : FieldInfo =
     let res =
-        if parentTI.IsGenericType then TypeBuilder.GetField(parentTI, fieldInfo)
-        else fieldInfo
+        if parentTI.IsGenericType then
+            TypeBuilder.GetField(parentTI, fieldInfo)
+        else
+            fieldInfo
+
     match res with
-    | Null -> error(Error(FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("field", fieldInfo.Name, parentTI.AssemblyQualifiedName, parentTI.Assembly.FullName), range0))
+    | Null ->
+        error (
+            Error(
+                FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen (
+                    "field",
+                    fieldInfo.Name,
+                    parentTI.AssemblyQualifiedName,
+                    parentTI.Assembly.FullName
+                ),
+                range0
+            )
+        )
     | NonNull res -> res
 
 let convFieldSpec cenv emEnv fspec =
     let fref = fspec.FieldRef
     let tref = fref.DeclaringTypeRef
     let parentTI = convType cenv emEnv fspec.DeclaringType
+
     if isEmittedTypeRef emEnv tref then
         // NOTE: if "convType becomes convCreatedType", then handle queryable types here too. [bug 4063] (necessary? what repro?)
         let fieldB = envGetFieldB emEnv fref
         nonQueryableTypeGetField parentTI fieldB
     else
-        // Prior type.
-        if typeIsNotQueryable parentTI then
-            let parentT = getTypeConstructor parentTI
-            let fieldInfo = queryableTypeGetField emEnv parentT fref
-            nonQueryableTypeGetField parentTI fieldInfo
-        else
-            queryableTypeGetField emEnv parentTI fspec.FieldRef
+    // Prior type.
+    if typeIsNotQueryable parentTI then
+        let parentT = getTypeConstructor parentTI
+        let fieldInfo = queryableTypeGetField emEnv parentT fref
+        nonQueryableTypeGetField parentTI fieldInfo
+    else
+        queryableTypeGetField emEnv parentTI fspec.FieldRef
 
 //----------------------------------------------------------------------------
 // convMethodRef
 //----------------------------------------------------------------------------
 let queryableTypeGetMethodBySearch cenv emEnv parentT (mref: ILMethodRef) =
-    assert(not (typeIsNotQueryable parentT))
-    let cconv = (if mref.CallingConv.IsStatic then BindingFlags.Static else BindingFlags.Instance)
-    let methInfos = parentT.GetMethods(cconv ||| BindingFlags.Public ||| BindingFlags.NonPublic) |> Array.toList
-      (* First, filter on name, if unique, then binding "done" *)
+    assert (not (typeIsNotQueryable parentT))
+
+    let cconv =
+        (if mref.CallingConv.IsStatic then
+             BindingFlags.Static
+         else
+             BindingFlags.Instance)
+
+    let methInfos =
+        parentT.GetMethods(cconv ||| BindingFlags.Public ||| BindingFlags.NonPublic)
+        |> Array.toList
+    (* First, filter on name, if unique, then binding "done" *)
     let tyargTs = getGenericArgumentsOfType parentT
     let methInfos = methInfos |> List.filter (fun methInfo -> methInfo.Name = mref.Name)
+
     match methInfos with
-    | [methInfo] ->
-        methInfo
+    | [ methInfo ] -> methInfo
     | _ ->
-      (* Second, type match. Note type erased (non-generic) F# code would not type match but they have unique names *)
+        (* Second, type match. Note type erased (non-generic) F# code would not type match but they have unique names *)
 
         let satisfiesParameter (a: Type option) (p: Type) =
             match a with
             | None -> true
             | Some a ->
-            if
-                // obvious case
-                p.IsAssignableFrom a
-            then true
-            elif
-                // both are generic
-                p.IsGenericType && a.IsGenericType
-                // non obvious due to contravariance: Action where T: IFoo accepts Action (for FooImpl: IFoo)
-                && p.GetGenericTypeDefinition().IsAssignableFrom(a.GetGenericTypeDefinition())
-            then true
-            else false
+                if
+                    // obvious case
+                    p.IsAssignableFrom a then
+                    true
+                elif
+                    p.IsGenericType && a.IsGenericType
+                    // non obvious due to contravariance: Action where T: IFoo accepts Action (for FooImpl: IFoo)
+                    && p.GetGenericTypeDefinition().IsAssignableFrom(a.GetGenericTypeDefinition())
+                then
+                    true
+                else
+                    false
 
         let satisfiesAllParameters (args: Type option array) (ps: Type array) =
-            if Array.length args <> Array.length ps then false
-            else Array.forall2 satisfiesParameter args ps
+            if Array.length args <> Array.length ps then
+                false
+            else
+                Array.forall2 satisfiesParameter args ps
 
         let select (methInfo: MethodInfo) =
             // mref implied Types
             let mtyargTIs = getGenericArgumentsOfMethod methInfo
 
-            if mtyargTIs.Length <> mref.GenericArity then false (* method generic arity mismatch *) else
-
-            // methInfo implied Types
-            let methodParameters = methInfo.GetParameters()
-            let argTypes = mref.ArgTypes |> List.toArray
-            if argTypes.Length <> methodParameters.Length then false (* method argument length mismatch *) else
-
-            let haveArgTs = methodParameters |> Array.map (fun param -> param.ParameterType)
-            let mrefParameterTypes = argTypes |> Array.map (fun t -> if t.IsNominal then Some (convTypeRefAux cenv t.TypeRef) else None)
+            if mtyargTIs.Length <> mref.GenericArity then
+                false (* method generic arity mismatch *)
+            else
 
-            // we should reject methods which don't satisfy parameter types by also checking
-            // type parameters which can be contravariant for delegates for example
-            // see https://github.com/dotnet/fsharp/issues/2411
-            // without this check, subsequent call to convTypes would fail because it
-            // constructs generic type without checking constraints
-            if not (satisfiesAllParameters mrefParameterTypes haveArgTs) then false else
+                // methInfo implied Types
+                let methodParameters = methInfo.GetParameters()
+                let argTypes = mref.ArgTypes |> List.toArray
 
-            let argTs, resT =
-                let emEnv = envPushTyvars emEnv (Array.append tyargTs mtyargTIs)
-                let argTs = convTypes cenv emEnv mref.ArgTypes
-                let resT = convType cenv emEnv mref.ReturnType
-                argTs, resT
+                if argTypes.Length <> methodParameters.Length then
+                    false (* method argument length mismatch *)
+                else
 
-            let haveResT = methInfo.ReturnType
-          (* check for match *)
-            if argTs.Length <> methodParameters.Length then false (* method argument length mismatch *) else
-            let res = equalTypes resT haveResT && equalTypeLists argTs (haveArgTs |> Array.toList)
-            res
+                    let haveArgTs = methodParameters |> Array.map (fun param -> param.ParameterType)
+
+                    let mrefParameterTypes =
+                        argTypes
+                        |> Array.map (fun t ->
+                            if t.IsNominal then
+                                Some(convTypeRefAux cenv t.TypeRef)
+                            else
+                                None)
+
+                    // we should reject methods which don't satisfy parameter types by also checking
+                    // type parameters which can be contravariant for delegates for example
+                    // see https://github.com/dotnet/fsharp/issues/2411
+                    // without this check, subsequent call to convTypes would fail because it
+                    // constructs generic type without checking constraints
+                    if not (satisfiesAllParameters mrefParameterTypes haveArgTs) then
+                        false
+                    else
+
+                        let argTs, resT =
+                            let emEnv = envPushTyvars emEnv (Array.append tyargTs mtyargTIs)
+                            let argTs = convTypes cenv emEnv mref.ArgTypes
+                            let resT = convType cenv emEnv mref.ReturnType
+                            argTs, resT
+
+                        let haveResT = methInfo.ReturnType
+                        (* check for match *)
+                        if argTs.Length <> methodParameters.Length then
+                            false (* method argument length mismatch *)
+                        else
+                            let res =
+                                equalTypes resT haveResT && equalTypeLists argTs (haveArgTs |> Array.toList)
+
+                            res
 
         match List.tryFind select methInfos with
         | None ->
             let methNames = methInfos |> List.map (fun m -> m.Name) |> List.distinct
-            failwithf "convMethodRef: could not bind to method '%A' of type '%s'" (String.Join(", ", methNames)) parentT.AssemblyQualifiedName
+
+            failwithf
+                "convMethodRef: could not bind to method '%A' of type '%s'"
+                (String.Join(", ", methNames))
+                parentT.AssemblyQualifiedName
         | Some methInfo -> methInfo (* return MethodInfo for (generic) type's (generic) method *)
 
 let queryableTypeGetMethod cenv emEnv parentT (mref: ILMethodRef) : MethodInfo =
-    assert(not (typeIsNotQueryable parentT))
+    assert (not (typeIsNotQueryable parentT))
+
     if mref.GenericArity = 0 then
         let tyargTs = getGenericArgumentsOfType parentT
+
         let argTs, resT =
             let emEnv = envPushTyvars emEnv tyargTs
             let argTs = convTypesToArray cenv emEnv mref.ArgTypes
             let resT = convType cenv emEnv mref.ReturnType
             argTs, resT
+
         let stat = mref.CallingConv.IsStatic
-        let cconv = (if stat then BindingFlags.Static else BindingFlags.Instance)
+
+        let cconv =
+            (if stat then
+                 BindingFlags.Static
+             else
+                 BindingFlags.Instance)
+
         let methInfo =
             try
-              parentT.GetMethod(mref.Name, cconv ||| BindingFlags.Public ||| BindingFlags.NonPublic,
-                                null,
-                                argTs,
-                                (null: ParameterModifier[]))
+                parentT.GetMethod(
+                    mref.Name,
+                    cconv ||| BindingFlags.Public ||| BindingFlags.NonPublic,
+                    null,
+                    argTs,
+                    (null: ParameterModifier[])
+                )
             // This can fail if there is an ambiguity w.r.t. return type
-            with _ -> null
+            with
+            | _ -> null
+
         if (isNotNull methInfo && equalTypes resT methInfo.ReturnType) then
-             methInfo
+            methInfo
         else
-             queryableTypeGetMethodBySearch cenv emEnv parentT mref
+            queryableTypeGetMethodBySearch cenv emEnv parentT mref
     else
         queryableTypeGetMethodBySearch cenv emEnv parentT mref
 
 let nonQueryableTypeGetMethod (parentTI: Type) (methInfo: MethodInfo) : MethodInfo MaybeNull =
-    if (parentTI.IsGenericType &&
-        not (equalTypes parentTI (getTypeConstructor parentTI)))
-    then TypeBuilder.GetMethod(parentTI, methInfo )
-    else methInfo
+    if (parentTI.IsGenericType
+        && not (equalTypes parentTI (getTypeConstructor parentTI))) then
+        TypeBuilder.GetMethod(parentTI, methInfo)
+    else
+        methInfo
 
 let convMethodRef cenv emEnv (parentTI: Type) (mref: ILMethodRef) =
     let parent = mref.DeclaringTypeRef
+
     let res =
         if isEmittedTypeRef emEnv parent then
             // NOTE: if "convType becomes convCreatedType", then handle queryable types here too. [bug 4063]
@@ -782,15 +1144,22 @@ let convMethodRef cenv emEnv (parentTI: Type) (mref: ILMethodRef) =
             let methB = envGetMethB emEnv mref
             nonQueryableTypeGetMethod parentTI methB
         else
-            // Prior type.
-            if typeIsNotQueryable parentTI then
-                let parentT = getTypeConstructor parentTI
-                let methInfo = queryableTypeGetMethod cenv emEnv parentT mref
-                nonQueryableTypeGetMethod parentTI methInfo
-            else
-                queryableTypeGetMethod cenv emEnv parentTI mref
+        // Prior type.
+        if typeIsNotQueryable parentTI then
+            let parentT = getTypeConstructor parentTI
+            let methInfo = queryableTypeGetMethod cenv emEnv parentT mref
+            nonQueryableTypeGetMethod parentTI methInfo
+        else
+            queryableTypeGetMethod cenv emEnv parentTI mref
+
     match res with
-    | Null -> error(Error(FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("method", mref.Name, parentTI.FullName, parentTI.Assembly.FullName), range0))
+    | Null ->
+        error (
+            Error(
+                FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("method", mref.Name, parentTI.FullName, parentTI.Assembly.FullName),
+                range0
+            )
+        )
     | NonNull res -> res
 
 //----------------------------------------------------------------------------
@@ -799,7 +1168,10 @@ let convMethodRef cenv emEnv (parentTI: Type) (mref: ILMethodRef) =
 
 let convMethodSpec cenv emEnv (mspec: ILMethodSpec) =
     let typT = convType cenv emEnv mspec.DeclaringType (* (instanced) parent Type *)
-    let methInfo = convMethodRef cenv emEnv typT mspec.MethodRef (* (generic) method of (generic) parent *)
+
+    let methInfo =
+        convMethodRef cenv emEnv typT mspec.MethodRef (* (generic) method of (generic) parent *)
+
     let methInfo =
         if isNil mspec.GenericArgs then
             methInfo // non generic
@@ -807,41 +1179,62 @@ let convMethodSpec cenv emEnv (mspec: ILMethodSpec) =
             let minstTs = convTypesToArray cenv emEnv mspec.GenericArgs
             let methInfo = methInfo.MakeGenericMethod minstTs // instantiate method
             methInfo
+
     methInfo
 
 /// Get a constructor on a non-TypeBuilder type
 let queryableTypeGetConstructor cenv emEnv (parentT: Type) (mref: ILMethodRef) =
     let tyargTs = getGenericArgumentsOfType parentT
+
     let reqArgTs =
         let emEnv = envPushTyvars emEnv tyargTs
         convTypesToArray cenv emEnv mref.ArgTypes
-    let res = parentT.GetConstructor(BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance, null, reqArgTs, null)
+
+    let res =
+        parentT.GetConstructor(BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance, null, reqArgTs, null)
+
     match res with
-    | null -> error(Error(FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("constructor", mref.Name, parentT.FullName, parentT.Assembly.FullName), range0))
+    | null ->
+        error (
+            Error(
+                FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("constructor", mref.Name, parentT.FullName, parentT.Assembly.FullName),
+                range0
+            )
+        )
     | _ -> res
 
-
-let nonQueryableTypeGetConstructor (parentTI:Type) (consInfo : ConstructorInfo) : ConstructorInfo MaybeNull =
-    if parentTI.IsGenericType then TypeBuilder.GetConstructor(parentTI, consInfo) else consInfo
+let nonQueryableTypeGetConstructor (parentTI: Type) (consInfo: ConstructorInfo) : ConstructorInfo MaybeNull =
+    if parentTI.IsGenericType then
+        TypeBuilder.GetConstructor(parentTI, consInfo)
+    else
+        consInfo
 
 /// convConstructorSpec (like convMethodSpec)
 let convConstructorSpec cenv emEnv (mspec: ILMethodSpec) =
     let mref = mspec.MethodRef
     let parentTI = convType cenv emEnv mspec.DeclaringType
+
     let res =
         if isEmittedTypeRef emEnv mref.DeclaringTypeRef then
             let consB = envGetConsB emEnv mref
             nonQueryableTypeGetConstructor parentTI consB
         else
-            // Prior type.
-            if typeIsNotQueryable parentTI then
-                let parentT = getTypeConstructor parentTI
-                let ctorG = queryableTypeGetConstructor cenv emEnv parentT mref
-                nonQueryableTypeGetConstructor parentTI ctorG
-            else
-                queryableTypeGetConstructor cenv emEnv parentTI mref
+        // Prior type.
+        if typeIsNotQueryable parentTI then
+            let parentT = getTypeConstructor parentTI
+            let ctorG = queryableTypeGetConstructor cenv emEnv parentT mref
+            nonQueryableTypeGetConstructor parentTI ctorG
+        else
+            queryableTypeGetConstructor cenv emEnv parentTI mref
+
     match res with
-    | Null -> error(Error(FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("constructor", "", parentTI.FullName, parentTI.Assembly.FullName), range0))
+    | Null ->
+        error (
+            Error(
+                FSComp.SR.itemNotFoundInTypeDuringDynamicCodeGen ("constructor", "", parentTI.FullName, parentTI.Assembly.FullName),
+                range0
+            )
+        )
     | NonNull res -> res
 
 let emitLabelMark emEnv (ilG: ILGenerator) (label: ILCodeLabel) =
@@ -851,27 +1244,28 @@ let emitLabelMark emEnv (ilG: ILGenerator) (label: ILCodeLabel) =
 ///Emit comparison instructions.
 let emitInstrCompare emEnv (ilG: ILGenerator) comp targ =
     match comp with
-    | BI_beq -> ilG.EmitAndLog (OpCodes.Beq, envGetLabel emEnv targ)
-    | BI_bge -> ilG.EmitAndLog (OpCodes.Bge, envGetLabel emEnv targ)
-    | BI_bge_un -> ilG.EmitAndLog (OpCodes.Bge_Un, envGetLabel emEnv targ)
-    | BI_bgt -> ilG.EmitAndLog (OpCodes.Bgt, envGetLabel emEnv targ)
-    | BI_bgt_un -> ilG.EmitAndLog (OpCodes.Bgt_Un, envGetLabel emEnv targ)
-    | BI_ble -> ilG.EmitAndLog (OpCodes.Ble, envGetLabel emEnv targ)
-    | BI_ble_un -> ilG.EmitAndLog (OpCodes.Ble_Un, envGetLabel emEnv targ)
-    | BI_blt -> ilG.EmitAndLog (OpCodes.Blt, envGetLabel emEnv targ)
-    | BI_blt_un -> ilG.EmitAndLog (OpCodes.Blt_Un, envGetLabel emEnv targ)
-    | BI_bne_un -> ilG.EmitAndLog (OpCodes.Bne_Un, envGetLabel emEnv targ)
-    | BI_brfalse -> ilG.EmitAndLog (OpCodes.Brfalse, envGetLabel emEnv targ)
-    | BI_brtrue -> ilG.EmitAndLog (OpCodes.Brtrue, envGetLabel emEnv targ)
-
+    | BI_beq -> ilG.EmitAndLog(OpCodes.Beq, envGetLabel emEnv targ)
+    | BI_bge -> ilG.EmitAndLog(OpCodes.Bge, envGetLabel emEnv targ)
+    | BI_bge_un -> ilG.EmitAndLog(OpCodes.Bge_Un, envGetLabel emEnv targ)
+    | BI_bgt -> ilG.EmitAndLog(OpCodes.Bgt, envGetLabel emEnv targ)
+    | BI_bgt_un -> ilG.EmitAndLog(OpCodes.Bgt_Un, envGetLabel emEnv targ)
+    | BI_ble -> ilG.EmitAndLog(OpCodes.Ble, envGetLabel emEnv targ)
+    | BI_ble_un -> ilG.EmitAndLog(OpCodes.Ble_Un, envGetLabel emEnv targ)
+    | BI_blt -> ilG.EmitAndLog(OpCodes.Blt, envGetLabel emEnv targ)
+    | BI_blt_un -> ilG.EmitAndLog(OpCodes.Blt_Un, envGetLabel emEnv targ)
+    | BI_bne_un -> ilG.EmitAndLog(OpCodes.Bne_Un, envGetLabel emEnv targ)
+    | BI_brfalse -> ilG.EmitAndLog(OpCodes.Brfalse, envGetLabel emEnv targ)
+    | BI_brtrue -> ilG.EmitAndLog(OpCodes.Brtrue, envGetLabel emEnv targ)
 
 /// Emit the volatile. prefix
-let emitInstrVolatile (ilG: ILGenerator) = function
+let emitInstrVolatile (ilG: ILGenerator) =
+    function
     | Volatile -> ilG.EmitAndLog OpCodes.Volatile
     | Nonvolatile -> ()
 
 /// Emit the align. prefix
-let emitInstrAlign (ilG: ILGenerator) = function
+let emitInstrAlign (ilG: ILGenerator) =
+    function
     | Aligned -> ()
     | Unaligned1 -> ilG.Emit(OpCodes.Unaligned, 1L) // note: doc says use "long" overload!
     | Unaligned2 -> ilG.Emit(OpCodes.Unaligned, 2L)
@@ -880,12 +1274,15 @@ let emitInstrAlign (ilG: ILGenerator) = function
 /// Emit the tail. prefix if necessary
 let emitInstrTail (cenv: cenv) (ilG: ILGenerator) tail emitTheCall =
     match tail with
-    | Tailcall when cenv.emitTailcalls -> ilG.EmitAndLog OpCodes.Tailcall; emitTheCall(); ilG.EmitAndLog OpCodes.Ret
-    | _ -> emitTheCall()
+    | Tailcall when cenv.emitTailcalls ->
+        ilG.EmitAndLog OpCodes.Tailcall
+        emitTheCall ()
+        ilG.EmitAndLog OpCodes.Ret
+    | _ -> emitTheCall ()
 
 let emitInstrNewobj cenv emEnv (ilG: ILGenerator) mspec varargs =
     match varargs with
-    | None -> ilG.EmitAndLog (OpCodes.Newobj, convConstructorSpec cenv emEnv mspec)
+    | None -> ilG.EmitAndLog(OpCodes.Newobj, convConstructorSpec cenv emEnv mspec)
     | Some _varargTys -> failwith "emit: pending new varargs" // XXX - gap
 
 let emitSilverlightCheck (ilG: ILGenerator) =
@@ -896,21 +1293,23 @@ let emitInstrCall cenv emEnv (ilG: ILGenerator) opCall tail (mspec: ILMethodSpec
     emitInstrTail cenv ilG tail (fun () ->
         if mspec.MethodRef.Name = ".ctor" || mspec.MethodRef.Name = ".cctor" then
             let cinfo = convConstructorSpec cenv emEnv mspec
+
             match varargs with
-            | None -> ilG.EmitAndLog (opCall, cinfo)
+            | None -> ilG.EmitAndLog(opCall, cinfo)
             | Some _varargTys -> failwith "emitInstrCall: .ctor and varargs"
         else
             let minfo = convMethodSpec cenv emEnv mspec
+
             match varargs with
-            | None -> ilG.EmitAndLog (opCall, minfo)
-            | Some varargTys -> ilG.EmitCall (opCall, minfo, convTypesToArray cenv emEnv varargTys)
-    )
+            | None -> ilG.EmitAndLog(opCall, minfo)
+            | Some varargTys -> ilG.EmitCall(opCall, minfo, convTypesToArray cenv emEnv varargTys))
 
 let getGenericMethodDefinition q (ty: Type) =
     let gminfo =
         match q with
-        | Quotations.Patterns.Call(_, minfo, _) -> minfo.GetGenericMethodDefinition()
+        | Quotations.Patterns.Call (_, minfo, _) -> minfo.GetGenericMethodDefinition()
         | _ -> failwith "unexpected failure decoding quotation at ilreflect startup"
+
     gminfo.MakeGenericMethod [| ty |]
 
 let getArrayMethInfo n ty =
@@ -927,7 +1326,6 @@ let setArrayMethInfo n ty =
     | 4 -> getGenericMethodDefinition <@@ LanguagePrimitives.IntrinsicFunctions.SetArray4D null 0 0 0 0 0 @@> ty
     | _ -> invalidArg "n" "not expecting array dimension > 4"
 
-
 //----------------------------------------------------------------------------
 // emitInstr cenv
 //----------------------------------------------------------------------------
@@ -1016,16 +1414,17 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
     | AI_pop -> ilG.EmitAndLog OpCodes.Pop
     | AI_ckfinite -> ilG.EmitAndLog OpCodes.Ckfinite
     | AI_nop -> ilG.EmitAndLog OpCodes.Nop
-    | AI_ldc (DT_I4, ILConst.I4 i32) -> ilG.EmitAndLog (OpCodes.Ldc_I4, i32)
+    | AI_ldc (DT_I4, ILConst.I4 i32) -> ilG.EmitAndLog(OpCodes.Ldc_I4, i32)
     | AI_ldc (DT_I8, ILConst.I8 i64) -> ilG.Emit(OpCodes.Ldc_I8, i64)
     | AI_ldc (DT_R4, ILConst.R4 r32) -> ilG.Emit(OpCodes.Ldc_R4, r32)
     | AI_ldc (DT_R8, ILConst.R8 r64) -> ilG.Emit(OpCodes.Ldc_R8, r64)
     | AI_ldc _ -> failwith "emitInstrI_arith (AI_ldc (ty, const)) iltyped"
-    | I_ldarg u16 -> ilG.EmitAndLog (OpCodes.Ldarg, int16 u16)
-    | I_ldarga u16 -> ilG.EmitAndLog (OpCodes.Ldarga, int16 u16)
+    | I_ldarg u16 -> ilG.EmitAndLog(OpCodes.Ldarg, int16 u16)
+    | I_ldarga u16 -> ilG.EmitAndLog(OpCodes.Ldarga, int16 u16)
     | I_ldind (align, vol, dt) ->
         emitInstrAlign ilG align
         emitInstrVolatile ilG vol
+
         match dt with
         | DT_I -> ilG.EmitAndLog OpCodes.Ldind_I
         | DT_I1 -> ilG.EmitAndLog OpCodes.Ldind_I1
@@ -1041,12 +1440,13 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
         | DT_U4 -> ilG.EmitAndLog OpCodes.Ldind_U4
         | DT_U8 -> failwith "emitInstr cenv: ldind U8"
         | DT_REF -> ilG.EmitAndLog OpCodes.Ldind_Ref
-    | I_ldloc u16 -> ilG.EmitAndLog (OpCodes.Ldloc, int16 u16)
-    | I_ldloca u16 -> ilG.EmitAndLog (OpCodes.Ldloca, int16 u16)
-    | I_starg u16 -> ilG.EmitAndLog (OpCodes.Starg, int16 u16)
+    | I_ldloc u16 -> ilG.EmitAndLog(OpCodes.Ldloc, int16 u16)
+    | I_ldloca u16 -> ilG.EmitAndLog(OpCodes.Ldloca, int16 u16)
+    | I_starg u16 -> ilG.EmitAndLog(OpCodes.Starg, int16 u16)
     | I_stind (align, vol, dt) ->
         emitInstrAlign ilG align
         emitInstrVolatile ilG vol
+
         match dt with
         | DT_I -> ilG.EmitAndLog OpCodes.Stind_I
         | DT_I1 -> ilG.EmitAndLog OpCodes.Stind_I1
@@ -1056,15 +1456,15 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
         | DT_R -> failwith "emitInstr cenv: stind R"
         | DT_R4 -> ilG.EmitAndLog OpCodes.Stind_R4
         | DT_R8 -> ilG.EmitAndLog OpCodes.Stind_R8
-        | DT_U -> ilG.EmitAndLog OpCodes.Stind_I    // NOTE: unsigned -> int conversion
-        | DT_U1 -> ilG.EmitAndLog OpCodes.Stind_I1   // NOTE: follows code ilwrite.fs
-        | DT_U2 -> ilG.EmitAndLog OpCodes.Stind_I2   // NOTE: is it ok?
-        | DT_U4 -> ilG.EmitAndLog OpCodes.Stind_I4   // NOTE: it is generated by bytearray tests
-        | DT_U8 -> ilG.EmitAndLog OpCodes.Stind_I8   // NOTE: unsigned -> int conversion
+        | DT_U -> ilG.EmitAndLog OpCodes.Stind_I // NOTE: unsigned -> int conversion
+        | DT_U1 -> ilG.EmitAndLog OpCodes.Stind_I1 // NOTE: follows code ilwrite.fs
+        | DT_U2 -> ilG.EmitAndLog OpCodes.Stind_I2 // NOTE: is it ok?
+        | DT_U4 -> ilG.EmitAndLog OpCodes.Stind_I4 // NOTE: it is generated by bytearray tests
+        | DT_U8 -> ilG.EmitAndLog OpCodes.Stind_I8 // NOTE: unsigned -> int conversion
         | DT_REF -> ilG.EmitAndLog OpCodes.Stind_Ref
-    | I_stloc u16 -> ilG.EmitAndLog (OpCodes.Stloc, int16 u16)
-    | I_br targ -> ilG.EmitAndLog (OpCodes.Br, envGetLabel emEnv targ)
-    | I_jmp mspec -> ilG.EmitAndLog (OpCodes.Jmp, convMethodSpec cenv emEnv mspec)
+    | I_stloc u16 -> ilG.EmitAndLog(OpCodes.Stloc, int16 u16)
+    | I_br targ -> ilG.EmitAndLog(OpCodes.Br, envGetLabel emEnv targ)
+    | I_jmp mspec -> ilG.EmitAndLog(OpCodes.Jmp, convMethodSpec cenv emEnv mspec)
     | I_brcmp (comp, targ) -> emitInstrCompare emEnv ilG comp targ
     | I_switch labels -> ilG.Emit(OpCodes.Switch, Array.ofList (List.map (envGetLabel emEnv) labels))
     | I_ret -> ilG.EmitAndLog OpCodes.Ret
@@ -1083,69 +1483,76 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
 
     | I_calli (tail, callsig, None) ->
         emitInstrTail cenv ilG tail (fun () ->
-        ilG.EmitCalli(OpCodes.Calli,
-                      convCallConv callsig.CallingConv,
-                      convType cenv emEnv callsig.ReturnType,
-                      convTypesToArray cenv emEnv callsig.ArgTypes,
-                      Unchecked.defaultof))
+            ilG.EmitCalli(
+                OpCodes.Calli,
+                convCallConv callsig.CallingConv,
+                convType cenv emEnv callsig.ReturnType,
+                convTypesToArray cenv emEnv callsig.ArgTypes,
+                Unchecked.defaultof
+            ))
 
     | I_calli (tail, callsig, Some varargTys) ->
         emitInstrTail cenv ilG tail (fun () ->
-        ilG.EmitCalli(OpCodes.Calli,
-                      convCallConv callsig.CallingConv,
-                      convType cenv emEnv callsig.ReturnType,
-                      convTypesToArray cenv emEnv callsig.ArgTypes,
-                      convTypesToArray cenv emEnv varargTys))
+            ilG.EmitCalli(
+                OpCodes.Calli,
+                convCallConv callsig.CallingConv,
+                convType cenv emEnv callsig.ReturnType,
+                convTypesToArray cenv emEnv callsig.ArgTypes,
+                convTypesToArray cenv emEnv varargTys
+            ))
 
-    | I_ldftn mspec ->
-        ilG.EmitAndLog (OpCodes.Ldftn, convMethodSpec cenv emEnv mspec)
+    | I_ldftn mspec -> ilG.EmitAndLog(OpCodes.Ldftn, convMethodSpec cenv emEnv mspec)
 
-    | I_newobj (mspec, varargs) ->
-        emitInstrNewobj cenv emEnv ilG mspec varargs
+    | I_newobj (mspec, varargs) -> emitInstrNewobj cenv emEnv ilG mspec varargs
 
     | I_throw -> ilG.EmitAndLog OpCodes.Throw
     | I_endfinally -> ilG.EmitAndLog OpCodes.Endfinally
     | I_endfilter -> ilG.EmitAndLog OpCodes.Endfilter
-    | I_leave label -> ilG.EmitAndLog (OpCodes.Leave, envGetLabel emEnv label)
-    | I_ldsfld (vol, fspec) -> emitInstrVolatile ilG vol; ilG.EmitAndLog (OpCodes.Ldsfld, convFieldSpec cenv emEnv fspec)
-    | I_ldfld (align, vol, fspec) -> emitInstrAlign ilG align; emitInstrVolatile ilG vol; ilG.EmitAndLog (OpCodes.Ldfld, convFieldSpec cenv emEnv fspec)
-    | I_ldsflda fspec -> ilG.EmitAndLog (OpCodes.Ldsflda, convFieldSpec cenv emEnv fspec)
-    | I_ldflda fspec -> ilG.EmitAndLog (OpCodes.Ldflda, convFieldSpec cenv emEnv fspec)
+    | I_leave label -> ilG.EmitAndLog(OpCodes.Leave, envGetLabel emEnv label)
+    | I_ldsfld (vol, fspec) ->
+        emitInstrVolatile ilG vol
+        ilG.EmitAndLog(OpCodes.Ldsfld, convFieldSpec cenv emEnv fspec)
+    | I_ldfld (align, vol, fspec) ->
+        emitInstrAlign ilG align
+        emitInstrVolatile ilG vol
+        ilG.EmitAndLog(OpCodes.Ldfld, convFieldSpec cenv emEnv fspec)
+    | I_ldsflda fspec -> ilG.EmitAndLog(OpCodes.Ldsflda, convFieldSpec cenv emEnv fspec)
+    | I_ldflda fspec -> ilG.EmitAndLog(OpCodes.Ldflda, convFieldSpec cenv emEnv fspec)
 
     | I_stsfld (vol, fspec) ->
         emitInstrVolatile ilG vol
-        ilG.EmitAndLog (OpCodes.Stsfld, convFieldSpec cenv emEnv fspec)
+        ilG.EmitAndLog(OpCodes.Stsfld, convFieldSpec cenv emEnv fspec)
 
     | I_stfld (align, vol, fspec) ->
         emitInstrAlign ilG align
         emitInstrVolatile ilG vol
-        ilG.EmitAndLog (OpCodes.Stfld, convFieldSpec cenv emEnv fspec)
-
-    | I_ldstr s -> ilG.EmitAndLog (OpCodes.Ldstr, s)
-    | I_isinst ty -> ilG.EmitAndLog (OpCodes.Isinst, convType cenv emEnv ty)
-    | I_castclass ty -> ilG.EmitAndLog (OpCodes.Castclass, convType cenv emEnv ty)
-    | I_ldtoken (ILToken.ILType ty) -> ilG.EmitAndLog (OpCodes.Ldtoken, convTypeOrTypeDef cenv emEnv ty)
-    | I_ldtoken (ILToken.ILMethod mspec) -> ilG.EmitAndLog (OpCodes.Ldtoken, convMethodSpec cenv emEnv mspec)
-    | I_ldtoken (ILToken.ILField fspec) -> ilG.EmitAndLog (OpCodes.Ldtoken, convFieldSpec cenv emEnv fspec)
-    | I_ldvirtftn mspec -> ilG.EmitAndLog (OpCodes.Ldvirtftn, convMethodSpec cenv emEnv mspec)
+        ilG.EmitAndLog(OpCodes.Stfld, convFieldSpec cenv emEnv fspec)
+
+    | I_ldstr s -> ilG.EmitAndLog(OpCodes.Ldstr, s)
+    | I_isinst ty -> ilG.EmitAndLog(OpCodes.Isinst, convType cenv emEnv ty)
+    | I_castclass ty -> ilG.EmitAndLog(OpCodes.Castclass, convType cenv emEnv ty)
+    | I_ldtoken (ILToken.ILType ty) -> ilG.EmitAndLog(OpCodes.Ldtoken, convTypeOrTypeDef cenv emEnv ty)
+    | I_ldtoken (ILToken.ILMethod mspec) -> ilG.EmitAndLog(OpCodes.Ldtoken, convMethodSpec cenv emEnv mspec)
+    | I_ldtoken (ILToken.ILField fspec) -> ilG.EmitAndLog(OpCodes.Ldtoken, convFieldSpec cenv emEnv fspec)
+    | I_ldvirtftn mspec -> ilG.EmitAndLog(OpCodes.Ldvirtftn, convMethodSpec cenv emEnv mspec)
     // Value type instructions
-    | I_cpobj ty -> ilG.EmitAndLog (OpCodes.Cpobj, convType cenv emEnv ty)
-    | I_initobj ty -> ilG.EmitAndLog (OpCodes.Initobj, convType cenv emEnv ty)
+    | I_cpobj ty -> ilG.EmitAndLog(OpCodes.Cpobj, convType cenv emEnv ty)
+    | I_initobj ty -> ilG.EmitAndLog(OpCodes.Initobj, convType cenv emEnv ty)
 
     | I_ldobj (align, vol, ty) ->
         emitInstrAlign ilG align
         emitInstrVolatile ilG vol
-        ilG.EmitAndLog (OpCodes.Ldobj, convType cenv emEnv ty)
+        ilG.EmitAndLog(OpCodes.Ldobj, convType cenv emEnv ty)
 
     | I_stobj (align, vol, ty) ->
         emitInstrAlign ilG align
         emitInstrVolatile ilG vol
-        ilG.EmitAndLog (OpCodes.Stobj, convType cenv emEnv ty)
+        ilG.EmitAndLog(OpCodes.Stobj, convType cenv emEnv ty)
 
-    | I_box ty -> ilG.EmitAndLog (OpCodes.Box, convType cenv emEnv ty)
-    | I_unbox ty -> ilG.EmitAndLog (OpCodes.Unbox, convType cenv emEnv ty)
-    | I_unbox_any ty -> ilG.EmitAndLog (OpCodes.Unbox_Any, convType cenv emEnv ty)
-    | I_sizeof ty -> ilG.EmitAndLog (OpCodes.Sizeof, convType cenv emEnv ty)
+    | I_box ty -> ilG.EmitAndLog(OpCodes.Box, convType cenv emEnv ty)
+    | I_unbox ty -> ilG.EmitAndLog(OpCodes.Unbox, convType cenv emEnv ty)
+    | I_unbox_any ty -> ilG.EmitAndLog(OpCodes.Unbox_Any, convType cenv emEnv ty)
+    | I_sizeof ty -> ilG.EmitAndLog(OpCodes.Sizeof, convType cenv emEnv ty)
 
     // Generalized array instructions.
     // In AbsIL these instructions include
@@ -1194,23 +1601,29 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
         | DT_REF -> ilG.EmitAndLog OpCodes.Stelem_Ref
 
     | I_ldelema (ro, _isNativePtr, shape, ty) ->
-        if (ro = ReadonlyAddress) then ilG.EmitAndLog OpCodes.Readonly
+        if (ro = ReadonlyAddress) then
+            ilG.EmitAndLog OpCodes.Readonly
+
         if shape = ILArrayShape.SingleDimensional then
-            ilG.EmitAndLog (OpCodes.Ldelema, convType cenv emEnv ty)
+            ilG.EmitAndLog(OpCodes.Ldelema, convType cenv emEnv ty)
         else
             let arrayTy = convType cenv emEnv (ILType.Array(shape, ty))
             let elemTy = arrayTy.GetElementType()
             let argTys = Array.create shape.Rank typeof
             let retTy = elemTy.MakeByRefType()
-            let meth = modB.GetArrayMethodAndLog (arrayTy, "Address", CallingConventions.HasThis, retTy, argTys)
-            ilG.EmitAndLog (OpCodes.Call, meth)
+
+            let meth =
+                modB.GetArrayMethodAndLog(arrayTy, "Address", CallingConventions.HasThis, retTy, argTys)
+
+            ilG.EmitAndLog(OpCodes.Call, meth)
 
     | I_ldelem_any (shape, ty) ->
         if shape = ILArrayShape.SingleDimensional then
-            ilG.EmitAndLog (OpCodes.Ldelem, convType cenv emEnv ty)
+            ilG.EmitAndLog(OpCodes.Ldelem, convType cenv emEnv ty)
         else
             let arrayTy = convType cenv emEnv (ILType.Array(shape, ty))
             let elemTy = arrayTy.GetElementType()
+
             let meth =
 #if ENABLE_MONO_SUPPORT
                 // See bug 6254: Mono has a bug in reflection-emit dynamic calls to the "Get", "Address" or "Set" methods on arrays
@@ -1218,15 +1631,17 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
                     getArrayMethInfo shape.Rank elemTy
                 else
 #endif
-                    modB.GetArrayMethodAndLog (arrayTy, "Get", CallingConventions.HasThis, elemTy, Array.create shape.Rank typeof )
-            ilG.EmitAndLog (OpCodes.Call, meth)
+                modB.GetArrayMethodAndLog(arrayTy, "Get", CallingConventions.HasThis, elemTy, Array.create shape.Rank typeof)
+
+            ilG.EmitAndLog(OpCodes.Call, meth)
 
     | I_stelem_any (shape, ty) ->
         if shape = ILArrayShape.SingleDimensional then
-            ilG.EmitAndLog (OpCodes.Stelem, convType cenv emEnv ty)
+            ilG.EmitAndLog(OpCodes.Stelem, convType cenv emEnv ty)
         else
             let arrayTy = convType cenv emEnv (ILType.Array(shape, ty))
             let elemTy = arrayTy.GetElementType()
+
             let meth =
 #if ENABLE_MONO_SUPPORT
                 // See bug 6254: Mono has a bug in reflection-emit dynamic calls to the "Get", "Address" or "Set" methods on arrays
@@ -1234,21 +1649,31 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
                     setArrayMethInfo shape.Rank elemTy
                 else
 #endif
-                    modB.GetArrayMethodAndLog(arrayTy, "Set", CallingConventions.HasThis, null, Array.append (Array.create shape.Rank typeof) (Array.ofList [ elemTy ]))
-            ilG.EmitAndLog (OpCodes.Call, meth)
+                modB.GetArrayMethodAndLog(
+                    arrayTy,
+                    "Set",
+                    CallingConventions.HasThis,
+                    null,
+                    Array.append (Array.create shape.Rank typeof) (Array.ofList [ elemTy ])
+                )
+
+            ilG.EmitAndLog(OpCodes.Call, meth)
 
     | I_newarr (shape, ty) ->
         if shape = ILArrayShape.SingleDimensional then
-            ilG.EmitAndLog (OpCodes.Newarr, convType cenv emEnv ty)
+            ilG.EmitAndLog(OpCodes.Newarr, convType cenv emEnv ty)
         else
             let arrayTy = convType cenv emEnv (ILType.Array(shape, ty))
-            let meth = modB.GetArrayMethodAndLog(arrayTy, ".ctor", CallingConventions.HasThis, null, Array.create shape.Rank typeof)
-            ilG.EmitAndLog (OpCodes.Newobj, meth)
+
+            let meth =
+                modB.GetArrayMethodAndLog(arrayTy, ".ctor", CallingConventions.HasThis, null, Array.create shape.Rank typeof)
+
+            ilG.EmitAndLog(OpCodes.Newobj, meth)
 
     | I_ldlen -> ilG.EmitAndLog OpCodes.Ldlen
-    | I_mkrefany ty -> ilG.EmitAndLog (OpCodes.Mkrefany, convType cenv emEnv ty)
+    | I_mkrefany ty -> ilG.EmitAndLog(OpCodes.Mkrefany, convType cenv emEnv ty)
     | I_refanytype -> ilG.EmitAndLog OpCodes.Refanytype
-    | I_refanyval ty -> ilG.EmitAndLog (OpCodes.Refanyval, convType cenv emEnv ty)
+    | I_refanyval ty -> ilG.EmitAndLog(OpCodes.Refanyval, convType cenv emEnv ty)
     | I_rethrow -> ilG.EmitAndLog OpCodes.Rethrow
     | I_break -> ilG.EmitAndLog OpCodes.Break
     | I_seqpoint src ->
@@ -1257,9 +1682,20 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
         ()
 #else
         if cenv.generatePdb && not (src.Document.File.EndsWithOrdinal("stdin")) then
-            let guid x = match x with None -> Guid.Empty | Some g -> Guid(g: byte[]) in
-            let symDoc = modB.DefineDocumentAndLog (src.Document.File, guid src.Document.Language, guid src.Document.Vendor, guid src.Document.DocumentType)
-            ilG.MarkSequencePointAndLog (symDoc, src.Line, src.Column, src.EndLine, src.EndColumn)
+            let guid x =
+                match x with
+                | None -> Guid.Empty
+                | Some g -> Guid(g: byte[]) in
+
+            let symDoc =
+                modB.DefineDocumentAndLog(
+                    src.Document.File,
+                    guid src.Document.Language,
+                    guid src.Document.Vendor,
+                    guid src.Document.DocumentType
+                )
+
+            ilG.MarkSequencePointAndLog(symDoc, src.Line, src.Column, src.EndLine, src.EndColumn)
 #endif
     | I_arglist -> ilG.EmitAndLog OpCodes.Arglist
     | I_localloc -> ilG.EmitAndLog OpCodes.Localloc
@@ -1276,54 +1712,73 @@ let rec emitInstr cenv (modB: ModuleBuilder) emEnv (ilG: ILGenerator) instr =
 
     | EI_ldlen_multi (_, m) ->
         emitInstr cenv modB emEnv ilG (mkLdcInt32 m)
-        emitInstr cenv modB emEnv ilG (mkNormalCall(mkILNonGenericMethSpecInTy(cenv.ilg.typ_Array, ILCallingConv.Instance, "GetLength", [cenv.ilg.typ_Int32], cenv.ilg.typ_Int32)))
 
-    | i -> failwithf "the IL instruction %s cannot be emitted" (i.ToString())
+        emitInstr
+            cenv
+            modB
+            emEnv
+            ilG
+            (mkNormalCall (
+                mkILNonGenericMethSpecInTy (
+                    cenv.ilg.typ_Array,
+                    ILCallingConv.Instance,
+                    "GetLength",
+                    [ cenv.ilg.typ_Int32 ],
+                    cenv.ilg.typ_Int32
+                )
+            ))
 
+    | i -> failwithf "the IL instruction %s cannot be emitted" (i.ToString())
 
 let emitCode cenv modB emEnv (ilG: ILGenerator) (code: ILCode) =
     // Pre-define the labels pending determining their actual marks
     let pc2lab = Dictionary()
+
     let emEnv =
-        (emEnv, code.Labels) ||> Seq.fold (fun emEnv (KeyValue(label, pc)) ->
-            let lab = ilG.DefineLabelAndLog ()
+        (emEnv, code.Labels)
+        ||> Seq.fold (fun emEnv (KeyValue (label, pc)) ->
+            let lab = ilG.DefineLabelAndLog()
+
             pc2lab[pc] <-
                 match pc2lab.TryGetValue pc with
                 | true, labels -> lab :: labels
-                | _ -> [lab]
+                | _ -> [ lab ]
+
             envSetLabel emEnv label lab)
 
     // Build a table that contains the operations that define where exception handlers are
     let pc2action = Dictionary()
     let lab2pc = code.Labels
+
     let add lab action =
         let pc = lab2pc[lab]
+
         pc2action[pc] <-
             match pc2action.TryGetValue pc with
-            | true, actions -> actions @ [action]
-            | _ -> [action]
+            | true, actions -> actions @ [ action ]
+            | _ -> [ action ]
 
     for exnSpec in code.Exceptions do
         let startTry, _endTry = exnSpec.Range
 
-        add startTry (fun () -> ilG.BeginExceptionBlockAndLog () |> ignore)
+        add startTry (fun () -> ilG.BeginExceptionBlockAndLog() |> ignore)
 
         match exnSpec.Clause with
-        | ILExceptionClause.Finally(startHandler, endHandler) ->
+        | ILExceptionClause.Finally (startHandler, endHandler) ->
             add startHandler ilG.BeginFinallyBlockAndLog
             add endHandler ilG.EndExceptionBlockAndLog
 
-        | ILExceptionClause.Fault(startHandler, endHandler) ->
+        | ILExceptionClause.Fault (startHandler, endHandler) ->
             add startHandler ilG.BeginFaultBlockAndLog
             add endHandler ilG.EndExceptionBlockAndLog
 
-        | ILExceptionClause.FilterCatch((startFilter, _), (startHandler, endHandler)) ->
+        | ILExceptionClause.FilterCatch ((startFilter, _), (startHandler, endHandler)) ->
             add startFilter ilG.BeginExceptFilterBlockAndLog
             add startHandler (fun () -> ilG.BeginCatchBlockAndLog null)
             add endHandler ilG.EndExceptionBlockAndLog
 
-        | ILExceptionClause.TypeCatch(ty, (startHandler, endHandler)) ->
-            add startHandler (fun () -> ilG.BeginCatchBlockAndLog (convType cenv emEnv ty))
+        | ILExceptionClause.TypeCatch (ty, (startHandler, endHandler)) ->
+            add startHandler (fun () -> ilG.BeginCatchBlockAndLog(convType cenv emEnv ty))
             add endHandler ilG.EndExceptionBlockAndLog
 
     // Emit the instructions
@@ -1333,7 +1788,7 @@ let emitCode cenv modB emEnv (ilG: ILGenerator) (code: ILCode) =
         match pc2action.TryGetValue pc with
         | true, actions ->
             for action in actions do
-                action()
+                action ()
         | _ -> ()
 
         match pc2lab.TryGetValue pc with
@@ -1347,13 +1802,12 @@ let emitCode cenv modB emEnv (ilG: ILGenerator) (code: ILCode) =
             | I_br l when code.Labels[l] = pc + 1 -> () // compress I_br to next instruction
             | i -> emitInstr cenv modB emEnv ilG i
 
-
 let emitLocal cenv emEnv (ilG: ILGenerator) (local: ILLocal) =
     let ty = convType cenv emEnv local.Type
-    let locBuilder = ilG.DeclareLocalAndLog (ty, local.IsPinned)
+    let locBuilder = ilG.DeclareLocalAndLog(ty, local.IsPinned)
 #if !FX_NO_PDB_WRITER
     match local.DebugInfo with
-    | Some(nm, start, finish) -> locBuilder.SetLocalSymInfo(nm, start, finish)
+    | Some (nm, start, finish) -> locBuilder.SetLocalSymInfo(nm, start, finish)
     | None -> ()
 #endif
     locBuilder
@@ -1363,10 +1817,9 @@ let emitILMethodBody cenv modB emEnv (ilG: ILGenerator) (ilmbody: ILMethodBody)
     let emEnv = envSetLocals emEnv localBs
     emitCode cenv modB emEnv ilG ilmbody.Code
 
-
 let emitMethodBody cenv modB emEnv ilG _name (mbody: MethodBody) =
     match mbody with
-    | MethodBody.IL ilmbody -> emitILMethodBody cenv modB emEnv (ilG()) ilmbody.Value
+    | MethodBody.IL ilmbody -> emitILMethodBody cenv modB emEnv (ilG ()) ilmbody.Value
     | MethodBody.PInvoke _pinvoke -> ()
     | MethodBody.Abstract -> ()
     | MethodBody.Native -> failwith "emitMethodBody: native"
@@ -1377,8 +1830,7 @@ let convCustomAttr cenv emEnv (cattr: ILAttribute) =
     let data = getCustomAttrData cattr
     (methInfo, data)
 
-let emitCustomAttr cenv emEnv add cattr =
-    add (convCustomAttr cenv emEnv cattr)
+let emitCustomAttr cenv emEnv add cattr = add (convCustomAttr cenv emEnv cattr)
 
 let emitCustomAttrs cenv emEnv add (cattrs: ILAttributes) =
     cattrs.AsArray() |> Array.iter (emitCustomAttr cenv emEnv add)
@@ -1394,37 +1846,55 @@ let buildGenParamsPass1 _emEnv defineGenericParameters (gps: ILGenericParameterD
         let gpsNames = gps |> List.map (fun gp -> gp.Name)
         defineGenericParameters (Array.ofList gpsNames) |> ignore
 
-
 let buildGenParamsPass1b cenv emEnv (genArgs: Type array) (gps: ILGenericParameterDefs) =
     let genpBs = genArgs |> Array.map (fun x -> (x :?> GenericTypeParameterBuilder))
-    gps |> List.iteri (fun i (gp: ILGenericParameterDef) ->
+
+    gps
+    |> List.iteri (fun i (gp: ILGenericParameterDef) ->
         let gpB = genpBs[i]
         // the Constraints are either the parent (base) type or interfaces.
         let constraintTs = convTypes cenv emEnv gp.Constraints
-        let interfaceTs, baseTs = List.partition (fun (ty: Type) -> ty.IsInterface) constraintTs
+
+        let interfaceTs, baseTs =
+            List.partition (fun (ty: Type) -> ty.IsInterface) constraintTs
         // set base type constraint
         (match baseTs with
-            [ ] -> () // Q: should a baseType be set? It is in some samples. Should this be a failure case?
-          | [ baseT ] -> gpB.SetBaseTypeConstraint baseT
-          | _ -> failwith "buildGenParam: multiple base types"
-        )
+         | [] -> () // Q: should a baseType be set? It is in some samples. Should this be a failure case?
+         | [ baseT ] -> gpB.SetBaseTypeConstraint baseT
+         | _ -> failwith "buildGenParam: multiple base types")
         // set interface constraints (interfaces that instances of gp must meet)
         gpB.SetInterfaceConstraints(Array.ofList interfaceTs)
-        gp.CustomAttrs |> emitCustomAttrs cenv emEnv (wrapCustomAttr gpB.SetCustomAttribute)
+
+        gp.CustomAttrs
+        |> emitCustomAttrs cenv emEnv (wrapCustomAttr gpB.SetCustomAttribute)
 
         let flags = GenericParameterAttributes.None
+
         let flags =
-           match gp.Variance with
-           | NonVariant -> flags
-           | CoVariant -> flags ||| GenericParameterAttributes.Covariant
-           | ContraVariant -> flags ||| GenericParameterAttributes.Contravariant
+            match gp.Variance with
+            | NonVariant -> flags
+            | CoVariant -> flags ||| GenericParameterAttributes.Covariant
+            | ContraVariant -> flags ||| GenericParameterAttributes.Contravariant
+
+        let flags =
+            if gp.HasReferenceTypeConstraint then
+                flags ||| GenericParameterAttributes.ReferenceTypeConstraint
+            else
+                flags
 
-        let flags = if gp.HasReferenceTypeConstraint then flags ||| GenericParameterAttributes.ReferenceTypeConstraint else flags
-        let flags = if gp.HasNotNullableValueTypeConstraint then flags ||| GenericParameterAttributes.NotNullableValueTypeConstraint else flags
-        let flags = if gp.HasDefaultConstructorConstraint then flags ||| GenericParameterAttributes.DefaultConstructorConstraint else flags
+        let flags =
+            if gp.HasNotNullableValueTypeConstraint then
+                flags ||| GenericParameterAttributes.NotNullableValueTypeConstraint
+            else
+                flags
+
+        let flags =
+            if gp.HasDefaultConstructorConstraint then
+                flags ||| GenericParameterAttributes.DefaultConstructorConstraint
+            else
+                flags
 
-        gpB.SetGenericParameterAttributes flags
-    )
+        gpB.SetGenericParameterAttributes flags)
 //----------------------------------------------------------------------------
 // emitParameter
 //----------------------------------------------------------------------------
@@ -1433,15 +1903,17 @@ let emitParameter cenv emEnv (defineParameter: int * ParameterAttributes * strin
     //  -Type: ty
     //  -Default: ILFieldInit option
     //  -Marshal: NativeType option; (* Marshalling map for parameters. COM Interop only. *)
-    let attrs = flagsIf param.IsIn ParameterAttributes.In |||
-                flagsIf param.IsOut ParameterAttributes.Out |||
-                flagsIf param.IsOptional ParameterAttributes.Optional
+    let attrs =
+        flagsIf param.IsIn ParameterAttributes.In
+        ||| flagsIf param.IsOut ParameterAttributes.Out
+        ||| flagsIf param.IsOptional ParameterAttributes.Optional
+
     let name =
         match param.Name with
         | Some name -> name
-        | None -> "X" + string(i+1)
+        | None -> "X" + string (i + 1)
 
-    let parB = defineParameter(i, attrs, name)
+    let parB = defineParameter (i, attrs, name)
     emitCustomAttrs cenv emEnv (wrapCustomAttr parB.SetCustomAttribute) param.CustomAttrs
 
 //----------------------------------------------------------------------------
@@ -1456,20 +1928,23 @@ let enablePInvoke = true
 
 // Use reflection to invoke the api when we are executing on a platform that doesn't directly have this API.
 let definePInvokeMethod =
-    typeof.GetMethod("DefinePInvokeMethod", [|
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof
-        typeof |])
+    typeof.GetMethod
+        ("DefinePInvokeMethod",
+         [|
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+             typeof
+         |])
 
 let enablePInvoke = definePInvokeMethod <> null
 #endif
@@ -1479,11 +1954,13 @@ let rec buildMethodPass2 cenv tref (typB: TypeBuilder) emEnv (mdef: ILMethodDef)
     let implflags = mdef.ImplAttributes
     let cconv = convCallConv mdef.CallingConv
     let mref = mkRefToILMethod (tref, mdef)
+
     let emEnv =
         if mdef.IsEntryPoint && isNil mdef.ParameterTypes then
             envAddEntryPt emEnv (typB, mdef.Name)
         else
             emEnv
+
     match mdef.Body with
     | MethodBody.PInvoke pLazy when enablePInvoke ->
         let p = pLazy.Value
@@ -1512,50 +1989,84 @@ let rec buildMethodPass2 cenv tref (typB: TypeBuilder) emEnv (mdef: ILMethodDef)
 #if !FX_RESHAPED_REFEMIT || NETCOREAPP3_1
         // DefinePInvokeMethod was removed in early versions of coreclr, it was added back in NETCOREAPP3.
         // It has always been available in the desktop framework
-        let methB = typB.DefinePInvokeMethod(mdef.Name, p.Where.Name, p.Name, attrs, cconv, retTy, null, null, argTys, null, null, pcc, pcs)
+        let methB =
+            typB.DefinePInvokeMethod(mdef.Name, p.Where.Name, p.Name, attrs, cconv, retTy, null, null, argTys, null, null, pcc, pcs)
 #else
         // Use reflection to invoke the api when we are executing on a platform that doesn't directly have this API.
         let methB =
-            System.Diagnostics.Debug.Assert(definePInvokeMethod <> null, "Runtime does not have DefinePInvokeMethod")   // Absolutely can't happen
-            definePInvokeMethod.Invoke(typB,  [| mdef.Name; p.Where.Name; p.Name; attrs; cconv; retTy; null; null; argTys; null; null; pcc; pcs |]) :?> MethodBuilder
+            System.Diagnostics.Debug.Assert(definePInvokeMethod <> null, "Runtime does not have DefinePInvokeMethod") // Absolutely can't happen
+
+            definePInvokeMethod.Invoke(
+                typB,
+                [|
+                    mdef.Name
+                    p.Where.Name
+                    p.Name
+                    attrs
+                    cconv
+                    retTy
+                    null
+                    null
+                    argTys
+                    null
+                    null
+                    pcc
+                    pcs
+                |]
+            )
+            :?> MethodBuilder
 #endif
         methB.SetImplementationFlagsAndLog implflags
         envBindMethodRef emEnv mref methB
 
     | _ ->
-      match mdef.Name with
-      | ".cctor"
-      | ".ctor" ->
-          let consB = typB.DefineConstructorAndLog (attrs, cconv, convTypesToArray cenv emEnv mdef.ParameterTypes)
-          consB.SetImplementationFlagsAndLog implflags
-          envBindConsRef emEnv mref consB
-      | _name ->
-          // The return/argument types may involve the generic parameters
-          let methB = typB.DefineMethodAndLog (mdef.Name, attrs, cconv)
-
-          // Method generic type parameters
-          buildGenParamsPass1 emEnv methB.DefineGenericParametersAndLog mdef.GenericParams
-          let genArgs = getGenericArgumentsOfMethod methB
-          let emEnv = envPushTyvars emEnv (Array.append (getGenericArgumentsOfType (typB.AsType())) genArgs)
-          buildGenParamsPass1b cenv emEnv genArgs mdef.GenericParams
-
-          // Set parameter and return types (may depend on generic args)
-          let parameterTypes = convTypesToArray cenv emEnv mdef.ParameterTypes
-          let parameterTypeRequiredCustomModifiers,parameterTypeOptionalCustomModifiers =
-              mdef.Parameters
-              |> List.toArray
-              |> Array.map (convParamModifiers cenv emEnv)
-              |> Array.unzip
-
-          let returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers = mdef.Return |> convReturnModifiers cenv emEnv
-          let returnType = convType cenv emEnv mdef.Return.Type
-
-          methB.SetSignatureAndLog (returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers,parameterTypeOptionalCustomModifiers)
-
-          let emEnv = envPopTyvars emEnv
-          methB.SetImplementationFlagsAndLog implflags
-          envBindMethodRef emEnv mref methB
-
+        match mdef.Name with
+        | ".cctor"
+        | ".ctor" ->
+            let consB =
+                typB.DefineConstructorAndLog(attrs, cconv, convTypesToArray cenv emEnv mdef.ParameterTypes)
+
+            consB.SetImplementationFlagsAndLog implflags
+            envBindConsRef emEnv mref consB
+        | _name ->
+            // The return/argument types may involve the generic parameters
+            let methB = typB.DefineMethodAndLog(mdef.Name, attrs, cconv)
+
+            // Method generic type parameters
+            buildGenParamsPass1 emEnv methB.DefineGenericParametersAndLog mdef.GenericParams
+            let genArgs = getGenericArgumentsOfMethod methB
+
+            let emEnv =
+                envPushTyvars emEnv (Array.append (getGenericArgumentsOfType (typB.AsType())) genArgs)
+
+            buildGenParamsPass1b cenv emEnv genArgs mdef.GenericParams
+
+            // Set parameter and return types (may depend on generic args)
+            let parameterTypes = convTypesToArray cenv emEnv mdef.ParameterTypes
+
+            let parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers =
+                mdef.Parameters
+                |> List.toArray
+                |> Array.map (convParamModifiers cenv emEnv)
+                |> Array.unzip
+
+            let returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers =
+                mdef.Return |> convReturnModifiers cenv emEnv
+
+            let returnType = convType cenv emEnv mdef.Return.Type
+
+            methB.SetSignatureAndLog(
+                returnType,
+                returnTypeRequiredCustomModifiers,
+                returnTypeOptionalCustomModifiers,
+                parameterTypes,
+                parameterTypeRequiredCustomModifiers,
+                parameterTypeOptionalCustomModifiers
+            )
+
+            let emEnv = envPopTyvars emEnv
+            methB.SetImplementationFlagsAndLog implflags
+            envBindMethodRef emEnv mref methB
 
 //----------------------------------------------------------------------------
 // buildMethodPass3 cenv
@@ -1563,41 +2074,50 @@ let rec buildMethodPass2 cenv tref (typB: TypeBuilder) emEnv (mdef: ILMethodDef)
 
 let rec buildMethodPass3 cenv tref modB (typB: TypeBuilder) emEnv (mdef: ILMethodDef) =
     let mref = mkRefToILMethod (tref, mdef)
+
     let isPInvoke =
         match mdef.Body with
         | MethodBody.PInvoke _p -> true
         | _ -> false
+
     match mdef.Name with
-    | ".cctor" | ".ctor" ->
-          let consB = envGetConsB emEnv mref
-          // Constructors can not have generic parameters
-          assert isNil mdef.GenericParams
-          // Value parameters
-          let defineParameter (i, attr, name) = consB.DefineParameterAndLog (i+1, attr, name)
-          mdef.Parameters |> List.iteri (emitParameter cenv emEnv defineParameter)
-          // Body
-          emitMethodBody cenv modB emEnv consB.GetILGenerator mdef.Name mdef.Body
-          emitCustomAttrs cenv emEnv (wrapCustomAttr consB.SetCustomAttribute) mdef.CustomAttrs
-          ()
-     | _name ->
-
-          let methB = envGetMethB emEnv mref
-          let emEnv = envPushTyvars emEnv (Array.append
-                                             (getGenericArgumentsOfType (typB.AsType()))
-                                             (getGenericArgumentsOfMethod methB))
-
-          if not (Array.isEmpty (mdef.Return.CustomAttrs.AsArray())) then
-              let retB = methB.DefineParameterAndLog (0, ParameterAttributes.Retval, null)
-              emitCustomAttrs cenv emEnv (wrapCustomAttr retB.SetCustomAttribute) mdef.Return.CustomAttrs
-
-          // Value parameters
-          let defineParameter (i, attr, name) = methB.DefineParameterAndLog (i+1, attr, name)
-          mdef.Parameters |> List.iteri (fun a b -> emitParameter cenv emEnv defineParameter a b)
-          // Body
-          if not isPInvoke then
-              emitMethodBody cenv modB emEnv methB.GetILGeneratorAndLog mdef.Name mdef.Body
-          let emEnv = envPopTyvars emEnv // case fold later...
-          emitCustomAttrs cenv emEnv methB.SetCustomAttributeAndLog mdef.CustomAttrs
+    | ".cctor"
+    | ".ctor" ->
+        let consB = envGetConsB emEnv mref
+        // Constructors can not have generic parameters
+        assert isNil mdef.GenericParams
+        // Value parameters
+        let defineParameter (i, attr, name) =
+            consB.DefineParameterAndLog(i + 1, attr, name)
+
+        mdef.Parameters |> List.iteri (emitParameter cenv emEnv defineParameter)
+        // Body
+        emitMethodBody cenv modB emEnv consB.GetILGenerator mdef.Name mdef.Body
+        emitCustomAttrs cenv emEnv (wrapCustomAttr consB.SetCustomAttribute) mdef.CustomAttrs
+        ()
+    | _name ->
+
+        let methB = envGetMethB emEnv mref
+
+        let emEnv =
+            envPushTyvars emEnv (Array.append (getGenericArgumentsOfType (typB.AsType())) (getGenericArgumentsOfMethod methB))
+
+        if not (Array.isEmpty (mdef.Return.CustomAttrs.AsArray())) then
+            let retB = methB.DefineParameterAndLog(0, ParameterAttributes.Retval, null)
+            emitCustomAttrs cenv emEnv (wrapCustomAttr retB.SetCustomAttribute) mdef.Return.CustomAttrs
+
+        // Value parameters
+        let defineParameter (i, attr, name) =
+            methB.DefineParameterAndLog(i + 1, attr, name)
+
+        mdef.Parameters
+        |> List.iteri (fun a b -> emitParameter cenv emEnv defineParameter a b)
+        // Body
+        if not isPInvoke then
+            emitMethodBody cenv modB emEnv methB.GetILGeneratorAndLog mdef.Name mdef.Body
+
+        let emEnv = envPopTyvars emEnv // case fold later...
+        emitCustomAttrs cenv emEnv methB.SetCustomAttributeAndLog mdef.CustomAttrs
 
 //----------------------------------------------------------------------------
 // buildFieldPass2
@@ -1607,11 +2127,11 @@ let buildFieldPass2 cenv tref (typB: TypeBuilder) emEnv (fdef: ILFieldDef) =
 
     let attrs = fdef.Attributes
     let fieldT = convType cenv emEnv fdef.FieldType
+
     let fieldB =
         match fdef.Data with
         | Some d -> typB.DefineInitializedData(fdef.Name, d, attrs)
-        | None ->
-        typB.DefineFieldAndLog (fdef.Name, fieldT, attrs)
+        | None -> typB.DefineFieldAndLog(fdef.Name, fieldT, attrs)
 
     // set default value
     let emEnv =
@@ -1619,9 +2139,8 @@ let buildFieldPass2 cenv tref (typB: TypeBuilder) emEnv (fdef: ILFieldDef) =
         | None -> emEnv
         | Some initial ->
             if not fieldT.IsEnum
-                // it is ok to init fields with type = enum that are defined in other assemblies
-                || not fieldT.Assembly.IsDynamic
-            then
+               // it is ok to init fields with type = enum that are defined in other assemblies
+               || not fieldT.Assembly.IsDynamic then
                 fieldB.SetConstant(initial.AsObject())
                 emEnv
             else
@@ -1629,7 +2148,10 @@ let buildFieldPass2 cenv tref (typB: TypeBuilder) emEnv (fdef: ILFieldDef) =
                 // => its underlying type cannot be explicitly specified and will be inferred at the very moment of first field definition
                 // => here we cannot detect if underlying type is already set so as a conservative solution we delay initialization of fields
                 // to the end of pass2 (types and members are already created but method bodies are yet not emitted)
-                { emEnv with delayedFieldInits = (fun() -> fieldB.SetConstant(initial.AsObject())) :: emEnv.delayedFieldInits }
+                { emEnv with
+                    delayedFieldInits = (fun () -> fieldB.SetConstant(initial.AsObject())) :: emEnv.delayedFieldInits
+                }
+
     fdef.Offset |> Option.iter (fun offset -> fieldB.SetOffset offset)
     // custom attributes: done on pass 3 as they may reference attribute constructors generated on
     // pass 2.
@@ -1646,39 +2168,55 @@ let buildFieldPass3 cenv tref (_typB: TypeBuilder) emEnv (fdef: ILFieldDef) =
 //----------------------------------------------------------------------------
 
 let buildPropertyPass2 cenv tref (typB: TypeBuilder) emEnv (prop: ILPropertyDef) =
-    let attrs = flagsIf prop.IsRTSpecialName PropertyAttributes.RTSpecialName |||
-                flagsIf prop.IsSpecialName PropertyAttributes.SpecialName
+    let attrs =
+        flagsIf prop.IsRTSpecialName PropertyAttributes.RTSpecialName
+        ||| flagsIf prop.IsSpecialName PropertyAttributes.SpecialName
+
+    let propB =
+        typB.DefinePropertyAndLog(prop.Name, attrs, convType cenv emEnv prop.PropertyType, convTypesToArray cenv emEnv prop.Args)
 
-    let propB = typB.DefinePropertyAndLog (prop.Name, attrs, convType cenv emEnv prop.PropertyType, convTypesToArray cenv emEnv prop.Args)
+    prop.SetMethod
+    |> Option.iter (fun mref -> propB.SetSetMethod(envGetMethB emEnv mref))
 
-    prop.SetMethod |> Option.iter (fun mref -> propB.SetSetMethod(envGetMethB emEnv mref))
-    prop.GetMethod |> Option.iter (fun mref -> propB.SetGetMethod(envGetMethB emEnv mref))
+    prop.GetMethod
+    |> Option.iter (fun mref -> propB.SetGetMethod(envGetMethB emEnv mref))
     // set default value
     prop.Init |> Option.iter (fun initial -> propB.SetConstant(initial.AsObject()))
     // custom attributes
-    let pref = ILPropertyRef.Create (tref, prop.Name)
+    let pref = ILPropertyRef.Create(tref, prop.Name)
     envBindPropRef emEnv pref propB
 
 let buildPropertyPass3 cenv tref (_typB: TypeBuilder) emEnv (prop: ILPropertyDef) =
-  let pref = ILPropertyRef.Create (tref, prop.Name)
-  let propB = envGetPropB emEnv pref
-  emitCustomAttrs cenv emEnv (wrapCustomAttr propB.SetCustomAttribute) prop.CustomAttrs
+    let pref = ILPropertyRef.Create(tref, prop.Name)
+    let propB = envGetPropB emEnv pref
+    emitCustomAttrs cenv emEnv (wrapCustomAttr propB.SetCustomAttribute) prop.CustomAttrs
 
 //----------------------------------------------------------------------------
 // buildEventPass3
 //----------------------------------------------------------------------------
 
-
 let buildEventPass3 cenv (typB: TypeBuilder) emEnv (eventDef: ILEventDef) =
-    let attrs = flagsIf eventDef.IsSpecialName EventAttributes.SpecialName |||
-                flagsIf eventDef.IsRTSpecialName EventAttributes.RTSpecialName
+    let attrs =
+        flagsIf eventDef.IsSpecialName EventAttributes.SpecialName
+        ||| flagsIf eventDef.IsRTSpecialName EventAttributes.RTSpecialName
+
     assert eventDef.EventType.IsSome
-    let eventB = typB.DefineEventAndLog (eventDef.Name, attrs, convType cenv emEnv eventDef.EventType.Value)
 
-    eventDef.AddMethod |> (fun mref -> eventB.SetAddOnMethod(envGetMethB emEnv mref))
-    eventDef.RemoveMethod |> (fun mref -> eventB.SetRemoveOnMethod(envGetMethB emEnv mref))
-    eventDef.FireMethod |> Option.iter (fun mref -> eventB.SetRaiseMethod(envGetMethB emEnv mref))
-    eventDef.OtherMethods |> List.iter (fun mref -> eventB.AddOtherMethod(envGetMethB emEnv mref))
+    let eventB =
+        typB.DefineEventAndLog(eventDef.Name, attrs, convType cenv emEnv eventDef.EventType.Value)
+
+    eventDef.AddMethod
+    |> (fun mref -> eventB.SetAddOnMethod(envGetMethB emEnv mref))
+
+    eventDef.RemoveMethod
+    |> (fun mref -> eventB.SetRemoveOnMethod(envGetMethB emEnv mref))
+
+    eventDef.FireMethod
+    |> Option.iter (fun mref -> eventB.SetRaiseMethod(envGetMethB emEnv mref))
+
+    eventDef.OtherMethods
+    |> List.iter (fun mref -> eventB.AddOtherMethod(envGetMethB emEnv mref))
+
     emitCustomAttrs cenv emEnv (wrapCustomAttr eventB.SetCustomAttribute) eventDef.CustomAttrs
 
 //----------------------------------------------------------------------------
@@ -1686,7 +2224,9 @@ let buildEventPass3 cenv (typB: TypeBuilder) emEnv (eventDef: ILEventDef) =
 //----------------------------------------------------------------------------
 
 let buildMethodImplsPass3 cenv _tref (typB: TypeBuilder) emEnv (mimpl: ILMethodImplDef) =
-    let bodyMethInfo = convMethodRef cenv emEnv (typB.AsType()) mimpl.OverrideBy.MethodRef // doc: must be MethodBuilder
+    let bodyMethInfo =
+        convMethodRef cenv emEnv (typB.AsType()) mimpl.OverrideBy.MethodRef // doc: must be MethodBuilder
+
     let (OverridesSpec (mref, dtyp)) = mimpl.Overrides
     let declMethTI = convType cenv emEnv dtyp
     let declMethInfo = convMethodRef cenv emEnv declMethTI mref
@@ -1726,27 +2266,38 @@ let typeAttributesOfTypeEncoding x =
     | ILDefaultPInvokeEncoding.Auto -> TypeAttributes.AutoClass
     | ILDefaultPInvokeEncoding.Unicode -> TypeAttributes.UnicodeClass
 
-
 let typeAttributesOfTypeLayout cenv emEnv x =
     let attr x p =
-      if p.Size =None && p.Pack = None then None
-      else
-        match cenv.tryFindSysILTypeRef "System.Runtime.InteropServices.StructLayoutAttribute", cenv.tryFindSysILTypeRef "System.Runtime.InteropServices.LayoutKind" with
-        | Some tref1, Some tref2 ->
-          Some(convCustomAttr cenv emEnv
-                (mkILCustomAttribute
-                  (tref1,
-                   [mkILNonGenericValueTy tref2 ],
-                   [ ILAttribElem.Int32 x ],
-                   (p.Pack |> Option.toList |> List.map (fun x -> ("Pack", cenv.ilg.typ_Int32, false, ILAttribElem.Int32 (int32 x)))) @
-                   (p.Size |> Option.toList |> List.map (fun x -> ("Size", cenv.ilg.typ_Int32, false, ILAttribElem.Int32 x))))))
-        | _ -> None
+        if p.Size = None && p.Pack = None then
+            None
+        else
+            match cenv.tryFindSysILTypeRef "System.Runtime.InteropServices.StructLayoutAttribute",
+                  cenv.tryFindSysILTypeRef "System.Runtime.InteropServices.LayoutKind"
+                with
+            | Some tref1, Some tref2 ->
+                Some(
+                    convCustomAttr
+                        cenv
+                        emEnv
+                        (mkILCustomAttribute (
+                            tref1,
+                            [ mkILNonGenericValueTy tref2 ],
+                            [ ILAttribElem.Int32 x ],
+                            (p.Pack
+                             |> Option.toList
+                             |> List.map (fun x -> ("Pack", cenv.ilg.typ_Int32, false, ILAttribElem.Int32(int32 x))))
+                            @ (p.Size
+                               |> Option.toList
+                               |> List.map (fun x -> ("Size", cenv.ilg.typ_Int32, false, ILAttribElem.Int32 x)))
+                        ))
+                )
+            | _ -> None
+
     match x with
     | ILTypeDefLayout.Auto -> None
     | ILTypeDefLayout.Explicit p -> (attr 0x02 p)
     | ILTypeDefLayout.Sequential p -> (attr 0x00 p)
 
-
 //----------------------------------------------------------------------------
 // buildTypeDefPass1 cenv
 //----------------------------------------------------------------------------
@@ -1767,16 +2318,20 @@ let rec buildTypeDefPass1 cenv emEnv (modB: ModuleBuilder) rootTypeBuilder nesti
     buildGenParamsPass1 emEnv typB.DefineGenericParametersAndLog tdef.GenericParams
     // bind tref -> (typT, typB)
     let tref = mkRefForNestedILTypeDef ILScopeRef.Local (nesting, tdef)
+
     let typT =
         // Q: would it be ok to use typB :> Type ?
         // Maybe not, recall TypeBuilder maybe subtype of Type, but it is not THE Type.
         let nameInModule = tref.QualifiedName
-        modB.GetTypeAndLog (nameInModule, false, false)
+        modB.GetTypeAndLog(nameInModule, false, false)
 
     let emEnv = envBindTypeRef emEnv tref (typT, typB, tdef)
     // recurse on nested types
-    let nesting = nesting @ [tdef]
-    let buildNestedType emEnv tdef = buildTypeTypeDef cenv emEnv modB typB nesting tdef
+    let nesting = nesting @ [ tdef ]
+
+    let buildNestedType emEnv tdef =
+        buildTypeTypeDef cenv emEnv modB typB nesting tdef
+
     let emEnv = Array.fold buildNestedType emEnv (tdef.NestedTypes.AsArray())
     emEnv
 
@@ -1793,12 +2348,13 @@ let rec buildTypeDefPass1b cenv nesting emEnv (tdef: ILTypeDef) =
     let genArgs = getGenericArgumentsOfType (typB.AsType())
     let emEnv = envPushTyvars emEnv genArgs
     // Parent may reference types being defined, so has to come after it's Pass1 creation
-    tdef.Extends |> Option.iter (fun ty -> typB.SetParentAndLog (convType cenv emEnv ty))
+    tdef.Extends
+    |> Option.iter (fun ty -> typB.SetParentAndLog(convType cenv emEnv ty))
     // build constraints on ILGenericParameterDefs. Constraints may reference types being defined,
     // so have to come after all types are created
     buildGenParamsPass1b cenv emEnv genArgs tdef.GenericParams
     let emEnv = envPopTyvars emEnv
-    let nesting = nesting @ [tdef]
+    let nesting = nesting @ [ tdef ]
     List.iter (buildTypeDefPass1b cenv nesting emEnv) (tdef.NestedTypes.AsList())
 
 //----------------------------------------------------------------------------
@@ -1810,15 +2366,25 @@ let rec buildTypeDefPass2 cenv nesting emEnv (tdef: ILTypeDef) =
     let typB = envGetTypB emEnv tref
     let emEnv = envPushTyvars emEnv (getGenericArgumentsOfType (typB.AsType()))
     // add interface impls
-    tdef.Implements |> convTypes cenv emEnv |> List.iter (fun implT -> typB.AddInterfaceImplementationAndLog implT)
+    tdef.Implements
+    |> convTypes cenv emEnv
+    |> List.iter (fun implT -> typB.AddInterfaceImplementationAndLog implT)
     // add methods, properties
-    let emEnv = Array.fold (buildMethodPass2 cenv tref typB) emEnv (tdef.Methods.AsArray())
+    let emEnv =
+        Array.fold (buildMethodPass2 cenv tref typB) emEnv (tdef.Methods.AsArray())
+
     let emEnv = List.fold (buildFieldPass2 cenv tref typB) emEnv (tdef.Fields.AsList())
-    let emEnv = List.fold (buildPropertyPass2 cenv tref typB) emEnv (tdef.Properties.AsList())
+
+    let emEnv =
+        List.fold (buildPropertyPass2 cenv tref typB) emEnv (tdef.Properties.AsList())
+
     let emEnv = envPopTyvars emEnv
     // nested types
-    let nesting = nesting @ [tdef]
-    let emEnv = List.fold (buildTypeDefPass2 cenv nesting) emEnv (tdef.NestedTypes.AsList())
+    let nesting = nesting @ [ tdef ]
+
+    let emEnv =
+        List.fold (buildTypeDefPass2 cenv nesting) emEnv (tdef.NestedTypes.AsList())
+
     emEnv
 
 //----------------------------------------------------------------------------
@@ -1834,13 +2400,19 @@ let rec buildTypeDefPass3 cenv nesting modB emEnv (tdef: ILTypeDef) =
     tdef.Properties.AsList() |> List.iter (buildPropertyPass3 cenv tref typB emEnv)
     tdef.Events.AsList() |> List.iter (buildEventPass3 cenv typB emEnv)
     tdef.Fields.AsList() |> List.iter (buildFieldPass3 cenv tref typB emEnv)
-    let emEnv = List.fold (buildMethodImplsPass3 cenv tref typB) emEnv (tdef.MethodImpls.AsList())
+
+    let emEnv =
+        List.fold (buildMethodImplsPass3 cenv tref typB) emEnv (tdef.MethodImpls.AsList())
+
     tdef.CustomAttrs |> emitCustomAttrs cenv emEnv typB.SetCustomAttributeAndLog
     // custom attributes
     let emEnv = envPopTyvars emEnv
     // nested types
-    let nesting = nesting @ [tdef]
-    let emEnv = List.fold (buildTypeDefPass3 cenv nesting modB) emEnv (tdef.NestedTypes.AsList())
+    let nesting = nesting @ [ tdef ]
+
+    let emEnv =
+        List.fold (buildTypeDefPass3 cenv nesting modB) emEnv (tdef.NestedTypes.AsList())
+
     emEnv
 
 //----------------------------------------------------------------------------
@@ -1883,20 +2455,22 @@ let rec buildTypeDefPass3 cenv nesting modB emEnv (tdef: ILTypeDef) =
 //----------------------------------------------------------------------------
 
 let getEnclosingTypeRefs (tref: ILTypeRef) =
-   match tref.Enclosing with
-   | [] -> []
-   | h :: t -> List.scan (fun tr nm -> mkILTyRefInTyRef (tr, nm)) (mkILTyRef(tref.Scope, h)) t
+    match tref.Enclosing with
+    | [] -> []
+    | h :: t -> List.scan (fun tr nm -> mkILTyRefInTyRef (tr, nm)) (mkILTyRef (tref.Scope, h)) t
 
 []
-type CollectTypes = ValueTypesOnly | All
+type CollectTypes =
+    | ValueTypesOnly
+    | All
 
 // Find all constituent type references
 let rec getTypeRefsInType (allTypes: CollectTypes) ty acc =
     match ty with
     | ILType.Void
     | ILType.TypeVar _ -> acc
-    | ILType.Ptr eltType | ILType.Byref eltType ->
-        getTypeRefsInType allTypes eltType acc
+    | ILType.Ptr eltType
+    | ILType.Byref eltType -> getTypeRefsInType allTypes eltType acc
     | ILType.Array (_, eltType) ->
         match allTypes with
         | CollectTypes.ValueTypesOnly -> acc
@@ -1904,11 +2478,14 @@ let rec getTypeRefsInType (allTypes: CollectTypes) ty acc =
     | ILType.Value tspec ->
         // We use CollectTypes.All because the .NET type loader appears to always eagerly require all types
         // referred to in an instantiation of a generic value type
-        tspec.TypeRef :: List.foldBack (getTypeRefsInType CollectTypes.All) tspec.GenericArgs acc
+        tspec.TypeRef
+        :: List.foldBack (getTypeRefsInType CollectTypes.All) tspec.GenericArgs acc
     | ILType.Boxed tspec ->
         match allTypes with
         | CollectTypes.ValueTypesOnly -> acc
-        | CollectTypes.All -> tspec.TypeRef :: List.foldBack (getTypeRefsInType allTypes) tspec.GenericArgs acc
+        | CollectTypes.All ->
+            tspec.TypeRef
+            :: List.foldBack (getTypeRefsInType allTypes) tspec.GenericArgs acc
     | ILType.FunctionPointer _callsig -> failwith "getTypeRefsInType: fptr"
     | ILType.Modified _ -> failwith "getTypeRefsInType: modified"
 
@@ -1917,35 +2494,49 @@ let verbose2 = false
 let createTypeRef (visited: Dictionary<_, _>, created: Dictionary<_, _>) emEnv tref =
 
     let rec traverseTypeDef (tref: ILTypeRef) (tdef: ILTypeDef) =
-        if verbose2 then dprintf "buildTypeDefPass4: Creating Enclosing Types of %s\n" tdef.Name
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Creating Enclosing Types of %s\n" tdef.Name
+
         for enc in getEnclosingTypeRefs tref do
             traverseTypeRef enc
 
         // WORKAROUND (ProductStudio FSharp 1.0 bug 615): the constraints on generic method parameters
         // are resolved overly eagerly by reflection emit's CreateType.
-        if verbose2 then dprintf "buildTypeDefPass4: Doing type typar constraints of %s\n" tdef.Name
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Doing type typar constraints of %s\n" tdef.Name
+
         for gp in tdef.GenericParams do
             for cx in gp.Constraints do
                 traverseType CollectTypes.All cx
 
-        if verbose2 then dprintf "buildTypeDefPass4: Doing method constraints of %s\n" tdef.Name
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Doing method constraints of %s\n" tdef.Name
+
         for md in tdef.Methods.AsArray() do
             for gp in md.GenericParams do
                 for cx in gp.Constraints do
                     traverseType CollectTypes.All cx
 
         // We absolutely need the exact parent type...
-        if verbose2 then dprintf "buildTypeDefPass4: Creating Super Class Chain of %s\n" tdef.Name
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Creating Super Class Chain of %s\n" tdef.Name
+
         tdef.Extends |> Option.iter (traverseType CollectTypes.All)
 
         // We absolutely need the exact interface types...
-        if verbose2 then dprintf "buildTypeDefPass4: Creating Interface Chain of %s\n" tdef.Name
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Creating Interface Chain of %s\n" tdef.Name
+
         tdef.Implements |> List.iter (traverseType CollectTypes.All)
 
-        if verbose2 then dprintf "buildTypeDefPass4: Do value types in fields of %s\n" tdef.Name
-        tdef.Fields.AsList() |> List.iter (fun fd -> traverseType CollectTypes.ValueTypesOnly fd.FieldType)
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Do value types in fields of %s\n" tdef.Name
+
+        tdef.Fields.AsList()
+        |> List.iter (fun fd -> traverseType CollectTypes.ValueTypesOnly fd.FieldType)
 
-        if verbose2 then dprintf "buildTypeDefPass4: Done with dependencies of %s\n" tdef.Name
+        if verbose2 then
+            dprintf "buildTypeDefPass4: Done with dependencies of %s\n" tdef.Name
 
     and traverseType allTypes ty =
         getTypeRefsInType allTypes ty []
@@ -1954,55 +2545,67 @@ let createTypeRef (visited: Dictionary<_, _>, created: Dictionary<_, _>) emEnv t
 
     and traverseTypeRef tref =
         let typB = envGetTypB emEnv tref
-        if verbose2 then dprintf "- considering reference to type %s\n" typB.FullName
+
+        if verbose2 then
+            dprintf "- considering reference to type %s\n" typB.FullName
 
         // Re-run traverseTypeDef if we've never visited the type.
         if not (visited.ContainsKey tref) then
             visited[tref] <- true
             let tdef = envGetTypeDef emEnv tref
-            if verbose2 then dprintf "- traversing type %s\n" typB.FullName
+
+            if verbose2 then
+                dprintf "- traversing type %s\n" typB.FullName
             // This looks like a special case (perhaps bogus) of the dependency logic above, where
             // we require the type r.Name, though with "nestingToProbe" being the enclosing types of the
             // type being defined.
             let typeCreationHandler =
                 let nestingToProbe = tref.Enclosing
-                ResolveEventHandler(
-                    fun o r ->
-                        let typeName = r.Name
-                        let typeRef = ILTypeRef.Create(ILScopeRef.Local, nestingToProbe, typeName)
-                        match emEnv.emTypMap.TryFind typeRef with
-                        | Some(_, tb, _, _) ->
-                                if not (tb.IsCreated()) then
-                                    tb.CreateTypeAndLog () |> ignore
-                                tb.Assembly
-                        | None -> null
-                )
+
+                ResolveEventHandler(fun o r ->
+                    let typeName = r.Name
+                    let typeRef = ILTypeRef.Create(ILScopeRef.Local, nestingToProbe, typeName)
+
+                    match emEnv.emTypMap.TryFind typeRef with
+                    | Some (_, tb, _, _) ->
+                        if not (tb.IsCreated()) then
+                            tb.CreateTypeAndLog() |> ignore
+
+                        tb.Assembly
+                    | None -> null)
             // For some reason, the handler is installed while running 'traverseTypeDef' but not while defining the type
             // itself.
             AppDomain.CurrentDomain.add_TypeResolve typeCreationHandler
+
             try
                 traverseTypeDef tref tdef
             finally
-               AppDomain.CurrentDomain.remove_TypeResolve typeCreationHandler
+                AppDomain.CurrentDomain.remove_TypeResolve typeCreationHandler
 
             // At this point, we've done everything we can to prepare the type for loading by eagerly forcing the
             // load of other types. Everything else is up to the implementation of System.Reflection.Emit.
             if not (created.ContainsKey tref) then
                 created[tref] <- true
-                if verbose2 then dprintf "- creating type %s\n" typB.FullName
-                typB.CreateTypeAndLog () |> ignore
+
+                if verbose2 then
+                    dprintf "- creating type %s\n" typB.FullName
+
+                typB.CreateTypeAndLog() |> ignore
 
     traverseTypeRef tref
 
 let rec buildTypeDefPass4 (visited, created) nesting emEnv (tdef: ILTypeDef) =
-    if verbose2 then dprintf "buildTypeDefPass4 %s\n" tdef.Name
+    if verbose2 then
+        dprintf "buildTypeDefPass4 %s\n" tdef.Name
+
     let tref = mkRefForNestedILTypeDef ILScopeRef.Local (nesting, tdef)
     createTypeRef (visited, created) emEnv tref
 
-
     // nested types
-    let nesting = nesting @ [tdef]
-    tdef.NestedTypes |> Seq.iter (buildTypeDefPass4 (visited, created) nesting emEnv)
+    let nesting = nesting @ [ tdef ]
+
+    tdef.NestedTypes
+    |> Seq.iter (buildTypeDefPass4 (visited, created) nesting emEnv)
 
 //----------------------------------------------------------------------------
 // buildModuleType
@@ -2013,7 +2616,10 @@ let buildModuleTypePass1 cenv (modB: ModuleBuilder) emEnv (tdef: ILTypeDef) =
 
 let buildModuleTypePass1b cenv emEnv tdef = buildTypeDefPass1b cenv [] emEnv tdef
 let buildModuleTypePass2 cenv emEnv tdef = buildTypeDefPass2 cenv [] emEnv tdef
-let buildModuleTypePass3 cenv modB emEnv tdef = buildTypeDefPass3 cenv [] modB emEnv tdef
+
+let buildModuleTypePass3 cenv modB emEnv tdef =
+    buildTypeDefPass3 cenv [] modB emEnv tdef
+
 let buildModuleTypePass4 visited emEnv tdef = buildTypeDefPass4 visited [] emEnv tdef
 
 //----------------------------------------------------------------------------
@@ -2028,7 +2634,7 @@ let buildModuleFragment cenv emEnv (asmB: AssemblyBuilder) (modB: ModuleBuilder)
     let emEnv = (emEnv, tdefs) ||> List.fold (buildModuleTypePass2 cenv)
 
     for delayedFieldInit in emEnv.delayedFieldInits do
-        delayedFieldInit()
+        delayedFieldInit ()
 
     let emEnv = { emEnv with delayedFieldInits = [] }
 
@@ -2041,16 +2647,19 @@ let buildModuleFragment cenv emEnv (asmB: AssemblyBuilder) (modB: ModuleBuilder)
 #if FX_RESHAPED_REFEMIT
     ignore asmB
 #else
-    m.Resources.AsList() |> List.iter (fun r ->
-        let attribs = (match r.Access with ILResourceAccess.Public -> ResourceAttributes.Public | ILResourceAccess.Private -> ResourceAttributes.Private)
+    m.Resources.AsList()
+    |> List.iter (fun r ->
+        let attribs =
+            (match r.Access with
+             | ILResourceAccess.Public -> ResourceAttributes.Public
+             | ILResourceAccess.Private -> ResourceAttributes.Private)
+
         match r.Location with
         | ILResourceLocation.Local bytes ->
             use stream = bytes.GetByteMemory().AsStream()
-            modB.DefineManifestResourceAndLog (r.Name, stream, attribs)
-        | ILResourceLocation.File (mr, _) ->
-           asmB.AddResourceFileAndLog (r.Name, mr.Name, attribs)
-        | ILResourceLocation.Assembly _ ->
-           failwith "references to resources other assemblies may not be emitted using System.Reflection")
+            modB.DefineManifestResourceAndLog(r.Name, stream, attribs)
+        | ILResourceLocation.File (mr, _) -> asmB.AddResourceFileAndLog(r.Name, mr.Name, attribs)
+        | ILResourceLocation.Assembly _ -> failwith "references to resources other assemblies may not be emitted using System.Reflection")
 #endif
     emEnv
 
@@ -2068,7 +2677,14 @@ let defineDynamicAssemblyAndLog (asmName, flags, asmDir: string) =
         printfn "open System"
         printfn "open System.Reflection"
         printfn "open System.Reflection.Emit"
-        printfn "let assemblyBuilder%d = System.AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName(Name=\"%s\"), enum %d, %A)" (abs <| hash asmB) asmName.Name (LanguagePrimitives.EnumToValue flags) asmDir
+
+        printfn
+            "let assemblyBuilder%d = System.AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName(Name=\"%s\"), enum %d, %A)"
+            (abs <| hash asmB)
+            asmName.Name
+            (LanguagePrimitives.EnumToValue flags)
+            asmDir
+
     asmB
 
 let mkDynamicAssemblyAndModule (assemblyName, optimize, debugInfo: bool, collectible) =
@@ -2076,46 +2692,80 @@ let mkDynamicAssemblyAndModule (assemblyName, optimize, debugInfo: bool, collect
     let asmDir = "."
     let asmName = AssemblyName()
     asmName.Name <- assemblyName
+
     let asmAccess =
-        if collectible then AssemblyBuilderAccess.RunAndCollect
+        if collectible then
+            AssemblyBuilderAccess.RunAndCollect
 #if FX_RESHAPED_REFEMIT
-        else AssemblyBuilderAccess.Run
+        else
+            AssemblyBuilderAccess.Run
 #else
-        else AssemblyBuilderAccess.RunAndSave
+        else
+            AssemblyBuilderAccess.RunAndSave
 #endif
     let asmB = defineDynamicAssemblyAndLog (asmName, asmAccess, asmDir)
+
     if not optimize then
         let daType = typeof
-        let daCtor = daType.GetConstructor [| typeof |]
-        let daBuilder = CustomAttributeBuilder(daCtor, [| System.Diagnostics.DebuggableAttribute.DebuggingModes.DisableOptimizations ||| System.Diagnostics.DebuggableAttribute.DebuggingModes.Default |])
+
+        let daCtor =
+            daType.GetConstructor [| typeof |]
+
+        let daBuilder =
+            CustomAttributeBuilder(
+                daCtor,
+                [|
+                    System.Diagnostics.DebuggableAttribute.DebuggingModes.DisableOptimizations
+                    ||| System.Diagnostics.DebuggableAttribute.DebuggingModes.Default
+                |]
+            )
+
         asmB.SetCustomAttributeAndLog daBuilder
 
-    let modB = asmB.DefineDynamicModuleAndLog (assemblyName, fileName, debugInfo)
+    let modB = asmB.DefineDynamicModuleAndLog(assemblyName, fileName, debugInfo)
     asmB, modB
 
-let EmitDynamicAssemblyFragment (ilg, emitTailcalls, emEnv, asmB: AssemblyBuilder, modB: ModuleBuilder, modul: ILModuleDef, debugInfo: bool, resolveAssemblyRef, tryFindSysILTypeRef) =
-    let cenv = { ilg = ilg ; emitTailcalls=emitTailcalls; generatePdb = debugInfo; resolveAssemblyRef=resolveAssemblyRef; tryFindSysILTypeRef=tryFindSysILTypeRef }
+let EmitDynamicAssemblyFragment
+    (
+        ilg,
+        emitTailcalls,
+        emEnv,
+        asmB: AssemblyBuilder,
+        modB: ModuleBuilder,
+        modul: ILModuleDef,
+        debugInfo: bool,
+        resolveAssemblyRef,
+        tryFindSysILTypeRef
+    ) =
+    let cenv =
+        {
+            ilg = ilg
+            emitTailcalls = emitTailcalls
+            generatePdb = debugInfo
+            resolveAssemblyRef = resolveAssemblyRef
+            tryFindSysILTypeRef = tryFindSysILTypeRef
+        }
 
     let emEnv = buildModuleFragment cenv emEnv asmB modB modul
+
     match modul.Manifest with
     | None -> ()
     | Some mani ->
-       // REVIEW: remainder of manifest
-       emitCustomAttrs cenv emEnv asmB.SetCustomAttributeAndLog mani.CustomAttrs
+        // REVIEW: remainder of manifest
+        emitCustomAttrs cenv emEnv asmB.SetCustomAttributeAndLog mani.CustomAttrs
 
     // invoke entry point methods
     let execEntryPtFun (typB: TypeBuilder, methodName) () =
-      try
-          ignore (typB.InvokeMemberAndLog (methodName, BindingFlags.InvokeMethod ||| BindingFlags.Public ||| BindingFlags.Static, [| |]))
-          None
-      with :? TargetInvocationException as exn ->
-          Some exn.InnerException
+        try
+            ignore (typB.InvokeMemberAndLog(methodName, BindingFlags.InvokeMethod ||| BindingFlags.Public ||| BindingFlags.Static, [||]))
+            None
+        with
+        | :? TargetInvocationException as exn -> Some exn.InnerException
 
     let emEnv, entryPts = envPopEntryPts emEnv
     let execs = List.map execEntryPtFun entryPts
     emEnv, execs
 
-
 //----------------------------------------------------------------------------
 // lookup* allow conversion from AbsIL to their emitted representations
 //----------------------------------------------------------------------------
@@ -2129,4 +2779,3 @@ let EmitDynamicAssemblyFragment (ilg, emitTailcalls, emEnv, asmB: AssemblyBuilde
 // So Type lookup will return the proper Type not TypeBuilder.
 let LookupTypeRef cenv emEnv tref = convCreatedTypeRef cenv emEnv tref
 let LookupType cenv emEnv ty = convCreatedType cenv emEnv ty
-
diff --git a/src/Compiler/AbstractIL/ilsign.fs b/src/Compiler/AbstractIL/ilsign.fs
index 2fa4598aabc..b4eb67b8acb 100644
--- a/src/Compiler/AbstractIL/ilsign.fs
+++ b/src/Compiler/AbstractIL/ilsign.fs
@@ -4,102 +4,118 @@ module internal FSharp.Compiler.AbstractIL.StrongNameSign
 
 #nowarn "9"
 
-    open System
-    open System.IO
-    open System.Collections.Immutable
-    open System.Reflection.PortableExecutable
-    open System.Security.Cryptography
-    open System.Reflection
-    open System.Runtime.InteropServices
-
-    open Internal.Utilities.Library
-    open FSharp.Compiler.IO
-
-    type KeyType =
+open System
+open System.IO
+open System.Collections.Immutable
+open System.Reflection.PortableExecutable
+open System.Security.Cryptography
+open System.Reflection
+open System.Runtime.InteropServices
+
+open Internal.Utilities.Library
+open FSharp.Compiler.IO
+
+type KeyType =
     | Public
     | KeyPair
 
-    let ALG_TYPE_RSA = int (2 <<< 9)
-    let ALG_CLASS_KEY_EXCHANGE = int (5 <<< 13)
-    let ALG_CLASS_SIGNATURE = int (1 <<< 13)
-    let CALG_RSA_KEYX = int (ALG_CLASS_KEY_EXCHANGE ||| ALG_TYPE_RSA)
-    let CALG_RSA_SIGN = int (ALG_CLASS_SIGNATURE ||| ALG_TYPE_RSA)
-
-    let ALG_CLASS_HASH = int (4 <<< 13)
-    let ALG_TYPE_ANY = int 0
-    let CALG_SHA1 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 4)
-    let CALG_SHA_256 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 12)
-    let CALG_SHA_384 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 13)
-    let CALG_SHA_512 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 14)
-
-    let PUBLICKEYBLOB = int 0x6
-    let PRIVATEKEYBLOB = int 0x7
-    let BLOBHEADER_CURRENT_BVERSION = int 0x2
-    let BLOBHEADER_LENGTH = int 20
-    let RSA_PUB_MAGIC = int 0x31415352
-    let RSA_PRIV_MAGIC = int 0x32415352
-
-    let getResourceString (_, str) = str
-    let check _action hresult =
-      if uint32 hresult >= 0x80000000ul then
+let ALG_TYPE_RSA = int (2 <<< 9)
+let ALG_CLASS_KEY_EXCHANGE = int (5 <<< 13)
+let ALG_CLASS_SIGNATURE = int (1 <<< 13)
+let CALG_RSA_KEYX = int (ALG_CLASS_KEY_EXCHANGE ||| ALG_TYPE_RSA)
+let CALG_RSA_SIGN = int (ALG_CLASS_SIGNATURE ||| ALG_TYPE_RSA)
+
+let ALG_CLASS_HASH = int (4 <<< 13)
+let ALG_TYPE_ANY = int 0
+let CALG_SHA1 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 4)
+let CALG_SHA_256 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 12)
+let CALG_SHA_384 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 13)
+let CALG_SHA_512 = int (ALG_CLASS_HASH ||| ALG_TYPE_ANY ||| 14)
+
+let PUBLICKEYBLOB = int 0x6
+let PRIVATEKEYBLOB = int 0x7
+let BLOBHEADER_CURRENT_BVERSION = int 0x2
+let BLOBHEADER_LENGTH = int 20
+let RSA_PUB_MAGIC = int 0x31415352
+let RSA_PRIV_MAGIC = int 0x32415352
+
+let getResourceString (_, str) = str
+
+let check _action hresult =
+    if uint32 hresult >= 0x80000000ul then
         Marshal.ThrowExceptionForHR hresult
 
-    []
-    type ByteArrayUnion =
-        []
-        val UnderlyingArray: byte[]
-
-        []
-        val ImmutableArray: ImmutableArray
-
-        new (immutableArray: ImmutableArray) = { UnderlyingArray = Array.empty; ImmutableArray = immutableArray}
-
-    let getUnderlyingArray (array: ImmutableArray) =ByteArrayUnion(array).UnderlyingArray
-
-    // Compute a hash over the elements of an assembly manifest file that should
-    // remain static (skip checksum, Authenticode signatures and strong name signature blob)
-    let hashAssembly (peReader:PEReader) (hashAlgorithm:IncrementalHash ) =
-        // Hash content of all headers
-        let peHeaders = peReader.PEHeaders
-        let peHeaderOffset = peHeaders.PEHeaderStartOffset
-
-        // Even though some data in OptionalHeader is different for 32 and 64,  this field is the same
-        let checkSumOffset = peHeaderOffset + 0x40;                         // offsetof(IMAGE_OPTIONAL_HEADER, CheckSum)
-        let securityDirectoryEntryOffset, peHeaderSize =
-            match peHeaders.PEHeader.Magic with
-            | PEMagic.PE32 ->       peHeaderOffset + 0x80, 0xE0             // offsetof(IMAGE_OPTIONAL_HEADER32, DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]), sizeof(IMAGE_OPTIONAL_HEADER32)
-            | PEMagic.PE32Plus ->   peHeaderOffset + 0x90,0xF0              // offsetof(IMAGE_OPTIONAL_HEADER64, DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]), sizeof(IMAGE_OPTIONAL_HEADER64)
-            | _ -> raise (BadImageFormatException(getResourceString(FSComp.SR.ilSignInvalidMagicValue())))
-
-        let allHeadersSize = peHeaderOffset + peHeaderSize + int peHeaders.CoffHeader.NumberOfSections * 0x28;      // sizeof(IMAGE_SECTION_HEADER)
-        let allHeaders =
-            let array:byte[] = Array.zeroCreate allHeadersSize
-            peReader.GetEntireImage().GetContent().CopyTo(0, array, 0, allHeadersSize)
-            array
-
-        // Clear checksum and security data directory
-        for i in 0 .. 3 do allHeaders[checkSumOffset + i] <- 0uy
-        for i in 0 .. 7 do allHeaders[securityDirectoryEntryOffset + i] <- 0uy
-        hashAlgorithm.AppendData(allHeaders, 0, allHeadersSize)
-
-        // Hash content of all sections
-        let signatureDirectory = peHeaders.CorHeader.StrongNameSignatureDirectory
-        let signatureStart =
-            match peHeaders.TryGetDirectoryOffset signatureDirectory with
-            | true, value -> value
-            | _           -> raise (BadImageFormatException(getResourceString(FSComp.SR.ilSignBadImageFormat())))
-        let signatureEnd = signatureStart + signatureDirectory.Size
-        let buffer = getUnderlyingArray (peReader.GetEntireImage().GetContent())
-        let sectionHeaders = peHeaders.SectionHeaders
-
-        for i in 0 .. (sectionHeaders.Length - 1) do
-            let section = sectionHeaders[i]
-            let mutable st = section.PointerToRawData
-            let en = st + section.SizeOfRawData
-
-            if st <= signatureStart && signatureStart < en then do
+[]
+type ByteArrayUnion =
+    []
+    val UnderlyingArray: byte[]
+
+    []
+    val ImmutableArray: ImmutableArray
+
+    new(immutableArray: ImmutableArray) =
+        {
+            UnderlyingArray = Array.empty
+            ImmutableArray = immutableArray
+        }
+
+let getUnderlyingArray (array: ImmutableArray) = ByteArrayUnion(array).UnderlyingArray
+
+// Compute a hash over the elements of an assembly manifest file that should
+// remain static (skip checksum, Authenticode signatures and strong name signature blob)
+let hashAssembly (peReader: PEReader) (hashAlgorithm: IncrementalHash) =
+    // Hash content of all headers
+    let peHeaders = peReader.PEHeaders
+    let peHeaderOffset = peHeaders.PEHeaderStartOffset
+
+    // Even though some data in OptionalHeader is different for 32 and 64,  this field is the same
+    let checkSumOffset = peHeaderOffset + 0x40 // offsetof(IMAGE_OPTIONAL_HEADER, CheckSum)
+
+    let securityDirectoryEntryOffset, peHeaderSize =
+        match peHeaders.PEHeader.Magic with
+        | PEMagic.PE32 -> peHeaderOffset + 0x80, 0xE0 // offsetof(IMAGE_OPTIONAL_HEADER32, DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]), sizeof(IMAGE_OPTIONAL_HEADER32)
+        | PEMagic.PE32Plus -> peHeaderOffset + 0x90, 0xF0 // offsetof(IMAGE_OPTIONAL_HEADER64, DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]), sizeof(IMAGE_OPTIONAL_HEADER64)
+        | _ -> raise (BadImageFormatException(getResourceString (FSComp.SR.ilSignInvalidMagicValue ())))
+
+    let allHeadersSize =
+        peHeaderOffset + peHeaderSize + int peHeaders.CoffHeader.NumberOfSections * 0x28 // sizeof(IMAGE_SECTION_HEADER)
+
+    let allHeaders =
+        let array: byte[] = Array.zeroCreate allHeadersSize
+        peReader.GetEntireImage().GetContent().CopyTo(0, array, 0, allHeadersSize)
+        array
+
+    // Clear checksum and security data directory
+    for i in 0..3 do
+        allHeaders[checkSumOffset + i] <- 0uy
+
+    for i in 0..7 do
+        allHeaders[securityDirectoryEntryOffset + i] <- 0uy
+
+    hashAlgorithm.AppendData(allHeaders, 0, allHeadersSize)
+
+    // Hash content of all sections
+    let signatureDirectory = peHeaders.CorHeader.StrongNameSignatureDirectory
+
+    let signatureStart =
+        match peHeaders.TryGetDirectoryOffset signatureDirectory with
+        | true, value -> value
+        | _ -> raise (BadImageFormatException(getResourceString (FSComp.SR.ilSignBadImageFormat ())))
+
+    let signatureEnd = signatureStart + signatureDirectory.Size
+    let buffer = getUnderlyingArray (peReader.GetEntireImage().GetContent())
+    let sectionHeaders = peHeaders.SectionHeaders
+
+    for i in 0 .. (sectionHeaders.Length - 1) do
+        let section = sectionHeaders[i]
+        let mutable st = section.PointerToRawData
+        let en = st + section.SizeOfRawData
+
+        if st <= signatureStart && signatureStart < en then
+            do
                 // The signature should better end within this section as well
-                if not ( (st < signatureEnd) && (signatureEnd <= en)) then raise (BadImageFormatException())
+                if not ((st < signatureEnd) && (signatureEnd <= en)) then
+                    raise (BadImageFormatException())
 
                 // Signature starts within this section - hash everything up to the signature start
                 hashAlgorithm.AppendData(buffer, st, signatureStart - st)
@@ -107,480 +123,615 @@ module internal FSharp.Compiler.AbstractIL.StrongNameSign
                 // Trim what we have written
                 st <- signatureEnd
 
-            hashAlgorithm.AppendData(buffer, st, en - st)
-            ()
-        hashAlgorithm.GetHashAndReset()
-
-    type BlobReader =
-        val mutable _blob:byte[]
-        val mutable _offset:int
-        new (blob:byte[]) = { _blob = blob; _offset = 0; }
-
-        member x.ReadInt32() : int =
-            let offset = x._offset
-            x._offset <- offset + 4
-            int x._blob[offset] ||| (int x._blob[offset + 1] <<< 8) ||| (int x._blob[offset + 2] <<< 16) ||| (int x._blob[offset + 3] <<< 24)
-
-        member x.ReadBigInteger (length:int):byte[] =
-            let arr:byte[] = Array.zeroCreate length
-            Array.Copy(x._blob, x._offset, arr, 0, length)
-            x._offset <- x._offset  + length
-            arr |> Array.rev
-
-    let RSAParamatersFromBlob (blob:byte[]) keyType =
-        let mutable reader = BlobReader blob
-        if reader.ReadInt32() <> 0x00000207 && keyType = KeyType.KeyPair then raise (CryptographicException(getResourceString(FSComp.SR.ilSignPrivateKeyExpected())))
-        reader.ReadInt32() |> ignore                                                                                                      // ALG_ID
-        if reader.ReadInt32() <> RSA_PRIV_MAGIC then raise (CryptographicException(getResourceString(FSComp.SR.ilSignRsaKeyExpected())))  // 'RSA2'
-        let byteLen, halfLen =
-            let bitLen = reader.ReadInt32()
-            match bitLen % 16 with
-            | 0 -> (bitLen / 8, bitLen / 16)
-            | _ -> raise (CryptographicException(getResourceString(FSComp.SR.ilSignInvalidBitLen())))
-        let mutable key = RSAParameters()
-        key.Exponent <- reader.ReadBigInteger 4
-        key.Modulus <- reader.ReadBigInteger byteLen
-        key.P <- reader.ReadBigInteger halfLen
-        key.Q <- reader.ReadBigInteger halfLen
-        key.DP <- reader.ReadBigInteger halfLen
-        key.DQ <- reader.ReadBigInteger halfLen
-        key.InverseQ <- reader.ReadBigInteger halfLen
-        key.D <- reader.ReadBigInteger byteLen
-        key
-
-    let validateRSAField (field: byte[] MaybeNull) expected (name: string) =
-        match field with 
-        | Null -> ()
-        | NonNull field ->
-            if field.Length <> expected then 
-                raise (CryptographicException(String.Format(getResourceString(FSComp.SR.ilSignInvalidRSAParams()), name)))
-
-    let toCLRKeyBlob (rsaParameters: RSAParameters) (algId: int) : byte[] = 
-
-        // The original FCall this helper emulates supports other algId's - however, the only algid we need to support is CALG_RSA_KEYX. We will not port the codepaths dealing with other algid's.
-        if algId <> CALG_RSA_KEYX then raise (CryptographicException(getResourceString(FSComp.SR.ilSignInvalidAlgId())))
-
-        // Validate the RSA structure first.
-        if rsaParameters.Modulus = null then raise (CryptographicException(String.Format(getResourceString(FSComp.SR.ilSignInvalidRSAParams()), "Modulus")))
-        if rsaParameters.Exponent = null || rsaParameters.Exponent.Length > 4 then raise (CryptographicException(String.Format(getResourceString(FSComp.SR.ilSignInvalidRSAParams()), "Exponent")))
-
-        let modulusLength = rsaParameters.Modulus.Length
-        let halfModulusLength = (modulusLength + 1) / 2
-
-        // We assume that if P != null, then so are Q, DP, DQ, InverseQ and D and indicate KeyPair RSA Parameters
-        let isPrivate =
-            if rsaParameters.P <> null then
-                validateRSAField rsaParameters.P halfModulusLength "P"
-                validateRSAField rsaParameters.Q halfModulusLength "Q"
-                validateRSAField rsaParameters.DP halfModulusLength "DP"
-                validateRSAField rsaParameters.InverseQ halfModulusLength "InverseQ"
-                validateRSAField rsaParameters.D halfModulusLength "D"
-                true
-            else false
-
-        let key =
-            use ms = new MemoryStream()
-            use bw = new BinaryWriter(ms)
-
-            bw.Write(int CALG_RSA_SIGN)                                                // CLRHeader.aiKeyAlg
-            bw.Write(int CALG_SHA1)                                                    // CLRHeader.aiHashAlg
-            bw.Write(int (modulusLength + BLOBHEADER_LENGTH))                          // CLRHeader.KeyLength
-
-            // Write out the BLOBHEADER
-            bw.Write(byte (if isPrivate = true then PRIVATEKEYBLOB else PUBLICKEYBLOB))// BLOBHEADER.bType
-            bw.Write(byte BLOBHEADER_CURRENT_BVERSION)                                 // BLOBHEADER.bVersion
-            bw.Write(int16 0)                                                          // BLOBHEADER.wReserved
-            bw.Write(int CALG_RSA_SIGN)                                                // BLOBHEADER.aiKeyAlg
-
-            // Write the RSAPubKey header
-            bw.Write(int (if isPrivate then RSA_PRIV_MAGIC else RSA_PUB_MAGIC))        // RSAPubKey.magic
-            bw.Write(int (modulusLength * 8))                                          // RSAPubKey.bitLen
-
-            let expAsDword =
-                let mutable buffer = int 0
-                for i in 0 .. rsaParameters.Exponent.Length - 1 do
-                   buffer <- (buffer <<< 8) ||| int rsaParameters.Exponent[i]
-                buffer
-
-            bw.Write expAsDword                                                        // RSAPubKey.pubExp
-            bw.Write(rsaParameters.Modulus |> Array.rev)                               // Copy over the modulus for both public and private
-            if isPrivate = true then do
-                bw.Write(rsaParameters.P  |> Array.rev)
-                bw.Write(rsaParameters.Q  |> Array.rev)
+        hashAlgorithm.AppendData(buffer, st, en - st)
+        ()
+
+    hashAlgorithm.GetHashAndReset()
+
+type BlobReader =
+    val mutable _blob: byte[]
+    val mutable _offset: int
+    new(blob: byte[]) = { _blob = blob; _offset = 0 }
+
+    member x.ReadInt32() : int =
+        let offset = x._offset
+        x._offset <- offset + 4
+
+        int x._blob[offset]
+        ||| (int x._blob[offset + 1] <<< 8)
+        ||| (int x._blob[offset + 2] <<< 16)
+        ||| (int x._blob[offset + 3] <<< 24)
+
+    member x.ReadBigInteger(length: int) : byte[] =
+        let arr: byte[] = Array.zeroCreate length
+        Array.Copy(x._blob, x._offset, arr, 0, length)
+        x._offset <- x._offset + length
+        arr |> Array.rev
+
+let RSAParamatersFromBlob (blob: byte[]) keyType =
+    let mutable reader = BlobReader blob
+
+    if reader.ReadInt32() <> 0x00000207 && keyType = KeyType.KeyPair then
+        raise (CryptographicException(getResourceString (FSComp.SR.ilSignPrivateKeyExpected ())))
+
+    reader.ReadInt32() |> ignore // ALG_ID
+
+    if reader.ReadInt32() <> RSA_PRIV_MAGIC then
+        raise (CryptographicException(getResourceString (FSComp.SR.ilSignRsaKeyExpected ()))) // 'RSA2'
+
+    let byteLen, halfLen =
+        let bitLen = reader.ReadInt32()
+
+        match bitLen % 16 with
+        | 0 -> (bitLen / 8, bitLen / 16)
+        | _ -> raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidBitLen ())))
+
+    let mutable key = RSAParameters()
+    key.Exponent <- reader.ReadBigInteger 4
+    key.Modulus <- reader.ReadBigInteger byteLen
+    key.P <- reader.ReadBigInteger halfLen
+    key.Q <- reader.ReadBigInteger halfLen
+    key.DP <- reader.ReadBigInteger halfLen
+    key.DQ <- reader.ReadBigInteger halfLen
+    key.InverseQ <- reader.ReadBigInteger halfLen
+    key.D <- reader.ReadBigInteger byteLen
+    key
+
+let validateRSAField (field: byte[] MaybeNull) expected (name: string) =
+    match field with
+    | Null -> ()
+    | NonNull field ->
+        if field.Length <> expected then
+            raise (CryptographicException(String.Format(getResourceString (FSComp.SR.ilSignInvalidRSAParams ()), name)))
+
+let toCLRKeyBlob (rsaParameters: RSAParameters) (algId: int) : byte[] =
+
+    // The original FCall this helper emulates supports other algId's - however, the only algid we need to support is CALG_RSA_KEYX. We will not port the codepaths dealing with other algid's.
+    if algId <> CALG_RSA_KEYX then
+        raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidAlgId ())))
+
+    // Validate the RSA structure first.
+    if rsaParameters.Modulus = null then
+        raise (CryptographicException(String.Format(getResourceString (FSComp.SR.ilSignInvalidRSAParams ()), "Modulus")))
+
+    if rsaParameters.Exponent = null || rsaParameters.Exponent.Length > 4 then
+        raise (CryptographicException(String.Format(getResourceString (FSComp.SR.ilSignInvalidRSAParams ()), "Exponent")))
+
+    let modulusLength = rsaParameters.Modulus.Length
+    let halfModulusLength = (modulusLength + 1) / 2
+
+    // We assume that if P != null, then so are Q, DP, DQ, InverseQ and D and indicate KeyPair RSA Parameters
+    let isPrivate =
+        if rsaParameters.P <> null then
+            validateRSAField rsaParameters.P halfModulusLength "P"
+            validateRSAField rsaParameters.Q halfModulusLength "Q"
+            validateRSAField rsaParameters.DP halfModulusLength "DP"
+            validateRSAField rsaParameters.InverseQ halfModulusLength "InverseQ"
+            validateRSAField rsaParameters.D halfModulusLength "D"
+            true
+        else
+            false
+
+    let key =
+        use ms = new MemoryStream()
+        use bw = new BinaryWriter(ms)
+
+        bw.Write(int CALG_RSA_SIGN) // CLRHeader.aiKeyAlg
+        bw.Write(int CALG_SHA1) // CLRHeader.aiHashAlg
+        bw.Write(int (modulusLength + BLOBHEADER_LENGTH)) // CLRHeader.KeyLength
+
+        // Write out the BLOBHEADER
+        bw.Write(
+            byte (
+                if isPrivate = true then
+                    PRIVATEKEYBLOB
+                else
+                    PUBLICKEYBLOB
+            )
+        ) // BLOBHEADER.bType
+
+        bw.Write(byte BLOBHEADER_CURRENT_BVERSION) // BLOBHEADER.bVersion
+        bw.Write(int16 0) // BLOBHEADER.wReserved
+        bw.Write(int CALG_RSA_SIGN) // BLOBHEADER.aiKeyAlg
+
+        // Write the RSAPubKey header
+        bw.Write(
+            int (
+                if isPrivate then
+                    RSA_PRIV_MAGIC
+                else
+                    RSA_PUB_MAGIC
+            )
+        ) // RSAPubKey.magic
+
+        bw.Write(int (modulusLength * 8)) // RSAPubKey.bitLen
+
+        let expAsDword =
+            let mutable buffer = int 0
+
+            for i in 0 .. rsaParameters.Exponent.Length - 1 do
+                buffer <- (buffer <<< 8) ||| int rsaParameters.Exponent[i]
+
+            buffer
+
+        bw.Write expAsDword // RSAPubKey.pubExp
+        bw.Write(rsaParameters.Modulus |> Array.rev) // Copy over the modulus for both public and private
+
+        if isPrivate = true then
+            do
+                bw.Write(rsaParameters.P |> Array.rev)
+                bw.Write(rsaParameters.Q |> Array.rev)
                 bw.Write(rsaParameters.DP |> Array.rev)
                 bw.Write(rsaParameters.DQ |> Array.rev)
                 bw.Write(rsaParameters.InverseQ |> Array.rev)
                 bw.Write(rsaParameters.D |> Array.rev)
 
-            bw.Flush()
-            ms.ToArray()
-        key
-
-    let createSignature (hash:byte[]) (keyBlob:byte[]) keyType =
-        use rsa = RSA.Create()
-        rsa.ImportParameters(RSAParamatersFromBlob keyBlob keyType)
-        let signature = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)
-        signature |>Array.rev
-
-    let patchSignature (stream:Stream) (peReader:PEReader) (signature:byte[]) =
-        let peHeaders = peReader.PEHeaders
-        let signatureDirectory = peHeaders.CorHeader.StrongNameSignatureDirectory
-        let signatureOffset =
-            if signatureDirectory.Size > signature.Length then raise (BadImageFormatException(getResourceString(FSComp.SR.ilSignInvalidSignatureSize())))
-            match peHeaders.TryGetDirectoryOffset signatureDirectory with
-            | false, _ -> raise (BadImageFormatException(getResourceString(FSComp.SR.ilSignNoSignatureDirectory())))
-            | true, signatureOffset -> int64 signatureOffset
-        stream.Seek(signatureOffset, SeekOrigin.Begin) |>ignore
-        stream.Write(signature, 0, signature.Length)
-
-        let corHeaderFlagsOffset = int64(peHeaders.CorHeaderStartOffset + 16)           // offsetof(IMAGE_COR20_HEADER, Flags)
-        stream.Seek(corHeaderFlagsOffset, SeekOrigin.Begin) |>ignore
-        stream.WriteByte (byte (peHeaders.CorHeader.Flags ||| CorFlags.StrongNameSigned))
-        ()
+        bw.Flush()
+        ms.ToArray()
+
+    key
+
+let createSignature (hash: byte[]) (keyBlob: byte[]) keyType =
+    use rsa = RSA.Create()
+    rsa.ImportParameters(RSAParamatersFromBlob keyBlob keyType)
+
+    let signature =
+        rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)
+
+    signature |> Array.rev
+
+let patchSignature (stream: Stream) (peReader: PEReader) (signature: byte[]) =
+    let peHeaders = peReader.PEHeaders
+    let signatureDirectory = peHeaders.CorHeader.StrongNameSignatureDirectory
+
+    let signatureOffset =
+        if signatureDirectory.Size > signature.Length then
+            raise (BadImageFormatException(getResourceString (FSComp.SR.ilSignInvalidSignatureSize ())))
 
-    let signStream stream keyBlob =
-        use peReader = new PEReader(stream, PEStreamOptions.PrefetchEntireImage ||| PEStreamOptions.LeaveOpen)
-        let hash =
-            use hashAlgorithm = IncrementalHash.CreateHash(HashAlgorithmName.SHA1)
-            hashAssembly peReader hashAlgorithm
-        let signature = createSignature hash keyBlob KeyType.KeyPair
-        patchSignature stream peReader signature
+        match peHeaders.TryGetDirectoryOffset signatureDirectory with
+        | false, _ -> raise (BadImageFormatException(getResourceString (FSComp.SR.ilSignNoSignatureDirectory ())))
+        | true, signatureOffset -> int64 signatureOffset
 
-    let signFile fileName keyBlob =
-        use fs = FileSystem.OpenFileForWriteShim(fileName, FileMode.Open, FileAccess.ReadWrite)
-        signStream fs keyBlob
+    stream.Seek(signatureOffset, SeekOrigin.Begin) |> ignore
+    stream.Write(signature, 0, signature.Length)
 
-    let signatureSize (pk:byte[]) =
-        if pk.Length < 25 then raise (CryptographicException(getResourceString(FSComp.SR.ilSignInvalidPKBlob())))
-        let mutable reader = BlobReader pk
-        reader.ReadBigInteger 12 |> ignore                                                     // Skip CLRHeader
-        reader.ReadBigInteger 8  |> ignore                                                     // Skip BlobHeader
-        let magic = reader.ReadInt32()                                                           // Read magic
-        if not (magic = RSA_PRIV_MAGIC || magic = RSA_PUB_MAGIC) then                          // RSAPubKey.magic
-            raise (CryptographicException(getResourceString(FSComp.SR.ilSignInvalidPKBlob())))
-        let x = reader.ReadInt32() / 8
-        x
+    let corHeaderFlagsOffset = int64 (peHeaders.CorHeaderStartOffset + 16) // offsetof(IMAGE_COR20_HEADER, Flags)
+    stream.Seek(corHeaderFlagsOffset, SeekOrigin.Begin) |> ignore
+    stream.WriteByte(byte (peHeaders.CorHeader.Flags ||| CorFlags.StrongNameSigned))
+    ()
 
-    // Returns a CLR Format Blob public key
-    let getPublicKeyForKeyPair keyBlob =
-        use rsa = RSA.Create()
-        rsa.ImportParameters(RSAParamatersFromBlob keyBlob KeyType.KeyPair)
-        let rsaParameters = rsa.ExportParameters false
-        toCLRKeyBlob rsaParameters CALG_RSA_KEYX
+let signStream stream keyBlob =
+    use peReader =
+        new PEReader(stream, PEStreamOptions.PrefetchEntireImage ||| PEStreamOptions.LeaveOpen)
 
-    // Key signing
-    type keyContainerName = string
-    type keyPair = byte[]
-    type pubkey = byte[]
-    type pubkeyOptions = byte[] * bool
+    let hash =
+        use hashAlgorithm = IncrementalHash.CreateHash(HashAlgorithmName.SHA1)
+        hashAssembly peReader hashAlgorithm
 
-    let signerOpenPublicKeyFile filePath = FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
+    let signature = createSignature hash keyBlob KeyType.KeyPair
+    patchSignature stream peReader signature
 
-    let signerOpenKeyPairFile filePath = FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
+let signFile fileName keyBlob =
+    use fs =
+        FileSystem.OpenFileForWriteShim(fileName, FileMode.Open, FileAccess.ReadWrite)
 
-    let signerGetPublicKeyForKeyPair (kp: keyPair) : pubkey = getPublicKeyForKeyPair kp
+    signStream fs keyBlob
 
-    let signerGetPublicKeyForKeyContainer (_kcName: keyContainerName) : pubkey =
-        raise (NotImplementedException("signerGetPublicKeyForKeyContainer is not yet implemented"))
+let signatureSize (pk: byte[]) =
+    if pk.Length < 25 then
+        raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidPKBlob ())))
 
-    let signerCloseKeyContainer (_kc: keyContainerName) : unit =
-        raise (NotImplementedException("signerCloseKeyContainer is not yet implemented"))
+    let mutable reader = BlobReader pk
+    reader.ReadBigInteger 12 |> ignore // Skip CLRHeader
+    reader.ReadBigInteger 8 |> ignore // Skip BlobHeader
+    let magic = reader.ReadInt32() // Read magic
 
-    let signerSignatureSize (pk: pubkey) : int = signatureSize pk
+    if not (magic = RSA_PRIV_MAGIC || magic = RSA_PUB_MAGIC) then // RSAPubKey.magic
+        raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidPKBlob ())))
 
-    let signerSignFileWithKeyPair (fileName: string) (kp: keyPair) : unit = signFile fileName kp
+    let x = reader.ReadInt32() / 8
+    x
 
-    let signerSignFileWithKeyContainer (_fileName: string) (_kcName: keyContainerName) : unit =
-        raise (NotImplementedException("signerSignFileWithKeyContainer is not yet implemented"))
+// Returns a CLR Format Blob public key
+let getPublicKeyForKeyPair keyBlob =
+    use rsa = RSA.Create()
+    rsa.ImportParameters(RSAParamatersFromBlob keyBlob KeyType.KeyPair)
+    let rsaParameters = rsa.ExportParameters false
+    toCLRKeyBlob rsaParameters CALG_RSA_KEYX
+
+// Key signing
+type keyContainerName = string
+type keyPair = byte[]
+type pubkey = byte[]
+type pubkeyOptions = byte[] * bool
+
+let signerOpenPublicKeyFile filePath =
+    FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
+
+let signerOpenKeyPairFile filePath =
+    FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
+
+let signerGetPublicKeyForKeyPair (kp: keyPair) : pubkey = getPublicKeyForKeyPair kp
+
+let signerGetPublicKeyForKeyContainer (_kcName: keyContainerName) : pubkey =
+    raise (NotImplementedException("signerGetPublicKeyForKeyContainer is not yet implemented"))
+
+let signerCloseKeyContainer (_kc: keyContainerName) : unit =
+    raise (NotImplementedException("signerCloseKeyContainer is not yet implemented"))
+
+let signerSignatureSize (pk: pubkey) : int = signatureSize pk
+
+let signerSignFileWithKeyPair (fileName: string) (kp: keyPair) : unit = signFile fileName kp
+
+let signerSignFileWithKeyContainer (_fileName: string) (_kcName: keyContainerName) : unit =
+    raise (NotImplementedException("signerSignFileWithKeyContainer is not yet implemented"))
 
 #if !FX_NO_CORHOST_SIGNER
-    open System.Runtime.CompilerServices
-
-    // New mscoree functionality
-    // This type represents methods that we don't currently need, so I'm leaving unimplemented
-    type UnusedCOMMethod = unit -> unit
-    []
-    []
-    type ICLRMetaHost =
-        []
-        abstract GetRuntime:
-            [] version: string *
-            [] interfaceId: System.Guid -> [] System.Object
-
-        // Methods that we don't need are stubbed out for now...
-        abstract GetVersionFromFile: UnusedCOMMethod
-        abstract EnumerateInstalledRuntimes: UnusedCOMMethod
-        abstract EnumerateLoadedRuntimes: UnusedCOMMethod
-        abstract Reserved01: UnusedCOMMethod
-
-    // We don't currently support ComConversionLoss
-    []
-    []
-    type ICLRStrongName =
-        // Methods that we don't need are stubbed out for now...
-        abstract GetHashFromAssemblyFile: UnusedCOMMethod
-        abstract GetHashFromAssemblyFileW: UnusedCOMMethod
-        abstract GetHashFromBlob: UnusedCOMMethod
-        abstract GetHashFromFile: UnusedCOMMethod
-        abstract GetHashFromFileW: UnusedCOMMethod
-        abstract GetHashFromHandle: UnusedCOMMethod
-        abstract StrongNameCompareAssemblies: UnusedCOMMethod
-
-        []
-        abstract StrongNameFreeBuffer: [] pbMemory: nativeint -> unit
-
-        abstract StrongNameGetBlob: UnusedCOMMethod
-        abstract StrongNameGetBlobFromImage: UnusedCOMMethod
-
-        []
-        abstract StrongNameGetPublicKey :
-                [] pwzKeyContainer: string *
-                [] pbKeyBlob: byte[] *
-                [] cbKeyBlob: uint32 *
-                [] ppbPublicKeyBlob: nativeint byref *
-                [] pcbPublicKeyBlob: uint32 byref -> unit
-
-        abstract StrongNameHashSize: UnusedCOMMethod
-
-        []
-        abstract StrongNameKeyDelete: [] pwzKeyContainer: string -> unit
-
-        abstract StrongNameKeyGen: UnusedCOMMethod
-        abstract StrongNameKeyGenEx: UnusedCOMMethod
-        abstract StrongNameKeyInstall: UnusedCOMMethod
-
-        []
-        abstract StrongNameSignatureGeneration :
-                [] pwzFilePath: string *
-                [] pwzKeyContainer: string *
-                [] pbKeyBlob: byte [] *
-                [] cbKeyBlob: uint32 *
-                [] ppbSignatureBlob: nativeint *
-                [] pcbSignatureBlob: uint32 byref -> unit
-
-        abstract StrongNameSignatureGenerationEx: UnusedCOMMethod
-
-        []
-        abstract StrongNameSignatureSize :
-                [] pbPublicKeyBlob: byte[] *
-                [] cbPublicKeyBlob: uint32 *
-                [] pcbSize: uint32 byref -> unit
-
-        abstract StrongNameSignatureVerification: UnusedCOMMethod
-
-        []
-        abstract StrongNameSignatureVerificationEx :
-                [] pwzFilePath: string *
-                [] fForceVerification: bool *
-                [] pfWasVerified: bool byref -> [] bool
-
-        abstract StrongNameSignatureVerificationFromImage: UnusedCOMMethod
-        abstract StrongNameTokenFromAssembly: UnusedCOMMethod
-        abstract StrongNameTokenFromAssemblyEx: UnusedCOMMethod
-        abstract StrongNameTokenFromPublicKey: UnusedCOMMethod
-
-
-    []
-    []
-    type ICLRRuntimeInfo =
-        // REVIEW: Methods that we don't need will be stubbed out for now...
-        abstract GetVersionString: unit -> unit
-        abstract GetRuntimeDirectory: unit -> unit
-        abstract IsLoaded: unit -> unit
-        abstract LoadErrorString: unit -> unit
-        abstract LoadLibrary: unit -> unit
-        abstract GetProcAddress: unit -> unit
-
-        []
-        abstract GetInterface :
-            [] coClassId: System.Guid *
-            [] interfaceId: System.Guid -> []System.Object
-
-    []
-    []
-    let CreateInterface (
-                        ([] _clsidguid: System.Guid),
-                        ([] _guid: System.Guid),
-                        ([] _metaHost :
-                            ICLRMetaHost byref)) : unit = failwith "CreateInterface"
-
-    let legacySignerOpenPublicKeyFile filePath = FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
-
-    let legacySignerOpenKeyPairFile filePath = FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
-
-    let mutable iclrsn: ICLRStrongName option = None
-    let getICLRStrongName () =
-        match iclrsn with
-        | None ->
-            let CLSID_CLRStrongName = System.Guid(0xB79B0ACDu, 0xF5CDus, 0x409bus, 0xB5uy, 0xA5uy, 0xA1uy, 0x62uy, 0x44uy, 0x61uy, 0x0Buy, 0x92uy)
-            let IID_ICLRStrongName = System.Guid(0x9FD93CCFu, 0x3280us, 0x4391us, 0xB3uy, 0xA9uy, 0x96uy, 0xE1uy, 0xCDuy, 0xE7uy, 0x7Cuy, 0x8Duy)
-            let CLSID_CLRMetaHost =  System.Guid(0x9280188Du, 0x0E8Eus, 0x4867us, 0xB3uy, 0x0Cuy, 0x7Fuy, 0xA8uy, 0x38uy, 0x84uy, 0xE8uy, 0xDEuy)
-            let IID_ICLRMetaHost = System.Guid(0xD332DB9Eu, 0xB9B3us, 0x4125us, 0x82uy, 0x07uy, 0xA1uy, 0x48uy, 0x84uy, 0xF5uy, 0x32uy, 0x16uy)
-            let clrRuntimeInfoGuid = System.Guid(0xBD39D1D2u, 0xBA2Fus, 0x486aus, 0x89uy, 0xB0uy, 0xB4uy, 0xB0uy, 0xCBuy, 0x46uy, 0x68uy, 0x91uy)
-
-            let runtimeVer = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()
-            let mutable metaHost = Unchecked.defaultof
-            CreateInterface(CLSID_CLRMetaHost, IID_ICLRMetaHost, &metaHost)
-            if Unchecked.defaultof = metaHost then
-                failwith "Unable to obtain ICLRMetaHost object - check freshness of mscoree.dll"
-            let runtimeInfo = metaHost.GetRuntime(runtimeVer, clrRuntimeInfoGuid) :?> ICLRRuntimeInfo
-            let sn = runtimeInfo.GetInterface(CLSID_CLRStrongName, IID_ICLRStrongName) :?> ICLRStrongName
-            if Unchecked.defaultof = sn then
-                failwith "Unable to obtain ICLRStrongName object"
-            iclrsn <- Some sn
-            sn
-        | Some sn -> sn
-
-    let legacySignerGetPublicKeyForKeyPair kp =
-     if runningOnMono then
+open System.Runtime.CompilerServices
+
+// New mscoree functionality
+// This type represents methods that we don't currently need, so I'm leaving unimplemented
+type UnusedCOMMethod = unit -> unit
+
+[]
+[]
+type ICLRMetaHost =
+    []
+    abstract GetRuntime:
+        [] version: string * [] interfaceId: System.Guid ->
+            [] System.Object
+
+    // Methods that we don't need are stubbed out for now...
+    abstract GetVersionFromFile: UnusedCOMMethod
+    abstract EnumerateInstalledRuntimes: UnusedCOMMethod
+    abstract EnumerateLoadedRuntimes: UnusedCOMMethod
+    abstract Reserved01: UnusedCOMMethod
+
+// We don't currently support ComConversionLoss
+[]
+[]
+type ICLRStrongName =
+    // Methods that we don't need are stubbed out for now...
+    abstract GetHashFromAssemblyFile: UnusedCOMMethod
+    abstract GetHashFromAssemblyFileW: UnusedCOMMethod
+    abstract GetHashFromBlob: UnusedCOMMethod
+    abstract GetHashFromFile: UnusedCOMMethod
+    abstract GetHashFromFileW: UnusedCOMMethod
+    abstract GetHashFromHandle: UnusedCOMMethod
+    abstract StrongNameCompareAssemblies: UnusedCOMMethod
+
+    []
+    abstract StrongNameFreeBuffer: [] pbMemory: nativeint -> unit
+
+    abstract StrongNameGetBlob: UnusedCOMMethod
+    abstract StrongNameGetBlobFromImage: UnusedCOMMethod
+
+    []
+    abstract StrongNameGetPublicKey:
+        [] pwzKeyContainer: string *
+        [] pbKeyBlob: byte[] *
+        [] cbKeyBlob: uint32 *
+        [] ppbPublicKeyBlob: nativeint byref *
+        [] pcbPublicKeyBlob: uint32 byref ->
+            unit
+
+    abstract StrongNameHashSize: UnusedCOMMethod
+
+    []
+    abstract StrongNameKeyDelete: [] pwzKeyContainer: string -> unit
+
+    abstract StrongNameKeyGen: UnusedCOMMethod
+    abstract StrongNameKeyGenEx: UnusedCOMMethod
+    abstract StrongNameKeyInstall: UnusedCOMMethod
+
+    []
+    abstract StrongNameSignatureGeneration:
+        [] pwzFilePath: string *
+        [] pwzKeyContainer: string *
+        [] pbKeyBlob: byte[] *
+        [] cbKeyBlob: uint32 *
+        [] ppbSignatureBlob: nativeint *
+        [] pcbSignatureBlob: uint32 byref ->
+            unit
+
+    abstract StrongNameSignatureGenerationEx: UnusedCOMMethod
+
+    []
+    abstract StrongNameSignatureSize:
+        [] pbPublicKeyBlob: byte[] *
+        [] cbPublicKeyBlob: uint32 *
+        [] pcbSize: uint32 byref ->
+            unit
+
+    abstract StrongNameSignatureVerification: UnusedCOMMethod
+
+    []
+    abstract StrongNameSignatureVerificationEx:
+        [] pwzFilePath: string *
+        [] fForceVerification: bool *
+        [] pfWasVerified: bool byref ->
+            [] bool
+
+    abstract StrongNameSignatureVerificationFromImage: UnusedCOMMethod
+    abstract StrongNameTokenFromAssembly: UnusedCOMMethod
+    abstract StrongNameTokenFromAssemblyEx: UnusedCOMMethod
+    abstract StrongNameTokenFromPublicKey: UnusedCOMMethod
+
+[]
+[]
+type ICLRRuntimeInfo =
+    // REVIEW: Methods that we don't need will be stubbed out for now...
+    abstract GetVersionString: unit -> unit
+    abstract GetRuntimeDirectory: unit -> unit
+    abstract IsLoaded: unit -> unit
+    abstract LoadErrorString: unit -> unit
+    abstract LoadLibrary: unit -> unit
+    abstract GetProcAddress: unit -> unit
+
+    []
+    abstract GetInterface:
+        [] coClassId: System.Guid *
+        [] interfaceId: System.Guid ->
+            [] System.Object
+
+[]
+[]
+let CreateInterface
+    (
+        ([] _clsidguid: System.Guid),
+        ([] _guid: System.Guid),
+        ([] _metaHost: ICLRMetaHost byref)
+    ) : unit =
+    failwith "CreateInterface"
+
+let legacySignerOpenPublicKeyFile filePath =
+    FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
+
+let legacySignerOpenKeyPairFile filePath =
+    FileSystem.OpenFileForReadShim(filePath).ReadAllBytes()
+
+let mutable iclrsn: ICLRStrongName option = None
+
+let getICLRStrongName () =
+    match iclrsn with
+    | None ->
+        let CLSID_CLRStrongName =
+            System.Guid(0xB79B0ACDu, 0xF5CDus, 0x409bus, 0xB5uy, 0xA5uy, 0xA1uy, 0x62uy, 0x44uy, 0x61uy, 0x0Buy, 0x92uy)
+
+        let IID_ICLRStrongName =
+            System.Guid(0x9FD93CCFu, 0x3280us, 0x4391us, 0xB3uy, 0xA9uy, 0x96uy, 0xE1uy, 0xCDuy, 0xE7uy, 0x7Cuy, 0x8Duy)
+
+        let CLSID_CLRMetaHost =
+            System.Guid(0x9280188Du, 0x0E8Eus, 0x4867us, 0xB3uy, 0x0Cuy, 0x7Fuy, 0xA8uy, 0x38uy, 0x84uy, 0xE8uy, 0xDEuy)
+
+        let IID_ICLRMetaHost =
+            System.Guid(0xD332DB9Eu, 0xB9B3us, 0x4125us, 0x82uy, 0x07uy, 0xA1uy, 0x48uy, 0x84uy, 0xF5uy, 0x32uy, 0x16uy)
+
+        let clrRuntimeInfoGuid =
+            System.Guid(0xBD39D1D2u, 0xBA2Fus, 0x486aus, 0x89uy, 0xB0uy, 0xB4uy, 0xB0uy, 0xCBuy, 0x46uy, 0x68uy, 0x91uy)
+
+        let runtimeVer =
+            System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()
+
+        let mutable metaHost = Unchecked.defaultof
+        CreateInterface(CLSID_CLRMetaHost, IID_ICLRMetaHost, &metaHost)
+
+        if Unchecked.defaultof = metaHost then
+            failwith "Unable to obtain ICLRMetaHost object - check freshness of mscoree.dll"
+
+        let runtimeInfo =
+            metaHost.GetRuntime(runtimeVer, clrRuntimeInfoGuid) :?> ICLRRuntimeInfo
+
+        let sn =
+            runtimeInfo.GetInterface(CLSID_CLRStrongName, IID_ICLRStrongName) :?> ICLRStrongName
+
+        if Unchecked.defaultof = sn then
+            failwith "Unable to obtain ICLRStrongName object"
+
+        iclrsn <- Some sn
+        sn
+    | Some sn -> sn
+
+let legacySignerGetPublicKeyForKeyPair kp =
+    if runningOnMono then
         let snt = System.Type.GetType("Mono.Security.StrongName")
         let sn = System.Activator.CreateInstance(snt, [| box kp |])
-        snt.InvokeMember("PublicKey", (BindingFlags.GetProperty ||| BindingFlags.Instance ||| BindingFlags.Public), null, sn, [| |], Globalization.CultureInfo.InvariantCulture) :?> byte[]
-     else
+
+        snt.InvokeMember(
+            "PublicKey",
+            (BindingFlags.GetProperty ||| BindingFlags.Instance ||| BindingFlags.Public),
+            null,
+            sn,
+            [||],
+            Globalization.CultureInfo.InvariantCulture
+        )
+        :?> byte[]
+    else
         let mutable pSize = 0u
-        let mutable pBuffer: nativeint = (nativeint)0
-        let iclrSN = getICLRStrongName()
+        let mutable pBuffer: nativeint = (nativeint) 0
+        let iclrSN = getICLRStrongName ()
+
+        iclrSN.StrongNameGetPublicKey(Unchecked.defaultof, kp, (uint32) kp.Length, &pBuffer, &pSize)
+        |> ignore
 
-        iclrSN.StrongNameGetPublicKey(Unchecked.defaultof, kp, (uint32) kp.Length, &pBuffer, &pSize) |> ignore
-        let mutable keybuffer: byte [] = Bytes.zeroCreate (int pSize)
+        let mutable keybuffer: byte[] = Bytes.zeroCreate (int pSize)
         // Copy the marshalled data over - we'll have to free this ourselves
         Marshal.Copy(pBuffer, keybuffer, 0, int pSize)
         iclrSN.StrongNameFreeBuffer pBuffer |> ignore
         keybuffer
 
-    let legacySignerGetPublicKeyForKeyContainer kc =
+let legacySignerGetPublicKeyForKeyContainer kc =
+    let mutable pSize = 0u
+    let mutable pBuffer: nativeint = (nativeint) 0
+    let iclrSN = getICLRStrongName ()
+
+    iclrSN.StrongNameGetPublicKey(kc, Unchecked.defaultof, 0u, &pBuffer, &pSize)
+    |> ignore
+
+    let mutable keybuffer: byte[] = Bytes.zeroCreate (int pSize)
+    // Copy the marshalled data over - we'll have to free this ourselves later
+    Marshal.Copy(pBuffer, keybuffer, 0, int pSize)
+    iclrSN.StrongNameFreeBuffer pBuffer |> ignore
+    keybuffer
+
+let legacySignerCloseKeyContainer kc =
+    let iclrSN = getICLRStrongName ()
+    iclrSN.StrongNameKeyDelete kc |> ignore
+
+let legacySignerSignatureSize (pk: byte[]) =
+    if runningOnMono then
+        if pk.Length > 32 then
+            pk.Length - 32
+        else
+            128
+    else
         let mutable pSize = 0u
-        let mutable pBuffer: nativeint = (nativeint)0
-        let iclrSN = getICLRStrongName()
-        iclrSN.StrongNameGetPublicKey(kc, Unchecked.defaultof, 0u, &pBuffer, &pSize) |> ignore
-        let mutable keybuffer: byte [] = Bytes.zeroCreate (int pSize)
-        // Copy the marshalled data over - we'll have to free this ourselves later
-        Marshal.Copy(pBuffer, keybuffer, 0, int pSize)
-        iclrSN.StrongNameFreeBuffer pBuffer |> ignore
-        keybuffer
-
-    let legacySignerCloseKeyContainer kc =
-        let iclrSN = getICLRStrongName()
-        iclrSN.StrongNameKeyDelete kc |> ignore
-
-    let legacySignerSignatureSize (pk: byte[]) =
-     if runningOnMono then
-       if pk.Length > 32 then pk.Length - 32 else 128
-     else
-        let mutable pSize =  0u
-        let iclrSN = getICLRStrongName()
+        let iclrSN = getICLRStrongName ()
         iclrSN.StrongNameSignatureSize(pk, uint32 pk.Length, &pSize) |> ignore
         int pSize
 
-    let legacySignerSignFileWithKeyPair fileName kp =
-     if runningOnMono then
+let legacySignerSignFileWithKeyPair fileName kp =
+    if runningOnMono then
         let snt = System.Type.GetType("Mono.Security.StrongName")
         let sn = System.Activator.CreateInstance(snt, [| box kp |])
         let conv (x: obj) = if (unbox x: bool) then 0 else -1
-        snt.InvokeMember("Sign", (BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public), null, sn, [| box fileName |], Globalization.CultureInfo.InvariantCulture) |> conv |> check "Sign"
-        snt.InvokeMember("Verify", (BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public), null, sn, [| box fileName |], Globalization.CultureInfo.InvariantCulture) |> conv |> check "Verify"
-     else
-        let mutable pcb = 0u
-        let mutable ppb = (nativeint)0
-        let mutable ok = false
-        let iclrSN = getICLRStrongName()
-        iclrSN.StrongNameSignatureGeneration(fileName, Unchecked.defaultof, kp, uint32 kp.Length, ppb, &pcb) |> ignore
-        iclrSN.StrongNameSignatureVerificationEx(fileName, true, &ok) |> ignore
 
-    let legacySignerSignFileWithKeyContainer fileName kcName =
+        snt.InvokeMember(
+            "Sign",
+            (BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public),
+            null,
+            sn,
+            [| box fileName |],
+            Globalization.CultureInfo.InvariantCulture
+        )
+        |> conv
+        |> check "Sign"
+
+        snt.InvokeMember(
+            "Verify",
+            (BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public),
+            null,
+            sn,
+            [| box fileName |],
+            Globalization.CultureInfo.InvariantCulture
+        )
+        |> conv
+        |> check "Verify"
+    else
         let mutable pcb = 0u
-        let mutable ppb = (nativeint)0
+        let mutable ppb = (nativeint) 0
         let mutable ok = false
-        let iclrSN = getICLRStrongName()
-        iclrSN.StrongNameSignatureGeneration(fileName, kcName, Unchecked.defaultof, 0u, ppb, &pcb) |> ignore
+        let iclrSN = getICLRStrongName ()
+
+        iclrSN.StrongNameSignatureGeneration(fileName, Unchecked.defaultof, kp, uint32 kp.Length, ppb, &pcb)
+        |> ignore
+
         iclrSN.StrongNameSignatureVerificationEx(fileName, true, &ok) |> ignore
+
+let legacySignerSignFileWithKeyContainer fileName kcName =
+    let mutable pcb = 0u
+    let mutable ppb = (nativeint) 0
+    let mutable ok = false
+    let iclrSN = getICLRStrongName ()
+
+    iclrSN.StrongNameSignatureGeneration(fileName, kcName, Unchecked.defaultof, 0u, ppb, &pcb)
+    |> ignore
+
+    iclrSN.StrongNameSignatureVerificationEx(fileName, true, &ok) |> ignore
 #endif
 
-    let failWithContainerSigningUnsupportedOnThisPlatform() = failwith (FSComp.SR.containerSigningUnsupportedOnThisPlatform() |> snd)
-
-    //---------------------------------------------------------------------
-    // Strong name signing
-    //---------------------------------------------------------------------
-    type ILStrongNameSigner =
-        | PublicKeySigner of pubkey
-        | PublicKeyOptionsSigner of pubkeyOptions
-        | KeyPair of keyPair
-        | KeyContainer of keyContainerName
-
-        static member OpenPublicKeyOptions s p = PublicKeyOptionsSigner((signerOpenPublicKeyFile s), p)
-        static member OpenPublicKey pubkey = PublicKeySigner pubkey
-        static member OpenKeyPairFile s = KeyPair(signerOpenKeyPairFile s)
-        static member OpenKeyContainer s = KeyContainer s
-
-        member s.Close () =
-            match s with
-            | PublicKeySigner _
-            | PublicKeyOptionsSigner _
-            | KeyPair _ -> ()
-            | KeyContainer containerName ->
+let failWithContainerSigningUnsupportedOnThisPlatform () =
+    failwith (FSComp.SR.containerSigningUnsupportedOnThisPlatform () |> snd)
+
+//---------------------------------------------------------------------
+// Strong name signing
+//---------------------------------------------------------------------
+type ILStrongNameSigner =
+    | PublicKeySigner of pubkey
+    | PublicKeyOptionsSigner of pubkeyOptions
+    | KeyPair of keyPair
+    | KeyContainer of keyContainerName
+
+    static member OpenPublicKeyOptions s p =
+        PublicKeyOptionsSigner((signerOpenPublicKeyFile s), p)
+
+    static member OpenPublicKey pubkey = PublicKeySigner pubkey
+    static member OpenKeyPairFile s = KeyPair(signerOpenKeyPairFile s)
+    static member OpenKeyContainer s = KeyContainer s
+
+    member s.Close() =
+        match s with
+        | PublicKeySigner _
+        | PublicKeyOptionsSigner _
+        | KeyPair _ -> ()
+        | KeyContainer containerName ->
 #if !FX_NO_CORHOST_SIGNER
-                legacySignerCloseKeyContainer containerName
+            legacySignerCloseKeyContainer containerName
 #else
-                ignore containerName
-                failWithContainerSigningUnsupportedOnThisPlatform()
+            ignore containerName
+            failWithContainerSigningUnsupportedOnThisPlatform ()
 #endif
-        member s.IsFullySigned =
-            match s with
-            | PublicKeySigner _ -> false
-            | PublicKeyOptionsSigner pko -> let _, usePublicSign = pko
-                                            usePublicSign
-            | KeyPair _ -> true
-            | KeyContainer _ ->
+    member s.IsFullySigned =
+        match s with
+        | PublicKeySigner _ -> false
+        | PublicKeyOptionsSigner pko ->
+            let _, usePublicSign = pko
+            usePublicSign
+        | KeyPair _ -> true
+        | KeyContainer _ ->
 #if !FX_NO_CORHOST_SIGNER
-                true
+            true
 #else
-                failWithContainerSigningUnsupportedOnThisPlatform()
+            failWithContainerSigningUnsupportedOnThisPlatform ()
 #endif
 
-        member s.PublicKey =
-            match s with
-            | PublicKeySigner pk -> pk
-            | PublicKeyOptionsSigner pko -> let pk, _ = pko
-                                            pk
-            | KeyPair kp -> signerGetPublicKeyForKeyPair kp
-            | KeyContainer containerName ->
+    member s.PublicKey =
+        match s with
+        | PublicKeySigner pk -> pk
+        | PublicKeyOptionsSigner pko ->
+            let pk, _ = pko
+            pk
+        | KeyPair kp -> signerGetPublicKeyForKeyPair kp
+        | KeyContainer containerName ->
 #if !FX_NO_CORHOST_SIGNER
-                legacySignerGetPublicKeyForKeyContainer containerName
+            legacySignerGetPublicKeyForKeyContainer containerName
 #else
-                ignore containerName
-                failWithContainerSigningUnsupportedOnThisPlatform()
+            ignore containerName
+            failWithContainerSigningUnsupportedOnThisPlatform ()
 #endif
 
-        member s.SignatureSize =
-            let pkSignatureSize pk =
-                try
-                    signerSignatureSize pk
-                with exn ->
-                    failwith ("A call to StrongNameSignatureSize failed ("+exn.Message+")")
-                    0x80
-
-            match s with
-            | PublicKeySigner pk -> pkSignatureSize pk
-            | PublicKeyOptionsSigner pko -> let pk, _ = pko
-                                            pkSignatureSize pk
-            | KeyPair kp -> pkSignatureSize (signerGetPublicKeyForKeyPair kp)
-            | KeyContainer containerName ->
+    member s.SignatureSize =
+        let pkSignatureSize pk =
+            try
+                signerSignatureSize pk
+            with
+            | exn ->
+                failwith ("A call to StrongNameSignatureSize failed (" + exn.Message + ")")
+                0x80
+
+        match s with
+        | PublicKeySigner pk -> pkSignatureSize pk
+        | PublicKeyOptionsSigner pko ->
+            let pk, _ = pko
+            pkSignatureSize pk
+        | KeyPair kp -> pkSignatureSize (signerGetPublicKeyForKeyPair kp)
+        | KeyContainer containerName ->
 #if !FX_NO_CORHOST_SIGNER
-                pkSignatureSize (legacySignerGetPublicKeyForKeyContainer containerName)
+            pkSignatureSize (legacySignerGetPublicKeyForKeyContainer containerName)
 #else
-                ignore containerName
-                failWithContainerSigningUnsupportedOnThisPlatform()
+            ignore containerName
+            failWithContainerSigningUnsupportedOnThisPlatform ()
 #endif
 
-        member s.SignFile file =
-            match s with
-            | PublicKeySigner _ -> ()
-            | PublicKeyOptionsSigner _ -> ()
-            | KeyPair kp -> signerSignFileWithKeyPair file kp
-            | KeyContainer containerName ->
+    member s.SignFile file =
+        match s with
+        | PublicKeySigner _ -> ()
+        | PublicKeyOptionsSigner _ -> ()
+        | KeyPair kp -> signerSignFileWithKeyPair file kp
+        | KeyContainer containerName ->
 #if !FX_NO_CORHOST_SIGNER
-                legacySignerSignFileWithKeyContainer file containerName
+            legacySignerSignFileWithKeyContainer file containerName
 #else
-                ignore containerName
-                failWithContainerSigningUnsupportedOnThisPlatform()
+            ignore containerName
+            failWithContainerSigningUnsupportedOnThisPlatform ()
 #endif
diff --git a/src/Compiler/AbstractIL/ilsupp.fs b/src/Compiler/AbstractIL/ilsupp.fs
index 83609883eb7..30050580dd5 100644
--- a/src/Compiler/AbstractIL/ilsupp.fs
+++ b/src/Compiler/AbstractIL/ilsupp.fs
@@ -15,8 +15,11 @@ open FSharp.Compiler.IO
 #if FX_NO_CORHOST_SIGNER
 #endif
 
-let DateTime1970Jan01 = DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) (* ECMA Spec (Oct2002), Part II, 24.2.2 PE File Header. *)
-let absilWriteGetTimeStamp () = (DateTime.UtcNow - DateTime1970Jan01).TotalSeconds |> int
+let DateTime1970Jan01 =
+    DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) (* ECMA Spec (Oct2002), Part II, 24.2.2 PE File Header. *)
+
+let absilWriteGetTimeStamp () =
+    (DateTime.UtcNow - DateTime1970Jan01).TotalSeconds |> int
 
 // Force inline, so GetLastWin32Error calls are immediately after interop calls as seen by FxCop under Debug build.
 let inline ignore _x = ()
@@ -25,232 +28,270 @@ let inline ignore _x = ()
 type IStream = System.Runtime.InteropServices.ComTypes.IStream
 
 let check _action hresult =
-  if uint32 hresult >= 0x80000000ul then
-    Marshal.ThrowExceptionForHR hresult
-  //printf "action = %s, hresult = 0x%nx \n" action hresult
+    if uint32 hresult >= 0x80000000ul then
+        Marshal.ThrowExceptionForHR hresult
+//printf "action = %s, hresult = 0x%nx \n" action hresult
 
 let MAX_PATH = 260
 
 let E_FAIL = 0x80004005
 
-let bytesToWord (b0: byte, b1: byte) =
-    int16 b0 ||| (int16 b1 <<< 8)
+let bytesToWord (b0: byte, b1: byte) = int16 b0 ||| (int16 b1 <<< 8)
 
 let bytesToDWord (b0: byte, b1: byte, b2: byte, b3: byte) =
     int b0 ||| (int b1 <<< 8) ||| (int b2 <<< 16) ||| (int b3 <<< 24)
 
 let bytesToQWord (b0: byte, b1: byte, b2: byte, b3: byte, b4: byte, b5: byte, b6: byte, b7: byte) =
-    int64 b0 ||| (int64 b1 <<< 8) ||| (int64 b2 <<< 16) ||| (int64 b3 <<< 24) ||| (int64 b4 <<< 32) ||| (int64 b5 <<< 40) ||| (int64 b6 <<< 48) ||| (int64 b7 <<< 56)
-
-let dwToBytes n = [| byte (n &&& 0xff) ; byte ((n >>> 8) &&& 0xff) ; byte ((n >>> 16) &&& 0xff) ; byte ((n >>> 24) &&& 0xff) |], 4
-let wToBytes (n: int16) = [| byte (n &&& 0xffs) ; byte ((n >>> 8) &&& 0xffs) |], 2
+    int64 b0
+    ||| (int64 b1 <<< 8)
+    ||| (int64 b2 <<< 16)
+    ||| (int64 b3 <<< 24)
+    ||| (int64 b4 <<< 32)
+    ||| (int64 b5 <<< 40)
+    ||| (int64 b6 <<< 48)
+    ||| (int64 b7 <<< 56)
+
+let dwToBytes n =
+    [|
+        byte (n &&& 0xff)
+        byte ((n >>> 8) &&& 0xff)
+        byte ((n >>> 16) &&& 0xff)
+        byte ((n >>> 24) &&& 0xff)
+    |],
+    4
+
+let wToBytes (n: int16) =
+    [| byte (n &&& 0xffs); byte ((n >>> 8) &&& 0xffs) |], 2
 
 // REVIEW: factor these classes under one hierarchy, use reflection for creation from buffer and toBytes()
 // Though, everything I'd like to unify is static - metaclasses?
-type IMAGE_FILE_HEADER (m: int16, secs: int16, tds: int32, ptst: int32, nos: int32, soh: int16, c: int16) =
-        let mutable machine = m
-        let mutable numberOfSections = secs
-        let mutable timeDateStamp = tds
-        let mutable pointerToSymbolTable = ptst
-        let mutable numberOfSymbols = nos
-        let mutable sizeOfOptionalHeader = soh
-        let mutable characteristics = c
-
-        member x.Machine
-            with get() = machine
-            and set value = machine <- value
-
-        member x.NumberOfSections
-            with get() = numberOfSections
-            and set value = numberOfSections <- value
-
-        member x.TimeDateStamp
-            with get() = timeDateStamp
-            and set value = timeDateStamp <- value
-
-        member x.PointerToSymbolTable
-            with get() = pointerToSymbolTable
-            and set value = pointerToSymbolTable <- value
-
-        member x.NumberOfSymbols
-            with get() = numberOfSymbols
-            and set value = numberOfSymbols <- value
-
-        member x.SizeOfOptionalHeader
-            with get() = sizeOfOptionalHeader
-            and set value = sizeOfOptionalHeader <- value
-
-        member x.Characteristics
-            with get() = characteristics
-            and set value = characteristics <- value
-
-        static member Width
-            with get() = 20
-
-        member x.toBytes () =
-            use buf = ByteBuffer.Create IMAGE_FILE_HEADER.Width
-            buf.EmitUInt16 (uint16 machine)
-            buf.EmitUInt16 (uint16 numberOfSections)
-            buf.EmitInt32 timeDateStamp
-            buf.EmitInt32 pointerToSymbolTable
-            buf.EmitInt32 numberOfSymbols
-            buf.EmitUInt16 (uint16 sizeOfOptionalHeader)
-            buf.EmitUInt16 (uint16 characteristics)
-            buf.AsMemory().ToArray()
+type IMAGE_FILE_HEADER(m: int16, secs: int16, tds: int32, ptst: int32, nos: int32, soh: int16, c: int16) =
+    let mutable machine = m
+    let mutable numberOfSections = secs
+    let mutable timeDateStamp = tds
+    let mutable pointerToSymbolTable = ptst
+    let mutable numberOfSymbols = nos
+    let mutable sizeOfOptionalHeader = soh
+    let mutable characteristics = c
+
+    member x.Machine
+        with get () = machine
+        and set value = machine <- value
+
+    member x.NumberOfSections
+        with get () = numberOfSections
+        and set value = numberOfSections <- value
+
+    member x.TimeDateStamp
+        with get () = timeDateStamp
+        and set value = timeDateStamp <- value
+
+    member x.PointerToSymbolTable
+        with get () = pointerToSymbolTable
+        and set value = pointerToSymbolTable <- value
+
+    member x.NumberOfSymbols
+        with get () = numberOfSymbols
+        and set value = numberOfSymbols <- value
+
+    member x.SizeOfOptionalHeader
+        with get () = sizeOfOptionalHeader
+        and set value = sizeOfOptionalHeader <- value
+
+    member x.Characteristics
+        with get () = characteristics
+        and set value = characteristics <- value
+
+    static member Width = 20
+
+    member x.toBytes() =
+        use buf = ByteBuffer.Create IMAGE_FILE_HEADER.Width
+        buf.EmitUInt16(uint16 machine)
+        buf.EmitUInt16(uint16 numberOfSections)
+        buf.EmitInt32 timeDateStamp
+        buf.EmitInt32 pointerToSymbolTable
+        buf.EmitInt32 numberOfSymbols
+        buf.EmitUInt16(uint16 sizeOfOptionalHeader)
+        buf.EmitUInt16(uint16 characteristics)
+        buf.AsMemory().ToArray()
 
 let bytesToIFH (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_FILE_HEADER.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_FILE_HEADER"
-    IMAGE_FILE_HEADER( bytesToWord(buffer[offset], buffer[offset+1]), // Machine
-            bytesToWord(buffer[offset+2], buffer[offset+3]), // NumberOfSections
-            bytesToDWord(buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7]), // TimeDateStamp
-            bytesToDWord(buffer[offset+8], buffer[offset+9], buffer[offset+10], buffer[offset+11]), // PointerToSymbolTable
-            bytesToDWord(buffer[offset+12], buffer[offset+13], buffer[offset+14], buffer[offset+15]), // NumberOfSymbols
-            bytesToWord(buffer[offset+16], buffer[offset+17]), // SizeOfOptionalHeader
-            bytesToWord(buffer[offset+18], buffer[offset+19])) // Characteristics
+
+    IMAGE_FILE_HEADER(
+        bytesToWord (buffer[offset], buffer[offset + 1]), // Machine
+        bytesToWord (buffer[offset + 2], buffer[offset + 3]), // NumberOfSections
+        bytesToDWord (buffer[offset + 4], buffer[offset + 5], buffer[offset + 6], buffer[offset + 7]), // TimeDateStamp
+        bytesToDWord (buffer[offset + 8], buffer[offset + 9], buffer[offset + 10], buffer[offset + 11]), // PointerToSymbolTable
+        bytesToDWord (buffer[offset + 12], buffer[offset + 13], buffer[offset + 14], buffer[offset + 15]), // NumberOfSymbols
+        bytesToWord (buffer[offset + 16], buffer[offset + 17]), // SizeOfOptionalHeader
+        bytesToWord (buffer[offset + 18], buffer[offset + 19])
+    ) // Characteristics
 
 type IMAGE_SECTION_HEADER(n: int64, ai: int32, va: int32, srd: int32, prd: int32, pr: int32, pln: int32, nr: int16, nl: int16, c: int32) =
-        let mutable name = n
-        let mutable addressInfo = ai // PhysicalAddress / VirtualSize
-        let mutable virtualAddress = va
-        let mutable sizeOfRawData = srd
-        let mutable pointerToRawData = prd
-        let mutable pointerToRelocations = pr
-        let mutable pointerToLineNumbers = pln
-        let mutable numberOfRelocations = nr
-        let mutable numberOfLineNumbers = nl
-        let mutable characteristics = c
-
-        member x.Name
-            with get() = name
-            and set value = name <- value
-
-        member x.PhysicalAddress
-            with get() = addressInfo
-            and set value = addressInfo <- value
-
-        member x.VirtualSize
-            with get() = addressInfo
-            and set value = addressInfo <- value
-
-        member x.VirtualAddress
-            with get() = virtualAddress
-            and set value = virtualAddress <- value
-
-        member x.SizeOfRawData
-            with get() = sizeOfRawData
-            and set value = sizeOfRawData <- value
-
-        member x.PointerToRawData
-            with get() = pointerToRawData
-            and set value = pointerToRawData <- value
-
-        member x.PointerToRelocations
-            with get() = pointerToRelocations
-            and set value = pointerToRelocations <- value
-
-        member x.PointerToLineNumbers
-            with get() = pointerToLineNumbers
-            and set value = pointerToLineNumbers <- value
-
-        member x.NumberOfRelocations
-            with get() = numberOfRelocations
-            and set value = numberOfRelocations <- value
-
-        member x.NumberOfLineNumbers
-            with get() = numberOfLineNumbers
-            and set value = numberOfLineNumbers <- value
-
-        member x.Characteristics
-            with get() = characteristics
-            and set value = characteristics <- value
-
-        static member Width
-            with get() = 40
-
-        member x.toBytes () =
-            use buf = ByteBuffer.Create IMAGE_SECTION_HEADER.Width
-            buf.EmitInt64 name
-            buf.EmitInt32 addressInfo
-            buf.EmitInt32 virtualAddress
-            buf.EmitInt32 sizeOfRawData
-            buf.EmitInt32 pointerToRawData
-            buf.EmitInt32 pointerToRelocations
-            buf.EmitInt32 pointerToLineNumbers
-            buf.EmitUInt16 (uint16 numberOfRelocations)
-            buf.EmitUInt16 (uint16 numberOfLineNumbers)
-            buf.EmitInt32 characteristics
-            buf.AsMemory().ToArray()
+    let mutable name = n
+    let mutable addressInfo = ai // PhysicalAddress / VirtualSize
+    let mutable virtualAddress = va
+    let mutable sizeOfRawData = srd
+    let mutable pointerToRawData = prd
+    let mutable pointerToRelocations = pr
+    let mutable pointerToLineNumbers = pln
+    let mutable numberOfRelocations = nr
+    let mutable numberOfLineNumbers = nl
+    let mutable characteristics = c
+
+    member x.Name
+        with get () = name
+        and set value = name <- value
+
+    member x.PhysicalAddress
+        with get () = addressInfo
+        and set value = addressInfo <- value
+
+    member x.VirtualSize
+        with get () = addressInfo
+        and set value = addressInfo <- value
+
+    member x.VirtualAddress
+        with get () = virtualAddress
+        and set value = virtualAddress <- value
+
+    member x.SizeOfRawData
+        with get () = sizeOfRawData
+        and set value = sizeOfRawData <- value
+
+    member x.PointerToRawData
+        with get () = pointerToRawData
+        and set value = pointerToRawData <- value
+
+    member x.PointerToRelocations
+        with get () = pointerToRelocations
+        and set value = pointerToRelocations <- value
 
+    member x.PointerToLineNumbers
+        with get () = pointerToLineNumbers
+        and set value = pointerToLineNumbers <- value
+
+    member x.NumberOfRelocations
+        with get () = numberOfRelocations
+        and set value = numberOfRelocations <- value
+
+    member x.NumberOfLineNumbers
+        with get () = numberOfLineNumbers
+        and set value = numberOfLineNumbers <- value
+
+    member x.Characteristics
+        with get () = characteristics
+        and set value = characteristics <- value
+
+    static member Width = 40
+
+    member x.toBytes() =
+        use buf = ByteBuffer.Create IMAGE_SECTION_HEADER.Width
+        buf.EmitInt64 name
+        buf.EmitInt32 addressInfo
+        buf.EmitInt32 virtualAddress
+        buf.EmitInt32 sizeOfRawData
+        buf.EmitInt32 pointerToRawData
+        buf.EmitInt32 pointerToRelocations
+        buf.EmitInt32 pointerToLineNumbers
+        buf.EmitUInt16(uint16 numberOfRelocations)
+        buf.EmitUInt16(uint16 numberOfLineNumbers)
+        buf.EmitInt32 characteristics
+        buf.AsMemory().ToArray()
 
 let bytesToISH (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_SECTION_HEADER.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_SECTION_HEADER"
-    IMAGE_SECTION_HEADER(bytesToQWord(buffer[offset], buffer[offset+1], buffer[offset+2], buffer[offset+3], buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7]), // Name
-            bytesToDWord(buffer[offset+8], buffer[offset+9], buffer[offset+10], buffer[offset+11]), // AddressInfo
-            bytesToDWord(buffer[offset+12], buffer[offset+13], buffer[offset+14], buffer[offset+15]), // VirtualAddress
-            bytesToDWord(buffer[offset+16], buffer[offset+17], buffer[offset+18], buffer[offset+19]), // SizeOfRawData
-            bytesToDWord(buffer[offset+20], buffer[offset+21], buffer[offset+22], buffer[offset+23]), // PointerToRawData
-            bytesToDWord(buffer[offset+24], buffer[offset+25], buffer[offset+26], buffer[offset+27]), // PointerToRelocations
-            bytesToDWord(buffer[offset+28], buffer[offset+29], buffer[offset+30], buffer[offset+31]), // PointerToLineNumbers
-            bytesToWord(buffer[offset+32], buffer[offset+33]), // NumberOfRelocations
-            bytesToWord(buffer[offset+34], buffer[offset+35]), // NumberOfLineNumbers
-            bytesToDWord(buffer[offset+36], buffer[offset+37], buffer[offset+38], buffer[offset+39])) // Characteristics
+
+    IMAGE_SECTION_HEADER(
+        bytesToQWord (
+            buffer[offset],
+            buffer[offset + 1],
+            buffer[offset + 2],
+            buffer[offset + 3],
+            buffer[offset + 4],
+            buffer[offset + 5],
+            buffer[offset + 6],
+            buffer[offset + 7]
+        ), // Name
+        bytesToDWord (buffer[offset + 8], buffer[offset + 9], buffer[offset + 10], buffer[offset + 11]), // AddressInfo
+        bytesToDWord (buffer[offset + 12], buffer[offset + 13], buffer[offset + 14], buffer[offset + 15]), // VirtualAddress
+        bytesToDWord (buffer[offset + 16], buffer[offset + 17], buffer[offset + 18], buffer[offset + 19]), // SizeOfRawData
+        bytesToDWord (buffer[offset + 20], buffer[offset + 21], buffer[offset + 22], buffer[offset + 23]), // PointerToRawData
+        bytesToDWord (buffer[offset + 24], buffer[offset + 25], buffer[offset + 26], buffer[offset + 27]), // PointerToRelocations
+        bytesToDWord (buffer[offset + 28], buffer[offset + 29], buffer[offset + 30], buffer[offset + 31]), // PointerToLineNumbers
+        bytesToWord (buffer[offset + 32], buffer[offset + 33]), // NumberOfRelocations
+        bytesToWord (buffer[offset + 34], buffer[offset + 35]), // NumberOfLineNumbers
+        bytesToDWord (buffer[offset + 36], buffer[offset + 37], buffer[offset + 38], buffer[offset + 39])
+    ) // Characteristics
 
 type IMAGE_SYMBOL(n: int64, v: int32, sn: int16, t: int16, sc: byte, nas: byte) =
-        let mutable name = n
-        let mutable value = v
-        let mutable sectionNumber = sn
-        let mutable stype = t
-        let mutable storageClass = sc
-        let mutable numberOfAuxSymbols = nas
-
-        member x.Name
-            with get() = name
-            and set v = name <- v
-
-        member x.Value
-            with get() = value
-            and set v = value <- v
-
-        member x.SectionNumber
-            with get() = sectionNumber
-            and set v = sectionNumber <- v
-
-        member x.Type
-            with get() = stype
-            and set v = stype <- v
-
-        member x.StorageClass
-            with get() = storageClass
-            and set v = storageClass <- v
-
-        member x.NumberOfAuxSymbols
-            with get() = numberOfAuxSymbols
-            and set v = numberOfAuxSymbols <- v
-
-        static member Width
-            with get() = 18
-
-        member x.toBytes() =
-            use buf = ByteBuffer.Create IMAGE_SYMBOL.Width
-            buf.EmitInt64 name
-            buf.EmitInt32 value
-            buf.EmitUInt16 (uint16 sectionNumber)
-            buf.EmitUInt16 (uint16 stype)
-            buf.EmitByte storageClass
-            buf.EmitByte numberOfAuxSymbols
-            buf.AsMemory().ToArray()
+    let mutable name = n
+    let mutable value = v
+    let mutable sectionNumber = sn
+    let mutable stype = t
+    let mutable storageClass = sc
+    let mutable numberOfAuxSymbols = nas
+
+    member x.Name
+        with get () = name
+        and set v = name <- v
+
+    member x.Value
+        with get () = value
+        and set v = value <- v
+
+    member x.SectionNumber
+        with get () = sectionNumber
+        and set v = sectionNumber <- v
+
+    member x.Type
+        with get () = stype
+        and set v = stype <- v
+
+    member x.StorageClass
+        with get () = storageClass
+        and set v = storageClass <- v
+
+    member x.NumberOfAuxSymbols
+        with get () = numberOfAuxSymbols
+        and set v = numberOfAuxSymbols <- v
+
+    static member Width = 18
+
+    member x.toBytes() =
+        use buf = ByteBuffer.Create IMAGE_SYMBOL.Width
+        buf.EmitInt64 name
+        buf.EmitInt32 value
+        buf.EmitUInt16(uint16 sectionNumber)
+        buf.EmitUInt16(uint16 stype)
+        buf.EmitByte storageClass
+        buf.EmitByte numberOfAuxSymbols
+        buf.AsMemory().ToArray()
 
 let bytesToIS (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_SYMBOL.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_SYMBOL"
-    IMAGE_SYMBOL( bytesToQWord(buffer[offset], buffer[offset+1], buffer[offset+2], buffer[offset+3], buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7]), // Name
-            bytesToDWord(buffer[offset+8], buffer[offset+9], buffer[offset+10], buffer[offset+11]), // Value
-            bytesToWord(buffer[offset+12], buffer[offset+13]), // SectionNumber
-            bytesToWord(buffer[offset+14], buffer[offset+15]), // Type
-            buffer[offset+16], // StorageClass
-            buffer[offset+17]) // NumberOfAuxSymbols
+
+    IMAGE_SYMBOL(
+        bytesToQWord (
+            buffer[offset],
+            buffer[offset + 1],
+            buffer[offset + 2],
+            buffer[offset + 3],
+            buffer[offset + 4],
+            buffer[offset + 5],
+            buffer[offset + 6],
+            buffer[offset + 7]
+        ), // Name
+        bytesToDWord (buffer[offset + 8], buffer[offset + 9], buffer[offset + 10], buffer[offset + 11]), // Value
+        bytesToWord (buffer[offset + 12], buffer[offset + 13]), // SectionNumber
+        bytesToWord (buffer[offset + 14], buffer[offset + 15]), // Type
+        buffer[offset + 16],
+        buffer[offset + 17]
+    ) // NumberOfAuxSymbols
 
 type IMAGE_RELOCATION(va: int32, sti: int32, t: int16) =
     let mutable virtualAddress = va // Also RelocCount
@@ -258,37 +299,39 @@ type IMAGE_RELOCATION(va: int32, sti: int32, t: int16) =
     let mutable ty = t // type
 
     member x.VirtualAddress
-        with get() = virtualAddress
+        with get () = virtualAddress
         and set v = virtualAddress <- v
 
     member x.RelocCount
-        with get() = virtualAddress
+        with get () = virtualAddress
         and set v = virtualAddress <- v
 
     member x.SymbolTableIndex
-        with get() = symbolTableIndex
+        with get () = symbolTableIndex
         and set v = symbolTableIndex <- v
 
     member x.Type
-        with get() = ty
+        with get () = ty
         and set v = ty <- v
 
-    static member Width
-        with get() = 10
+    static member Width = 10
 
     member x.toBytes() =
         use buf = ByteBuffer.Create IMAGE_RELOCATION.Width
         buf.EmitInt32 virtualAddress
         buf.EmitInt32 symbolTableIndex
-        buf.EmitUInt16 (uint16 ty)
+        buf.EmitUInt16(uint16 ty)
         buf.AsMemory().ToArray()
 
 let bytesToIR (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_RELOCATION.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_RELOCATION"
-    IMAGE_RELOCATION( bytesToDWord(buffer[offset], buffer[offset+1], buffer[offset+2], buffer[offset+3]),
-            bytesToDWord(buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7]),
-            bytesToWord(buffer[offset+8], buffer[offset+9]))
+
+    IMAGE_RELOCATION(
+        bytesToDWord (buffer[offset], buffer[offset + 1], buffer[offset + 2], buffer[offset + 3]),
+        bytesToDWord (buffer[offset + 4], buffer[offset + 5], buffer[offset + 6], buffer[offset + 7]),
+        bytesToWord (buffer[offset + 8], buffer[offset + 9])
+    )
 
 type IMAGE_RESOURCE_DIRECTORY(c: int32, tds: int32, mjv: int16, mnv: int16, nne: int16, nie: int16) =
     let mutable characteristics = c
@@ -299,72 +342,73 @@ type IMAGE_RESOURCE_DIRECTORY(c: int32, tds: int32, mjv: int16, mnv: int16, nne:
     let mutable numberOfIdEntries = nie
 
     member x.Characteristics
-        with get() = characteristics
+        with get () = characteristics
         and set v = characteristics <- v
 
     member x.TimeDateStamp
-        with get() = timeDateStamp
+        with get () = timeDateStamp
         and set v = timeDateStamp <- v
 
     member x.MajorVersion
-        with get() = majorVersion
+        with get () = majorVersion
         and set v = majorVersion <- v
 
     member x.MinorVersion
-        with get() = minorVersion
+        with get () = minorVersion
         and set v = minorVersion <- v
 
     member x.NumberOfNamedEntries
-        with get() = numberOfNamedEntries
+        with get () = numberOfNamedEntries
         and set v = numberOfNamedEntries <- v
 
     member x.NumberOfIdEntries
-        with get() = numberOfIdEntries
+        with get () = numberOfIdEntries
         and set v = numberOfIdEntries <- v
 
     static member Width = 16
 
-    member x.toBytes () =
+    member x.toBytes() =
         use buf = ByteBuffer.Create IMAGE_RESOURCE_DIRECTORY.Width
         buf.EmitInt32 characteristics
         buf.EmitInt32 timeDateStamp
-        buf.EmitUInt16 (uint16 majorVersion)
-        buf.EmitUInt16 (uint16 minorVersion)
-        buf.EmitUInt16 (uint16 numberOfNamedEntries)
-        buf.EmitUInt16 (uint16 numberOfIdEntries)
+        buf.EmitUInt16(uint16 majorVersion)
+        buf.EmitUInt16(uint16 minorVersion)
+        buf.EmitUInt16(uint16 numberOfNamedEntries)
+        buf.EmitUInt16(uint16 numberOfIdEntries)
         buf.AsMemory().ToArray()
 
 let bytesToIRD (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_RESOURCE_DIRECTORY.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_RESOURCE_DIRECTORY"
-    IMAGE_RESOURCE_DIRECTORY( bytesToDWord(buffer[offset], buffer[offset+1], buffer[offset+2], buffer[offset+3]), // Characteristics
-        bytesToDWord(buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7]), // TimeDateStamp
-        bytesToWord(buffer[offset+8], buffer[offset+9]), // MajorVersion
-        bytesToWord(buffer[offset+10], buffer[offset+11]), // MinorVersion
-        bytesToWord(buffer[offset+12], buffer[offset+13]), // NumberOfNamedEntries
-        bytesToWord(buffer[offset+14], buffer[offset+15])) // NumberOfIdEntries
+
+    IMAGE_RESOURCE_DIRECTORY(
+        bytesToDWord (buffer[offset], buffer[offset + 1], buffer[offset + 2], buffer[offset + 3]), // Characteristics
+        bytesToDWord (buffer[offset + 4], buffer[offset + 5], buffer[offset + 6], buffer[offset + 7]), // TimeDateStamp
+        bytesToWord (buffer[offset + 8], buffer[offset + 9]), // MajorVersion
+        bytesToWord (buffer[offset + 10], buffer[offset + 11]), // MinorVersion
+        bytesToWord (buffer[offset + 12], buffer[offset + 13]), // NumberOfNamedEntries
+        bytesToWord (buffer[offset + 14], buffer[offset + 15])
+    ) // NumberOfIdEntries
 
 type IMAGE_RESOURCE_DIRECTORY_ENTRY(n: int32, o: int32) =
     let mutable name = n
     let mutable offset = o
 
     member x.Name
-        with get() = name
+        with get () = name
         and set v = name <- v
 
     member x.OffsetToData
-        with get() = offset
+        with get () = offset
         and set v = offset <- v
 
-    member x.OffsetToDirectory
-        with get() = offset &&& 0x7fffffff
+    member x.OffsetToDirectory = offset &&& 0x7fffffff
 
-    member x.DataIsDirectory
-        with get() = (offset &&& 0x80000000) <> 0
+    member x.DataIsDirectory = (offset &&& 0x80000000) <> 0
 
     static member Width = 8
 
-    member x.toBytes () =
+    member x.toBytes() =
         use buf = ByteBuffer.Create IMAGE_RESOURCE_DIRECTORY_ENTRY.Width
         buf.EmitInt32 name
         buf.EmitInt32 offset
@@ -373,8 +417,11 @@ type IMAGE_RESOURCE_DIRECTORY_ENTRY(n: int32, o: int32) =
 let bytesToIRDE (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_RESOURCE_DIRECTORY_ENTRY.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_RESOURCE_DIRECTORY_ENTRY"
-    IMAGE_RESOURCE_DIRECTORY_ENTRY( bytesToDWord(buffer[offset], buffer[offset+1], buffer[offset+2], buffer[offset+3]), // Name
-        bytesToDWord(buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7])) // Offset
+
+    IMAGE_RESOURCE_DIRECTORY_ENTRY(
+        bytesToDWord (buffer[offset], buffer[offset + 1], buffer[offset + 2], buffer[offset + 3]), // Name
+        bytesToDWord (buffer[offset + 4], buffer[offset + 5], buffer[offset + 6], buffer[offset + 7])
+    ) // Offset
 
 type IMAGE_RESOURCE_DATA_ENTRY(o: int32, s: int32, c: int32, r: int32) =
     let mutable offsetToData = o
@@ -383,16 +430,19 @@ type IMAGE_RESOURCE_DATA_ENTRY(o: int32, s: int32, c: int32, r: int32) =
     let mutable reserved = r
 
     member x.OffsetToData
-        with get() = offsetToData
+        with get () = offsetToData
         and set v = offsetToData <- v
+
     member x.Size
-        with get() = size
+        with get () = size
         and set v = size <- v
+
     member x.CodePage
-        with get() = codePage
+        with get () = codePage
         and set v = codePage <- v
+
     member x.Reserved
-        with get() = reserved
+        with get () = reserved
         and set v = reserved <- v
 
     static member Width = 16
@@ -407,11 +457,13 @@ type IMAGE_RESOURCE_DATA_ENTRY(o: int32, s: int32, c: int32, r: int32) =
 let bytesToIRDataE (buffer: byte[]) (offset: int) =
     if (buffer.Length - offset) < IMAGE_RESOURCE_DATA_ENTRY.Width then
         invalidArg "buffer" "buffer too small to fit an IMAGE_RESOURCE_DATA_ENTRY"
-    IMAGE_RESOURCE_DATA_ENTRY(bytesToDWord(buffer[offset], buffer[offset+1], buffer[offset+2], buffer[offset+3]), // OffsetToData
-        bytesToDWord(buffer[offset+4], buffer[offset+5], buffer[offset+6], buffer[offset+7]), // Size
-        bytesToDWord(buffer[offset+8], buffer[offset+9], buffer[offset+10], buffer[offset+11]), // CodePage
-        bytesToDWord(buffer[offset+12], buffer[offset+13], buffer[offset+14], buffer[offset+15])) // Reserved
 
+    IMAGE_RESOURCE_DATA_ENTRY(
+        bytesToDWord (buffer[offset], buffer[offset + 1], buffer[offset + 2], buffer[offset + 3]), // OffsetToData
+        bytesToDWord (buffer[offset + 4], buffer[offset + 5], buffer[offset + 6], buffer[offset + 7]), // Size
+        bytesToDWord (buffer[offset + 8], buffer[offset + 9], buffer[offset + 10], buffer[offset + 11]), // CodePage
+        bytesToDWord (buffer[offset + 12], buffer[offset + 13], buffer[offset + 14], buffer[offset + 15])
+    ) // Reserved
 
 type ResFormatHeader() =
     let mutable dwDataSize = 0
@@ -425,39 +477,39 @@ type ResFormatHeader() =
     let mutable dwCharacteristics = 0
 
     member x.DataSize
-        with get() = dwDataSize
+        with get () = dwDataSize
         and set v = dwDataSize <- v
 
     member x.HeaderSize
-        with get() = dwHeaderSize
+        with get () = dwHeaderSize
         and set v = dwHeaderSize <- v
 
     member x.TypeID
-        with get() = dwTypeID
+        with get () = dwTypeID
         and set v = dwTypeID <- v
 
     member x.NameID
-        with get() = dwNameID
+        with get () = dwNameID
         and set v = dwNameID <- v
 
     member x.DataVersion
-        with get() = dwDataVersion
+        with get () = dwDataVersion
         and set v = dwDataVersion <- v
 
     member x.MemFlags
-        with get() = wMemFlags
+        with get () = wMemFlags
         and set v = wMemFlags <- v
 
     member x.LangID
-        with get() = wLangID
+        with get () = wLangID
         and set v = wLangID <- v
 
     member x.Version
-        with get() = dwVersion
+        with get () = dwVersion
         and set v = dwVersion <- v
 
     member x.Characteristics
-        with get() = dwCharacteristics
+        with get () = dwCharacteristics
         and set v = dwCharacteristics <- v
 
     static member Width = 32
@@ -469,8 +521,8 @@ type ResFormatHeader() =
         buf.EmitInt32 dwTypeID
         buf.EmitInt32 dwNameID
         buf.EmitInt32 dwDataVersion
-        buf.EmitUInt16 (uint16 wMemFlags)
-        buf.EmitUInt16 (uint16 wLangID)
+        buf.EmitUInt16(uint16 wMemFlags)
+        buf.EmitUInt16(uint16 wLangID)
         buf.EmitInt32 dwVersion
         buf.EmitInt32 dwCharacteristics
         buf.AsMemory().ToArray()
@@ -487,7 +539,10 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
         if (tid &&& 0x80000000) <> 0 then // REVIEW: Are names and types mutually exclusive?  The C++ code didn't seem to think so, but I can't find any documentation
             resHdr.TypeID <- 0
             let mtid = tid &&& 0x7fffffff
-            cType <- bytesToDWord(pbLinkedResource[mtid], pbLinkedResource[mtid+1], pbLinkedResource[mtid+2], pbLinkedResource[mtid+3])
+
+            cType <-
+                bytesToDWord (pbLinkedResource[mtid], pbLinkedResource[mtid + 1], pbLinkedResource[mtid + 2], pbLinkedResource[mtid + 3])
+
             wzType <- Bytes.zeroCreate ((cType + 1) * 2)
             Bytes.blit pbLinkedResource 4 wzType 0 (cType * 2)
         else
@@ -496,7 +551,10 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
         if (nid &&& 0x80000000) <> 0 then
             resHdr.NameID <- 0
             let mnid = nid &&& 0x7fffffff
-            cName <- bytesToDWord(pbLinkedResource[mnid], pbLinkedResource[mnid+1], pbLinkedResource[mnid+2], pbLinkedResource[mnid+3])
+
+            cName <-
+                bytesToDWord (pbLinkedResource[mnid], pbLinkedResource[mnid + 1], pbLinkedResource[mnid + 2], pbLinkedResource[mnid + 3])
+
             wzName <- Bytes.zeroCreate ((cName + 1) * 2)
             Bytes.blit pbLinkedResource 4 wzName 0 (cName * 2)
         else
@@ -519,13 +577,15 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
         //resHdr.HeaderSize <- 32
         if Unchecked.defaultof <> wzType then
             resHdr.HeaderSize <- resHdr.HeaderSize + ((cType + 1) * 2) - 4
+
         if Unchecked.defaultof <> wzName then
             resHdr.HeaderSize <- resHdr.HeaderSize + ((cName + 1) * 2) - 4
 
-        let SaveChunk(p: byte[], sz: int) =
-            if Unchecked.defaultof <>  pUnlinkedResource then
+        let SaveChunk (p: byte[], sz: int) =
+            if Unchecked.defaultof <> pUnlinkedResource then
                 Bytes.blit p 0 pUnlinkedResource (unlinkedResourceOffset + offset) sz
                 unlinkedResourceOffset <- unlinkedResourceOffset + sz
+
             size <- size + sz
 
             ()
@@ -541,6 +601,7 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
             dwFiller <- dwFiller + cType + 1
         else
             SaveChunk(dwToBytes resHdr.TypeID)
+
         if Unchecked.defaultof <> wzName then
             SaveChunk(wzName, ((cName + 1) * 2))
             dwFiller <- dwFiller + cName + 1
@@ -566,28 +627,36 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
         SaveChunk(pbData, dataEntry.Size)
 
         dwFiller <- dataEntry.Size &&& 0x3
+
         if dwFiller <> 0 then
             SaveChunk(bNil, 4 - dwFiller)
 
         size
 
-let linkNativeResources (unlinkedResources: byte[] list)  (rva: int32) =
-   let resources =
-       unlinkedResources
-       |> Seq.map (fun s -> new MemoryStream(s))
-       |> Seq.map (fun s ->
-           let res = CvtResFile.ReadResFile s
-           s.Dispose()
-           res)
-       |> Seq.collect id
-       // See MakeWin32ResourceList https://github.com/dotnet/roslyn/blob/f40b89234db51da1e1153c14af184e618504be41/src/Compilers/Core/Portable/Compilation/Compilation.cs
-       |> Seq.map (fun r ->
-           Win32Resource(data = r.data, codePage = 0u, languageId = uint32 r.LanguageId,
-                               id = int (int16 r.pstringName.Ordinal), name = r.pstringName.theString,
-                               typeId = int (int16 r.pstringType.Ordinal), typeName = r.pstringType.theString))
-   let bb = System.Reflection.Metadata.BlobBuilder()
-   NativeResourceWriter.SerializeWin32Resources(bb, resources, rva)
-   bb.ToArray()
+let linkNativeResources (unlinkedResources: byte[] list) (rva: int32) =
+    let resources =
+        unlinkedResources
+        |> Seq.map (fun s -> new MemoryStream(s))
+        |> Seq.map (fun s ->
+            let res = CvtResFile.ReadResFile s
+            s.Dispose()
+            res)
+        |> Seq.collect id
+        // See MakeWin32ResourceList https://github.com/dotnet/roslyn/blob/f40b89234db51da1e1153c14af184e618504be41/src/Compilers/Core/Portable/Compilation/Compilation.cs
+        |> Seq.map (fun r ->
+            Win32Resource(
+                data = r.data,
+                codePage = 0u,
+                languageId = uint32 r.LanguageId,
+                id = int (int16 r.pstringName.Ordinal),
+                name = r.pstringName.theString,
+                typeId = int (int16 r.pstringType.Ordinal),
+                typeName = r.pstringType.theString
+            ))
+
+    let bb = System.Reflection.Metadata.BlobBuilder()
+    NativeResourceWriter.SerializeWin32Resources(bb, resources, rva)
+    bb.ToArray()
 
 let unlinkResource (ulLinkedResourceBaseRVA: int32) (pbLinkedResource: byte[]) =
     let mutable nResNodes = 0
@@ -621,7 +690,7 @@ let unlinkResource (ulLinkedResourceBaseRVA: int32) (pbLinkedResource: byte[]) =
         else
             nResNodes <- nResNodes + 1
 
-    let pResNodes: ResFormatNode [] = Array.zeroCreate nResNodes
+    let pResNodes: ResFormatNode[] = Array.zeroCreate nResNodes
     nResNodes <- 0
 
     // fill out the entry buffer
@@ -631,6 +700,7 @@ let unlinkResource (ulLinkedResourceBaseRVA: int32) (pbLinkedResource: byte[]) =
         // Need to skip VERSION and RT_MANIFEST resources
         // REVIEW: ideally we shouldn't allocate space for these, or rename properly so we don't get the naming conflict
         let skipResource = (0x10 = dwTypeID) || (0x18 = dwTypeID)
+
         if pirdeType.DataIsDirectory then
             let nameBase = pirdeType.OffsetToDirectory
             let pirdName = bytesToIRD pbLinkedResource nameBase
@@ -654,28 +724,34 @@ let unlinkResource (ulLinkedResourceBaseRVA: int32) (pbLinkedResource: byte[]) =
                         if pirdeLang.DataIsDirectory then
                             // Resource hierarchy exceeds three levels
                             Marshal.ThrowExceptionForHR(E_FAIL)
-                        else
-                            if (not skipResource) then
-                                let rfn = ResFormatNode(dwTypeID, dwNameID, dwLangID, pirdeLang.OffsetToData, pbLinkedResource)
-                                pResNodes[nResNodes] <- rfn
-                                nResNodes <- nResNodes + 1
-                else
-                    if (not skipResource) then
-                        let rfn = ResFormatNode(dwTypeID, dwNameID, 0, pirdeName.OffsetToData, pbLinkedResource)
-                        pResNodes[nResNodes] <- rfn
-                        nResNodes <- nResNodes + 1
-        else
-            if (not skipResource) then
-                let rfn = ResFormatNode(dwTypeID, 0, 0, pirdeType.OffsetToData, pbLinkedResource) // REVIEW: I believe these 0s are what's causing the duplicate res naming problems
-                pResNodes[nResNodes] <- rfn
-                nResNodes <- nResNodes + 1
+                        else if (not skipResource) then
+                            let rfn =
+                                ResFormatNode(dwTypeID, dwNameID, dwLangID, pirdeLang.OffsetToData, pbLinkedResource)
+
+                            pResNodes[nResNodes] <- rfn
+                            nResNodes <- nResNodes + 1
+                else if (not skipResource) then
+                    let rfn =
+                        ResFormatNode(dwTypeID, dwNameID, 0, pirdeName.OffsetToData, pbLinkedResource)
+
+                    pResNodes[nResNodes] <- rfn
+                    nResNodes <- nResNodes + 1
+        else if (not skipResource) then
+            let rfn = ResFormatNode(dwTypeID, 0, 0, pirdeType.OffsetToData, pbLinkedResource) // REVIEW: I believe these 0s are what's causing the duplicate res naming problems
+            pResNodes[nResNodes] <- rfn
+            nResNodes <- nResNodes + 1
 
     // Ok, all tree leaves are in ResFormatNode structs, and nResNodes ptrs are in pResNodes
     let mutable size = 0
+
     if nResNodes <> 0 then
-        size <- size + ResFormatHeader.Width ; // sizeof ResFormatHeader
+        size <- size + ResFormatHeader.Width // sizeof ResFormatHeader
+
         for i = 0 to (nResNodes - 1) do
-            size <- size + pResNodes[i].Save(ulLinkedResourceBaseRVA, pbLinkedResource, Unchecked.defaultof, 0)
+            size <-
+                size
+                + pResNodes[i]
+                    .Save(ulLinkedResourceBaseRVA, pbLinkedResource, Unchecked.defaultof, 0)
 
     let pResBuffer = Bytes.zeroCreate size
 
@@ -684,12 +760,15 @@ let unlinkResource (ulLinkedResourceBaseRVA: int32) (pbLinkedResource: byte[]) =
 
         // Write a dummy header
         let rfh = ResFormatHeader()
-        let rfhBytes = rfh.toBytes()
+        let rfhBytes = rfh.toBytes ()
         Bytes.blit rfhBytes 0 pResBuffer 0 ResFormatHeader.Width
         resBufferOffset <- resBufferOffset + ResFormatHeader.Width
 
         for i = 0 to (nResNodes - 1) do
-            resBufferOffset <- resBufferOffset + pResNodes[i].Save(ulLinkedResourceBaseRVA, pbLinkedResource, pResBuffer, resBufferOffset)
+            resBufferOffset <-
+                resBufferOffset
+                + pResNodes[i]
+                    .Save(ulLinkedResourceBaseRVA, pbLinkedResource, pResBuffer, resBufferOffset)
 
     pResBuffer
 
@@ -697,10 +776,16 @@ let unlinkResource (ulLinkedResourceBaseRVA: int32) (pbLinkedResource: byte[]) =
 // PDB Writing
 
 []
-[]
+[]
 type IMetaDataDispenser =
     abstract DefineScope: unit -> unit // need this here to fill the first vtable slot
-    abstract OpenScope: [] szScope: string * [] dwOpenFlags: Int32 * [] riid: System.Guid byref * [] punk: Object byref -> unit
+
+    abstract OpenScope:
+        [] szScope: string *
+        [] dwOpenFlags: Int32 *
+        [] riid: System.Guid byref *
+        [] punk: Object byref ->
+            unit
 
 []
 []
@@ -715,11 +800,11 @@ type IMetadataEmit =
     abstract Placeholder: unit -> unit
 
 []
-[< Guid("B01FAFEB-C450-3A4D-BEEC-B4CEEC01E006") ; InterfaceType(ComInterfaceType.InterfaceIsIUnknown) >]
-[< ComVisible(false) >]
+[]
+[]
 type ISymUnmanagedDocumentWriter =
-    abstract SetSource: sourceSize: int *  [] source: byte[] -> unit
-    abstract SetCheckSum: algorithmId: System.Guid * checkSumSize: int * []  checkSum: byte [] -> unit
+    abstract SetSource: sourceSize: int * [] source: byte[] -> unit
+    abstract SetCheckSum: algorithmId: System.Guid * checkSumSize: int * [] checkSum: byte[] -> unit
 
 // Struct used to retrieve info on the debug output
 []
@@ -736,138 +821,190 @@ type ImageDebugDirectory =
 []
 []
 type ISymUnmanagedWriter2 =
-    abstract DefineDocument: [] url: string *
-                              language: System.Guid byref *
-                              languageVendor: System.Guid byref *
-                              documentType: System.Guid byref *
-                              [] RetVal: ISymUnmanagedDocumentWriter byref -> unit
+    abstract DefineDocument:
+        [] url: string *
+        language: System.Guid byref *
+        languageVendor: System.Guid byref *
+        documentType: System.Guid byref *
+        [] RetVal: ISymUnmanagedDocumentWriter byref ->
+            unit
+
     abstract SetUserEntryPoint: entryMethod: uint32 -> unit
     abstract OpenMethod: meth: int -> unit
     abstract CloseMethod: unit -> unit
     abstract OpenScope: startOffset: int * pRetVal: int byref -> unit
     abstract CloseScope: endOffset: int -> unit
     abstract SetScopeRange: scopeID: int * startOffset: int * endOffset: int -> unit
-    abstract DefineLocalVariable: [] varName: string *
-                                attributes: int *
-                                cSig: int *
-                                []signature: byte[] *
-                                addressKind: int *
-                                addr1: int *
-                                addr2: int *
-                                addr3: int *
-                                startOffset: int *
-                                endOffset: int -> unit
-    abstract DefineParameter: [] paramName: string *
-                            attributes: int *
-                            sequence: int *
-                            addressKind: int *
-                            addr1: int *
-                            addr2: int *
-                            addr3: int -> unit
-    abstract DefineField: parent: int *
-                      [] fieldName: string *
-                      attributes: int *
-                      cSig: int *
-                      []signature: byte[] *
-                      addressKind: int *
-                      addr1: int *
-                      addr2: int *
-                      addr3: int -> unit
-    abstract DefineGlobalVariable: [] globalVarName: string *
-                                  attributes: int *
-                                  cSig: int *
-                                  []signature: byte[] *
-                                  addressKind: int *
-                                  addr1: int *
-                                  addr2: int *
-                                  addr3: int -> unit
+
+    abstract DefineLocalVariable:
+        [] varName: string *
+        attributes: int *
+        cSig: int *
+        [] signature: byte[] *
+        addressKind: int *
+        addr1: int *
+        addr2: int *
+        addr3: int *
+        startOffset: int *
+        endOffset: int ->
+            unit
+
+    abstract DefineParameter:
+        [] paramName: string *
+        attributes: int *
+        sequence: int *
+        addressKind: int *
+        addr1: int *
+        addr2: int *
+        addr3: int ->
+            unit
+
+    abstract DefineField:
+        parent: int *
+        [] fieldName: string *
+        attributes: int *
+        cSig: int *
+        [] signature: byte[] *
+        addressKind: int *
+        addr1: int *
+        addr2: int *
+        addr3: int ->
+            unit
+
+    abstract DefineGlobalVariable:
+        [] globalVarName: string *
+        attributes: int *
+        cSig: int *
+        [] signature: byte[] *
+        addressKind: int *
+        addr1: int *
+        addr2: int *
+        addr3: int ->
+            unit
+
     abstract Close: unit -> unit
-    abstract SetSymAttribute: parent: int *
-                            [] attName: string *
-                            cData: int *
-                            []data: byte[] -> unit
+
+    abstract SetSymAttribute:
+        parent: int *
+        [] attName: string *
+        cData: int *
+        [] data: byte[] ->
+            unit
+
     abstract OpenNamespace: [] nsname: string -> unit
     abstract CloseNamespace: unit -> unit
     abstract UsingNamespace: [] fullName: string -> unit
-    abstract SetMethodSourceRange: startDoc: ISymUnmanagedDocumentWriter *
-                                  startLine: int *
-                                  startColumn: int *
-                                  endDoc: ISymUnmanagedDocumentWriter *
-                                  endLine: int *
-                                  endColumn: int -> unit
-    abstract Initialize: emitter: nativeint *
-                    [] fileName: string *
-                    stream: IStream *
-                    fullBuild: bool -> unit
-    abstract GetDebugInfo: iDD: ImageDebugDirectory byref *
-                          cData: int *
-                          pcData: int byref *
-                          []data: byte[] -> unit
-    abstract DefineSequencePoints: document: ISymUnmanagedDocumentWriter *
-                                  spCount: int *
-                                  []offsets: int [] *
-                                  []lines: int [] *
-                                  []columns: int [] *
-                                  []endLines: int [] *
-                                  []endColumns: int [] -> unit
+
+    abstract SetMethodSourceRange:
+        startDoc: ISymUnmanagedDocumentWriter *
+        startLine: int *
+        startColumn: int *
+        endDoc: ISymUnmanagedDocumentWriter *
+        endLine: int *
+        endColumn: int ->
+            unit
+
+    abstract Initialize:
+        emitter: nativeint * [] fileName: string * stream: IStream * fullBuild: bool -> unit
+
+    abstract GetDebugInfo:
+        iDD: ImageDebugDirectory byref *
+        cData: int *
+        pcData: int byref *
+        [] data: byte[] ->
+            unit
+
+    abstract DefineSequencePoints:
+        document: ISymUnmanagedDocumentWriter *
+        spCount: int *
+        [] offsets: int[] *
+        [] lines: int[] *
+        [] columns: int[] *
+        [] endLines: int[] *
+        [] endColumns: int[] ->
+            unit
+
     abstract RemapToken: oldToken: int * newToken: int -> unit
-    abstract Initialize2: emitter: nativeint *
-                    [] tempFileName: string *
-                    stream: IStream *
-                    fullBuild: bool *
-                    [] finalFileName: string -> unit
-    abstract DefineConstant: [] constName: string *
-                            value: Object *
-                            cSig: int *
-                            []signature: byte[] -> unit
+
+    abstract Initialize2:
+        emitter: nativeint *
+        [] tempFileName: string *
+        stream: IStream *
+        fullBuild: bool *
+        [] finalFileName: string ->
+            unit
+
+    abstract DefineConstant:
+        [] constName: string *
+        value: Object *
+        cSig: int *
+        [] signature: byte[] ->
+            unit
+
     abstract Abort: unit -> unit
-    abstract DefineLocalVariable2: [] localVarName2: string *
-                                  attributes: int *
-                                  sigToken: int *
-                                  addressKind: int *
-                                  addr1: int *
-                                  addr2: int *
-                                  addr3: int *
-                                  startOffset: int *
-                                  endOffset: int -> unit
-    abstract DefineGlobalVariable2: [] globalVarName2: string *
-                                    attributes: int *
-                                    sigToken: int *
-                                    addressKind: int *
-                                    addr1: int *
-                                    addr2: int *
-                                    addr3: int -> unit
-    abstract DefineConstant2: [] constantName2: string *
-                              value: Object *
-                              sigToken: int -> unit
-    abstract OpenMethod2: method2: int *
-                          isect: int *
-                          offset: int -> unit
+
+    abstract DefineLocalVariable2:
+        [] localVarName2: string *
+        attributes: int *
+        sigToken: int *
+        addressKind: int *
+        addr1: int *
+        addr2: int *
+        addr3: int *
+        startOffset: int *
+        endOffset: int ->
+            unit
+
+    abstract DefineGlobalVariable2:
+        [] globalVarName2: string *
+        attributes: int *
+        sigToken: int *
+        addressKind: int *
+        addr1: int *
+        addr2: int *
+        addr3: int ->
+            unit
+
+    abstract DefineConstant2: [] constantName2: string * value: Object * sigToken: int -> unit
+    abstract OpenMethod2: method2: int * isect: int * offset: int -> unit
 
 type PdbWriter = { symWriter: ISymUnmanagedWriter2 }
-type PdbDocumentWriter = { symDocWriter: ISymUnmanagedDocumentWriter }  (* pointer to pDocumentWriter COM object *)
+
+type PdbDocumentWriter =
+    {
+        symDocWriter: ISymUnmanagedDocumentWriter
+    } (* pointer to pDocumentWriter COM object *)
+
 type idd =
-    { iddCharacteristics: int32
-      iddMajorVersion: int32; (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
-      iddMinorVersion: int32; (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
-      iddType: int32
-      iddData: byte[] }
+    {
+        iddCharacteristics: int32
+        iddMajorVersion: int32 (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
+        iddMinorVersion: int32 (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
+        iddType: int32
+        iddData: byte[]
+    }
 #endif
 
 #if !FX_NO_PDB_WRITER
 let pdbInitialize (binaryName: string) (pdbName: string) =
     // collect necessary COM types
-    let CorMetaDataDispenser = System.Type.GetTypeFromProgID("CLRMetaData.CorMetaDataDispenser")
+    let CorMetaDataDispenser =
+        System.Type.GetTypeFromProgID("CLRMetaData.CorMetaDataDispenser")
 
     // get the importer pointer
-    let mdd = System.Activator.CreateInstance(CorMetaDataDispenser) :?> IMetaDataDispenser
+    let mdd =
+        System.Activator.CreateInstance(CorMetaDataDispenser) :?> IMetaDataDispenser
+
     let mutable IID_IMetaDataEmit = new Guid("BA3FEE4C-ECB9-4E41-83B7-183FA41CD859")
     let mutable o = Object()
     mdd.OpenScope(binaryName, 0x1, &IID_IMetaDataEmit, &o) // 0x1 = ofWrite
     let emitterPtr = Marshal.GetComInterfaceForObject(o, typeof)
+
     let writer =
         try
-            let writer = Activator.CreateInstance(System.Type.GetTypeFromProgID("CorSymWriter_SxS")) :?> ISymUnmanagedWriter2
+            let writer =
+                Activator.CreateInstance(System.Type.GetTypeFromProgID("CorSymWriter_SxS")) :?> ISymUnmanagedWriter2
+
             writer.Initialize(emitterPtr, pdbName, Unchecked.defaultof, true)
             writer
         finally
@@ -877,10 +1014,8 @@ let pdbInitialize (binaryName: string) (pdbName: string) =
 
     { symWriter = writer }
 
-
-let pdbCloseDocument(documentWriter: PdbDocumentWriter) =
-    Marshal.ReleaseComObject (documentWriter.symDocWriter)
-    |> ignore
+let pdbCloseDocument (documentWriter: PdbDocumentWriter) =
+    Marshal.ReleaseComObject(documentWriter.symDocWriter) |> ignore
 
 let pdbClose (writer: PdbWriter) dllFilename pdbFilename =
     writer.symWriter.Close()
@@ -896,17 +1031,20 @@ let pdbClose (writer: PdbWriter) dllFilename pdbFilename =
     let rc = Marshal.ReleaseComObject(writer.symWriter)
 
     for i = 0 to (rc - 1) do
-      Marshal.ReleaseComObject(writer.symWriter) |> ignore
+        Marshal.ReleaseComObject(writer.symWriter) |> ignore
 
     let isLocked fileName =
         try
-            use _holder = FileSystem.OpenFileForWriteShim(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
+            use _holder =
+                FileSystem.OpenFileForWriteShim(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
+
             false
         with
         | _ -> true
 
     let mutable attempts = 0
-    while (isLocked dllFilename || isLocked pdbFilename)  && attempts < 3 do
+
+    while (isLocked dllFilename || isLocked pdbFilename) && attempts < 3 do
         // Need to induce two full collections for finalizers to run
         System.GC.Collect()
         System.GC.Collect()
@@ -914,11 +1052,13 @@ let pdbClose (writer: PdbWriter) dllFilename pdbFilename =
         attempts <- attempts + 1
 
 let pdbSetUserEntryPoint (writer: PdbWriter) (entryMethodToken: int32) =
-    writer.symWriter.SetUserEntryPoint((uint32)entryMethodToken)
+    writer.symWriter.SetUserEntryPoint((uint32) entryMethodToken)
 
 // Document checksum algorithms
 
-let guidSourceHashMD5 = System.Guid(0x406ea660u, 0x64cfus, 0x4c82us, 0xb6uy, 0xf0uy, 0x42uy, 0xd4uy, 0x81uy, 0x72uy, 0xa7uy, 0x99uy) //406ea660-64cf-4c82-b6f0-42d48172a799
+let guidSourceHashMD5 =
+    System.Guid(0x406ea660u, 0x64cfus, 0x4c82us, 0xb6uy, 0xf0uy, 0x42uy, 0xd4uy, 0x81uy, 0x72uy, 0xa7uy, 0x99uy) //406ea660-64cf-4c82-b6f0-42d48172a799
+
 let hashSizeOfMD5 = 16
 
 // If the FIPS algorithm policy is enabled on the computer (e.g., for US government employees and contractors)
@@ -929,42 +1069,66 @@ let internal setCheckSum (url: string, writer: ISymUnmanagedDocumentWriter) =
         use file = FileSystem.OpenFileForReadShim(url)
         use md5 = System.Security.Cryptography.MD5.Create()
         let checkSum = md5.ComputeHash file
+
         if (checkSum.Length = hashSizeOfMD5) then
-            writer.SetCheckSum (guidSourceHashMD5, hashSizeOfMD5, checkSum)
-    with _ -> ()
+            writer.SetCheckSum(guidSourceHashMD5, hashSizeOfMD5, checkSum)
+    with
+    | _ -> ()
 
 let pdbDefineDocument (writer: PdbWriter) (url: string) =
     //3F5162F8-07C6-11D3-9053-00C04FA302A1
     //let mutable corSymLanguageTypeCSharp = System.Guid(0x3F5162F8u, 0x07C6us, 0x11D3us, 0x90uy, 0x53uy, 0x00uy, 0xC0uy, 0x4Fuy, 0xA3uy, 0x02uy, 0xA1uy)
-    let mutable corSymLanguageTypeFSharp = System.Guid(0xAB4F38C9u, 0xB6E6us, 0x43baus, 0xBEuy, 0x3Buy, 0x58uy, 0x08uy, 0x0Buy, 0x2Cuy, 0xCCuy, 0xE3uy)
-    let mutable corSymLanguageVendorMicrosoft = System.Guid(0x994b45c4u, 0xe6e9us, 0x11d2us, 0x90uy, 0x3fuy, 0x00uy, 0xc0uy, 0x4fuy, 0xa3uy, 0x02uy, 0xa1uy)
-    let mutable corSymDocumentTypeText = System.Guid(0x5a869d0bu, 0x6611us, 0x11d3us, 0xbduy, 0x2auy, 0x0uy, 0x0uy, 0xf8uy, 0x8uy, 0x49uy, 0xbduy)
+    let mutable corSymLanguageTypeFSharp =
+        System.Guid(0xAB4F38C9u, 0xB6E6us, 0x43baus, 0xBEuy, 0x3Buy, 0x58uy, 0x08uy, 0x0Buy, 0x2Cuy, 0xCCuy, 0xE3uy)
+
+    let mutable corSymLanguageVendorMicrosoft =
+        System.Guid(0x994b45c4u, 0xe6e9us, 0x11d2us, 0x90uy, 0x3fuy, 0x00uy, 0xc0uy, 0x4fuy, 0xa3uy, 0x02uy, 0xa1uy)
+
+    let mutable corSymDocumentTypeText =
+        System.Guid(0x5a869d0bu, 0x6611us, 0x11d3us, 0xbduy, 0x2auy, 0x0uy, 0x0uy, 0xf8uy, 0x8uy, 0x49uy, 0xbduy)
+
     let mutable docWriter = Unchecked.defaultof
     writer.symWriter.DefineDocument(url, &corSymLanguageTypeFSharp, &corSymLanguageVendorMicrosoft, &corSymDocumentTypeText, &docWriter)
     setCheckSum (url, docWriter)
     { symDocWriter = docWriter }
 
-let pdbOpenMethod (writer: PdbWriter) (methodToken: int32) =
-    writer.symWriter.OpenMethod methodToken
+let pdbOpenMethod (writer: PdbWriter) (methodToken: int32) = writer.symWriter.OpenMethod methodToken
 
-let pdbCloseMethod (writer: PdbWriter) =
-    writer.symWriter.CloseMethod()
+let pdbCloseMethod (writer: PdbWriter) = writer.symWriter.CloseMethod()
 
 let pdbOpenScope (writer: PdbWriter) (startOffset: int32) =
     let mutable retInt = 0
     writer.symWriter.OpenScope(startOffset, &retInt)
     check "action" (retInt)
 
-let pdbCloseScope (writer: PdbWriter) (endOffset: int32) =
-    writer.symWriter.CloseScope endOffset
+let pdbCloseScope (writer: PdbWriter) (endOffset: int32) = writer.symWriter.CloseScope endOffset
 
 let pdbDefineLocalVariable (writer: PdbWriter) (name: string) (signature: byte[]) (addr1: int32) =
-    writer.symWriter.DefineLocalVariable(name, 0, signature.Length, signature, int System.Diagnostics.SymbolStore.SymAddressKind.ILOffset, addr1, 0, 0, 0, 0)
-
-let pdbSetMethodRange (writer: PdbWriter) (docWriter1: PdbDocumentWriter) (startLine: int) (startCol: int) (docWriter2: PdbDocumentWriter) (endLine: int) (endCol: int) =
+    writer.symWriter.DefineLocalVariable(
+        name,
+        0,
+        signature.Length,
+        signature,
+        int System.Diagnostics.SymbolStore.SymAddressKind.ILOffset,
+        addr1,
+        0,
+        0,
+        0,
+        0
+    )
+
+let pdbSetMethodRange
+    (writer: PdbWriter)
+    (docWriter1: PdbDocumentWriter)
+    (startLine: int)
+    (startCol: int)
+    (docWriter2: PdbDocumentWriter)
+    (endLine: int)
+    (endCol: int)
+    =
     writer.symWriter.SetMethodSourceRange(docWriter1.symDocWriter, startLine, startCol, docWriter2.symDocWriter, endLine, endCol)
 
-let pdbDefineSequencePoints (writer: PdbWriter) (docWriter: PdbDocumentWriter) (pts: (int * int * int * int * int)[])  =
+let pdbDefineSequencePoints (writer: PdbWriter) (docWriter: PdbDocumentWriter) (pts: (int * int * int * int * int)[]) =
     let offsets = (Array.map (fun (x, _, _, _, _) -> x) pts)
     let lines = (Array.map (fun (_, x, _, _, _) -> x) pts)
     let columns = (Array.map (fun (_, _, x, _, _) -> x) pts)
@@ -976,86 +1140,120 @@ let pdbWriteDebugInfo (writer: PdbWriter) =
     let mutable iDD = new ImageDebugDirectory()
     let mutable length = 0
     writer.symWriter.GetDebugInfo(&iDD, 0, &length, null)
-    let mutable data: byte [] = Array.zeroCreate length
+    let mutable data: byte[] = Array.zeroCreate length
     writer.symWriter.GetDebugInfo(&iDD, length, &length, data)
 
-    { iddCharacteristics = iDD.Characteristics
-      iddMajorVersion = int32 iDD.MajorVersion
-      iddMinorVersion = int32 iDD.MinorVersion
-      iddType = iDD.Type
-      iddData = data}
+    {
+        iddCharacteristics = iDD.Characteristics
+        iddMajorVersion = int32 iDD.MajorVersion
+        iddMinorVersion = int32 iDD.MinorVersion
+        iddType = iDD.Type
+        iddData = data
+    }
 #endif
 
-
 #if !FX_NO_PDB_WRITER
 // PDB reading
-type PdbReader  = { symReader: ISymbolReader }
-type PdbDocument  = { symDocument: ISymbolDocument }
-type PdbMethod  = { symMethod: ISymbolMethod }
+type PdbReader = { symReader: ISymbolReader }
+type PdbDocument = { symDocument: ISymbolDocument }
+type PdbMethod = { symMethod: ISymbolMethod }
 type PdbVariable = { symVariable: ISymbolVariable }
 type PdbMethodScope = { symScope: ISymbolScope }
 
 type PdbDebugPoint =
-    { pdbSeqPointOffset: int
-      pdbSeqPointDocument: PdbDocument
-      pdbSeqPointLine: int
-      pdbSeqPointColumn: int
-      pdbSeqPointEndLine: int
-      pdbSeqPointEndColumn: int }
+    {
+        pdbSeqPointOffset: int
+        pdbSeqPointDocument: PdbDocument
+        pdbSeqPointLine: int
+        pdbSeqPointColumn: int
+        pdbSeqPointEndLine: int
+        pdbSeqPointEndColumn: int
+    }
 
 let pdbReadOpen (moduleName: string) (path: string) : PdbReader =
-    let CorMetaDataDispenser = System.Type.GetTypeFromProgID("CLRMetaData.CorMetaDataDispenser")
+    let CorMetaDataDispenser =
+        System.Type.GetTypeFromProgID("CLRMetaData.CorMetaDataDispenser")
+
     let mutable IID_IMetaDataImport = new Guid("7DAC8207-D3AE-4c75-9B67-92801A497D44")
-    let mdd = System.Activator.CreateInstance(CorMetaDataDispenser) :?> IMetaDataDispenser
+
+    let mdd =
+        System.Activator.CreateInstance(CorMetaDataDispenser) :?> IMetaDataDispenser
+
     let mutable o: Object = new Object()
     mdd.OpenScope(moduleName, 0, &IID_IMetaDataImport, &o)
     let importerPtr = Marshal.GetComInterfaceForObject(o, typeof)
+
     try
 #if ENABLE_MONO_SUPPORT
         // ISymWrapper.dll is not available as a compile-time dependency for the cross-platform compiler, since it is Windows-only
         // Access it via reflection instead.System.Diagnostics.SymbolStore.SymBinder
         try
-            let isym = System.Reflection.Assembly.Load("ISymWrapper, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
+            let isym =
+                System.Reflection.Assembly.Load("ISymWrapper, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
+
             let symbolBinder = isym.CreateInstance("System.Diagnostics.SymbolStore.SymBinder")
             let symbolBinderTy = symbolBinder.GetType()
-            let reader = symbolBinderTy.InvokeMember("GetReader", BindingFlags.Public ||| BindingFlags.InvokeMethod ||| BindingFlags.Instance,  null, symbolBinder, [| box importerPtr; box moduleName; box path |])
+
+            let reader =
+                symbolBinderTy.InvokeMember(
+                    "GetReader",
+                    BindingFlags.Public ||| BindingFlags.InvokeMethod ||| BindingFlags.Instance,
+                    null,
+                    symbolBinder,
+                    [| box importerPtr; box moduleName; box path |]
+                )
+
             { symReader = reader :?> ISymbolReader }
-        with _ ->
-            { symReader = null }
+        with
+        | _ -> { symReader = null }
 #else
         let symbolBinder = new System.Diagnostics.SymbolStore.SymBinder()
-        { symReader = symbolBinder.GetReader(importerPtr, moduleName, path) }
+
+        {
+            symReader = symbolBinder.GetReader(importerPtr, moduleName, path)
+        }
 #endif
     finally
         // Marshal.GetComInterfaceForObject adds an extra ref for importerPtr
         if IntPtr.Zero <> importerPtr then
-          Marshal.Release importerPtr |> ignore
+            Marshal.Release importerPtr |> ignore
 
 // The symbol reader's finalize method will clean up any unmanaged resources.
 // If file locks persist, we may want to manually invoke finalize
 let pdbReadClose (_reader: PdbReader) : unit = ()
 
 let pdbReaderGetMethod (reader: PdbReader) (token: int32) : PdbMethod =
-    { symMethod = reader.symReader.GetMethod(SymbolToken token) }
+    {
+        symMethod = reader.symReader.GetMethod(SymbolToken token)
+    }
 
-let pdbReaderGetMethodFromDocumentPosition (reader: PdbReader)  (document: PdbDocument) (line: int) (column: int) : PdbMethod =
-    { symMethod = reader.symReader.GetMethodFromDocumentPosition(document.symDocument, line, column) }
+let pdbReaderGetMethodFromDocumentPosition (reader: PdbReader) (document: PdbDocument) (line: int) (column: int) : PdbMethod =
+    {
+        symMethod = reader.symReader.GetMethodFromDocumentPosition(document.symDocument, line, column)
+    }
 
 let pdbReaderGetDocuments (reader: PdbReader) : PdbDocument[] =
     let arr = reader.symReader.GetDocuments()
-    Array.map (fun i -> { symDocument=i }) arr
-
-let pdbReaderGetDocument (reader: PdbReader) (url: string) (language: byte[]) (languageVendor: byte[]) (documentType: byte[]) : PdbDocument =
-    { symDocument = reader.symReader.GetDocument(url, Guid language, Guid languageVendor, System.Guid documentType) }
-
-let pdbDocumentGetURL (document: PdbDocument) : string =
-    document.symDocument.URL
-
-let pdbDocumentGetType (document: PdbDocument) : byte[] (* guid *) =
+    Array.map (fun i -> { symDocument = i }) arr
+
+let pdbReaderGetDocument
+    (reader: PdbReader)
+    (url: string)
+    (language: byte[])
+    (languageVendor: byte[])
+    (documentType: byte[])
+    : PdbDocument =
+    {
+        symDocument = reader.symReader.GetDocument(url, Guid language, Guid languageVendor, System.Guid documentType)
+    }
+
+let pdbDocumentGetURL (document: PdbDocument) : string = document.symDocument.URL
+
+let pdbDocumentGetType (document: PdbDocument) : byte (* guid *) [] =
     let guid = document.symDocument.DocumentType
     guid.ToByteArray()
 
-let pdbDocumentGetLanguage (document: PdbDocument) : byte[] (* guid *) =
+let pdbDocumentGetLanguage (document: PdbDocument) : byte (* guid *) [] =
     let guid = document.symDocument.Language
     guid.ToByteArray()
 
@@ -1071,7 +1269,7 @@ let pdbMethodGetToken (meth: PdbMethod) : int32 =
     token.GetToken()
 
 let pdbMethodGetDebugPoints (meth: PdbMethod) : PdbDebugPoint[] =
-    let  pSize = meth.symMethod.SequencePointCount
+    let pSize = meth.symMethod.SequencePointCount
     let offsets = Array.zeroCreate pSize
     let docs = Array.zeroCreate pSize
     let lines = Array.zeroCreate pSize
@@ -1082,29 +1280,29 @@ let pdbMethodGetDebugPoints (meth: PdbMethod) : PdbDebugPoint[] =
     meth.symMethod.GetSequencePoints(offsets, docs, lines, cols, endLines, endColumns)
 
     Array.init pSize (fun i ->
-        { pdbSeqPointOffset = offsets.[i]
-          pdbSeqPointDocument = { symDocument = docs.[i] }
-          pdbSeqPointLine = lines.[i]
-          pdbSeqPointColumn = cols.[i]
-          pdbSeqPointEndLine = endLines.[i]
-          pdbSeqPointEndColumn = endColumns.[i] })
+        {
+            pdbSeqPointOffset = offsets.[i]
+            pdbSeqPointDocument = { symDocument = docs.[i] }
+            pdbSeqPointLine = lines.[i]
+            pdbSeqPointColumn = cols.[i]
+            pdbSeqPointEndLine = endLines.[i]
+            pdbSeqPointEndColumn = endColumns.[i]
+        })
 
 let pdbScopeGetChildren (scope: PdbMethodScope) : PdbMethodScope[] =
     let arr = scope.symScope.GetChildren()
-    Array.map (fun i -> { symScope=i }) arr
+    Array.map (fun i -> { symScope = i }) arr
 
 let pdbScopeGetOffsets (scope: PdbMethodScope) : int * int =
     (scope.symScope.StartOffset, scope.symScope.EndOffset)
 
 let pdbScopeGetLocals (scope: PdbMethodScope) : PdbVariable[] =
     let arr = scope.symScope.GetLocals()
-    Array.map (fun i -> { symVariable=i }) arr
+    Array.map (fun i -> { symVariable = i }) arr
 
-let pdbVariableGetName (variable: PdbVariable) : string =
-    variable.symVariable.Name
+let pdbVariableGetName (variable: PdbVariable) : string = variable.symVariable.Name
 
-let pdbVariableGetSignature (variable: PdbVariable) : byte[] =
-    variable.symVariable.GetSignature()
+let pdbVariableGetSignature (variable: PdbVariable) : byte[] = variable.symVariable.GetSignature()
 
 // The tuple is (AddressKind, AddressField1)
 let pdbVariableGetAddressAttributes (variable: PdbVariable) : (int32 * int32) =
diff --git a/src/Compiler/AbstractIL/ilwritepdb.fs b/src/Compiler/AbstractIL/ilwritepdb.fs
index a6a5bb8afb0..0a4a5b53794 100644
--- a/src/Compiler/AbstractIL/ilwritepdb.fs
+++ b/src/Compiler/AbstractIL/ilwritepdb.fs
@@ -20,27 +20,40 @@ open FSharp.Compiler.DiagnosticsLogger
 open FSharp.Compiler.IO
 open FSharp.Compiler.Text.Range
 
-type BlobBuildingStream () =
+type BlobBuildingStream() =
     inherit Stream()
 
     static let chunkSize = 32 * 1024
     let builder = BlobBuilder(chunkSize)
 
     override _.CanWrite = true
-    override _.CanRead  = false
-    override _.CanSeek  = false
-    override _.Length   = int64 builder.Count
 
-    override _.Write(buffer: byte array, offset: int, count: int) = builder.WriteBytes(buffer, offset, count)
+    override _.CanRead = false
+
+    override _.CanSeek = false
+
+    override _.Length = int64 builder.Count
+
+    override _.Write(buffer: byte array, offset: int, count: int) =
+        builder.WriteBytes(buffer, offset, count)
+
     override _.WriteByte(value: byte) = builder.WriteByte value
-    member   _.WriteInt32(value: int) = builder.WriteInt32 value
-    member   _.ToImmutableArray() = builder.ToImmutableArray()
-    member   _.TryWriteBytes(stream: Stream, length: int) = builder.TryWriteBytes(stream, length)
+
+    member _.WriteInt32(value: int) = builder.WriteInt32 value
+
+    member _.ToImmutableArray() = builder.ToImmutableArray()
+
+    member _.TryWriteBytes(stream: Stream, length: int) = builder.TryWriteBytes(stream, length)
 
     override _.Flush() = ()
+
     override _.Dispose(_disposing: bool) = ()
+
     override _.Seek(_offset: int64, _origin: SeekOrigin) = raise (NotSupportedException())
+
     override _.Read(_buffer: byte array, _offset: int, _count: int) = raise (NotSupportedException())
+
+
     override _.SetLength(_value: int64) = raise (NotSupportedException())
     override val Position = 0L with get, set
 
@@ -51,101 +64,106 @@ type PdbDocumentData = ILSourceDocument
 
 type PdbLocalVar =
     {
-      Name: string
-      Signature: byte[]
-      /// the local index the name corresponds to
-      Index: int32
+        Name: string
+        Signature: byte[]
+        /// the local index the name corresponds to
+        Index: int32
     }
 
 type PdbImport =
     | ImportType of targetTypeToken: int32 (* alias: string option *)
-    | ImportNamespace of targetNamespace: string (* assembly: ILAssemblyRef option * alias: string option *) 
-    //| ReferenceAlias of string
-    //| OpenXmlNamespace of prefix: string * xmlNamespace: string
+    | ImportNamespace of targetNamespace: string (* assembly: ILAssemblyRef option * alias: string option *)
+//| ReferenceAlias of string
+//| OpenXmlNamespace of prefix: string * xmlNamespace: string
 
 type PdbImports =
-    { 
-      Parent: PdbImports option
-      Imports: PdbImport[]
+    {
+        Parent: PdbImports option
+        Imports: PdbImport[]
     }
 
 type PdbMethodScope =
-    { 
-      Children: PdbMethodScope[]
-      StartOffset: int
-      EndOffset: int
-      Locals: PdbLocalVar[]
-      Imports: PdbImports option
+    {
+        Children: PdbMethodScope[]
+        StartOffset: int
+        EndOffset: int
+        Locals: PdbLocalVar[]
+        Imports: PdbImports option
     }
 
 type PdbSourceLoc =
     {
-      Document: int
-      Line: int
-      Column: int
+        Document: int
+        Line: int
+        Column: int
     }
 
 type PdbDebugPoint =
     {
-      Document: int
-      Offset: int
-      Line: int
-      Column: int
-      EndLine: int
-      EndColumn: int
+        Document: int
+        Offset: int
+        Line: int
+        Column: int
+        EndLine: int
+        EndColumn: int
     }
-    override x.ToString() = sprintf "(%d,%d)-(%d,%d)" x.Line x.Column x.EndLine x.EndColumn
+
+    override x.ToString() =
+        sprintf "(%d,%d)-(%d,%d)" x.Line x.Column x.EndLine x.EndColumn
 
 type PdbMethodData =
     {
-      MethToken: int32
-      MethName: string
-      LocalSignatureToken: int32
-      Params: PdbLocalVar array
-      RootScope: PdbMethodScope option
-      DebugRange: (PdbSourceLoc * PdbSourceLoc) option
-      DebugPoints: PdbDebugPoint array
+        MethToken: int32
+        MethName: string
+        LocalSignatureToken: int32
+        Params: PdbLocalVar array
+        RootScope: PdbMethodScope option
+        DebugRange: (PdbSourceLoc * PdbSourceLoc) option
+        DebugPoints: PdbDebugPoint array
     }
 
 module SequencePoint =
     let orderBySource sp1 sp2 =
         let c1 = compare sp1.Document sp2.Document
+
         if c1 <> 0 then
             c1
         else
             let c1 = compare sp1.Line sp2.Line
+
             if c1 <> 0 then
                 c1
             else
                 compare sp1.Column sp2.Column
 
-    let orderByOffset sp1 sp2 =
-        compare sp1.Offset sp2.Offset
+    let orderByOffset sp1 sp2 = compare sp1.Offset sp2.Offset
 
 /// 28 is the size of the IMAGE_DEBUG_DIRECTORY in ntimage.h
 let sizeof_IMAGE_DEBUG_DIRECTORY = 28
 
 []
 type PdbData =
-    { EntryPoint: int32 option
-      Timestamp: int32
-      ModuleID: byte[]
-      Documents: PdbDocumentData[]
-      Methods: PdbMethodData[]
-      TableRowCounts: int[] }
+    {
+        EntryPoint: int32 option
+        Timestamp: int32
+        ModuleID: byte[]
+        Documents: PdbDocumentData[]
+        Methods: PdbMethodData[]
+        TableRowCounts: int[]
+    }
 
-type BinaryChunk =
-    { size: int32
-      addr: int32 }
+type BinaryChunk = { size: int32; addr: int32 }
 
 type idd =
-    { iddCharacteristics: int32
-      iddMajorVersion: int32; (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
-      iddMinorVersion: int32; (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
-      iddType: int32
-      iddTimestamp: int32
-      iddData: byte[]
-      iddChunk: BinaryChunk }
+    {
+        iddCharacteristics: int32
+        iddMajorVersion: int32 (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
+        iddMinorVersion: int32 (* actually u16 in IMAGE_DEBUG_DIRECTORY *)
+        iddType: int32
+        iddTimestamp: int32
+        iddData: byte[]
+        iddChunk: BinaryChunk
+    }
 
 /// The specified Hash algorithm to use on portable pdb files.
 type HashAlgorithm =
@@ -159,14 +177,16 @@ let guidSha2 = Guid("8829d00f-11b8-4213-878b-770e8597ac16")
 let checkSum (url: string) (checksumAlgorithm: HashAlgorithm) =
     try
         use file = FileSystem.OpenFileForReadShim(url)
+
         let guid, alg =
             match checksumAlgorithm with
             | HashAlgorithm.Sha1 -> guidSha1, SHA1.Create() :> System.Security.Cryptography.HashAlgorithm
             | HashAlgorithm.Sha256 -> guidSha2, SHA256.Create() :> System.Security.Cryptography.HashAlgorithm
 
         let checkSum = alg.ComputeHash file
-        Some (guid, checkSum)
-    with _ -> None
+        Some(guid, checkSum)
+    with
+    | _ -> None
 
 //---------------------------------------------------------------------
 // Portable PDB Writer
@@ -176,92 +196,117 @@ let b0 n = (n &&& 0xFF)
 let b1 n = ((n >>> 8) &&& 0xFF)
 let b2 n = ((n >>> 16) &&& 0xFF)
 let b3 n = ((n >>> 24) &&& 0xFF)
-let i32AsBytes i = [| byte (b0 i); byte (b1 i); byte (b2 i); byte (b3 i) |]
+
+let i32AsBytes i =
+    [| byte (b0 i); byte (b1 i); byte (b2 i); byte (b3 i) |]
 
 let cvMagicNumber = 0x53445352L
+
 let pdbGetCvDebugInfo (mvid: byte[]) (timestamp: int32) (filepath: string) (cvChunk: BinaryChunk) =
     let iddCvBuffer =
         // Debug directory entry
         let path = (Encoding.UTF8.GetBytes filepath)
-        let buffer = Array.zeroCreate (sizeof + mvid.Length + sizeof + path.Length + 1)
-        let offset, size = (0, sizeof)                    // Magic Number RSDS dword: 0x53445352L
+
+        let buffer =
+            Array.zeroCreate (sizeof + mvid.Length + sizeof + path.Length + 1)
+
+        let offset, size = (0, sizeof) // Magic Number RSDS dword: 0x53445352L
         Buffer.BlockCopy(i32AsBytes (int cvMagicNumber), 0, buffer, offset, size)
-        let offset, size = (offset + size, mvid.Length)         // mvid Guid
+        let offset, size = (offset + size, mvid.Length) // mvid Guid
         Buffer.BlockCopy(mvid, 0, buffer, offset, size)
-        let offset, size = (offset + size, sizeof)       // # of pdb files generated (1)
+        let offset, size = (offset + size, sizeof) // # of pdb files generated (1)
         Buffer.BlockCopy(i32AsBytes 1, 0, buffer, offset, size)
-        let offset, size = (offset + size, path.Length)         // Path to pdb string
+        let offset, size = (offset + size, path.Length) // Path to pdb string
         Buffer.BlockCopy(path, 0, buffer, offset, size)
         buffer
-    { iddCharacteristics = 0                                                    // Reserved
-      iddMajorVersion = 0x0100                                                  // VersionMajor should be 0x0100
-      iddMinorVersion = 0x504d                                                  // VersionMinor should be 0x504d
-      iddType = 2                                                               // IMAGE_DEBUG_TYPE_CODEVIEW
-      iddTimestamp = timestamp
-      iddData = iddCvBuffer                                                     // Path name to the pdb file when built
-      iddChunk = cvChunk
+
+    {
+        iddCharacteristics = 0 // Reserved
+        iddMajorVersion = 0x0100 // VersionMajor should be 0x0100
+        iddMinorVersion = 0x504d // VersionMinor should be 0x504d
+        iddType = 2 // IMAGE_DEBUG_TYPE_CODEVIEW
+        iddTimestamp = timestamp
+        iddData = iddCvBuffer // Path name to the pdb file when built
+        iddChunk = cvChunk
     }
 
-let pdbMagicNumber= 0x4244504dL
+let pdbMagicNumber = 0x4244504dL
+
 let pdbGetEmbeddedPdbDebugInfo (embeddedPdbChunk: BinaryChunk) (uncompressedLength: int64) (compressedStream: MemoryStream) =
     let iddPdbBuffer =
-        let buffer = Array.zeroCreate (sizeof + sizeof + int(compressedStream.Length))
-        let offset, size = (0, sizeof)                    // Magic Number dword: 0x4244504dL
+        let buffer =
+            Array.zeroCreate (sizeof + sizeof + int (compressedStream.Length))
+
+        let offset, size = (0, sizeof) // Magic Number dword: 0x4244504dL
         Buffer.BlockCopy(i32AsBytes (int pdbMagicNumber), 0, buffer, offset, size)
-        let offset, size = (offset + size, sizeof)        // Uncompressed size
+        let offset, size = (offset + size, sizeof) // Uncompressed size
         Buffer.BlockCopy(i32AsBytes (int uncompressedLength), 0, buffer, offset, size)
-        let offset, size = (offset + size, int(compressedStream.Length))   // Uncompressed size
+        let offset, size = (offset + size, int (compressedStream.Length)) // Uncompressed size
         Buffer.BlockCopy(compressedStream.ToArray(), 0, buffer, offset, size)
         buffer
-    { iddCharacteristics = 0                                                    // Reserved
-      iddMajorVersion = 0x0100                                                  // VersionMajor should be 0x0100
-      iddMinorVersion = 0x0100                                                  // VersionMinor should be 0x0100
-      iddType = 17                                                              // IMAGE_DEBUG_TYPE_EMBEDDEDPDB
-      iddTimestamp = 0
-      iddData = iddPdbBuffer                                                    // Path name to the pdb file when built
-      iddChunk = embeddedPdbChunk
+
+    {
+        iddCharacteristics = 0 // Reserved
+        iddMajorVersion = 0x0100 // VersionMajor should be 0x0100
+        iddMinorVersion = 0x0100 // VersionMinor should be 0x0100
+        iddType = 17 // IMAGE_DEBUG_TYPE_EMBEDDEDPDB
+        iddTimestamp = 0
+        iddData = iddPdbBuffer // Path name to the pdb file when built
+        iddChunk = embeddedPdbChunk
     }
 
-let pdbChecksumDebugInfo timestamp (checksumPdbChunk: BinaryChunk) (algorithmName:string) (checksum: byte[]) =
+let pdbChecksumDebugInfo timestamp (checksumPdbChunk: BinaryChunk) (algorithmName: string) (checksum: byte[]) =
     let iddBuffer =
         let alg = Encoding.UTF8.GetBytes(algorithmName)
         let buffer = Array.zeroCreate (alg.Length + 1 + checksum.Length)
         Buffer.BlockCopy(alg, 0, buffer, 0, alg.Length)
         Buffer.BlockCopy(checksum, 0, buffer, alg.Length + 1, checksum.Length)
         buffer
-    { iddCharacteristics = 0                                                    // Reserved
-      iddMajorVersion = 1                                                       // VersionMajor should be 1
-      iddMinorVersion = 0                                                       // VersionMinor should be 0
-      iddType = 19                                                              // IMAGE_DEBUG_TYPE_CHECKSUMPDB
-      iddTimestamp = timestamp
-      iddData = iddBuffer                                                       // Path name to the pdb file when built
-      iddChunk = checksumPdbChunk
+
+    {
+        iddCharacteristics = 0 // Reserved
+        iddMajorVersion = 1 // VersionMajor should be 1
+        iddMinorVersion = 0 // VersionMinor should be 0
+        iddType = 19 // IMAGE_DEBUG_TYPE_CHECKSUMPDB
+        iddTimestamp = timestamp
+        iddData = iddBuffer // Path name to the pdb file when built
+        iddChunk = checksumPdbChunk
     }
 
 let pdbGetPdbDebugDeterministicInfo (deterministicPdbChunk: BinaryChunk) =
-    { iddCharacteristics = 0                                                    // Reserved
-      iddMajorVersion = 0                                                       // VersionMajor should be 0
-      iddMinorVersion = 0                                                       // VersionMinor should be 00
-      iddType = 16                                                              // IMAGE_DEBUG_TYPE_DETERMINISTIC
-      iddTimestamp = 0
-      iddData = Array.empty                                                     // No DATA
-      iddChunk = deterministicPdbChunk
+    {
+        iddCharacteristics = 0 // Reserved
+        iddMajorVersion = 0 // VersionMajor should be 0
+        iddMinorVersion = 0 // VersionMinor should be 00
+        iddType = 16 // IMAGE_DEBUG_TYPE_DETERMINISTIC
+        iddTimestamp = 0
+        iddData = Array.empty // No DATA
+        iddChunk = deterministicPdbChunk
     }
 
-let pdbGetDebugInfo (contentId: byte[]) (timestamp: int32) (filepath: string)
-                    (cvChunk: BinaryChunk)
-                    (embeddedPdbChunk: BinaryChunk option)
-                    (deterministicPdbChunk: BinaryChunk)
-                    (checksumPdbChunk: BinaryChunk) (algorithmName:string) (checksum: byte [])
-                    (uncompressedLength: int64) (compressedStream: MemoryStream option)
-                    (embeddedPdb: bool) (deterministic: bool) =
-    [|  yield pdbGetCvDebugInfo contentId timestamp filepath cvChunk
+let pdbGetDebugInfo
+    (contentId: byte[])
+    (timestamp: int32)
+    (filepath: string)
+    (cvChunk: BinaryChunk)
+    (embeddedPdbChunk: BinaryChunk option)
+    (deterministicPdbChunk: BinaryChunk)
+    (checksumPdbChunk: BinaryChunk)
+    (algorithmName: string)
+    (checksum: byte[])
+    (uncompressedLength: int64)
+    (compressedStream: MemoryStream option)
+    (embeddedPdb: bool)
+    (deterministic: bool)
+    =
+    [|
+        yield pdbGetCvDebugInfo contentId timestamp filepath cvChunk
         yield pdbChecksumDebugInfo timestamp checksumPdbChunk algorithmName checksum
         if embeddedPdb then
             match compressedStream, embeddedPdbChunk with
-            | None, _ | _, None -> ()
-            | Some compressedStream, Some chunk ->
-                yield pdbGetEmbeddedPdbDebugInfo chunk uncompressedLength compressedStream
+            | None, _
+            | _, None -> ()
+            | Some compressedStream, Some chunk -> yield pdbGetEmbeddedPdbDebugInfo chunk uncompressedLength compressedStream
         if deterministic then
             yield pdbGetPdbDebugDeterministicInfo deterministicPdbChunk
     |]
@@ -274,13 +319,13 @@ let pdbGetDebugInfo (contentId: byte[]) (timestamp: int32) (filepath: string)
 // This function takes output file name and returns debug file name.
 let getDebugFileName outfile (portablePDB: bool) =
 #if ENABLE_MONO_SUPPORT
-  if runningOnMono && not portablePDB then
-      outfile + ".mdb"
-  else
+    if runningOnMono && not portablePDB then
+        outfile + ".mdb"
+    else
 #else
-      ignore portablePDB
+    ignore portablePDB
 #endif
-      (FileSystemUtils.chopExtension outfile) + ".pdb"
+    (FileSystemUtils.chopExtension outfile) + ".pdb"
 
 let sortMethods showTimes info =
     reportTime showTimes (sprintf "PDB: Defined %d documents" info.Documents.Length)
@@ -290,17 +335,31 @@ let sortMethods showTimes info =
 
 let getRowCounts tableRowCounts =
     let builder = ImmutableArray.CreateBuilder(tableRowCounts |> Array.length)
-    tableRowCounts |> Seq.iter(fun x -> builder.Add x)
+    tableRowCounts |> Seq.iter (fun x -> builder.Add x)
     builder.MoveToImmutable()
 
 let scopeSorter (scope1: PdbMethodScope) (scope2: PdbMethodScope) =
-    if scope1.StartOffset > scope2.StartOffset then 1
-    elif scope1.StartOffset < scope2.StartOffset then -1
-    elif (scope1.EndOffset - scope1.StartOffset) > (scope2.EndOffset - scope2.StartOffset) then -1
-    elif (scope1.EndOffset - scope1.StartOffset) < (scope2.EndOffset - scope2.StartOffset) then 1
-    else 0
-
-type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, sourceLink: string, checksumAlgorithm, showTimes, info: PdbData, pathMap: PathMap) =
+    if scope1.StartOffset > scope2.StartOffset then
+        1
+    elif scope1.StartOffset < scope2.StartOffset then
+        -1
+    elif (scope1.EndOffset - scope1.StartOffset) > (scope2.EndOffset - scope2.StartOffset) then
+        -1
+    elif (scope1.EndOffset - scope1.StartOffset) < (scope2.EndOffset - scope2.StartOffset) then
+        1
+    else
+        0
+
+type PortablePdbGenerator
+    (
+        embedAllSource: bool,
+        embedSourceList: string list,
+        sourceLink: string,
+        checksumAlgorithm,
+        showTimes,
+        info: PdbData,
+        pathMap: PathMap
+    ) =
 
     let docs =
         match info.Documents with
@@ -313,23 +372,37 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
 
     let serializeDocumentName (name: string) =
         let name = PathMap.apply pathMap name
-        let count s c = s |> Seq.filter(fun ch -> c = ch) |> Seq.length
+
+        let count s c =
+            s |> Seq.filter (fun ch -> c = ch) |> Seq.length
 
         let s1, s2 = '/', '\\'
-        let separator = if (count name s1) >= (count name s2) then s1 else s2
+
+        let separator =
+            if (count name s1) >= (count name s2) then
+                s1
+            else
+                s2
 
         let writer = BlobBuilder()
         writer.WriteByte(byte separator)
 
-        for part in name.Split( [| separator |] ) do
-            let partIndex = MetadataTokens.GetHeapOffset(BlobHandle.op_Implicit(metadata.GetOrAddBlobUTF8 part))
+        for part in name.Split([| separator |]) do
+            let partIndex =
+                MetadataTokens.GetHeapOffset(BlobHandle.op_Implicit (metadata.GetOrAddBlobUTF8 part))
+
             writer.WriteCompressedInteger(int partIndex)
 
         metadata.GetOrAddBlob writer
 
-    let corSymLanguageTypeId = Guid(0xAB4F38C9u, 0xB6E6us, 0x43baus, 0xBEuy, 0x3Buy, 0x58uy, 0x08uy, 0x0Buy, 0x2Cuy, 0xCCuy, 0xE3uy)
-    let embeddedSourceId     = Guid(0x0e8a571bu, 0x6926us, 0x466eus, 0xb4uy, 0xaduy, 0x8auy, 0xb0uy, 0x46uy, 0x11uy, 0xf5uy, 0xfeuy)
-    let sourceLinkId         = Guid(0xcc110556u, 0xa091us, 0x4d38us, 0x9fuy, 0xecuy, 0x25uy, 0xabuy, 0x9auy, 0x35uy, 0x1auy, 0x6auy)
+    let corSymLanguageTypeId =
+        Guid(0xAB4F38C9u, 0xB6E6us, 0x43baus, 0xBEuy, 0x3Buy, 0x58uy, 0x08uy, 0x0Buy, 0x2Cuy, 0xCCuy, 0xE3uy)
+
+    let embeddedSourceId =
+        Guid(0x0e8a571bu, 0x6926us, 0x466eus, 0xb4uy, 0xaduy, 0x8auy, 0xb0uy, 0x46uy, 0x11uy, 0xf5uy, 0xfeuy)
+
+    let sourceLinkId =
+        Guid(0xcc110556u, 0xa091us, 0x4d38us, 0x9fuy, 0xecuy, 0x25uy, 0xabuy, 0x9auy, 0x35uy, 0x1auy, 0x6auy)
 
     /// 
     /// The maximum number of bytes in to write out uncompressed.
@@ -343,7 +416,9 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
     let sourceCompressionThreshold = 200
 
     let includeSource file =
-        let isInList = embedSourceList |> List.exists (fun f -> String.Compare(file, f, StringComparison.OrdinalIgnoreCase ) = 0)
+        let isInList =
+            embedSourceList
+            |> List.exists (fun f -> String.Compare(file, f, StringComparison.OrdinalIgnoreCase) = 0)
 
         if not embedAllSource && not isInList || not (FileSystem.FileExistsShim file) then
             None
@@ -351,10 +426,13 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
             use stream = FileSystem.OpenFileForReadShim(file)
 
             let length64 = stream.Length
-            if length64 > int64 Int32.MaxValue then raise (IOException("File is too long"))
+
+            if length64 > int64 Int32.MaxValue then
+                raise (IOException("File is too long"))
 
             let builder = new BlobBuildingStream()
             let length = int length64
+
             if length < sourceCompressionThreshold then
                 builder.WriteInt32 0
                 builder.TryWriteBytes(stream, length) |> ignore
@@ -362,14 +440,23 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                 builder.WriteInt32 length
                 use deflater = new DeflateStream(builder, CompressionMode.Compress, true)
                 stream.CopyTo deflater
-            Some (builder.ToImmutableArray())
+
+            Some(builder.ToImmutableArray())
 
     let documentIndex =
         let mutable index = Dictionary(docs.Length)
-        let docLength = docs.Length + if String.IsNullOrEmpty sourceLink then 1 else 0
+
+        let docLength =
+            docs.Length
+            + if String.IsNullOrEmpty sourceLink then
+                  1
+              else
+                  0
+
         metadata.SetCapacity(TableIndex.Document, docLength)
+
         for doc in docs do
-          // For F# Interactive, file name 'stdin' gets generated for interactive inputs
+            // For F# Interactive, file name 'stdin' gets generated for interactive inputs
             let handle =
                 match checkSum doc.File checksumAlgorithm with
                 | Some (hashAlg, checkSum) ->
@@ -377,31 +464,44 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                         (serializeDocumentName doc.File,
                          metadata.GetOrAddGuid hashAlg,
                          metadata.GetOrAddBlob(checkSum.ToImmutableArray()),
-                         metadata.GetOrAddGuid corSymLanguageTypeId) |> metadata.AddDocument
+                         metadata.GetOrAddGuid corSymLanguageTypeId)
+                        |> metadata.AddDocument
+
                     match includeSource doc.File with
                     | None -> ()
                     | Some blob ->
-                        metadata.AddCustomDebugInformation(DocumentHandle.op_Implicit dbgInfo,
-                                                           metadata.GetOrAddGuid embeddedSourceId,
-                                                           metadata.GetOrAddBlob blob) |> ignore
+                        metadata.AddCustomDebugInformation(
+                            DocumentHandle.op_Implicit dbgInfo,
+                            metadata.GetOrAddGuid embeddedSourceId,
+                            metadata.GetOrAddBlob blob
+                        )
+                        |> ignore
+
                     dbgInfo
                 | None ->
                     let dbgInfo =
                         (serializeDocumentName doc.File,
                          metadata.GetOrAddGuid(Guid.Empty),
                          metadata.GetOrAddBlob(ImmutableArray.Empty),
-                         metadata.GetOrAddGuid corSymLanguageTypeId) |> metadata.AddDocument
+                         metadata.GetOrAddGuid corSymLanguageTypeId)
+                        |> metadata.AddDocument
+
                     dbgInfo
+
             index.Add(doc.File, handle)
 
         if not (String.IsNullOrWhiteSpace sourceLink) then
             use fs = FileSystem.OpenFileForReadShim(sourceLink)
             use ms = new MemoryStream()
             fs.CopyTo ms
+
             metadata.AddCustomDebugInformation(
-                ModuleDefinitionHandle.op_Implicit(EntityHandle.ModuleDefinition),
+                ModuleDefinitionHandle.op_Implicit (EntityHandle.ModuleDefinition),
                 metadata.GetOrAddGuid sourceLinkId,
-                metadata.GetOrAddBlob(ms.ToArray())) |> ignore
+                metadata.GetOrAddBlob(ms.ToArray())
+            )
+            |> ignore
+
         index
 
     let mutable lastLocalVariableHandle = Unchecked.defaultof
@@ -438,57 +538,57 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
         // Corresponds to an 'open ' or 'open type' in F#
         | ImportType targetTypeToken ->
 
-                //if (import.AliasOpt != null)
-                //{
-                //    //  ::= AliasType  
-                //    writer.WriteByte((byte)ImportDefinitionKind.AliasType);
-                //    writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
-                //}
-                //else
-                    //  ::= ImportType 
-                writer.WriteByte(byte ImportDefinitionKind.ImportType)
+            //if (import.AliasOpt != null)
+            //{
+            //    //  ::= AliasType  
+            //    writer.WriteByte((byte)ImportDefinitionKind.AliasType);
+            //    writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
+            //}
+            //else
+            //  ::= ImportType 
+            writer.WriteByte(byte ImportDefinitionKind.ImportType)
 
-                writer.WriteCompressedInteger(targetTypeToken)
+            writer.WriteCompressedInteger(targetTypeToken)
 
-        // Corresponds to an 'open ' 
+        // Corresponds to an 'open '
         | ImportNamespace targetNamespace ->
-                //if (import.TargetAssemblyOpt != null)
-                //{
-                //    if (import.AliasOpt != null)
-                //    {
-                //        //  ::= AliasAssemblyNamespace   
-                //        writer.WriteByte((byte)ImportDefinitionKind.AliasAssemblyNamespace);
-                //        writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
-                //    }
-                //    else
-                //    {
-                //        //  ::= ImportAssemblyNamespace  
-                //        writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyNamespace);
-                //    }
-
-                //    writer.WriteCompressedInteger(MetadataTokens.GetRowNumber(GetAssemblyReferenceHandle(import.TargetAssemblyOpt)));
-                //}
-                //else
-                //{
-                    //if (import.AliasOpt != null)
-                    //{
-                    //    //  ::= AliasNamespace  
-                    //    writer.WriteByte((byte)ImportDefinitionKind.AliasNamespace);
-                    //    writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
-                    //}
-                    //else
-                    //{
-                        //  ::= ImportNamespace 
-                writer.WriteByte(byte ImportDefinitionKind.ImportNamespace);
-                writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(metadata.GetOrAddBlobUTF8(targetNamespace)))
-
-        //| ReferenceAlias alias ->
-        //        //  ::= ImportReferenceAlias 
-        //        Debug.Assert(import.AliasOpt != null);
-        //        Debug.Assert(import.TargetAssemblyOpt == null);
-
-        //        writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyReferenceAlias);
-        //        writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
+            //if (import.TargetAssemblyOpt != null)
+            //{
+            //    if (import.AliasOpt != null)
+            //    {
+            //        //  ::= AliasAssemblyNamespace   
+            //        writer.WriteByte((byte)ImportDefinitionKind.AliasAssemblyNamespace);
+            //        writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
+            //    }
+            //    else
+            //    {
+            //        //  ::= ImportAssemblyNamespace  
+            //        writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyNamespace);
+            //    }
+
+            //    writer.WriteCompressedInteger(MetadataTokens.GetRowNumber(GetAssemblyReferenceHandle(import.TargetAssemblyOpt)));
+            //}
+            //else
+            //{
+            //if (import.AliasOpt != null)
+            //{
+            //    //  ::= AliasNamespace  
+            //    writer.WriteByte((byte)ImportDefinitionKind.AliasNamespace);
+            //    writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
+            //}
+            //else
+            //{
+            //  ::= ImportNamespace 
+            writer.WriteByte(byte ImportDefinitionKind.ImportNamespace)
+            writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(metadata.GetOrAddBlobUTF8(targetNamespace)))
+
+    //| ReferenceAlias alias ->
+    //        //  ::= ImportReferenceAlias 
+    //        Debug.Assert(import.AliasOpt != null);
+    //        Debug.Assert(import.TargetAssemblyOpt == null);
+
+    //        writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyReferenceAlias);
+    //        writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt)));
 
     let serializeImportsBlob (imports: PdbImport[]) =
         let writer = new BlobBuilder()
@@ -499,33 +599,38 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
         metadata.GetOrAddBlob(writer)
 
     // Define the empty global imports scope for the whole assembly,it gets index #1 (the first entry in the table)
-    let defineModuleImportScope() =
+    let defineModuleImportScope () =
         let writer = new BlobBuilder()
         let blob = metadata.GetOrAddBlob writer
-        let rid = metadata.AddImportScope(parentScope=Unchecked.defaultof<_>,imports=blob)
-        assert(rid = moduleImportScopeHandle)
+
+        let rid =
+            metadata.AddImportScope(parentScope = Unchecked.defaultof<_>, imports = blob)
+
+        assert (rid = moduleImportScopeHandle)
 
     let rec getImportScopeIndex (imports: PdbImports) =
         match importScopesTable.TryGetValue(imports) with
         | true, v -> v
-        | _ -> 
+        | _ ->
 
-        let parentScopeHandle =
-            match imports.Parent with
-            | None -> moduleImportScopeHandle
-            | Some parent -> getImportScopeIndex parent
+            let parentScopeHandle =
+                match imports.Parent with
+                | None -> moduleImportScopeHandle
+                | Some parent -> getImportScopeIndex parent
 
-        let blob = serializeImportsBlob imports.Imports
-        let result = metadata.AddImportScope(parentScopeHandle, blob)
+            let blob = serializeImportsBlob imports.Imports
+            let result = metadata.AddImportScope(parentScopeHandle, blob)
 
-        importScopesTable.Add(imports, result)
-        result
+            importScopesTable.Add(imports, result)
+            result
 
-    let flattenScopes rootScope = 
+    let flattenScopes rootScope =
         let list = List()
+
         let rec flattenScopes scope parent =
 
             list.Add scope
+
             for nestedScope in scope.Children do
                 let isNested =
                     match parent with
@@ -536,8 +641,7 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
 
         flattenScopes rootScope None
 
-        list.ToArray() 
-        |> Array.sortWith scopeSorter
+        list.ToArray() |> Array.sortWith scopeSorter
 
     let writeMethodScopes methToken rootScope =
 
@@ -547,21 +651,32 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
             // Get or create the import scope for this method
             let importScopeHandle =
 
-                match scope.Imports with 
+                match scope.Imports with
                 | None -> Unchecked.defaultof<_>
                 | Some imports -> getImportScopeIndex imports
 
-            let lastRowNumber = MetadataTokens.GetRowNumber(LocalVariableHandle.op_Implicit lastLocalVariableHandle)
+            let lastRowNumber =
+                MetadataTokens.GetRowNumber(LocalVariableHandle.op_Implicit lastLocalVariableHandle)
+
             let nextHandle = MetadataTokens.LocalVariableHandle(lastRowNumber + 1)
 
-            metadata.AddLocalScope(MetadataTokens.MethodDefinitionHandle(methToken),
+            metadata.AddLocalScope(
+                MetadataTokens.MethodDefinitionHandle(methToken),
                 importScopeHandle,
                 nextHandle,
                 Unchecked.defaultof,
-                scope.StartOffset, scope.EndOffset - scope.StartOffset ) |>ignore
+                scope.StartOffset,
+                scope.EndOffset - scope.StartOffset
+            )
+            |> ignore
 
             for localVariable in scope.Locals do
-                lastLocalVariableHandle <- metadata.AddLocalVariable(LocalVariableAttributes.None, localVariable.Index, metadata.GetOrAddString(localVariable.Name))
+                lastLocalVariableHandle <-
+                    metadata.AddLocalVariable(
+                        LocalVariableAttributes.None,
+                        localVariable.Index,
+                        metadata.GetOrAddString(localVariable.Name)
+                    )
 
     let emitMethod minfo =
         let docHandle, sequencePointBlob =
@@ -577,8 +692,8 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
             builder.WriteCompressedInteger(minfo.LocalSignatureToken)
 
             if sps.Length = 0 then
-                builder.WriteCompressedInteger( 0 )
-                builder.WriteCompressedInteger( 0 )
+                builder.WriteCompressedInteger(0)
+                builder.WriteCompressedInteger(0)
                 Unchecked.defaultof, Unchecked.defaultof
             else
 
@@ -586,15 +701,20 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                 // If part of the method body is in another document returns nil handle.
                 let tryGetSingleDocumentIndex =
                     let mutable singleDocumentIndex = sps[0].Document
+
                     for i in 1 .. sps.Length - 1 do
                         if sps[i].Document <> singleDocumentIndex then
                             singleDocumentIndex <- -1
+
                     singleDocumentIndex
 
                 // Initial document:  When sp's spread over more than one document we put the initial document here.
                 let singleDocumentIndex = tryGetSingleDocumentIndex
+
                 if singleDocumentIndex = -1 then
-                    builder.WriteCompressedInteger( MetadataTokens.GetRowNumber(DocumentHandle.op_Implicit(getDocumentHandle sps[0].Document)) )
+                    builder.WriteCompressedInteger(
+                        MetadataTokens.GetRowNumber(DocumentHandle.op_Implicit (getDocumentHandle sps[0].Document))
+                    )
 
                 let mutable previousNonHiddenStartLine = -1
                 let mutable previousNonHiddenStartColumn = 0
@@ -602,8 +722,11 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                 for i in 0 .. (sps.Length - 1) do
 
                     if singleDocumentIndex <> -1 && sps[i].Document <> singleDocumentIndex then
-                        builder.WriteCompressedInteger( 0 )
-                        builder.WriteCompressedInteger( MetadataTokens.GetRowNumber(DocumentHandle.op_Implicit(getDocumentHandle sps[i].Document)) )
+                        builder.WriteCompressedInteger(0)
+
+                        builder.WriteCompressedInteger(
+                            MetadataTokens.GetRowNumber(DocumentHandle.op_Implicit (getDocumentHandle sps[i].Document))
+                        )
                     else
                         //=============================================================================================================================================
                         // Sequence-point-record
@@ -622,7 +745,7 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                             else v
 
                         let capOffset v = capValue v 0xfffe
-                        let capLine v =  capValue v 0x1ffffffe
+                        let capLine v = capValue v 0x1ffffffe
                         let capColumn v = capValue v 0xfffe
 
                         let offset = capOffset sps[i].Offset
@@ -631,34 +754,36 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                         let startColumn = capColumn sps[i].Column
                         let endColumn = capColumn sps[i].EndColumn
 
-                        let offsetDelta =                                                               // delta from previous offset
-                            if i > 0 then offset - capOffset sps[i - 1].Offset
-                            else offset
+                        let offsetDelta = // delta from previous offset
+                            if i > 0 then
+                                offset - capOffset sps[i - 1].Offset
+                            else
+                                offset
 
                         if i < 1 || offsetDelta > 0 then
                             builder.WriteCompressedInteger offsetDelta
 
                             // Check for hidden-sequence-point-record
-                            if startLine = 0xfeefee ||
-                               endLine = 0xfeefee ||
-                               (startColumn = 0 && endColumn = 0) ||
-                               ((endLine - startLine) = 0 && (endColumn - startColumn)  = 0)
-                            then
+                            if startLine = 0xfeefee
+                               || endLine = 0xfeefee
+                               || (startColumn = 0 && endColumn = 0)
+                               || ((endLine - startLine) = 0 && (endColumn - startColumn) = 0) then
                                 // Hidden-sequence-point-record
                                 builder.WriteCompressedInteger 0
                                 builder.WriteCompressedInteger 0
                             else
                                 // Non-hidden-sequence-point-record
-                                let deltaLines = endLine - startLine                                    // lines
+                                let deltaLines = endLine - startLine // lines
                                 builder.WriteCompressedInteger deltaLines
 
-                                let deltaColumns = endColumn - startColumn                              // Columns
+                                let deltaColumns = endColumn - startColumn // Columns
+
                                 if deltaLines = 0 then
                                     builder.WriteCompressedInteger deltaColumns
                                 else
                                     builder.WriteCompressedSignedInteger deltaColumns
 
-                                if previousNonHiddenStartLine < 0 then                                  // delta Start Line & Column:
+                                if previousNonHiddenStartLine < 0 then // delta Start Line & Column:
                                     builder.WriteCompressedInteger startLine
                                     builder.WriteCompressedInteger startColumn
                                 else
@@ -674,13 +799,13 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
 
         match minfo.RootScope with
         | None -> ()
-        | Some scope -> writeMethodScopes minfo.MethToken scope 
+        | Some scope -> writeMethodScopes minfo.MethToken scope
 
     member _.Emit() =
         sortMethods showTimes info
         metadata.SetCapacity(TableIndex.MethodDebugInformation, info.Methods.Length)
 
-        defineModuleImportScope()
+        defineModuleImportScope ()
 
         for minfo in info.Methods do
             emitMethod minfo
@@ -704,34 +829,100 @@ type PortablePdbGenerator (embedAllSource: bool, embedSourceList: string list, s
                 let contentBytes = content |> Seq.collect (fun c -> c.GetBytes()) |> Array.ofSeq
                 contentHash <- contentBytes |> hashAlgorithm.ComputeHash
                 BlobContentId.FromHash contentHash
+
             Func, BlobContentId>(convert)
 
         let externalRowCounts = getRowCounts info.TableRowCounts
 
-        let serializer = PortablePdbBuilder(metadata, externalRowCounts, entryPoint, idProvider)
+        let serializer =
+            PortablePdbBuilder(metadata, externalRowCounts, entryPoint, idProvider)
+
         let blobBuilder = BlobBuilder()
-        let contentId= serializer.Serialize blobBuilder
+        let contentId = serializer.Serialize blobBuilder
         let portablePdbStream = new MemoryStream()
         blobBuilder.WriteContentTo portablePdbStream
         reportTime showTimes "PDB: Created"
         (portablePdbStream.Length, contentId, portablePdbStream, algorithmName, contentHash)
 
-let generatePortablePdb (embedAllSource: bool) (embedSourceList: string list) (sourceLink: string) checksumAlgorithm showTimes (info: PdbData) (pathMap: PathMap) =
-    let generator = PortablePdbGenerator (embedAllSource, embedSourceList, sourceLink, checksumAlgorithm, showTimes, info, pathMap)
+let generatePortablePdb
+    (embedAllSource: bool)
+    (embedSourceList: string list)
+    (sourceLink: string)
+    checksumAlgorithm
+    showTimes
+    (info: PdbData)
+    (pathMap: PathMap)
+    =
+    let generator =
+        PortablePdbGenerator(embedAllSource, embedSourceList, sourceLink, checksumAlgorithm, showTimes, info, pathMap)
+
     generator.Emit()
 
 let compressPortablePdbStream (stream: MemoryStream) =
     let compressedStream = new MemoryStream()
-    use compressionStream = new DeflateStream(compressedStream, CompressionMode.Compress,true)
+
+    use compressionStream =
+        new DeflateStream(compressedStream, CompressionMode.Compress, true)
+
     stream.WriteTo compressionStream
     compressedStream
 
-let getInfoForPortablePdb (contentId: BlobContentId) pdbfile pathMap cvChunk deterministicPdbChunk checksumPdbChunk algorithmName checksum embeddedPdb deterministic =
-    pdbGetDebugInfo (contentId.Guid.ToByteArray()) (int32 contentId.Stamp) (PathMap.apply pathMap pdbfile) cvChunk None deterministicPdbChunk checksumPdbChunk algorithmName checksum 0L None embeddedPdb deterministic
-
-let getInfoForEmbeddedPortablePdb (uncompressedLength: int64)  (contentId: BlobContentId) (compressedStream: MemoryStream) pdbfile cvChunk pdbChunk deterministicPdbChunk checksumPdbChunk algorithmName checksum deterministic =
+let getInfoForPortablePdb
+    (contentId: BlobContentId)
+    pdbfile
+    pathMap
+    cvChunk
+    deterministicPdbChunk
+    checksumPdbChunk
+    algorithmName
+    checksum
+    embeddedPdb
+    deterministic
+    =
+    pdbGetDebugInfo
+        (contentId.Guid.ToByteArray())
+        (int32 contentId.Stamp)
+        (PathMap.apply pathMap pdbfile)
+        cvChunk
+        None
+        deterministicPdbChunk
+        checksumPdbChunk
+        algorithmName
+        checksum
+        0L
+        None
+        embeddedPdb
+        deterministic
+
+let getInfoForEmbeddedPortablePdb
+    (uncompressedLength: int64)
+    (contentId: BlobContentId)
+    (compressedStream: MemoryStream)
+    pdbfile
+    cvChunk
+    pdbChunk
+    deterministicPdbChunk
+    checksumPdbChunk
+    algorithmName
+    checksum
+    deterministic
+    =
     let fn = Path.GetFileName pdbfile
-    pdbGetDebugInfo (contentId.Guid.ToByteArray()) (int32 contentId.Stamp) fn cvChunk (Some pdbChunk) deterministicPdbChunk checksumPdbChunk algorithmName checksum uncompressedLength (Some compressedStream) true deterministic
+
+    pdbGetDebugInfo
+        (contentId.Guid.ToByteArray())
+        (int32 contentId.Stamp)
+        fn
+        cvChunk
+        (Some pdbChunk)
+        deterministicPdbChunk
+        checksumPdbChunk
+        algorithmName
+        checksum
+        uncompressedLength
+        (Some compressedStream)
+        true
+        deterministic
 
 #if !FX_NO_PDB_WRITER
 
@@ -743,22 +934,29 @@ open Microsoft.Win32
 //---------------------------------------------------------------------
 let writePdbInfo showTimes outfile pdbfile info cvChunk =
 
-    try FileSystem.FileDeleteShim pdbfile with _ -> ()
+    try
+        FileSystem.FileDeleteShim pdbfile
+    with
+    | _ -> ()
 
     let pdbw =
         try
             pdbInitialize outfile pdbfile
-        with _ -> 
-            error(Error(FSComp.SR.ilwriteErrorCreatingPdb pdbfile, rangeCmdArgs))
+        with
+        | _ -> error (Error(FSComp.SR.ilwriteErrorCreatingPdb pdbfile, rangeCmdArgs))
 
     match info.EntryPoint with
     | None -> ()
     | Some x -> pdbSetUserEntryPoint pdbw x
 
     let docs = info.Documents |> Array.map (fun doc -> pdbDefineDocument pdbw doc.File)
+
     let getDocument i =
-      if i < 0 || i > docs.Length then failwith "getDocument: bad doc number"
-      docs.[i]
+        if i < 0 || i > docs.Length then
+            failwith "getDocument: bad doc number"
+
+        docs.[i]
+
     reportTime showTimes (sprintf "PDB: Defined %d documents" info.Documents.Length)
     Array.sortInPlaceBy (fun x -> x.MethToken) info.Methods
     reportTime showTimes (sprintf "PDB: Sorted %d methods" info.Methods.Length)
@@ -767,81 +965,101 @@ let writePdbInfo showTimes outfile pdbfile info cvChunk =
     let allSps = Array.collect (fun x -> x.DebugPoints) info.Methods |> Array.indexed
 
     let mutable spOffset = 0
-    info.Methods |> Array.iteri (fun i minfo ->
-
-          let sps = Array.sub allSps spOffset spCounts.[i]
-          spOffset <- spOffset + spCounts.[i]
-          begin match minfo.DebugRange with
-          | None -> ()
-          | Some (a,b) ->
-              pdbOpenMethod pdbw minfo.MethToken
-
-              pdbSetMethodRange pdbw
-                (getDocument a.Document) a.Line a.Column
-                (getDocument b.Document) b.Line b.Column
-
-              // Partition the sequence points by document
-              let spsets =
-                  let res = Dictionary()
-                  for (_,sp) in sps do
-                      let k = sp.Document
-                      match res.TryGetValue(k) with
-                      | true, xsR ->
-                          xsR.Value <- sp :: xsR.Value
-                      | _ ->
-                          res.[k] <- ref [sp]
-
-                  res
-
-              spsets
-              |> Seq.iter (fun (KeyValue(_, vref)) ->
-                  let spset = vref.Value
-                  if not spset.IsEmpty then
-                    let spset = Array.ofList spset
-                    Array.sortInPlaceWith SequencePoint.orderByOffset spset
-                    let sps =
-                        spset |> Array.map (fun sp ->
-                            // Ildiag.dprintf "token 0x%08lx has an sp at offset 0x%08x\n" minfo.MethToken sp.Offset
-                            (sp.Offset, sp.Line, sp.Column,sp.EndLine, sp.EndColumn))
-                    // Use of alloca in implementation of pdbDefineSequencePoints can give stack overflow here
-                    if sps.Length < 5000 then
-                        pdbDefineSequencePoints pdbw (getDocument spset.[0].Document) sps)
-
-              // Avoid stack overflow when writing linearly nested scopes
-              let stackGuard = StackGuard(100)
-              // Write the scopes
-              let rec writePdbScope parent sco =
-                  stackGuard.Guard <| fun () ->
-                  if parent = None || sco.Locals.Length <> 0 || sco.Children.Length <> 0 then
-                      // Only nest scopes if the child scope is a different size from
-                      let nested =
-                          match parent with
-                          | Some p -> sco.StartOffset <> p.StartOffset || sco.EndOffset <> p.EndOffset
-                          | None -> true
-                      if nested then pdbOpenScope pdbw sco.StartOffset
-                      sco.Locals |> Array.iter (fun v -> pdbDefineLocalVariable pdbw v.Name v.Signature v.Index)
-                      sco.Children |> Array.iter (writePdbScope (if nested then Some sco else parent))
-                      if nested then pdbCloseScope pdbw sco.EndOffset
-
-              match minfo.RootScope with
-              | None -> ()
-              | Some rootscope -> writePdbScope None rootscope
-              pdbCloseMethod pdbw
-          end)
+
+    info.Methods
+    |> Array.iteri (fun i minfo ->
+
+        let sps = Array.sub allSps spOffset spCounts.[i]
+        spOffset <- spOffset + spCounts.[i]
+
+        (match minfo.DebugRange with
+         | None -> ()
+         | Some (a, b) ->
+             pdbOpenMethod pdbw minfo.MethToken
+
+             pdbSetMethodRange pdbw (getDocument a.Document) a.Line a.Column (getDocument b.Document) b.Line b.Column
+
+             // Partition the sequence points by document
+             let spsets =
+                 let res = Dictionary()
+
+                 for (_, sp) in sps do
+                     let k = sp.Document
+
+                     match res.TryGetValue(k) with
+                     | true, xsR -> xsR.Value <- sp :: xsR.Value
+                     | _ -> res.[k] <- ref [ sp ]
+
+                 res
+
+             spsets
+             |> Seq.iter (fun (KeyValue (_, vref)) ->
+                 let spset = vref.Value
+
+                 if not spset.IsEmpty then
+                     let spset = Array.ofList spset
+                     Array.sortInPlaceWith SequencePoint.orderByOffset spset
+
+                     let sps =
+                         spset
+                         |> Array.map (fun sp ->
+                             // Ildiag.dprintf "token 0x%08lx has an sp at offset 0x%08x\n" minfo.MethToken sp.Offset
+                             (sp.Offset, sp.Line, sp.Column, sp.EndLine, sp.EndColumn))
+                     // Use of alloca in implementation of pdbDefineSequencePoints can give stack overflow here
+                     if sps.Length < 5000 then
+                         pdbDefineSequencePoints pdbw (getDocument spset.[0].Document) sps)
+
+             // Avoid stack overflow when writing linearly nested scopes
+             let stackGuard = StackGuard(100)
+             // Write the scopes
+             let rec writePdbScope parent sco =
+                 stackGuard.Guard(fun () ->
+                     if parent = None || sco.Locals.Length <> 0 || sco.Children.Length <> 0 then
+                         // Only nest scopes if the child scope is a different size from
+                         let nested =
+                             match parent with
+                             | Some p -> sco.StartOffset <> p.StartOffset || sco.EndOffset <> p.EndOffset
+                             | None -> true
+
+                         if nested then
+                             pdbOpenScope pdbw sco.StartOffset
+
+                         sco.Locals
+                         |> Array.iter (fun v -> pdbDefineLocalVariable pdbw v.Name v.Signature v.Index)
+
+                         sco.Children |> Array.iter (writePdbScope (if nested then Some sco else parent))
+
+                         if nested then
+                             pdbCloseScope pdbw sco.EndOffset)
+
+             match minfo.RootScope with
+             | None -> ()
+             | Some rootscope -> writePdbScope None rootscope
+
+             pdbCloseMethod pdbw))
+
     reportTime showTimes "PDB: Wrote methods"
 
     let res = pdbWriteDebugInfo pdbw
-    for pdbDoc in docs do pdbCloseDocument pdbDoc
+
+    for pdbDoc in docs do
+        pdbCloseDocument pdbDoc
+
     pdbClose pdbw outfile pdbfile
 
     reportTime showTimes "PDB: Closed"
-    [| { iddCharacteristics = res.iddCharacteristics
-         iddMajorVersion = res.iddMajorVersion
-         iddMinorVersion = res.iddMinorVersion
-         iddType = res.iddType
-         iddTimestamp = info.Timestamp
-         iddData = res.iddData
-         iddChunk = cvChunk } |]
+
+    [|
+        {
+            iddCharacteristics = res.iddCharacteristics
+            iddMajorVersion = res.iddMajorVersion
+            iddMinorVersion = res.iddMinorVersion
+            iddType = res.iddType
+            iddTimestamp = info.Timestamp
+            iddData = res.iddData
+            iddChunk = cvChunk
+        }
+    |]
 #endif
 
 #if ENABLE_MONO_SUPPORT
@@ -858,23 +1076,32 @@ open Microsoft.FSharp.Reflection
 //   obj?Foo(1, "a")  // call with two arguments (extracted from tuple)
 // NOTE: This doesn't actually handle all overloads.  It just picks first entry with right
 // number of arguments.
-let (?) this memb (args:'Args) : 'R =
+let (?) this memb (args: 'Args) : 'R =
     // Get array of 'obj' arguments for the reflection call
     let args =
-        if typeof<'Args> = typeof then [| |]
-        elif FSharpType.IsTuple typeof<'Args> then FSharpValue.GetTupleFields args
-        else [| box args |]
+        if typeof<'Args> = typeof then
+            [||]
+        elif FSharpType.IsTuple typeof<'Args> then
+            FSharpValue.GetTupleFields args
+        else
+            [| box args |]
 
     // Get methods and perform overload resolution
     let methods = this.GetType().GetMethods()
-    let bestMatch = methods |> Array.tryFind (fun mi -> mi.Name = memb && mi.GetParameters().Length = args.Length)
+
+    let bestMatch =
+        methods
+        |> Array.tryFind (fun mi -> mi.Name = memb && mi.GetParameters().Length = args.Length)
+
     match bestMatch with
-    | Some mi -> unbox(mi.Invoke(this, args))
-    | None -> error(Error(FSComp.SR.ilwriteMDBMemberMissing memb, rangeCmdArgs))
+    | Some mi -> unbox (mi.Invoke(this, args))
+    | None -> error (Error(FSComp.SR.ilwriteMDBMemberMissing memb, rangeCmdArgs))
 
 // Creating instances of needed classes from 'Mono.CompilerServices.SymbolWriter' assembly
 
-let monoCompilerSvc = AssemblyName("Mono.CompilerServices.SymbolWriter, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756")
+let monoCompilerSvc =
+    AssemblyName("Mono.CompilerServices.SymbolWriter, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756")
+
 let ctor (asmName: AssemblyName) clsName (args: obj[]) =
     let asm = Assembly.Load asmName
     let ty = asm.GetType clsName
@@ -891,59 +1118,72 @@ let createWriter (f: string) =
 //---------------------------------------------------------------------
 let writeMdbInfo fmdb f info =
     // Note, if we can't delete it code will fail later
-    try FileSystem.FileDeleteShim fmdb with _ -> ()
+    try
+        FileSystem.FileDeleteShim fmdb
+    with
+    | _ -> ()
 
     // Try loading the MDB symbol writer from an assembly available on Mono dynamically
     // Report an error if the assembly is not available.
     let wr =
         try
             createWriter f
-        with _ ->
-            error(Error(FSComp.SR.ilwriteErrorCreatingMdb(), rangeCmdArgs))
+        with
+        | _ -> error (Error(FSComp.SR.ilwriteErrorCreatingMdb (), rangeCmdArgs))
 
     // NOTE: MonoSymbolWriter doesn't need information about entrypoints, so 'info.EntryPoint' is unused here.
     // Write information about Documents. Returns '(SourceFileEntry*CompileUnitEntry)[]'
     let docs =
-        [| for doc in info.Documents do
-             let doc = wr?DefineDocument(doc.File)
-             let unit = wr?DefineCompilationUnit doc
-             yield doc, unit |]
+        [|
+            for doc in info.Documents do
+                let doc = wr?DefineDocument (doc.File)
+                let unit = wr?DefineCompilationUnit doc
+                yield doc, unit
+        |]
 
     let getDocument i =
-        if i < 0 || i >= Array.length docs then failwith "getDocument: bad doc number" else docs[i]
+        if i < 0 || i >= Array.length docs then
+            failwith "getDocument: bad doc number"
+        else
+            docs[i]
 
     // Sort methods and write them to the MDB file
     Array.sortInPlaceBy (fun x -> x.MethToken) info.Methods
+
     for meth in info.Methods do
         // Creates an instance of 'SourceMethodImpl' which is a private class that implements 'IMethodDef' interface
         // We need this as an argument to 'OpenMethod' below. Using private class is ugly, but since we don't reference
         // the assembly, the only way to implement 'IMethodDef' interface would be dynamically using Reflection.Emit...
         let sm = createSourceMethodImpl meth.MethName meth.MethToken 0
+
         match meth.DebugRange with
-        | Some(mstart, _) ->
+        | Some (mstart, _) ->
             // NOTE: 'meth.Params' is not needed, Mono debugger apparently reads this from meta-data
             let _, cue = getDocument mstart.Document
-            wr?OpenMethod(cue, 0, sm) |> ignore
+            wr?OpenMethod (cue, 0, sm) |> ignore
 
             // Write sequence points
             for sp in meth.DebugPoints do
-                wr?MarkSequencePoint(sp.Offset, cue?get_SourceFile(), sp.Line, sp.Column, false)
+                wr?MarkSequencePoint (sp.Offset, cue?get_SourceFile (), sp.Line, sp.Column, false)
 
             // Walk through the tree of scopes and write all variables
             let rec writeScope (scope: PdbMethodScope) =
-                wr?OpenScope(scope.StartOffset) |> ignore
+                wr?OpenScope (scope.StartOffset) |> ignore
+
                 for local in scope.Locals do
-                    wr?DefineLocalVariable(local.Index, local.Name)
+                    wr?DefineLocalVariable (local.Index, local.Name)
+
                 for child in scope.Children do
                     writeScope child
-                wr?CloseScope(scope.EndOffset)
+
+                wr?CloseScope (scope.EndOffset)
+
             match meth.RootScope with
             | None -> ()
             | Some rootscope -> writeScope rootscope
 
-
             // Finished generating debug information for the curretn method
-            wr?CloseMethod()
+            wr?CloseMethod ()
         | _ -> ()
 
     // Finalize - MDB requires the MVID of the generated .NET module
@@ -961,98 +1201,125 @@ let logDebugInfo (outfile: string) (info: PdbData) =
 
     fprintfn sw "ENTRYPOINT\r\n  %b\r\n" info.EntryPoint.IsSome
     fprintfn sw "DOCUMENTS"
-    for i, doc in Seq.zip [0 .. info.Documents.Length-1] info.Documents do
-      // File names elided because they are ephemeral during testing
-      fprintfn sw " [%d] "  i // doc.File
-      fprintfn sw "     Type: %A" doc.DocumentType
-      fprintfn sw "     Language: %A" doc.Language
-      fprintfn sw "     Vendor: %A" doc.Vendor
+
+    for i, doc in Seq.zip [ 0 .. info.Documents.Length - 1 ] info.Documents do
+        // File names elided because they are ephemeral during testing
+        fprintfn sw " [%d] " i // doc.File
+        fprintfn sw "     Type: %A" doc.DocumentType
+        fprintfn sw "     Language: %A" doc.Language
+        fprintfn sw "     Vendor: %A" doc.Vendor
 
     // Sort methods (because they are sorted in PDBs/MDBs too)
     fprintfn sw "\r\nMETHODS"
     Array.sortInPlaceBy (fun x -> x.MethToken) info.Methods
+
     for meth in info.Methods do
-      fprintfn sw " %s" meth.MethName
-      fprintfn sw "     Params: %A" [ for p in meth.Params -> sprintf "%d: %s" p.Index p.Name ]
-      fprintfn sw "     Range: %A" (meth.DebugRange |> Option.map (fun (f, t) ->
-                                      sprintf "[%d,%d:%d] - [%d,%d:%d]" f.Document f.Line f.Column t.Document t.Line t.Column))
-      fprintfn sw "     Points:"
+        fprintfn sw " %s" meth.MethName
+        fprintfn sw "     Params: %A" [ for p in meth.Params -> sprintf "%d: %s" p.Index p.Name ]
+
+        fprintfn
+            sw
+            "     Range: %A"
+            (meth.DebugRange
+             |> Option.map (fun (f, t) -> sprintf "[%d,%d:%d] - [%d,%d:%d]" f.Document f.Line f.Column t.Document t.Line t.Column))
+
+        fprintfn sw "     Points:"
 
-      for sp in meth.DebugPoints do
-        fprintfn sw "      - Doc: %d Offset:%d [%d:%d]-[%d-%d]" sp.Document sp.Offset sp.Line sp.Column sp.EndLine sp.EndColumn
+        for sp in meth.DebugPoints do
+            fprintfn sw "      - Doc: %d Offset:%d [%d:%d]-[%d-%d]" sp.Document sp.Offset sp.Line sp.Column sp.EndLine sp.EndColumn
 
-      // Walk through the tree of scopes and write all variables
-      fprintfn sw "     Scopes:"
-      let rec writeScope offs (scope: PdbMethodScope) =
-        fprintfn sw "      %s- [%d-%d]" offs scope.StartOffset scope.EndOffset
-        if scope.Locals.Length > 0 then
-          fprintfn sw "      %s  Locals: %A" offs [ for p in scope.Locals -> sprintf "%d: %s" p.Index p.Name ]
+        // Walk through the tree of scopes and write all variables
+        fprintfn sw "     Scopes:"
 
-        for child in scope.Children do writeScope (offs + "  ") child
+        let rec writeScope offs (scope: PdbMethodScope) =
+            fprintfn sw "      %s- [%d-%d]" offs scope.StartOffset scope.EndOffset
 
-      match meth.RootScope with
-      | None -> ()
-      | Some rootscope -> writeScope "" rootscope
-      fprintfn sw ""
+            if scope.Locals.Length > 0 then
+                fprintfn sw "      %s  Locals: %A" offs [ for p in scope.Locals -> sprintf "%d: %s" p.Index p.Name ]
+
+            for child in scope.Children do
+                writeScope (offs + "  ") child
+
+        match meth.RootScope with
+        | None -> ()
+        | Some rootscope -> writeScope "" rootscope
+
+        fprintfn sw ""
 
 let rec allNamesOfScope acc (scope: PdbMethodScope) =
     let acc = (acc, scope.Locals) ||> Array.fold (fun z l -> Set.add l.Name z)
     let acc = (acc, scope.Children) ||> allNamesOfScopes
     acc
+
 and allNamesOfScopes acc (scopes: PdbMethodScope[]) =
     (acc, scopes) ||> Array.fold allNamesOfScope
 
 let rec pushShadowedLocals (stackGuard: StackGuard) (localsToPush: PdbLocalVar[]) (scope: PdbMethodScope) =
-    stackGuard.Guard <| fun () ->
-    // Check if child scopes are properly nested
-    if scope.Children |> Array.forall (fun child ->
-            child.StartOffset >= scope.StartOffset && child.EndOffset <= scope.EndOffset) then
-
-        let children = scope.Children |> Array.sortWith scopeSorter
-
-        // Find all the names defined in this scope
-        let scopeNames = set [| for n in scope.Locals -> n.Name |]
-
-        // Rename if necessary as we push
-        let rename, unprocessed = localsToPush |> Array.partition (fun l -> scopeNames.Contains l.Name)
-        let renamed = [| for l in rename -> { l with Name = l.Name + " (shadowed)" } |]
-
-        let localsToPush2 = [| yield! renamed; yield! unprocessed; yield! scope.Locals |]
-        let newChildren, splits = children |> Array.map (pushShadowedLocals stackGuard localsToPush2) |> Array.unzip
-        
-        // Check if a rename in any of the children forces a split
-        if splits |> Array.exists id then
-            let results =
-                [| 
-                    // First fill in the gaps between the children with an adjusted version of this scope.
-                    let gaps = 
-                        [| yield (scope.StartOffset, scope.StartOffset) 
-                           for newChild in children do   
-                                yield (newChild.StartOffset, newChild.EndOffset)
-                           yield (scope.EndOffset, scope.EndOffset)  |]
-
-                    for ((_,a),(b,_)) in Array.pairwise gaps do 
-                        if a < b then
-                            yield { scope with Locals=localsToPush2; Children = [| |]; StartOffset = a; EndOffset = b}
-                       
-                    yield! Array.concat newChildren
-                |]
-            let results2 = results |> Array.sortWith scopeSorter
-            results2, true
-        else 
-            let splitsParent = renamed.Length > 0
-            [| { scope with Locals=localsToPush2 } |], splitsParent
-    else
-        [| scope |], false
+    stackGuard.Guard(fun () ->
+        // Check if child scopes are properly nested
+        if scope.Children
+           |> Array.forall (fun child -> child.StartOffset >= scope.StartOffset && child.EndOffset <= scope.EndOffset) then
+
+            let children = scope.Children |> Array.sortWith scopeSorter
+
+            // Find all the names defined in this scope
+            let scopeNames = set [| for n in scope.Locals -> n.Name |]
+
+            // Rename if necessary as we push
+            let rename, unprocessed =
+                localsToPush |> Array.partition (fun l -> scopeNames.Contains l.Name)
+
+            let renamed = [| for l in rename -> { l with Name = l.Name + " (shadowed)" } |]
+
+            let localsToPush2 = [| yield! renamed; yield! unprocessed; yield! scope.Locals |]
+
+            let newChildren, splits =
+                children
+                |> Array.map (pushShadowedLocals stackGuard localsToPush2)
+                |> Array.unzip
+
+            // Check if a rename in any of the children forces a split
+            if splits |> Array.exists id then
+                let results =
+                    [|
+                        // First fill in the gaps between the children with an adjusted version of this scope.
+                        let gaps =
+                            [|
+                                yield (scope.StartOffset, scope.StartOffset)
+                                for newChild in children do
+                                    yield (newChild.StartOffset, newChild.EndOffset)
+                                yield (scope.EndOffset, scope.EndOffset)
+                            |]
+
+                        for ((_, a), (b, _)) in Array.pairwise gaps do
+                            if a < b then
+                                yield
+                                    { scope with
+                                        Locals = localsToPush2
+                                        Children = [||]
+                                        StartOffset = a
+                                        EndOffset = b
+                                    }
+
+                        yield! Array.concat newChildren
+                    |]
+
+                let results2 = results |> Array.sortWith scopeSorter
+                results2, true
+            else
+                let splitsParent = renamed.Length > 0
+                [| { scope with Locals = localsToPush2 } |], splitsParent
+        else
+            [| scope |], false)
 
 // Check to see if a scope has a local with the same name as any of its children
-// 
-// If so, do not emit 'scope' itself. Instead, 
+//
+// If so, do not emit 'scope' itself. Instead,
 //  1. Emit a copy of 'scope' in each true gap, with all locals
-//  2. Adjust each child scope to also contain the locals from 'scope', 
+//  2. Adjust each child scope to also contain the locals from 'scope',
 //     adding the text " (shadowed)" to the names of those with name conflicts.
 let unshadowScopes rootScope =
     // Avoid stack overflow when writing linearly nested scopes
     let stackGuard = StackGuard(100)
-    let result, _ = pushShadowedLocals stackGuard [| |] rootScope
+    let result, _ = pushShadowedLocals stackGuard [||] rootScope
     result
diff --git a/src/Compiler/AbstractIL/ilx.fs b/src/Compiler/AbstractIL/ilx.fs
index 8fb986a0755..5ef632ba2a5 100644
--- a/src/Compiler/AbstractIL/ilx.fs
+++ b/src/Compiler/AbstractIL/ilx.fs
@@ -3,13 +3,17 @@
 /// Defines an extension of the IL algebra
 module internal FSharp.Compiler.AbstractIL.ILX.Types
 
-open FSharp.Compiler.AbstractIL.IL 
-open Internal.Utilities.Library 
+open FSharp.Compiler.AbstractIL.IL
+open Internal.Utilities.Library
 
 let mkLowerName (nm: string) =
     // Use the lower case name of a field or constructor as the field/parameter name if it differs from the uppercase name
     let lowerName = String.uncapitalize nm
-    if lowerName = nm then "_" + nm else lowerName
+
+    if lowerName = nm then
+        "_" + nm
+    else
+        lowerName
 
 []
 type IlxUnionCaseField(fd: ILFieldDef) =
@@ -18,148 +22,169 @@ type IlxUnionCaseField(fd: ILFieldDef) =
     member x.Type = x.ILField.FieldType
     member x.Name = x.ILField.Name
     member x.LowerName = lowerName
-    
-type IlxUnionCase = 
-    { altName: string
-      altFields: IlxUnionCaseField[]
-      altCustomAttrs: ILAttributes }
+
+type IlxUnionCase =
+    {
+        altName: string
+        altFields: IlxUnionCaseField[]
+        altCustomAttrs: ILAttributes
+    }
 
     member x.FieldDefs = x.altFields
     member x.FieldDef n = x.altFields[n]
     member x.Name = x.altName
-    member x.IsNullary  = (x.FieldDefs.Length = 0)
-    member x.FieldTypes = x.FieldDefs |> Array.map (fun fd -> fd.Type) 
-
-type IlxUnionHasHelpers = 
-   | NoHelpers
-   | AllHelpers
-   | SpecialFSharpListHelpers 
-   | SpecialFSharpOptionHelpers 
-   
-type IlxUnionRef = 
-    | IlxUnionRef of boxity: ILBoxity * ILTypeRef * IlxUnionCase[] * bool * (* hasHelpers: *) IlxUnionHasHelpers 
-
-type IlxUnionSpec = 
+    member x.IsNullary = (x.FieldDefs.Length = 0)
+    member x.FieldTypes = x.FieldDefs |> Array.map (fun fd -> fd.Type)
+
+type IlxUnionHasHelpers =
+    | NoHelpers
+    | AllHelpers
+    | SpecialFSharpListHelpers
+    | SpecialFSharpOptionHelpers
+
+type IlxUnionRef = IlxUnionRef of boxity: ILBoxity * ILTypeRef * IlxUnionCase[] * bool (* hasHelpers: *)  * IlxUnionHasHelpers
+
+type IlxUnionSpec =
     | IlxUnionSpec of IlxUnionRef * ILGenericArgs
-    member x.DeclaringType = let (IlxUnionSpec(IlxUnionRef(bx, tref, _, _, _), inst)) = x in mkILNamedTy bx tref inst
-    member x.Boxity = let (IlxUnionSpec(IlxUnionRef(bx, _, _, _, _), _)) = x in bx 
-    member x.TypeRef = let (IlxUnionSpec(IlxUnionRef(_, tref, _, _, _), _)) = x in tref
-    member x.GenericArgs = let (IlxUnionSpec(_, inst)) = x in inst
-    member x.AlternativesArray = let (IlxUnionSpec(IlxUnionRef(_, _, alts, _, _), _)) = x in alts
-    member x.IsNullPermitted = let (IlxUnionSpec(IlxUnionRef(_, _, _, np, _), _)) = x in np
-    member x.HasHelpers = let (IlxUnionSpec(IlxUnionRef(_, _, _, _, b), _)) = x in b
+
+    member x.DeclaringType =
+        let (IlxUnionSpec (IlxUnionRef (bx, tref, _, _, _), inst)) = x in mkILNamedTy bx tref inst
+
+    member x.Boxity = let (IlxUnionSpec (IlxUnionRef (bx, _, _, _, _), _)) = x in bx
+    member x.TypeRef = let (IlxUnionSpec (IlxUnionRef (_, tref, _, _, _), _)) = x in tref
+    member x.GenericArgs = let (IlxUnionSpec (_, inst)) = x in inst
+
+    member x.AlternativesArray =
+        let (IlxUnionSpec (IlxUnionRef (_, _, alts, _, _), _)) = x in alts
+
+    member x.IsNullPermitted =
+        let (IlxUnionSpec (IlxUnionRef (_, _, _, np, _), _)) = x in np
+
+    member x.HasHelpers = let (IlxUnionSpec (IlxUnionRef (_, _, _, _, b), _)) = x in b
     member x.Alternatives = Array.toList x.AlternativesArray
     member x.Alternative idx = x.AlternativesArray[idx]
     member x.FieldDef idx fidx = x.Alternative(idx).FieldDef(fidx)
 
-type IlxClosureLambdas = 
+type IlxClosureLambdas =
     | Lambdas_forall of ILGenericParameterDef * IlxClosureLambdas
     | Lambdas_lambda of ILParameter * IlxClosureLambdas
     | Lambdas_return of ILType
 
-type IlxClosureApps = 
-  | Apps_tyapp of ILType * IlxClosureApps 
-  | Apps_app of ILType * IlxClosureApps 
-  | Apps_done of ILType
+type IlxClosureApps =
+    | Apps_tyapp of ILType * IlxClosureApps
+    | Apps_app of ILType * IlxClosureApps
+    | Apps_done of ILType
 
 let rec instAppsAux n inst apps =
     match apps with
     | Apps_tyapp (ty, rest) -> Apps_tyapp(instILTypeAux n inst ty, instAppsAux n inst rest)
-    | Apps_app (dty, rest) ->  Apps_app(instILTypeAux n inst dty, instAppsAux n inst rest)
-    | Apps_done retTy ->  Apps_done(instILTypeAux n inst retTy)
+    | Apps_app (dty, rest) -> Apps_app(instILTypeAux n inst dty, instAppsAux n inst rest)
+    | Apps_done retTy -> Apps_done(instILTypeAux n inst retTy)
 
 let rec instLambdasAux n inst lambdas =
     match lambdas with
-    | Lambdas_forall (gpdef, bodyTy) -> 
-        Lambdas_forall(gpdef, instLambdasAux n inst bodyTy)
-    | Lambdas_lambda (pdef, bodyTy) ->  
-        Lambdas_lambda({ pdef with Type=instILTypeAux n inst pdef.Type}, instLambdasAux n inst bodyTy)
-    | Lambdas_return retTy ->  Lambdas_return(instILTypeAux n inst retTy)
+    | Lambdas_forall (gpdef, bodyTy) -> Lambdas_forall(gpdef, instLambdasAux n inst bodyTy)
+    | Lambdas_lambda (pdef, bodyTy) ->
+        Lambdas_lambda(
+            { pdef with
+                Type = instILTypeAux n inst pdef.Type
+            },
+            instLambdasAux n inst bodyTy
+        )
+    | Lambdas_return retTy -> Lambdas_return(instILTypeAux n inst retTy)
 
 let instLambdas i t = instLambdasAux 0 i t
 
-type IlxClosureFreeVar = 
-    { fvName: string  
-      fvCompilerGenerated:bool 
-      fvType: ILType }
+type IlxClosureFreeVar =
+    {
+        fvName: string
+        fvCompilerGenerated: bool
+        fvType: ILType
+    }
 
-let mkILFreeVar (name, compgen, ty) = 
-    { fvName=name
-      fvCompilerGenerated=compgen
-      fvType=ty }
+let mkILFreeVar (name, compgen, ty) =
+    {
+        fvName = name
+        fvCompilerGenerated = compgen
+        fvType = ty
+    }
 
-type IlxClosureRef = 
-    | IlxClosureRef of ILTypeRef * IlxClosureLambdas * IlxClosureFreeVar[]
-    
-type IlxClosureSpec = 
+type IlxClosureRef = IlxClosureRef of ILTypeRef * IlxClosureLambdas * IlxClosureFreeVar[]
+
+type IlxClosureSpec =
     | IlxClosureSpec of IlxClosureRef * ILGenericArgs * ILType * useStaticField: bool
 
-    member x.TypeRef = let (IlxClosureRef(tref, _, _)) = x.ClosureRef in tref
+    member x.TypeRef = let (IlxClosureRef (tref, _, _)) = x.ClosureRef in tref
 
-    member x.ILType = let (IlxClosureSpec(_, _, ty, _)) = x in ty
+    member x.ILType = let (IlxClosureSpec (_, _, ty, _)) = x in ty
 
-    member x.ClosureRef = let (IlxClosureSpec(cloref, _, _, _)) = x in cloref 
+    member x.ClosureRef = let (IlxClosureSpec (cloref, _, _, _)) = x in cloref
 
-    member x.FormalFreeVars = let (IlxClosureRef(_, _, fvs)) = x.ClosureRef in fvs
+    member x.FormalFreeVars = let (IlxClosureRef (_, _, fvs)) = x.ClosureRef in fvs
 
-    member x.FormalLambdas = let (IlxClosureRef(_, lambdas, _)) = x.ClosureRef in lambdas
+    member x.FormalLambdas = let (IlxClosureRef (_, lambdas, _)) = x.ClosureRef in lambdas
 
-    member x.GenericArgs = let (IlxClosureSpec(_, inst, _, _)) = x in inst
+    member x.GenericArgs = let (IlxClosureSpec (_, inst, _, _)) = x in inst
 
-    static member Create (cloref, inst, useStaticField) = 
-        let (IlxClosureRef(tref, _, _)) = cloref
+    static member Create(cloref, inst, useStaticField) =
+        let (IlxClosureRef (tref, _, _)) = cloref
         IlxClosureSpec(cloref, inst, mkILBoxedType (mkILTySpec (tref, inst)), useStaticField)
 
-    member x.Constructor = 
+    member x.Constructor =
         let cloTy = x.ILType
         let fields = x.FormalFreeVars
         mkILCtorMethSpecForTy (cloTy, fields |> Array.map (fun fv -> fv.fvType) |> Array.toList)
 
-    member x.UseStaticField = 
-        let (IlxClosureSpec(_, _, _, useStaticField)) = x
+    member x.UseStaticField =
+        let (IlxClosureSpec (_, _, _, useStaticField)) = x
         useStaticField
 
-    member x.GetStaticFieldSpec() = 
+    member x.GetStaticFieldSpec() =
         assert x.UseStaticField
         let formalCloTy = mkILFormalBoxedTy x.TypeRef (mkILFormalTypars x.GenericArgs)
         mkILFieldSpecInTy (x.ILType, "@_instance", formalCloTy)
 
 // Define an extension of the IL algebra of type definitions
-type IlxClosureInfo = 
-    { cloStructure: IlxClosureLambdas
-      cloFreeVars: IlxClosureFreeVar[]  
-      cloCode: Lazy
-      cloUseStaticField: bool}
+type IlxClosureInfo =
+    {
+        cloStructure: IlxClosureLambdas
+        cloFreeVars: IlxClosureFreeVar[]
+        cloCode: Lazy
+        cloUseStaticField: bool
+    }
 
-type IlxUnionInfo = 
-    { 
-      UnionCasesAccessibility: ILMemberAccess 
+type IlxUnionInfo =
+    {
+        UnionCasesAccessibility: ILMemberAccess
 
-      HelpersAccessibility: ILMemberAccess 
+        HelpersAccessibility: ILMemberAccess
 
-      HasHelpers: IlxUnionHasHelpers 
+        HasHelpers: IlxUnionHasHelpers
 
-      GenerateDebugProxies: bool 
+        GenerateDebugProxies: bool
 
-      DebugDisplayAttributes: ILAttribute list
+        DebugDisplayAttributes: ILAttribute list
 
-      UnionCases: IlxUnionCase[]
+        UnionCases: IlxUnionCase[]
 
-      IsNullPermitted: bool
+        IsNullPermitted: bool
 
-      DebugPoint: ILDebugPoint option
+        DebugPoint: ILDebugPoint option
 
-      DebugImports: ILDebugImports option
-     }
+        DebugImports: ILDebugImports option
+    }
 
 // --------------------------------------------------------------------
 // Define these as extensions of the IL types
-// -------------------------------------------------------------------- 
-
-let destTyFuncApp = function Apps_tyapp (b, c) -> b, c | _ -> failwith "destTyFuncApp"
+// --------------------------------------------------------------------
 
-let mkILFormalCloRef gparams csig useStaticField = IlxClosureSpec.Create(csig, mkILFormalGenericArgs 0 gparams, useStaticField)
+let destTyFuncApp =
+    function
+    | Apps_tyapp (b, c) -> b, c
+    | _ -> failwith "destTyFuncApp"
 
-let actualTypOfIlxUnionField (cuspec : IlxUnionSpec) idx fidx =
-  instILType cuspec.GenericArgs (cuspec.FieldDef idx fidx).Type
+let mkILFormalCloRef gparams csig useStaticField =
+    IlxClosureSpec.Create(csig, mkILFormalGenericArgs 0 gparams, useStaticField)
 
+let actualTypOfIlxUnionField (cuspec: IlxUnionSpec) idx fidx =
+    instILType cuspec.GenericArgs (cuspec.FieldDef idx fidx).Type
diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs
index 773d1a01ba5..0d03bfead85 100644
--- a/src/Compiler/Checking/CheckExpressions.fs
+++ b/src/Compiler/Checking/CheckExpressions.fs
@@ -1947,9 +1947,13 @@ let MakeAndPublishSimpleValsForMergedScope (cenv: cenv) env m (names: NameMap<_>
                             notifyNameResolution (pos, item, itemGroup, itemTyparInst, occurence, nenv, ad, m, replacing)
 
                         member _.NotifyExprHasType(_, _, _, _) = assert false // no expr typings in MakeAndPublishSimpleVals
+
                         member _.NotifyFormatSpecifierLocation(_, _) = ()
+
                         member _.NotifyOpenDeclaration _ = ()
+
                         member _.CurrentSourceText = None
+
                         member _.FormatStringCheckContext = None }
 
                 use _h = WithNewTypecheckResultsSink(sink, cenv.tcSink)
diff --git a/src/Compiler/Service/ServiceLexing.fsi b/src/Compiler/Service/ServiceLexing.fsi
index 5e2e5a9732d..df1dfdde294 100755
--- a/src/Compiler/Service/ServiceLexing.fsi
+++ b/src/Compiler/Service/ServiceLexing.fsi
@@ -6,6 +6,7 @@ open System
 open System.Threading
 open FSharp.Compiler
 open FSharp.Compiler.Text
+
 #nowarn "57"
 
 /// Represents encoded information for the end-of-line continuation of lexing

From 9d6c92a30d8dd5b45696763132dccf5f3e40f0d8 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Wed, 25 May 2022 14:23:30 +0100
Subject: [PATCH 15/20] Emit all documents from command line in debug info
 (#13185)

* emit all documents in debug info

* add tests

* reduce diff
---
 src/Compiler/AbstractIL/ilwrite.fs            | 288 +++++++++---------
 src/Compiler/AbstractIL/ilwrite.fsi           |   1 +
 src/Compiler/Driver/fsc.fs                    |  26 +-
 src/Compiler/Interactive/fsi.fs               |   2 +
 .../Debugger/PortablePdbs.fs                  |  34 +++
 tests/FSharp.Test.Utilities/Compiler.fs       |  26 +-
 6 files changed, 215 insertions(+), 162 deletions(-)

diff --git a/src/Compiler/AbstractIL/ilwrite.fs b/src/Compiler/AbstractIL/ilwrite.fs
index b0e77e253f7..988e21c1d13 100644
--- a/src/Compiler/AbstractIL/ilwrite.fs
+++ b/src/Compiler/AbstractIL/ilwrite.fs
@@ -574,7 +574,6 @@ type cenv =
     }
     member cenv.GetTable (tab: TableName) = cenv.tables[tab.Index]
 
-
     member cenv.AddCode ((reqdStringFixupsOffset, requiredStringFixups), code) =
         if align 4 cenv.nextCodeAddr <> cenv.nextCodeAddr then dprintn "warning: code not 4-byte aligned"
         cenv.requiredStringFixups <- (cenv.nextCodeAddr + reqdStringFixupsOffset, requiredStringFixups) :: cenv.requiredStringFixups
@@ -583,6 +582,10 @@ type cenv =
 
     member cenv.GetCode() = cenv.codeChunks.AsMemory().ToArray()
 
+    member cenv.EmitDebugDocument (doc: ILSourceDocument) =
+        if cenv.generatePdb then
+          cenv.documents.FindOrAddSharedEntry doc |> ignore
+
     override x.ToString() = ""
 
     interface IDisposable with
@@ -3041,7 +3044,21 @@ let DataCapacity = 200
 []
 let ResourceCapacity = 200
 
-let generateIL requiredDataFixups (desiredMetadataVersion, generatePdb, ilg : ILGlobals, emitTailcalls, deterministic, showTimes, referenceAssemblyOnly, referenceAssemblyAttribOpt: ILAttribute option) (m : ILModuleDef) cilStartAddress normalizeAssemblyRefs =
+let generateIL (
+    requiredDataFixups,
+    desiredMetadataVersion,
+    generatePdb,
+    ilg: ILGlobals,
+    emitTailcalls,
+    deterministic,
+    showTimes,
+    referenceAssemblyOnly,
+    referenceAssemblyAttribOpt: ILAttribute option,
+    allGivenSources,
+    m: ILModuleDef,
+    cilStartAddress,
+    normalizeAssemblyRefs) =
+
     let isDll = m.IsDLL
 
     let hasInternalsVisibleToAttrib =
@@ -3113,6 +3130,9 @@ let generateIL requiredDataFixups (desiredMetadataVersion, generatePdb, ilg : IL
     // Now the main compilation step
     GenModule cenv m
 
+    for doc in allGivenSources do
+        cenv.EmitDebugDocument(doc)
+
     // .exe files have a .entrypoint instruction. Do not write it to the entrypoint when writing dll.
     let entryPointToken =
         match cenv.entrypoint with
@@ -3209,7 +3229,21 @@ let TableCapacity = 20000
 []
 let MetadataCapacity = 500000
 
-let writeILMetadataAndCode (generatePdb, desiredMetadataVersion, ilg, emitTailcalls, deterministic, showTimes, referenceAssemblyOnly, referenceAssemblyAttribOpt) modul cilStartAddress normalizeAssemblyRefs =
+let writeILMetadataAndCode (
+    generatePdb,
+    desiredMetadataVersion,
+    ilg,
+    emitTailcalls,
+    deterministic,
+    showTimes,
+    referenceAssemblyOnly,
+    referenceAssemblyAttribOpt,
+    allGivenSources,
+    modul,
+    cilStartAddress,
+    normalizeAssemblyRefs
+) =
+
     // When we know the real RVAs of the data section we fixup the references for the FieldRVA table.
     // These references are stored as offsets into the metadata we return from this function
     let requiredDataFixups = ref []
@@ -3217,7 +3251,20 @@ let writeILMetadataAndCode (generatePdb, desiredMetadataVersion, ilg, emitTailca
     let next = cilStartAddress
 
     let strings, userStrings, blobs, guids, tables, entryPointToken, code, requiredStringFixups, data, resources, pdbData, mappings =
-      generateIL requiredDataFixups (desiredMetadataVersion, generatePdb, ilg, emitTailcalls, deterministic, showTimes, referenceAssemblyOnly, referenceAssemblyAttribOpt) modul cilStartAddress normalizeAssemblyRefs
+      generateIL (
+          requiredDataFixups,
+          desiredMetadataVersion,
+          generatePdb,
+          ilg,
+          emitTailcalls,
+          deterministic,
+          showTimes,
+          referenceAssemblyOnly,
+          referenceAssemblyAttribOpt,
+          allGivenSources,
+          modul,
+          cilStartAddress,
+          normalizeAssemblyRefs)
 
     reportTime showTimes "Generated Tables and Code"
     let tableSize (tab: TableName) = tables[tab.Index].Count
@@ -3764,35 +3811,39 @@ let writePdb (
     reportTime showTimes "Signing Image"
     pdbBytes
 
-let writeBinaryAux (
-    stream: Stream,
-    ilg: ILGlobals,
-    pdbfile: string option,
-    signer: ILStrongNameSigner option,
-    portablePDB,
-    embeddedPDB,
-    embedAllSource,
-    embedSourceList,
-    sourceLink,
-    checksumAlgorithm,
-    emitTailcalls,
-    deterministic,
-    showTimes,
-    referenceAssemblyOnly,
-    referenceAssemblyAttribOpt,
-    pathMap, modul, 
-    normalizeAssemblyRefs) =
+type options =
+   { ilg: ILGlobals
+     outfile: string
+     pdbfile: string option
+     portablePDB: bool
+     embeddedPDB: bool
+     embedAllSource: bool
+     embedSourceList: string list
+     allGivenSources: ILSourceDocument list
+     sourceLink: string
+     checksumAlgorithm: HashAlgorithm
+     signer: ILStrongNameSigner option
+     emitTailcalls: bool
+     deterministic: bool
+     showTimes: bool
+     dumpDebugInfo: bool
+     referenceAssemblyOnly: bool
+     referenceAssemblyAttribOpt: ILAttribute option
+     pathMap: PathMap }
+
+let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRefs) =
 
     // Store the public key from the signer into the manifest. This means it will be written
     // to the binary and also acts as an indicator to leave space for delay sign
 
-    reportTime showTimes "Write Started"
+    reportTime options.showTimes "Write Started"
     let isDll = modul.IsDLL
+    let ilg = options.ilg
 
     let signer =
-        match signer, modul.Manifest with
-        | Some _, _ -> signer
-        | _, None -> signer
+        match options.signer, modul.Manifest with
+        | Some _, _ -> options.signer
+        | _, None -> options.signer
         | None, Some {PublicKey=Some pubkey} ->
             (dprintn "Note: The output assembly will be delay-signed using the original public"
              dprintn "Note: key. In order to load it you will need to either sign it with"
@@ -3802,7 +3853,7 @@ let writeBinaryAux (
              dprintn "Note: private key when converting the assembly, assuming you have access to"
              dprintn "Note: it."
              Some (ILStrongNameSigner.OpenPublicKey pubkey))
-        | _ -> signer
+        | _ -> options.signer
 
     let modul =
         let pubkey =
@@ -3890,9 +3941,22 @@ let writeBinaryAux (
                     | None -> failwith "Expected mscorlib to have a version number"
 
           let entryPointToken, code, codePadding, metadata, data, resources, requiredDataFixups, pdbData, mappings, guidStart =
-            writeILMetadataAndCode ((pdbfile <> None), desiredMetadataVersion, ilg, emitTailcalls, deterministic, showTimes, referenceAssemblyOnly, referenceAssemblyAttribOpt) modul next normalizeAssemblyRefs
-
-          reportTime showTimes "Generated IL and metadata"
+              writeILMetadataAndCode (
+                  options.pdbfile.IsSome,
+                  desiredMetadataVersion,
+                  ilg,
+                  options.emitTailcalls,
+                  options.deterministic,
+                  options.showTimes,
+                  options.referenceAssemblyOnly,
+                  options.referenceAssemblyAttribOpt,
+                  options.allGivenSources,
+                  modul,
+                  next,
+                  normalizeAssemblyRefs
+              )
+
+          reportTime options.showTimes "Generated IL and metadata"
           let _codeChunk, next = chunk code.Length next
           let _codePaddingChunk, next = chunk codePadding.Length next
 
@@ -3924,12 +3988,12 @@ let writeBinaryAux (
           let globalpointerCodeChunk, next = chunk (if isItanium then 0x8 else 0x0) next
 
           let pdbInfoOpt =
-            match pdbfile, portablePDB with
+            match options.pdbfile, options.portablePDB with
             | Some _, true ->
                 let pdbInfo =
-                    generatePortablePdb embedAllSource embedSourceList sourceLink checksumAlgorithm showTimes pdbData pathMap
+                    generatePortablePdb options.embedAllSource options.embedSourceList options.sourceLink options.checksumAlgorithm options.showTimes pdbData options.pathMap
 
-                if embeddedPDB then
+                if options.embeddedPDB then
                     let (uncompressedLength, contentId, stream, algorithmName, checkSum) = pdbInfo
                     let compressedStream = compressPortablePdbStream stream
                     Some (uncompressedLength, contentId, compressedStream, algorithmName, checkSum)
@@ -3939,12 +4003,12 @@ let writeBinaryAux (
             | _ -> None
 
           let debugDirectoryChunk, next =
-            chunk (if pdbfile = None then
+            chunk (if options.pdbfile = None then
                         0x0
                    else
                         sizeof_IMAGE_DEBUG_DIRECTORY * 2 +
-                        (if embeddedPDB then sizeof_IMAGE_DEBUG_DIRECTORY else 0) +
-                        (if deterministic then sizeof_IMAGE_DEBUG_DIRECTORY else 0)
+                        (if options.embeddedPDB then sizeof_IMAGE_DEBUG_DIRECTORY else 0) +
+                        (if options.deterministic then sizeof_IMAGE_DEBUG_DIRECTORY else 0)
                   ) next
 
           // The debug data is given to us by the PDB writer and appears to
@@ -3953,7 +4017,7 @@ let writeBinaryAux (
           // to what PDB writers seem to require and leave extra space just in case...
           let debugDataJustInCase = 40
           let debugDataChunk, next =
-              chunk (align 0x4 (match pdbfile with
+              chunk (align 0x4 (match options.pdbfile with
                                 | None -> 0
                                 | Some f -> (24
                                             + System.Text.Encoding.Unicode.GetByteCount f // See bug 748444
@@ -3968,19 +4032,19 @@ let writeBinaryAux (
                                 | None -> 0)) next
 
           let debugEmbeddedPdbChunk, next =
-              if embeddedPDB then
+              if options.embeddedPDB then
                   let streamLength =
                       match pdbInfoOpt with
                       | Some (_, _, stream, _, _) -> int stream.Length
                       | None -> 0
-                  chunk (align 0x4 (match embeddedPDB with
+                  chunk (align 0x4 (match options.embeddedPDB with
                                      | true -> 8 + streamLength
                                      | _ -> 0 )) next
               else
                   nochunk next
 
           let debugDeterministicPdbChunk, next =
-              if deterministic then emptychunk next
+              if options.deterministic then emptychunk next
               else nochunk next
 
           let textSectionSize = next - textSectionAddr
@@ -4053,7 +4117,7 @@ let writeBinaryAux (
           let imageEndSectionPhysLoc = nextPhys
           let imageEndAddr = next
 
-          reportTime showTimes "Layout image"
+          reportTime options.showTimes "Layout image"
 
           let write p (os: BinaryWriter) chunkName chunk =
               match p with
@@ -4091,9 +4155,9 @@ let writeBinaryAux (
 
           let pdbData =
             // Hash code, data and metadata
-            if deterministic then
+            if options.deterministic then
               use sha =
-                  match checksumAlgorithm with
+                  match options.checksumAlgorithm with
                   | HashAlgorithm.Sha1 -> System.Security.Cryptography.SHA1.Create() :> System.Security.Cryptography.HashAlgorithm
                   | HashAlgorithm.Sha256 -> System.Security.Cryptography.SHA256.Create() :> System.Security.Cryptography.HashAlgorithm
 
@@ -4394,15 +4458,15 @@ let writeBinaryAux (
               write (Some (textV2P globalpointerCodeChunk.addr)) os " itanium global pointer"
                    [| 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy |]
 
-          if pdbfile.IsSome then
+          if options.pdbfile.IsSome then
               write (Some (textV2P debugDirectoryChunk.addr)) os "debug directory" (Array.create debugDirectoryChunk.size 0x0uy)
               write (Some (textV2P debugDataChunk.addr)) os "debug data" (Array.create debugDataChunk.size 0x0uy)
               write (Some (textV2P debugChecksumPdbChunk.addr)) os "debug checksum" (Array.create debugChecksumPdbChunk.size 0x0uy)
 
-          if embeddedPDB then
+          if options.embeddedPDB then
               write (Some (textV2P debugEmbeddedPdbChunk.addr)) os "debug data" (Array.create debugEmbeddedPdbChunk.size 0x0uy)
 
-          if deterministic then
+          if options.deterministic then
               write (Some (textV2P debugDeterministicPdbChunk.addr)) os "debug deterministic" Array.empty
 
           writePadding os "end of .text" (dataSectionPhysLoc - textSectionPhysLoc - textSectionSize)
@@ -4444,108 +4508,70 @@ let writeBinaryAux (
 
           pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, mappings
 
-    reportTime showTimes "Writing Image"
+    reportTime options.showTimes "Writing Image"
     pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, mappings
 
-let writeBinaryFiles (outfile,
-    ilg: ILGlobals,
-    pdbfile: string option,
-    signer: ILStrongNameSigner option,
-    portablePDB,
-    embeddedPDB,
-    embedAllSource,
-    embedSourceList,
-    sourceLink,
-    checksumAlgorithm,
-    emitTailcalls,
-    deterministic,
-    showTimes,
-    dumpDebugInfo,
-    referenceAssemblyOnly,
-    referenceAssemblyAttribOpt,
-    pathMap,
-    modul, normalizeAssemblyRefs) =
+let writeBinaryFiles (options: options, modul, normalizeAssemblyRefs) =
 
     let stream =
         try
             // Ensure the output directory exists otherwise it will fail
-            let dir = FileSystem.GetDirectoryNameShim outfile
+            let dir = FileSystem.GetDirectoryNameShim options.outfile
             if not (FileSystem.DirectoryExistsShim dir) then FileSystem.DirectoryCreateShim dir |> ignore
-            FileSystem.OpenFileForWriteShim(outfile, FileMode.Create, FileAccess.Write, FileShare.Read)
+            FileSystem.OpenFileForWriteShim(options.outfile, FileMode.Create, FileAccess.Write, FileShare.Read)
         with _ ->
-            failwith ("Could not open file for writing (binary mode): " + outfile)
+            failwith ("Could not open file for writing (binary mode): " + options.outfile)
 
     let pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, mappings =
         try
             try
-                writeBinaryAux(
-                    stream, ilg, pdbfile, signer,
-                    portablePDB, embeddedPDB, embedAllSource,
-                    embedSourceList, sourceLink,
-                    checksumAlgorithm, emitTailcalls, deterministic, showTimes,
-                    referenceAssemblyOnly,
-                    referenceAssemblyAttribOpt,
-                    pathMap,
-                    modul, normalizeAssemblyRefs)
+                writeBinaryAux(stream, options, modul, normalizeAssemblyRefs)
             finally
                 stream.Close()
 
         with _ ->
-            try FileSystem.FileDeleteShim outfile with | _ -> ()
+            try FileSystem.FileDeleteShim options.outfile with | _ -> ()
             reraise()
 
     try
-        FileSystemUtilities.setExecutablePermission outfile
+        FileSystemUtilities.setExecutablePermission options.outfile
     with _ ->
         ()
 
     let reopenOutput () =
-        FileSystem.OpenFileForWriteShim(outfile, FileMode.Open, FileAccess.Write, FileShare.Read)
+        FileSystem.OpenFileForWriteShim(options.outfile, FileMode.Open, FileAccess.Write, FileShare.Read)
 
-    writePdb (dumpDebugInfo,
-        showTimes, portablePDB,
-        embeddedPDB, pdbfile, outfile,
-        reopenOutput, false, signer, deterministic, pathMap,
+    writePdb (options.dumpDebugInfo,
+        options.showTimes, options.portablePDB,
+        options.embeddedPDB, options.pdbfile, options.outfile,
+        reopenOutput, false, options.signer, options.deterministic, options.pathMap,
         pdbData, pdbInfoOpt, debugDirectoryChunk,
         debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk,
         debugDeterministicPdbChunk, textV2P) |> ignore
 
     mappings
 
-let writeBinaryInMemory (
-    outfile: string,
-    ilg: ILGlobals,
-    pdbfile: string option,
-    signer: ILStrongNameSigner option,
-    portablePDB,
-    embeddedPDB,
-    embedAllSource,
-    embedSourceList,
-    sourceLink,
-    checksumAlgorithm,
-    emitTailcalls, deterministic,
-    showTimes,
-    dumpDebugInfo,
-    pathMap,
-    modul,
-    normalizeAssemblyRefs) =
+let writeBinaryInMemory (options: options, modul, normalizeAssemblyRefs) =
 
     let stream = new MemoryStream()
+    let options = { options with referenceAssemblyOnly = false; referenceAssemblyAttribOpt = None }
     let pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, _mappings =
-        writeBinaryAux(stream, ilg,
-            pdbfile, signer,
-            portablePDB, embeddedPDB, embedAllSource,
-            embedSourceList, sourceLink,
-            checksumAlgorithm, emitTailcalls,
-            deterministic, showTimes, false, None, pathMap, modul, normalizeAssemblyRefs)
+        writeBinaryAux(stream, options, modul, normalizeAssemblyRefs)
 
     let reopenOutput () = stream
 
     let pdbBytes =
-        writePdb (dumpDebugInfo,
-            showTimes, portablePDB, embeddedPDB, pdbfile,
-            outfile, reopenOutput, true,
-            signer, deterministic, pathMap,
+        writePdb (options.dumpDebugInfo,
+            options.showTimes,
+            options.portablePDB,
+            options.embeddedPDB,
+            options.pdbfile,
+            options.outfile,
+            reopenOutput,
+            true,
+            options.signer,
+            options.deterministic,
+            options.pathMap,
             pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk,
             debugChecksumPdbChunk, debugEmbeddedPdbChunk,
             debugDeterministicPdbChunk, textV2P)
@@ -4555,45 +4581,9 @@ let writeBinaryInMemory (
     stream.ToArray(), pdbBytes
 
 
-type options =
-   { ilg: ILGlobals
-     outfile: string
-     pdbfile: string option
-     portablePDB: bool
-     embeddedPDB: bool
-     embedAllSource: bool
-     embedSourceList: string list
-     sourceLink: string
-     checksumAlgorithm: HashAlgorithm
-     signer: ILStrongNameSigner option
-     emitTailcalls: bool
-     deterministic: bool
-     showTimes: bool
-     dumpDebugInfo: bool
-     referenceAssemblyOnly: bool
-     referenceAssemblyAttribOpt: ILAttribute option
-     pathMap: PathMap }
-
 let WriteILBinaryFile (options: options, inputModule, normalizeAssemblyRefs) =
-    writeBinaryFiles (options.outfile,
-        options.ilg, options.pdbfile, options.signer,
-        options.portablePDB, options.embeddedPDB,options.embedAllSource,
-        options.embedSourceList, options.sourceLink, options.checksumAlgorithm,
-        options.emitTailcalls, options.deterministic, options.showTimes,
-        options.dumpDebugInfo,
-        options.referenceAssemblyOnly,
-        options.referenceAssemblyAttribOpt,
-        options.pathMap,
-        inputModule, normalizeAssemblyRefs)
+    writeBinaryFiles (options, inputModule, normalizeAssemblyRefs)
     |> ignore
 
 let WriteILBinaryInMemory (options: options, inputModule: ILModuleDef, normalizeAssemblyRefs) =
-    writeBinaryInMemory (options.outfile,
-        options.ilg,
-        options.pdbfile,
-        options.signer,
-        options.portablePDB, options.embeddedPDB, options.embedAllSource,
-        options.embedSourceList, options.sourceLink, options.checksumAlgorithm,
-        options.emitTailcalls, options.deterministic,
-        options.showTimes, options.dumpDebugInfo, options.pathMap,
-        inputModule, normalizeAssemblyRefs)
+    writeBinaryInMemory (options, inputModule, normalizeAssemblyRefs)
diff --git a/src/Compiler/AbstractIL/ilwrite.fsi b/src/Compiler/AbstractIL/ilwrite.fsi
index 252c3fcc6ff..780a6a95f09 100644
--- a/src/Compiler/AbstractIL/ilwrite.fsi
+++ b/src/Compiler/AbstractIL/ilwrite.fsi
@@ -16,6 +16,7 @@ type options =
       embeddedPDB: bool
       embedAllSource: bool
       embedSourceList: string list
+      allGivenSources: ILSourceDocument list
       sourceLink: string
       checksumAlgorithm: HashAlgorithm
       signer: ILStrongNameSigner option
diff --git a/src/Compiler/Driver/fsc.fs b/src/Compiler/Driver/fsc.fs
index 773b9ae7d5a..7b484a41fee 100644
--- a/src/Compiler/Driver/fsc.fs
+++ b/src/Compiler/Driver/fsc.fs
@@ -490,6 +490,8 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
         TcImports.BuildFrameworkTcImports (foundationalTcConfigP, sysRes, otherRes)
         |> NodeCode.RunImmediateWithoutCancellation
 
+    let ilSourceDocs = [ for sourceFile in sourceFiles -> tcGlobals.memoize_file (FileIndex.fileIndexOfFile sourceFile)]
+
     // Register framework tcImports to be disposed in future
     disposables.Register frameworkTcImports
 
@@ -556,7 +558,7 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
     AbortOnError(diagnosticsLogger, exiter)
     ReportTime tcConfig "Typechecked"
 
-    Args (ctok, tcGlobals, tcImports, frameworkTcImports, tcState.Ccu, typedAssembly, topAttrs, tcConfig, outfile, pdbfile, assemblyName, diagnosticsLogger, exiter)
+    Args (ctok, tcGlobals, tcImports, frameworkTcImports, tcState.Ccu, typedAssembly, topAttrs, tcConfig, outfile, pdbfile, assemblyName, diagnosticsLogger, exiter, ilSourceDocs)
 
 /// Alternative first phase of compilation.  This is for the compile-from-AST feature of FCS.
 ///   - Import assemblies
@@ -676,11 +678,11 @@ let main1OfAst
     AbortOnError(diagnosticsLogger, exiter)
     ReportTime tcConfig "Typechecked"
 
-    Args (ctok, tcGlobals, tcImports, frameworkTcImports, tcState.Ccu, typedAssembly, topAttrs, tcConfig, outfile, pdbFile, assemblyName, diagnosticsLogger, exiter)
+    Args (ctok, tcGlobals, tcImports, frameworkTcImports, tcState.Ccu, typedAssembly, topAttrs, tcConfig, outfile, pdbFile, assemblyName, diagnosticsLogger, exiter, [])
 
 /// Second phase of compilation.
 ///   - Write the signature file, check some attributes
-let main2(Args (ctok, tcGlobals, tcImports: TcImports, frameworkTcImports, generatedCcu: CcuThunk, typedImplFiles, topAttrs, tcConfig: TcConfig, outfile, pdbfile, assemblyName, diagnosticsLogger, exiter: Exiter)) =
+let main2(Args (ctok, tcGlobals, tcImports: TcImports, frameworkTcImports, generatedCcu: CcuThunk, typedImplFiles, topAttrs, tcConfig: TcConfig, outfile, pdbfile, assemblyName, diagnosticsLogger, exiter: Exiter, ilSourceDocs)) =
 
     if tcConfig.typeCheckOnly then exiter.Exit 0
 
@@ -727,7 +729,7 @@ let main2(Args (ctok, tcGlobals, tcImports: TcImports, frameworkTcImports, gener
         XmlDocWriter.WriteXmlDocFile (tcGlobals, assemblyName, generatedCcu, xmlFile))
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, frameworkTcImports, tcGlobals, diagnosticsLogger, generatedCcu, outfile, typedImplFiles, topAttrs, pdbfile, assemblyName, assemVerFromAttrib, signingInfo, exiter)
+    Args (ctok, tcConfig, tcImports, frameworkTcImports, tcGlobals, diagnosticsLogger, generatedCcu, outfile, typedImplFiles, topAttrs, pdbfile, assemblyName, assemVerFromAttrib, signingInfo, exiter, ilSourceDocs)
 
 
 /// Third phase of compilation.
@@ -736,7 +738,7 @@ let main2(Args (ctok, tcGlobals, tcImports: TcImports, frameworkTcImports, gener
 ///   - encode optimization data
 let main3(Args (ctok, tcConfig, tcImports, frameworkTcImports: TcImports, tcGlobals,
                  diagnosticsLogger: DiagnosticsLogger, generatedCcu: CcuThunk, outfile, typedImplFiles,
-                 topAttrs, pdbfile, assemblyName, assemVerFromAttrib, signingInfo, exiter: Exiter)) =
+                 topAttrs, pdbfile, assemblyName, assemVerFromAttrib, signingInfo, exiter: Exiter, ilSourceDocs)) =
 
     // Encode the signature data
     ReportTime tcConfig "Encode Interface Data"
@@ -780,7 +782,7 @@ let main3(Args (ctok, tcConfig, tcImports, frameworkTcImports: TcImports, tcGlob
     // Pass on only the minimum information required for the next phase
     Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger,
           generatedCcu, outfile, optimizedImpls, topAttrs, pdbfile, assemblyName,
-          sigDataAttributes, sigDataResources, optDataResources, assemVerFromAttrib, signingInfo, metadataVersion, exiter)
+          sigDataAttributes, sigDataResources, optDataResources, assemVerFromAttrib, signingInfo, metadataVersion, exiter, ilSourceDocs)
 
 /// Fourth phase of compilation.
 ///   -  Static linking
@@ -789,7 +791,7 @@ let main4
       (tcImportsCapture,dynamicAssemblyCreator)
       (Args (ctok, tcConfig: TcConfig, tcImports, tcGlobals: TcGlobals, diagnosticsLogger,
              generatedCcu: CcuThunk, outfile, optimizedImpls, topAttrs, pdbfile, assemblyName,
-             sigDataAttributes, sigDataResources, optDataResources, assemVerFromAttrib, signingInfo, metadataVersion, exiter: Exiter)) =
+             sigDataAttributes, sigDataResources, optDataResources, assemVerFromAttrib, signingInfo, metadataVersion, exiter: Exiter, ilSourceDocs)) =
 
     match tcImportsCapture with
     | None -> ()
@@ -830,11 +832,11 @@ let main4
     AbortOnError(diagnosticsLogger, exiter)
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, staticLinker, outfile, pdbfile, ilxMainModule, signingInfo, exiter)
+    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, staticLinker, outfile, pdbfile, ilxMainModule, signingInfo, exiter, ilSourceDocs)
 
 /// Fifth phase of compilation.
 ///   -  static linking
-let main5(Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: DiagnosticsLogger, staticLinker, outfile, pdbfile, ilxMainModule, signingInfo, exiter: Exiter)) =
+let main5(Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: DiagnosticsLogger, staticLinker, outfile, pdbfile, ilxMainModule, signingInfo, exiter: Exiter, ilSourceDocs)) =
 
     use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Output
 
@@ -848,13 +850,13 @@ let main5(Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: Diagnos
     AbortOnError(diagnosticsLogger, exiter)
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, ilxMainModule, outfile, pdbfile, signingInfo, exiter)
+    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, ilxMainModule, outfile, pdbfile, signingInfo, exiter, ilSourceDocs)
 
 /// Sixth phase of compilation.
 ///   -  write the binaries
 let main6 dynamicAssemblyCreator (Args (ctok, tcConfig,  tcImports: TcImports, tcGlobals: TcGlobals,
                                         diagnosticsLogger: DiagnosticsLogger, ilxMainModule, outfile, pdbfile,
-                                        signingInfo, exiter: Exiter)) =
+                                        signingInfo, exiter: Exiter, ilSourceDocs)) =
 
     ReportTime tcConfig "Write .NET Binary"
 
@@ -901,6 +903,7 @@ let main6 dynamicAssemblyCreator (Args (ctok, tcConfig,  tcImports: TcImports, t
                         embeddedPDB = false
                         embedAllSource = tcConfig.embedAllSource
                         embedSourceList = tcConfig.embedSourceList
+                        allGivenSources = ilSourceDocs
                         sourceLink = tcConfig.sourceLink
                         checksumAlgorithm = tcConfig.checksumAlgorithm
                         signer = GetStrongNameSigner signingInfo
@@ -929,6 +932,7 @@ let main6 dynamicAssemblyCreator (Args (ctok, tcConfig,  tcImports: TcImports, t
                         embeddedPDB = tcConfig.embeddedPDB
                         embedAllSource = tcConfig.embedAllSource
                         embedSourceList = tcConfig.embedSourceList
+                        allGivenSources = ilSourceDocs
                         sourceLink = tcConfig.sourceLink
                         checksumAlgorithm = tcConfig.checksumAlgorithm
                         signer = GetStrongNameSigner signingInfo
diff --git a/src/Compiler/Interactive/fsi.fs b/src/Compiler/Interactive/fsi.fs
index 1a79a6e94f8..797f098f585 100644
--- a/src/Compiler/Interactive/fsi.fs
+++ b/src/Compiler/Interactive/fsi.fs
@@ -1445,6 +1445,8 @@ type internal FsiDynamicCompiler(
               embeddedPDB = false
               embedAllSource = tcConfig.embedAllSource
               embedSourceList = tcConfig.embedSourceList
+              // we don't add additional source files to the debug document set
+              allGivenSources = []
               sourceLink = tcConfig.sourceLink
               checksumAlgorithm = tcConfig.checksumAlgorithm
               signer = None
diff --git a/tests/FSharp.Compiler.ComponentTests/Debugger/PortablePdbs.fs b/tests/FSharp.Compiler.ComponentTests/Debugger/PortablePdbs.fs
index dbb3544bbd5..83e28666235 100644
--- a/tests/FSharp.Compiler.ComponentTests/Debugger/PortablePdbs.fs
+++ b/tests/FSharp.Compiler.ComponentTests/Debugger/PortablePdbs.fs
@@ -4,7 +4,10 @@ namespace FSharp.Compiler.ComponentTests.Debugger
 
 open Xunit
 open FSharp.Test.Compiler
+open System
+open System.IO
 open System.Reflection.Metadata
+open FSharp.Test
 
 module PortablePdbs =
 
@@ -71,4 +74,35 @@ module Baz =
                 Line 16, Col 20, Line 16, Col 22
                 Line 21, Col 20, Line 21, Col 22
             ]
+            VerifyDocuments [
+                Path.Combine(Environment.CurrentDirectory, "test.fs")
+            ]
+        ]
+
+    []
+    let ``Portable PDBs contain signature files`` () =
+
+        let compilation =
+            Fsi """
+namespace Foo
+
+module M =
+   val f: unit -> int
+        """
+        compilation
+        |> withAdditionalSourceFile (FsSource """
+namespace Foo
+
+module M =
+   let f () = 1
+        """)
+        |> asLibrary
+        |> withPortablePdb
+        |> compile
+        |> shouldSucceed
+        |> verifyPdb [
+            VerifyDocuments [
+                Path.Combine(Environment.CurrentDirectory, "test.fsi")
+                Path.Combine(Environment.CurrentDirectory, "test.fs")
+            ]
         ]
diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs
index de0942d94f9..68ce2aff88f 100644
--- a/tests/FSharp.Test.Utilities/Compiler.fs
+++ b/tests/FSharp.Test.Utilities/Compiler.fs
@@ -272,10 +272,13 @@ module rec Compiler =
         fsFromString (SourceFromPath path) |> FS
 
     let Fs (source: string) : CompilationUnit =
-        fsFromString (SourceCodeFileKind.Fs({FileName="test.fs"; SourceText=Some source })) |> FS
+        fsFromString (FsSource source) |> FS
+
+    let Fsi (source: string) : CompilationUnit =
+        fsFromString (FsiSource source) |> FS
 
     let FSharp (source: string) : CompilationUnit =
-        fsFromString (SourceCodeFileKind.Fs({FileName="test.fs"; SourceText=Some source })) |> FS
+        Fs source
 
     let FsFromPath (path: string) : CompilationUnit =
         fsFromString (SourceFromPath path)
@@ -876,6 +879,7 @@ module rec Compiler =
     type PdbVerificationOption =
     | VerifyImportScopes of ImportScope list list
     | VerifySequencePoints of (Line * Col * Line * Col) list
+    | VerifyDocuments of string list
     | Dummy of unit
 
     let private verifyPdbFormat (reader: MetadataReader) compilationType =
@@ -941,12 +945,30 @@ module rec Compiler =
         if sequencePoints <> expectedSequencePoints then
             failwith $"Expected sequence points are different from PDB.\nExpected: %A{expectedSequencePoints}\nActual: %A{sequencePoints}"
 
+    let private verifyDocuments (reader: MetadataReader) expectedDocuments =
+
+        let documents = 
+            [ for doc in reader.Documents do
+                if not doc.IsNil then
+                    let di = reader.GetDocument doc
+                    let nmh = di.Name
+                    if not nmh.IsNil then
+                        let name = reader.GetString nmh
+                        name ]
+            |> List.sort
+        
+        let expectedDocuments = expectedDocuments |> List.sort
+
+        if documents <> expectedDocuments then
+            failwith $"Expected documents are different from PDB.\nExpected: %A{expectedDocuments}\nActual: %A{documents}"
+
 
     let private verifyPdbOptions reader options =
         for option in options do
             match option with
             | VerifyImportScopes scopes -> verifyPdbImportTables reader scopes
             | VerifySequencePoints sp -> verifySequencePoints reader sp
+            | VerifyDocuments docs -> verifyDocuments reader docs
             | _ -> failwith $"Unknown verification option: {option.ToString()}"
 
     let private verifyPortablePdb (result: CompilationOutput) options : unit =

From d0baa9a5762a74209d7943d680ae062c2e4ded44 Mon Sep 17 00:00:00 2001
From: "Kevin Ransom (msft)" 
Date: Thu, 26 May 2022 18:27:28 -0700
Subject: [PATCH 16/20] add support for arm to --platform (#13120)

* merge

* temp

* temp

* update tests

* wrong api

* proj tweak

* Oops

* fantomas
---
 src/Compiler/AbstractIL/il.fs                 |   2 +
 src/Compiler/AbstractIL/il.fsi                |   2 +
 src/Compiler/AbstractIL/ilread.fs             |   2 +
 src/Compiler/AbstractIL/ilwrite.fs            | 267 +++++++++---------
 src/Compiler/AbstractIL/ilwritepdb.fs         |   1 -
 src/Compiler/Driver/CompilerImports.fs        |   2 +
 src/Compiler/Driver/CompilerOptions.fs        |   2 +
 src/Compiler/Driver/CreateILModule.fs         |   4 +-
 src/Compiler/FSComp.txt                       |   4 +-
 src/Compiler/Service/ServiceLexing.fsi        |   1 -
 src/Compiler/xlf/FSComp.txt.cs.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.de.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.es.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.fr.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.it.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.ja.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.ko.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.pl.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.pt-BR.xlf         |   8 +-
 src/Compiler/xlf/FSComp.txt.ru.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.tr.xlf            |   8 +-
 src/Compiler/xlf/FSComp.txt.zh-Hans.xlf       |   8 +-
 src/Compiler/xlf/FSComp.txt.zh-Hant.xlf       |   8 +-
 src/FSharp.Build/Fsc.fs                       |   2 +
 .../CompilerOptions/fsc/platform.fs           |  36 +--
 .../EmittedIL/Platform/AssemblyNameForDll.fs  |   7 +
 .../EmittedIL/Platform/AssemblyNameForExe.fs  |   7 +
 .../EmittedIL/Platform/Platform.fs            | 136 +++++++++
 .../EmittedIL/Platform/PlatformedDll.fs       |   3 +
 .../EmittedIL/Platform/PlatformedExe.fs       |   1 +
 .../FSharp.Compiler.ComponentTests.fsproj     |  24 +-
 tests/FSharp.Test.Utilities/Compiler.fs       |   2 +-
 tests/FSharp.Test.Utilities/ILChecker.fs      |   8 +
 tests/fsharp/test.fs                          |  54 ++++
 tests/fsharp/test.fsx                         |   3 +
 .../fsc/help/help40.437.1033.bsl              |   6 +-
 36 files changed, 447 insertions(+), 233 deletions(-)
 create mode 100644 tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForDll.fs
 create mode 100644 tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForExe.fs
 create mode 100644 tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/Platform.fs
 create mode 100644 tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedDll.fs
 create mode 100644 tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedExe.fs
 create mode 100644 tests/fsharp/test.fs
 create mode 100644 tests/fsharp/test.fsx

diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs
index 7cd377cad3d..3afa9326f47 100644
--- a/src/Compiler/AbstractIL/il.fs
+++ b/src/Compiler/AbstractIL/il.fs
@@ -1114,6 +1114,8 @@ type ILPlatform =
     | X86
     | AMD64
     | IA64
+    | ARM
+    | ARM64
 
 type ILSourceDocument =
     {
diff --git a/src/Compiler/AbstractIL/il.fsi b/src/Compiler/AbstractIL/il.fsi
index d205162b991..55c713968e2 100644
--- a/src/Compiler/AbstractIL/il.fsi
+++ b/src/Compiler/AbstractIL/il.fsi
@@ -25,6 +25,8 @@ type ILPlatform =
     | X86
     | AMD64
     | IA64
+    | ARM
+    | ARM64
 
 /// Debug info.  Values of type "source" can be attached at sequence
 /// points and some other locations.
diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs
index 6ec7cec7c1c..022d5eeb839 100644
--- a/src/Compiler/AbstractIL/ilread.fs
+++ b/src/Compiler/AbstractIL/ilread.fs
@@ -4741,7 +4741,9 @@ let openPEFileReader (fileName, pefile: BinaryFile, pdbDirPath, noFileOnDisk) =
     let platform =
         match machine with
         | 0x8664 -> Some AMD64
+        | 0xaa64 -> Some ARM64
         | 0x200 -> Some IA64
+        | 0x1c0 -> Some ARM
         | _ -> Some X86
 
     let sectionHeadersStartPhysLoc = peOptionalHeaderPhysLoc + headerSizeOpt
diff --git a/src/Compiler/AbstractIL/ilwrite.fs b/src/Compiler/AbstractIL/ilwrite.fs
index 988e21c1d13..61f02d8cc0d 100644
--- a/src/Compiler/AbstractIL/ilwrite.fs
+++ b/src/Compiler/AbstractIL/ilwrite.fs
@@ -3875,13 +3875,16 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
 
           let os = new BinaryWriter(stream, System.Text.Encoding.UTF8)
 
-          let imageBaseReal = modul.ImageBase // FIXED CHOICE
-          let alignVirt = modul.VirtualAlignment // FIXED CHOICE
-          let alignPhys = modul.PhysicalAlignment // FIXED CHOICE
+          let imageBaseReal = modul.ImageBase       // FIXED CHOICE
+          let alignVirt = modul.VirtualAlignment    // FIXED CHOICE
+          let alignPhys = modul.PhysicalAlignment   // FIXED CHOICE
 
           let isItanium = modul.Platform = Some IA64
-
-          let numSections = 3 // .text, .sdata, .reloc
+          let isItaniumOrAMD = match modul.Platform with | Some IA64 | Some AMD64 -> true | _ -> false
+          let hasEntryPointStub = match modul.Platform with | Some ARM64 | Some ARM -> false | _ -> true
+          let numSections =
+              if hasEntryPointStub then 3           // .text, .sdata, .reloc
+              else 2                                // .text, .sdata
 
           // HEADERS
           let next = 0x0
@@ -3889,26 +3892,13 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           let headerAddr = next
           let next = headerAddr
 
-          let msdosHeaderSize = 0x80
-          let msdosHeaderChunk, next = chunk msdosHeaderSize next
-
-          let peSignatureSize = 0x04
-          let peSignatureChunk, next = chunk peSignatureSize next
-
-          let peFileHeaderSize = 0x14
-          let peFileHeaderChunk, next = chunk peFileHeaderSize next
-
-          let peOptionalHeaderSize = if modul.Is64Bit then 0xf0 else 0xe0
-          let peOptionalHeaderChunk, next = chunk peOptionalHeaderSize next
-
-          let textSectionHeaderSize = 0x28
-          let textSectionHeaderChunk, next = chunk textSectionHeaderSize next
-
-          let dataSectionHeaderSize = 0x28
-          let dataSectionHeaderChunk, next = chunk dataSectionHeaderSize next
-
-          let relocSectionHeaderSize = 0x28
-          let relocSectionHeaderChunk, next = chunk relocSectionHeaderSize next
+          let msdosHeaderChunk, next = chunk 0x80 next
+          let peSignatureChunk, next = chunk 0x04 next
+          let peFileHeaderChunk, next = chunk 0x14 next
+          let peOptionalHeaderChunk, next = chunk (if modul.Is64Bit then 0xf0 else 0xe0) next
+          let textSectionHeaderChunk, next = chunk 0x28 next
+          let dataSectionHeaderChunk, next = chunk 0x28 next
+          let relocSectionHeaderChunk, next = if hasEntryPointStub then chunk 0x28 next else nochunk next
 
           let headerSize = next - headerAddr
           let nextPhys = align alignPhys (headerSectionPhysLoc + headerSize)
@@ -3921,7 +3911,8 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           let textSectionAddr = next
           let next = textSectionAddr
 
-          let importAddrTableChunk, next = chunk 0x08 next
+          // IAT not for ARM
+          let importAddrTableChunk, next = if hasEntryPointStub then chunk 0x08 next else nochunk next
           let cliHeaderPadding = (if isItanium then (align 16 next) else next) - next
           let next = next + cliHeaderPadding
           let cliHeaderChunk, next = chunk 0x48 next
@@ -3974,17 +3965,15 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           let vtfixupsChunk, next = nochunk next   // Note: only needed for mixed mode assemblies
           let importTableChunkPrePadding = (if isItanium then (align 16 next) else next) - next
           let next = next + importTableChunkPrePadding
-          let importTableChunk, next = chunk 0x28 next
-          let importLookupTableChunk, next = chunk 0x14 next
-          let importNameHintTableChunk, next = chunk 0x0e next
-          let mscoreeStringChunk, next = chunk 0x0c next
+          let importTableChunk, next = if hasEntryPointStub then chunk 0x28 next else nochunk next
+          let importLookupTableChunk, next = if hasEntryPointStub then chunk 0x14 next else nochunk next
+          let importNameHintTableChunk, next = if hasEntryPointStub then chunk 0x0e next else nochunk next
+          let mscoreeStringChunk, next = if hasEntryPointStub then chunk 0x0c next else nochunk next
 
-          let next = align 0x10 (next + 0x05) - 0x05
+          let next = if hasEntryPointStub then align 0x10 (next + 0x05) - 0x05 else next
           let importTableChunk = { addr=importTableChunk.addr; size = next - importTableChunk.addr}
-          let importTableChunkPadding = importTableChunk.size - (0x28 + 0x14 + 0x0e + 0x0c)
-
-          let next = next + 0x03
-          let entrypointCodeChunk, next = chunk 0x06 next
+          let importTableChunkPadding = if hasEntryPointStub then importTableChunk.size - (0x28 + 0x14 + 0x0e + 0x0c) else importTableChunk.size
+          let entrypointCodeChunk, next = if hasEntryPointStub then chunk 0x06 (next + 0x03) else nochunk next
           let globalpointerCodeChunk, next = chunk (if isItanium then 0x8 else 0x0) next
 
           let pdbInfoOpt =
@@ -4084,13 +4073,13 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
 
           // .RELOC SECTION base reloc table: 0x0c size
           let relocSectionPhysLoc = nextPhys
-          let relocSectionAddr = next
-          let baseRelocTableChunk, next = chunk 0x0c next
+          let relocSectionAddr =  if hasEntryPointStub then next else 0x00
+          let baseRelocTableChunk, next =  if hasEntryPointStub then chunk 0x0c next else nochunk next
 
-          let relocSectionSize = next - relocSectionAddr
-          let nextPhys = align alignPhys (relocSectionPhysLoc + relocSectionSize)
-          let relocSectionPhysSize = nextPhys - relocSectionPhysLoc
-          let next = align alignVirt (relocSectionAddr + relocSectionSize)
+          let relocSectionSize = if hasEntryPointStub then next - relocSectionAddr else 0x00
+          let nextPhys = if hasEntryPointStub then align alignPhys (relocSectionPhysLoc + relocSectionSize) else nextPhys
+          let relocSectionPhysSize = if hasEntryPointStub then nextPhys - relocSectionPhysLoc else 0x00
+          let next = if hasEntryPointStub then align alignVirt (relocSectionAddr + relocSectionSize) else align alignVirt next
 
          // Now we know where the data section lies we can fix up the
          // references into the data section from the metadata tables.
@@ -4144,12 +4133,12 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
 
           write (Some peFileHeaderChunk.addr) os "pe file header" [| |]
 
-          if (modul.Platform = Some AMD64) then
-            writeInt32AsUInt16 os 0x8664    // Machine - IMAGE_FILE_MACHINE_AMD64
-          elif isItanium then
-            writeInt32AsUInt16 os 0x200
-          else
-            writeInt32AsUInt16 os 0x014c   // Machine - IMAGE_FILE_MACHINE_I386
+          match modul.Platform with
+          | Some AMD64 -> writeInt32AsUInt16 os 0x8664      // Machine - IMAGE_FILE_MACHINE_AMD64
+          | Some IA64 -> writeInt32AsUInt16 os 0x200        // Machine - IMAGE_FILE_MACHINE_IA64
+          | Some ARM64 -> writeInt32AsUInt16 os 0xaa64      // Machine - IMAGE_FILE_MACHINE_ARM64
+          | Some ARM -> writeInt32AsUInt16 os 0x1c0         // Machine - IMAGE_FILE_MACHINE_ARM
+          | _ ->  writeInt32AsUInt16 os 0x014c              // Machine - IMAGE_FILE_MACHINE_I386
 
           writeInt32AsUInt16 os numSections
 
@@ -4187,37 +4176,35 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           writeInt32 os 0x00 // Pointer to Symbol Table Always 0
        // 00000090
           writeInt32 os 0x00 // Number of Symbols Always 0
-          writeInt32AsUInt16 os peOptionalHeaderSize // Size of the optional header, the format is described below.
+          writeInt32AsUInt16 os peOptionalHeaderChunk.size                      // Format is described below.
 
-          // 64bit: IMAGE_FILE_32BIT_MACHINE ||| IMAGE_FILE_LARGE_ADDRESS_AWARE
+          // 64bit: IMAGE_FILE_LARGE_ADDRESS_AWARE
           // 32bit: IMAGE_FILE_32BIT_MACHINE
-          // Yes, 32BIT_MACHINE is set for AMD64...
-          let iMachineCharacteristic = match modul.Platform with | Some IA64 -> 0x20 | Some AMD64 -> 0x0120 | _ -> 0x0100
+          let iMachineCharacteristic = match modul.Platform with | Some IA64 | Some AMD64 | Some ARM64 -> 0x20 | _ -> 0x0100
 
-          writeInt32AsUInt16 os ((if isDll then 0x2000 else 0x0000) ||| 0x0002 ||| 0x0004 ||| 0x0008 ||| iMachineCharacteristic)
+          writeInt32AsUInt16 os ((if isDll then 0x2000 else 0x0000) ||| 0x0002 ||| iMachineCharacteristic)
 
        // Now comes optional header
-
           let peOptionalHeaderByte = peOptionalHeaderByteByCLRVersion desiredMetadataVersion
 
           write (Some peOptionalHeaderChunk.addr) os "pe optional header" [| |]
           if modul.Is64Bit then
-              writeInt32AsUInt16 os 0x020B // Magic number is 0x020B for 64-bit
+              writeInt32AsUInt16 os 0x020B              // Magic number is 0x020B for 64-bit
           else
-              writeInt32AsUInt16 os 0x010b // Always 0x10B (see Section 23.1).
-          writeInt32AsUInt16 os peOptionalHeaderByte // ECMA spec says 6, some binaries, e.g. fscmanaged.exe say 7, Whidbey binaries say 8
-          writeInt32 os textSectionPhysSize          // Size of the code (text) section, or the sum of all code sections if there are multiple sections.
+              writeInt32AsUInt16 os 0x010b              // Always 0x10B (see Section 23.1).
+          writeInt32AsUInt16 os peOptionalHeaderByte    // ECMA spec says 6, some binaries, e.g. fscmanaged.exe say 7, Whidbey binaries say 8
+          writeInt32 os textSectionPhysSize             // Size of the code (text) section, or the sum of all code sections if there are multiple sections.
         // 000000a0
-          writeInt32 os dataSectionPhysSize          // Size of the initialized data section
-          writeInt32 os 0x00                         // Size of the uninitialized data section
-          writeInt32 os entrypointCodeChunk.addr     // RVA of entry point, needs to point to bytes 0xFF 0x25 followed by the RVA+!0x4000000
-          writeInt32 os textSectionAddr              // e.g. 0x0002000
+          writeInt32 os dataSectionPhysSize             // Size of the initialized data section
+          writeInt32 os 0x00                            // Size of the uninitialized data section
+          writeInt32 os entrypointCodeChunk.addr        // RVA of entry point, needs to point to bytes 0xFF 0x25 followed by the RVA+!0x4000000
+          writeInt32 os textSectionAddr                 // e.g. 0x0002000
        // 000000b0
           if modul.Is64Bit then
-              writeInt64 os (int64 imageBaseReal)    // REVIEW: For 64-bit, we should use a 64-bit image base
+              writeInt64 os (int64 imageBaseReal)
           else
-              writeInt32 os dataSectionAddr // e.g. 0x0000c000
-              writeInt32 os imageBaseReal // Image Base Always 0x400000 (see Section 23.1). - QUERY : no it's not always 0x400000, e.g. 0x034f0000
+              writeInt32 os dataSectionAddr             // e.g. 0x0000c000
+              writeInt32 os (int32 imageBaseReal)       // Image Base Always 0x400000 (see Section 23.1). - QUERY : no it's not always 0x400000, e.g. 0x034f0000
 
           writeInt32 os alignVirt //  Section Alignment Always 0x2000 (see Section 23.1).
           writeInt32 os alignPhys // File Alignment Either 0x200 or 0x1000.
@@ -4337,21 +4324,22 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           writeInt32AsUInt16 os 0x00  //  NumberOfLinenumbers Always 0 (see Section 23.1).
           writeBytes os [| 0x40uy; 0x00uy; 0x00uy; 0x40uy |] //  Characteristics Flags: IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA
 
-          write (Some relocSectionHeaderChunk.addr) os "reloc section header" [| |]
+          if hasEntryPointStub then
+              write (Some relocSectionHeaderChunk.addr) os "reloc section header" [| |]
        // 000001a0
-          writeBytes os [| 0x2euy; 0x72uy; 0x65uy; 0x6cuy; 0x6fuy; 0x63uy; 0x00uy; 0x00uy; |] // ".reloc\000\000"
-          writeInt32 os relocSectionSize // VirtualSize: Total size of the section when loaded into memory in bytes rounded to Section Alignment.
-          writeInt32 os relocSectionAddr //  VirtualAddress For executable images this is the address of the first byte of the section.
+              writeBytes os [| 0x2euy; 0x72uy; 0x65uy; 0x6cuy; 0x6fuy; 0x63uy; 0x00uy; 0x00uy; |] // ".reloc\000\000"
+              writeInt32 os relocSectionSize // VirtualSize: Total size of the section when loaded into memory in bytes rounded to Section Alignment.
+              writeInt32 os relocSectionAddr //  VirtualAddress For executable images this is the address of the first byte of the section.
        // 000001b0
-          writeInt32 os relocSectionPhysSize //  SizeOfRawData Size of the initialized reloc on disk in bytes
-          writeInt32 os relocSectionPhysLoc // PointerToRawData QUERY: Why does ECMA say "RVA" here? Offset to section's first page within the PE file.
+              writeInt32 os relocSectionPhysSize //  SizeOfRawData Size of the initialized reloc on disk in bytes
+              writeInt32 os relocSectionPhysLoc // PointerToRawData QUERY: Why does ECMA say "RVA" here? Offset to section's first page within the PE file.
        // 000001b8
-          writeInt32 os 0x00 // PointerToRelocations RVA of Relocation section.
-          writeInt32 os 0x00 // PointerToLineNumbers Always 0 (see Section 23.1).
+              writeInt32 os 0x00 // PointerToRelocations RVA of Relocation section.
+              writeInt32 os 0x00 // PointerToLineNumbers Always 0 (see Section 23.1).
        // 000001c0
-          writeInt32AsUInt16 os 0x00 // NumberOfRelocations Number of relocations, set to 0 if unused.
-          writeInt32AsUInt16 os 0x00  //  NumberOfLinenumbers Always 0 (see Section 23.1).
-          writeBytes os [| 0x40uy; 0x00uy; 0x00uy; 0x42uy |] //  Characteristics Flags: IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
+              writeInt32AsUInt16 os 0x00 // NumberOfRelocations Number of relocations, set to 0 if unused.
+              writeInt32AsUInt16 os 0x00  //  NumberOfLinenumbers Always 0 (see Section 23.1).
+              writeBytes os [| 0x40uy; 0x00uy; 0x00uy; 0x42uy |] //  Characteristics Flags: IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
 
           writePadding os "pad to text begin" (textSectionPhysLoc - headerSize)
 
@@ -4360,12 +4348,12 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           let textV2P v = v - textSectionAddr + textSectionPhysLoc
 
           // e.g. 0x0200
-          write (Some (textV2P importAddrTableChunk.addr)) os "import addr table" [| |]
-          writeInt32 os importNameHintTableChunk.addr
-          writeInt32 os 0x00  // QUERY 4 bytes of zeros not 2 like ECMA 24.3.1 says
+          if hasEntryPointStub then
+              write (Some (textV2P importAddrTableChunk.addr)) os "import addr table" [| |]
+              writeInt32 os importNameHintTableChunk.addr
+              writeInt32 os 0x00  // QUERY 4 bytes of zeros not 2 like ECMA 24.3.1 says
 
           // e.g. 0x0208
-
           let flags =
             (if modul.IsILOnly then 0x01 else 0x00) |||
             (if modul.Is32Bit then 0x02 else 0x00) |||
@@ -4413,50 +4401,51 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           write (Some (textV2P rawdataChunk.addr)) os "raw data" [| |]
           writeBytes os data
 
-          writePadding os "start of import table" importTableChunkPrePadding
-
-          // vtfixups would go here
-          write (Some (textV2P importTableChunk.addr)) os "import table" [| |]
-
-          writeInt32 os importLookupTableChunk.addr
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-          writeInt32 os mscoreeStringChunk.addr
-          writeInt32 os importAddrTableChunk.addr
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-
-          write (Some (textV2P importLookupTableChunk.addr)) os "import lookup table" [| |]
-          writeInt32 os importNameHintTableChunk.addr
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-          writeInt32 os 0x00
-
-
-          write (Some (textV2P importNameHintTableChunk.addr)) os "import name hint table" [| |]
-          // Two zero bytes of hint, then Case sensitive, null-terminated ASCII string containing name to import.
-          // Shall _CorExeMain a .exe file _CorDllMain for a .dll file.
-          if isDll then
-              writeBytes os [| 0x00uy; 0x00uy; 0x5fuy; 0x43uy ; 0x6fuy; 0x72uy; 0x44uy; 0x6cuy; 0x6cuy; 0x4duy; 0x61uy; 0x69uy; 0x6euy; 0x00uy |]
-          else
-              writeBytes os [| 0x00uy; 0x00uy; 0x5fuy; 0x43uy; 0x6fuy; 0x72uy; 0x45uy; 0x78uy; 0x65uy; 0x4duy; 0x61uy; 0x69uy; 0x6euy; 0x00uy |]
+          if hasEntryPointStub then
+              writePadding os "start of import table" importTableChunkPrePadding
+
+              // vtfixups would go here
+              write (Some (textV2P importTableChunk.addr)) os "import table" [| |]
+
+              writeInt32 os importLookupTableChunk.addr
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+              writeInt32 os mscoreeStringChunk.addr
+              writeInt32 os importAddrTableChunk.addr
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+
+              write (Some (textV2P importLookupTableChunk.addr)) os "import lookup table" [| |]
+              writeInt32 os importNameHintTableChunk.addr
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+              writeInt32 os 0x00
+
+
+              write (Some (textV2P importNameHintTableChunk.addr)) os "import name hint table" [| |]
+              // Two zero bytes of hint, then Case sensitive, null-terminated ASCII string containing name to import.
+              // Shall _CorExeMain a .exe file _CorDllMain for a .dll file.
+              if isDll then
+                  writeBytes os [| 0x00uy; 0x00uy; 0x5fuy; 0x43uy ; 0x6fuy; 0x72uy; 0x44uy; 0x6cuy; 0x6cuy; 0x4duy; 0x61uy; 0x69uy; 0x6euy; 0x00uy |]
+              else
+                  writeBytes os [| 0x00uy; 0x00uy; 0x5fuy; 0x43uy; 0x6fuy; 0x72uy; 0x45uy; 0x78uy; 0x65uy; 0x4duy; 0x61uy; 0x69uy; 0x6euy; 0x00uy |]
 
-          write (Some (textV2P mscoreeStringChunk.addr)) os "mscoree string"
-            [| 0x6duy; 0x73uy; 0x63uy; 0x6fuy ; 0x72uy; 0x65uy ; 0x65uy; 0x2euy ; 0x64uy; 0x6cuy ; 0x6cuy; 0x00uy ; |]
+              write (Some (textV2P mscoreeStringChunk.addr)) os "mscoree string"
+                [| 0x6duy; 0x73uy; 0x63uy; 0x6fuy ; 0x72uy; 0x65uy ; 0x65uy; 0x2euy ; 0x64uy; 0x6cuy ; 0x6cuy; 0x00uy ; |]
 
-          writePadding os "end of import tab" importTableChunkPadding
+              writePadding os "end of import tab" importTableChunkPadding
 
-          writePadding os "head of entrypoint" 0x03
-          let ep = (imageBaseReal + textSectionAddr)
-          write (Some (textV2P entrypointCodeChunk.addr)) os " entrypoint code"
-                 [| 0xFFuy; 0x25uy; (* x86 Instructions for entry *) b0 ep; b1 ep; b2 ep; b3 ep |]
-          if isItanium then
-              write (Some (textV2P globalpointerCodeChunk.addr)) os " itanium global pointer"
-                   [| 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy |]
+              writePadding os "head of entrypoint" 0x03
+              let ep = (imageBaseReal + textSectionAddr)
+              write (Some (textV2P entrypointCodeChunk.addr)) os " entrypoint code"
+                     [| 0xFFuy; 0x25uy; (* x86 Instructions for entry *) b0 ep; b1 ep; b2 ep; b3 ep |]
+              if isItanium then
+                  write (Some (textV2P globalpointerCodeChunk.addr)) os " itanium global pointer"
+                       [| 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy; 0x0uy |]
 
           if options.pdbfile.IsSome then
               write (Some (textV2P debugDirectoryChunk.addr)) os "debug directory" (Array.create debugDirectoryChunk.size 0x0uy)
@@ -4484,26 +4473,26 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
           writePadding os "end of .rsrc" (relocSectionPhysLoc - dataSectionPhysLoc - dataSectionSize)
 
           // RELOC SECTION
+          if hasEntryPointStub then
+              // See ECMA 24.3.2
+              let relocV2P v = v - relocSectionAddr + relocSectionPhysLoc
+
+              let entrypointFixupAddr = entrypointCodeChunk.addr + 0x02
+              let entrypointFixupBlock = (entrypointFixupAddr / 4096) * 4096
+              let entrypointFixupOffset = entrypointFixupAddr - entrypointFixupBlock
+              let reloc = (if isItaniumOrAMD then 0xA000 (* IMAGE_REL_BASED_DIR64 *) else 0x3000 (* IMAGE_REL_BASED_HIGHLOW *)) ||| entrypointFixupOffset
+              // For the itanium, you need to set a relocation entry for the global pointer
+              let reloc2 =
+                  if not isItanium then
+                      0x0
+                  else
+                      0xA000 ||| (globalpointerCodeChunk.addr - ((globalpointerCodeChunk.addr / 4096) * 4096))
 
-          // See ECMA 24.3.2
-          let relocV2P v = v - relocSectionAddr + relocSectionPhysLoc
-
-          let entrypointFixupAddr = entrypointCodeChunk.addr + 0x02
-          let entrypointFixupBlock = (entrypointFixupAddr / 4096) * 4096
-          let entrypointFixupOffset = entrypointFixupAddr - entrypointFixupBlock
-          let reloc = (if modul.Is64Bit then 0xA000 (* IMAGE_REL_BASED_DIR64 *) else 0x3000 (* IMAGE_REL_BASED_HIGHLOW *)) ||| entrypointFixupOffset
-          // For the itanium, you need to set a relocation entry for the global pointer
-          let reloc2 =
-              if not isItanium then
-                  0x0
-              else
-                  0xA000 ||| (globalpointerCodeChunk.addr - ((globalpointerCodeChunk.addr / 4096) * 4096))
-
-          write (Some (relocV2P baseRelocTableChunk.addr)) os "base reloc table"
-              [| b0 entrypointFixupBlock; b1 entrypointFixupBlock; b2 entrypointFixupBlock; b3 entrypointFixupBlock
-                 0x0cuy; 0x00uy; 0x00uy; 0x00uy
-                 b0 reloc; b1 reloc
-                 b0 reloc2; b1 reloc2; |]
+              write (Some (relocV2P baseRelocTableChunk.addr)) os "base reloc table"
+                  [| b0 entrypointFixupBlock; b1 entrypointFixupBlock; b2 entrypointFixupBlock; b3 entrypointFixupBlock
+                     0x0cuy; 0x00uy; 0x00uy; 0x00uy
+                     b0 reloc; b1 reloc
+                     b0 reloc2; b1 reloc2; |]
           writePadding os "end of .reloc" (imageEndSectionPhysLoc - relocSectionPhysLoc - relocSectionSize)
 
           pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, mappings
diff --git a/src/Compiler/AbstractIL/ilwritepdb.fs b/src/Compiler/AbstractIL/ilwritepdb.fs
index 0a4a5b53794..df3ff04764c 100644
--- a/src/Compiler/AbstractIL/ilwritepdb.fs
+++ b/src/Compiler/AbstractIL/ilwritepdb.fs
@@ -53,7 +53,6 @@ type BlobBuildingStream() =
 
     override _.Read(_buffer: byte array, _offset: int, _count: int) = raise (NotSupportedException())
 
-
     override _.SetLength(_value: int64) = raise (NotSupportedException())
     override val Position = 0L with get, set
 
diff --git a/src/Compiler/Driver/CompilerImports.fs b/src/Compiler/Driver/CompilerImports.fs
index 9acec5b6649..21604212b79 100644
--- a/src/Compiler/Driver/CompilerImports.fs
+++ b/src/Compiler/Driver/CompilerImports.fs
@@ -405,6 +405,8 @@ type TcConfig with
             | None -> "MSIL"
             | Some X86 -> "x86"
             | Some AMD64 -> "amd64"
+            | Some ARM -> "arm"
+            | Some ARM64 -> "arm64"
             | Some IA64 -> "ia64"
 
         try
diff --git a/src/Compiler/Driver/CompilerOptions.fs b/src/Compiler/Driver/CompilerOptions.fs
index 681d6fa92f1..1d211eee26a 100644
--- a/src/Compiler/Driver/CompilerOptions.fs
+++ b/src/Compiler/Driver/CompilerOptions.fs
@@ -708,6 +708,8 @@ let outputFileFlagsFsc (tcConfigB: TcConfigBuilder) =
                     match s with
                     | "x86" -> Some X86
                     | "x64" -> Some AMD64
+                    | "arm" -> Some ARM
+                    | "arm64" -> Some ARM64
                     | "Itanium" -> Some IA64
                     | "anycpu32bitpreferred" ->
                         tcConfigB.prefer32Bit <- true
diff --git a/src/Compiler/Driver/CreateILModule.fs b/src/Compiler/Driver/CreateILModule.fs
index c510fcca310..5cb00a7f215 100644
--- a/src/Compiler/Driver/CreateILModule.fs
+++ b/src/Compiler/Driver/CreateILModule.fs
@@ -487,8 +487,8 @@ module MainModuleBuilder =
               ImageBase = (match tcConfig.baseAddress with None -> 0x00400000l | Some b -> b)
               IsDLL=(tcConfig.target = CompilerTarget.Dll || tcConfig.target=CompilerTarget.Module)
               Platform = tcConfig.platform
-              Is32Bit=(match tcConfig.platform with Some X86 -> true | _ -> false)
-              Is64Bit=(match tcConfig.platform with Some AMD64 | Some IA64 -> true | _ -> false)
+              Is32Bit=(match tcConfig.platform with Some X86 | Some ARM -> true | _ -> false)
+              Is64Bit=(match tcConfig.platform with Some AMD64 | Some IA64 | Some ARM64 -> true | _ -> false)
               Is32BitPreferred = if tcConfig.prefer32Bit && not tcConfig.target.IsExe then (error(Error(FSComp.SR.invalidPlatformTarget(), rangeCmdArgs))) else tcConfig.prefer32Bit
               CustomAttrsStored=
                   storeILCustomAttrs
diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt
index d59eaf4ff53..513117632cc 100644
--- a/src/Compiler/FSComp.txt
+++ b/src/Compiler/FSComp.txt
@@ -850,7 +850,7 @@ optsPublicSign,"Public-sign the assembly using only the public portion of the st
 optsWriteXml,"Write the xmldoc of the assembly to the given file"
 optsStrongKeyFile,"Specify a strong name key file"
 optsStrongKeyContainer,"Specify a strong name key container"
-optsPlatform,"Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu."
+optsPlatform,"Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu."
 optsNoOpt,"Only include optimization information essential for implementing inlined constructs. Inhibits cross-module inlining but improves binary compatibility."
 optsNoInterface,"Don't add a resource to the generated assembly containing F#-specific metadata"
 optsSig,"Print the inferred interface of the assembly to a file"
@@ -917,7 +917,7 @@ optsHelpBannerMisc,"- MISCELLANEOUS -"
 optsHelpBannerLanguage,"- LANGUAGE -"
 optsHelpBannerErrsAndWarns,"- ERRORS AND WARNINGS -"
 1063,optsUnknownArgumentToTheTestSwitch,"Unknown --test argument: '%s'"
-1064,optsUnknownPlatform,"Unrecognized platform '%s', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+1064,optsUnknownPlatform,"Unrecognized platform '%s', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu."
 1065,optsUnknownChecksumAlgorithm,"Algorithm '%s' is not supported"
 optsInternalNoDescription,"The command-line option '%s' is for test purposes only"
 optsDCLONoDescription,"The command-line option '%s' has been deprecated"
diff --git a/src/Compiler/Service/ServiceLexing.fsi b/src/Compiler/Service/ServiceLexing.fsi
index df1dfdde294..5e2e5a9732d 100755
--- a/src/Compiler/Service/ServiceLexing.fsi
+++ b/src/Compiler/Service/ServiceLexing.fsi
@@ -6,7 +6,6 @@ open System
 open System.Threading
 open FSharp.Compiler
 open FSharp.Compiler.Text
-
 #nowarn "57"
 
 /// Represents encoded information for the end-of-line continuation of lexing
diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf
index 92860a2902f..d0a995aa3b0 100644
--- a/src/Compiler/xlf/FSComp.txt.cs.xlf
+++ b/src/Compiler/xlf/FSComp.txt.cs.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Omezuje platformy, na kterých je možné tento kód spustit: x86, Itanium, x64, anycpu32bitpreferred a anycpu. Výchozí je anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Omezuje platformy, na kterých je možné tento kód spustit: x86, Itanium, x64, anycpu32bitpreferred a anycpu. Výchozí je anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Nerozpoznaná platforma {0}. Platné hodnoty jsou x86, x64, Itanium, anycpu32bitpreferred a anycpu.
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Nerozpoznaná platforma {0}. Platné hodnoty jsou x86, x64, Itanium, anycpu32bitpreferred a anycpu.
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf
index 22bd16c63c3..57d1e5450ea 100644
--- a/src/Compiler/xlf/FSComp.txt.de.xlf
+++ b/src/Compiler/xlf/FSComp.txt.de.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Schränken Sie ein, auf welchen Plattformen dieser Code ausgeführt werden kann: "x86", "Itanium", "x64", "anycpu32bitpreferred" oder "anycpu". Der Standard ist "anycpu".
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Schränken Sie ein, auf welchen Plattformen dieser Code ausgeführt werden kann: "x86", "Itanium", "x64", "anycpu32bitpreferred" oder "anycpu". Der Standard ist "anycpu".
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Unbekannte Plattform "{0}", gültige Werte sind "x86", "x64", "Itanium", "anycpu32bitpreferred" und "anycpu".
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Unbekannte Plattform "{0}", gültige Werte sind "x86", "x64", "Itanium", "anycpu32bitpreferred" und "anycpu".
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf
index 4dc904402bf..6a59b872c1b 100644
--- a/src/Compiler/xlf/FSComp.txt.es.xlf
+++ b/src/Compiler/xlf/FSComp.txt.es.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Limitar las plataformas en las que se puede ejecutar este código: x86, Itanium, x64, anycpu32bitpreferred o anycpu. El valor predeterminado es anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Limitar las plataformas en las que se puede ejecutar este código: x86, Itanium, x64, anycpu32bitpreferred o anycpu. El valor predeterminado es anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Plataforma '{0}' no reconocida. Los valores válidos son 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' y 'anycpu'
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Plataforma '{0}' no reconocida. Los valores válidos son 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' y 'anycpu'
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf
index 0c131e6353c..7b64cc5b780 100644
--- a/src/Compiler/xlf/FSComp.txt.fr.xlf
+++ b/src/Compiler/xlf/FSComp.txt.fr.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Limiter les plateformes sur lesquelles ce code peut s'exécuter : x86, Itanium, x64, anycpu32bitpreferred ou anycpu. La valeur par défaut est anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Limiter les plateformes sur lesquelles ce code peut s'exécuter : x86, Itanium, x64, anycpu32bitpreferred ou anycpu. La valeur par défaut est anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Plateforme '{0}' non reconnue, les valeurs valides sont 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' et 'anycpu'
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Plateforme '{0}' non reconnue, les valeurs valides sont 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' et 'anycpu'
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf
index 5a6035a7014..8a36ec3b730 100644
--- a/src/Compiler/xlf/FSComp.txt.it.xlf
+++ b/src/Compiler/xlf/FSComp.txt.it.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Limita le piattaforme in cui è possibile eseguire il codice: x86, Itanium, x64, anycpu32bitpreferred o anycpu. Il valore predefinito è anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Limita le piattaforme in cui è possibile eseguire il codice: x86, Itanium, x64, anycpu32bitpreferred o anycpu. Il valore predefinito è anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Piattaforma '{0}' non riconosciuta. I valori validi sono 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' e 'anycpu'
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Piattaforma '{0}' non riconosciuta. I valori validi sono 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' e 'anycpu'
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf
index d8f1be72618..2509bc7585a 100644
--- a/src/Compiler/xlf/FSComp.txt.ja.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ja.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        このコードが実行されるプラットフォームの制限: x86、Itanium、x64、anycpu32bitpreferred、または anycpu。既定は anycpu です。
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        このコードが実行されるプラットフォームの制限: x86、Itanium、x64、anycpu32bitpreferred、または anycpu。既定は anycpu です。
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        認識されないプラットフォーム '{0}'。有効な値は 'x86'、'x64'、'Itanium'、'anycpu32bitpreferred'、および 'anycpu' です。
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        認識されないプラットフォーム '{0}'。有効な値は 'x86'、'x64'、'Itanium'、'anycpu32bitpreferred'、および 'anycpu' です。
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf
index 0cd7929d6ff..8f209c3ada1 100644
--- a/src/Compiler/xlf/FSComp.txt.ko.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ko.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        이 코드를 실행할 수 있는 플랫폼을 x86, Itanium, x64, anycpu32bitpreferred 또는 anycpu로 제한합니다. 기본값은 anycpu입니다.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        이 코드를 실행할 수 있는 플랫폼을 x86, Itanium, x64, anycpu32bitpreferred 또는 anycpu로 제한합니다. 기본값은 anycpu입니다.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        인식할 수 없는 플랫폼 '{0}'입니다. 올바른 값은 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' 및 'anycpu'입니다.
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        인식할 수 없는 플랫폼 '{0}'입니다. 올바른 값은 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' 및 'anycpu'입니다.
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf
index f92eed64abc..9539f9af34f 100644
--- a/src/Compiler/xlf/FSComp.txt.pl.xlf
+++ b/src/Compiler/xlf/FSComp.txt.pl.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Ogranicz platformy, na jakich można uruchomić ten kod: x86, Itanium, x64, anycpu32bitpreferred lub anycpu. Domyślna platforma to anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Ogranicz platformy, na jakich można uruchomić ten kod: x86, Itanium, x64, anycpu32bitpreferred lub anycpu. Domyślna platforma to anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Nierozpoznana platforma „{0}”. Prawidłowe wartości to „x86”, „x64”, „Itanium”, „anycpu32bitpreferred” i „anycpu”
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Nierozpoznana platforma „{0}”. Prawidłowe wartości to „x86”, „x64”, „Itanium”, „anycpu32bitpreferred” i „anycpu”
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
index 0c6ceac112a..29bcce01250 100644
--- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
+++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Limite em quais plataformas este código pode ser executado: x86, Itanium, x64, anycpu32bitpreferred ou anycpu. O padrão é anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Limite em quais plataformas este código pode ser executado: x86, Itanium, x64, anycpu32bitpreferred ou anycpu. O padrão é anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Plataforma não reconhecida '{0}', os valores válidos são 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' e 'anycpu'
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Plataforma não reconhecida '{0}', os valores válidos são 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' e 'anycpu'
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf
index 22b93f16437..1fb3a354f86 100644
--- a/src/Compiler/xlf/FSComp.txt.ru.xlf
+++ b/src/Compiler/xlf/FSComp.txt.ru.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Выберите платформы, на которых может выполняться этот код: x86, Itanium, x64, anycpu32bitpreferred или anycpu. По умолчанию используется любой процессор (anycpu).
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Выберите платформы, на которых может выполняться этот код: x86, Itanium, x64, anycpu32bitpreferred или anycpu. По умолчанию используется любой процессор (anycpu).
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Нераспознанная платформа "{0}"; допустимые значения: x86, x64, Itanium, anycpu32bitpreferred и anycpu
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Нераспознанная платформа "{0}"; допустимые значения: x86, x64, Itanium, anycpu32bitpreferred и anycpu
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf
index 3a720bf0f0d..d36bd224b9d 100644
--- a/src/Compiler/xlf/FSComp.txt.tr.xlf
+++ b/src/Compiler/xlf/FSComp.txt.tr.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        Bu kodun üzerinde çalışabileceği platformları sınırlandırın: x86, Itanium, x64, anycpu32bitpreferred veya anycpu. Varsayılan: anycpu.
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        Bu kodun üzerinde çalışabileceği platformları sınırlandırın: x86, Itanium, x64, anycpu32bitpreferred veya anycpu. Varsayılan: anycpu.
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        Tanınmayan platform '{0}', geçerli değerler: 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' ve 'anycpu'
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        Tanınmayan platform '{0}', geçerli değerler: 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred' ve 'anycpu'
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
index f9eea243b93..a1b2b225415 100644
--- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
+++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        限制可以运行此代码的平台: x86、Itanium、x64、anycpu32bitpreferred 或 anycpu。默认值为 anycpu。
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        限制可以运行此代码的平台: x86、Itanium、x64、anycpu32bitpreferred 或 anycpu。默认值为 anycpu。
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        无法识别的平台“{0}”,有效值为“x86”、“x64”、“Itanium”、“anycpu32bitpreferred”和“anycpu”
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        无法识别的平台“{0}”,有效值为“x86”、“x64”、“Itanium”、“anycpu32bitpreferred”和“anycpu”
         
       
       
diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
index d05d8005fea..b8854a10e66 100644
--- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
+++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
@@ -5008,8 +5008,8 @@
         
       
       
-        Limit which platforms this code can run on: x86, Itanium, x64, anycpu32bitpreferred, or anycpu. The default is anycpu.
-        限制這個程式碼可以在哪些平台執行: x86、Itanium、x64、anycpu32bitpreferred 或 anycpu。預設為 anycpu。
+        Limit which platforms this code can run on: x86, x64, Arm, Arm64, Itanium, anycpu32bitpreferred, or anycpu. The default is anycpu.
+        限制這個程式碼可以在哪些平台執行: x86、Itanium、x64、anycpu32bitpreferred 或 anycpu。預設為 anycpu。
         
       
       
@@ -5303,8 +5303,8 @@
         
       
       
-        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
-        無法辨識的平台 '{0}',有效的值是 'x86'、'x64'、'Itanium'、'anycpu32bitpreferred' 和 'anycpu'
+        Unrecognized platform '{0}', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'. The default is anycpu.
+        無法辨識的平台 '{0}',有效的值是 'x86'、'x64'、'Itanium'、'anycpu32bitpreferred' 和 'anycpu'
         
       
       
diff --git a/src/FSharp.Build/Fsc.fs b/src/FSharp.Build/Fsc.fs
index 76447c2f961..436f481d7c5 100644
--- a/src/FSharp.Build/Fsc.fs
+++ b/src/FSharp.Build/Fsc.fs
@@ -197,6 +197,8 @@ type public Fsc() as this =
             | "ANYCPU", _, _ -> "anycpu"
             | "X86", _, _ -> "x86"
             | "X64", _, _ -> "x64"
+            | "ARM", _, _ -> "arm"
+            | "ARM64", _, _ -> "arm64"
             | _ -> null
         )
 
diff --git a/tests/FSharp.Compiler.ComponentTests/CompilerOptions/fsc/platform.fs b/tests/FSharp.Compiler.ComponentTests/CompilerOptions/fsc/platform.fs
index 0ad8d24603f..8063e7aa5b2 100644
--- a/tests/FSharp.Compiler.ComponentTests/CompilerOptions/fsc/platform.fs
+++ b/tests/FSharp.Compiler.ComponentTests/CompilerOptions/fsc/platform.fs
@@ -48,7 +48,7 @@ module platform =
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'ITANIUM', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'ITANIUM', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_03.fs - --platform:ITANIUM`` compilation =
         compilation
@@ -57,11 +57,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'ITANIUM', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'ITANIUM', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'ANYCPU', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'ANYCPU', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_04.fs - --platform:ANYCPU`` compilation =
         compilation
@@ -70,11 +70,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'ANYCPU', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'ANYCPU', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'X86', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'X86', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_05.fs - --platform:X86`` compilation =
         compilation
@@ -83,11 +83,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'X86', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'X86', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'X64', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'X64', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_06.fs - --platform:X64`` compilation =
         compilation
@@ -96,11 +96,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'X64', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'X64', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'IA64', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'IA64', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_07.fs - --platform:IA64`` compilation =
         compilation
@@ -109,11 +109,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'IA64', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'IA64', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'i386', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'i386', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_08.fs - --platform:i386`` compilation =
         compilation
@@ -122,11 +122,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'i386', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'i386', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'AMD64', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'AMD64', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_09.fs - --platform:AMD64`` compilation =
         compilation
@@ -135,11 +135,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'AMD64', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'AMD64', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'PPC', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'PPC', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_10.fs - --platform:PPC`` compilation =
         compilation
@@ -148,11 +148,11 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'PPC', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'PPC', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
-    //Unrecognized platform 'ARM', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
+    //Unrecognized platform 'ARM', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'
     []
     let ``platform - error_15.fs - --platform:ARM`` compilation =
         compilation
@@ -161,7 +161,7 @@ module platform =
         |> compile
         |> shouldFail
         |> withErrorCode 1064
-        |> withDiagnosticMessageMatches "Unrecognized platform 'ARM', valid values are 'x86', 'x64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
+        |> withDiagnosticMessageMatches "Unrecognized platform 'ARM', valid values are 'x86', 'x64', 'Arm', 'Arm64', 'Itanium', 'anycpu32bitpreferred', and 'anycpu'"
         |> ignore
 
     // This test was automatically generated (moved from FSharpQA suite - CompilerOptions/fsc/platform)
diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForDll.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForDll.fs
new file mode 100644
index 00000000000..523cd90e06d
--- /dev/null
+++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForDll.fs
@@ -0,0 +1,7 @@
+open System
+open System.IO
+open System.Reflection
+
+let path = Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location)
+let asm = AssemblyName.GetAssemblyName(Path.Combine(path, "PlatformedDll.dll"))
+printfn "%s" (asm.ToString())
diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForExe.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForExe.fs
new file mode 100644
index 00000000000..a80beeb0300
--- /dev/null
+++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/AssemblyNameForExe.fs
@@ -0,0 +1,7 @@
+open System
+open System.IO
+open System.Reflection
+
+let path = Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location)
+let asm = AssemblyName.GetAssemblyName(Path.Combine(path, "PlatformedExe.exe"))
+printfn "%s" (asm.ToString())
\ No newline at end of file
diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/Platform.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/Platform.fs
new file mode 100644
index 00000000000..0094d9fdb70
--- /dev/null
+++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/Platform.fs
@@ -0,0 +1,136 @@
+namespace FSharp.Compiler.ComponentTests.EmittedIL
+
+open Xunit
+open FSharp.Test
+open FSharp.Test.Compiler
+open System.Runtime.InteropServices
+
+module Platform =
+
+    let isArm =
+        match System.Runtime.InteropServices.Architecture() with
+        | Architecture.Arm | Architecture.Arm64 -> true
+        | _ -> false
+
+    let buildPlatformedDll =
+        FsFromPath (__SOURCE_DIRECTORY__ ++ "PlatformedDll.fs")
+        |> asLibrary
+        |> withName "PlatformedDll.dll"
+
+    let buildPlatformedExe =
+        FsFromPath (__SOURCE_DIRECTORY__ ++ "PlatformedExe.fs")
+        |> asExe
+        |> withName "PlatformedExe.exe"
+
+    []
+    let platformExeAnyCpuDefault compilation =
+        compilation
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.Anycpu ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformExeAnyCpu32BitPreferred compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.AnyCpu32bitPreferred ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformExeArm compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.Arm ]
+        |>  if isArm then compileExeAndRun else compile
+        |> shouldSucceed
+
+    []
+    let platformExeArm64 compilation =
+        compilation
+        |> asExe
+        |> withPlatform ExecutionPlatform.Arm64
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.Arm64 ]
+        |>  if isArm then compileExeAndRun else compile
+        |> shouldSucceed
+
+    []
+    let platformExeItanium compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.Itanium ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformExeX86 compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.X86 ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformExeX64 compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedExe |> withPlatform ExecutionPlatform.X64 ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformDllDefault compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedDll ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformDllAnyCpuDefault compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedDll |> withPlatform ExecutionPlatform.Anycpu ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformDllArm compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedDll |> withPlatform ExecutionPlatform.Arm ]
+        |>  if isArm then compileExeAndRun else compile
+        |> shouldSucceed
+
+    []
+    let platformDllArm64 compilation =
+        compilation
+        |> asExe
+        |> withPlatform ExecutionPlatform.Arm64
+        |> withReferences [ buildPlatformedDll |> withPlatform ExecutionPlatform.Arm64 ]
+        |>  if isArm then compileExeAndRun else compile
+        |> shouldSucceed
+
+    []
+    let platformDllItanium compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedDll |> withPlatform ExecutionPlatform.Itanium ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformDllX86 compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedDll |> withPlatform ExecutionPlatform.X86 ]
+        |> compileExeAndRun
+        |> shouldSucceed
+
+    []
+    let platformDllX64 compilation =
+        compilation
+        |> asExe
+        |> withReferences [ buildPlatformedDll |> withPlatform ExecutionPlatform.X64 ]
+        |> compileExeAndRun
+        |> shouldSucceed
diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedDll.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedDll.fs
new file mode 100644
index 00000000000..8514772167a
--- /dev/null
+++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedDll.fs
@@ -0,0 +1,3 @@
+namespace Platformed
+
+type aType = unit
\ No newline at end of file
diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedExe.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedExe.fs
new file mode 100644
index 00000000000..f3c7decf4b6
--- /dev/null
+++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/Platform/PlatformedExe.fs
@@ -0,0 +1 @@
+printfn "Hello, World"
diff --git a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj
index a91bb7b00a2..5caee3b58ac 100644
--- a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj
+++ b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj
@@ -18,12 +18,6 @@
   
 
   
-	
-		%(RelativeDir)\TestSource\%(Filename)%(Extension)
-	
-	
-		%(RelativeDir)\TestSource\%(Filename)%(Extension)
-	
     
     
     
@@ -93,8 +87,8 @@
     
     
     
-    
     
+    
     
     
     
@@ -117,6 +111,7 @@
     
     
     
+    
     
     
     
@@ -160,8 +155,8 @@
     
     
     
-	
-	
+    
+    
     
     
     
@@ -185,11 +180,12 @@
     
       %(RelativeDir)\BaseLine\%(Filename)%(Extension)
     
-  
-  
-    
-    
-    
+    
+      %(RelativeDir)\TestSource\%(Filename)%(Extension)
+    
+    
+      %(RelativeDir)\TestSource\%(Filename)%(Extension)
+    
   
   
     
diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs
index 68ce2aff88f..6408f14dae0 100644
--- a/tests/FSharp.Test.Utilities/Compiler.fs
+++ b/tests/FSharp.Test.Utilities/Compiler.fs
@@ -452,7 +452,7 @@ module rec Compiler =
                 match platform with
                 | ExecutionPlatform.Anycpu -> "anycpu"
                 | ExecutionPlatform.AnyCpu32bitPreferred -> "anycpu32bitpreferred"
-                | ExecutionPlatform.Itanium -> "itanium"
+                | ExecutionPlatform.Itanium -> "Itanium"
                 | ExecutionPlatform.X64 -> "x64"
                 | ExecutionPlatform.X86 -> "x86"
                 | ExecutionPlatform.Arm -> "arm"
diff --git a/tests/FSharp.Test.Utilities/ILChecker.fs b/tests/FSharp.Test.Utilities/ILChecker.fs
index 117f2bb42ae..a0b1ddefdfe 100644
--- a/tests/FSharp.Test.Utilities/ILChecker.fs
+++ b/tests/FSharp.Test.Utilities/ILChecker.fs
@@ -83,11 +83,19 @@ module ILChecker =
                     RegexOptions.Singleline)
             pass3
 
+        let unifyImageBase ilCode =
+            Regex.Replace(
+                ilCode,
+                "\.imagebase\s*0x\d*",".imagebase {value}",
+                RegexOptions.Singleline)
+
         let unifyIlText (text:string) =
             let unifyingAssemblyNames (text:string)=
                 let asmName = Path.GetFileNameWithoutExtension(dllFilePath)
                 text.Replace(asmName, "assembly")
                 |> unifyRuntimeAssemblyName
+                |> unifyImageBase
+
             text.Trim() |> stripComments |> unifyingAssemblyNames
 
         let raw = File.ReadAllText(ilFilePath)
diff --git a/tests/fsharp/test.fs b/tests/fsharp/test.fs
new file mode 100644
index 00000000000..c0bff1e649f
--- /dev/null
+++ b/tests/fsharp/test.fs
@@ -0,0 +1,54 @@
+
+let fail msg =
+    printfn "%s" msg
+    failwith msg
+
+[]
+type T1 = { v1: int }
+and T2 = 
+  | T2C1 of int * string
+  | T2C2 of T1 * T2
+and [] T3 = { v3: T2 }
+and T4() =
+    let mutable _v4 = { v3 = T2C2({v1=0}, T2C1(1, "hey")) }
+    member __.v4 with get() = _v4 and set (x) = _v4 <- x
+
+[] 
+let (|P1|_|) =
+    function
+    | 0 -> ValueNone
+    | _ -> ValueSome()
+
+[] 
+let (|P2|_|) =
+    function
+    | "foo" -> ValueNone
+    | _ -> ValueSome "bar"
+
+[] 
+let (|P3|_|) (x: T2) =
+  match x with
+  | T2C1(a, b) -> ValueSome(a, b)
+  | _ -> ValueNone
+
+[] 
+let (|P4|_|) (x: T4) =
+  match x.v4 with
+  | { v3 = T2C2 ({v1=a}, P3(b, c)) } -> ValueSome (a, b, c)
+  | _ -> ValueNone
+
+match 0, 1 with
+| P1, _ -> fail "unit"
+| _, P1 -> ()
+| _     -> fail "unit"
+
+match "foo", "bar" with
+| P2 _, _ -> fail "string"
+| _, P2("bar") -> ()
+| _ -> fail "string"
+
+let t4 = T4()
+match t4 with
+| P4 (0, 1, "hey") -> ()
+| _ -> fail "nested"
+            
\ No newline at end of file
diff --git a/tests/fsharp/test.fsx b/tests/fsharp/test.fsx
new file mode 100644
index 00000000000..309111e6b4b
--- /dev/null
+++ b/tests/fsharp/test.fsx
@@ -0,0 +1,3 @@
+
+#load @"C:\kevinransom\fsharp\tests\fsharp\Compiler\Language\../../typeProviders/helloWorld\provider.fsx"
+        
\ No newline at end of file
diff --git a/tests/fsharpqa/Source/CompilerOptions/fsc/help/help40.437.1033.bsl b/tests/fsharpqa/Source/CompilerOptions/fsc/help/help40.437.1033.bsl
index 7e7c4a13a99..2e41efee8f2 100644
--- a/tests/fsharpqa/Source/CompilerOptions/fsc/help/help40.437.1033.bsl
+++ b/tests/fsharpqa/Source/CompilerOptions/fsc/help/help40.437.1033.bsl
@@ -21,9 +21,9 @@ Copyright (c) Microsoft Corporation. All Rights Reserved.
                                          the given file
 --keyfile:                         Specify a strong name key file
 --platform:                      Limit which platforms this code can
-                                         run on: x86, Itanium, x64,
-                                         anycpu32bitpreferred, or anycpu. The
-                                         default is anycpu.
+                                         run on: x86, x64, Arm, Arm64,
+                                         Itanium, anycpu32bitpreferred, or
+                                         anycpu. The default is anycpu.
 --nooptimizationdata                     Only include optimization
                                          information essential for
                                          implementing inlined constructs.

From 778b04c08b8e4646ed328f2c9db70cd6e5a7cd6a Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 30 May 2022 14:35:31 +0100
Subject: [PATCH 17/20] update fantomas (#13206)

---
 .config/dotnet-tools.json                     |  2 +-
 src/Compiler/AbstractIL/il.fs                 |  4 +--
 src/Compiler/AbstractIL/ilmorph.fs            |  4 +--
 src/Compiler/AbstractIL/ilnativeres.fs        |  8 +++---
 src/Compiler/AbstractIL/ilread.fs             | 25 ++++++++---------
 src/Compiler/AbstractIL/ilreflect.fs          | 12 ++++----
 src/Compiler/AbstractIL/ilsign.fs             |  3 +-
 src/Compiler/AbstractIL/ilsupp.fs             | 12 ++++----
 src/Compiler/AbstractIL/ilwritepdb.fs         | 20 ++++++-------
 src/Compiler/Utilities/FileSystem.fs          | 11 ++++----
 src/Compiler/Utilities/illib.fs               | 18 ++++++------
 src/Compiler/Utilities/illib.fsi              | 15 ++++++----
 src/Compiler/Utilities/range.fs               |  8 +++---
 src/Compiler/Utilities/sformat.fs             | 25 ++++++++---------
 .../CreateFSharpManifestResourceName.fs       |  4 +--
 src/FSharp.Build/FSharpEmbedResXSource.fs     |  3 +-
 src/FSharp.Build/FSharpEmbedResourceText.fs   |  6 ++--
 src/FSharp.Build/Fsc.fs                       |  7 ++---
 src/FSharp.Build/Fsi.fs                       |  7 ++---
 src/FSharp.Build/SubstituteText.fs            |  4 +--
 src/FSharp.Build/WriteCodeFragment.fs         |  3 +-
 .../fsiaux.fs                                 |  4 +--
 src/FSharp.Core/event.fsi                     |  3 +-
 src/FSharp.Core/local.fsi                     |  5 ++--
 src/FSharp.Core/map.fsi                       | 10 ++++---
 src/FSharp.Core/seqcore.fsi                   |  4 +--
 src/FSharp.Core/set.fsi                       |  8 +++---
 .../FSharp.DependencyManager.ProjectFile.fs   |  4 +--
 .../FSharp.DependencyManager.fs               | 12 ++++----
 src/fsc/fscmain.fs                            |  7 ++---
 src/fsi/console.fs                            |  7 ++---
 src/fsi/fsimain.fs                            | 28 ++++++++-----------
 32 files changed, 139 insertions(+), 154 deletions(-)

diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json
index 0dd72130c0c..057b9c816db 100644
--- a/.config/dotnet-tools.json
+++ b/.config/dotnet-tools.json
@@ -3,7 +3,7 @@
   "isRoot": true,
   "tools": {
     "fantomas": {
-      "version": "5.0.0-alpha-006",
+      "version": "5.0.0-alpha-008",
       "commands": [
         "fantomas"
       ]
diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs
index 3afa9326f47..30f920524c0 100644
--- a/src/Compiler/AbstractIL/il.fs
+++ b/src/Compiler/AbstractIL/il.fs
@@ -5143,8 +5143,8 @@ let decodeILAttribData (ca: ILAttribute) =
                     try
                         let parser = ILTypeSigParser n
                         parser.ParseTypeSpec(), sigptr
-                    with
-                    | exn -> failwith (sprintf "decodeILAttribData: error parsing type in custom attribute blob: %s" exn.Message)
+                    with exn ->
+                        failwith (sprintf "decodeILAttribData: error parsing type in custom attribute blob: %s" exn.Message)
             | ILType.Boxed tspec when tspec.Name = "System.Object" ->
                 let et, sigptr = sigptr_get_u8 bytes sigptr
 
diff --git a/src/Compiler/AbstractIL/ilmorph.fs b/src/Compiler/AbstractIL/ilmorph.fs
index dd60d94d0fc..08bd3a292c6 100644
--- a/src/Compiler/AbstractIL/ilmorph.fs
+++ b/src/Compiler/AbstractIL/ilmorph.fs
@@ -184,8 +184,8 @@ let cattr_ty2ty f (c: ILAttribute) =
             let elems = elems |> List.map (celem_ty2ty f)
             let namedArgs = namedArgs |> List.map (cnamedarg_ty2ty f)
             mkILCustomAttribMethRef (meth, elems, namedArgs)
-        with
-        | _ -> c.WithMethod(meth)
+        with _ ->
+            c.WithMethod(meth)
     else
         c.WithMethod(meth)
 
diff --git a/src/Compiler/AbstractIL/ilnativeres.fs b/src/Compiler/AbstractIL/ilnativeres.fs
index 8bbf79f9afc..f403a6c5985 100644
--- a/src/Compiler/AbstractIL/ilnativeres.fs
+++ b/src/Compiler/AbstractIL/ilnativeres.fs
@@ -253,8 +253,8 @@ type COFFResourceReader() =
 
             if int64 relocLastAddress > stream.Length then
                 raise <| ResourceException "CoffResourceInvalidRelocation"
-        with
-        | :? OverflowException -> (raise <| ResourceException("CoffResourceInvalidRelocation"))
+        with :? OverflowException ->
+            (raise <| ResourceException("CoffResourceInvalidRelocation"))
 
         let mutable relocationOffsets = Array.zeroCreate (int rsrc1.NumberOfRelocations)
 
@@ -284,8 +284,8 @@ type COFFResourceReader() =
 
             if lastSymAddress > stream.Length then
                 raise <| ResourceException "CoffResourceInvalidSymbol"
-        with
-        | :? OverflowException -> (raise <| ResourceException("CoffResourceInvalidSymbol"))
+        with :? OverflowException ->
+            (raise <| ResourceException("CoffResourceInvalidSymbol"))
 
         let mutable outputStream = new MemoryStream(imageResourceSectionBytes)
         let mutable writer = new BinaryWriter(outputStream)
diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs
index 022d5eeb839..f9401396e73 100644
--- a/src/Compiler/AbstractIL/ilread.fs
+++ b/src/Compiler/AbstractIL/ilread.fs
@@ -41,15 +41,15 @@ let _ =
 let noStableFileHeuristic =
     try
         (Environment.GetEnvironmentVariable("FSharp_NoStableFileHeuristic") <> null)
-    with
-    | _ -> false
+    with _ ->
+        false
 
 let alwaysMemoryMapFSC =
     try
         (Environment.GetEnvironmentVariable("FSharp_AlwaysMemoryMapCommandLineCompiler")
          <> null)
-    with
-    | _ -> false
+    with _ ->
+        false
 
 let stronglyHeldReaderCacheSizeDefault = 30
 
@@ -58,8 +58,8 @@ let stronglyHeldReaderCacheSize =
         (match Environment.GetEnvironmentVariable("FSharp_StronglyHeldBinaryReaderCacheSize") with
          | null -> stronglyHeldReaderCacheSizeDefault
          | s -> int32 s)
-    with
-    | _ -> stronglyHeldReaderCacheSizeDefault
+    with _ ->
+        stronglyHeldReaderCacheSizeDefault
 
 let singleOfBits (x: int32) =
     BitConverter.ToSingle(BitConverter.GetBytes x, 0)
@@ -3804,8 +3804,7 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (idx, nm, _int
                                     [] (*  scopes fail for mscorlib  scopes rootScope *)
                                 // REVIEW: look through sps to get ranges? Use GetRanges?? Change AbsIL??
                                 (localPdbInfos, None, seqpoints)
-                            with
-                            | e ->
+                            with e ->
                                 // "* Warning: PDB info for method "+nm+" could not be read and will be ignored: "+e.Message
                                 [], None, []
 #endif
@@ -4222,8 +4221,7 @@ let getPdbReader pdbDirPath fileName =
                 | _ -> failwith ("Document with URL " + url + " not found in list of documents in the PDB file")
 
             Some(pdbr, docfun)
-        with
-        | e ->
+        with e ->
             dprintn ("* Warning: PDB file could not be read and will be ignored: " + e.Message)
             None
 #endif
@@ -5111,8 +5109,8 @@ let stableFileHeuristicApplies fileName =
     not noStableFileHeuristic
     && try
         FileSystem.IsStableFileHeuristic fileName
-       with
-       | _ -> false
+       with _ ->
+           false
 
 let createByteFileChunk opts fileName chunk =
     // If we're trying to reduce memory usage then we are willing to go back and re-read the binary, so we can use
@@ -5184,8 +5182,7 @@ let OpenILModuleReader fileName opts =
                 ILModuleReaderCacheKey(fullPath, writeTime, opts.pdbDirPath.IsSome, opts.reduceMemoryUsage, opts.metadataOnly)
 
             key, true
-        with
-        | exn ->
+        with exn ->
             Debug.Assert(
                 false,
                 sprintf
diff --git a/src/Compiler/AbstractIL/ilreflect.fs b/src/Compiler/AbstractIL/ilreflect.fs
index 574075ea6b8..63497ca4d4c 100644
--- a/src/Compiler/AbstractIL/ilreflect.fs
+++ b/src/Compiler/AbstractIL/ilreflect.fs
@@ -646,8 +646,8 @@ let envUpdateCreatedTypeRef emEnv (tref: ILTypeRef) =
             try
                 System.Runtime.Serialization.FormatterServices.GetUninitializedObject ty
                 |> ignore
-            with
-            | _ -> ()
+            with _ ->
+                ()
 #endif
         { emEnv with
             emTypMap = Zmap.add tref (typT, typB, typeDef, Some ty) emEnv.emTypMap
@@ -1117,8 +1117,8 @@ let queryableTypeGetMethod cenv emEnv parentT (mref: ILMethodRef) : MethodInfo =
                     (null: ParameterModifier[])
                 )
             // This can fail if there is an ambiguity w.r.t. return type
-            with
-            | _ -> null
+            with _ ->
+                null
 
         if (isNotNull methInfo && equalTypes resT methInfo.ReturnType) then
             methInfo
@@ -2759,8 +2759,8 @@ let EmitDynamicAssemblyFragment
         try
             ignore (typB.InvokeMemberAndLog(methodName, BindingFlags.InvokeMethod ||| BindingFlags.Public ||| BindingFlags.Static, [||]))
             None
-        with
-        | :? TargetInvocationException as exn -> Some exn.InnerException
+        with :? TargetInvocationException as exn ->
+            Some exn.InnerException
 
     let emEnv, entryPts = envPopEntryPts emEnv
     let execs = List.map execEntryPtFun entryPts
diff --git a/src/Compiler/AbstractIL/ilsign.fs b/src/Compiler/AbstractIL/ilsign.fs
index b4eb67b8acb..bcd64cb28d4 100644
--- a/src/Compiler/AbstractIL/ilsign.fs
+++ b/src/Compiler/AbstractIL/ilsign.fs
@@ -704,8 +704,7 @@ type ILStrongNameSigner =
         let pkSignatureSize pk =
             try
                 signerSignatureSize pk
-            with
-            | exn ->
+            with exn ->
                 failwith ("A call to StrongNameSignatureSize failed (" + exn.Message + ")")
                 0x80
 
diff --git a/src/Compiler/AbstractIL/ilsupp.fs b/src/Compiler/AbstractIL/ilsupp.fs
index 30050580dd5..fea9a764487 100644
--- a/src/Compiler/AbstractIL/ilsupp.fs
+++ b/src/Compiler/AbstractIL/ilsupp.fs
@@ -1039,8 +1039,8 @@ let pdbClose (writer: PdbWriter) dllFilename pdbFilename =
                 FileSystem.OpenFileForWriteShim(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
 
             false
-        with
-        | _ -> true
+        with _ ->
+            true
 
     let mutable attempts = 0
 
@@ -1072,8 +1072,8 @@ let internal setCheckSum (url: string, writer: ISymUnmanagedDocumentWriter) =
 
         if (checkSum.Length = hashSizeOfMD5) then
             writer.SetCheckSum(guidSourceHashMD5, hashSizeOfMD5, checkSum)
-    with
-    | _ -> ()
+    with _ ->
+        ()
 
 let pdbDefineDocument (writer: PdbWriter) (url: string) =
     //3F5162F8-07C6-11D3-9053-00C04FA302A1
@@ -1204,8 +1204,8 @@ let pdbReadOpen (moduleName: string) (path: string) : PdbReader =
                 )
 
             { symReader = reader :?> ISymbolReader }
-        with
-        | _ -> { symReader = null }
+        with _ ->
+            { symReader = null }
 #else
         let symbolBinder = new System.Diagnostics.SymbolStore.SymBinder()
 
diff --git a/src/Compiler/AbstractIL/ilwritepdb.fs b/src/Compiler/AbstractIL/ilwritepdb.fs
index df3ff04764c..3988e136dd8 100644
--- a/src/Compiler/AbstractIL/ilwritepdb.fs
+++ b/src/Compiler/AbstractIL/ilwritepdb.fs
@@ -184,8 +184,8 @@ let checkSum (url: string) (checksumAlgorithm: HashAlgorithm) =
 
         let checkSum = alg.ComputeHash file
         Some(guid, checkSum)
-    with
-    | _ -> None
+    with _ ->
+        None
 
 //---------------------------------------------------------------------
 // Portable PDB Writer
@@ -935,14 +935,14 @@ let writePdbInfo showTimes outfile pdbfile info cvChunk =
 
     try
         FileSystem.FileDeleteShim pdbfile
-    with
-    | _ -> ()
+    with _ ->
+        ()
 
     let pdbw =
         try
             pdbInitialize outfile pdbfile
-        with
-        | _ -> error (Error(FSComp.SR.ilwriteErrorCreatingPdb pdbfile, rangeCmdArgs))
+        with _ ->
+            error (Error(FSComp.SR.ilwriteErrorCreatingPdb pdbfile, rangeCmdArgs))
 
     match info.EntryPoint with
     | None -> ()
@@ -1119,16 +1119,16 @@ let writeMdbInfo fmdb f info =
     // Note, if we can't delete it code will fail later
     try
         FileSystem.FileDeleteShim fmdb
-    with
-    | _ -> ()
+    with _ ->
+        ()
 
     // Try loading the MDB symbol writer from an assembly available on Mono dynamically
     // Report an error if the assembly is not available.
     let wr =
         try
             createWriter f
-        with
-        | _ -> error (Error(FSComp.SR.ilwriteErrorCreatingMdb (), rangeCmdArgs))
+        with _ ->
+            error (Error(FSComp.SR.ilwriteErrorCreatingMdb (), rangeCmdArgs))
 
     // NOTE: MonoSymbolWriter doesn't need information about entrypoints, so 'info.EntryPoint' is unused here.
     // Write information about Documents. Returns '(SourceFileEntry*CompileUnitEntry)[]'
diff --git a/src/Compiler/Utilities/FileSystem.fs b/src/Compiler/Utilities/FileSystem.fs
index 5296f567cde..bdd64e87b69 100644
--- a/src/Compiler/Utilities/FileSystem.fs
+++ b/src/Compiler/Utilities/FileSystem.fs
@@ -381,12 +381,11 @@ module MemoryMappedFileExtensions =
                     use stream = mmf.CreateViewStream(0L, length, MemoryMappedFileAccess.ReadWrite)
                     copyTo stream
                     Some mmf
-                with
-                | _ ->
+                with _ ->
                     mmf.Dispose()
                     None
-            with
-            | _ -> None
+            with _ ->
+                None
 
     type MemoryMappedFile with
 
@@ -615,8 +614,8 @@ type DefaultFileSystem() as this =
                 ifs.GetFullPathShim path
             else
                 path
-        with
-        | _ -> path
+        with _ ->
+            path
 
     abstract IsInvalidPathShim: path: string -> bool
 
diff --git a/src/Compiler/Utilities/illib.fs b/src/Compiler/Utilities/illib.fs
index f9cfe5ce0e8..85e8b8d9d98 100644
--- a/src/Compiler/Utilities/illib.fs
+++ b/src/Compiler/Utilities/illib.fs
@@ -71,8 +71,7 @@ module internal PervasiveAutoOpens =
         // "How can I detect if am running in Mono?" section
         try
             Type.GetType "Mono.Runtime" <> null
-        with
-        | _ ->
+        with _ ->
             // Must be robust in the case that someone else has installed a handler into System.AppDomain.OnTypeResolveEvent
             // that is not reliable.
             // This is related to bug 5506--the issue is actually a bug in VSTypeResolutionService.EnsurePopulated which is
@@ -373,8 +372,8 @@ module Option =
     let attempt (f: unit -> 'T) =
         try
             Some(f ())
-        with
-        | _ -> None
+        with _ ->
+            None
 
 module List =
 
@@ -991,8 +990,8 @@ module Cancellable =
                 match f ct with
                 | ValueOrCancelled.Value res -> ValueOrCancelled.Value(Choice1Of2 res)
                 | ValueOrCancelled.Cancelled exn -> ValueOrCancelled.Cancelled exn
-            with
-            | err -> ValueOrCancelled.Value(Choice2Of2 err))
+            with err ->
+                ValueOrCancelled.Value(Choice2Of2 err))
 
     /// Implement try/finally for a cancellable computation
     let inline tryFinally comp compensation =
@@ -1162,8 +1161,7 @@ type LazyWithContext<'T, 'Ctxt> =
                 x.value <- res
                 x.funcOrException <- null
                 res
-            with
-            | exn ->
+            with exn ->
                 x.funcOrException <- box (LazyWithContextFailure(exn))
                 reraise ()
         | _ -> failwith "unreachable"
@@ -1281,8 +1279,8 @@ module NameMap =
             (fun n x2 acc ->
                 try
                     f n (Map.find n m1) x2 acc
-                with
-                | :? KeyNotFoundException -> errf n x2)
+                with :? KeyNotFoundException ->
+                    errf n x2)
             m2
             acc
 
diff --git a/src/Compiler/Utilities/illib.fsi b/src/Compiler/Utilities/illib.fsi
index 6f7a0591bdc..4d7ca7a57d6 100644
--- a/src/Compiler/Utilities/illib.fsi
+++ b/src/Compiler/Utilities/illib.fsi
@@ -531,18 +531,21 @@ module internal NameMap =
     val layer: m1: NameMap<'T> -> m2: Map -> Map
 
     /// Not a very useful function - only called in one place - should be changed
-    val layerAdditive: addf: ('a list -> 'b -> 'a list) -> m1: Map<'c, 'b> -> m2: Map<'c, 'a list> -> Map<'c, 'a list>
-        when 'c: comparison
+    val layerAdditive:
+        addf: ('a list -> 'b -> 'a list) -> m1: Map<'c, 'b> -> m2: Map<'c, 'a list> -> Map<'c, 'a list>
+            when 'c: comparison
 
     /// Union entries by identical key, using the provided function to union sets of values
     val union: unionf: (seq<'a> -> 'b) -> ms: seq> -> Map
 
     /// For every entry in m2 find an entry in m1 and fold
-    val subfold2: errf: ('a -> 'b -> 'c) -> f: ('a -> 'd -> 'b -> 'c -> 'c) -> m1: Map<'a, 'd> -> m2: Map<'a, 'b> -> acc: 'c -> 'c
-        when 'a: comparison
+    val subfold2:
+        errf: ('a -> 'b -> 'c) -> f: ('a -> 'd -> 'b -> 'c -> 'c) -> m1: Map<'a, 'd> -> m2: Map<'a, 'b> -> acc: 'c -> 'c
+            when 'a: comparison
 
-    val suball2: errf: ('a -> 'b -> bool) -> p: ('c -> 'b -> bool) -> m1: Map<'a, 'c> -> m2: Map<'a, 'b> -> bool
-        when 'a: comparison
+    val suball2:
+        errf: ('a -> 'b -> bool) -> p: ('c -> 'b -> bool) -> m1: Map<'a, 'c> -> m2: Map<'a, 'b> -> bool
+            when 'a: comparison
 
     val mapFold: f: ('a -> string -> 'T -> 'b * 'a) -> s: 'a -> l: NameMap<'T> -> Map * 'a
 
diff --git a/src/Compiler/Utilities/range.fs b/src/Compiler/Utilities/range.fs
index b8c98f92ada..035a5de80f4 100755
--- a/src/Compiler/Utilities/range.fs
+++ b/src/Compiler/Utilities/range.fs
@@ -363,8 +363,8 @@ type Range(code1: int64, code2: int64) =
                     |> Seq.take (m.EndLine - m.StartLine + 1)
                     |> String.concat "\n"
                     |> fun s -> s.Substring(startCol + 1, s.LastIndexOf("\n", StringComparison.Ordinal) + 1 - startCol + endCol)
-            with
-            | e -> e.ToString()
+            with e ->
+                e.ToString()
 
     member m.ToShortString() =
         sprintf "(%d,%d--%d,%d)" m.StartLine m.StartColumn m.EndLine m.EndColumn
@@ -542,5 +542,5 @@ module Range =
                 match nonEmptyLine with
                 | Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
                 | None -> mkRange file (mkPos 1 0) (mkPos 1 80)
-        with
-        | _ -> mkRange file (mkPos 1 0) (mkPos 1 80)
+        with _ ->
+            mkRange file (mkPos 1 0) (mkPos 1 80)
diff --git a/src/Compiler/Utilities/sformat.fs b/src/Compiler/Utilities/sformat.fs
index d0e0d6fe5ef..1c9a3dd5c51 100644
--- a/src/Compiler/Utilities/sformat.fs
+++ b/src/Compiler/Utilities/sformat.fs
@@ -589,14 +589,14 @@ module Display =
                 ty.GetMethod("ToString", BindingFlags.Public ||| BindingFlags.Instance, null, [||], null)
 
             methInfo.DeclaringType = typeof
-        with
-        | _e -> false
+        with _e ->
+            false
 
     let catchExn f =
         try
             Choice1Of2(f ())
-        with
-        | e -> Choice2Of2 e
+        with e ->
+            Choice2Of2 e
 
     // An implementation of break stack.
     // Uses mutable state, relying on linear threading of the state.
@@ -1039,8 +1039,7 @@ module Display =
 
                             path.Remove(x) |> ignore
                             res
-            with
-            | e ->
+            with e ->
                 countNodes 1
                 wordL (tagText ("Error: " + e.Message))
 
@@ -1157,8 +1156,8 @@ module Display =
                                                 )
                                             )
                                         )
-                            with
-                            | _ -> None
+                            with _ ->
+                                None
 
                 // Seed with an empty layout with a space to the left for formatting purposes
                 buildObjMessageL txt [ leftL (tagText "") ]
@@ -1463,12 +1462,11 @@ module Display =
                                tagProperty m.Name),
                           (try
                               Some(nestedObjL nDepth Precedence.BracketIfTuple ((getProperty ty obj m.Name), ty))
-                           with
-                           | _ ->
+                           with _ ->
                                try
                                    Some(nestedObjL nDepth Precedence.BracketIfTuple ((getField obj (m :?> FieldInfo)), ty))
-                               with
-                               | _ -> None)))
+                               with _ ->
+                                   None)))
                      |> Array.toList
                      |> makePropertiesL)
 
@@ -1594,8 +1592,7 @@ module Display =
                     match text with
                     | null -> ""
                     | _ -> text
-                with
-                | e ->
+                with e ->
                     // If a .ToString() call throws an exception, catch it and use the message as the result.
                     // This may be informative, e.g. division by zero etc...
                     ""
diff --git a/src/FSharp.Build/CreateFSharpManifestResourceName.fs b/src/FSharp.Build/CreateFSharpManifestResourceName.fs
index 175a76d2030..9c86fb7fb7d 100644
--- a/src/FSharp.Build/CreateFSharpManifestResourceName.fs
+++ b/src/FSharp.Build/CreateFSharpManifestResourceName.fs
@@ -35,8 +35,8 @@ type CreateFSharpManifestResourceName public () =
                 let runningOnMono =
                     try
                         System.Type.GetType("Mono.Runtime") <> null
-                    with
-                    | e -> false
+                    with e ->
+                        false
 
                 let fileName =
                     if
diff --git a/src/FSharp.Build/FSharpEmbedResXSource.fs b/src/FSharp.Build/FSharpEmbedResXSource.fs
index 4318ff75e46..59a3930059d 100644
--- a/src/FSharp.Build/FSharpEmbedResXSource.fs
+++ b/src/FSharp.Build/FSharpEmbedResXSource.fs
@@ -118,8 +118,7 @@ module internal {1} =
                 File.WriteAllText(sourcePath, body.ToString())
                 printMessage <| sprintf "Done: %s" sourcePath
                 Some(sourcePath)
-        with
-        | e ->
+        with e ->
             printf "An exception occurred when processing '%s'\n%s" resx (e.ToString())
             None
 
diff --git a/src/FSharp.Build/FSharpEmbedResourceText.fs b/src/FSharp.Build/FSharpEmbedResourceText.fs
index ede5581c02e..77002aa4f54 100644
--- a/src/FSharp.Build/FSharpEmbedResourceText.fs
+++ b/src/FSharp.Build/FSharpEmbedResourceText.fs
@@ -236,8 +236,7 @@ type FSharpEmbedResourceText() =
                 let str =
                     try
                         System.String.Format(Unquote(txt.Substring i)) // Format turns e.g '\n' into that char, but also requires that we 'escape' curlies in the original .txt file, e.g. "{{"
-                    with
-                    | e ->
+                    with e ->
                         Err(
                             fileName,
                             lineNum,
@@ -551,8 +550,7 @@ open Printf
                 xd.Save outXmlStream
                 printMessage (sprintf "Done %s" outFileName)
                 Some(fileName, outFileName, outXmlFileName)
-        with
-        | e ->
+        with e ->
             PrintErr(fileName, 0, sprintf "An exception occurred when processing '%s'\n%s" fileName (e.ToString()))
             None
 
diff --git a/src/FSharp.Build/Fsc.fs b/src/FSharp.Build/Fsc.fs
index 436f481d7c5..d7e0d8c52e4 100644
--- a/src/FSharp.Build/Fsc.fs
+++ b/src/FSharp.Build/Fsc.fs
@@ -68,8 +68,8 @@ type public Fsc() as this =
         let locationOfThisDll =
             try
                 Some(Path.GetDirectoryName(typeof.Assembly.Location))
-            with
-            | _ -> None
+            with _ ->
+                None
 
         match FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(locationOfThisDll) with
         | Some s -> s
@@ -677,8 +677,7 @@ type public Fsc() as this =
 
                 try
                     invokeCompiler baseCallDelegate
-                with
-                | e ->
+                with e ->
                     Debug.Fail(
                         "HostObject received by Fsc task did not have a Compile method or the compile method threw an exception. "
                         + (e.ToString())
diff --git a/src/FSharp.Build/Fsi.fs b/src/FSharp.Build/Fsi.fs
index 634f92023bd..76dec9c78e4 100644
--- a/src/FSharp.Build/Fsi.fs
+++ b/src/FSharp.Build/Fsi.fs
@@ -51,8 +51,8 @@ type public Fsi() as this =
         let locationOfThisDll =
             try
                 Some(Path.GetDirectoryName(typeof.Assembly.Location))
-            with
-            | _ -> None
+            with _ ->
+                None
 
         match FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(locationOfThisDll) with
         | Some s -> s
@@ -361,8 +361,7 @@ type public Fsi() as this =
 
                 try
                     invokeCompiler baseCallDelegate
-                with
-                | e ->
+                with e ->
                     Debug.Assert(
                         false,
                         "HostObject received by Fsi task did not have a Compile method or the compile method threw an exception. "
diff --git a/src/FSharp.Build/SubstituteText.fs b/src/FSharp.Build/SubstituteText.fs
index c4be2300c0f..0f036da0a86 100644
--- a/src/FSharp.Build/SubstituteText.fs
+++ b/src/FSharp.Build/SubstituteText.fs
@@ -87,8 +87,8 @@ type SubstituteText() =
                                     Directory.CreateDirectory(directory) |> ignore
 
                                 File.WriteAllText(targetPath, contents)
-                            with
-                            | _ -> ()
+                            with _ ->
+                                ()
 
                     copiedFiles.Add(item)
 
diff --git a/src/FSharp.Build/WriteCodeFragment.fs b/src/FSharp.Build/WriteCodeFragment.fs
index 7cc80eeafc4..ab390ae4a59 100644
--- a/src/FSharp.Build/WriteCodeFragment.fs
+++ b/src/FSharp.Build/WriteCodeFragment.fs
@@ -166,7 +166,6 @@ type WriteCodeFragment() =
                     _outputFile <- outputFileItem
                     true
 
-            with
-            | e ->
+            with e ->
                 printf "Error writing code fragment: %s" (e.ToString())
                 false
diff --git a/src/FSharp.Compiler.Interactive.Settings/fsiaux.fs b/src/FSharp.Compiler.Interactive.Settings/fsiaux.fs
index bf70d1535b3..89362feb114 100644
--- a/src/FSharp.Compiler.Interactive.Settings/fsiaux.fs
+++ b/src/FSharp.Compiler.Interactive.Settings/fsiaux.fs
@@ -52,8 +52,8 @@ type internal SimpleEventLoop() =
                         result <-
                             try
                                 Some(f ())
-                            with
-                            | _ -> None)
+                            with _ ->
+                                None)
 
                     setSignal doneSignal
                     run ()
diff --git a/src/FSharp.Core/event.fsi b/src/FSharp.Core/event.fsi
index 536b414ba54..12557f0dc22 100644
--- a/src/FSharp.Core/event.fsi
+++ b/src/FSharp.Core/event.fsi
@@ -34,7 +34,8 @@ type DelegateEvent<'Delegate when 'Delegate :> System.Delegate> =
 ///
 /// Events and Observables
 []
-type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct> =
+type Event<'Delegate, 'Args
+    when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct> =
 
     /// Creates an event object suitable for delegate types following the standard .NET Framework convention of a first 'sender' argument.
     /// The created event.
diff --git a/src/FSharp.Core/local.fsi b/src/FSharp.Core/local.fsi
index ddf14b9c2bc..e4d26e76e2b 100644
--- a/src/FSharp.Core/local.fsi
+++ b/src/FSharp.Core/local.fsi
@@ -43,8 +43,9 @@ module internal List =
 
     val distinctWithComparer: System.Collections.Generic.IEqualityComparer<'T> -> 'T list -> 'T list
 
-    val distinctByWithComparer: System.Collections.Generic.IEqualityComparer<'Key> -> ('T -> 'Key) -> list: 'T list -> 'T list
-        when 'Key: equality
+    val distinctByWithComparer:
+        System.Collections.Generic.IEqualityComparer<'Key> -> ('T -> 'Key) -> list: 'T list -> 'T list
+            when 'Key: equality
 
     val init: int -> (int -> 'T) -> 'T list
     val filter: predicate: ('T -> bool) -> 'T list -> 'T list
diff --git a/src/FSharp.Core/map.fsi b/src/FSharp.Core/map.fsi
index d500c9a0ebe..eae4943b5b8 100644
--- a/src/FSharp.Core/map.fsi
+++ b/src/FSharp.Core/map.fsi
@@ -484,8 +484,9 @@ module Map =
     /// Evaluates to "1 a 2 b initial"
     /// 
     []
-    val foldBack<'Key, 'T, 'State> : folder: ('Key -> 'T -> 'State -> 'State) -> table: Map<'Key, 'T> -> state: 'State -> 'State
-        when 'Key: comparison
+    val foldBack<'Key, 'T, 'State> :
+        folder: ('Key -> 'T -> 'State -> 'State) -> table: Map<'Key, 'T> -> state: 'State -> 'State
+            when 'Key: comparison
 
     /// Folds over the bindings in the map 
     ///
@@ -504,8 +505,9 @@ module Map =
     /// Evaluates to "initial 1 a 2 b".
     /// 
     []
-    val fold<'Key, 'T, 'State> : folder: ('State -> 'Key -> 'T -> 'State) -> state: 'State -> table: Map<'Key, 'T> -> 'State
-        when 'Key: comparison
+    val fold<'Key, 'T, 'State> :
+        folder: ('State -> 'Key -> 'T -> 'State) -> state: 'State -> table: Map<'Key, 'T> -> 'State
+            when 'Key: comparison
 
     /// Applies the given function to each binding in the dictionary
     ///
diff --git a/src/FSharp.Core/seqcore.fsi b/src/FSharp.Core/seqcore.fsi
index 162458d8c90..03496378232 100644
--- a/src/FSharp.Core/seqcore.fsi
+++ b/src/FSharp.Core/seqcore.fsi
@@ -100,8 +100,8 @@ module RuntimeHelpers =
     /// The input sequence.
     ///
     /// The result sequence.
-    val EnumerateUsing: resource: 'T -> source: ('T -> 'Collection) -> seq<'U>
-        when 'T :> IDisposable and 'Collection :> seq<'U>
+    val EnumerateUsing:
+        resource: 'T -> source: ('T -> 'Collection) -> seq<'U> when 'T :> IDisposable and 'Collection :> seq<'U>
 
     /// Creates an anonymous event with the given handlers.
     ///
diff --git a/src/FSharp.Core/set.fsi b/src/FSharp.Core/set.fsi
index a564360689f..45056191d24 100644
--- a/src/FSharp.Core/set.fsi
+++ b/src/FSharp.Core/set.fsi
@@ -471,8 +471,8 @@ module Set =
     /// The reverse of the set is [3; 2; 1]
     /// 
     []
-    val fold<'T, 'State> : folder: ('State -> 'T -> 'State) -> state: 'State -> set: Set<'T> -> 'State
-        when 'T: comparison
+    val fold<'T, 'State> :
+        folder: ('State -> 'T -> 'State) -> state: 'State -> set: Set<'T> -> 'State when 'T: comparison
 
     /// Applies the given accumulating function to all the elements of the set.
     ///
@@ -492,8 +492,8 @@ module Set =
     /// The set is [1; 2; 3]
     /// 
     []
-    val foldBack<'T, 'State> : folder: ('T -> 'State -> 'State) -> set: Set<'T> -> state: 'State -> 'State
-        when 'T: comparison
+    val foldBack<'T, 'State> :
+        folder: ('T -> 'State -> 'State) -> set: Set<'T> -> state: 'State -> 'State when 'T: comparison
 
     /// Tests if all elements of the collection satisfy the given predicate.
     /// If the input function is f and the elements are i0...iN and "j0...jN"
diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs
index e0d6660b99b..24ad0c02f38 100644
--- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs
+++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs
@@ -88,8 +88,8 @@ module internal ProjectFile =
                     .ReadAllText(resolutionsFile)
                     .Split([| '\r'; '\n' |], StringSplitOptions.None)
                 |> Array.filter (fun line -> not (String.IsNullOrEmpty(line)))
-            with
-            | _ -> [||]
+            with _ ->
+                [||]
 
         [|
             for line in lines do
diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs
index 794aa591874..e5f8cd95704 100644
--- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs
+++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs
@@ -267,8 +267,8 @@ type FSharpDependencyManager(outputDirectory: string option) =
                     Directory.CreateDirectory(directory) |> ignore
 
                 directory
-            with
-            | _ -> directory
+            with _ ->
+                directory
 
     let deleteScripts () =
         try
@@ -279,16 +279,16 @@ type FSharpDependencyManager(outputDirectory: string option) =
 #else
             ()
 #endif
-        with
-        | _ -> ()
+        with _ ->
+            ()
 
     let emitFile fileName (body: string) =
         try
             // Create a file to write to
             use sw = File.CreateText(fileName)
             sw.WriteLine(body)
-        with
-        | _ -> ()
+        with _ ->
+            ()
 
     let prepareDependencyResolutionFiles
         (
diff --git a/src/fsc/fscmain.fs b/src/fsc/fscmain.fs
index 334f22c36e9..e29f07e8b20 100644
--- a/src/fsc/fscmain.fs
+++ b/src/fsc/fscmain.fs
@@ -81,8 +81,8 @@ let main (argv) =
                 member _.Exit(n) =
                     try
                         exit n
-                    with
-                    | _ -> ()
+                    with _ ->
+                        ()
 
                     failwithf "%s" (FSComp.SR.elSysEnvExitDidntExit ())
             }
@@ -116,8 +116,7 @@ let main (argv) =
 
         0
 
-    with
-    | e ->
+    with e ->
         // Last-chance error recovery (note, with a poor error range)
         errorRecovery e Range.range0
         1
diff --git a/src/fsi/console.fs b/src/fsi/console.fs
index cd84b705b4a..aede59c3889 100644
--- a/src/fsi/console.fs
+++ b/src/fsi/console.fs
@@ -76,8 +76,7 @@ module internal Utils =
     let guard (f) =
         try
             f ()
-        with
-        | e ->
+        with e ->
             warning (
                 Failure(
                     sprintf
@@ -197,8 +196,8 @@ type internal ReadLineConsole() =
                 |> Seq.iter (fun option -> optionsCache.Add(option))
 
                 optionsCache.Root <- root
-            with
-            | _ -> optionsCache.Clear()
+            with _ ->
+                optionsCache.Clear()
 
             optionsCache, true
         else
diff --git a/src/fsi/fsimain.fs b/src/fsi/fsimain.fs
index 9df8f0cead4..6bd2000f9a3 100644
--- a/src/fsi/fsimain.fs
+++ b/src/fsi/fsimain.fs
@@ -139,8 +139,7 @@ let internal TrySetUnhandledExceptionMode () =
 
     try
         Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
-    with
-    | _ ->
+    with _ ->
         decr i
         ()
 
@@ -155,8 +154,7 @@ let StartServer (fsiSession: FsiEvaluationSession) (fsiServerName) =
                 //printf "FSI-SERVER: received CTRL-C request...\n"
                 try
                     fsiSession.Interrupt()
-                with
-                | _ ->
+                with _ ->
                     // Final sanity check! - catch all exns - but not expected
                     assert false
                     ()
@@ -200,8 +198,7 @@ let evaluateSession (argv: string[]) =
                         let _ = Console.ForegroundColor
                         let _ = Console.CursorLeft <- Console.CursorLeft
                         true
-                    with
-                    | _ ->
+                    with _ ->
                         //if progress then fprintfn outWriter "probe failed, we have no console..."
                         false
                 else
@@ -248,8 +245,7 @@ let evaluateSession (argv: string[]) =
             lazy
                 try
                     Some(WinFormsEventLoop())
-                with
-                | e ->
+                with e ->
                     printfn "Your system doesn't seem to support WinForms correctly. You will"
                     printfn "need to set fsi.EventLoop use GUI windows from F# Interactive."
                     printfn "You can set different event loops for MonoMac, Gtk#, WinForms and other"
@@ -341,8 +337,8 @@ let evaluateSession (argv: string[]) =
         if fsiSession.IsGui then
             try
                 Application.EnableVisualStyles()
-            with
-            | _ -> ()
+            with _ ->
+                ()
 
             // Route GUI application exceptions to the exception handlers
             Application.add_ThreadException (
@@ -352,14 +348,14 @@ let evaluateSession (argv: string[]) =
             let runningOnMono =
                 try
                     System.Type.GetType("Mono.Runtime") <> null
-                with
-                | e -> false
+                with e ->
+                    false
 
             if not runningOnMono then
                 try
                     TrySetUnhandledExceptionMode()
-                with
-                | _ -> ()
+                with _ ->
+                    ()
 
             fsiWinFormsLoop.Value |> Option.iter (fun l -> l.LCID <- fsiSession.LCID)
 #endif
@@ -400,8 +396,8 @@ let MainMain argv =
             member _.Dispose() =
                 try
                     Console.SetOut(savedOut)
-                with
-                | _ -> ()
+                with _ ->
+                    ()
         }
 
 #if !FX_NO_APP_DOMAINS

From 05b560d09c611306d91a26cbacd45a1504827e14 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 30 May 2022 15:44:17 +0100
Subject: [PATCH 18/20] Format most of FSharp.Core (#13150)

* modify fantomasignore

* fix setting

* no single line functions in FSHarp.Core

* update fantomas

* apply formatting
---
 .fantomasignore                           |   18 +-
 src/FSharp.Core/.editorconfig             |    2 +
 src/FSharp.Core/MutableTuple.fs           |  160 +-
 src/FSharp.Core/Nullable.fs               |  329 ++-
 src/FSharp.Core/QueryExtensions.fs        |  348 +--
 src/FSharp.Core/array.fs                  | 1676 +++++++++-----
 src/FSharp.Core/async.fs                  | 1589 +++++++------
 src/FSharp.Core/collections.fs            |   67 +-
 src/FSharp.Core/event.fs                  |  195 +-
 src/FSharp.Core/eventmodule.fs            |   87 +-
 src/FSharp.Core/fslib-extra-pervasives.fs |  467 ++--
 src/FSharp.Core/list.fs                   |  578 +++--
 src/FSharp.Core/mailbox.fs                |  246 +-
 src/FSharp.Core/map.fs                    | 1159 ++++++----
 src/FSharp.Core/math/z.fs                 |  114 +-
 src/FSharp.Core/observable.fs             |  276 ++-
 src/FSharp.Core/option.fs                 |   48 +-
 src/FSharp.Core/quotations.fs             | 2466 +++++++++++++--------
 src/FSharp.Core/reflect.fs                | 1085 +++++----
 src/FSharp.Core/result.fs                 |   15 +-
 src/FSharp.Core/resumable.fs              |  225 +-
 src/FSharp.Core/seq.fs                    | 1189 ++++++----
 src/FSharp.Core/set.fs                    | 1115 ++++++----
 src/FSharp.Core/string.fs                 |   98 +-
 src/FSharp.Core/tasks.fs                  |  335 +--
 25 files changed, 8619 insertions(+), 5268 deletions(-)
 create mode 100644 src/FSharp.Core/.editorconfig

diff --git a/.fantomasignore b/.fantomasignore
index 1f44854852e..2fa5a774170 100644
--- a/.fantomasignore
+++ b/.fantomasignore
@@ -13,7 +13,6 @@ artifacts/
 
 # Explicitly unformatted implementation files
 
-src/FSharp.Core/**/*.fs
 src/Compiler/Checking/**/*.fs
 src/Compiler/CodeGen/**/*.fs
 src/Compiler/DependencyManager/**/*.fs
@@ -28,6 +27,23 @@ src/Compiler/SyntaxTree/**/*.fs
 src/Compiler/TypedTree/**/*.fs
 src/Microsoft.FSharp.Compiler/**/*.fs
 
+# Fantomas limitations on implementation files in FSharp.Core (to investigate)
+
+src/FSharp.Core/array2.fs
+src/FSharp.Core/array3.fs
+src/FSharp.Core/Linq.fs
+src/FSharp.Core/local.fs
+src/FSharp.Core/nativeptr.fs
+src/FSharp.Core/prim-types-prelude.fs
+src/FSharp.Core/prim-types.fs
+src/FSharp.Core/printf.fs
+src/FSharp.Core/Query.fs
+src/FSharp.Core/seqcore.fs
+
+# Fantomas limitation https://github.com/fsprojects/fantomas/issues/2264
+
+src/FSharp.Core/SI.fs
+
 # Fantomas limitations on implementation files  (to investigate)
 
 src/Compiler/AbstractIL/ilwrite.fs
diff --git a/src/FSharp.Core/.editorconfig b/src/FSharp.Core/.editorconfig
new file mode 100644
index 00000000000..28218c4a78e
--- /dev/null
+++ b/src/FSharp.Core/.editorconfig
@@ -0,0 +1,2 @@
+[*.fs]
+fsharp_max_function_binding_width=1
diff --git a/src/FSharp.Core/MutableTuple.fs b/src/FSharp.Core/MutableTuple.fs
index 79e7d5c73ed..a9292f1c48b 100644
--- a/src/FSharp.Core/MutableTuple.fs
+++ b/src/FSharp.Core/MutableTuple.fs
@@ -7,170 +7,210 @@ open Microsoft.FSharp.Core
 
 // ----------------------------------------------------------------------------
 // Mutable Tuples - used when translating queries that use F# tuples
-// and records. We replace tuples/records with anonymous types which 
+// and records. We replace tuples/records with anonymous types which
 // are handled correctly by LINQ to SQL/Entities and other providers.
 //
 // NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
 //
-// The terminology "mutable tuple" is now incorrect in this code - 
+// The terminology "mutable tuple" is now incorrect in this code -
 // "immutable anonymous tuple-like types" are used instead. The key thing in this
 // code is that the anonymous types used conform to the shape and style
 // expected by LINQ providers, and we pass the correspondence between constructor
 // arguments and properties to the magic "members" argument of the Expression.New
 // constructor in Linq.fs.
 //
-// This terminology mistake also runs all the way through Query.fs. 
+// This terminology mistake also runs all the way through Query.fs.
 // ----------------------------------------------------------------------------
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    new (Item1) = { item1 = Item1 }
+    new(Item1) = { item1 = Item1 }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    new (Item1, Item2) = { item1 = Item1; item2 = Item2  }
+    new(Item1, Item2) = { item1 = Item1; item2 = Item2 }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2, 'T3> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    val private item3 : 'T3
+    val private item3: 'T3
     member x.Item3 = x.item3
 
-    new (Item1, Item2, Item3) = { item1 = Item1; item2 = Item2; item3 = Item3  }
-
+    new(Item1, Item2, Item3) =
+        {
+            item1 = Item1
+            item2 = Item2
+            item3 = Item3
+        }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2, 'T3, 'T4> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    val private item3 : 'T3
+    val private item3: 'T3
     member x.Item3 = x.item3
 
-    val private item4 : 'T4
+    val private item4: 'T4
     member x.Item4 = x.item4
 
-    new (Item1, Item2, Item3, Item4) = { item1 = Item1; item2 = Item2; item3 = Item3; item4 = Item4  }
-
-
+    new(Item1, Item2, Item3, Item4) =
+        {
+            item1 = Item1
+            item2 = Item2
+            item3 = Item3
+            item4 = Item4
+        }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2, 'T3, 'T4, 'T5> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    val private item3 : 'T3
+    val private item3: 'T3
     member x.Item3 = x.item3
 
-    val private item4 : 'T4
+    val private item4: 'T4
     member x.Item4 = x.item4
 
-    val private item5 : 'T5
+    val private item5: 'T5
     member x.Item5 = x.item5
 
-    new (Item1, Item2, Item3, Item4, Item5) = { item1 = Item1; item2 = Item2; item3 = Item3; item4 = Item4 ; item5 = Item5  }
-
+    new(Item1, Item2, Item3, Item4, Item5) =
+        {
+            item1 = Item1
+            item2 = Item2
+            item3 = Item3
+            item4 = Item4
+            item5 = Item5
+        }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    val private item3 : 'T3
+    val private item3: 'T3
     member x.Item3 = x.item3
 
-    val private item4 : 'T4
+    val private item4: 'T4
     member x.Item4 = x.item4
 
-    val private item5 : 'T5
+    val private item5: 'T5
     member x.Item5 = x.item5
 
-    val private item6 : 'T6
+    val private item6: 'T6
     member x.Item6 = x.item6
 
-    new (Item1, Item2, Item3, Item4, Item5, Item6) = { item1 = Item1; item2 = Item2; item3 = Item3; item4 = Item4 ; item5 = Item5 ; item6 = Item6  }
-
+    new(Item1, Item2, Item3, Item4, Item5, Item6) =
+        {
+            item1 = Item1
+            item2 = Item2
+            item3 = Item3
+            item4 = Item4
+            item5 = Item5
+            item6 = Item6
+        }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    val private item3 : 'T3
+    val private item3: 'T3
     member x.Item3 = x.item3
 
-    val private item4 : 'T4
+    val private item4: 'T4
     member x.Item4 = x.item4
 
-    val private item5 : 'T5
+    val private item5: 'T5
     member x.Item5 = x.item5
 
-    val private item6 : 'T6
+    val private item6: 'T6
     member x.Item6 = x.item6
 
-    val private item7 : 'T7
+    val private item7: 'T7
     member x.Item7 = x.item7
 
-    new (Item1, Item2, Item3, Item4, Item5, Item6, Item7) = { item1 = Item1; item2 = Item2; item3 = Item3; item4 = Item4 ; item5 = Item5 ; item6 = Item6  ; item7 = Item7  }
+    new(Item1, Item2, Item3, Item4, Item5, Item6, Item7) =
+        {
+            item1 = Item1
+            item2 = Item2
+            item3 = Item3
+            item4 = Item4
+            item5 = Item5
+            item6 = Item6
+            item7 = Item7
+        }
 
 /// This type shouldn't be used directly from user code.
 /// 
 type AnonymousObject<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'T8> =
-    val private item1 : 'T1
-    member x.Item1 = x.item1 
+    val private item1: 'T1
+    member x.Item1 = x.item1
 
-    val private item2 : 'T2
+    val private item2: 'T2
     member x.Item2 = x.item2
 
-    val private item3 : 'T3
+    val private item3: 'T3
     member x.Item3 = x.item3
 
-    val private item4 : 'T4
+    val private item4: 'T4
     member x.Item4 = x.item4
 
-    val private item5 : 'T5
+    val private item5: 'T5
     member x.Item5 = x.item5
 
-    val private item6 : 'T6
+    val private item6: 'T6
     member x.Item6 = x.item6
 
-    val private item7 : 'T7
+    val private item7: 'T7
     member x.Item7 = x.item7
 
-    val private item8 : 'T8
+    val private item8: 'T8
     member x.Item8 = x.item8
 
-    new (Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8) = { item1 = Item1; item2 = Item2; item3 = Item3; item4 = Item4 ; item5 = Item5 ; item6 = Item6  ; item7 = Item7; item8 = Item8  }
+    new(Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8) =
+        {
+            item1 = Item1
+            item2 = Item2
+            item3 = Item3
+            item4 = Item4
+            item5 = Item5
+            item6 = Item6
+            item7 = Item7
+            item8 = Item8
+        }
diff --git a/src/FSharp.Core/Nullable.fs b/src/FSharp.Core/Nullable.fs
index 02d58b2a8ac..354ce7bca75 100644
--- a/src/FSharp.Core/Nullable.fs
+++ b/src/FSharp.Core/Nullable.fs
@@ -9,134 +9,297 @@ open Microsoft.FSharp.Core
 open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
 
 module NullableOperators =
-    let (?>=) (x : Nullable<'T>) (y: 'T) = x.HasValue && x.Value >= y
+    let (?>=) (x: Nullable<'T>) (y: 'T) =
+        x.HasValue && x.Value >= y
 
-    let (?>) (x : Nullable<'T>) (y: 'T) = x.HasValue && x.Value > y
+    let (?>) (x: Nullable<'T>) (y: 'T) =
+        x.HasValue && x.Value > y
 
-    let (?<=) (x : Nullable<'T>) (y: 'T) = x.HasValue && x.Value <= y
+    let (?<=) (x: Nullable<'T>) (y: 'T) =
+        x.HasValue && x.Value <= y
 
-    let (?<) (x : Nullable<'T>) (y: 'T) = x.HasValue && x.Value < y
+    let (?<) (x: Nullable<'T>) (y: 'T) =
+        x.HasValue && x.Value < y
+
+    let (?=) (x: Nullable<'T>) (y: 'T) =
+        x.HasValue && x.Value = y
 
-    let (?=) (x : Nullable<'T>) (y: 'T) = x.HasValue && x.Value = y
-
-    let (?<>) (x : Nullable<'T>) (y: 'T) = not (x ?= y)
-
-    let (>=?) (x : 'T) (y: Nullable<'T>) = y.HasValue && x >= y.Value
-
-    let (>?) (x : 'T) (y: Nullable<'T>) = y.HasValue && x > y.Value
-
-    let (<=?) (x : 'T) (y: Nullable<'T>) = y.HasValue && x <= y.Value
-
-    let () = y.HasValue && x < y.Value
-
-    let (=?) (x : 'T) (y: Nullable<'T>) = y.HasValue && x = y.Value
-
-    let (<>?) (x : 'T) (y: Nullable<'T>) = not (x =? y)
-
-    let (?>=?) (x : Nullable<'T>) (y: Nullable<'T>) = (x.HasValue && y.HasValue && x.Value >= y.Value)
-
-    let (?>?) (x : Nullable<'T>) (y: Nullable<'T>) = (x.HasValue && y.HasValue && x.Value > y.Value)
-
-    let (?<=?) (x : Nullable<'T>) (y: Nullable<'T>) = (x.HasValue && y.HasValue && x.Value <= y.Value)
-
-    let (?) (y: Nullable<'T>) = (x.HasValue && y.HasValue && x.Value < y.Value)
-
-    let (?=?) (x : Nullable<'T>) (y: Nullable<'T>) = (not x.HasValue && not y.HasValue) || (x.HasValue && y.HasValue && x.Value = y.Value)
-
-    let (?<>?) (x : Nullable<'T>) (y: Nullable<'T>) = not (x ?=? y)
-
-    let inline (?+) (x : Nullable<_>) y = if x.HasValue then Nullable(x.Value + y) else Nullable()
-
-    let inline (+?) x (y: Nullable<_>) = if y.HasValue then Nullable(x + y.Value) else Nullable()
-
-    let inline (?+?) (x : Nullable<_>) (y: Nullable<_>) = if x.HasValue && y.HasValue then Nullable(x.Value + y.Value) else Nullable()
-
-    let inline (?-) (x : Nullable<_>) y = if x.HasValue then Nullable(x.Value - y) else Nullable()
-
-    let inline (-?) x (y: Nullable<_>) = if y.HasValue then Nullable(x - y.Value) else Nullable()
-
-    let inline (?-?) (x : Nullable<_>) (y: Nullable<_>) = if x.HasValue && y.HasValue then Nullable(x.Value - y.Value) else Nullable()
-
-    let inline ( ?*  ) (x : Nullable<_>) y = if x.HasValue then Nullable(x.Value * y) else Nullable()
-
-    let inline ( *?  ) x (y: Nullable<_>) = if y.HasValue then Nullable(x * y.Value) else Nullable()
-
-    let inline ( ?*? ) (x : Nullable<_>) (y: Nullable<_>) = if x.HasValue && y.HasValue then Nullable(x.Value * y.Value) else Nullable()
-
-    let inline ( ?%  ) (x : Nullable<_>) y = if x.HasValue then Nullable(x.Value % y) else Nullable()
-
-    let inline ( %?  ) x (y: Nullable<_>) = if y.HasValue then Nullable(x % y.Value) else Nullable()
-
-    let inline ( ?%? ) (x : Nullable<_>) (y: Nullable<_>) = if x.HasValue && y.HasValue then Nullable(x.Value % y.Value) else Nullable()
-
-    let inline ( ?/  ) (x : Nullable<_>) y = if x.HasValue then Nullable(x.Value / y) else Nullable()
-
-    let inline ( /?  ) x (y: Nullable<_>) = if y.HasValue then Nullable(x / y.Value) else Nullable()
-
-    let inline ( ?/? ) (x : Nullable<_>) (y: Nullable<_>) = if x.HasValue && y.HasValue then Nullable(x.Value / y.Value) else Nullable()
+    let (?<>) (x: Nullable<'T>) (y: 'T) =
+        not (x ?= y)
+
+    let (>=?) (x: 'T) (y: Nullable<'T>) =
+        y.HasValue && x >= y.Value
+
+    let (>?) (x: 'T) (y: Nullable<'T>) =
+        y.HasValue && x > y.Value
+
+    let (<=?) (x: 'T) (y: Nullable<'T>) =
+        y.HasValue && x <= y.Value
+
+    let () =
+        y.HasValue && x < y.Value
+
+    let (=?) (x: 'T) (y: Nullable<'T>) =
+        y.HasValue && x = y.Value
+
+    let (<>?) (x: 'T) (y: Nullable<'T>) =
+        not (x =? y)
+
+    let (?>=?) (x: Nullable<'T>) (y: Nullable<'T>) =
+        (x.HasValue && y.HasValue && x.Value >= y.Value)
+
+    let (?>?) (x: Nullable<'T>) (y: Nullable<'T>) =
+        (x.HasValue && y.HasValue && x.Value > y.Value)
+
+    let (?<=?) (x: Nullable<'T>) (y: Nullable<'T>) =
+        (x.HasValue && y.HasValue && x.Value <= y.Value)
+
+    let (?) (y: Nullable<'T>) =
+        (x.HasValue && y.HasValue && x.Value < y.Value)
+
+    let (?=?) (x: Nullable<'T>) (y: Nullable<'T>) =
+        (not x.HasValue && not y.HasValue)
+        || (x.HasValue && y.HasValue && x.Value = y.Value)
+
+    let (?<>?) (x: Nullable<'T>) (y: Nullable<'T>) =
+        not (x ?=? y)
+
+    let inline (?+) (x: Nullable<_>) y =
+        if x.HasValue then
+            Nullable(x.Value + y)
+        else
+            Nullable()
+
+    let inline (+?) x (y: Nullable<_>) =
+        if y.HasValue then
+            Nullable(x + y.Value)
+        else
+            Nullable()
+
+    let inline (?+?) (x: Nullable<_>) (y: Nullable<_>) =
+        if x.HasValue && y.HasValue then
+            Nullable(x.Value + y.Value)
+        else
+            Nullable()
+
+    let inline (?-) (x: Nullable<_>) y =
+        if x.HasValue then
+            Nullable(x.Value - y)
+        else
+            Nullable()
+
+    let inline (-?) x (y: Nullable<_>) =
+        if y.HasValue then
+            Nullable(x - y.Value)
+        else
+            Nullable()
+
+    let inline (?-?) (x: Nullable<_>) (y: Nullable<_>) =
+        if x.HasValue && y.HasValue then
+            Nullable(x.Value - y.Value)
+        else
+            Nullable()
+
+    let inline (?*) (x: Nullable<_>) y =
+        if x.HasValue then
+            Nullable(x.Value * y)
+        else
+            Nullable()
+
+    let inline ( *? ) x (y: Nullable<_>) =
+        if y.HasValue then
+            Nullable(x * y.Value)
+        else
+            Nullable()
+
+    let inline (?*?) (x: Nullable<_>) (y: Nullable<_>) =
+        if x.HasValue && y.HasValue then
+            Nullable(x.Value * y.Value)
+        else
+            Nullable()
+
+    let inline (?%) (x: Nullable<_>) y =
+        if x.HasValue then
+            Nullable(x.Value % y)
+        else
+            Nullable()
+
+    let inline (%?) x (y: Nullable<_>) =
+        if y.HasValue then
+            Nullable(x % y.Value)
+        else
+            Nullable()
+
+    let inline (?%?) (x: Nullable<_>) (y: Nullable<_>) =
+        if x.HasValue && y.HasValue then
+            Nullable(x.Value % y.Value)
+        else
+            Nullable()
+
+    let inline (?/) (x: Nullable<_>) y =
+        if x.HasValue then
+            Nullable(x.Value / y)
+        else
+            Nullable()
+
+    let inline (/?) x (y: Nullable<_>) =
+        if y.HasValue then
+            Nullable(x / y.Value)
+        else
+            Nullable()
+
+    let inline (?/?) (x: Nullable<_>) (y: Nullable<_>) =
+        if x.HasValue && y.HasValue then
+            Nullable(x.Value / y.Value)
+        else
+            Nullable()
 
 []
 []
 module Nullable =
     []
-    let inline uint8 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.byte value.Value) else Nullable()
+    let inline uint8 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.byte value.Value)
+        else
+            Nullable()
 
     []
-    let inline int8 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.sbyte value.Value) else Nullable()
+    let inline int8 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.sbyte value.Value)
+        else
+            Nullable()
 
     []
-    let inline byte (value:Nullable<_>) = if value.HasValue then Nullable(Operators.byte value.Value) else Nullable()
+    let inline byte (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.byte value.Value)
+        else
+            Nullable()
 
     []
-    let inline sbyte (value:Nullable<_>) = if value.HasValue then Nullable(Operators.sbyte value.Value) else Nullable()
+    let inline sbyte (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.sbyte value.Value)
+        else
+            Nullable()
 
     []
-    let inline int16 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.int16 value.Value) else Nullable()
+    let inline int16 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.int16 value.Value)
+        else
+            Nullable()
 
     []
-    let inline uint16 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.uint16 value.Value) else Nullable()
+    let inline uint16 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.uint16 value.Value)
+        else
+            Nullable()
 
     []
-    let inline int (value:Nullable<_>) = if value.HasValue then Nullable(Operators.int value.Value) else Nullable()
+    let inline int (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.int value.Value)
+        else
+            Nullable()
 
     []
-    let inline uint (value: Nullable<_>) = if value.HasValue then Nullable(Operators.uint value.Value) else Nullable()
-    
+    let inline uint (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.uint value.Value)
+        else
+            Nullable()
+
     []
-    let inline enum (value:Nullable< int32 >) = if value.HasValue then Nullable(Operators.enum value.Value) else Nullable()
+    let inline enum (value: Nullable) =
+        if value.HasValue then
+            Nullable(Operators.enum value.Value)
+        else
+            Nullable()
 
     []
-    let inline int32 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.int32 value.Value) else Nullable()
+    let inline int32 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.int32 value.Value)
+        else
+            Nullable()
 
     []
-    let inline uint32 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.uint32 value.Value) else Nullable()
+    let inline uint32 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.uint32 value.Value)
+        else
+            Nullable()
 
     []
-    let inline int64 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.int64 value.Value) else Nullable()
+    let inline int64 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.int64 value.Value)
+        else
+            Nullable()
 
     []
-    let inline uint64 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.uint64 value.Value) else Nullable()
+    let inline uint64 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.uint64 value.Value)
+        else
+            Nullable()
 
     []
-    let inline float32 (value:Nullable<_>) = if value.HasValue then Nullable(Operators.float32 value.Value) else Nullable()
+    let inline float32 (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.float32 value.Value)
+        else
+            Nullable()
 
     []
-    let inline float (value:Nullable<_>) = if value.HasValue then Nullable(Operators.float value.Value) else Nullable()
+    let inline float (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.float value.Value)
+        else
+            Nullable()
 
     []
-    let inline single (value:Nullable<_>) = if value.HasValue then Nullable(Operators.float32 value.Value) else Nullable()
+    let inline single (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.float32 value.Value)
+        else
+            Nullable()
 
     []
-    let inline double (value:Nullable<_>) = if value.HasValue then Nullable(Operators.float value.Value) else Nullable()
+    let inline double (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.float value.Value)
+        else
+            Nullable()
 
     []
-    let inline nativeint (value:Nullable<_>) = if value.HasValue then Nullable(Operators.nativeint value.Value) else Nullable()
+    let inline nativeint (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.nativeint value.Value)
+        else
+            Nullable()
 
     []
-    let inline unativeint (value:Nullable<_>) = if value.HasValue then Nullable(Operators.unativeint value.Value) else Nullable()
+    let inline unativeint (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.unativeint value.Value)
+        else
+            Nullable()
 
     []
-    let inline decimal (value:Nullable<_>) = if value.HasValue then Nullable(Operators.decimal value.Value) else Nullable()
+    let inline decimal (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.decimal value.Value)
+        else
+            Nullable()
 
     []
-    let inline char (value:Nullable<_>) = if value.HasValue then Nullable(Operators.char value.Value) else Nullable()
+    let inline char (value: Nullable<_>) =
+        if value.HasValue then
+            Nullable(Operators.char value.Value)
+        else
+            Nullable()
diff --git a/src/FSharp.Core/QueryExtensions.fs b/src/FSharp.Core/QueryExtensions.fs
index d7aead4813b..c2e86390c94 100644
--- a/src/FSharp.Core/QueryExtensions.fs
+++ b/src/FSharp.Core/QueryExtensions.fs
@@ -17,268 +17,326 @@ open System.Linq.Expressions
 
 // ----------------------------------------------------------------------------
 
-/// A type used to reconstruct a grouping after applying a mutable->immutable mapping transformation 
+/// A type used to reconstruct a grouping after applying a mutable->immutable mapping transformation
 /// on a result of a query.
-type Grouping<'K, 'T>(key:'K, values:seq<'T>) =
+type Grouping<'K, 'T>(key: 'K, values: seq<'T>) =
     interface System.Linq.IGrouping<'K, 'T> with
         member _.Key = key
 
     interface System.Collections.IEnumerable with
-        member _.GetEnumerator() = values.GetEnumerator() :> System.Collections.IEnumerator
+        member _.GetEnumerator() =
+            values.GetEnumerator() :> System.Collections.IEnumerator
 
     interface System.Collections.Generic.IEnumerable<'T> with
-        member _.GetEnumerator() = values.GetEnumerator()
+        member _.GetEnumerator() =
+            values.GetEnumerator()
 
-module internal Adapters = 
+module internal Adapters =
 
-    let memoize f = 
-         let d = new System.Collections.Concurrent.ConcurrentDictionary(HashIdentity.Structural)        
-         fun x -> d.GetOrAdd(x, fun r -> f r)
+    let memoize f =
+        let d =
+            new System.Collections.Concurrent.ConcurrentDictionary(HashIdentity.Structural)
 
-    let isPartiallyImmutableRecord : Type -> bool = 
-        memoize (fun t -> 
-             FSharpType.IsRecord t && 
-             not (FSharpType.GetRecordFields t |> Array.forall (fun f -> f.CanWrite)) )
+        fun x -> d.GetOrAdd(x, (fun r -> f r))
 
-    let MemberInitializationHelperMeth = 
+    let isPartiallyImmutableRecord: Type -> bool =
+        memoize (fun t ->
+            FSharpType.IsRecord t
+            && not (FSharpType.GetRecordFields t |> Array.forall (fun f -> f.CanWrite)))
+
+    let MemberInitializationHelperMeth =
         methodhandleof (fun x -> LeafExpressionConverter.MemberInitializationHelper x)
-        |> System.Reflection.MethodInfo.GetMethodFromHandle 
+        |> System.Reflection.MethodInfo.GetMethodFromHandle
         :?> System.Reflection.MethodInfo
 
-    let NewAnonymousObjectHelperMeth = 
+    let NewAnonymousObjectHelperMeth =
         methodhandleof (fun x -> LeafExpressionConverter.NewAnonymousObjectHelper x)
-        |> System.Reflection.MethodInfo.GetMethodFromHandle 
+        |> System.Reflection.MethodInfo.GetMethodFromHandle
         :?> System.Reflection.MethodInfo
 
-    // The following patterns are used to recognize object construction 
+    // The following patterns are used to recognize object construction
     // using the 'new O(Prop1 = , Prop2 = )' syntax
 
     /// Recognize sequential series written as (... ((; ); ); ...)
     let (|LeftSequentialSeries|) e =
         let rec leftSequentialSeries acc e =
-            match e with 
-            | Patterns.Sequential(e1, e2) -> leftSequentialSeries (e2 :: acc) e1
+            match e with
+            | Patterns.Sequential (e1, e2) -> leftSequentialSeries (e2 :: acc) e1
             | _ -> e :: acc
+
         leftSequentialSeries [] e
 
-    /// Tests whether a list consists only of assignments of properties of the 
+    /// Tests whether a list consists only of assignments of properties of the
     /// given variable, null values (ignored) and ends by returning the given variable
     /// (pattern returns only property assignments)
-    let (|PropSetList|_|) varArg (list:Expr list) =
-        let rec propSetList acc x = 
-            match x with 
+    let (|PropSetList|_|) varArg (list: Expr list) =
+        let rec propSetList acc x =
+            match x with
             // detect " v.X <- y"
-            | ((Patterns.PropertySet(Some(Patterns.Var var), _, _, _)) as p) :: xs when var = varArg ->
-                propSetList (p :: acc) xs
+            | ((Patterns.PropertySet (Some (Patterns.Var var), _, _, _)) as p) :: xs when var = varArg -> propSetList (p :: acc) xs
             // skip unit values
             | (Patterns.Value (v, _)) :: xs when v = null -> propSetList acc xs
             // detect "v"
-            | [Patterns.Var var] when var = varArg -> Some acc
+            | [ Patterns.Var var ] when var = varArg -> Some acc
             | _ -> None
+
         propSetList [] list
 
     /// Recognize object construction written using 'new O(Prop1 = , Prop2 = , ...)'
-    let (|ObjectConstruction|_|) e = 
+    let (|ObjectConstruction|_|) e =
         match e with
-        | Patterns.Let ( var, (Patterns.NewObject(_, []) as init), LeftSequentialSeries propSets ) ->
-            match propSets with 
+        | Patterns.Let (var, (Patterns.NewObject (_, []) as init), LeftSequentialSeries propSets) ->
+            match propSets with
             | PropSetList var propSets -> Some(var, init, propSets)
             | _ -> None
         | _ -> None
 
-
-
     // Get arrays of types & map of transformations
-    let tupleTypes = 
-      [|  typedefof>,               typedefof>
-          typedefof<_ * _>,                         typedefof>
-          typedefof<_ * _ * _>,                     typedefof>
-          typedefof<_ * _ * _ * _>,                 typedefof>
-          typedefof<_ * _ * _ * _ * _>,             typedefof>
-          typedefof<_ * _ * _ * _ * _ * _>,         typedefof>
-          typedefof<_ * _ * _ * _ * _ * _ * _>,     typedefof>
-          typedefof<_ * _ * _ * _ * _ * _ * _ * _>, typedefof> |]
+    let tupleTypes =
+        [|
+            typedefof>, typedefof>
+            typedefof<_ * _>, typedefof>
+            typedefof<_ * _ * _>, typedefof>
+            typedefof<_ * _ * _ * _>, typedefof>
+            typedefof<_ * _ * _ * _ * _>, typedefof>
+            typedefof<_ * _ * _ * _ * _ * _>, typedefof>
+            typedefof<_ * _ * _ * _ * _ * _ * _>, typedefof>
+            typedefof<_ * _ * _ * _ * _ * _ * _ * _>, typedefof>
+        |]
+
     let anonObjectTypes = tupleTypes |> Array.map snd
-    let tupleToAnonTypeMap = 
-        let t = new Dictionary()
-        for (k,v) in tupleTypes do t.[k] <- v
-        t
 
-    let anonToTupleTypeMap = 
-        let t = new Dictionary()
-        for (k,v) in tupleTypes do t.[v] <- k
+    let tupleToAnonTypeMap =
+        let t = new Dictionary()
+
+        for (k, v) in tupleTypes do
+            t.[k] <- v
+
         t
 
+    let anonToTupleTypeMap =
+        let t = new Dictionary()
+
+        for (k, v) in tupleTypes do
+            t.[v] <- k
+
+        t
 
     /// Recognize anonymous type construction written using 'new AnonymousObject(, , ...)'
-    let (|NewAnonymousObject|_|) e = 
+    let (|NewAnonymousObject|_|) e =
         match e with
-        | Patterns.NewObject(ctor,args) when 
-                 let dty = ctor.DeclaringType 
-                 dty.IsGenericType && anonToTupleTypeMap.ContainsKey (dty.GetGenericTypeDefinition()) -> 
-             Some (ctor, args)
+        | Patterns.NewObject (ctor, args) when
+            let dty = ctor.DeclaringType
+
+            dty.IsGenericType
+            && anonToTupleTypeMap.ContainsKey(dty.GetGenericTypeDefinition())
+            ->
+            Some(ctor, args)
         | _ -> None
 
-    let OneNewAnonymousObject (args:Expr list) =
+    let OneNewAnonymousObject (args: Expr list) =
         // Will fit into a single tuple type
         let typ = anonObjectTypes.[args.Length - 1]
         let typ = typ.MakeGenericType [| for a in args -> a.Type |]
         let ctor = typ.GetConstructors().[0]
-        let res = Expr.NewObject (ctor, args)
-        assert (match res with NewAnonymousObject _ -> true | _ -> false)
+        let res = Expr.NewObject(ctor, args)
+
+        assert
+            (match res with
+             | NewAnonymousObject _ -> true
+             | _ -> false)
+
         res
 
-    let rec NewAnonymousObject (args:Expr list) : Expr = 
-        match args with 
+    let rec NewAnonymousObject (args: Expr list) : Expr =
+        match args with
         | x1 :: x2 :: x3 :: x4 :: x5 :: x6 :: x7 :: x8 :: tail ->
             // Too long to fit single tuple - nested tuple after first 7
-            OneNewAnonymousObject [ x1; x2; x3; x4; x5; x6; x7; NewAnonymousObject (x8 :: tail) ]
-        | args -> 
-            OneNewAnonymousObject args
+            OneNewAnonymousObject [ x1; x2; x3; x4; x5; x6; x7; NewAnonymousObject(x8 :: tail) ]
+        | args -> OneNewAnonymousObject args
 
-    let AnonymousObjectGet (e:Expr,i:int) = 
-        // Recursively generate tuple get 
+    let AnonymousObjectGet (e: Expr, i: int) =
+        // Recursively generate tuple get
         // (may be nested e.g. TupleGet(, 9) ~> .Item8.Item3)
-        let rec walk i (inst:Expr) (newType:Type) = 
+        let rec walk i (inst: Expr) (newType: Type) =
 
             // Get property (at most the last one)
-            let propInfo = newType.GetProperty ("Item"  + string (1 + min i 7))
-            let res = Expr.PropertyGet (inst, propInfo)
+            let propInfo = newType.GetProperty("Item" + string (1 + min i 7))
+            let res = Expr.PropertyGet(inst, propInfo)
             // Do we need to add another property get for the last property?
-            if i < 7 then res 
-            else walk (i - 7) res (newType.GetGenericArguments().[7]) 
-            
+            if i < 7 then
+                res
+            else
+                walk (i - 7) res (newType.GetGenericArguments().[7])
+
         walk i e e.Type
 
-    let RewriteTupleType (ty:Type) conv = 
-        // Tuples are generic, so lookup only for generic types 
-        assert ty.IsGenericType 
+    let RewriteTupleType (ty: Type) conv =
+        // Tuples are generic, so lookup only for generic types
+        assert ty.IsGenericType
         let generic = ty.GetGenericTypeDefinition()
+
         match tupleToAnonTypeMap.TryGetValue generic with
         | true, mutableTupleType ->
             // Recursively transform type arguments
-            mutableTupleType.MakeGenericType (ty.GetGenericArguments() |> Array.toList |> conv |> Array.ofList)
-        | _ -> 
+            mutableTupleType.MakeGenericType(ty.GetGenericArguments() |> Array.toList |> conv |> Array.ofList)
+        | _ ->
             assert false
             failwith "unreachable"
 
-    let (|RecordFieldGetSimplification|_|) (expr:Expr) = 
-        match expr with 
-        | Patterns.PropertyGet(Some (Patterns.NewRecord(typ,els)),propInfo,[]) ->
-            let fields = Microsoft.FSharp.Reflection.FSharpType.GetRecordFields(typ,System.Reflection.BindingFlags.Public|||System.Reflection.BindingFlags.NonPublic) 
-            match fields |> Array.tryFindIndex (fun p -> p = propInfo) with 
+    let (|RecordFieldGetSimplification|_|) (expr: Expr) =
+        match expr with
+        | Patterns.PropertyGet (Some (Patterns.NewRecord (typ, els)), propInfo, []) ->
+            let fields =
+                Microsoft.FSharp.Reflection.FSharpType.GetRecordFields(
+                    typ,
+                    System.Reflection.BindingFlags.Public
+                    ||| System.Reflection.BindingFlags.NonPublic
+                )
+
+            match fields |> Array.tryFindIndex (fun p -> p = propInfo) with
             | None -> None
-            | Some i -> if i < els.Length then Some els.[i] else None
+            | Some i ->
+                if i < els.Length then
+                    Some els.[i]
+                else
+                    None
         | _ -> None
 
-
     /// The generic MethodInfo for Select function
     /// Describes how we got from productions of immutable objects to productions of anonymous objects, with enough information
     /// that we can invert the process in final query results.
     []
-    type ConversionDescription = 
+    type ConversionDescription =
         | TupleConv of ConversionDescription list
         | RecordConv of Type * ConversionDescription list
-        | GroupingConv of (* origKeyType: *) Type * (* origElemType: *) Type * ConversionDescription
+        | GroupingConv (* origKeyType: *)  of Type (* origElemType: *)  * Type * ConversionDescription
         | SeqConv of ConversionDescription
         | NoConv
 
     /// Given an type involving immutable tuples and records, logically corresponding to the type produced at a
     /// "yield" or "select", convert it to a type involving anonymous objects according to the conversion data.
-    let rec ConvImmutableTypeToMutableType conv ty = 
-        match conv with 
-        | TupleConv convs -> 
+    let rec ConvImmutableTypeToMutableType conv ty =
+        match conv with
+        | TupleConv convs ->
             assert (FSharpType.IsTuple ty)
-            match convs with 
+
+            match convs with
             | x1 :: x2 :: x3 :: x4 :: x5 :: x6 :: x7 :: x8 :: tail ->
-                RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType [x1;x2;x3;x4;x5;x6;x7;TupleConv (x8 :: tail)])
-            | _ -> 
-                RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType convs)
-        | RecordConv (_,convs) -> 
+                RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType [ x1; x2; x3; x4; x5; x6; x7; TupleConv(x8 :: tail) ])
+            | _ -> RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType convs)
+        | RecordConv (_, convs) ->
             assert (isPartiallyImmutableRecord ty)
             let types = [| for f in FSharpType.GetRecordFields ty -> f.PropertyType |]
-            ConvImmutableTypeToMutableType (TupleConv convs) (FSharpType.MakeTupleType types) 
-        | GroupingConv (_keyTy,_elemTy,conv) -> 
-            assert ty.IsGenericType 
+            ConvImmutableTypeToMutableType (TupleConv convs) (FSharpType.MakeTupleType types)
+        | GroupingConv (_keyTy, _elemTy, conv) ->
+            assert ty.IsGenericType
             assert (ty.GetGenericTypeDefinition() = typedefof>)
             let keyt1 = ty.GetGenericArguments().[0]
             let valt1 = ty.GetGenericArguments().[1]
             typedefof>.MakeGenericType [| keyt1; ConvImmutableTypeToMutableType conv valt1 |]
-        | SeqConv conv -> 
+        | SeqConv conv ->
             assert ty.IsGenericType
             let isIQ = ty.GetGenericTypeDefinition() = typedefof>
-            assert (ty.GetGenericTypeDefinition() = typedefof> || ty.GetGenericTypeDefinition() = typedefof>)
+
+            assert
+                (ty.GetGenericTypeDefinition() = typedefof>
+                 || ty.GetGenericTypeDefinition() = typedefof>)
+
             let elemt1 = ty.GetGenericArguments().[0]
             let args = [| ConvImmutableTypeToMutableType conv elemt1 |]
-            if isIQ then typedefof>.MakeGenericType args else typedefof>.MakeGenericType args
+
+            if isIQ then
+                typedefof>.MakeGenericType args
+            else
+                typedefof>.MakeGenericType args
         | NoConv -> ty
 
-    let IsNewAnonymousObjectHelperQ = 
-        let mhandle = (methodhandleof (fun x -> LeafExpressionConverter.NewAnonymousObjectHelper x))
-        let minfo = (System.Reflection.MethodInfo.GetMethodFromHandle mhandle) :?> System.Reflection.MethodInfo
-        let gmd = minfo.GetGenericMethodDefinition() 
-        (fun tm -> 
+    let IsNewAnonymousObjectHelperQ =
+        let mhandle =
+            (methodhandleof (fun x -> LeafExpressionConverter.NewAnonymousObjectHelper x))
+
+        let minfo =
+            (System.Reflection.MethodInfo.GetMethodFromHandle mhandle) :?> System.Reflection.MethodInfo
+
+        let gmd = minfo.GetGenericMethodDefinition()
+
+        (fun tm ->
             match tm with
-            | Patterns.Call(_obj,minfo2,_args) -> minfo2.IsGenericMethod && (gmd = minfo2.GetGenericMethodDefinition()) 
+            | Patterns.Call (_obj, minfo2, _args) -> minfo2.IsGenericMethod && (gmd = minfo2.GetGenericMethodDefinition())
             | _ -> false)
 
     /// Cleanup the use of property-set object constructions in leaf expressions that form parts of F# queries.
-    let rec CleanupLeaf expr = 
-        if IsNewAnonymousObjectHelperQ expr then expr else // this has already been cleaned up, don't do it twice
-
-        // rewrite bottom-up
-        let expr = 
-            match expr with 
-            | ExprShape.ShapeCombination(comb,args) -> match args with [] -> expr | _ -> ExprShape.RebuildShapeCombination(comb,List.map CleanupLeaf args)
-            | ExprShape.ShapeLambda(v,body) -> Expr.Lambda (v, CleanupLeaf body)
-            | ExprShape.ShapeVar _ -> expr
-        match expr with 
-
-        // Detect all object construction expressions - wrap them in 'MemberInitializationHelper'
-        // so that it can be translated to Expression.MemberInit 
-        | ObjectConstruction(var, init, propSets) ->
-            // Wrap object initialization into a value (
-            let methInfo = MemberInitializationHelperMeth.MakeGenericMethod [|  var.Type |]
-            Expr.Call (methInfo, [ List.reduceBack (fun a b -> Expr.Sequential (a,b)) (propSets @ [init]) ])
-
-        // Detect all anonymous type constructions - wrap them in 'NewAnonymousObjectHelper'
-        // so that it can be translated to Expression.New with member arguments.
-        | NewAnonymousObject(ctor, args) ->
-            let methInfo = NewAnonymousObjectHelperMeth.MakeGenericMethod [|  ctor.DeclaringType |]
-            Expr.Call (methInfo, [ Expr.NewObject (ctor,args) ])
-        | expr -> 
+    let rec CleanupLeaf expr =
+        if IsNewAnonymousObjectHelperQ expr then
             expr
+        else // this has already been cleaned up, don't do it twice
+
+            // rewrite bottom-up
+            let expr =
+                match expr with
+                | ExprShape.ShapeCombination (comb, args) ->
+                    match args with
+                    | [] -> expr
+                    | _ -> ExprShape.RebuildShapeCombination(comb, List.map CleanupLeaf args)
+                | ExprShape.ShapeLambda (v, body) -> Expr.Lambda(v, CleanupLeaf body)
+                | ExprShape.ShapeVar _ -> expr
+
+            match expr with
+
+            // Detect all object construction expressions - wrap them in 'MemberInitializationHelper'
+            // so that it can be translated to Expression.MemberInit
+            | ObjectConstruction (var, init, propSets) ->
+                // Wrap object initialization into a value (
+                let methInfo = MemberInitializationHelperMeth.MakeGenericMethod [| var.Type |]
+                Expr.Call(methInfo, [ List.reduceBack (fun a b -> Expr.Sequential(a, b)) (propSets @ [ init ]) ])
+
+            // Detect all anonymous type constructions - wrap them in 'NewAnonymousObjectHelper'
+            // so that it can be translated to Expression.New with member arguments.
+            | NewAnonymousObject (ctor, args) ->
+                let methInfo =
+                    NewAnonymousObjectHelperMeth.MakeGenericMethod [| ctor.DeclaringType |]
+
+                Expr.Call(methInfo, [ Expr.NewObject(ctor, args) ])
+            | expr -> expr
 
     /// Simplify gets of tuples and gets of record fields.
-    let rec SimplifyConsumingExpr e = 
+    let rec SimplifyConsumingExpr e =
         // rewrite bottom-up
-        let e = 
-            match e with 
-            | ExprShape.ShapeCombination(comb,args) -> ExprShape.RebuildShapeCombination(comb,List.map SimplifyConsumingExpr args)
-            | ExprShape.ShapeLambda(v,body) -> Expr.Lambda (v, SimplifyConsumingExpr body)
+        let e =
+            match e with
+            | ExprShape.ShapeCombination (comb, args) -> ExprShape.RebuildShapeCombination(comb, List.map SimplifyConsumingExpr args)
+            | ExprShape.ShapeLambda (v, body) -> Expr.Lambda(v, SimplifyConsumingExpr body)
             | ExprShape.ShapeVar _ -> e
+
         match e with
-        | Patterns.TupleGet(Patterns.NewTuple els,i) -> els.[i]
-        | RecordFieldGetSimplification newExpr -> newExpr 
+        | Patterns.TupleGet (Patterns.NewTuple els, i) -> els.[i]
+        | RecordFieldGetSimplification newExpr -> newExpr
         | _ -> e
 
     /// Given the expression part of a "yield" or "select" which produces a result in terms of immutable tuples or immutable records,
     /// generate an equivalent expression yielding anonymous objects. Also return the conversion for the immutable-to-mutable correspondence
     /// so we can reverse this later.
-    let rec ProduceMoreMutables tipf expr = 
+    let rec ProduceMoreMutables tipf expr =
+
+        match expr with
+        // Replace immutable tuples by anonymous objects
+        | Patterns.NewTuple exprs ->
+            let argExprsNow, argScripts =
+                exprs |> List.map (ProduceMoreMutables tipf) |> List.unzip
+
+            NewAnonymousObject argExprsNow, TupleConv argScripts
 
-        match expr with 
-        // Replace immutable tuples by anonymous objects 
-        | Patterns.NewTuple exprs -> 
-            let argExprsNow, argScripts = exprs |> List.map (ProduceMoreMutables tipf) |> List.unzip
-            NewAnonymousObject argExprsNow, TupleConv  argScripts
+        // Replace immutable records by anonymous objects
+        | Patterns.NewRecord (typ, args) when isPartiallyImmutableRecord typ ->
+            let argExprsNow, argScripts =
+                args |> List.map (ProduceMoreMutables tipf) |> List.unzip
 
-        // Replace immutable records by anonymous objects 
-        | Patterns.NewRecord(typ, args) when isPartiallyImmutableRecord typ ->
-            let argExprsNow, argScripts = args |> List.map (ProduceMoreMutables tipf) |> List.unzip
             NewAnonymousObject argExprsNow, RecordConv(typ, argScripts)
 
-        | expr -> 
-            tipf expr
+        | expr -> tipf expr
 
-    let MakeSeqConv conv = match conv with NoConv -> NoConv | _ -> SeqConv conv
+    let MakeSeqConv conv =
+        match conv with
+        | NoConv -> NoConv
+        | _ -> SeqConv conv
diff --git a/src/FSharp.Core/array.fs b/src/FSharp.Core/array.fs
index cf8ec3924f8..56465b1be47 100644
--- a/src/FSharp.Core/array.fs
+++ b/src/FSharp.Core/array.fs
@@ -14,169 +14,229 @@ open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
 /// Basic operations on arrays
 []
 []
-module Array = 
+module Array =
 
     let inline checkNonNull argName arg =
-        if isNull arg then
-            nullArg argName
+        if isNull arg then nullArg argName
 
-    let inline indexNotFound() = raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
+    let inline indexNotFound () =
+        raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
 
     []
-    let length (array: _[])    = 
+    let length (array: _[]) =
         checkNonNull "array" array
         array.Length
-    
+
     []
     let inline last (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
-        array.[array.Length-1]
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        array.[array.Length - 1]
 
     []
     let tryLast (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then None 
-        else Some array.[array.Length-1]
+
+        if array.Length = 0 then
+            None
+        else
+            Some array.[array.Length - 1]
 
     []
-    let inline init count initializer = Microsoft.FSharp.Primitives.Basics.Array.init count initializer
+    let inline init count initializer =
+        Microsoft.FSharp.Primitives.Basics.Array.init count initializer
 
     []
-    let zeroCreate count = 
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
+    let zeroCreate count =
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
         Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
 
     []
     let create (count: int) (value: 'T) =
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
         let array: 'T[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
+
         for i = 0 to Operators.Checked.(-) array.Length 1 do // use checked arithmetic here to satisfy FxCop
             array.[i] <- value
+
         array
 
     []
     let tryHead (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then None
-        else Some array.[0]
+
+        if array.Length = 0 then
+            None
+        else
+            Some array.[0]
 
     []
-    let isEmpty (array: 'T[]) = 
+    let isEmpty (array: 'T[]) =
         checkNonNull "array" array
         array.Length = 0
 
     []
     let tail (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" (SR.GetString(SR.notEnoughElements))            
+
+        if array.Length = 0 then
+            invalidArg "array" (SR.GetString(SR.notEnoughElements))
+
         Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 1 (array.Length - 1) array
 
     []
-    let empty<'T> : 'T [] = [| |]
+    let empty<'T> : 'T[] = [||]
 
     []
-    let inline blit (source: 'T[]) (sourceIndex: int) (target: 'T[]) (targetIndex: int) (count: int) = 
+    let inline blit (source: 'T[]) (sourceIndex: int) (target: 'T[]) (targetIndex: int) (count: int) =
         Array.Copy(source, sourceIndex, target, targetIndex, count)
-                   
+
     let concatArrays (arrs: 'T[][]) : 'T[] =
-        let mutable acc = 0    
+        let mutable acc = 0
+
         for h in arrs do
             acc <- acc + h.Length
-            
-        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked acc  
-            
+
+        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked acc
+
         let mutable j = 0
-        for i = 0 to arrs.Length-1 do
+
+        for i = 0 to arrs.Length - 1 do
             let h = arrs.[i]
             let len = h.Length
             Array.Copy(h, 0, res, j, len)
             j <- j + len
-        res               
+
+        res
 
     []
-    let concat (arrays: seq<'T[]>) = 
+    let concat (arrays: seq<'T[]>) =
         checkNonNull "arrays" arrays
+
         match arrays with
         | :? ('T[][]) as ts -> ts |> concatArrays // avoid a clone, since we only read the array
         | _ -> arrays |> Seq.toArray |> concatArrays
-        
+
     []
-    let replicate count initial = 
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
-        let arr: 'T array = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
-        for i = 0 to arr.Length-1 do 
+    let replicate count initial =
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
+        let arr: 'T array =
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
+
+        for i = 0 to arr.Length - 1 do
             arr.[i] <- initial
+
         arr
 
     []
-    let collect (mapping: 'T -> 'U[]) (array: 'T[]) : 'U[]=
+    let collect (mapping: 'T -> 'U[]) (array: 'T[]) : 'U[] =
         checkNonNull "array" array
         let len = array.Length
         let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked<'U[]> len
-        for i = 0 to result.Length-1 do
+
+        for i = 0 to result.Length - 1 do
             result.[i] <- mapping array.[i]
+
         concatArrays result
-    
+
     []
     let splitAt index (array: 'T[]) =
         checkNonNull "array" array
-        if index < 0 then invalidArgInputMustBeNonNegative "index" index
-        if array.Length < index then raise <| InvalidOperationException (SR.GetString(SR.notEnoughElements))
+
+        if index < 0 then
+            invalidArgInputMustBeNonNegative "index" index
+
+        if array.Length < index then
+            raise <| InvalidOperationException(SR.GetString(SR.notEnoughElements))
+
         if index = 0 then
-            let right = Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 array.Length array
+            let right =
+                Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 array.Length array
+
             [||], right
         elif index = array.Length then
-            let left = Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 array.Length array
-            left, [||] 
+            let left =
+                Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 array.Length array
+
+            left, [||]
         else
             let res1 = Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 index array
-            let res2 = Microsoft.FSharp.Primitives.Basics.Array.subUnchecked index (array.Length-index) array
+
+            let res2 =
+                Microsoft.FSharp.Primitives.Basics.Array.subUnchecked index (array.Length - index) array
 
             res1, res2
 
     []
     let take count (array: 'T[]) =
         checkNonNull "array" array
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
-        if count = 0 then 
+
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
+        if count = 0 then
             empty
         else
             if count > array.Length then
-                raise <| InvalidOperationException (SR.GetString(SR.notEnoughElements))
+                raise <| InvalidOperationException(SR.GetString(SR.notEnoughElements))
 
             Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count array
 
     []
-    let takeWhile predicate (array: 'T[]) = 
+    let takeWhile predicate (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then 
-            empty 
+
+        if array.Length = 0 then
+            empty
         else
             let mutable count = 0
+
             while count < array.Length && predicate array.[count] do
                 count <- count + 1
 
             Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count array
 
-    let inline countByImpl (comparer: IEqualityComparer<'SafeKey>) ([] projection: 'T->'SafeKey) ([] getKey: 'SafeKey->'Key) (array: 'T[]) =
+    let inline countByImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        ([] projection: 'T -> 'SafeKey)
+        ([] getKey: 'SafeKey -> 'Key)
+        (array: 'T[])
+        =
         let length = array.Length
-        if length = 0 then Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0 else
 
-        let dict = Dictionary comparer
+        if length = 0 then
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
+        else
 
-        // Build the groupings
-        for v in array do
-            let safeKey = projection v
-            let mutable prev = Unchecked.defaultof<_>
-            if dict.TryGetValue(safeKey, &prev) then dict.[safeKey] <- prev + 1 else dict.[safeKey] <- 1
+            let dict = Dictionary comparer
 
-        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
-        let mutable i = 0
-        for group in dict do
-            res.[i] <- getKey group.Key, group.Value
-            i <- i + 1
-        res
+            // Build the groupings
+            for v in array do
+                let safeKey = projection v
+                let mutable prev = Unchecked.defaultof<_>
+
+                if dict.TryGetValue(safeKey, &prev) then
+                    dict.[safeKey] <- prev + 1
+                else
+                    dict.[safeKey] <- 1
+
+            let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
+            let mutable i = 0
+
+            for group in dict do
+                res.[i] <- getKey group.Key, group.Value
+                i <- i + 1
+
+            res
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
     let countByValueType (projection: 'T -> 'Key) (array: 'T[]) =
@@ -184,43 +244,52 @@ module Array =
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let countByRefType (projection: 'T -> 'Key) (array: 'T[]) =
-        countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox (projection t)) (fun sb -> sb.Value) array
+        countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(projection t)) (fun sb -> sb.Value) array
 
     []
-    let countBy (projection: 'T->'Key) (array: 'T[]) =
+    let countBy (projection: 'T -> 'Key) (array: 'T[]) =
         checkNonNull "array" array
-        if typeof<'Key>.IsValueType
-            then countByValueType projection array
-            else countByRefType   projection array
+
+        if typeof<'Key>.IsValueType then
+            countByValueType projection array
+        else
+            countByRefType projection array
 
     []
-    let append (array1: 'T[]) (array2: 'T[]) = 
+    let append (array1: 'T[]) (array2: 'T[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let n1 = array1.Length 
-        let n2 = array2.Length 
-        let res: 'T[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (n1 + n2)
+        let n1 = array1.Length
+        let n2 = array2.Length
+
+        let res: 'T[] =
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (n1 + n2)
+
         Array.Copy(array1, 0, res, 0, n1)
         Array.Copy(array2, 0, res, n1, n2)
-        res   
+        res
 
     []
     let head (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString else array.[0]
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+        else
+            array.[0]
 
     []
     let copy (array: 'T[]) =
         checkNonNull "array" array
         (array.Clone() :?> 'T[]) // this is marginally faster
-        //let len = array.Length 
-        //let res = zeroCreate len 
-        //for i = 0 to len - 1 do 
-        //    res.[i] <- array.[i]
-        //res
+    //let len = array.Length
+    //let res = zeroCreate len
+    //for i = 0 to len - 1 do
+    //    res.[i] <- array.[i]
+    //res
 
     []
-    let toList array = 
+    let toList array =
         checkNonNull "array" array
         List.ofArray array
 
@@ -230,16 +299,19 @@ module Array =
 
     []
     let indexed (array: 'T[]) =
-        checkNonNull "array" array            
+        checkNonNull "array" array
         let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
-        for i = 0 to res.Length-1 do
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- (i, array.[i])
+
         res
 
     []
-    let inline iter ([] action) (array: 'T[]) = 
-        checkNonNull "array" array            
-        for i = 0 to array.Length-1 do 
+    let inline iter ([] action) (array: 'T[]) =
+        checkNonNull "array" array
+
+        for i = 0 to array.Length - 1 do
             action array.[i]
 
     []
@@ -249,7 +321,8 @@ module Array =
         let mutable i = 0
 
         let hashSet = HashSet<'T>(HashIdentity.Structural<'T>)
-        for v in array do 
+
+        for v in array do
             if hashSet.Add(v) then
                 temp.[i] <- v
                 i <- i + 1
@@ -258,96 +331,127 @@ module Array =
 
     []
     let inline map ([] mapping: 'T -> 'U) (array: 'T[]) =
-        checkNonNull "array" array            
-        let res: 'U[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
-        for i = 0 to res.Length-1 do 
+        checkNonNull "array" array
+
+        let res: 'U[] =
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- mapping array.[i]
+
         res
 
     []
-    let iter2 action (array1: 'T[]) (array2: 'U[]) = 
+    let iter2 action (array1: 'T[]) (array2: 'U[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
-        if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        for i = 0 to array1.Length-1 do 
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (action)
+
+        if array1.Length <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        for i = 0 to array1.Length - 1 do
             f.Invoke(array1.[i], array2.[i])
 
     []
     let distinctBy projection (array: 'T[]) =
         checkNonNull "array" array
         let length = array.Length
-        if length = 0 then Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0 else
 
-        let temp = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
-        let mutable i = 0 
-        let hashSet = HashSet<_>(HashIdentity.Structural<_>)
-        for v in array do
-            if hashSet.Add(projection v) then
-                temp.[i] <- v
-                i <- i + 1
+        if length = 0 then
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
+        else
 
-        Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 i temp
+            let temp = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
+            let mutable i = 0
+            let hashSet = HashSet<_>(HashIdentity.Structural<_>)
+
+            for v in array do
+                if hashSet.Add(projection v) then
+                    temp.[i] <- v
+                    i <- i + 1
+
+            Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 i temp
 
     []
-    let map2 mapping (array1: 'T[]) (array2: 'U[]) = 
+    let map2 mapping (array1: 'T[]) (array2: 'U[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(mapping)
-        if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (mapping)
+
+        if array1.Length <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
         let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array1.Length
-        for i = 0 to res.Length-1 do 
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- f.Invoke(array1.[i], array2.[i])
+
         res
 
     []
-    let map3 mapping (array1: 'T1[]) (array2: 'T2[]) (array3: 'T3[]) = 
+    let map3 mapping (array1: 'T1[]) (array2: 'T2[]) (array3: 'T3[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
         checkNonNull "array3" array3
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(mapping)
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (mapping)
         let len1 = array1.Length
-        if len1 <> array2.Length || len1 <> array3.Length then invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
-        
+
+        if len1 <> array2.Length || len1 <> array3.Length then
+            invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
+
         let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1
-        for i = 0 to res.Length-1 do
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- f.Invoke(array1.[i], array2.[i], array3.[i])
+
         res
 
     []
-    let mapi2 mapping (array1: 'T[]) (array2: 'U[]) = 
+    let mapi2 mapping (array1: 'T[]) (array2: 'U[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(mapping)
-        if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array1.Length 
-        for i = 0 to res.Length-1 do 
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (mapping)
+
+        if array1.Length <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array1.Length
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- f.Invoke(i, array1.[i], array2.[i])
+
         res
 
     []
     let iteri action (array: 'T[]) =
         checkNonNull "array" array
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)            
-        for i = 0 to array.Length-1 do 
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (action)
+
+        for i = 0 to array.Length - 1 do
             f.Invoke(i, array.[i])
 
     []
-    let iteri2 action (array1: 'T[]) (array2: 'U[]) = 
+    let iteri2 action (array1: 'T[]) (array2: 'U[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(action)
-        if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        for i = 0 to array1.Length-1 do 
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (action)
+
+        if array1.Length <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        for i = 0 to array1.Length - 1 do
             f.Invoke(i, array1.[i], array2.[i])
 
     []
     let mapi (mapping: int -> 'T -> 'U) (array: 'T[]) =
         checkNonNull "array" array
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(mapping)            
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (mapping)
         let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
-        for i = 0 to array.Length-1 do 
+
+        for i = 0 to array.Length - 1 do
             res.[i] <- f.Invoke(i, array.[i])
+
         res
 
     []
@@ -365,9 +469,11 @@ module Array =
         checkNonNull "array" array
         let mutable state = false
         let mutable i = 0
+
         while not state && i < array.Length do
             state <- predicate array.[i]
             i <- i + 1
+
         state
 
     []
@@ -375,202 +481,356 @@ module Array =
         checkNonNull "array" array
         let mutable state = false
         let mutable i = 0
+
         while not state && i < array.Length do
             state <- value = array.[i]
             i <- i + 1
+
         state
 
     []
-    let exists2 predicate (array1: _[]) (array2: _[]) = 
+    let exists2 predicate (array1: _[]) (array2: _[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate)
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (predicate)
         let len1 = array1.Length
-        if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        let rec loop i = i < len1 && (f.Invoke(array1.[i], array2.[i]) || loop (i+1))
+
+        if len1 <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        let rec loop i =
+            i < len1 && (f.Invoke(array1.[i], array2.[i]) || loop (i + 1))
+
         loop 0
 
     []
     let forall (predicate: 'T -> bool) (array: 'T[]) =
         checkNonNull "array" array
         let len = array.Length
-        let rec loop i = i >= len || (predicate array.[i] && loop (i+1))
+
+        let rec loop i =
+            i >= len || (predicate array.[i] && loop (i + 1))
+
         loop 0
 
     []
-    let forall2 predicate (array1: _[]) (array2: _[]) = 
+    let forall2 predicate (array1: _[]) (array2: _[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate)
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (predicate)
         let len1 = array1.Length
-        if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        let rec loop i = i >= len1 || (f.Invoke(array1.[i], array2.[i]) && loop (i+1))
+
+        if len1 <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        let rec loop i =
+            i >= len1 || (f.Invoke(array1.[i], array2.[i]) && loop (i + 1))
+
         loop 0
 
-    let inline groupByImpl (comparer: IEqualityComparer<'SafeKey>) ([] keyf: 'T->'SafeKey) ([] getKey: 'SafeKey->'Key) (array: 'T[]) =
+    let inline groupByImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        ([] keyf: 'T -> 'SafeKey)
+        ([] getKey: 'SafeKey -> 'Key)
+        (array: 'T[])
+        =
         let length = array.Length
-        if length = 0 then Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0 else
-        let dict = Dictionary<_, ResizeArray<_>> comparer
-
-        // Build the groupings
-        for i = 0 to length - 1 do
-            let v = array.[i]
-            let safeKey = keyf v
-            let mutable prev = Unchecked.defaultof<_>
-            if dict.TryGetValue(safeKey, &prev) then
-                prev.Add v
-            else 
-                let prev = ResizeArray ()
-                dict.[safeKey] <- prev
-                prev.Add v
-                 
-        // Return the array-of-arrays.
-        let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
-        let mutable i = 0
-        for group in dict do
-            result.[i] <- getKey group.Key, group.Value.ToArray()
-            i <- i + 1
 
-        result
+        if length = 0 then
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
+        else
+            let dict = Dictionary<_, ResizeArray<_>> comparer
+
+            // Build the groupings
+            for i = 0 to length - 1 do
+                let v = array.[i]
+                let safeKey = keyf v
+                let mutable prev = Unchecked.defaultof<_>
+
+                if dict.TryGetValue(safeKey, &prev) then
+                    prev.Add v
+                else
+                    let prev = ResizeArray()
+                    dict.[safeKey] <- prev
+                    prev.Add v
+
+            // Return the array-of-arrays.
+            let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
+            let mutable i = 0
+
+            for group in dict do
+                result.[i] <- getKey group.Key, group.Value.ToArray()
+                i <- i + 1
+
+            result
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
-    let groupByValueType (keyf: 'T->'Key) (array: 'T[]) = groupByImpl HashIdentity.Structural<'Key> keyf id array
+    let groupByValueType (keyf: 'T -> 'Key) (array: 'T[]) =
+        groupByImpl HashIdentity.Structural<'Key> keyf id array
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
-    let groupByRefType   (keyf: 'T->'Key) (array: 'T[]) = groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox (keyf t)) (fun sb -> sb.Value) array
+    let groupByRefType (keyf: 'T -> 'Key) (array: 'T[]) =
+        groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value) array
 
     []
-    let groupBy (projection: 'T->'Key) (array: 'T[]) =
+    let groupBy (projection: 'T -> 'Key) (array: 'T[]) =
         checkNonNull "array" array
-        if typeof<'Key>.IsValueType
-            then groupByValueType projection array
-            else groupByRefType   projection array
+
+        if typeof<'Key>.IsValueType then
+            groupByValueType projection array
+        else
+            groupByRefType projection array
 
     []
-    let pick chooser (array: _[]) = 
-        checkNonNull "array" array
-        let rec loop i = 
-            if i >= array.Length then 
-                indexNotFound()
-            else 
-                match chooser array.[i] with 
-                | None -> loop(i+1)
+    let pick chooser (array: _[]) =
+        checkNonNull "array" array
+
+        let rec loop i =
+            if i >= array.Length then
+                indexNotFound ()
+            else
+                match chooser array.[i] with
+                | None -> loop (i + 1)
                 | Some res -> res
-        loop 0 
+
+        loop 0
 
     []
-    let tryPick chooser (array: _[]) = 
-        checkNonNull "array" array
-        let rec loop i = 
-            if i >= array.Length then None else 
-            match chooser array.[i] with 
-            | None -> loop(i+1)
-            | res -> res
-        loop 0 
-    
+    let tryPick chooser (array: _[]) =
+        checkNonNull "array" array
+
+        let rec loop i =
+            if i >= array.Length then
+                None
+            else
+                match chooser array.[i] with
+                | None -> loop (i + 1)
+                | res -> res
+
+        loop 0
+
     []
-    let choose (chooser: 'T -> 'U Option) (array: 'T[]) =             
-        checkNonNull "array" array                    
-        
+    let choose (chooser: 'T -> 'U Option) (array: 'T[]) =
+        checkNonNull "array" array
+
         let mutable i = 0
         let mutable first = Unchecked.defaultof<'U>
         let mutable found = false
+
         while i < array.Length && not found do
             let element = array.[i]
-            match chooser element with 
+
+            match chooser element with
             | None -> i <- i + 1
-            | Some b -> first <- b; found <- true                            
-            
+            | Some b ->
+                first <- b
+                found <- true
+
         if i <> array.Length then
 
-            let chunk1: 'U[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked ((array.Length >>> 2) + 1)
+            let chunk1: 'U[] =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked ((array.Length >>> 2) + 1)
+
             chunk1.[0] <- first
-            let mutable count = 1            
-            i <- i + 1                                
+            let mutable count = 1
+            i <- i + 1
+
             while count < chunk1.Length && i < array.Length do
-                let element = array.[i]                                
+                let element = array.[i]
+
                 match chooser element with
                 | None -> ()
-                | Some b -> chunk1.[count] <- b
-                            count <- count + 1                            
+                | Some b ->
+                    chunk1.[count] <- b
+                    count <- count + 1
+
                 i <- i + 1
-            
-            if i < array.Length then                            
-                let chunk2: 'U[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (array.Length-i)                        
+
+            if i < array.Length then
+                let chunk2: 'U[] =
+                    Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (array.Length - i)
+
                 count <- 0
+
                 while i < array.Length do
-                    let element = array.[i]                                
+                    let element = array.[i]
+
                     match chooser element with
                     | None -> ()
-                    | Some b -> chunk2.[count] <- b
-                                count <- count + 1                            
+                    | Some b ->
+                        chunk2.[count] <- b
+                        count <- count + 1
+
                     i <- i + 1
 
-                let res: 'U[] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (chunk1.Length + count)
+                let res: 'U[] =
+                    Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (chunk1.Length + count)
+
                 Array.Copy(chunk1, res, chunk1.Length)
                 Array.Copy(chunk2, 0, res, chunk1.Length, count)
                 res
             else
-                Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count chunk1                
+                Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count chunk1
         else
             empty
 
-    // The filter module is a space and performance for Array.filter based optimization that uses 
-    // a bitarray to store the results of the filtering of every element of the array. This means 
+    // The filter module is a space and performance for Array.filter based optimization that uses
+    // a bitarray to store the results of the filtering of every element of the array. This means
     // that the only additional temporary garbage that needs to be allocated is {array.Length/8} bytes.
     //
-    // Other optimizations include: 
+    // Other optimizations include:
     // - arrays < 32 elements don't allocate any garbage at all
     // - when the predicate yields consecutive runs of true data that is >= 32 elements (and fall
     //   into maskArray buckets) are copied in chunks using System.Array.Copy
     module Filter =
-        let private populateMask<'a> (f: 'a->bool) (src: array<'a>) (maskArray: array) =
+        let private populateMask<'a> (f: 'a -> bool) (src: array<'a>) (maskArray: array) =
             let mutable count = 0
-            for maskIdx = 0 to maskArray.Length-1 do
+
+            for maskIdx = 0 to maskArray.Length - 1 do
                 let srcIdx = maskIdx * 32
                 let mutable mask = 0u
-                if f src.[srcIdx+0x00] then mask <- mask ||| (1u <<< 0x00); count <- count + 1
-                if f src.[srcIdx+0x01] then mask <- mask ||| (1u <<< 0x01); count <- count + 1
-                if f src.[srcIdx+0x02] then mask <- mask ||| (1u <<< 0x02); count <- count + 1
-                if f src.[srcIdx+0x03] then mask <- mask ||| (1u <<< 0x03); count <- count + 1
-                if f src.[srcIdx+0x04] then mask <- mask ||| (1u <<< 0x04); count <- count + 1
-                if f src.[srcIdx+0x05] then mask <- mask ||| (1u <<< 0x05); count <- count + 1
-                if f src.[srcIdx+0x06] then mask <- mask ||| (1u <<< 0x06); count <- count + 1
-                if f src.[srcIdx+0x07] then mask <- mask ||| (1u <<< 0x07); count <- count + 1
-                if f src.[srcIdx+0x08] then mask <- mask ||| (1u <<< 0x08); count <- count + 1
-                if f src.[srcIdx+0x09] then mask <- mask ||| (1u <<< 0x09); count <- count + 1
-                if f src.[srcIdx+0x0A] then mask <- mask ||| (1u <<< 0x0A); count <- count + 1
-                if f src.[srcIdx+0x0B] then mask <- mask ||| (1u <<< 0x0B); count <- count + 1
-                if f src.[srcIdx+0x0C] then mask <- mask ||| (1u <<< 0x0C); count <- count + 1
-                if f src.[srcIdx+0x0D] then mask <- mask ||| (1u <<< 0x0D); count <- count + 1
-                if f src.[srcIdx+0x0E] then mask <- mask ||| (1u <<< 0x0E); count <- count + 1
-                if f src.[srcIdx+0x0F] then mask <- mask ||| (1u <<< 0x0F); count <- count + 1
-                if f src.[srcIdx+0x10] then mask <- mask ||| (1u <<< 0x10); count <- count + 1
-                if f src.[srcIdx+0x11] then mask <- mask ||| (1u <<< 0x11); count <- count + 1
-                if f src.[srcIdx+0x12] then mask <- mask ||| (1u <<< 0x12); count <- count + 1
-                if f src.[srcIdx+0x13] then mask <- mask ||| (1u <<< 0x13); count <- count + 1
-                if f src.[srcIdx+0x14] then mask <- mask ||| (1u <<< 0x14); count <- count + 1
-                if f src.[srcIdx+0x15] then mask <- mask ||| (1u <<< 0x15); count <- count + 1
-                if f src.[srcIdx+0x16] then mask <- mask ||| (1u <<< 0x16); count <- count + 1
-                if f src.[srcIdx+0x17] then mask <- mask ||| (1u <<< 0x17); count <- count + 1
-                if f src.[srcIdx+0x18] then mask <- mask ||| (1u <<< 0x18); count <- count + 1
-                if f src.[srcIdx+0x19] then mask <- mask ||| (1u <<< 0x19); count <- count + 1
-                if f src.[srcIdx+0x1A] then mask <- mask ||| (1u <<< 0x1A); count <- count + 1
-                if f src.[srcIdx+0x1B] then mask <- mask ||| (1u <<< 0x1B); count <- count + 1
-                if f src.[srcIdx+0x1C] then mask <- mask ||| (1u <<< 0x1C); count <- count + 1
-                if f src.[srcIdx+0x1D] then mask <- mask ||| (1u <<< 0x1D); count <- count + 1
-                if f src.[srcIdx+0x1E] then mask <- mask ||| (1u <<< 0x1E); count <- count + 1
-                if f src.[srcIdx+0x1F] then mask <- mask ||| (1u <<< 0x1F); count <- count + 1
+
+                if f src.[srcIdx + 0x00] then
+                    mask <- mask ||| (1u <<< 0x00)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x01] then
+                    mask <- mask ||| (1u <<< 0x01)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x02] then
+                    mask <- mask ||| (1u <<< 0x02)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x03] then
+                    mask <- mask ||| (1u <<< 0x03)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x04] then
+                    mask <- mask ||| (1u <<< 0x04)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x05] then
+                    mask <- mask ||| (1u <<< 0x05)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x06] then
+                    mask <- mask ||| (1u <<< 0x06)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x07] then
+                    mask <- mask ||| (1u <<< 0x07)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x08] then
+                    mask <- mask ||| (1u <<< 0x08)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x09] then
+                    mask <- mask ||| (1u <<< 0x09)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x0A] then
+                    mask <- mask ||| (1u <<< 0x0A)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x0B] then
+                    mask <- mask ||| (1u <<< 0x0B)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x0C] then
+                    mask <- mask ||| (1u <<< 0x0C)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x0D] then
+                    mask <- mask ||| (1u <<< 0x0D)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x0E] then
+                    mask <- mask ||| (1u <<< 0x0E)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x0F] then
+                    mask <- mask ||| (1u <<< 0x0F)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x10] then
+                    mask <- mask ||| (1u <<< 0x10)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x11] then
+                    mask <- mask ||| (1u <<< 0x11)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x12] then
+                    mask <- mask ||| (1u <<< 0x12)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x13] then
+                    mask <- mask ||| (1u <<< 0x13)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x14] then
+                    mask <- mask ||| (1u <<< 0x14)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x15] then
+                    mask <- mask ||| (1u <<< 0x15)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x16] then
+                    mask <- mask ||| (1u <<< 0x16)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x17] then
+                    mask <- mask ||| (1u <<< 0x17)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x18] then
+                    mask <- mask ||| (1u <<< 0x18)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x19] then
+                    mask <- mask ||| (1u <<< 0x19)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x1A] then
+                    mask <- mask ||| (1u <<< 0x1A)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x1B] then
+                    mask <- mask ||| (1u <<< 0x1B)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x1C] then
+                    mask <- mask ||| (1u <<< 0x1C)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x1D] then
+                    mask <- mask ||| (1u <<< 0x1D)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x1E] then
+                    mask <- mask ||| (1u <<< 0x1E)
+                    count <- count + 1
+
+                if f src.[srcIdx + 0x1F] then
+                    mask <- mask ||| (1u <<< 0x1F)
+                    count <- count + 1
+
                 maskArray.[maskIdx] <- mask
-            count 
 
-        let private createMask<'a> (f: 'a->bool) (src: array<'a>) (maskArrayOut: byref>) (leftoverMaskOut: byref) =
+            count
+
+        let private createMask<'a> (f: 'a -> bool) (src: array<'a>) (maskArrayOut: byref>) (leftoverMaskOut: byref) =
             let maskArrayLength = src.Length / 0x20
 
             // null when there are less than 32 items in src array.
             let maskArray =
-                if maskArrayLength = 0 then Unchecked.defaultof<_>
-                else Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked maskArrayLength
+                if maskArrayLength = 0 then
+                    Unchecked.defaultof<_>
+                else
+                    Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked maskArrayLength
 
             let mutable count =
                 match maskArray with
@@ -580,23 +840,30 @@ module Array =
             let leftoverMask =
                 match src.Length % 0x20 with
                 | 0 -> 0u
-                | _ -> 
+                | _ ->
                     let mutable mask = 0u
                     let mutable elementMask = 1u
-                    for arrayIdx = maskArrayLength*0x20 to src.Length-1 do
-                        if f src.[arrayIdx] then mask <- mask ||| elementMask; count <- count + 1
+
+                    for arrayIdx = maskArrayLength * 0x20 to src.Length - 1 do
+                        if f src.[arrayIdx] then
+                            mask <- mask ||| elementMask
+                            count <- count + 1
+
                         elementMask <- elementMask <<< 1
+
                     mask
 
-            maskArrayOut    <- maskArray
+            maskArrayOut <- maskArray
             leftoverMaskOut <- leftoverMask
             count
 
-        let private populateDstViaMask<'a> (src: array<'a>) (maskArray: array) (dst: array<'a>)  =
+        let private populateDstViaMask<'a> (src: array<'a>) (maskArray: array) (dst: array<'a>) =
             let mutable dstIdx = 0
             let mutable batchCount = 0
-            for maskIdx = 0 to maskArray.Length-1 do
+
+            for maskIdx = 0 to maskArray.Length - 1 do
                 let mask = maskArray.[maskIdx]
+
                 if mask = 0xFFFFFFFFu then
                     batchCount <- batchCount + 1
                 else
@@ -604,48 +871,143 @@ module Array =
 
                     if batchCount <> 0 then
                         let batchSize = batchCount * 0x20
-                        System.Array.Copy (src, srcIdx-batchSize, dst, dstIdx, batchSize)
+                        System.Array.Copy(src, srcIdx - batchSize, dst, dstIdx, batchSize)
                         dstIdx <- dstIdx + batchSize
                         batchCount <- 0
 
                     if mask <> 0u then
-                        if mask &&& (1u <<< 0x00) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x00]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x01) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x01]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x02) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x02]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x03) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x03]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x04) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x04]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x05) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x05]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x06) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x06]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x07) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x07]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x08) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x08]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x09) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x09]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x0A) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x0A]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x0B) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x0B]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x0C) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x0C]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x0D) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x0D]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x0E) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x0E]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x0F) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x0F]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x10) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x10]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x11) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x11]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x12) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x12]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x13) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x13]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x14) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x14]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x15) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x15]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x16) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x16]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x17) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x17]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x18) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x18]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x19) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x19]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x1A) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x1A]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x1B) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x1B]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x1C) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x1C]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x1D) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x1D]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x1E) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x1E]; dstIdx <- dstIdx + 1
-                        if mask &&& (1u <<< 0x1F) <> 0u then dst.[dstIdx] <- src.[srcIdx+0x1F]; dstIdx <- dstIdx + 1
+                        if mask &&& (1u <<< 0x00) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x00]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x01) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x01]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x02) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x02]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x03) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x03]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x04) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x04]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x05) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x05]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x06) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x06]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x07) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x07]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x08) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x08]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x09) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x09]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x0A) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x0A]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x0B) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x0B]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x0C) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x0C]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x0D) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x0D]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x0E) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x0E]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x0F) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x0F]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x10) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x10]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x11) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x11]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x12) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x12]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x13) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x13]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x14) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x14]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x15) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x15]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x16) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x16]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x17) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x17]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x18) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x18]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x19) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x19]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x1A) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x1A]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x1B) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x1B]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x1C) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x1C]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x1D) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x1D]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x1E) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x1E]
+                            dstIdx <- dstIdx + 1
+
+                        if mask &&& (1u <<< 0x1F) <> 0u then
+                            dst.[dstIdx] <- src.[srcIdx + 0x1F]
+                            dstIdx <- dstIdx + 1
 
             if batchCount <> 0 then
                 let srcIdx = maskArray.Length * 0x20
                 let batchSize = batchCount * 0x20
-                System.Array.Copy (src, srcIdx-batchSize, dst, dstIdx, batchSize)
+                System.Array.Copy(src, srcIdx - batchSize, dst, dstIdx, batchSize)
                 dstIdx <- dstIdx + batchSize
 
             dstIdx
@@ -654,34 +1016,41 @@ module Array =
             let dst = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
 
             let mutable dstIdx = 0
+
             let srcIdx =
                 match maskArray with
                 | null -> 0
                 | _ ->
                     dstIdx <- populateDstViaMask src maskArray dst
-                    maskArray.Length*0x20
+                    maskArray.Length * 0x20
 
             let mutable elementMask = 1u
-            for srcIdx = srcIdx to src.Length-1 do
-                if leftoverMask &&& elementMask <> 0u then dst.[dstIdx] <- src.[srcIdx]; dstIdx <- dstIdx + 1
+
+            for srcIdx = srcIdx to src.Length - 1 do
+                if leftoverMask &&& elementMask <> 0u then
+                    dst.[dstIdx] <- src.[srcIdx]
+                    dstIdx <- dstIdx + 1
+
                 elementMask <- elementMask <<< 1
 
             dst
 
         let filter f (src: array<_>) =
-            let mutable maskArray    = Unchecked.defaultof<_>
+            let mutable maskArray = Unchecked.defaultof<_>
             let mutable leftOverMask = Unchecked.defaultof<_>
+
             match createMask f src &maskArray &leftOverMask with
-            | 0     -> empty
+            | 0 -> empty
             | count -> filterViaMask maskArray leftOverMask count src
 
     []
-    let filter predicate (array: _[]) =         
+    let filter predicate (array: _[]) =
         checkNonNull "array" array
         Filter.filter predicate array
-        
+
     []
-    let where predicate (array: _[]) = filter predicate array
+    let where predicate (array: _[]) =
+        filter predicate array
 
     []
     let except (itemsToExclude: seq<_>) (array: _[]) =
@@ -695,49 +1064,68 @@ module Array =
             array |> filter cached.Add
 
     []
-    let partition predicate (array: _[]) = 
+    let partition predicate (array: _[]) =
         checkNonNull "array" array
-        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length        
+        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
         let mutable upCount = 0
-        let mutable downCount = array.Length-1    
-        for x in array do                
-            if predicate x then 
+        let mutable downCount = array.Length - 1
+
+        for x in array do
+            if predicate x then
                 res.[upCount] <- x
                 upCount <- upCount + 1
             else
                 res.[downCount] <- x
                 downCount <- downCount - 1
-            
+
         let res1 = Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 upCount res
-        let res2 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (array.Length - upCount)    
-    
-        downCount <- array.Length-1
-        for i = 0 to res2.Length-1 do
+
+        let res2 =
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (array.Length - upCount)
+
+        downCount <- array.Length - 1
+
+        for i = 0 to res2.Length - 1 do
             res2.[i] <- res.[downCount]
             downCount <- downCount - 1
-    
+
         res1, res2
 
     []
-    let find predicate (array: _[]) = 
+    let find predicate (array: _[]) =
         checkNonNull "array" array
-        let rec loop i = 
-            if i >= array.Length then indexNotFound() else
-            if predicate array.[i] then array.[i]  else loop (i+1)
-        loop 0 
+
+        let rec loop i =
+            if i >= array.Length then
+                indexNotFound ()
+            else if predicate array.[i] then
+                array.[i]
+            else
+                loop (i + 1)
+
+        loop 0
 
     []
-    let tryFind predicate (array: _[]) = 
+    let tryFind predicate (array: _[]) =
         checkNonNull "array" array
-        let rec loop i = 
-            if i >= array.Length then None else 
-            if predicate array.[i] then Some array.[i]  else loop (i+1)
-        loop 0 
+
+        let rec loop i =
+            if i >= array.Length then
+                None
+            else if predicate array.[i] then
+                Some array.[i]
+            else
+                loop (i + 1)
+
+        loop 0
 
     []
     let skip count (array: 'T[]) =
         checkNonNull "array" array
-        if count > array.Length then invalidArgOutOfRange "count" count "array.Length" array.Length
+
+        if count > array.Length then
+            invalidArgOutOfRange "count" count "array.Length" array.Length
+
         if count = array.Length then
             empty
         else
@@ -745,10 +1133,12 @@ module Array =
             Microsoft.FSharp.Primitives.Basics.Array.subUnchecked count (array.Length - count) array
 
     []
-    let skipWhile predicate (array: 'T[]) =        
+    let skipWhile predicate (array: 'T[]) =
         checkNonNull "array" array
-        let mutable i = 0            
-        while i < array.Length && predicate array.[i] do i <- i + 1
+        let mutable i = 0
+
+        while i < array.Length && predicate array.[i] do
+            i <- i + 1
 
         match array.Length - i with
         | 0 -> empty
@@ -777,61 +1167,91 @@ module Array =
     []
     let windowed windowSize (array: 'T[]) =
         checkNonNull "array" array
-        if windowSize <= 0 then invalidArgInputMustBePositive "windowSize" windowSize
+
+        if windowSize <= 0 then
+            invalidArgInputMustBePositive "windowSize" windowSize
+
         let len = array.Length
+
         if windowSize > len then
             empty
         else
-            let res: 'T[][] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (len - windowSize + 1) 
+            let res: 'T[][] =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (len - windowSize + 1)
+
             for i = 0 to len - windowSize do
                 res.[i] <- Microsoft.FSharp.Primitives.Basics.Array.subUnchecked i windowSize array
+
             res
 
     []
     let chunkBySize chunkSize (array: 'T[]) =
         checkNonNull "array" array
-        if chunkSize <= 0 then invalidArgInputMustBePositive "chunkSize" chunkSize
+
+        if chunkSize <= 0 then
+            invalidArgInputMustBePositive "chunkSize" chunkSize
+
         let len = array.Length
+
         if len = 0 then
             empty
         else if chunkSize > len then
             [| copy array |]
         else
             let chunkCount = (len - 1) / chunkSize + 1
-            let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked chunkCount: 'T[][]
+
+            let res: 'T[][] =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked chunkCount
+
             for i = 0 to len / chunkSize - 1 do
                 res.[i] <- Microsoft.FSharp.Primitives.Basics.Array.subUnchecked (i * chunkSize) chunkSize array
+
             if len % chunkSize <> 0 then
-                res.[chunkCount - 1] <- Microsoft.FSharp.Primitives.Basics.Array.subUnchecked ((chunkCount - 1) * chunkSize) (len % chunkSize) array
+                res.[chunkCount - 1] <-
+                    Microsoft.FSharp.Primitives.Basics.Array.subUnchecked ((chunkCount - 1) * chunkSize) (len % chunkSize) array
+
             res
 
     []
     let splitInto count (array: _[]) =
         checkNonNull "array" array
-        if count <= 0 then invalidArgInputMustBePositive "count" count
+
+        if count <= 0 then
+            invalidArgInputMustBePositive "count" count
+
         Microsoft.FSharp.Primitives.Basics.Array.splitInto count array
 
     []
-    let zip (array1: _[]) (array2: _[]) = 
+    let zip (array1: _[]) (array2: _[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let len1 = array1.Length 
-        if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1 
-        for i = 0 to res.Length-1 do 
+        let len1 = array1.Length
+
+        if len1 <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- (array1.[i], array2.[i])
+
         res
 
     []
-    let zip3 (array1: _[]) (array2: _[]) (array3: _[]) = 
+    let zip3 (array1: _[]) (array2: _[]) (array3: _[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
         checkNonNull "array3" array3
         let len1 = array1.Length
-        if len1 <> array2.Length || len1 <> array3.Length then invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
-        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1 
-        for i = 0 to res.Length-1 do 
+
+        if len1 <> array2.Length || len1 <> array3.Length then
+            invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
+
+        let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1
+
+        for i = 0 to res.Length - 1 do
             res.[i] <- (array1.[i], array2.[i], array3.[i])
+
         res
 
     []
@@ -841,179 +1261,218 @@ module Array =
         let len1 = array1.Length
         let len2 = array2.Length
         let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (len1 * len2)
-        for i = 0 to array1.Length-1 do
-            for j = 0 to array2.Length-1 do
+
+        for i = 0 to array1.Length - 1 do
+            for j = 0 to array2.Length - 1 do
                 res.[i * len2 + j] <- (array1.[i], array2.[j])
+
         res
 
     []
-    let unfold<'T, 'State> (generator: 'State -> ('T*'State) option) (state: 'State) =
+    let unfold<'T, 'State> (generator: 'State -> ('T * 'State) option) (state: 'State) =
         let res = ResizeArray<_>()
+
         let rec loop state =
             match generator state with
             | None -> ()
             | Some (x, s') ->
                 res.Add(x)
                 loop s'
+
         loop state
         res.ToArray()
 
     []
-    let unzip (array: _[]) = 
+    let unzip (array: _[]) =
         checkNonNull "array" array
-        let len = array.Length 
-        let res1 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len 
-        let res2 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len 
-        for i = 0 to array.Length-1 do 
-            let x, y = array.[i] 
+        let len = array.Length
+        let res1 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
+        let res2 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
+
+        for i = 0 to array.Length - 1 do
+            let x, y = array.[i]
             res1.[i] <- x
             res2.[i] <- y
+
         res1, res2
 
     []
-    let unzip3 (array: _[]) = 
-        checkNonNull "array" array
-        let len = array.Length 
-        let res1 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len 
-        let res2 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len 
-        let res3 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len 
-        for i = 0 to array.Length-1 do 
-            let x, y, z = array.[i] 
+    let unzip3 (array: _[]) =
+        checkNonNull "array" array
+        let len = array.Length
+        let res1 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
+        let res2 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
+        let res3 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
+
+        for i = 0 to array.Length - 1 do
+            let x, y, z = array.[i]
             res1.[i] <- x
             res2.[i] <- y
             res3.[i] <- z
+
         res1, res2, res3
 
     []
-    let rev (array: _[]) = 
+    let rev (array: _[]) =
         checkNonNull "array" array
         let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
-        let mutable j = array.Length-1
-        for i = 0 to array.Length-1 do 
+        let mutable j = array.Length - 1
+
+        for i = 0 to array.Length - 1 do
             res.[j] <- array.[i]
             j <- j - 1
+
         res
 
     []
     let fold<'T, 'State> (folder: 'State -> 'T -> 'State) (state: 'State) (array: 'T[]) =
         checkNonNull "array" array
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(folder)
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (folder)
         let mutable state = state
-        for i = 0 to array.Length-1 do 
+
+        for i = 0 to array.Length - 1 do
             state <- f.Invoke(state, array.[i])
+
         state
 
     []
     let foldBack<'T, 'State> (folder: 'T -> 'State -> 'State) (array: 'T[]) (state: 'State) =
         checkNonNull "array" array
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(folder)
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (folder)
         let mutable res = state
-        for i = array.Length-1 downto 0 do 
+
+        for i = array.Length - 1 downto 0 do
             res <- f.Invoke(array.[i], res)
+
         res
 
     []
-    let foldBack2<'T1, 'T2, 'State>  folder (array1: 'T1[]) (array2: 'T2 []) (state: 'State) =
+    let foldBack2<'T1, 'T2, 'State> folder (array1: 'T1[]) (array2: 'T2[]) (state: 'State) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(folder)
-        let mutable res = state 
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (folder)
+        let mutable res = state
         let len = array1.Length
-        if len <> array2.Length then invalidArgDifferentArrayLength "array1" len "array2" array2.Length
-        for i = len-1 downto 0 do 
+
+        if len <> array2.Length then
+            invalidArgDifferentArrayLength "array1" len "array2" array2.Length
+
+        for i = len - 1 downto 0 do
             res <- f.Invoke(array1.[i], array2.[i], res)
+
         res
 
     []
-    let fold2<'T1, 'T2, 'State>  folder (state: 'State) (array1: 'T1[]) (array2: 'T2 []) =
+    let fold2<'T1, 'T2, 'State> folder (state: 'State) (array1: 'T1[]) (array2: 'T2[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(folder)
-        let mutable state = state 
-        if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
-        for i = 0 to array1.Length-1 do 
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (folder)
+        let mutable state = state
+
+        if array1.Length <> array2.Length then
+            invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
+
+        for i = 0 to array1.Length - 1 do
             state <- f.Invoke(state, array1.[i], array2.[i])
+
         state
 
-    let foldSubRight f (array: _[]) start fin acc = 
+    let foldSubRight f (array: _[]) start fin acc =
         checkNonNull "array" array
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(f)
-        let mutable res = acc 
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (f)
+        let mutable res = acc
+
         for i = fin downto start do
             res <- f.Invoke(array.[i], res)
+
         res
 
-    let scanSubLeft f initState (array: _[]) start fin = 
+    let scanSubLeft f initState (array: _[]) start fin =
         checkNonNull "array" array
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(f)
-        let mutable state = initState 
-        let res = create (2+fin-start) initState 
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (f)
+        let mutable state = initState
+        let res = create (2 + fin - start) initState
+
         for i = start to fin do
             state <- f.Invoke(state, array.[i])
-            res.[i - start+1] <- state
+            res.[i - start + 1] <- state
+
         res
 
     []
-    let scan<'T, 'State> folder (state: 'State) (array: 'T[]) = 
+    let scan<'T, 'State> folder (state: 'State) (array: 'T[]) =
         checkNonNull "array" array
         let len = array.Length
         scanSubLeft folder state array 0 (len - 1)
 
     []
-    let scanBack<'T, 'State> folder (array: 'T[]) (state: 'State) = 
+    let scanBack<'T, 'State> folder (array: 'T[]) (state: 'State) =
         checkNonNull "array" array
         Microsoft.FSharp.Primitives.Basics.Array.scanSubRight folder array 0 (array.Length - 1) state
 
     []
-    let inline singleton value = [|value|]
+    let inline singleton value =
+        [| value |]
 
     []
     let pairwise (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length < 2 then empty else
-        init (array.Length-1) (fun i -> array.[i], array.[i+1])
+
+        if array.Length < 2 then
+            empty
+        else
+            init (array.Length - 1) (fun i -> array.[i], array.[i + 1])
 
     []
-    let reduce reduction (array: _[]) = 
+    let reduce reduction (array: _[]) =
         checkNonNull "array" array
         let len = array.Length
-        if len = 0 then 
+
+        if len = 0 then
             invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
-        else 
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(reduction)
+        else
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (reduction)
             let mutable res = array.[0]
-            for i = 1 to array.Length-1 do
+
+            for i = 1 to array.Length - 1 do
                 res <- f.Invoke(res, array.[i])
+
             res
 
     []
-    let reduceBack reduction (array: _[]) = 
+    let reduceBack reduction (array: _[]) =
         checkNonNull "array" array
         let len = array.Length
-        if len = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
-        else foldSubRight reduction array 0 (len - 2) array.[len - 1]
+
+        if len = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+        else
+            foldSubRight reduction array 0 (len - 2) array.[len - 1]
 
     []
     let sortInPlaceWith comparer (array: 'T[]) =
         checkNonNull "array" array
-        let len = array.Length 
-        if len < 2 then () 
-        elif len = 2 then 
-            let c = comparer array.[0] array.[1] 
+        let len = array.Length
+
+        if len < 2 then
+            ()
+        elif len = 2 then
+            let c = comparer array.[0] array.[1]
+
             if c > 0 then
-                let tmp = array.[0] 
+                let tmp = array.[0]
                 array.[0] <- array.[1]
                 array.[1] <- tmp
-        else 
+        else
             Array.Sort(array, ComparisonIdentity.FromFunction(comparer))
 
     []
-    let sortInPlaceBy (projection: 'T -> 'U) (array: 'T[]) = 
+    let sortInPlaceBy (projection: 'T -> 'U) (array: 'T[]) =
         checkNonNull "array" array
         Microsoft.FSharp.Primitives.Basics.Array.unstableSortInPlaceBy projection array
 
     []
-    let sortInPlace (array: 'T[]) = 
+    let sortInPlace (array: 'T[]) =
         checkNonNull "array" array
         Microsoft.FSharp.Primitives.Basics.Array.unstableSortInPlace array
 
@@ -1032,7 +1491,7 @@ module Array =
         result
 
     []
-    let sort array = 
+    let sort array =
         checkNonNull "array" array
         let result = copy array
         sortInPlace result
@@ -1041,144 +1500,189 @@ module Array =
     []
     let inline sortByDescending projection array =
         checkNonNull "array" array
-        let inline compareDescending a b = compare (projection b) (projection a)
+
+        let inline compareDescending a b =
+            compare (projection b) (projection a)
+
         sortWith compareDescending array
 
     []
     let inline sortDescending array =
         checkNonNull "array" array
-        let inline compareDescending a b = compare b a
+
+        let inline compareDescending a b =
+            compare b a
+
         sortWith compareDescending array
 
     []
-    let toSeq array = 
+    let toSeq array =
         checkNonNull "array" array
         Seq.ofArray array
 
     []
-    let ofSeq source = 
+    let ofSeq source =
         checkNonNull "source" source
         Seq.toArray source
 
     []
-    let findIndex predicate (array: _[]) = 
-        checkNonNull "array" array
-        let len = array.Length 
-        let rec go n = 
-            if n >= len then 
-                indexNotFound()
-            elif predicate array.[n] then
-                n 
-            else go (n+1)
+    let findIndex predicate (array: _[]) =
+        checkNonNull "array" array
+        let len = array.Length
+
+        let rec go n =
+            if n >= len then indexNotFound ()
+            elif predicate array.[n] then n
+            else go (n + 1)
+
         go 0
 
     []
-    let tryFindIndex predicate (array: _[]) = 
+    let tryFindIndex predicate (array: _[]) =
         checkNonNull "array" array
-        let len = array.Length 
-        let rec go n = if n >= len then None elif predicate array.[n] then Some n else go (n+1)
-        go 0 
+        let len = array.Length
+
+        let rec go n =
+            if n >= len then None
+            elif predicate array.[n] then Some n
+            else go (n + 1)
+
+        go 0
 
     []
-    let permute indexMap (array: _[]) =  
+    let permute indexMap (array: _[]) =
         checkNonNull "array" array
         Microsoft.FSharp.Primitives.Basics.Array.permute indexMap array
 
     []
-    let inline sum (array: ^T[] ) : ^T = 
+    let inline sum (array: ^T[]) : ^T =
         checkNonNull "array" array
         let mutable acc = LanguagePrimitives.GenericZero< ^T>
+
         for i = 0 to array.Length - 1 do
             acc <- Checked.(+) acc array.[i]
+
         acc
 
     []
-    let inline sumBy ([] projection: 'T -> ^U) (array: 'T[]) : ^U = 
+    let inline sumBy ([] projection: 'T -> ^U) (array: 'T[]) : ^U =
         checkNonNull "array" array
         let mutable acc = LanguagePrimitives.GenericZero< ^U>
+
         for i = 0 to array.Length - 1 do
             acc <- Checked.(+) acc (projection array.[i])
+
         acc
 
     []
-    let inline min (array: _[]) = 
+    let inline min (array: _[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
         let mutable acc = array.[0]
+
         for i = 1 to array.Length - 1 do
             let curr = array.[i]
-            if curr < acc then 
-                acc <- curr
+            if curr < acc then acc <- curr
+
         acc
 
     []
-    let inline minBy ([] projection) (array: _[]) = 
+    let inline minBy ([] projection) (array: _[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
         let mutable accv = array.[0]
         let mutable acc = projection accv
+
         for i = 1 to array.Length - 1 do
             let currv = array.[i]
             let curr = projection currv
+
             if curr < acc then
                 acc <- curr
                 accv <- currv
+
         accv
 
     []
-    let inline max (array: _[]) = 
+    let inline max (array: _[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
         let mutable acc = array.[0]
+
         for i = 1 to array.Length - 1 do
             let curr = array.[i]
-            if curr > acc then 
-                acc <- curr
+            if curr > acc then acc <- curr
+
         acc
 
     []
-    let inline maxBy projection (array: _[]) = 
+    let inline maxBy projection (array: _[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
         let mutable accv = array.[0]
         let mutable acc = projection accv
+
         for i = 1 to array.Length - 1 do
             let currv = array.[i]
             let curr = projection currv
+
             if curr > acc then
                 acc <- curr
                 accv <- currv
+
         accv
 
     []
-    let inline average (array: 'T[]) = 
+    let inline average (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
         let mutable acc = LanguagePrimitives.GenericZero< ^T>
+
         for i = 0 to array.Length - 1 do
             acc <- Checked.(+) acc array.[i]
+
         LanguagePrimitives.DivideByInt< ^T> acc array.Length
 
     []
-    let inline averageBy ([] projection: 'T -> ^U) (array: 'T[]) : ^U = 
+    let inline averageBy ([] projection: 'T -> ^U) (array: 'T[]) : ^U =
         checkNonNull "array" array
-        if array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
+        if array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
+
         let mutable acc = LanguagePrimitives.GenericZero< ^U>
+
         for i = 0 to array.Length - 1 do
             acc <- Checked.(+) acc (projection array.[i])
+
         LanguagePrimitives.DivideByInt< ^U> acc array.Length
 
     []
-    let inline compareWith ([] comparer: 'T -> 'T -> int) (array1: 'T[]) (array2: 'T[]) = 
+    let inline compareWith ([] comparer: 'T -> 'T -> int) (array1: 'T[]) (array2: 'T[]) =
         checkNonNull "array1" array1
         checkNonNull "array2" array2
 
         let length1 = array1.Length
         let length2 = array2.Length
-        
+
         let mutable i = 0
         let mutable result = 0
-        
+
         if length1 < length2 then
             while i < array1.Length && result = 0 do
                 result <- comparer array1.[i] array2.[i]
@@ -1196,9 +1700,16 @@ module Array =
     []
     let sub (array: 'T[]) (startIndex: int) (count: int) =
         checkNonNull "array" array
-        if startIndex < 0 then invalidArgInputMustBeNonNegative "startIndex" startIndex
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
-        if startIndex + count > array.Length then invalidArgOutOfRange "count" count "array.Length" array.Length
+
+        if startIndex < 0 then
+            invalidArgInputMustBeNonNegative "startIndex" startIndex
+
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
+        if startIndex + count > array.Length then
+            invalidArgOutOfRange "count" count "array.Length" array.Length
+
         Microsoft.FSharp.Primitives.Basics.Array.subUnchecked startIndex count array
 
     []
@@ -1208,57 +1719,80 @@ module Array =
     []
     let tryItem index (array: 'T[]) =
         checkNonNull "array" array
-        if index < 0 || index >= array.Length then None
-        else Some(array.[index])
+
+        if index < 0 || index >= array.Length then
+            None
+        else
+            Some(array.[index])
 
     []
-    let get (array: _[]) index = 
+    let get (array: _[]) index =
         array.[index]
 
     []
-    let set (array: _[]) index value = 
+    let set (array: _[]) index value =
         array.[index] <- value
 
     []
     let fill (target: 'T[]) (targetIndex: int) (count: int) (value: 'T) =
         checkNonNull "target" target
-        if targetIndex < 0 then invalidArgInputMustBeNonNegative "targetIndex" targetIndex
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
-        for i = targetIndex to targetIndex + count - 1 do 
+
+        if targetIndex < 0 then
+            invalidArgInputMustBeNonNegative "targetIndex" targetIndex
+
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
+        for i = targetIndex to targetIndex + count - 1 do
             target.[i] <- value
 
     []
     let exactlyOne (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 1 then array.[0]
-        elif array.Length = 0 then invalidArg "array" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
-        else invalidArg "array" (SR.GetString(SR.inputSequenceTooLong))
+
+        if array.Length = 1 then
+            array.[0]
+        elif array.Length = 0 then
+            invalidArg "array" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+        else
+            invalidArg "array" (SR.GetString(SR.inputSequenceTooLong))
 
     []
     let tryExactlyOne (array: 'T[]) =
         checkNonNull "array" array
-        if array.Length = 1 then Some array.[0]
-        else None
+
+        if array.Length = 1 then
+            Some array.[0]
+        else
+            None
 
     let transposeArrays (array: 'T[][]) =
         let len = array.Length
-        if len = 0 then Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0 else
-        let lenInner = array.[0].Length
-
-        for j in 1..len-1 do
-            if lenInner <> array.[j].Length then
-                invalidArgDifferentArrayLength "array.[0]" lenInner (String.Format("array.[{0}]", j)) array.[j].Length
-
-        let result: 'T[][] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked lenInner
-        for i in 0..lenInner-1 do
-            result.[i] <- Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
-            for j in 0..len-1 do
-                result.[i].[j] <- array.[j].[i]
-        result
+
+        if len = 0 then
+            Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
+        else
+            let lenInner = array.[0].Length
+
+            for j in 1 .. len - 1 do
+                if lenInner <> array.[j].Length then
+                    invalidArgDifferentArrayLength "array.[0]" lenInner (String.Format("array.[{0}]", j)) array.[j].Length
+
+            let result: 'T[][] =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked lenInner
+
+            for i in 0 .. lenInner - 1 do
+                result.[i] <- Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
+
+                for j in 0 .. len - 1 do
+                    result.[i].[j] <- array.[j].[i]
+
+            result
 
     []
     let transpose (arrays: seq<'T[]>) =
         checkNonNull "arrays" arrays
+
         match arrays with
         | :? ('T[][]) as ts -> ts |> transposeArrays // avoid a clone, since we only read the array
         | _ -> arrays |> Seq.toArray |> transposeArrays
@@ -1266,7 +1800,9 @@ module Array =
     []
     let truncate count (array: 'T[]) =
         checkNonNull "array" array
-        if count <= 0 then empty
+
+        if count <= 0 then
+            empty
         else
             let len = array.Length
             let count' = Operators.min count len
@@ -1275,186 +1811,234 @@ module Array =
     []
     let removeAt (index: int) (source: 'T[]) : 'T[] =
         checkNonNull "source" source
-        if index < 0 || index >= source.Length then invalidArg "index" "index must be within bounds of the array"
-        
+
+        if index < 0 || index >= source.Length then
+            invalidArg "index" "index must be within bounds of the array"
+
         let length = source.Length - 1
         let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length
-        if index > 0 then 
+
+        if index > 0 then
             Array.Copy(source, result, index)
+
         if length - index > 0 then
             Array.Copy(source, index + 1, result, index, length - index)
-    
+
         result
-    
+
     []
     let removeManyAt (index: int) (count: int) (source: 'T[]) : 'T[] =
         checkNonNull "source" source
-        if index < 0 || index > source.Length - count then invalidArg "index" "index must be within bounds of the array"
-        
+
+        if index < 0 || index > source.Length - count then
+            invalidArg "index" "index must be within bounds of the array"
+
         let length = source.Length - count
         let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length
 
         if index > 0 then
             Array.Copy(source, result, index)
+
         if length - index > 0 then
             Array.Copy(source, index + count, result, index, length - index)
-        
+
         result
-    
+
     []
     let updateAt (index: int) (value: 'T) (source: 'T[]) : 'T[] =
         checkNonNull "source" source
-        if index < 0 || index >= source.Length then invalidArg "index" "index must be within bounds of the array"
-        
+
+        if index < 0 || index >= source.Length then
+            invalidArg "index" "index must be within bounds of the array"
+
         let length = source.Length
         let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length
 
         if length > 0 then
             Array.Copy(source, result, length)
+
         result.[index] <- value
-        
+
         result
-    
+
     []
     let insertAt (index: int) (value: 'T) (source: 'T[]) : 'T[] =
         checkNonNull "source" source
-        if index < 0 || index > source.Length then invalidArg "index" "index must be within bounds of the array"
-        
+
+        if index < 0 || index > source.Length then
+            invalidArg "index" "index must be within bounds of the array"
+
         let length = source.Length + 1
         let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length
 
         if index > 0 then
             Array.Copy(source, result, index)
-        
+
         result.[index] <- value
-        
+
         if source.Length - index > 0 then
             Array.Copy(source, index, result, index + 1, source.Length - index)
-        
+
         result
-    
+
     []
     let insertManyAt (index: int) (values: seq<'T>) (source: 'T[]) : 'T[] =
         checkNonNull "source" source
-        if index < 0 || index > source.Length then invalidArg "index" "index must be within bounds of the array"
-        
+
+        if index < 0 || index > source.Length then
+            invalidArg "index" "index must be within bounds of the array"
+
         let valuesArray = Seq.toArray values
-        if valuesArray.Length = 0 then source
+
+        if valuesArray.Length = 0 then
+            source
         else
             let length = source.Length + valuesArray.Length
             let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked length
-            
+
             if index > 0 then
                 Array.Copy(source, result, index)
-                
+
             Array.Copy(valuesArray, 0, result, index, valuesArray.Length)
-            
+
             if source.Length - index > 0 then
                 Array.Copy(source, index, result, index + valuesArray.Length, source.Length - index)
-            
+
             result
 
     module Parallel =
         open System.Threading.Tasks
-        
+
         []
-        let choose chooser (array: 'T[]) = 
+        let choose chooser (array: 'T[]) =
             checkNonNull "array" array
             let inputLength = array.Length
 
-            let isChosen: bool [] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
-            let results: 'U [] = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength                
-            let mutable outputLength = 0        
-            Parallel.For(0, 
-                         inputLength, 
-                         (fun () ->0),
-                         (fun i _ count -> 
-                            match chooser array.[i] with 
-                            | None -> count 
-                            | Some v -> 
-                                isChosen.[i] <- true; 
-                                results.[i] <- v
-                                count+1),
-                         Action (fun x -> System.Threading.Interlocked.Add(&outputLength, x) |> ignore )
-                         ) |> ignore
-
-            let output = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked outputLength
+            let isChosen: bool[] =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
+            let results: 'U[] =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
+            let mutable outputLength = 0
+
+            Parallel.For(
+                0,
+                inputLength,
+                (fun () -> 0),
+                (fun i _ count ->
+                    match chooser array.[i] with
+                    | None -> count
+                    | Some v ->
+                        isChosen.[i] <- true
+                        results.[i] <- v
+                        count + 1),
+                Action(fun x -> System.Threading.Interlocked.Add(&outputLength, x) |> ignore)
+            )
+            |> ignore
+
+            let output =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked outputLength
+
             let mutable curr = 0
-            for i = 0 to isChosen.Length-1 do 
-                if isChosen.[i] then 
+
+            for i = 0 to isChosen.Length - 1 do
+                if isChosen.[i] then
                     output.[curr] <- results.[i]
                     curr <- curr + 1
+
             output
-            
+
         []
-        let collect (mapping: 'T -> 'U[])  (array: 'T[]) : 'U[]=
+        let collect (mapping: 'T -> 'U[]) (array: 'T[]) : 'U[] =
             checkNonNull "array" array
             let inputLength = array.Length
-            let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
-            Parallel.For(0, inputLength, 
-                (fun i -> result.[i] <- mapping array.[i])) |> ignore
+
+            let result =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
+            Parallel.For(0, inputLength, (fun i -> result.[i] <- mapping array.[i]))
+            |> ignore
+
             concatArrays result
-            
+
         []
-        let map (mapping: 'T -> 'U) (array: 'T[]) : 'U[]=
+        let map (mapping: 'T -> 'U) (array: 'T[]) : 'U[] =
             checkNonNull "array" array
             let inputLength = array.Length
-            let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
-            Parallel.For(0, inputLength, fun i ->
-                result.[i] <- mapping array.[i]) |> ignore
+
+            let result =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
+            Parallel.For(0, inputLength, (fun i -> result.[i] <- mapping array.[i]))
+            |> ignore
+
             result
-            
+
         []
         let mapi mapping (array: 'T[]) =
             checkNonNull "array" array
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(mapping)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (mapping)
             let inputLength = array.Length
-            let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength 
-            Parallel.For(0, inputLength, fun i ->
-                result.[i] <- f.Invoke (i, array.[i])) |> ignore
+
+            let result =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
+            Parallel.For(0, inputLength, (fun i -> result.[i] <- f.Invoke(i, array.[i])))
+            |> ignore
+
             result
-            
+
         []
         let iter action (array: 'T[]) =
             checkNonNull "array" array
-            Parallel.For (0, array.Length, fun i -> action array.[i]) |> ignore  
-            
+            Parallel.For(0, array.Length, (fun i -> action array.[i])) |> ignore
+
         []
         let iteri action (array: 'T[]) =
             checkNonNull "array" array
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
-            Parallel.For (0, array.Length, fun i -> f.Invoke(i, array.[i])) |> ignore        
-            
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (action)
+            Parallel.For(0, array.Length, (fun i -> f.Invoke(i, array.[i]))) |> ignore
+
         []
         let init count initializer =
             let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
-            Parallel.For (0, count, fun i -> result.[i] <- initializer i) |> ignore
+            Parallel.For(0, count, (fun i -> result.[i] <- initializer i)) |> ignore
             result
-            
+
         []
         let partition predicate (array: 'T[]) =
             checkNonNull "array" array
             let inputLength = array.Length
-           
-            let isTrue = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
+            let isTrue =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked inputLength
+
             let mutable trueLength = 0
-            Parallel.For(0, 
-                         inputLength, 
-                         (fun () -> 0), 
-                         (fun i _ trueCount -> 
-                            if predicate array.[i] then
-                                isTrue.[i] <- true
-                                trueCount + 1
-                            else
-                                trueCount),
-                         Action (fun x -> System.Threading.Interlocked.Add(&trueLength, x) |> ignore) ) |> ignore
-                            
+
+            Parallel.For(
+                0,
+                inputLength,
+                (fun () -> 0),
+                (fun i _ trueCount ->
+                    if predicate array.[i] then
+                        isTrue.[i] <- true
+                        trueCount + 1
+                    else
+                        trueCount),
+                Action(fun x -> System.Threading.Interlocked.Add(&trueLength, x) |> ignore)
+            )
+            |> ignore
+
             let res1 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked trueLength
-            let res2 = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (inputLength - trueLength)
+
+            let res2 =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (inputLength - trueLength)
 
             let mutable iTrue = 0
             let mutable iFalse = 0
-            for i = 0 to isTrue.Length-1 do
+
+            for i = 0 to isTrue.Length - 1 do
                 if isTrue.[i] then
                     res1.[iTrue] <- array.[i]
                     iTrue <- iTrue + 1
@@ -1462,4 +2046,4 @@ module Array =
                     res2.[iFalse] <- array.[i]
                     iFalse <- iFalse + 1
 
-            res1, res2
\ No newline at end of file
+            res1, res2
diff --git a/src/FSharp.Core/async.fs b/src/FSharp.Core/async.fs
index 0b64a6450d1..977f071d496 100644
--- a/src/FSharp.Core/async.fs
+++ b/src/FSharp.Core/async.fs
@@ -21,18 +21,21 @@ type LinkedSubSource(cancellationToken: CancellationToken) =
 
     let failureCTS = new CancellationTokenSource()
 
-    let linkedCTS = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, failureCTS.Token)
+    let linkedCTS =
+        CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, failureCTS.Token)
 
     member _.Token = linkedCTS.Token
 
-    member _.Cancel() = failureCTS.Cancel()
+    member _.Cancel() =
+        failureCTS.Cancel()
 
     member _.Dispose() =
         linkedCTS.Dispose()
         failureCTS.Dispose()
 
     interface IDisposable with
-        member this.Dispose() = this.Dispose()
+        member this.Dispose() =
+            this.Dispose()
 
 /// Global mutable state used to associate Exception
 []
@@ -45,7 +48,11 @@ module ExceptionDispatchInfoHelpers =
         member edi.GetAssociatedSourceException() =
             let exn = edi.SourceException
             // Try to store the entry in the association table to allow us to recover it later.
-            try associationTable.Add(exn, edi) with _ -> ()
+            try
+                associationTable.Add(exn, edi)
+            with _ ->
+                ()
+
             exn
 
         // Capture, but prefer the saved information if available
@@ -53,8 +60,7 @@ module ExceptionDispatchInfoHelpers =
         static member RestoreOrCapture exn =
             match associationTable.TryGetValue exn with
             | true, edi -> edi
-            | _ ->
-                ExceptionDispatchInfo.Capture exn
+            | _ -> ExceptionDispatchInfo.Capture exn
 
         member inline edi.ThrowAny() =
             edi.Throw()
@@ -66,8 +72,9 @@ module ExceptionDispatchInfoHelpers =
 []
 type AsyncReturn =
     | AsyncReturn
-    with
-        static member inline Fake() = Unchecked.defaultof
+
+    static member inline Fake() =
+        Unchecked.defaultof
 
 type cont<'T> = ('T -> AsyncReturn)
 type econt = (ExceptionDispatchInfo -> AsyncReturn)
@@ -82,8 +89,7 @@ type Trampoline() =
     []
     static val mutable private thisThreadHasTrampoline: bool
 
-    static member ThisThreadHasTrampoline =
-        Trampoline.thisThreadHasTrampoline
+    static member ThisThreadHasTrampoline = Trampoline.thisThreadHasTrampoline
 
     let mutable storedCont = None
     let mutable storedExnCont = None
@@ -92,26 +98,28 @@ type Trampoline() =
     /// Use this trampoline on the synchronous stack if none exists, and execute
     /// the given function. The function might write its continuation into the trampoline.
     []
-    member _.Execute (firstAction: unit -> AsyncReturn) =
+    member _.Execute(firstAction: unit -> AsyncReturn) =
 
         let thisThreadHadTrampoline = Trampoline.thisThreadHasTrampoline
         Trampoline.thisThreadHasTrampoline <- true
+
         try
             let mutable keepGoing = true
             let mutable action = firstAction
+
             while keepGoing do
                 try
-                    action() |> ignore
+                    action () |> ignore
+
                     match storedCont with
-                    | None ->
-                        keepGoing <- false
+                    | None -> keepGoing <- false
                     | Some cont ->
                         storedCont <- None
                         action <- cont
-                
+
                 // Catch exceptions at the trampoline to get a full .StackTrace entry
                 // This is because of this problem https://stackoverflow.com/questions/5301535/exception-call-stack-truncated-without-any-re-throwing
-                // where only a limited number of stack frames are included in the .StackTrace property 
+                // where only a limited number of stack frames are included in the .StackTrace property
                 // of a .NET exception when it is thrown, up to the first catch handler.
                 //
                 // So when running async code, there aren't any intermediate catch handlers (though there
@@ -127,7 +135,7 @@ type Trampoline() =
                         // direct uses of combinators (not using async {...}) may cause
                         // code to execute unprotected, e.g. async.While((fun () -> failwith ".."), ...) executes the first
                         // guardExpr unprotected.
-                        reraise()
+                        reraise ()
 
                     | Some econt ->
                         storedExnCont <- None
@@ -136,6 +144,7 @@ type Trampoline() =
 
         finally
             Trampoline.thisThreadHasTrampoline <- thisThreadHadTrampoline
+
         AsyncReturn.Fake()
 
     /// Increment the counter estimating the size of the synchronous stack and
@@ -152,7 +161,7 @@ type Trampoline() =
         AsyncReturn.Fake()
 
     /// Save the exception continuation during propagation of an exception, or prior to raising an exception
-    member _.OnExceptionRaised (action: econt) =
+    member _.OnExceptionRaised(action: econt) =
         assert storedExnCont.IsNone
         storedExnCont <- Some action
 
@@ -160,40 +169,47 @@ type TrampolineHolder() =
     let mutable trampoline = null
 
     // On-demand allocate this delegate and keep it in the trampoline holder.
-    let mutable sendOrPostCallbackWithTrampoline : SendOrPostCallback = null
-    let getSendOrPostCallbackWithTrampoline(this: TrampolineHolder) =
-        match sendOrPostCallbackWithTrampoline with 
+    let mutable sendOrPostCallbackWithTrampoline: SendOrPostCallback = null
+
+    let getSendOrPostCallbackWithTrampoline (this: TrampolineHolder) =
+        match sendOrPostCallbackWithTrampoline with
         | null ->
-            sendOrPostCallbackWithTrampoline <- 
-                SendOrPostCallback (fun o ->
-                let f = unbox AsyncReturn> o
-                // Reminder: the ignore below ignores an AsyncReturn.
-                this.ExecuteWithTrampoline f |> ignore)
+            sendOrPostCallbackWithTrampoline <-
+                SendOrPostCallback(fun o ->
+                    let f = unbox AsyncReturn> o
+                    // Reminder: the ignore below ignores an AsyncReturn.
+                    this.ExecuteWithTrampoline f |> ignore)
         | _ -> ()
+
         sendOrPostCallbackWithTrampoline
 
     // On-demand allocate this delegate and keep it in the trampoline holder.
-    let mutable waitCallbackForQueueWorkItemWithTrampoline : WaitCallback = null
-    let getWaitCallbackForQueueWorkItemWithTrampoline(this: TrampolineHolder) =
-        match waitCallbackForQueueWorkItemWithTrampoline with 
+    let mutable waitCallbackForQueueWorkItemWithTrampoline: WaitCallback = null
+
+    let getWaitCallbackForQueueWorkItemWithTrampoline (this: TrampolineHolder) =
+        match waitCallbackForQueueWorkItemWithTrampoline with
         | null ->
             waitCallbackForQueueWorkItemWithTrampoline <-
-                WaitCallback (fun o ->
+                WaitCallback(fun o ->
                     let f = unbox AsyncReturn> o
                     this.ExecuteWithTrampoline f |> ignore)
         | _ -> ()
+
         waitCallbackForQueueWorkItemWithTrampoline
 
     // On-demand allocate this delegate and keep it in the trampoline holder.
-    let mutable threadStartCallbackForStartThreadWithTrampoline : ParameterizedThreadStart = null
-    let getThreadStartCallbackForStartThreadWithTrampoline(this: TrampolineHolder) =
-        match threadStartCallbackForStartThreadWithTrampoline with 
+    let mutable threadStartCallbackForStartThreadWithTrampoline: ParameterizedThreadStart =
+        null
+
+    let getThreadStartCallbackForStartThreadWithTrampoline (this: TrampolineHolder) =
+        match threadStartCallbackForStartThreadWithTrampoline with
         | null ->
             threadStartCallbackForStartThreadWithTrampoline <-
-                ParameterizedThreadStart (fun o ->
+                ParameterizedThreadStart(fun o ->
                     let f = unbox AsyncReturn> o
                     this.ExecuteWithTrampoline f |> ignore)
         | _ -> ()
+
         threadStartCallbackForStartThreadWithTrampoline
 
     /// Execute an async computation after installing a trampoline on its synchronous stack.
@@ -202,13 +218,14 @@ type TrampolineHolder() =
         trampoline <- Trampoline()
         trampoline.Execute firstAction
 
-    member this.PostWithTrampoline (syncCtxt: SynchronizationContext)  (f: unit -> AsyncReturn) =
-        syncCtxt.Post (getSendOrPostCallbackWithTrampoline(this), state=(f |> box))
+    member this.PostWithTrampoline (syncCtxt: SynchronizationContext) (f: unit -> AsyncReturn) =
+        syncCtxt.Post(getSendOrPostCallbackWithTrampoline (this), state = (f |> box))
         AsyncReturn.Fake()
 
-    member this.QueueWorkItemWithTrampoline (f: unit -> AsyncReturn) =
-        if not (ThreadPool.QueueUserWorkItem(getWaitCallbackForQueueWorkItemWithTrampoline(this), f |> box)) then
+    member this.QueueWorkItemWithTrampoline(f: unit -> AsyncReturn) =
+        if not (ThreadPool.QueueUserWorkItem(getWaitCallbackForQueueWorkItemWithTrampoline (this), f |> box)) then
             failwith "failed to queue user work item"
+
         AsyncReturn.Fake()
 
     member this.PostOrQueueWithTrampoline (syncCtxt: SynchronizationContext) f =
@@ -217,8 +234,10 @@ type TrampolineHolder() =
         | _ -> this.PostWithTrampoline syncCtxt f
 
     // This should be the only call to Thread.Start in this library. We must always install a trampoline.
-    member this.StartThreadWithTrampoline (f: unit -> AsyncReturn) =
-        Thread(getThreadStartCallbackForStartThreadWithTrampoline(this), IsBackground=true).Start(f|>box)
+    member this.StartThreadWithTrampoline(f: unit -> AsyncReturn) =
+        Thread(getThreadStartCallbackForStartThreadWithTrampoline (this), IsBackground = true)
+            .Start(f |> box)
+
         AsyncReturn.Fake()
 
     /// Save the exception continuation during propagation of an exception, or prior to raising an exception
@@ -228,7 +247,7 @@ type TrampolineHolder() =
     /// Call a continuation, but first check if an async computation should trampoline on its synchronous stack.
     member inline _.HijackCheckThenCall (cont: 'T -> AsyncReturn) res =
         if trampoline.IncrementBindCount() then
-            trampoline.Set (fun () -> cont res)
+            trampoline.Set(fun () -> cont res)
         else
             // NOTE: this must be a tailcall
             cont res
@@ -237,27 +256,31 @@ type TrampolineHolder() =
 []
 []
 type AsyncActivationAux =
-    { /// The active cancellation token
-      token: CancellationToken
+    {
+        /// The active cancellation token
+        token: CancellationToken
 
-      /// The exception continuation
-      econt: econt
+        /// The exception continuation
+        econt: econt
 
-      /// The cancellation continuation
-      ccont: ccont
+        /// The cancellation continuation
+        ccont: ccont
 
-      /// Holds some commonly-allocated callbacks and a mutable location to use for a trampoline
-      trampolineHolder: TrampolineHolder }
+        /// Holds some commonly-allocated callbacks and a mutable location to use for a trampoline
+        trampolineHolder: TrampolineHolder
+    }
 
 /// Represents context for an in-flight async computation
 []
 []
 type AsyncActivationContents<'T> =
-    { /// The success continuation
-      cont: cont<'T>
+    {
+        /// The success continuation
+        cont: cont<'T>
 
-      /// The rarely changing components
-      aux: AsyncActivationAux }
+        /// The rarely changing components
+        aux: AsyncActivationAux
+    }
 
 /// A struct wrapper around AsyncActivationContents. Using a struct wrapper allows us to change representation of the
 /// contents at a later point, e.g. to change the contents to a .NET Task or some other representation.
@@ -265,19 +288,42 @@ type AsyncActivationContents<'T> =
 type AsyncActivation<'T>(contents: AsyncActivationContents<'T>) =
 
     /// Produce a new execution context for a composite async
-    member ctxt.WithCancellationContinuation ccont = AsyncActivation<'T> { contents with aux = { ctxt.aux with ccont = ccont } }
+    member ctxt.WithCancellationContinuation ccont =
+        AsyncActivation<'T>
+            { contents with
+                aux = { ctxt.aux with ccont = ccont }
+            }
 
     /// Produce a new execution context for a composite async
-    member ctxt.WithExceptionContinuation econt = AsyncActivation<'T> { contents with aux = { ctxt.aux with econt = econt } }
+    member ctxt.WithExceptionContinuation econt =
+        AsyncActivation<'T>
+            { contents with
+                aux = { ctxt.aux with econt = econt }
+            }
 
     /// Produce a new execution context for a composite async
-    member _.WithContinuation cont = AsyncActivation<'U> { cont = cont; aux = contents.aux }
+    member _.WithContinuation cont =
+        AsyncActivation<'U> { cont = cont; aux = contents.aux }
 
     /// Produce a new execution context for a composite async
-    member _.WithContinuations(cont, econt) = AsyncActivation<'U> { cont = cont; aux = { contents.aux with econt = econt } }
+    member _.WithContinuations(cont, econt) =
+        AsyncActivation<'U>
+            {
+                cont = cont
+                aux = { contents.aux with econt = econt }
+            }
 
     /// Produce a new execution context for a composite async
-    member ctxt.WithContinuations(cont, econt, ccont) = AsyncActivation<'T> { contents with cont = cont; aux = { ctxt.aux with econt = econt; ccont = ccont } }
+    member ctxt.WithContinuations(cont, econt, ccont) =
+        AsyncActivation<'T>
+            { contents with
+                cont = cont
+                aux =
+                    { ctxt.aux with
+                        econt = econt
+                        ccont = ccont
+                    }
+            }
 
     /// The extra information relevant to the execution of the async
     member _.aux = contents.aux
@@ -301,8 +347,8 @@ type AsyncActivation<'T>(contents: AsyncActivationContents<'T>) =
     member _.IsCancellationRequested = contents.aux.token.IsCancellationRequested
 
     /// Call the cancellation continuation of the active computation
-    member _.OnCancellation () =
-        contents.aux.ccont (OperationCanceledException (contents.aux.token))
+    member _.OnCancellation() =
+        contents.aux.ccont (OperationCanceledException(contents.aux.token))
 
     /// Check for trampoline hijacking.
     //
@@ -319,13 +365,14 @@ type AsyncActivation<'T>(contents: AsyncActivationContents<'T>) =
     // Note, this must make tailcalls, so may not be an instance member taking a byref argument.
     static member Success (ctxt: AsyncActivation<'T>) result =
         if ctxt.IsCancellationRequested then
-            ctxt.OnCancellation ()
+            ctxt.OnCancellation()
         else
             AsyncActivation<'T>.HijackCheckThenCall ctxt ctxt.cont result
 
     // For backwards API Compat
     []
-    member ctxt.OnSuccess (result: 'T) = AsyncActivation<'T>.Success ctxt result
+    member ctxt.OnSuccess(result: 'T) =
+        AsyncActivation<'T>.Success ctxt result
 
     /// Save the exception continuation during propagation of an exception, or prior to raising an exception
     member _.OnExceptionRaised() =
@@ -333,11 +380,21 @@ type AsyncActivation<'T>(contents: AsyncActivationContents<'T>) =
 
     /// Make an initial async activation.
     static member Create cancellationToken trampolineHolder cont econt ccont : AsyncActivation<'T> =
-        AsyncActivation { cont = cont; aux = { token = cancellationToken; econt = econt; ccont = ccont; trampolineHolder = trampolineHolder } }
+        AsyncActivation
+            {
+                cont = cont
+                aux =
+                    {
+                        token = cancellationToken
+                        econt = econt
+                        ccont = ccont
+                        trampolineHolder = trampolineHolder
+                    }
+            }
 
     /// Queue the success continuation of the asynchronous execution context as a work item in the thread pool
     /// after installing a trampoline
-    member ctxt.QueueContinuationWithTrampoline (result: 'T) =
+    member ctxt.QueueContinuationWithTrampoline(result: 'T) =
         let cont = ctxt.cont
         ctxt.aux.trampolineHolder.QueueWorkItemWithTrampoline(fun () -> cont result)
 
@@ -349,17 +406,17 @@ type AsyncActivation<'T>(contents: AsyncActivationContents<'T>) =
     []
     member ctxt.ProtectCode userCode =
         let mutable ok = false
+
         try
-            let res = userCode()
+            let res = userCode ()
             ok <- true
             res
         finally
-            if not ok then
-                ctxt.OnExceptionRaised()
+            if not ok then ctxt.OnExceptionRaised()
 
-    member ctxt.PostWithTrampoline (syncCtxt: SynchronizationContext)  (f: unit -> AsyncReturn) =
+    member ctxt.PostWithTrampoline (syncCtxt: SynchronizationContext) (f: unit -> AsyncReturn) =
         let holder = contents.aux.trampolineHolder
-        ctxt.ProtectCode (fun () -> holder.PostWithTrampoline syncCtxt f)
+        ctxt.ProtectCode(fun () -> holder.PostWithTrampoline syncCtxt f)
 
     /// Call the success continuation of the asynchronous execution context
     member ctxt.CallContinuation(result: 'T) =
@@ -368,7 +425,9 @@ type AsyncActivation<'T>(contents: AsyncActivationContents<'T>) =
 /// Represents an asynchronous computation
 []
 type Async<'T> =
-    { Invoke: (AsyncActivation<'T> -> AsyncReturn) }
+    {
+        Invoke: (AsyncActivation<'T> -> AsyncReturn)
+    }
 
 /// Mutable register to help ensure that code is only executed once
 []
@@ -376,18 +435,19 @@ type Latch() =
     let mutable i = 0
 
     /// Execute the latch
-    member _.Enter() = Interlocked.CompareExchange(&i, 1, 0) = 0
+    member _.Enter() =
+        Interlocked.CompareExchange(&i, 1, 0) = 0
 
 /// Represents the result of an asynchronous computation
 []
-type AsyncResult<'T>  =
+type AsyncResult<'T> =
     | Ok of 'T
     | Error of ExceptionDispatchInfo
     | Canceled of OperationCanceledException
 
     /// Get the result of an asynchronous computation
     []
-    member res.Commit () =
+    member res.Commit() =
         match res with
         | AsyncResult.Ok res -> res
         | AsyncResult.Error edi -> edi.ThrowAny()
@@ -396,9 +456,11 @@ type AsyncResult<'T>  =
 /// Primitives to execute asynchronous computations
 module AsyncPrimitives =
 
-    let inline fake () = Unchecked.defaultof
+    let inline fake () =
+        Unchecked.defaultof
 
-    let inline unfake (_: AsyncReturn)  = ()
+    let inline unfake (_: AsyncReturn) =
+        ()
 
     /// The mutable global CancellationTokenSource, see Async.DefaultCancellationToken
     let mutable defaultCancellationTokenSource = new CancellationTokenSource()
@@ -424,13 +486,12 @@ module AsyncPrimitives =
             result <- userCode arg
             ok <- true
         finally
-            if not ok then
-                ctxt.OnExceptionRaised()
+            if not ok then ctxt.OnExceptionRaised()
 
         if ok then
             AsyncActivation<'T>.HijackCheckThenCall ctxt ctxt.cont result
         else
-            fake()
+            fake ()
 
     /// Apply 'part2' to 'result1' and invoke the resulting computation.
     ///
@@ -447,13 +508,12 @@ module AsyncPrimitives =
             result <- part2 result1
             ok <- true
         finally
-            if not ok then
-                ctxt.OnExceptionRaised()
+            if not ok then ctxt.OnExceptionRaised()
 
         if ok then
             Invoke result ctxt
         else
-            fake()
+            fake ()
 
     /// Like `CallThenInvoke` but does not do a hijack check for historical reasons (exact code compat)
     []
@@ -465,13 +525,9 @@ module AsyncPrimitives =
             res <- userCode result1
             ok <- true
         finally
-            if not ok then
-                ctxt.OnExceptionRaised()
+            if not ok then ctxt.OnExceptionRaised()
 
-        if ok then
-            res.Invoke ctxt
-        else
-            fake()
+        if ok then res.Invoke ctxt else fake ()
 
     /// Apply 'filterFunction' to 'arg'. If the result is 'Some' invoke the resulting computation. If the result is 'None'
     /// then send 'result1' to the exception continuation.
@@ -487,32 +543,30 @@ module AsyncPrimitives =
             resOpt <- filterFunction (edi.GetAssociatedSourceException())
             ok <- true
         finally
-            if not ok then
-                ctxt.OnExceptionRaised()
+            if not ok then ctxt.OnExceptionRaised()
 
         if ok then
             match resOpt with
-            | None ->
-                AsyncActivation<'T>.HijackCheckThenCall ctxt ctxt.econt edi
-            | Some res ->
-                Invoke res ctxt
+            | None -> AsyncActivation<'T>.HijackCheckThenCall ctxt ctxt.econt edi
+            | Some res -> Invoke res ctxt
         else
-            fake()
+            fake ()
 
     /// Build a primitive without any exception or resync protection
     []
-    let MakeAsync body = { Invoke = body }
+    let MakeAsync body =
+        { Invoke = body }
 
     []
     let MakeAsyncWithCancelCheck body =
-        MakeAsync (fun ctxt ->
+        MakeAsync(fun ctxt ->
             if ctxt.IsCancellationRequested then
-                ctxt.OnCancellation ()
+                ctxt.OnCancellation()
             else
                 body ctxt)
 
     /// Execute part1, then apply part2, then execute the result of that
-    /// 
+    ///
     /// Note: direct calls to this function end up in user assemblies via inlining
     ///   - Initial cancellation check
     ///   - Initial hijack check (see Invoke)
@@ -522,7 +576,7 @@ module AsyncPrimitives =
     []
     let Bind (ctxt: AsyncActivation<'T>) (part1: Async<'U>) (part2: 'U -> Async<'T>) : AsyncReturn =
         if ctxt.IsCancellationRequested then
-            ctxt.OnCancellation ()
+            ctxt.OnCancellation()
         else
             // Note, no cancellation check is done before calling 'part2'.  This is
             // because part1 may bind a resource, while part2 is a try/finally, and, if
@@ -552,11 +606,15 @@ module AsyncPrimitives =
         // The cancellation continuation runs the finallyFunction and then runs the previous cancellation continuation.
         // If an exception is thrown we continue with the previous cancellation continuation (the exception is lost)
         let ccont cexn =
-            CallThenContinue finallyFunction () (ctxt.WithContinuations(cont=(fun () -> ctxt.ccont cexn), econt = (fun _ -> ctxt.ccont cexn)))
+            CallThenContinue
+                finallyFunction
+                ()
+                (ctxt.WithContinuations(cont = (fun () -> ctxt.ccont cexn), econt = (fun _ -> ctxt.ccont cexn)))
+
+        let ctxt = ctxt.WithContinuations(cont = cont, econt = econt, ccont = ccont)
 
-        let ctxt = ctxt.WithContinuations(cont=cont, econt=econt, ccont=ccont)
         if ctxt.IsCancellationRequested then
-            ctxt.OnCancellation ()
+            ctxt.OnCancellation()
         else
             computation.Invoke ctxt
 
@@ -570,12 +628,12 @@ module AsyncPrimitives =
     []
     let TryWith (ctxt: AsyncActivation<'T>) (computation: Async<'T>) catchFunction =
         if ctxt.IsCancellationRequested then
-            ctxt.OnCancellation ()
+            ctxt.OnCancellation()
         else
             let ctxt =
                 ctxt.WithExceptionContinuation(fun edi ->
                     if ctxt.IsCancellationRequested then
-                        ctxt.OnCancellation ()
+                        ctxt.OnCancellation()
                     else
                         CallFilterThenInvoke ctxt catchFunction edi)
 
@@ -585,7 +643,7 @@ module AsyncPrimitives =
     //   - No cancellation check
     //   - No hijack check
     let CreateAsyncResultAsync res =
-        MakeAsync (fun ctxt ->
+        MakeAsync(fun ctxt ->
             match res with
             | AsyncResult.Ok r -> ctxt.cont r
             | AsyncResult.Error edi -> ctxt.econt edi
@@ -596,7 +654,7 @@ module AsyncPrimitives =
     ///   - Hijack check (see OnSuccess)
     let inline CreateReturnAsync res =
         // Note: this code ends up in user assemblies via inlining
-        MakeAsync (fun ctxt -> AsyncActivation.Success ctxt res)
+        MakeAsync(fun ctxt -> AsyncActivation.Success ctxt res)
 
     /// Runs the first process, takes its result, applies f and then runs the new process produced.
     ///   - Initial cancellation check (see Bind)
@@ -604,18 +662,16 @@ module AsyncPrimitives =
     ///   - No hijack check after applying 'part2' to argument (see Bind)
     ///   - No cancellation check after applying 'part2' to argument (see Bind)
     ///   - Apply 'part2' to argument with exception protection (see Bind)
-    let inline CreateBindAsync part1 part2  =
+    let inline CreateBindAsync part1 part2 =
         // Note: this code ends up in user assemblies via inlining
-        MakeAsync (fun ctxt ->
-            Bind ctxt part1 part2)
+        MakeAsync(fun ctxt -> Bind ctxt part1 part2)
 
     /// Call the given function with exception protection.
     ///   - No initial cancellation check
     ///   - Hijack check after applying part2 to argument (see CallThenInvoke)
     let inline CreateCallAsync part2 result1 =
         // Note: this code ends up in user assemblies via inlining
-        MakeAsync (fun ctxt ->
-            CallThenInvoke ctxt result1 part2)
+        MakeAsync(fun ctxt -> CallThenInvoke ctxt result1 part2)
 
     /// Call the given function with exception protection.
     ///   - Initial cancellation check
@@ -623,8 +679,7 @@ module AsyncPrimitives =
     ///   - Apply 'computation' to argument with exception protection (see CallThenInvoke)
     let inline CreateDelayAsync computation =
         // Note: this code ends up in user assemblies via inlining
-        MakeAsyncWithCancelCheck (fun ctxt ->
-            CallThenInvoke ctxt () computation)
+        MakeAsyncWithCancelCheck(fun ctxt -> CallThenInvoke ctxt () computation)
 
     /// Implements the sequencing construct of async computation expressions
     ///   - Initial cancellation check (see CreateBindAsync)
@@ -641,7 +696,7 @@ module AsyncPrimitives =
     ///   - Hijack check after 'entering' the try/finally and before running the body (see TryFinally)
     ///   - Apply 'finallyFunction' with exception protection (see TryFinally)
     let inline CreateTryFinallyAsync finallyFunction computation =
-        MakeAsync (fun ctxt -> TryFinally ctxt computation finallyFunction)
+        MakeAsync(fun ctxt -> TryFinally ctxt computation finallyFunction)
 
     /// Create an async for a try/with filtering exceptions through a pattern match
     ///   - Cancellation check before entering the try (see TryWith)
@@ -649,7 +704,7 @@ module AsyncPrimitives =
     ///   - Apply `filterFunction' to argument with exception protection (see TryWith)
     ///   - Hijack check before invoking the resulting computation or exception continuation
     let inline CreateTryWithFilterAsync filterFunction computation =
-        MakeAsync (fun ctxt -> TryWith ctxt computation filterFunction)
+        MakeAsync(fun ctxt -> TryWith ctxt computation filterFunction)
 
     /// Create an async for a try/with filtering
     ///   - Cancellation check before entering the try (see TryWith)
@@ -657,7 +712,7 @@ module AsyncPrimitives =
     ///   - Apply `catchFunction' to argument with exception protection (see TryWith)
     ///   - Hijack check before invoking the resulting computation or exception continuation
     let inline CreateTryWithAsync catchFunction computation =
-        MakeAsync (fun ctxt -> TryWith ctxt computation (fun exn -> Some (catchFunction exn)))
+        MakeAsync(fun ctxt -> TryWith ctxt computation (fun exn -> Some(catchFunction exn)))
 
     /// Call the finallyFunction if the computation results in a cancellation, and then continue with cancellation.
     /// If the finally function gives an exception then continue with cancellation regardless.
@@ -666,22 +721,25 @@ module AsyncPrimitives =
     ///   - Apply `finallyFunction' to argument with exception protection (see CallThenContinue)
     ///   - Hijack check before continuing with cancellation (see CallThenContinue)
     let CreateWhenCancelledAsync (finallyFunction: OperationCanceledException -> unit) computation =
-        MakeAsync (fun ctxt ->
+        MakeAsync(fun ctxt ->
             let ccont = ctxt.ccont
+
             let ctxt =
                 ctxt.WithCancellationContinuation(fun cexn ->
-                    CallThenContinue finallyFunction cexn (ctxt.WithContinuations(cont = (fun _ -> ccont cexn), econt = (fun _ -> ccont cexn))))
+                    CallThenContinue
+                        finallyFunction
+                        cexn
+                        (ctxt.WithContinuations(cont = (fun _ -> ccont cexn), econt = (fun _ -> ccont cexn))))
+
             computation.Invoke ctxt)
 
     /// A single pre-allocated computation that fetched the current cancellation token
-    let cancellationTokenAsync  =
-        MakeAsync (fun ctxt -> ctxt.cont ctxt.aux.token)
+    let cancellationTokenAsync = MakeAsync(fun ctxt -> ctxt.cont ctxt.aux.token)
 
     /// A single pre-allocated computation that returns a unit result
     ///   - Cancellation check (see CreateReturnAsync)
     ///   - Hijack check (see CreateReturnAsync)
-    let unitAsync =
-        CreateReturnAsync()
+    let unitAsync = CreateReturnAsync()
 
     /// Implement use/Dispose
     ///
@@ -690,8 +748,10 @@ module AsyncPrimitives =
     ///   - Cancellation check after 'entering' the implied try/finally and before running the body  (see CreateTryFinallyAsync)
     ///   - Hijack check after 'entering' the implied try/finally and before running the body  (see CreateTryFinallyAsync)
     ///   - Run 'disposeFunction' with exception protection (see CreateTryFinallyAsync)
-    let CreateUsingAsync (resource:'T :> IDisposable) (computation:'T -> Async<'a>) : Async<'a> =
-        let disposeFunction () = Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.Dispose resource
+    let CreateUsingAsync (resource: 'T :> IDisposable) (computation: 'T -> Async<'a>) : Async<'a> =
+        let disposeFunction () =
+            Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.Dispose resource
+
         CreateTryFinallyAsync disposeFunction (CreateCallAsync computation resource)
 
     ///   - Initial cancellation check (see CreateBindAsync)
@@ -714,40 +774,47 @@ module AsyncPrimitives =
     //
     // Note: There are allocations during loop set up, but no allocations during iterations of the loop
     let CreateWhileAsync guardFunc computation =
-        if guardFunc() then
+        if guardFunc () then
             let mutable whileAsync = Unchecked.defaultof<_>
-            whileAsync <- CreateBindAsync computation (fun () -> if guardFunc() then whileAsync else unitAsync)
+
+            whileAsync <-
+                CreateBindAsync computation (fun () ->
+                    if guardFunc () then
+                        whileAsync
+                    else
+                        unitAsync)
+
             whileAsync
         else
             unitAsync
 
 #if REDUCED_ALLOCATIONS_BUT_RUNS_SLOWER
-    /// Implement the while loop construct of async computation expressions
-    ///   - Initial cancellation check before each execution of guard
-    ///   - No initial hijack check before each execution of guard
-    ///   - No cancellation check before each execution of the body after guard
-    ///   - Hijack check before each execution of the body after guard (see Invoke)
-    ///   - Cancellation check after guard fails (see OnSuccess)
-    ///   - Hijack check after guard fails (see OnSuccess)
-    ///   - Apply 'guardFunc' with exception protection (see ProtectCode)
-    //
-    // Note: There are allocations during loop set up, but no allocations during iterations of the loop
+        /// Implement the while loop construct of async computation expressions
+        ///   - Initial cancellation check before each execution of guard
+        ///   - No initial hijack check before each execution of guard
+        ///   - No cancellation check before each execution of the body after guard
+        ///   - Hijack check before each execution of the body after guard (see Invoke)
+        ///   - Cancellation check after guard fails (see OnSuccess)
+        ///   - Hijack check after guard fails (see OnSuccess)
+        ///   - Apply 'guardFunc' with exception protection (see ProtectCode)
+        //
+        // Note: There are allocations during loop set up, but no allocations during iterations of the loop
         // One allocation for While async
         // One allocation for While async context function
-        MakeAsync (fun ctxtGuard -> 
-           // One allocation for ctxtLoop reference cell
-           let mutable ctxtLoop = Unchecked.defaultof<_>
-           // One allocation for While recursive closure
-           let rec WhileLoop () =
-               if ctxtGuard.IsCancellationRequested then
-                   ctxtGuard.OnCancellation ()
-               elif ctxtGuard.ProtectCode guardFunc then
-                   Invoke computation ctxtLoop
-               else
-                   ctxtGuard.OnSuccess ()
-           // One allocation for While body activation context
-           ctxtLoop <- ctxtGuard.WithContinuation(WhileLoop)
-           WhileLoop ()) 
+        MakeAsync(fun ctxtGuard ->
+            // One allocation for ctxtLoop reference cell
+            let mutable ctxtLoop = Unchecked.defaultof<_>
+            // One allocation for While recursive closure
+            let rec WhileLoop () =
+                if ctxtGuard.IsCancellationRequested then
+                    ctxtGuard.OnCancellation()
+                elif ctxtGuard.ProtectCode guardFunc then
+                    Invoke computation ctxtLoop
+                else
+                    ctxtGuard.OnSuccess()
+            // One allocation for While body activation context
+            ctxtLoop <- ctxtGuard.WithContinuation(WhileLoop)
+            WhileLoop())
 #endif
 
     /// Implement the for loop construct of async commputation expressions
@@ -765,53 +832,51 @@ module AsyncPrimitives =
     // applying the loop body to the element
     let CreateForLoopAsync (source: seq<_>) computation =
         CreateUsingAsync (source.GetEnumerator()) (fun ie ->
-            CreateWhileAsync
-                (fun () -> ie.MoveNext())
-                (CreateDelayAsync (fun () -> computation ie.Current)))
+            CreateWhileAsync (fun () -> ie.MoveNext()) (CreateDelayAsync(fun () -> computation ie.Current)))
 
 #if REDUCED_ALLOCATIONS_BUT_RUNS_SLOWER
         CreateUsingAsync (source.GetEnumerator()) (fun ie ->
             // One allocation for While async
             // One allocation for While async context function
-            MakeAsync (fun ctxtGuard -> 
-               // One allocation for ctxtLoop reference cell
-               let mutable ctxtLoop = Unchecked.defaultof<_>
-               // Two allocations for protected functions
-               let guardFunc() = ie.MoveNext()
-               let currentFunc() = ie.Current
-               // One allocation for ForLoop recursive closure
-               let rec ForLoop () =
-                   if ctxtGuard.IsCancellationRequested then
-                       ctxtGuard.OnCancellation ()
-                   elif ctxtGuard.ProtectCode guardFunc then
-                       let x = ctxtGuard.ProtectCode currentFunc
-                       CallThenInvoke ctxtLoop x computation 
-                   else
-                       ctxtGuard.OnSuccess ()
-               // One allocation for loop activation context
-               ctxtLoop <- ctxtGuard.WithContinuation(ForLoop)
-               ForLoop ())) 
+            MakeAsync(fun ctxtGuard ->
+                // One allocation for ctxtLoop reference cell
+                let mutable ctxtLoop = Unchecked.defaultof<_>
+                // Two allocations for protected functions
+                let guardFunc () =
+                    ie.MoveNext()
+
+                let currentFunc () =
+                    ie.Current
+                // One allocation for ForLoop recursive closure
+                let rec ForLoop () =
+                    if ctxtGuard.IsCancellationRequested then
+                        ctxtGuard.OnCancellation()
+                    elif ctxtGuard.ProtectCode guardFunc then
+                        let x = ctxtGuard.ProtectCode currentFunc
+                        CallThenInvoke ctxtLoop x computation
+                    else
+                        ctxtGuard.OnSuccess()
+                // One allocation for loop activation context
+                ctxtLoop <- ctxtGuard.WithContinuation(ForLoop)
+                ForLoop()))
 #endif
 
     ///   - Initial cancellation check
     ///   - Call syncCtxt.Post with exception protection. THis may fail as it is arbitrary user code
     let CreateSwitchToAsync (syncCtxt: SynchronizationContext) =
-        MakeAsyncWithCancelCheck (fun ctxt ->
-            ctxt.PostWithTrampoline syncCtxt ctxt.cont)
+        MakeAsyncWithCancelCheck(fun ctxt -> ctxt.PostWithTrampoline syncCtxt ctxt.cont)
 
     ///   - Initial cancellation check
     ///   - Create Thread and call Start() with exception protection. We don't expect this
     ///     to fail but protect nevertheless.
-    let CreateSwitchToNewThreadAsync() =
-        MakeAsyncWithCancelCheck (fun ctxt ->
-            ctxt.ProtectCode (fun () -> ctxt.trampolineHolder.StartThreadWithTrampoline ctxt.cont))
+    let CreateSwitchToNewThreadAsync () =
+        MakeAsyncWithCancelCheck(fun ctxt -> ctxt.ProtectCode(fun () -> ctxt.trampolineHolder.StartThreadWithTrampoline ctxt.cont))
 
     ///   - Initial cancellation check
     ///   - Call ThreadPool.QueueUserWorkItem with exception protection. We don't expect this
     ///     to fail but protect nevertheless.
-    let CreateSwitchToThreadPoolAsync() =
-        MakeAsyncWithCancelCheck (fun ctxt ->
-            ctxt.ProtectCode (fun () -> ctxt.trampolineHolder.QueueWorkItemWithTrampoline ctxt.cont))
+    let CreateSwitchToThreadPoolAsync () =
+        MakeAsyncWithCancelCheck(fun ctxt -> ctxt.ProtectCode(fun () -> ctxt.trampolineHolder.QueueWorkItemWithTrampoline ctxt.cont))
 
     /// Post back to the sync context regardless of which continuation is taken
     ///   - Call syncCtxt.Post with exception protection
@@ -819,9 +884,11 @@ module AsyncPrimitives =
         match SynchronizationContext.Current with
         | null -> ctxt
         | syncCtxt ->
-            ctxt.WithContinuations(cont = (fun x -> ctxt.PostWithTrampoline syncCtxt (fun () -> ctxt.cont x)),
-                                   econt = (fun edi -> ctxt.PostWithTrampoline syncCtxt (fun () -> ctxt.econt edi)),
-                                   ccont = (fun cexn -> ctxt.PostWithTrampoline syncCtxt (fun () -> ctxt.ccont cexn)))
+            ctxt.WithContinuations(
+                cont = (fun x -> ctxt.PostWithTrampoline syncCtxt (fun () -> ctxt.cont x)),
+                econt = (fun edi -> ctxt.PostWithTrampoline syncCtxt (fun () -> ctxt.econt edi)),
+                ccont = (fun cexn -> ctxt.PostWithTrampoline syncCtxt (fun () -> ctxt.ccont cexn))
+            )
 
     []
     []
@@ -837,19 +904,21 @@ module AsyncPrimitives =
         let trampolineHolder = ctxt.trampolineHolder
 
         member _.ContinueImmediate res =
-            let action () = ctxt.cont res
-            let inline executeImmediately () = trampolineHolder.ExecuteWithTrampoline action
+            let action () =
+                ctxt.cont res
+
+            let inline executeImmediately () =
+                trampolineHolder.ExecuteWithTrampoline action
+
             let currentSyncCtxt = SynchronizationContext.Current
+
             match syncCtxt, currentSyncCtxt with
-            | null, null ->
-                executeImmediately ()
+            | null, null -> executeImmediately ()
             // This logic was added in F# 2.0 though is incorrect from the perspective of
             // how SynchronizationContext is meant to work. However the logic works for
             // mainline scenarios (WinForms/WPF) and for compatibility reasons we won't change it.
-            | _ when Object.Equals(syncCtxt, currentSyncCtxt) && thread.Equals Thread.CurrentThread ->
-                executeImmediately ()
-            | _ ->
-                trampolineHolder.PostOrQueueWithTrampoline syncCtxt action
+            | _ when Object.Equals(syncCtxt, currentSyncCtxt) && thread.Equals Thread.CurrentThread -> executeImmediately ()
+            | _ -> trampolineHolder.PostOrQueueWithTrampoline syncCtxt action
 
         member _.PostOrQueueWithTrampoline res =
             trampolineHolder.PostOrQueueWithTrampoline syncCtxt (fun () -> ctxt.cont res)
@@ -873,25 +942,26 @@ module AsyncPrimitives =
         let mutable disposed = false
 
         // All writers of result are protected by lock on syncRoot.
-        let syncRoot = obj()
+        let syncRoot = obj ()
 
         member x.GetWaitHandle() =
             lock syncRoot (fun () ->
                 if disposed then
                     raise (System.ObjectDisposedException("ResultCell"))
+
                 match resEvent with
                 | null ->
                     // Start in signalled state if a result is already present.
                     let ev = new ManualResetEvent(result.IsSome)
                     resEvent <- ev
                     (ev :> WaitHandle)
-                | ev ->
-                    (ev :> WaitHandle))
+                | ev -> (ev :> WaitHandle))
 
         member x.Close() =
             lock syncRoot (fun () ->
                 if not disposed then
                     disposed <- true
+
                     match resEvent with
                     | null -> ()
                     | ev ->
@@ -899,7 +969,8 @@ module AsyncPrimitives =
                         resEvent <- null)
 
         interface IDisposable with
-            member x.Dispose() = x.Close()
+            member x.Dispose() =
+                x.Close()
 
         member x.GrabResult() =
             match result with
@@ -907,47 +978,49 @@ module AsyncPrimitives =
             | None -> failwith "Unexpected no result"
 
         /// Record the result in the ResultCell.
-        member x.RegisterResult (res:'T, reuseThread) =
+        member x.RegisterResult(res: 'T, reuseThread) =
             let grabbedConts =
                 lock syncRoot (fun () ->
                     // Ignore multiple sets of the result. This can happen, e.g. for a race between a cancellation and a success
                     if x.ResultAvailable then
                         [] // invalidOp "multiple results registered for asynchronous operation"
                     else
-                        // In this case the ResultCell has already been disposed, e.g. due to a timeout.
-                        // The result is dropped on the floor.
-                        if disposed then
-                            []
-                        else
-                            result <- Some res
-                            // If the resEvent exists then set it. If not we can skip setting it altogether and it won't be
-                            // created
-                            match resEvent with
-                            | null ->
-                                ()
-                            | ev ->
-                                // Setting the event need to happen under lock so as not to race with Close()
-                                ev.Set () |> ignore
-                            List.rev savedConts)
+                    // In this case the ResultCell has already been disposed, e.g. due to a timeout.
+                    // The result is dropped on the floor.
+                    if disposed then
+                        []
+                    else
+                        result <- Some res
+                        // If the resEvent exists then set it. If not we can skip setting it altogether and it won't be
+                        // created
+                        match resEvent with
+                        | null -> ()
+                        | ev ->
+                            // Setting the event need to happen under lock so as not to race with Close()
+                            ev.Set() |> ignore
+
+                        List.rev savedConts)
 
             // Run the action outside the lock
             match grabbedConts with
-            | [] -> fake()
-            | [cont] ->
+            | [] -> fake ()
+            | [ cont ] ->
                 if reuseThread then
                     cont.ContinueImmediate res
                 else
                     cont.PostOrQueueWithTrampoline res
             | otherwise ->
-                otherwise |> List.iter (fun cont -> cont.PostOrQueueWithTrampoline res |> unfake) |> fake
+                otherwise
+                |> List.iter (fun cont -> cont.PostOrQueueWithTrampoline res |> unfake)
+                |> fake
 
         member x.ResultAvailable = result.IsSome
 
         /// Await the result of a result cell, without a direct timeout or direct
         /// cancellation. That is, the underlying computation must fill the result
         /// if cancellation or timeout occurs.
-        member x.AwaitResult_NoDirectCancelOrTimeout  =
-            MakeAsync (fun ctxt ->
+        member x.AwaitResult_NoDirectCancelOrTimeout =
+            MakeAsync(fun ctxt ->
                 // Check if a result is available synchronously
                 let resOpt =
                     match result with
@@ -955,35 +1028,32 @@ module AsyncPrimitives =
                     | None ->
                         lock syncRoot (fun () ->
                             match result with
-                            | Some _ ->
-                                result
+                            | Some _ -> result
                             | None ->
                                 // Otherwise save the continuation and call it in RegisterResult
                                 savedConts <- (SuspendedAsync<_>(ctxt)) :: savedConts
-                                None
-                        )
+                                None)
+
                 match resOpt with
                 | Some res -> ctxt.cont res
-                | None -> fake()
-            )
+                | None -> fake ())
 
-        member x.TryWaitForResultSynchronously (?timeout) : 'T option =
+        member x.TryWaitForResultSynchronously(?timeout) : 'T option =
             // Check if a result is available.
             match result with
-            | Some _ as r ->
-                r
+            | Some _ as r -> r
             | None ->
                 // Force the creation of the WaitHandle
                 let resHandle = x.GetWaitHandle()
                 // Check again. While we were in GetWaitHandle, a call to RegisterResult may have set result then skipped the
                 // Set because the resHandle wasn't forced.
                 match result with
-                | Some _ as r ->
-                    r
+                | Some _ as r -> r
                 | None ->
                     // OK, let's really wait for the Set signal. This may block.
                     let timeout = defaultArg timeout Threading.Timeout.Infinite
-                    let ok = resHandle.WaitOne(millisecondsTimeout= timeout, exitContext=true)
+                    let ok = resHandle.WaitOne(millisecondsTimeout = timeout, exitContext = true)
+
                     if ok then
                         // Now the result really must be available
                         result
@@ -991,25 +1061,34 @@ module AsyncPrimitives =
                         // timed out
                         None
 
-
     /// Create an instance of an arbitrary delegate type delegating to the given F# function
     type FuncDelegate<'T>(f) =
-        member _.Invoke(sender:obj, a:'T) : unit = ignore sender; f a
+        member _.Invoke(sender: obj, a: 'T) : unit =
+            ignore sender
+            f a
+
         static member Create<'Delegate when 'Delegate :> Delegate>(f) =
             let obj = FuncDelegate<'T>(f)
-            let invokeMeth = (typeof>).GetMethod("Invoke", BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance)
+
+            let invokeMeth =
+                (typeof>)
+                    .GetMethod("Invoke", BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance)
+
             System.Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeMeth) :?> 'Delegate
 
     []
     let QueueAsync cancellationToken cont econt ccont computation =
         let trampolineHolder = TrampolineHolder()
-        trampolineHolder.QueueWorkItemWithTrampoline (fun () ->
-            let ctxt = AsyncActivation.Create cancellationToken trampolineHolder cont econt ccont
+
+        trampolineHolder.QueueWorkItemWithTrampoline(fun () ->
+            let ctxt =
+                AsyncActivation.Create cancellationToken trampolineHolder cont econt ccont
+
             computation.Invoke ctxt)
 
     /// Run the asynchronous workflow and wait for its result.
     []
-    let QueueAsyncAndWaitForResultSynchronously (token:CancellationToken) computation timeout =
+    let QueueAsyncAndWaitForResultSynchronously (token: CancellationToken) computation timeout =
         let token, innerCTS =
             // If timeout is provided, we govern the async by our own CTS, to cancel
             // when execution times out. Otherwise, the user-supplied token governs the async.
@@ -1020,43 +1099,50 @@ module AsyncPrimitives =
                 subSource.Token, Some subSource
 
         use resultCell = new ResultCell>()
+
         QueueAsync
             token
-            (fun res -> resultCell.RegisterResult(AsyncResult.Ok res, reuseThread=true))
-            (fun edi -> resultCell.RegisterResult(AsyncResult.Error edi, reuseThread=true))
-            (fun exn -> resultCell.RegisterResult(AsyncResult.Canceled exn, reuseThread=true))
+            (fun res -> resultCell.RegisterResult(AsyncResult.Ok res, reuseThread = true))
+            (fun edi -> resultCell.RegisterResult(AsyncResult.Error edi, reuseThread = true))
+            (fun exn -> resultCell.RegisterResult(AsyncResult.Canceled exn, reuseThread = true))
             computation
         |> unfake
 
         let res = resultCell.TryWaitForResultSynchronously(?timeout = timeout)
+
         match res with
         | None -> // timed out
             // issue cancellation signal
-            if innerCTS.IsSome then innerCTS.Value.Cancel()
+            if innerCTS.IsSome then
+                innerCTS.Value.Cancel()
             // wait for computation to quiesce; drop result on the floor
             resultCell.TryWaitForResultSynchronously() |> ignore
             // dispose the CancellationTokenSource
-            if innerCTS.IsSome then innerCTS.Value.Dispose()
+            if innerCTS.IsSome then
+                innerCTS.Value.Dispose()
+
             raise (System.TimeoutException())
         | Some res ->
             match innerCTS with
             | Some subSource -> subSource.Dispose()
             | None -> ()
+
             res.Commit()
 
     []
-    let RunImmediate (cancellationToken:CancellationToken) computation =
+    let RunImmediate (cancellationToken: CancellationToken) computation =
         use resultCell = new ResultCell>()
         let trampolineHolder = TrampolineHolder()
 
-        trampolineHolder.ExecuteWithTrampoline (fun () ->
+        trampolineHolder.ExecuteWithTrampoline(fun () ->
             let ctxt =
                 AsyncActivation.Create
                     cancellationToken
                     trampolineHolder
-                    (fun res -> resultCell.RegisterResult(AsyncResult.Ok res, reuseThread=true))
-                    (fun edi -> resultCell.RegisterResult(AsyncResult.Error edi, reuseThread=true))
-                    (fun exn -> resultCell.RegisterResult(AsyncResult.Canceled exn, reuseThread=true))
+                    (fun res -> resultCell.RegisterResult(AsyncResult.Ok res, reuseThread = true))
+                    (fun edi -> resultCell.RegisterResult(AsyncResult.Error edi, reuseThread = true))
+                    (fun exn -> resultCell.RegisterResult(AsyncResult.Canceled exn, reuseThread = true))
+
             computation.Invoke ctxt)
         |> unfake
 
@@ -1071,25 +1157,28 @@ module AsyncPrimitives =
         | _ -> QueueAsyncAndWaitForResultSynchronously cancellationToken computation timeout
 
     []
-    let Start cancellationToken (computation:Async) =
+    let Start cancellationToken (computation: Async) =
         QueueAsync
             cancellationToken
-            (fun () -> fake())   // nothing to do on success
-            (fun edi -> edi.ThrowAny())   // raise exception in child
-            (fun _ -> fake())    // ignore cancellation in child
+            (fun () -> fake ()) // nothing to do on success
+            (fun edi -> edi.ThrowAny()) // raise exception in child
+            (fun _ -> fake ()) // ignore cancellation in child
             computation
         |> unfake
 
     []
-    let StartWithContinuations cancellationToken (computation:Async<'T>) cont econt ccont =
+    let StartWithContinuations cancellationToken (computation: Async<'T>) cont econt ccont =
         let trampolineHolder = TrampolineHolder()
-        trampolineHolder.ExecuteWithTrampoline (fun () ->
-            let ctxt = AsyncActivation.Create cancellationToken trampolineHolder (cont >> fake) (econt >> fake) (ccont >> fake)
+
+        trampolineHolder.ExecuteWithTrampoline(fun () ->
+            let ctxt =
+                AsyncActivation.Create cancellationToken trampolineHolder (cont >> fake) (econt >> fake) (ccont >> fake)
+
             computation.Invoke ctxt)
         |> unfake
 
     []
-    let StartAsTask cancellationToken (computation:Async<'T>) taskCreationOptions =
+    let StartAsTask cancellationToken (computation: Async<'T>) taskCreationOptions =
         let taskCreationOptions = defaultArg taskCreationOptions TaskCreationOptions.None
         let tcs = TaskCompletionSource<_>(taskCreationOptions)
 
@@ -1097,6 +1186,7 @@ module AsyncPrimitives =
         //      a) cancellation signal should always propagate to the computation
         //      b) when the task IsCompleted -> nothing is running anymore
         let task = tcs.Task
+
         QueueAsync
             cancellationToken
             (fun r -> tcs.SetResult r |> fake)
@@ -1104,12 +1194,14 @@ module AsyncPrimitives =
             (fun _ -> tcs.SetCanceled() |> fake)
             computation
         |> unfake
+
         task
 
     // Call the appropriate continuation on completion of a task
     []
-    let OnTaskCompleted (completedTask: Task<'T>) (ctxt: AsyncActivation<'T>)  =
+    let OnTaskCompleted (completedTask: Task<'T>) (ctxt: AsyncActivation<'T>) =
         assert completedTask.IsCompleted
+
         if completedTask.IsCanceled then
             let edi = ExceptionDispatchInfo.Capture(TaskCanceledException completedTask)
             ctxt.econt edi
@@ -1124,8 +1216,9 @@ module AsyncPrimitives =
     // the overall async (they may be governed by different cancellation tokens, or
     // the task may not have a cancellation token at all).
     []
-    let OnUnitTaskCompleted (completedTask: Task) (ctxt: AsyncActivation)  =
+    let OnUnitTaskCompleted (completedTask: Task) (ctxt: AsyncActivation) =
         assert completedTask.IsCompleted
+
         if completedTask.IsCanceled then
             let edi = ExceptionDispatchInfo.Capture(TaskCanceledException(completedTask))
             ctxt.econt edi
@@ -1140,11 +1233,13 @@ module AsyncPrimitives =
     // completing the task. This will install a new trampoline on that thread and continue the
     // execution of the async there.
     []
-    let AttachContinuationToTask (task: Task<'T>) (ctxt: AsyncActivation<'T>)  =
-        task.ContinueWith(Action>(fun completedTask -> 
-            ctxt.trampolineHolder.ExecuteWithTrampoline (fun () ->
-                OnTaskCompleted completedTask ctxt)
-            |> unfake), TaskContinuationOptions.ExecuteSynchronously)
+    let AttachContinuationToTask (task: Task<'T>) (ctxt: AsyncActivation<'T>) =
+        task.ContinueWith(
+            Action>(fun completedTask ->
+                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> OnTaskCompleted completedTask ctxt)
+                |> unfake),
+            TaskContinuationOptions.ExecuteSynchronously
+        )
         |> ignore
         |> fake
 
@@ -1154,11 +1249,13 @@ module AsyncPrimitives =
     // execution of the async there.
     []
     let AttachContinuationToUnitTask (task: Task) (ctxt: AsyncActivation) =
-        task.ContinueWith(Action(fun completedTask ->
-            ctxt.trampolineHolder.ExecuteWithTrampoline (fun () ->
-                OnUnitTaskCompleted completedTask ctxt)
-            |> unfake), TaskContinuationOptions.ExecuteSynchronously)
-        |> ignore 
+        task.ContinueWith(
+            Action(fun completedTask ->
+                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> OnUnitTaskCompleted completedTask ctxt)
+                |> unfake),
+            TaskContinuationOptions.ExecuteSynchronously
+        )
+        |> ignore
         |> fake
 
     /// Removes a registration places on a cancellation token
@@ -1173,7 +1270,7 @@ module AsyncPrimitives =
     let DisposeTimer (timer: byref) =
         match timer with
         | None -> ()
-        | Some t -> 
+        | Some t ->
             timer <- None
             t.Dispose()
 
@@ -1188,119 +1285,137 @@ module AsyncPrimitives =
     /// Unregisters a delegate handler, helper for AwaitEvent
     let RemoveHandler (event: IEvent<_, _>) (del: byref<'Delegate option>) =
         match del with
-        | Some d -> 
+        | Some d ->
             del <- None
             event.RemoveHandler d
         | None -> ()
 
     []
-    type AsyncIAsyncResult<'T>(callback: System.AsyncCallback, state:obj) =
-         // This gets set to false if the result is not available by the
-         // time the IAsyncResult is returned to the caller of Begin
-         let mutable completedSynchronously = true
+    type AsyncIAsyncResult<'T>(callback: System.AsyncCallback, state: obj) =
+        // This gets set to false if the result is not available by the
+        // time the IAsyncResult is returned to the caller of Begin
+        let mutable completedSynchronously = true
 
-         let mutable disposed = false
+        let mutable disposed = false
 
-         let cts = new CancellationTokenSource()
+        let cts = new CancellationTokenSource()
 
-         let result = new ResultCell>()
+        let result = new ResultCell>()
 
-         member s.SetResult(v: AsyncResult<'T>) =
-             result.RegisterResult(v, reuseThread=true) |> unfake
-             match callback with
-             | null -> ()
-             | d ->
-                 // The IASyncResult becomes observable here
-                 d.Invoke (s :> System.IAsyncResult)
+        member s.SetResult(v: AsyncResult<'T>) =
+            result.RegisterResult(v, reuseThread = true) |> unfake
 
-         member s.GetResult() =
-             match result.TryWaitForResultSynchronously (-1) with
-             | Some (AsyncResult.Ok v) -> v
-             | Some (AsyncResult.Error edi) -> edi.ThrowAny()
-             | Some (AsyncResult.Canceled err) -> raise err
-             | None -> failwith "unreachable"
+            match callback with
+            | null -> ()
+            | d ->
+                // The IASyncResult becomes observable here
+                d.Invoke(s :> System.IAsyncResult)
 
-         member x.IsClosed = disposed
+        member s.GetResult() =
+            match result.TryWaitForResultSynchronously(-1) with
+            | Some (AsyncResult.Ok v) -> v
+            | Some (AsyncResult.Error edi) -> edi.ThrowAny()
+            | Some (AsyncResult.Canceled err) -> raise err
+            | None -> failwith "unreachable"
 
-         member x.Close() =
-             if not disposed  then
-                 disposed <- true
-                 cts.Dispose()
-                 result.Close()
+        member x.IsClosed = disposed
 
-         member x.Token = cts.Token
+        member x.Close() =
+            if not disposed then
+                disposed <- true
+                cts.Dispose()
+                result.Close()
 
-         member x.CancelAsync() = cts.Cancel()
+        member x.Token = cts.Token
 
-         member x.CheckForNotSynchronous() =
-             if not result.ResultAvailable then
-                 completedSynchronously <- false
+        member x.CancelAsync() =
+            cts.Cancel()
 
-         interface System.IAsyncResult with
-              member _.IsCompleted = result.ResultAvailable
-              member _.CompletedSynchronously = completedSynchronously
-              member _.AsyncWaitHandle = result.GetWaitHandle()
-              member _.AsyncState = state
+        member x.CheckForNotSynchronous() =
+            if not result.ResultAvailable then
+                completedSynchronously <- false
 
-         interface System.IDisposable with
-             member x.Dispose() = x.Close()
+        interface System.IAsyncResult with
+            member _.IsCompleted = result.ResultAvailable
+            member _.CompletedSynchronously = completedSynchronously
+            member _.AsyncWaitHandle = result.GetWaitHandle()
+            member _.AsyncState = state
+
+        interface System.IDisposable with
+            member x.Dispose() =
+                x.Close()
 
     module AsBeginEndHelpers =
         let beginAction (computation, callback, state) =
             let aiar = new AsyncIAsyncResult<'T>(callback, state)
-            let cont res = aiar.SetResult (AsyncResult.Ok res)
-            let econt edi = aiar.SetResult (AsyncResult.Error edi)
-            let ccont cexn = aiar.SetResult (AsyncResult.Canceled cexn)
+
+            let cont res =
+                aiar.SetResult(AsyncResult.Ok res)
+
+            let econt edi =
+                aiar.SetResult(AsyncResult.Error edi)
+
+            let ccont cexn =
+                aiar.SetResult(AsyncResult.Canceled cexn)
+
             StartWithContinuations aiar.Token computation cont econt ccont
             aiar.CheckForNotSynchronous()
             (aiar :> IAsyncResult)
 
-        let endAction<'T> (iar:IAsyncResult) =
+        let endAction<'T> (iar: IAsyncResult) =
             match iar with
             | :? AsyncIAsyncResult<'T> as aiar ->
                 if aiar.IsClosed then
                     raise (System.ObjectDisposedException("AsyncResult"))
                 else
                     let res = aiar.GetResult()
-                    aiar.Close ()
+                    aiar.Close()
                     res
-            | _ ->
-                invalidArg "iar" (SR.GetString(SR.mismatchIAREnd))
+            | _ -> invalidArg "iar" (SR.GetString(SR.mismatchIAREnd))
 
-        let cancelAction<'T>(iar:IAsyncResult) =
+        let cancelAction<'T> (iar: IAsyncResult) =
             match iar with
-            | :? AsyncIAsyncResult<'T> as aiar ->
-                aiar.CancelAsync()
-            | _ ->
-                invalidArg "iar" (SR.GetString(SR.mismatchIARCancel))
+            | :? AsyncIAsyncResult<'T> as aiar -> aiar.CancelAsync()
+            | _ -> invalidArg "iar" (SR.GetString(SR.mismatchIARCancel))
 
 open AsyncPrimitives
 
 []
 type AsyncBuilder() =
-    member _.Zero () = unitAsync
+    member _.Zero() =
+        unitAsync
 
-    member _.Delay generator = CreateDelayAsync generator
+    member _.Delay generator =
+        CreateDelayAsync generator
 
-    member inline _.Return value = CreateReturnAsync value
+    member inline _.Return value =
+        CreateReturnAsync value
 
-    member inline _.ReturnFrom (computation:Async<_>) = computation
+    member inline _.ReturnFrom(computation: Async<_>) =
+        computation
 
-    member inline _.Bind (computation, binder) = CreateBindAsync computation binder
+    member inline _.Bind(computation, binder) =
+        CreateBindAsync computation binder
 
-    member _.Using (resource, binder) = CreateUsingAsync resource binder
+    member _.Using(resource, binder) =
+        CreateUsingAsync resource binder
 
-    member _.While (guard, computation) = CreateWhileAsync guard computation
+    member _.While(guard, computation) =
+        CreateWhileAsync guard computation
 
-    member _.For (sequence, body) = CreateForLoopAsync sequence body
+    member _.For(sequence, body) =
+        CreateForLoopAsync sequence body
 
-    member inline _.Combine (computation1, computation2) = CreateSequentialAsync computation1 computation2
+    member inline _.Combine(computation1, computation2) =
+        CreateSequentialAsync computation1 computation2
 
-    member inline _.TryFinally (computation, compensation) = CreateTryFinallyAsync compensation computation
+    member inline _.TryFinally(computation, compensation) =
+        CreateTryFinallyAsync compensation computation
 
-    member inline _.TryWith (computation, catchHandler) = CreateTryWithAsync catchHandler computation
+    member inline _.TryWith(computation, catchHandler) =
+        CreateTryWithAsync catchHandler computation
 
-    // member inline _.TryWithFilter (computation, catchHandler) = CreateTryWithFilterAsync catchHandler computation
+// member inline _.TryWithFilter (computation, catchHandler) = CreateTryWithFilterAsync catchHandler computation
 
 []
 module AsyncBuilderImpl =
@@ -1311,35 +1426,44 @@ type Async =
 
     static member CancellationToken = cancellationTokenAsync
 
-    static member CancelCheck () = unitAsync
+    static member CancelCheck() =
+        unitAsync
 
-    static member FromContinuations (callback: ('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) : Async<'T> =
-        MakeAsyncWithCancelCheck (fun ctxt ->
+    static member FromContinuations(callback: ('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) : Async<'T> =
+        MakeAsyncWithCancelCheck(fun ctxt ->
             let mutable underCurrentThreadStack = true
             let mutable contToTailCall = None
             let thread = Thread.CurrentThread
             let latch = Latch()
+
             let once cont x =
-                if not(latch.Enter()) then invalidOp(SR.GetString(SR.controlContinuationInvokedMultipleTimes))
+                if not (latch.Enter()) then
+                    invalidOp (SR.GetString(SR.controlContinuationInvokedMultipleTimes))
+
                 if Thread.CurrentThread.Equals thread && underCurrentThreadStack then
                     contToTailCall <- Some(fun () -> cont x)
                 elif Trampoline.ThisThreadHasTrampoline then
                     let syncCtxt = SynchronizationContext.Current
-                    ctxt.trampolineHolder.PostOrQueueWithTrampoline syncCtxt (fun () -> cont x) |> unfake
+
+                    ctxt.trampolineHolder.PostOrQueueWithTrampoline syncCtxt (fun () -> cont x)
+                    |> unfake
                 else
-                    ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> cont x ) |> unfake
+                    ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> cont x) |> unfake
+
             try
                 callback (once ctxt.cont, (fun exn -> once ctxt.econt (ExceptionDispatchInfo.RestoreOrCapture exn)), once ctxt.ccont)
             with exn ->
-                if not(latch.Enter()) then invalidOp(SR.GetString(SR.controlContinuationInvokedMultipleTimes))
+                if not (latch.Enter()) then
+                    invalidOp (SR.GetString(SR.controlContinuationInvokedMultipleTimes))
+
                 let edi = ExceptionDispatchInfo.RestoreOrCapture exn
                 ctxt.econt edi |> unfake
 
             underCurrentThreadStack <- false
 
             match contToTailCall with
-            | Some k -> k()
-            | _ -> fake())
+            | Some k -> k ()
+            | _ -> fake ())
 
     static member DefaultCancellationToken = defaultCancellationTokenSource.Token
 
@@ -1348,110 +1472,123 @@ type Async =
         // set new CancellationTokenSource before calling Cancel - otherwise if Cancel throws token will stay unchanged
         defaultCancellationTokenSource <- new CancellationTokenSource()
         cts.Cancel()
-        // we do not dispose the old default CTS - let GC collect it
+    // we do not dispose the old default CTS - let GC collect it
 
-    static member Catch (computation: Async<'T>) =
-        MakeAsync (fun ctxt ->
+    static member Catch(computation: Async<'T>) =
+        MakeAsync(fun ctxt ->
             // Turn the success or exception into data
-            let newCtxt = ctxt.WithContinuations(cont = (fun res -> ctxt.cont (Choice1Of2 res)),
-                                                 econt = (fun edi -> ctxt.cont (Choice2Of2 (edi.GetAssociatedSourceException()))))
+            let newCtxt =
+                ctxt.WithContinuations(
+                    cont = (fun res -> ctxt.cont (Choice1Of2 res)),
+                    econt = (fun edi -> ctxt.cont (Choice2Of2(edi.GetAssociatedSourceException())))
+                )
+
             computation.Invoke newCtxt)
 
-    static member RunSynchronously (computation: Async<'T>, ?timeout, ?cancellationToken:CancellationToken) =
+    static member RunSynchronously(computation: Async<'T>, ?timeout, ?cancellationToken: CancellationToken) =
         let timeout, cancellationToken =
             match cancellationToken with
             | None -> timeout, defaultCancellationTokenSource.Token
             | Some token when not token.CanBeCanceled -> timeout, token
             | Some token -> None, token
+
         AsyncPrimitives.RunSynchronously cancellationToken computation timeout
 
-    static member Start (computation, ?cancellationToken) =
-        let cancellationToken = defaultArg cancellationToken defaultCancellationTokenSource.Token
+    static member Start(computation, ?cancellationToken) =
+        let cancellationToken =
+            defaultArg cancellationToken defaultCancellationTokenSource.Token
+
         AsyncPrimitives.Start cancellationToken computation
 
-    static member StartAsTask (computation, ?taskCreationOptions, ?cancellationToken)=
-        let cancellationToken = defaultArg cancellationToken defaultCancellationTokenSource.Token
+    static member StartAsTask(computation, ?taskCreationOptions, ?cancellationToken) =
+        let cancellationToken =
+            defaultArg cancellationToken defaultCancellationTokenSource.Token
+
         AsyncPrimitives.StartAsTask cancellationToken computation taskCreationOptions
 
-    static member StartChildAsTask (computation, ?taskCreationOptions) =
-        async { 
+    static member StartChildAsTask(computation, ?taskCreationOptions) =
+        async {
             let! cancellationToken = cancellationTokenAsync
             return AsyncPrimitives.StartAsTask cancellationToken computation taskCreationOptions
         }
 
-    static member Parallel (computations: seq>) =
-        Async.Parallel(computations, ?maxDegreeOfParallelism=None)
+    static member Parallel(computations: seq>) =
+        Async.Parallel(computations, ?maxDegreeOfParallelism = None)
 
-    static member Parallel (computations: seq>, ?maxDegreeOfParallelism: int) =
+    static member Parallel(computations: seq>, ?maxDegreeOfParallelism: int) =
         match maxDegreeOfParallelism with
-        | Some x when x < 1 -> raise(System.ArgumentException(String.Format(SR.GetString(SR.maxDegreeOfParallelismNotPositive), x), "maxDegreeOfParallelism"))
+        | Some x when x < 1 ->
+            raise (System.ArgumentException(String.Format(SR.GetString(SR.maxDegreeOfParallelismNotPositive), x), "maxDegreeOfParallelism"))
         | _ -> ()
 
-        MakeAsyncWithCancelCheck (fun ctxt ->
+        MakeAsyncWithCancelCheck(fun ctxt ->
             // manually protect eval of seq
             let result =
                 try
-                    Choice1Of2 (Seq.toArray computations)
+                    Choice1Of2(Seq.toArray computations)
                 with exn ->
-                    Choice2Of2 (ExceptionDispatchInfo.RestoreOrCapture exn)
+                    Choice2Of2(ExceptionDispatchInfo.RestoreOrCapture exn)
 
             match result with
             | Choice2Of2 edi -> ctxt.econt edi
-            | Choice1Of2 [| |] -> ctxt.cont [| |]
+            | Choice1Of2 [||] -> ctxt.cont [||]
             | Choice1Of2 computations ->
-              ctxt.ProtectCode (fun () ->
-                let ctxt = DelimitSyncContext ctxt  // manually resync
-                let mutable count = computations.Length
-                let mutable firstExn = None
-                let results = Array.zeroCreate computations.Length
-                // Attempt to cancel the individual operations if an exception happens on any of the other threads
-                let innerCTS = new LinkedSubSource(ctxt.token)
-
-                let finishTask remaining =
-                    if (remaining = 0) then
-                        innerCTS.Dispose()
-                        match firstExn with
-                        | None -> ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.cont results)
-                        | Some (Choice1Of2 exn) -> ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.econt exn)
-                        | Some (Choice2Of2 cexn) -> ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.ccont cexn)
-                    else
-                        fake()
+                ctxt.ProtectCode(fun () ->
+                    let ctxt = DelimitSyncContext ctxt // manually resync
+                    let mutable count = computations.Length
+                    let mutable firstExn = None
+                    let results = Array.zeroCreate computations.Length
+                    // Attempt to cancel the individual operations if an exception happens on any of the other threads
+                    let innerCTS = new LinkedSubSource(ctxt.token)
+
+                    let finishTask remaining =
+                        if (remaining = 0) then
+                            innerCTS.Dispose()
+
+                            match firstExn with
+                            | None -> ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.cont results)
+                            | Some (Choice1Of2 exn) -> ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.econt exn)
+                            | Some (Choice2Of2 cexn) -> ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont cexn)
+                        else
+                            fake ()
+
+                    // recordSuccess and recordFailure between them decrement count to 0 and
+                    // as soon as 0 is reached dispose innerCancellationSource
+
+                    let recordSuccess i res =
+                        results.[i] <- res
+                        finishTask (Interlocked.Decrement &count)
+
+                    let recordFailure exn =
+                        // capture first exception and then decrement the counter to avoid race when
+                        // - thread 1 decremented counter and preempted by the scheduler
+                        // - thread 2 decremented counter and called finishTask
+                        // since exception is not yet captured - finishtask will fall into success branch
+                        match Interlocked.CompareExchange(&firstExn, Some exn, None) with
+                        | None ->
+                            // signal cancellation before decrementing the counter - this guarantees that no other thread can sneak to finishTask and dispose innerCTS
+                            // NOTE: Cancel may introduce reentrancy - i.e. when handler registered for the cancellation token invokes cancel continuation that will call 'recordFailure'
+                            // to correctly handle this we need to return decremented value, not the current value of 'count' otherwise we may invoke finishTask with value '0' several times
+                            innerCTS.Cancel()
+                        | _ -> ()
 
-                // recordSuccess and recordFailure between them decrement count to 0 and
-                // as soon as 0 is reached dispose innerCancellationSource
+                        finishTask (Interlocked.Decrement &count)
 
-                let recordSuccess i res =
-                    results.[i] <- res
-                    finishTask(Interlocked.Decrement &count)
+                    // If maxDegreeOfParallelism is set but is higher then the number of tasks we have we set it back to None to fall into the simple
+                    // queue all items branch
+                    let maxDegreeOfParallelism =
+                        match maxDegreeOfParallelism with
+                        | None -> None
+                        | Some x when x >= computations.Length -> None
+                        | Some _ as x -> x
 
-                let recordFailure exn =
-                    // capture first exception and then decrement the counter to avoid race when
-                    // - thread 1 decremented counter and preempted by the scheduler
-                    // - thread 2 decremented counter and called finishTask
-                    // since exception is not yet captured - finishtask will fall into success branch
-                    match Interlocked.CompareExchange(&firstExn, Some exn, None) with
-                    | None ->
-                        // signal cancellation before decrementing the counter - this guarantees that no other thread can sneak to finishTask and dispose innerCTS
-                        // NOTE: Cancel may introduce reentrancy - i.e. when handler registered for the cancellation token invokes cancel continuation that will call 'recordFailure'
-                        // to correctly handle this we need to return decremented value, not the current value of 'count' otherwise we may invoke finishTask with value '0' several times
-                        innerCTS.Cancel()
-                    | _ -> ()
-                    finishTask(Interlocked.Decrement &count)
-
-                // If maxDegreeOfParallelism is set but is higher then the number of tasks we have we set it back to None to fall into the simple
-                // queue all items branch
-                let maxDegreeOfParallelism =
+                    // Simple case (no maxDegreeOfParallelism) just queue all the work, if we have maxDegreeOfParallelism set we start that many workers
+                    // which will make progress on the actual computations
                     match maxDegreeOfParallelism with
-                    | None -> None
-                    | Some x when x >= computations.Length -> None
-                    | Some _ as x -> x
-
-                // Simple case (no maxDegreeOfParallelism) just queue all the work, if we have maxDegreeOfParallelism set we start that many workers
-                // which will make progress on the actual computations
-                match maxDegreeOfParallelism with
-                | None ->
-                    computations |> Array.iteri (fun i p ->
-                        QueueAsync
+                    | None ->
+                        computations
+                        |> Array.iteri (fun i p ->
+                            QueueAsync
                                 innerCTS.Token
                                 // on success, record the result
                                 (fun res -> recordSuccess i res)
@@ -1461,52 +1598,62 @@ type Async =
                                 (fun cexn -> recordFailure (Choice2Of2 cexn))
                                 p
                             |> unfake)
-                | Some maxDegreeOfParallelism ->
-                    let mutable i = -1
-                    let rec worker (trampolineHolder : TrampolineHolder) =
-                        if i < computations.Length then
-                            let j = Interlocked.Increment &i
-                            if j < computations.Length then
-                                if innerCTS.Token.IsCancellationRequested then
-                                    let cexn = OperationCanceledException (innerCTS.Token)
-                                    recordFailure (Choice2Of2 cexn) |> unfake
-                                    worker trampolineHolder
-                                else
-                                    let taskCtxt =
-                                        AsyncActivation.Create
-                                            innerCTS.Token
-                                            trampolineHolder
-                                            (fun res -> recordSuccess j res |> unfake; worker trampolineHolder |> fake)
-                                            (fun edi -> recordFailure (Choice1Of2 edi) |> unfake; worker trampolineHolder |> fake)
-                                            (fun cexn -> recordFailure (Choice2Of2 cexn) |> unfake; worker trampolineHolder |> fake)
-                                    computations.[j].Invoke taskCtxt |> unfake
-
-                    for x = 1 to maxDegreeOfParallelism do
-                        let trampolineHolder = TrampolineHolder()
-                        trampolineHolder.QueueWorkItemWithTrampoline (fun () ->
-                            worker trampolineHolder |> fake)
-                        |> unfake
-
-                fake()))
-
-    static member Sequential (computations: seq>) =
-        Async.Parallel(computations, maxDegreeOfParallelism=1)
+                    | Some maxDegreeOfParallelism ->
+                        let mutable i = -1
+
+                        let rec worker (trampolineHolder: TrampolineHolder) =
+                            if i < computations.Length then
+                                let j = Interlocked.Increment &i
+
+                                if j < computations.Length then
+                                    if innerCTS.Token.IsCancellationRequested then
+                                        let cexn = OperationCanceledException(innerCTS.Token)
+                                        recordFailure (Choice2Of2 cexn) |> unfake
+                                        worker trampolineHolder
+                                    else
+                                        let taskCtxt =
+                                            AsyncActivation.Create
+                                                innerCTS.Token
+                                                trampolineHolder
+                                                (fun res ->
+                                                    recordSuccess j res |> unfake
+                                                    worker trampolineHolder |> fake)
+                                                (fun edi ->
+                                                    recordFailure (Choice1Of2 edi) |> unfake
+                                                    worker trampolineHolder |> fake)
+                                                (fun cexn ->
+                                                    recordFailure (Choice2Of2 cexn) |> unfake
+                                                    worker trampolineHolder |> fake)
+
+                                        computations.[j].Invoke taskCtxt |> unfake
+
+                        for x = 1 to maxDegreeOfParallelism do
+                            let trampolineHolder = TrampolineHolder()
+
+                            trampolineHolder.QueueWorkItemWithTrampoline(fun () -> worker trampolineHolder |> fake)
+                            |> unfake
+
+                    fake ()))
+
+    static member Sequential(computations: seq>) =
+        Async.Parallel(computations, maxDegreeOfParallelism = 1)
 
     static member Choice(computations: Async<'T option> seq) : Async<'T option> =
-        MakeAsyncWithCancelCheck (fun ctxt ->
+        MakeAsyncWithCancelCheck(fun ctxt ->
             // manually protect eval of seq
             let result =
                 try
-                    Choice1Of2 (Seq.toArray computations)
+                    Choice1Of2(Seq.toArray computations)
                 with exn ->
-                    Choice2Of2 (ExceptionDispatchInfo.RestoreOrCapture exn)
+                    Choice2Of2(ExceptionDispatchInfo.RestoreOrCapture exn)
 
             match result with
             | Choice2Of2 edi -> ctxt.econt edi
-            | Choice1Of2 [| |] -> ctxt.cont None
+            | Choice1Of2 [||] -> ctxt.cont None
             | Choice1Of2 computations ->
-                 let ctxt = DelimitSyncContext ctxt
-                 ctxt.ProtectCode (fun () ->
+                let ctxt = DelimitSyncContext ctxt
+
+                ctxt.ProtectCode(fun () ->
                     let mutable count = computations.Length
                     let mutable noneCount = 0
                     let mutable someOrExnCount = 0
@@ -1517,15 +1664,17 @@ type Async =
                             match result with
                             | Some _ ->
                                 if Interlocked.Increment &someOrExnCount = 1 then
-                                    innerCts.Cancel(); ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.cont result)
+                                    innerCts.Cancel()
+                                    ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.cont result)
                                 else
-                                    fake()
+                                    fake ()
 
                             | None ->
                                 if Interlocked.Increment &noneCount = computations.Length then
-                                    innerCts.Cancel(); ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.cont None)
+                                    innerCts.Cancel()
+                                    ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.cont None)
                                 else
-                                    fake()
+                                    fake ()
 
                         if Interlocked.Decrement &count = 0 then
                             innerCts.Dispose()
@@ -1535,9 +1684,10 @@ type Async =
                     let econt (exn: ExceptionDispatchInfo) =
                         let result =
                             if Interlocked.Increment &someOrExnCount = 1 then
-                                innerCts.Cancel(); ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.econt exn)
+                                innerCts.Cancel()
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.econt exn)
                             else
-                                fake()
+                                fake ()
 
                         if Interlocked.Decrement &count = 0 then
                             innerCts.Dispose()
@@ -1547,9 +1697,10 @@ type Async =
                     let ccont (cexn: OperationCanceledException) =
                         let result =
                             if Interlocked.Increment &someOrExnCount = 1 then
-                                innerCts.Cancel(); ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.ccont cexn)
+                                innerCts.Cancel()
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont cexn)
                             else
-                                fake()
+                                fake ()
 
                         if Interlocked.Decrement &count = 0 then
                             innerCts.Dispose()
@@ -1559,85 +1710,125 @@ type Async =
                     for computation in computations do
                         QueueAsync innerCts.Token scont econt ccont computation |> unfake
 
-                    fake()))
+                    fake ()))
 
     /// StartWithContinuations, except the exception continuation is given an ExceptionDispatchInfo
-    static member StartWithContinuationsUsingDispatchInfo(computation:Async<'T>, continuation, exceptionContinuation, cancellationContinuation, ?cancellationToken) : unit =
-        let cancellationToken = defaultArg cancellationToken defaultCancellationTokenSource.Token
+    static member StartWithContinuationsUsingDispatchInfo
+        (
+            computation: Async<'T>,
+            continuation,
+            exceptionContinuation,
+            cancellationContinuation,
+            ?cancellationToken
+        ) : unit =
+        let cancellationToken =
+            defaultArg cancellationToken defaultCancellationTokenSource.Token
+
         AsyncPrimitives.StartWithContinuations cancellationToken computation continuation exceptionContinuation cancellationContinuation
 
-    static member StartWithContinuations(computation:Async<'T>, continuation, exceptionContinuation, cancellationContinuation, ?cancellationToken) : unit =
-        Async.StartWithContinuationsUsingDispatchInfo(computation, continuation, (fun edi -> exceptionContinuation (edi.GetAssociatedSourceException())), cancellationContinuation, ?cancellationToken=cancellationToken)
+    static member StartWithContinuations
+        (
+            computation: Async<'T>,
+            continuation,
+            exceptionContinuation,
+            cancellationContinuation,
+            ?cancellationToken
+        ) : unit =
+        Async.StartWithContinuationsUsingDispatchInfo(
+            computation,
+            continuation,
+            (fun edi -> exceptionContinuation (edi.GetAssociatedSourceException())),
+            cancellationContinuation,
+            ?cancellationToken = cancellationToken
+        )
+
+    static member StartImmediateAsTask(computation: Async<'T>, ?cancellationToken) : Task<'T> =
+        let cancellationToken =
+            defaultArg cancellationToken defaultCancellationTokenSource.Token
 
-    static member StartImmediateAsTask (computation: Async<'T>, ?cancellationToken ) : Task<'T>=
-        let cancellationToken = defaultArg cancellationToken defaultCancellationTokenSource.Token
         let ts = TaskCompletionSource<'T>()
         let task = ts.Task
+
         Async.StartWithContinuations(
             computation,
             (fun k -> ts.SetResult k),
             (fun exn -> ts.SetException exn),
             (fun _ -> ts.SetCanceled()),
-            cancellationToken)
+            cancellationToken
+        )
+
         task
 
-    static member StartImmediate(computation:Async, ?cancellationToken) : unit =
-        let cancellationToken = defaultArg cancellationToken defaultCancellationTokenSource.Token
+    static member StartImmediate(computation: Async, ?cancellationToken) : unit =
+        let cancellationToken =
+            defaultArg cancellationToken defaultCancellationTokenSource.Token
+
         AsyncPrimitives.StartWithContinuations cancellationToken computation id (fun edi -> edi.ThrowAny()) ignore
 
-    static member Sleep (millisecondsDueTime: int64) : Async =
-        MakeAsyncWithCancelCheck (fun ctxt ->
+    static member Sleep(millisecondsDueTime: int64) : Async =
+        MakeAsyncWithCancelCheck(fun ctxt ->
             let ctxt = DelimitSyncContext ctxt
             let mutable edi = null
             let latch = Latch()
             let mutable timer: Timer option = None
             let mutable registration: CancellationTokenRegistration option = None
+
             registration <-
-                ctxt.token.Register(Action(fun () ->
-                    if latch.Enter() then
-                        // Make sure we're not cancelled again
-                        DisposeCancellationRegistration ®istration 
-                        DisposeTimer &timer
-                        ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.ccont(OperationCanceledException(ctxt.token))) |> unfake)
-                ) |> Some
+                ctxt.token.Register(
+                    Action(fun () ->
+                        if latch.Enter() then
+                            // Make sure we're not cancelled again
+                            DisposeCancellationRegistration ®istration
+                            DisposeTimer &timer
+
+                            ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont (OperationCanceledException(ctxt.token)))
+                            |> unfake)
+                )
+                |> Some
+
             try
-                timer <- new Timer(TimerCallback(fun _ ->
-                                    if latch.Enter() then
-                                        // Ensure cancellation is not possible beyond this point
-                                        DisposeCancellationRegistration ®istration 
-                                        DisposeTimer &timer
-                                        // Now we're done, so call the continuation
-                                        ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.cont()) |> unfake),
-                                 null, dueTime=millisecondsDueTime, period = -1L) |> Some
+                timer <-
+                    new Timer(
+                        TimerCallback(fun _ ->
+                            if latch.Enter() then
+                                // Ensure cancellation is not possible beyond this point
+                                DisposeCancellationRegistration ®istration
+                                DisposeTimer &timer
+                                // Now we're done, so call the continuation
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.cont ()) |> unfake),
+                        null,
+                        dueTime = millisecondsDueTime,
+                        period = -1L
+                    )
+                    |> Some
             with exn ->
                 if latch.Enter() then
                     // Ensure cancellation is not possible beyond this point
-                    DisposeCancellationRegistration ®istration 
+                    DisposeCancellationRegistration ®istration
                     // Prepare to call exception continuation
                     edi <- ExceptionDispatchInfo.RestoreOrCapture exn
 
             // Call exception continuation if necessary
             match edi with
-            | null ->
-                fake()
-            | _ ->
-                ctxt.econt edi)
+            | null -> fake ()
+            | _ -> ctxt.econt edi)
 
-    static member Sleep (millisecondsDueTime: int32) : Async =
-        Async.Sleep (millisecondsDueTime |> int64)
+    static member Sleep(millisecondsDueTime: int32) : Async =
+        Async.Sleep(millisecondsDueTime |> int64)
 
-    static member Sleep (dueTime: TimeSpan) =
+    static member Sleep(dueTime: TimeSpan) =
         if dueTime < TimeSpan.Zero then
             raise (ArgumentOutOfRangeException("dueTime"))
         else
-            Async.Sleep (dueTime.TotalMilliseconds |> Checked.int64)
+            Async.Sleep(dueTime.TotalMilliseconds |> Checked.int64)
 
     /// Wait for a wait handle. Both timeout and cancellation are supported
-    static member AwaitWaitHandle(waitHandle: WaitHandle, ?millisecondsTimeout:int) =
-        MakeAsyncWithCancelCheck (fun ctxt ->
+    static member AwaitWaitHandle(waitHandle: WaitHandle, ?millisecondsTimeout: int) =
+        MakeAsyncWithCancelCheck(fun ctxt ->
             let millisecondsTimeout = defaultArg millisecondsTimeout Threading.Timeout.Infinite
+
             if millisecondsTimeout = 0 then
-                let ok = waitHandle.WaitOne(0, exitContext=false)
+                let ok = waitHandle.WaitOne(0, exitContext = false)
                 ctxt.cont ok
             else
                 let ctxt = DelimitSyncContext ctxt
@@ -1645,42 +1836,50 @@ type Async =
                 let latch = Latch()
                 let mutable rwh: RegisteredWaitHandle option = None
                 let mutable registration: CancellationTokenRegistration option = None
+
                 registration <-
-                    ctxt.token.Register(Action(fun () ->
-                        if latch.Enter() then
-                            // Make sure we're not cancelled again
-                            DisposeCancellationRegistration ®istration
+                    ctxt.token.Register(
+                        Action(fun () ->
+                            if latch.Enter() then
+                                // Make sure we're not cancelled again
+                                DisposeCancellationRegistration ®istration
 
-                            UnregisterWaitHandle &rwh
+                                UnregisterWaitHandle &rwh
 
-                            // Call the cancellation continuation
-                            ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.ccont(OperationCanceledException(ctxt.token))) |> unfake))
+                                // Call the cancellation continuation
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont (OperationCanceledException(ctxt.token)))
+                                |> unfake)
+                    )
                     |> Some
 
                 try
-                     rwh <- ThreadPool.RegisterWaitForSingleObject(waitObject=waitHandle,
-                                    callBack=WaitOrTimerCallback(fun _ timeOut ->
-                                                if latch.Enter() then
-                                                    // Ensure cancellation is not possible beyond this point
-                                                    DisposeCancellationRegistration ®istration 
-                                                    UnregisterWaitHandle &rwh
-                                                    // Call the success continuation
-                                                    ctxt.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxt.cont (not timeOut)) |> unfake),
-                                    state=null,
-                                    millisecondsTimeOutInterval=millisecondsTimeout,
-                                    executeOnlyOnce=true)
-                                |> Some
+                    rwh <-
+                        ThreadPool.RegisterWaitForSingleObject(
+                            waitObject = waitHandle,
+                            callBack =
+                                WaitOrTimerCallback(fun _ timeOut ->
+                                    if latch.Enter() then
+                                        // Ensure cancellation is not possible beyond this point
+                                        DisposeCancellationRegistration ®istration
+                                        UnregisterWaitHandle &rwh
+                                        // Call the success continuation
+                                        ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.cont (not timeOut))
+                                        |> unfake),
+                            state = null,
+                            millisecondsTimeOutInterval = millisecondsTimeout,
+                            executeOnlyOnce = true
+                        )
+                        |> Some
                 with exn ->
                     if latch.Enter() then
                         // Ensure cancellation is not possible beyond this point
-                        DisposeCancellationRegistration ®istration 
+                        DisposeCancellationRegistration ®istration
                         // Prepare to call exception continuation
                         edi <- ExceptionDispatchInfo.RestoreOrCapture exn
 
                 // Call exception continuation if necessary
                 match edi with
-                | null ->
-                    fake()
+                | null -> fake ()
                 | _ ->
                     // Call the exception continuation
                     ctxt.econt edi)
@@ -1690,7 +1889,7 @@ type Async =
             if iar.CompletedSynchronously then
                 return true
             else
-                return! Async.AwaitWaitHandle(iar.AsyncWaitHandle, ?millisecondsTimeout=millisecondsTimeout)
+                return! Async.AwaitWaitHandle(iar.AsyncWaitHandle, ?millisecondsTimeout = millisecondsTimeout)
         }
 
     /// Await and use the result of a result cell. The resulting async doesn't support cancellation
@@ -1705,25 +1904,33 @@ type Async =
     /// Await the result of a result cell belonging to a child computation.  The resulting async supports timeout and if
     /// it happens the child computation will be cancelled.   The resulting async doesn't support cancellation
     /// directly, rather the underlying computation must fill the result if cancellation occurs.
-    static member AwaitAndBindChildResult(innerCTS: CancellationTokenSource, resultCell: ResultCell>, millisecondsTimeout) : Async<'T> =
+    static member AwaitAndBindChildResult
+        (
+            innerCTS: CancellationTokenSource,
+            resultCell: ResultCell>,
+            millisecondsTimeout
+        ) : Async<'T> =
         match millisecondsTimeout with
-        | None | Some -1 ->
-            resultCell |> Async.AwaitAndBindResult_NoDirectCancelOrTimeout
+        | None
+        | Some -1 -> resultCell |> Async.AwaitAndBindResult_NoDirectCancelOrTimeout
 
         | Some 0 ->
-            async { if resultCell.ResultAvailable then
-                        let res = resultCell.GrabResult()
-                        return res.Commit()
-                    else
-                        return raise (System.TimeoutException()) }
+            async {
+                if resultCell.ResultAvailable then
+                    let res = resultCell.GrabResult()
+                    return res.Commit()
+                else
+                    return raise (System.TimeoutException())
+            }
         | _ ->
-            async { 
+            async {
                 try
                     if resultCell.ResultAvailable then
                         let res = resultCell.GrabResult()
                         return res.Commit()
                     else
-                        let! ok = Async.AwaitWaitHandle (resultCell.GetWaitHandle(), ?millisecondsTimeout=millisecondsTimeout)
+                        let! ok = Async.AwaitWaitHandle(resultCell.GetWaitHandle(), ?millisecondsTimeout = millisecondsTimeout)
+
                         if ok then
                             let res = resultCell.GrabResult()
                             return res.Commit()
@@ -1731,36 +1938,41 @@ type Async =
                             // issue cancellation signal
                             innerCTS.Cancel()
                             // wait for computation to quiesce
-                            let! _ = Async.AwaitWaitHandle (resultCell.GetWaitHandle())
+                            let! _ = Async.AwaitWaitHandle(resultCell.GetWaitHandle())
                             return raise (System.TimeoutException())
                 finally
-                    resultCell.Close() 
+                    resultCell.Close()
             }
 
-
-    static member FromBeginEnd(beginAction, endAction, ?cancelAction): Async<'T> =
+    static member FromBeginEnd(beginAction, endAction, ?cancelAction) : Async<'T> =
         async {
             let! ct = cancellationTokenAsync
             let resultCell = new ResultCell<_>()
 
             let latch = Latch()
             let mutable registration: CancellationTokenRegistration option = None
+
             registration <-
-                ct.Register(Action(fun () ->
-                    if latch.Enter() then
-                        // Make sure we're not cancelled again
-                        DisposeCancellationRegistration ®istration 
-
-                        // Call the cancellation function. Ignore any exceptions from the
-                        // cancellation function.
-                        match cancelAction with
-                        | None -> ()
-                        | Some cancel ->
-                            try cancel() with _ -> ()
-
-                        // Register the cancellation result.
-                        let canceledResult = Canceled (OperationCanceledException ct)
-                        resultCell.RegisterResult(canceledResult, reuseThread=true) |> unfake))
+                ct.Register(
+                    Action(fun () ->
+                        if latch.Enter() then
+                            // Make sure we're not cancelled again
+                            DisposeCancellationRegistration ®istration
+
+                            // Call the cancellation function. Ignore any exceptions from the
+                            // cancellation function.
+                            match cancelAction with
+                            | None -> ()
+                            | Some cancel ->
+                                try
+                                    cancel ()
+                                with _ ->
+                                    ()
+
+                            // Register the cancellation result.
+                            let canceledResult = Canceled(OperationCanceledException ct)
+                            resultCell.RegisterResult(canceledResult, reuseThread = true) |> unfake)
+                )
                 |> Some
 
             let callback =
@@ -1768,7 +1980,7 @@ type Async =
                     if not iar.CompletedSynchronously then
                         if latch.Enter() then
                             // Ensure cancellation is not possible beyond this point
-                            DisposeCancellationRegistration ®istration 
+                            DisposeCancellationRegistration ®istration
 
                             // Run the endAction and collect its result.
                             let res =
@@ -1778,13 +1990,14 @@ type Async =
                                     let edi = ExceptionDispatchInfo.RestoreOrCapture exn
                                     Error edi
 
-                            // Register the result. 
-                            resultCell.RegisterResult(res, reuseThread=true) |> unfake)
+                            // Register the result.
+                            resultCell.RegisterResult(res, reuseThread = true) |> unfake)
+
+            let (iar: IAsyncResult) = beginAction (callback, (null: obj))
 
-            let (iar:IAsyncResult) = beginAction (callback, (null:obj))
             if iar.CompletedSynchronously then
                 // Ensure cancellation is not possible beyond this point
-                DisposeCancellationRegistration ®istration 
+                DisposeCancellationRegistration ®istration
                 return endAction iar
             else
                 // Note: ok to use "NoDirectCancel" here because cancellation has been registered above
@@ -1792,27 +2005,25 @@ type Async =
                 return! Async.AwaitAndBindResult_NoDirectCancelOrTimeout resultCell
         }
 
+    static member FromBeginEnd(arg, beginAction, endAction, ?cancelAction) : Async<'T> =
+        Async.FromBeginEnd((fun (iar, state) -> beginAction (arg, iar, state)), endAction, ?cancelAction = cancelAction)
 
-    static member FromBeginEnd(arg, beginAction, endAction, ?cancelAction): Async<'T> =
-        Async.FromBeginEnd((fun (iar, state) -> beginAction(arg, iar, state)), endAction, ?cancelAction=cancelAction)
+    static member FromBeginEnd(arg1, arg2, beginAction, endAction, ?cancelAction) : Async<'T> =
+        Async.FromBeginEnd((fun (iar, state) -> beginAction (arg1, arg2, iar, state)), endAction, ?cancelAction = cancelAction)
 
-    static member FromBeginEnd(arg1, arg2, beginAction, endAction, ?cancelAction): Async<'T> =
-        Async.FromBeginEnd((fun (iar, state) -> beginAction(arg1, arg2, iar, state)), endAction, ?cancelAction=cancelAction)
+    static member FromBeginEnd(arg1, arg2, arg3, beginAction, endAction, ?cancelAction) : Async<'T> =
+        Async.FromBeginEnd((fun (iar, state) -> beginAction (arg1, arg2, arg3, iar, state)), endAction, ?cancelAction = cancelAction)
 
-    static member FromBeginEnd(arg1, arg2, arg3, beginAction, endAction, ?cancelAction): Async<'T> =
-        Async.FromBeginEnd((fun (iar, state) -> beginAction(arg1, arg2, arg3, iar, state)), endAction, ?cancelAction=cancelAction)
+    static member AsBeginEnd<'Arg, 'T>
+        (computation: ('Arg -> Async<'T>))
+        // The 'Begin' member
+        : ('Arg * System.AsyncCallback * obj -> System.IAsyncResult) * (System.IAsyncResult -> 'T) * (System.IAsyncResult -> unit) =
+        let beginAction =
+            fun (a1, callback, state) -> AsBeginEndHelpers.beginAction ((computation a1), callback, state)
 
-    static member AsBeginEnd<'Arg, 'T> (computation:('Arg -> Async<'T>)) :
-            // The 'Begin' member
-            ('Arg * System.AsyncCallback * obj -> System.IAsyncResult) *
-            // The 'End' member
-            (System.IAsyncResult -> 'T) *
-            // The 'Cancel' member
-            (System.IAsyncResult -> unit) =
-                let beginAction = fun (a1, callback, state) -> AsBeginEndHelpers.beginAction ((computation a1), callback, state)
-                beginAction, AsBeginEndHelpers.endAction<'T>, AsBeginEndHelpers.cancelAction<'T>
+        beginAction, AsBeginEndHelpers.endAction<'T>, AsBeginEndHelpers.cancelAction<'T>
 
-    static member AwaitEvent(event:IEvent<'Delegate, 'T>, ?cancelAction) : Async<'T> =
+    static member AwaitEvent(event: IEvent<'Delegate, 'T>, ?cancelAction) : Async<'T> =
         async {
             let! ct = cancellationTokenAsync
             let resultCell = new ResultCell<_>()
@@ -1820,37 +2031,45 @@ type Async =
             let latch = Latch()
             let mutable registration: CancellationTokenRegistration option = None
             let mutable del: 'Delegate option = None
-            registration <- 
-                ct.Register(Action(fun () ->
-                    if latch.Enter() then
-                        // Make sure we're not cancelled again
-                        DisposeCancellationRegistration ®istration 
-
-                        // Stop listening to events
-                        RemoveHandler event &del
-
-                        // Call the given cancellation routine if we've been given one
-                        // Exceptions from a cooperative cancellation are ignored.
-                        match cancelAction with
-                        | None -> ()
-                        | Some cancel ->
-                            try cancel() with _ -> ()
-                        
-                        // Register the cancellation result.
-                        resultCell.RegisterResult(Canceled (OperationCanceledException ct), reuseThread=true) |> unfake
-                )) |> Some
+
+            registration <-
+                ct.Register(
+                    Action(fun () ->
+                        if latch.Enter() then
+                            // Make sure we're not cancelled again
+                            DisposeCancellationRegistration ®istration
+
+                            // Stop listening to events
+                            RemoveHandler event &del
+
+                            // Call the given cancellation routine if we've been given one
+                            // Exceptions from a cooperative cancellation are ignored.
+                            match cancelAction with
+                            | None -> ()
+                            | Some cancel ->
+                                try
+                                    cancel ()
+                                with _ ->
+                                    ()
+
+                            // Register the cancellation result.
+                            resultCell.RegisterResult(Canceled(OperationCanceledException ct), reuseThread = true)
+                            |> unfake)
+                )
+                |> Some
 
             let del =
-                FuncDelegate<'T>.Create<'Delegate>(fun eventArgs ->
-                    if latch.Enter() then
-                        // Ensure cancellation is not possible beyond this point
-                        DisposeCancellationRegistration ®istration 
+                FuncDelegate<'T>
+                    .Create<'Delegate>(fun eventArgs ->
+                        if latch.Enter() then
+                            // Ensure cancellation is not possible beyond this point
+                            DisposeCancellationRegistration ®istration
 
-                        // Stop listening to events
-                        RemoveHandler event &del
+                            // Stop listening to events
+                            RemoveHandler event &del
 
-                        // Register the successful result.
-                        resultCell.RegisterResult(Ok eventArgs, reuseThread=true) |> unfake)
+                            // Register the successful result.
+                            resultCell.RegisterResult(Ok eventArgs, reuseThread = true) |> unfake)
 
             // Start listening to events
             event.AddHandler del
@@ -1858,63 +2077,87 @@ type Async =
             // Return the async computation that allows us to await the result
             // Note: ok to use "NoDirectCancel" here because cancellation has been registered above
             // Note: ok to use "NoDirectTimeout" here because no timeout parameter to this method
-            return! Async.AwaitAndBindResult_NoDirectCancelOrTimeout resultCell }
+            return! Async.AwaitAndBindResult_NoDirectCancelOrTimeout resultCell
+        }
 
-    static member Ignore (computation: Async<'T>) = CreateIgnoreAsync computation
+    static member Ignore(computation: Async<'T>) =
+        CreateIgnoreAsync computation
 
-    static member SwitchToNewThread() = CreateSwitchToNewThreadAsync()
+    static member SwitchToNewThread() =
+        CreateSwitchToNewThreadAsync()
 
-    static member SwitchToThreadPool() = CreateSwitchToThreadPoolAsync()
+    static member SwitchToThreadPool() =
+        CreateSwitchToThreadPoolAsync()
 
-    static member StartChild (computation:Async<'T>, ?millisecondsTimeout) =
+    static member StartChild(computation: Async<'T>, ?millisecondsTimeout) =
         async {
             let resultCell = new ResultCell<_>()
             let! ct = cancellationTokenAsync
             let innerCTS = new CancellationTokenSource() // innerCTS does not require disposal
             let mutable ctsRef = innerCTS
-            let registration =
-                ct.Register(Action(fun () ->
-                    match ctsRef with
-                    | null -> ()
-                    | otherwise -> otherwise.Cancel()))
 
-            do QueueAsync
-                   innerCTS.Token
-                   // since innerCTS is not ever Disposed, can call reg.Dispose() without a safety Latch
-                   (fun res -> ctsRef <- null; registration.Dispose(); resultCell.RegisterResult (Ok res, reuseThread=true))
-                   (fun edi -> ctsRef <- null; registration.Dispose(); resultCell.RegisterResult (Error edi, reuseThread=true))
-                   (fun err -> ctsRef <- null; registration.Dispose(); resultCell.RegisterResult (Canceled err, reuseThread=true))
-                   computation
-                 |> unfake
+            let registration =
+                ct.Register(
+                    Action(fun () ->
+                        match ctsRef with
+                        | null -> ()
+                        | otherwise -> otherwise.Cancel())
+                )
 
-            return Async.AwaitAndBindChildResult(innerCTS, resultCell, millisecondsTimeout) }
+            do
+                QueueAsync
+                    innerCTS.Token
+                    // since innerCTS is not ever Disposed, can call reg.Dispose() without a safety Latch
+                    (fun res ->
+                        ctsRef <- null
+                        registration.Dispose()
+                        resultCell.RegisterResult(Ok res, reuseThread = true))
+                    (fun edi ->
+                        ctsRef <- null
+                        registration.Dispose()
+                        resultCell.RegisterResult(Error edi, reuseThread = true))
+                    (fun err ->
+                        ctsRef <- null
+                        registration.Dispose()
+                        resultCell.RegisterResult(Canceled err, reuseThread = true))
+                    computation
+                |> unfake
+
+            return Async.AwaitAndBindChildResult(innerCTS, resultCell, millisecondsTimeout)
+        }
 
     static member SwitchToContext syncContext =
-        async { 
+        async {
             match syncContext with
             | null ->
                 // no synchronization context, just switch to the thread pool
                 do! Async.SwitchToThreadPool()
             | syncCtxt ->
                 // post the continuation to the synchronization context
-                return! CreateSwitchToAsync syncCtxt 
+                return! CreateSwitchToAsync syncCtxt
         }
 
     static member OnCancel interruption =
-        async { 
+        async {
             let! ct = cancellationTokenAsync
             // latch protects cancellation and disposal contention
             let latch = Latch()
             let mutable registration: CancellationTokenRegistration option = None
+
             registration <-
-                ct.Register(Action(fun () ->
+                ct.Register(
+                    Action(fun () ->
                         if latch.Enter() then
                             // Make sure we're not cancelled again
-                            DisposeCancellationRegistration ®istration 
+                            DisposeCancellationRegistration ®istration
+
                             try
                                 interruption ()
-                            with _ -> ()))
+                            with _ ->
+                                ())
+                )
                 |> Some
+
             let disposer =
                 { new System.IDisposable with
                     member _.Dispose() =
@@ -1923,32 +2166,34 @@ type Async =
                         if not ct.IsCancellationRequested then
                             if latch.Enter() then
                                 // Ensure cancellation is not possible beyond this point
-                                DisposeCancellationRegistration ®istration }
+                                DisposeCancellationRegistration ®istration
+                }
+
             return disposer
         }
 
-    static member TryCancelled (computation: Async<'T>, compensation) =
+    static member TryCancelled(computation: Async<'T>, compensation) =
         CreateWhenCancelledAsync compensation computation
 
-    static member AwaitTask (task:Task<'T>) : Async<'T> =
-        MakeAsyncWithCancelCheck (fun ctxt -> 
+    static member AwaitTask(task: Task<'T>) : Async<'T> =
+        MakeAsyncWithCancelCheck(fun ctxt ->
             if task.IsCompleted then
                 // Run synchronously without installing new trampoline
                 OnTaskCompleted task ctxt
             else
                 // Continue asynchronously, via syncContext if necessary, installing new trampoline
                 let ctxt = DelimitSyncContext ctxt
-                ctxt.ProtectCode (fun () -> AttachContinuationToTask task ctxt))
+                ctxt.ProtectCode(fun () -> AttachContinuationToTask task ctxt))
 
-    static member AwaitTask (task:Task) : Async =
-        MakeAsyncWithCancelCheck (fun ctxt -> 
+    static member AwaitTask(task: Task) : Async =
+        MakeAsyncWithCancelCheck(fun ctxt ->
             if task.IsCompleted then
                 // Continue synchronously without installing new trampoline
                 OnUnitTaskCompleted task ctxt
             else
                 // Continue asynchronously, via syncContext if necessary, installing new trampoline
                 let ctxt = DelimitSyncContext ctxt
-                ctxt.ProtectCode (fun () -> AttachContinuationToUnitTask task ctxt))
+                ctxt.ProtectCode(fun () -> AttachContinuationToUnitTask task ctxt))
 
 module CommonExtensions =
 
@@ -1957,80 +2202,98 @@ module CommonExtensions =
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
         member stream.AsyncRead(buffer: byte[], ?offset, ?count) =
             let offset = defaultArg offset 0
-            let count  = defaultArg count buffer.Length
-            Async.FromBeginEnd (buffer, offset, count, stream.BeginRead, stream.EndRead)
+            let count = defaultArg count buffer.Length
+            Async.FromBeginEnd(buffer, offset, count, stream.BeginRead, stream.EndRead)
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
         member stream.AsyncRead count =
-            async { 
+            async {
                 let buffer = Array.zeroCreate count
                 let mutable i = 0
+
                 while i < count do
                     let! n = stream.AsyncRead(buffer, i, count - i)
                     i <- i + n
+
                     if n = 0 then
-                        raise(System.IO.EndOfStreamException(SR.GetString(SR.failedReadEnoughBytes)))
-                return buffer 
+                        raise (System.IO.EndOfStreamException(SR.GetString(SR.failedReadEnoughBytes)))
+
+                return buffer
             }
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
-        member stream.AsyncWrite(buffer:byte[], ?offset:int, ?count:int) =
+        member stream.AsyncWrite(buffer: byte[], ?offset: int, ?count: int) =
             let offset = defaultArg offset 0
-            let count  = defaultArg count buffer.Length
-            Async.FromBeginEnd (buffer, offset, count, stream.BeginWrite, stream.EndWrite)
+            let count = defaultArg count buffer.Length
+            Async.FromBeginEnd(buffer, offset, count, stream.BeginWrite, stream.EndWrite)
 
     type IObservable<'Args> with
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
-        member x.Add(callback: 'Args -> unit) = x.Subscribe callback |> ignore
+        member x.Add(callback: 'Args -> unit) =
+            x.Subscribe callback |> ignore
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
         member x.Subscribe callback =
-            x.Subscribe { new IObserver<'Args> with
-                              member x.OnNext args = callback args
-                              member x.OnError e = ()
-                              member x.OnCompleted() = () }
+            x.Subscribe
+                { new IObserver<'Args> with
+                    member x.OnNext args =
+                        callback args
+
+                    member x.OnError e =
+                        ()
+
+                    member x.OnCompleted() =
+                        ()
+                }
 
 module WebExtensions =
 
     type System.Net.WebRequest with
+
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
-        member req.AsyncGetResponse() : Async=
+        member req.AsyncGetResponse() : Async =
 
             let mutable canceled = false // WebException with Status = WebExceptionStatus.RequestCanceled  can be raised in other situations except cancellation, use flag to filter out false positives
 
             // Use CreateTryWithFilterAsync to allow propagation of exception without losing stack
-            Async.FromBeginEnd(beginAction=req.BeginGetResponse,
-                               endAction = req.EndGetResponse,
-                               cancelAction = fun() -> canceled <- true; req.Abort())
-            |> CreateTryWithFilterAsync (fun exn ->
+            Async.FromBeginEnd(
+                beginAction = req.BeginGetResponse,
+                endAction = req.EndGetResponse,
+                cancelAction =
+                    fun () ->
+                        canceled <- true
+                        req.Abort()
+            )
+            |> CreateTryWithFilterAsync(fun exn ->
                 match exn with
-                | :? System.Net.WebException as webExn
-                        when webExn.Status = System.Net.WebExceptionStatus.RequestCanceled && canceled ->
+                | :? System.Net.WebException as webExn when webExn.Status = System.Net.WebExceptionStatus.RequestCanceled && canceled ->
 
-                    Some (CreateAsyncResultAsync(AsyncResult.Canceled (OperationCanceledException webExn.Message)))
-                | _ ->
-                    None)
+                    Some(CreateAsyncResultAsync(AsyncResult.Canceled(OperationCanceledException webExn.Message)))
+                | _ -> None)
 
     type System.Net.WebClient with
+
         member inline private this.Download(event: IEvent<'T, _>, handler: _ -> 'T, start, result) =
             let downloadAsync =
-                Async.FromContinuations (fun (cont, econt, ccont) ->
-                    let userToken = obj()
+                Async.FromContinuations(fun (cont, econt, ccont) ->
+                    let userToken = obj ()
+
                     let rec delegate' (_: obj) (args: #ComponentModel.AsyncCompletedEventArgs) =
                         // ensure we handle the completed event from correct download call
                         if userToken = args.UserState then
                             event.RemoveHandler handle
+
                             if args.Cancelled then
                                 ccont (OperationCanceledException())
                             elif isNotNull args.Error then
                                 econt args.Error
                             else
                                 cont (result args)
+
                     and handle = handler delegate'
                     event.AddHandler handle
-                    start userToken
-                )
+                    start userToken)
 
             async {
                 use! _holder = Async.OnCancel(fun _ -> this.CancelAsync())
@@ -2038,28 +2301,28 @@ module WebExtensions =
             }
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
-        member this.AsyncDownloadString (address:Uri) : Async =
+        member this.AsyncDownloadString(address: Uri) : Async =
             this.Download(
-                event   = this.DownloadStringCompleted,
-                handler = (fun action    -> Net.DownloadStringCompletedEventHandler action),
-                start   = (fun userToken -> this.DownloadStringAsync(address, userToken)),
-                result  = (fun args      -> args.Result)
+                event = this.DownloadStringCompleted,
+                handler = (fun action -> Net.DownloadStringCompletedEventHandler action),
+                start = (fun userToken -> this.DownloadStringAsync(address, userToken)),
+                result = (fun args -> args.Result)
             )
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
-        member this.AsyncDownloadData (address:Uri) : Async =
+        member this.AsyncDownloadData(address: Uri) : Async =
             this.Download(
-                event   = this.DownloadDataCompleted,
-                handler = (fun action    -> Net.DownloadDataCompletedEventHandler action),
-                start   = (fun userToken -> this.DownloadDataAsync(address, userToken)),
-                result  = (fun args      -> args.Result)
+                event = this.DownloadDataCompleted,
+                handler = (fun action -> Net.DownloadDataCompletedEventHandler action),
+                start = (fun userToken -> this.DownloadDataAsync(address, userToken)),
+                result = (fun args -> args.Result)
             )
 
         [] // give the extension member a 'nice', unmangled compiled name, unique within this module
-        member this.AsyncDownloadFile (address:Uri, fileName:string) : Async =
+        member this.AsyncDownloadFile(address: Uri, fileName: string) : Async =
             this.Download(
-                event   = this.DownloadFileCompleted,
-                handler = (fun action    -> ComponentModel.AsyncCompletedEventHandler action),
-                start   = (fun userToken -> this.DownloadFileAsync(address, fileName, userToken)),
-                result  = (fun _         -> ())
+                event = this.DownloadFileCompleted,
+                handler = (fun action -> ComponentModel.AsyncCompletedEventHandler action),
+                start = (fun userToken -> this.DownloadFileAsync(address, fileName, userToken)),
+                result = (fun _ -> ())
             )
diff --git a/src/FSharp.Core/collections.fs b/src/FSharp.Core/collections.fs
index b08a1989622..3094b9977e3 100644
--- a/src/FSharp.Core/collections.fs
+++ b/src/FSharp.Core/collections.fs
@@ -8,41 +8,58 @@ open Microsoft.FSharp.Core
 open Microsoft.FSharp.Core.Operators
 open System.Collections.Generic
 
-module HashIdentity = 
-            
-    let inline Structural<'T when 'T : equality> : IEqualityComparer<'T> = 
+module HashIdentity =
+
+    let inline Structural<'T when 'T: equality> : IEqualityComparer<'T> =
         LanguagePrimitives.FastGenericEqualityComparer<'T>
-          
-    let inline LimitedStructural<'T when 'T : equality>(limit) : IEqualityComparer<'T> = 
+
+    let inline LimitedStructural<'T when 'T: equality> (limit) : IEqualityComparer<'T> =
         LanguagePrimitives.FastLimitedGenericEqualityComparer<'T>(limit)
-          
-    let Reference<'T when 'T : not struct > : IEqualityComparer<'T> = 
+
+    let Reference<'T when 'T: not struct> : IEqualityComparer<'T> =
+        { new IEqualityComparer<'T> with
+            member _.GetHashCode(x) =
+                LanguagePrimitives.PhysicalHash(x)
+
+            member _.Equals(x, y) =
+                LanguagePrimitives.PhysicalEquality x y
+        }
+
+    let inline NonStructural<'T when 'T: equality and 'T: (static member (=): 'T * 'T -> bool)> =
         { new IEqualityComparer<'T> with
-              member _.GetHashCode(x) = LanguagePrimitives.PhysicalHash(x) 
-              member _.Equals(x,y) = LanguagePrimitives.PhysicalEquality x y }
+            member _.GetHashCode(x) =
+                NonStructuralComparison.hash x
+
+            member _.Equals(x, y) =
+                NonStructuralComparison.(=) x y
+        }
+
+    let inline FromFunctions hasher equality : IEqualityComparer<'T> =
+        let eq = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (equality)
 
-    let inline NonStructural< 'T when 'T : equality and 'T  : (static member ( = ) : 'T * 'T    -> bool) > = 
         { new IEqualityComparer<'T> with
-              member _.GetHashCode(x) = NonStructuralComparison.hash x 
-              member _.Equals(x, y) = NonStructuralComparison.(=) x y  }
+            member _.GetHashCode(x) =
+                hasher x
 
-    let inline FromFunctions hasher equality : IEqualityComparer<'T> = 
-        let eq = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(equality)
-        { new IEqualityComparer<'T> with 
-            member _.GetHashCode(x) = hasher x
-            member _.Equals(x,y) = eq.Invoke(x,y)  }
+            member _.Equals(x, y) =
+                eq.Invoke(x, y)
+        }
 
-module ComparisonIdentity = 
+module ComparisonIdentity =
 
-    let inline Structural<'T when 'T : comparison > : IComparer<'T> = 
+    let inline Structural<'T when 'T: comparison> : IComparer<'T> =
         LanguagePrimitives.FastGenericComparer<'T>
 
-    let inline NonStructural< 'T when 'T : (static member ( < ) : 'T * 'T    -> bool) and 'T : (static member ( > ) : 'T * 'T    -> bool) > : IComparer<'T> = 
+    let inline NonStructural<'T when 'T: (static member (<): 'T * 'T -> bool) and 'T: (static member (>): 'T * 'T -> bool)> : IComparer<'T> =
         { new IComparer<'T> with
-              member _.Compare(x,y) = NonStructuralComparison.compare x y } 
+            member _.Compare(x, y) =
+                NonStructuralComparison.compare x y
+        }
 
-    let FromFunction comparer = 
-        let comparer = OptimizedClosures.FSharpFunc<'T,'T,int>.Adapt(comparer)
-        { new IComparer<'T> with
-              member _.Compare(x,y) = comparer.Invoke(x,y) } 
+    let FromFunction comparer =
+        let comparer = OptimizedClosures.FSharpFunc<'T, 'T, int>.Adapt (comparer)
 
+        { new IComparer<'T> with
+            member _.Compare(x, y) =
+                comparer.Invoke(x, y)
+        }
diff --git a/src/FSharp.Core/event.fs b/src/FSharp.Core/event.fs
index 054b31cbd0b..108dd9b88eb 100644
--- a/src/FSharp.Core/event.fs
+++ b/src/FSharp.Core/event.fs
@@ -17,138 +17,179 @@ module private Atomic =
     let inline setWith (thunk: 'a -> 'a) (value: byref<'a>) =
         let mutable exchanged = false
         let mutable oldValue = value
+
         while not exchanged do
             let comparand = oldValue
             let newValue = thunk comparand
             oldValue <- Interlocked.CompareExchange(&value, newValue, comparand)
+
             if obj.ReferenceEquals(comparand, oldValue) then
                 exchanged <- true
 
 []
-type DelegateEvent<'Delegate when 'Delegate :> System.Delegate>() = 
-    let mutable multicast : System.Delegate = null
-    member x.Trigger(args:obj[]) = 
-        match multicast with 
+type DelegateEvent<'Delegate when 'Delegate :> System.Delegate>() =
+    let mutable multicast: System.Delegate = null
+
+    member x.Trigger(args: obj[]) =
+        match multicast with
         | null -> ()
         | d -> d.DynamicInvoke(args) |> ignore
-    member x.Publish = 
-        { new IDelegateEvent<'Delegate> with 
+
+    member x.Publish =
+        { new IDelegateEvent<'Delegate> with
             member x.AddHandler(d) =
                 Atomic.setWith (fun value -> System.Delegate.Combine(value, d)) &multicast
+
             member x.RemoveHandler(d) =
-                Atomic.setWith (fun value -> System.Delegate.Remove(value, d)) &multicast }
+                Atomic.setWith (fun value -> System.Delegate.Remove(value, d)) &multicast
+        }
 
 type EventDelegee<'Args>(observer: System.IObserver<'Args>) =
-    static let makeTuple = 
+    static let makeTuple =
         if Microsoft.FSharp.Reflection.FSharpType.IsTuple(typeof<'Args>) then
             Microsoft.FSharp.Reflection.FSharpValue.PreComputeTupleConstructor(typeof<'Args>)
         else
-            fun _ -> assert false; null // should not be called, one-argument case don't use makeTuple function
+            fun _ ->
+                assert false
+                null // should not be called, one-argument case don't use makeTuple function
 
-    member x.Invoke(_sender:obj, args: 'Args) = 
+    member x.Invoke(_sender: obj, args: 'Args) =
         observer.OnNext args
-    member x.Invoke(_sender:obj, a, b) = 
-        let args = makeTuple([|a; b|]) :?> 'Args
-        observer.OnNext args
-    member x.Invoke(_sender:obj, a, b, c) = 
-        let args = makeTuple([|a; b; c|]) :?> 'Args
+
+    member x.Invoke(_sender: obj, a, b) =
+        let args = makeTuple ([| a; b |]) :?> 'Args
         observer.OnNext args
-    member x.Invoke(_sender:obj, a, b, c, d) = 
-        let args = makeTuple([|a; b; c; d|]) :?> 'Args
+
+    member x.Invoke(_sender: obj, a, b, c) =
+        let args = makeTuple ([| a; b; c |]) :?> 'Args
         observer.OnNext args
-    member x.Invoke(_sender:obj, a, b, c, d, e) = 
-        let args = makeTuple([|a; b; c; d; e|]) :?> 'Args
+
+    member x.Invoke(_sender: obj, a, b, c, d) =
+        let args = makeTuple ([| a; b; c; d |]) :?> 'Args
         observer.OnNext args
-    member x.Invoke(_sender:obj, a, b, c, d, e, f) = 
-        let args = makeTuple([|a; b; c; d; e; f|]) :?> 'Args
+
+    member x.Invoke(_sender: obj, a, b, c, d, e) =
+        let args = makeTuple ([| a; b; c; d; e |]) :?> 'Args
         observer.OnNext args
 
+    member x.Invoke(_sender: obj, a, b, c, d, e, f) =
+        let args = makeTuple ([| a; b; c; d; e; f |]) :?> 'Args
+        observer.OnNext args
 
-type EventWrapper<'Delegate,'Args> = delegate of 'Delegate * obj * 'Args -> unit
+type EventWrapper<'Delegate, 'Args> = delegate of 'Delegate * obj * 'Args -> unit
 
 []
-type Event<'Delegate, 'Args when 'Delegate : delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct>() =  
+type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct>() =
+
+    let mutable multicast: 'Delegate = Unchecked.defaultof<_>
 
-    let mutable multicast : 'Delegate = Unchecked.defaultof<_>     
+    static let mi, argTypes =
+        let instanceBindingFlags =
+            BindingFlags.Instance
+            ||| BindingFlags.Public
+            ||| BindingFlags.NonPublic
+            ||| BindingFlags.DeclaredOnly
 
-    static let mi, argTypes = 
-        let instanceBindingFlags = BindingFlags.Instance ||| BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.DeclaredOnly
-        let mi = typeof<'Delegate>.GetMethod("Invoke",instanceBindingFlags)
+        let mi = typeof<'Delegate>.GetMethod ("Invoke", instanceBindingFlags)
         let actualTypes = mi.GetParameters() |> Array.map (fun p -> p.ParameterType)
         mi, actualTypes.[1..]
 
-    // For the one-argument case, use an optimization that allows a fast call. 
+    // For the one-argument case, use an optimization that allows a fast call.
     // CreateDelegate creates a delegate that is fast to invoke.
-    static let invoker = 
-        if argTypes.Length = 1 then 
-            (System.Delegate.CreateDelegate(typeof>, mi) :?> EventWrapper<'Delegate,'Args>)
+    static let invoker =
+        if argTypes.Length = 1 then
+            (System.Delegate.CreateDelegate(typeof>, mi) :?> EventWrapper<'Delegate, 'Args>)
         else
             null
 
     // For the multi-arg case, use a slower DynamicInvoke.
     static let invokeInfo =
-        let instanceBindingFlags = BindingFlags.Instance ||| BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.DeclaredOnly
-        let mi = 
-            typeof>.GetMethods(instanceBindingFlags)
-            |> Seq.filter(fun mi -> mi.Name = "Invoke" && mi.GetParameters().Length = argTypes.Length + 1)
+        let instanceBindingFlags =
+            BindingFlags.Instance
+            ||| BindingFlags.Public
+            ||| BindingFlags.NonPublic
+            ||| BindingFlags.DeclaredOnly
+
+        let mi =
+            typeof>.GetMethods (instanceBindingFlags)
+            |> Seq.filter (fun mi -> mi.Name = "Invoke" && mi.GetParameters().Length = argTypes.Length + 1)
             |> Seq.exactlyOne
+
         if mi.IsGenericMethodDefinition then
             mi.MakeGenericMethod argTypes
-        else 
-            mi                
+        else
+            mi
 
-    member x.Trigger(sender:obj,args: 'Args) = 
-        // Copy multicast value into local variable to avoid changing during member call. 
+    member x.Trigger(sender: obj, args: 'Args) =
+        // Copy multicast value into local variable to avoid changing during member call.
         let multicast = multicast
-        match box multicast with 
-        | null -> () 
-        | _ -> 
-            match invoker with 
-            | null ->  
-                let args = Array.append [| sender |] (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields(box args))
+
+        match box multicast with
+        | null -> ()
+        | _ ->
+            match invoker with
+            | null ->
+                let args =
+                    Array.append [| sender |] (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields(box args))
+
                 multicast.DynamicInvoke(args) |> ignore
-            | _ -> 
-                // For the one-argument case, use an optimization that allows a fast call. 
+            | _ ->
+                // For the one-argument case, use an optimization that allows a fast call.
                 // CreateDelegate creates a delegate that is fast to invoke.
                 invoker.Invoke(multicast, sender, args) |> ignore
 
     member x.Publish =
         { new obj() with
-              member x.ToString() = ""
-          interface IEvent<'Delegate,'Args> with 
-            member e.AddHandler(d) =
-                Atomic.setWith (fun value -> System.Delegate.Combine(value, d) :?> 'Delegate) &multicast
-            member e.RemoveHandler(d) = 
-                Atomic.setWith (fun value -> System.Delegate.Remove(value, d) :?> 'Delegate) &multicast
-          interface System.IObservable<'Args> with 
-            member e.Subscribe(observer) = 
-               let obj = new EventDelegee<'Args>(observer)
-               let h = System.Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeInfo) :?> 'Delegate
-               (e :?> IDelegateEvent<'Delegate>).AddHandler(h)
-               { new System.IDisposable with 
-                    member x.Dispose() = (e :?> IDelegateEvent<'Delegate>).RemoveHandler(h) } } 
+            member x.ToString() =
+                ""
+          interface IEvent<'Delegate, 'Args> with
+              member e.AddHandler(d) =
+                  Atomic.setWith (fun value -> System.Delegate.Combine(value, d) :?> 'Delegate) &multicast
+
+              member e.RemoveHandler(d) =
+                  Atomic.setWith (fun value -> System.Delegate.Remove(value, d) :?> 'Delegate) &multicast
+          interface System.IObservable<'Args> with
+              member e.Subscribe(observer) =
+                  let obj = new EventDelegee<'Args>(observer)
+
+                  let h =
+                      System.Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeInfo) :?> 'Delegate
 
+                  (e :?> IDelegateEvent<'Delegate>).AddHandler(h)
+
+                  { new System.IDisposable with
+                      member x.Dispose() =
+                          (e :?> IDelegateEvent<'Delegate>).RemoveHandler(h)
+                  }
+        }
 
 []
-type Event<'T> = 
-    val mutable multicast : Handler<'T>
+type Event<'T> =
+    val mutable multicast: Handler<'T>
     new() = { multicast = null }
 
-    member x.Trigger(arg:'T) = 
-        match x.multicast with 
+    member x.Trigger(arg: 'T) =
+        match x.multicast with
         | null -> ()
-        | d -> d.Invoke(null,arg) |> ignore
+        | d -> d.Invoke(null, arg) |> ignore
+
     member x.Publish =
         { new obj() with
-              member x.ToString() = ""
-          interface IEvent<'T> with 
-            member e.AddHandler(d) =
-                Atomic.setWith (fun value -> System.Delegate.Combine(value, d) :?> Handler<'T>) &x.multicast
-            member e.RemoveHandler(d) = 
-                Atomic.setWith (fun value -> System.Delegate.Remove(value, d) :?> Handler<'T>) &x.multicast
-          interface System.IObservable<'T> with 
-            member e.Subscribe(observer) = 
-               let h = new Handler<_>(fun sender args -> observer.OnNext(args))
-               (e :?> IEvent<_,_>).AddHandler(h)
-               { new System.IDisposable with 
-                    member x.Dispose() = (e :?> IEvent<_,_>).RemoveHandler(h) } }
+            member x.ToString() =
+                ""
+          interface IEvent<'T> with
+              member e.AddHandler(d) =
+                  Atomic.setWith (fun value -> System.Delegate.Combine(value, d) :?> Handler<'T>) &x.multicast
+
+              member e.RemoveHandler(d) =
+                  Atomic.setWith (fun value -> System.Delegate.Remove(value, d) :?> Handler<'T>) &x.multicast
+          interface System.IObservable<'T> with
+              member e.Subscribe(observer) =
+                  let h = new Handler<_>(fun sender args -> observer.OnNext(args))
+                  (e :?> IEvent<_, _>).AddHandler(h)
+
+                  { new System.IDisposable with
+                      member x.Dispose() =
+                          (e :?> IEvent<_, _>).RemoveHandler(h)
+                  }
+        }
diff --git a/src/FSharp.Core/eventmodule.fs b/src/FSharp.Core/eventmodule.fs
index b9776a692d9..cd9dc68a76d 100644
--- a/src/FSharp.Core/eventmodule.fs
+++ b/src/FSharp.Core/eventmodule.fs
@@ -9,73 +9,92 @@ open Microsoft.FSharp.Control
 []
 module Event =
     []
-    let create<'T>() = 
-        let ev = new Event<'T>() 
+    let create<'T> () =
+        let ev = new Event<'T>()
         ev.Trigger, ev.Publish
 
     []
-    let map mapping (sourceEvent: IEvent<'Delegate,'T>) =
-        let ev = new Event<_>() 
+    let map mapping (sourceEvent: IEvent<'Delegate, 'T>) =
+        let ev = new Event<_>()
         sourceEvent.Add(fun x -> ev.Trigger(mapping x))
         ev.Publish
 
     []
-    let filter predicate (sourceEvent: IEvent<'Delegate,'T>) =
-        let ev = new Event<_>() 
+    let filter predicate (sourceEvent: IEvent<'Delegate, 'T>) =
+        let ev = new Event<_>()
         sourceEvent.Add(fun x -> if predicate x then ev.Trigger x)
         ev.Publish
 
     []
-    let partition predicate (sourceEvent: IEvent<'Delegate,'T>) =
-        let ev1 = new Event<_>() 
-        let ev2 = new Event<_>() 
-        sourceEvent.Add(fun x -> if predicate x then ev1.Trigger x else ev2.Trigger x)
-        ev1.Publish,ev2.Publish
+    let partition predicate (sourceEvent: IEvent<'Delegate, 'T>) =
+        let ev1 = new Event<_>()
+        let ev2 = new Event<_>()
+
+        sourceEvent.Add(fun x ->
+            if predicate x then
+                ev1.Trigger x
+            else
+                ev2.Trigger x)
+
+        ev1.Publish, ev2.Publish
 
     []
-    let choose chooser (sourceEvent: IEvent<'Delegate,'T>) =
-        let ev = new Event<_>() 
-        sourceEvent.Add(fun x -> match chooser x with None -> () | Some r -> ev.Trigger r)
+    let choose chooser (sourceEvent: IEvent<'Delegate, 'T>) =
+        let ev = new Event<_>()
+
+        sourceEvent.Add(fun x ->
+            match chooser x with
+            | None -> ()
+            | Some r -> ev.Trigger r)
+
         ev.Publish
 
     []
-    let scan collector state (sourceEvent: IEvent<'Delegate,'T>) =
+    let scan collector state (sourceEvent: IEvent<'Delegate, 'T>) =
         let mutable state = state
-        let ev = new Event<_>() 
+        let ev = new Event<_>()
+
         sourceEvent.Add(fun msg ->
-             let z = state
-             let z = collector z msg
-             state <- z; 
-             ev.Trigger(z))
+            let z = state
+            let z = collector z msg
+            state <- z
+            ev.Trigger(z))
+
         ev.Publish
 
     []
-    let add callback (sourceEvent: IEvent<'Delegate,'T>) = sourceEvent.Add(callback)
+    let add callback (sourceEvent: IEvent<'Delegate, 'T>) =
+        sourceEvent.Add(callback)
 
     []
-    let pairwise (sourceEvent : IEvent<'Delegate,'T>) : IEvent<'T * 'T> = 
-        let ev = new Event<'T * 'T>() 
+    let pairwise (sourceEvent: IEvent<'Delegate, 'T>) : IEvent<'T * 'T> =
+        let ev = new Event<'T * 'T>()
         let mutable lastArgs = None
-        sourceEvent.Add(fun args2 -> 
-            (match lastArgs with 
-             | None -> () 
-             | Some args1 -> ev.Trigger(args1,args2))
+
+        sourceEvent.Add(fun args2 ->
+            (match lastArgs with
+             | None -> ()
+             | Some args1 -> ev.Trigger(args1, args2))
+
             lastArgs <- Some args2)
 
         ev.Publish
 
     []
-    let merge (event1: IEvent<'Del1,'T>) (event2: IEvent<'Del2,'T>) =
-        let ev = new Event<_>() 
+    let merge (event1: IEvent<'Del1, 'T>) (event2: IEvent<'Del2, 'T>) =
+        let ev = new Event<_>()
         event1.Add(fun x -> ev.Trigger(x))
         event2.Add(fun x -> ev.Trigger(x))
         ev.Publish
 
     []
-    let split (splitter : 'T -> Choice<'U1,'U2>) (sourceEvent: IEvent<'Delegate,'T>) =
-        let ev1 = new Event<_>() 
-        let ev2 = new Event<_>() 
-        sourceEvent.Add(fun x -> match splitter x with Choice1Of2 y -> ev1.Trigger(y) | Choice2Of2 z -> ev2.Trigger(z))
-        ev1.Publish,ev2.Publish
+    let split (splitter: 'T -> Choice<'U1, 'U2>) (sourceEvent: IEvent<'Delegate, 'T>) =
+        let ev1 = new Event<_>()
+        let ev2 = new Event<_>()
 
+        sourceEvent.Add(fun x ->
+            match splitter x with
+            | Choice1Of2 y -> ev1.Trigger(y)
+            | Choice2Of2 z -> ev2.Trigger(z))
 
+        ev1.Publish, ev2.Publish
diff --git a/src/FSharp.Core/fslib-extra-pervasives.fs b/src/FSharp.Core/fslib-extra-pervasives.fs
index 2c0c462a9bc..f7699ce66be 100644
--- a/src/FSharp.Core/fslib-extra-pervasives.fs
+++ b/src/FSharp.Core/fslib-extra-pervasives.fs
@@ -16,112 +16,137 @@ module ExtraTopLevelOperators =
     open Microsoft.FSharp.Primitives.Basics
     open Microsoft.FSharp.Core.CompilerServices
 
-    let inline checkNonNullNullArg argName arg = 
-        match box arg with 
-        | null -> nullArg argName 
+    let inline checkNonNullNullArg argName arg =
+        match box arg with
+        | null -> nullArg argName
         | _ -> ()
 
-    let inline checkNonNullInvalidArg argName message arg = 
-        match box arg with 
+    let inline checkNonNullInvalidArg argName message arg =
+        match box arg with
         | null -> invalidArg argName message
         | _ -> ()
 
     []
-    let set elements = Collections.Set.ofSeq elements
+    let set elements =
+        Collections.Set.ofSeq elements
 
     let dummyArray = [||]
-    let inline dont_tail_call f = 
+
+    let inline dont_tail_call f =
         let result = f ()
         dummyArray.Length |> ignore // pretty stupid way to avoid tail call, would be better if attribute existed, but this should be inlineable by the JIT
         result
 
-    let inline ICollection_Contains<'collection,'item when 'collection :> ICollection<'item>> (collection:'collection) (item:'item) =
+    let inline ICollection_Contains<'collection, 'item when 'collection :> ICollection<'item>> (collection: 'collection) (item: 'item) =
         collection.Contains item
 
     []
-    [>)>]
-    type DictImpl<'SafeKey,'Key,'T>(t : Dictionary<'SafeKey,'T>, makeSafeKey : 'Key->'SafeKey, getKey : 'SafeKey->'Key) =
+    [>)>]
+    type DictImpl<'SafeKey, 'Key, 'T>(t: Dictionary<'SafeKey, 'T>, makeSafeKey: 'Key -> 'SafeKey, getKey: 'SafeKey -> 'Key) =
 #if NETSTANDARD
-        static let emptyEnumerator = (Array.empty> :> seq<_>).GetEnumerator()
+        static let emptyEnumerator =
+            (Array.empty> :> seq<_>).GetEnumerator()
 #endif
         member _.Count = t.Count
 
         // Give a read-only view of the dictionary
         interface IDictionary<'Key, 'T> with
-            member _.Item 
+            member _.Item
                 with get x = dont_tail_call (fun () -> t.[makeSafeKey x])
-                and  set _ _ = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+                and set _ _ = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-            member _.Keys = 
+            member _.Keys =
                 let keys = t.Keys
-                { new ICollection<'Key> with 
-                      member _.Add(x) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-                      member _.Clear() = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+                { new ICollection<'Key> with
+                    member _.Add(x) =
+                        raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-                      member _.Remove(x) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+                    member _.Clear() =
+                        raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-                      member _.Contains(x) = t.ContainsKey (makeSafeKey x)
+                    member _.Remove(x) =
+                        raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-                      member _.CopyTo(arr,i) =
-                          let mutable n = 0 
-                          for k in keys do 
-                              arr.[i+n] <- getKey k
-                              n <- n + 1
+                    member _.Contains(x) =
+                        t.ContainsKey(makeSafeKey x)
 
-                      member _.IsReadOnly = true
+                    member _.CopyTo(arr, i) =
+                        let mutable n = 0
 
-                      member _.Count = keys.Count
+                        for k in keys do
+                            arr.[i + n] <- getKey k
+                            n <- n + 1
 
-                  interface IEnumerable<'Key> with
-                        member _.GetEnumerator() = (keys |> Seq.map getKey).GetEnumerator()
+                    member _.IsReadOnly = true
 
+                    member _.Count = keys.Count
+                  interface IEnumerable<'Key> with
+                      member _.GetEnumerator() =
+                          (keys |> Seq.map getKey).GetEnumerator()
                   interface System.Collections.IEnumerable with
-                        member _.GetEnumerator() = ((keys |> Seq.map getKey) :> System.Collections.IEnumerable).GetEnumerator() }
-                
+                      member _.GetEnumerator() =
+                          ((keys |> Seq.map getKey) :> System.Collections.IEnumerable).GetEnumerator()
+                }
+
             member _.Values = upcast t.Values
 
-            member _.Add(_,_) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+            member _.Add(_, _) =
+                raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-            member _.ContainsKey(k) = dont_tail_call (fun () -> t.ContainsKey(makeSafeKey k))
+            member _.ContainsKey(k) =
+                dont_tail_call (fun () -> t.ContainsKey(makeSafeKey k))
 
-            member _.TryGetValue(k,r) = 
+            member _.TryGetValue(k, r) =
                 let safeKey = makeSafeKey k
-                if t.ContainsKey(safeKey) then (r <- t.[safeKey]; true) else false
 
-            member _.Remove(_ : 'Key) = (raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))) : bool) 
+                if t.ContainsKey(safeKey) then
+                    (r <- t.[safeKey]
+                     true)
+                else
+                    false
+
+            member _.Remove(_: 'Key) =
+                (raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))): bool)
 
         interface IReadOnlyDictionary<'Key, 'T> with
 
-            member _.Item with get key = t.[makeSafeKey key]
+            member _.Item
+                with get key = t.[makeSafeKey key]
 
             member _.Keys = t.Keys |> Seq.map getKey
 
             member _.TryGetValue(key, r) =
-                match t.TryGetValue (makeSafeKey key) with
+                match t.TryGetValue(makeSafeKey key) with
                 | false, _ -> false
                 | true, value ->
                     r <- value
                     true
 
-            member _.Values = (t :> IReadOnlyDictionary<_,_>).Values
+            member _.Values = (t :> IReadOnlyDictionary<_, _>).Values
+
+            member _.ContainsKey k =
+                t.ContainsKey(makeSafeKey k)
 
-            member _.ContainsKey k = t.ContainsKey (makeSafeKey k)
+        interface ICollection> with
 
-        interface ICollection> with 
+            member _.Add(_) =
+                raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-            member _.Add(_) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+            member _.Clear() =
+                raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-            member _.Clear() = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+            member _.Remove(_) =
+                raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
 
-            member _.Remove(_) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated)))
+            member _.Contains(KeyValue (k, v)) =
+                ICollection_Contains t (KeyValuePair<_, _>(makeSafeKey k, v))
 
-            member _.Contains(KeyValue(k,v)) = ICollection_Contains t (KeyValuePair<_,_>(makeSafeKey k,v))
+            member _.CopyTo(arr, i) =
+                let mutable n = 0
 
-            member _.CopyTo(arr,i) = 
-                let mutable n = 0 
-                for (KeyValue(k,v)) in t do 
-                    arr.[i+n] <- KeyValuePair<_,_>(getKey k,v)
+                for (KeyValue (k, v)) in t do
+                    arr.[i + n] <- KeyValuePair<_, _>(getKey k, v)
                     n <- n + 1
 
             member _.IsReadOnly = true
@@ -135,104 +160,129 @@ module ExtraTopLevelOperators =
 
             member _.GetEnumerator() =
                 // We use an array comprehension here instead of seq {} as otherwise we get incorrect
-                // IEnumerator.Reset() and IEnumerator.Current semantics. 
+                // IEnumerator.Reset() and IEnumerator.Current semantics.
                 // Coreclr has a bug with SZGenericEnumerators --- implement a correct enumerator.  On desktop use the desktop implementation because it's ngened.
-#if !NETSTANDARD               
-                let kvps = [| for (KeyValue (k,v)) in t -> KeyValuePair (getKey k, v) |] :> seq<_>
+#if !NETSTANDARD
+                let kvps = [| for (KeyValue (k, v)) in t -> KeyValuePair(getKey k, v) |] :> seq<_>
                 kvps.GetEnumerator()
 #else
                 let endIndex = t.Count
-                if endIndex = 0 then emptyEnumerator
+
+                if endIndex = 0 then
+                    emptyEnumerator
                 else
-                    let kvps = [| for (KeyValue (k,v)) in t -> KeyValuePair (getKey k, v) |]
+                    let kvps = [| for (KeyValue (k, v)) in t -> KeyValuePair(getKey k, v) |]
                     let mutable index = -1
+
                     let current () =
-                        if index < 0 then raise <| InvalidOperationException(SR.GetString(SR.enumerationNotStarted)) 
-                        if index >= endIndex then  raise <| InvalidOperationException(SR.GetString(SR.enumerationAlreadyFinished)) 
+                        if index < 0 then
+                            raise <| InvalidOperationException(SR.GetString(SR.enumerationNotStarted))
+
+                        if index >= endIndex then
+                            raise <| InvalidOperationException(SR.GetString(SR.enumerationAlreadyFinished))
+
                         kvps.[index]
 
-                    {new IEnumerator<_> with
+                    { new IEnumerator<_> with
                         member _.Current = current ()
-
                       interface System.Collections.IEnumerator with
-                            member _.Current = box(current())
-
-                            member _.MoveNext() =
-                                if index < endIndex then
-                                    index <- index + 1
-                                    index < endIndex
-                                else false
-
-                            member _.Reset() = index <- -1
-
-                      interface System.IDisposable with 
-                            member _.Dispose() = () }
+                          member _.Current = box (current ())
+
+                          member _.MoveNext() =
+                              if index < endIndex then
+                                  index <- index + 1
+                                  index < endIndex
+                              else
+                                  false
+
+                          member _.Reset() =
+                              index <- -1
+                      interface System.IDisposable with
+                          member _.Dispose() =
+                              ()
+                    }
 #endif
 
         interface System.Collections.IEnumerable with
             member _.GetEnumerator() =
                 // We use an array comprehension here instead of seq {} as otherwise we get incorrect
-                // IEnumerator.Reset() and IEnumerator.Current semantics. 
-                let kvps = [| for (KeyValue (k,v)) in t -> KeyValuePair (getKey k, v) |] :> System.Collections.IEnumerable
+                // IEnumerator.Reset() and IEnumerator.Current semantics.
+                let kvps =
+                    [| for (KeyValue (k, v)) in t -> KeyValuePair(getKey k, v) |] :> System.Collections.IEnumerable
+
                 kvps.GetEnumerator()
 
-    and DictDebugView<'SafeKey,'Key,'T>(d:DictImpl<'SafeKey,'Key,'T>) =
+    and DictDebugView<'SafeKey, 'Key, 'T>(d: DictImpl<'SafeKey, 'Key, 'T>) =
         []
         member _.Items = Array.ofSeq d
 
-    let inline dictImpl (comparer:IEqualityComparer<'SafeKey>) (makeSafeKey : 'Key->'SafeKey) (getKey : 'SafeKey->'Key) (l:seq<'Key*'T>) =
+    let inline dictImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        (makeSafeKey: 'Key -> 'SafeKey)
+        (getKey: 'SafeKey -> 'Key)
+        (l: seq<'Key * 'T>)
+        =
         let t = Dictionary comparer
-        for (k,v) in l do
+
+        for (k, v) in l do
             t.[makeSafeKey k] <- v
+
         DictImpl(t, makeSafeKey, getKey)
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
-    let dictValueType (l:seq<'Key*'T>) =
+    let dictValueType (l: seq<'Key * 'T>) =
         dictImpl HashIdentity.Structural<'Key> id id l
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
-    let dictRefType (l:seq<'Key*'T>) =
+    let dictRefType (l: seq<'Key * 'T>) =
         dictImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun k -> RuntimeHelpers.StructBox k) (fun sb -> sb.Value) l
 
     []
-    let dict (keyValuePairs:seq<'Key*'T>) : IDictionary<'Key,'T> =
+    let dict (keyValuePairs: seq<'Key * 'T>) : IDictionary<'Key, 'T> =
         if typeof<'Key>.IsValueType then
             dictValueType keyValuePairs
         else
             dictRefType keyValuePairs
 
     []
-    let readOnlyDict (keyValuePairs:seq<'Key*'T>) : IReadOnlyDictionary<'Key,'T> =
+    let readOnlyDict (keyValuePairs: seq<'Key * 'T>) : IReadOnlyDictionary<'Key, 'T> =
         if typeof<'Key>.IsValueType then
             dictValueType keyValuePairs
         else
             dictRefType keyValuePairs
 
-    let getArray (vals : seq<'T>) = 
+    let getArray (vals: seq<'T>) =
         match vals with
         | :? ('T[]) as arr -> arr
         | _ -> Seq.toArray vals
 
     []
-    let array2D (rows : seq<#seq<'T>>) = 
+    let array2D (rows: seq<#seq<'T>>) =
         checkNonNullNullArg "rows" rows
         let rowsArr = getArray rows
         let m = rowsArr.Length
-        if m = 0 
-        then Array2D.zeroCreate<'T> 0 0 
+
+        if m = 0 then
+            Array2D.zeroCreate<'T> 0 0
         else
             checkNonNullInvalidArg "rows" (SR.GetString(SR.nullsNotAllowedInArray)) rowsArr.[0]
             let firstRowArr = getArray rowsArr.[0]
             let n = firstRowArr.Length
             let res = Array2D.zeroCreate<'T> m n
-            for j in 0..(n-1) do    
-                res.[0,j] <- firstRowArr.[j]
-            for i in 1..(m-1) do
+
+            for j in 0 .. (n - 1) do
+                res.[0, j] <- firstRowArr.[j]
+
+            for i in 1 .. (m - 1) do
                 checkNonNullInvalidArg "rows" (SR.GetString(SR.nullsNotAllowedInArray)) rowsArr.[i]
                 let rowiArr = getArray rowsArr.[i]
-                if rowiArr.Length <> n then invalidArg "vals" (SR.GetString(SR.arraysHadDifferentLengths))
-                for j in 0..(n-1) do
-                    res.[i,j] <- rowiArr.[j]
+
+                if rowiArr.Length <> n then
+                    invalidArg "vals" (SR.GetString(SR.arraysHadDifferentLengths))
+
+                for j in 0 .. (n - 1) do
+                    res.[i, j] <- rowiArr.[j]
+
             res
 
     []
@@ -244,28 +294,28 @@ module ExtraTopLevelOperators =
         Printf.failwithf format
 
     []
-    let fprintf (textWriter:TextWriter) format =
-        Printf.fprintf textWriter  format 
+    let fprintf (textWriter: TextWriter) format =
+        Printf.fprintf textWriter format
 
     []
-    let fprintfn (textWriter:TextWriter) format =
-        Printf.fprintfn textWriter format 
-    
+    let fprintfn (textWriter: TextWriter) format =
+        Printf.fprintfn textWriter format
+
     []
     let printf format =
-        Printf.printf format 
+        Printf.printf format
 
     []
     let eprintf format =
-        Printf.eprintf format 
+        Printf.eprintf format
 
     []
     let printfn format =
-        Printf.printfn format 
+        Printf.printfn format
 
     []
     let eprintfn format =
-        Printf.eprintfn format 
+        Printf.eprintfn format
 
     []
     let failwith s =
@@ -275,167 +325,204 @@ module ExtraTopLevelOperators =
     let async = AsyncBuilder()
 
     []
-    let inline single value = float32 value
+    let inline single value =
+        float32 value
 
     []
-    let inline double value = float value
+    let inline double value =
+        float value
 
     []
-    let inline uint8 value = byte value
+    let inline uint8 value =
+        byte value
 
     []
-    let inline int8 value = sbyte value
+    let inline int8 value =
+        sbyte value
 
-    module Checked = 
+    module Checked =
 
         []
-        let inline uint8 value = Checked.byte value
+        let inline uint8 value =
+            Checked.byte value
 
         []
-        let inline int8 value = Checked.sbyte value
+        let inline int8 value =
+            Checked.sbyte value
 
     []
-    let (~%) (expression:Microsoft.FSharp.Quotations.Expr<'T>) : 'T =
+    let (~%) (expression: Microsoft.FSharp.Quotations.Expr<'T>) : 'T =
         ignore expression
         raise (InvalidOperationException(SR.GetString(SR.firstClassUsesOfSplice)))
 
     []
     let (~%%) (expression: Microsoft.FSharp.Quotations.Expr) : 'T =
         ignore expression
-        raise (InvalidOperationException (SR.GetString(SR.firstClassUsesOfSplice)))
+        raise (InvalidOperationException(SR.GetString(SR.firstClassUsesOfSplice)))
 
     []
     []
     []
     []
     []
-    #if !BUILDING_WITH_LKG && !BUILD_FROM_SOURCE
+#if !BUILDING_WITH_LKG && !BUILD_FROM_SOURCE
     []
     []
     []
-    #endif
+#endif
     []
     []
-    do()
+    do ()
 
     []
-    let (|Lazy|) (input:Lazy<_>) =
+    let (|Lazy|) (input: Lazy<_>) =
         input.Force()
 
     let query = Microsoft.FSharp.Linq.QueryBuilder()
 
-
 namespace Microsoft.FSharp.Core.CompilerServices
 
-    open System
-    open System.Reflection
-    open Microsoft.FSharp.Core
-    open Microsoft.FSharp.Control
-    open Microsoft.FSharp.Quotations
-
-    /// Represents the product of two measure expressions when returned as a generic argument of a provided type.
-    []
-    type MeasureProduct<'Measure1, 'Measure2>() = class end
-
-    /// Represents the inverse of a measure expressions when returned as a generic argument of a provided type.
-    []
-    type MeasureInverse<'Measure>  = class end
-
-    /// Represents the '1' measure expression when returned as a generic argument of a provided type.
-    []
-    type MeasureOne  = class end
-
-    []
-    type TypeProviderAttribute() =
-        inherit System.Attribute()
+open System
+open System.Reflection
+open Microsoft.FSharp.Core
+open Microsoft.FSharp.Control
+open Microsoft.FSharp.Quotations
+
+/// Represents the product of two measure expressions when returned as a generic argument of a provided type.
+[]
+type MeasureProduct<'Measure1, 'Measure2>() =
+    class
+    end
+
+/// Represents the inverse of a measure expressions when returned as a generic argument of a provided type.
+[]
+type MeasureInverse<'Measure> =
+    class
+    end
+
+/// Represents the '1' measure expression when returned as a generic argument of a provided type.
+[]
+type MeasureOne =
+    class
+    end
+
+[]
+type TypeProviderAttribute() =
+    inherit System.Attribute()
 
-    []
-    type TypeProviderAssemblyAttribute(assemblyName : string) = 
-        inherit System.Attribute()
-        new () = TypeProviderAssemblyAttribute(null)
+[]
+type TypeProviderAssemblyAttribute(assemblyName: string) =
+    inherit System.Attribute()
+    new() = TypeProviderAssemblyAttribute(null)
 
-        member _.AssemblyName = assemblyName
+    member _.AssemblyName = assemblyName
 
-    []
-    type TypeProviderXmlDocAttribute(commentText: string) = 
-        inherit System.Attribute()
+[]
+type TypeProviderXmlDocAttribute(commentText: string) =
+    inherit System.Attribute()
 
-        member _.CommentText = commentText
+    member _.CommentText = commentText
 
-    []
-    type TypeProviderDefinitionLocationAttribute() = 
-        inherit System.Attribute()
-        let mutable filePath : string = null
-        let mutable line : int = 0
-        let mutable column : int = 0
+[]
+type TypeProviderDefinitionLocationAttribute() =
+    inherit System.Attribute()
+    let mutable filePath: string = null
+    let mutable line: int = 0
+    let mutable column: int = 0
 
-        member _.FilePath with get() = filePath and set v = filePath <- v
+    member _.FilePath
+        with get () = filePath
+        and set v = filePath <- v
 
-        member _.Line with get() = line and set v = line <- v
+    member _.Line
+        with get () = line
+        and set v = line <- v
 
-        member _.Column with get() = column and set v = column <- v
+    member _.Column
+        with get () = column
+        and set v = column <- v
 
-    []
-    type TypeProviderEditorHideMethodsAttribute() = 
-        inherit System.Attribute()
+[]
+type TypeProviderEditorHideMethodsAttribute() =
+    inherit System.Attribute()
 
-    /// Additional type attribute flags related to provided types
-    type TypeProviderTypeAttributes =
-        | SuppressRelocate = 0x80000000
-        | IsErased = 0x40000000
+/// Additional type attribute flags related to provided types
+type TypeProviderTypeAttributes =
+    | SuppressRelocate = 0x80000000
+    | IsErased = 0x40000000
 
-    type TypeProviderConfig( systemRuntimeContainsType : string -> bool ) =
-        let mutable resolutionFolder: string   = null  
-        let mutable runtimeAssembly: string   = null  
-        let mutable referencedAssemblies: string[] = null  
-        let mutable temporaryFolder: string   = null  
-        let mutable isInvalidationSupported: bool   = false 
-        let mutable useResolutionFolderAtRuntime: bool = false
-        let mutable systemRuntimeAssemblyVersion: System.Version = null
+type TypeProviderConfig(systemRuntimeContainsType: string -> bool) =
+    let mutable resolutionFolder: string = null
+    let mutable runtimeAssembly: string = null
+    let mutable referencedAssemblies: string[] = null
+    let mutable temporaryFolder: string = null
+    let mutable isInvalidationSupported: bool = false
+    let mutable useResolutionFolderAtRuntime: bool = false
+    let mutable systemRuntimeAssemblyVersion: System.Version = null
 
-        member _.ResolutionFolder with get() = resolutionFolder and set v = resolutionFolder <- v
+    member _.ResolutionFolder
+        with get () = resolutionFolder
+        and set v = resolutionFolder <- v
 
-        member _.RuntimeAssembly with get() = runtimeAssembly and set v = runtimeAssembly <- v
+    member _.RuntimeAssembly
+        with get () = runtimeAssembly
+        and set v = runtimeAssembly <- v
 
-        member _.ReferencedAssemblies with get() = referencedAssemblies and set v = referencedAssemblies <- v
+    member _.ReferencedAssemblies
+        with get () = referencedAssemblies
+        and set v = referencedAssemblies <- v
 
-        member _.TemporaryFolder with get() = temporaryFolder and set v = temporaryFolder <- v
+    member _.TemporaryFolder
+        with get () = temporaryFolder
+        and set v = temporaryFolder <- v
 
-        member _.IsInvalidationSupported with get() = isInvalidationSupported and set v = isInvalidationSupported <- v
+    member _.IsInvalidationSupported
+        with get () = isInvalidationSupported
+        and set v = isInvalidationSupported <- v
 
-        member _.IsHostedExecution with get() = useResolutionFolderAtRuntime and set v = useResolutionFolderAtRuntime <- v
+    member _.IsHostedExecution
+        with get () = useResolutionFolderAtRuntime
+        and set v = useResolutionFolderAtRuntime <- v
 
-        member _.SystemRuntimeAssemblyVersion with get() = systemRuntimeAssemblyVersion and set v = systemRuntimeAssemblyVersion <- v
+    member _.SystemRuntimeAssemblyVersion
+        with get () = systemRuntimeAssemblyVersion
+        and set v = systemRuntimeAssemblyVersion <- v
 
-        member _.SystemRuntimeContainsType (typeName: string) = systemRuntimeContainsType typeName
+    member _.SystemRuntimeContainsType(typeName: string) =
+        systemRuntimeContainsType typeName
 
-    type IProvidedNamespace =
+type IProvidedNamespace =
 
-        abstract NamespaceName: string
+    abstract NamespaceName: string
 
-        abstract GetNestedNamespaces: unit -> IProvidedNamespace[] 
+    abstract GetNestedNamespaces: unit -> IProvidedNamespace[]
 
-        abstract GetTypes: unit -> Type[] 
+    abstract GetTypes: unit -> Type[]
 
-        abstract ResolveTypeName: typeName: string -> Type
+    abstract ResolveTypeName: typeName: string -> Type
 
-    type ITypeProvider =
-        inherit System.IDisposable
+type ITypeProvider =
+    inherit System.IDisposable
 
-        abstract GetNamespaces: unit -> IProvidedNamespace[] 
+    abstract GetNamespaces: unit -> IProvidedNamespace[]
 
-        abstract GetStaticParameters: typeWithoutArguments: Type -> ParameterInfo[]
+    abstract GetStaticParameters: typeWithoutArguments: Type -> ParameterInfo[]
 
-        abstract ApplyStaticArguments: typeWithoutArguments: Type * typePathWithArguments: string[] * staticArguments:obj[] -> Type 
+    abstract ApplyStaticArguments: typeWithoutArguments: Type * typePathWithArguments: string[] * staticArguments: obj[] -> Type
 
-        abstract GetInvokerExpression: syntheticMethodBase:MethodBase * parameters:Expr[] -> Expr
+    abstract GetInvokerExpression: syntheticMethodBase: MethodBase * parameters: Expr[] -> Expr
 
-        []
-        abstract Invalidate : IEvent
-        abstract GetGeneratedAssemblyContents: assembly:System.Reflection.Assembly -> byte[]
+    []
+    abstract Invalidate: IEvent
 
-    type ITypeProvider2 =
-        abstract GetStaticParametersForMethod: methodWithoutArguments:MethodBase -> ParameterInfo[] 
+    abstract GetGeneratedAssemblyContents: assembly: System.Reflection.Assembly -> byte[]
 
-        abstract ApplyStaticArgumentsForMethod: methodWithoutArguments:MethodBase * methodNameWithArguments:string * staticArguments:obj[] -> MethodBase
+type ITypeProvider2 =
+    abstract GetStaticParametersForMethod: methodWithoutArguments: MethodBase -> ParameterInfo[]
 
+    abstract ApplyStaticArgumentsForMethod:
+        methodWithoutArguments: MethodBase * methodNameWithArguments: string * staticArguments: obj[] -> MethodBase
diff --git a/src/FSharp.Core/list.fs b/src/FSharp.Core/list.fs
index cd8b7ae3ad1..bbefce3003f 100644
--- a/src/FSharp.Core/list.fs
+++ b/src/FSharp.Core/list.fs
@@ -15,13 +15,14 @@ open System.Collections.Generic
 module List =
 
     let inline checkNonNull argName arg =
-        if isNull arg then
-            nullArg argName
+        if isNull arg then nullArg argName
 
-    let inline indexNotFound() = raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
+    let inline indexNotFound () =
+        raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
 
     []
-    let length (list: 'T list) = list.Length
+    let length (list: 'T list) =
+        list.Length
 
     []
     let last (list: 'T list) =
@@ -33,170 +34,236 @@ module List =
     let rec tryLast (list: 'T list) =
         match Microsoft.FSharp.Primitives.Basics.List.tryLastV list with
         | ValueSome x -> Some x
-        | ValueNone -> None            
+        | ValueNone -> None
 
     []
-    let rev list = Microsoft.FSharp.Primitives.Basics.List.rev list
+    let rev list =
+        Microsoft.FSharp.Primitives.Basics.List.rev list
 
     []
-    let concat lists = Microsoft.FSharp.Primitives.Basics.List.concat lists
-
-    let inline countByImpl (comparer:IEqualityComparer<'SafeKey>) ([] projection: 'T->'SafeKey) ([] getKey:'SafeKey->'Key) (list: 'T list) =            
+    let concat lists =
+        Microsoft.FSharp.Primitives.Basics.List.concat lists
+
+    let inline countByImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        ([] projection: 'T -> 'SafeKey)
+        ([] getKey: 'SafeKey -> 'Key)
+        (list: 'T list)
+        =
         let dict = Dictionary comparer
-        let rec loop srcList  =
+
+        let rec loop srcList =
             match srcList with
             | [] -> ()
             | h :: t ->
                 let safeKey = projection h
                 let mutable prev = 0
-                if dict.TryGetValue(safeKey, &prev) then dict.[safeKey] <- prev + 1 else dict.[safeKey] <- 1
+
+                if dict.TryGetValue(safeKey, &prev) then
+                    dict.[safeKey] <- prev + 1
+                else
+                    dict.[safeKey] <- 1
+
                 loop t
+
         loop list
         Microsoft.FSharp.Primitives.Basics.List.countBy dict getKey
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
-    let countByValueType (projection: 'T->'Key) (list: 'T list) = countByImpl HashIdentity.Structural<'Key> projection id list
+    let countByValueType (projection: 'T -> 'Key) (list: 'T list) =
+        countByImpl HashIdentity.Structural<'Key> projection id list
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
-    let countByRefType   (projection: 'T->'Key) (list: 'T list) = countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox (projection t)) (fun sb -> sb.Value) list
+    let countByRefType (projection: 'T -> 'Key) (list: 'T list) =
+        countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(projection t)) (fun sb -> sb.Value) list
 
     []
-    let countBy (projection: 'T->'Key) (list: 'T list) =
+    let countBy (projection: 'T -> 'Key) (list: 'T list) =
         match list with
         | [] -> []
         | _ ->
-            if typeof<'Key>.IsValueType
-                then countByValueType projection list
-                else countByRefType   projection list
+            if typeof<'Key>.IsValueType then
+                countByValueType projection list
+            else
+                countByRefType projection list
 
     []
-    let map mapping list = Microsoft.FSharp.Primitives.Basics.List.map mapping list
+    let map mapping list =
+        Microsoft.FSharp.Primitives.Basics.List.map mapping list
 
     []
-    let mapi mapping list = Microsoft.FSharp.Primitives.Basics.List.mapi mapping list
+    let mapi mapping list =
+        Microsoft.FSharp.Primitives.Basics.List.mapi mapping list
 
     []
-    let indexed list = Microsoft.FSharp.Primitives.Basics.List.indexed list
+    let indexed list =
+        Microsoft.FSharp.Primitives.Basics.List.indexed list
 
     []
-    let mapFold<'T, 'State, 'Result> (mapping:'State -> 'T -> 'Result * 'State) state list =
+    let mapFold<'T, 'State, 'Result> (mapping: 'State -> 'T -> 'Result * 'State) state list =
         Microsoft.FSharp.Primitives.Basics.List.mapFold mapping state list
 
     []
     let mapFoldBack<'T, 'State, 'Result> (mapping: 'T -> 'State -> 'Result * 'State) list state =
         match list with
         | [] -> [], state
-        | [h] -> let h', s' = mapping h state in [h'], s'
+        | [ h ] -> let h', s' = mapping h state in [ h' ], s'
         | _ ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(mapping)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (mapping)
+
             let rec loop res list =
                 match list, res with
                 | [], _ -> res
                 | h :: t, (list', acc') ->
                     let h', s' = f.Invoke(h, acc')
                     loop (h' :: list', s') t
+
             loop ([], state) (rev list)
 
     []
-    let inline iter ([] action) (list: 'T list) = for x in list do action x
+    let inline iter ([] action) (list: 'T list) =
+        for x in list do
+            action x
 
     []
-    let distinct (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.distinctWithComparer HashIdentity.Structural<'T> list
+    let distinct (list: 'T list) =
+        Microsoft.FSharp.Primitives.Basics.List.distinctWithComparer HashIdentity.Structural<'T> list
 
     []
-    let distinctBy projection (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.distinctByWithComparer HashIdentity.Structural<_> projection list
+    let distinctBy projection (list: 'T list) =
+        Microsoft.FSharp.Primitives.Basics.List.distinctByWithComparer HashIdentity.Structural<_> projection list
 
     []
-    let ofArray (array: 'T array) = Microsoft.FSharp.Primitives.Basics.List.ofArray array
+    let ofArray (array: 'T array) =
+        Microsoft.FSharp.Primitives.Basics.List.ofArray array
 
     []
-    let toArray (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.toArray list
+    let toArray (list: 'T list) =
+        Microsoft.FSharp.Primitives.Basics.List.toArray list
 
     []
-    let empty<'T> = ([ ] : 'T list)
+    let empty<'T> = ([]: 'T list)
 
     []
-    let head list = match list with x :: _ -> x | [] -> invalidArg "list" (SR.GetString(SR.inputListWasEmpty))
+    let head list =
+        match list with
+        | x :: _ -> x
+        | [] -> invalidArg "list" (SR.GetString(SR.inputListWasEmpty))
 
     []
-    let tryHead list = match list with x :: _ -> Some x | [] -> None
+    let tryHead list =
+        match list with
+        | x :: _ -> Some x
+        | [] -> None
 
     []
-    let tail list = match list with _ :: t -> t | [] -> invalidArg "list" (SR.GetString(SR.inputListWasEmpty))
+    let tail list =
+        match list with
+        | _ :: t -> t
+        | [] -> invalidArg "list" (SR.GetString(SR.inputListWasEmpty))
 
     []
-    let isEmpty list = match list with [] -> true | _ -> false
+    let isEmpty list =
+        match list with
+        | [] -> true
+        | _ -> false
 
     []
-    let append list1 list2 = list1 @ list2
+    let append list1 list2 =
+        list1 @ list2
 
     []
     let rec item index list =
         match list with
         | h :: t when index >= 0 ->
-            if index = 0 then h else item (index - 1) t
-        | _ ->
-            invalidArg "index" (SR.GetString(SR.indexOutOfBounds))
+            if index = 0 then
+                h
+            else
+                item (index - 1) t
+        | _ -> invalidArg "index" (SR.GetString(SR.indexOutOfBounds))
 
     []
     let rec tryItem index list =
         match list with
         | h :: t when index >= 0 ->
-            if index = 0 then Some h else tryItem (index - 1) t
-        | _ ->
-            None
+            if index = 0 then
+                Some h
+            else
+                tryItem (index - 1) t
+        | _ -> None
 
     []
-    let nth list index = item index list
+    let nth list index =
+        item index list
 
     []
-    let choose chooser list = Microsoft.FSharp.Primitives.Basics.List.choose chooser list
+    let choose chooser list =
+        Microsoft.FSharp.Primitives.Basics.List.choose chooser list
 
     []
-    let splitAt index (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.splitAt index list
+    let splitAt index (list: 'T list) =
+        Microsoft.FSharp.Primitives.Basics.List.splitAt index list
 
     []
-    let take count (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.take count list
+    let take count (list: 'T list) =
+        Microsoft.FSharp.Primitives.Basics.List.take count list
 
     []
-    let takeWhile predicate (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.takeWhile predicate list
+    let takeWhile predicate (list: 'T list) =
+        Microsoft.FSharp.Primitives.Basics.List.takeWhile predicate list
 
     []
     let inline iteri ([] action) (list: 'T list) =
         let mutable n = 0
-        for x in list do action n x; n <- n + 1
+
+        for x in list do
+            action n x
+            n <- n + 1
 
     []
-    let init length initializer = Microsoft.FSharp.Primitives.Basics.List.init length initializer
+    let init length initializer =
+        Microsoft.FSharp.Primitives.Basics.List.init length initializer
 
     []
     let replicate count initial =
-        if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
+        if count < 0 then
+            invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
+
         let mutable result = []
-        for i in 0..count-1 do
-           result <- initial :: result
+
+        for i in 0 .. count - 1 do
+            result <- initial :: result
+
         result
 
     []
     let iter2 action list1 list2 =
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (action)
+
         let rec loop list1 list2 =
             match list1, list2 with
             | [], [] -> ()
-            | h1 :: t1, h2 :: t2 -> f.Invoke(h1, h2); loop t1 t2
+            | h1 :: t1, h2 :: t2 ->
+                f.Invoke(h1, h2)
+                loop t1 t2
             | [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
             | xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
+
         loop list1 list2
 
     []
     let iteri2 action list1 list2 =
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(action)
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (action)
+
         let rec loop n list1 list2 =
             match list1, list2 with
             | [], [] -> ()
-            | h1 :: t1, h2 :: t2 -> f.Invoke(n, h1, h2); loop (n+1) t1 t2
+            | h1 :: t1, h2 :: t2 ->
+                f.Invoke(n, h1, h2)
+                loop (n + 1) t1 t2
             | [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
             | xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
+
         loop 0 list1 list2
 
     []
@@ -208,17 +275,20 @@ module List =
         Microsoft.FSharp.Primitives.Basics.List.mapi2 mapping list1 list2
 
     []
-    let map2 mapping list1 list2 = Microsoft.FSharp.Primitives.Basics.List.map2 mapping list1 list2
+    let map2 mapping list1 list2 =
+        Microsoft.FSharp.Primitives.Basics.List.map2 mapping list1 list2
 
     []
-    let fold<'T, 'State> folder (state:'State) (list: 'T list) =
+    let fold<'T, 'State> folder (state: 'State) (list: 'T list) =
         match list with
         | [] -> state
         | _ ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(folder)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (folder)
             let mutable acc = state
+
             for x in list do
                 acc <- f.Invoke(acc, x)
+
             acc
 
     []
@@ -232,39 +302,45 @@ module List =
         | h :: t -> fold reduction h t
 
     []
-    let scan<'T, 'State> folder (state:'State) (list: 'T list) =
+    let scan<'T, 'State> folder (state: 'State) (list: 'T list) =
         Microsoft.FSharp.Primitives.Basics.List.scan folder state list
 
     []
-    let inline singleton value = [value]
+    let inline singleton value =
+        [ value ]
 
     []
-    let fold2<'T1, 'T2, 'State> folder (state:'State) (list1:'T1 list) (list2:'T2 list) =
-        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(folder)
+    let fold2<'T1, 'T2, 'State> folder (state: 'State) (list1: 'T1 list) (list2: 'T2 list) =
+        let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (folder)
+
         let rec loop acc list1 list2 =
             match list1, list2 with
             | [], [] -> acc
             | h1 :: t1, h2 :: t2 -> loop (f.Invoke(acc, h1, h2)) t1 t2
             | [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
             | xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
+
         loop state list1 list2
 
-    let foldArraySubRight (f:OptimizedClosures.FSharpFunc<'T, _, _>) (arr: 'T[]) start fin acc =
+    let foldArraySubRight (f: OptimizedClosures.FSharpFunc<'T, _, _>) (arr: 'T[]) start fin acc =
         let mutable state = acc
+
         for i = fin downto start do
             state <- f.Invoke(arr.[i], state)
+
         state
 
     // this version doesn't causes stack overflow - it uses a private stack
     []
-    let foldBack<'T, 'State> folder (list: 'T list) (state:'State) =
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(folder)
+    let foldBack<'T, 'State> folder (list: 'T list) (state: 'State) =
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (folder)
+
         match list with
         | [] -> state
-        | [h] -> f.Invoke(h, state)
-        | [h1; h2] -> f.Invoke(h1, f.Invoke(h2, state))
-        | [h1; h2; h3] -> f.Invoke(h1, f.Invoke(h2, f.Invoke(h3, state)))
-        | [h1; h2; h3; h4] -> f.Invoke(h1, f.Invoke(h2, f.Invoke(h3, f.Invoke(h4, state))))
+        | [ h ] -> f.Invoke(h, state)
+        | [ h1; h2 ] -> f.Invoke(h1, f.Invoke(h2, state))
+        | [ h1; h2; h3 ] -> f.Invoke(h1, f.Invoke(h2, f.Invoke(h3, state)))
+        | [ h1; h2; h3; h4 ] -> f.Invoke(h1, f.Invoke(h2, f.Invoke(h3, f.Invoke(h4, state))))
         | _ ->
             // It is faster to allocate and iterate an array than to create all those
             // highly nested stacks.  It also means we won't get stack overflows here.
@@ -277,66 +353,73 @@ module List =
         match list with
         | [] -> invalidArg "list" (SR.GetString(SR.inputListWasEmpty))
         | _ ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(reduction)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (reduction)
             let arr = toArray list
             let arrn = arr.Length
             foldArraySubRight f arr 0 (arrn - 2) arr.[arrn - 1]
 
-    let scanArraySubRight<'T, 'State> (f:OptimizedClosures.FSharpFunc<'T, 'State, 'State>) (arr: _[]) start fin initState =
+    let scanArraySubRight<'T, 'State> (f: OptimizedClosures.FSharpFunc<'T, 'State, 'State>) (arr: _[]) start fin initState =
         let mutable state = initState
-        let mutable res = [state]
+        let mutable res = [ state ]
+
         for i = fin downto start do
             state <- f.Invoke(arr.[i], state)
             res <- state :: res
+
         res
 
     []
-    let scanBack<'T, 'State> folder (list: 'T list) (state:'State) =
+    let scanBack<'T, 'State> folder (list: 'T list) (state: 'State) =
         match list with
-        | [] -> [state]
-        | [h] ->
-            [folder h state; state]
+        | [] -> [ state ]
+        | [ h ] -> [ folder h state; state ]
         | _ ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(folder)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (folder)
             // It is faster to allocate and iterate an array than to create all those
             // highly nested stacks.  It also means we won't get stack overflows here.
             let arr = toArray list
             let arrn = arr.Length
             scanArraySubRight f arr 0 (arrn - 1) state
 
-    let foldBack2UsingArrays (f:OptimizedClosures.FSharpFunc<_, _, _, _>) list1 list2 acc =
+    let foldBack2UsingArrays (f: OptimizedClosures.FSharpFunc<_, _, _, _>) list1 list2 acc =
         let arr1 = toArray list1
         let arr2 = toArray list2
         let n1 = arr1.Length
         let n2 = arr2.Length
+
         if n1 <> n2 then
-            invalidArgFmt "list1, list2"
+            invalidArgFmt
+                "list1, list2"
                 "{0}\nlist1.Length = {1}, list2.Length = {2}"
-                [|SR.GetString SR.listsHadDifferentLengths; arr1.Length; arr2.Length|]
+                [| SR.GetString SR.listsHadDifferentLengths; arr1.Length; arr2.Length |]
+
         let mutable res = acc
+
         for i = n1 - 1 downto 0 do
             res <- f.Invoke(arr1.[i], arr2.[i], res)
+
         res
 
     []
-    let rec foldBack2<'T1, 'T2, 'State> folder (list1:'T1 list) (list2:'T2 list) (state:'State) =
+    let rec foldBack2<'T1, 'T2, 'State> folder (list1: 'T1 list) (list2: 'T2 list) (state: 'State) =
         match list1, list2 with
         | [], [] -> state
         | h1 :: rest1, k1 :: rest2 ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(folder)
+            let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (folder)
+
             match rest1, rest2 with
             | [], [] -> f.Invoke(h1, k1, state)
-            | [h2], [k2] -> f.Invoke(h1, k1, f.Invoke(h2, k2, state))
-            | [h2; h3], [k2; k3] -> f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, state)))
-            | [h2; h3; h4], [k2; k3; k4] -> f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, f.Invoke(h4, k4, state))))
+            | [ h2 ], [ k2 ] -> f.Invoke(h1, k1, f.Invoke(h2, k2, state))
+            | [ h2; h3 ], [ k2; k3 ] -> f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, state)))
+            | [ h2; h3; h4 ], [ k2; k3; k4 ] -> f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, f.Invoke(h4, k4, state))))
             | _ -> foldBack2UsingArrays f list1 list2 state
         | [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
         | xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
 
-    let rec forall2aux (f:OptimizedClosures.FSharpFunc<_, _, _>) list1 list2 =
+    let rec forall2aux (f: OptimizedClosures.FSharpFunc<_, _, _>) list1 list2 =
         match list1, list2 with
         | [], [] -> true
-        | h1 :: t1, h2 :: t2 -> f.Invoke(h1, h2)  && forall2aux f t1 t2
+        | h1 :: t1, h2 :: t2 -> f.Invoke(h1, h2) && forall2aux f t1 t2
         | [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
         | xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
 
@@ -345,14 +428,16 @@ module List =
         match list1, list2 with
         | [], [] -> true
         | _ ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (predicate)
             forall2aux f list1 list2
 
     []
-    let forall predicate list = Microsoft.FSharp.Primitives.Basics.List.forall predicate list
+    let forall predicate list =
+        Microsoft.FSharp.Primitives.Basics.List.forall predicate list
 
     []
-    let exists predicate list = Microsoft.FSharp.Primitives.Basics.List.exists predicate list
+    let exists predicate list =
+        Microsoft.FSharp.Primitives.Basics.List.exists predicate list
 
     []
     let inline contains value source =
@@ -360,12 +445,13 @@ module List =
             match xs1 with
             | [] -> false
             | h1 :: t1 -> e = h1 || contains e t1
+
         contains value source
 
-    let rec exists2aux (f:OptimizedClosures.FSharpFunc<_, _, _>) list1 list2 =
+    let rec exists2aux (f: OptimizedClosures.FSharpFunc<_, _, _>) list1 list2 =
         match list1, list2 with
         | [], [] -> false
-        | h1 :: t1, h2 :: t2 ->f.Invoke(h1, h2)  || exists2aux f t1 t2
+        | h1 :: t1, h2 :: t2 -> f.Invoke(h1, h2) || exists2aux f t1 t2
         | _ -> invalidArg "list2" (SR.GetString(SR.listsHadDifferentLengths))
 
     []
@@ -373,26 +459,38 @@ module List =
         match list1, list2 with
         | [], [] -> false
         | _ ->
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (predicate)
             exists2aux f list1 list2
 
     []
-    let rec find predicate list = 
+    let rec find predicate list =
         match list with
-        | [] -> indexNotFound()
-        | h :: t -> if predicate h then h else find predicate t
+        | [] -> indexNotFound ()
+        | h :: t ->
+            if predicate h then
+                h
+            else
+                find predicate t
 
     []
     let rec tryFind predicate list =
         match list with
-        | [] -> None 
-        | h :: t -> if predicate h then Some h else tryFind predicate t
+        | [] -> None
+        | h :: t ->
+            if predicate h then
+                Some h
+            else
+                tryFind predicate t
 
     []
-    let findBack predicate list = list |> toArray |> Microsoft.FSharp.Primitives.Basics.Array.findBack predicate
+    let findBack predicate list =
+        list |> toArray |> Microsoft.FSharp.Primitives.Basics.Array.findBack predicate
 
     []
-    let tryFindBack predicate list = list |> toArray |> Microsoft.FSharp.Primitives.Basics.Array.tryFindBack predicate
+    let tryFindBack predicate list =
+        list
+        |> toArray
+        |> Microsoft.FSharp.Primitives.Basics.Array.tryFindBack predicate
 
     []
     let rec tryPick chooser list =
@@ -406,18 +504,20 @@ module List =
     []
     let rec pick chooser list =
         match list with
-        | [] -> indexNotFound()
+        | [] -> indexNotFound ()
         | h :: t ->
             match chooser h with
             | None -> pick chooser t
             | Some r -> r
 
     []
-    let filter predicate list = Microsoft.FSharp.Primitives.Basics.List.filter predicate list
+    let filter predicate list =
+        Microsoft.FSharp.Primitives.Basics.List.filter predicate list
 
     []
     let except (itemsToExclude: seq<'T>) list =
         checkNonNull "itemsToExclude" itemsToExclude
+
         match list with
         | [] -> list
         | _ ->
@@ -425,59 +525,74 @@ module List =
             list |> filter cached.Add
 
     []
-    let where predicate list = Microsoft.FSharp.Primitives.Basics.List.filter predicate list
+    let where predicate list =
+        Microsoft.FSharp.Primitives.Basics.List.filter predicate list
 
-    let inline groupByImpl (comparer:IEqualityComparer<'SafeKey>) (keyf: 'T->'SafeKey) (getKey:'SafeKey->'Key) (list: 'T list) =
+    let inline groupByImpl (comparer: IEqualityComparer<'SafeKey>) (keyf: 'T -> 'SafeKey) (getKey: 'SafeKey -> 'Key) (list: 'T list) =
         Microsoft.FSharp.Primitives.Basics.List.groupBy comparer keyf getKey list
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
-    let groupByValueType (keyf: 'T->'Key) (list: 'T list) = groupByImpl HashIdentity.Structural<'Key> keyf id list
+    let groupByValueType (keyf: 'T -> 'Key) (list: 'T list) =
+        groupByImpl HashIdentity.Structural<'Key> keyf id list
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
-    let groupByRefType   (keyf: 'T->'Key) (list: 'T list) = groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox (keyf t)) (fun sb -> sb.Value) list
+    let groupByRefType (keyf: 'T -> 'Key) (list: 'T list) =
+        groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value) list
 
     []
-    let groupBy (projection: 'T->'Key) (list: 'T list) =
+    let groupBy (projection: 'T -> 'Key) (list: 'T list) =
         match list with
         | [] -> []
         | _ ->
-            if typeof<'Key>.IsValueType
-                then groupByValueType projection list
-                else groupByRefType   projection list
+            if typeof<'Key>.IsValueType then
+                groupByValueType projection list
+            else
+                groupByRefType projection list
 
     []
-    let partition predicate list = Microsoft.FSharp.Primitives.Basics.List.partition predicate list
+    let partition predicate list =
+        Microsoft.FSharp.Primitives.Basics.List.partition predicate list
 
     []
-    let unzip list = Microsoft.FSharp.Primitives.Basics.List.unzip list
+    let unzip list =
+        Microsoft.FSharp.Primitives.Basics.List.unzip list
 
     []
-    let unzip3 list = Microsoft.FSharp.Primitives.Basics.List.unzip3 list
+    let unzip3 list =
+        Microsoft.FSharp.Primitives.Basics.List.unzip3 list
 
     []
-    let windowed windowSize list = Microsoft.FSharp.Primitives.Basics.List.windowed windowSize list
+    let windowed windowSize list =
+        Microsoft.FSharp.Primitives.Basics.List.windowed windowSize list
 
     []
-    let chunkBySize chunkSize list = Microsoft.FSharp.Primitives.Basics.List.chunkBySize chunkSize list
+    let chunkBySize chunkSize list =
+        Microsoft.FSharp.Primitives.Basics.List.chunkBySize chunkSize list
 
     []
-    let splitInto count list = Microsoft.FSharp.Primitives.Basics.List.splitInto count list
+    let splitInto count list =
+        Microsoft.FSharp.Primitives.Basics.List.splitInto count list
 
     []
-    let zip list1 list2 = Microsoft.FSharp.Primitives.Basics.List.zip list1 list2
+    let zip list1 list2 =
+        Microsoft.FSharp.Primitives.Basics.List.zip list1 list2
 
     []
-    let zip3 list1 list2 list3 = Microsoft.FSharp.Primitives.Basics.List.zip3 list1 list2 list3
+    let zip3 list1 list2 list3 =
+        Microsoft.FSharp.Primitives.Basics.List.zip3 list1 list2 list3
 
     []
     let skip count list =
-        if count <= 0 then list else
-        let rec loop i lst =
-            match lst with
-            | _ when i = 0 -> lst
-            | _ :: t -> loop (i-1) t
-            | [] -> invalidArgOutOfRange "count" count "distance past the list" i
-        loop count list
+        if count <= 0 then
+            list
+        else
+            let rec loop i lst =
+                match lst with
+                | _ when i = 0 -> lst
+                | _ :: t -> loop (i - 1) t
+                | [] -> invalidArgOutOfRange "count" count "distance past the list" i
+
+            loop count list
 
     []
     let rec skipWhile predicate list =
@@ -488,7 +603,8 @@ module List =
     []
     let sortWith comparer list =
         match list with
-        | [] | [_] -> list
+        | []
+        | [ _ ] -> list
         | _ ->
             let array = Microsoft.FSharp.Primitives.Basics.List.toArray list
             Microsoft.FSharp.Primitives.Basics.Array.stableSortInPlaceWith comparer array
@@ -497,7 +613,8 @@ module List =
     []
     let sortBy projection list =
         match list with
-        | [] | [_] -> list
+        | []
+        | [ _ ] -> list
         | _ ->
             let array = Microsoft.FSharp.Primitives.Basics.List.toArray list
             Microsoft.FSharp.Primitives.Basics.Array.stableSortInPlaceBy projection array
@@ -506,7 +623,8 @@ module List =
     []
     let sort list =
         match list with
-        | [] | [_] -> list
+        | []
+        | [ _ ] -> list
         | _ ->
             let array = Microsoft.FSharp.Primitives.Basics.List.toArray list
             Microsoft.FSharp.Primitives.Basics.Array.stableSortInPlace array
@@ -514,35 +632,49 @@ module List =
 
     []
     let inline sortByDescending projection list =
-        let inline compareDescending a b = compare (projection b) (projection a)
+        let inline compareDescending a b =
+            compare (projection b) (projection a)
+
         sortWith compareDescending list
 
     []
     let inline sortDescending list =
-        let inline compareDescending a b = compare b a
+        let inline compareDescending a b =
+            compare b a
+
         sortWith compareDescending list
 
     []
-    let ofSeq source = Seq.toList source
+    let ofSeq source =
+        Seq.toList source
 
     []
-    let toSeq list = Seq.ofList list
+    let toSeq list =
+        Seq.ofList list
 
     []
     let findIndex predicate list =
-        let rec loop n list = 
-            match list with 
-            | [] -> indexNotFound()
-            | h :: t -> if predicate h then n else loop (n + 1) t
+        let rec loop n list =
+            match list with
+            | [] -> indexNotFound ()
+            | h :: t ->
+                if predicate h then
+                    n
+                else
+                    loop (n + 1) t
 
         loop 0 list
 
     []
     let tryFindIndex predicate list =
-        let rec loop n list = 
+        let rec loop n list =
             match list with
             | [] -> None
-            | h :: t -> if predicate h then Some n else loop (n + 1) t
+            | h :: t ->
+                if predicate h then
+                    Some n
+                else
+                    loop (n + 1) t
 
         loop 0 list
 
@@ -564,8 +696,10 @@ module List =
         | [] -> LanguagePrimitives.GenericZero<'T>
         | t ->
             let mutable acc = LanguagePrimitives.GenericZero<'T>
+
             for x in t do
                 acc <- Checked.(+) acc x
+
             acc
 
     []
@@ -574,8 +708,10 @@ module List =
         | [] -> LanguagePrimitives.GenericZero<'U>
         | t ->
             let mutable acc = LanguagePrimitives.GenericZero<'U>
+
             for x in t do
                 acc <- Checked.(+) acc (projection x)
+
             acc
 
     []
@@ -584,9 +720,10 @@ module List =
         | [] -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
         | h :: t ->
             let mutable acc = h
+
             for x in t do
-                if x > acc then
-                    acc <- x
+                if x > acc then acc <- x
+
             acc
 
     []
@@ -596,11 +733,14 @@ module List =
         | h :: t ->
             let mutable acc = h
             let mutable accv = projection h
+
             for x in t do
                 let currv = projection x
+
                 if currv > accv then
                     acc <- x
                     accv <- currv
+
             acc
 
     []
@@ -609,9 +749,10 @@ module List =
         | [] -> invalidArg "list" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
         | h :: t ->
             let mutable acc = h
+
             for x in t do
-                if x < acc then
-                    acc <- x
+                if x < acc then acc <- x
+
             acc
 
     []
@@ -621,11 +762,14 @@ module List =
         | h :: t ->
             let mutable acc = h
             let mutable accv = projection h
+
             for x in t do
                 let currv = projection x
+
                 if currv < accv then
                     acc <- x
                     accv <- currv
+
             acc
 
     []
@@ -635,9 +779,11 @@ module List =
         | xs ->
             let mutable sum = LanguagePrimitives.GenericZero<'T>
             let mutable count = 0
+
             for x in xs do
                 sum <- Checked.(+) sum x
                 count <- count + 1
+
             LanguagePrimitives.DivideByInt sum count
 
     []
@@ -647,45 +793,53 @@ module List =
         | xs ->
             let mutable sum = LanguagePrimitives.GenericZero<'U>
             let mutable count = 0
+
             for x in xs do
                 sum <- Checked.(+) sum (projection x)
                 count <- count + 1
+
             LanguagePrimitives.DivideByInt sum count
 
     []
-    let collect mapping list = Microsoft.FSharp.Primitives.Basics.List.collect mapping list
+    let collect mapping list =
+        Microsoft.FSharp.Primitives.Basics.List.collect mapping list
 
     []
-    let allPairs list1 list2 = Microsoft.FSharp.Primitives.Basics.List.allPairs list1 list2
+    let allPairs list1 list2 =
+        Microsoft.FSharp.Primitives.Basics.List.allPairs list1 list2
 
     []
     let inline compareWith ([] comparer: 'T -> 'T -> int) (list1: 'T list) (list2: 'T list) =
         let rec loop list1 list2 =
-             match list1, list2 with
-             | head1 :: tail1, head2 :: tail2 ->
-                   let c = comparer head1 head2
-                   if c = 0 then loop tail1 tail2 else c
-             | [], [] -> 0
-             | _, [] -> 1
-             | [], _ -> -1
+            match list1, list2 with
+            | head1 :: tail1, head2 :: tail2 ->
+                let c = comparer head1 head2
+                if c = 0 then loop tail1 tail2 else c
+            | [], [] -> 0
+            | _, [] -> 1
+            | [], _ -> -1
 
         loop list1 list2
 
     []
-    let permute indexMap list = list |> toArray |> Microsoft.FSharp.Primitives.Basics.Array.permute indexMap |> ofArray
+    let permute indexMap list =
+        list
+        |> toArray
+        |> Microsoft.FSharp.Primitives.Basics.Array.permute indexMap
+        |> ofArray
 
     []
     let exactlyOne (list: _ list) =
         match list with
-        | [x] -> x
-        | []  -> invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
-        | _   -> invalidArg "source" (SR.GetString(SR.inputSequenceTooLong))
+        | [ x ] -> x
+        | [] -> invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+        | _ -> invalidArg "source" (SR.GetString(SR.inputSequenceTooLong))
 
     []
     let tryExactlyOne (list: _ list) =
         match list with
-        | [x] -> Some x
-        | _   -> None
+        | [ x ] -> Some x
+        | _ -> None
 
     []
     let transpose (lists: seq<'T list>) =
@@ -693,93 +847,119 @@ module List =
         Microsoft.FSharp.Primitives.Basics.List.transpose (ofSeq lists)
 
     []
-    let truncate count list = Microsoft.FSharp.Primitives.Basics.List.truncate count list
+    let truncate count list =
+        Microsoft.FSharp.Primitives.Basics.List.truncate count list
 
     []
-    let unfold<'T, 'State> (generator:'State -> ('T*'State) option) (state:'State) = Microsoft.FSharp.Primitives.Basics.List.unfold generator state
+    let unfold<'T, 'State> (generator: 'State -> ('T * 'State) option) (state: 'State) =
+        Microsoft.FSharp.Primitives.Basics.List.unfold generator state
 
     []
     let removeAt (index: int) (source: 'T list) : 'T list =
-        if index < 0 then invalidArg "index" "index must be within bounds of the list"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the list"
 
         let mutable i = 0
         let mutable coll = ListCollector()
         let mutable curr = source
+
         while i < index do // traverse and save the linked list until item to be removed
-              match curr with
-              | [] -> invalidArg "index" "index must be within bounds of the list" 
-              | h::t ->
-                  coll.Add(h)
-                  curr <- t
-              i <- i + 1
-        if curr.IsEmpty then invalidArg "index" "index must be within bounds of the list"
-        else coll.AddManyAndClose(curr.Tail) // when i = index, Head is the item which is ignored and Tail is the rest of the list
+            match curr with
+            | [] -> invalidArg "index" "index must be within bounds of the list"
+            | h :: t ->
+                coll.Add(h)
+                curr <- t
+
+            i <- i + 1
+
+        if curr.IsEmpty then
+            invalidArg "index" "index must be within bounds of the list"
+        else
+            coll.AddManyAndClose(curr.Tail) // when i = index, Head is the item which is ignored and Tail is the rest of the list
 
     []
     let removeManyAt (index: int) (count: int) (source: 'T list) : 'T list =
-        if index < 0 then invalidArg "index" "index must be within bounds of the list"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the list"
 
         let mutable i = 0
         let mutable coll = ListCollector()
         let mutable curr = source
+
         while i < index + count do // traverse and save the linked list until the last item to be removed
-              match curr with
-              | [] -> invalidArg "index" "index must be within bounds of the list" 
-              | h::t ->
-                  if i < index then coll.Add(h) //items before index we keep
-                  curr <- t
-              i <- i + 1
+            match curr with
+            | [] -> invalidArg "index" "index must be within bounds of the list"
+            | h :: t ->
+                if i < index then coll.Add(h) //items before index we keep
+                curr <- t
+
+            i <- i + 1
+
         coll.AddManyAndClose(curr) // when i = index + count, we keep the rest of the list
 
     []
     let updateAt (index: int) (value: 'T) (source: 'T list) : 'T list =
-        if index < 0 then invalidArg "index" "index must be within bounds of the list"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the list"
 
         let mutable i = 0
         let mutable coll = ListCollector()
         let mutable curr = source
+
         while i < index do // Traverse and save the linked list until index
-              match curr with
-              | [] -> invalidArg "index" "index must be within bounds of the list" 
-              | h::t ->
-                  coll.Add(h)
-                  curr <- t
-              i <- i + 1
+            match curr with
+            | [] -> invalidArg "index" "index must be within bounds of the list"
+            | h :: t ->
+                coll.Add(h)
+                curr <- t
+
+            i <- i + 1
+
         coll.Add(value) // add value instead of Head
-        if curr.IsEmpty then invalidArg "index" "index must be within bounds of the list"
-        else coll.AddManyAndClose(curr.Tail)
+
+        if curr.IsEmpty then
+            invalidArg "index" "index must be within bounds of the list"
+        else
+            coll.AddManyAndClose(curr.Tail)
 
     []
     let insertAt (index: int) (value: 'T) (source: 'T list) : 'T list =
-        if index < 0 then invalidArg "index" "index must be within bounds of the list"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the list"
 
         let mutable i = 0
         let mutable coll = ListCollector()
         let mutable curr = source
+
         while i < index do // traverse and save the linked list until index
-              match curr with
-              | [] -> invalidArg "index" "index must be within bounds of the list" 
-              | h::t ->
-                  coll.Add(h)
-                  curr <- t
-              i <- i + 1
-        
+            match curr with
+            | [] -> invalidArg "index" "index must be within bounds of the list"
+            | h :: t ->
+                coll.Add(h)
+                curr <- t
+
+            i <- i + 1
+
         coll.Add(value)
         coll.AddManyAndClose(curr) // insert item BEFORE the item at the index
 
     []
     let insertManyAt (index: int) (values: seq<'T>) (source: 'T list) : 'T list =
-        if index < 0 then invalidArg "index" "index must be within bounds of the list"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the list"
 
         let mutable i = 0
         let mutable coll = ListCollector()
         let mutable curr = source
+
         while i < index do // traverse and save the linked list until index
-              match curr with
-              | [] -> invalidArg "index" "index must be within bounds of the list" 
-              | h::t ->
-                  coll.Add(h)
-                  curr <- t
-              i <- i + 1
+            match curr with
+            | [] -> invalidArg "index" "index must be within bounds of the list"
+            | h :: t ->
+                coll.Add(h)
+                curr <- t
+
+            i <- i + 1
+
         coll.AddMany(values) // insert values BEFORE the item at the index
-        coll.AddManyAndClose(curr)
\ No newline at end of file
+        coll.AddManyAndClose(curr)
diff --git a/src/FSharp.Core/mailbox.fs b/src/FSharp.Core/mailbox.fs
index 78035f34727..15e2b639f91 100644
--- a/src/FSharp.Core/mailbox.fs
+++ b/src/FSharp.Core/mailbox.fs
@@ -18,13 +18,22 @@ module AsyncHelpers =
         async {
             let resultCell = new ResultCell<_>()
             let! cancellationToken = Async.CancellationToken
+
             let start a f =
-                Async.StartWithContinuationsUsingDispatchInfo(a,
-                    (fun res -> resultCell.RegisterResult(f res |> AsyncResult.Ok, reuseThread=false) |> ignore),
-                    (fun edi -> resultCell.RegisterResult(edi |> AsyncResult.Error, reuseThread=false) |> ignore),
-                    (fun oce -> resultCell.RegisterResult(oce |> AsyncResult.Canceled, reuseThread=false) |> ignore),
+                Async.StartWithContinuationsUsingDispatchInfo(
+                    a,
+                    (fun res ->
+                        resultCell.RegisterResult(f res |> AsyncResult.Ok, reuseThread = false)
+                        |> ignore),
+                    (fun edi ->
+                        resultCell.RegisterResult(edi |> AsyncResult.Error, reuseThread = false)
+                        |> ignore),
+                    (fun oce ->
+                        resultCell.RegisterResult(oce |> AsyncResult.Canceled, reuseThread = false)
+                        |> ignore),
                     cancellationToken = cancellationToken
-                    )
+                )
+
             start a1 Choice1Of2
             start a2 Choice2Of2
             // Note: It is ok to use "NoDirectCancel" here because the started computations use the same
@@ -37,12 +46,14 @@ module AsyncHelpers =
     let timeout msec cancellationToken =
         assert (msec >= 0)
         let resultCell = new ResultCell<_>()
+
         Async.StartWithContinuations(
-            computation=Async.Sleep msec,
-            continuation=(fun () -> resultCell.RegisterResult((), reuseThread = false) |> ignore),
-            exceptionContinuation=ignore,
-            cancellationContinuation=ignore,
-            cancellationToken = cancellationToken)
+            computation = Async.Sleep msec,
+            continuation = (fun () -> resultCell.RegisterResult((), reuseThread = false) |> ignore),
+            exceptionContinuation = ignore,
+            cancellationContinuation = ignore,
+            cancellationToken = cancellationToken
+        )
         // Note: It is ok to use "NoDirectCancel" here because the started computations use the same
         //       cancellation token and will register a cancelled result if cancellation occurs.
         // Note: It is ok to use "NoDirectTimeout" here because the child compuation above looks after the timeout.
@@ -51,7 +62,7 @@ module AsyncHelpers =
 []
 []
 type Mailbox<'Msg>(cancellationSupported: bool) =
-    let mutable inboxStore  = null
+    let mutable inboxStore = null
     let arrivals = Queue<'Msg>()
     let syncRoot = arrivals
 
@@ -59,22 +70,21 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
     // asynchronous receive, either
     //     -- "cont" is non-null and the reader is "activated" by re-scheduling cont in the thread pool; or
     //     -- "pulse" is non-null and the reader is "activated" by setting this event
-    let mutable savedCont : (bool -> AsyncReturn) option = None
+    let mutable savedCont: (bool -> AsyncReturn) option = None
 
     // Readers who have a timeout use this event
-    let mutable pulse : AutoResetEvent = null
+    let mutable pulse: AutoResetEvent = null
 
     // Make sure that the "pulse" value is created
-    let ensurePulse() =
+    let ensurePulse () =
         match pulse with
-        | null ->
-            pulse <- new AutoResetEvent(false)
-        | _ ->
-            ()
+        | null -> pulse <- new AutoResetEvent(false)
+        | _ -> ()
+
         pulse
 
     let waitOneNoTimeoutOrCancellation =
-        MakeAsync (fun ctxt ->
+        MakeAsync(fun ctxt ->
             match savedCont with
             | None ->
                 let descheduled =
@@ -86,16 +96,16 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
                             true
                         else
                             false)
+
                 if descheduled then
                     Unchecked.defaultof<_>
                 else
                     // If we didn't deschedule then run the continuation immediately
                     ctxt.CallContinuation true
-            | Some _ ->
-                failwith "multiple waiting reader continuations for mailbox")
+            | Some _ -> failwith "multiple waiting reader continuations for mailbox")
 
     let waitOneWithCancellation timeout =
-        Async.AwaitWaitHandle(ensurePulse(), millisecondsTimeout=timeout)
+        Async.AwaitWaitHandle(ensurePulse (), millisecondsTimeout = timeout)
 
     let waitOne timeout =
         if timeout < 0 && not cancellationSupported then
@@ -107,16 +117,17 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
         match inboxStore with
         | null -> inboxStore <- new System.Collections.Generic.List<'Msg>(1)
         | _ -> ()
+
         inboxStore
 
-    member x.CurrentQueueLength =
-        lock syncRoot (fun () -> x.inbox.Count + arrivals.Count)
+    member x.CurrentQueueLength = lock syncRoot (fun () -> x.inbox.Count + arrivals.Count)
 
     member x.ScanArrivalsUnsafe f =
         if arrivals.Count = 0 then
             None
         else
             let msg = arrivals.Dequeue()
+
             match f msg with
             | None ->
                 x.inbox.Add msg
@@ -131,13 +142,16 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
         match inboxStore with
         | null -> None
         | inbox ->
-            if n >= inbox.Count
-            then None
+            if n >= inbox.Count then
+                None
             else
                 let msg = inbox.[n]
+
                 match f msg with
-                | None -> x.ScanInbox (f, n+1)
-                | res -> inbox.RemoveAt n; res
+                | None -> x.ScanInbox(f, n + 1)
+                | res ->
+                    inbox.RemoveAt n
+                    res
 
     member x.ReceiveFromArrivalsUnsafe() =
         if arrivals.Count = 0 then
@@ -170,8 +184,7 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
             match savedCont with
             | None ->
                 match pulse with
-                | null ->
-                    () // no one waiting, leaving the message in the queue is sufficient
+                | null -> () // no one waiting, leaving the message in the queue is sufficient
                 | ev ->
                     // someone is waiting on the wait handle
                     ev.Set() |> ignore
@@ -180,18 +193,17 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
                 savedCont <- None
                 action true |> ignore)
 
-    member x.TryScan ((f: 'Msg -> (Async<'T>) option), timeout) : Async<'T option> =
-        let rec scan timeoutAsync (timeoutCts:CancellationTokenSource) =
+    member x.TryScan((f: 'Msg -> (Async<'T>) option), timeout) : Async<'T option> =
+        let rec scan timeoutAsync (timeoutCts: CancellationTokenSource) =
             async {
                 match x.ScanArrivals f with
                 | None ->
                     // Deschedule and wait for a message. When it comes, rescan the arrivals
                     let! ok = AsyncHelpers.awaitEither waitOneNoTimeoutOrCancellation timeoutAsync
+
                     match ok with
-                    | Choice1Of2 true ->
-                        return! scan timeoutAsync timeoutCts
-                    | Choice1Of2 false ->
-                        return failwith "should not happen - waitOneNoTimeoutOrCancellation always returns true"
+                    | Choice1Of2 true -> return! scan timeoutAsync timeoutCts
+                    | Choice1Of2 false -> return failwith "should not happen - waitOneNoTimeoutOrCancellation always returns true"
                     | Choice2Of2 () ->
                         lock syncRoot (fun () ->
                             // Cancel the outstanding wait for messages installed by waitOneWithCancellation
@@ -214,13 +226,15 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
                     let! res = resP
                     return Some res
             }
+
         let rec scanNoTimeout () =
             async {
                 match x.ScanArrivals f with
                 | None ->
                     let! ok = waitOne Timeout.Infinite
+
                     if ok then
-                        return! scanNoTimeout()
+                        return! scanNoTimeout ()
                     else
                         return (failwith "Timed out with infinite timeout??")
                 | Some resP ->
@@ -231,11 +245,13 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
         // Look in the inbox first
         async {
             match x.ScanInbox(f, 0) with
-            | None  when timeout < 0 ->
-                return! scanNoTimeout()
+            | None when timeout < 0 -> return! scanNoTimeout ()
             | None ->
                 let! cancellationToken = Async.CancellationToken
-                let timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, CancellationToken.None)
+
+                let timeoutCts =
+                    CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, CancellationToken.None)
+
                 let timeoutAsync = AsyncHelpers.timeout timeout timeoutCts.Token
                 return! scan timeoutAsync timeoutCts
             | Some resP ->
@@ -246,13 +262,14 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
     member x.Scan((f: 'Msg -> (Async<'T>) option), timeout) =
         async {
             let! resOpt = x.TryScan(f, timeout)
+
             match resOpt with
-            | None -> return raise(TimeoutException(SR.GetString(SR.mailboxScanTimedOut)))
+            | None -> return raise (TimeoutException(SR.GetString(SR.mailboxScanTimedOut)))
             | Some res -> return res
         }
 
     member x.TryReceive timeout =
-        let rec processFirstArrival() =
+        let rec processFirstArrival () =
             async {
                 match x.ReceiveFromArrivals() with
                 | None ->
@@ -261,13 +278,14 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
                     // check arrivals again.
                     match pulse with
                     | null when timeout >= 0 || cancellationSupported ->
-                        ensurePulse() |> ignore
-                        return! processFirstArrival()
+                        ensurePulse () |> ignore
+                        return! processFirstArrival ()
                     | _ ->
                         // Wait until we have been notified about a message. When that happens, rescan the arrivals
                         let! ok = waitOne timeout
+
                         if ok then
-                            return! processFirstArrival()
+                            return! processFirstArrival ()
                         else
                             return None
                 | res -> return res
@@ -276,13 +294,13 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
         // look in the inbox first
         async {
             match x.ReceiveFromInbox() with
-            | None -> return! processFirstArrival()
+            | None -> return! processFirstArrival ()
             | res -> return res
         }
 
     member x.Receive timeout =
 
-        let rec processFirstArrival() =
+        let rec processFirstArrival () =
             async {
                 match x.ReceiveFromArrivals() with
                 | None ->
@@ -291,39 +309,40 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
                     // check arrivals again.
                     match pulse with
                     | null when timeout >= 0 || cancellationSupported ->
-                        ensurePulse() |> ignore
-                        return! processFirstArrival()
+                        ensurePulse () |> ignore
+                        return! processFirstArrival ()
                     | _ ->
                         // Wait until we have been notified about a message. When that happens, rescan the arrivals
                         let! ok = waitOne timeout
+
                         if ok then
-                            return! processFirstArrival()
+                            return! processFirstArrival ()
                         else
-                            return raise(TimeoutException(SR.GetString(SR.mailboxReceiveTimedOut)))
+                            return raise (TimeoutException(SR.GetString(SR.mailboxReceiveTimedOut)))
                 | Some res -> return res
             }
 
         // look in the inbox first
         async {
             match x.ReceiveFromInbox() with
-            | None -> return! processFirstArrival()
+            | None -> return! processFirstArrival ()
             | Some res -> return res
         }
 
     interface System.IDisposable with
         member _.Dispose() =
-            if isNotNull pulse then (pulse :> IDisposable).Dispose()
+            if isNotNull pulse then
+                (pulse :> IDisposable).Dispose()
 
 #if DEBUG
-    member x.UnsafeContents =
-        (x.inbox, arrivals, pulse, savedCont) |> box
+    member x.UnsafeContents = (x.inbox, arrivals, pulse, savedCont) |> box
 #endif
 
-
 []
 []
-type AsyncReplyChannel<'Reply>(replyf : 'Reply -> unit) =
-    member x.Reply value = replyf value
+type AsyncReplyChannel<'Reply>(replyf: 'Reply -> unit) =
+    member x.Reply value =
+        replyf value
 
 []
 []
@@ -340,7 +359,7 @@ type MailboxProcessor<'Msg>(body, ?cancellationToken) =
     member _.CurrentQueueLength = mailbox.CurrentQueueLength // nb. unprotected access gives an approximation of the queue length
 
     member _.DefaultTimeout
-        with get() = defaultTimeout
+        with get () = defaultTimeout
         and set v = defaultTimeout <- v
 
     []
@@ -360,81 +379,116 @@ type MailboxProcessor<'Msg>(body, ?cancellationToken) =
             // Note that exception stack traces are lost in this design - in an extended design
             // the event could propagate an ExceptionDispatchInfo instead of an Exception.
             let p =
-                async { try
-                            do! body x
-                        with exn ->
-                            errorEvent.Trigger exn }
+                async {
+                    try
+                        do! body x
+                    with exn ->
+                        errorEvent.Trigger exn
+                }
 
-            Async.Start(computation=p, cancellationToken=cancellationToken)
+            Async.Start(computation = p, cancellationToken = cancellationToken)
 
-    member _.Post message = mailbox.Post message
+    member _.Post message =
+        mailbox.Post message
 
-    member _.TryPostAndReply(buildMessage : (_ -> 'Msg), ?timeout) : 'Reply option =
+    member _.TryPostAndReply(buildMessage: (_ -> 'Msg), ?timeout) : 'Reply option =
         let timeout = defaultArg timeout defaultTimeout
         use resultCell = new ResultCell<_>()
-        let msg = buildMessage (new AsyncReplyChannel<_>(fun reply ->
-                                // Note the ResultCell may have been disposed if the operation
-                                // timed out. In this case RegisterResult drops the result on the floor.
-                                resultCell.RegisterResult(reply, reuseThread=false) |> ignore))
+
+        let msg =
+            buildMessage (
+                new AsyncReplyChannel<_>(fun reply ->
+                    // Note the ResultCell may have been disposed if the operation
+                    // timed out. In this case RegisterResult drops the result on the floor.
+                    resultCell.RegisterResult(reply, reuseThread = false) |> ignore)
+            )
+
         mailbox.Post msg
-        resultCell.TryWaitForResultSynchronously(timeout=timeout)
+        resultCell.TryWaitForResultSynchronously(timeout = timeout)
 
     member x.PostAndReply(buildMessage, ?timeout) : 'Reply =
-        match x.TryPostAndReply(buildMessage, ?timeout=timeout) with
-        | None ->  raise (TimeoutException(SR.GetString(SR.mailboxProcessorPostAndReplyTimedOut)))
+        match x.TryPostAndReply(buildMessage, ?timeout = timeout) with
+        | None -> raise (TimeoutException(SR.GetString(SR.mailboxProcessorPostAndReplyTimedOut)))
         | Some res -> res
 
     member _.PostAndTryAsyncReply(buildMessage, ?timeout) : Async<'Reply option> =
         let timeout = defaultArg timeout defaultTimeout
         let resultCell = new ResultCell<_>()
-        let msg = buildMessage (new AsyncReplyChannel<_>(fun reply ->
-                                // Note the ResultCell may have been disposed if the operation
-                                // timed out. In this case RegisterResult drops the result on the floor.
-                                resultCell.RegisterResult(reply, reuseThread=false) |> ignore))
+
+        let msg =
+            buildMessage (
+                new AsyncReplyChannel<_>(fun reply ->
+                    // Note the ResultCell may have been disposed if the operation
+                    // timed out. In this case RegisterResult drops the result on the floor.
+                    resultCell.RegisterResult(reply, reuseThread = false) |> ignore)
+            )
+
         mailbox.Post msg
+
         match timeout with
         | Threading.Timeout.Infinite when not cancellationSupported ->
-            async { let! result = resultCell.AwaitResult_NoDirectCancelOrTimeout
-                    return Some result }
+            async {
+                let! result = resultCell.AwaitResult_NoDirectCancelOrTimeout
+                return Some result
+            }
 
         | _ ->
-            async { use _disposeCell = resultCell
-                    let! ok =  Async.AwaitWaitHandle(resultCell.GetWaitHandle(), millisecondsTimeout=timeout)
-                    let res = (if ok then Some(resultCell.GrabResult()) else None)
-                    return res }
+            async {
+                use _disposeCell = resultCell
+                let! ok = Async.AwaitWaitHandle(resultCell.GetWaitHandle(), millisecondsTimeout = timeout)
+
+                let res =
+                    (if ok then
+                         Some(resultCell.GrabResult())
+                     else
+                         None)
+
+                return res
+            }
 
-    member x.PostAndAsyncReply(buildMessage, ?timeout:int) =
+    member x.PostAndAsyncReply(buildMessage, ?timeout: int) =
         let timeout = defaultArg timeout defaultTimeout
+
         match timeout with
         | Threading.Timeout.Infinite when not cancellationSupported ->
             // Nothing to dispose, no wait handles used
             let resultCell = new ResultCell<_>()
-            let msg = buildMessage (new AsyncReplyChannel<_>(fun reply -> resultCell.RegisterResult(reply, reuseThread=false) |> ignore))
+
+            let msg =
+                buildMessage (new AsyncReplyChannel<_>(fun reply -> resultCell.RegisterResult(reply, reuseThread = false) |> ignore))
+
             mailbox.Post msg
             resultCell.AwaitResult_NoDirectCancelOrTimeout
         | _ ->
-            let asyncReply = x.PostAndTryAsyncReply(buildMessage, timeout=timeout)
-            async { let! res = asyncReply
-                    match res with
-                    | None ->  return! raise (TimeoutException(SR.GetString(SR.mailboxProcessorPostAndAsyncReplyTimedOut)))
-                    | Some res -> return res }
+            let asyncReply = x.PostAndTryAsyncReply(buildMessage, timeout = timeout)
+
+            async {
+                let! res = asyncReply
+
+                match res with
+                | None -> return! raise (TimeoutException(SR.GetString(SR.mailboxProcessorPostAndAsyncReplyTimedOut)))
+                | Some res -> return res
+            }
 
     member _.Receive(?timeout) =
-        mailbox.Receive(timeout=defaultArg timeout defaultTimeout)
+        mailbox.Receive(timeout = defaultArg timeout defaultTimeout)
 
     member _.TryReceive(?timeout) =
-        mailbox.TryReceive(timeout=defaultArg timeout defaultTimeout)
+        mailbox.TryReceive(timeout = defaultArg timeout defaultTimeout)
 
     member _.Scan(scanner: 'Msg -> (Async<'T>) option, ?timeout) =
-        mailbox.Scan(scanner, timeout=defaultArg timeout defaultTimeout)
+        mailbox.Scan(scanner, timeout = defaultArg timeout defaultTimeout)
 
     member _.TryScan(scanner: 'Msg -> (Async<'T>) option, ?timeout) =
-        mailbox.TryScan(scanner, timeout=defaultArg timeout defaultTimeout)
+        mailbox.TryScan(scanner, timeout = defaultArg timeout defaultTimeout)
 
     interface System.IDisposable with
-        member _.Dispose() = (mailbox :> IDisposable).Dispose()
+        member _.Dispose() =
+            (mailbox :> IDisposable).Dispose()
 
     static member Start(body, ?cancellationToken) =
-        let mailboxProcessor = new MailboxProcessor<'Msg>(body, ?cancellationToken=cancellationToken)
+        let mailboxProcessor =
+            new MailboxProcessor<'Msg>(body, ?cancellationToken = cancellationToken)
+
         mailboxProcessor.Start()
         mailboxProcessor
diff --git a/src/FSharp.Core/map.fs b/src/FSharp.Core/map.fs
index 7a72f29b1d8..70f90a72ddd 100644
--- a/src/FSharp.Core/map.fs
+++ b/src/FSharp.Core/map.fs
@@ -17,38 +17,38 @@ type internal MapTree<'Key, 'Value>(k: 'Key, v: 'Value, h: int) =
     member _.Height = h
     member _.Key = k
     member _.Value = v
-    new(k: 'Key, v: 'Value) = MapTree(k,v,1)
-    
+    new(k: 'Key, v: 'Value) = MapTree(k, v, 1)
+
 []
 []
 []
-type internal MapTreeNode<'Key, 'Value>(k:'Key, v:'Value, left:MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) =
-    inherit MapTree<'Key,'Value>(k, v, h)
+type internal MapTreeNode<'Key, 'Value>(k: 'Key, v: 'Value, left: MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) =
+    inherit MapTree<'Key, 'Value>(k, v, h)
     member _.Left = left
     member _.Right = right
-    
-    
+
 []
-module MapTree = 
-    
+module MapTree =
+
     let empty = null
-    
-    let inline isEmpty (m:MapTree<'Key, 'Value>) = isNull m
-        
-    let inline private asNode(value:MapTree<'Key,'Value>) : MapTreeNode<'Key,'Value> =
-        value :?> MapTreeNode<'Key,'Value>
-        
-    let rec sizeAux acc (m:MapTree<'Key, 'Value>) = 
+
+    let inline isEmpty (m: MapTree<'Key, 'Value>) =
+        isNull m
+
+    let inline private asNode (value: MapTree<'Key, 'Value>) : MapTreeNode<'Key, 'Value> =
+        value :?> MapTreeNode<'Key, 'Value>
+
+    let rec sizeAux acc (m: MapTree<'Key, 'Value>) =
         if isEmpty m then
             acc
+        else if m.Height = 1 then
+            acc + 1
         else
-            if m.Height = 1 then
-                acc + 1
-            else
-                let mn = asNode m
-                sizeAux (sizeAux (acc+1) mn.Left) mn.Right 
-            
-    let size x = sizeAux 0 x
+            let mn = asNode m
+            sizeAux (sizeAux (acc + 1) mn.Left) mn.Right
+
+    let size x =
+        sizeAux 0 x
 
 #if TRACE_SETS_AND_MAPS
     let mutable traceCount = 0
@@ -64,373 +64,440 @@ module MapTree =
     let mutable largestMapSize = 0
     let mutable largestMapStackTrace = Unchecked.defaultof<_>
 
-    let report() = 
-       traceCount <- traceCount + 1 
-       if traceCount % 1000000 = 0 then 
-           System.Console.WriteLine(
-               "#MapOne = {0}, #MapNode = {1}, #Add = {2}, #Remove = {3}, #Unions = {4}, #Lookups = {5}, avMapTreeSizeOnNodeCreation = {6}, avMapSizeOnCreation = {7}, avMapSizeOnLookup = {8}", 
-               numOnes, numNodes, numAdds, numRemoves, numUnions, numLookups, 
-               (totalSizeOnNodeCreation / float (numNodes + numOnes)), (totalSizeOnMapAdd / float numAdds), 
-               (totalSizeOnMapLookup / float numLookups))
-           System.Console.WriteLine("#largestMapSize = {0}, largestMapStackTrace = {1}", largestMapSize, largestMapStackTrace)
-
-    let MapTree (k,v) = 
-        report()
+    let report () =
+        traceCount <- traceCount + 1
+
+        if traceCount % 1000000 = 0 then
+            System.Console.WriteLine(
+                "#MapOne = {0}, #MapNode = {1}, #Add = {2}, #Remove = {3}, #Unions = {4}, #Lookups = {5}, avMapTreeSizeOnNodeCreation = {6}, avMapSizeOnCreation = {7}, avMapSizeOnLookup = {8}",
+                numOnes,
+                numNodes,
+                numAdds,
+                numRemoves,
+                numUnions,
+                numLookups,
+                (totalSizeOnNodeCreation / float (numNodes + numOnes)),
+                (totalSizeOnMapAdd / float numAdds),
+                (totalSizeOnMapLookup / float numLookups)
+            )
+
+            System.Console.WriteLine("#largestMapSize = {0}, largestMapStackTrace = {1}", largestMapSize, largestMapStackTrace)
+
+    let MapTree (k, v) =
+        report ()
         numOnes <- numOnes + 1
         totalSizeOnNodeCreation <- totalSizeOnNodeCreation + 1.0
-        MapTree (k,v)
+        MapTree(k, v)
 
-    let MapTreeNode (x, l, v, r, h) = 
-        report()
+    let MapTreeNode (x, l, v, r, h) =
+        report ()
         numNodes <- numNodes + 1
-        let n = MapTreeNode (x, l, v, r, h)
+        let n = MapTreeNode(x, l, v, r, h)
         totalSizeOnNodeCreation <- totalSizeOnNodeCreation + float (size n)
         n
 #endif
 
-    let inline height (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then 0
-        else m.Height
-    
+    let inline height (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then 0 else m.Height
+
     []
     let tolerance = 2
-    
-    let mk l k v r : MapTree<'Key, 'Value> = 
+
+    let mk l k v r : MapTree<'Key, 'Value> =
         let hl = height l
         let hr = height r
         let m = if hl < hr then hr else hl
-        if m = 0 then // m=0 ~ isEmpty l && isEmpty r 
-            MapTree(k,v)
+
+        if m = 0 then // m=0 ~ isEmpty l && isEmpty r
+            MapTree(k, v)
         else
-            MapTreeNode(k,v,l,r,m+1) :> MapTree<'Key, 'Value>  // new map is higher by 1 than the highest
-        
+            MapTreeNode(k, v, l, r, m + 1) :> MapTree<'Key, 'Value> // new map is higher by 1 than the highest
+
     let rebalance t1 (k: 'Key) (v: 'Value) t2 : MapTree<'Key, 'Value> =
         let t1h = height t1
-        let t2h = height t2 
-        if  t2h > t1h + tolerance then (* right is heavier than left *)
-            let t2' = asNode(t2)
+        let t2h = height t2
+
+        if t2h > t1h + tolerance then (* right is heavier than left *)
+            let t2' = asNode (t2)
             (* one of the nodes must have height > height t1 + 1 *)
-            if height t2'.Left > t1h + 1 then  (* balance left: combination *)
-                let t2l = asNode(t2'.Left)
+            if height t2'.Left > t1h + 1 then (* balance left: combination *)
+                let t2l = asNode (t2'.Left)
                 mk (mk t1 k v t2l.Left) t2l.Key t2l.Value (mk t2l.Right t2'.Key t2'.Value t2'.Right)
             else (* rotate left *)
                 mk (mk t1 k v t2'.Left) t2'.Key t2'.Value t2'.Right
-        else
-            if  t1h > t2h + tolerance then (* left is heavier than right *)
-                let t1' = asNode(t1)
-                (* one of the nodes must have height > height t2 + 1 *)
-                if height t1'.Right > t2h + 1 then 
+        else if t1h > t2h + tolerance then (* left is heavier than right *)
+            let t1' = asNode (t1)
+            (* one of the nodes must have height > height t2 + 1 *)
+            if height t1'.Right > t2h + 1 then
                 (* balance right: combination *)
-                    let t1r = asNode(t1'.Right)
-                    mk (mk t1'.Left t1'.Key t1'.Value t1r.Left) t1r.Key t1r.Value (mk t1r.Right k v t2)
-                else
-                    mk t1'.Left t1'.Key t1'.Value (mk t1'.Right k v t2)
-            else mk t1 k v t2
-            
-    let rec add (comparer: IComparer<'Key>) k (v: 'Value) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> = 
-        if isEmpty m then MapTree(k,v)
+                let t1r = asNode (t1'.Right)
+                mk (mk t1'.Left t1'.Key t1'.Value t1r.Left) t1r.Key t1r.Value (mk t1r.Right k v t2)
+            else
+                mk t1'.Left t1'.Key t1'.Value (mk t1'.Right k v t2)
         else
-            let c = comparer.Compare(k,m.Key)
+            mk t1 k v t2
+
+    let rec add (comparer: IComparer<'Key>) k (v: 'Value) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> =
+        if isEmpty m then
+            MapTree(k, v)
+        else
+            let c = comparer.Compare(k, m.Key)
+
             if m.Height = 1 then
-                if c < 0   then MapTreeNode (k,v,empty,m,2) :> MapTree<'Key, 'Value>
-                elif c = 0 then MapTree(k,v)
-                else            MapTreeNode (k,v,m,empty,2) :> MapTree<'Key, 'Value> 
+                if c < 0 then
+                    MapTreeNode(k, v, empty, m, 2) :> MapTree<'Key, 'Value>
+                elif c = 0 then
+                    MapTree(k, v)
+                else
+                    MapTreeNode(k, v, m, empty, 2) :> MapTree<'Key, 'Value>
             else
                 let mn = asNode m
-                if c < 0 then rebalance (add comparer k v mn.Left) mn.Key mn.Value mn.Right
-                elif c = 0 then MapTreeNode(k,v,mn.Left,mn.Right,mn.Height) :> MapTree<'Key, 'Value>
-                else rebalance mn.Left mn.Key mn.Value (add comparer k v mn.Right)
-                
-    let rec tryGetValue (comparer: IComparer<'Key>) k (v: byref<'Value>) (m: MapTree<'Key, 'Value>) =                     
-        if isEmpty m then false
+
+                if c < 0 then
+                    rebalance (add comparer k v mn.Left) mn.Key mn.Value mn.Right
+                elif c = 0 then
+                    MapTreeNode(k, v, mn.Left, mn.Right, mn.Height) :> MapTree<'Key, 'Value>
+                else
+                    rebalance mn.Left mn.Key mn.Value (add comparer k v mn.Right)
+
+    let rec tryGetValue (comparer: IComparer<'Key>) k (v: byref<'Value>) (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            false
         else
             let c = comparer.Compare(k, m.Key)
-            if c = 0 then v <- m.Value; true
+
+            if c = 0 then
+                v <- m.Value
+                true
+            else if m.Height = 1 then
+                false
             else
-                if m.Height = 1 then false
-                else
-                    let mn = asNode m
-                    tryGetValue comparer k &v (if c < 0 then mn.Left else mn.Right)
-                
+                let mn = asNode m
+                tryGetValue comparer k &v (if c < 0 then mn.Left else mn.Right)
+
     []
-    let throwKeyNotFound() = raise (KeyNotFoundException())
-    
+    let throwKeyNotFound () =
+        raise (KeyNotFoundException())
+
     []
     let find (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) =
         let mutable v = Unchecked.defaultof<'Value>
+
         if tryGetValue comparer k &v m then
             v
         else
-            throwKeyNotFound()
+            throwKeyNotFound ()
 
-    let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = 
+    let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) =
         let mutable v = Unchecked.defaultof<'Value>
+
         if tryGetValue comparer k &v m then
             Some v
         else
             None
 
-    let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) = 
-        if f.Invoke (k, v) then (add comparer k v acc1, acc2) else (acc1, add comparer k v acc2) 
+    let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) =
+        if f.Invoke(k, v) then
+            (add comparer k v acc1, acc2)
+        else
+            (acc1, add comparer k v acc2)
 
-    let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = 
-        if isEmpty m then acc
+    let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc =
+        if isEmpty m then
+            acc
+        else if m.Height = 1 then
+            partition1 comparer f m.Key m.Value acc
         else
-            if m.Height = 1 then        
-                partition1 comparer f m.Key m.Value acc
-            else
-                let mn = asNode m
-                let acc = partitionAux comparer f mn.Right acc 
-                let acc = partition1 comparer f mn.Key mn.Value acc
-                partitionAux comparer f mn.Left acc
-            
+            let mn = asNode m
+            let acc = partitionAux comparer f mn.Right acc
+            let acc = partition1 comparer f mn.Key mn.Value acc
+            partitionAux comparer f mn.Left acc
+
     let partition (comparer: IComparer<'Key>) f m =
         partitionAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m (empty, empty)
 
     let filter1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v acc =
-        if f.Invoke (k, v) then add comparer k v acc else acc 
+        if f.Invoke(k, v) then
+            add comparer k v acc
+        else
+            acc
 
-    let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc = 
-        if isEmpty m then acc
+    let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc =
+        if isEmpty m then
+            acc
+        else if m.Height = 1 then
+            filter1 comparer f m.Key m.Value acc
         else
-            if m.Height = 1 then  
-                filter1 comparer f m.Key m.Value acc
-            else
-                let mn = asNode m
-                let acc = filterAux comparer f mn.Left acc
-                let acc = filter1 comparer f mn.Key mn.Value acc
-                filterAux comparer f mn.Right acc
-            
+            let mn = asNode m
+            let acc = filterAux comparer f mn.Left acc
+            let acc = filter1 comparer f mn.Key mn.Value acc
+            filterAux comparer f mn.Right acc
 
     let filter (comparer: IComparer<'Key>) f m =
         filterAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m empty
 
-    let rec spliceOutSuccessor (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then failwith "internal error: Map.spliceOutSuccessor"
+    let rec spliceOutSuccessor (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            failwith "internal error: Map.spliceOutSuccessor"
+        else if m.Height = 1 then
+            m.Key, m.Value, empty
         else
-            if m.Height = 1 then
-                m.Key, m.Value, empty
+            let mn = asNode m
+
+            if isEmpty mn.Left then
+                mn.Key, mn.Value, mn.Right
             else
-                let mn = asNode m
-                if isEmpty mn.Left then mn.Key, mn.Value, mn.Right
-                else let k3, v3, l' = spliceOutSuccessor mn.Left in k3, v3, mk l' mn.Key mn.Value mn.Right
+                let k3, v3, l' = spliceOutSuccessor mn.Left in k3, v3, mk l' mn.Key mn.Value mn.Right
 
-    let rec remove (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then empty
+    let rec remove (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            empty
         else
             let c = comparer.Compare(k, m.Key)
-            if m.Height = 1 then 
+
+            if m.Height = 1 then
                 if c = 0 then empty else m
             else
-                let mn = asNode m 
-                if c < 0 then rebalance (remove comparer k mn.Left) mn.Key mn.Value mn.Right
+                let mn = asNode m
+
+                if c < 0 then
+                    rebalance (remove comparer k mn.Left) mn.Key mn.Value mn.Right
                 elif c = 0 then
-                    if isEmpty mn.Left then mn.Right
-                    elif isEmpty mn.Right then mn.Left
+                    if isEmpty mn.Left then
+                        mn.Right
+                    elif isEmpty mn.Right then
+                        mn.Left
                     else
-                        let sk, sv, r' = spliceOutSuccessor mn.Right 
+                        let sk, sv, r' = spliceOutSuccessor mn.Right
                         mk mn.Left sk sv r'
-                else rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right)
-            
+                else
+                    rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right)
 
-    let rec change (comparer: IComparer<'Key>) k (u: 'Value option -> 'Value option) (m: MapTree<'Key, 'Value>) : MapTree<'Key,'Value> =
+    let rec change (comparer: IComparer<'Key>) k (u: 'Value option -> 'Value option) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> =
         if isEmpty m then
             match u None with
+            | None -> m
+            | Some v -> MapTree(k, v)
+        else if m.Height = 1 then
+            let c = comparer.Compare(k, m.Key)
+
+            if c < 0 then
+                match u None with
+                | None -> m
+                | Some v -> MapTreeNode(k, v, empty, m, 2) :> MapTree<'Key, 'Value>
+            elif c = 0 then
+                match u (Some m.Value) with
+                | None -> empty
+                | Some v -> MapTree(k, v)
+            else
+                match u None with
                 | None -> m
-                | Some v -> MapTree (k, v)
+                | Some v -> MapTreeNode(k, v, m, empty, 2) :> MapTree<'Key, 'Value>
         else
-            if m.Height = 1 then
-                let c = comparer.Compare(k, m.Key)
-                if c < 0 then
-                    match u None with
-                    | None -> m
-                    | Some v -> MapTreeNode (k, v, empty, m, 2) :> MapTree<'Key,'Value>
-                elif c = 0 then
-                    match u (Some m.Value) with
-                    | None -> empty
-                    | Some v -> MapTree (k, v)
-                else
-                    match u None with
-                    | None -> m
-                    | Some v -> MapTreeNode (k, v, m, empty, 2) :> MapTree<'Key,'Value>
+            let mn = asNode m
+            let c = comparer.Compare(k, mn.Key)
+
+            if c < 0 then
+                rebalance (change comparer k u mn.Left) mn.Key mn.Value mn.Right
+            elif c = 0 then
+                match u (Some mn.Value) with
+                | None ->
+                    if isEmpty mn.Left then
+                        mn.Right
+                    elif isEmpty mn.Right then
+                        mn.Left
+                    else
+                        let sk, sv, r' = spliceOutSuccessor mn.Right
+                        mk mn.Left sk sv r'
+                | Some v -> MapTreeNode(k, v, mn.Left, mn.Right, mn.Height) :> MapTree<'Key, 'Value>
             else
-                let mn = asNode m
-                let c = comparer.Compare(k, mn.Key)
-                if c < 0 then
-                    rebalance (change comparer k u mn.Left) mn.Key mn.Value mn.Right
-                elif c = 0 then
-                    match u (Some mn.Value) with
-                    | None ->
-                        if isEmpty mn.Left then mn.Right
-                        elif isEmpty mn.Right then mn.Left
-                        else
-                            let sk, sv, r' = spliceOutSuccessor mn.Right
-                            mk mn.Left sk sv r'
-                    | Some v -> MapTreeNode (k, v, mn.Left, mn.Right, mn.Height) :> MapTree<'Key,'Value>
-                else
-                    rebalance mn.Left mn.Key mn.Value (change comparer k u mn.Right)
+                rebalance mn.Left mn.Key mn.Value (change comparer k u mn.Right)
 
-    let rec mem (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then false
+    let rec mem (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            false
         else
             let c = comparer.Compare(k, m.Key)
-            if m.Height = 1 then 
+
+            if m.Height = 1 then
                 c = 0
             else
                 let mn = asNode m
-                if c < 0 then mem comparer k mn.Left
-                else (c = 0 || mem comparer k mn.Right)
-            
+
+                if c < 0 then
+                    mem comparer k mn.Left
+                else
+                    (c = 0 || mem comparer k mn.Right)
 
     let rec iterOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) =
-        if isEmpty m then ()
+        if isEmpty m then
+            ()
+        else if m.Height = 1 then
+            f.Invoke(m.Key, m.Value)
         else
-            if m.Height = 1 then 
-                f.Invoke (m.Key, m.Value)
-            else
-                let mn = asNode m
-                iterOpt f mn.Left; f.Invoke (mn.Key, mn.Value); iterOpt f mn.Right
-            
+            let mn = asNode m
+            iterOpt f mn.Left
+            f.Invoke(mn.Key, mn.Value)
+            iterOpt f mn.Right
 
     let iter f m =
         iterOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m
 
     let rec tryPickOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) =
-        if isEmpty m then None
+        if isEmpty m then
+            None
+        else if m.Height = 1 then
+            f.Invoke(m.Key, m.Value)
         else
-            if m.Height = 1 then 
-                f.Invoke (m.Key, m.Value)
-            else
-                let mn = asNode m
-                match tryPickOpt f mn.Left with 
-                | Some _ as res -> res 
-                | None -> 
-                match f.Invoke (mn.Key, mn.Value) with 
-                | Some _ as res -> res 
-                | None -> 
-                tryPickOpt f mn.Right
-            
+            let mn = asNode m
+
+            match tryPickOpt f mn.Left with
+            | Some _ as res -> res
+            | None ->
+                match f.Invoke(mn.Key, mn.Value) with
+                | Some _ as res -> res
+                | None -> tryPickOpt f mn.Right
 
     let tryPick f m =
         tryPickOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m
 
-    let rec existsOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then false
+    let rec existsOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            false
+        else if m.Height = 1 then
+            f.Invoke(m.Key, m.Value)
         else
-            if m.Height = 1 then 
-                f.Invoke (m.Key, m.Value)
-            else
-                let mn = asNode m
-                existsOpt f mn.Left || f.Invoke (mn.Key, mn.Value) || existsOpt f mn.Right
-            
+            let mn = asNode m
+            existsOpt f mn.Left || f.Invoke(mn.Key, mn.Value) || existsOpt f mn.Right
 
     let exists f m =
         existsOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m
 
-    let rec forallOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then true
+    let rec forallOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            true
+        else if m.Height = 1 then
+            f.Invoke(m.Key, m.Value)
         else
-            if m.Height = 1 then 
-                f.Invoke (m.Key, m.Value)
-            else
-                let mn = asNode m
-                forallOpt f mn.Left && f.Invoke (mn.Key, mn.Value) && forallOpt f mn.Right
-            
-            
+            let mn = asNode m
+            forallOpt f mn.Left && f.Invoke(mn.Key, mn.Value) && forallOpt f mn.Right
 
     let forall f m =
         forallOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m
 
-    let rec map (f:'Value -> 'Result) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Result> = 
-        if isEmpty m then empty
+    let rec map (f: 'Value -> 'Result) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Result> =
+        if isEmpty m then
+            empty
+        else if m.Height = 1 then
+            MapTree(m.Key, f m.Value)
         else
-            if m.Height = 1 then 
-                MapTree (m.Key, f m.Value)
-            else
-                let mn = asNode m
-                let l2 = map f mn.Left 
-                let v2 = f mn.Value
-                let r2 = map f mn.Right
-                MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result>
+            let mn = asNode m
+            let l2 = map f mn.Left
+            let v2 = f mn.Value
+            let r2 = map f mn.Right
+            MapTreeNode(mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result>
 
-    let rec mapiOpt (f: OptimizedClosures.FSharpFunc<'Key, 'Value, 'Result>) (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then empty
+    let rec mapiOpt (f: OptimizedClosures.FSharpFunc<'Key, 'Value, 'Result>) (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            empty
+        else if m.Height = 1 then
+            MapTree(m.Key, f.Invoke(m.Key, m.Value))
         else
-            if m.Height = 1 then
-                MapTree (m.Key, f.Invoke (m.Key, m.Value))
-            else
-                let mn = asNode m
-                let l2 = mapiOpt f mn.Left
-                let v2 = f.Invoke (mn.Key, mn.Value) 
-                let r2 = mapiOpt f mn.Right
-                MapTreeNode (mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result>
-            
+            let mn = asNode m
+            let l2 = mapiOpt f mn.Left
+            let v2 = f.Invoke(mn.Key, mn.Value)
+            let r2 = mapiOpt f mn.Right
+            MapTreeNode(mn.Key, v2, l2, r2, mn.Height) :> MapTree<'Key, 'Result>
 
     let mapi f m =
         mapiOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m
 
-    let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = 
-        if isEmpty m then x
+    let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x =
+        if isEmpty m then
+            x
+        else if m.Height = 1 then
+            f.Invoke(m.Key, m.Value, x)
         else
-            if m.Height = 1 then 
-                f.Invoke (m.Key, m.Value, x)
-            else
-                let mn = asNode m
-                let x = foldBackOpt f mn.Right x
-                let x = f.Invoke (mn.Key, mn.Value, x)
-                foldBackOpt f mn.Left x
-            
+            let mn = asNode m
+            let x = foldBackOpt f mn.Right x
+            let x = f.Invoke(mn.Key, mn.Value, x)
+            foldBackOpt f mn.Left x
 
     let foldBack f m x =
         foldBackOpt (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x
 
-    let rec foldOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) x (m: MapTree<'Key, 'Value>) = 
-        if isEmpty m then x
+    let rec foldOpt (f: OptimizedClosures.FSharpFunc<_, _, _, _>) x (m: MapTree<'Key, 'Value>) =
+        if isEmpty m then
+            x
+        else if m.Height = 1 then
+            f.Invoke(x, m.Key, m.Value)
         else
-            if m.Height = 1 then 
-                f.Invoke (x, m.Key, m.Value)
-            else
-                let mn = asNode m
-                let x = foldOpt f x mn.Left
-                let x = f.Invoke (x, mn.Key, mn.Value)
-                foldOpt f x mn.Right
+            let mn = asNode m
+            let x = foldOpt f x mn.Left
+            let x = f.Invoke(x, mn.Key, mn.Value)
+            foldOpt f x mn.Right
 
     let fold f x m =
         foldOpt (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) x m
 
     let foldSectionOpt (comparer: IComparer<'Key>) lo hi (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x =
-        let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x = 
-            if isEmpty m then x
+        let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x =
+            if isEmpty m then
+                x
+            else if m.Height = 1 then
+                let cLoKey = comparer.Compare(lo, m.Key)
+                let cKeyHi = comparer.Compare(m.Key, hi)
+
+                let x =
+                    if cLoKey <= 0 && cKeyHi <= 0 then
+                        f.Invoke(m.Key, m.Value, x)
+                    else
+                        x
+
+                x
             else
-                if m.Height = 1 then 
-                    let cLoKey = comparer.Compare(lo, m.Key)
-                    let cKeyHi = comparer.Compare(m.Key, hi)
-                    let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (m.Key, m.Value, x) else x
-                    x
-                else
-                    let mn = asNode m
-                    let cLoKey = comparer.Compare(lo, mn.Key)
-                    let cKeyHi = comparer.Compare(mn.Key, hi)
-                    let x = if cLoKey < 0 then foldFromTo f mn.Left x else x
-                    let x = if cLoKey <= 0 && cKeyHi <= 0 then f.Invoke (mn.Key, mn.Value, x) else x
-                    let x = if cKeyHi < 0 then foldFromTo f mn.Right x else x
-                    x
+                let mn = asNode m
+                let cLoKey = comparer.Compare(lo, mn.Key)
+                let cKeyHi = comparer.Compare(mn.Key, hi)
 
-        if comparer.Compare(lo, hi) = 1 then x else foldFromTo f m x
+                let x =
+                    if cLoKey < 0 then
+                        foldFromTo f mn.Left x
+                    else
+                        x
+
+                let x =
+                    if cLoKey <= 0 && cKeyHi <= 0 then
+                        f.Invoke(mn.Key, mn.Value, x)
+                    else
+                        x
+
+                let x =
+                    if cKeyHi < 0 then
+                        foldFromTo f mn.Right x
+                    else
+                        x
+
+                x
+
+        if comparer.Compare(lo, hi) = 1 then
+            x
+        else
+            foldFromTo f m x
 
     let foldSection (comparer: IComparer<'Key>) lo hi f m x =
         foldSectionOpt comparer lo hi (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x
 
-    let toList (m: MapTree<'Key, 'Value>) = 
-        let rec loop (m: MapTree<'Key, 'Value>) acc = 
-            if isEmpty m then acc
+    let toList (m: MapTree<'Key, 'Value>) =
+        let rec loop (m: MapTree<'Key, 'Value>) acc =
+            if isEmpty m then
+                acc
+            else if m.Height = 1 then
+                (m.Key, m.Value) :: acc
             else
-                if m.Height = 1 then
-                    (m.Key, m.Value) :: acc
-                else
-                    let mn = asNode m
-                    loop mn.Left ((mn.Key, mn.Value) :: loop mn.Right acc)
-                
+                let mn = asNode m
+                loop mn.Left ((mn.Key, mn.Value) :: loop mn.Right acc)
+
         loop m []
 
     let toArray m =
@@ -439,78 +506,92 @@ module MapTree =
     let ofList comparer l =
         List.fold (fun acc (k, v) -> add comparer k v acc) empty l
 
-    let rec mkFromEnumerator comparer acc (e : IEnumerator<_>) = 
-        if e.MoveNext() then 
-            let (x, y) = e.Current 
+    let rec mkFromEnumerator comparer acc (e: IEnumerator<_>) =
+        if e.MoveNext() then
+            let (x, y) = e.Current
             mkFromEnumerator comparer (add comparer x y acc) e
-        else acc
+        else
+            acc
 
-    let ofArray comparer (arr : array<'Key * 'Value>) =
+    let ofArray comparer (arr: array<'Key * 'Value>) =
         let mutable res = empty
+
         for (x, y) in arr do
-            res <- add comparer x y res 
+            res <- add comparer x y res
+
         res
 
-    let ofSeq comparer (c : seq<'Key * 'T>) =
-        match c with 
+    let ofSeq comparer (c: seq<'Key * 'T>) =
+        match c with
         | :? (('Key * 'T)[]) as xs -> ofArray comparer xs
         | :? (('Key * 'T) list) as xs -> ofList comparer xs
-        | _ -> 
+        | _ ->
             use ie = c.GetEnumerator()
-            mkFromEnumerator comparer empty ie 
+            mkFromEnumerator comparer empty ie
 
     let copyToArray m (arr: _[]) i =
-        let mutable j = i 
-        m |> iter (fun x y -> arr.[j] <- KeyValuePair(x, y); j <- j + 1)
+        let mutable j = i
+
+        m
+        |> iter (fun x y ->
+            arr.[j] <- KeyValuePair(x, y)
+            j <- j + 1)
 
     /// Imperative left-to-right iterators.
     []
-    type MapIterator<'Key, 'Value when 'Key : comparison > = 
-         { /// invariant: always collapseLHS result 
-           mutable stack: MapTree<'Key, 'Value> list
+    type MapIterator<'Key, 'Value when 'Key: comparison> =
+        {
+            /// invariant: always collapseLHS result
+            mutable stack: MapTree<'Key, 'Value> list
 
-           /// true when MoveNext has been called 
-           mutable started : bool }
+            /// true when MoveNext has been called
+            mutable started: bool
+        }
 
     // collapseLHS:
     // a) Always returns either [] or a list starting with MapOne.
-    // b) The "fringe" of the set stack is unchanged. 
-    let rec collapseLHS (stack:MapTree<'Key, 'Value> list) =
+    // b) The "fringe" of the set stack is unchanged.
+    let rec collapseLHS (stack: MapTree<'Key, 'Value> list) =
         match stack with
         | [] -> []
         | m :: rest ->
-            if isEmpty m then collapseLHS rest
+            if isEmpty m then
+                collapseLHS rest
+            else if m.Height = 1 then
+                stack
             else
-                if m.Height = 1 then
-                    stack
-                else
-                    let mn = asNode m
-                    collapseLHS (mn.Left :: MapTree (mn.Key, mn.Value) :: mn.Right :: rest)
+                let mn = asNode m
+                collapseLHS (mn.Left :: MapTree(mn.Key, mn.Value) :: mn.Right :: rest)
 
     let mkIterator m =
-        { stack = collapseLHS [m]; started = false }
+        {
+            stack = collapseLHS [ m ]
+            started = false
+        }
 
-    let notStarted() =
+    let notStarted () =
         raise (InvalidOperationException(SR.GetString(SR.enumerationNotStarted)))
 
-    let alreadyFinished() =
+    let alreadyFinished () =
         raise (InvalidOperationException(SR.GetString(SR.enumerationAlreadyFinished)))
-        
-    let unexpectedStackForCurrent() =
+
+    let unexpectedStackForCurrent () =
         failwith "Please report error: Map iterator, unexpected stack for current"
-        
-    let unexpectedStackForMoveNext() =
+
+    let unexpectedStackForMoveNext () =
         failwith "Please report error: Map iterator, unexpected stack for moveNext"
 
     let current i =
         if i.started then
             match i.stack with
-            | []     -> alreadyFinished()
+            | [] -> alreadyFinished ()
             | m :: _ ->
-                if m.Height = 1 then KeyValuePair<_, _>(m.Key, m.Value)
-                else unexpectedStackForCurrent()
+                if m.Height = 1 then
+                    KeyValuePair<_, _>(m.Key, m.Value)
+                else
+                    unexpectedStackForCurrent ()
         else
-            notStarted()
+            notStarted ()
 
     let rec moveNext i =
         if i.started then
@@ -520,54 +601,70 @@ module MapTree =
                 if m.Height = 1 then
                     i.stack <- collapseLHS rest
                     not i.stack.IsEmpty
-                else unexpectedStackForMoveNext()
+                else
+                    unexpectedStackForMoveNext ()
         else
-            i.started <- true  (* The first call to MoveNext "starts" the enumeration. *)
+            i.started <- true (* The first call to MoveNext "starts" the enumeration. *)
             not i.stack.IsEmpty
 
-    let mkIEnumerator m = 
-        let mutable i = mkIterator m 
-        { new IEnumerator<_> with 
-              member _.Current = current i
+    let mkIEnumerator m =
+        let mutable i = mkIterator m
 
+        { new IEnumerator<_> with
+            member _.Current = current i
           interface System.Collections.IEnumerator with
               member _.Current = box (current i)
-              member _.MoveNext() = moveNext i
-              member _.Reset() = i <- mkIterator m
 
-          interface System.IDisposable with 
-              member _.Dispose() = ()}
+              member _.MoveNext() =
+                  moveNext i
+
+              member _.Reset() =
+                  i <- mkIterator m
+          interface System.IDisposable with
+              member _.Dispose() =
+                  ()
+        }
 
     let rec leftmost m =
-        if isEmpty m then 
-            throwKeyNotFound()
+        if isEmpty m then
+            throwKeyNotFound ()
         else if m.Height = 1 then
             (m.Key, m.Value)
         else
-           let nd = asNode m
-           if isNull nd.Left then (m.Key, m.Value)
-           else leftmost nd.Left 
-        
+            let nd = asNode m
+
+            if isNull nd.Left then
+                (m.Key, m.Value)
+            else
+                leftmost nd.Left
+
     let rec rightmost m =
-        if isEmpty m then 
-            throwKeyNotFound()
+        if isEmpty m then
+            throwKeyNotFound ()
         else if m.Height = 1 then
             (m.Key, m.Value)
         else
-           let nd = asNode m
-           if isNull nd.Right then (m.Key, m.Value)
-           else rightmost nd.Right 
+            let nd = asNode m
+
+            if isNull nd.Right then
+                (m.Key, m.Value)
+            else
+                rightmost nd.Right
 
 [>)>]
 []
 []
 []
-type Map<[]'Key, []'Value when 'Key : comparison >(comparer: IComparer<'Key>, tree: MapTree<'Key, 'Value>) =
+type Map<[] 'Key, [] 'Value when 'Key: comparison>
+    (
+        comparer: IComparer<'Key>,
+        tree: MapTree<'Key, 'Value>
+    ) =
 
     []
     // This type is logically immutable. This field is only mutated during deserialization.
     let mutable comparer = comparer
- 
+
     []
     // This type is logically immutable. This field is only mutated during deserialization.
     let mutable tree = tree
@@ -580,8 +677,8 @@ type Map<[]'Key, [ 
+    static let empty =
+        let comparer = LanguagePrimitives.FastGenericComparer<'Key>
         new Map<'Key, 'Value>(comparer, MapTree.empty)
 
     []
@@ -598,18 +695,22 @@ type Map<[]'Key, [
-        tree <- serializedData |> Array.map (fun kvp -> kvp.Key, kvp.Value) |> MapTree.ofArray comparer
+
+        tree <-
+            serializedData
+            |> Array.map (fun kvp -> kvp.Key, kvp.Value)
+            |> MapTree.ofArray comparer
+
         serializedData <- null
 
-    static member Empty : Map<'Key, 'Value> =
-        empty
+    static member Empty: Map<'Key, 'Value> = empty
 
-    static member Create(ie : IEnumerable<_>) : Map<'Key, 'Value> = 
-        let comparer = LanguagePrimitives.FastGenericComparer<'Key> 
+    static member Create(ie: IEnumerable<_>) : Map<'Key, 'Value> =
+        let comparer = LanguagePrimitives.FastGenericComparer<'Key>
         new Map<_, _>(comparer, MapTree.ofSeq comparer ie)
 
-    new (elements : seq<_>) = 
-        let comparer = LanguagePrimitives.FastGenericComparer<'Key> 
+    new(elements: seq<_>) =
+        let comparer = LanguagePrimitives.FastGenericComparer<'Key>
         new Map<_, _>(comparer, MapTree.ofSeq comparer elements)
 
     []
@@ -618,13 +719,14 @@ type Map<[]'Key, []
     member internal m.Tree = tree
 
-    member m.Add(key, value) : Map<'Key, 'Value> = 
+    member m.Add(key, value) : Map<'Key, 'Value> =
 #if TRACE_SETS_AND_MAPS
-        MapTree.report()
+        MapTree.report ()
         MapTree.numAdds <- MapTree.numAdds + 1
         let size = MapTree.size m.Tree + 1
         MapTree.totalSizeOnMapAdd <- MapTree.totalSizeOnMapAdd + float size
-        if size > MapTree.largestMapSize then 
+
+        if size > MapTree.largestMapSize then
             MapTree.largestMapSize <- size
             MapTree.largestMapStackTrace <- System.Diagnostics.StackTrace().ToString()
 #endif
@@ -636,66 +738,65 @@ type Map<[]'Key, []
     member m.IsEmpty = MapTree.isEmpty tree
 
-    member m.Item 
-     with get(key : 'Key) = 
+    member m.Item
+        with get (key: 'Key) =
 #if TRACE_SETS_AND_MAPS
-        MapTree.report()
-        MapTree.numLookups <- MapTree.numLookups + 1
-        MapTree.totalSizeOnMapLookup <- MapTree.totalSizeOnMapLookup + float (MapTree.size tree)
+            MapTree.report ()
+            MapTree.numLookups <- MapTree.numLookups + 1
+            MapTree.totalSizeOnMapLookup <- MapTree.totalSizeOnMapLookup + float (MapTree.size tree)
 #endif
-        MapTree.find comparer key tree
+            MapTree.find comparer key tree
 
     member m.TryPick f =
-        MapTree.tryPick f tree 
+        MapTree.tryPick f tree
 
     member m.Exists predicate =
-        MapTree.exists predicate tree 
+        MapTree.exists predicate tree
 
     member m.Filter predicate =
         new Map<'Key, 'Value>(comparer, MapTree.filter comparer predicate tree)
 
     member m.ForAll predicate =
-        MapTree.forall predicate tree 
+        MapTree.forall predicate tree
 
     member m.Fold f acc =
         MapTree.foldBack f tree acc
 
-    member m.FoldSection (lo:'Key) (hi:'Key) f (acc:'z) =
-        MapTree.foldSection comparer lo hi f tree acc 
+    member m.FoldSection (lo: 'Key) (hi: 'Key) f (acc: 'z) =
+        MapTree.foldSection comparer lo hi f tree acc
 
     member m.Iterate f =
         MapTree.iter f tree
 
-    member m.MapRange (f:'Value->'Result) =
+    member m.MapRange(f: 'Value -> 'Result) =
         new Map<'Key, 'Result>(comparer, MapTree.map f tree)
 
     member m.Map f =
         new Map<'Key, 'b>(comparer, MapTree.mapi f tree)
 
-    member m.Partition predicate : Map<'Key, 'Value> * Map<'Key, 'Value> = 
+    member m.Partition predicate : Map<'Key, 'Value> * Map<'Key, 'Value> =
         let r1, r2 = MapTree.partition comparer predicate tree
         new Map<'Key, 'Value>(comparer, r1), new Map<'Key, 'Value>(comparer, r2)
 
-    member m.Count =
-        MapTree.size tree
+    member m.Count = MapTree.size tree
 
-    member m.ContainsKey key = 
+    member m.ContainsKey key =
 #if TRACE_SETS_AND_MAPS
-        MapTree.report()
+        MapTree.report ()
         MapTree.numLookups <- MapTree.numLookups + 1
         MapTree.totalSizeOnMapLookup <- MapTree.totalSizeOnMapLookup + float (MapTree.size tree)
 #endif
         MapTree.mem comparer key tree
 
-    member m.Remove key = 
+    member m.Remove key =
         new Map<'Key, 'Value>(comparer, MapTree.remove comparer key tree)
 
-    member m.TryGetValue(key, [] value: byref<'Value>) = 
+    member m.TryGetValue(key, [] value: byref<'Value>) =
         MapTree.tryGetValue comparer key &value tree
 
-    member m.TryFind key = 
+    member m.TryFind key =
 #if TRACE_SETS_AND_MAPS
-        MapTree.report()
+        MapTree.report ()
         MapTree.numLookups <- MapTree.numLookups + 1
         MapTree.totalSizeOnMapLookup <- MapTree.totalSizeOnMapLookup + float (MapTree.size tree)
 #endif
@@ -708,209 +809,296 @@ type Map<[]'Key, [ ICollection<'Key>
-    
+
     member m.Values = ValueCollection(m) :> ICollection<'Value>
-    
+
     member m.MinKeyValue = MapTree.leftmost tree
     member m.MaxKeyValue = MapTree.rightmost tree
 
-    static member ofList l : Map<'Key, 'Value> = 
-       let comparer = LanguagePrimitives.FastGenericComparer<'Key> 
-       new Map<_, _>(comparer, MapTree.ofList comparer l)
+    static member ofList l : Map<'Key, 'Value> =
+        let comparer = LanguagePrimitives.FastGenericComparer<'Key>
+        new Map<_, _>(comparer, MapTree.ofList comparer l)
+
+    member this.ComputeHashCode() =
+        let combineHash x y =
+            (x <<< 1) + y + 631
 
-    member this.ComputeHashCode() = 
-        let combineHash x y = (x <<< 1) + y + 631 
         let mutable res = 0
-        for (KeyValue(x, y)) in this do
+
+        for (KeyValue (x, y)) in this do
             res <- combineHash res (hash x)
             res <- combineHash res (Unchecked.hash y)
+
         res
 
-    override this.Equals that = 
-        match that with 
-        | :? Map<'Key, 'Value> as that -> 
-            use e1 = (this :> seq<_>).GetEnumerator() 
-            use e2 = (that :> seq<_>).GetEnumerator() 
-            let rec loop () = 
-                let m1 = e1.MoveNext() 
+    override this.Equals that =
+        match that with
+        | :? Map<'Key, 'Value> as that ->
+            use e1 = (this :> seq<_>).GetEnumerator()
+            use e2 = (that :> seq<_>).GetEnumerator()
+
+            let rec loop () =
+                let m1 = e1.MoveNext()
                 let m2 = e2.MoveNext()
-                (m1 = m2) && (not m1 || 
-                                 (let e1c = e1.Current
-                                  let e2c = e2.Current
-                                  ((e1c.Key = e2c.Key) && (Unchecked.equals e1c.Value e2c.Value) && loop())))
-            loop()
+
+                (m1 = m2)
+                && (not m1
+                    || (let e1c = e1.Current
+                        let e2c = e2.Current
+                        ((e1c.Key = e2c.Key) && (Unchecked.equals e1c.Value e2c.Value) && loop ())))
+
+            loop ()
         | _ -> false
 
-    override this.GetHashCode() = this.ComputeHashCode()
+    override this.GetHashCode() =
+        this.ComputeHashCode()
 
     interface IEnumerable> with
-        member _.GetEnumerator() = MapTree.mkIEnumerator tree
+        member _.GetEnumerator() =
+            MapTree.mkIEnumerator tree
 
     interface IEnumerable with
-        member _.GetEnumerator() = (MapTree.mkIEnumerator tree :> IEnumerator)
+        member _.GetEnumerator() =
+            (MapTree.mkIEnumerator tree :> IEnumerator)
 
-    interface IDictionary<'Key, 'Value> with 
-        member m.Item 
-            with get x = m.[x] 
-            and  set _ _ = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+    interface IDictionary<'Key, 'Value> with
+        member m.Item
+            with get x = m.[x]
+            and set _ _ = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
         member m.Keys = m.Keys
 
         member m.Values = m.Values
 
-        member m.Add(_, _) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member m.Add(_, _) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member m.ContainsKey k = m.ContainsKey k
+        member m.ContainsKey k =
+            m.ContainsKey k
 
-        member m.TryGetValue(k, r) = m.TryGetValue(k, &r) 
+        member m.TryGetValue(k, r) =
+            m.TryGetValue(k, &r)
 
-        member m.Remove(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member m.Remove(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-    interface ICollection> with 
-        member _.Add(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+    interface ICollection> with
+        member _.Add(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Clear() = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Clear() =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Remove(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Remove(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member m.Contains x = m.ContainsKey x.Key && Unchecked.equals m.[x.Key] x.Value
+        member m.Contains x =
+            m.ContainsKey x.Key && Unchecked.equals m.[x.Key] x.Value
 
-        member _.CopyTo(arr, i) = MapTree.copyToArray tree arr i
+        member _.CopyTo(arr, i) =
+            MapTree.copyToArray tree arr i
 
         member _.IsReadOnly = true
 
         member m.Count = m.Count
 
-    interface System.IComparable with 
-        member m.CompareTo(obj: obj) = 
-            match obj with 
-            | :? Map<'Key, 'Value>  as m2->
-                Seq.compareWith 
-                   (fun (kvp1 : KeyValuePair<_, _>) (kvp2 : KeyValuePair<_, _>)-> 
-                       let c = comparer.Compare(kvp1.Key, kvp2.Key) in 
-                       if c <> 0 then c else Unchecked.compare kvp1.Value kvp2.Value)
-                   m m2 
-            | _ -> 
-                invalidArg "obj" (SR.GetString(SR.notComparable))
+    interface System.IComparable with
+        member m.CompareTo(obj: obj) =
+            match obj with
+            | :? Map<'Key, 'Value> as m2 ->
+                Seq.compareWith
+                    (fun (kvp1: KeyValuePair<_, _>) (kvp2: KeyValuePair<_, _>) ->
+                        let c = comparer.Compare(kvp1.Key, kvp2.Key) in
+
+                        if c <> 0 then
+                            c
+                        else
+                            Unchecked.compare kvp1.Value kvp2.Value)
+                    m
+                    m2
+            | _ -> invalidArg "obj" (SR.GetString(SR.notComparable))
 
     interface IReadOnlyCollection> with
         member m.Count = m.Count
 
     interface IReadOnlyDictionary<'Key, 'Value> with
 
-        member m.Item with get key = m.[key]
+        member m.Item
+            with get key = m.[key]
 
         member m.Keys = m.Keys :> IEnumerable<'Key>
 
-        member m.TryGetValue(key, value: byref<'Value>) = m.TryGetValue(key, &value) 
+        member m.TryGetValue(key, value: byref<'Value>) =
+            m.TryGetValue(key, &value)
 
         member m.Values = m.Values :> IEnumerable<'Value>
 
-        member m.ContainsKey key = m.ContainsKey key
+        member m.ContainsKey key =
+            m.ContainsKey key
 
-    override x.ToString() = 
-        match List.ofSeq (Seq.truncate 4 x) with 
+    override x.ToString() =
+        match List.ofSeq (Seq.truncate 4 x) with
         | [] -> "map []"
-        | [KeyValue h1] ->
+        | [ KeyValue h1 ] ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             StringBuilder().Append("map [").Append(txt1).Append("]").ToString()
-        | [KeyValue h1; KeyValue h2] ->
+        | [ KeyValue h1; KeyValue h2 ] ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             let txt2 = LanguagePrimitives.anyToStringShowingNull h2
-            StringBuilder().Append("map [").Append(txt1).Append("; ").Append(txt2).Append("]").ToString()
-        | [KeyValue h1; KeyValue h2; KeyValue h3] ->
+
+            StringBuilder()
+                .Append("map [")
+                .Append(txt1)
+                .Append("; ")
+                .Append(txt2)
+                .Append("]")
+                .ToString()
+        | [ KeyValue h1; KeyValue h2; KeyValue h3 ] ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             let txt2 = LanguagePrimitives.anyToStringShowingNull h2
             let txt3 = LanguagePrimitives.anyToStringShowingNull h3
-            StringBuilder().Append("map [").Append(txt1).Append("; ").Append(txt2).Append("; ").Append(txt3).Append("]").ToString()
+
+            StringBuilder()
+                .Append("map [")
+                .Append(txt1)
+                .Append("; ")
+                .Append(txt2)
+                .Append("; ")
+                .Append(txt3)
+                .Append("]")
+                .ToString()
         | KeyValue h1 :: KeyValue h2 :: KeyValue h3 :: _ ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             let txt2 = LanguagePrimitives.anyToStringShowingNull h2
             let txt3 = LanguagePrimitives.anyToStringShowingNull h3
-            StringBuilder().Append("map [").Append(txt1).Append("; ").Append(txt2).Append("; ").Append(txt3).Append("; ... ]").ToString() 
 
-and
-    []
-    MapDebugView<'Key, 'Value when 'Key : comparison>(v: Map<'Key, 'Value>)  = 
+            StringBuilder()
+                .Append("map [")
+                .Append(txt1)
+                .Append("; ")
+                .Append(txt2)
+                .Append("; ")
+                .Append(txt3)
+                .Append("; ... ]")
+                .ToString()
+
+and [] MapDebugView<'Key, 'Value when 'Key: comparison>(v: Map<'Key, 'Value>) =
 
-        []
-        member x.Items =
-            v |> Seq.truncate 10000 |> Seq.map KeyValuePairDebugFriendly |> Seq.toArray
+    []
+    member x.Items =
+        v |> Seq.truncate 10000 |> Seq.map KeyValuePairDebugFriendly |> Seq.toArray
 
-and
-    []
-    KeyValuePairDebugFriendly<'Key, 'Value>(keyValue : KeyValuePair<'Key, 'Value>) =
+and [] KeyValuePairDebugFriendly<'Key, 'Value>
+    (
+        keyValue: KeyValuePair<'Key, 'Value>
+    ) =
 
-        []
-        member x.KeyValue = keyValue
+    []
+    member x.KeyValue = keyValue
 
-and KeyCollection<'Key, 'Value when 'Key : comparison>(parent: Map<'Key, 'Value>) =
+and KeyCollection<'Key, 'Value when 'Key: comparison>(parent: Map<'Key, 'Value>) =
     interface ICollection<'Key> with
-        member _.Add(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
-        
-        member _.Clear() = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Add(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Remove(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Clear() =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Contains x = parent.ContainsKey x
+        member _.Remove(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+
+        member _.Contains x =
+            parent.ContainsKey x
 
         member _.CopyTo(arr, index) =
             if isNull arr then nullArg "arr"
-            if index < 0 then invalidArg "index" "index must be positive"
-            if index + parent.Count > arr.Length then invalidArg "index" "array is smaller than index plus the number of items to copy"
-             
+
+            if index < 0 then
+                invalidArg "index" "index must be positive"
+
+            if index + parent.Count > arr.Length then
+                invalidArg "index" "array is smaller than index plus the number of items to copy"
+
             let mutable i = index
-            for item in parent do 
+
+            for item in parent do
                 arr.[i] <- item.Key
                 i <- i + 1
 
         member _.IsReadOnly = true
 
         member _.Count = parent.Count
-        
+
     interface IEnumerable<'Key> with
         member _.GetEnumerator() =
-            (seq { for item in parent do item.Key}).GetEnumerator()
-            
+            (seq {
+                for item in parent do
+                    item.Key
+            })
+                .GetEnumerator()
+
     interface IEnumerable with
-        member _.GetEnumerator() = 
-            (seq { for item in parent do item.Key}).GetEnumerator() :> IEnumerator
-    
-and ValueCollection<'Key, 'Value when 'Key : comparison>(parent: Map<'Key, 'Value>) =
+        member _.GetEnumerator() =
+            (seq {
+                for item in parent do
+                    item.Key
+            })
+                .GetEnumerator()
+            :> IEnumerator
+
+and ValueCollection<'Key, 'Value when 'Key: comparison>(parent: Map<'Key, 'Value>) =
     interface ICollection<'Value> with
-        member _.Add(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Add(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Clear() = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Clear() =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Remove(_) = raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
+        member _.Remove(_) =
+            raise (NotSupportedException(SR.GetString(SR.mapCannotBeMutated)))
 
-        member _.Contains x = parent.Exists(fun _ value -> Unchecked.equals value x)
+        member _.Contains x =
+            parent.Exists(fun _ value -> Unchecked.equals value x)
 
-        member _.CopyTo(arr, index) = 
+        member _.CopyTo(arr, index) =
             if isNull arr then nullArg "arr"
-            if index < 0 then invalidArg "index" "index must be positive"
-            if index + parent.Count > arr.Length then invalidArg "index" "array is smaller than index plus the number of items to copy"
-             
+
+            if index < 0 then
+                invalidArg "index" "index must be positive"
+
+            if index + parent.Count > arr.Length then
+                invalidArg "index" "array is smaller than index plus the number of items to copy"
+
             let mutable i = index
-            for item in parent do 
+
+            for item in parent do
                 arr.[i] <- item.Value
                 i <- i + 1
 
         member _.IsReadOnly = true
 
         member _.Count = parent.Count
-        
+
     interface IEnumerable<'Value> with
         member _.GetEnumerator() =
-            (seq { for item in parent do item.Value}).GetEnumerator()
-            
+            (seq {
+                for item in parent do
+                    item.Value
+            })
+                .GetEnumerator()
+
     interface IEnumerable with
-        member _.GetEnumerator() = 
-            (seq { for item in parent do item.Value }).GetEnumerator() :> IEnumerator
+        member _.GetEnumerator() =
+            (seq {
+                for item in parent do
+                    item.Value
+            })
+                .GetEnumerator()
+            :> IEnumerator
 
 []
 []
-module Map = 
+module Map =
 
     []
     let isEmpty (table: Map<_, _>) =
@@ -918,11 +1106,11 @@ module Map =
 
     []
     let add key value (table: Map<_, _>) =
-        table.Add (key, value)
+        table.Add(key, value)
 
     []
     let change key f (table: Map<_, _>) =
-        table.Change (key, f)
+        table.Change(key, f)
 
     []
     let find key (table: Map<_, _>) =
@@ -975,11 +1163,11 @@ module Map =
         table.Map mapping
 
     []
-    let fold<'Key, 'T, 'State when 'Key : comparison> folder (state:'State) (table: Map<'Key, 'T>) =
+    let fold<'Key, 'T, 'State when 'Key: comparison> folder (state: 'State) (table: Map<'Key, 'T>) =
         MapTree.fold folder state table.Tree
 
     []
-    let foldBack<'Key, 'T, 'State  when 'Key : comparison> folder (table: Map<'Key, 'T>) (state:'State) =
+    let foldBack<'Key, 'T, 'State when 'Key: comparison> folder (table: Map<'Key, 'T>) (state: 'State) =
         MapTree.foldBack folder table.Tree state
 
     []
@@ -987,12 +1175,26 @@ module Map =
         table |> Seq.map (fun kvp -> kvp.Key, kvp.Value)
 
     []
-    let findKey predicate (table : Map<_, _>) =
-        table |> Seq.pick (fun kvp -> let k = kvp.Key in if predicate k kvp.Value then Some k else None)
+    let findKey predicate (table: Map<_, _>) =
+        table
+        |> Seq.pick (fun kvp ->
+            let k = kvp.Key in
+
+            if predicate k kvp.Value then
+                Some k
+            else
+                None)
 
     []
-    let tryFindKey predicate (table : Map<_, _>) =
-        table |> Seq.tryPick (fun kvp -> let k = kvp.Key in if predicate k kvp.Value then Some k else None)
+    let tryFindKey predicate (table: Map<_, _>) =
+        table
+        |> Seq.tryPick (fun kvp ->
+            let k = kvp.Key in
+
+            if predicate k kvp.Value then
+                Some k
+            else
+                None)
 
     []
     let ofList (elements: ('Key * 'Value) list) =
@@ -1003,9 +1205,9 @@ module Map =
         Map<_, _>.Create elements
 
     []
-    let ofArray (elements: ('Key * 'Value) array) = 
-       let comparer = LanguagePrimitives.FastGenericComparer<'Key> 
-       new Map<_, _>(comparer, MapTree.ofArray comparer elements)
+    let ofArray (elements: ('Key * 'Value) array) =
+        let comparer = LanguagePrimitives.FastGenericComparer<'Key>
+        new Map<_, _>(comparer, MapTree.ofArray comparer elements)
 
     []
     let toList (table: Map<_, _>) =
@@ -1016,21 +1218,24 @@ module Map =
         table.ToArray()
 
     []
-    let empty<'Key, 'Value  when 'Key : comparison> =
-        Map<'Key, 'Value>.Empty
+    let empty<'Key, 'Value when 'Key: comparison> = Map<'Key, 'Value>.Empty
 
     []
     let count (table: Map<_, _>) =
         table.Count
 
     []
-    let keys (table: Map<_, _>) = table.Keys
+    let keys (table: Map<_, _>) =
+        table.Keys
 
     []
-    let values (table: Map<_, _>) = table.Values
-    
+    let values (table: Map<_, _>) =
+        table.Values
+
     []
-    let minKeyValue (table: Map<_,_>) = table.MinKeyValue
+    let minKeyValue (table: Map<_, _>) =
+        table.MinKeyValue
 
     []
-    let maxKeyValue (table: Map<_,_>) = table.MaxKeyValue
+    let maxKeyValue (table: Map<_, _>) =
+        table.MaxKeyValue
diff --git a/src/FSharp.Core/math/z.fs b/src/FSharp.Core/math/z.fs
index b79ad3671fd..28d0deead19 100644
--- a/src/FSharp.Core/math/z.fs
+++ b/src/FSharp.Core/math/z.fs
@@ -26,60 +26,64 @@ open System.Numerics
 []
 module NumericLiterals =
 
-    module NumericLiteralI = 
-
-        let tab64 = new System.Collections.Generic.Dictionary()
-        let tabParse = new System.Collections.Generic.Dictionary()
-        
-        let FromInt64Dynamic (value:int64) : obj = 
-            lock tab64 (fun () -> 
-                let mutable res = Unchecked.defaultof<_> 
-                let ok = tab64.TryGetValue(value,&res)
-                if ok then res else 
-                res <- BigInteger(value)
-                tab64.[value] <- res
-                res)                 
-
-        let inline get32 (x32:int32) =  FromInt64Dynamic (int64 x32)
-
-        let inline isOX s = not (System.String.IsNullOrEmpty(s)) && s.Length > 2 && s.[0] = '0' && s.[1] = 'x'
-        
-        let FromZero () : 'T = 
-            (get32 0 :?> 'T)
-            when 'T : BigInteger = BigInteger.Zero 
-
-        let FromOne () : 'T = 
-            (get32 1 :?> 'T)
-            when 'T : BigInteger = BigInteger.One
-
-        let FromInt32 (value:int32): 'T = 
-            (get32 value :?> 'T)
-            when 'T : BigInteger = new BigInteger(value)
-        
-        let FromInt64 (value:int64): 'T = 
-            (FromInt64Dynamic value :?> 'T)
-            when 'T : BigInteger = new BigInteger(value)
-            
-        let getParse s = 
-            lock tabParse (fun () -> 
-            let mutable res = Unchecked.defaultof<_> 
-            let ok = tabParse.TryGetValue(s,&res)
-            if ok then 
-                res 
-            else 
-                let v = 
-                   if  isOX s then 
-                      BigInteger.Parse (s.[2..],NumberStyles.AllowHexSpecifier,CultureInfo.InvariantCulture)
-                   else
-                      BigInteger.Parse (s,NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture)
-                res <-  v
-                tabParse.[s] <- res
-                res)
-
-        let FromStringDynamic (text:string) : obj = 
+    module NumericLiteralI =
+
+        let tab64 = new System.Collections.Generic.Dictionary()
+        let tabParse = new System.Collections.Generic.Dictionary()
+
+        let FromInt64Dynamic (value: int64) : obj =
+            lock tab64 (fun () ->
+                let mutable res = Unchecked.defaultof<_>
+                let ok = tab64.TryGetValue(value, &res)
+
+                if ok then
+                    res
+                else
+                    res <- BigInteger(value)
+                    tab64.[value] <- res
+                    res)
+
+        let inline get32 (x32: int32) =
+            FromInt64Dynamic(int64 x32)
+
+        let inline isOX s =
+            not (System.String.IsNullOrEmpty(s))
+            && s.Length > 2
+            && s.[0] = '0'
+            && s.[1] = 'x'
+
+        let FromZero () : 'T =
+            (get32 0 :?> 'T) when 'T: BigInteger = BigInteger.Zero
+
+        let FromOne () : 'T =
+            (get32 1 :?> 'T) when 'T: BigInteger = BigInteger.One
+
+        let FromInt32 (value: int32) : 'T =
+            (get32 value :?> 'T) when 'T: BigInteger = new BigInteger(value)
+
+        let FromInt64 (value: int64) : 'T =
+            (FromInt64Dynamic value :?> 'T) when 'T: BigInteger = new BigInteger(value)
+
+        let getParse s =
+            lock tabParse (fun () ->
+                let mutable res = Unchecked.defaultof<_>
+                let ok = tabParse.TryGetValue(s, &res)
+
+                if ok then
+                    res
+                else
+                    let v =
+                        if isOX s then
+                            BigInteger.Parse(s.[2..], NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture)
+                        else
+                            BigInteger.Parse(s, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture)
+
+                    res <- v
+                    tabParse.[s] <- res
+                    res)
+
+        let FromStringDynamic (text: string) : obj =
             getParse text
-            
-        let FromString (text:string) : 'T = 
-            (FromStringDynamic text :?> 'T)
-            when 'T : BigInteger = getParse text
 
+        let FromString (text: string) : 'T =
+            (FromStringDynamic text :?> 'T) when 'T: BigInteger = getParse text
diff --git a/src/FSharp.Core/observable.fs b/src/FSharp.Core/observable.fs
index 4e5af5232c7..d1bcd160313 100644
--- a/src/FSharp.Core/observable.fs
+++ b/src/FSharp.Core/observable.fs
@@ -12,7 +12,11 @@ open Microsoft.FSharp.Control
 module Observable =
 
     let inline protect f succeed fail =
-        match (try Choice1Of2 (f ()) with e -> Choice2Of2 e) with
+        match (try
+                   Choice1Of2(f ())
+               with e ->
+                   Choice2Of2 e)
+            with
         | Choice1Of2 x -> (succeed x)
         | Choice2Of2 e -> (fail e)
 
@@ -21,55 +25,67 @@ module Observable =
 
         let mutable stopped = false
 
-        abstract Next : value : 'T -> unit
+        abstract Next: value: 'T -> unit
 
-        abstract Error : error : exn -> unit
+        abstract Error: error: exn -> unit
 
-        abstract Completed : unit -> unit
+        abstract Completed: unit -> unit
 
         interface IObserver<'T> with
 
-            member x.OnNext value = 
-                if not stopped then 
-                    x.Next value
+            member x.OnNext value =
+                if not stopped then x.Next value
 
-            member x.OnError e = 
-                if not stopped then 
+            member x.OnError e =
+                if not stopped then
                     stopped <- true
                     x.Error e
 
-            member x.OnCompleted () = 
-                if not stopped then 
-                  stopped <- true
-                  x.Completed ()
+            member x.OnCompleted() =
+                if not stopped then
+                    stopped <- true
+                    x.Completed()
 
     []
     let map mapping (source: IObservable<'T>) =
-        { new IObservable<'U> with 
-             member x.Subscribe(observer) =
-                 source.Subscribe 
-                     { new BasicObserver<'T>() with  
-                         
-                         member x.Next(v) = 
-                             protect (fun () -> mapping v) observer.OnNext observer.OnError
-                         
-                         member x.Error(e) = observer.OnError(e)
-                         
-                         member x.Completed() = observer.OnCompleted() } }
+        { new IObservable<'U> with
+            member x.Subscribe(observer) =
+                source.Subscribe
+                    { new BasicObserver<'T>() with
 
-    []
-    let choose chooser (source: IObservable<'T>) =
-        { new IObservable<'U> with 
-             member x.Subscribe(observer) =
-                 source.Subscribe 
-                     { new BasicObserver<'T>() with  
+                        member x.Next(v) =
+                            protect (fun () -> mapping v) observer.OnNext observer.OnError
 
-                         member x.Next(v) = 
-                             protect (fun () -> chooser v) (function None -> () | Some v2 -> observer.OnNext v2) observer.OnError
+                        member x.Error(e) =
+                            observer.OnError(e)
 
-                         member x.Error(e) = observer.OnError(e)
+                        member x.Completed() =
+                            observer.OnCompleted()
+                    }
+        }
 
-                         member x.Completed() = observer.OnCompleted() } }
+    []
+    let choose chooser (source: IObservable<'T>) =
+        { new IObservable<'U> with
+            member x.Subscribe(observer) =
+                source.Subscribe
+                    { new BasicObserver<'T>() with
+
+                        member x.Next(v) =
+                            protect
+                                (fun () -> chooser v)
+                                (function
+                                | None -> ()
+                                | Some v2 -> observer.OnNext v2)
+                                observer.OnError
+
+                        member x.Error(e) =
+                            observer.OnError(e)
+
+                        member x.Completed() =
+                            observer.OnCompleted()
+                    }
+        }
 
     []
     let filter predicate (source: IObservable<'T>) =
@@ -81,97 +97,129 @@ module Observable =
 
     []
     let scan collector state (source: IObservable<'T>) =
-        { new IObservable<'U> with 
-             member x.Subscribe(observer) =
-                 let mutable state = state
-                 source.Subscribe 
-                     { new BasicObserver<'T>() with  
-
-                         member x.Next(v) = 
-                             let z = state
-                             protect (fun () -> collector z v) (fun z -> 
-                                 state <- z
-                                 observer.OnNext z) observer.OnError
-                                        
-                         member x.Error(e) = observer.OnError(e)
-
-                         member x.Completed() = observer.OnCompleted() } }
+        { new IObservable<'U> with
+            member x.Subscribe(observer) =
+                let mutable state = state
+
+                source.Subscribe
+                    { new BasicObserver<'T>() with
+
+                        member x.Next(v) =
+                            let z = state
+
+                            protect
+                                (fun () -> collector z v)
+                                (fun z ->
+                                    state <- z
+                                    observer.OnNext z)
+                                observer.OnError
+
+                        member x.Error(e) =
+                            observer.OnError(e)
+
+                        member x.Completed() =
+                            observer.OnCompleted()
+                    }
+        }
 
     []
-    let add callback (source: IObservable<'T>) = source.Add(callback)
+    let add callback (source: IObservable<'T>) =
+        source.Add(callback)
 
     []
-    let subscribe (callback: 'T -> unit) (source: IObservable<'T>) = source.Subscribe(callback)
+    let subscribe (callback: 'T -> unit) (source: IObservable<'T>) =
+        source.Subscribe(callback)
 
     []
-    let pairwise (source : IObservable<'T>) : IObservable<'T * 'T> = 
-        { new IObservable<_> with 
-             member x.Subscribe(observer) =
-                 let mutable lastArgs = None
-                 source.Subscribe 
-                     { new BasicObserver<'T>() with  
+    let pairwise (source: IObservable<'T>) : IObservable<'T * 'T> =
+        { new IObservable<_> with
+            member x.Subscribe(observer) =
+                let mutable lastArgs = None
 
-                         member x.Next(args2) = 
-                             match lastArgs with 
-                             | None -> ()
-                             | Some args1 -> observer.OnNext (args1,args2)
-                             lastArgs <- Some args2
+                source.Subscribe
+                    { new BasicObserver<'T>() with
 
-                         member x.Error(e) = observer.OnError(e)
+                        member x.Next(args2) =
+                            match lastArgs with
+                            | None -> ()
+                            | Some args1 -> observer.OnNext(args1, args2)
 
-                         member x.Completed() = observer.OnCompleted() } }
+                            lastArgs <- Some args2
+
+                        member x.Error(e) =
+                            observer.OnError(e)
+
+                        member x.Completed() =
+                            observer.OnCompleted()
+                    }
+        }
 
     []
     let merge (source1: IObservable<'T>) (source2: IObservable<'T>) =
-        { new IObservable<_> with 
-             member x.Subscribe(observer) =
-                 let mutable stopped = false
-                 let mutable completed1 = false
-                 let mutable completed2 = false
-                 let h1 = 
-                     source1.Subscribe 
-                         { new IObserver<'T> with  
-                             member x.OnNext(v) = 
-                                 if not stopped then 
-                                     observer.OnNext v
-
-                             member x.OnError(e) = 
-                                 if not stopped then 
-                                     stopped <- true
-                                     observer.OnError(e)
-
-                             member x.OnCompleted() = 
-                                 if not stopped then 
-                                     completed1 <- true
-                                     if completed1 && completed2 then 
-                                         stopped <- true
-                                         observer.OnCompleted() } 
-                 let h2 = 
-                     source2.Subscribe 
-                         { new IObserver<'T> with  
-                             member x.OnNext(v) = 
-                                 if not stopped then 
-                                     observer.OnNext v
-
-                             member x.OnError(e) = 
-                                 if not stopped then 
-                                     stopped <- true
-                                     observer.OnError(e)
-
-                             member x.OnCompleted() = 
-                                 if not stopped then 
-                                     completed2 <- true
-                                     if completed1 && completed2 then 
-                                         stopped <- true
-                                         observer.OnCompleted() } 
-
-                 { new IDisposable with 
-                       member x.Dispose() = 
-                           h1.Dispose()
-                           h2.Dispose() } }
+        { new IObservable<_> with
+            member x.Subscribe(observer) =
+                let mutable stopped = false
+                let mutable completed1 = false
+                let mutable completed2 = false
+
+                let h1 =
+                    source1.Subscribe
+                        { new IObserver<'T> with
+                            member x.OnNext(v) =
+                                if not stopped then observer.OnNext v
+
+                            member x.OnError(e) =
+                                if not stopped then
+                                    stopped <- true
+                                    observer.OnError(e)
+
+                            member x.OnCompleted() =
+                                if not stopped then
+                                    completed1 <- true
+
+                                    if completed1 && completed2 then
+                                        stopped <- true
+                                        observer.OnCompleted()
+                        }
+
+                let h2 =
+                    source2.Subscribe
+                        { new IObserver<'T> with
+                            member x.OnNext(v) =
+                                if not stopped then observer.OnNext v
+
+                            member x.OnError(e) =
+                                if not stopped then
+                                    stopped <- true
+                                    observer.OnError(e)
+
+                            member x.OnCompleted() =
+                                if not stopped then
+                                    completed2 <- true
+
+                                    if completed1 && completed2 then
+                                        stopped <- true
+                                        observer.OnCompleted()
+                        }
+
+                { new IDisposable with
+                    member x.Dispose() =
+                        h1.Dispose()
+                        h2.Dispose()
+                }
+        }
 
     []
-    let split (splitter : 'T -> Choice<'U1,'U2>) (source: IObservable<'T>) =
-        choose (fun v -> match splitter v with Choice1Of2 x -> Some x | _ -> None) source,
-        choose (fun v -> match splitter v with Choice2Of2 x -> Some x | _ -> None) source
-
+    let split (splitter: 'T -> Choice<'U1, 'U2>) (source: IObservable<'T>) =
+        choose
+            (fun v ->
+                match splitter v with
+                | Choice1Of2 x -> Some x
+                | _ -> None)
+            source,
+        choose
+            (fun v ->
+                match splitter v with
+                | Choice2Of2 x -> Some x
+                | _ -> None)
+            source
diff --git a/src/FSharp.Core/option.fs b/src/FSharp.Core/option.fs
index 8b28af7531d..720ac9d1c45 100644
--- a/src/FSharp.Core/option.fs
+++ b/src/FSharp.Core/option.fs
@@ -5,7 +5,7 @@ namespace Microsoft.FSharp.Core
 open Microsoft.FSharp.Core.Operators
 
 []
-module Option = 
+module Option =
 
     []
     let get option =
@@ -56,13 +56,13 @@ module Option =
         | Some _ -> 1
 
     []
-    let fold<'T,'State> folder (state:'State) (option: 'T option) =
+    let fold<'T, 'State> folder (state: 'State) (option: 'T option) =
         match option with
         | None -> state
         | Some x -> folder state x
 
     []
-    let foldBack<'T,'State> folder (option: option<'T>) (state:'State) =
+    let foldBack<'T, 'State> folder (option: option<'T>) (state: 'State) =
         match option with
         | None -> state
         | Some x -> folder x state
@@ -95,18 +95,18 @@ module Option =
     let map mapping option =
         match option with
         | None -> None
-        | Some x -> Some (mapping x)
+        | Some x -> Some(mapping x)
 
     []
-    let map2 mapping option1 option2 = 
+    let map2 mapping option1 option2 =
         match option1, option2 with
-        | Some x, Some y -> Some (mapping x y)
+        | Some x, Some y -> Some(mapping x y)
         | _ -> None
 
     []
-    let map3 mapping option1 option2 option3 = 
+    let map3 mapping option1 option2 option3 =
         match option1, option2, option3 with
-        | Some x, Some y, Some z -> Some (mapping x y z)
+        | Some x, Some y, Some z -> Some(mapping x y z)
         | _ -> None
 
     []
@@ -130,13 +130,13 @@ module Option =
     []
     let toArray option =
         match option with
-        | None -> [| |]
+        | None -> [||]
         | Some x -> [| x |]
 
     []
     let toList option =
         match option with
-        | None -> [ ]
+        | None -> []
         | Some x -> [ x ]
 
     []
@@ -146,7 +146,7 @@ module Option =
         | Some v -> System.Nullable(v)
 
     []
-    let ofNullable (value:System.Nullable<'T>) =
+    let ofNullable (value: System.Nullable<'T>) =
         if value.HasValue then
             Some value.Value
         else
@@ -215,13 +215,13 @@ module ValueOption =
         | ValueSome _ -> 1
 
     []
-    let fold<'T,'State> folder (state:'State) (voption: voption<'T>) =
+    let fold<'T, 'State> folder (state: 'State) (voption: voption<'T>) =
         match voption with
         | ValueNone -> state
         | ValueSome x -> folder state x
 
     []
-    let foldBack<'T,'State> folder (voption: voption<'T>) (state:'State) =
+    let foldBack<'T, 'State> folder (voption: voption<'T>) (state: 'State) =
         match voption with
         | ValueNone -> state
         | ValueSome x -> folder x state
@@ -254,18 +254,18 @@ module ValueOption =
     let map mapping voption =
         match voption with
         | ValueNone -> ValueNone
-        | ValueSome x -> ValueSome (mapping x)
+        | ValueSome x -> ValueSome(mapping x)
 
     []
-    let map2 mapping voption1 voption2 = 
+    let map2 mapping voption1 voption2 =
         match voption1, voption2 with
-        | ValueSome x, ValueSome y -> ValueSome (mapping x y)
+        | ValueSome x, ValueSome y -> ValueSome(mapping x y)
         | _ -> ValueNone
 
     []
-    let map3 mapping voption1 voption2 voption3 = 
+    let map3 mapping voption1 voption2 voption3 =
         match voption1, voption2, voption3 with
-        | ValueSome x, ValueSome y, ValueSome z -> ValueSome (mapping x y z)
+        | ValueSome x, ValueSome y, ValueSome z -> ValueSome(mapping x y z)
         | _ -> ValueNone
 
     []
@@ -284,18 +284,22 @@ module ValueOption =
     let filter predicate voption =
         match voption with
         | ValueNone -> ValueNone
-        | ValueSome x -> if predicate x then ValueSome x else ValueNone
+        | ValueSome x ->
+            if predicate x then
+                ValueSome x
+            else
+                ValueNone
 
     []
     let toArray voption =
         match voption with
-        | ValueNone -> [| |]
+        | ValueNone -> [||]
         | ValueSome x -> [| x |]
 
     []
     let toList voption =
         match voption with
-        | ValueNone -> [ ]
+        | ValueNone -> []
         | ValueSome x -> [ x ]
 
     []
@@ -305,7 +309,7 @@ module ValueOption =
         | ValueSome v -> System.Nullable(v)
 
     []
-    let ofNullable (value:System.Nullable<'T>) =
+    let ofNullable (value: System.Nullable<'T>) =
         if value.HasValue then
             ValueSome value.Value
         else
diff --git a/src/FSharp.Core/quotations.fs b/src/FSharp.Core/quotations.fs
index 3ef11b97bd3..389ad12b79a 100644
--- a/src/FSharp.Core/quotations.fs
+++ b/src/FSharp.Core/quotations.fs
@@ -28,29 +28,50 @@ module Helpers =
     let qOneOrMoreRLinear q inp =
         let rec queryAcc rvs e =
             match q e with
-            | Some(v, body) -> queryAcc (v :: rvs) body
+            | Some (v, body) -> queryAcc (v :: rvs) body
             | None ->
                 match rvs with
                 | [] -> None
                 | _ -> Some(List.rev rvs, e)
+
         queryAcc [] inp
 
     let qOneOrMoreLLinear q inp =
         let rec queryAcc e rvs =
             match q e with
-            | Some(body, v) -> queryAcc body (v :: rvs)
+            | Some (body, v) -> queryAcc body (v :: rvs)
             | None ->
                 match rvs with
                 | [] -> None
                 | _ -> Some(e, rvs)
+
         queryAcc inp []
 
-    let mkRLinear mk (vs, body) = List.foldBack (fun v acc -> mk(v, acc)) vs body
-    let mkLLinear mk (body, vs) = List.fold (fun acc v -> mk(acc, v)) body vs
+    let mkRLinear mk (vs, body) =
+        List.foldBack (fun v acc -> mk (v, acc)) vs body
+
+    let mkLLinear mk (body, vs) =
+        List.fold (fun acc v -> mk (acc, v)) body vs
+
+    let staticBindingFlags =
+        BindingFlags.Static
+        ||| BindingFlags.Public
+        ||| BindingFlags.NonPublic
+        ||| BindingFlags.DeclaredOnly
+
+    let staticOrInstanceBindingFlags =
+        BindingFlags.Instance
+        ||| BindingFlags.Static
+        ||| BindingFlags.Public
+        ||| BindingFlags.NonPublic
+        ||| BindingFlags.DeclaredOnly
+
+    let instanceBindingFlags =
+        BindingFlags.Instance
+        ||| BindingFlags.Public
+        ||| BindingFlags.NonPublic
+        ||| BindingFlags.DeclaredOnly
 
-    let staticBindingFlags = BindingFlags.Static ||| BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.DeclaredOnly
-    let staticOrInstanceBindingFlags = BindingFlags.Instance ||| BindingFlags.Static ||| BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.DeclaredOnly
-    let instanceBindingFlags = BindingFlags.Instance ||| BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.DeclaredOnly
     let publicOrPrivateBindingFlags = BindingFlags.Public ||| BindingFlags.NonPublic
 
     let isDelegateType (typ: Type) =
@@ -62,20 +83,21 @@ module Helpers =
             false
 
     let getDelegateInvoke ty =
-        if not (isDelegateType ty) then invalidArg  "ty" (SR.GetString(SR.delegateExpected))
-        ty.GetMethod("Invoke", instanceBindingFlags)
+        if not (isDelegateType ty) then
+            invalidArg "ty" (SR.GetString(SR.delegateExpected))
 
+        ty.GetMethod("Invoke", instanceBindingFlags)
 
     let inline checkNonNull argName (v: 'T) =
         match box v with
         | null -> nullArg argName
         | _ -> ()
 
-    let getTypesFromParamInfos (infos : ParameterInfo[]) = infos |> Array.map (fun pi -> pi.ParameterType)
+    let getTypesFromParamInfos (infos: ParameterInfo[]) =
+        infos |> Array.map (fun pi -> pi.ParameterType)
 
 open Helpers
 
-
 []
 []
 type Var(name: string, typ: Type, ?isMutable: bool) =
@@ -85,7 +107,7 @@ type Var(name: string, typ: Type, ?isMutable: bool) =
         let mutable lastStamp = -1L // first value retrieved will be 0
         fun () -> System.Threading.Interlocked.Increment &lastStamp
 
-    static let globals = new Dictionary<(string*Type), Var>(11)
+    static let globals = new Dictionary<(string * Type), Var>(11)
 
     let stamp = getStamp ()
     let isMutable = defaultArg isMutable false
@@ -101,84 +123,102 @@ type Var(name: string, typ: Type, ?isMutable: bool) =
     static member Global(name, typ: Type) =
         checkNonNull "name" name
         checkNonNull "typ" typ
+
         lock globals (fun () ->
             let mutable res = Unchecked.defaultof
             let ok = globals.TryGetValue((name, typ), &res)
-            if ok then res else
-            let res = new Var(name, typ)
-            globals.[(name, typ)] <- res
-            res)
 
-    override _.ToString() = name
+            if ok then
+                res
+            else
+                let res = new Var(name, typ)
+                globals.[(name, typ)] <- res
+                res)
+
+    override _.ToString() =
+        name
 
-    override _.GetHashCode() = base.GetHashCode()
+    override _.GetHashCode() =
+        base.GetHashCode()
 
-    override v.Equals(obj:obj) =
+    override v.Equals(obj: obj) =
         match obj with
         | :? Var as v2 -> System.Object.ReferenceEquals(v, v2)
         | _ -> false
 
     interface System.IComparable with
-        member v.CompareTo(obj:obj) =
+        member v.CompareTo(obj: obj) =
             match obj with
             | :? Var as v2 ->
-                if System.Object.ReferenceEquals(v, v2) then 0 else
-                let c = compare v.Name v2.Name
-                if c <> 0 then c else
-                let c = compare v.Type.MetadataToken v2.Type.MetadataToken
-                if c <> 0 then c else
-                let c = compare v.Type.Module.MetadataToken v2.Type.Module.MetadataToken
-                if c <> 0 then c else
-                let c = compare v.Type.Assembly.FullName v2.Type.Assembly.FullName
-                if c <> 0 then c else
-                compare v.Stamp v2.Stamp
+                if System.Object.ReferenceEquals(v, v2) then
+                    0
+                else
+                    let c = compare v.Name v2.Name
+
+                    if c <> 0 then
+                        c
+                    else
+                        let c = compare v.Type.MetadataToken v2.Type.MetadataToken
+
+                        if c <> 0 then
+                            c
+                        else
+                            let c = compare v.Type.Module.MetadataToken v2.Type.Module.MetadataToken
+
+                            if c <> 0 then
+                                c
+                            else
+                                let c = compare v.Type.Assembly.FullName v2.Type.Assembly.FullName
+
+                                if c <> 0 then
+                                    c
+                                else
+                                    compare v.Stamp v2.Stamp
             | _ -> 0
 
 /// Represents specifications of a subset of F# expressions
 []
 type Tree =
-    | CombTerm   of ExprConstInfo * Expr list
-    | VarTerm    of Var
+    | CombTerm of ExprConstInfo * Expr list
+    | VarTerm of Var
     | LambdaTerm of Var * Expr
-    | HoleTerm   of Type * int
+    | HoleTerm of Type * int
 
-and
-  []
-  ExprConstInfo =
+and [] ExprConstInfo =
     | AppOp
     | IfThenElseOp
     | LetRecOp
     | LetRecCombOp
     | LetOp
-    | NewRecordOp      of Type
-    | NewUnionCaseOp       of UnionCaseInfo
-    | UnionCaseTestOp  of UnionCaseInfo
-    | NewTupleOp     of Type
-    | TupleGetOp    of Type * int
-    | InstancePropGetOp    of PropertyInfo
-    | StaticPropGetOp    of PropertyInfo
-    | InstancePropSetOp    of PropertyInfo
-    | StaticPropSetOp    of PropertyInfo
-    | InstanceFieldGetOp   of FieldInfo
-    | StaticFieldGetOp   of FieldInfo
-    | InstanceFieldSetOp   of FieldInfo
-    | StaticFieldSetOp   of FieldInfo
-    | NewObjectOp   of ConstructorInfo
+    | NewRecordOp of Type
+    | NewUnionCaseOp of UnionCaseInfo
+    | UnionCaseTestOp of UnionCaseInfo
+    | NewTupleOp of Type
+    | TupleGetOp of Type * int
+    | InstancePropGetOp of PropertyInfo
+    | StaticPropGetOp of PropertyInfo
+    | InstancePropSetOp of PropertyInfo
+    | StaticPropSetOp of PropertyInfo
+    | InstanceFieldGetOp of FieldInfo
+    | StaticFieldGetOp of FieldInfo
+    | InstanceFieldSetOp of FieldInfo
+    | StaticFieldSetOp of FieldInfo
+    | NewObjectOp of ConstructorInfo
     | InstanceMethodCallOp of MethodInfo
     | StaticMethodCallOp of MethodInfo
     /// A new Call node type in F# 5.0, storing extra information about witnesses
     | InstanceMethodCallWOp of MethodInfo * MethodInfo * int
     /// A new Call node type in F# 5.0, storing extra information about witnesses
     | StaticMethodCallWOp of MethodInfo * MethodInfo * int
-    | CoerceOp     of Type
-    | NewArrayOp    of Type
-    | NewDelegateOp   of Type
+    | CoerceOp of Type
+    | NewArrayOp of Type
+    | NewDelegateOp of Type
     | QuoteOp of bool
     | SequentialOp
     | AddressOfOp
     | VarSetOp
     | AddressSetOp
-    | TypeTestOp  of Type
+    | TypeTestOp of Type
     | TryWithOp
     | TryFinallyOp
     | ForIntegerRangeLoopOp
@@ -188,8 +228,7 @@ and
     | WithValueOp of obj * Type
     | DefaultValueOp of Type
 
-and []
-    Expr(term:Tree, attribs: Expr list) =
+and [] Expr(term: Tree, attribs: Expr list) =
     member x.Tree = term
     member x.CustomAttributes = attribs
 
@@ -199,43 +238,47 @@ and []
             let rec eq t1 t2 =
                 match t1, t2 with
                 // We special-case ValueOp to ensure that ValueWithName = Value
-                | CombTerm(ValueOp(v1, ty1, _), []), CombTerm(ValueOp(v2, ty2, _), []) -> (v1 = v2) && (ty1 = ty2)
+                | CombTerm (ValueOp (v1, ty1, _), []), CombTerm (ValueOp (v2, ty2, _), []) -> (v1 = v2) && (ty1 = ty2)
 
                 // We strip off InstanceMethodCallWOp to ensure that CallWithWitness = Call
-                | CombTerm(InstanceMethodCallWOp(minfo1, _minfoW1, nWitnesses1), obj1::args1WithoutObj), _ ->
+                | CombTerm (InstanceMethodCallWOp (minfo1, _minfoW1, nWitnesses1), obj1 :: args1WithoutObj), _ ->
                     if nWitnesses1 <= args1WithoutObj.Length then
                         let args1WithoutWitnesses = List.skip nWitnesses1 args1WithoutObj
-                        eq (CombTerm(InstanceMethodCallOp(minfo1), obj1::args1WithoutWitnesses)) t2
-                    else 
+                        eq (CombTerm(InstanceMethodCallOp(minfo1), obj1 :: args1WithoutWitnesses)) t2
+                    else
                         false
 
                 // We strip off InstanceMethodCallWOp to ensure that CallWithWitness = Call
-                | _, CombTerm(InstanceMethodCallWOp(minfo2, _minfoW2, nWitnesses2), obj2::args2WithoutObj) when nWitnesses2 <= args2WithoutObj.Length ->
+                | _, CombTerm (InstanceMethodCallWOp (minfo2, _minfoW2, nWitnesses2), obj2 :: args2WithoutObj) when
+                    nWitnesses2 <= args2WithoutObj.Length
+                    ->
                     let args2WithoutWitnesses = List.skip nWitnesses2 args2WithoutObj
-                    eq t1 (CombTerm(InstanceMethodCallOp(minfo2), obj2::args2WithoutWitnesses))
+                    eq t1 (CombTerm(InstanceMethodCallOp(minfo2), obj2 :: args2WithoutWitnesses))
 
                 // We strip off StaticMethodCallWOp to ensure that CallWithWitness = Call
-                | CombTerm(StaticMethodCallWOp(minfo1, _minfoW1, nWitnesses1), args1), _ when nWitnesses1 <= args1.Length ->
+                | CombTerm (StaticMethodCallWOp (minfo1, _minfoW1, nWitnesses1), args1), _ when nWitnesses1 <= args1.Length ->
                     let argsWithoutWitnesses1 = List.skip nWitnesses1 args1
                     eq (CombTerm(StaticMethodCallOp(minfo1), argsWithoutWitnesses1)) t2
 
                 // We strip off StaticMethodCallWOp to ensure that CallWithWitness = Call
-                | _, CombTerm(StaticMethodCallWOp(minfo2, _minfoW2, nWitnesses2), args2) when nWitnesses2 <= args2.Length ->
+                | _, CombTerm (StaticMethodCallWOp (minfo2, _minfoW2, nWitnesses2), args2) when nWitnesses2 <= args2.Length ->
                     let argsWithoutWitnesses2 = List.skip nWitnesses2 args2
                     eq t1 (CombTerm(StaticMethodCallOp(minfo2), argsWithoutWitnesses2))
 
-                | CombTerm(c1, es1), CombTerm(c2, es2) -> c1 = c2 && es1.Length = es2.Length && (es1 = es2)
+                | CombTerm (c1, es1), CombTerm (c2, es2) -> c1 = c2 && es1.Length = es2.Length && (es1 = es2)
                 | VarTerm v1, VarTerm v2 -> (v1 = v2)
-                | LambdaTerm (v1, e1), LambdaTerm(v2, e2) -> (v1 = v2) && (e1 = e2)
-                | HoleTerm (ty1, n1), HoleTerm(ty2, n2) -> (ty1 = ty2) && (n1 = n2)
+                | LambdaTerm (v1, e1), LambdaTerm (v2, e2) -> (v1 = v2) && (e1 = e2)
+                | HoleTerm (ty1, n1), HoleTerm (ty2, n2) -> (ty1 = ty2) && (n1 = n2)
                 | _ -> false
+
             eq x.Tree y.Tree
         | _ -> false
 
     override x.GetHashCode() =
         x.Tree.GetHashCode()
 
-    override x.ToString() = x.ToString false
+    override x.ToString() =
+        x.ToString false
 
     member x.ToString full =
         Display.layout_to_string FormatOptions.Default (x.GetLayout(full))
@@ -243,101 +286,157 @@ and []
     member x.DebugText = x.ToString(false)
 
     member x.GetLayout long =
-        let expr (e: Expr ) = e.GetLayout long
-        let exprs (es: Expr list) = es |> List.map expr
-        let parens ls = bracketL (commaListL ls)
-        let pairL l1 l2 = bracketL (l1 ^^ sepL comma ^^ l2)
-        let listL ls = squareBracketL (commaListL ls)
-        let combTaggedL nm ls = wordL nm ^^ parens ls
-        let combL nm ls = combTaggedL (tagKeyword nm) ls
+        let expr (e: Expr) =
+            e.GetLayout long
+
+        let exprs (es: Expr list) =
+            es |> List.map expr
+
+        let parens ls =
+            bracketL (commaListL ls)
+
+        let pairL l1 l2 =
+            bracketL (l1 ^^ sepL comma ^^ l2)
+
+        let listL ls =
+            squareBracketL (commaListL ls)
+
+        let combTaggedL nm ls =
+            wordL nm ^^ parens ls
+
+        let combL nm ls =
+            combTaggedL (tagKeyword nm) ls
+
         let noneL = wordL (tagProperty "None")
-        let someL e = combTaggedL (tagMethod "Some") [expr e]
-        let typeL (o: Type) = wordL (tagClass (if long then o.FullName else o.Name))
-        let objL (o: 'T) = wordL (tagText (sprintf "%A" o))
-        let varL (v: Var) = wordL (tagLocal v.Name)
-        let (|E|) (e: Expr) = e.Tree
-        let (|Lambda|_|) (E x) = match x with LambdaTerm(a, b) -> Some (a, b) | _ -> None
-        let (|IteratedLambda|_|) (e: Expr) = qOneOrMoreRLinear (|Lambda|_|) e
-        let ucaseL (unionCase:UnionCaseInfo) = (if long then objL unionCase else wordL (tagUnionCase unionCase.Name))
-        let minfoL (minfo: MethodInfo) = if long then objL minfo else wordL (tagMethod minfo.Name)
-        let cinfoL (cinfo: ConstructorInfo) = if long then objL cinfo else wordL (tagMethod cinfo.DeclaringType.Name)
-        let pinfoL (pinfo: PropertyInfo) = if long then objL pinfo else wordL (tagProperty pinfo.Name)
-        let finfoL (finfo: FieldInfo) = if long then objL finfo else wordL (tagField finfo.Name)
+
+        let someL e =
+            combTaggedL (tagMethod "Some") [ expr e ]
+
+        let typeL (o: Type) =
+            wordL (tagClass (if long then o.FullName else o.Name))
+
+        let objL (o: 'T) =
+            wordL (tagText (sprintf "%A" o))
+
+        let varL (v: Var) =
+            wordL (tagLocal v.Name)
+
+        let (|E|) (e: Expr) =
+            e.Tree
+
+        let (|Lambda|_|) (E x) =
+            match x with
+            | LambdaTerm (a, b) -> Some(a, b)
+            | _ -> None
+
+        let (|IteratedLambda|_|) (e: Expr) =
+            qOneOrMoreRLinear (|Lambda|_|) e
+
+        let ucaseL (unionCase: UnionCaseInfo) =
+            (if long then
+                 objL unionCase
+             else
+                 wordL (tagUnionCase unionCase.Name))
+
+        let minfoL (minfo: MethodInfo) =
+            if long then
+                objL minfo
+            else
+                wordL (tagMethod minfo.Name)
+
+        let cinfoL (cinfo: ConstructorInfo) =
+            if long then
+                objL cinfo
+            else
+                wordL (tagMethod cinfo.DeclaringType.Name)
+
+        let pinfoL (pinfo: PropertyInfo) =
+            if long then
+                objL pinfo
+            else
+                wordL (tagProperty pinfo.Name)
+
+        let finfoL (finfo: FieldInfo) =
+            if long then
+                objL finfo
+            else
+                wordL (tagField finfo.Name)
+
         let rec (|NLambdas|_|) n (e: Expr) =
             match e with
             | _ when n <= 0 -> Some([], e)
-            | Lambda(v, NLambdas ((-) n 1) (vs, b)) -> Some(v :: vs, b)
+            | Lambda (v, NLambdas ((-) n 1) (vs, b)) -> Some(v :: vs, b)
             | _ -> None
 
         match x.Tree with
-        | CombTerm(AppOp, args) -> combL "Application" (exprs args)
-        | CombTerm(IfThenElseOp, args) -> combL "IfThenElse" (exprs args)
-        | CombTerm(LetRecOp, [IteratedLambda(vs, E(CombTerm(LetRecCombOp, b2 :: bs)))]) -> combL "LetRecursive" [listL (List.map2 pairL (List.map varL vs) (exprs bs) ); b2.GetLayout long]
-        | CombTerm(LetOp, [e;E(LambdaTerm(v, b))]) -> combL "Let" [varL v; e.GetLayout long; b.GetLayout long]
-        | CombTerm(NewRecordOp ty, args) -> combL "NewRecord" (typeL ty :: exprs args)
-        | CombTerm(NewUnionCaseOp unionCase, args) -> combL "NewUnionCase" (ucaseL unionCase :: exprs args)
-        | CombTerm(UnionCaseTestOp unionCase, args) -> combL "UnionCaseTest" (exprs args@ [ucaseL unionCase])
-        | CombTerm(NewTupleOp _, args) -> combL "NewTuple" (exprs args)
-        | CombTerm(TupleGetOp (_, i), [arg]) -> combL "TupleGet" ([expr arg] @ [objL i])
-        | CombTerm(ValueOp(v, _, Some nm), []) -> combL "ValueWithName" [objL v; wordL (tagLocal nm)]
-        | CombTerm(ValueOp(v, _, None), []) -> combL "Value" [objL v]
-        | CombTerm(WithValueOp(v, _), [defn]) -> combL "WithValue" [objL v; expr defn]
-
-        | CombTerm(InstanceMethodCallOp(minfo), obj::args) ->
-            combL "Call" [someL obj; minfoL minfo; listL (exprs args)]
-        
-        | CombTerm(StaticMethodCallOp(minfo), args) ->
-            combL "Call" [noneL; minfoL minfo; listL (exprs args)]
-
-        | CombTerm(InstanceMethodCallWOp(minfo, _minfoW, nWitnesses), obj::argsWithoutObj) when nWitnesses <= argsWithoutObj.Length ->
+        | CombTerm (AppOp, args) -> combL "Application" (exprs args)
+        | CombTerm (IfThenElseOp, args) -> combL "IfThenElse" (exprs args)
+        | CombTerm (LetRecOp, [ IteratedLambda (vs, E (CombTerm (LetRecCombOp, b2 :: bs))) ]) ->
+            combL "LetRecursive" [ listL (List.map2 pairL (List.map varL vs) (exprs bs)); b2.GetLayout long ]
+        | CombTerm (LetOp, [ e; E (LambdaTerm (v, b)) ]) -> combL "Let" [ varL v; e.GetLayout long; b.GetLayout long ]
+        | CombTerm (NewRecordOp ty, args) -> combL "NewRecord" (typeL ty :: exprs args)
+        | CombTerm (NewUnionCaseOp unionCase, args) -> combL "NewUnionCase" (ucaseL unionCase :: exprs args)
+        | CombTerm (UnionCaseTestOp unionCase, args) -> combL "UnionCaseTest" (exprs args @ [ ucaseL unionCase ])
+        | CombTerm (NewTupleOp _, args) -> combL "NewTuple" (exprs args)
+        | CombTerm (TupleGetOp (_, i), [ arg ]) -> combL "TupleGet" ([ expr arg ] @ [ objL i ])
+        | CombTerm (ValueOp (v, _, Some nm), []) -> combL "ValueWithName" [ objL v; wordL (tagLocal nm) ]
+        | CombTerm (ValueOp (v, _, None), []) -> combL "Value" [ objL v ]
+        | CombTerm (WithValueOp (v, _), [ defn ]) -> combL "WithValue" [ objL v; expr defn ]
+
+        | CombTerm (InstanceMethodCallOp (minfo), obj :: args) -> combL "Call" [ someL obj; minfoL minfo; listL (exprs args) ]
+
+        | CombTerm (StaticMethodCallOp (minfo), args) -> combL "Call" [ noneL; minfoL minfo; listL (exprs args) ]
+
+        | CombTerm (InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj :: argsWithoutObj) when nWitnesses <= argsWithoutObj.Length ->
             let argsWithoutWitnesses = List.skip nWitnesses argsWithoutObj
-            combL "Call" [someL obj; minfoL minfo; listL (exprs argsWithoutWitnesses)]
+            combL "Call" [ someL obj; minfoL minfo; listL (exprs argsWithoutWitnesses) ]
 
-        | CombTerm(StaticMethodCallWOp(minfo, _minfoW, nWitnesses), args) when nWitnesses <= args.Length  ->
+        | CombTerm (StaticMethodCallWOp (minfo, _minfoW, nWitnesses), args) when nWitnesses <= args.Length ->
             let argsWithoutWitnesses = List.skip nWitnesses args
-            combL "Call" [noneL; minfoL minfo; listL (exprs argsWithoutWitnesses)]
-
-        | CombTerm(InstancePropGetOp(pinfo), (obj::args)) -> combL "PropertyGet"  [someL obj; pinfoL pinfo; listL (exprs args)]
-        | CombTerm(StaticPropGetOp(pinfo), args) -> combL "PropertyGet"  [noneL; pinfoL pinfo; listL (exprs args)]
-        | CombTerm(InstancePropSetOp(pinfo), (obj::args)) -> combL "PropertySet" [someL obj; pinfoL pinfo; listL (exprs args)]
-        | CombTerm(StaticPropSetOp(pinfo), args) -> combL "PropertySet"  [noneL; pinfoL pinfo; listL (exprs args)]
-        | CombTerm(InstanceFieldGetOp(finfo), [obj]) -> combL "FieldGet" [someL obj; finfoL finfo]
-        | CombTerm(StaticFieldGetOp(finfo), []) -> combL "FieldGet" [noneL; finfoL finfo]
-        | CombTerm(InstanceFieldSetOp(finfo), [obj;v]) -> combL "FieldSet" [someL obj; finfoL finfo; expr v;]
-        | CombTerm(StaticFieldSetOp(finfo), [v]) -> combL "FieldSet" [noneL; finfoL finfo; expr v;]
-        | CombTerm(CoerceOp(ty), [arg]) -> combL "Coerce"  [ expr arg; typeL ty]
-        | CombTerm(NewObjectOp cinfo, args) -> combL "NewObject" ([ cinfoL cinfo ] @ exprs args)
-        | CombTerm(DefaultValueOp ty, args) -> combL "DefaultValue" ([ typeL ty ] @ exprs args)
-        | CombTerm(NewArrayOp ty, args) -> combL "NewArray" ([ typeL ty ] @ exprs args)
-        | CombTerm(TypeTestOp ty, args) -> combL "TypeTest" ([ typeL ty] @ exprs args)
-        | CombTerm(AddressOfOp, args) -> combL "AddressOf" (exprs args)
-        | CombTerm(VarSetOp, [E(VarTerm v); e]) -> combL "VarSet" [varL v; expr e]
-        | CombTerm(AddressSetOp, args) -> combL "AddressSet" (exprs args)
-        | CombTerm(ForIntegerRangeLoopOp, [e1;e2;E(LambdaTerm(v, e3))]) -> combL "ForIntegerRangeLoop" [varL v; expr e1; expr e2; expr e3]
-        | CombTerm(WhileLoopOp, args) -> combL "WhileLoop" (exprs args)
-        | CombTerm(TryFinallyOp, args) -> combL "TryFinally" (exprs args)
-        | CombTerm(TryWithOp, [e1;Lambda(v1, e2);Lambda(v2, e3)]) -> combL "TryWith" [expr e1; varL v1; expr e2; varL v2; expr e3]
-        | CombTerm(SequentialOp, args) -> combL "Sequential" (exprs args)
-
-        | CombTerm(NewDelegateOp ty, [e]) ->
+            combL "Call" [ noneL; minfoL minfo; listL (exprs argsWithoutWitnesses) ]
+
+        | CombTerm (InstancePropGetOp (pinfo), (obj :: args)) -> combL "PropertyGet" [ someL obj; pinfoL pinfo; listL (exprs args) ]
+        | CombTerm (StaticPropGetOp (pinfo), args) -> combL "PropertyGet" [ noneL; pinfoL pinfo; listL (exprs args) ]
+        | CombTerm (InstancePropSetOp (pinfo), (obj :: args)) -> combL "PropertySet" [ someL obj; pinfoL pinfo; listL (exprs args) ]
+        | CombTerm (StaticPropSetOp (pinfo), args) -> combL "PropertySet" [ noneL; pinfoL pinfo; listL (exprs args) ]
+        | CombTerm (InstanceFieldGetOp (finfo), [ obj ]) -> combL "FieldGet" [ someL obj; finfoL finfo ]
+        | CombTerm (StaticFieldGetOp (finfo), []) -> combL "FieldGet" [ noneL; finfoL finfo ]
+        | CombTerm (InstanceFieldSetOp (finfo), [ obj; v ]) -> combL "FieldSet" [ someL obj; finfoL finfo; expr v ]
+        | CombTerm (StaticFieldSetOp (finfo), [ v ]) -> combL "FieldSet" [ noneL; finfoL finfo; expr v ]
+        | CombTerm (CoerceOp (ty), [ arg ]) -> combL "Coerce" [ expr arg; typeL ty ]
+        | CombTerm (NewObjectOp cinfo, args) -> combL "NewObject" ([ cinfoL cinfo ] @ exprs args)
+        | CombTerm (DefaultValueOp ty, args) -> combL "DefaultValue" ([ typeL ty ] @ exprs args)
+        | CombTerm (NewArrayOp ty, args) -> combL "NewArray" ([ typeL ty ] @ exprs args)
+        | CombTerm (TypeTestOp ty, args) -> combL "TypeTest" ([ typeL ty ] @ exprs args)
+        | CombTerm (AddressOfOp, args) -> combL "AddressOf" (exprs args)
+        | CombTerm (VarSetOp, [ E (VarTerm v); e ]) -> combL "VarSet" [ varL v; expr e ]
+        | CombTerm (AddressSetOp, args) -> combL "AddressSet" (exprs args)
+        | CombTerm (ForIntegerRangeLoopOp, [ e1; e2; E (LambdaTerm (v, e3)) ]) ->
+            combL "ForIntegerRangeLoop" [ varL v; expr e1; expr e2; expr e3 ]
+        | CombTerm (WhileLoopOp, args) -> combL "WhileLoop" (exprs args)
+        | CombTerm (TryFinallyOp, args) -> combL "TryFinally" (exprs args)
+        | CombTerm (TryWithOp, [ e1; Lambda (v1, e2); Lambda (v2, e3) ]) -> combL "TryWith" [ expr e1; varL v1; expr e2; varL v2; expr e3 ]
+        | CombTerm (SequentialOp, args) -> combL "Sequential" (exprs args)
+
+        | CombTerm (NewDelegateOp ty, [ e ]) ->
             let nargs = (getDelegateInvoke ty).GetParameters().Length
+
             if nargs = 0 then
                 match e with
-                | NLambdas 1 ([_], e) -> combL "NewDelegate" ([typeL ty] @ [expr e])
-                | NLambdas 0 ([], e) -> combL "NewDelegate" ([typeL ty] @ [expr e])
-                | _ -> combL "NewDelegate" [typeL ty; expr e]
+                | NLambdas 1 ([ _ ], e) -> combL "NewDelegate" ([ typeL ty ] @ [ expr e ])
+                | NLambdas 0 ([], e) -> combL "NewDelegate" ([ typeL ty ] @ [ expr e ])
+                | _ -> combL "NewDelegate" [ typeL ty; expr e ]
             else
                 match e with
-                | NLambdas nargs (vs, e) -> combL "NewDelegate" ([typeL ty] @ (vs |> List.map varL) @ [expr e])
-                | _ -> combL "NewDelegate" [typeL ty; expr e]
+                | NLambdas nargs (vs, e) -> combL "NewDelegate" ([ typeL ty ] @ (vs |> List.map varL) @ [ expr e ])
+                | _ -> combL "NewDelegate" [ typeL ty; expr e ]
         | VarTerm v -> wordL (tagLocal v.Name)
-        | LambdaTerm(v, b) -> combL "Lambda" [varL v; expr b]
+        | LambdaTerm (v, b) -> combL "Lambda" [ varL v; expr b ]
         | HoleTerm _ -> wordL (tagLocal "_")
-        | CombTerm(QuoteOp _, args) -> combL "Quote" (exprs args)
+        | CombTerm (QuoteOp _, args) -> combL "Quote" (exprs args)
         | _ -> failwithf "Unexpected term"
 
-and []
-    Expr<'T>(term:Tree, attribs) =
+and [] Expr<'T>(term: Tree, attribs) =
     inherit Expr(term, attribs)
     member x.Raw = (x :> Expr)
 
@@ -348,20 +447,24 @@ module Patterns =
     /// as a computation.
     type Instantiable<'T> = (int -> Type) -> 'T
 
-    type ByteStream(bytes:byte[], initial: int, len: int) =
+    type ByteStream(bytes: byte[], initial: int, len: int) =
 
         let mutable pos = initial
         let lim = initial + len
 
         member b.ReadByte() =
-            if pos >= lim then failwith "end of stream"
+            if pos >= lim then
+                failwith "end of stream"
+
             let res = int32 bytes.[pos]
             pos <- pos + 1
             res
 
         member b.ReadBytes n =
-            if pos + n > lim then failwith "ByteStream.ReadBytes: end of stream"
-            let res = bytes.[pos..pos+n-1]
+            if pos + n > lim then
+                failwith "ByteStream.ReadBytes: end of stream"
+
+            let res = bytes.[pos .. pos + n - 1]
             pos <- pos + n
             res
 
@@ -370,217 +473,317 @@ module Patterns =
             pos <- pos + n
             res
 
+    let E t =
+        new Expr(t, [])
 
-    let E t = new Expr< >(t, [])
-    let EA (t, attribs) = new Expr< >(t, attribs)
-    let ES ts = List.map E ts
+    let EA (t, attribs) =
+        new Expr(t, attribs)
 
-    let (|E|) (e: Expr) = e.Tree
-    let (|ES|) (es: Expr list) = es |> List.map (fun e -> e.Tree)
-    let (|FrontAndBack|_|) es =
-        let rec loop acc xs = match xs with [] -> None | [h] -> Some (List.rev acc, h) | h :: t -> loop (h :: acc) t
-        loop [] es
+    let ES ts =
+        List.map E ts
+
+    let (|E|) (e: Expr) =
+        e.Tree
+
+    let (|ES|) (es: Expr list) =
+        es |> List.map (fun e -> e.Tree)
 
+    let (|FrontAndBack|_|) es =
+        let rec loop acc xs =
+            match xs with
+            | [] -> None
+            | [ h ] -> Some(List.rev acc, h)
+            | h :: t -> loop (h :: acc) t
 
+        loop [] es
 
-    let funTyC = typeof<(obj -> obj)>.GetGenericTypeDefinition()
+    let funTyC = typeof<(obj -> obj)>.GetGenericTypeDefinition ()
     let exprTyC = typedefof>
     let voidTy = typeof
     let unitTy = typeof
-    let removeVoid a = if a = voidTy then unitTy else a
-    let addVoid a = if a = unitTy then voidTy else a
+
+    let removeVoid a =
+        if a = voidTy then unitTy else a
+
+    let addVoid a =
+        if a = unitTy then voidTy else a
+
     let mkFunTy a b =
         let (a, b) = removeVoid a, removeVoid b
-        funTyC.MakeGenericType([| a;b |])
+        funTyC.MakeGenericType([| a; b |])
 
-    let mkArrayTy (t: Type) = t.MakeArrayType()
-    let mkExprTy (t: Type) = exprTyC.MakeGenericType([| t |])
-    let rawExprTy = typeof
+    let mkArrayTy (t: Type) =
+        t.MakeArrayType()
 
+    let mkExprTy (t: Type) =
+        exprTyC.MakeGenericType([| t |])
+
+    let rawExprTy = typeof
 
     //--------------------------------------------------------------------------
     // Active patterns for decomposing quotations
     //--------------------------------------------------------------------------
 
-    let (|Comb0|_|) (E x) = match x with CombTerm(k, []) -> Some k | _ -> None
+    let (|Comb0|_|) (E x) =
+        match x with
+        | CombTerm (k, []) -> Some k
+        | _ -> None
 
-    let (|Comb1|_|) (E x) = match x with CombTerm(k, [x]) -> Some(k, x) | _ -> None
+    let (|Comb1|_|) (E x) =
+        match x with
+        | CombTerm (k, [ x ]) -> Some(k, x)
+        | _ -> None
 
-    let (|Comb2|_|) (E x) = match x with CombTerm(k, [x1;x2]) -> Some(k, x1, x2) | _ -> None
+    let (|Comb2|_|) (E x) =
+        match x with
+        | CombTerm (k, [ x1; x2 ]) -> Some(k, x1, x2)
+        | _ -> None
 
-    let (|Comb3|_|) (E x) = match x with CombTerm(k, [x1;x2;x3]) -> Some(k, x1, x2, x3) | _ -> None
+    let (|Comb3|_|) (E x) =
+        match x with
+        | CombTerm (k, [ x1; x2; x3 ]) -> Some(k, x1, x2, x3)
+        | _ -> None
 
     []
-    let (|Var|_|) (E x) = match x with VarTerm v -> Some v | _ -> None
+    let (|Var|_|) (E x) =
+        match x with
+        | VarTerm v -> Some v
+        | _ -> None
 
     []
-    let (|Application|_|) input = match input with Comb2(AppOp, a, b) -> Some (a, b) | _ -> None
+    let (|Application|_|) input =
+        match input with
+        | Comb2 (AppOp, a, b) -> Some(a, b)
+        | _ -> None
 
     []
-    let (|Lambda|_|) (E x) = match x with LambdaTerm(a, b) -> Some (a, b) | _ -> None
+    let (|Lambda|_|) (E x) =
+        match x with
+        | LambdaTerm (a, b) -> Some(a, b)
+        | _ -> None
 
     []
-    let (|Quote|_|) (E x) = match x with CombTerm(QuoteOp _, [a]) -> Some (a) | _ -> None
+    let (|Quote|_|) (E x) =
+        match x with
+        | CombTerm (QuoteOp _, [ a ]) -> Some(a)
+        | _ -> None
 
     []
-    let (|QuoteRaw|_|) (E x) = match x with CombTerm(QuoteOp false, [a]) -> Some (a) | _ -> None
+    let (|QuoteRaw|_|) (E x) =
+        match x with
+        | CombTerm (QuoteOp false, [ a ]) -> Some(a)
+        | _ -> None
 
     []
-    let (|QuoteTyped|_|) (E x) = match x with CombTerm(QuoteOp true, [a]) -> Some (a) | _ -> None
+    let (|QuoteTyped|_|) (E x) =
+        match x with
+        | CombTerm (QuoteOp true, [ a ]) -> Some(a)
+        | _ -> None
 
     []
-    let (|IfThenElse|_|) input = match input with Comb3(IfThenElseOp, e1, e2, e3) -> Some(e1, e2, e3) | _ -> None
+    let (|IfThenElse|_|) input =
+        match input with
+        | Comb3 (IfThenElseOp, e1, e2, e3) -> Some(e1, e2, e3)
+        | _ -> None
 
     []
-    let (|NewTuple|_|) input = match input with E(CombTerm(NewTupleOp(_), es)) -> Some es | _ -> None
+    let (|NewTuple|_|) input =
+        match input with
+        | E (CombTerm (NewTupleOp (_), es)) -> Some es
+        | _ -> None
 
     []
-    let (|NewStructTuple|_|) input = match input with E(CombTerm(NewTupleOp(ty), es)) when ty.IsValueType -> Some es | _ -> None
+    let (|NewStructTuple|_|) input =
+        match input with
+        | E (CombTerm (NewTupleOp (ty), es)) when ty.IsValueType -> Some es
+        | _ -> None
 
     []
-    let (|DefaultValue|_|) input = match input with E(CombTerm(DefaultValueOp ty, [])) -> Some ty | _ -> None
+    let (|DefaultValue|_|) input =
+        match input with
+        | E (CombTerm (DefaultValueOp ty, [])) -> Some ty
+        | _ -> None
 
     []
-    let (|NewRecord|_|) input = match input with E(CombTerm(NewRecordOp x, es)) -> Some(x, es) | _ -> None
+    let (|NewRecord|_|) input =
+        match input with
+        | E (CombTerm (NewRecordOp x, es)) -> Some(x, es)
+        | _ -> None
 
     []
-    let (|NewUnionCase|_|) input = match input with E(CombTerm(NewUnionCaseOp unionCase, es)) -> Some(unionCase, es) | _ -> None
+    let (|NewUnionCase|_|) input =
+        match input with
+        | E (CombTerm (NewUnionCaseOp unionCase, es)) -> Some(unionCase, es)
+        | _ -> None
 
     []
-    let (|UnionCaseTest|_|) input = match input with Comb1(UnionCaseTestOp unionCase, e) -> Some(e, unionCase) | _ -> None
+    let (|UnionCaseTest|_|) input =
+        match input with
+        | Comb1 (UnionCaseTestOp unionCase, e) -> Some(e, unionCase)
+        | _ -> None
 
     []
-    let (|TupleGet|_|) input = match input with Comb1(TupleGetOp(_, n), e) -> Some(e, n) | _ -> None
+    let (|TupleGet|_|) input =
+        match input with
+        | Comb1 (TupleGetOp (_, n), e) -> Some(e, n)
+        | _ -> None
 
     []
-    let (|Coerce|_|) input = match input with Comb1(CoerceOp ty, e1) -> Some(e1, ty) | _ -> None
+    let (|Coerce|_|) input =
+        match input with
+        | Comb1 (CoerceOp ty, e1) -> Some(e1, ty)
+        | _ -> None
 
     []
-    let (|TypeTest|_|) input = match input with Comb1(TypeTestOp ty, e1) -> Some(e1, ty) | _ -> None
+    let (|TypeTest|_|) input =
+        match input with
+        | Comb1 (TypeTestOp ty, e1) -> Some(e1, ty)
+        | _ -> None
 
     []
-    let (|NewArray|_|) input = match input with E(CombTerm(NewArrayOp ty, es)) -> Some(ty, es) | _ -> None
+    let (|NewArray|_|) input =
+        match input with
+        | E (CombTerm (NewArrayOp ty, es)) -> Some(ty, es)
+        | _ -> None
 
     []
-    let (|AddressSet|_|) input = match input with E(CombTerm(AddressSetOp, [e;v])) -> Some(e, v) | _ -> None
+    let (|AddressSet|_|) input =
+        match input with
+        | E (CombTerm (AddressSetOp, [ e; v ])) -> Some(e, v)
+        | _ -> None
 
     []
-    let (|TryFinally|_|) input = match input with E(CombTerm(TryFinallyOp, [e1;e2])) -> Some(e1, e2) | _ -> None
+    let (|TryFinally|_|) input =
+        match input with
+        | E (CombTerm (TryFinallyOp, [ e1; e2 ])) -> Some(e1, e2)
+        | _ -> None
 
     []
-    let (|TryWith|_|) input = match input with E(CombTerm(TryWithOp, [e1;Lambda(v1, e2);Lambda(v2, e3)])) -> Some(e1, v1, e2, v2, e3) | _ -> None
+    let (|TryWith|_|) input =
+        match input with
+        | E (CombTerm (TryWithOp, [ e1; Lambda (v1, e2); Lambda (v2, e3) ])) -> Some(e1, v1, e2, v2, e3)
+        | _ -> None
 
     []
-    let (|VarSet|_| ) input = match input with E(CombTerm(VarSetOp, [E(VarTerm v); e])) -> Some(v, e) | _ -> None
+    let (|VarSet|_|) input =
+        match input with
+        | E (CombTerm (VarSetOp, [ E (VarTerm v); e ])) -> Some(v, e)
+        | _ -> None
 
     []
-    let (|Value|_|) input = match input with E(CombTerm(ValueOp (v, ty, _), _)) -> Some(v, ty) | _ -> None
+    let (|Value|_|) input =
+        match input with
+        | E (CombTerm (ValueOp (v, ty, _), _)) -> Some(v, ty)
+        | _ -> None
 
     []
-    let (|ValueObj|_|) input = match input with E(CombTerm(ValueOp (v, _, _), _)) -> Some v | _ -> None
+    let (|ValueObj|_|) input =
+        match input with
+        | E (CombTerm (ValueOp (v, _, _), _)) -> Some v
+        | _ -> None
 
     []
     let (|ValueWithName|_|) input =
         match input with
-        | E(CombTerm(ValueOp (v, ty, Some nm), _)) -> Some(v, ty, nm)
+        | E (CombTerm (ValueOp (v, ty, Some nm), _)) -> Some(v, ty, nm)
         | _ -> None
 
     []
     let (|WithValue|_|) input =
         match input with
-        | E(CombTerm(WithValueOp (v, ty), [e])) -> Some(v, ty, e)
+        | E (CombTerm (WithValueOp (v, ty), [ e ])) -> Some(v, ty, e)
         | _ -> None
 
     []
     let (|AddressOf|_|) input =
         match input with
-        | Comb1(AddressOfOp, e) -> Some e
+        | Comb1 (AddressOfOp, e) -> Some e
         | _ -> None
 
     []
     let (|Sequential|_|) input =
         match input with
-        | Comb2(SequentialOp, e1, e2) -> Some(e1, e2)
+        | Comb2 (SequentialOp, e1, e2) -> Some(e1, e2)
         | _ -> None
 
     []
     let (|ForIntegerRangeLoop|_|) input =
         match input with
-        | Comb3(ForIntegerRangeLoopOp, e1, e2, Lambda(v, e3)) -> Some(v, e1, e2, e3)
+        | Comb3 (ForIntegerRangeLoopOp, e1, e2, Lambda (v, e3)) -> Some(v, e1, e2, e3)
         | _ -> None
 
     []
     let (|WhileLoop|_|) input =
         match input with
-        | Comb2(WhileLoopOp, e1, e2) -> Some(e1, e2)
+        | Comb2 (WhileLoopOp, e1, e2) -> Some(e1, e2)
         | _ -> None
 
     []
     let (|PropertyGet|_|) input =
         match input with
-        | E(CombTerm(StaticPropGetOp pinfo, args)) -> Some(None, pinfo, args)
-        | E(CombTerm(InstancePropGetOp pinfo, obj :: args)) -> Some(Some obj, pinfo, args)
+        | E (CombTerm (StaticPropGetOp pinfo, args)) -> Some(None, pinfo, args)
+        | E (CombTerm (InstancePropGetOp pinfo, obj :: args)) -> Some(Some obj, pinfo, args)
         | _ -> None
 
     []
     let (|PropertySet|_|) input =
         match input with
-        | E(CombTerm(StaticPropSetOp pinfo, FrontAndBack(args, v))) -> Some(None, pinfo, args, v)
-        | E(CombTerm(InstancePropSetOp pinfo, obj :: FrontAndBack(args, v))) -> Some(Some obj, pinfo, args, v)
+        | E (CombTerm (StaticPropSetOp pinfo, FrontAndBack (args, v))) -> Some(None, pinfo, args, v)
+        | E (CombTerm (InstancePropSetOp pinfo, obj :: FrontAndBack (args, v))) -> Some(Some obj, pinfo, args, v)
         | _ -> None
 
-
     []
     let (|FieldGet|_|) input =
         match input with
-        | E(CombTerm(StaticFieldGetOp finfo, [])) -> Some(None, finfo)
-        | E(CombTerm(InstanceFieldGetOp finfo, [obj])) -> Some(Some obj, finfo)
+        | E (CombTerm (StaticFieldGetOp finfo, [])) -> Some(None, finfo)
+        | E (CombTerm (InstanceFieldGetOp finfo, [ obj ])) -> Some(Some obj, finfo)
         | _ -> None
 
     []
     let (|FieldSet|_|) input =
         match input with
-        | E(CombTerm(StaticFieldSetOp finfo, [v])) -> Some(None, finfo, v)
-        | E(CombTerm(InstanceFieldSetOp finfo, [obj;v])) -> Some(Some obj, finfo, v)
+        | E (CombTerm (StaticFieldSetOp finfo, [ v ])) -> Some(None, finfo, v)
+        | E (CombTerm (InstanceFieldSetOp finfo, [ obj; v ])) -> Some(Some obj, finfo, v)
         | _ -> None
 
     []
     let (|NewObject|_|) input =
         match input with
-        | E(CombTerm(NewObjectOp ty, e)) -> Some(ty, e) | _ -> None
+        | E (CombTerm (NewObjectOp ty, e)) -> Some(ty, e)
+        | _ -> None
 
     []
     let (|Call|_|) input =
         match input with
-        | E(CombTerm(StaticMethodCallOp minfo, args)) -> Some(None, minfo, args)
+        | E (CombTerm (StaticMethodCallOp minfo, args)) -> Some(None, minfo, args)
 
-        | E(CombTerm(InstanceMethodCallOp minfo, (obj::args))) -> Some(Some(obj), minfo, args)
+        | E (CombTerm (InstanceMethodCallOp minfo, (obj :: args))) -> Some(Some(obj), minfo, args)
 
         // A StaticMethodCallWOp matches as if it were a StaticMethodCallOp
-        | E(CombTerm(StaticMethodCallWOp (minfo, _minfoW, nWitnesses), args)) when nWitnesses <= args.Length  ->
+        | E (CombTerm (StaticMethodCallWOp (minfo, _minfoW, nWitnesses), args)) when nWitnesses <= args.Length ->
             Some(None, minfo, List.skip nWitnesses args)
 
         // A InstanceMethodCallWOp matches as if it were a InstanceMethodCallOp
-        | E(CombTerm(InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj::argsWithoutObj)) when nWitnesses <= argsWithoutObj.Length  ->
+        | E (CombTerm (InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj :: argsWithoutObj)) when nWitnesses <= argsWithoutObj.Length ->
             let argsWithoutWitnesses = List.skip nWitnesses argsWithoutObj
-            Some (Some obj, minfo, argsWithoutWitnesses)
+            Some(Some obj, minfo, argsWithoutWitnesses)
 
         | _ -> None
 
     []
     let (|CallWithWitnesses|_|) input =
         match input with
-        | E(CombTerm(StaticMethodCallWOp (minfo, minfoW, nWitnesses), args)) ->
+        | E (CombTerm (StaticMethodCallWOp (minfo, minfoW, nWitnesses), args)) ->
             if args.Length >= nWitnesses then
                 let witnessArgs, argsWithoutWitnesses = List.splitAt nWitnesses args
                 Some(None, minfo, minfoW, witnessArgs, argsWithoutWitnesses)
             else
                 None
 
-        | E(CombTerm(InstanceMethodCallWOp (minfo, minfoW, nWitnesses), obj::argsWithoutObj)) ->
+        | E (CombTerm (InstanceMethodCallWOp (minfo, minfoW, nWitnesses), obj :: argsWithoutObj)) ->
             if argsWithoutObj.Length >= nWitnesses then
                 let witnessArgs, argsWithoutWitnesses = List.splitAt nWitnesses argsWithoutObj
-                Some (Some obj, minfo, minfoW, witnessArgs, argsWithoutWitnesses)
+                Some(Some obj, minfo, minfoW, witnessArgs, argsWithoutWitnesses)
             else
                 None
 
@@ -588,36 +791,38 @@ module Patterns =
 
     let (|LetRaw|_|) input =
         match input with
-        | Comb2(LetOp, e1, e2) -> Some(e1, e2)
+        | Comb2 (LetOp, e1, e2) -> Some(e1, e2)
         | _ -> None
 
     let (|LetRecRaw|_|) input =
         match input with
-        | Comb1(LetRecOp, e1) -> Some e1
+        | Comb1 (LetRecOp, e1) -> Some e1
         | _ -> None
 
     []
-    let (|Let|_|)input =
+    let (|Let|_|) input =
         match input with
-        | LetRaw(e, Lambda(v, body)) -> Some(v, e, body)
+        | LetRaw (e, Lambda (v, body)) -> Some(v, e, body)
         | _ -> None
 
-    let (|IteratedLambda|_|) (e: Expr) = qOneOrMoreRLinear (|Lambda|_|) e
+    let (|IteratedLambda|_|) (e: Expr) =
+        qOneOrMoreRLinear (|Lambda|_|) e
 
     let rec (|NLambdas|_|) n (e: Expr) =
         match e with
         | _ when n <= 0 -> Some([], e)
-        | Lambda(v, NLambdas ((-) n 1) (vs, b)) -> Some(v :: vs, b)
+        | Lambda (v, NLambdas ((-) n 1) (vs, b)) -> Some(v :: vs, b)
         | _ -> None
 
     []
-    let (|NewDelegate|_|) input  =
+    let (|NewDelegate|_|) input =
         match input with
-        | Comb1(NewDelegateOp ty, e) ->
+        | Comb1 (NewDelegateOp ty, e) ->
             let nargs = (getDelegateInvoke ty).GetParameters().Length
+
             if nargs = 0 then
                 match e with
-                | NLambdas 1 ([_], e) -> Some(ty, [], e) // try to strip the unit parameter if there is one
+                | NLambdas 1 ([ _ ], e) -> Some(ty, [], e) // try to strip the unit parameter if there is one
                 | NLambdas 0 ([], e) -> Some(ty, [], e)
                 | _ -> None
             else
@@ -629,7 +834,7 @@ module Patterns =
     []
     let (|LetRecursive|_|) input =
         match input with
-        | LetRecRaw(IteratedLambda(vs1, E(CombTerm(LetRecCombOp, body :: es)))) -> Some(List.zip vs1 es, body)
+        | LetRecRaw (IteratedLambda (vs1, E (CombTerm (LetRecCombOp, body :: es)))) -> Some(List.zip vs1 es, body)
         | _ -> None
 
     //--------------------------------------------------------------------------
@@ -637,44 +842,57 @@ module Patterns =
     //--------------------------------------------------------------------------
 
     // Returns record member specified by name
-    let getRecordProperty(ty, fieldName) =
+    let getRecordProperty (ty, fieldName) =
         let mems = FSharpType.GetRecordFields(ty, publicOrPrivateBindingFlags)
+
         match mems |> Array.tryFind (fun minfo -> minfo.Name = fieldName) with
         | Some (m) -> m
-        | _ -> invalidArg  "fieldName" (String.Format(SR.GetString(SR.QmissingRecordField), ty.FullName, fieldName))
+        | _ -> invalidArg "fieldName" (String.Format(SR.GetString(SR.QmissingRecordField), ty.FullName, fieldName))
 
-    let getUnionCaseInfo(ty, unionCaseName) =
+    let getUnionCaseInfo (ty, unionCaseName) =
         let cases = FSharpType.GetUnionCases(ty, publicOrPrivateBindingFlags)
+
         match cases |> Array.tryFind (fun ucase -> ucase.Name = unionCaseName) with
         | Some case -> case
-        | _ -> invalidArg  "unionCaseName" (String.Format(SR.GetString(SR.QmissingUnionCase), ty.FullName, unionCaseName))
+        | _ -> invalidArg "unionCaseName" (String.Format(SR.GetString(SR.QmissingUnionCase), ty.FullName, unionCaseName))
 
-    let getUnionCaseInfoField(unionCase:UnionCaseInfo, index) =
+    let getUnionCaseInfoField (unionCase: UnionCaseInfo, index) =
         let fields = unionCase.GetFields()
-        if index < 0 || index >= fields.Length then invalidArg "index" (SR.GetString(SR.QinvalidCaseIndex))
+
+        if index < 0 || index >= fields.Length then
+            invalidArg "index" (SR.GetString(SR.QinvalidCaseIndex))
+
         fields.[index]
 
     /// Returns type of lambda application - something like "(fun a -> ..) b"
     let rec typeOfAppliedLambda f =
         let fty = ((typeOf f): Type)
+
         match fty.GetGenericArguments() with
-        | [| _; b|] -> b
+        | [| _; b |] -> b
         | _ -> invalidOp (SR.GetString(SR.QillFormedAppOrLet))
 
     /// Returns type of the Raw quotation or fails if the quotation is ill formed
     /// if 'verify' is true, verifies all branches, otherwise ignores some of them when not needed
-    and typeOf<'T when 'T :> Expr> (e : 'T): Type =
+    and typeOf<'T when 'T :> Expr> (e: 'T) : Type =
         let (E t) = e
+
         match t with
-        | VarTerm    v -> v.Type
+        | VarTerm v -> v.Type
         | LambdaTerm (v, b) -> mkFunTy v.Type (typeOf b)
-        | HoleTerm   (ty, _) -> ty
-        | CombTerm   (c, args) ->
+        | HoleTerm (ty, _) -> ty
+        | CombTerm (c, args) ->
             match c, args with
-            | AppOp, [f;_] -> typeOfAppliedLambda f
-            | LetOp, _ -> match e with Let(_, _, b) -> typeOf b | _ -> failwith "unreachable"
-            | IfThenElseOp, [_;t;_] -> typeOf t
-            | LetRecOp, _ -> match e with LetRecursive(_, b) -> typeOf b | _ -> failwith "unreachable"
+            | AppOp, [ f; _ ] -> typeOfAppliedLambda f
+            | LetOp, _ ->
+                match e with
+                | Let (_, _, b) -> typeOf b
+                | _ -> failwith "unreachable"
+            | IfThenElseOp, [ _; t; _ ] -> typeOf t
+            | LetRecOp, _ ->
+                match e with
+                | LetRecursive (_, b) -> typeOf b
+                | _ -> failwith "unreachable"
             | LetRecCombOp, _ -> failwith "typeOfConst: LetRecCombOp"
             | NewRecordOp ty, _ -> ty
             | NewUnionCaseOp unionCase, _ -> unionCase.DeclaringType
@@ -697,33 +915,50 @@ module Patterns =
             | InstanceMethodCallWOp (_, minfoW, _), _ -> minfoW.ReturnType |> removeVoid
             | StaticMethodCallWOp (_, minfoW, _), _ -> minfoW.ReturnType |> removeVoid
             | CoerceOp ty, _ -> ty
-            | SequentialOp, [_;b] -> typeOf b
+            | SequentialOp, [ _; b ] -> typeOf b
             | ForIntegerRangeLoopOp, _ -> typeof
             | NewArrayOp ty, _ -> mkArrayTy ty
             | NewDelegateOp ty, _ -> ty
             | DefaultValueOp ty, _ -> ty
             | TypeTestOp _, _ -> typeof
-            | QuoteOp true, [expr] -> mkExprTy (typeOf expr)
-            | QuoteOp false, [_] -> rawExprTy
-            | TryFinallyOp, [e1;_] -> typeOf e1
-            | TryWithOp, [e1;_;_] -> typeOf e1
+            | QuoteOp true, [ expr ] -> mkExprTy (typeOf expr)
+            | QuoteOp false, [ _ ] -> rawExprTy
+            | TryFinallyOp, [ e1; _ ] -> typeOf e1
+            | TryWithOp, [ e1; _; _ ] -> typeOf e1
             | WhileLoopOp, _
             | VarSetOp, _
             | AddressSetOp, _ -> typeof
-            | AddressOfOp, [expr]-> (typeOf expr).MakeByRefType()
-            | (AddressOfOp | QuoteOp _ | SequentialOp | TryWithOp | TryFinallyOp | IfThenElseOp | AppOp), _ -> failwith "unreachable"
-
+            | AddressOfOp, [ expr ] -> (typeOf expr).MakeByRefType()
+            | (AddressOfOp
+              | QuoteOp _
+              | SequentialOp
+              | TryWithOp
+              | TryFinallyOp
+              | IfThenElseOp
+              | AppOp),
+              _ -> failwith "unreachable"
 
     //--------------------------------------------------------------------------
     // Constructors for building Raw quotations
     //--------------------------------------------------------------------------
 
-    let mkFEN op l = E(CombTerm(op, l))
-    let mkFE0 op = E(CombTerm(op, []))
-    let mkFE1 op x = E(CombTerm(op, [(x:>Expr)]))
-    let mkFE2 op (x, y) = E(CombTerm(op, [(x:>Expr);(y:>Expr)]))
-    let mkFE3 op (x, y, z) = E(CombTerm(op, [(x:>Expr);(y:>Expr);(z:>Expr)]) )
-    let mkOp v () = v
+    let mkFEN op l =
+        E(CombTerm(op, l))
+
+    let mkFE0 op =
+        E(CombTerm(op, []))
+
+    let mkFE1 op x =
+        E(CombTerm(op, [ (x :> Expr) ]))
+
+    let mkFE2 op (x, y) =
+        E(CombTerm(op, [ (x :> Expr); (y :> Expr) ]))
+
+    let mkFE3 op (x, y, z) =
+        E(CombTerm(op, [ (x :> Expr); (y :> Expr); (z :> Expr) ]))
+
+    let mkOp v () =
+        v
 
     //--------------------------------------------------------------------------
     // Type-checked constructors for building Raw quotations
@@ -733,79 +968,135 @@ module Patterns =
     let assignableFrom (t1: Type) (t2: Type) =
         t1.IsAssignableFrom t2
 
-    let checkTypesSR (expectedType: Type) (receivedType: Type) name (threeHoleSR : string) =
+    let checkTypesSR (expectedType: Type) (receivedType: Type) name (threeHoleSR: string) =
         if (expectedType <> receivedType) then
-          invalidArg "receivedType" (String.Format(threeHoleSR, name, expectedType, receivedType))
+            invalidArg "receivedType" (String.Format(threeHoleSR, name, expectedType, receivedType))
 
-    let checkTypesWeakSR (expectedType: Type) (receivedType: Type) name (threeHoleSR : string) =
+    let checkTypesWeakSR (expectedType: Type) (receivedType: Type) name (threeHoleSR: string) =
         if (not (assignableFrom expectedType receivedType)) then
-          invalidArg "receivedType" (String.Format(threeHoleSR, name, expectedType, receivedType))
+            invalidArg "receivedType" (String.Format(threeHoleSR, name, expectedType, receivedType))
 
     let checkArgs (paramInfos: ParameterInfo[]) (args: Expr list) =
-        if (paramInfos.Length <> args.Length) then invalidArg "args" (SR.GetString(SR.QincorrectNumArgs))
+        if (paramInfos.Length <> args.Length) then
+            invalidArg "args" (SR.GetString(SR.QincorrectNumArgs))
+
         List.iter2
-            ( fun (p:ParameterInfo) a -> checkTypesWeakSR p.ParameterType (typeOf a) "args" (SR.GetString(SR.QtmmInvalidParam)))
+            (fun (p: ParameterInfo) a -> checkTypesWeakSR p.ParameterType (typeOf a) "args" (SR.GetString(SR.QtmmInvalidParam)))
             (paramInfos |> Array.toList)
             args
-                                                // todo: shouldn't this be "strong" type check? sometimes?
+    // todo: shouldn't this be "strong" type check? sometimes?
 
     let checkAssignableFrom ty1 ty2 =
-        if not (assignableFrom ty1 ty2) then invalidArg "ty2" (SR.GetString(SR.QincorrectType))
+        if not (assignableFrom ty1 ty2) then
+            invalidArg "ty2" (SR.GetString(SR.QincorrectType))
 
-    let checkObj  (membInfo: MemberInfo) (obj: Expr) =
+    let checkObj (membInfo: MemberInfo) (obj: Expr) =
         // The MemberInfo may be a property associated with a union
         // find the actual related union type
-        let rec loop (ty: Type) = if FSharpType.IsUnion ty && FSharpType.IsUnion ty.BaseType then loop ty.BaseType else ty
+        let rec loop (ty: Type) =
+            if FSharpType.IsUnion ty && FSharpType.IsUnion ty.BaseType then
+                loop ty.BaseType
+            else
+                ty
+
         let declType = loop membInfo.DeclaringType
-        if not (assignableFrom declType (typeOf obj)) then invalidArg "obj" (SR.GetString(SR.QincorrectInstanceType))
 
+        if not (assignableFrom declType (typeOf obj)) then
+            invalidArg "obj" (SR.GetString(SR.QincorrectInstanceType))
 
     // Checks lambda application for correctness
     let checkAppliedLambda (f, v) =
         let fty = typeOf f
-        let ftyG = (if fty.IsGenericType then  fty.GetGenericTypeDefinition() else fty)
+
+        let ftyG =
+            (if fty.IsGenericType then
+                 fty.GetGenericTypeDefinition()
+             else
+                 fty)
+
         checkTypesSR funTyC ftyG "f" (SR.GetString(SR.QtmmExpectedFunction))
         let vty = (typeOf v)
+
         match fty.GetGenericArguments() with
-          | [| a; _ |] -> checkTypesSR vty a "f" (SR.GetString(SR.QtmmFunctionArgTypeMismatch))
-          | _ -> invalidArg  "f" (SR.GetString(SR.QinvalidFuncType))
+        | [| a; _ |] -> checkTypesSR vty a "f" (SR.GetString(SR.QtmmFunctionArgTypeMismatch))
+        | _ -> invalidArg "f" (SR.GetString(SR.QinvalidFuncType))
 
     // Returns option (by name) of a NewUnionCase type
     let getUnionCaseFields ty str =
         let cases = FSharpType.GetUnionCases(ty, publicOrPrivateBindingFlags)
+
         match cases |> Array.tryFind (fun ucase -> ucase.Name = str) with
         | Some case -> case.GetFields()
-        | _ -> invalidArg  "ty" (String.Format(SR.GetString(SR.notAUnionType), ty.FullName))
+        | _ -> invalidArg "ty" (String.Format(SR.GetString(SR.notAUnionType), ty.FullName))
 
-    let checkBind(v: Var, e) =
+    let checkBind (v: Var, e) =
         let ety = typeOf e
         checkTypesSR v.Type ety "let" (SR.GetString(SR.QtmmVarTypeNotMatchRHS))
 
     // [Correct by definition]
-    let mkVar v = E(VarTerm v )
-    let mkQuote(a, isTyped) = E(CombTerm(QuoteOp isTyped, [(a:>Expr)] ))
+    let mkVar v =
+        E(VarTerm v)
+
+    let mkQuote (a, isTyped) =
+        E(CombTerm(QuoteOp isTyped, [ (a :> Expr) ]))
+
+    let mkValue (v, ty) =
+        mkFE0 (ValueOp(v, ty, None))
+
+    let mkValueWithName (v, ty, nm) =
+        mkFE0 (ValueOp(v, ty, Some nm))
+
+    let mkValueWithDefn (v, ty, defn) =
+        mkFE1 (WithValueOp(v, ty)) defn
+
+    let mkValueG (v: 'T) =
+        mkValue (box v, typeof<'T>)
 
-    let mkValue (v, ty) = mkFE0 (ValueOp(v, ty, None))
-    let mkValueWithName (v, ty, nm) = mkFE0 (ValueOp(v, ty, Some nm))
-    let mkValueWithDefn (v, ty, defn) = mkFE1 (WithValueOp(v, ty)) defn
-    let mkValueG (v: 'T) = mkValue(box v, typeof<'T>)
     let mkLiftedValueOpG (v, ty: System.Type) =
-        let obj = if ty.IsEnum then System.Enum.ToObject(ty, box v) else box v
+        let obj =
+            if ty.IsEnum then
+                System.Enum.ToObject(ty, box v)
+            else
+                box v
+
         ValueOp(obj, ty, None)
-    let mkUnit       () = mkValue(null, typeof)
-    let mkAddressOf     v = mkFE1 AddressOfOp v
-    let mkSequential  (e1, e2) = mkFE2 SequentialOp (e1, e2)
-    let mkTypeTest    (e, ty) = mkFE1 (TypeTestOp ty) e
-    let mkVarSet    (v, e) = mkFE2 VarSetOp (mkVar v, e)
-    let mkAddressSet    (e1, e2) = mkFE2 AddressSetOp (e1, e2)
-    let mkLambda(var, body) = E(LambdaTerm(var, (body:>Expr)))
-    let mkTryWith(e1, v1, e2, v2, e3) = mkFE3 TryWithOp (e1, mkLambda(v1, e2), mkLambda(v2, e3))
-    let mkTryFinally(e1, e2) = mkFE2 TryFinallyOp (e1, e2)
 
-    let mkCoerce      (ty, x) = mkFE1 (CoerceOp ty) x
-    let mkNull        (ty) = mkFE0 (ValueOp(null, ty, None))
+    let mkUnit () =
+        mkValue (null, typeof)
+
+    let mkAddressOf v =
+        mkFE1 AddressOfOp v
+
+    let mkSequential (e1, e2) =
+        mkFE2 SequentialOp (e1, e2)
+
+    let mkTypeTest (e, ty) =
+        mkFE1 (TypeTestOp ty) e
 
-    let mkApplication v = checkAppliedLambda v; mkFE2 AppOp v
+    let mkVarSet (v, e) =
+        mkFE2 VarSetOp (mkVar v, e)
+
+    let mkAddressSet (e1, e2) =
+        mkFE2 AddressSetOp (e1, e2)
+
+    let mkLambda (var, body) =
+        E(LambdaTerm(var, (body :> Expr)))
+
+    let mkTryWith (e1, v1, e2, v2, e3) =
+        mkFE3 TryWithOp (e1, mkLambda (v1, e2), mkLambda (v2, e3))
+
+    let mkTryFinally (e1, e2) =
+        mkFE2 TryFinallyOp (e1, e2)
+
+    let mkCoerce (ty, x) =
+        mkFE1 (CoerceOp ty) x
+
+    let mkNull (ty) =
+        mkFE0 (ValueOp(null, ty, None))
+
+    let mkApplication v =
+        checkAppliedLambda v
+        mkFE2 AppOp v
 
     let mkLetRaw v =
         mkFE2 LetOp v
@@ -815,10 +1106,13 @@ module Patterns =
         mkLetRaw v
 
     // Tuples
-    let mkNewTupleWithType    (ty, args: Expr list) =
+    let mkNewTupleWithType (ty, args: Expr list) =
         let mems = FSharpType.GetTupleElements ty |> Array.toList
-        if (args.Length <> mems.Length) then invalidArg  "args" (SR.GetString(SR.QtupleLengthsDiffer))
-        List.iter2(fun mt a -> checkTypesSR mt (typeOf a) "args" (SR.GetString(SR.QtmmTuple)) ) mems args
+
+        if (args.Length <> mems.Length) then
+            invalidArg "args" (SR.GetString(SR.QtupleLengthsDiffer))
+
+        List.iter2 (fun mt a -> checkTypesSR mt (typeOf a) "args" (SR.GetString(SR.QtmmTuple))) mems args
         mkFEN (NewTupleOp ty) args
 
     let mkNewTuple (args) =
@@ -832,27 +1126,47 @@ module Patterns =
     let mkTupleGet (ty, n, x) =
         checkTypesSR ty (typeOf x) "tupleGet" (SR.GetString(SR.QtmmExprNotMatchTuple))
         let mems = FSharpType.GetTupleElements ty
-        if (n < 0 || mems.Length <= n) then invalidArg  "n" (SR.GetString(SR.QtupleAccessOutOfRange))
-        mkFE1 (TupleGetOp (ty, n)) x
+
+        if (n < 0 || mems.Length <= n) then
+            invalidArg "n" (SR.GetString(SR.QtupleAccessOutOfRange))
+
+        mkFE1 (TupleGetOp(ty, n)) x
 
     // Records
     let mkNewRecord (ty, args: Expr list) =
         let mems = FSharpType.GetRecordFields(ty, publicOrPrivateBindingFlags)
-        if (args.Length <> mems.Length) then invalidArg  "args" (SR.GetString(SR.QincompatibleRecordLength))
-        List.iter2 (fun (minfo: PropertyInfo) a -> checkTypesSR minfo.PropertyType (typeOf a) "recd" (SR.GetString(SR.QtmmIncorrectArgForRecord))) (Array.toList mems) args
-        mkFEN (NewRecordOp ty) args
 
+        if (args.Length <> mems.Length) then
+            invalidArg "args" (SR.GetString(SR.QincompatibleRecordLength))
+
+        List.iter2
+            (fun (minfo: PropertyInfo) a -> checkTypesSR minfo.PropertyType (typeOf a) "recd" (SR.GetString(SR.QtmmIncorrectArgForRecord)))
+            (Array.toList mems)
+            args
+
+        mkFEN (NewRecordOp ty) args
 
     // Discriminated unions
-    let mkNewUnionCase (unionCase:UnionCaseInfo, args: Expr list) =
-        if Unchecked.defaultof = unionCase then raise (new ArgumentNullException())
+    let mkNewUnionCase (unionCase: UnionCaseInfo, args: Expr list) =
+        if Unchecked.defaultof = unionCase then
+            raise (new ArgumentNullException())
+
         let sargs = unionCase.GetFields()
-        if (args.Length <> sargs.Length) then invalidArg  "args" (SR.GetString(SR.QunionNeedsDiffNumArgs))
-        List.iter2 (fun (minfo: PropertyInfo) a -> checkTypesSR minfo.PropertyType (typeOf a) "sum" (SR.GetString(SR.QtmmIncorrectArgForUnion))) (Array.toList sargs) args
+
+        if (args.Length <> sargs.Length) then
+            invalidArg "args" (SR.GetString(SR.QunionNeedsDiffNumArgs))
+
+        List.iter2
+            (fun (minfo: PropertyInfo) a -> checkTypesSR minfo.PropertyType (typeOf a) "sum" (SR.GetString(SR.QtmmIncorrectArgForUnion)))
+            (Array.toList sargs)
+            args
+
         mkFEN (NewUnionCaseOp unionCase) args
 
-    let mkUnionCaseTest (unionCase:UnionCaseInfo, expr) =
-        if Unchecked.defaultof = unionCase then raise (new ArgumentNullException())
+    let mkUnionCaseTest (unionCase: UnionCaseInfo, expr) =
+        if Unchecked.defaultof = unionCase then
+            raise (new ArgumentNullException())
+
         checkTypesSR unionCase.DeclaringType (typeOf expr) "UnionCaseTagTest" (SR.GetString(SR.QtmmExprTypeMismatch))
         mkFE1 (UnionCaseTestOp unionCase) expr
 
@@ -866,38 +1180,50 @@ module Patterns =
         List.iter (fun a -> checkTypesSR ty (typeOf a) "newArray" (SR.GetString(SR.QtmmInitArray))) args
         mkFEN (NewArrayOp ty) args
 
-    let mkInstanceFieldGet(obj, finfo:FieldInfo) =
-        if Unchecked.defaultof = finfo then raise (new ArgumentNullException())
+    let mkInstanceFieldGet (obj, finfo: FieldInfo) =
+        if Unchecked.defaultof = finfo then
+            raise (new ArgumentNullException())
+
         match finfo.IsStatic with
         | false ->
             checkObj finfo obj
             mkFE1 (InstanceFieldGetOp finfo) obj
-        | true -> invalidArg  "finfo" (SR.GetString(SR.QstaticWithReceiverObject))
+        | true -> invalidArg "finfo" (SR.GetString(SR.QstaticWithReceiverObject))
+
+    let mkStaticFieldGet (finfo: FieldInfo) =
+        if Unchecked.defaultof = finfo then
+            raise (new ArgumentNullException())
 
-    let mkStaticFieldGet (finfo:FieldInfo) =
-        if Unchecked.defaultof = finfo then raise (new ArgumentNullException())
         match finfo.IsStatic with
         | true -> mkFE0 (StaticFieldGetOp finfo)
-        | false -> invalidArg  "finfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+        | false -> invalidArg "finfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+
+    let mkStaticFieldSet (finfo: FieldInfo, value: Expr) =
+        if Unchecked.defaultof = finfo then
+            raise (new ArgumentNullException())
 
-    let mkStaticFieldSet (finfo:FieldInfo, value: Expr) =
-        if Unchecked.defaultof = finfo then raise (new ArgumentNullException())
         checkTypesSR (typeOf value) finfo.FieldType "value" (SR.GetString(SR.QtmmBadFieldType))
+
         match finfo.IsStatic with
         | true -> mkFE1 (StaticFieldSetOp finfo) value
-        | false -> invalidArg  "finfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+        | false -> invalidArg "finfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+
+    let mkInstanceFieldSet (obj, finfo: FieldInfo, value: Expr) =
+        if Unchecked.defaultof = finfo then
+            raise (new ArgumentNullException())
 
-    let mkInstanceFieldSet (obj, finfo:FieldInfo, value: Expr) =
-        if Unchecked.defaultof = finfo then raise (new ArgumentNullException())
         checkTypesSR (typeOf value) finfo.FieldType "value" (SR.GetString(SR.QtmmBadFieldType))
+
         match finfo.IsStatic with
         | false ->
             checkObj finfo obj
             mkFE2 (InstanceFieldSetOp finfo) (obj, value)
-        | true -> invalidArg  "finfo" (SR.GetString(SR.QstaticWithReceiverObject))
+        | true -> invalidArg "finfo" (SR.GetString(SR.QstaticWithReceiverObject))
+
+    let mkCtorCall (ci: ConstructorInfo, args: Expr list) =
+        if Unchecked.defaultof = ci then
+            raise (new ArgumentNullException())
 
-    let mkCtorCall (ci:ConstructorInfo, args: Expr list) =
-        if Unchecked.defaultof = ci then raise (new ArgumentNullException())
         checkArgs (ci.GetParameters()) args
         mkFEN (NewObjectOp ci) args
 
@@ -905,78 +1231,110 @@ module Patterns =
         mkFE0 (DefaultValueOp ty)
 
     let mkStaticPropGet (pinfo: PropertyInfo, args: Expr list) =
-        if Unchecked.defaultof = pinfo then raise (new ArgumentNullException())
-        if (not pinfo.CanRead) then invalidArg  "pinfo" (SR.GetString(SR.QreadingSetOnly))
+        if Unchecked.defaultof = pinfo then
+            raise (new ArgumentNullException())
+
+        if (not pinfo.CanRead) then
+            invalidArg "pinfo" (SR.GetString(SR.QreadingSetOnly))
+
         checkArgs (pinfo.GetIndexParameters()) args
+
         match pinfo.GetGetMethod(true).IsStatic with
-        | true -> mkFEN (StaticPropGetOp  pinfo) args
-        | false -> invalidArg  "pinfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+        | true -> mkFEN (StaticPropGetOp pinfo) args
+        | false -> invalidArg "pinfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
 
     let mkInstancePropGet (obj, pinfo: PropertyInfo, args: Expr list) =
-        if Unchecked.defaultof = pinfo then raise (new ArgumentNullException())
-        if (not pinfo.CanRead) then invalidArg  "pinfo" (SR.GetString(SR.QreadingSetOnly))
+        if Unchecked.defaultof = pinfo then
+            raise (new ArgumentNullException())
+
+        if (not pinfo.CanRead) then
+            invalidArg "pinfo" (SR.GetString(SR.QreadingSetOnly))
+
         checkArgs (pinfo.GetIndexParameters()) args
+
         match pinfo.GetGetMethod(true).IsStatic with
         | false ->
             checkObj pinfo obj
             mkFEN (InstancePropGetOp pinfo) (obj :: args)
-        | true -> invalidArg  "pinfo" (SR.GetString(SR.QstaticWithReceiverObject))
+        | true -> invalidArg "pinfo" (SR.GetString(SR.QstaticWithReceiverObject))
 
     let mkStaticPropSet (pinfo: PropertyInfo, args: Expr list, value: Expr) =
-        if Unchecked.defaultof = pinfo then raise (new ArgumentNullException())
-        if (not pinfo.CanWrite) then invalidArg  "pinfo" (SR.GetString(SR.QwritingGetOnly))
+        if Unchecked.defaultof = pinfo then
+            raise (new ArgumentNullException())
+
+        if (not pinfo.CanWrite) then
+            invalidArg "pinfo" (SR.GetString(SR.QwritingGetOnly))
+
         checkArgs (pinfo.GetIndexParameters()) args
+
         match pinfo.GetSetMethod(true).IsStatic with
-        | true -> mkFEN (StaticPropSetOp pinfo) (args@[value])
-        | false -> invalidArg  "pinfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+        | true -> mkFEN (StaticPropSetOp pinfo) (args @ [ value ])
+        | false -> invalidArg "pinfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
 
     let mkInstancePropSet (obj, pinfo: PropertyInfo, args: Expr list, value: Expr) =
-        if Unchecked.defaultof = pinfo then raise (new ArgumentNullException())
-        if (not pinfo.CanWrite) then invalidArg  "pinfo" (SR.GetString(SR.QwritingGetOnly))
+        if Unchecked.defaultof = pinfo then
+            raise (new ArgumentNullException())
+
+        if (not pinfo.CanWrite) then
+            invalidArg "pinfo" (SR.GetString(SR.QwritingGetOnly))
+
         checkArgs (pinfo.GetIndexParameters()) args
+
         match pinfo.GetSetMethod(true).IsStatic with
         | false ->
             checkObj pinfo obj
-            mkFEN (InstancePropSetOp pinfo) (obj :: (args@[value]))
-        | true -> invalidArg  "pinfo" (SR.GetString(SR.QstaticWithReceiverObject))
+            mkFEN (InstancePropSetOp pinfo) (obj :: (args @ [ value ]))
+        | true -> invalidArg "pinfo" (SR.GetString(SR.QstaticWithReceiverObject))
+
+    let mkInstanceMethodCall (obj, minfo: MethodInfo, args: Expr list) =
+        if Unchecked.defaultof = minfo then
+            raise (new ArgumentNullException())
 
-    let mkInstanceMethodCall (obj, minfo:MethodInfo, args: Expr list) =
-        if Unchecked.defaultof = minfo then raise (new ArgumentNullException())
         checkArgs (minfo.GetParameters()) args
+
         match minfo.IsStatic with
         | false ->
             checkObj minfo obj
             mkFEN (InstanceMethodCallOp minfo) (obj :: args)
-        | true -> invalidArg  "minfo" (SR.GetString(SR.QstaticWithReceiverObject))
+        | true -> invalidArg "minfo" (SR.GetString(SR.QstaticWithReceiverObject))
 
     let mkInstanceMethodCallW (obj, minfo: MethodInfo, minfoW: MethodInfo, nWitnesses: int, args: Expr list) =
-        if Unchecked.defaultof = minfo then raise (new ArgumentNullException())
+        if Unchecked.defaultof = minfo then
+            raise (new ArgumentNullException())
+
         checkArgs (minfoW.GetParameters()) args
+
         match minfoW.IsStatic with
         | false ->
             checkObj minfo obj
-            mkFEN (InstanceMethodCallWOp (minfo, minfoW, nWitnesses)) (obj::args)
-        | true -> invalidArg  "minfo" (SR.GetString(SR.QstaticWithReceiverObject))
+            mkFEN (InstanceMethodCallWOp(minfo, minfoW, nWitnesses)) (obj :: args)
+        | true -> invalidArg "minfo" (SR.GetString(SR.QstaticWithReceiverObject))
+
+    let mkStaticMethodCall (minfo: MethodInfo, args: Expr list) =
+        if Unchecked.defaultof = minfo then
+            raise (new ArgumentNullException())
 
-    let mkStaticMethodCall (minfo:MethodInfo, args: Expr list) =
-        if Unchecked.defaultof = minfo then raise (new ArgumentNullException())
         checkArgs (minfo.GetParameters()) args
+
         match minfo.IsStatic with
         | true -> mkFEN (StaticMethodCallOp minfo) args
-        | false -> invalidArg  "minfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+        | false -> invalidArg "minfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
 
     let mkStaticMethodCallW (minfo: MethodInfo, minfoW: MethodInfo, nWitnesses: int, args: Expr list) =
-        if Unchecked.defaultof = minfo then raise (new ArgumentNullException())
+        if Unchecked.defaultof = minfo then
+            raise (new ArgumentNullException())
+
         checkArgs (minfoW.GetParameters()) args
+
         match minfo.IsStatic with
-        | true -> mkFEN (StaticMethodCallWOp (minfo, minfoW, nWitnesses)) args
-        | false -> invalidArg  "minfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
+        | true -> mkFEN (StaticMethodCallWOp(minfo, minfoW, nWitnesses)) args
+        | false -> invalidArg "minfo" (SR.GetString(SR.QnonStaticNoReceiverObject))
 
     let mkForLoop (v: Var, lowerBound, upperBound, body) =
         checkTypesSR (typeof) (typeOf lowerBound) "lowerBound" (SR.GetString(SR.QtmmLowerUpperBoundMustBeInt))
         checkTypesSR (typeof) (typeOf upperBound) "upperBound" (SR.GetString(SR.QtmmLowerUpperBoundMustBeInt))
         checkTypesSR (typeof) (v.Type) "for" (SR.GetString(SR.QtmmLoopBodyMustBeLambdaTakingInteger))
-        mkFE3 ForIntegerRangeLoopOp (lowerBound, upperBound, mkLambda(v, body))
+        mkFE3 ForIntegerRangeLoopOp (lowerBound, upperBound, mkLambda (v, body))
 
     let mkWhileLoop (guard, body) =
         checkTypesSR (typeof) (typeOf guard) "guard" (SR.GetString(SR.QtmmGuardMustBeBool))
@@ -986,31 +1344,40 @@ module Patterns =
     let mkNewDelegate (ty, e) =
         let mi = getDelegateInvoke ty
         let ps = mi.GetParameters()
-        let dlfun = Array.foldBack (fun (p:ParameterInfo) retTy -> mkFunTy p.ParameterType retTy) ps mi.ReturnType
+
+        let dlfun =
+            Array.foldBack (fun (p: ParameterInfo) retTy -> mkFunTy p.ParameterType retTy) ps mi.ReturnType
+
         checkTypesSR dlfun (typeOf e) "ty" (SR.GetString(SR.QtmmFunTypeNotMatchDelegate))
         mkFE1 (NewDelegateOp ty) e
 
     let mkLet (v, e, b) =
         checkBind (v, e)
-        mkLetRaw (e, mkLambda(v, b))
+        mkLetRaw (e, mkLambda (v, b))
 
     //let mkLambdas(vs, b) = mkRLinear mkLambdaRaw (vs, (b:>Expr))
     let mkTupledApplication (f, args) =
         match args with
-        | [] -> mkApplication (f, mkUnit())
-        | [x] -> mkApplication (f, x)
+        | [] -> mkApplication (f, mkUnit ())
+        | [ x ] -> mkApplication (f, x)
         | _ -> mkApplication (f, mkNewTuple args)
 
-    let mkApplications(f: Expr, es: Expr list list) = mkLLinear mkTupledApplication (f, es)
+    let mkApplications (f: Expr, es: Expr list list) =
+        mkLLinear mkTupledApplication (f, es)
+
+    let mkIteratedLambdas (vs, b) =
+        mkRLinear mkLambda (vs, b)
+
+    let mkLetRecRaw v =
+        mkFE1 LetRecOp v
 
-    let mkIteratedLambdas(vs, b) = mkRLinear  mkLambda (vs, b)
+    let mkLetRecCombRaw v =
+        mkFEN LetRecCombOp v
 
-    let mkLetRecRaw v = mkFE1 LetRecOp v
-    let mkLetRecCombRaw v = mkFEN LetRecCombOp v
-    let mkLetRec (ves:(Var*Expr) list, body) =
+    let mkLetRec (ves: (Var * Expr) list, body) =
         List.iter checkBind ves
         let vs, es = List.unzip ves
-        mkLetRecRaw(mkIteratedLambdas (vs, mkLetRecCombRaw (body :: es)))
+        mkLetRecRaw (mkIteratedLambdas (vs, mkLetRecCombRaw (body :: es)))
 
     let ReflectedDefinitionsResourceNameBase = "ReflectedDefinitions"
 
@@ -1025,77 +1392,107 @@ module Patterns =
         | Unique of 'T
         | Ambiguous of 'R
 
-    let typeEquals (s: Type) (t: Type) = s.Equals t
+    let typeEquals (s: Type) (t: Type) =
+        s.Equals t
 
     let typesEqual (ss: Type list) (tt: Type list) =
-      (ss.Length = tt.Length) && List.forall2 typeEquals ss tt
+        (ss.Length = tt.Length) && List.forall2 typeEquals ss tt
 
-    let instFormal (typarEnv: Type[]) (ty:Instantiable<'T>) = ty (fun i -> typarEnv.[i])
+    let instFormal (typarEnv: Type[]) (ty: Instantiable<'T>) =
+        ty (fun i -> typarEnv.[i])
 
-    let getGenericArguments(genericType: Type) =
-        if genericType.IsGenericType then genericType.GetGenericArguments() else [| |]
+    let getGenericArguments (genericType: Type) =
+        if genericType.IsGenericType then
+            genericType.GetGenericArguments()
+        else
+            [||]
 
-    let getNumGenericArguments(genericType: Type) =
-        if genericType.IsGenericType then genericType.GetGenericArguments().Length else 0
+    let getNumGenericArguments (genericType: Type) =
+        if genericType.IsGenericType then
+            genericType.GetGenericArguments().Length
+        else
+            0
 
     let bindMethodBySearch (knownArgCount: int voption, parentT: Type, nm, marity, argTys, retTy) =
         let methInfos = parentT.GetMethods staticOrInstanceBindingFlags |> Array.toList
         // First, filter on name, if unique, then binding "done"
         let tyargTs = getGenericArguments parentT
         let methInfos = methInfos |> List.filter (fun methInfo -> methInfo.Name = nm)
+
         match methInfos with
-        | [methInfo] ->
-            methInfo
+        | [ methInfo ] -> methInfo
         | _ ->
             // Second, type match.
-            let select (methInfo:MethodInfo) =
+            let select (methInfo: MethodInfo) =
                 // mref implied Types
-                let mtyargTIs = if methInfo.IsGenericMethod then methInfo.GetGenericArguments() else [| |]
-                if mtyargTIs.Length  <> marity then false (* method generic arity mismatch *) else
-                let typarEnv = (Array.append tyargTs mtyargTIs)
-                let argTs = argTys |> List.map (instFormal typarEnv)
-                let resT  = instFormal typarEnv retTy
-
-                // methInfo implied Types
-                let haveArgTs =
-                    let parameters = Array.toList (methInfo.GetParameters())
-                    parameters |> List.map (fun param -> param.ParameterType)
-                let haveResT  = methInfo.ReturnType
-                
-                let nargTs = argTs.Length
-                
-                // check for match
-                if nargTs <> haveArgTs.Length then false (* method argument length mismatch *) else
-
-                // If a known-number-of-arguments-including-object-argument has been given then check that
-                if (match knownArgCount with 
-                    | ValueNone -> false
-                    | ValueSome n -> n <> (if methInfo.IsStatic then 0 else 1) + nargTs) then false else
-
-                let res = typesEqual (resT :: argTs) (haveResT :: haveArgTs)
-                res
+                let mtyargTIs =
+                    if methInfo.IsGenericMethod then
+                        methInfo.GetGenericArguments()
+                    else
+                        [||]
+
+                if mtyargTIs.Length <> marity then
+                    false (* method generic arity mismatch *)
+                else
+                    let typarEnv = (Array.append tyargTs mtyargTIs)
+                    let argTs = argTys |> List.map (instFormal typarEnv)
+                    let resT = instFormal typarEnv retTy
+
+                    // methInfo implied Types
+                    let haveArgTs =
+                        let parameters = Array.toList (methInfo.GetParameters())
+                        parameters |> List.map (fun param -> param.ParameterType)
+
+                    let haveResT = methInfo.ReturnType
+
+                    let nargTs = argTs.Length
+
+                    // check for match
+                    if nargTs <> haveArgTs.Length then
+                        false (* method argument length mismatch *)
+                    else
+
+                    // If a known-number-of-arguments-including-object-argument has been given then check that
+                    if (match knownArgCount with
+                        | ValueNone -> false
+                        | ValueSome n -> n <> (if methInfo.IsStatic then 0 else 1) + nargTs) then
+                        false
+                    else
+
+                        let res = typesEqual (resT :: argTs) (haveResT :: haveArgTs)
+                        res
             // return MethodInfo for (generic) type's (generic) method
             match List.tryFind select methInfos with
-            | None          -> invalidOp (SR.GetString SR.QcannotBindToMethod)
+            | None -> invalidOp (SR.GetString SR.QcannotBindToMethod)
             | Some methInfo -> methInfo
 
     let bindMethodHelper (knownArgCount, (parentT: Type, nm, marity, argTys, retTy)) =
-      if isNull parentT then invalidArg "parentT" (SR.GetString(SR.QparentCannotBeNull))
-      if marity = 0 then
-          let tyargTs = if parentT.IsGenericType then parentT.GetGenericArguments() else [| |]
-          let argTs = Array.ofList (List.map (instFormal tyargTs) argTys)
-          let resT  = instFormal tyargTs retTy
-          let methInfo =
-              try
-                 match parentT.GetMethod(nm, staticOrInstanceBindingFlags, null, argTs, null) with
-                 | null -> None
-                 | res -> Some res
-               with :? AmbiguousMatchException -> None
-          match methInfo with
-          | Some methInfo when (typeEquals resT methInfo.ReturnType) -> methInfo
-          | _ -> bindMethodBySearch(knownArgCount, parentT, nm, marity, argTys, retTy)
-      else
-          bindMethodBySearch(knownArgCount, parentT, nm, marity, argTys, retTy)
+        if isNull parentT then
+            invalidArg "parentT" (SR.GetString(SR.QparentCannotBeNull))
+
+        if marity = 0 then
+            let tyargTs =
+                if parentT.IsGenericType then
+                    parentT.GetGenericArguments()
+                else
+                    [||]
+
+            let argTs = Array.ofList (List.map (instFormal tyargTs) argTys)
+            let resT = instFormal tyargTs retTy
+
+            let methInfo =
+                try
+                    match parentT.GetMethod(nm, staticOrInstanceBindingFlags, null, argTs, null) with
+                    | null -> None
+                    | res -> Some res
+                with :? AmbiguousMatchException ->
+                    None
+
+            match methInfo with
+            | Some methInfo when (typeEquals resT methInfo.ReturnType) -> methInfo
+            | _ -> bindMethodBySearch (knownArgCount, parentT, nm, marity, argTys, retTy)
+        else
+            bindMethodBySearch (knownArgCount, parentT, nm, marity, argTys, retTy)
 
     let bindModuleProperty (ty: Type, nm) =
         match ty.GetProperty(nm, staticBindingFlags) with
@@ -1105,37 +1502,45 @@ module Patterns =
     let bindModuleFunctionWithCallSiteArgs (ty: Type, nm, argTypes: Type list, tyArgs: Type list) =
         let argTypes = List.toArray argTypes
         let tyArgs = List.toArray tyArgs
+
         let methInfo =
             try
                 match ty.GetMethod(nm, staticOrInstanceBindingFlags, null, argTypes, null) with
                 | null -> None
                 | res -> Some res
-            with :? AmbiguousMatchException -> None
+            with :? AmbiguousMatchException ->
+                None
+
         match methInfo with
         | Some methInfo -> methInfo
         | _ ->
             // narrow down set of candidates by removing methods with a different name\number of arguments\number of type parameters
             let candidates =
                 ty.GetMethods staticBindingFlags
-                |> Array.filter(fun mi ->
-                    mi.Name = nm &&
-                    mi.GetParameters().Length = argTypes.Length &&
-                    let methodTyArgCount = if mi.IsGenericMethod then mi.GetGenericArguments().Length else 0
-                    methodTyArgCount = tyArgs.Length
-                )
-            let fail() = invalidOp (String.Format(SR.GetString(SR.QcannotBindFunction), nm, ty.ToString()))
+                |> Array.filter (fun mi ->
+                    mi.Name = nm
+                    && mi.GetParameters().Length = argTypes.Length
+                    && let methodTyArgCount =
+                        if mi.IsGenericMethod then
+                            mi.GetGenericArguments().Length
+                        else
+                            0 in
+                       methodTyArgCount = tyArgs.Length)
+
+            let fail () =
+                invalidOp (String.Format(SR.GetString(SR.QcannotBindFunction), nm, ty.ToString()))
+
             match candidates with
-            | [||] -> fail()
+            | [||] -> fail ()
             | [| solution |] -> solution
             | candidates ->
                 let solution =
                     // no type arguments - just perform pairwise comparison of type in methods signature and argument type from the callsite
                     if tyArgs.Length = 0 then
                         candidates
-                        |> Array.tryFind(fun mi ->
+                        |> Array.tryFind (fun mi ->
                             let paramTys = mi.GetParameters() |> Array.map (fun pi -> pi.ParameterType)
-                            Array.forall2 (=) argTypes paramTys
-                        )
+                            Array.forall2 (=) argTypes paramTys)
                     else
                         let FAIL = -1
                         let MATCH = 2
@@ -1154,39 +1559,53 @@ module Patterns =
                         // - exact match with actual argument type adds MATCH value to the final result
                         // - parameter type is generic that after instantiation matches actual argument type adds GENERIC_MATCH to the final result
                         // - parameter type is generic that after instantiation doesn't actual argument type stops computation and return FAIL as the final result
-                        let weight (mi : MethodInfo) =
+                        let weight (mi: MethodInfo) =
                             let parameters = mi.GetParameters()
+
                             let rec iter i acc =
-                                if i >= argTypes.Length then acc
-                                else
-                                let param = parameters.[i]
-                                if param.ParameterType.IsGenericParameter then
-                                    let actualTy = tyArgs.[param.ParameterType.GenericParameterPosition]
-                                    if actualTy = argTypes.[i] then iter (i + 1) (acc + GENERIC_MATCH) else FAIL
+                                if i >= argTypes.Length then
+                                    acc
                                 else
-                                    if param.ParameterType = argTypes.[i] then iter (i + 1) (acc + MATCH) else FAIL
+                                    let param = parameters.[i]
+
+                                    if param.ParameterType.IsGenericParameter then
+                                        let actualTy = tyArgs.[param.ParameterType.GenericParameterPosition]
+
+                                        if actualTy = argTypes.[i] then
+                                            iter (i + 1) (acc + GENERIC_MATCH)
+                                        else
+                                            FAIL
+                                    else if param.ParameterType = argTypes.[i] then
+                                        iter (i + 1) (acc + MATCH)
+                                    else
+                                        FAIL
+
                             iter 0 0
+
                         let solution, weight =
-                            candidates
-                            |> Array.map (fun mi -> mi, weight mi)
-                            |> Array.maxBy snd
-                        if weight = FAIL then None
-                        else Some solution
+                            candidates |> Array.map (fun mi -> mi, weight mi) |> Array.maxBy snd
+
+                        if weight = FAIL then
+                            None
+                        else
+                            Some solution
+
                 match solution with
                 | Some mi -> mi
-                | None -> fail()
+                | None -> fail ()
 
     let mkNamedType (genericType: Type, tyargs) =
-        match  tyargs with
+        match tyargs with
         | [] -> genericType
         | _ -> genericType.MakeGenericType(Array.ofList tyargs)
 
-    let inline checkNonNullResult (arg:string, err:string) y =
+    let inline checkNonNullResult (arg: string, err: string) y =
         match box y with
         | null -> raise (new ArgumentNullException(arg, err))
         | _ -> y
 
-    let inst (tyargs: Type list) (i: Instantiable<'T>) = i (fun idx -> tyargs.[idx]) // Note, O n looks, but #tyargs is always small
+    let inst (tyargs: Type list) (i: Instantiable<'T>) =
+        i (fun idx -> tyargs.[idx]) // Note, O n looks, but #tyargs is always small
 
     let bindPropBySearchIfCandidateIsNull (ty: Type) propName retType argTypes candidate =
         match candidate with
@@ -1195,11 +1614,12 @@ module Patterns =
                 ty.GetProperties staticOrInstanceBindingFlags
                 |> Array.filter (fun pi ->
                     let paramTypes = getTypesFromParamInfos (pi.GetIndexParameters())
-                    pi.Name = propName &&
-                    pi.PropertyType = retType &&
-                    Array.length argTypes = paramTypes.Length &&
-                    Array.forall2 (=) argTypes paramTypes
-                    )
+
+                    pi.Name = propName
+                    && pi.PropertyType = retType
+                    && Array.length argTypes = paramTypes.Length
+                    && Array.forall2 (=) argTypes paramTypes)
+
             match props with
             | [| pi |] -> pi
             | _ -> null
@@ -1212,9 +1632,10 @@ module Patterns =
                 ty.GetConstructors instanceBindingFlags
                 |> Array.filter (fun ci ->
                     let paramTypes = getTypesFromParamInfos (ci.GetParameters())
-                    Array.length argTypes = paramTypes.Length &&
-                    Array.forall2 (=) argTypes paramTypes
-                )
+
+                    Array.length argTypes = paramTypes.Length
+                    && Array.forall2 (=) argTypes paramTypes)
+
             match ctors with
             | [| ctor |] -> ctor
             | _ -> null
@@ -1223,79 +1644,97 @@ module Patterns =
     let bindProp (genericType, propName, retType, argTypes, tyargs) =
         // We search in the instantiated type, rather than searching the generic type.
         let typ = mkNamedType (genericType, tyargs)
-        let argTypes : Type list = argTypes |> inst tyargs
-        let retType : Type = retType |> inst tyargs |> removeVoid
+        let argTypes: Type list = argTypes |> inst tyargs
+        let retType: Type = retType |> inst tyargs |> removeVoid
         // fxcop may not see "propName" as an arg
         typ.GetProperty(propName, staticOrInstanceBindingFlags, null, retType, Array.ofList argTypes, null)
         |> checkNonNullResult ("propName", String.Format(SR.GetString(SR.QfailedToBindProperty), propName))
 
     let bindField (genericType, fldName, tyargs) =
         let typ = mkNamedType (genericType, tyargs)
+
         typ.GetField(fldName, staticOrInstanceBindingFlags)
         |> checkNonNullResult ("fldName", String.Format(SR.GetString(SR.QfailedToBindField), fldName)) // fxcop may not see "fldName" as an arg
 
     let bindGenericCctor (genericType: Type) =
-        genericType.GetConstructor(staticBindingFlags, null, [| |], null)
+        genericType.GetConstructor(staticBindingFlags, null, [||], null)
         |> checkNonNullResult ("genericType", SR.GetString(SR.QfailedToBindConstructor))
 
     let bindGenericCtor (genericType: Type, argTypes: Instantiable) =
         let argTypes = instFormal (getGenericArguments genericType) argTypes
+
         genericType.GetConstructor(instanceBindingFlags, null, Array.ofList argTypes, null)
         |> checkNonNullResult ("genericType", SR.GetString(SR.QfailedToBindConstructor))
 
     let bindCtor (genericType, argTypes: Instantiable, tyargs) =
         let typ = mkNamedType (genericType, tyargs)
         let argTypes = argTypes |> inst tyargs
+
         typ.GetConstructor(instanceBindingFlags, null, Array.ofList argTypes, null)
         |> checkNonNullResult ("genericType", SR.GetString(SR.QfailedToBindConstructor))
 
     let chop n xs =
-        if n < 0 then invalidArg "n" (SR.GetString(SR.inputMustBeNonNegative))
+        if n < 0 then
+            invalidArg "n" (SR.GetString(SR.inputMustBeNonNegative))
+
         let rec split l =
             match l with
             | 0, xs -> [], xs
             | n, x :: xs ->
-                let front, back = split (n-1, xs)
+                let front, back = split (n - 1, xs)
                 x :: front, back
             | _, [] -> failwith "List.chop: not enough elts list"
+
         split (n, xs)
 
     let instMeth (ngmeth: MethodInfo, methTypeArgs) =
-        if ngmeth.GetGenericArguments().Length = 0 then ngmeth(* non generic *)
-        else ngmeth.MakeGenericMethod(Array.ofList methTypeArgs)
+        if ngmeth.GetGenericArguments().Length = 0 then
+            ngmeth (* non generic *)
+        else
+            ngmeth.MakeGenericMethod(Array.ofList methTypeArgs)
 
     let bindGenericMeth (knownArgCount, (genericType: Type, argTypes, retType, methName, numMethTyargs)) =
-        bindMethodHelper(knownArgCount, (genericType, methName, numMethTyargs, argTypes, retType))
+        bindMethodHelper (knownArgCount, (genericType, methName, numMethTyargs, argTypes, retType))
 
     let bindMeth (knownArgCount, (genericType: Type, argTypes, retType, methName, numMethTyargs), tyargs) =
         let numEnclTypeArgs = genericType.GetGenericArguments().Length
         let enclTypeArgs, methTypeArgs = chop numEnclTypeArgs tyargs
         let ty = mkNamedType (genericType, enclTypeArgs)
-        let ngmeth = bindMethodHelper(knownArgCount, (ty, methName, numMethTyargs, argTypes, retType))
-        instMeth(ngmeth, methTypeArgs)
+
+        let ngmeth =
+            bindMethodHelper (knownArgCount, (ty, methName, numMethTyargs, argTypes, retType))
+
+        instMeth (ngmeth, methTypeArgs)
 
     let pinfoIsStatic (pinfo: PropertyInfo) =
-        if pinfo.CanRead then pinfo.GetGetMethod(true).IsStatic
-        elif pinfo.CanWrite then pinfo.GetSetMethod(true).IsStatic
-        else false
+        if pinfo.CanRead then
+            pinfo.GetGetMethod(true).IsStatic
+        elif pinfo.CanWrite then
+            pinfo.GetSetMethod(true).IsStatic
+        else
+            false
 
     /// Unpickling
     module SimpleUnpickle =
 
         []
         type InputState =
-          { is: ByteStream
-            istrings: string[]
-            localAssembly: System.Reflection.Assembly
-            referencedTypeDefs: Type[] }
+            {
+                is: ByteStream
+                istrings: string[]
+                localAssembly: System.Reflection.Assembly
+                referencedTypeDefs: Type[]
+            }
 
-        let u_byte_as_int st = st.is.ReadByte()
+        let u_byte_as_int st =
+            st.is.ReadByte()
 
         let u_bool st =
             let b = u_byte_as_int st
             (b = 1)
 
-        let u_void (_: InputState) = ()
+        let u_void (_: InputState) =
+            ()
 
         let prim_u_int32 st =
             let b0 = (u_byte_as_int st)
@@ -1306,7 +1745,9 @@ module Patterns =
 
         let u_int32 st =
             let b0 = u_byte_as_int st
-            if b0 <= 0x7F then b0
+
+            if b0 <= 0x7F then
+                b0
             elif b0 <= 0xbf then
                 let b0 = b0 &&& 0x7f
                 let b1 = (u_byte_as_int st)
@@ -1322,72 +1763,111 @@ module Patterns =
             let len = u_int32 st
             st.is.ReadUtf8BytesAsString len
 
-        let u_int st = u_int32 st
+        let u_int st =
+            u_int32 st
 
-        let u_sbyte st = sbyte (u_int32 st)
+        let u_sbyte st =
+            sbyte (u_int32 st)
 
-        let u_byte st = byte (u_byte_as_int st)
+        let u_byte st =
+            byte (u_byte_as_int st)
 
-        let u_int16 st = int16 (u_int32 st)
+        let u_int16 st =
+            int16 (u_int32 st)
 
-        let u_uint16 st = uint16 (u_int32 st)
+        let u_uint16 st =
+            uint16 (u_int32 st)
 
-        let u_uint32 st = uint32 (u_int32 st)
+        let u_uint32 st =
+            uint32 (u_int32 st)
 
         let u_int64 st =
             let b1 = int64 (u_int32 st) &&& 0xFFFFFFFFL
             let b2 = int64 (u_int32 st)
             b1 ||| (b2 <<< 32)
 
-        let u_uint64 st = uint64 (u_int64 st)
+        let u_uint64 st =
+            uint64 (u_int64 st)
 
-        let u_double st = System.BitConverter.ToDouble(System.BitConverter.GetBytes(u_int64 st), 0)
+        let u_double st =
+            System.BitConverter.ToDouble(System.BitConverter.GetBytes(u_int64 st), 0)
 
-        let u_float32 st = System.BitConverter.ToSingle(System.BitConverter.GetBytes(u_int32 st), 0)
+        let u_float32 st =
+            System.BitConverter.ToSingle(System.BitConverter.GetBytes(u_int32 st), 0)
 
-        let u_char st = char (int32 (u_uint16 st))
+        let u_char st =
+            char (int32 (u_uint16 st))
 
-        let inline u_tup2 p1 p2 st = let a = p1 st in let b = p2 st in (a, b)
+        let inline u_tup2 p1 p2 st =
+            let a = p1 st in
+            let b = p2 st in
+            (a, b)
 
         let inline u_tup3 p1 p2 p3 st =
-            let a = p1 st in let b = p2 st in let c = p3 st in (a, b, c)
+            let a = p1 st in
+            let b = p2 st in
+            let c = p3 st in
+            (a, b, c)
 
         let inline u_tup4 p1 p2 p3 p4 st =
-            let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in (a, b, c, d)
+            let a = p1 st in
+            let b = p2 st in
+            let c = p3 st in
+            let d = p4 st in
+            (a, b, c, d)
 
         let inline u_tup5 p1 p2 p3 p4 p5 st =
-            let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in let e = p5 st in (a, b, c, d, e)
+            let a = p1 st in
+            let b = p2 st in
+            let c = p3 st in
+            let d = p4 st in
+            let e = p5 st in
+            (a, b, c, d, e)
 
         let u_uniq (tbl: _ array) st =
             let n = u_int st
-            if n < 0 || n >= tbl.Length then failwith ("u_uniq: out of range, n = "+string n+ ", sizeof tab = " + string tbl.Length)
+
+            if n < 0 || n >= tbl.Length then
+                failwith ("u_uniq: out of range, n = " + string n + ", sizeof tab = " + string tbl.Length)
+
             tbl.[n]
 
-        let u_string st = u_uniq st.istrings st
+        let u_string st =
+            u_uniq st.istrings st
 
         let rec u_list_aux f acc st =
             let tag = u_byte_as_int st
+
             match tag with
             | 0 -> List.rev acc
             | 1 -> let a = f st in u_list_aux f (a :: acc) st
             | n -> failwith ("u_list: found number " + string n)
 
-        let u_list f st = u_list_aux f [] st
+        let u_list f st =
+            u_list_aux f [] st
 
         let unpickleObj localAssembly referencedTypeDefs u phase2bytes =
             let phase2data =
                 let st2 =
-                   { is = new ByteStream(phase2bytes, 0, phase2bytes.Length)
-                     istrings = [| |]
-                     localAssembly=localAssembly
-                     referencedTypeDefs=referencedTypeDefs  }
+                    {
+                        is = new ByteStream(phase2bytes, 0, phase2bytes.Length)
+                        istrings = [||]
+                        localAssembly = localAssembly
+                        referencedTypeDefs = referencedTypeDefs
+                    }
+
                 u_tup2 (u_list prim_u_string) u_bytes st2
+
             let stringTab, phase1bytes = phase2data
+
             let st1 =
-               { is = new ByteStream(phase1bytes, 0, phase1bytes.Length)
-                 istrings = Array.ofList stringTab
-                 localAssembly=localAssembly
-                 referencedTypeDefs=referencedTypeDefs  }
+                {
+                    is = new ByteStream(phase1bytes, 0, phase1bytes.Length)
+                    istrings = Array.ofList stringTab
+                    localAssembly = localAssembly
+                    referencedTypeDefs = referencedTypeDefs
+                }
+
             let res = u st1
             res
 
@@ -1395,36 +1875,44 @@ module Patterns =
 
     let decodeFunTy args =
         match args with
-        | [d;r] -> funTyC.MakeGenericType([| d; r |])
+        | [ d; r ] -> funTyC.MakeGenericType([| d; r |])
         | _ -> invalidArg "args" (SR.GetString(SR.QexpectedTwoTypes))
 
     let decodeArrayTy n (tys: Type list) =
         match tys with
-        | [ty] -> if (n = 1) then ty.MakeArrayType() else ty.MakeArrayType n
-                  // typeof.MakeArrayType 1 returns "Int[*]" but we need "Int[]"
+        | [ ty ] ->
+            if (n = 1) then
+                ty.MakeArrayType()
+            else
+                ty.MakeArrayType n
+        // typeof.MakeArrayType 1 returns "Int[*]" but we need "Int[]"
         | _ -> invalidArg "tys" (SR.GetString(SR.QexpectedOneType))
 
-    let mkNamedTycon (tcName, assembly:Assembly) =
+    let mkNamedTycon (tcName, assembly: Assembly) =
         match assembly.GetType tcName with
-        | null  ->
+        | null ->
             // For some reason we can get 'null' returned here even when a type with the right name exists... Hence search the slow way...
             match (assembly.GetTypes() |> Array.tryFind (fun a -> a.FullName = tcName)) with
             | Some ty -> ty
             | None -> invalidArg "tcName" (String.Format(SR.GetString(SR.QfailedToBindTypeInAssembly), tcName, assembly.FullName))
         | ty -> ty
 
-    let decodeNamedTy genericType tsR = mkNamedType (genericType, tsR)
+    let decodeNamedTy genericType tsR =
+        mkNamedType (genericType, tsR)
 
     let mscorlib = typeof.Assembly
 
-    let u_assemblyRef st = u_string st
+    let u_assemblyRef st =
+        u_string st
 
     let decodeAssemblyRef st a =
-        if a = "" then mscorlib
-        elif a = "." then st.localAssembly
+        if a = "" then
+            mscorlib
+        elif a = "." then
+            st.localAssembly
         else
             match System.Reflection.Assembly.Load a with
-            | null -> invalidOp(String.Format(SR.GetString(SR.QfailedToBindAssembly), a.ToString()))
+            | null -> invalidOp (String.Format(SR.GetString(SR.QfailedToBindAssembly), a.ToString()))
             | assembly -> assembly
 
     let u_NamedType st =
@@ -1437,12 +1925,15 @@ module Patterns =
         else
             // escape commas found in type name, which are not already escaped
             // '\' is not valid in a type name except as an escape character, so logic can be pretty simple
-            let escapedTcName = System.Text.RegularExpressions.Regex.Replace(a, @"(? u_void st |> (fun () -> decodeFunTy)
         | 2 -> u_NamedType st |> decodeNamedTy
@@ -1454,62 +1945,84 @@ module Patterns =
 
     let rec u_dtype st : (int -> Type) -> Type =
         let tag = u_byte_as_int st
+
         match tag with
-        | 0 -> u_int st |> (fun x env     -> env x)
+        | 0 -> u_int st |> (fun x env -> env x)
         | 1 -> u_tup2 u_tyconstSpec (u_list u_dtype) st |> (fun (a, b) env -> a (appL b env))
         | _ -> failwith "u_dtype"
 
-    let u_dtypes st = let a = u_list u_dtype st in appL a
+    let u_dtypes st =
+        let a = u_list u_dtype st in appL a
 
-    let (|NoTyArgs|) input = match input with [] -> () | _ -> failwith "incorrect number of arguments during deserialization"
+    let (|NoTyArgs|) input =
+        match input with
+        | [] -> ()
+        | _ -> failwith "incorrect number of arguments during deserialization"
 
-    let (|OneTyArg|) input = match input with [x] -> x | _ -> failwith "incorrect number of arguments during deserialization"
+    let (|OneTyArg|) input =
+        match input with
+        | [ x ] -> x
+        | _ -> failwith "incorrect number of arguments during deserialization"
 
     []
     type BindingEnv =
-        { /// Mapping from variable index to Var object for the variable
-          vars : Map
-          /// The number of indexes in the mapping
-          varn: int
-          /// The active type instantiation for generic type parameters
-          typeInst: int -> Type }
+        {
+            /// Mapping from variable index to Var object for the variable
+            vars: Map
+            /// The number of indexes in the mapping
+            varn: int
+            /// The active type instantiation for generic type parameters
+            typeInst: int -> Type
+        }
 
     let addVar env v =
-        { env with vars = env.vars.Add(env.varn, v); varn=env.varn+1 }
+        { env with
+            vars = env.vars.Add(env.varn, v)
+            varn = env.varn + 1
+        }
 
     let mkTyparSubst (tyargs: Type[]) =
         let n = tyargs.Length
+
         fun idx ->
-          if idx < n then tyargs.[idx]
-          else invalidOp (SR.GetString(SR.QtypeArgumentOutOfRange))
+            if idx < n then
+                tyargs.[idx]
+            else
+                invalidOp (SR.GetString(SR.QtypeArgumentOutOfRange))
 
     let envClosed (spliceTypes: Type[]) =
-        { vars = Map.empty
-          varn = 0
-          typeInst = mkTyparSubst spliceTypes }
+        {
+            vars = Map.empty
+            varn = 0
+            typeInst = mkTyparSubst spliceTypes
+        }
 
     type Bindable<'T> = BindingEnv -> 'T
 
     let rec u_Expr st =
         let tag = u_byte_as_int st
+
         match tag with
         | 0 ->
             let a = u_constSpec st
             let b = u_dtypes st
             let args = u_list u_Expr st
+
             (fun (env: BindingEnv) ->
                 let args = List.map (fun e -> e env) args
+
                 let a =
                     match a with
                     | Unique v -> v
                     | Ambiguous f ->
                         let argTys = List.map typeOf args
                         f argTys
+
                 let tyargs = b env.typeInst
-                E (CombTerm (a tyargs (ValueSome args.Length), args)))
+                E(CombTerm(a tyargs (ValueSome args.Length), args)))
         | 1 ->
             let x = u_VarRef st
-            (fun env -> E(VarTerm (x env)))
+            (fun env -> E(VarTerm(x env)))
         | 2 ->
             let a = u_VarDecl st
             let b = u_Expr st
@@ -1520,17 +2033,17 @@ module Patterns =
             (fun env -> E(HoleTerm(a env.typeInst, idx)))
         | 4 ->
             let a = u_Expr st
-            (fun env -> mkQuote(a env, true))
+            (fun env -> mkQuote (a env, true))
         | 5 ->
             let a = u_Expr st
             let attrs = u_list u_Expr st
             (fun env -> let e = (a env) in EA(e.Tree, (e.CustomAttributes @ List.map (fun attrf -> attrf env) attrs)))
         | 6 ->
             let a = u_dtype st
-            (fun env -> mkVar(Var.Global("this", a env.typeInst)))
+            (fun env -> mkVar (Var.Global("this", a env.typeInst)))
         | 7 ->
             let a = u_Expr st
-            (fun env -> mkQuote(a env, false))
+            (fun env -> mkQuote (a env, false))
         | _ -> failwith "u_Expr"
 
     and u_VarDecl st =
@@ -1543,49 +2056,53 @@ module Patterns =
 
     and u_RecdField st =
         let ty, nm = u_tup2 u_NamedType u_string st
-        (fun tyargs -> getRecordProperty(mkNamedType (ty, tyargs), nm))
+        (fun tyargs -> getRecordProperty (mkNamedType (ty, tyargs), nm))
 
     and u_UnionCaseInfo st =
         let ty, nm = u_tup2 u_NamedType u_string st
-        (fun tyargs -> getUnionCaseInfo(mkNamedType (ty, tyargs), nm))
+        (fun tyargs -> getUnionCaseInfo (mkNamedType (ty, tyargs), nm))
 
     and u_UnionCaseField st =
         let case, i = u_tup2 u_UnionCaseInfo u_int st
-        (fun tyargs -> getUnionCaseInfoField(case tyargs, i))
+        (fun tyargs -> getUnionCaseInfoField (case tyargs, i))
 
     and u_ModuleDefn witnessInfo st =
         let (ty, nm, isProp) = u_tup3 u_NamedType u_string u_bool st
-        if isProp then Unique(StaticPropGetOp(bindModuleProperty(ty, nm)))
+
+        if isProp then
+            Unique(StaticPropGetOp(bindModuleProperty (ty, nm)))
         else
-        let meths = ty.GetMethods staticBindingFlags |> Array.filter (fun mi -> mi.Name = nm)
-        match meths with
-        | [||] ->
-            invalidOp (String.Format(SR.GetString(SR.QcannotBindFunction), nm, ty.ToString()))
-        | [| minfo |] ->
-            match witnessInfo with
-            | None ->
-                Unique(StaticMethodCallOp(minfo))
-            | Some (nmW, nWitnesses) ->
-                let methsW = ty.GetMethods(staticBindingFlags) |> Array.filter (fun mi -> mi.Name = nmW)
-                match methsW with
-                | [||] ->
-                    invalidOp (String.Format(SR.GetString(SR.QcannotBindFunction), nmW, ty.ToString()))
-                | [| minfoW |] ->
-                    Unique(StaticMethodCallWOp(minfo, minfoW, nWitnesses))
-                | _ ->
-                    Ambiguous(fun argTypes tyargs ->
-                        let minfoW = bindModuleFunctionWithCallSiteArgs(ty, nm, argTypes, tyargs)
-                        StaticMethodCallWOp(minfo, minfoW, nWitnesses))
-        | _ ->
-            Ambiguous(fun argTypes tyargs ->
+            let meths =
+                ty.GetMethods staticBindingFlags |> Array.filter (fun mi -> mi.Name = nm)
+
+            match meths with
+            | [||] -> invalidOp (String.Format(SR.GetString(SR.QcannotBindFunction), nm, ty.ToString()))
+            | [| minfo |] ->
                 match witnessInfo with
-                | None ->
-                    let minfo = bindModuleFunctionWithCallSiteArgs(ty, nm, argTypes, tyargs)
-                    StaticMethodCallOp minfo
+                | None -> Unique(StaticMethodCallOp(minfo))
                 | Some (nmW, nWitnesses) ->
-                    let minfo = bindModuleFunctionWithCallSiteArgs(ty, nm, List.skip nWitnesses argTypes, tyargs)
-                    let minfoW = bindModuleFunctionWithCallSiteArgs(ty, nmW, argTypes, tyargs)
-                    StaticMethodCallWOp(minfo, minfoW, nWitnesses))
+                    let methsW =
+                        ty.GetMethods(staticBindingFlags) |> Array.filter (fun mi -> mi.Name = nmW)
+
+                    match methsW with
+                    | [||] -> invalidOp (String.Format(SR.GetString(SR.QcannotBindFunction), nmW, ty.ToString()))
+                    | [| minfoW |] -> Unique(StaticMethodCallWOp(minfo, minfoW, nWitnesses))
+                    | _ ->
+                        Ambiguous(fun argTypes tyargs ->
+                            let minfoW = bindModuleFunctionWithCallSiteArgs (ty, nm, argTypes, tyargs)
+                            StaticMethodCallWOp(minfo, minfoW, nWitnesses))
+            | _ ->
+                Ambiguous(fun argTypes tyargs ->
+                    match witnessInfo with
+                    | None ->
+                        let minfo = bindModuleFunctionWithCallSiteArgs (ty, nm, argTypes, tyargs)
+                        StaticMethodCallOp minfo
+                    | Some (nmW, nWitnesses) ->
+                        let minfo =
+                            bindModuleFunctionWithCallSiteArgs (ty, nm, List.skip nWitnesses argTypes, tyargs)
+
+                        let minfoW = bindModuleFunctionWithCallSiteArgs (ty, nmW, argTypes, tyargs)
+                        StaticMethodCallWOp(minfo, minfoW, nWitnesses))
 
     and u_MethodInfoData st =
         u_tup5 u_NamedType (u_list u_dtype) u_dtype u_string u_int st
@@ -1598,15 +2115,17 @@ module Patterns =
 
     and u_MethodBase st =
         let tag = u_byte_as_int st
+
         match tag with
         | 0 ->
             match u_ModuleDefn None st with
-            | Unique(StaticMethodCallOp minfo) -> (minfo :> MethodBase)
-            | Unique(StaticPropGetOp pinfo) -> (pinfo.GetGetMethod true :> MethodBase)
-            | Ambiguous(_) -> raise (System.Reflection.AmbiguousMatchException())
+            | Unique (StaticMethodCallOp minfo) -> (minfo :> MethodBase)
+            | Unique (StaticPropGetOp pinfo) -> (pinfo.GetGetMethod true :> MethodBase)
+            | Ambiguous (_) -> raise (System.Reflection.AmbiguousMatchException())
             | _ -> failwith "unreachable"
         | 1 ->
             let ((genericType, _, _, methName, _) as data) = u_MethodInfoData st
+
             if methName = ".cctor" then
                 let cinfo = bindGenericCctor genericType
                 (cinfo :> MethodBase)
@@ -1620,98 +2139,151 @@ module Patterns =
         | 3 ->
             let methNameW = u_string st
             let nWitnesses = u_int st
-            match u_ModuleDefn (Some (methNameW, nWitnesses)) st with
-            | Unique(StaticMethodCallOp(minfo)) -> (minfo :> MethodBase)
-            | Unique(StaticMethodCallWOp(_minfo, minfoW, _)) -> (minfoW :> MethodBase)
-            | Unique(StaticPropGetOp(pinfo)) -> (pinfo.GetGetMethod(true) :> MethodBase)
-            | Ambiguous(_) -> raise (System.Reflection.AmbiguousMatchException())
+
+            match u_ModuleDefn (Some(methNameW, nWitnesses)) st with
+            | Unique (StaticMethodCallOp (minfo)) -> (minfo :> MethodBase)
+            | Unique (StaticMethodCallWOp (_minfo, minfoW, _)) -> (minfoW :> MethodBase)
+            | Unique (StaticPropGetOp (pinfo)) -> (pinfo.GetGetMethod(true) :> MethodBase)
+            | Ambiguous (_) -> raise (System.Reflection.AmbiguousMatchException())
             | _ -> failwith "unreachable"
         | _ -> failwith "u_MethodBase"
 
-
     and instModuleDefnOp r tyargs _ =
         match r with
-        | StaticMethodCallOp(minfo) -> StaticMethodCallOp(instMeth(minfo, tyargs))
-        | StaticMethodCallWOp(minfo, minfoW, n) -> StaticMethodCallWOp(instMeth(minfo, tyargs), instMeth(minfoW, tyargs), n)
+        | StaticMethodCallOp (minfo) -> StaticMethodCallOp(instMeth (minfo, tyargs))
+        | StaticMethodCallWOp (minfo, minfoW, n) -> StaticMethodCallWOp(instMeth (minfo, tyargs), instMeth (minfoW, tyargs), n)
         // OK to throw away the tyargs here since this only non-generic values in modules get represented by static properties
         | x -> x
 
     and u_constSpec st =
         let tag = u_byte_as_int st
+
         if tag = 1 then
             match u_ModuleDefn None st with
-            | Unique r -> Unique (instModuleDefnOp r)
-            | Ambiguous f -> Ambiguous (fun argTypes tyargs -> instModuleDefnOp (f argTypes tyargs) tyargs)
+            | Unique r -> Unique(instModuleDefnOp r)
+            | Ambiguous f -> Ambiguous(fun argTypes tyargs -> instModuleDefnOp (f argTypes tyargs) tyargs)
         elif tag = 51 then
             let nmW = u_string st
             let nWitnesses = u_int st
-            match u_ModuleDefn (Some (nmW, nWitnesses)) st with
+
+            match u_ModuleDefn (Some(nmW, nWitnesses)) st with
             | Unique r -> Unique(instModuleDefnOp r)
             | Ambiguous f -> Ambiguous(fun argTypes tyargs -> instModuleDefnOp (f argTypes tyargs) tyargs)
         else
-        let constSpec =
-            match tag with
-            | 0 -> u_void st |> (fun () NoTyArgs _ -> IfThenElseOp)
-            // 1 taken above
-            | 2 -> u_void st |> (fun () NoTyArgs _ -> LetRecOp)
-            | 3 -> u_NamedType st |> (fun x tyargs _ -> NewRecordOp (mkNamedType (x, tyargs)))
-            | 4 -> u_RecdField st |> (fun prop tyargs _ -> InstancePropGetOp(prop tyargs))
-            | 5 -> u_UnionCaseInfo st |> (fun unionCase tyargs _ -> NewUnionCaseOp(unionCase tyargs))
-            | 6 -> u_UnionCaseField st |> (fun prop tyargs _ -> InstancePropGetOp(prop tyargs) )
-            | 7 -> u_UnionCaseInfo st |> (fun unionCase tyargs _ -> UnionCaseTestOp(unionCase tyargs))
-            | 8 -> u_void st |> (fun () (OneTyArg tyarg) _ -> NewTupleOp tyarg)
-            | 9 -> u_int st |> (fun x (OneTyArg tyarg) _ -> TupleGetOp (tyarg, x))
-            // Note, these get type args because they may be the result of reading literal field constants
-            | 11 -> u_bool st |> (fun x (OneTyArg tyarg) _ -> mkLiftedValueOpG (x, tyarg))
-            | 12 -> u_string st |> (fun x (OneTyArg tyarg) _ -> mkLiftedValueOpG (x, tyarg))
-            | 13 -> u_float32 st |> (fun x (OneTyArg tyarg) _ -> mkLiftedValueOpG (x, tyarg))
-            | 14 -> u_double st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 15 -> u_char st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 16 -> u_sbyte st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 17 -> u_byte st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 18 -> u_int16 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 19 -> u_uint16 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 20 -> u_int32 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 21 -> u_uint32 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 22 -> u_int64 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 23 -> u_uint64 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
-            | 24 -> u_void st |> (fun () NoTyArgs _ -> mkLiftedValueOpG ((), typeof))
-            | 25 -> u_PropInfoData st |> (fun (a, b, c, d) tyargs _ -> let pinfo = bindProp(a, b, c, d, tyargs) in if pinfoIsStatic pinfo then StaticPropGetOp pinfo else InstancePropGetOp pinfo)
-            | 26 -> u_CtorInfoData st |> (fun (a, b) tyargs _ -> NewObjectOp (bindCtor(a, b, tyargs)))
-            | 28 -> u_void st |> (fun () (OneTyArg ty) _ -> CoerceOp ty)
-            | 29 -> u_void st |> (fun () NoTyArgs _ -> SequentialOp)
-            | 30 -> u_void st |> (fun () NoTyArgs _ -> ForIntegerRangeLoopOp)
-            | 31 -> u_MethodInfoData st |> (fun p tyargs knownArgCount -> let minfo = bindMeth(knownArgCount, p, tyargs) in if minfo.IsStatic then StaticMethodCallOp minfo else InstanceMethodCallOp minfo)
-            | 32 -> u_void st |> (fun () (OneTyArg ty) _ -> NewArrayOp ty)
-            | 33 -> u_void st |> (fun () (OneTyArg ty) _ -> NewDelegateOp ty)
-            | 34 -> u_void st |> (fun () NoTyArgs _ -> WhileLoopOp)
-            | 35 -> u_void st |> (fun () NoTyArgs _ -> LetOp)
-            | 36 -> u_RecdField st |> (fun prop tyargs _ -> InstancePropSetOp(prop tyargs))
-            | 37 -> u_tup2 u_NamedType u_string st |> (fun (a, b) tyargs _ -> let finfo = bindField(a, b, tyargs) in if finfo.IsStatic then StaticFieldGetOp finfo else InstanceFieldGetOp finfo)
-            | 38 -> u_void st |> (fun () NoTyArgs _ -> LetRecCombOp)
-            | 39 -> u_void st |> (fun () NoTyArgs _ -> AppOp)
-            | 40 -> u_void st |> (fun () (OneTyArg ty) _ -> ValueOp(null, ty, None))
-            | 41 -> u_void st |> (fun () (OneTyArg ty) _ -> DefaultValueOp ty)
-            | 42 -> u_PropInfoData st |> (fun (a, b, c, d) tyargs _ -> let pinfo = bindProp(a, b, c, d, tyargs) in if pinfoIsStatic pinfo then StaticPropSetOp pinfo else InstancePropSetOp pinfo)
-            | 43 -> u_tup2 u_NamedType u_string st |> (fun (a, b) tyargs _ -> let finfo = bindField(a, b, tyargs) in if finfo.IsStatic then StaticFieldSetOp finfo else InstanceFieldSetOp finfo)
-            | 44 -> u_void st |> (fun () NoTyArgs _ -> AddressOfOp)
-            | 45 -> u_void st |> (fun () NoTyArgs _ -> AddressSetOp)
-            | 46 -> u_void st |> (fun () (OneTyArg ty) _ -> TypeTestOp ty)
-            | 47 -> u_void st |> (fun () NoTyArgs _ -> TryFinallyOp)
-            | 48 -> u_void st |> (fun () NoTyArgs _ -> TryWithOp)
-            | 49 -> u_void st |> (fun () NoTyArgs _ -> VarSetOp)
-            | 50 ->
-                let m1 = u_MethodInfoData st
-                let m2 = u_MethodInfoData st
-                let n = u_int st
-                (fun tyargs _ ->
-                    let minfo = bindMeth (ValueNone, m1, tyargs)
-                    let minfoW = bindMeth (ValueNone, m2, tyargs)
-                    if minfo.IsStatic then StaticMethodCallWOp(minfo, minfoW, n)
-                    else InstanceMethodCallWOp(minfo, minfoW, n))
-            // 51 taken above
-            | _ -> failwith ("u_constSpec, unrecognized tag " + string tag)
-        Unique constSpec
+            let constSpec =
+                match tag with
+                | 0 -> u_void st |> (fun () NoTyArgs _ -> IfThenElseOp)
+                // 1 taken above
+                | 2 -> u_void st |> (fun () NoTyArgs _ -> LetRecOp)
+                | 3 -> u_NamedType st |> (fun x tyargs _ -> NewRecordOp(mkNamedType (x, tyargs)))
+                | 4 -> u_RecdField st |> (fun prop tyargs _ -> InstancePropGetOp(prop tyargs))
+                | 5 ->
+                    u_UnionCaseInfo st
+                    |> (fun unionCase tyargs _ -> NewUnionCaseOp(unionCase tyargs))
+                | 6 -> u_UnionCaseField st |> (fun prop tyargs _ -> InstancePropGetOp(prop tyargs))
+                | 7 ->
+                    u_UnionCaseInfo st
+                    |> (fun unionCase tyargs _ -> UnionCaseTestOp(unionCase tyargs))
+                | 8 -> u_void st |> (fun () (OneTyArg tyarg) _ -> NewTupleOp tyarg)
+                | 9 -> u_int st |> (fun x (OneTyArg tyarg) _ -> TupleGetOp(tyarg, x))
+                // Note, these get type args because they may be the result of reading literal field constants
+                | 11 -> u_bool st |> (fun x (OneTyArg tyarg) _ -> mkLiftedValueOpG (x, tyarg))
+                | 12 -> u_string st |> (fun x (OneTyArg tyarg) _ -> mkLiftedValueOpG (x, tyarg))
+                | 13 -> u_float32 st |> (fun x (OneTyArg tyarg) _ -> mkLiftedValueOpG (x, tyarg))
+                | 14 -> u_double st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 15 -> u_char st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 16 -> u_sbyte st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 17 -> u_byte st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 18 -> u_int16 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 19 -> u_uint16 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 20 -> u_int32 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 21 -> u_uint32 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 22 -> u_int64 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 23 -> u_uint64 st |> (fun a (OneTyArg tyarg) _ -> mkLiftedValueOpG (a, tyarg))
+                | 24 -> u_void st |> (fun () NoTyArgs _ -> mkLiftedValueOpG ((), typeof))
+                | 25 ->
+                    u_PropInfoData st
+                    |> (fun (a, b, c, d) tyargs _ ->
+                        let pinfo = bindProp (a, b, c, d, tyargs) in
+
+                        if pinfoIsStatic pinfo then
+                            StaticPropGetOp pinfo
+                        else
+                            InstancePropGetOp pinfo)
+                | 26 ->
+                    u_CtorInfoData st
+                    |> (fun (a, b) tyargs _ -> NewObjectOp(bindCtor (a, b, tyargs)))
+                | 28 -> u_void st |> (fun () (OneTyArg ty) _ -> CoerceOp ty)
+                | 29 -> u_void st |> (fun () NoTyArgs _ -> SequentialOp)
+                | 30 -> u_void st |> (fun () NoTyArgs _ -> ForIntegerRangeLoopOp)
+                | 31 ->
+                    u_MethodInfoData st
+                    |> (fun p tyargs knownArgCount ->
+                        let minfo = bindMeth (knownArgCount, p, tyargs) in
+
+                        if minfo.IsStatic then
+                            StaticMethodCallOp minfo
+                        else
+                            InstanceMethodCallOp minfo)
+                | 32 -> u_void st |> (fun () (OneTyArg ty) _ -> NewArrayOp ty)
+                | 33 -> u_void st |> (fun () (OneTyArg ty) _ -> NewDelegateOp ty)
+                | 34 -> u_void st |> (fun () NoTyArgs _ -> WhileLoopOp)
+                | 35 -> u_void st |> (fun () NoTyArgs _ -> LetOp)
+                | 36 -> u_RecdField st |> (fun prop tyargs _ -> InstancePropSetOp(prop tyargs))
+                | 37 ->
+                    u_tup2 u_NamedType u_string st
+                    |> (fun (a, b) tyargs _ ->
+                        let finfo = bindField (a, b, tyargs) in
+
+                        if finfo.IsStatic then
+                            StaticFieldGetOp finfo
+                        else
+                            InstanceFieldGetOp finfo)
+                | 38 -> u_void st |> (fun () NoTyArgs _ -> LetRecCombOp)
+                | 39 -> u_void st |> (fun () NoTyArgs _ -> AppOp)
+                | 40 -> u_void st |> (fun () (OneTyArg ty) _ -> ValueOp(null, ty, None))
+                | 41 -> u_void st |> (fun () (OneTyArg ty) _ -> DefaultValueOp ty)
+                | 42 ->
+                    u_PropInfoData st
+                    |> (fun (a, b, c, d) tyargs _ ->
+                        let pinfo = bindProp (a, b, c, d, tyargs) in
+
+                        if pinfoIsStatic pinfo then
+                            StaticPropSetOp pinfo
+                        else
+                            InstancePropSetOp pinfo)
+                | 43 ->
+                    u_tup2 u_NamedType u_string st
+                    |> (fun (a, b) tyargs _ ->
+                        let finfo = bindField (a, b, tyargs) in
+
+                        if finfo.IsStatic then
+                            StaticFieldSetOp finfo
+                        else
+                            InstanceFieldSetOp finfo)
+                | 44 -> u_void st |> (fun () NoTyArgs _ -> AddressOfOp)
+                | 45 -> u_void st |> (fun () NoTyArgs _ -> AddressSetOp)
+                | 46 -> u_void st |> (fun () (OneTyArg ty) _ -> TypeTestOp ty)
+                | 47 -> u_void st |> (fun () NoTyArgs _ -> TryFinallyOp)
+                | 48 -> u_void st |> (fun () NoTyArgs _ -> TryWithOp)
+                | 49 -> u_void st |> (fun () NoTyArgs _ -> VarSetOp)
+                | 50 ->
+                    let m1 = u_MethodInfoData st
+                    let m2 = u_MethodInfoData st
+                    let n = u_int st
+
+                    (fun tyargs _ ->
+                        let minfo = bindMeth (ValueNone, m1, tyargs)
+                        let minfoW = bindMeth (ValueNone, m2, tyargs)
+
+                        if minfo.IsStatic then
+                            StaticMethodCallWOp(minfo, minfoW, n)
+                        else
+                            InstanceMethodCallWOp(minfo, minfoW, n))
+                // 51 taken above
+                | _ -> failwith ("u_constSpec, unrecognized tag " + string tag)
+
+            Unique constSpec
 
     let u_ReflectedDefinition = u_tup2 u_MethodBase u_Expr
 
@@ -1732,29 +2304,45 @@ module Patterns =
     let rec fillHolesInRawExpr (l: Expr[]) (E t as e) =
         match t with
         | VarTerm _ -> e
-        | LambdaTerm (v, b) -> EA(LambdaTerm(v, fillHolesInRawExpr l b ), e.CustomAttributes)
-        | CombTerm   (op, args) -> EA(CombTerm(op, args |> List.map (fillHolesInRawExpr l)), e.CustomAttributes)
-        | HoleTerm   (ty, idx) ->
-           if idx < 0 || idx >= l.Length then failwith "hole index out of range"
-           let h = l.[idx]
-           match typeOf h with
-           | expected when expected <> ty -> invalidArg "receivedType" (String.Format(SR.GetString(SR.QtmmRaw), expected, ty))
-           | _ -> h
+        | LambdaTerm (v, b) -> EA(LambdaTerm(v, fillHolesInRawExpr l b), e.CustomAttributes)
+        | CombTerm (op, args) -> EA(CombTerm(op, args |> List.map (fillHolesInRawExpr l)), e.CustomAttributes)
+        | HoleTerm (ty, idx) ->
+            if idx < 0 || idx >= l.Length then
+                failwith "hole index out of range"
+
+            let h = l.[idx]
+
+            match typeOf h with
+            | expected when expected <> ty -> invalidArg "receivedType" (String.Format(SR.GetString(SR.QtmmRaw), expected, ty))
+            | _ -> h
 
     let rec freeInExprAcc bvs acc (E t) =
         match t with
-        | HoleTerm   _ -> acc
+        | HoleTerm _ -> acc
         | CombTerm (_, ag) -> ag |> List.fold (freeInExprAcc bvs) acc
-        | VarTerm    v -> if Set.contains v bvs || Set.contains v acc then acc else Set.add v acc
+        | VarTerm v ->
+            if Set.contains v bvs || Set.contains v acc then
+                acc
+            else
+                Set.add v acc
         | LambdaTerm (v, b) -> freeInExprAcc (Set.add v bvs) acc b
-    and freeInExpr e = freeInExprAcc Set.empty Set.empty e
+
+    and freeInExpr e =
+        freeInExprAcc Set.empty Set.empty e
 
     // utility for folding
     let foldWhile f st (ie: seq<'T>) =
         use e = ie.GetEnumerator()
         let mutable res = Some st
+
         while (res.IsSome && e.MoveNext()) do
-            res <- f (match res with Some a -> a | _ -> failwith "internal error") e.Current
+            res <-
+                f
+                    (match res with
+                     | Some a -> a
+                     | _ -> failwith "internal error")
+                    e.Current
+
         res
 
     []
@@ -1767,304 +2355,362 @@ module Patterns =
         | CombTerm (c, args) ->
             let substargs = args |> List.map (fun arg -> substituteInExpr bvs tmsubst arg)
             EA(CombTerm(c, substargs), e.CustomAttributes)
-        | VarTerm    v ->
+        | VarTerm v ->
             match tmsubst v with
             | None -> e
             | Some e2 ->
                 let fvs = freeInExpr e2
                 let clashes = Set.intersect fvs bvs in
-                if clashes.IsEmpty then e2
-                else raise (Clash(clashes.MinimumElement))
+
+                if clashes.IsEmpty then
+                    e2
+                else
+                    raise (Clash(clashes.MinimumElement))
         | LambdaTerm (v, b) ->
-             try EA(LambdaTerm(v, substituteInExpr (Set.add v bvs) tmsubst b), e.CustomAttributes)
-             with Clash bv ->
-                 if v = bv then
-                     let v2 = new Var(v.Name, v.Type)
-                     let v2exp = E(VarTerm v2)
-                     EA(LambdaTerm(v2, substituteInExpr bvs (fun v -> if v = bv then Some v2exp else tmsubst v) b), e.CustomAttributes)
-                 else
-                     reraise()
+            try
+                EA(LambdaTerm(v, substituteInExpr (Set.add v bvs) tmsubst b), e.CustomAttributes)
+            with Clash bv ->
+                if v = bv then
+                    let v2 = new Var(v.Name, v.Type)
+                    let v2exp = E(VarTerm v2)
+                    EA(LambdaTerm(v2, substituteInExpr bvs (fun v -> if v = bv then Some v2exp else tmsubst v) b), e.CustomAttributes)
+                else
+                    reraise ()
         | HoleTerm _ -> e
 
+    let substituteRaw tmsubst e =
+        substituteInExpr Set.empty tmsubst e
 
-    let substituteRaw tmsubst e = substituteInExpr Set.empty tmsubst e
-
-    let readToEnd (s : Stream) =
+    let readToEnd (s: Stream) =
         let n = int s.Length
         let res = Array.zeroCreate n
         let mutable i = 0
+
         while (i < n) do
-             i <- i + s.Read(res, i, (n - i))
+            i <- i + s.Read(res, i, (n - i))
+
         res
 
-    let decodedTopResources = new Dictionary(10, HashIdentity.Structural)
+    let decodedTopResources =
+        new Dictionary(10, HashIdentity.Structural)
 
     []
     type ReflectedDefinitionTableKey =
         | Key of ModuleHandle * int
+
         static member GetKey(methodBase: MethodBase) =
             Key(methodBase.Module.ModuleHandle, methodBase.MetadataToken)
 
     []
     type ReflectedDefinitionTableEntry = Entry of Bindable
 
-    let reflectedDefinitionTable = new Dictionary(10, HashIdentity.Structural)
+    let reflectedDefinitionTable =
+        new Dictionary(10, HashIdentity.Structural)
 
     let registerReflectedDefinitions (assem, resourceName, bytes, referencedTypes) =
         let defns = unpickleReflectedDefns assem referencedTypes bytes
-        defns |> List.iter (fun (minfo, exprBuilder) ->
+
+        defns
+        |> List.iter (fun (minfo, exprBuilder) ->
             let key = ReflectedDefinitionTableKey.GetKey minfo
-            lock reflectedDefinitionTable (fun () ->
-                reflectedDefinitionTable.Add(key, Entry exprBuilder)))
+            lock reflectedDefinitionTable (fun () -> reflectedDefinitionTable.Add(key, Entry exprBuilder)))
+
         decodedTopResources.Add((assem, resourceName), 0)
 
     /// Get the reflected definition at the given (always generic) instantiation
-    let tryGetReflectedDefinition (methodBase: MethodBase, tyargs: Type []) =
+    let tryGetReflectedDefinition (methodBase: MethodBase, tyargs: Type[]) =
         checkNonNull "methodBase" methodBase
+
         let data =
-          let assem = methodBase.DeclaringType.Assembly
-          let key = ReflectedDefinitionTableKey.GetKey methodBase
-          let ok, res = lock reflectedDefinitionTable (fun () -> reflectedDefinitionTable.TryGetValue key)
-
-          if ok then Some res else
-
-            let qdataResources =
-                // dynamic assemblies don't support the GetManifestResourceNames
-                match assem with
-                | a when a.FullName = "System.Reflection.Emit.AssemblyBuilder" -> []
-                | null | _ ->
-                    let resources =
-                        // This raises NotSupportedException for dynamic assemblies
-                        try assem.GetManifestResourceNames()
-                        with :? NotSupportedException -> [| |]
-                    [ for resourceName in resources do
-                          if resourceName.StartsWith(ReflectedDefinitionsResourceNameBase, StringComparison.Ordinal) &&
-                             not (decodedTopResources.ContainsKey((assem, resourceName))) then
-
-                            let cmaAttribForResource =
-                                assem.GetCustomAttributes(typeof, false)
-                                |> (function null -> [| |] | x -> x)
-                                |> Array.tryPick (fun ca ->
-                                     match ca with
-                                     | :? CompilationMappingAttribute as cma when cma.ResourceName = resourceName -> Some cma
-                                     | _ -> None)
-                            let resourceBytes = readToEnd (assem.GetManifestResourceStream resourceName)
-                            let referencedTypes =
-                                match cmaAttribForResource with
-                                | None -> [| |]
-                                | Some cma -> cma.TypeDefinitions
-                            yield (resourceName, unpickleReflectedDefns assem referencedTypes resourceBytes) ]
-
-            // ok, add to the table
+            let assem = methodBase.DeclaringType.Assembly
+            let key = ReflectedDefinitionTableKey.GetKey methodBase
+
             let ok, res =
-                lock reflectedDefinitionTable (fun () ->
-                     // check another thread didn't get in first
-                     if not (reflectedDefinitionTable.ContainsKey key) then
-                         qdataResources
-                         |> List.iter (fun (resourceName, defns) ->
-                             defns |> List.iter (fun (methodBase, exprBuilder) ->
-                                reflectedDefinitionTable.[ReflectedDefinitionTableKey.GetKey methodBase] <- Entry exprBuilder)
-                             decodedTopResources.[(assem, resourceName)] <- 0)
-                     // we know it's in the table now, if it's ever going to be there
-                     reflectedDefinitionTable.TryGetValue key
-                )
-
-            if ok then Some res else None
+                lock reflectedDefinitionTable (fun () -> reflectedDefinitionTable.TryGetValue key)
+
+            if ok then
+                Some res
+            else
+
+                let qdataResources =
+                    // dynamic assemblies don't support the GetManifestResourceNames
+                    match assem with
+                    | a when a.FullName = "System.Reflection.Emit.AssemblyBuilder" -> []
+                    | null
+                    | _ ->
+                        let resources =
+                            // This raises NotSupportedException for dynamic assemblies
+                            try
+                                assem.GetManifestResourceNames()
+                            with :? NotSupportedException ->
+                                [||]
+
+                        [
+                            for resourceName in resources do
+                                if
+                                    resourceName.StartsWith(ReflectedDefinitionsResourceNameBase, StringComparison.Ordinal)
+                                    && not (decodedTopResources.ContainsKey((assem, resourceName)))
+                                then
+
+                                    let cmaAttribForResource =
+                                        assem.GetCustomAttributes(typeof, false)
+                                        |> (function
+                                        | null -> [||]
+                                        | x -> x)
+                                        |> Array.tryPick (fun ca ->
+                                            match ca with
+                                            | :? CompilationMappingAttribute as cma when cma.ResourceName = resourceName -> Some cma
+                                            | _ -> None)
+
+                                    let resourceBytes = readToEnd (assem.GetManifestResourceStream resourceName)
+
+                                    let referencedTypes =
+                                        match cmaAttribForResource with
+                                        | None -> [||]
+                                        | Some cma -> cma.TypeDefinitions
+
+                                    yield (resourceName, unpickleReflectedDefns assem referencedTypes resourceBytes)
+                        ]
+
+                // ok, add to the table
+                let ok, res =
+                    lock reflectedDefinitionTable (fun () ->
+                        // check another thread didn't get in first
+                        if not (reflectedDefinitionTable.ContainsKey key) then
+                            qdataResources
+                            |> List.iter (fun (resourceName, defns) ->
+                                defns
+                                |> List.iter (fun (methodBase, exprBuilder) ->
+                                    reflectedDefinitionTable.[ReflectedDefinitionTableKey.GetKey methodBase] <- Entry exprBuilder)
+
+                                decodedTopResources.[(assem, resourceName)] <- 0)
+                        // we know it's in the table now, if it's ever going to be there
+                        reflectedDefinitionTable.TryGetValue key)
+
+                if ok then Some res else None
 
         match data with
         | Some (Entry exprBuilder) ->
             let expectedNumTypars =
-                getNumGenericArguments(methodBase.DeclaringType) +
-                (match methodBase with
-                 | :? MethodInfo as minfo -> if minfo.IsGenericMethod then minfo.GetGenericArguments().Length else 0
-                 | _ -> 0)
+                getNumGenericArguments (methodBase.DeclaringType)
+                + (match methodBase with
+                   | :? MethodInfo as minfo ->
+                       if minfo.IsGenericMethod then
+                           minfo.GetGenericArguments().Length
+                       else
+                           0
+                   | _ -> 0)
+
             if (expectedNumTypars <> tyargs.Length) then
-                invalidArg "tyargs" (String.Format(SR.GetString(SR.QwrongNumOfTypeArgs), methodBase.Name, expectedNumTypars.ToString(), tyargs.Length.ToString()))
+                invalidArg
+                    "tyargs"
+                    (String.Format(
+                        SR.GetString(SR.QwrongNumOfTypeArgs),
+                        methodBase.Name,
+                        expectedNumTypars.ToString(),
+                        tyargs.Length.ToString()
+                    ))
+
             Some(exprBuilder (envClosed tyargs))
         | None -> None
 
     /// Get the reflected definition at the generic instantiation
     let tryGetReflectedDefinitionInstantiated (methodBase: MethodBase) =
         checkNonNull "methodBase" methodBase
+
         match methodBase with
         | :? MethodInfo as minfo ->
-               let tyargs =
-                   Array.append
-                       (getGenericArguments minfo.DeclaringType)
-                       (if minfo.IsGenericMethod then minfo.GetGenericArguments() else [| |])
-               tryGetReflectedDefinition (methodBase, tyargs)
+            let tyargs =
+                Array.append
+                    (getGenericArguments minfo.DeclaringType)
+                    (if minfo.IsGenericMethod then
+                         minfo.GetGenericArguments()
+                     else
+                         [||])
+
+            tryGetReflectedDefinition (methodBase, tyargs)
         | :? ConstructorInfo as cinfo ->
-               let tyargs = getGenericArguments cinfo.DeclaringType
-               tryGetReflectedDefinition (methodBase, tyargs)
-        | _ ->
-               tryGetReflectedDefinition (methodBase, [| |])
+            let tyargs = getGenericArguments cinfo.DeclaringType
+            tryGetReflectedDefinition (methodBase, tyargs)
+        | _ -> tryGetReflectedDefinition (methodBase, [||])
 
     let deserialize (localAssembly, referencedTypeDefs, spliceTypes, spliceExprs, bytes) : Expr =
-        let expr = unpickleExpr localAssembly referencedTypeDefs bytes (envClosed spliceTypes)
-        fillHolesInRawExpr spliceExprs expr
+        let expr =
+            unpickleExpr localAssembly referencedTypeDefs bytes (envClosed spliceTypes)
 
+        fillHolesInRawExpr spliceExprs expr
 
     let cast (expr: Expr) : Expr<'T> =
-        checkTypesSR  (typeof<'T>) (typeOf expr) "expr" (SR.GetString(SR.QtmmExprHasWrongType))
+        checkTypesSR (typeof<'T>) (typeOf expr) "expr" (SR.GetString(SR.QtmmExprHasWrongType))
         new Expr<'T>(expr.Tree, expr.CustomAttributes)
 
 open Patterns
 
-
 type Expr with
-    member x.Substitute substitution = substituteRaw substitution x
-    member x.GetFreeVars () = (freeInExpr x :> seq<_>)
+
+    member x.Substitute substitution =
+        substituteRaw substitution x
+
+    member x.GetFreeVars() =
+        (freeInExpr x :> seq<_>)
+
     member x.Type = typeOf x
 
-    static member AddressOf (target: Expr) =
+    static member AddressOf(target: Expr) =
         mkAddressOf target
 
-    static member AddressSet (target: Expr, value: Expr) =
+    static member AddressSet(target: Expr, value: Expr) =
         mkAddressSet (target, value)
 
-    static member Application (functionExpr: Expr, argument: Expr) =
+    static member Application(functionExpr: Expr, argument: Expr) =
         mkApplication (functionExpr, argument)
 
-    static member Applications (functionExpr: Expr, arguments) =
+    static member Applications(functionExpr: Expr, arguments) =
         mkApplications (functionExpr, arguments)
 
-    static member Call (methodInfo:MethodInfo, arguments) =
+    static member Call(methodInfo: MethodInfo, arguments) =
         checkNonNull "methodInfo" methodInfo
         mkStaticMethodCall (methodInfo, arguments)
 
-    static member Call (obj: Expr, methodInfo:MethodInfo, arguments) =
+    static member Call(obj: Expr, methodInfo: MethodInfo, arguments) =
         checkNonNull "methodInfo" methodInfo
         mkInstanceMethodCall (obj, methodInfo, arguments)
 
-    static member CallWithWitnesses (methodInfo: MethodInfo, methodInfoWithWitnesses: MethodInfo, witnesses, arguments) =
+    static member CallWithWitnesses(methodInfo: MethodInfo, methodInfoWithWitnesses: MethodInfo, witnesses, arguments) =
         checkNonNull "methodInfo" methodInfo
         checkNonNull "methodInfoWithWitnesses" methodInfoWithWitnesses
-        mkStaticMethodCallW (methodInfo, methodInfoWithWitnesses, List.length witnesses, witnesses@arguments)
+        mkStaticMethodCallW (methodInfo, methodInfoWithWitnesses, List.length witnesses, witnesses @ arguments)
 
-    static member CallWithWitnesses (obj: Expr, methodInfo: MethodInfo, methodInfoWithWitnesses: MethodInfo, witnesses, arguments) =
+    static member CallWithWitnesses(obj: Expr, methodInfo: MethodInfo, methodInfoWithWitnesses: MethodInfo, witnesses, arguments) =
         checkNonNull "methodInfo" methodInfo
         checkNonNull "methodInfoWithWitnesses" methodInfoWithWitnesses
-        mkInstanceMethodCallW (obj, methodInfo, methodInfoWithWitnesses, List.length witnesses, witnesses@arguments)
+        mkInstanceMethodCallW (obj, methodInfo, methodInfoWithWitnesses, List.length witnesses, witnesses @ arguments)
 
-    static member Coerce (source: Expr, target: Type) =
+    static member Coerce(source: Expr, target: Type) =
         checkNonNull "target" target
         mkCoerce (target, source)
 
-    static member IfThenElse (guard: Expr, thenExpr: Expr, elseExpr: Expr) =
+    static member IfThenElse(guard: Expr, thenExpr: Expr, elseExpr: Expr) =
         mkIfThenElse (guard, thenExpr, elseExpr)
 
-    static member ForIntegerRangeLoop (loopVariable, start: Expr, endExpr: Expr, body: Expr) =
-        mkForLoop(loopVariable, start, endExpr, body)
+    static member ForIntegerRangeLoop(loopVariable, start: Expr, endExpr: Expr, body: Expr) =
+        mkForLoop (loopVariable, start, endExpr, body)
 
-    static member FieldGet (fieldInfo:FieldInfo) =
+    static member FieldGet(fieldInfo: FieldInfo) =
         checkNonNull "fieldInfo" fieldInfo
         mkStaticFieldGet fieldInfo
 
-    static member FieldGet (obj: Expr, fieldInfo:FieldInfo) =
+    static member FieldGet(obj: Expr, fieldInfo: FieldInfo) =
         checkNonNull "fieldInfo" fieldInfo
         mkInstanceFieldGet (obj, fieldInfo)
 
-    static member FieldSet (fieldInfo:FieldInfo, value: Expr) =
+    static member FieldSet(fieldInfo: FieldInfo, value: Expr) =
         checkNonNull "fieldInfo" fieldInfo
         mkStaticFieldSet (fieldInfo, value)
 
-    static member FieldSet (obj: Expr, fieldInfo:FieldInfo, value: Expr) =
+    static member FieldSet(obj: Expr, fieldInfo: FieldInfo, value: Expr) =
         checkNonNull "fieldInfo" fieldInfo
         mkInstanceFieldSet (obj, fieldInfo, value)
 
-    static member Lambda (parameter: Var, body: Expr) = mkLambda (parameter, body)
+    static member Lambda(parameter: Var, body: Expr) =
+        mkLambda (parameter, body)
 
-    static member Let (letVariable: Var, letExpr: Expr, body: Expr) = mkLet (letVariable, letExpr, body)
+    static member Let(letVariable: Var, letExpr: Expr, body: Expr) =
+        mkLet (letVariable, letExpr, body)
 
-    static member LetRecursive (bindings, body: Expr) = mkLetRec (bindings, body)
+    static member LetRecursive(bindings, body: Expr) =
+        mkLetRec (bindings, body)
 
-    static member NewObject (constructorInfo:ConstructorInfo, arguments) =
+    static member NewObject(constructorInfo: ConstructorInfo, arguments) =
         checkNonNull "constructorInfo" constructorInfo
         mkCtorCall (constructorInfo, arguments)
 
-    static member DefaultValue (expressionType: Type) =
+    static member DefaultValue(expressionType: Type) =
         checkNonNull "expressionType" expressionType
         mkDefaultValue expressionType
 
     static member NewTuple elements =
         mkNewTuple elements
 
-    static member NewStructTuple (asm:Assembly, elements) =
+    static member NewStructTuple(asm: Assembly, elements) =
         mkNewStructTuple (asm, elements)
 
-    static member NewRecord (recordType: Type, elements) =
+    static member NewRecord(recordType: Type, elements) =
         checkNonNull "recordType" recordType
         mkNewRecord (recordType, elements)
 
-    static member NewArray (elementType: Type, elements) =
+    static member NewArray(elementType: Type, elements) =
         checkNonNull "elementType" elementType
-        mkNewArray(elementType, elements)
+        mkNewArray (elementType, elements)
 
-    static member NewDelegate (delegateType: Type, parameters: Var list, body: Expr) =
+    static member NewDelegate(delegateType: Type, parameters: Var list, body: Expr) =
         checkNonNull "delegateType" delegateType
-        mkNewDelegate(delegateType, mkIteratedLambdas (parameters, body))
+        mkNewDelegate (delegateType, mkIteratedLambdas (parameters, body))
 
-    static member NewUnionCase (unionCase, arguments) =
+    static member NewUnionCase(unionCase, arguments) =
         mkNewUnionCase (unionCase, arguments)
 
-    static member PropertyGet (obj: Expr, property: PropertyInfo, ?indexerArgs) =
+    static member PropertyGet(obj: Expr, property: PropertyInfo, ?indexerArgs) =
         checkNonNull "property" property
         mkInstancePropGet (obj, property, defaultArg indexerArgs [])
 
-    static member PropertyGet (property: PropertyInfo, ?indexerArgs) =
+    static member PropertyGet(property: PropertyInfo, ?indexerArgs) =
         checkNonNull "property" property
         mkStaticPropGet (property, defaultArg indexerArgs [])
 
-    static member PropertySet (obj: Expr, property: PropertyInfo, value: Expr, ?indexerArgs) =
+    static member PropertySet(obj: Expr, property: PropertyInfo, value: Expr, ?indexerArgs) =
         checkNonNull "property" property
-        mkInstancePropSet(obj, property, defaultArg indexerArgs [], value)
+        mkInstancePropSet (obj, property, defaultArg indexerArgs [], value)
 
-    static member PropertySet (property: PropertyInfo, value: Expr, ?indexerArgs) =
-        mkStaticPropSet(property, defaultArg indexerArgs [], value)
+    static member PropertySet(property: PropertyInfo, value: Expr, ?indexerArgs) =
+        mkStaticPropSet (property, defaultArg indexerArgs [], value)
 
-    static member Quote (inner: Expr) = mkQuote (inner, true)
+    static member Quote(inner: Expr) =
+        mkQuote (inner, true)
 
-    static member QuoteRaw (inner: Expr) = mkQuote (inner, false)
+    static member QuoteRaw(inner: Expr) =
+        mkQuote (inner, false)
 
-    static member QuoteTyped (inner: Expr) = mkQuote (inner, true)
+    static member QuoteTyped(inner: Expr) =
+        mkQuote (inner, true)
 
-    static member Sequential (first: Expr, second: Expr) =
+    static member Sequential(first: Expr, second: Expr) =
         mkSequential (first, second)
 
-    static member TryWith (body: Expr, filterVar: Var, filterBody: Expr, catchVar: Var, catchBody: Expr) =
+    static member TryWith(body: Expr, filterVar: Var, filterBody: Expr, catchVar: Var, catchBody: Expr) =
         mkTryWith (body, filterVar, filterBody, catchVar, catchBody)
 
-    static member TryFinally (body: Expr, compensation: Expr) =
+    static member TryFinally(body: Expr, compensation: Expr) =
         mkTryFinally (body, compensation)
 
-    static member TupleGet (tuple: Expr, index: int) =
+    static member TupleGet(tuple: Expr, index: int) =
         mkTupleGet (typeOf tuple, index, tuple)
 
-    static member TypeTest (source: Expr, target: Type) =
+    static member TypeTest(source: Expr, target: Type) =
         checkNonNull "target" target
         mkTypeTest (source, target)
 
-    static member UnionCaseTest (source: Expr, unionCase: UnionCaseInfo) =
+    static member UnionCaseTest(source: Expr, unionCase: UnionCaseInfo) =
         mkUnionCaseTest (unionCase, source)
 
-    static member Value (value: 'T) =
+    static member Value(value: 'T) =
         mkValue (box value, typeof<'T>)
 
     static member Value(value: obj, expressionType: Type) =
         checkNonNull "expressionType" expressionType
-        mkValue(value, expressionType)
+        mkValue (value, expressionType)
 
-    static member ValueWithName (value: 'T, name:string) =
+    static member ValueWithName(value: 'T, name: string) =
         checkNonNull "name" name
         mkValueWithName (box value, typeof<'T>, name)
 
-    static member ValueWithName(value: obj, expressionType: Type, name:string) =
+    static member ValueWithName(value: obj, expressionType: Type, name: string) =
         checkNonNull "expressionType" expressionType
         checkNonNull "name" name
-        mkValueWithName(value, expressionType, name)
+        mkValueWithName (value, expressionType, name)
 
-    static member WithValue (value: 'T, definition: Expr<'T>) =
-        let raw = mkValueWithDefn(box value, typeof<'T>, definition)
+    static member WithValue(value: 'T, definition: Expr<'T>) =
+        let raw = mkValueWithDefn (box value, typeof<'T>, definition)
         new Expr<'T>(raw.Tree, raw.CustomAttributes)
 
     static member WithValue(value: obj, expressionType: Type, definition: Expr) =
@@ -2074,22 +2720,23 @@ type Expr with
     static member Var variable =
         mkVar variable
 
-    static member VarSet (variable, value: Expr) =
+    static member VarSet(variable, value: Expr) =
         mkVarSet (variable, value)
 
-    static member WhileLoop (guard: Expr, body: Expr) =
+    static member WhileLoop(guard: Expr, body: Expr) =
         mkWhileLoop (guard, body)
 
     static member TryGetReflectedDefinition(methodBase: MethodBase) =
         checkNonNull "methodBase" methodBase
         tryGetReflectedDefinitionInstantiated methodBase
 
-    static member Cast(source: Expr) = cast source
+    static member Cast(source: Expr) =
+        cast source
 
     static member Deserialize(qualifyingType: Type, spliceTypes, spliceExprs, bytes: byte[]) =
         checkNonNull "qualifyingType" qualifyingType
         checkNonNull "bytes" bytes
-        deserialize (qualifyingType, [| |], Array.ofList spliceTypes, Array.ofList spliceExprs, bytes)
+        deserialize (qualifyingType, [||], Array.ofList spliceTypes, Array.ofList spliceExprs, bytes)
 
     static member Deserialize40(qualifyingType: Type, referencedTypes, spliceTypes, spliceExprs, bytes: byte[]) =
         checkNonNull "spliceExprs" spliceExprs
@@ -2100,61 +2747,103 @@ type Expr with
         deserialize (qualifyingType, referencedTypes, spliceTypes, spliceExprs, bytes)
 
     static member RegisterReflectedDefinitions(assembly, resource, serializedValue) =
-        Expr.RegisterReflectedDefinitions (assembly, resource, serializedValue, [| |])
+        Expr.RegisterReflectedDefinitions(assembly, resource, serializedValue, [||])
 
     static member RegisterReflectedDefinitions(assembly, resource, serializedValue, referencedTypes) =
         checkNonNull "assembly" assembly
-        registerReflectedDefinitions(assembly, resource, serializedValue, referencedTypes)
+        registerReflectedDefinitions (assembly, resource, serializedValue, referencedTypes)
 
     static member GlobalVar<'T>(name) : Expr<'T> =
         checkNonNull "name" name
-        Expr.Var (Var.Global(name, typeof<'T>)) |> Expr.Cast
+        Expr.Var(Var.Global(name, typeof<'T>)) |> Expr.Cast
 
 []
 module DerivedPatterns =
     open Patterns
 
     []
-    let (|Bool|_|) input = match input with ValueObj(:? bool   as v) -> Some v | _ -> None
+    let (|Bool|_|) input =
+        match input with
+        | ValueObj (:? bool as v) -> Some v
+        | _ -> None
 
     []
-    let (|String|_|) input = match input with ValueObj(:? string as v) -> Some v | _ -> None
+    let (|String|_|) input =
+        match input with
+        | ValueObj (:? string as v) -> Some v
+        | _ -> None
 
     []
-    let (|Single|_|) input = match input with ValueObj(:? single as v) -> Some v | _ -> None
+    let (|Single|_|) input =
+        match input with
+        | ValueObj (:? single as v) -> Some v
+        | _ -> None
 
     []
-    let (|Double|_|) input = match input with ValueObj(:? double as v) -> Some v | _ -> None
+    let (|Double|_|) input =
+        match input with
+        | ValueObj (:? double as v) -> Some v
+        | _ -> None
 
     []
-    let (|Char|_|) input = match input with ValueObj(:? char   as v) -> Some v | _ -> None
+    let (|Char|_|) input =
+        match input with
+        | ValueObj (:? char as v) -> Some v
+        | _ -> None
 
     []
-    let (|SByte|_|) input = match input with ValueObj(:? sbyte  as v) -> Some v | _ -> None
+    let (|SByte|_|) input =
+        match input with
+        | ValueObj (:? sbyte as v) -> Some v
+        | _ -> None
 
     []
-    let (|Byte|_|) input = match input with ValueObj(:? byte   as v) -> Some v | _ -> None
+    let (|Byte|_|) input =
+        match input with
+        | ValueObj (:? byte as v) -> Some v
+        | _ -> None
 
     []
-    let (|Int16|_|) input = match input with ValueObj(:? int16  as v) -> Some v | _ -> None
+    let (|Int16|_|) input =
+        match input with
+        | ValueObj (:? int16 as v) -> Some v
+        | _ -> None
 
     []
-    let (|UInt16|_|) input = match input with ValueObj(:? uint16 as v) -> Some v | _ -> None
+    let (|UInt16|_|) input =
+        match input with
+        | ValueObj (:? uint16 as v) -> Some v
+        | _ -> None
 
     []
-    let (|Int32|_|) input = match input with ValueObj(:? int32  as v) -> Some v | _ -> None
+    let (|Int32|_|) input =
+        match input with
+        | ValueObj (:? int32 as v) -> Some v
+        | _ -> None
 
     []
-    let (|UInt32|_|) input = match input with ValueObj(:? uint32 as v) -> Some v | _ -> None
+    let (|UInt32|_|) input =
+        match input with
+        | ValueObj (:? uint32 as v) -> Some v
+        | _ -> None
 
     []
-    let (|Int64|_|) input = match input with ValueObj(:? int64  as v) -> Some v | _ -> None
+    let (|Int64|_|) input =
+        match input with
+        | ValueObj (:? int64 as v) -> Some v
+        | _ -> None
 
     []
-    let (|UInt64|_|) input = match input with ValueObj(:? uint64 as v) -> Some v | _ -> None
+    let (|UInt64|_|) input =
+        match input with
+        | ValueObj (:? uint64 as v) -> Some v
+        | _ -> None
 
     []
-    let (|Unit|_|) input = match input with Comb0(ValueOp(_, ty, None)) when ty = typeof -> Some() | _ -> None
+    let (|Unit|_|) input =
+        match input with
+        | Comb0 (ValueOp (_, ty, None)) when ty = typeof -> Some()
+        | _ -> None
 
     /// (fun (x, y) -> z) is represented as 'fun p -> let x = p#0 let y = p#1' etc.
     /// This reverses this encoding.
@@ -2162,85 +2851,96 @@ module DerivedPatterns =
         /// Strip off the 'let' bindings for an TupledLambda
         let rec stripSuccessiveProjLets (p: Var) n expr =
             match expr with
-            | Let(v1, TupleGet(Var pA, m), rest)
-                  when p = pA && m = n->
-                      let restvs, b = stripSuccessiveProjLets p (n+1) rest
-                      v1 :: restvs, b
+            | Let (v1, TupleGet (Var pA, m), rest) when p = pA && m = n ->
+                let restvs, b = stripSuccessiveProjLets p (n + 1) rest
+                v1 :: restvs, b
             | _ -> ([], expr)
+
         match lam.Tree with
-        | LambdaTerm(v, body) ->
-              match stripSuccessiveProjLets v 0 body with
-              | [], b -> Some([v], b)
-              | letvs, b -> Some(letvs, b)
+        | LambdaTerm (v, body) ->
+            match stripSuccessiveProjLets v 0 body with
+            | [], b -> Some([ v ], b)
+            | letvs, b -> Some(letvs, b)
         | _ -> None
 
     let (|TupledApplication|_|) e =
         match e with
-        | Application(f, x) ->
+        | Application (f, x) ->
             match x with
             | Unit -> Some(f, [])
             | NewTuple x -> Some(f, x)
-            | x -> Some(f, [x])
+            | x -> Some(f, [ x ])
         | _ -> None
 
     []
-    let (|Lambdas|_|) (input: Expr) = qOneOrMoreRLinear (|TupledLambda|_|) input
+    let (|Lambdas|_|) (input: Expr) =
+        qOneOrMoreRLinear (|TupledLambda|_|) input
 
     []
-    let (|Applications|_|) (input: Expr) = qOneOrMoreLLinear (|TupledApplication|_|) input
+    let (|Applications|_|) (input: Expr) =
+        qOneOrMoreLLinear (|TupledApplication|_|) input
 
     /// Reverse the compilation of And and Or
     []
     let (|AndAlso|_|) input =
         match input with
-        | IfThenElse(x, y, Bool false) -> Some(x, y)
+        | IfThenElse (x, y, Bool false) -> Some(x, y)
         | _ -> None
 
     []
     let (|OrElse|_|) input =
         match input with
-        | IfThenElse(x, Bool true, y) -> Some(x, y)
+        | IfThenElse (x, Bool true, y) -> Some(x, y)
         | _ -> None
 
     []
     let (|SpecificCall|_|) templateParameter =
         // Note: precomputation
         match templateParameter with
-        | (Lambdas(_, Call(_, minfo1, _)) | Call(_, minfo1, _)) ->
+        | (Lambdas (_, Call (_, minfo1, _))
+        | Call (_, minfo1, _)) ->
             let isg1 = minfo1.IsGenericMethod
-            let gmd = if isg1 then minfo1.GetGenericMethodDefinition() else null
+
+            let gmd =
+                if isg1 then
+                    minfo1.GetGenericMethodDefinition()
+                else
+                    null
 
             // end-of-precomputation
 
             (fun tm ->
-               match tm with
-               | Call(obj, minfo2, args)
+                match tm with
+                | Call (obj, minfo2, args) when
 #if FX_NO_REFLECTION_METADATA_TOKENS
-                  when ( // if metadata tokens are not available we'll rely only on equality of method references
+                    ( // if metadata tokens are not available we'll rely only on equality of method references
 #else
-                  when (minfo1.MetadataToken = minfo2.MetadataToken &&
+                    (minfo1.MetadataToken = minfo2.MetadataToken
+                     &&
 #endif
-                        if isg1 then
-                          minfo2.IsGenericMethod && gmd = minfo2.GetGenericMethodDefinition()
-                        else
-                          minfo1 = minfo2) ->
-                   Some(obj, (minfo2.GetGenericArguments() |> Array.toList), args)
-               | _ -> None)
-        | _ ->
-            invalidArg "templateParameter" (SR.GetString(SR.QunrecognizedMethodCall))
+                    if isg1 then
+                        minfo2.IsGenericMethod && gmd = minfo2.GetGenericMethodDefinition()
+                    else
+                        minfo1 = minfo2)
+                    ->
+                    Some(obj, (minfo2.GetGenericArguments() |> Array.toList), args)
+                | _ -> None)
+        | _ -> invalidArg "templateParameter" (SR.GetString(SR.QunrecognizedMethodCall))
 
     let private new_decimal_info =
-       methodhandleof (fun (low, medium, high, isNegative, scale) -> LanguagePrimitives.IntrinsicFunctions.MakeDecimal low medium high isNegative scale)
-       |> System.Reflection.MethodInfo.GetMethodFromHandle
-       :?> MethodInfo
+        methodhandleof (fun (low, medium, high, isNegative, scale) ->
+            LanguagePrimitives.IntrinsicFunctions.MakeDecimal low medium high isNegative scale)
+        |> System.Reflection.MethodInfo.GetMethodFromHandle
+        :?> MethodInfo
 
     []
     let (|Decimal|_|) input =
         match input with
-        | Call (None, mi, [Int32 low; Int32 medium; Int32 high; Bool isNegative; Byte scale])
-          when mi.Name = new_decimal_info.Name
-               && mi.DeclaringType.FullName = new_decimal_info.DeclaringType.FullName ->
-            Some (LanguagePrimitives.IntrinsicFunctions.MakeDecimal low medium high isNegative scale)
+        | Call (None, mi, [ Int32 low; Int32 medium; Int32 high; Bool isNegative; Byte scale ]) when
+            mi.Name = new_decimal_info.Name
+            && mi.DeclaringType.FullName = new_decimal_info.DeclaringType.FullName
+            ->
+            Some(LanguagePrimitives.IntrinsicFunctions.MakeDecimal low medium high isNegative scale)
         | _ -> None
 
     []
@@ -2248,61 +2948,63 @@ module DerivedPatterns =
         Expr.TryGetReflectedDefinition methodBase
 
     []
-    let (|PropertyGetterWithReflectedDefinition|_|) (propertyInfo:System.Reflection.PropertyInfo) =
-        Expr.TryGetReflectedDefinition (propertyInfo.GetGetMethod true)
+    let (|PropertyGetterWithReflectedDefinition|_|) (propertyInfo: System.Reflection.PropertyInfo) =
+        Expr.TryGetReflectedDefinition(propertyInfo.GetGetMethod true)
 
     []
-    let (|PropertySetterWithReflectedDefinition|_|) (propertyInfo:System.Reflection.PropertyInfo) =
-        Expr.TryGetReflectedDefinition (propertyInfo.GetSetMethod true)
+    let (|PropertySetterWithReflectedDefinition|_|) (propertyInfo: System.Reflection.PropertyInfo) =
+        Expr.TryGetReflectedDefinition(propertyInfo.GetSetMethod true)
 
 []
 module ExprShape =
     open Patterns
-    let RebuildShapeCombination(shape:obj, arguments) =
+
+    let RebuildShapeCombination (shape: obj, arguments) =
         // preserve the attributes
-        let op, attrs = unbox(shape)
+        let op, attrs = unbox (shape)
+
         let e =
             match op, arguments with
-            | AppOp, [f;x] -> mkApplication(f, x)
-            | IfThenElseOp, [g;t;e] -> mkIfThenElse(g, t, e)
-            | LetRecOp, [e1] -> mkLetRecRaw e1
+            | AppOp, [ f; x ] -> mkApplication (f, x)
+            | IfThenElseOp, [ g; t; e ] -> mkIfThenElse (g, t, e)
+            | LetRecOp, [ e1 ] -> mkLetRecRaw e1
             | LetRecCombOp, _ -> mkLetRecCombRaw arguments
-            | LetOp, [e1;e2] -> mkLetRawWithCheck(e1, e2)
-            | NewRecordOp ty, _ -> mkNewRecord(ty, arguments)
-            | NewUnionCaseOp unionCase, _ -> mkNewUnionCase(unionCase, arguments)
-            | UnionCaseTestOp unionCase, [arg] -> mkUnionCaseTest(unionCase, arg)
-            | NewTupleOp ty, _ -> mkNewTupleWithType(ty, arguments)
-            | TupleGetOp(ty, i), [arg] -> mkTupleGet(ty, i, arg)
-            | InstancePropGetOp pinfo, (obj :: args) -> mkInstancePropGet(obj, pinfo, args)
-            | StaticPropGetOp pinfo, _ -> mkStaticPropGet(pinfo, arguments)
-            | InstancePropSetOp pinfo, obj :: (FrontAndBack(args, v)) -> mkInstancePropSet(obj, pinfo, args, v)
-            | StaticPropSetOp pinfo, (FrontAndBack(args, v)) -> mkStaticPropSet(pinfo, args, v)
-            | InstanceFieldGetOp finfo, [obj] -> mkInstanceFieldGet(obj, finfo)
-            | StaticFieldGetOp finfo, [] -> mkStaticFieldGet(finfo )
-            | InstanceFieldSetOp finfo, [obj;v] -> mkInstanceFieldSet(obj, finfo, v)
-            | StaticFieldSetOp finfo, [v] -> mkStaticFieldSet(finfo, v)
-            | NewObjectOp minfo, _ -> mkCtorCall(minfo, arguments)
+            | LetOp, [ e1; e2 ] -> mkLetRawWithCheck (e1, e2)
+            | NewRecordOp ty, _ -> mkNewRecord (ty, arguments)
+            | NewUnionCaseOp unionCase, _ -> mkNewUnionCase (unionCase, arguments)
+            | UnionCaseTestOp unionCase, [ arg ] -> mkUnionCaseTest (unionCase, arg)
+            | NewTupleOp ty, _ -> mkNewTupleWithType (ty, arguments)
+            | TupleGetOp (ty, i), [ arg ] -> mkTupleGet (ty, i, arg)
+            | InstancePropGetOp pinfo, (obj :: args) -> mkInstancePropGet (obj, pinfo, args)
+            | StaticPropGetOp pinfo, _ -> mkStaticPropGet (pinfo, arguments)
+            | InstancePropSetOp pinfo, obj :: (FrontAndBack (args, v)) -> mkInstancePropSet (obj, pinfo, args, v)
+            | StaticPropSetOp pinfo, (FrontAndBack (args, v)) -> mkStaticPropSet (pinfo, args, v)
+            | InstanceFieldGetOp finfo, [ obj ] -> mkInstanceFieldGet (obj, finfo)
+            | StaticFieldGetOp finfo, [] -> mkStaticFieldGet (finfo)
+            | InstanceFieldSetOp finfo, [ obj; v ] -> mkInstanceFieldSet (obj, finfo, v)
+            | StaticFieldSetOp finfo, [ v ] -> mkStaticFieldSet (finfo, v)
+            | NewObjectOp minfo, _ -> mkCtorCall (minfo, arguments)
             | DefaultValueOp ty, _ -> mkDefaultValue ty
-            | StaticMethodCallOp minfo, _ -> mkStaticMethodCall(minfo, arguments)
-            | InstanceMethodCallOp minfo, obj :: args -> mkInstanceMethodCall(obj, minfo, args)
-            | StaticMethodCallWOp (minfo, minfoW, n), _ -> mkStaticMethodCallW(minfo, minfoW, n, arguments)
-            | InstanceMethodCallWOp (minfo, minfoW, n), obj::args -> mkInstanceMethodCallW(obj, minfo, minfoW, n, args)
-            | CoerceOp ty, [arg] -> mkCoerce(ty, arg)
-            | NewArrayOp ty, _ -> mkNewArray(ty, arguments)
-            | NewDelegateOp ty, [arg] -> mkNewDelegate(ty, arg)
-            | SequentialOp, [e1;e2] -> mkSequential(e1, e2)
-            | TypeTestOp ty, [e1] -> mkTypeTest(e1, ty)
-            | AddressOfOp, [e1] -> mkAddressOf e1
-            | VarSetOp, [E(VarTerm v); e] -> mkVarSet(v, e)
-            | AddressSetOp, [e1;e2] -> mkAddressSet(e1, e2)
-            | ForIntegerRangeLoopOp, [e1;e2;E(LambdaTerm(v, e3))] -> mkForLoop(v, e1, e2, e3)
-            | WhileLoopOp, [e1;e2] -> mkWhileLoop(e1, e2)
-            | TryFinallyOp, [e1;e2] -> mkTryFinally(e1, e2)
-            | TryWithOp, [e1;Lambda(v1, e2);Lambda(v2, e3)] -> mkTryWith(e1, v1, e2, v2, e3)
-            | QuoteOp flg, [e1] -> mkQuote(e1, flg)
-            | ValueOp(v, ty, None), [] -> mkValue(v, ty)
-            | ValueOp(v, ty, Some nm), [] -> mkValueWithName(v, ty, nm)
-            | WithValueOp(v, ty), [e] -> mkValueWithDefn(v, ty, e)
+            | StaticMethodCallOp minfo, _ -> mkStaticMethodCall (minfo, arguments)
+            | InstanceMethodCallOp minfo, obj :: args -> mkInstanceMethodCall (obj, minfo, args)
+            | StaticMethodCallWOp (minfo, minfoW, n), _ -> mkStaticMethodCallW (minfo, minfoW, n, arguments)
+            | InstanceMethodCallWOp (minfo, minfoW, n), obj :: args -> mkInstanceMethodCallW (obj, minfo, minfoW, n, args)
+            | CoerceOp ty, [ arg ] -> mkCoerce (ty, arg)
+            | NewArrayOp ty, _ -> mkNewArray (ty, arguments)
+            | NewDelegateOp ty, [ arg ] -> mkNewDelegate (ty, arg)
+            | SequentialOp, [ e1; e2 ] -> mkSequential (e1, e2)
+            | TypeTestOp ty, [ e1 ] -> mkTypeTest (e1, ty)
+            | AddressOfOp, [ e1 ] -> mkAddressOf e1
+            | VarSetOp, [ E (VarTerm v); e ] -> mkVarSet (v, e)
+            | AddressSetOp, [ e1; e2 ] -> mkAddressSet (e1, e2)
+            | ForIntegerRangeLoopOp, [ e1; e2; E (LambdaTerm (v, e3)) ] -> mkForLoop (v, e1, e2, e3)
+            | WhileLoopOp, [ e1; e2 ] -> mkWhileLoop (e1, e2)
+            | TryFinallyOp, [ e1; e2 ] -> mkTryFinally (e1, e2)
+            | TryWithOp, [ e1; Lambda (v1, e2); Lambda (v2, e3) ] -> mkTryWith (e1, v1, e2, v2, e3)
+            | QuoteOp flg, [ e1 ] -> mkQuote (e1, flg)
+            | ValueOp (v, ty, None), [] -> mkValue (v, ty)
+            | ValueOp (v, ty, Some nm), [] -> mkValueWithName (v, ty, nm)
+            | WithValueOp (v, ty), [ e ] -> mkValueWithDefn (v, ty, e)
             | _ -> invalidOp (SR.GetString(SR.QillFormedAppOrLet))
 
         EA(e.Tree, attrs)
@@ -2311,9 +3013,11 @@ module ExprShape =
     let rec (|ShapeVar|ShapeLambda|ShapeCombination|) input =
         let rec loop expr =
             let (E t) = expr
+
             match t with
             | VarTerm v -> ShapeVar v
-            | LambdaTerm(v, b) -> ShapeLambda(v, b)
-            | CombTerm(op, args) -> ShapeCombination(box (op, expr.CustomAttributes), args)
+            | LambdaTerm (v, b) -> ShapeLambda(v, b)
+            | CombTerm (op, args) -> ShapeCombination(box (op, expr.CustomAttributes), args)
             | HoleTerm _ -> invalidArg "expr" (SR.GetString(SR.QunexpectedHole))
+
         loop (input :> Expr)
diff --git a/src/FSharp.Core/reflect.fs b/src/FSharp.Core/reflect.fs
index 63b8d6d4651..0a6fdd12e27 100644
--- a/src/FSharp.Core/reflect.fs
+++ b/src/FSharp.Core/reflect.fs
@@ -28,27 +28,35 @@ module internal ReflectionUtils =
 []
 module internal Impl =
 
-    let getBindingFlags allowAccess = ReflectionUtils.toBindingFlags (defaultArg allowAccess false)
+    let getBindingFlags allowAccess =
+        ReflectionUtils.toBindingFlags (defaultArg allowAccess false)
 
     let inline checkNonNull argName (v: 'T) =
         match box v with
         | null -> nullArg argName
         | _ -> ()
 
-    let isNamedType(typ: Type) = not (typ.IsArray || typ.IsByRef || typ.IsPointer)
+    let isNamedType (typ: Type) =
+        not (typ.IsArray || typ.IsByRef || typ.IsPointer)
 
     let equivHeadTypes (ty1: Type) (ty2: Type) =
-        isNamedType ty1 &&
-        if ty1.IsGenericType then
-          ty2.IsGenericType && (ty1.GetGenericTypeDefinition()).Equals(ty2.GetGenericTypeDefinition())
-        else
-          ty1.Equals ty2
+        isNamedType ty1
+        && if ty1.IsGenericType then
+               ty2.IsGenericType
+               && (ty1.GetGenericTypeDefinition()).Equals(ty2.GetGenericTypeDefinition())
+           else
+               ty1.Equals ty2
 
     let func = typedefof<(obj -> obj)>
 
-    let isOptionType typ = equivHeadTypes typ (typeof)
-    let isFunctionType typ = equivHeadTypes typ (typeof<(int -> int)>)
-    let isListType typ = equivHeadTypes typ (typeof)
+    let isOptionType typ =
+        equivHeadTypes typ (typeof)
+
+    let isFunctionType typ =
+        equivHeadTypes typ (typeof<(int -> int)>)
+
+    let isListType typ =
+        equivHeadTypes typ (typeof)
 
     //-----------------------------------------------------------------
     // GENERAL UTILITIES
@@ -57,165 +65,171 @@ module internal Impl =
     let staticPropertyFlags = BindingFlags.GetProperty ||| BindingFlags.Static
     let staticFieldFlags = BindingFlags.GetField ||| BindingFlags.Static
     let staticMethodFlags = BindingFlags.Static
-    let getInstancePropertyInfo (typ: Type, propName, bindingFlags) = typ.GetProperty(propName, instancePropertyFlags ||| bindingFlags)
-    let getInstancePropertyInfos (typ, names, bindingFlags) = names |> Array.map (fun nm -> getInstancePropertyInfo (typ, nm, bindingFlags))
+
+    let getInstancePropertyInfo (typ: Type, propName, bindingFlags) =
+        typ.GetProperty(propName, instancePropertyFlags ||| bindingFlags)
+
+    let getInstancePropertyInfos (typ, names, bindingFlags) =
+        names |> Array.map (fun nm -> getInstancePropertyInfo (typ, nm, bindingFlags))
+
     let getInstancePropertyReader (typ: Type, propName, bindingFlags) =
-        match getInstancePropertyInfo(typ, propName, bindingFlags) with
+        match getInstancePropertyInfo (typ, propName, bindingFlags) with
         | null -> None
-        | prop -> Some(fun (obj: obj) -> prop.GetValue (obj, instancePropertyFlags ||| bindingFlags, null, null, null))
+        | prop -> Some(fun (obj: obj) -> prop.GetValue(obj, instancePropertyFlags ||| bindingFlags, null, null, null))
 
     //-----------------------------------------------------------------
     // EXPRESSION TREE COMPILATION
 
     let compilePropGetterFunc (prop: PropertyInfo) =
-        let param = Expression.Parameter (typeof, "param")
-        
+        let param = Expression.Parameter(typeof, "param")
+
         let expr =
-            Expression.Lambda> (
-                Expression.Convert (
-                    Expression.Property (
-                        Expression.Convert (param, prop.DeclaringType),
-                        prop),
-                    typeof),
-                param)
-        expr.Compile ()
+            Expression.Lambda>(
+                Expression.Convert(Expression.Property(Expression.Convert(param, prop.DeclaringType), prop), typeof),
+                param
+            )
+
+        expr.Compile()
 
     let compileRecordOrUnionCaseReaderFunc (typ, props: PropertyInfo[]) =
-        let param = Expression.Parameter (typeof, "param")
+        let param = Expression.Parameter(typeof, "param")
         let typedParam = Expression.Variable typ
-    
+
         let expr =
-            Expression.Lambda> (
-                Expression.Block (
+            Expression.Lambda>(
+                Expression.Block(
                     [ typedParam ],
-                    Expression.Assign (typedParam, Expression.Convert (param, typ)),
-                    Expression.NewArrayInit (typeof, [
-                        for prop in props ->
-                            Expression.Convert (Expression.Property (typedParam, prop), typeof) :> Expression
-                    ])
+                    Expression.Assign(typedParam, Expression.Convert(param, typ)),
+                    Expression.NewArrayInit(
+                        typeof,
+                        [
+                            for prop in props -> Expression.Convert(Expression.Property(typedParam, prop), typeof) :> Expression
+                        ]
+                    )
                 ),
-                param)
-        expr.Compile ()
+                param
+            )
+
+        expr.Compile()
 
     let compileRecordConstructorFunc (ctorInfo: ConstructorInfo) =
-        let ctorParams = ctorInfo.GetParameters ()
-        let paramArray = Expression.Parameter (typeof, "paramArray")
+        let ctorParams = ctorInfo.GetParameters()
+        let paramArray = Expression.Parameter(typeof, "paramArray")
 
         let expr =
-            Expression.Lambda> (
-                Expression.Convert (
-                    Expression.New (
+            Expression.Lambda>(
+                Expression.Convert(
+                    Expression.New(
                         ctorInfo,
                         [
                             for paramIndex in 0 .. ctorParams.Length - 1 do
                                 let p = ctorParams.[paramIndex]
 
-                                Expression.Convert (
-                                    Expression.ArrayAccess (paramArray, Expression.Constant paramIndex),
-                                    p.ParameterType
-                                ) :> Expression
+                                Expression.Convert(Expression.ArrayAccess(paramArray, Expression.Constant paramIndex), p.ParameterType)
+                                :> Expression
                         ]
                     ),
-                    typeof),
+                    typeof
+                ),
                 paramArray
             )
-        expr.Compile ()
+
+        expr.Compile()
 
     let compileUnionCaseConstructorFunc (methodInfo: MethodInfo) =
-        let methodParams = methodInfo.GetParameters ()
-        let paramArray = Expression.Parameter (typeof, "param")
-        
+        let methodParams = methodInfo.GetParameters()
+        let paramArray = Expression.Parameter(typeof, "param")
+
         let expr =
-            Expression.Lambda> (
-                Expression.Convert (
-                    Expression.Call (
+            Expression.Lambda>(
+                Expression.Convert(
+                    Expression.Call(
                         methodInfo,
                         [
                             for paramIndex in 0 .. methodParams.Length - 1 do
                                 let p = methodParams.[paramIndex]
 
-                                Expression.Convert (
-                                    Expression.ArrayAccess (paramArray, Expression.Constant paramIndex),
-                                    p.ParameterType
-                                ) :> Expression
+                                Expression.Convert(Expression.ArrayAccess(paramArray, Expression.Constant paramIndex), p.ParameterType)
+                                :> Expression
                         ]
                     ),
-                    typeof),
+                    typeof
+                ),
                 paramArray
             )
-        expr.Compile ()
+
+        expr.Compile()
 
     let compileUnionTagReaderFunc (info: Choice) =
-        let param = Expression.Parameter (typeof, "param")
+        let param = Expression.Parameter(typeof, "param")
+
         let tag =
             match info with
-            | Choice1Of2 info -> Expression.Call (info, Expression.Convert (param, info.DeclaringType)) :> Expression
-            | Choice2Of2 info -> Expression.Property (Expression.Convert (param, info.DeclaringType), info) :> _
-        
-        let expr =
-            Expression.Lambda> (
-                tag,
-                param)
-        expr.Compile ()
+            | Choice1Of2 info -> Expression.Call(info, Expression.Convert(param, info.DeclaringType)) :> Expression
+            | Choice2Of2 info -> Expression.Property(Expression.Convert(param, info.DeclaringType), info) :> _
+
+        let expr = Expression.Lambda>(tag, param)
+        expr.Compile()
 
     let compileTupleConstructor tupleEncField getTupleConstructorMethod typ =
         let rec constituentTuple (typ: Type) elements startIndex =
-            Expression.New (
+            Expression.New(
                 getTupleConstructorMethod typ,
                 [
-                    let genericArgs = typ.GetGenericArguments ()
+                    let genericArgs = typ.GetGenericArguments()
 
                     for paramIndex in 0 .. genericArgs.Length - 1 do
                         let genericArg = genericArgs.[paramIndex]
-                        
+
                         if paramIndex = tupleEncField then
                             constituentTuple genericArg elements (startIndex + paramIndex) :> Expression
                         else
-                            Expression.Convert (Expression.ArrayAccess (elements, Expression.Constant (startIndex + paramIndex)), genericArg)
-                ])
+                            Expression.Convert(Expression.ArrayAccess(elements, Expression.Constant(startIndex + paramIndex)), genericArg)
+                ]
+            )
 
-        let elements = Expression.Parameter (typeof, "elements")
+        let elements = Expression.Parameter(typeof, "elements")
 
         let expr =
-            Expression.Lambda> (
-                Expression.Convert (
-                    constituentTuple typ elements 0,
-                    typeof
-                ),
-                elements
-            )
+            Expression.Lambda>(Expression.Convert(constituentTuple typ elements 0, typeof), elements)
 
-        expr.Compile ()
+        expr.Compile()
 
     let compileTupleReader tupleEncField getTupleElementAccessors typ =
-        let rec writeTupleIntoArray (typ: Type) (tuple: Expression) outputArray startIndex = seq {
-            let elements =
-                match getTupleElementAccessors typ with
-                // typ is a struct tuple and its elements are accessed via fields 
-                | Choice1Of2 (fi: FieldInfo[]) -> fi |> Array.map (fun fi -> Expression.Field (tuple, fi), fi.FieldType)
-                // typ is a class tuple and its elements are accessed via properties 
-                | Choice2Of2 (pi: PropertyInfo[]) -> pi |> Array.map (fun pi -> Expression.Property (tuple, pi), pi.PropertyType)
-
-            for index, (element, elementType) in elements |> Array.indexed do
-                if index = tupleEncField then
-                    let innerTupleParam = Expression.Parameter (elementType, "innerTuple")
-                    Expression.Block (
-                        [ innerTupleParam ],
-                        [
-                            yield Expression.Assign (innerTupleParam, element) :> Expression
-                            yield! writeTupleIntoArray elementType innerTupleParam outputArray (startIndex + index)
-                        ]
-                    ) :> Expression
-                else
-                    Expression.Assign (
-                        Expression.ArrayAccess (outputArray, Expression.Constant (index + startIndex)),
-                        Expression.Convert (element, typeof)
-                    ) :> Expression }
+        let rec writeTupleIntoArray (typ: Type) (tuple: Expression) outputArray startIndex =
+            seq {
+                let elements =
+                    match getTupleElementAccessors typ with
+                    // typ is a struct tuple and its elements are accessed via fields
+                    | Choice1Of2 (fi: FieldInfo[]) -> fi |> Array.map (fun fi -> Expression.Field(tuple, fi), fi.FieldType)
+                    // typ is a class tuple and its elements are accessed via properties
+                    | Choice2Of2 (pi: PropertyInfo[]) -> pi |> Array.map (fun pi -> Expression.Property(tuple, pi), pi.PropertyType)
+
+                for index, (element, elementType) in elements |> Array.indexed do
+                    if index = tupleEncField then
+                        let innerTupleParam = Expression.Parameter(elementType, "innerTuple")
+
+                        Expression.Block(
+                            [ innerTupleParam ],
+                            [
+                                yield Expression.Assign(innerTupleParam, element) :> Expression
+                                yield! writeTupleIntoArray elementType innerTupleParam outputArray (startIndex + index)
+                            ]
+                        )
+                        :> Expression
+                    else
+                        Expression.Assign(
+                            Expression.ArrayAccess(outputArray, Expression.Constant(index + startIndex)),
+                            Expression.Convert(element, typeof)
+                        )
+                        :> Expression
+            }
+
+        let param = Expression.Parameter(typeof, "outerTuple")
+        let outputArray = Expression.Variable(typeof, "output")
 
-        let param = Expression.Parameter (typeof, "outerTuple")
-        let outputArray = Expression.Variable (typeof, "output")
         let rec outputLength tupleEncField (typ: Type) =
-            let genericArgs = typ.GetGenericArguments ()
+            let genericArgs = typ.GetGenericArguments()
 
             if genericArgs.Length > tupleEncField then
                 tupleEncField + outputLength tupleEncField genericArgs.[genericArgs.Length - 1]
@@ -223,35 +237,39 @@ module internal Impl =
                 genericArgs.Length
 
         let expr =
-            Expression.Lambda> (
-                Expression.Block (
+            Expression.Lambda>(
+                Expression.Block(
                     [ outputArray ],
                     [
-                        yield Expression.Assign (
-                            outputArray,
-                            Expression.NewArrayBounds (typeof, Expression.Constant (outputLength tupleEncField typ))
-                        ) :> Expression
-                        yield! writeTupleIntoArray typ (Expression.Convert (param, typ)) outputArray 0
+                        yield
+                            Expression.Assign(
+                                outputArray,
+                                Expression.NewArrayBounds(typeof, Expression.Constant(outputLength tupleEncField typ))
+                            )
+                            :> Expression
+                        yield! writeTupleIntoArray typ (Expression.Convert(param, typ)) outputArray 0
                         yield outputArray :> Expression
                     ]
                 ),
-                param)
+                param
+            )
 
-        expr.Compile ()
+        expr.Compile()
 
     //-----------------------------------------------------------------
     // ATTRIBUTE DECOMPILATION
 
     let tryFindCompilationMappingAttribute (attrs: obj[]) =
-      match attrs with
-      | null | [| |] -> None
-      | [| res |] -> let a = (res :?> CompilationMappingAttribute) in Some (a.SourceConstructFlags, a.SequenceNumber, a.VariantNumber)
-      | _ -> invalidOp (SR.GetString (SR.multipleCompilationMappings))
+        match attrs with
+        | null
+        | [||] -> None
+        | [| res |] -> let a = (res :?> CompilationMappingAttribute) in Some(a.SourceConstructFlags, a.SequenceNumber, a.VariantNumber)
+        | _ -> invalidOp (SR.GetString(SR.multipleCompilationMappings))
 
     let findCompilationMappingAttribute (attrs: obj[]) =
-      match tryFindCompilationMappingAttribute attrs with
-      | None -> failwith "no compilation mapping attribute"
-      | Some a -> a
+        match tryFindCompilationMappingAttribute attrs with
+        | None -> failwith "no compilation mapping attribute"
+        | Some a -> a
 
     let cmaName = typeof.FullName
     let assemblyName = typeof.Assembly.GetName().Name
@@ -262,58 +280,73 @@ module internal Impl =
         | null -> None
         | _ ->
             let mutable res = None
+
             for a in attrs do
                 if a.Constructor.DeclaringType.FullName = cmaName then
                     let args = a.ConstructorArguments
+
                     let flags =
-                         match args.Count  with
-                         | 1 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), 0, 0)
-                         | 2 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), (let x = args.[1] in x.Value :?> int), 0)
-                         | 3 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), (let x = args.[1] in x.Value :?> int), (let x = args.[2] in x.Value :?> int))
-                         | _ -> (enum 0, 0, 0)
+                        match args.Count with
+                        | 1 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), 0, 0)
+                        | 2 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), (let x = args.[1] in x.Value :?> int), 0)
+                        | 3 ->
+                            ((let x = args.[0] in x.Value :?> SourceConstructFlags),
+                             (let x = args.[1] in x.Value :?> int),
+                             (let x = args.[2] in x.Value :?> int))
+                        | _ -> (enum 0, 0, 0)
+
                     res <- Some flags
+
             res
 
     let findCompilationMappingAttributeFromData attrs =
-      match tryFindCompilationMappingAttributeFromData attrs with
-      | None -> failwith "no compilation mapping attribute"
-      | Some a -> a
+        match tryFindCompilationMappingAttributeFromData attrs with
+        | None -> failwith "no compilation mapping attribute"
+        | Some a -> a
 
-    let tryFindCompilationMappingAttributeFromType       (typ: Type)        =
+    let tryFindCompilationMappingAttributeFromType (typ: Type) =
         let assem = typ.Assembly
+
         if (not (isNull assem)) && assem.ReflectionOnly then
-           tryFindCompilationMappingAttributeFromData ( typ.GetCustomAttributesData())
+            tryFindCompilationMappingAttributeFromData (typ.GetCustomAttributesData())
         else
-        tryFindCompilationMappingAttribute ( typ.GetCustomAttributes (typeof, false))
+            tryFindCompilationMappingAttribute (typ.GetCustomAttributes(typeof, false))
 
     let tryFindCompilationMappingAttributeFromMemberInfo (info: MemberInfo) =
         let assem = info.DeclaringType.Assembly
+
         if (not (isNull assem)) && assem.ReflectionOnly then
-           tryFindCompilationMappingAttributeFromData (info.GetCustomAttributesData())
+            tryFindCompilationMappingAttributeFromData (info.GetCustomAttributesData())
         else
-        tryFindCompilationMappingAttribute (info.GetCustomAttributes (typeof, false))
+            tryFindCompilationMappingAttribute (info.GetCustomAttributes(typeof, false))
 
-    let    findCompilationMappingAttributeFromMemberInfo (info: MemberInfo) =
+    let findCompilationMappingAttributeFromMemberInfo (info: MemberInfo) =
         let assem = info.DeclaringType.Assembly
+
         if (not (isNull assem)) && assem.ReflectionOnly then
             findCompilationMappingAttributeFromData (info.GetCustomAttributesData())
         else
-        findCompilationMappingAttribute (info.GetCustomAttributes (typeof, false))
+            findCompilationMappingAttribute (info.GetCustomAttributes(typeof, false))
 
-    let sequenceNumberOfMember          (x: MemberInfo) = let (_, n, _) = findCompilationMappingAttributeFromMemberInfo x in n
-    let variantNumberOfMember           (x: MemberInfo) = let (_, _, vn) = findCompilationMappingAttributeFromMemberInfo x in vn
+    let sequenceNumberOfMember (x: MemberInfo) =
+        let (_, n, _) = findCompilationMappingAttributeFromMemberInfo x in n
 
-    let sortFreshArray f arr = Array.sortInPlaceWith f arr; arr
+    let variantNumberOfMember (x: MemberInfo) =
+        let (_, _, vn) = findCompilationMappingAttributeFromMemberInfo x in vn
 
-    let isFieldProperty (prop : PropertyInfo) =
+    let sortFreshArray f arr =
+        Array.sortInPlaceWith f arr
+        arr
+
+    let isFieldProperty (prop: PropertyInfo) =
         match tryFindCompilationMappingAttributeFromMemberInfo prop with
         | None -> false
         | Some (flags, _n, _vn) -> (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.Field
 
     let tryFindSourceConstructFlagsOfType (typ: Type) =
-      match tryFindCompilationMappingAttributeFromType typ with
-      | None -> None
-      | Some (flags, _n, _vn) -> Some flags
+        match tryFindCompilationMappingAttributeFromType typ with
+        | None -> None
+        | Some (flags, _n, _vn) -> Some flags
 
     //-----------------------------------------------------------------
     // UNION DECOMPILATION
@@ -321,11 +354,14 @@ module internal Impl =
     // Get the type where the type definitions are stored
     let getUnionCasesTyp (typ: Type, _bindingFlags) =
 #if CASES_IN_NESTED_CLASS
-       let casesTyp = typ.GetNestedType("Cases", bindingFlags)
-       if casesTyp.IsGenericTypeDefinition then casesTyp.MakeGenericType(typ.GetGenericArguments())
-       else casesTyp
+        let casesTyp = typ.GetNestedType("Cases", bindingFlags)
+
+        if casesTyp.IsGenericTypeDefinition then
+            casesTyp.MakeGenericType(typ.GetGenericArguments())
+        else
+            casesTyp
 #else
-       typ
+        typ
 #endif
 
     let getUnionTypeTagNameMap (typ: Type, bindingFlags) =
@@ -343,11 +379,16 @@ module internal Impl =
                         // chop "get_" or  "New" off the front
                         let nm =
                             if not (isListType typ) && not (isOptionType typ) && nm.Length > 3 then
-                                if   nm.StartsWith ("get_", StringComparison.Ordinal) then nm.[4..]
-                                elif nm.StartsWith ("New", StringComparison.Ordinal) then nm.[3..]
-                                else nm
-                            else nm
-                        Some (n, nm)
+                                if nm.StartsWith("get_", StringComparison.Ordinal) then
+                                    nm.[4..]
+                                elif nm.StartsWith("New", StringComparison.Ordinal) then
+                                    nm.[3..]
+                                else
+                                    nm
+                            else
+                                nm
+
+                        Some(n, nm)
                     else
                         None)
         | _ ->
@@ -357,8 +398,11 @@ module internal Impl =
             |> Array.map (fun tagfield -> (tagfield.GetValue null :?> int), tagfield.Name)
 
     let getUnionCaseTyp (typ: Type, tag: int, bindingFlags) =
-        let tagFields = getUnionTypeTagNameMap(typ, bindingFlags)
-        let tagField = tagFields |> Array.pick (fun (i, f) -> if i = tag then Some f else None)
+        let tagFields = getUnionTypeTagNameMap (typ, bindingFlags)
+
+        let tagField =
+            tagFields |> Array.pick (fun (i, f) -> if i = tag then Some f else None)
+
         if tagFields.Length = 1 then
             typ
         else
@@ -367,75 +411,107 @@ module internal Impl =
             let isTwoCasedDU =
                 if tagFields.Length = 2 then
                     match typ.GetCustomAttributes(typeof, false) with
-                    | [|:? CompilationRepresentationAttribute as attr|] ->
+                    | [| :? CompilationRepresentationAttribute as attr |] ->
                         (attr.Flags &&& CompilationRepresentationFlags.UseNullAsTrueValue) = CompilationRepresentationFlags.UseNullAsTrueValue
                     | _ -> false
                 else
                     false
+
             if isTwoCasedDU then
                 typ
             else
-            let casesTyp = getUnionCasesTyp (typ, bindingFlags)
-            let caseTyp = casesTyp.GetNestedType(tagField, bindingFlags) // if this is null then the union is nullary
-            match caseTyp with
-            | null -> null
-            | _ when caseTyp.IsGenericTypeDefinition -> caseTyp.MakeGenericType(casesTyp.GetGenericArguments())
-            | _ -> caseTyp
+                let casesTyp = getUnionCasesTyp (typ, bindingFlags)
+                let caseTyp = casesTyp.GetNestedType(tagField, bindingFlags) // if this is null then the union is nullary
+
+                match caseTyp with
+                | null -> null
+                | _ when caseTyp.IsGenericTypeDefinition -> caseTyp.MakeGenericType(casesTyp.GetGenericArguments())
+                | _ -> caseTyp
 
     let getUnionTagConverter (typ: Type, bindingFlags) =
-        if isOptionType typ then (fun tag -> match tag with 0 -> "None" | 1 -> "Some" | _ -> invalidArg "tag" (SR.GetString (SR.outOfRange)))
-        elif isListType typ then (fun tag -> match tag with  0 -> "Empty" | 1 -> "Cons" | _ -> invalidArg "tag" (SR.GetString (SR.outOfRange)))
+        if isOptionType typ then
+            (fun tag ->
+                match tag with
+                | 0 -> "None"
+                | 1 -> "Some"
+                | _ -> invalidArg "tag" (SR.GetString(SR.outOfRange)))
+        elif isListType typ then
+            (fun tag ->
+                match tag with
+                | 0 -> "Empty"
+                | 1 -> "Cons"
+                | _ -> invalidArg "tag" (SR.GetString(SR.outOfRange)))
         else
-          let tagfieldmap = getUnionTypeTagNameMap (typ, bindingFlags) |> Map.ofSeq
-          (fun tag -> tagfieldmap.[tag])
+            let tagfieldmap = getUnionTypeTagNameMap (typ, bindingFlags) |> Map.ofSeq
+            (fun tag -> tagfieldmap.[tag])
 
     let isUnionType (typ: Type, bindingFlags: BindingFlags) =
-        isOptionType typ ||
-        isListType typ ||
-        match tryFindSourceConstructFlagsOfType typ with
-        | None -> false
-        | Some flags ->
-          (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.SumType &&
-          // We see private representations only if BindingFlags.NonPublic is set
-          (if (flags &&& SourceConstructFlags.NonPublicRepresentation) <> enum 0 then
-              (bindingFlags &&& BindingFlags.NonPublic) <> enum 0
-           else
-              true)
+        isOptionType typ
+        || isListType typ
+        || match tryFindSourceConstructFlagsOfType typ with
+           | None -> false
+           | Some flags ->
+               (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.SumType
+               &&
+               // We see private representations only if BindingFlags.NonPublic is set
+               (if (flags &&& SourceConstructFlags.NonPublicRepresentation) <> enum 0 then
+                    (bindingFlags &&& BindingFlags.NonPublic) <> enum 0
+                else
+                    true)
 
     // Check the base type - if it is also an F# type then
     // for the moment we know it is a Discriminated Union
     let isConstructorRepr (typ, bindingFlags) =
-        let rec get typ = isUnionType (typ, bindingFlags) || match typ.BaseType with null -> false | b -> get b
+        let rec get typ =
+            isUnionType (typ, bindingFlags)
+            || match typ.BaseType with
+               | null -> false
+               | b -> get b
+
         get typ
 
     let unionTypeOfUnionCaseType (typ, bindingFlags) =
-        let rec get typ = if isUnionType (typ, bindingFlags) then typ else match typ.BaseType with null -> typ | b -> get b
+        let rec get typ =
+            if isUnionType (typ, bindingFlags) then
+                typ
+            else
+                match typ.BaseType with
+                | null -> typ
+                | b -> get b
+
         get typ
 
     let fieldsPropsOfUnionCase (typ, tag, bindingFlags) =
         if isOptionType typ then
             match tag with
-            | 0 (* None *) -> getInstancePropertyInfos (typ, [| |], bindingFlags)
-            | 1 (* Some *) -> getInstancePropertyInfos (typ, [| "Value" |], bindingFlags)
+            | 0 (* None *)  -> getInstancePropertyInfos (typ, [||], bindingFlags)
+            | 1 (* Some *)  -> getInstancePropertyInfos (typ, [| "Value" |], bindingFlags)
             | _ -> failwith "fieldsPropsOfUnionCase"
         elif isListType typ then
             match tag with
-            | 0 (* Nil *)  -> getInstancePropertyInfos (typ, [| |], bindingFlags)
-            | 1 (* Cons *) -> getInstancePropertyInfos (typ, [| "Head"; "Tail" |], bindingFlags)
+            | 0 (* Nil *)  -> getInstancePropertyInfos (typ, [||], bindingFlags)
+            | 1 (* Cons *)  -> getInstancePropertyInfos (typ, [| "Head"; "Tail" |], bindingFlags)
             | _ -> failwith "fieldsPropsOfUnionCase"
         else
             // Lookup the type holding the fields for the union case
             let caseTyp = getUnionCaseTyp (typ, tag, bindingFlags)
-            let caseTyp = match caseTyp with null ->  typ | _ -> caseTyp
+
+            let caseTyp =
+                match caseTyp with
+                | null -> typ
+                | _ -> caseTyp
+
             caseTyp.GetProperties(instancePropertyFlags ||| bindingFlags)
             |> Array.filter isFieldProperty
             |> Array.filter (fun prop -> variantNumberOfMember prop = tag)
             |> sortFreshArray (fun p1 p2 -> compare (sequenceNumberOfMember p1) (sequenceNumberOfMember p2))
 
-
     let getUnionCaseRecordReader (typ: Type, tag: int, bindingFlags) =
         let props = fieldsPropsOfUnionCase (typ, tag, bindingFlags)
-        (fun (obj: obj) -> props |> Array.map (fun prop -> prop.GetValue (obj, bindingFlags, null, null, null)))
+
+        (fun (obj: obj) ->
+            props
+            |> Array.map (fun prop -> prop.GetValue(obj, bindingFlags, null, null, null)))
 
     let getUnionCaseRecordReaderCompiled (typ: Type, tag: int, bindingFlags) =
         let props = fieldsPropsOfUnionCase (typ, tag, bindingFlags)
@@ -445,29 +521,41 @@ module internal Impl =
 
     let getUnionTagReader (typ: Type, bindingFlags) : (obj -> int) =
         if isOptionType typ then
-            (fun (obj: obj) -> match obj with null -> 0 | _ -> 1)
+            (fun (obj: obj) ->
+                match obj with
+                | null -> 0
+                | _ -> 1)
         else
             let tagMap = getUnionTypeTagNameMap (typ, bindingFlags)
+
             if tagMap.Length <= 1 then
                 (fun (_obj: obj) -> 0)
             else
                 match getInstancePropertyReader (typ, "Tag", bindingFlags) with
                 | Some reader -> (fun (obj: obj) -> reader obj :?> int)
                 | None ->
-                    let m2b = typ.GetMethod("GetTag", BindingFlags.Static ||| bindingFlags, null, [| typ |], null)
-                    (fun (obj: obj) -> m2b.Invoke(null, [|obj|]) :?> int)
+                    let m2b =
+                        typ.GetMethod("GetTag", BindingFlags.Static ||| bindingFlags, null, [| typ |], null)
+
+                    (fun (obj: obj) -> m2b.Invoke(null, [| obj |]) :?> int)
 
     let getUnionTagReaderCompiled (typ: Type, bindingFlags) : (obj -> int) =
         if isOptionType typ then
-            (fun (obj: obj) -> match obj with null -> 0 | _ -> 1)
+            (fun (obj: obj) ->
+                match obj with
+                | null -> 0
+                | _ -> 1)
         else
             let tagMap = getUnionTypeTagNameMap (typ, bindingFlags)
+
             if tagMap.Length <= 1 then
                 (fun (_obj: obj) -> 0)
             else
                 match getInstancePropertyInfo (typ, "Tag", bindingFlags) with
                 | null ->
-                    let m2b = typ.GetMethod("GetTag", BindingFlags.Static ||| bindingFlags, null, [| typ |], null)
+                    let m2b =
+                        typ.GetMethod("GetTag", BindingFlags.Static ||| bindingFlags, null, [| typ |], null)
+
                     compileUnionTagReaderFunc(Choice1Of2 m2b).Invoke
                 | info -> compileUnionTagReaderFunc(Choice2Of2 info).Invoke
 
@@ -481,19 +569,22 @@ module internal Impl =
 
     let getUnionCaseConstructorMethod (typ: Type, tag: int, bindingFlags) =
         let constrname = getUnionTagConverter (typ, bindingFlags) tag
+
         let methname =
-            if isUnionCaseNullary (typ, tag, bindingFlags) then "get_" + constrname
-            elif isListType typ || isOptionType typ then constrname
-            else "New" + constrname
+            if isUnionCaseNullary (typ, tag, bindingFlags) then
+                "get_" + constrname
+            elif isListType typ || isOptionType typ then
+                constrname
+            else
+                "New" + constrname
 
-        match typ.GetMethod(methname, BindingFlags.Static  ||| bindingFlags) with
-        | null -> invalidOp (String.Format (SR.GetString (SR.constructorForUnionCaseNotFound), methname))
+        match typ.GetMethod(methname, BindingFlags.Static ||| bindingFlags) with
+        | null -> invalidOp (String.Format(SR.GetString(SR.constructorForUnionCaseNotFound), methname))
         | meth -> meth
 
     let getUnionCaseConstructor (typ: Type, tag: int, bindingFlags) =
         let meth = getUnionCaseConstructorMethod (typ, tag, bindingFlags)
-        (fun args ->
-            meth.Invoke(null, BindingFlags.Static ||| BindingFlags.InvokeMethod ||| bindingFlags, null, args, null))
+        (fun args -> meth.Invoke(null, BindingFlags.Static ||| BindingFlags.InvokeMethod ||| bindingFlags, null, args, null))
 
     let getUnionCaseConstructorCompiled (typ: Type, tag: int, bindingFlags) =
         let meth = getUnionCaseConstructorMethod (typ, tag, bindingFlags)
@@ -501,51 +592,56 @@ module internal Impl =
 
     let checkUnionType (unionType, bindingFlags) =
         checkNonNull "unionType" unionType
+
         if not (isUnionType (unionType, bindingFlags)) then
             if isUnionType (unionType, bindingFlags ||| BindingFlags.NonPublic) then
-                invalidArg "unionType" (String.Format (SR.GetString (SR.privateUnionType), unionType.FullName))
+                invalidArg "unionType" (String.Format(SR.GetString(SR.privateUnionType), unionType.FullName))
             else
-                invalidArg "unionType" (String.Format (SR.GetString (SR.notAUnionType), unionType.FullName))
+                invalidArg "unionType" (String.Format(SR.GetString(SR.notAUnionType), unionType.FullName))
 
     //-----------------------------------------------------------------
     // TUPLE DECOMPILATION
     let tupleNames =
-        [| "System.Tuple`1"
-           "System.Tuple`2"
-           "System.Tuple`3"
-           "System.Tuple`4"
-           "System.Tuple`5"
-           "System.Tuple`6"
-           "System.Tuple`7"
-           "System.Tuple`8"
-           "System.Tuple"
-           "System.ValueTuple`1"
-           "System.ValueTuple`2"
-           "System.ValueTuple`3"
-           "System.ValueTuple`4"
-           "System.ValueTuple`5"
-           "System.ValueTuple`6"
-           "System.ValueTuple`7"
-           "System.ValueTuple`8"
-           "System.ValueTuple" |]
-
-    let simpleTupleNames = 
-        [| "Tuple`1"
-           "Tuple`2"
-           "Tuple`3"
-           "Tuple`4"
-           "Tuple`5"
-           "Tuple`6"
-           "Tuple`7"
-           "Tuple`8"
-           "ValueTuple`1"
-           "ValueTuple`2"
-           "ValueTuple`3"
-           "ValueTuple`4"
-           "ValueTuple`5"
-           "ValueTuple`6"
-           "ValueTuple`7"
-           "ValueTuple`8" |]
+        [|
+            "System.Tuple`1"
+            "System.Tuple`2"
+            "System.Tuple`3"
+            "System.Tuple`4"
+            "System.Tuple`5"
+            "System.Tuple`6"
+            "System.Tuple`7"
+            "System.Tuple`8"
+            "System.Tuple"
+            "System.ValueTuple`1"
+            "System.ValueTuple`2"
+            "System.ValueTuple`3"
+            "System.ValueTuple`4"
+            "System.ValueTuple`5"
+            "System.ValueTuple`6"
+            "System.ValueTuple`7"
+            "System.ValueTuple`8"
+            "System.ValueTuple"
+        |]
+
+    let simpleTupleNames =
+        [|
+            "Tuple`1"
+            "Tuple`2"
+            "Tuple`3"
+            "Tuple`4"
+            "Tuple`5"
+            "Tuple`6"
+            "Tuple`7"
+            "Tuple`8"
+            "ValueTuple`1"
+            "ValueTuple`2"
+            "ValueTuple`3"
+            "ValueTuple`4"
+            "ValueTuple`5"
+            "ValueTuple`6"
+            "ValueTuple`7"
+            "ValueTuple`8"
+        |]
 
     let isTupleType (typ: Type) =
         // We need to be careful that we only rely typ.IsGenericType, typ.Namespace and typ.Name here.
@@ -554,15 +650,15 @@ module internal Impl =
         // System.Type that don't have functionality such as .IsEnum and .FullName fully implemented.
         // This happens particularly over TypeBuilderInstantiation types in the ProvideTypes implementation of System.Type
         // used in F# type providers.
-        typ.IsGenericType &&
-        typ.Namespace = "System" &&
-        simpleTupleNames |> Seq.exists typ.Name.StartsWith
+        typ.IsGenericType
+        && typ.Namespace = "System"
+        && simpleTupleNames |> Seq.exists typ.Name.StartsWith
 
     let maxTuple = 8
     // Which field holds the nested tuple?
     let tupleEncField = maxTuple - 1
 
-    let dictionaryLock = obj()
+    let dictionaryLock = obj ()
     let refTupleTypes = Dictionary()
     let valueTupleTypes = Dictionary()
 
@@ -583,18 +679,25 @@ module internal Impl =
                 | 6 -> asm.GetType(tupleFullName 6)
                 | 7 -> asm.GetType(tupleFullName 7)
                 | 8 -> asm.GetType(tupleFullName 8)
-                | _ -> invalidArg "tys" (SR.GetString (SR.invalidTupleTypes))
+                | _ -> invalidArg "tys" (SR.GetString(SR.invalidTupleTypes))
+
+            let tables =
+                if isStruct then
+                    valueTupleTypes
+                else
+                    refTupleTypes
 
-            let tables = if isStruct then valueTupleTypes else refTupleTypes
             match lock dictionaryLock (fun () -> tables.TryGetValue asm) with
             | false, _ ->
                 // the Dictionary<>s here could be ConcurrentDictionary<>'s, but then
                 // that would lock while initializing the Type array (maybe not an issue)
                 let mutable a = Array.init 8 (fun i -> makeIt (i + 1))
+
                 lock dictionaryLock (fun () ->
                     match tables.TryGetValue asm with
                     | true, t -> a <- t
                     | false, _ -> tables.Add(asm, a))
+
                 a
             | true, t -> t
 
@@ -607,18 +710,20 @@ module internal Impl =
         | 6 -> table.[5].MakeGenericType tys
         | 7 -> table.[6].MakeGenericType tys
         | n when n >= maxTuple ->
-            let tysA = tys.[0..tupleEncField-1]
-            let tysB = tys.[maxTuple-1..]
+            let tysA = tys.[0 .. tupleEncField - 1]
+            let tysB = tys.[maxTuple - 1 ..]
             let tyB = mkTupleType isStruct asm tysB
             table.[7].MakeGenericType(Array.append tysA [| tyB |])
-        | _ -> invalidArg "tys" (SR.GetString (SR.invalidTupleTypes))
+        | _ -> invalidArg "tys" (SR.GetString(SR.invalidTupleTypes))
 
     let rec getTupleTypeInfo (typ: Type) =
         if not (isTupleType typ) then
-            invalidArg "typ" (String.Format (SR.GetString (SR.notATupleType), typ.FullName))
+            invalidArg "typ" (String.Format(SR.GetString(SR.notATupleType), typ.FullName))
+
         let tyargs = typ.GetGenericArguments()
+
         if tyargs.Length = maxTuple then
-            let tysA = tyargs.[0..tupleEncField-1]
+            let tysA = tyargs.[0 .. tupleEncField - 1]
             let tyB = tyargs.[tupleEncField]
             Array.append tysA (getTupleTypeInfo tyB)
         else
@@ -632,17 +737,28 @@ module internal Impl =
         //   Item1, Item2, ..., Item, Rest
         // The PropertyInfo may not come back in order, so ensure ordering here.
 #if !NETSTANDARD
-        assert(maxTuple < 10) // Alphasort will only works for upto 9 items: Item1, Item10, Item2, Item3, ..., Item9, Rest
+        assert (maxTuple < 10) // Alphasort will only works for upto 9 items: Item1, Item10, Item2, Item3, ..., Item9, Rest
 #endif
         let props = props |> Array.sortBy (fun p -> p.Name) // they are not always in alphabetic order
 #if !NETSTANDARD
-        assert(props.Length <= maxTuple)
-        assert(let haveNames   = props |> Array.map (fun p -> p.Name)
-               let expectNames = Array.init props.Length (fun i -> let j = i+1 // index j = 1, 2, .., props.Length <= maxTuple
-                                                                   if   j Array.map (fun p -> p.Name)
+
+             let expectNames =
+                 Array.init props.Length (fun i ->
+                     let j = i + 1 // index j = 1, 2, .., props.Length <= maxTuple
+
+                     if j < maxTuple then
+                         "Item" + string j
+                     elif j = maxTuple then
+                         "Rest"
+                     else
+                         (assert false
+                          "")) // dead code under prior assert, props.Length <= maxTuple
+
+             haveNames = expectNames)
 #endif
         props
 
@@ -654,43 +770,60 @@ module internal Impl =
         //   Item1, Item2, ..., Item, Rest
         // The PropertyInfo may not come back in order, so ensure ordering here.
 #if !NETSTANDARD
-        assert(maxTuple < 10) // Alphasort will only works for upto 9 items: Item1, Item10, Item2, Item3, ..., Item9, Rest
+        assert (maxTuple < 10) // Alphasort will only works for upto 9 items: Item1, Item10, Item2, Item3, ..., Item9, Rest
 #endif
         let fields = fields |> Array.sortBy (fun fi -> fi.Name) // they are not always in alphabetic order
 #if !NETSTANDARD
-        assert(fields.Length <= maxTuple)
-        assert(let haveNames   = fields |> Array.map (fun fi -> fi.Name)
-               let expectNames = Array.init fields.Length (fun i -> let j = i+1 // index j = 1, 2, .., fields.Length <= maxTuple
-                                                                    if   j Array.map (fun fi -> fi.Name)
+
+             let expectNames =
+                 Array.init fields.Length (fun i ->
+                     let j = i + 1 // index j = 1, 2, .., fields.Length <= maxTuple
+
+                     if j < maxTuple then
+                         "Item" + string j
+                     elif j = maxTuple then
+                         "Rest"
+                     else
+                         (assert false
+                          "")) // dead code under prior assert, props.Length <= maxTuple
+
+             haveNames = expectNames)
 #endif
         fields
 
     let getTupleConstructorMethod (typ: Type) =
         let ctor =
             if typ.IsValueType then
-                let fields = typ.GetFields (instanceFieldFlags ||| BindingFlags.Public) |> orderTupleFields
+                let fields =
+                    typ.GetFields(instanceFieldFlags ||| BindingFlags.Public) |> orderTupleFields
+
                 typ.GetConstructor(BindingFlags.Public ||| BindingFlags.Instance, null, fields |> Array.map (fun fi -> fi.FieldType), null)
             else
                 let props = typ.GetProperties() |> orderTupleProperties
                 typ.GetConstructor(BindingFlags.Public ||| BindingFlags.Instance, null, props |> Array.map (fun p -> p.PropertyType), null)
+
         match ctor with
-        | null -> raise (ArgumentException (String.Format (SR.GetString (SR.invalidTupleTypeConstructorNotDefined), typ.FullName)))
+        | null -> raise (ArgumentException(String.Format(SR.GetString(SR.invalidTupleTypeConstructorNotDefined), typ.FullName)))
         | _ -> ()
+
         ctor
 
-    let getTupleCtor(typ: Type) =
-          let ctor = getTupleConstructorMethod typ
-          (fun (args: obj[]) ->
-              ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public, null, args, null))
+    let getTupleCtor (typ: Type) =
+        let ctor = getTupleConstructorMethod typ
+        (fun (args: obj[]) -> ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public, null, args, null))
 
     let getTupleElementAccessors (typ: Type) =
         if typ.IsValueType then
-            Choice1Of2 (typ.GetFields (instanceFieldFlags ||| BindingFlags.Public) |> orderTupleFields)
+            Choice1Of2(typ.GetFields(instanceFieldFlags ||| BindingFlags.Public) |> orderTupleFields)
         else
-            Choice2Of2 (typ.GetProperties (instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties)
+            Choice2Of2(
+                typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public)
+                |> orderTupleProperties
+            )
 
     let rec getTupleReader (typ: Type) =
         let etys = typ.GetGenericArguments()
@@ -698,32 +831,37 @@ module internal Impl =
         let reader =
             match getTupleElementAccessors typ with
             | Choice1Of2 fi -> fun obj -> fi |> Array.map (fun f -> f.GetValue obj)
-            | Choice2Of2 pi -> fun obj -> pi |> Array.map (fun p -> p.GetValue (obj, null))
-        if etys.Length < maxTuple
-        then reader
+            | Choice2Of2 pi -> fun obj -> pi |> Array.map (fun p -> p.GetValue(obj, null))
+
+        if etys.Length < maxTuple then
+            reader
         else
             let tyBenc = etys.[tupleEncField]
             let reader2 = getTupleReader tyBenc
+
             (fun obj ->
                 let directVals = reader obj
                 let encVals = reader2 directVals.[tupleEncField]
-                Array.append directVals.[0..tupleEncField-1] encVals)
+                Array.append directVals.[0 .. tupleEncField - 1] encVals)
 
     let rec getTupleConstructor (typ: Type) =
         let etys = typ.GetGenericArguments()
-        let maker1 =  getTupleCtor typ
-        if etys.Length < maxTuple
-        then maker1
+        let maker1 = getTupleCtor typ
+
+        if etys.Length < maxTuple then
+            maker1
         else
             let tyBenc = etys.[tupleEncField]
             let maker2 = getTupleConstructor tyBenc
+
             (fun (args: obj[]) ->
                 let encVal = maker2 args.[tupleEncField..]
-                maker1 (Array.append args.[0..tupleEncField-1] [| encVal |]))
+                maker1 (Array.append args.[0 .. tupleEncField - 1] [| encVal |]))
 
     let getTupleConstructorInfo (typ: Type) =
         let etys = typ.GetGenericArguments()
-        let maker1 =  getTupleConstructorMethod typ
+        let maker1 = getTupleConstructorMethod typ
+
         if etys.Length < maxTuple then
             maker1, None
         else
@@ -731,81 +869,97 @@ module internal Impl =
 
     let getTupleReaderInfo (typ: Type, index: int) =
         if index < 0 then
-            invalidArg "index" (String.Format (SR.GetString (SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+            invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
 
         let get index =
             if typ.IsValueType then
-                let props = typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties
+                let props =
+                    typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public)
+                    |> orderTupleProperties
+
                 if index >= props.Length then
-                    invalidArg "index" (String.Format (SR.GetString (SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+                    invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+
                 props.[index]
             else
-                let props = typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties
+                let props =
+                    typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public)
+                    |> orderTupleProperties
+
                 if index >= props.Length then
-                    invalidArg "index" (String.Format (SR.GetString (SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+                    invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+
                 props.[index]
 
         if index < tupleEncField then
             get index, None
         else
             let etys = typ.GetGenericArguments()
-            get tupleEncField, Some(etys.[tupleEncField], index-(maxTuple-1))
+            get tupleEncField, Some(etys.[tupleEncField], index - (maxTuple - 1))
 
     let getFunctionTypeInfo (typ: Type) =
-      if not (isFunctionType typ) then
-          invalidArg "typ" (String.Format (SR.GetString (SR.notAFunctionType), typ.FullName))
-      let tyargs = typ.GetGenericArguments()
-      tyargs.[0], tyargs.[1]
+        if not (isFunctionType typ) then
+            invalidArg "typ" (String.Format(SR.GetString(SR.notAFunctionType), typ.FullName))
+
+        let tyargs = typ.GetGenericArguments()
+        tyargs.[0], tyargs.[1]
 
     let isModuleType (typ: Type) =
-      match tryFindSourceConstructFlagsOfType typ with
-      | None -> false
-      | Some flags ->
-        (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.Module
+        match tryFindSourceConstructFlagsOfType typ with
+        | None -> false
+        | Some flags -> (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.Module
 
     let rec isClosureRepr typ =
-        isFunctionType typ ||
-        (match typ.BaseType with null -> false | bty -> isClosureRepr bty)
+        isFunctionType typ
+        || (match typ.BaseType with
+            | null -> false
+            | bty -> isClosureRepr bty)
 
     let isRecordType (typ: Type, bindingFlags: BindingFlags) =
-      match tryFindSourceConstructFlagsOfType typ with
-      | None -> false
-      | Some flags ->
-        (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.RecordType &&
-        // We see private representations only if BindingFlags.NonPublic is set
-        (if (flags &&& SourceConstructFlags.NonPublicRepresentation) <> enum 0 then
-            (bindingFlags &&& BindingFlags.NonPublic) <> enum 0
-         else
-            true)
-
-    let fieldPropsOfRecordType(typ: Type, bindingFlags) =
-      typ.GetProperties(instancePropertyFlags ||| bindingFlags)
-      |> Array.filter isFieldProperty
-      |> sortFreshArray (fun p1 p2 -> compare (sequenceNumberOfMember p1) (sequenceNumberOfMember p2))
-
-    let getRecordReader(typ: Type, bindingFlags) =
-        let props = fieldPropsOfRecordType(typ, bindingFlags)
-        (fun (obj: obj) -> props |> Array.map (fun prop -> prop.GetValue (obj, null)))
-
-    let getRecordReaderCompiled(typ: Type, bindingFlags) =
-        let props = fieldPropsOfRecordType(typ, bindingFlags)
+        match tryFindSourceConstructFlagsOfType typ with
+        | None -> false
+        | Some flags ->
+            (flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.RecordType
+            &&
+            // We see private representations only if BindingFlags.NonPublic is set
+            (if (flags &&& SourceConstructFlags.NonPublicRepresentation) <> enum 0 then
+                 (bindingFlags &&& BindingFlags.NonPublic) <> enum 0
+             else
+                 true)
+
+    let fieldPropsOfRecordType (typ: Type, bindingFlags) =
+        typ.GetProperties(instancePropertyFlags ||| bindingFlags)
+        |> Array.filter isFieldProperty
+        |> sortFreshArray (fun p1 p2 -> compare (sequenceNumberOfMember p1) (sequenceNumberOfMember p2))
+
+    let getRecordReader (typ: Type, bindingFlags) =
+        let props = fieldPropsOfRecordType (typ, bindingFlags)
+        (fun (obj: obj) -> props |> Array.map (fun prop -> prop.GetValue(obj, null)))
+
+    let getRecordReaderCompiled (typ: Type, bindingFlags) =
+        let props = fieldPropsOfRecordType (typ, bindingFlags)
         compileRecordOrUnionCaseReaderFunc(typ, props).Invoke
 
-    let getRecordConstructorMethod(typ: Type, bindingFlags) =
-        let props = fieldPropsOfRecordType(typ, bindingFlags)
-        let ctor = typ.GetConstructor(BindingFlags.Instance ||| bindingFlags, null, props |> Array.map (fun p -> p.PropertyType), null)
+    let getRecordConstructorMethod (typ: Type, bindingFlags) =
+        let props = fieldPropsOfRecordType (typ, bindingFlags)
+
+        let ctor =
+            typ.GetConstructor(BindingFlags.Instance ||| bindingFlags, null, props |> Array.map (fun p -> p.PropertyType), null)
+
         match ctor with
-        | null -> raise <| ArgumentException (String.Format (SR.GetString (SR.invalidRecordTypeConstructorNotDefined), typ.FullName))
+        | null ->
+            raise
+            <| ArgumentException(String.Format(SR.GetString(SR.invalidRecordTypeConstructorNotDefined), typ.FullName))
         | _ -> ()
+
         ctor
 
-    let getRecordConstructor(typ: Type, bindingFlags) =
-        let ctor = getRecordConstructorMethod(typ, bindingFlags)
-        (fun (args: obj[]) ->
-            ctor.Invoke(BindingFlags.InvokeMethod  ||| BindingFlags.Instance ||| bindingFlags, null, args, null))
+    let getRecordConstructor (typ: Type, bindingFlags) =
+        let ctor = getRecordConstructorMethod (typ, bindingFlags)
+        (fun (args: obj[]) -> ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| bindingFlags, null, args, null))
 
-    let getRecordConstructorCompiled(typ: Type, bindingFlags) =
-        let ctor = getRecordConstructorMethod(typ, bindingFlags)
+    let getRecordConstructorCompiled (typ: Type, bindingFlags) =
+        let ctor = getRecordConstructorMethod (typ, bindingFlags)
         compileRecordConstructorFunc(ctor).Invoke
 
     /// EXCEPTION DECOMPILATION
@@ -815,20 +969,31 @@ module internal Impl =
         match tryFindSourceConstructFlagsOfType typ with
         | None -> false
         | Some flags ->
-          ((flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.Exception) &&
-          // We see private representations only if BindingFlags.NonPublic is set
-          (if (flags &&& SourceConstructFlags.NonPublicRepresentation) <> enum 0 then
-              (bindingFlags &&& BindingFlags.NonPublic) <> enum 0
-           else
-              true)
+            ((flags &&& SourceConstructFlags.KindMask) = SourceConstructFlags.Exception)
+            &&
+            // We see private representations only if BindingFlags.NonPublic is set
+            (if (flags &&& SourceConstructFlags.NonPublicRepresentation) <> enum 0 then
+                 (bindingFlags &&& BindingFlags.NonPublic) <> enum 0
+             else
+                 true)
 
     let getTypeOfReprType (typ: Type, bindingFlags) =
-        if isExceptionRepr (typ, bindingFlags) then typ.BaseType
-        elif isConstructorRepr (typ, bindingFlags) then unionTypeOfUnionCaseType(typ, bindingFlags)
+        if isExceptionRepr (typ, bindingFlags) then
+            typ.BaseType
+        elif isConstructorRepr (typ, bindingFlags) then
+            unionTypeOfUnionCaseType (typ, bindingFlags)
         elif isClosureRepr typ then
-          let rec get (typ: Type) = if isFunctionType typ then typ else match typ.BaseType with null -> typ | b -> get b
-          get typ
-        else typ
+            let rec get (typ: Type) =
+                if isFunctionType typ then
+                    typ
+                else
+                    match typ.BaseType with
+                    | null -> typ
+                    | b -> get b
+
+            get typ
+        else
+            typ
 
     //-----------------------------------------------------------------
     // CHECKING ROUTINES
@@ -836,22 +1001,24 @@ module internal Impl =
     let checkExnType (exceptionType, bindingFlags) =
         if not (isExceptionRepr (exceptionType, bindingFlags)) then
             if isExceptionRepr (exceptionType, bindingFlags ||| BindingFlags.NonPublic) then
-                invalidArg "exceptionType" (String.Format (SR.GetString (SR.privateExceptionType), exceptionType.FullName))
+                invalidArg "exceptionType" (String.Format(SR.GetString(SR.privateExceptionType), exceptionType.FullName))
             else
-                invalidArg "exceptionType" (String.Format (SR.GetString (SR.notAnExceptionType), exceptionType.FullName))
+                invalidArg "exceptionType" (String.Format(SR.GetString(SR.notAnExceptionType), exceptionType.FullName))
 
     let checkRecordType (argName, recordType, bindingFlags) =
         checkNonNull argName recordType
-        if not (isRecordType (recordType, bindingFlags) ) then
+
+        if not (isRecordType (recordType, bindingFlags)) then
             if isRecordType (recordType, bindingFlags ||| BindingFlags.NonPublic) then
-                invalidArg argName (String.Format (SR.GetString (SR.privateRecordType), recordType.FullName))
+                invalidArg argName (String.Format(SR.GetString(SR.privateRecordType), recordType.FullName))
             else
-                invalidArg argName (String.Format (SR.GetString (SR.notARecordType), recordType.FullName))
+                invalidArg argName (String.Format(SR.GetString(SR.notARecordType), recordType.FullName))
 
-    let checkTupleType(argName, (tupleType: Type)) =
+    let checkTupleType (argName, (tupleType: Type)) =
         checkNonNull argName tupleType
+
         if not (isTupleType tupleType) then
-            invalidArg argName (String.Format (SR.GetString (SR.notATupleType), tupleType.FullName))
+            invalidArg argName (String.Format(SR.GetString(SR.notATupleType), tupleType.FullName))
 
 []
 type UnionCaseInfo(typ: System.Type, tag: int) =
@@ -859,16 +1026,18 @@ type UnionCaseInfo(typ: System.Type, tag: int) =
     // Cache the tag -> name map
     let mutable names = None
 
-    let getMethInfo() = getUnionCaseConstructorMethod (typ, tag, BindingFlags.Public ||| BindingFlags.NonPublic)
+    let getMethInfo () =
+        getUnionCaseConstructorMethod (typ, tag, BindingFlags.Public ||| BindingFlags.NonPublic)
 
     member _.Name =
         match names with
         | None ->
-            let conv = getUnionTagConverter (typ, BindingFlags.Public ||| BindingFlags.NonPublic)
+            let conv =
+                getUnionTagConverter (typ, BindingFlags.Public ||| BindingFlags.NonPublic)
+
             names <- Some conv
             conv tag
-        | Some conv ->
-            conv tag
+        | Some conv -> conv tag
 
     member _.DeclaringType = typ
 
@@ -886,9 +1055,11 @@ type UnionCaseInfo(typ: System.Type, tag: int) =
 
     member _.Tag = tag
 
-    override x.ToString() = typ.Name + "." + x.Name
+    override x.ToString() =
+        typ.Name + "." + x.Name
 
-    override x.GetHashCode() = typ.GetHashCode() + tag
+    override x.GetHashCode() =
+        typ.GetHashCode() + tag
 
     override _.Equals(obj: obj) =
         match obj with
@@ -907,74 +1078,94 @@ type FSharpType =
         checkNonNull "typ" typ
         isRecordType (typ, bindingFlags)
 
-    static member IsUnion (typ: Type, ?bindingFlags) =
+    static member IsUnion(typ: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "typ" typ
         let typ = getTypeOfReprType (typ, BindingFlags.Public ||| BindingFlags.NonPublic)
         isUnionType (typ, bindingFlags)
 
-    static member IsFunction (typ: Type) =
+    static member IsFunction(typ: Type) =
         checkNonNull "typ" typ
         let typ = getTypeOfReprType (typ, BindingFlags.Public ||| BindingFlags.NonPublic)
         isFunctionType typ
 
-    static member IsModule (typ: Type) =
+    static member IsModule(typ: Type) =
         checkNonNull "typ" typ
         isModuleType typ
 
-    static member MakeFunctionType (domain: Type, range: Type) =
+    static member MakeFunctionType(domain: Type, range: Type) =
         checkNonNull "domain" domain
         checkNonNull "range" range
         func.MakeGenericType [| domain; range |]
 
-    static member MakeTupleType (types: Type[]) =
+    static member MakeTupleType(types: Type[]) =
         checkNonNull "types" types
 
         // No assembly passed therefore just get framework local version of Tuple
         let asm = typeof.Assembly
-        if types |> Array.exists (function null -> true | _ -> false) then
-            invalidArg "types" (SR.GetString (SR.nullsNotAllowedInArray))
+
+        if types
+           |> Array.exists (function
+               | null -> true
+               | _ -> false) then
+            invalidArg "types" (SR.GetString(SR.nullsNotAllowedInArray))
+
         mkTupleType false asm types
 
-    static member MakeTupleType (asm: Assembly, types: Type[])  =
+    static member MakeTupleType(asm: Assembly, types: Type[]) =
         checkNonNull "types" types
-        if types |> Array.exists (function null -> true | _ -> false) then
-             invalidArg "types" (SR.GetString (SR.nullsNotAllowedInArray))
+
+        if types
+           |> Array.exists (function
+               | null -> true
+               | _ -> false) then
+            invalidArg "types" (SR.GetString(SR.nullsNotAllowedInArray))
+
         mkTupleType false asm types
 
-    static member MakeStructTupleType (asm: Assembly, types: Type[]) =
+    static member MakeStructTupleType(asm: Assembly, types: Type[]) =
         checkNonNull "types" types
-        if types |> Array.exists (function null -> true | _ -> false) then
-             invalidArg "types" (SR.GetString (SR.nullsNotAllowedInArray))
+
+        if types
+           |> Array.exists (function
+               | null -> true
+               | _ -> false) then
+            invalidArg "types" (SR.GetString(SR.nullsNotAllowedInArray))
+
         mkTupleType true asm types
 
-    static member GetTupleElements (tupleType: Type) =
-        checkTupleType("tupleType", tupleType)
+    static member GetTupleElements(tupleType: Type) =
+        checkTupleType ("tupleType", tupleType)
         getTupleTypeInfo tupleType
 
-    static member GetFunctionElements (functionType: Type) =
+    static member GetFunctionElements(functionType: Type) =
         checkNonNull "functionType" functionType
-        let functionType = getTypeOfReprType (functionType, BindingFlags.Public ||| BindingFlags.NonPublic)
+
+        let functionType =
+            getTypeOfReprType (functionType, BindingFlags.Public ||| BindingFlags.NonPublic)
+
         getFunctionTypeInfo functionType
 
-    static member GetRecordFields (recordType: Type, ?bindingFlags) =
+    static member GetRecordFields(recordType: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkRecordType ("recordType", recordType, bindingFlags)
-        fieldPropsOfRecordType(recordType, bindingFlags)
+        fieldPropsOfRecordType (recordType, bindingFlags)
 
-    static member GetUnionCases (unionType: Type, ?bindingFlags) =
+    static member GetUnionCases(unionType: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "unionType" unionType
         let unionType = getTypeOfReprType (unionType, bindingFlags)
         checkUnionType (unionType, bindingFlags)
-        getUnionTypeTagNameMap(unionType, bindingFlags) |> Array.mapi (fun i _ -> UnionCaseInfo(unionType, i))
 
-    static member IsExceptionRepresentation (exceptionType: Type, ?bindingFlags) =
+        getUnionTypeTagNameMap (unionType, bindingFlags)
+        |> Array.mapi (fun i _ -> UnionCaseInfo(unionType, i))
+
+    static member IsExceptionRepresentation(exceptionType: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "exceptionType" exceptionType
         isExceptionRepr (exceptionType, bindingFlags)
 
-    static member GetExceptionFields (exceptionType: Type, ?bindingFlags) =
+    static member GetExceptionFields(exceptionType: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "exceptionType" exceptionType
         checkExnType (exceptionType, bindingFlags)
@@ -982,13 +1173,14 @@ type FSharpType =
 
 type DynamicFunction<'T1, 'T2>() =
     inherit FSharpFunc obj, obj>()
+
     override _.Invoke(impl: obj -> obj) : obj =
-        box<('T1 -> 'T2)> (fun inp -> unbox<'T2>(impl (box<'T1>(inp))))
+        box<('T1 -> 'T2)> (fun inp -> unbox<'T2> (impl (box<'T1> (inp))))
 
 []
 type FSharpValue =
 
-    static member MakeRecord (recordType: Type, values, ?bindingFlags) =
+    static member MakeRecord(recordType: Type, values, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkRecordType ("recordType", recordType, bindingFlags)
         getRecordConstructor (recordType, bindingFlags) values
@@ -997,19 +1189,23 @@ type FSharpValue =
         checkNonNull "info" info
         checkNonNull "record" record
         let reprty = record.GetType()
+
         if not (isRecordType (reprty, BindingFlags.Public ||| BindingFlags.NonPublic)) then
-            invalidArg "record" (SR.GetString (SR.objIsNotARecord))
-        info.GetValue (record, null)
+            invalidArg "record" (SR.GetString(SR.objIsNotARecord))
+
+        info.GetValue(record, null)
 
-    static member GetRecordFields (record: obj, ?bindingFlags) =
+    static member GetRecordFields(record: obj, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "record" record
         let typ = record.GetType()
+
         if not (isRecordType (typ, bindingFlags)) then
-            invalidArg "record" (SR.GetString (SR.objIsNotARecord))
+            invalidArg "record" (SR.GetString(SR.objIsNotARecord))
+
         getRecordReader (typ, bindingFlags) record
 
-    static member PreComputeRecordFieldReader(info: PropertyInfo): obj -> obj =
+    static member PreComputeRecordFieldReader(info: PropertyInfo) : obj -> obj =
         checkNonNull "info" info
         compilePropGetterFunc(info).Invoke
 
@@ -1026,64 +1222,74 @@ type FSharpValue =
     static member PreComputeRecordConstructorInfo(recordType: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkRecordType ("recordType", recordType, bindingFlags)
-        getRecordConstructorMethod(recordType, bindingFlags)
+        getRecordConstructorMethod (recordType, bindingFlags)
 
-    static member MakeFunction(functionType: Type, implementation:(obj->obj)) =
+    static member MakeFunction(functionType: Type, implementation: (obj -> obj)) =
         checkNonNull "functionType" functionType
+
         if not (isFunctionType functionType) then
-            invalidArg "functionType" (String.Format (SR.GetString (SR.notAFunctionType), functionType.FullName))
+            invalidArg "functionType" (String.Format(SR.GetString(SR.notAFunctionType), functionType.FullName))
+
         checkNonNull "implementation" implementation
         let domain, range = getFunctionTypeInfo functionType
         let dynCloMakerTy = typedefof>
         let saverTy = dynCloMakerTy.MakeGenericType [| domain; range |]
         let o = Activator.CreateInstance saverTy
-        let (f : (obj -> obj) -> obj) = downcast o
+        let (f: (obj -> obj) -> obj) = downcast o
         f implementation
 
     static member MakeTuple(tupleElements: obj[], tupleType: Type) =
         checkNonNull "tupleElements" tupleElements
-        checkTupleType("tupleType", tupleType)
+        checkTupleType ("tupleType", tupleType)
         getTupleConstructor tupleType tupleElements
 
     static member GetTupleFields(tuple: obj) = // argument name(s) used in error message
         checkNonNull "tuple" tuple
         let typ = tuple.GetType()
-        if not (isTupleType typ ) then
-            invalidArg "tuple" (String.Format (SR.GetString (SR.notATupleType), tuple.GetType().FullName))
+
+        if not (isTupleType typ) then
+            invalidArg "tuple" (String.Format(SR.GetString(SR.notATupleType), tuple.GetType().FullName))
+
         getTupleReader typ tuple
 
     static member GetTupleField(tuple: obj, index: int) = // argument name(s) used in error message
         checkNonNull "tuple" tuple
         let typ = tuple.GetType()
-        if not (isTupleType typ ) then
-            invalidArg "tuple" (String.Format (SR.GetString (SR.notATupleType), tuple.GetType().FullName))
+
+        if not (isTupleType typ) then
+            invalidArg "tuple" (String.Format(SR.GetString(SR.notATupleType), tuple.GetType().FullName))
+
         let fields = getTupleReader typ tuple
+
         if index < 0 || index >= fields.Length then
-            invalidArg "index" (String.Format (SR.GetString (SR.tupleIndexOutOfRange), tuple.GetType().FullName, index.ToString()))
+            invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), tuple.GetType().FullName, index.ToString()))
+
         fields.[index]
 
-    static member PreComputeTupleReader(tupleType: Type) : (obj -> obj[])  =
-        checkTupleType("tupleType", tupleType)
+    static member PreComputeTupleReader(tupleType: Type) : (obj -> obj[]) =
+        checkTupleType ("tupleType", tupleType)
         (compileTupleReader tupleEncField getTupleElementAccessors tupleType).Invoke
 
     static member PreComputeTuplePropertyInfo(tupleType: Type, index: int) =
-        checkTupleType("tupleType", tupleType)
+        checkTupleType ("tupleType", tupleType)
         getTupleReaderInfo (tupleType, index)
 
     static member PreComputeTupleConstructor(tupleType: Type) =
-        checkTupleType("tupleType", tupleType)
-        (compileTupleConstructor tupleEncField getTupleConstructorMethod tupleType).Invoke
+        checkTupleType ("tupleType", tupleType)
+
+        (compileTupleConstructor tupleEncField getTupleConstructorMethod tupleType)
+            .Invoke
 
     static member PreComputeTupleConstructorInfo(tupleType: Type) =
-        checkTupleType("tupleType", tupleType)
+        checkTupleType ("tupleType", tupleType)
         getTupleConstructorInfo tupleType
 
-    static member MakeUnion(unionCase: UnionCaseInfo, args: obj [], ?bindingFlags) =
+    static member MakeUnion(unionCase: UnionCaseInfo, args: obj[], ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "unionCase" unionCase
         getUnionCaseConstructor (unionCase.DeclaringType, unionCase.Tag, bindingFlags) args
 
-    static member PreComputeUnionConstructor (unionCase: UnionCaseInfo, ?bindingFlags) =
+    static member PreComputeUnionConstructor(unionCase: UnionCaseInfo, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "unionCase" unionCase
         getUnionCaseConstructorCompiled (unionCase.DeclaringType, unionCase.Tag, bindingFlags)
@@ -1095,15 +1301,16 @@ type FSharpValue =
 
     static member GetUnionFields(value: obj, unionType: Type, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
+
         let ensureType (typ: Type, obj: obj) =
-                match typ with
-                | null ->
-                    match obj with
-                    | null -> invalidArg "obj" (SR.GetString (SR.objIsNullAndNoType))
-                    | _ -> obj.GetType()
-                | _ -> typ
+            match typ with
+            | null ->
+                match obj with
+                | null -> invalidArg "obj" (SR.GetString(SR.objIsNullAndNoType))
+                | _ -> obj.GetType()
+            | _ -> typ
 
-        let unionType = ensureType(unionType, value)
+        let unionType = ensureType (unionType, value)
 
         checkNonNull "unionType" unionType
         let unionType = getTypeOfReprType (unionType, bindingFlags)
@@ -1111,7 +1318,7 @@ type FSharpValue =
         checkUnionType (unionType, bindingFlags)
         let tag = getUnionTagReader (unionType, bindingFlags) value
         let flds = getUnionCaseRecordReader (unionType, tag, bindingFlags) value
-        UnionCaseInfo (unionType, tag), flds
+        UnionCaseInfo(unionType, tag), flds
 
     static member PreComputeUnionTagReader(unionType: Type, ?bindingFlags) : (obj -> int) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
@@ -1127,13 +1334,13 @@ type FSharpValue =
         checkUnionType (unionType, bindingFlags)
         getUnionTagMemberInfo (unionType, bindingFlags)
 
-    static member PreComputeUnionReader(unionCase: UnionCaseInfo, ?bindingFlags) : (obj -> obj[])  =
+    static member PreComputeUnionReader(unionCase: UnionCaseInfo, ?bindingFlags) : (obj -> obj[]) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "unionCase" unionCase
         let typ = unionCase.DeclaringType
         getUnionCaseRecordReaderCompiled (typ, unionCase.Tag, bindingFlags)
 
-    static member GetExceptionFields (exn: obj, ?bindingFlags) =
+    static member GetExceptionFields(exn: obj, ?bindingFlags) =
         let bindingFlags = defaultArg bindingFlags BindingFlags.Public
         checkNonNull "exn" exn
         let typ = exn.GetType()
@@ -1144,80 +1351,80 @@ module FSharpReflectionExtensions =
 
     type FSharpType with
 
-        static member GetExceptionFields (exceptionType: Type, ?allowAccessToPrivateRepresentation) =
+        static member GetExceptionFields(exceptionType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpType.GetExceptionFields (exceptionType, bindingFlags)
+            FSharpType.GetExceptionFields(exceptionType, bindingFlags)
 
         static member IsExceptionRepresentation(exceptionType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpType.IsExceptionRepresentation (exceptionType, bindingFlags)
+            FSharpType.IsExceptionRepresentation(exceptionType, bindingFlags)
 
-        static member GetUnionCases (unionType: Type, ?allowAccessToPrivateRepresentation) =
+        static member GetUnionCases(unionType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpType.GetUnionCases (unionType, bindingFlags)
+            FSharpType.GetUnionCases(unionType, bindingFlags)
 
-        static member GetRecordFields (recordType: Type, ?allowAccessToPrivateRepresentation) =
+        static member GetRecordFields(recordType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpType.GetRecordFields (recordType, bindingFlags)
+            FSharpType.GetRecordFields(recordType, bindingFlags)
 
-        static member IsUnion (typ: Type, ?allowAccessToPrivateRepresentation) =
+        static member IsUnion(typ: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpType.IsUnion (typ, bindingFlags)
+            FSharpType.IsUnion(typ, bindingFlags)
 
         static member IsRecord(typ: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpType.IsRecord (typ, bindingFlags)
+            FSharpType.IsRecord(typ, bindingFlags)
 
     type FSharpValue with
+
         static member MakeRecord(recordType: Type, values, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.MakeRecord (recordType, values, bindingFlags)
+            FSharpValue.MakeRecord(recordType, values, bindingFlags)
 
-        static member GetRecordFields (record: obj, ?allowAccessToPrivateRepresentation) =
+        static member GetRecordFields(record: obj, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.GetRecordFields (record, bindingFlags)
+            FSharpValue.GetRecordFields(record, bindingFlags)
 
         static member PreComputeRecordReader(recordType: Type, ?allowAccessToPrivateRepresentation) : (obj -> obj[]) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeRecordReader (recordType, bindingFlags)
+            FSharpValue.PreComputeRecordReader(recordType, bindingFlags)
 
         static member PreComputeRecordConstructor(recordType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeRecordConstructor (recordType, bindingFlags)
+            FSharpValue.PreComputeRecordConstructor(recordType, bindingFlags)
 
         static member PreComputeRecordConstructorInfo(recordType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeRecordConstructorInfo (recordType, bindingFlags)
+            FSharpValue.PreComputeRecordConstructorInfo(recordType, bindingFlags)
 
-        static member MakeUnion(unionCase: UnionCaseInfo, args: obj [], ?allowAccessToPrivateRepresentation) =
+        static member MakeUnion(unionCase: UnionCaseInfo, args: obj[], ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.MakeUnion (unionCase, args, bindingFlags)
+            FSharpValue.MakeUnion(unionCase, args, bindingFlags)
 
-        static member PreComputeUnionConstructor (unionCase: UnionCaseInfo, ?allowAccessToPrivateRepresentation) =
+        static member PreComputeUnionConstructor(unionCase: UnionCaseInfo, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeUnionConstructor (unionCase, bindingFlags)
+            FSharpValue.PreComputeUnionConstructor(unionCase, bindingFlags)
 
         static member PreComputeUnionConstructorInfo(unionCase: UnionCaseInfo, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeUnionConstructorInfo (unionCase, bindingFlags)
+            FSharpValue.PreComputeUnionConstructorInfo(unionCase, bindingFlags)
 
         static member PreComputeUnionTagMemberInfo(unionType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeUnionTagMemberInfo (unionType, bindingFlags)
+            FSharpValue.PreComputeUnionTagMemberInfo(unionType, bindingFlags)
 
         static member GetUnionFields(value: obj, unionType: Type, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.GetUnionFields (value, unionType, bindingFlags)
+            FSharpValue.GetUnionFields(value, unionType, bindingFlags)
 
         static member PreComputeUnionTagReader(unionType: Type, ?allowAccessToPrivateRepresentation) : (obj -> int) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeUnionTagReader (unionType, bindingFlags)
+            FSharpValue.PreComputeUnionTagReader(unionType, bindingFlags)
 
-        static member PreComputeUnionReader(unionCase: UnionCaseInfo, ?allowAccessToPrivateRepresentation) : (obj -> obj[])  =
+        static member PreComputeUnionReader(unionCase: UnionCaseInfo, ?allowAccessToPrivateRepresentation) : (obj -> obj[]) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.PreComputeUnionReader (unionCase, bindingFlags)
+            FSharpValue.PreComputeUnionReader(unionCase, bindingFlags)
 
-        static member GetExceptionFields (exn: obj, ?allowAccessToPrivateRepresentation) =
+        static member GetExceptionFields(exn: obj, ?allowAccessToPrivateRepresentation) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
-            FSharpValue.GetExceptionFields (exn, bindingFlags)
-
+            FSharpValue.GetExceptionFields(exn, bindingFlags)
diff --git a/src/FSharp.Core/result.fs b/src/FSharp.Core/result.fs
index ae9a7ca545a..1f82740fa5f 100644
--- a/src/FSharp.Core/result.fs
+++ b/src/FSharp.Core/result.fs
@@ -6,10 +6,19 @@ namespace Microsoft.FSharp.Core
 module Result =
 
     []
-    let map mapping result = match result with Error e -> Error e | Ok x -> Ok (mapping x)
+    let map mapping result =
+        match result with
+        | Error e -> Error e
+        | Ok x -> Ok(mapping x)
 
     []
-    let mapError mapping result = match result with Error e -> Error (mapping e) | Ok x -> Ok x
+    let mapError mapping result =
+        match result with
+        | Error e -> Error(mapping e)
+        | Ok x -> Ok x
 
     []
-    let bind binder result = match result with Error e -> Error e | Ok x -> binder x
+    let bind binder result =
+        match result with
+        | Error e -> Error e
+        | Ok x -> binder x
diff --git a/src/FSharp.Core/resumable.fs b/src/FSharp.Core/resumable.fs
index 02b896f62fa..3fd88f52c95 100644
--- a/src/FSharp.Core/resumable.fs
+++ b/src/FSharp.Core/resumable.fs
@@ -20,9 +20,9 @@ open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
 open Microsoft.FSharp.Control
 open Microsoft.FSharp.Collections
 
-[]  
+[]
 []
-type NoEagerConstraintApplicationAttribute() = 
+type NoEagerConstraintApplicationAttribute() =
     inherit System.Attribute()
 
 type IResumableStateMachine<'Data> =
@@ -43,26 +43,28 @@ type ResumableStateMachine<'Data> =
     []
     val mutable ResumptionDynamicInfo: ResumptionDynamicInfo<'Data>
 
-    interface IResumableStateMachine<'Data> with 
+    interface IResumableStateMachine<'Data> with
         member sm.ResumptionPoint = sm.ResumptionPoint
-        member sm.Data with get() = sm.Data and set v = sm.Data <- v
 
-    interface IAsyncStateMachine with 
-        
+        member sm.Data
+            with get () = sm.Data
+            and set v = sm.Data <- v
+
+    interface IAsyncStateMachine with
+
         // Used for dynamic execution.  For "__stateMachine" it is replaced.
-        member sm.MoveNext() = 
+        member sm.MoveNext() =
             sm.ResumptionDynamicInfo.MoveNext(&sm)
 
         // Used when dynamic execution.  For "__stateMachine" it is replaced.
-        member sm.SetStateMachine(state) = 
+        member sm.SetStateMachine(state) =
             sm.ResumptionDynamicInfo.SetStateMachine(&sm, state)
 
 and ResumptionFunc<'Data> = delegate of byref> -> bool
 
-and []
-    ResumptionDynamicInfo<'Data>(initial: ResumptionFunc<'Data>) =
-    member val ResumptionFunc: ResumptionFunc<'Data> = initial with get, set 
-    member val ResumptionData: obj = null with get, set 
+and [] ResumptionDynamicInfo<'Data>(initial: ResumptionFunc<'Data>) =
+    member val ResumptionFunc: ResumptionFunc<'Data> = initial with get, set
+    member val ResumptionData: obj = null with get, set
     abstract MoveNext: machine: byref> -> unit
     abstract SetStateMachine: machine: byref> * machineState: IAsyncStateMachine -> unit
 
@@ -78,29 +80,31 @@ type SetStateMachineMethodImpl<'Data> = delegate of byref = delegate of byref> -> 'Result
 
 []
-module StateMachineHelpers = 
+module StateMachineHelpers =
 
     /// Statically determines whether resumable code is being used
     []
     let __useResumableCode<'T> : bool = false
 
     []
-    let __debugPoint (_name: string) : unit = ()
+    let __debugPoint (_name: string) : unit =
+        ()
 
     []
-    let __resumableEntry () : int option = 
+    let __resumableEntry () : int option =
         failwith "__resumableEntry should always be guarded by __useResumableCode and only used in valid state machine implementations"
 
     []
-    let __resumeAt<'T> (programLabel: int) : 'T = 
+    let __resumeAt<'T> (programLabel: int) : 'T =
         ignore programLabel
         failwith "__resumeAt should always be guarded by __useResumableCode and only used in valid state machine implementations"
 
     []
-    let __stateMachine<'Data, 'Result> 
-           (moveNextMethod: MoveNextMethodImpl<'Data>) 
-           (setStateMachineMethod: SetStateMachineMethodImpl<'Data>) 
-           (afterCode: AfterCode<'Data, 'Result>): 'Result =
+    let __stateMachine<'Data, 'Result>
+        (moveNextMethod: MoveNextMethodImpl<'Data>)
+        (setStateMachineMethod: SetStateMachineMethodImpl<'Data>)
+        (afterCode: AfterCode<'Data, 'Result>)
+        : 'Result =
         ignore moveNextMethod
         ignore setStateMachineMethod
         ignore afterCode
@@ -114,23 +118,28 @@ module ResumableCode =
     let inline GetResumptionFunc (sm: byref>) =
         sm.ResumptionDynamicInfo.ResumptionFunc
 
-    let inline Delay(f : unit -> ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> =
-        ResumableCode<'Data, 'T>(fun sm -> (f()).Invoke(&sm))
+    let inline Delay (f: unit -> ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> =
+        ResumableCode<'Data, 'T>(fun sm -> (f ()).Invoke(&sm))
 
     /// Used to represent no-ops like the implicit empty "else" branch of an "if" expression.
-    let inline Zero() : ResumableCode<'Data, unit> =
+    let inline Zero () : ResumableCode<'Data, unit> =
         ResumableCode<'Data, unit>(fun sm -> true)
 
     /// Chains together a step with its following step.
     /// Note that this requires that the first step has no result.
     /// This prevents constructs like `task { return 1; return 2; }`.
-    let CombineDynamic(sm: byref>, code1: ResumableCode<'Data, unit>, code2: ResumableCode<'Data, 'T>) : bool =
-        if code1.Invoke(&sm) then 
+    let CombineDynamic
+        (
+            sm: byref>,
+            code1: ResumableCode<'Data, unit>,
+            code2: ResumableCode<'Data, 'T>
+        ) : bool =
+        if code1.Invoke(&sm) then
             code2.Invoke(&sm)
         else
             let rec resume (mf: ResumptionFunc<'Data>) =
-                ResumptionFunc<'Data>(fun sm -> 
-                    if mf.Invoke(&sm) then 
+                ResumptionFunc<'Data>(fun sm ->
+                    if mf.Invoke(&sm) then
                         code2.Invoke(&sm)
                     else
                         sm.ResumptionDynamicInfo.ResumptionFunc <- (resume (GetResumptionFunc &sm))
@@ -142,92 +151,111 @@ module ResumableCode =
     /// Chains together a step with its following step.
     /// Note that this requires that the first step has no result.
     /// This prevents constructs like `task { return 1; return 2; }`.
-    let inline Combine(code1: ResumableCode<'Data, unit>, code2: ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> =
+    let inline Combine (code1: ResumableCode<'Data, unit>, code2: ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> =
         ResumableCode<'Data, 'T>(fun sm ->
             if __useResumableCode then
                 //-- RESUMABLE CODE START
                 // NOTE: The code for code1 may contain await points! Resuming may branch directly
                 // into this code!
                 let __stack_fin = code1.Invoke(&sm)
-                if __stack_fin then 
+
+                if __stack_fin then
                     code2.Invoke(&sm)
                 else
                     false
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
             else
                 CombineDynamic(&sm, code1, code2))
 
-    let rec WhileDynamic (sm: byref>, condition: unit -> bool, body: ResumableCode<'Data,unit>) : bool =
-        if condition() then 
-            if body.Invoke (&sm) then
-                WhileDynamic (&sm, condition, body)
+    let rec WhileDynamic (sm: byref>, condition: unit -> bool, body: ResumableCode<'Data, unit>) : bool =
+        if condition () then
+            if body.Invoke(&sm) then
+                WhileDynamic(&sm, condition, body)
             else
                 let rf = GetResumptionFunc &sm
                 sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf)))
                 false
         else
             true
-    and WhileBodyDynamicAux (sm: byref>, condition: unit -> bool, body: ResumableCode<'Data,unit>, rf: ResumptionFunc<_>) : bool =
-        if rf.Invoke (&sm) then
-            WhileDynamic (&sm, condition, body)
+
+    and WhileBodyDynamicAux
+        (
+            sm: byref>,
+            condition: unit -> bool,
+            body: ResumableCode<'Data, unit>,
+            rf: ResumptionFunc<_>
+        ) : bool =
+        if rf.Invoke(&sm) then
+            WhileDynamic(&sm, condition, body)
         else
             let rf = GetResumptionFunc &sm
             sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf)))
             false
 
     /// Builds a step that executes the body while the condition predicate is true.
-    let inline While ([] condition : unit -> bool, body : ResumableCode<'Data, unit>) : ResumableCode<'Data, unit> =
+    let inline While ([] condition: unit -> bool, body: ResumableCode<'Data, unit>) : ResumableCode<'Data, unit> =
         ResumableCode<'Data, unit>(fun sm ->
-            if __useResumableCode then 
+            if __useResumableCode then
                 //-- RESUMABLE CODE START
-                let mutable __stack_go = true 
-                while __stack_go && condition() do
+                let mutable __stack_go = true
+
+                while __stack_go && condition () do
                     // NOTE: The body of the state machine code for 'while' may contain await points, so resuming
                     // the code will branch directly into the expanded 'body', branching directly into the while loop
                     let __stack_body_fin = body.Invoke(&sm)
                     // If the body completed, we go back around the loop (__stack_go = true)
                     // If the body yielded, we yield (__stack_go = false)
                     __stack_go <- __stack_body_fin
+
                 __stack_go
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
             else
                 WhileDynamic(&sm, condition, body))
 
-    let rec TryWithDynamic (sm: byref>, body: ResumableCode<'Data, 'T>, handler: exn -> ResumableCode<'Data, 'T>) : bool =
+    let rec TryWithDynamic
+        (
+            sm: byref>,
+            body: ResumableCode<'Data, 'T>,
+            handler: exn -> ResumableCode<'Data, 'T>
+        ) : bool =
         try
-            if body.Invoke(&sm) then 
+            if body.Invoke(&sm) then
                 true
             else
                 let rf = GetResumptionFunc &sm
-                sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> TryWithDynamic(&sm, ResumableCode<'Data,'T>(fun sm -> rf.Invoke(&sm)), handler)))
+
+                sm.ResumptionDynamicInfo.ResumptionFunc <-
+                    (ResumptionFunc<'Data>(fun sm -> TryWithDynamic(&sm, ResumableCode<'Data, 'T>(fun sm -> rf.Invoke(&sm)), handler)))
+
                 false
-        with exn -> 
+        with exn ->
             (handler exn).Invoke(&sm)
 
     /// Wraps a step in a try/with. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
     let inline TryWith (body: ResumableCode<'Data, 'T>, catch: exn -> ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> =
         ResumableCode<'Data, 'T>(fun sm ->
-            if __useResumableCode then 
+            if __useResumableCode then
                 //-- RESUMABLE CODE START
                 let mutable __stack_fin = false
                 let mutable __stack_caught = false
                 let mutable __stack_savedExn = Unchecked.defaultof<_>
+
                 try
                     // The try block may contain await points.
                     let __stack_body_fin = body.Invoke(&sm)
                     // If we make it to the assignment we prove we've made a step
                     __stack_fin <- __stack_body_fin
-                with exn -> 
+                with exn ->
                     __stack_caught <- true
                     __stack_savedExn <- exn
 
-                if __stack_caught then 
-                    // Place the catch code outside the catch block 
+                if __stack_caught then
+                    // Place the catch code outside the catch block
                     (catch __stack_savedExn).Invoke(&sm)
                 else
                     __stack_fin
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
 
             else
                 TryWithDynamic(&sm, body, catch))
@@ -235,38 +263,51 @@ module ResumableCode =
     let rec TryFinallyCompensateDynamic (sm: byref>, mf: ResumptionFunc<'Data>, savedExn: exn option) : bool =
         let mutable fin = false
         fin <- mf.Invoke(&sm)
+
         if fin then
             // reraise at the end of the finally block
-            match savedExn with 
+            match savedExn with
             | None -> true
             | Some exn -> raise exn
-        else 
+        else
             let rf = GetResumptionFunc &sm
             sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> TryFinallyCompensateDynamic(&sm, rf, savedExn)))
             false
 
-    let rec TryFinallyAsyncDynamic (sm: byref>, body: ResumableCode<'Data, 'T>, compensation: ResumableCode<'Data,unit>) : bool =
+    let rec TryFinallyAsyncDynamic
+        (
+            sm: byref>,
+            body: ResumableCode<'Data, 'T>,
+            compensation: ResumableCode<'Data, unit>
+        ) : bool =
         let mutable fin = false
         let mutable savedExn = None
+
         try
             fin <- body.Invoke(&sm)
         with exn ->
-            savedExn <- Some exn 
+            savedExn <- Some exn
             fin <- true
-        if fin then 
+
+        if fin then
             TryFinallyCompensateDynamic(&sm, ResumptionFunc<'Data>(fun sm -> compensation.Invoke(&sm)), savedExn)
         else
             let rf = GetResumptionFunc &sm
-            sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> TryFinallyAsyncDynamic(&sm, ResumableCode<'Data,'T>(fun sm -> rf.Invoke(&sm)), compensation)))
+
+            sm.ResumptionDynamicInfo.ResumptionFunc <-
+                (ResumptionFunc<'Data>(fun sm ->
+                    TryFinallyAsyncDynamic(&sm, ResumableCode<'Data, 'T>(fun sm -> rf.Invoke(&sm)), compensation)))
+
             false
 
     /// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    let inline TryFinally (body: ResumableCode<'Data, 'T>, compensation: ResumableCode<'Data,unit>) =
+    let inline TryFinally (body: ResumableCode<'Data, 'T>, compensation: ResumableCode<'Data, unit>) =
         ResumableCode<'Data, 'T>(fun sm ->
-            if __useResumableCode then 
+            if __useResumableCode then
                 //-- RESUMABLE CODE START
                 let mutable __stack_fin = false
+
                 try
                     let __stack_body_fin = body.Invoke(&sm)
                     // If we make it to the assignment we prove we've made a step, an early 'ret' exit out of the try/with
@@ -274,24 +315,26 @@ module ResumableCode =
                     __stack_fin <- __stack_body_fin
                 with _exn ->
                     let __stack_ignore = compensation.Invoke(&sm)
-                    reraise()
+                    reraise ()
 
-                if __stack_fin then 
+                if __stack_fin then
                     let __stack_ignore = compensation.Invoke(&sm)
                     ()
+
                 __stack_fin
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
             else
-                TryFinallyAsyncDynamic(&sm, body, ResumableCode<_,_>(fun sm -> compensation.Invoke(&sm))))
+                TryFinallyAsyncDynamic(&sm, body, ResumableCode<_, _>(fun sm -> compensation.Invoke(&sm))))
 
     /// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    let inline TryFinallyAsync (body: ResumableCode<'Data, 'T>, compensation: ResumableCode<'Data,unit>) : ResumableCode<'Data, 'T> =
+    let inline TryFinallyAsync (body: ResumableCode<'Data, 'T>, compensation: ResumableCode<'Data, unit>) : ResumableCode<'Data, 'T> =
         ResumableCode<'Data, 'T>(fun sm ->
-            if __useResumableCode then 
+            if __useResumableCode then
                 //-- RESUMABLE CODE START
                 let mutable __stack_fin = false
                 let mutable savedExn = None
+
                 try
                     let __stack_body_fin = body.Invoke(&sm)
                     // If we make it to the assignment we prove we've made a step, an early 'ret' exit out of the try/with
@@ -301,59 +344,67 @@ module ResumableCode =
                     savedExn <- Some exn
                     __stack_fin <- true
 
-                if __stack_fin then 
+                if __stack_fin then
                     let __stack_compensation_fin = compensation.Invoke(&sm)
                     __stack_fin <- __stack_compensation_fin
 
-                if __stack_fin then 
-                    match savedExn with 
+                if __stack_fin then
+                    match savedExn with
                     | None -> ()
                     | Some exn -> raise exn
 
                 __stack_fin
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
             else
                 TryFinallyAsyncDynamic(&sm, body, compensation))
 
-    let inline Using (resource : 'Resource, body : 'Resource -> ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> when 'Resource :> IDisposable = 
+    let inline Using
+        (
+            resource: 'Resource,
+            body: 'Resource -> ResumableCode<'Data, 'T>
+        ) : ResumableCode<'Data, 'T> when 'Resource :> IDisposable =
         // A using statement is just a try/finally with the finally block disposing if non-null.
         TryFinally(
             ResumableCode<'Data, 'T>(fun sm -> (body resource).Invoke(&sm)),
-            ResumableCode<'Data,unit>(fun sm -> 
-                if not (isNull (box resource)) then 
+            ResumableCode<'Data, unit>(fun sm ->
+                if not (isNull (box resource)) then
                     resource.Dispose()
-                true))
 
-    let inline For (sequence : seq<'T>, body : 'T -> ResumableCode<'Data, unit>) : ResumableCode<'Data, unit> =
+                true)
+        )
+
+    let inline For (sequence: seq<'T>, body: 'T -> ResumableCode<'Data, unit>) : ResumableCode<'Data, unit> =
         // A for loop is just a using statement on the sequence's enumerator...
-        Using (sequence.GetEnumerator(), 
+        Using(
+            sequence.GetEnumerator(),
             // ... and its body is a while loop that advances the enumerator and runs the body on each element.
             (fun e ->
                 While(
-                    (fun () -> 
+                    (fun () ->
                         __debugPoint "ForLoop.InOrToKeyword"
-                        e.MoveNext()), 
-                    ResumableCode<'Data, unit>(fun sm -> 
-                        (body e.Current).Invoke(&sm)))))
+                        e.MoveNext()),
+                    ResumableCode<'Data, unit>(fun sm -> (body e.Current).Invoke(&sm))
+                ))
+        )
 
-    let YieldDynamic (sm: byref>) : bool = 
+    let YieldDynamic (sm: byref>) : bool =
         let cont = ResumptionFunc<'Data>(fun _sm -> true)
         sm.ResumptionDynamicInfo.ResumptionFunc <- cont
         false
 
-    let inline Yield () : ResumableCode<'Data, unit> = 
-        ResumableCode<'Data, unit>(fun sm -> 
-            if __useResumableCode then 
+    let inline Yield () : ResumableCode<'Data, unit> =
+        ResumableCode<'Data, unit>(fun sm ->
+            if __useResumableCode then
                 //-- RESUMABLE CODE START
-                match __resumableEntry() with 
+                match __resumableEntry () with
                 | Some contID ->
                     sm.ResumptionPoint <- contID
-                    //if verbose then printfn $"[{sm.Id}] Yield: returning false to indicate yield, contID = {contID}" 
+                    //if verbose then printfn $"[{sm.Id}] Yield: returning false to indicate yield, contID = {contID}"
                     false
                 | None ->
-                    //if verbose then printfn $"[{sm.Id}] Yield: returning true to indicate post-yield" 
+                    //if verbose then printfn $"[{sm.Id}] Yield: returning true to indicate post-yield"
                     true
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
             else
                 YieldDynamic(&sm))
 
diff --git a/src/FSharp.Core/seq.fs b/src/FSharp.Core/seq.fs
index 27699c40abb..7b867e661e4 100644
--- a/src/FSharp.Core/seq.fs
+++ b/src/FSharp.Core/seq.fs
@@ -22,19 +22,31 @@ module Internal =
 
         open Microsoft.FSharp.Collections.IEnumerator
 
-        let rec tryItem index (e : IEnumerator<'T>) =
+        let rec tryItem index (e: IEnumerator<'T>) =
             if not (e.MoveNext()) then None
             elif index = 0 then Some e.Current
-            else tryItem (index-1) e
+            else tryItem (index - 1) e
 
-        let rec nth index (e : IEnumerator<'T>) =
+        let rec nth index (e: IEnumerator<'T>) =
             if not (e.MoveNext()) then
                 let shortBy = index + 1
-                invalidArgFmt "index"
+
+                invalidArgFmt
+                    "index"
                     "{0}\nseq was short by {1} {2}"
-                    [|SR.GetString SR.notEnoughElements; shortBy; (if shortBy = 1 then "element" else "elements")|]
-            if index = 0 then e.Current
-            else nth (index - 1) e
+                    [|
+                        SR.GetString SR.notEnoughElements
+                        shortBy
+                        (if shortBy = 1 then
+                             "element"
+                         else
+                             "elements")
+                    |]
+
+            if index = 0 then
+                e.Current
+            else
+                nth (index - 1) e
 
         []
         type MapEnumeratorState =
@@ -43,98 +55,114 @@ module Internal =
             | Finished
 
         []
-        type MapEnumerator<'T> () =
+        type MapEnumerator<'T>() =
             let mutable state = NotStarted
 
             []
-            val mutable private curr : 'T
+            val mutable private curr: 'T
 
-            member this.GetCurrent () =
+            member this.GetCurrent() =
                 match state with
-                | NotStarted -> notStarted()
-                | Finished -> alreadyFinished()
+                | NotStarted -> notStarted ()
+                | Finished -> alreadyFinished ()
                 | InProcess -> ()
+
                 this.curr
 
-            abstract DoMoveNext : byref<'T> -> bool
-            abstract Dispose : unit -> unit
+            abstract DoMoveNext: byref<'T> -> bool
+            abstract Dispose: unit -> unit
 
             interface IEnumerator<'T> with
                 member this.Current = this.GetCurrent()
 
             interface IEnumerator with
-                member this.Current = box(this.GetCurrent())
-                member this.MoveNext () =
+                member this.Current = box (this.GetCurrent())
+
+                member this.MoveNext() =
                     state <- InProcess
+
                     if this.DoMoveNext(&this.curr) then
                         true
                     else
                         state <- Finished
                         false
-                member _.Reset() = noReset()
+
+                member _.Reset() =
+                    noReset ()
 
             interface System.IDisposable with
-                member this.Dispose() = this.Dispose()
+                member this.Dispose() =
+                    this.Dispose()
 
-        let map f (e : IEnumerator<_>) : IEnumerator<_>=
+        let map f (e: IEnumerator<_>) : IEnumerator<_> =
             upcast
                 { new MapEnumerator<_>() with
-                    member _.DoMoveNext (curr : byref<_>) =
+                    member _.DoMoveNext(curr: byref<_>) =
                         if e.MoveNext() then
                             curr <- f e.Current
                             true
                         else
                             false
-                    member _.Dispose() = e.Dispose()
+
+                    member _.Dispose() =
+                        e.Dispose()
                 }
 
-        let mapi f (e : IEnumerator<_>) : IEnumerator<_> =
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(f)
+        let mapi f (e: IEnumerator<_>) : IEnumerator<_> =
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (f)
             let mutable i = -1
+
             upcast
                 { new MapEnumerator<_>() with
                     member _.DoMoveNext curr =
                         i <- i + 1
+
                         if e.MoveNext() then
                             curr <- f.Invoke(i, e.Current)
                             true
                         else
                             false
-                    member _.Dispose() = e.Dispose()
+
+                    member _.Dispose() =
+                        e.Dispose()
                 }
 
-        let map2 f (e1 : IEnumerator<_>) (e2 : IEnumerator<_>) : IEnumerator<_>=
-            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(f)
+        let map2 f (e1: IEnumerator<_>) (e2: IEnumerator<_>) : IEnumerator<_> =
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (f)
+
             upcast
                 { new MapEnumerator<_>() with
                     member _.DoMoveNext curr =
                         let n1 = e1.MoveNext()
                         let n2 = e2.MoveNext()
+
                         if n1 && n2 then
                             curr <- f.Invoke(e1.Current, e2.Current)
                             true
                         else
                             false
 
-                     member _.Dispose() =
+                    member _.Dispose() =
                         try
                             e1.Dispose()
                         finally
                             e2.Dispose()
                 }
 
-        let mapi2 f (e1 : IEnumerator<_>) (e2 : IEnumerator<_>) : IEnumerator<_> =
-            let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(f)
+        let mapi2 f (e1: IEnumerator<_>) (e2: IEnumerator<_>) : IEnumerator<_> =
+            let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (f)
             let mutable i = -1
+
             upcast
                 { new MapEnumerator<_>() with
                     member _.DoMoveNext curr =
                         i <- i + 1
+
                         if (e1.MoveNext() && e2.MoveNext()) then
-                           curr <- f.Invoke(i, e1.Current, e2.Current)
-                           true
+                            curr <- f.Invoke(i, e1.Current, e2.Current)
+                            true
                         else
-                           false
+                            false
 
                     member _.Dispose() =
                         try
@@ -143,8 +171,9 @@ module Internal =
                             e2.Dispose()
                 }
 
-        let map3 f (e1 : IEnumerator<_>) (e2 : IEnumerator<_>) (e3 : IEnumerator<_>) : IEnumerator<_> =
-            let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(f)
+        let map3 f (e1: IEnumerator<_>) (e2: IEnumerator<_>) (e3: IEnumerator<_>) : IEnumerator<_> =
+            let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt (f)
+
             upcast
                 { new MapEnumerator<_>() with
                     member _.DoMoveNext curr =
@@ -168,79 +197,96 @@ module Internal =
                                 e3.Dispose()
                 }
 
-        let choose f (e : IEnumerator<'T>) =
+        let choose f (e: IEnumerator<'T>) =
             let mutable started = false
             let mutable curr = None
-            let get() = 
+
+            let get () =
                 check started
-                match curr with 
-                | None -> alreadyFinished() 
+
+                match curr with
+                | None -> alreadyFinished ()
                 | Some x -> x
 
             { new IEnumerator<'U> with
-                member _.Current = get()
-
+                member _.Current = get ()
               interface IEnumerator with
-                member _.Current = box (get())
-                member _.MoveNext() =
-                    if not started then started <- true
-                    curr <- None
-                    while (curr.IsNone && e.MoveNext()) do
-                        curr <- f e.Current
-                    Option.isSome curr
+                  member _.Current = box (get ())
+
+                  member _.MoveNext() =
+                      if not started then started <- true
+                      curr <- None
+
+                      while (curr.IsNone && e.MoveNext()) do
+                          curr <- f e.Current
 
-                member _.Reset() = noReset()
+                      Option.isSome curr
 
+                  member _.Reset() =
+                      noReset ()
               interface System.IDisposable with
-                member _.Dispose() = e.Dispose()  }
+                  member _.Dispose() =
+                      e.Dispose()
+            }
 
-        let filter f (e : IEnumerator<'T>) =
+        let filter f (e: IEnumerator<'T>) =
             let mutable started = false
+
             let this =
                 { new IEnumerator<'T> with
-                    member _.Current = check started; e.Current
-
+                    member _.Current =
+                        check started
+                        e.Current
                   interface IEnumerator with
-                    member _.Current = check started; box e.Current
+                      member _.Current =
+                          check started
+                          box e.Current
 
-                    member _.MoveNext() =
-                        let rec next() =
-                            if not started then started <- true
-                            e.MoveNext() && (f e.Current || next())
-                        next()
+                      member _.MoveNext() =
+                          let rec next () =
+                              if not started then started <- true
+                              e.MoveNext() && (f e.Current || next ())
 
-                    member _.Reset() = noReset()
+                          next ()
 
+                      member _.Reset() =
+                          noReset ()
                   interface System.IDisposable with
-                    member _.Dispose() = e.Dispose() }
+                      member _.Dispose() =
+                          e.Dispose()
+                }
+
             this
 
         let unfold f x : IEnumerator<_> =
             let mutable state = x
+
             upcast
                 { new MapEnumerator<_>() with
                     member _.DoMoveNext curr =
                         match f state with
                         | None -> false
-                        | Some (r,s) ->
+                        | Some (r, s) ->
                             curr <- r
                             state <- s
                             true
 
-                    member _.Dispose() = ()
+                    member _.Dispose() =
+                        ()
                 }
 
         let upto lastOption f =
             match lastOption with
-            | Some b when b < 0 -> Empty()    // a request for -ve length returns empty sequence
+            | Some b when b < 0 -> Empty() // a request for -ve length returns empty sequence
             | _ ->
-                let unstarted = -1  // index value means unstarted (and no valid index)
-                let completed = -2  // index value means completed (and no valid index)
-                let unreachable = -3  // index is unreachable from 0,1,2,3,...
+                let unstarted = -1 // index value means unstarted (and no valid index)
+                let completed = -2 // index value means completed (and no valid index)
+                let unreachable = -3 // index is unreachable from 0,1,2,3,...
+
                 let finalIndex =
                     match lastOption with
-                    | Some b -> b             // here b>=0, a valid end value.
-                    | None   -> unreachable   // run "forever", well as far as Int32.MaxValue since indexing with a bounded type.
+                    | Some b -> b // here b>=0, a valid end value.
+                    | None -> unreachable // run "forever", well as far as Int32.MaxValue since indexing with a bounded type.
 
                 // The Current value for a valid index is "f i".
                 // Lazy<_> values are used as caches, to store either the result or an exception if thrown.
@@ -252,54 +298,64 @@ module Internal =
 
                 // a Lazy node to cache the result/exception
                 let mutable current = Unchecked.defaultof<_>
+
                 let setIndex i =
                     index <- i
                     current <- (Unchecked.defaultof<_>) // cache node unprimed, initialised on demand.
 
-                let getCurrent() =
-                    if index = unstarted then notStarted()
-                    if index = completed then alreadyFinished()
+                let getCurrent () =
+                    if index = unstarted then notStarted ()
+
+                    if index = completed then
+                        alreadyFinished ()
+
                     match box current with
-                    | null ->
-                        current <- Lazy<_>.Create(fun () -> f index)
-                    | _ ->  ()
+                    | null -> current <- Lazy<_>.Create (fun () -> f index)
+                    | _ -> ()
                     // forced or re-forced immediately.
                     current.Force()
-                { new IEnumerator<'U> with
-                    member _.Current = getCurrent()
 
+                { new IEnumerator<'U> with
+                    member _.Current = getCurrent ()
                   interface IEnumerator with
-                    member _.Current = box (getCurrent())
-                    member _.MoveNext() =
-                        if index = completed then
-                            false
-                        elif index = unstarted then
-                            setIndex 0
-                            true
-                        else
-                            if index = System.Int32.MaxValue then invalidOp (SR.GetString(SR.enumerationPastIntMaxValue))
-                            if index = finalIndex then
-                                false
-                            else
-                                setIndex (index + 1)
-                                true
-
-                    member _.Reset() = noReset()
-
+                      member _.Current = box (getCurrent ())
+
+                      member _.MoveNext() =
+                          if index = completed then
+                              false
+                          elif index = unstarted then
+                              setIndex 0
+                              true
+                          else
+                              if index = System.Int32.MaxValue then
+                                  invalidOp (SR.GetString(SR.enumerationPastIntMaxValue))
+
+                              if index = finalIndex then
+                                  false
+                              else
+                                  setIndex (index + 1)
+                                  true
+
+                      member _.Reset() =
+                          noReset ()
                   interface System.IDisposable with
-                    member _.Dispose() = ()
+                      member _.Dispose() =
+                          ()
                 }
 
         []
         type ArrayEnumerator<'T>(arr: 'T array) =
             let mutable curr = -1
             let mutable len = arr.Length
+
             member _.Get() =
                 if curr >= 0 then
-                    if curr >= len then alreadyFinished()
-                    else arr.[curr]
+                    if curr >= len then
+                        alreadyFinished ()
+                    else
+                        arr.[curr]
                 else
-                    notStarted()
+                    notStarted ()
 
             interface IEnumerator<'T> with
                 member x.Current = x.Get()
@@ -312,14 +368,17 @@ module Internal =
                         curr <- curr + 1
                         curr < len
 
-                member x.Current = box(x.Get())
+                member x.Current = box (x.Get())
 
-                member _.Reset() = noReset()
+                member _.Reset() =
+                    noReset ()
 
             interface System.IDisposable with
-                member _.Dispose() = ()
+                member _.Dispose() =
+                    ()
 
-        let ofArray arr = (new ArrayEnumerator<'T>(arr) :> IEnumerator<'T>)
+        let ofArray arr =
+            (new ArrayEnumerator<'T>(arr) :> IEnumerator<'T>)
 
     // Use generators for some implementations of IEnumerables.
     //
@@ -335,18 +394,17 @@ module Internal =
             abstract Apply: (unit -> Step<'T>)
             abstract Disposer: (unit -> unit) option
 
-        let disposeG (g:Generator<'T>) =
+        let disposeG (g: Generator<'T>) =
             match g.Disposer with
             | None -> ()
-            | Some f -> f()
+            | Some f -> f ()
 
-        let appG (g:Generator<_>) =
+        let appG (g: Generator<_>) =
             let res = g.Apply()
+
             match res with
-            | Goto next ->
-                Goto next
-            | Yield _ ->
-                res
+            | Goto next -> Goto next
+            | Yield _ -> res
             | Stop ->
                 disposeG g
                 res
@@ -362,7 +420,7 @@ module Internal =
         //                         yield! rwalk (n-1)
         //                         yield n }
 
-        type GenerateThen<'T>(g:Generator<'T>, cont : unit -> Generator<'T>) =
+        type GenerateThen<'T>(g: Generator<'T>, cont: unit -> Generator<'T>) =
 
             member _.Generator = g
 
@@ -370,27 +428,26 @@ module Internal =
 
             interface Generator<'T> with
 
-                 member _.Apply = (fun () ->
-                     match appG g with
-                     | Stop ->
-                         // OK, move onto the generator given by the continuation
-                         Goto(cont())
+                member _.Apply =
+                    (fun () ->
+                        match appG g with
+                        | Stop ->
+                            // OK, move onto the generator given by the continuation
+                            Goto(cont ())
 
-                     | Yield _ as res ->
-                         res
+                        | Yield _ as res -> res
 
-                     | Goto next ->
-                         Goto(GenerateThen<_>.Bind(next, cont)))
+                        | Goto next -> Goto(GenerateThen<_>.Bind (next, cont)))
 
-                 member _.Disposer =
-                     g.Disposer
+                member _.Disposer = g.Disposer
 
-            static member Bind (g:Generator<'T>, cont) =
+            static member Bind(g: Generator<'T>, cont) =
                 match g with
-                | :? GenerateThen<'T> as g -> GenerateThen<_>.Bind(g.Generator, (fun () -> GenerateThen<_>.Bind (g.Cont(), cont)))
+                | :? GenerateThen<'T> as g -> GenerateThen<_>.Bind (g.Generator, (fun () -> GenerateThen<_>.Bind (g.Cont(), cont)))
                 | g -> (new GenerateThen<'T>(g, cont) :> Generator<'T>)
 
-        let bindG g cont = GenerateThen<_>.Bind(g,cont)
+        let bindG g cont =
+            GenerateThen<_>.Bind (g, cont)
 
         // Internal type. Drive an underlying generator. Crucially when the generator returns
         // a new generator we simply update our current generator and continue. Thus the enumerator
@@ -414,7 +471,7 @@ module Internal =
         // and GenerateFromEnumerator.
 
         []
-        type EnumeratorWrappingLazyGenerator<'T>(g:Generator<'T>) =
+        type EnumeratorWrappingLazyGenerator<'T>(g: Generator<'T>) =
             let mutable g = g
             let mutable curr = None
             let mutable finished = false
@@ -422,66 +479,75 @@ module Internal =
             member _.Generator = g
 
             interface IEnumerator<'T> with
-                member _.Current = 
-                    match curr with 
-                    | Some v -> v 
+                member _.Current =
+                    match curr with
+                    | Some v -> v
                     | None -> invalidOp (SR.GetString(SR.moveNextNotCalledOrFinished))
 
             interface System.Collections.IEnumerator with
                 member x.Current = box (x :> IEnumerator<_>).Current
 
                 member x.MoveNext() =
-                    not finished &&
-                    match appG g with
-                    | Stop ->
-                        curr <- None
-                        finished <- true
-                        false
-                    | Yield v ->
-                        curr <- Some v
-                        true
-                    | Goto next ->
-                        (g <- next)
-                        (x :> IEnumerator).MoveNext()
+                    not finished
+                    && match appG g with
+                       | Stop ->
+                           curr <- None
+                           finished <- true
+                           false
+                       | Yield v ->
+                           curr <- Some v
+                           true
+                       | Goto next ->
+                           (g <- next)
+                           (x :> IEnumerator).MoveNext()
 
-                member _.Reset() = IEnumerator.noReset()
+                member _.Reset() =
+                    IEnumerator.noReset ()
 
             interface System.IDisposable with
                 member _.Dispose() =
                     if not finished then disposeG g
 
         // Internal type, used to optimize Enumerator/Generator chains
-        type LazyGeneratorWrappingEnumerator<'T>(e:IEnumerator<'T>) =
+        type LazyGeneratorWrappingEnumerator<'T>(e: IEnumerator<'T>) =
             member _.Enumerator = e
+
             interface Generator<'T> with
-                member _.Apply = (fun () ->
-                    if e.MoveNext() then
-                        Yield e.Current
-                    else
-                        Stop)
-                member _.Disposer= Some e.Dispose
+                member _.Apply =
+                    (fun () ->
+                        if e.MoveNext() then
+                            Yield e.Current
+                        else
+                            Stop)
 
-        let EnumerateFromGenerator(g:Generator<'T>) =
+                member _.Disposer = Some e.Dispose
+
+        let EnumerateFromGenerator (g: Generator<'T>) =
             match g with
             | :? LazyGeneratorWrappingEnumerator<'T> as g -> g.Enumerator
             | _ -> (new EnumeratorWrappingLazyGenerator<'T>(g) :> IEnumerator<'T>)
 
-        let GenerateFromEnumerator (e:IEnumerator<'T>) =
+        let GenerateFromEnumerator (e: IEnumerator<'T>) =
             match e with
-            | :? EnumeratorWrappingLazyGenerator<'T> as e ->  e.Generator
+            | :? EnumeratorWrappingLazyGenerator<'T> as e -> e.Generator
             | _ -> (new LazyGeneratorWrappingEnumerator<'T>(e) :> Generator<'T>)
 
-
 []
-type CachedSeq<'T>(cleanup,res:seq<'T>) =
+type CachedSeq<'T>(cleanup, res: seq<'T>) =
     interface System.IDisposable with
-        member x.Dispose() = cleanup()
+        member x.Dispose() =
+            cleanup ()
+
     interface System.Collections.Generic.IEnumerable<'T> with
-        member x.GetEnumerator() = res.GetEnumerator()
+        member x.GetEnumerator() =
+            res.GetEnumerator()
+
     interface System.Collections.IEnumerable with
-        member x.GetEnumerator() = (res :> System.Collections.IEnumerable).GetEnumerator()
-    member obj.Clear() = cleanup()
+        member x.GetEnumerator() =
+            (res :> System.Collections.IEnumerable).GetEnumerator()
 
+    member obj.Clear() =
+        cleanup ()
 
 []
 []
@@ -490,137 +556,165 @@ module Seq =
     open Internal
     open IEnumerator
 
-    let mkDelayedSeq (f: unit -> IEnumerable<'T>) = mkSeq (fun () -> f().GetEnumerator())
-    let mkUnfoldSeq f x = mkSeq (fun () -> IEnumerator.unfold f x)
-    let inline indexNotFound() = raise (new System.Collections.Generic.KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
+    let mkDelayedSeq (f: unit -> IEnumerable<'T>) =
+        mkSeq (fun () -> f().GetEnumerator())
+
+    let mkUnfoldSeq f x =
+        mkSeq (fun () -> IEnumerator.unfold f x)
+
+    let inline indexNotFound () =
+        raise (new System.Collections.Generic.KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
 
     []
-    let delay generator = mkDelayedSeq generator
+    let delay generator =
+        mkDelayedSeq generator
 
     []
-    let unfold generator state = mkUnfoldSeq generator state
+    let unfold generator state =
+        mkUnfoldSeq generator state
 
     []
     let empty<'T> = (EmptyEnumerable :> seq<'T>)
 
     []
-    let initInfinite initializer = mkSeq (fun () -> IEnumerator.upto None initializer)
+    let initInfinite initializer =
+        mkSeq (fun () -> IEnumerator.upto None initializer)
 
     []
     let init count initializer =
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
-        mkSeq (fun () -> IEnumerator.upto (Some (count - 1)) initializer)
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
+        mkSeq (fun () -> IEnumerator.upto (Some(count - 1)) initializer)
 
     []
-    let iter action (source : seq<'T>) =
+    let iter action (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         while e.MoveNext() do
             action e.Current
 
     []
-    let item index (source : seq<'T>) =
+    let item index (source: seq<'T>) =
         checkNonNull "source" source
-        if index < 0 then invalidArgInputMustBeNonNegative "index" index
+
+        if index < 0 then
+            invalidArgInputMustBeNonNegative "index" index
+
         use e = source.GetEnumerator()
         IEnumerator.nth index e
 
     []
-    let tryItem index (source : seq<'T>) =
+    let tryItem index (source: seq<'T>) =
         checkNonNull "source" source
-        if index < 0 then None else
-        use e = source.GetEnumerator()
-        IEnumerator.tryItem index e
+
+        if index < 0 then
+            None
+        else
+            use e = source.GetEnumerator()
+            IEnumerator.tryItem index e
 
     []
-    let nth index (source : seq<'T>) =
+    let nth index (source: seq<'T>) =
         item index source
 
     []
-    let iteri action (source : seq<'T>) =
+    let iteri action (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
-        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
+        let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (action)
         let mutable i = 0
+
         while e.MoveNext() do
             f.Invoke(i, e.Current)
             i <- i + 1
 
     []
-    let exists predicate (source : seq<'T>) =
+    let exists predicate (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable state = false
+
         while (not state && e.MoveNext()) do
             state <- predicate e.Current
+
         state
 
     []
-    let inline contains value (source : seq<'T>) =
+    let inline contains value (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable state = false
+
         while (not state && e.MoveNext()) do
             state <- value = e.Current
+
         state
 
     []
-    let forall predicate (source : seq<'T>) =
+    let forall predicate (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable state = true
+
         while (state && e.MoveNext()) do
             state <- predicate e.Current
+
         state
 
     []
-    let iter2 action (source1 : seq<_>) (source2 : seq<_>)    =
+    let iter2 action (source1: seq<_>) (source2: seq<_>) =
         checkNonNull "source1" source1
         checkNonNull "source2" source2
         use e1 = source1.GetEnumerator()
         use e2 = source2.GetEnumerator()
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt action
+
         while (e1.MoveNext() && e2.MoveNext()) do
             f.Invoke(e1.Current, e2.Current)
 
     []
-    let iteri2 action (source1 : seq<_>) (source2 : seq<_>) =
+    let iteri2 action (source1: seq<_>) (source2: seq<_>) =
         checkNonNull "source1" source1
         checkNonNull "source2" source2
         use e1 = source1.GetEnumerator()
         use e2 = source2.GetEnumerator()
         let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt action
         let mutable i = 0
+
         while (e1.MoveNext() && e2.MoveNext()) do
             f.Invoke(i, e1.Current, e2.Current)
             i <- i + 1
 
     // Build an IEnumerable by wrapping/transforming iterators as they get generated.
-    let revamp f (ie : seq<_>) = mkSeq (fun () -> f (ie.GetEnumerator()))
+    let revamp f (ie: seq<_>) =
+        mkSeq (fun () -> f (ie.GetEnumerator()))
 
-    let revamp2 f (ie1 : seq<_>) (source2 : seq<_>) =
+    let revamp2 f (ie1: seq<_>) (source2: seq<_>) =
         mkSeq (fun () -> f (ie1.GetEnumerator()) (source2.GetEnumerator()))
 
-    let revamp3 f (ie1 : seq<_>) (source2 : seq<_>) (source3 : seq<_>) =
+    let revamp3 f (ie1: seq<_>) (source2: seq<_>) (source3: seq<_>) =
         mkSeq (fun () -> f (ie1.GetEnumerator()) (source2.GetEnumerator()) (source3.GetEnumerator()))
 
     []
-    let filter predicate source      =
+    let filter predicate source =
         checkNonNull "source" source
         revamp (IEnumerator.filter predicate) source
 
     []
-    let where predicate source = filter predicate source
+    let where predicate source =
+        filter predicate source
 
     []
-    let map mapping source      =
+    let map mapping source =
         checkNonNull "source" source
         revamp (IEnumerator.map mapping) source
 
     []
-    let mapi mapping source      =
+    let mapi mapping source =
         checkNonNull "source" source
-        revamp  (IEnumerator.mapi   mapping) source
+        revamp (IEnumerator.mapi mapping) source
 
     []
     let mapi2 mapping source1 source2 =
@@ -662,7 +756,7 @@ module Seq =
         checkNonNull "source1" source1
         checkNonNull "source2" source2
         checkNonNull "source3" source3
-        map2 (fun x (y,z) -> x, y, z) source1 (zip source2 source3)
+        map2 (fun x (y, z) -> x, y, z) source1 (zip source2 source3)
 
     []
     let cast (source: IEnumerable) =
@@ -670,7 +764,7 @@ module Seq =
         mkSeq (fun () -> IEnumerator.cast (source.GetEnumerator()))
 
     []
-    let tryPick chooser (source : seq<'T>) =
+    let tryPick chooser (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable res = None
@@ -685,11 +779,11 @@ module Seq =
         checkNonNull "source" source
 
         match tryPick chooser source with
-        | None -> indexNotFound()
+        | None -> indexNotFound ()
         | Some x -> x
 
     []
-    let tryFind predicate (source : seq<'T>)  =
+    let tryFind predicate (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable res = None
@@ -705,27 +799,40 @@ module Seq =
         checkNonNull "source" source
 
         match tryFind predicate source with
-        | None -> indexNotFound()
+        | None -> indexNotFound ()
         | Some x -> x
 
     []
-    let take count (source : seq<'T>) =
+    let take count (source: seq<'T>) =
         checkNonNull "source" source
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
+
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
         // Note: don't create or dispose any IEnumerable if n = 0
         if count = 0 then
             empty
         else
-            seq { use e = source.GetEnumerator()
-                  for x in count .. - 1 .. 1 do
-                      if not (e.MoveNext()) then
-                          invalidOpFmt "{0}: tried to take {1} {2} past the end of the seq. Use Seq.truncate to get {3} or less elements"
-                            [|SR.GetString SR.notEnoughElements; x; (if x = 1 then "element" else "elements"); count|]
-                      yield e.Current }
+            seq {
+                use e = source.GetEnumerator()
+
+                for x in count .. - 1 .. 1 do
+                    if not (e.MoveNext()) then
+                        invalidOpFmt
+                            "{0}: tried to take {1} {2} past the end of the seq. Use Seq.truncate to get {3} or less elements"
+                            [|
+                                SR.GetString SR.notEnoughElements
+                                x
+                                (if x = 1 then "element" else "elements")
+                                count
+                            |]
+
+                    yield e.Current
+            }
 
     []
-    let isEmpty (source : seq<'T>) =
+    let isEmpty (source: seq<'T>) =
         checkNonNull "source" source
+
         match source with
         | :? ('T[]) as a -> a.Length = 0
         | :? ('T list) as a -> a.IsEmpty
@@ -734,15 +841,15 @@ module Seq =
             use ie = source.GetEnumerator()
             not (ie.MoveNext())
 
-
     []
     let concat sources =
         checkNonNull "sources" sources
         RuntimeHelpers.mkConcatSeq sources
 
     []
-    let length (source : seq<'T>) =
+    let length (source: seq<'T>) =
         checkNonNull "source" source
+
         match source with
         | :? ('T[]) as a -> a.Length
         | :? ('T list) as a -> a.Length
@@ -750,22 +857,26 @@ module Seq =
         | _ ->
             use e = source.GetEnumerator()
             let mutable state = 0
+
             while e.MoveNext() do
                 state <- state + 1
+
             state
 
     []
-    let fold<'T,'State> folder (state:'State) (source : seq<'T>)  =
+    let fold<'T, 'State> folder (state: 'State) (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt folder
         let mutable state = state
+
         while e.MoveNext() do
             state <- f.Invoke(state, e.Current)
+
         state
 
     []
-    let fold2<'T1,'T2,'State> folder (state:'State) (source1: seq<'T1>) (source2: seq<'T2>) =
+    let fold2<'T1, 'T2, 'State> folder (state: 'State) (source1: seq<'T1>) (source2: seq<'T2>) =
         checkNonNull "source1" source1
         checkNonNull "source2" source2
 
@@ -775,75 +886,94 @@ module Seq =
         let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt folder
 
         let mutable state = state
+
         while e1.MoveNext() && e2.MoveNext() do
             state <- f.Invoke(state, e1.Current, e2.Current)
 
         state
 
     []
-    let reduce reduction (source : seq<'T>) =
+    let reduce reduction (source: seq<'T>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
-        if not (e.MoveNext()) then invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
+        if not (e.MoveNext()) then
+            invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt reduction
         let mutable state = e.Current
+
         while e.MoveNext() do
             state <- f.Invoke(state, e.Current)
+
         state
 
-    let fromGenerator f = mkSeq(fun () -> Generator.EnumerateFromGenerator (f()))
-    let toGenerator (ie : seq<_>) = Generator.GenerateFromEnumerator (ie.GetEnumerator())
+    let fromGenerator f =
+        mkSeq (fun () -> Generator.EnumerateFromGenerator(f ()))
+
+    let toGenerator (ie: seq<_>) =
+        Generator.GenerateFromEnumerator(ie.GetEnumerator())
 
     []
     let replicate count initial =
-        System.Linq.Enumerable.Repeat(initial,count)
+        System.Linq.Enumerable.Repeat(initial, count)
 
     []
     let append (source1: seq<'T>) (source2: seq<'T>) =
         checkNonNull "source1" source1
         checkNonNull "source2" source2
-        fromGenerator(fun () -> Generator.bindG (toGenerator source1) (fun () -> toGenerator source2))
+        fromGenerator (fun () -> Generator.bindG (toGenerator source1) (fun () -> toGenerator source2))
 
     []
-    let collect mapping source = map mapping source |> concat
+    let collect mapping source =
+        map mapping source |> concat
 
     []
-    let compareWith (comparer:'T -> 'T -> int) (source1 : seq<'T>) (source2: seq<'T>) =
+    let compareWith (comparer: 'T -> 'T -> int) (source1: seq<'T>) (source2: seq<'T>) =
         checkNonNull "source1" source1
         checkNonNull "source2" source2
         use e1 = source1.GetEnumerator()
         use e2 = source2.GetEnumerator()
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt comparer
+
         let rec go () =
             let e1ok = e1.MoveNext()
             let e2ok = e2.MoveNext()
-            let c = if e1ok = e2ok then 0 else if e1ok then 1 else -1
-            if c <> 0 then c else
-            if not e1ok || not e2ok then 0
+
+            let c =
+                if e1ok = e2ok then 0
+                else if e1ok then 1
+                else -1
+
+            if c <> 0 then
+                c
+            else if not e1ok || not e2ok then
+                0
             else
                 let c = f.Invoke(e1.Current, e2.Current)
-                if c <> 0 then c else
-                go ()
-        go()
+                if c <> 0 then c else go ()
+
+        go ()
 
     []
-    let ofList (source : 'T list) =
+    let ofList (source: 'T list) =
         (source :> seq<'T>)
 
     []
-    let toList (source : seq<'T>) =
+    let toList (source: seq<'T>) =
         checkNonNull "source" source
         Microsoft.FSharp.Primitives.Basics.List.ofSeq source
 
     // Create a new object to ensure underlying array may not be mutated by a backdoor cast
     []
-    let ofArray (source : 'T array) =
+    let ofArray (source: 'T array) =
         checkNonNull "source" source
         mkSeq (fun () -> IEnumerator.ofArray source)
 
     []
-    let toArray (source : seq<'T>)  =
+    let toArray (source: seq<'T>) =
         checkNonNull "source" source
+
         match source with
         | :? ('T[]) as res -> (res.Clone() :?> 'T[])
         | :? ('T list) as res -> List.toArray res
@@ -857,14 +987,16 @@ module Seq =
             let res = ResizeArray<_>(source)
             res.ToArray()
 
-    let foldArraySubRight (f:OptimizedClosures.FSharpFunc<'T,_,_>) (arr: 'T[]) start fin acc =
+    let foldArraySubRight (f: OptimizedClosures.FSharpFunc<'T, _, _>) (arr: 'T[]) start fin acc =
         let mutable state = acc
+
         for i = fin downto start do
             state <- f.Invoke(arr.[i], state)
+
         state
 
     []
-    let foldBack<'T,'State> folder (source : seq<'T>) (state:'State) =
+    let foldBack<'T, 'State> folder (source: seq<'T>) (state: 'State) =
         checkNonNull "source" source
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt folder
         let arr = toArray source
@@ -872,14 +1004,15 @@ module Seq =
         foldArraySubRight f arr 0 (len - 1) state
 
     []
-    let foldBack2<'T1,'T2,'State> folder (source1 : seq<'T1>) (source2 : seq<'T2>) (state:'State) =
+    let foldBack2<'T1, 'T2, 'State> folder (source1: seq<'T1>) (source2: seq<'T2>) (state: 'State) =
         let zipped = zip source1 source2
         foldBack ((<||) folder) zipped state
 
     []
-    let reduceBack reduction (source : seq<'T>) =
+    let reduceBack reduction (source: seq<'T>) =
         checkNonNull "source" source
         let arr = toArray source
+
         match arr.Length with
         | 0 -> invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
         | len ->
@@ -887,42 +1020,58 @@ module Seq =
             foldArraySubRight f arr 0 (len - 2) arr.[len - 1]
 
     []
-    let singleton value = mkSeq (fun () -> IEnumerator.Singleton value)
+    let singleton value =
+        mkSeq (fun () -> IEnumerator.Singleton value)
 
     []
     let truncate count (source: seq<'T>) =
         checkNonNull "source" source
-        if count <= 0 then empty else
-        seq { let mutable i = 0
-              use ie = source.GetEnumerator()
-              while i < count && ie.MoveNext() do
-                 i <- i + 1
-                 yield ie.Current }
+
+        if count <= 0 then
+            empty
+        else
+            seq {
+                let mutable i = 0
+                use ie = source.GetEnumerator()
+
+                while i < count && ie.MoveNext() do
+                    i <- i + 1
+                    yield ie.Current
+            }
 
     []
     let pairwise (source: seq<'T>) =
         checkNonNull "source" source
-        seq { use ie = source.GetEnumerator()
-              if ie.MoveNext() then
-                  let mutable iref = ie.Current
-                  while ie.MoveNext() do
-                      let j = ie.Current
-                      yield (iref, j)
-                      iref <- j }
+
+        seq {
+            use ie = source.GetEnumerator()
+
+            if ie.MoveNext() then
+                let mutable iref = ie.Current
+
+                while ie.MoveNext() do
+                    let j = ie.Current
+                    yield (iref, j)
+                    iref <- j
+        }
 
     []
-    let scan<'T,'State> folder (state:'State) (source : seq<'T>) =
+    let scan<'T, 'State> folder (state: 'State) (source: seq<'T>) =
         checkNonNull "source" source
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt folder
-        seq { let mutable zref = state
-              yield zref
-              use ie = source.GetEnumerator()
-              while ie.MoveNext() do
-                  zref <- f.Invoke(zref, ie.Current)
-                  yield zref }
+
+        seq {
+            let mutable zref = state
+            yield zref
+            use ie = source.GetEnumerator()
+
+            while ie.MoveNext() do
+                zref <- f.Invoke(zref, ie.Current)
+                yield zref
+        }
 
     []
-    let tryFindBack predicate (source : seq<'T>) =
+    let tryFindBack predicate (source: seq<'T>) =
         checkNonNull "source" source
         source |> toArray |> Array.tryFindBack predicate
 
@@ -932,41 +1081,48 @@ module Seq =
         source |> toArray |> Array.findBack predicate
 
     []
-    let scanBack<'T,'State> folder (source : seq<'T>) (state:'State) =
+    let scanBack<'T, 'State> folder (source: seq<'T>) (state: 'State) =
         checkNonNull "source" source
-        mkDelayedSeq(fun () ->
+
+        mkDelayedSeq (fun () ->
             let arr = source |> toArray
             let res = Array.scanSubRight folder arr 0 (arr.Length - 1) state
             res :> seq<_>)
 
     []
-    let findIndex predicate (source:seq<_>) =
+    let findIndex predicate (source: seq<_>) =
         checkNonNull "source" source
         use ie = source.GetEnumerator()
+
         let rec loop i =
             if ie.MoveNext() then
                 if predicate ie.Current then
                     i
-                else loop (i + 1)
+                else
+                    loop (i + 1)
             else
-                indexNotFound()
+                indexNotFound ()
+
         loop 0
 
     []
-    let tryFindIndex predicate (source:seq<_>) =
+    let tryFindIndex predicate (source: seq<_>) =
         checkNonNull "source" source
         use ie = source.GetEnumerator()
+
         let rec loop i =
             if ie.MoveNext() then
                 if predicate ie.Current then
                     Some i
-                else loop (i + 1)
+                else
+                    loop (i + 1)
             else
                 None
+
         loop 0
 
     []
-    let tryFindIndexBack predicate (source : seq<'T>) =
+    let tryFindIndexBack predicate (source: seq<'T>) =
         checkNonNull "source" source
         source |> toArray |> Array.tryFindIndexBack predicate
 
@@ -979,29 +1135,34 @@ module Seq =
     []
     let windowed windowSize (source: seq<_>) =
         checkNonNull "source" source
-        if windowSize <= 0 then invalidArgFmt "windowSize" "{0}\nwindowSize = {1}"
-                                    [|SR.GetString SR.inputMustBePositive; windowSize|]
+
+        if windowSize <= 0 then
+            invalidArgFmt "windowSize" "{0}\nwindowSize = {1}" [| SR.GetString SR.inputMustBePositive; windowSize |]
+
         seq {
             let arr = Array.zeroCreateUnchecked windowSize
-            let mutable r =windowSize - 1
+            let mutable r = windowSize - 1
             let mutable i = 0
             use e = source.GetEnumerator()
+
             while e.MoveNext() do
                 arr.[i] <- e.Current
                 i <- (i + 1) % windowSize
+
                 if r = 0 then
                     if windowSize < 32 then
-                        yield Array.init windowSize (fun j -> arr.[(i+j) % windowSize])
+                        yield Array.init windowSize (fun j -> arr.[(i + j) % windowSize])
                     else
                         let result = Array.zeroCreateUnchecked windowSize
                         Array.Copy(arr, i, result, 0, windowSize - i)
                         Array.Copy(arr, 0, result, windowSize - i, i)
                         yield result
-                else r <- (r - 1)
+                else
+                    r <- (r - 1)
         }
 
     []
-    let cache (source : seq<'T>) =
+    let cache (source: seq<'T>) =
         checkNonNull "source" source
         // Wrap a seq to ensure that it is enumerated just once and only as far as is necessary.
         //
@@ -1012,7 +1173,7 @@ module Seq =
         // The state is (prefix,enumerator) with invariants:
         //   * the prefix followed by elts from the enumerator are the initial sequence.
         //   * the prefix contains only as many elements as the longest enumeration so far.
-        let prefix      = ResizeArray<_>()
+        let prefix = ResizeArray<_>()
 
         // None          = Unstarted.
         // Some(Some e)  = Started.
@@ -1020,49 +1181,54 @@ module Seq =
         let mutable enumeratorR = None
 
         let oneStepTo i =
-          // If possible, step the enumeration to prefix length i (at most one step).
-          // Be speculative, since this could have already happened via another thread.
-          if i >= prefix.Count then // is a step still required?
-              // If not yet started, start it (create enumerator).
-              let optEnumerator =
-                  match enumeratorR with
-                  | None ->
-                      let optEnumerator = Some (source.GetEnumerator())
-                      enumeratorR <- Some optEnumerator
-                      optEnumerator
-                  | Some optEnumerator ->
-                      optEnumerator
-
-              match optEnumerator with
-              | Some enumerator ->
-                  if enumerator.MoveNext() then
-                      prefix.Add(enumerator.Current)
-                  else
-                      enumerator.Dispose()     // Move failed, dispose enumerator,
-                      enumeratorR <- Some None // drop it and record finished.
-              | None -> ()
+            // If possible, step the enumeration to prefix length i (at most one step).
+            // Be speculative, since this could have already happened via another thread.
+            if i >= prefix.Count then // is a step still required?
+                // If not yet started, start it (create enumerator).
+                let optEnumerator =
+                    match enumeratorR with
+                    | None ->
+                        let optEnumerator = Some(source.GetEnumerator())
+                        enumeratorR <- Some optEnumerator
+                        optEnumerator
+                    | Some optEnumerator -> optEnumerator
+
+                match optEnumerator with
+                | Some enumerator ->
+                    if enumerator.MoveNext() then
+                        prefix.Add(enumerator.Current)
+                    else
+                        enumerator.Dispose() // Move failed, dispose enumerator,
+                        enumeratorR <- Some None // drop it and record finished.
+                | None -> ()
 
         let result =
-            unfold (fun i ->
-                // i being the next position to be returned
-                // A lock is needed over the reads to prefix.Count since the list may be being resized
-                // NOTE: we could change to a reader/writer lock here
-                lock prefix (fun () ->
-                    if i < prefix.Count then
-                        Some (prefix.[i],i+1)
-                    else
-                        oneStepTo i
+            unfold
+                (fun i ->
+                    // i being the next position to be returned
+                    // A lock is needed over the reads to prefix.Count since the list may be being resized
+                    // NOTE: we could change to a reader/writer lock here
+                    lock prefix (fun () ->
                         if i < prefix.Count then
-                            Some (prefix.[i],i+1)
+                            Some(prefix.[i], i + 1)
                         else
-                            None)) 0
-        let cleanup() =
-           lock prefix (fun () ->
-               prefix.Clear()
-               match enumeratorR with
-               | Some (Some e) -> IEnumerator.dispose e
-               | _ -> ()
-               enumeratorR <- None)
+                            oneStepTo i
+
+                            if i < prefix.Count then
+                                Some(prefix.[i], i + 1)
+                            else
+                                None))
+                0
+
+        let cleanup () =
+            lock prefix (fun () ->
+                prefix.Clear()
+
+                match enumeratorR with
+                | Some (Some e) -> IEnumerator.dispose e
+                | _ -> ()
+
+                enumeratorR <- None)
 
         (new CachedSeq<_>(cleanup, result) :> seq<_>)
 
@@ -1074,14 +1240,19 @@ module Seq =
         source1 |> collect (fun x -> cached |> map (fun y -> x, y))
 
     []
-    let readonly (source:seq<_>) =
+    let readonly (source: seq<_>) =
         checkNonNull "source" source
         mkSeq (fun () -> source.GetEnumerator())
 
-    let inline groupByImpl (comparer:IEqualityComparer<'SafeKey>) ([] keyf:'T->'SafeKey) ([] getKey:'SafeKey->'Key) (seq:seq<'T>) =
+    let inline groupByImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        ([] keyf: 'T -> 'SafeKey)
+        ([] getKey: 'SafeKey -> 'Key)
+        (seq: seq<'T>)
+        =
         checkNonNull "seq" seq
 
-        let dict = Dictionary<_,ResizeArray<_>> comparer
+        let dict = Dictionary<_, ResizeArray<_>> comparer
 
         // Previously this was 1, but I think this is rather stingy, considering that we are already paying
         // for at least a key, the ResizeArray reference, which includes an array reference, an Entry in the
@@ -1089,63 +1260,77 @@ module Seq =
         let minimumBucketSize = 4
 
         // Build the groupings
-        seq |> iter (fun v ->
+        seq
+        |> iter (fun v ->
             let safeKey = keyf v
             let mutable prev = Unchecked.defaultof<_>
-            match dict.TryGetValue (safeKey, &prev) with
+
+            match dict.TryGetValue(safeKey, &prev) with
             | true -> prev.Add v
             | false ->
-                let prev = ResizeArray ()
+                let prev = ResizeArray()
                 dict.[safeKey] <- prev
                 prev.Add v)
 
         // Trim the size of each result group, don't trim very small buckets, as excessive work, and garbage for
         // minimal gain
-        dict |> iter (fun group -> if group.Value.Count > minimumBucketSize then group.Value.TrimExcess())
+        dict
+        |> iter (fun group ->
+            if group.Value.Count > minimumBucketSize then
+                group.Value.TrimExcess())
 
         // Return the sequence-of-sequences. Don't reveal the
         // internal collections: just reveal them as sequences
         dict |> map (fun group -> (getKey group.Key, readonly group.Value))
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
-    let groupByValueType (keyf:'T->'Key) (seq:seq<'T>) = seq |> groupByImpl HashIdentity.Structural<'Key> keyf id
+    let groupByValueType (keyf: 'T -> 'Key) (seq: seq<'T>) =
+        seq |> groupByImpl HashIdentity.Structural<'Key> keyf id
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
-    let groupByRefType   (keyf:'T->'Key) (seq:seq<'T>) = seq |> groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox (keyf t)) (fun sb -> sb.Value)
+    let groupByRefType (keyf: 'T -> 'Key) (seq: seq<'T>) =
+        seq
+        |> groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value)
 
     []
-    let groupBy (projection:'T->'Key) (source:seq<'T>) =
-        if typeof<'Key>.IsValueType
-            then mkDelayedSeq (fun () -> groupByValueType projection source)
-            else mkDelayedSeq (fun () -> groupByRefType   projection source)
+    let groupBy (projection: 'T -> 'Key) (source: seq<'T>) =
+        if typeof<'Key>.IsValueType then
+            mkDelayedSeq (fun () -> groupByValueType projection source)
+        else
+            mkDelayedSeq (fun () -> groupByRefType projection source)
 
     []
     let transpose (source: seq<#seq<'T>>) =
         checkNonNull "source" source
-        source
-        |> collect indexed
-        |> groupBy fst
-        |> map (snd >> (map snd))
+        source |> collect indexed |> groupBy fst |> map (snd >> (map snd))
 
     []
     let distinct source =
         checkNonNull "source" source
-        seq { let hashSet = HashSet<'T>(HashIdentity.Structural<'T>)
-              for v in source do
-                  if hashSet.Add v then
-                      yield v }
+
+        seq {
+            let hashSet = HashSet<'T>(HashIdentity.Structural<'T>)
+
+            for v in source do
+                if hashSet.Add v then yield v
+        }
 
     []
     let distinctBy projection source =
         checkNonNull "source" source
-        seq { let hashSet = HashSet<_>(HashIdentity.Structural<_>)
-              for v in source do
+
+        seq {
+            let hashSet = HashSet<_>(HashIdentity.Structural<_>)
+
+            for v in source do
                 if hashSet.Add(projection v) then
-                    yield v }
+                    yield v
+        }
 
     []
     let sortBy projection source =
         checkNonNull "source" source
+
         mkDelayedSeq (fun () ->
             let array = source |> toArray
             Array.stableSortInPlaceBy projection array
@@ -1154,6 +1339,7 @@ module Seq =
     []
     let sort source =
         checkNonNull "source" source
+
         mkDelayedSeq (fun () ->
             let array = source |> toArray
             Array.stableSortInPlace array
@@ -1162,6 +1348,7 @@ module Seq =
     []
     let sortWith comparer source =
         checkNonNull "source" source
+
         mkDelayedSeq (fun () ->
             let array = source |> toArray
             Array.stableSortInPlaceWith comparer array
@@ -1170,175 +1357,240 @@ module Seq =
     []
     let inline sortByDescending projection source =
         checkNonNull "source" source
-        let inline compareDescending a b = compare (projection b) (projection a)
+
+        let inline compareDescending a b =
+            compare (projection b) (projection a)
+
         sortWith compareDescending source
 
     []
     let inline sortDescending source =
         checkNonNull "source" source
-        let inline compareDescending a b = compare b a
+
+        let inline compareDescending a b =
+            compare b a
+
         sortWith compareDescending source
 
-    let inline countByImpl (comparer:IEqualityComparer<'SafeKey>) ([] keyf:'T->'SafeKey) ([] getKey:'SafeKey->'Key) (source:seq<'T>) =
+    let inline countByImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        ([] keyf: 'T -> 'SafeKey)
+        ([] getKey: 'SafeKey -> 'Key)
+        (source: seq<'T>)
+        =
         checkNonNull "source" source
 
         let dict = Dictionary comparer
 
         // Build the groupings
-        source |> iter (fun v ->
+        source
+        |> iter (fun v ->
             let safeKey = keyf v
             let mutable prev = Unchecked.defaultof<_>
-            if dict.TryGetValue(safeKey, &prev)
-                then dict.[safeKey] <- prev + 1
-                else dict.[safeKey] <- 1)
+
+            if dict.TryGetValue(safeKey, &prev) then
+                dict.[safeKey] <- prev + 1
+            else
+                dict.[safeKey] <- 1)
 
         dict |> map (fun group -> (getKey group.Key, group.Value))
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
-    let countByValueType (keyf:'T->'Key) (seq:seq<'T>) = seq |> countByImpl HashIdentity.Structural<'Key> keyf id
+    let countByValueType (keyf: 'T -> 'Key) (seq: seq<'T>) =
+        seq |> countByImpl HashIdentity.Structural<'Key> keyf id
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
-    let countByRefType   (keyf:'T->'Key) (seq:seq<'T>) = seq |> countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox (keyf t)) (fun sb -> sb.Value)
+    let countByRefType (keyf: 'T -> 'Key) (seq: seq<'T>) =
+        seq
+        |> countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value)
 
     []
-    let countBy (projection:'T->'Key) (source:seq<'T>) =
+    let countBy (projection: 'T -> 'Key) (source: seq<'T>) =
         checkNonNull "source" source
 
-        if typeof<'Key>.IsValueType
-            then mkDelayedSeq (fun () -> countByValueType projection source)
-            else mkDelayedSeq (fun () -> countByRefType   projection source)
+        if typeof<'Key>.IsValueType then
+            mkDelayedSeq (fun () -> countByValueType projection source)
+        else
+            mkDelayedSeq (fun () -> countByRefType projection source)
 
     []
-    let inline sum (source: seq< ^a>) : ^a =
+    let inline sum (source: seq< ^a >) : ^a =
         use e = source.GetEnumerator()
         let mutable acc = LanguagePrimitives.GenericZero< ^a>
+
         while e.MoveNext() do
             acc <- Checked.(+) acc e.Current
+
         acc
 
     []
-    let inline sumBy ([] projection : 'T -> ^U) (source: seq<'T>) : ^U =
+    let inline sumBy ([] projection: 'T -> ^U) (source: seq<'T>) : ^U =
         use e = source.GetEnumerator()
         let mutable acc = LanguagePrimitives.GenericZero< ^U>
+
         while e.MoveNext() do
             acc <- Checked.(+) acc (projection e.Current)
+
         acc
 
     []
-    let inline average (source: seq< ^a>) : ^a =
+    let inline average (source: seq< ^a >) : ^a =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable acc = LanguagePrimitives.GenericZero< ^a>
         let mutable count = 0
+
         while e.MoveNext() do
             acc <- Checked.(+) acc e.Current
             count <- count + 1
+
         if count = 0 then
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         LanguagePrimitives.DivideByInt< ^a> acc count
 
     []
-    let inline averageBy ([] projection : 'T -> ^U) (source: seq<'T>) : ^U =
+    let inline averageBy ([] projection: 'T -> ^U) (source: seq<'T>) : ^U =
         checkNonNull "source" source
         use e = source.GetEnumerator()
         let mutable acc = LanguagePrimitives.GenericZero< ^U>
         let mutable count = 0
+
         while e.MoveNext() do
             acc <- Checked.(+) acc (projection e.Current)
             count <- count + 1
+
         if count = 0 then
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         LanguagePrimitives.DivideByInt< ^U> acc count
 
     []
     let inline min (source: seq<_>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         if not (e.MoveNext()) then
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         let mutable acc = e.Current
+
         while e.MoveNext() do
             let curr = e.Current
-            if curr < acc then
-                acc <- curr
+            if curr < acc then acc <- curr
+
         acc
 
     []
-    let inline minBy (projection : 'T -> 'U) (source: seq<'T>) : 'T =
+    let inline minBy (projection: 'T -> 'U) (source: seq<'T>) : 'T =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         if not (e.MoveNext()) then
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         let first = e.Current
         let mutable acc = projection first
         let mutable accv = first
+
         while e.MoveNext() do
             let currv = e.Current
             let curr = projection currv
+
             if curr < acc then
                 acc <- curr
                 accv <- currv
+
         accv
 
     []
     let inline max (source: seq<_>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         if not (e.MoveNext()) then
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         let mutable acc = e.Current
+
         while e.MoveNext() do
             let curr = e.Current
-            if curr > acc then
-                acc <- curr
+            if curr > acc then acc <- curr
+
         acc
 
     []
-    let inline maxBy (projection : 'T -> 'U) (source: seq<'T>) : 'T =
+    let inline maxBy (projection: 'T -> 'U) (source: seq<'T>) : 'T =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         if not (e.MoveNext()) then
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
         let first = e.Current
         let mutable acc = projection first
         let mutable accv = first
+
         while e.MoveNext() do
             let currv = e.Current
             let curr = projection currv
+
             if curr > acc then
                 acc <- curr
                 accv <- currv
+
         accv
 
     []
     let takeWhile predicate (source: seq<_>) =
         checkNonNull "source" source
-        seq { use e = source.GetEnumerator()
-              let mutable latest = Unchecked.defaultof<_>
-              while e.MoveNext() && (latest <- e.Current; predicate latest) do
-                  yield latest }
+
+        seq {
+            use e = source.GetEnumerator()
+            let mutable latest = Unchecked.defaultof<_>
+
+            while e.MoveNext()
+                  && (latest <- e.Current
+                      predicate latest) do
+                yield latest
+        }
 
     []
     let skip count (source: seq<_>) =
         checkNonNull "source" source
-        seq { use e = source.GetEnumerator()
-              for x in 1 .. count do
-                  if not (e.MoveNext()) then
-                    invalidOpFmt "tried to skip {0} {1} past the end of the seq"
-                      [|SR.GetString SR.notEnoughElements; x; (if x=1 then "element" else "elements")|]
-              while e.MoveNext() do
-                  yield e.Current }
+
+        seq {
+            use e = source.GetEnumerator()
+
+            for x in 1..count do
+                if not (e.MoveNext()) then
+                    invalidOpFmt
+                        "tried to skip {0} {1} past the end of the seq"
+                        [|
+                            SR.GetString SR.notEnoughElements
+                            x
+                            (if x = 1 then "element" else "elements")
+                        |]
+
+            while e.MoveNext() do
+                yield e.Current
+        }
 
     []
     let skipWhile predicate (source: seq<_>) =
         checkNonNull "source" source
-        seq { use e = source.GetEnumerator()
-              let mutable latest = Unchecked.defaultof<_>
-              let mutable ok = false
-              while e.MoveNext() do
-                  if (latest <- e.Current; (ok || not (predicate latest))) then
-                      ok <- true
-                      yield latest }
+
+        seq {
+            use e = source.GetEnumerator()
+            let mutable latest = Unchecked.defaultof<_>
+            let mutable ok = false
+
+            while e.MoveNext() do
+                if (latest <- e.Current
+                    (ok || not (predicate latest))) then
+                    ok <- true
+                    yield latest
+        }
 
     []
     let forall2 predicate (source1: seq<_>) (source2: seq<_>) =
@@ -1346,10 +1598,12 @@ module Seq =
         checkNonNull "source2" source2
         use e1 = source1.GetEnumerator()
         use e2 = source2.GetEnumerator()
-        let p = OptimizedClosures.FSharpFunc<_,_,_>.Adapt predicate
+        let p = OptimizedClosures.FSharpFunc<_, _, _>.Adapt predicate
         let mutable ok = true
+
         while (ok && e1.MoveNext() && e2.MoveNext()) do
             ok <- p.Invoke(e1.Current, e2.Current)
+
         ok
 
     []
@@ -1358,55 +1612,72 @@ module Seq =
         checkNonNull "source2" source2
         use e1 = source1.GetEnumerator()
         use e2 = source2.GetEnumerator()
-        let p = OptimizedClosures.FSharpFunc<_,_,_>.Adapt predicate
+        let p = OptimizedClosures.FSharpFunc<_, _, _>.Adapt predicate
         let mutable ok = false
+
         while (not ok && e1.MoveNext() && e2.MoveNext()) do
             ok <- p.Invoke(e1.Current, e2.Current)
+
         ok
 
     []
-    let head (source : seq<_>) =
+    let head (source: seq<_>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
-        if (e.MoveNext()) then e.Current
-        else invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
+
+        if (e.MoveNext()) then
+            e.Current
+        else
+            invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
 
     []
-    let tryHead (source : seq<_>) =
+    let tryHead (source: seq<_>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
-        if (e.MoveNext()) then Some e.Current
-        else None
+
+        if (e.MoveNext()) then
+            Some e.Current
+        else
+            None
 
     []
     let tail (source: seq<'T>) =
         checkNonNull "source" source
-        seq { use e = source.GetEnumerator()
-              if not (e.MoveNext()) then
-                  invalidArg "source" (SR.GetString(SR.notEnoughElements))
-              while e.MoveNext() do
-                  yield e.Current }
-                       
+
+        seq {
+            use e = source.GetEnumerator()
+
+            if not (e.MoveNext()) then
+                invalidArg "source" (SR.GetString(SR.notEnoughElements))
+
+            while e.MoveNext() do
+                yield e.Current
+        }
+
     []
-    let last (source : seq<_>) =
+    let last (source: seq<_>) =
         checkNonNull "source" source
+
         match Microsoft.FSharp.Primitives.Basics.Seq.tryLastV source with
         | ValueSome x -> x
         | ValueNone -> invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
-    
+
     []
-    let tryLast (source : seq<_>) =
+    let tryLast (source: seq<_>) =
         checkNonNull "source" source
+
         match Microsoft.FSharp.Primitives.Basics.Seq.tryLastV source with
         | ValueSome x -> Some x
         | ValueNone -> None
-        
+
     []
-    let exactlyOne (source : seq<_>) =
+    let exactlyOne (source: seq<_>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         if e.MoveNext() then
             let v = e.Current
+
             if e.MoveNext() then
                 invalidArg "source" (SR.GetString(SR.inputSequenceTooLong))
             else
@@ -1415,43 +1686,41 @@ module Seq =
             invalidArg "source" LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
 
     []
-    let tryExactlyOne (source : seq<_>) =
+    let tryExactlyOne (source: seq<_>) =
         checkNonNull "source" source
         use e = source.GetEnumerator()
+
         if e.MoveNext() then
             let v = e.Current
-            if e.MoveNext() then
-                None
-            else
-                Some v
+            if e.MoveNext() then None else Some v
         else
             None
 
     []
     let rev source =
         checkNonNull "source" source
+
         mkDelayedSeq (fun () ->
             let array = source |> toArray
             Array.Reverse array
             array :> seq<_>)
 
     []
-    let permute indexMap (source : seq<_>) =
+    let permute indexMap (source: seq<_>) =
         checkNonNull "source" source
-        mkDelayedSeq (fun () ->
-            source |> toArray |> Array.permute indexMap :> seq<_>)
+        mkDelayedSeq (fun () -> source |> toArray |> Array.permute indexMap :> seq<_>)
 
     []
-    let mapFold<'T,'State,'Result> (mapping: 'State -> 'T -> 'Result * 'State) state source =
+    let mapFold<'T, 'State, 'Result> (mapping: 'State -> 'T -> 'Result * 'State) state source =
         checkNonNull "source" source
-        let arr,state = source |> toArray |> Array.mapFold mapping state
+        let arr, state = source |> toArray |> Array.mapFold mapping state
         readonly arr, state
 
     []
-    let mapFoldBack<'T,'State,'Result> (mapping: 'T -> 'State -> 'Result * 'State) source state =
+    let mapFoldBack<'T, 'State, 'Result> (mapping: 'T -> 'State -> 'Result * 'State) source state =
         checkNonNull "source" source
         let array = source |> toArray
-        let arr,state = Array.mapFoldBack mapping array state
+        let arr, state = Array.mapFoldBack mapping array state
         readonly arr, state
 
     []
@@ -1461,102 +1730,142 @@ module Seq =
 
         seq {
             use e = source.GetEnumerator()
+
             if e.MoveNext() then
                 let cached = HashSet(itemsToExclude, HashIdentity.Structural)
                 let next = e.Current
                 if cached.Add next then yield next
+
                 while e.MoveNext() do
                     let next = e.Current
-                    if cached.Add next then yield next }
+                    if cached.Add next then yield next
+        }
 
     []
-    let chunkBySize chunkSize (source : seq<_>) =
-        checkNonNull "source" source
-        if chunkSize <= 0 then invalidArgFmt "chunkSize" "{0}\nchunkSize = {1}"
-                                [|SR.GetString SR.inputMustBePositive; chunkSize|]
-        seq { use e = source.GetEnumerator()
-              let nextChunk() =
-                  let res = Array.zeroCreateUnchecked chunkSize
-                  res.[0] <- e.Current
-                  let mutable i = 1
-                  while i < chunkSize && e.MoveNext() do
-                      res.[i] <- e.Current
-                      i <- i + 1
-                  if i = chunkSize then
-                      res
-                  else
-                      res |> Array.subUnchecked 0 i
-              while e.MoveNext() do
-                  yield nextChunk() }
+    let chunkBySize chunkSize (source: seq<_>) =
+        checkNonNull "source" source
+
+        if chunkSize <= 0 then
+            invalidArgFmt "chunkSize" "{0}\nchunkSize = {1}" [| SR.GetString SR.inputMustBePositive; chunkSize |]
+
+        seq {
+            use e = source.GetEnumerator()
+
+            let nextChunk () =
+                let res = Array.zeroCreateUnchecked chunkSize
+                res.[0] <- e.Current
+                let mutable i = 1
+
+                while i < chunkSize && e.MoveNext() do
+                    res.[i] <- e.Current
+                    i <- i + 1
+
+                if i = chunkSize then
+                    res
+                else
+                    res |> Array.subUnchecked 0 i
+
+            while e.MoveNext() do
+                yield nextChunk ()
+        }
 
     []
     let splitInto count source =
         checkNonNull "source" source
-        if count <= 0 then invalidArgFmt "count" "{0}\ncount = {1}"
-                            [|SR.GetString SR.inputMustBePositive; count|]
-        mkDelayedSeq (fun () ->
-            source |> toArray |> Array.splitInto count :> seq<_>)
+
+        if count <= 0 then
+            invalidArgFmt "count" "{0}\ncount = {1}" [| SR.GetString SR.inputMustBePositive; count |]
+
+        mkDelayedSeq (fun () -> source |> toArray |> Array.splitInto count :> seq<_>)
 
     []
     let removeAt (index: int) (source: seq<'T>) : seq<'T> =
-        if index < 0 then invalidArg "index" "index must be within bounds of the array"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the array"
+
         seq {
             let mutable i = 0
+
             for item in source do
-                if i <> index then 
-                    yield item
+                if i <> index then yield item
                 i <- i + 1
-            if i <= index then invalidArg "index" "index must be within bounds of the array"
+
+            if i <= index then
+                invalidArg "index" "index must be within bounds of the array"
         }
 
     []
     let removeManyAt (index: int) (count: int) (source: seq<'T>) : seq<'T> =
-        if index < 0 then invalidArg "index" "index must be within bounds of the array"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the array"
+
         seq {
             let mutable i = 0
+
             for item in source do
-                if i < index || i >= index + count then 
+                if i < index || i >= index + count then
                     yield item
+
                 i <- i + 1
-            if i <= index then invalidArg "index" "index must be within bounds of the array"
+
+            if i <= index then
+                invalidArg "index" "index must be within bounds of the array"
         }
 
     []
     let updateAt (index: int) (value: 'T) (source: seq<'T>) : seq<'T> =
-        if index < 0 then invalidArg "index" "index must be within bounds of the array"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the array"
+
         seq {
             let mutable i = 0
+
             for item in source do
                 if i <> index then
                     yield item
-                else yield value
+                else
+                    yield value
+
                 i <- i + 1
-            if i <= index then invalidArg "index" "index must be within bounds of the array"
+
+            if i <= index then
+                invalidArg "index" "index must be within bounds of the array"
         }
 
     []
     let insertAt (index: int) (value: 'T) (source: seq<'T>) : seq<'T> =
-        if index < 0 then invalidArg "index" "index must be within bounds of the array"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the array"
+
         seq {
             let mutable i = 0
+
             for item in source do
-                if i = index then
-                    yield value
+                if i = index then yield value
                 yield item
                 i <- i + 1
+
             if i = index then yield value
-            if i < index then invalidArg "index" "index must be within bounds of the array"
+
+            if i < index then
+                invalidArg "index" "index must be within bounds of the array"
         }
 
     []
     let insertManyAt (index: int) (values: seq<'T>) (source: seq<'T>) : seq<'T> =
-        if index < 0 then invalidArg "index" "index must be within bounds of the array"
+        if index < 0 then
+            invalidArg "index" "index must be within bounds of the array"
+
         seq {
             let mutable i = 0
+
             for item in source do
                 if i = index then yield! values
-                yield item 
+                yield item
                 i <- i + 1
+
             if i = index then yield! values // support inserting at the end
-            if i < index then invalidArg "index" "index must be within bounds of the array"
-        }
\ No newline at end of file
+
+            if i < index then
+                invalidArg "index" "index must be within bounds of the array"
+        }
diff --git a/src/FSharp.Core/set.fs b/src/FSharp.Core/set.fs
index e59d3052168..1734dc1a54d 100644
--- a/src/FSharp.Core/set.fs
+++ b/src/FSharp.Core/set.fs
@@ -19,37 +19,38 @@ open Microsoft.FSharp.Collections
 type internal SetTree<'T>(k: 'T, h: int) =
     member _.Height = h
     member _.Key = k
-    new(k: 'T) = SetTree(k,1)
-    
+    new(k: 'T) = SetTree(k, 1)
+
 []
 []
 []
-type internal SetTreeNode<'T>(v:'T, left:SetTree<'T>, right: SetTree<'T>, h: int) =
-    inherit SetTree<'T>(v,h)
+type internal SetTreeNode<'T>(v: 'T, left: SetTree<'T>, right: SetTree<'T>, h: int) =
+    inherit SetTree<'T>(v, h)
     member _.Left = left
     member _.Right = right
-    
+
 []
-module internal SetTree = 
-    
+module internal SetTree =
+
     let empty = null
-    
-    let inline isEmpty (t:SetTree<'T>) = isNull t
 
-    let inline private asNode(value:SetTree<'T>) : SetTreeNode<'T> =
+    let inline isEmpty (t: SetTree<'T>) =
+        isNull t
+
+    let inline private asNode (value: SetTree<'T>) : SetTreeNode<'T> =
         value :?> SetTreeNode<'T>
-                
-    let rec countAux (t:SetTree<'T>) acc = 
+
+    let rec countAux (t: SetTree<'T>) acc =
         if isEmpty t then
             acc
+        else if t.Height = 1 then
+            acc + 1
         else
-            if t.Height = 1 then
-                acc + 1
-            else
-                let tn = asNode t
-                countAux tn.Left (countAux tn.Right (acc+1)) 
+            let tn = asNode t
+            countAux tn.Left (countAux tn.Right (acc + 1))
 
-    let count s = countAux s 0
+    let count s =
+        countAux s 0
 
 #if TRACE_SETS_AND_MAPS
     let mutable traceCount = 0
@@ -63,377 +64,472 @@ module internal SetTree =
     let mutable totalSizeOnSetAdd = 0.0
     let mutable totalSizeOnSetLookup = 0.0
 
-    let report() = 
-       traceCount <- traceCount + 1 
-       if traceCount % 10000 = 0 then 
-           System.Console.WriteLine(
+    let report () =
+        traceCount <- traceCount + 1
+
+        if traceCount % 10000 = 0 then
+            System.Console.WriteLine(
                 "#SetOne = {0}, #SetNode = {1}, #Add = {2}, #Remove = {3}, #Unions = {4}, #Lookups = {5}, avSetSizeOnNodeCreation = {6}, avSetSizeOnSetCreation = {7}, avSetSizeOnSetLookup = {8}",
-                numOnes, numNodes, numAdds, numRemoves, numUnions, numLookups,
+                numOnes,
+                numNodes,
+                numAdds,
+                numRemoves,
+                numUnions,
+                numLookups,
                 (totalSizeOnNodeCreation / float (numNodes + numOnes)),
                 (totalSizeOnSetAdd / float numAdds),
-                (totalSizeOnSetLookup / float numLookups))
+                (totalSizeOnSetLookup / float numLookups)
+            )
 
-    let SetTree n = 
-        report()
+    let SetTree n =
+        report ()
         numOnes <- numOnes + 1
         totalSizeOnNodeCreation <- totalSizeOnNodeCreation + 1.0
         SetTree n
 
-    let SetTreeNode (x, l, r, h) = 
-        report()
+    let SetTreeNode (x, l, r, h) =
+        report ()
         numNodes <- numNodes + 1
-        let n = SetTreeNode (x, l, r, h)
+        let n = SetTreeNode(x, l, r, h)
         totalSizeOnNodeCreation <- totalSizeOnNodeCreation + float (count n)
         n
 #endif
 
-    let inline height (t:SetTree<'T>) = 
-        if isEmpty t then 0
-        else t.Height 
+    let inline height (t: SetTree<'T>) =
+        if isEmpty t then 0 else t.Height
 
     []
     let private tolerance = 2
 
-    let mk l k r : SetTree<'T> = 
-        let hl = height l 
-        let hr = height r 
+    let mk l k r : SetTree<'T> =
+        let hl = height l
+        let hr = height r
         let m = if hl < hr then hr else hl
+
         if m = 0 then // m=0 ~ isEmpty l && isEmpty r
             SetTree k
         else
-            SetTreeNode (k, l, r, m+1) :> SetTree<'T>
+            SetTreeNode(k, l, r, m + 1) :> SetTree<'T>
 
     let rebalance t1 v t2 =
-        let t1h = height t1 
-        let t2h = height t2 
-        if  t2h > t1h + tolerance then // right is heavier than left 
-            let t2' = asNode(t2)
-            // one of the nodes must have height > height t1 + 1 
-            if height t2'.Left > t1h + 1 then  // balance left: combination 
-                let t2l = asNode(t2'.Left)
-                mk (mk t1 v t2l.Left) t2l.Key (mk t2l.Right t2'.Key t2'.Right) 
+        let t1h = height t1
+        let t2h = height t2
+
+        if t2h > t1h + tolerance then // right is heavier than left
+            let t2' = asNode (t2)
+            // one of the nodes must have height > height t1 + 1
+            if height t2'.Left > t1h + 1 then // balance left: combination
+                let t2l = asNode (t2'.Left)
+                mk (mk t1 v t2l.Left) t2l.Key (mk t2l.Right t2'.Key t2'.Right)
             else // rotate left
                 mk (mk t1 v t2'.Left) t2.Key t2'.Right
+        else if t1h > t2h + tolerance then // left is heavier than right
+            let t1' = asNode (t1)
+            // one of the nodes must have height > height t2 + 1
+            if height t1'.Right > t2h + 1 then
+                // balance right: combination
+                let t1r = asNode (t1'.Right)
+                mk (mk t1'.Left t1.Key t1r.Left) t1r.Key (mk t1r.Right v t2)
+            else
+                mk t1'.Left t1'.Key (mk t1'.Right v t2)
         else
-            if  t1h > t2h + tolerance then // left is heavier than right
-                let t1' = asNode(t1)
-                // one of the nodes must have height > height t2 + 1 
-                if height t1'.Right > t2h + 1 then 
-                    // balance right: combination
-                    let t1r = asNode(t1'.Right)
-                    mk (mk t1'.Left t1.Key t1r.Left) t1r.Key (mk t1r.Right v t2)
-                else
-                    mk t1'.Left t1'.Key (mk t1'.Right v t2)
-            else mk t1 v t2
+            mk t1 v t2
 
-    let rec add (comparer: IComparer<'T>) k (t:SetTree<'T>) : SetTree<'T> = 
-        if isEmpty t then SetTree k
+    let rec add (comparer: IComparer<'T>) k (t: SetTree<'T>) : SetTree<'T> =
+        if isEmpty t then
+            SetTree k
         else
             let c = comparer.Compare(k, t.Key)
+
             if t.Height = 1 then
-                // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated 
-                if c < 0   then SetTreeNode (k, empty, t, 2) :> SetTree<'T>
-                elif c = 0 then t
-                else            SetTreeNode (k, t, empty, 2) :> SetTree<'T>
+                // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated
+                if c < 0 then
+                    SetTreeNode(k, empty, t, 2) :> SetTree<'T>
+                elif c = 0 then
+                    t
+                else
+                    SetTreeNode(k, t, empty, 2) :> SetTree<'T>
             else
                 let tn = asNode t
-                if   c < 0 then rebalance (add comparer k tn.Left) tn.Key tn.Right
-                elif c = 0 then t
-                else            rebalance tn.Left tn.Key (add comparer k tn.Right)
 
-    let rec balance comparer (t1:SetTree<'T>) k (t2:SetTree<'T>) =
-        // Given t1 < k < t2 where t1 and t2 are "balanced", 
+                if c < 0 then
+                    rebalance (add comparer k tn.Left) tn.Key tn.Right
+                elif c = 0 then
+                    t
+                else
+                    rebalance tn.Left tn.Key (add comparer k tn.Right)
+
+    let rec balance comparer (t1: SetTree<'T>) k (t2: SetTree<'T>) =
+        // Given t1 < k < t2 where t1 and t2 are "balanced",
         // return a balanced tree for .
         // Recall: balance means subtrees heights differ by at most "tolerance"
-        if isEmpty t1 then add comparer k t2 // drop t1 = empty
-        elif isEmpty t2 then add comparer k t1 // drop t2 = empty
+        if isEmpty t1 then
+            add comparer k t2 // drop t1 = empty
+        elif isEmpty t2 then
+            add comparer k t1 // drop t2 = empty
+        else if t1.Height = 1 then
+            add comparer k (add comparer t1.Key t2)
         else
-            if t1.Height = 1 then add comparer k (add comparer t1.Key t2)
+            let t1n = asNode t1
+
+            if t2.Height = 1 then
+                add comparer k (add comparer t2.Key t1)
             else
-                let t1n = asNode t1
-                if t2.Height = 1 then add comparer k (add comparer t2.Key t1)
+                let t2n = asNode t2
+                // Have:  (t1l < k1 < t1r) < k < (t2l < k2 < t2r)
+                // Either (a) h1, h2 differ by at most 2 - no rebalance needed.
+                //        (b) h1 too small, i.e. h1+2 < h2
+                //        (c) h2 too small, i.e. h2+2 < h1
+                if t1n.Height + tolerance < t2n.Height then
+                    // case: b, h1 too small
+                    // push t1 into low side of t2, may increase height by 1 so rebalance
+                    rebalance (balance comparer t1 k t2n.Left) t2n.Key t2n.Right
+                elif t2n.Height + tolerance < t1n.Height then
+                    // case: c, h2 too small
+                    // push t2 into high side of t1, may increase height by 1 so rebalance
+                    rebalance t1n.Left t1n.Key (balance comparer t1n.Right k t2)
                 else
-                    let t2n = asNode t2
-                    // Have:  (t1l < k1 < t1r) < k < (t2l < k2 < t2r)
-                    // Either (a) h1, h2 differ by at most 2 - no rebalance needed.
-                    //        (b) h1 too small, i.e. h1+2 < h2
-                    //        (c) h2 too small, i.e. h2+2 < h1 
-                    if t1n.Height + tolerance < t2n.Height then
-                        // case: b, h1 too small 
-                        // push t1 into low side of t2, may increase height by 1 so rebalance 
-                        rebalance (balance comparer t1 k t2n.Left) t2n.Key t2n.Right
-                    elif t2n.Height + tolerance < t1n.Height then
-                        // case: c, h2 too small 
-                        // push t2 into high side of t1, may increase height by 1 so rebalance 
-                        rebalance t1n.Left t1n.Key (balance comparer t1n.Right k t2)
-                    else
-                        // case: a, h1 and h2 meet balance requirement 
-                        mk t1 k t2
+                    // case: a, h1 and h2 meet balance requirement
+                    mk t1 k t2
 
-    let rec split (comparer: IComparer<'T>) pivot (t:SetTree<'T>) =
+    let rec split (comparer: IComparer<'T>) pivot (t: SetTree<'T>) =
         // Given a pivot and a set t
-        // Return { x in t s.t. x < pivot }, pivot in t?, { x in t s.t. x > pivot } 
-        if isEmpty t then empty, false, empty
+        // Return { x in t s.t. x < pivot }, pivot in t?, { x in t s.t. x > pivot }
+        if isEmpty t then
+            empty, false, empty
+        else if t.Height = 1 then
+            let c = comparer.Compare(t.Key, pivot)
+
+            if c < 0 then t, false, empty // singleton under pivot
+            elif c = 0 then empty, true, empty // singleton is    pivot
+            else empty, false, t // singleton over  pivot
         else
-            if t.Height = 1 then
-                let c = comparer.Compare(t.Key, pivot)
-                if   c < 0 then t, false, empty // singleton under pivot 
-                elif c = 0 then empty, true, empty // singleton is    pivot 
-                else            empty, false, t        // singleton over  pivot
-            else
-                let tn = asNode t
-                let c = comparer.Compare(pivot, tn.Key)
-                if   c < 0 then // pivot t1 
-                    let t11Lo, havePivot, t11Hi = split comparer pivot tn.Left
-                    t11Lo, havePivot, balance comparer t11Hi tn.Key tn.Right
-                elif c = 0 then // pivot is k1 
-                    tn.Left, true, tn.Right
-                else            // pivot t2 
-                    let t12Lo, havePivot, t12Hi = split comparer pivot tn.Right
-                    balance comparer tn.Left tn.Key t12Lo, havePivot, t12Hi
-
-    let rec spliceOutSuccessor (t:SetTree<'T>) = 
-        if isEmpty t then failwith "internal error: Set.spliceOutSuccessor"
-        else
-            if t.Height = 1 then t.Key, empty
+            let tn = asNode t
+            let c = comparer.Compare(pivot, tn.Key)
+
+            if c < 0 then // pivot t1
+                let t11Lo, havePivot, t11Hi = split comparer pivot tn.Left
+                t11Lo, havePivot, balance comparer t11Hi tn.Key tn.Right
+            elif c = 0 then // pivot is k1
+                tn.Left, true, tn.Right
+            else // pivot t2
+                let t12Lo, havePivot, t12Hi = split comparer pivot tn.Right
+                balance comparer tn.Left tn.Key t12Lo, havePivot, t12Hi
+
+    let rec spliceOutSuccessor (t: SetTree<'T>) =
+        if isEmpty t then
+            failwith "internal error: Set.spliceOutSuccessor"
+        else if t.Height = 1 then
+            t.Key, empty
+        else
+            let tn = asNode t
+
+            if isEmpty tn.Left then
+                tn.Key, tn.Right
             else
-                let tn = asNode t
-                if isEmpty tn.Left then tn.Key, tn.Right
-                else let k3, l' = spliceOutSuccessor tn.Left in k3, mk l' tn.Key tn.Right
+                let k3, l' = spliceOutSuccessor tn.Left in k3, mk l' tn.Key tn.Right
 
-    let rec remove (comparer: IComparer<'T>) k (t:SetTree<'T>) = 
-        if isEmpty t then t
+    let rec remove (comparer: IComparer<'T>) k (t: SetTree<'T>) =
+        if isEmpty t then
+            t
         else
             let c = comparer.Compare(k, t.Key)
+
             if t.Height = 1 then
                 if c = 0 then empty else t
             else
                 let tn = asNode t
-                if   c < 0 then rebalance (remove comparer k tn.Left) tn.Key tn.Right
+
+                if c < 0 then
+                    rebalance (remove comparer k tn.Left) tn.Key tn.Right
                 elif c = 0 then
-                    if isEmpty tn.Left then tn.Right
-                    elif isEmpty tn.Right then tn.Left
+                    if isEmpty tn.Left then
+                        tn.Right
+                    elif isEmpty tn.Right then
+                        tn.Left
                     else
-                        let sk, r' = spliceOutSuccessor tn.Right 
+                        let sk, r' = spliceOutSuccessor tn.Right
                         mk tn.Left sk r'
-                else rebalance tn.Left tn.Key (remove comparer k tn.Right)               
+                else
+                    rebalance tn.Left tn.Key (remove comparer k tn.Right)
 
-    let rec mem (comparer: IComparer<'T>) k (t:SetTree<'T>) = 
-        if isEmpty t then false
+    let rec mem (comparer: IComparer<'T>) k (t: SetTree<'T>) =
+        if isEmpty t then
+            false
         else
-            let c = comparer.Compare(k, t.Key) 
-            if t.Height = 1 then (c = 0)
+            let c = comparer.Compare(k, t.Key)
+
+            if t.Height = 1 then
+                (c = 0)
             else
                 let tn = asNode t
-                if   c < 0 then mem comparer k tn.Left
+
+                if c < 0 then mem comparer k tn.Left
                 elif c = 0 then true
                 else mem comparer k tn.Right
 
-    let rec iter f (t:SetTree<'T>) = 
-        if isEmpty t then ()
+    let rec iter f (t: SetTree<'T>) =
+        if isEmpty t then
+            ()
+        else if t.Height = 1 then
+            f t.Key
         else
-            if t.Height = 1 then f t.Key
-            else
-                let tn = asNode t
-                iter f tn.Left; f tn.Key; iter f tn.Right 
+            let tn = asNode t
+            iter f tn.Left
+            f tn.Key
+            iter f tn.Right
 
-    let rec foldBackOpt (f:OptimizedClosures.FSharpFunc<_, _, _>) (t:SetTree<'T>) x = 
-        if isEmpty t then x
+    let rec foldBackOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) (t: SetTree<'T>) x =
+        if isEmpty t then
+            x
+        else if t.Height = 1 then
+            f.Invoke(t.Key, x)
         else
-            if t.Height = 1 then f.Invoke(t.Key, x)
-            else
-                let tn = asNode t
-                foldBackOpt f tn.Left (f.Invoke(tn.Key, (foldBackOpt f tn.Right x)))
+            let tn = asNode t
+            foldBackOpt f tn.Left (f.Invoke(tn.Key, (foldBackOpt f tn.Right x)))
 
-    let foldBack f m x = foldBackOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m x
+    let foldBack f m x =
+        foldBackOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m x
 
-    let rec foldOpt (f:OptimizedClosures.FSharpFunc<_, _, _>) x (t:SetTree<'T>) = 
-        if isEmpty t then x
+    let rec foldOpt (f: OptimizedClosures.FSharpFunc<_, _, _>) x (t: SetTree<'T>) =
+        if isEmpty t then
+            x
+        else if t.Height = 1 then
+            f.Invoke(x, t.Key)
         else
-            if t.Height = 1 then f.Invoke(x, t.Key)
-            else
-                let tn = asNode t 
-                let x = foldOpt f x tn.Left in 
-                let x = f.Invoke(x, tn.Key)
-                foldOpt f x tn.Right
+            let tn = asNode t
+            let x = foldOpt f x tn.Left in
+            let x = f.Invoke(x, tn.Key)
+            foldOpt f x tn.Right
 
-    let fold f x m = foldOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) x m
+    let fold f x m =
+        foldOpt (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) x m
 
-    let rec forall f (t:SetTree<'T>) = 
-        if isEmpty t then true
+    let rec forall f (t: SetTree<'T>) =
+        if isEmpty t then
+            true
+        else if t.Height = 1 then
+            f t.Key
         else
-            if t.Height = 1 then f t.Key
-            else
-                let tn = asNode t
-                f tn.Key && forall f tn.Left && forall f tn.Right
+            let tn = asNode t
+            f tn.Key && forall f tn.Left && forall f tn.Right
 
-    let rec exists f (t:SetTree<'T>) = 
-        if isEmpty t then false
+    let rec exists f (t: SetTree<'T>) =
+        if isEmpty t then
+            false
+        else if t.Height = 1 then
+            f t.Key
         else
-            if t.Height = 1 then f t.Key
-            else
-                let tn = asNode t
-                f tn.Key || exists f tn.Left || exists f tn.Right
+            let tn = asNode t
+            f tn.Key || exists f tn.Left || exists f tn.Right
 
-    let subset comparer a b  =
+    let subset comparer a b =
         forall (fun x -> mem comparer x b) a
 
-    let properSubset comparer a b  =
-        forall (fun x -> mem comparer x b) a && exists (fun x -> not (mem comparer x a)) b
+    let properSubset comparer a b =
+        forall (fun x -> mem comparer x b) a
+        && exists (fun x -> not (mem comparer x a)) b
 
-    let rec filterAux comparer f (t:SetTree<'T>) acc = 
-        if isEmpty t then acc
-        else
-            if t.Height = 1 then
-                if f t.Key then add comparer t.Key acc else acc
+    let rec filterAux comparer f (t: SetTree<'T>) acc =
+        if isEmpty t then
+            acc
+        else if t.Height = 1 then
+            if f t.Key then
+                add comparer t.Key acc
             else
-                let tn = asNode t
-                let acc = if f tn.Key then add comparer tn.Key acc else acc 
-                filterAux comparer f tn.Left (filterAux comparer f tn.Right acc)
+                acc
+        else
+            let tn = asNode t
+
+            let acc =
+                if f tn.Key then
+                    add comparer tn.Key acc
+                else
+                    acc
+
+            filterAux comparer f tn.Left (filterAux comparer f tn.Right acc)
 
-    let filter comparer f s = filterAux comparer f s empty
+    let filter comparer f s =
+        filterAux comparer f s empty
 
-    let rec diffAux comparer (t:SetTree<'T>) acc = 
-        if isEmpty acc then acc
+    let rec diffAux comparer (t: SetTree<'T>) acc =
+        if isEmpty acc then
+            acc
+        else if isEmpty t then
+            acc
+        else if t.Height = 1 then
+            remove comparer t.Key acc
         else
-            if isEmpty t then acc
-            else
-                if t.Height = 1 then remove comparer t.Key acc
-                else
-                    let tn = asNode t
-                    diffAux comparer tn.Left (diffAux comparer tn.Right (remove comparer tn.Key acc)) 
+            let tn = asNode t
+            diffAux comparer tn.Left (diffAux comparer tn.Right (remove comparer tn.Key acc))
 
-    let diff comparer a b = diffAux comparer b a
+    let diff comparer a b =
+        diffAux comparer b a
 
-    let rec union comparer (t1:SetTree<'T>) (t2:SetTree<'T>) =
-        // Perf: tried bruteForce for low heights, but nothing significant 
-        if isEmpty t1 then t2
-        elif isEmpty t2 then t1
+    let rec union comparer (t1: SetTree<'T>) (t2: SetTree<'T>) =
+        // Perf: tried bruteForce for low heights, but nothing significant
+        if isEmpty t1 then
+            t2
+        elif isEmpty t2 then
+            t1
+        else if t1.Height = 1 then
+            add comparer t1.Key t2
+        else if t2.Height = 1 then
+            add comparer t2.Key t1
         else
-            if t1.Height = 1 then add comparer t1.Key t2
+            let t1n = asNode t1
+            let t2n = asNode t2 // (t1l < k < t1r) AND (t2l < k2 < t2r)
+            // Divide and Conquer:
+            //   Suppose t1 is largest.
+            //   Split t2 using pivot k1 into lo and hi.
+            //   Union disjoint subproblems and then combine.
+            if t1n.Height > t2n.Height then
+                let lo, _, hi = split comparer t1n.Key t2 in
+
+                balance comparer (union comparer t1n.Left lo) t1n.Key (union comparer t1n.Right hi)
             else
-                if t2.Height = 1 then add comparer t2.Key t1
-                else
-                    let t1n = asNode t1
-                    let t2n = asNode t2 // (t1l < k < t1r) AND (t2l < k2 < t2r) 
-                    // Divide and Conquer:
-                    //   Suppose t1 is largest.
-                    //   Split t2 using pivot k1 into lo and hi.
-                    //   Union disjoint subproblems and then combine. 
-                    if t1n.Height > t2n.Height then
-                        let lo, _, hi = split comparer t1n.Key t2 in
-                        balance comparer (union comparer t1n.Left lo) t1n.Key (union comparer t1n.Right hi)
-                    else
-                        let lo, _, hi = split comparer t2n.Key t1 in
-                        balance comparer (union comparer t2n.Left lo) t2n.Key (union comparer t2n.Right hi)
+                let lo, _, hi = split comparer t2n.Key t1 in
 
-    let rec intersectionAux comparer b (t:SetTree<'T>) acc = 
-        if isEmpty t then acc
-        else
-            if t.Height = 1 then
-                if mem comparer t.Key b then add comparer t.Key acc else acc
+                balance comparer (union comparer t2n.Left lo) t2n.Key (union comparer t2n.Right hi)
+
+    let rec intersectionAux comparer b (t: SetTree<'T>) acc =
+        if isEmpty t then
+            acc
+        else if t.Height = 1 then
+            if mem comparer t.Key b then
+                add comparer t.Key acc
             else
-                let tn = asNode t 
-                let acc = intersectionAux comparer b tn.Right acc 
-                let acc = if mem comparer tn.Key b then add comparer tn.Key acc else acc 
-                intersectionAux comparer b tn.Left acc
+                acc
+        else
+            let tn = asNode t
+            let acc = intersectionAux comparer b tn.Right acc
+
+            let acc =
+                if mem comparer tn.Key b then
+                    add comparer tn.Key acc
+                else
+                    acc
 
-    let intersection comparer a b = intersectionAux comparer b a empty
+            intersectionAux comparer b tn.Left acc
 
-    let partition1 comparer f k (acc1, acc2) = if f k then (add comparer k acc1, acc2) else (acc1, add comparer k acc2) 
+    let intersection comparer a b =
+        intersectionAux comparer b a empty
 
-    let rec partitionAux comparer f (t:SetTree<'T>) acc = 
-        if isEmpty t then acc
+    let partition1 comparer f k (acc1, acc2) =
+        if f k then
+            (add comparer k acc1, acc2)
         else
-            if t.Height = 1 then partition1 comparer f t.Key acc
-            else
-                let tn = asNode t 
-                let acc = partitionAux comparer f tn.Right acc 
-                let acc = partition1 comparer f tn.Key acc
-                partitionAux comparer f tn.Left acc
+            (acc1, add comparer k acc2)
 
-    let partition comparer f s = partitionAux comparer f s (empty, empty)
+    let rec partitionAux comparer f (t: SetTree<'T>) acc =
+        if isEmpty t then
+            acc
+        else if t.Height = 1 then
+            partition1 comparer f t.Key acc
+        else
+            let tn = asNode t
+            let acc = partitionAux comparer f tn.Right acc
+            let acc = partition1 comparer f tn.Key acc
+            partitionAux comparer f tn.Left acc
 
-    let rec minimumElementAux (t:SetTree<'T>) n = 
-        if isEmpty t then n
+    let partition comparer f s =
+        partitionAux comparer f s (empty, empty)
+
+    let rec minimumElementAux (t: SetTree<'T>) n =
+        if isEmpty t then
+            n
+        else if t.Height = 1 then
+            t.Key
         else
-            if t.Height = 1 then t.Key
-            else
-                let tn = asNode t
-                minimumElementAux tn.Left tn.Key
+            let tn = asNode t
+            minimumElementAux tn.Left tn.Key
 
-    and minimumElementOpt (t:SetTree<'T>) = 
-        if isEmpty t then None
+    and minimumElementOpt (t: SetTree<'T>) =
+        if isEmpty t then
+            None
+        else if t.Height = 1 then
+            Some t.Key
         else
-            if t.Height = 1 then Some t.Key
-            else
-                let tn = asNode t
-                Some(minimumElementAux tn.Left tn.Key) 
+            let tn = asNode t
+            Some(minimumElementAux tn.Left tn.Key)
 
-    and maximumElementAux (t:SetTree<'T>) n = 
-        if isEmpty t then n
+    and maximumElementAux (t: SetTree<'T>) n =
+        if isEmpty t then
+            n
+        else if t.Height = 1 then
+            t.Key
         else
-            if t.Height = 1 then t.Key
-            else
-                let tn = asNode t
-                maximumElementAux tn.Right tn.Key 
+            let tn = asNode t
+            maximumElementAux tn.Right tn.Key
 
-    and maximumElementOpt (t:SetTree<'T>) = 
-        if isEmpty t then None
+    and maximumElementOpt (t: SetTree<'T>) =
+        if isEmpty t then
+            None
+        else if t.Height = 1 then
+            Some t.Key
         else
-            if t.Height = 1 then Some t.Key
-            else
-                let tn = asNode t
-                Some(maximumElementAux tn.Right tn.Key)
+            let tn = asNode t
+            Some(maximumElementAux tn.Right tn.Key)
 
-    let minimumElement s = 
-        match minimumElementOpt s with 
+    let minimumElement s =
+        match minimumElementOpt s with
         | Some k -> k
-        | None -> invalidArg "s" (SR.GetString(SR.setContainsNoElements)) 
+        | None -> invalidArg "s" (SR.GetString(SR.setContainsNoElements))
 
-    let maximumElement s = 
-        match maximumElementOpt s with 
+    let maximumElement s =
+        match maximumElementOpt s with
         | Some k -> k
-        | None -> invalidArg "s" (SR.GetString(SR.setContainsNoElements)) 
+        | None -> invalidArg "s" (SR.GetString(SR.setContainsNoElements))
 
     // Imperative left-to-right iterators.
     []
-    type SetIterator<'T> when 'T: comparison  = 
-        { mutable stack: SetTree<'T> list; // invariant: always collapseLHS result 
-          mutable started: bool           // true when MoveNext has been called 
+    type SetIterator<'T> when 'T: comparison =
+        {
+            mutable stack: SetTree<'T> list // invariant: always collapseLHS result
+            mutable started: bool // true when MoveNext has been called
         }
 
     // collapseLHS:
     // a) Always returns either [] or a list starting with SetOne.
     // b) The "fringe" of the set stack is unchanged.
-    let rec collapseLHS (stack: SetTree<'T> list)  =
+    let rec collapseLHS (stack: SetTree<'T> list) =
         match stack with
         | [] -> []
         | x :: rest ->
-            if isEmpty x then collapseLHS rest
+            if isEmpty x then
+                collapseLHS rest
+            else if x.Height = 1 then
+                stack
             else
-                if x.Height = 1 then stack
-                else
-                    let xn = asNode x
-                    collapseLHS (xn.Left :: SetTree xn.Key :: xn.Right :: rest)
+                let xn = asNode x
+                collapseLHS (xn.Left :: SetTree xn.Key :: xn.Right :: rest)
 
-    let mkIterator s = { stack = collapseLHS [s]; started = false }
+    let mkIterator s =
+        {
+            stack = collapseLHS [ s ]
+            started = false
+        }
 
-    let notStarted() = raise (InvalidOperationException(SR.GetString(SR.enumerationNotStarted)))
+    let notStarted () =
+        raise (InvalidOperationException(SR.GetString(SR.enumerationNotStarted)))
 
-    let alreadyFinished() = raise (InvalidOperationException(SR.GetString(SR.enumerationAlreadyFinished)))
+    let alreadyFinished () =
+        raise (InvalidOperationException(SR.GetString(SR.enumerationAlreadyFinished)))
 
     let current i =
         if i.started then
             match i.stack with
             | k :: _ -> k.Key
-            | []     -> alreadyFinished()
+            | [] -> alreadyFinished ()
         else
-            notStarted()
+            notStarted ()
+
+    let unexpectedStackForMoveNext () =
+        failwith "Please report error: Set iterator, unexpected stack for moveNext"
+
+    let unexpectedstateInSetTreeCompareStacks () =
+        failwith "unexpected state in SetTree.compareStacks"
 
-    let unexpectedStackForMoveNext() = failwith "Please report error: Set iterator, unexpected stack for moveNext" 
-    let unexpectedstateInSetTreeCompareStacks() = failwith "unexpected state in SetTree.compareStacks"
-    
     let rec moveNext i =
         if i.started then
             match i.stack with
@@ -443,122 +539,161 @@ module internal SetTree =
                     i.stack <- collapseLHS rest
                     not i.stack.IsEmpty
                 else
-                    unexpectedStackForMoveNext()
+                    unexpectedStackForMoveNext ()
         else
-            i.started <- true; // The first call to MoveNext "starts" the enumeration.
-            not i.stack.IsEmpty 
+            i.started <- true // The first call to MoveNext "starts" the enumeration.
+            not i.stack.IsEmpty
+
+    let mkIEnumerator s =
+        let mutable i = mkIterator s
 
-    let mkIEnumerator s = 
-        let mutable  i = mkIterator s
-        { new IEnumerator<_> with 
-              member _.Current = current i
-          interface IEnumerator with 
+        { new IEnumerator<_> with
+            member _.Current = current i
+          interface IEnumerator with
               member _.Current = box (current i)
-              member _.MoveNext() = moveNext i
-              member _.Reset() = i <- mkIterator s
-          interface System.IDisposable with 
-              member _.Dispose() = () }
+
+              member _.MoveNext() =
+                  moveNext i
+
+              member _.Reset() =
+                  i <- mkIterator s
+          interface System.IDisposable with
+              member _.Dispose() =
+                  ()
+        }
 
     /// Set comparison.  Note this can be expensive.
-    let rec compareStacks (comparer: IComparer<'T>) (l1:SetTree<'T> list) (l2:SetTree<'T> list) : int =
-        let cont() =
-            match l1, l2 with 
+    let rec compareStacks (comparer: IComparer<'T>) (l1: SetTree<'T> list) (l2: SetTree<'T> list) : int =
+        let cont () =
+            match l1, l2 with
             | (x1 :: t1), _ when not (isEmpty x1) ->
                 if x1.Height = 1 then
                     compareStacks comparer (empty :: SetTree x1.Key :: t1) l2
                 else
                     let x1n = asNode x1
-                    compareStacks comparer (x1n.Left :: (SetTreeNode (x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2
+                    compareStacks comparer (x1n.Left :: (SetTreeNode(x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2
             | _, (x2 :: t2) when not (isEmpty x2) ->
                 if x2.Height = 1 then
                     compareStacks comparer l1 (empty :: SetTree x2.Key :: t2)
                 else
                     let x2n = asNode x2
-                    compareStacks comparer l1 (x2n.Left :: (SetTreeNode (x2n.Key, empty, x2n.Right, 0) :> SetTree<'T>  ) :: t2)
-            | _ -> unexpectedstateInSetTreeCompareStacks()
-        
-        match l1, l2 with 
-        | [], [] ->  0
-        | [], _  -> -1
-        | _, [] ->  1
+                    compareStacks comparer l1 (x2n.Left :: (SetTreeNode(x2n.Key, empty, x2n.Right, 0) :> SetTree<'T>) :: t2)
+            | _ -> unexpectedstateInSetTreeCompareStacks ()
+
+        match l1, l2 with
+        | [], [] -> 0
+        | [], _ -> -1
+        | _, [] -> 1
         | (x1 :: t1), (x2 :: t2) ->
             if isEmpty x1 then
-                if isEmpty x2 then compareStacks comparer t1 t2
-                else cont()
-            elif isEmpty x2 then cont()
+                if isEmpty x2 then
+                    compareStacks comparer t1 t2
+                else
+                    cont ()
+            elif isEmpty x2 then
+                cont ()
+            else if x1.Height = 1 then
+                if x2.Height = 1 then
+                    let c = comparer.Compare(x1.Key, x2.Key)
+
+                    if c <> 0 then
+                        c
+                    else
+                        compareStacks comparer t1 t2
+                else
+                    let x2n = asNode x2
+
+                    if isEmpty x2n.Left then
+                        let c = comparer.Compare(x1.Key, x2n.Key)
+
+                        if c <> 0 then
+                            c
+                        else
+                            compareStacks comparer (empty :: t1) (x2n.Right :: t2)
+                    else
+                        cont ()
             else
-                if x1.Height = 1 then
+                let x1n = asNode x1
+
+                if isEmpty x1n.Left then
                     if x2.Height = 1 then
-                        let c = comparer.Compare(x1.Key, x2.Key) 
-                        if c <> 0 then c else compareStacks comparer t1 t2
+                        let c = comparer.Compare(x1n.Key, x2.Key)
+
+                        if c <> 0 then
+                            c
+                        else
+                            compareStacks comparer (x1n.Right :: t1) (empty :: t2)
                     else
                         let x2n = asNode x2
+
                         if isEmpty x2n.Left then
-                            let c = comparer.Compare(x1.Key, x2n.Key) 
-                            if c <> 0 then c else compareStacks comparer (empty :: t1) (x2n.Right :: t2)
-                        else cont()
-                else
-                    let x1n = asNode x1
-                    if isEmpty x1n.Left then
-                        if x2.Height = 1 then
-                            let c = comparer.Compare(x1n.Key, x2.Key) 
-                            if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (empty :: t2)
+                            let c = comparer.Compare(x1n.Key, x2n.Key)
+
+                            if c <> 0 then
+                                c
+                            else
+                                compareStacks comparer (x1n.Right :: t1) (x2n.Right :: t2)
                         else
-                            let x2n = asNode x2
-                            if isEmpty x2n.Left then
-                                let c = comparer.Compare(x1n.Key, x2n.Key) 
-                                if c <> 0 then c else compareStacks comparer (x1n.Right :: t1) (x2n.Right :: t2)
-                            else cont()
-                    else cont()
-            
-    let compare comparer (t1:SetTree<'T>) (t2:SetTree<'T>) = 
+                            cont ()
+                else
+                    cont ()
+
+    let compare comparer (t1: SetTree<'T>) (t2: SetTree<'T>) =
         if isEmpty t1 then
-            if isEmpty t2 then 0
-            else -1
+            if isEmpty t2 then 0 else -1
+        else if isEmpty t2 then
+            1
         else
-            if isEmpty t2 then 1
-            else compareStacks comparer [t1] [t2]
+            compareStacks comparer [ t1 ] [ t2 ]
 
     let choose s =
         minimumElement s
 
-    let toList (t:SetTree<'T>) = 
-        let rec loop (t':SetTree<'T>) acc =
-            if isEmpty t' then acc
+    let toList (t: SetTree<'T>) =
+        let rec loop (t': SetTree<'T>) acc =
+            if isEmpty t' then
+                acc
+            else if t'.Height = 1 then
+                t'.Key :: acc
             else
-                if t'.Height = 1 then t'.Key :: acc
-                else
-                    let tn = asNode t'
-                    loop tn.Left (tn.Key :: loop tn.Right acc)
+                let tn = asNode t'
+                loop tn.Left (tn.Key :: loop tn.Right acc)
+
         loop t []
 
     let copyToArray s (arr: _[]) i =
-        let mutable j = i 
-        iter (fun x -> arr.[j] <- x; j <- j + 1) s
+        let mutable j = i
+
+        iter
+            (fun x ->
+                arr.[j] <- x
+                j <- j + 1)
+            s
 
-    let toArray s = 
-        let n = (count s) 
-        let res = Array.zeroCreate n 
+    let toArray s =
+        let n = (count s)
+        let res = Array.zeroCreate n
         copyToArray s res 0
         res
 
-    let rec mkFromEnumerator comparer acc (e: IEnumerator<_>) = 
-        if e.MoveNext() then 
+    let rec mkFromEnumerator comparer acc (e: IEnumerator<_>) =
+        if e.MoveNext() then
             mkFromEnumerator comparer (add comparer e.Current acc) e
-        else acc
+        else
+            acc
 
     let ofSeq comparer (c: IEnumerable<_>) =
         use ie = c.GetEnumerator()
-        mkFromEnumerator comparer empty ie 
+        mkFromEnumerator comparer empty ie
 
     let ofArray comparer l =
-        Array.fold (fun acc k -> add comparer k acc) empty l 
+        Array.fold (fun acc k -> add comparer k acc) empty l
 
 []
 []
 [>)>]
 []
-type Set<[]'T when 'T: comparison >(comparer:IComparer<'T>, tree: SetTree<'T>) =
+type Set<[] 'T when 'T: comparison>(comparer: IComparer<'T>, tree: SetTree<'T>) =
 
     []
     // NOTE: This type is logically immutable. This field is only mutated during deserialization.
@@ -576,8 +711,8 @@ type Set<[]'T when 'T: comparison >(comparer:IComparer<'T
     // We use .NET generics per-instantiation static fields to avoid allocating a new object for each empty
     // set (it is just a lookup into a .NET table of type-instantiation-indexed static fields).
 
-    static let empty: Set<'T> = 
-        let comparer = LanguagePrimitives.FastGenericComparer<'T> 
+    static let empty: Set<'T> =
+        let comparer = LanguagePrimitives.FastGenericComparer<'T>
         Set<'T>(comparer, SetTree.empty)
 
     []
@@ -605,54 +740,54 @@ type Set<[]'T when 'T: comparison >(comparer:IComparer<'T
     []
     static member Empty: Set<'T> = empty
 
-    member s.Add value: Set<'T> = 
+    member s.Add value : Set<'T> =
 #if TRACE_SETS_AND_MAPS
-        SetTree.report()
+        SetTree.report ()
         SetTree.numAdds <- SetTree.numAdds + 1
         SetTree.totalSizeOnSetAdd <- SetTree.totalSizeOnSetAdd + float (SetTree.count s.Tree)
 #endif
-        Set<'T>(s.Comparer, SetTree.add s.Comparer value s.Tree )
+        Set<'T>(s.Comparer, SetTree.add s.Comparer value s.Tree)
 
-    member s.Remove value: Set<'T> = 
+    member s.Remove value : Set<'T> =
 #if TRACE_SETS_AND_MAPS
-        SetTree.report()
+        SetTree.report ()
         SetTree.numRemoves <- SetTree.numRemoves + 1
 #endif
         Set<'T>(s.Comparer, SetTree.remove s.Comparer value s.Tree)
 
-    member s.Count =
-        SetTree.count s.Tree
+    member s.Count = SetTree.count s.Tree
 
-    member s.Contains value = 
+    member s.Contains value =
 #if TRACE_SETS_AND_MAPS
-        SetTree.report()
+        SetTree.report ()
         SetTree.numLookups <- SetTree.numLookups + 1
         SetTree.totalSizeOnSetLookup <- SetTree.totalSizeOnSetLookup + float (SetTree.count s.Tree)
 #endif
-        SetTree.mem s.Comparer  value s.Tree
+        SetTree.mem s.Comparer value s.Tree
 
     member s.Iterate x =
         SetTree.iter x s.Tree
 
-    member s.Fold f z  = 
+    member s.Fold f z =
         let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt f
-        SetTree.fold (fun x z -> f.Invoke(z, x)) z s.Tree 
+        SetTree.fold (fun x z -> f.Invoke(z, x)) z s.Tree
 
     []
-    member s.IsEmpty =
-        SetTree.isEmpty s.Tree
+    member s.IsEmpty = SetTree.isEmpty s.Tree
 
-    member s.Partition f : Set<'T> *  Set<'T> = 
-        if SetTree.isEmpty s.Tree then s,s
+    member s.Partition f : Set<'T> * Set<'T> =
+        if SetTree.isEmpty s.Tree then
+            s, s
         else
             let t1, t2 = SetTree.partition s.Comparer f s.Tree in Set(s.Comparer, t1), Set(s.Comparer, t2)
 
-    member s.Filter f : Set<'T> = 
-        if SetTree.isEmpty s.Tree then s
+    member s.Filter f : Set<'T> =
+        if SetTree.isEmpty s.Tree then
+            s
         else
             Set(s.Comparer, SetTree.filter s.Comparer f s.Tree)
 
-    member s.Map f : Set<'U> = 
+    member s.Map f : Set<'U> =
         let comparer = LanguagePrimitives.FastGenericComparer<'U>
         Set(comparer, SetTree.fold (fun acc k -> SetTree.add comparer (f k) acc) (SetTree.empty) s.Tree)
 
@@ -662,39 +797,45 @@ type Set<[]'T when 'T: comparison >(comparer:IComparer<'T
     member s.ForAll f =
         SetTree.forall f s.Tree
 
-    static member (-) (set1: Set<'T>, set2: Set<'T>) = 
-        if SetTree.isEmpty set1.Tree then set1 (* 0 - B = 0 *)
+    static member (-)(set1: Set<'T>, set2: Set<'T>) =
+        if SetTree.isEmpty set1.Tree then
+            set1 (* 0 - B = 0 *)
+        else if SetTree.isEmpty set2.Tree then
+            set1 (* A - 0 = A *)
         else
-            if SetTree.isEmpty set2.Tree then set1 (* A - 0 = A *)
-            else Set(set1.Comparer, SetTree.diff set1.Comparer set1.Tree set2.Tree)
+            Set(set1.Comparer, SetTree.diff set1.Comparer set1.Tree set2.Tree)
 
-    static member (+) (set1: Set<'T>, set2: Set<'T>) = 
+    static member (+)(set1: Set<'T>, set2: Set<'T>) =
 #if TRACE_SETS_AND_MAPS
-        SetTree.report()
+        SetTree.report ()
         SetTree.numUnions <- SetTree.numUnions + 1
 #endif
-        if SetTree.isEmpty set2.Tree then set1  (* A U 0 = A *)
+        if SetTree.isEmpty set2.Tree then
+            set1 (* A U 0 = A *)
+        else if SetTree.isEmpty set1.Tree then
+            set2 (* 0 U B = B *)
         else
-            if SetTree.isEmpty set1.Tree then set2  (* 0 U B = B *)
-            else Set(set1.Comparer, SetTree.union set1.Comparer set1.Tree set2.Tree)
+            Set(set1.Comparer, SetTree.union set1.Comparer set1.Tree set2.Tree)
 
-    static member Intersection(a: Set<'T>, b: Set<'T>) : Set<'T>  = 
-        if SetTree.isEmpty b.Tree then b  (* A INTER 0 = 0 *)
+    static member Intersection(a: Set<'T>, b: Set<'T>) : Set<'T> =
+        if SetTree.isEmpty b.Tree then
+            b (* A INTER 0 = 0 *)
+        else if SetTree.isEmpty a.Tree then
+            a (* 0 INTER B = 0 *)
         else
-            if SetTree.isEmpty a.Tree then a (* 0 INTER B = 0 *)
-            else Set(a.Comparer, SetTree.intersection a.Comparer a.Tree b.Tree)
+            Set(a.Comparer, SetTree.intersection a.Comparer a.Tree b.Tree)
 
-    static member Union(sets:seq>) : Set<'T>  = 
+    static member Union(sets: seq>) : Set<'T> =
         Seq.fold (fun s1 s2 -> s1 + s2) Set<'T>.Empty sets
 
-    static member Intersection(sets:seq>) : Set<'T>  = 
+    static member Intersection(sets: seq>) : Set<'T> =
         Seq.reduce (fun s1 s2 -> Set.Intersection(s1, s2)) sets
 
     static member Equality(a: Set<'T>, b: Set<'T>) =
-        (SetTree.compare a.Comparer  a.Tree b.Tree = 0)
+        (SetTree.compare a.Comparer a.Tree b.Tree = 0)
 
     static member Compare(a: Set<'T>, b: Set<'T>) =
-        SetTree.compare a.Comparer  a.Tree b.Tree
+        SetTree.compare a.Comparer a.Tree b.Tree
 
     []
     member x.Choose = SetTree.choose x.Tree
@@ -706,55 +847,72 @@ type Set<[]'T when 'T: comparison >(comparer:IComparer<'T
     member x.MaximumElement = SetTree.maximumElement x.Tree
 
     member x.IsSubsetOf(otherSet: Set<'T>) =
-        SetTree.subset x.Comparer x.Tree otherSet.Tree 
+        SetTree.subset x.Comparer x.Tree otherSet.Tree
 
     member x.IsSupersetOf(otherSet: Set<'T>) =
         SetTree.subset x.Comparer otherSet.Tree x.Tree
 
     member x.IsProperSubsetOf(otherSet: Set<'T>) =
-        SetTree.properSubset x.Comparer x.Tree otherSet.Tree 
+        SetTree.properSubset x.Comparer x.Tree otherSet.Tree
 
     member x.IsProperSupersetOf(otherSet: Set<'T>) =
         SetTree.properSubset x.Comparer otherSet.Tree x.Tree
 
-    member x.ToList () = SetTree.toList x.Tree
+    member x.ToList() =
+        SetTree.toList x.Tree
+
+    member x.ToArray() =
+        SetTree.toArray x.Tree
 
-    member x.ToArray () = SetTree.toArray x.Tree
+    member this.ComputeHashCode() =
+        let combineHash x y =
+            (x <<< 1) + y + 631
 
-    member this.ComputeHashCode() = 
-        let combineHash x y = (x <<< 1) + y + 631 
         let mutable res = 0
+
         for x in this do
             res <- combineHash res (hash x)
+
         res
 
-    override this.GetHashCode() = this.ComputeHashCode()
+    override this.GetHashCode() =
+        this.ComputeHashCode()
 
-    override this.Equals that = 
-        match that with 
-        | :? Set<'T> as that -> 
-            use e1 = (this :> seq<_>).GetEnumerator() 
-            use e2 = (that :> seq<_>).GetEnumerator() 
-            let rec loop () = 
-                let m1 = e1.MoveNext() 
+    override this.Equals that =
+        match that with
+        | :? Set<'T> as that ->
+            use e1 = (this :> seq<_>).GetEnumerator()
+            use e2 = (that :> seq<_>).GetEnumerator()
+
+            let rec loop () =
+                let m1 = e1.MoveNext()
                 let m2 = e2.MoveNext()
-                (m1 = m2) && (not m1 || ((e1.Current = e2.Current) && loop()))
-            loop()
+                (m1 = m2) && (not m1 || ((e1.Current = e2.Current) && loop ()))
+
+            loop ()
         | _ -> false
 
-    interface System.IComparable with 
-        member this.CompareTo(that: obj) = SetTree.compare this.Comparer this.Tree ((that :?> Set<'T>).Tree)
+    interface System.IComparable with
+        member this.CompareTo(that: obj) =
+            SetTree.compare this.Comparer this.Tree ((that :?> Set<'T>).Tree)
 
-    interface ICollection<'T> with 
-        member s.Add x = ignore x; raise (new System.NotSupportedException("ReadOnlyCollection"))
+    interface ICollection<'T> with
+        member s.Add x =
+            ignore x
+            raise (new System.NotSupportedException("ReadOnlyCollection"))
 
-        member s.Clear() = raise (new System.NotSupportedException("ReadOnlyCollection"))
+        member s.Clear() =
+            raise (new System.NotSupportedException("ReadOnlyCollection"))
 
-        member s.Remove x = ignore x; raise (new System.NotSupportedException("ReadOnlyCollection"))
+        member s.Remove x =
+            ignore x
+            raise (new System.NotSupportedException("ReadOnlyCollection"))
 
-        member s.Contains x = SetTree.mem s.Comparer x s.Tree
+        member s.Contains x =
+            SetTree.mem s.Comparer x s.Tree
 
-        member s.CopyTo(arr, i) = SetTree.copyToArray s.Tree arr i
+        member s.CopyTo(arr, i) =
+            SetTree.copyToArray s.Tree arr i
 
         member s.IsReadOnly = true
 
@@ -764,150 +922,205 @@ type Set<[]'T when 'T: comparison >(comparer:IComparer<'T
         member s.Count = s.Count
 
     interface IEnumerable<'T> with
-        member s.GetEnumerator() = SetTree.mkIEnumerator s.Tree
+        member s.GetEnumerator() =
+            SetTree.mkIEnumerator s.Tree
 
     interface IEnumerable with
-        override s.GetEnumerator() = (SetTree.mkIEnumerator s.Tree :> IEnumerator)
+        override s.GetEnumerator() =
+            (SetTree.mkIEnumerator s.Tree :> IEnumerator)
 
-    static member Singleton(x:'T) : Set<'T> = Set<'T>.Empty.Add x
+    static member Singleton(x: 'T) : Set<'T> =
+        Set<'T>.Empty.Add x
 
-    new (elements : seq<'T>) = 
+    new(elements: seq<'T>) =
         let comparer = LanguagePrimitives.FastGenericComparer<'T>
         Set(comparer, SetTree.ofSeq comparer elements)
 
-    static member Create(elements : seq<'T>) =  Set<'T>(elements)
+    static member Create(elements: seq<'T>) =
+        Set<'T>(elements)
 
-    static member FromArray(arr : 'T array) : Set<'T> = 
+    static member FromArray(arr: 'T array) : Set<'T> =
         let comparer = LanguagePrimitives.FastGenericComparer<'T>
         Set(comparer, SetTree.ofArray comparer arr)
 
-    override x.ToString() = 
-        match List.ofSeq (Seq.truncate 4 x) with 
+    override x.ToString() =
+        match List.ofSeq (Seq.truncate 4 x) with
         | [] -> "set []"
-        | [h1] ->
+        | [ h1 ] ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             StringBuilder().Append("set [").Append(txt1).Append("]").ToString()
-        | [h1; h2] ->
+        | [ h1; h2 ] ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             let txt2 = LanguagePrimitives.anyToStringShowingNull h2
-            StringBuilder().Append("set [").Append(txt1).Append("; ").Append(txt2).Append("]").ToString()
-        | [h1; h2; h3] ->
+
+            StringBuilder()
+                .Append("set [")
+                .Append(txt1)
+                .Append("; ")
+                .Append(txt2)
+                .Append("]")
+                .ToString()
+        | [ h1; h2; h3 ] ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             let txt2 = LanguagePrimitives.anyToStringShowingNull h2
             let txt3 = LanguagePrimitives.anyToStringShowingNull h3
-            StringBuilder().Append("set [").Append(txt1).Append("; ").Append(txt2).Append("; ").Append(txt3).Append("]").ToString()
+
+            StringBuilder()
+                .Append("set [")
+                .Append(txt1)
+                .Append("; ")
+                .Append(txt2)
+                .Append("; ")
+                .Append(txt3)
+                .Append("]")
+                .ToString()
         | h1 :: h2 :: h3 :: _ ->
             let txt1 = LanguagePrimitives.anyToStringShowingNull h1
             let txt2 = LanguagePrimitives.anyToStringShowingNull h2
             let txt3 = LanguagePrimitives.anyToStringShowingNull h3
-            StringBuilder().Append("set [").Append(txt1).Append("; ").Append(txt2).Append("; ").Append(txt3).Append("; ... ]").ToString() 
 
-and 
-    []
-    SetDebugView<'T when 'T : comparison>(v: Set<'T>)  = 
+            StringBuilder()
+                .Append("set [")
+                .Append(txt1)
+                .Append("; ")
+                .Append(txt2)
+                .Append("; ")
+                .Append(txt3)
+                .Append("; ... ]")
+                .ToString()
 
-         []
-         member x.Items = v |> Seq.truncate 1000 |> Seq.toArray 
+and [] SetDebugView<'T when 'T: comparison>(v: Set<'T>) =
+
+    []
+    member x.Items = v |> Seq.truncate 1000 |> Seq.toArray
 
 []
 []
-module Set = 
+module Set =
 
     []
-    let isEmpty (set: Set<'T>) = set.IsEmpty
+    let isEmpty (set: Set<'T>) =
+        set.IsEmpty
 
     []
-    let contains element (set: Set<'T>) = set.Contains element
+    let contains element (set: Set<'T>) =
+        set.Contains element
 
     []
-    let add value (set: Set<'T>) = set.Add value
+    let add value (set: Set<'T>) =
+        set.Add value
 
     []
-    let singleton value = Set<'T>.Singleton value
+    let singleton value =
+        Set<'T>.Singleton value
 
     []
-    let remove value (set: Set<'T>) = set.Remove value
+    let remove value (set: Set<'T>) =
+        set.Remove value
 
     []
-    let union (set1: Set<'T>) (set2: Set<'T>)  = set1 + set2
+    let union (set1: Set<'T>) (set2: Set<'T>) =
+        set1 + set2
 
     []
-    let unionMany sets = Set.Union sets
+    let unionMany sets =
+        Set.Union sets
 
     []
-    let intersect (set1: Set<'T>) (set2: Set<'T>)  = Set<'T>.Intersection(set1, set2)
+    let intersect (set1: Set<'T>) (set2: Set<'T>) =
+        Set<'T>.Intersection (set1, set2)
 
     []
-    let intersectMany sets  = Set.Intersection sets
+    let intersectMany sets =
+        Set.Intersection sets
 
     []
-    let iter action (set: Set<'T>)  = set.Iterate action
+    let iter action (set: Set<'T>) =
+        set.Iterate action
 
     []
-    let empty<'T when 'T : comparison> : Set<'T> = Set<'T>.Empty
+    let empty<'T when 'T: comparison> : Set<'T> = Set<'T>.Empty
 
     []
-    let forall predicate (set: Set<'T>) = set.ForAll predicate
+    let forall predicate (set: Set<'T>) =
+        set.ForAll predicate
 
     []
-    let exists predicate (set: Set<'T>) = set.Exists predicate
+    let exists predicate (set: Set<'T>) =
+        set.Exists predicate
 
     []
-    let filter predicate (set: Set<'T>) = set.Filter predicate
+    let filter predicate (set: Set<'T>) =
+        set.Filter predicate
 
     []
-    let partition predicate (set: Set<'T>) = set.Partition predicate 
+    let partition predicate (set: Set<'T>) =
+        set.Partition predicate
 
     []
-    let fold<'T, 'State  when 'T : comparison> folder (state:'State) (set: Set<'T>) = SetTree.fold folder state set.Tree
+    let fold<'T, 'State when 'T: comparison> folder (state: 'State) (set: Set<'T>) =
+        SetTree.fold folder state set.Tree
 
     []
-    let foldBack<'T, 'State when 'T : comparison> folder (set: Set<'T>) (state:'State) = SetTree.foldBack folder set.Tree state
+    let foldBack<'T, 'State when 'T: comparison> folder (set: Set<'T>) (state: 'State) =
+        SetTree.foldBack folder set.Tree state
 
     []
-    let map mapping (set: Set<'T>) = set.Map mapping
+    let map mapping (set: Set<'T>) =
+        set.Map mapping
 
     []
-    let count (set: Set<'T>) = set.Count
+    let count (set: Set<'T>) =
+        set.Count
 
     []
-    let ofList elements = Set(List.toSeq elements)
+    let ofList elements =
+        Set(List.toSeq elements)
 
     []
-    let ofArray (array: 'T array) = Set<'T>.FromArray array
+    let ofArray (array: 'T array) =
+        Set<'T>.FromArray array
 
     []
-    let toList (set: Set<'T>) = set.ToList()
+    let toList (set: Set<'T>) =
+        set.ToList()
 
     []
-    let toArray (set: Set<'T>) = set.ToArray()
+    let toArray (set: Set<'T>) =
+        set.ToArray()
 
     []
-    let toSeq (set: Set<'T>) = (set:> seq<'T>)
+    let toSeq (set: Set<'T>) =
+        (set :> seq<'T>)
 
     []
-    let ofSeq (elements: seq<_>) = Set elements
+    let ofSeq (elements: seq<_>) =
+        Set elements
 
     []
-    let difference (set1: Set<'T>) (set2: Set<'T>) = set1 - set2
+    let difference (set1: Set<'T>) (set2: Set<'T>) =
+        set1 - set2
 
     []
-    let isSubset (set1:Set<'T>) (set2: Set<'T>) = SetTree.subset set1.Comparer set1.Tree set2.Tree 
+    let isSubset (set1: Set<'T>) (set2: Set<'T>) =
+        SetTree.subset set1.Comparer set1.Tree set2.Tree
 
     []
-    let isSuperset (set1:Set<'T>) (set2: Set<'T>) = SetTree.subset set1.Comparer set2.Tree set1.Tree
+    let isSuperset (set1: Set<'T>) (set2: Set<'T>) =
+        SetTree.subset set1.Comparer set2.Tree set1.Tree
 
     []
-    let isProperSubset (set1:Set<'T>) (set2: Set<'T>) = SetTree.properSubset set1.Comparer set1.Tree set2.Tree 
+    let isProperSubset (set1: Set<'T>) (set2: Set<'T>) =
+        SetTree.properSubset set1.Comparer set1.Tree set2.Tree
 
     []
-    let isProperSuperset (set1:Set<'T>) (set2: Set<'T>) = SetTree.properSubset set1.Comparer set2.Tree set1.Tree
+    let isProperSuperset (set1: Set<'T>) (set2: Set<'T>) =
+        SetTree.properSubset set1.Comparer set2.Tree set1.Tree
 
     []
-    let minElement (set: Set<'T>) = set.MinimumElement
+    let minElement (set: Set<'T>) =
+        set.MinimumElement
 
     []
-    let maxElement (set: Set<'T>) = set.MaximumElement
-
-
-
+    let maxElement (set: Set<'T>) =
+        set.MaximumElement
diff --git a/src/FSharp.Core/string.fs b/src/FSharp.Core/string.fs
index f36e430e66c..8580c6451d2 100644
--- a/src/FSharp.Core/string.fs
+++ b/src/FSharp.Core/string.fs
@@ -20,13 +20,13 @@ module String =
     let LOH_CHAR_THRESHOLD = 40_000
 
     []
-    let length (str:string) =
+    let length (str: string) =
         if isNull str then 0 else str.Length
 
     []
-    let concat sep (strings : seq) =  
+    let concat sep (strings: seq) =
 
-        let concatArray sep (strings: string []) =
+        let concatArray sep (strings: string[]) =
             match length sep with
             | 0 -> String.Concat strings
             // following line should be used when this overload becomes part of .NET Standard (it's only in .NET Core)
@@ -34,37 +34,34 @@ module String =
             | _ -> String.Join(sep, strings, 0, strings.Length)
 
         match strings with
-        | :? (string[]) as arr -> 
-            concatArray sep arr
+        | :? (string[]) as arr -> concatArray sep arr
 
-        | :? (string list) as lst -> 
-            lst 
-            |> List.toArray 
-            |> concatArray sep
+        | :? (string list) as lst -> lst |> List.toArray |> concatArray sep
 
-        | _ ->
-            String.Join(sep, strings)
+        | _ -> String.Join(sep, strings)
 
     []
-    let iter (action : (char -> unit)) (str:string) =
+    let iter (action: (char -> unit)) (str: string) =
         if not (String.IsNullOrEmpty str) then
             for i = 0 to str.Length - 1 do
-                action str.[i] 
+                action str.[i]
 
     []
-    let iteri action (str:string) =
+    let iteri action (str: string) =
         if not (String.IsNullOrEmpty str) then
-            let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(action)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (action)
+
             for i = 0 to str.Length - 1 do
-                f.Invoke(i, str.[i]) 
+                f.Invoke(i, str.[i])
 
     []
-    let map (mapping: char -> char) (str:string) =
+    let map (mapping: char -> char) (str: string) =
         if String.IsNullOrEmpty str then
             String.Empty
         else
             let result = str.ToCharArray()
             let mutable i = 0
+
             for c in result do
                 result.[i] <- mapping c
                 i <- i + 1
@@ -72,15 +69,17 @@ module String =
             new String(result)
 
     []
-    let mapi (mapping: int -> char -> char) (str:string) =
+    let mapi (mapping: int -> char -> char) (str: string) =
         let len = length str
-        if len = 0 then 
+
+        if len = 0 then
             String.Empty
         else
             let result = str.ToCharArray()
-            let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(mapping)
+            let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt (mapping)
 
             let mutable i = 0
+
             while i < len do
                 result.[i] <- f.Invoke(i, result.[i])
                 i <- i + 1
@@ -88,33 +87,39 @@ module String =
             new String(result)
 
     []
-    let filter (predicate: char -> bool) (str:string) =
+    let filter (predicate: char -> bool) (str: string) =
         let len = length str
 
-        if len = 0 then 
+        if len = 0 then
             String.Empty
 
         elif len > LOH_CHAR_THRESHOLD then
-            // By using SB here, which is twice slower than the optimized path, we prevent LOH allocations 
+            // By using SB here, which is twice slower than the optimized path, we prevent LOH allocations
             // and 'stop the world' collections if the filtering results in smaller strings.
             // We also don't pre-allocate SB here, to allow for less mem pressure when filter result is small.
             let res = StringBuilder()
-            str |> iter (fun c -> if predicate c then res.Append c |> ignore)
+
+            str
+            |> iter (fun c ->
+                if predicate c then
+                    res.Append c |> ignore)
+
             res.ToString()
 
         else
             // Must do it this way, since array.fs is not yet in scope, but this is safe
             let target = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len
             let mutable i = 0
+
             for c in str do
-                if predicate c then 
+                if predicate c then
                     target.[i] <- c
                     i <- i + 1
 
             String(target, 0, i)
 
     []
-    let collect (mapping: char -> string) (str:string) =
+    let collect (mapping: char -> string) (str: string) =
         if String.IsNullOrEmpty str then
             String.Empty
         else
@@ -123,19 +128,25 @@ module String =
             res.ToString()
 
     []
-    let init (count:int) (initializer: int-> string) =
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
+    let init (count: int) (initializer: int -> string) =
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
+
         let res = StringBuilder count
-        for i = 0 to count - 1 do 
-           res.Append(initializer i) |> ignore
+
+        for i = 0 to count - 1 do
+            res.Append(initializer i) |> ignore
+
         res.ToString()
 
     []
-    let replicate (count:int) (str:string) =
-        if count < 0 then invalidArgInputMustBeNonNegative "count" count
+    let replicate (count: int) (str: string) =
+        if count < 0 then
+            invalidArgInputMustBeNonNegative "count" count
 
         let len = length str
-        if len = 0 || count = 0 then 
+
+        if len = 0 || count = 0 then
             String.Empty
 
         elif len = 1 then
@@ -150,14 +161,17 @@ module String =
 
         else
             // Using the primitive, because array.fs is not yet in scope. It's safe: both len and count are positive.
-            let target = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (len * count)
+            let target =
+                Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (len * count)
+
             let source = str.ToCharArray()
 
             // O(log(n)) performance loop:
-            // Copy first string, then keep copying what we already copied 
+            // Copy first string, then keep copying what we already copied
             // (i.e., doubling it) until we reach or pass the halfway point
             Array.Copy(source, 0, target, 0, len)
             let mutable i = len
+
             while i * 2 < target.Length do
                 Array.Copy(target, 0, target, i, i)
                 i <- i * 2
@@ -167,17 +181,21 @@ module String =
             new String(target)
 
     []
-    let forall predicate (str:string) =
+    let forall predicate (str: string) =
         if String.IsNullOrEmpty str then
             true
         else
-            let rec check i = (i >= str.Length) || (predicate str.[i] && check (i+1)) 
+            let rec check i =
+                (i >= str.Length) || (predicate str.[i] && check (i + 1))
+
             check 0
 
     []
-    let exists predicate (str:string) =
+    let exists predicate (str: string) =
         if String.IsNullOrEmpty str then
             false
         else
-            let rec check i = (i < str.Length) && (predicate str.[i] || check (i+1)) 
-            check 0  
+            let rec check i =
+                (i < str.Length) && (predicate str.[i] || check (i + 1))
+
+            check 0
diff --git a/src/FSharp.Core/tasks.fs b/src/FSharp.Core/tasks.fs
index 4ec83a25be9..b348121149c 100644
--- a/src/FSharp.Core/tasks.fs
+++ b/src/FSharp.Core/tasks.fs
@@ -30,10 +30,10 @@ open Microsoft.FSharp.Collections
 type TaskStateMachineData<'T> =
 
     []
-    val mutable Result : 'T
+    val mutable Result: 'T
 
     []
-    val mutable MethodBuilder : AsyncTaskMethodBuilder<'T>
+    val mutable MethodBuilder: AsyncTaskMethodBuilder<'T>
 
 and TaskStateMachine<'TOverall> = ResumableStateMachine>
 and TaskResumptionFunc<'TOverall> = ResumptionFunc>
@@ -42,15 +42,16 @@ and TaskCode<'TOverall, 'T> = ResumableCode, 'T>
 
 type TaskBuilderBase() =
 
-    member inline _.Delay(generator : unit -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
-        TaskCode<'TOverall, 'T>(fun sm -> (generator()).Invoke(&sm))
+    member inline _.Delay(generator: unit -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
+        TaskCode<'TOverall, 'T>(fun sm -> (generator ()).Invoke(&sm))
 
     /// Used to represent no-ops like the implicit empty "else" branch of an "if" expression.
     []
-    member inline _.Zero() : TaskCode<'TOverall, unit> = ResumableCode.Zero()
+    member inline _.Zero() : TaskCode<'TOverall, unit> =
+        ResumableCode.Zero()
 
-    member inline _.Return (value: 'T) : TaskCode<'T, 'T> = 
-        TaskCode<'T, _>(fun sm -> 
+    member inline _.Return(value: 'T) : TaskCode<'T, 'T> =
+        TaskCode<'T, _>(fun sm ->
             sm.Data.Result <- value
             true)
 
@@ -61,117 +62,137 @@ type TaskBuilderBase() =
         ResumableCode.Combine(task1, task2)
 
     /// Builds a step that executes the body while the condition predicate is true.
-    member inline _.While ([] condition : unit -> bool, body : TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
+    member inline _.While([] condition: unit -> bool, body: TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
         ResumableCode.While(condition, body)
 
     /// Wraps a step in a try/with. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    member inline _.TryWith (body: TaskCode<'TOverall, 'T>, catch: exn -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
+    member inline _.TryWith(body: TaskCode<'TOverall, 'T>, catch: exn -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
         ResumableCode.TryWith(body, catch)
 
     /// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    member inline _.TryFinally (body: TaskCode<'TOverall, 'T>, [] compensation : unit -> unit) : TaskCode<'TOverall, 'T> =
-        ResumableCode.TryFinally(body, ResumableCode<_,_>(fun _sm -> compensation(); true))
-
-    member inline _.For (sequence : seq<'T>, body : 'T -> TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
+    member inline _.TryFinally(body: TaskCode<'TOverall, 'T>, [] compensation: unit -> unit) : TaskCode<'TOverall, 'T> =
+        ResumableCode.TryFinally(
+            body,
+            ResumableCode<_, _>(fun _sm ->
+                compensation ()
+                true)
+        )
+
+    member inline _.For(sequence: seq<'T>, body: 'T -> TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
         ResumableCode.For(sequence, body)
 
 #if NETSTANDARD2_1
-    member inline internal this.TryFinallyAsync(body: TaskCode<'TOverall, 'T>, compensation : unit -> ValueTask) : TaskCode<'TOverall, 'T> =
-        ResumableCode.TryFinallyAsync(body, ResumableCode<_,_>(fun sm -> 
-            if __useResumableCode then
-                let mutable __stack_condition_fin = true
-                let __stack_vtask = compensation()
-                if not __stack_vtask.IsCompleted then
-                    let mutable awaiter = __stack_vtask.GetAwaiter()
-                    let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
-                    __stack_condition_fin <- __stack_yield_fin
-
-                    if not __stack_condition_fin then 
-                        sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
-
-                __stack_condition_fin
-            else
-                let vtask = compensation()
-                let mutable awaiter = vtask.GetAwaiter()
+    member inline internal this.TryFinallyAsync(body: TaskCode<'TOverall, 'T>, compensation: unit -> ValueTask) : TaskCode<'TOverall, 'T> =
+        ResumableCode.TryFinallyAsync(
+            body,
+            ResumableCode<_, _>(fun sm ->
+                if __useResumableCode then
+                    let mutable __stack_condition_fin = true
+                    let __stack_vtask = compensation ()
+
+                    if not __stack_vtask.IsCompleted then
+                        let mutable awaiter = __stack_vtask.GetAwaiter()
+                        let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
+                        __stack_condition_fin <- __stack_yield_fin
 
-                let cont = 
-                    TaskResumptionFunc<'TOverall>( fun sm -> 
-                        awaiter.GetResult() |> ignore
-                        true)
+                        if not __stack_condition_fin then
+                            sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
 
-                // shortcut to continue immediately
-                if awaiter.IsCompleted then 
-                    true
+                    __stack_condition_fin
                 else
-                    sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
-                    sm.ResumptionDynamicInfo.ResumptionFunc <- cont
-                    false
-                ))
+                    let vtask = compensation ()
+                    let mutable awaiter = vtask.GetAwaiter()
+
+                    let cont =
+                        TaskResumptionFunc<'TOverall>(fun sm ->
+                            awaiter.GetResult() |> ignore
+                            true)
 
-    member inline this.Using<'Resource, 'TOverall, 'T when 'Resource :> IAsyncDisposable> (resource: 'Resource, body: 'Resource -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
+                    // shortcut to continue immediately
+                    if awaiter.IsCompleted then
+                        true
+                    else
+                        sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
+                        sm.ResumptionDynamicInfo.ResumptionFunc <- cont
+                        false)
+        )
+
+    member inline this.Using<'Resource, 'TOverall, 'T when 'Resource :> IAsyncDisposable>
+        (
+            resource: 'Resource,
+            body: 'Resource -> TaskCode<'TOverall, 'T>
+        ) : TaskCode<'TOverall, 'T> =
         this.TryFinallyAsync(
             (fun sm -> (body resource).Invoke(&sm)),
-            (fun () -> 
-                if not (isNull (box resource)) then 
+            (fun () ->
+                if not (isNull (box resource)) then
                     resource.DisposeAsync()
                 else
-                    ValueTask()))
+                    ValueTask())
+        )
 #endif
 
-
 type TaskBuilder() =
 
     inherit TaskBuilderBase()
 
     // This is the dynamic implementation - this is not used
-    // for statically compiled tasks.  An executor (resumptionFuncExecutor) is 
+    // for statically compiled tasks.  An executor (resumptionFuncExecutor) is
     // registered with the state machine, plus the initial resumption.
     // The executor stays constant throughout the execution, it wraps each step
     // of the execution in a try/with.  The resumption is changed at each step
     // to represent the continuation of the computation.
-    static member RunDynamic(code: TaskCode<'T, 'T>) : Task<'T> = 
+    static member RunDynamic(code: TaskCode<'T, 'T>) : Task<'T> =
         let mutable sm = TaskStateMachine<'T>()
         let initialResumptionFunc = TaskResumptionFunc<'T>(fun sm -> code.Invoke(&sm))
-        let resumptionInfo = 
-            { new TaskResumptionDynamicInfo<'T>(initialResumptionFunc) with 
-                member info.MoveNext(sm) = 
+
+        let resumptionInfo =
+            { new TaskResumptionDynamicInfo<'T>(initialResumptionFunc) with
+                member info.MoveNext(sm) =
                     let mutable savedExn = null
+
                     try
                         sm.ResumptionDynamicInfo.ResumptionData <- null
-                        let step = info.ResumptionFunc.Invoke(&sm) 
-                        if step then 
+                        let step = info.ResumptionFunc.Invoke(&sm)
+
+                        if step then
                             sm.Data.MethodBuilder.SetResult(sm.Data.Result)
                         else
-                            let mutable awaiter = sm.ResumptionDynamicInfo.ResumptionData :?> ICriticalNotifyCompletion
+                            let mutable awaiter =
+                                sm.ResumptionDynamicInfo.ResumptionData :?> ICriticalNotifyCompletion
+
                             assert not (isNull awaiter)
                             sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
 
                     with exn ->
                         savedExn <- exn
                     // Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
-                    match savedExn with 
+                    match savedExn with
                     | null -> ()
                     | exn -> sm.Data.MethodBuilder.SetException exn
 
                 member _.SetStateMachine(sm, state) =
                     sm.Data.MethodBuilder.SetStateMachine(state)
-                }
+            }
+
         sm.ResumptionDynamicInfo <- resumptionInfo
-        sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
+        sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create ()
         sm.Data.MethodBuilder.Start(&sm)
         sm.Data.MethodBuilder.Task
 
-    member inline _.Run(code : TaskCode<'T, 'T>) : Task<'T> = 
-        if __useResumableCode then 
+    member inline _.Run(code: TaskCode<'T, 'T>) : Task<'T> =
+        if __useResumableCode then
             __stateMachine, Task<'T>>
-                (MoveNextMethodImpl<_>(fun sm -> 
+                (MoveNextMethodImpl<_>(fun sm ->
                     //-- RESUMABLE CODE START
-                    __resumeAt sm.ResumptionPoint 
-                    let mutable __stack_exn : Exception = null
+                    __resumeAt sm.ResumptionPoint
+                    let mutable __stack_exn: Exception = null
+
                     try
                         let __stack_code_fin = code.Invoke(&sm)
+
                         if __stack_code_fin then
                             sm.Data.MethodBuilder.SetResult(sm.Data.Result)
                     with exn ->
@@ -180,11 +201,11 @@ type TaskBuilder() =
                     match __stack_exn with
                     | null -> ()
                     | exn -> sm.Data.MethodBuilder.SetException exn
-                    //-- RESUMABLE CODE END
+                //-- RESUMABLE CODE END
                 ))
                 (SetStateMachineMethodImpl<_>(fun sm state -> sm.Data.MethodBuilder.SetStateMachine(state)))
-                (AfterCode<_,_>(fun sm -> 
-                    sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
+                (AfterCode<_, _>(fun sm ->
+                    sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create ()
                     sm.Data.MethodBuilder.Start(&sm)
                     sm.Data.MethodBuilder.Task))
         else
@@ -194,53 +215,62 @@ type BackgroundTaskBuilder() =
 
     inherit TaskBuilderBase()
 
-    static member RunDynamic(code: TaskCode<'T, 'T>) : Task<'T> = 
+    static member RunDynamic(code: TaskCode<'T, 'T>) : Task<'T> =
         // backgroundTask { .. } escapes to a background thread where necessary
         // See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/
-        if isNull SynchronizationContext.Current && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default) then
+        if
+            isNull SynchronizationContext.Current
+            && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default)
+        then
             TaskBuilder.RunDynamic(code)
         else
             Task.Run<'T>(fun () -> TaskBuilder.RunDynamic(code))
 
     //// Same as TaskBuilder.Run except the start is inside Task.Run if necessary
-    member inline _.Run(code : TaskCode<'T, 'T>) : Task<'T> = 
-         if __useResumableCode then 
+    member inline _.Run(code: TaskCode<'T, 'T>) : Task<'T> =
+        if __useResumableCode then
             __stateMachine, Task<'T>>
-                (MoveNextMethodImpl<_>(fun sm -> 
+                (MoveNextMethodImpl<_>(fun sm ->
                     //-- RESUMABLE CODE START
-                    __resumeAt sm.ResumptionPoint 
+                    __resumeAt sm.ResumptionPoint
+
                     try
                         let __stack_code_fin = code.Invoke(&sm)
+
                         if __stack_code_fin then
                             sm.Data.MethodBuilder.SetResult(sm.Data.Result)
                     with exn ->
                         sm.Data.MethodBuilder.SetException exn
-                    //-- RESUMABLE CODE END
+                //-- RESUMABLE CODE END
                 ))
                 (SetStateMachineMethodImpl<_>(fun sm state -> sm.Data.MethodBuilder.SetStateMachine(state)))
-                (AfterCode<_,Task<'T>>(fun sm -> 
+                (AfterCode<_, Task<'T>>(fun sm ->
                     // backgroundTask { .. } escapes to a background thread where necessary
                     // See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/
-                    if isNull SynchronizationContext.Current && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default) then
-                        sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
+                    if
+                        isNull SynchronizationContext.Current
+                        && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default)
+                    then
+                        sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create ()
                         sm.Data.MethodBuilder.Start(&sm)
                         sm.Data.MethodBuilder.Task
                     else
                         let sm = sm // copy contents of state machine so we can capture it
-                        Task.Run<'T>(fun () -> 
+
+                        Task.Run<'T>(fun () ->
                             let mutable sm = sm // host local mutable copy of contents of state machine on this thread pool thread
-                            sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
+                            sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create ()
                             sm.Data.MethodBuilder.Start(&sm)
                             sm.Data.MethodBuilder.Task)))
-         else
+        else
             BackgroundTaskBuilder.RunDynamic(code)
 
-module TaskBuilder = 
+module TaskBuilder =
 
     let task = TaskBuilder()
     let backgroundTask = BackgroundTaskBuilder()
 
-namespace Microsoft.FSharp.Control.TaskBuilderExtensions 
+namespace Microsoft.FSharp.Control.TaskBuilderExtensions
 
 open Microsoft.FSharp.Control
 open System
@@ -251,112 +281,132 @@ open Microsoft.FSharp.Core.CompilerServices
 open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
 open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
 
-module LowPriority = 
+module LowPriority =
     // Low priority extensions
     type TaskBuilderBase with
 
         []
-        static member inline BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter , 'TOverall
-                                            when  ^TaskLike: (member GetAwaiter:  unit ->  ^Awaiter)
-                                            and ^Awaiter :> ICriticalNotifyCompletion
-                                            and ^Awaiter: (member get_IsCompleted:  unit -> bool)
-                                            and ^Awaiter: (member GetResult:  unit ->  'TResult1)>
-                    (sm: byref<_>, task: ^TaskLike, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : bool =
-
-                let mutable awaiter = (^TaskLike: (member GetAwaiter : unit -> ^Awaiter)(task)) 
-
-                let cont = 
-                    (TaskResumptionFunc<'TOverall>( fun sm -> 
-                        let result = (^Awaiter : (member GetResult : unit -> 'TResult1)(awaiter))
-                        (continuation result).Invoke(&sm)))
-
-                // shortcut to continue immediately
-                if (^Awaiter : (member get_IsCompleted : unit -> bool)(awaiter)) then 
-                    cont.Invoke(&sm)
-                else
-                    sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
-                    sm.ResumptionDynamicInfo.ResumptionFunc <- cont
-                    false
+        static member inline BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall
+            when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
+            and ^Awaiter :> ICriticalNotifyCompletion
+            and ^Awaiter: (member get_IsCompleted: unit -> bool)
+            and ^Awaiter: (member GetResult: unit -> 'TResult1)>
+            (
+                sm: byref<_>,
+                task: ^TaskLike,
+                continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
+            ) : bool =
+
+            let mutable awaiter = (^TaskLike: (member GetAwaiter: unit -> ^Awaiter) (task))
+
+            let cont =
+                (TaskResumptionFunc<'TOverall>(fun sm ->
+                    let result = (^Awaiter: (member GetResult: unit -> 'TResult1) (awaiter))
+                    (continuation result).Invoke(&sm)))
+
+            // shortcut to continue immediately
+            if (^Awaiter: (member get_IsCompleted: unit -> bool) (awaiter)) then
+                cont.Invoke(&sm)
+            else
+                sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
+                sm.ResumptionDynamicInfo.ResumptionFunc <- cont
+                false
 
         []
-        member inline _.Bind< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter , 'TOverall
-                                            when  ^TaskLike: (member GetAwaiter:  unit ->  ^Awaiter)
-                                            and ^Awaiter :> ICriticalNotifyCompletion
-                                            and ^Awaiter: (member get_IsCompleted:  unit -> bool)
-                                            and ^Awaiter: (member GetResult:  unit ->  'TResult1)>
-                    (task: ^TaskLike, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : TaskCode<'TOverall, 'TResult2> =
-
-            TaskCode<'TOverall, _>(fun sm -> 
-                if __useResumableCode then 
+        member inline _.Bind< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall
+            when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
+            and ^Awaiter :> ICriticalNotifyCompletion
+            and ^Awaiter: (member get_IsCompleted: unit -> bool)
+            and ^Awaiter: (member GetResult: unit -> 'TResult1)>
+            (
+                task: ^TaskLike,
+                continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
+            ) : TaskCode<'TOverall, 'TResult2> =
+
+            TaskCode<'TOverall, _>(fun sm ->
+                if __useResumableCode then
                     //-- RESUMABLE CODE START
                     // Get an awaiter from the awaitable
-                    let mutable awaiter = (^TaskLike: (member GetAwaiter : unit -> ^Awaiter)(task)) 
+                    let mutable awaiter = (^TaskLike: (member GetAwaiter: unit -> ^Awaiter) (task))
 
                     let mutable __stack_fin = true
-                    if not (^Awaiter : (member get_IsCompleted : unit -> bool)(awaiter)) then
+
+                    if not (^Awaiter: (member get_IsCompleted: unit -> bool) (awaiter)) then
                         // This will yield with __stack_yield_fin = false
                         // This will resume with __stack_yield_fin = true
                         let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
                         __stack_fin <- __stack_yield_fin
-                
-                    if __stack_fin then 
-                        let result = (^Awaiter : (member GetResult : unit -> 'TResult1)(awaiter))
+
+                    if __stack_fin then
+                        let result = (^Awaiter: (member GetResult: unit -> 'TResult1) (awaiter))
                         (continuation result).Invoke(&sm)
                     else
                         sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
                         false
                 else
-                    TaskBuilderBase.BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter , 'TOverall>(&sm, task, continuation)
-                //-- RESUMABLE CODE END
+                    TaskBuilderBase.BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall>(&sm, task, continuation)
+            //-- RESUMABLE CODE END
             )
 
         []
         member inline this.ReturnFrom< ^TaskLike, ^Awaiter, 'T
-                                              when  ^TaskLike: (member GetAwaiter:  unit ->  ^Awaiter)
-                                              and ^Awaiter :> ICriticalNotifyCompletion
-                                              and ^Awaiter: (member get_IsCompleted: unit -> bool)
-                                              and ^Awaiter: (member GetResult: unit ->  'T)>
-                (task: ^TaskLike) : TaskCode< 'T,  'T> =
+            when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
+            and ^Awaiter :> ICriticalNotifyCompletion
+            and ^Awaiter: (member get_IsCompleted: unit -> bool)
+            and ^Awaiter: (member GetResult: unit -> 'T)>
+            (task: ^TaskLike)
+            : TaskCode<'T, 'T> =
 
             this.Bind(task, (fun v -> this.Return v))
 
-        member inline _.Using<'Resource, 'TOverall, 'T when 'Resource :> IDisposable> (resource: 'Resource, body: 'Resource -> TaskCode<'TOverall, 'T>) =
+        member inline _.Using<'Resource, 'TOverall, 'T when 'Resource :> IDisposable>
+            (
+                resource: 'Resource,
+                body: 'Resource -> TaskCode<'TOverall, 'T>
+            ) =
             ResumableCode.Using(resource, body)
 
-module HighPriority = 
+module HighPriority =
     // High priority extensions
     type TaskBuilderBase with
-        static member BindDynamic (sm: byref<_>, task: Task<'TResult1>, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : bool =
+
+        static member BindDynamic(sm: byref<_>, task: Task<'TResult1>, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : bool =
             let mutable awaiter = task.GetAwaiter()
 
-            let cont = 
-                (TaskResumptionFunc<'TOverall>(fun sm -> 
+            let cont =
+                (TaskResumptionFunc<'TOverall>(fun sm ->
                     let result = awaiter.GetResult()
                     (continuation result).Invoke(&sm)))
 
             // shortcut to continue immediately
-            if awaiter.IsCompleted then 
+            if awaiter.IsCompleted then
                 cont.Invoke(&sm)
             else
                 sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
                 sm.ResumptionDynamicInfo.ResumptionFunc <- cont
                 false
 
-        member inline _.Bind (task: Task<'TResult1>, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : TaskCode<'TOverall, 'TResult2> =
+        member inline _.Bind
+            (
+                task: Task<'TResult1>,
+                continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
+            ) : TaskCode<'TOverall, 'TResult2> =
 
-            TaskCode<'TOverall, _>(fun sm -> 
-                if __useResumableCode then 
+            TaskCode<'TOverall, _>(fun sm ->
+                if __useResumableCode then
                     //-- RESUMABLE CODE START
                     // Get an awaiter from the task
                     let mutable awaiter = task.GetAwaiter()
 
                     let mutable __stack_fin = true
+
                     if not awaiter.IsCompleted then
                         // This will yield with __stack_yield_fin = false
                         // This will resume with __stack_yield_fin = true
                         let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
                         __stack_fin <- __stack_yield_fin
-                    if __stack_fin then 
+
+                    if __stack_fin then
                         let result = awaiter.GetResult()
                         (continuation result).Invoke(&sm)
                     else
@@ -364,21 +414,26 @@ module HighPriority =
                         false
                 else
                     TaskBuilderBase.BindDynamic(&sm, task, continuation)
-                //-- RESUMABLE CODE END
+            //-- RESUMABLE CODE END
             )
 
-        member inline this.ReturnFrom (task: Task<'T>) : TaskCode<'T, 'T> =
+        member inline this.ReturnFrom(task: Task<'T>) : TaskCode<'T, 'T> =
             this.Bind(task, (fun v -> this.Return v))
 
-module MediumPriority = 
+module MediumPriority =
     open HighPriority
 
     // Medium priority extensions
     type TaskBuilderBase with
-        member inline this.Bind (computation: Async<'TResult1>, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : TaskCode<'TOverall, 'TResult2> =
-            this.Bind (Async.StartAsTask computation, continuation)
 
-        member inline this.ReturnFrom (computation: Async<'T>)  : TaskCode<'T, 'T> =
-            this.ReturnFrom (Async.StartAsTask computation)
+        member inline this.Bind
+            (
+                computation: Async<'TResult1>,
+                continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
+            ) : TaskCode<'TOverall, 'TResult2> =
+            this.Bind(Async.StartAsTask computation, continuation)
+
+        member inline this.ReturnFrom(computation: Async<'T>) : TaskCode<'T, 'T> =
+            this.ReturnFrom(Async.StartAsTask computation)
 
 #endif

From 36af364b538cde99d8de8df93db241af53329f22 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Mon, 30 May 2022 15:58:30 +0100
Subject: [PATCH 19/20] Format src/Compiler/Driver (#13195)

* adjust settings

* adjust code

* adjust settings

* adjust code

* fix code before formatting

* remove unnecessary yield

* manual pre-formatting

* preadjust code

* preadjust code

* preadjust code

* preadjust code

* adjust settings"

* adjust settings"

* adjust settings

* adjust settings

* fix build

* adjust settings

* adjust code

* adjust code

* adjust code

* update fantomas

* apply formatting
---
 .editorconfig                                 |   13 +
 .fantomasignore                               |    1 -
 src/Compiler/AbstractIL/il.fs                 |  144 +-
 src/Compiler/AbstractIL/ilnativeres.fs        |   16 +-
 src/Compiler/AbstractIL/ilprint.fs            |   99 +-
 src/Compiler/AbstractIL/ilread.fs             |   90 +-
 src/Compiler/AbstractIL/ilreflect.fs          |   12 +-
 src/Compiler/AbstractIL/ilsign.fs             |   23 +-
 src/Compiler/AbstractIL/ilsupp.fs             |    6 +-
 src/Compiler/AbstractIL/ilwrite.fs            |    7 +-
 src/Compiler/AbstractIL/ilwritepdb.fs         |   24 +-
 src/Compiler/AbstractIL/ilx.fs                |    5 +-
 .../Checking/AugmentWithHashCompare.fs        |   16 +-
 src/Compiler/Checking/CheckDeclarations.fs    |   21 +-
 src/Compiler/CodeGen/IlxGen.fs                |    9 +-
 src/Compiler/Driver/BinaryResourceFormats.fs  |  331 +-
 src/Compiler/Driver/CompilerConfig.fs         | 1311 ++++---
 src/Compiler/Driver/CompilerConfig.fsi        |    6 +-
 src/Compiler/Driver/CompilerDiagnostics.fs    | 3329 +++++++++--------
 src/Compiler/Driver/CompilerImports.fs        | 2492 +++++++-----
 src/Compiler/Driver/CompilerOptions.fs        | 2846 ++++++++------
 src/Compiler/Driver/CreateILModule.fs         |  618 +--
 src/Compiler/Driver/FxResolver.fs             | 1211 +++---
 src/Compiler/Driver/OptimizeInputs.fs         |  182 +-
 src/Compiler/Driver/ParseAndCheckInputs.fs    | 1170 +++---
 src/Compiler/Driver/ScriptClosure.fs          |  566 ++-
 src/Compiler/Driver/StaticLinking.fs          |  788 ++--
 src/Compiler/Driver/XmlDocFileWriter.fs       |   46 +-
 src/Compiler/Driver/fsc.fs                    |  995 +++--
 src/Compiler/Interactive/fsi.fs               |    3 +-
 src/Compiler/Utilities/EditDistance.fs        |    5 +-
 src/Compiler/Utilities/FileSystem.fs          |   18 +-
 src/Compiler/Utilities/HashMultiMap.fs        |    3 +-
 src/Compiler/Utilities/ImmutableArray.fs      |   18 +-
 src/Compiler/Utilities/InternalCollections.fs |   18 +-
 src/Compiler/Utilities/QueueList.fs           |    5 +-
 src/Compiler/Utilities/ResizeArray.fs         |    9 +-
 src/Compiler/Utilities/TaggedCollections.fs   |   90 +-
 src/Compiler/Utilities/illib.fs               |   55 +-
 src/Compiler/Utilities/illib.fsi              |    2 +
 src/Compiler/Utilities/rational.fs            |   20 +-
 src/Compiler/Utilities/sformat.fs             |   25 +-
 .../FSharp.DependencyManager.Utilities.fs     |   17 +-
 src/fsi/console.fs                            |   33 +-
 src/fsi/fsimain.fs                            |   18 +-
 45 files changed, 9724 insertions(+), 6992 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 5d68b1d516b..8f9f7fca164 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,8 +1,11 @@
 root = true
 
+# max_line_length is set to 140. At some point we will reduce it to 120 for as many files as reasonable.
 [*.fs]
 max_line_length=140
 fsharp_newline_between_type_definition_and_members=true
+fsharp_max_function_binding_width=40
+fsharp_max_if_then_else_short_width=60
 fsharp_max_infix_operator_expression=80
 fsharp_max_array_or_list_width=80
 fsharp_max_array_or_list_number_of_items=5
@@ -13,3 +16,13 @@ fsharp_keep_max_number_of_blank_lines=1
 [*.fsi]
 fsharp_newline_between_type_definition_and_members=true
 fsharp_keep_max_number_of_blank_lines=1
+
+# These files contains many imperative if-then expressions which are not clearer on one line
+# Reducing fsharp_max_if_then_else_short_width back to its default formats these over multiple lines.
+[src/FSharp.Build/*.fs]
+fsharp_max_if_then_else_short_width=40
+
+# This file contains a long list of one-line function definitions. Increasing
+# fsharp_max_function_binding_width formats these over a single line.
+[src/Compiler/Driver/CompilerDiagnostics.fs]
+fsharp_max_function_binding_width=70
diff --git a/.fantomasignore b/.fantomasignore
index 2fa5a774170..6fb09e40227 100644
--- a/.fantomasignore
+++ b/.fantomasignore
@@ -16,7 +16,6 @@ artifacts/
 src/Compiler/Checking/**/*.fs
 src/Compiler/CodeGen/**/*.fs
 src/Compiler/DependencyManager/**/*.fs
-src/Compiler/Driver/**/*.fs
 src/Compiler/Facilities/**/*.fs
 src/Compiler/Interactive/**/*.fs
 src/Compiler/Legacy/**/*.fs
diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs
index 30f920524c0..ed951e0a03b 100644
--- a/src/Compiler/AbstractIL/il.fs
+++ b/src/Compiler/AbstractIL/il.fs
@@ -25,9 +25,7 @@ open Internal.Utilities
 
 let logging = false
 
-let _ =
-    if logging then
-        dprintn "* warning: Il.logging is on"
+let _ = if logging then dprintn "* warning: Il.logging is on"
 
 let int_order = LanguagePrimitives.FastGenericComparer
 
@@ -70,19 +68,13 @@ let memoizeNamespaceRightTable =
 let memoizeNamespacePartTable = ConcurrentDictionary()
 
 let splitNameAt (nm: string) idx =
-    if idx < 0 then
-        failwith "splitNameAt: idx < 0"
+    if idx < 0 then failwith "splitNameAt: idx < 0"
 
     let last = nm.Length - 1
 
-    if idx > last then
-        failwith "splitNameAt: idx > last"
+    if idx > last then failwith "splitNameAt: idx > last"
 
-    (nm.Substring(0, idx)),
-    (if idx < last then
-         nm.Substring(idx + 1, last - idx)
-     else
-         "")
+    (nm.Substring(0, idx)), (if idx < last then nm.Substring(idx + 1, last - idx) else "")
 
 let rec splitNamespaceAux (nm: string) =
     match nm.IndexOf '.' with
@@ -218,14 +210,10 @@ module SHA1 =
     let inline (>>>&) (x: int) (y: int) = int32 (uint32 x >>> y)
 
     let f (t, b, c, d) =
-        if t < 20 then
-            (b &&& c) ||| ((~~~b) &&& d)
-        elif t < 40 then
-            b ^^^ c ^^^ d
-        elif t < 60 then
-            (b &&& c) ||| (b &&& d) ||| (c &&& d)
-        else
-            b ^^^ c ^^^ d
+        if t < 20 then (b &&& c) ||| ((~~~b) &&& d)
+        elif t < 40 then b ^^^ c ^^^ d
+        elif t < 60 then (b &&& c) ||| (b &&& d) ||| (c &&& d)
+        else b ^^^ c ^^^ d
 
     []
     let k0to19 = 0x5A827999
@@ -563,8 +551,7 @@ type ILAssemblyRef(data) =
                     addC (convDigit (int32 v / 16))
                     addC (convDigit (int32 v % 16))
             // retargetable can be true only for system assemblies that definitely have Version
-            if aref.Retargetable then
-                add ", Retargetable=Yes"
+            if aref.Retargetable then add ", Retargetable=Yes"
 
         b.ToString()
 
@@ -773,17 +760,9 @@ type ILTypeRef =
             else
                 y.ApproxId
 
-        let xScope =
-            if isPrimaryX then
-                primaryScopeRef
-            else
-                x.Scope
+        let xScope = if isPrimaryX then primaryScopeRef else x.Scope
 
-        let yScope =
-            if isPrimaryY then
-                primaryScopeRef
-            else
-                y.Scope
+        let yScope = if isPrimaryY then primaryScopeRef else y.Scope
 
         (xApproxId = yApproxId)
         && (xScope = yScope)
@@ -806,10 +785,7 @@ type ILTypeRef =
                 else
                     let c = compare x.Name y.Name
 
-                    if c <> 0 then
-                        c
-                    else
-                        compare x.Enclosing y.Enclosing
+                    if c <> 0 then c else compare x.Enclosing y.Enclosing
 
     member tref.FullName = String.concat "." (tref.Enclosing @ [ tref.Name ])
 
@@ -883,11 +859,7 @@ and ["
+        x.TypeRef.ToString() + if isNil x.GenericArgs then "" else "<...>"
 
 and [] ILType =
     | Void
@@ -1859,20 +1831,13 @@ type ILGenericParameterDefs = ILGenericParameterDef list
 let memberAccessOfFlags flags =
     let f = (flags &&& 0x00000007)
 
-    if f = 0x00000001 then
-        ILMemberAccess.Private
-    elif f = 0x00000006 then
-        ILMemberAccess.Public
-    elif f = 0x00000004 then
-        ILMemberAccess.Family
-    elif f = 0x00000002 then
-        ILMemberAccess.FamilyAndAssembly
-    elif f = 0x00000005 then
-        ILMemberAccess.FamilyOrAssembly
-    elif f = 0x00000003 then
-        ILMemberAccess.Assembly
-    else
-        ILMemberAccess.CompilerControlled
+    if f = 0x00000001 then ILMemberAccess.Private
+    elif f = 0x00000006 then ILMemberAccess.Public
+    elif f = 0x00000004 then ILMemberAccess.Family
+    elif f = 0x00000002 then ILMemberAccess.FamilyAndAssembly
+    elif f = 0x00000005 then ILMemberAccess.FamilyOrAssembly
+    elif f = 0x00000003 then ILMemberAccess.Assembly
+    else ILMemberAccess.CompilerControlled
 
 let convertMemberAccess (ilMemberAccess: ILMemberAccess) =
     match ilMemberAccess with
@@ -2509,12 +2474,9 @@ let typeAccessOfFlags flags =
 let typeEncodingOfFlags flags =
     let f = (flags &&& 0x00030000)
 
-    if f = 0x00020000 then
-        ILDefaultPInvokeEncoding.Auto
-    elif f = 0x00010000 then
-        ILDefaultPInvokeEncoding.Unicode
-    else
-        ILDefaultPInvokeEncoding.Ansi
+    if f = 0x00020000 then ILDefaultPInvokeEncoding.Auto
+    elif f = 0x00010000 then ILDefaultPInvokeEncoding.Unicode
+    else ILDefaultPInvokeEncoding.Ansi
 
 []
 type ILTypeDefKind =
@@ -3613,32 +3575,20 @@ and rescopeILType scoref ty =
     | ILType.Boxed cr1 ->
         let cr2 = rescopeILTypeSpec scoref cr1
 
-        if cr1 === cr2 then
-            ty
-        else
-            mkILBoxedType cr2
+        if cr1 === cr2 then ty else mkILBoxedType cr2
     | ILType.Array (s, ety1) ->
         let ety2 = rescopeILType scoref ety1
 
-        if ety1 === ety2 then
-            ty
-        else
-            ILType.Array(s, ety2)
+        if ety1 === ety2 then ty else ILType.Array(s, ety2)
     | ILType.Value cr1 ->
         let cr2 = rescopeILTypeSpec scoref cr1
 
-        if cr1 === cr2 then
-            ty
-        else
-            ILType.Value cr2
+        if cr1 === cr2 then ty else ILType.Value cr2
     | ILType.Modified (b, tref, ty) -> ILType.Modified(b, rescopeILTypeRef scoref tref, rescopeILType scoref ty)
     | x -> x
 
 and rescopeILTypes scoref i =
-    if isNil i then
-        i
-    else
-        List.mapq (rescopeILType scoref) i
+    if isNil i then i else List.mapq (rescopeILType scoref) i
 
 and rescopeILCallSig scoref csig =
     mkILCallSig (csig.CallingConv, rescopeILTypes scoref csig.ArgTypes, rescopeILType scoref csig.ReturnType)
@@ -3933,13 +3883,7 @@ let prependInstrsToCode (instrs: ILInstr list) (c2: ILCode) =
             let dict = Dictionary.newWithSize c2.Labels.Count
 
             for kvp in c2.Labels do
-                dict.Add(
-                    kvp.Key,
-                    if kvp.Value = 0 then
-                        0
-                    else
-                        kvp.Value + n
-                )
+                dict.Add(kvp.Key, (if kvp.Value = 0 then 0 else kvp.Value + n))
 
             dict
 
@@ -4013,22 +3957,10 @@ let mkILField (isStatic, nm, ty, init: ILFieldInit option, at: byte[] option, ac
         fieldType = ty,
         attributes =
             (convertFieldAccess access
-             ||| (if isStatic then
-                      FieldAttributes.Static
-                  else
-                      enum 0)
-             ||| (if isLiteral then
-                      FieldAttributes.Literal
-                  else
-                      enum 0)
-             ||| (if init.IsSome then
-                      FieldAttributes.HasDefault
-                  else
-                      enum 0)
-             ||| (if at.IsSome then
-                      FieldAttributes.HasFieldRVA
-                  else
-                      enum 0)),
+             ||| (if isStatic then FieldAttributes.Static else enum 0)
+             ||| (if isLiteral then FieldAttributes.Literal else enum 0)
+             ||| (if init.IsSome then FieldAttributes.HasDefault else enum 0)
+             ||| (if at.IsSome then FieldAttributes.HasFieldRVA else enum 0)),
         literalValue = init,
         data = at,
         offset = None,
@@ -4362,12 +4294,7 @@ let mkCtorMethSpecForDelegate (ilg: ILGlobals) (ty: ILType, useUIntPtr) =
     let argTys =
         [
             rescopeILType scoref ilg.typ_Object
-            rescopeILType
-                scoref
-                (if useUIntPtr then
-                     ilg.typ_UIntPtr
-                 else
-                     ilg.typ_IntPtr)
+            rescopeILType scoref (if useUIntPtr then ilg.typ_UIntPtr else ilg.typ_IntPtr)
         ]
 
     mkILInstanceMethSpecInTy (ty, ".ctor", argTys, ILType.Void, emptyILGenericArgsList)
@@ -5605,10 +5532,7 @@ and unscopeILType ty =
     | x -> x
 
 and unscopeILTypes i =
-    if List.isEmpty i then
-        i
-    else
-        List.map unscopeILType i
+    if List.isEmpty i then i else List.map unscopeILType i
 
 and unscopeILCallSig csig =
     mkILCallSig (csig.CallingConv, unscopeILTypes csig.ArgTypes, unscopeILType csig.ReturnType)
diff --git a/src/Compiler/AbstractIL/ilnativeres.fs b/src/Compiler/AbstractIL/ilnativeres.fs
index f403a6c5985..3c0752ea8db 100644
--- a/src/Compiler/AbstractIL/ilnativeres.fs
+++ b/src/Compiler/AbstractIL/ilnativeres.fs
@@ -400,10 +400,7 @@ type VersionHelper() =
                 let mutable (values: uint16[]) = Array.zeroCreate 4
 
                 let mutable (lastExplicitValue: int) =
-                    if hasWildcard then
-                        elements.Length - 1
-                    else
-                        elements.Length
+                    if hasWildcard then elements.Length - 1 else elements.Length
 
                 let mutable (parseError: bool) = false
                 let mutable earlyReturn = None
@@ -1147,19 +1144,12 @@ type NativeResourceWriter() =
                             dataWriter.WriteByte 0uy
 
                         false
-                    | e ->
-                        failwithf
-                            "Unknown entry %s"
-                            (if isNull e then
-                                 ""
-                             else
-                                 e.GetType().FullName)
+                    | e -> failwithf "Unknown entry %s" (if isNull e then "" else e.GetType().FullName)
 
                 if id >= 0 then
                     writer.WriteInt32 id
                 else
-                    if name = Unchecked.defaultof<_> then
-                        name <- String.Empty
+                    if name = Unchecked.defaultof<_> then name <- String.Empty
 
                     writer.WriteUInt32(nameOffset ||| 0x80000000u)
                     dataWriter.WriteUInt16(uint16 name.Length)
diff --git a/src/Compiler/AbstractIL/ilprint.fs b/src/Compiler/AbstractIL/ilprint.fs
index 71efc7e0c4b..a9f95cbc1b0 100644
--- a/src/Compiler/AbstractIL/ilprint.fs
+++ b/src/Compiler/AbstractIL/ilprint.fs
@@ -661,20 +661,16 @@ let goutput_fdef _tref env os (fd: ILFieldDef) =
     output_member_access os fd.Access
     output_string os " "
 
-    if fd.IsStatic then
-        output_string os " static "
+    if fd.IsStatic then output_string os " static "
 
-    if fd.IsLiteral then
-        output_string os " literal "
+    if fd.IsLiteral then output_string os " literal "
 
     if fd.IsSpecialName then
         output_string os " specialname rtspecialname "
 
-    if fd.IsInitOnly then
-        output_string os " initonly "
+    if fd.IsInitOnly then output_string os " initonly "
 
-    if fd.NotSerialized then
-        output_string os " notserialized "
+    if fd.NotSerialized then output_string os " notserialized "
 
     goutput_typ env os fd.FieldType
     output_string os " "
@@ -744,8 +740,7 @@ let output_code_label os lab = output_string os (formatCodeLabel lab)
 let goutput_local env os (l: ILLocal) =
     goutput_typ env os l.Type
 
-    if l.IsPinned then
-        output_string os " pinned"
+    if l.IsPinned then output_string os " pinned"
 
 let goutput_param env os (l: ILParameter) =
     match l.Name with
@@ -990,8 +985,7 @@ let rec goutput_instr env os inst =
             let rank = shape.Rank
             output_parens (output_array ", " (goutput_typ env)) os (Array.create rank PrimaryAssemblyILGlobals.typ_Int32)
     | I_ldelema (ro, _, shape, tok) ->
-        if ro = ReadonlyAddress then
-            output_string os "readonly. "
+        if ro = ReadonlyAddress then output_string os "readonly. "
 
         if shape = ILArrayShape.SingleDimensional then
             output_string os "ldelema "
@@ -1040,8 +1034,7 @@ let rec goutput_instr env os inst =
     | _ -> output_string os ""
 
 let goutput_ilmbody env os (il: ILMethodBody) =
-    if il.IsZeroInit then
-        output_string os " .zeroinit\n"
+    if il.IsZeroInit then output_string os " .zeroinit\n"
 
     output_string os " .maxstack "
     output_i32 os il.MaxStack
@@ -1060,21 +1053,11 @@ let goutput_mbody is_entrypoint env os (md: ILMethodDef) =
     else
         output_string os "runtime "
 
-    output_string
-        os
-        (if md.IsInternalCall then
-             "internalcall "
-         else
-             " ")
+    output_string os (if md.IsInternalCall then "internalcall " else " ")
 
     output_string os (if md.IsManaged then "managed " else " ")
 
-    output_string
-        os
-        (if md.IsForwardRef then
-             "forwardref "
-         else
-             " ")
+    output_string os (if md.IsForwardRef then "forwardref " else " ")
 
     output_string os " \n{ \n"
     goutput_security_decls env os md.SecurityDecls
@@ -1084,8 +1067,7 @@ let goutput_mbody is_entrypoint env os (md: ILMethodDef) =
     | MethodBody.IL il -> goutput_ilmbody env os il.Value
     | _ -> ()
 
-    if is_entrypoint then
-        output_string os " .entrypoint"
+    if is_entrypoint then output_string os " .entrypoint"
 
     output_string os "\n"
     output_string os "}\n"
@@ -1096,14 +1078,8 @@ let goutput_mdef env os (md: ILMethodDef) =
             "virtual "
             + (if md.IsFinal then "final " else "")
             + (if md.IsNewSlot then "newslot " else "")
-            + (if md.IsCheckAccessOnOverride then
-                   " strict "
-               else
-                   "")
-            + (if md.IsAbstract then
-                   " abstract "
-               else
-                   "")
+            + (if md.IsCheckAccessOnOverride then " strict " else "")
+            + (if md.IsAbstract then " abstract " else "")
             + "  "
         elif md.IsNonVirtualInstance then
             ""
@@ -1136,14 +1112,8 @@ let goutput_mdef env os (md: ILMethodDef) =
                     | PInvokeCharEncoding.Auto -> " autochar")
                    +
 
-                   (if attr.NoMangle then
-                        " nomangle"
-                    else
-                        "")
-                   + (if attr.LastError then
-                          " lasterr"
-                      else
-                          "")
+                   (if attr.NoMangle then " nomangle" else "")
+                   + (if attr.LastError then " lasterr" else "")
                    + ")"
                | _ -> "")
         elif md.IsClassInitializer then
@@ -1155,14 +1125,11 @@ let goutput_mdef env os (md: ILMethodDef) =
     let menv = ppenv_enter_method (List.length md.GenericParams) env
     output_string os " .method "
 
-    if md.IsHideBySig then
-        output_string os "hidebysig "
+    if md.IsHideBySig then output_string os "hidebysig "
 
-    if md.IsReqSecObj then
-        output_string os "reqsecobj "
+    if md.IsReqSecObj then output_string os "reqsecobj "
 
-    if md.IsSpecialName then
-        output_string os "specialname "
+    if md.IsSpecialName then output_string os "specialname "
 
     if md.IsUnmanagedExport then
         output_string os "unmanagedexp "
@@ -1182,17 +1149,13 @@ let goutput_mdef env os (md: ILMethodDef) =
     (goutput_params menv) os md.Parameters
     output_string os " "
 
-    if md.IsSynchronized then
-        output_string os "synchronized "
+    if md.IsSynchronized then output_string os "synchronized "
 
-    if md.IsMustRun then
-        output_string os "/* mustrun */ "
+    if md.IsMustRun then output_string os "/* mustrun */ "
 
-    if md.IsPreserveSig then
-        output_string os "preservesig "
+    if md.IsPreserveSig then output_string os "preservesig "
 
-    if md.IsNoInline then
-        output_string os "noinlining "
+    if md.IsNoInline then output_string os "noinlining "
 
     if md.IsAggressiveInline then
         output_string os "aggressiveinlining "
@@ -1292,17 +1255,13 @@ let rec goutput_tdef enc env contents os (cd: ILTypeDef) =
         output_string os layout_attr
         output_string os " "
 
-        if cd.IsSealed then
-            output_string os "sealed "
+        if cd.IsSealed then output_string os "sealed "
 
-        if cd.IsAbstract then
-            output_string os "abstract "
+        if cd.IsAbstract then output_string os "abstract "
 
-        if cd.IsSerializable then
-            output_string os "serializable "
+        if cd.IsSerializable then output_string os "serializable "
 
-        if cd.IsComInterop then
-            output_string os "import "
+        if cd.IsComInterop then output_string os "import "
 
         output_sqstring os cd.Name
         goutput_gparams env os cd.GenericParams
@@ -1380,8 +1339,7 @@ let output_assemblyRef os (aref: ILAssemblyRef) =
     output_string os " .assembly extern "
     output_sqstring os aref.Name
 
-    if aref.Retargetable then
-        output_string os " retargetable "
+    if aref.Retargetable then output_string os " retargetable "
 
     output_string os " { "
     output_option output_hash os aref.Hash
@@ -1470,10 +1428,7 @@ let goutput_module_manifest env os modul =
         os
         ((if modul.IsILOnly then 0x0001 else 0)
          ||| (if modul.Is32Bit then 0x0002 else 0)
-         ||| (if modul.Is32BitPreferred then
-                  0x00020003
-              else
-                  0))
+         ||| (if modul.Is32BitPreferred then 0x00020003 else 0))
 
     List.iter (fun r -> goutput_resource env os r) (modul.Resources.AsList())
     output_string os "\n"
diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs
index f9401396e73..dd32b630c70 100644
--- a/src/Compiler/AbstractIL/ilread.fs
+++ b/src/Compiler/AbstractIL/ilread.fs
@@ -101,12 +101,9 @@ let uncodedTokenToTypeDefOrRefOrSpec (tab, tok) =
 
 let uncodedTokenToMethodDefOrRef (tab, tok) =
     let tag =
-        if tab = TableNames.Method then
-            mdor_MethodDef
-        elif tab = TableNames.MemberRef then
-            mdor_MemberRef
-        else
-            failwith "bad table in uncodedTokenToMethodDefOrRef"
+        if tab = TableNames.Method then mdor_MethodDef
+        elif tab = TableNames.MemberRef then mdor_MemberRef
+        else failwith "bad table in uncodedTokenToMethodDefOrRef"
 
     TaggedIndex(tag, tok)
 
@@ -1382,8 +1379,7 @@ let seekReadGuidIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadId
 let seekReadBlobIdx (ctxt: ILMetadataReader) mdv (addr: byref) = seekReadIdx ctxt.blobsBigness mdv &addr
 
 let seekReadModuleRow (ctxt: ILMetadataReader) mdv idx =
-    if idx = 0 then
-        failwith "cannot read Module table row 0"
+    if idx = 0 then failwith "cannot read Module table row 0"
 
     let mutable addr = ctxt.rowAddr TableNames.Module idx
     let generation = seekReadUInt16Adv mdv &addr
@@ -1695,10 +1691,7 @@ let readStringHeapUncached ctxtH idx =
 let readStringHeap (ctxt: ILMetadataReader) idx = ctxt.readStringHeap idx
 
 let readStringHeapOption (ctxt: ILMetadataReader) idx =
-    if idx = 0 then
-        None
-    else
-        Some(readStringHeap ctxt idx)
+    if idx = 0 then None else Some(readStringHeap ctxt idx)
 
 let readBlobHeapUncached ctxtH idx =
     let (ctxt: ILMetadataReader) = getHole ctxtH
@@ -1713,10 +1706,7 @@ let readBlobHeapUncached ctxtH idx =
 let readBlobHeap (ctxt: ILMetadataReader) idx = ctxt.readBlobHeap idx
 
 let readBlobHeapOption ctxt idx =
-    if idx = 0 then
-        None
-    else
-        Some(readBlobHeap ctxt idx)
+    if idx = 0 then None else Some(readBlobHeap ctxt idx)
 
 //let readGuidHeap ctxt idx = seekReadGuid ctxt.mdv (ctxt.guidsStreamPhysicalLoc + idx)
 
@@ -2210,14 +2200,10 @@ and seekReadGenericParamsUncached ctxtH (GenericParamsIdx (numTypars, a, b)) =
                 let variance_flags = flags &&& 0x0003
 
                 let variance =
-                    if variance_flags = 0x0000 then
-                        NonVariant
-                    elif variance_flags = 0x0001 then
-                        CoVariant
-                    elif variance_flags = 0x0002 then
-                        ContraVariant
-                    else
-                        NonVariant
+                    if variance_flags = 0x0000 then NonVariant
+                    elif variance_flags = 0x0001 then CoVariant
+                    elif variance_flags = 0x0002 then ContraVariant
+                    else NonVariant
 
                 let constraints = seekReadGenericParamConstraints ctxt mdv numTypars gpidx
 
@@ -2522,16 +2508,7 @@ and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
         let struct (n, sigptr) = sigptrGetZInt32 bytes sigptr
         let argTys, sigptr = sigptrFold (sigptrGetTy ctxt numTypars) n bytes sigptr
 
-        seekReadTypeDefOrRef
-            ctxt
-            numTypars
-            (if b0 = et_CLASS then
-                 AsObject
-             else
-                 AsValue)
-            argTys
-            tdorIdx,
-        sigptr
+        seekReadTypeDefOrRef ctxt numTypars (if b0 = et_CLASS then AsObject else AsValue) argTys tdorIdx, sigptr
 
     elif b0 = et_CLASS then
         let tdorIdx, sigptr = sigptrGetTypeDefOrRefOrSpecIdx bytes sigptr
@@ -2570,10 +2547,7 @@ and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
                      Some(List.item i lobounds)
                  else
                      None),
-                (if i < numSized then
-                     Some(List.item i sizes)
-                 else
-                     None)
+                (if i < numSized then Some(List.item i sizes) else None)
 
             ILArrayShape(List.init rank dim)
 
@@ -2591,8 +2565,7 @@ and sigptrGetTy (ctxt: ILMetadataReader) numTypars bytes sigptr =
         let ccByte, sigptr = sigptrGetByte bytes sigptr
         let generic, cc = byteAsCallConv ccByte
 
-        if generic then
-            failwith "fptr sig may not be generic"
+        if generic then failwith "fptr sig may not be generic"
 
         let struct (numparams, sigptr) = sigptrGetZInt32 bytes sigptr
         let retTy, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
@@ -2632,10 +2605,7 @@ and sigptrGetLocal (ctxt: ILMetadataReader) numTypars bytes sigptr =
     let pinned, sigptr =
         let b0, sigptr' = sigptrGetByte bytes sigptr
 
-        if b0 = et_PINNED then
-            true, sigptr'
-        else
-            false, sigptr
+        if b0 = et_PINNED then true, sigptr' else false, sigptr
 
     let ty, sigptr = sigptrGetTy ctxt numTypars bytes sigptr
 
@@ -2844,12 +2814,9 @@ and seekReadMethodDefAsMethodDataUncached ctxtH idx =
             (fun i -> i, seekReadTypeDefRowWithExtents ctxt i),
             (fun r -> r),
             (fun (_, ((_, _, _, _, _, methodsIdx), (_, endMethodsIdx))) ->
-                if endMethodsIdx <= idx then
-                    1
-                elif methodsIdx <= idx && idx < endMethodsIdx then
-                    0
-                else
-                    -1),
+                if endMethodsIdx <= idx then 1
+                elif methodsIdx <= idx && idx < endMethodsIdx then 0
+                else -1),
             true,
             fst
         )
@@ -2895,12 +2862,9 @@ and seekReadFieldDefAsFieldSpecUncached ctxtH idx =
             (fun i -> i, seekReadTypeDefRowWithExtents ctxt i),
             (fun r -> r),
             (fun (_, ((_, _, _, _, fieldsIdx, _), (endFieldsIdx, _))) ->
-                if endFieldsIdx <= idx then
-                    1
-                elif fieldsIdx <= idx && idx < endFieldsIdx then
-                    0
-                else
-                    -1),
+                if endFieldsIdx <= idx then 1
+                elif fieldsIdx <= idx && idx < endFieldsIdx then 0
+                else -1),
             true,
             fst
         )
@@ -3619,10 +3583,7 @@ and seekReadTopCode (ctxt: ILMetadataReader) pev mdv numTypars (sz: int) start s
                     dprintn (
                         "invalid instruction: "
                         + string lastb
-                        + (if lastb = 0xfe then
-                               ", " + string lastb2
-                           else
-                               "")
+                        + (if lastb = 0xfe then ", " + string lastb2 else "")
                     )
 
                     I_ret
@@ -3719,8 +3680,7 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (idx, nm, _int
         let isFatFormat = (b &&& e_CorILMethod_FormatMask) = e_CorILMethod_FatFormat
 
         if not isTinyFormat && not isFatFormat then
-            if logging then
-                failwith "unknown format"
+            if logging then failwith "unknown format"
 
             MethodBody.Abstract
         else
@@ -3964,8 +3924,7 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (idx, nm, _int
                             nextSectionBase <- sectionBase + sectionSize
 
                         // Convert the linear code format to the nested code format
-                        if logging then
-                            dprintn "doing localPdbInfos2"
+                        if logging then dprintn "doing localPdbInfos2"
 
                         let localPdbInfos2 = List.map (fun f -> f raw2nextLab) localPdbInfos
 
@@ -3974,8 +3933,7 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (idx, nm, _int
 
                         let code = buildILCode nm lab2pc instrs seh localPdbInfos2
 
-                        if logging then
-                            dprintn "done checking code."
+                        if logging then dprintn "done checking code."
 
                         {
                             IsZeroInit = initlocals
diff --git a/src/Compiler/AbstractIL/ilreflect.fs b/src/Compiler/AbstractIL/ilreflect.fs
index 63497ca4d4c..af089d2a8bc 100644
--- a/src/Compiler/AbstractIL/ilreflect.fs
+++ b/src/Compiler/AbstractIL/ilreflect.fs
@@ -1101,11 +1101,7 @@ let queryableTypeGetMethod cenv emEnv parentT (mref: ILMethodRef) : MethodInfo =
 
         let stat = mref.CallingConv.IsStatic
 
-        let cconv =
-            (if stat then
-                 BindingFlags.Static
-             else
-                 BindingFlags.Instance)
+        let cconv = (if stat then BindingFlags.Static else BindingFlags.Instance)
 
         let methInfo =
             try
@@ -2568,8 +2564,7 @@ let createTypeRef (visited: Dictionary<_, _>, created: Dictionary<_, _>) emEnv t
 
                     match emEnv.emTypMap.TryFind typeRef with
                     | Some (_, tb, _, _) ->
-                        if not (tb.IsCreated()) then
-                            tb.CreateTypeAndLog() |> ignore
+                        if not (tb.IsCreated()) then tb.CreateTypeAndLog() |> ignore
 
                         tb.Assembly
                     | None -> null)
@@ -2595,8 +2590,7 @@ let createTypeRef (visited: Dictionary<_, _>, created: Dictionary<_, _>) emEnv t
     traverseTypeRef tref
 
 let rec buildTypeDefPass4 (visited, created) nesting emEnv (tdef: ILTypeDef) =
-    if verbose2 then
-        dprintf "buildTypeDefPass4 %s\n" tdef.Name
+    if verbose2 then dprintf "buildTypeDefPass4 %s\n" tdef.Name
 
     let tref = mkRefForNestedILTypeDef ILScopeRef.Local (nesting, tdef)
     createTypeRef (visited, created) emEnv tref
diff --git a/src/Compiler/AbstractIL/ilsign.fs b/src/Compiler/AbstractIL/ilsign.fs
index bcd64cb28d4..6118383758e 100644
--- a/src/Compiler/AbstractIL/ilsign.fs
+++ b/src/Compiler/AbstractIL/ilsign.fs
@@ -221,28 +221,14 @@ let toCLRKeyBlob (rsaParameters: RSAParameters) (algId: int) : byte[] =
         bw.Write(int (modulusLength + BLOBHEADER_LENGTH)) // CLRHeader.KeyLength
 
         // Write out the BLOBHEADER
-        bw.Write(
-            byte (
-                if isPrivate = true then
-                    PRIVATEKEYBLOB
-                else
-                    PUBLICKEYBLOB
-            )
-        ) // BLOBHEADER.bType
+        bw.Write(byte (if isPrivate = true then PRIVATEKEYBLOB else PUBLICKEYBLOB)) // BLOBHEADER.bType
 
         bw.Write(byte BLOBHEADER_CURRENT_BVERSION) // BLOBHEADER.bVersion
         bw.Write(int16 0) // BLOBHEADER.wReserved
         bw.Write(int CALG_RSA_SIGN) // BLOBHEADER.aiKeyAlg
 
         // Write the RSAPubKey header
-        bw.Write(
-            int (
-                if isPrivate then
-                    RSA_PRIV_MAGIC
-                else
-                    RSA_PUB_MAGIC
-            )
-        ) // RSAPubKey.magic
+        bw.Write(int (if isPrivate then RSA_PRIV_MAGIC else RSA_PUB_MAGIC)) // RSAPubKey.magic
 
         bw.Write(int (modulusLength * 8)) // RSAPubKey.bitLen
 
@@ -580,10 +566,7 @@ let legacySignerCloseKeyContainer kc =
 
 let legacySignerSignatureSize (pk: byte[]) =
     if runningOnMono then
-        if pk.Length > 32 then
-            pk.Length - 32
-        else
-            128
+        if pk.Length > 32 then pk.Length - 32 else 128
     else
         let mutable pSize = 0u
         let iclrSN = getICLRStrongName ()
diff --git a/src/Compiler/AbstractIL/ilsupp.fs b/src/Compiler/AbstractIL/ilsupp.fs
index fea9a764487..1db894b1801 100644
--- a/src/Compiler/AbstractIL/ilsupp.fs
+++ b/src/Compiler/AbstractIL/ilsupp.fs
@@ -611,8 +611,7 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
         let bNil = Bytes.zeroCreate 3
 
         // Align remaining fields on DWORD (nb. poor bit twiddling code taken from ildasm's dres.cpp)
-        if (dwFiller &&& 0x1) <> 0 then
-            SaveChunk(bNil, 2)
+        if (dwFiller &&& 0x1) <> 0 then SaveChunk(bNil, 2)
 
         //---- Constant part of the header: DWORD, WORD, WORD, DWORD, DWORD
         SaveChunk(dwToBytes resHdr.DataVersion)
@@ -628,8 +627,7 @@ type ResFormatNode(tid: int32, nid: int32, lid: int32, dataOffset: int32, pbLink
 
         dwFiller <- dataEntry.Size &&& 0x3
 
-        if dwFiller <> 0 then
-            SaveChunk(bNil, 4 - dwFiller)
+        if dwFiller <> 0 then SaveChunk(bNil, 4 - dwFiller)
 
         size
 
diff --git a/src/Compiler/AbstractIL/ilwrite.fs b/src/Compiler/AbstractIL/ilwrite.fs
index 61f02d8cc0d..8b8a13be429 100644
--- a/src/Compiler/AbstractIL/ilwrite.fs
+++ b/src/Compiler/AbstractIL/ilwrite.fs
@@ -768,17 +768,16 @@ and GetTypeDescAsTypeRefIdx cenv (scoref, enc, n) =
     GetTypeRefAsTypeRefIdx cenv (mkILNestedTyRef (scoref, enc, n))
 
 and GetResolutionScopeAsElem cenv (scoref, enc) =
-    if isNil enc then
+    match List.tryFrontAndBack enc with
+    | None ->
         match scoref with
         | ILScopeRef.Local -> (rs_Module, 1)
         | ILScopeRef.Assembly aref -> (rs_AssemblyRef, GetAssemblyRefAsIdx cenv aref)
         | ILScopeRef.Module mref -> (rs_ModuleRef, GetModuleRefAsIdx cenv mref)
         | ILScopeRef.PrimaryAssembly -> (rs_AssemblyRef, GetAssemblyRefAsIdx cenv cenv.ilg.primaryAssemblyRef)
-    else
-        let enc2, n2 = List.frontAndBack enc
+    | Some (enc2, n2) ->
         (rs_TypeRef, GetTypeDescAsTypeRefIdx cenv (scoref, enc2, n2))
 
-
 let getTypeInfoAsTypeDefOrRefEncoded cenv (scoref, enc, nm) =
     if isScopeRefLocal scoref then
         let idx = GetIdxForTypeDef cenv (TdKey(enc, nm))
diff --git a/src/Compiler/AbstractIL/ilwritepdb.fs b/src/Compiler/AbstractIL/ilwritepdb.fs
index 3988e136dd8..c81cfc23ad3 100644
--- a/src/Compiler/AbstractIL/ilwritepdb.fs
+++ b/src/Compiler/AbstractIL/ilwritepdb.fs
@@ -130,10 +130,7 @@ module SequencePoint =
         else
             let c1 = compare sp1.Line sp2.Line
 
-            if c1 <> 0 then
-                c1
-            else
-                compare sp1.Column sp2.Column
+            if c1 <> 0 then c1 else compare sp1.Column sp2.Column
 
     let orderByOffset sp1 sp2 = compare sp1.Offset sp2.Offset
 
@@ -377,11 +374,7 @@ type PortablePdbGenerator
 
         let s1, s2 = '/', '\\'
 
-        let separator =
-            if (count name s1) >= (count name s2) then
-                s1
-            else
-                s2
+        let separator = if (count name s1) >= (count name s2) then s1 else s2
 
         let writer = BlobBuilder()
         writer.WriteByte(byte separator)
@@ -445,12 +438,7 @@ type PortablePdbGenerator
     let documentIndex =
         let mutable index = Dictionary(docs.Length)
 
-        let docLength =
-            docs.Length
-            + if String.IsNullOrEmpty sourceLink then
-                  1
-              else
-                  0
+        let docLength = docs.Length + if String.IsNullOrEmpty sourceLink then 1 else 0
 
         metadata.SetCapacity(TableIndex.Document, docLength)
 
@@ -1020,16 +1008,14 @@ let writePdbInfo showTimes outfile pdbfile info cvChunk =
                              | Some p -> sco.StartOffset <> p.StartOffset || sco.EndOffset <> p.EndOffset
                              | None -> true
 
-                         if nested then
-                             pdbOpenScope pdbw sco.StartOffset
+                         if nested then pdbOpenScope pdbw sco.StartOffset
 
                          sco.Locals
                          |> Array.iter (fun v -> pdbDefineLocalVariable pdbw v.Name v.Signature v.Index)
 
                          sco.Children |> Array.iter (writePdbScope (if nested then Some sco else parent))
 
-                         if nested then
-                             pdbCloseScope pdbw sco.EndOffset)
+                         if nested then pdbCloseScope pdbw sco.EndOffset)
 
              match minfo.RootScope with
              | None -> ()
diff --git a/src/Compiler/AbstractIL/ilx.fs b/src/Compiler/AbstractIL/ilx.fs
index 5ef632ba2a5..6a7adab880b 100644
--- a/src/Compiler/AbstractIL/ilx.fs
+++ b/src/Compiler/AbstractIL/ilx.fs
@@ -10,10 +10,7 @@ let mkLowerName (nm: string) =
     // Use the lower case name of a field or constructor as the field/parameter name if it differs from the uppercase name
     let lowerName = String.uncapitalize nm
 
-    if lowerName = nm then
-        "_" + nm
-    else
-        lowerName
+    if lowerName = nm then "_" + nm else lowerName
 
 []
 type IlxUnionCaseField(fd: ILFieldDef) =
diff --git a/src/Compiler/Checking/AugmentWithHashCompare.fs b/src/Compiler/Checking/AugmentWithHashCompare.fs
index 8e2646ada99..f6faeb7bee7 100644
--- a/src/Compiler/Checking/AugmentWithHashCompare.fs
+++ b/src/Compiler/Checking/AugmentWithHashCompare.fs
@@ -150,11 +150,9 @@ let mkDerefThis g m (thisv: Val) thise =
     else thise
 
 let mkCompareTestConjuncts g m exprs =
-    match exprs with 
-    | [] -> mkZero g m
-    | [h] -> h
-    | l -> 
-        let a, b = List.frontAndBack l 
+    match List.tryFrontAndBack exprs with 
+    | None -> mkZero g m
+    | Some (a,b) -> 
         (a, b) ||> List.foldBack (fun e acc -> 
             let nv, ne = mkCompGenLocal m "n" g.int_ty
             mkCompGenLet m nv e
@@ -167,11 +165,9 @@ let mkCompareTestConjuncts g m exprs =
                     acc)))
 
 let mkEqualsTestConjuncts g m exprs =
-    match exprs with 
-    | [] -> mkOne g m
-    | [h] -> h
-    | l -> 
-        let a, b = List.frontAndBack l 
+    match List.tryFrontAndBack exprs with 
+    | None -> mkOne g m
+    | Some (a,b) ->
         List.foldBack (fun e acc -> mkCond DebugPointAtBinding.NoneAtSticky m g.bool_ty e acc (mkFalse g m)) a b
 
 let mkMinimalTy (g: TcGlobals) (tcref: TyconRef) = 
diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs
index 0086fb44d2f..f18b0d1e7ce 100644
--- a/src/Compiler/Checking/CheckDeclarations.fs
+++ b/src/Compiler/Checking/CheckDeclarations.fs
@@ -5816,16 +5816,17 @@ let ApplyAssemblyLevelAutoOpenAttributeToTcEnv g amap (ccu: CcuThunk) scopem env
         warning(Error(FSComp.SR.tcAttributeAutoOpenWasIgnored(p, ccu.AssemblyName), scopem))
         [], env
     let p = splitNamespace p 
-    if isNil p then warn() else
-    let h, t = List.frontAndBack p 
-    let modref = mkNonLocalTyconRef (mkNonLocalEntityRef ccu (Array.ofList h)) t
-    match modref.TryDeref with 
-    | ValueNone -> warn()
-    | ValueSome _ -> 
-        let openTarget = SynOpenDeclTarget.ModuleOrNamespace([], scopem)
-        let openDecl = OpenDeclaration.Create (openTarget, [modref], [], scopem, false)
-        let envinner = OpenModuleOrNamespaceRefs TcResultsSink.NoSink g amap scopem root env [modref] openDecl
-        [openDecl], envinner
+    match List.tryFrontAndBack p with
+    | None -> warn()
+    | Some (h, t) ->
+        let modref = mkNonLocalTyconRef (mkNonLocalEntityRef ccu (Array.ofList h)) t
+        match modref.TryDeref with 
+        | ValueNone -> warn()
+        | ValueSome _ -> 
+            let openTarget = SynOpenDeclTarget.ModuleOrNamespace([], scopem)
+            let openDecl = OpenDeclaration.Create (openTarget, [modref], [], scopem, false)
+            let envinner = OpenModuleOrNamespaceRefs TcResultsSink.NoSink g amap scopem root env [modref] openDecl
+            [openDecl], envinner
 
 // Add the CCU and apply the "AutoOpen" attributes
 let AddCcuToTcEnv (g, amap, scopem, env, assemblyName, ccu, autoOpens, internalsVisibleToAttributes) = 
diff --git a/src/Compiler/CodeGen/IlxGen.fs b/src/Compiler/CodeGen/IlxGen.fs
index 0bb39d8d750..d79c19fe96a 100644
--- a/src/Compiler/CodeGen/IlxGen.fs
+++ b/src/Compiler/CodeGen/IlxGen.fs
@@ -8571,10 +8571,11 @@ and GenExnDef cenv mgbuf eenv m (exnc: Tycon) =
 
 
 let CodegenAssembly cenv eenv mgbuf implFiles =
-    if not (isNil implFiles) then
-        let a, b = List.frontAndBack implFiles
-        let eenv = List.fold (GenImplFile cenv mgbuf None) eenv a
-        let eenv = GenImplFile cenv mgbuf cenv.options.mainMethodInfo eenv b
+    match List.tryFrontAndBack implFiles with
+    | None -> ()
+    | Some (firstImplFiles, lastImplFile) -> 
+        let eenv = List.fold (GenImplFile cenv mgbuf None) eenv firstImplFiles
+        let eenv = GenImplFile cenv mgbuf cenv.options.mainMethodInfo eenv lastImplFile
 
         // Some constructs generate residue types and bindings. Generate these now. They don't result in any
         // top-level initialization code.
diff --git a/src/Compiler/Driver/BinaryResourceFormats.fs b/src/Compiler/Driver/BinaryResourceFormats.fs
index 24ea75803d9..6c5c0ce3a66 100644
--- a/src/Compiler/Driver/BinaryResourceFormats.fs
+++ b/src/Compiler/Driver/BinaryResourceFormats.fs
@@ -8,37 +8,41 @@ open FSharp.Compiler.AbstractIL.IL
 // Helpers for generating binary blobs
 module BinaryGenerationUtilities =
     // Little-endian encoding of int32
-    let b0 n =  byte (n &&& 0xFF)
-    let b1 n =  byte ((n >>> 8) &&& 0xFF)
-    let b2 n =  byte ((n >>> 16) &&& 0xFF)
-    let b3 n =  byte ((n >>> 24) &&& 0xFF)
+    let b0 n = byte (n &&& 0xFF)
+    let b1 n = byte ((n >>> 8) &&& 0xFF)
+    let b2 n = byte ((n >>> 16) &&& 0xFF)
+    let b3 n = byte ((n >>> 24) &&& 0xFF)
 
     let i16 (i: int32) = [| b0 i; b1 i |]
     let i32 (i: int32) = [| b0 i; b1 i; b2 i; b3 i |]
 
     // Emit the bytes and pad to a 32-bit alignment
     let Padded initialAlignment (v: byte[]) =
-        [| yield! v
-           for _ in 1..(4 - (initialAlignment + v.Length) % 4) % 4 do
-               yield 0x0uy |]
+        [|
+            yield! v
+            for _ in 1 .. (4 - (initialAlignment + v.Length) % 4) % 4 do
+                0x0uy
+        |]
 
 // Generate nodes in a .res file format. These are then linked by Abstract IL using linkNativeResources
 module ResFileFormat =
     open BinaryGenerationUtilities
 
-    let ResFileNode(dwTypeID, dwNameID, wMemFlags, wLangID, data: byte[]) =
-        [| yield! i32 data.Length  // DWORD ResHdr.dwDataSize
-           yield! i32 0x00000020  // dwHeaderSize
-           yield! i32 ((dwTypeID <<< 16) ||| 0x0000FFFF)  // dwTypeID, sizeof(DWORD)
-           yield! i32 ((dwNameID <<< 16) ||| 0x0000FFFF)   // dwNameID, sizeof(DWORD)
-           yield! i32 0x00000000 // DWORD       dwDataVersion
-           yield! i16 wMemFlags // WORD        wMemFlags
-           yield! i16 wLangID   // WORD        wLangID
-           yield! i32 0x00000000 // DWORD       dwVersion
-           yield! i32 0x00000000 // DWORD       dwCharacteristics
-           yield! Padded 0 data |]
+    let ResFileNode (dwTypeID, dwNameID, wMemFlags, wLangID, data: byte[]) =
+        [|
+            yield! i32 data.Length // DWORD ResHdr.dwDataSize
+            yield! i32 0x00000020 // dwHeaderSize
+            yield! i32 ((dwTypeID <<< 16) ||| 0x0000FFFF) // dwTypeID, sizeof(DWORD)
+            yield! i32 ((dwNameID <<< 16) ||| 0x0000FFFF) // dwNameID, sizeof(DWORD)
+            yield! i32 0x00000000 // DWORD       dwDataVersion
+            yield! i16 wMemFlags // WORD        wMemFlags
+            yield! i16 wLangID // WORD        wLangID
+            yield! i32 0x00000000 // DWORD       dwVersion
+            yield! i32 0x00000000 // DWORD       dwCharacteristics
+            yield! Padded 0 data
+        |]
 
-    let ResFileHeader() = ResFileNode(0x0, 0x0, 0x0, 0x0, [| |])
+    let ResFileHeader () = ResFileNode(0x0, 0x0, 0x0, 0x0, [||])
 
 // Generate the VS_VERSION_INFO structure held in a Win32 Version Resource in a PE file
 //
@@ -46,172 +50,191 @@ module ResFileFormat =
 module VersionResourceFormat =
     open BinaryGenerationUtilities
 
-    let VersionInfoNode(data: byte[]) =
-        [| yield! i16 (data.Length + 2) // wLength : int16 // Specifies the length, in bytes, of the VS_VERSION_INFO structure.
-           yield! data |]
+    let VersionInfoNode (data: byte[]) =
+        [|
+            yield! i16 (data.Length + 2) // wLength : int16 // Specifies the length, in bytes, of the VS_VERSION_INFO structure.
+            yield! data
+        |]
 
-    let VersionInfoElement(wType, szKey, valueOpt: byte[] option, children: byte[][], isString) =
+    let VersionInfoElement (wType, szKey, valueOpt: byte[] option, children: byte[][], isString) =
         // for String structs, wValueLength represents the word count, not the byte count
-        let wValueLength = (match valueOpt with None -> 0 | Some value -> (if isString then value.Length / 2 else value.Length))
+        let wValueLength =
+            (match valueOpt with
+             | None -> 0
+             | Some value -> (if isString then value.Length / 2 else value.Length))
+
         VersionInfoNode
-            [| yield! i16 wValueLength // wValueLength: int16. Specifies the length, in words, of the Value member.
-               yield! i16 wType        // wType : int16 Specifies the type of data in the version resource.
-               yield! Padded 2 szKey
-               match valueOpt with
-               | None -> yield! []
-               | Some value -> yield! Padded 0 value
-               for child in children do
-                   yield! child  |]
-
-    let Version(version: ILVersionInfo) =
+            [|
+                yield! i16 wValueLength // wValueLength: int16. Specifies the length, in words, of the Value member.
+                yield! i16 wType // wType : int16 Specifies the type of data in the version resource.
+                yield! Padded 2 szKey
+                match valueOpt with
+                | None -> yield! []
+                | Some value -> yield! Padded 0 value
+                for child in children do
+                    yield! child
+            |]
+
+    let Version (version: ILVersionInfo) =
         [| // DWORD dwFileVersionMS
-           // Specifies the most significant 32 bits of the file's binary
-           // version number. This member is used with dwFileVersionLS to form a 64-bit value used
-           // for numeric comparisons.
-           yield! i32 (int32 version.Major <<< 16 ||| int32 version.Minor)
-
-           // DWORD dwFileVersionLS
-           // Specifies the least significant 32 bits of the file's binary
-           // version number. This member is used with dwFileVersionMS to form a 64-bit value used
-           // for numeric comparisons.
-           yield! i32 (int32 version.Build <<< 16 ||| int32 version.Revision)
+            // Specifies the most significant 32 bits of the file's binary
+            // version number. This member is used with dwFileVersionLS to form a 64-bit value used
+            // for numeric comparisons.
+            yield! i32 (int32 version.Major <<< 16 ||| int32 version.Minor)
+
+            // DWORD dwFileVersionLS
+            // Specifies the least significant 32 bits of the file's binary
+            // version number. This member is used with dwFileVersionMS to form a 64-bit value used
+            // for numeric comparisons.
+            yield! i32 (int32 version.Build <<< 16 ||| int32 version.Revision)
         |]
 
-    let String(string, value) =
+    let String (string, value) =
         let wType = 0x1 // Specifies the type of data in the version resource.
         let szKey = Bytes.stringAsUnicodeNullTerminated string
-        VersionInfoElement(wType, szKey, Some (Bytes.stringAsUnicodeNullTerminated value), [| |], true)
+        VersionInfoElement(wType, szKey, Some(Bytes.stringAsUnicodeNullTerminated value), [||], true)
 
-    let StringTable(language, strings) =
+    let StringTable (language, strings) =
         let wType = 0x1 // Specifies the type of data in the version resource.
         let szKey = Bytes.stringAsUnicodeNullTerminated language
-             // Specifies an 8-digit hexadecimal number stored as a Unicode string.
+        // Specifies an 8-digit hexadecimal number stored as a Unicode string.
 
         let children =
-            [| for string in strings do
-                   yield String string |]
+            [|
+                for string in strings do
+                    String string
+            |]
+
         VersionInfoElement(wType, szKey, None, children, false)
 
-    let StringFileInfo(stringTables: #seq >) =
+    let StringFileInfo (stringTables: #seq>) =
         let wType = 0x1 // Specifies the type of data in the version resource.
         let szKey = Bytes.stringAsUnicodeNullTerminated "StringFileInfo" // Contains the Unicode string StringFileInfo
         // Contains an array of one or more StringTable structures.
         let children =
-            [| for stringTable in stringTables do
-                   yield StringTable stringTable |]
+            [|
+                for stringTable in stringTables do
+                    StringTable stringTable
+            |]
+
         VersionInfoElement(wType, szKey, None, children, false)
 
-    let VarFileInfo(vars: #seq) =
+    let VarFileInfo (vars: #seq) =
         let wType = 0x1 // Specifies the type of data in the version resource.
         let szKey = Bytes.stringAsUnicodeNullTerminated "VarFileInfo" // Contains the Unicode string StringFileInfo
         // Contains an array of one or more StringTable structures.
         let children =
-            [| for lang, codePage in vars do
-                   let szKey = Bytes.stringAsUnicodeNullTerminated "Translation"
-                   yield VersionInfoElement(0x0, szKey, Some([| yield! i16 lang
-                                                                yield! i16 codePage |]), [| |], false) |]
+            [|
+                for lang, codePage in vars do
+                    let szKey = Bytes.stringAsUnicodeNullTerminated "Translation"
+                    VersionInfoElement(0x0, szKey, Some([| yield! i16 lang; yield! i16 codePage |]), [||], false)
+            |]
+
         VersionInfoElement(wType, szKey, None, children, false)
 
-    let VS_FIXEDFILEINFO(fileVersion: ILVersionInfo,
-                         productVersion: ILVersionInfo,
-                         dwFileFlagsMask,
-                         dwFileFlags, dwFileOS,
-                         dwFileType, dwFileSubtype,
-                         lwFileDate: int64) =
+    let VS_FIXEDFILEINFO
+        (
+            fileVersion: ILVersionInfo,
+            productVersion: ILVersionInfo,
+            dwFileFlagsMask,
+            dwFileFlags,
+            dwFileOS,
+            dwFileType,
+            dwFileSubtype,
+            lwFileDate: int64
+        ) =
         let dwStrucVersion = 0x00010000
+
         [| // DWORD dwSignature // Contains the value 0xFEEFO4BD.
-           yield! i32  0xFEEF04BD
-
-           // DWORD dwStrucVersion // Specifies the binary version number of this structure.
-           yield! i32 dwStrucVersion
-
-           // DWORD dwFileVersionMS, dwFileVersionLS // Specifies the most/least significant 32 bits of the file's binary version number.
-           yield! Version fileVersion
-
-           // DWORD dwProductVersionMS, dwProductVersionLS // Specifies the most/least significant 32 bits of the file's binary version number.
-           yield! Version productVersion
-
-           // DWORD dwFileFlagsMask // Contains a bitmask that specifies the valid bits in dwFileFlags.
-           yield! i32 dwFileFlagsMask
-
-           // DWORD dwFileFlags // Contains a bitmask that specifies the Boolean attributes of the file.
-           yield! i32 dwFileFlags
-                  // VS_FF_DEBUG 0x1L     The file contains debugging information or is compiled with debugging features enabled.
-                  // VS_FF_INFOINFERRED   The file's version structure was created dynamically; therefore, some of the members
-                  //                      in this structure may be empty or incorrect. This flag should never be set in a file's
-                  //                      VS_VERSION_INFO data.
-                  // VS_FF_PATCHED        The file has been modified and is not identical to the original shipping file of
-                  //                      the same version number.
-                  // VS_FF_PRERELEASE     The file is a development version, not a commercially released product.
-                  // VS_FF_PRIVATEBUILD   The file was not built using standard release procedures. If this flag is
-                  //                      set, the StringFileInfo structure should contain a PrivateBuild entry.
-                  // VS_FF_SPECIALBUILD   The file was built by the original company using standard release procedures
-                  //                      but is a variation of the normal file of the same version number. If this
-                  //                      flag is set, the StringFileInfo structure should contain a SpecialBuild entry.
-
-           //Specifies the operating system for which this file was designed. This member can be one of the following values: Flag
-           yield! i32 dwFileOS
-                  //VOS_DOS 0x0001L  The file was designed for MS-DOS.
-                  //VOS_NT  0x0004L  The file was designed for Windows NT.
-                  //VOS__WINDOWS16  The file was designed for 16-bit Windows.
-                  //VOS__WINDOWS32  The file was designed for the Win32 API.
-                  //VOS_OS216 0x00020000L  The file was designed for 16-bit OS/2.
-                  //VOS_OS232  0x00030000L  The file was designed for 32-bit OS/2.
-                  //VOS__PM16  The file was designed for 16-bit Presentation Manager.
-                  //VOS__PM32  The file was designed for 32-bit Presentation Manager.
-                  //VOS_UNKNOWN  The operating system for which the file was designed is unknown to Windows.
-
-           // Specifies the general type of file. This member can be one of the following values:
-           yield! i32 dwFileType
-
-                //VFT_UNKNOWN The file type is unknown to Windows.
-                //VFT_APP  The file contains an application.
-                //VFT_DLL  The file contains a dynamic-link library (DLL).
-                //VFT_DRV  The file contains a device driver. If dwFileType is VFT_DRV, dwFileSubtype contains a more specific description of the driver.
-                //VFT_FONT  The file contains a font. If dwFileType is VFT_FONT, dwFileSubtype contains a more specific description of the font file.
-                //VFT_VXD  The file contains a virtual device.
-                //VFT_STATIC_LIB  The file contains a static-link library.
-
-           // Specifies the function of the file. The possible values depend on the value of
-           // dwFileType. For all values of dwFileType not described in the following list,
-           // dwFileSubtype is zero. If dwFileType is VFT_DRV, dwFileSubtype can be one of the following values:
-           yield! i32 dwFileSubtype
-                //VFT2_UNKNOWN  The driver type is unknown by Windows.
-                //VFT2_DRV_COMM  The file contains a communications driver.
-                //VFT2_DRV_PRINTER  The file contains a printer driver.
-                //VFT2_DRV_KEYBOARD  The file contains a keyboard driver.
-                //VFT2_DRV_LANGUAGE  The file contains a language driver.
-                //VFT2_DRV_DISPLAY  The file contains a display driver.
-                //VFT2_DRV_MOUSE  The file contains a mouse driver.
-                //VFT2_DRV_NETWORK  The file contains a network driver.
-                //VFT2_DRV_SYSTEM  The file contains a system driver.
-                //VFT2_DRV_INSTALLABLE  The file contains an installable driver.
-                //VFT2_DRV_SOUND  The file contains a sound driver.
-                //
-                //If dwFileType is VFT_FONT, dwFileSubtype can be one of the following values:
-                //
-                //VFT2_UNKNOWN  The font type is unknown by Windows.
-                //VFT2_FONT_RASTER  The file contains a raster font.
-                //VFT2_FONT_VECTOR  The file contains a vector font.
-                //VFT2_FONT_TRUETYPE  The file contains a TrueType font.
-                //
-                //If dwFileType is VFT_VXD, dwFileSubtype contains the virtual device identifier included in the virtual device control block.
-
-           // Specifies the most significant 32 bits of the file's 64-bit binary creation date and time stamp.
-           yield! i32 (int32 (lwFileDate >>> 32))
-
-           //Specifies the least significant 32 bits of the file's 64-bit binary creation date and time stamp.
-           yield! i32 (int32 lwFileDate)
-         |]
-
-    let VS_VERSION_INFO(fixedFileInfo, stringFileInfo, varFileInfo)  =
+            yield! i32 0xFEEF04BD
+
+            // DWORD dwStrucVersion // Specifies the binary version number of this structure.
+            yield! i32 dwStrucVersion
+
+            // DWORD dwFileVersionMS, dwFileVersionLS // Specifies the most/least significant 32 bits of the file's binary version number.
+            yield! Version fileVersion
+
+            // DWORD dwProductVersionMS, dwProductVersionLS // Specifies the most/least significant 32 bits of the file's binary version number.
+            yield! Version productVersion
+
+            // DWORD dwFileFlagsMask // Contains a bitmask that specifies the valid bits in dwFileFlags.
+            yield! i32 dwFileFlagsMask
+
+            // DWORD dwFileFlags // Contains a bitmask that specifies the Boolean attributes of the file.
+            yield! i32 dwFileFlags
+            // VS_FF_DEBUG 0x1L     The file contains debugging information or is compiled with debugging features enabled.
+            // VS_FF_INFOINFERRED   The file's version structure was created dynamically; therefore, some of the members
+            //                      in this structure may be empty or incorrect. This flag should never be set in a file's
+            //                      VS_VERSION_INFO data.
+            // VS_FF_PATCHED        The file has been modified and is not identical to the original shipping file of
+            //                      the same version number.
+            // VS_FF_PRERELEASE     The file is a development version, not a commercially released product.
+            // VS_FF_PRIVATEBUILD   The file was not built using standard release procedures. If this flag is
+            //                      set, the StringFileInfo structure should contain a PrivateBuild entry.
+            // VS_FF_SPECIALBUILD   The file was built by the original company using standard release procedures
+            //                      but is a variation of the normal file of the same version number. If this
+            //                      flag is set, the StringFileInfo structure should contain a SpecialBuild entry.
+
+            //Specifies the operating system for which this file was designed. This member can be one of the following values: Flag
+            yield! i32 dwFileOS
+            //VOS_DOS 0x0001L  The file was designed for MS-DOS.
+            //VOS_NT  0x0004L  The file was designed for Windows NT.
+            //VOS__WINDOWS16  The file was designed for 16-bit Windows.
+            //VOS__WINDOWS32  The file was designed for the Win32 API.
+            //VOS_OS216 0x00020000L  The file was designed for 16-bit OS/2.
+            //VOS_OS232  0x00030000L  The file was designed for 32-bit OS/2.
+            //VOS__PM16  The file was designed for 16-bit Presentation Manager.
+            //VOS__PM32  The file was designed for 32-bit Presentation Manager.
+            //VOS_UNKNOWN  The operating system for which the file was designed is unknown to Windows.
+
+            // Specifies the general type of file. This member can be one of the following values:
+            yield! i32 dwFileType
+
+            //VFT_UNKNOWN The file type is unknown to Windows.
+            //VFT_APP  The file contains an application.
+            //VFT_DLL  The file contains a dynamic-link library (DLL).
+            //VFT_DRV  The file contains a device driver. If dwFileType is VFT_DRV, dwFileSubtype contains a more specific description of the driver.
+            //VFT_FONT  The file contains a font. If dwFileType is VFT_FONT, dwFileSubtype contains a more specific description of the font file.
+            //VFT_VXD  The file contains a virtual device.
+            //VFT_STATIC_LIB  The file contains a static-link library.
+
+            // Specifies the function of the file. The possible values depend on the value of
+            // dwFileType. For all values of dwFileType not described in the following list,
+            // dwFileSubtype is zero. If dwFileType is VFT_DRV, dwFileSubtype can be one of the following values:
+            yield! i32 dwFileSubtype
+            //VFT2_UNKNOWN  The driver type is unknown by Windows.
+            //VFT2_DRV_COMM  The file contains a communications driver.
+            //VFT2_DRV_PRINTER  The file contains a printer driver.
+            //VFT2_DRV_KEYBOARD  The file contains a keyboard driver.
+            //VFT2_DRV_LANGUAGE  The file contains a language driver.
+            //VFT2_DRV_DISPLAY  The file contains a display driver.
+            //VFT2_DRV_MOUSE  The file contains a mouse driver.
+            //VFT2_DRV_NETWORK  The file contains a network driver.
+            //VFT2_DRV_SYSTEM  The file contains a system driver.
+            //VFT2_DRV_INSTALLABLE  The file contains an installable driver.
+            //VFT2_DRV_SOUND  The file contains a sound driver.
+            //
+            //If dwFileType is VFT_FONT, dwFileSubtype can be one of the following values:
+            //
+            //VFT2_UNKNOWN  The font type is unknown by Windows.
+            //VFT2_FONT_RASTER  The file contains a raster font.
+            //VFT2_FONT_VECTOR  The file contains a vector font.
+            //VFT2_FONT_TRUETYPE  The file contains a TrueType font.
+            //
+            //If dwFileType is VFT_VXD, dwFileSubtype contains the virtual device identifier included in the virtual device control block.
+
+            // Specifies the most significant 32 bits of the file's 64-bit binary creation date and time stamp.
+            yield! i32 (int32 (lwFileDate >>> 32))
+
+            //Specifies the least significant 32 bits of the file's 64-bit binary creation date and time stamp.
+            yield! i32 (int32 lwFileDate)
+        |]
+
+    let VS_VERSION_INFO (fixedFileInfo, stringFileInfo, varFileInfo) =
         let wType = 0x0
         let szKey = Bytes.stringAsUnicodeNullTerminated "VS_VERSION_INFO" // Contains the Unicode string VS_VERSION_INFO
         let value = VS_FIXEDFILEINFO fixedFileInfo
-        let children =
-            [| yield StringFileInfo stringFileInfo
-               yield VarFileInfo varFileInfo
-            |]
+        let children = [| StringFileInfo stringFileInfo; VarFileInfo varFileInfo |]
         VersionInfoElement(wType, szKey, Some value, children, false)
 
     let VS_VERSION_INFO_RESOURCE data =
@@ -223,7 +246,7 @@ module VersionResourceFormat =
 
 module ManifestResourceFormat =
 
-    let VS_MANIFEST_RESOURCE(data, isLibrary) =
+    let VS_MANIFEST_RESOURCE (data, isLibrary) =
         let dwTypeID = 0x0018
         let dwNameID = if isLibrary then 0x2 else 0x1
         let wMemFlags = 0x0
diff --git a/src/Compiler/Driver/CompilerConfig.fs b/src/Compiler/Driver/CompilerConfig.fs
index 7027e682d8e..256d06456f3 100644
--- a/src/Compiler/Driver/CompilerConfig.fs
+++ b/src/Compiler/Driver/CompilerConfig.fs
@@ -30,23 +30,22 @@ open FSharp.Compiler.BuildGraph
 open FSharp.Core.CompilerServices
 #endif
 
-let (++) x s = x @ [s]
+let (++) x s = x @ [ s ]
 
 //----------------------------------------------------------------------------
 // Some Globals
 //--------------------------------------------------------------------------
 
-let FSharpSigFileSuffixes = [".mli";".fsi"]
+let FSharpSigFileSuffixes = [ ".mli"; ".fsi" ]
 
-let mlCompatSuffixes = [".mli";".ml"]
+let FSharpMLCompatFileSuffixes = [ ".mli"; ".ml" ]
 
-let FSharpImplFileSuffixes = [".ml";".fs";".fsscript";".fsx"]
+let FSharpImplFileSuffixes = [ ".ml"; ".fs"; ".fsscript"; ".fsx" ]
 
-let FSharpScriptFileSuffixes = [".fsscript";".fsx"]
+let FSharpScriptFileSuffixes = [ ".fsscript"; ".fsx" ]
 
-let doNotRequireNamespaceOrModuleSuffixes = [".mli";".ml"] @ FSharpScriptFileSuffixes
-
-let FSharpIndentationAwareSyntaxFileSuffixes = [ ".fs";".fsscript";".fsx";".fsi" ]
+let FSharpIndentationAwareSyntaxFileSuffixes =
+    [ ".fs"; ".fsscript"; ".fsx"; ".fsi" ]
 
 //--------------------------------------------------------------------------
 // General file name resolver
@@ -57,52 +56,62 @@ exception FileNameNotResolved of searchedLocations: string * fileName: string *
 exception LoadedSourceNotFoundIgnoring of fileName: string * range: range
 
 /// Will return None if the fileName is not found.
-let TryResolveFileUsingPaths(paths, m, fileName) =
+let TryResolveFileUsingPaths (paths, m, fileName) =
     let () =
-        try FileSystem.IsPathRootedShim fileName |> ignore
-        with :? ArgumentException as e -> error(Error(FSComp.SR.buildProblemWithFilename(fileName, e.Message), m))
+        try
+            FileSystem.IsPathRootedShim fileName |> ignore
+        with :? ArgumentException as e ->
+            error (Error(FSComp.SR.buildProblemWithFilename (fileName, e.Message), m))
+
     if FileSystem.IsPathRootedShim fileName then
         if FileSystem.FileExistsShim fileName then
             Some fileName
         else
             None
     else
-        let res = paths |> Seq.tryPick (fun path ->
-            let n = Path.Combine(path, fileName)
-            if FileSystem.FileExistsShim n then Some n
-            else None)
+        let res =
+            paths
+            |> Seq.tryPick (fun path ->
+                let n = Path.Combine(path, fileName)
+                if FileSystem.FileExistsShim n then Some n else None)
+
         res
 
 /// Will raise FileNameNotResolved if the fileName was not found
-let ResolveFileUsingPaths(paths, m, fileName) =
+let ResolveFileUsingPaths (paths, m, fileName) =
     match TryResolveFileUsingPaths(paths, m, fileName) with
     | Some res -> res
     | None ->
         let searchMessage = String.concat "\n " paths
         raise (FileNameNotResolved(fileName, searchMessage, m))
 
-let GetWarningNumber(m, warningNumber: string) =
+let GetWarningNumber (m, warningNumber: string) =
     try
         // Okay so ...
         //      #pragma strips FS of the #pragma "FS0004" and validates the warning number
         //      therefore if we have warning id that starts with a numeric digit we convert it to Some (int32)
         //      anything else is ignored None
-        if Char.IsDigit(warningNumber[0]) then Some (int32 warningNumber)
-        elif warningNumber.StartsWithOrdinal("FS") = true then raise (ArgumentException())
-        else None
+        if Char.IsDigit(warningNumber[0]) then
+            Some(int32 warningNumber)
+        elif warningNumber.StartsWithOrdinal("FS") = true then
+            raise (ArgumentException())
+        else
+            None
     with _ ->
-        warning(Error(FSComp.SR.buildInvalidWarningNumber warningNumber, m))
+        warning (Error(FSComp.SR.buildInvalidWarningNumber warningNumber, m))
         None
 
 let ComputeMakePathAbsolute implicitIncludeDir (path: string) =
     try
         // remove any quotation marks from the path first
         let path = path.Replace("\"", "")
-        if not (FileSystem.IsPathRootedShim path)
-        then Path.Combine (implicitIncludeDir, path)
-        else path
-    with
-        :? ArgumentException -> path
+
+        if not (FileSystem.IsPathRootedShim path) then
+            Path.Combine(implicitIncludeDir, path)
+        else
+            path
+    with :? ArgumentException ->
+        path
 
 //----------------------------------------------------------------------------
 // Configuration
@@ -114,38 +123,56 @@ type CompilerTarget =
     | ConsoleExe
     | Dll
     | Module
-    member x.IsExe = (match x with ConsoleExe | WinExe -> true | _ -> false)
+
+    member x.IsExe =
+        (match x with
+         | ConsoleExe
+         | WinExe -> true
+         | _ -> false)
 
 []
-type ResolveAssemblyReferenceMode = Speculative | ReportErrors
+type ResolveAssemblyReferenceMode =
+    | Speculative
+    | ReportErrors
 
 []
-type CopyFSharpCoreFlag = Yes | No
+type CopyFSharpCoreFlag =
+    | Yes
+    | No
 
 /// Represents the file or string used for the --version flag
 type VersionFlag =
     | VersionString of string
     | VersionFile of string
     | VersionNone
+
     member x.GetVersionInfo implicitIncludeDir =
         let vstr = x.GetVersionString implicitIncludeDir
+
         try
             parseILVersion vstr
-        with _ -> errorR(Error(FSComp.SR.buildInvalidVersionString vstr, rangeStartup)); parseILVersion "0.0.0.0"
+        with _ ->
+            errorR (Error(FSComp.SR.buildInvalidVersionString vstr, rangeStartup))
+            parseILVersion "0.0.0.0"
 
     member x.GetVersionString implicitIncludeDir =
-         match x with
-         | VersionString s -> s
-         | VersionFile s ->
-             let s = if FileSystem.IsPathRootedShim s then s else Path.Combine(implicitIncludeDir, s)
-             if not(FileSystem.FileExistsShim s) then
-                 errorR(Error(FSComp.SR.buildInvalidVersionFile s, rangeStartup)); "0.0.0.0"
-             else
-                 use fs = FileSystem.OpenFileForReadShim(s)
-                 use is = new StreamReader(fs)
-                 is.ReadLine()
-         | VersionNone -> "0.0.0.0"
+        match x with
+        | VersionString s -> s
+        | VersionFile s ->
+            let s =
+                if FileSystem.IsPathRootedShim s then
+                    s
+                else
+                    Path.Combine(implicitIncludeDir, s)
 
+            if not (FileSystem.FileExistsShim s) then
+                errorR (Error(FSComp.SR.buildInvalidVersionFile s, rangeStartup))
+                "0.0.0.0"
+            else
+                use fs = FileSystem.OpenFileForReadShim(s)
+                use is = new StreamReader(fs)
+                is.ReadLine()
+        | VersionNone -> "0.0.0.0"
 
 /// Represents a reference to an assembly. May be backed by a real assembly on disk, or a cross-project
 /// reference backed by information generated by the the compiler service.
@@ -164,7 +191,8 @@ type IRawFSharpAssemblyData =
     abstract GetRawFSharpSignatureData: range * ilShortAssemName: string * fileName: string -> (string * (unit -> ReadOnlyByteMemory)) list
 
     ///  The raw F# optimization data in the assembly, if any
-    abstract GetRawFSharpOptimizationData: range * ilShortAssemName: string * fileName: string -> (string * (unit -> ReadOnlyByteMemory)) list
+    abstract GetRawFSharpOptimizationData:
+        range * ilShortAssemName: string * fileName: string -> (string * (unit -> ReadOnlyByteMemory)) list
 
     ///  The table of type forwarders in the assembly
     abstract GetRawTypeForwarders: unit -> ILExportedTypesAndForwarders
@@ -185,29 +213,36 @@ type IRawFSharpAssemblyData =
 /// Cache of time stamps as we traverse a project description
 type TimeStampCache(defaultTimeStamp: DateTime) =
     let files = ConcurrentDictionary()
-    let projects = ConcurrentDictionary(HashIdentity.Reference)
+
+    let projects =
+        ConcurrentDictionary(HashIdentity.Reference)
 
     member _.GetFileTimeStamp fileName =
         let ok, v = files.TryGetValue fileName
-        if ok then v else
-        let v =
-            try
-                FileSystem.GetLastWriteTimeShim fileName
-            with
-            | :? FileNotFoundException ->
-                defaultTimeStamp
-        files[fileName] <- v
-        v
 
-    member cache.GetProjectReferenceTimeStamp (projectReference: IProjectReference) =
+        if ok then
+            v
+        else
+            let v =
+                try
+                    FileSystem.GetLastWriteTimeShim fileName
+                with :? FileNotFoundException ->
+                    defaultTimeStamp
+
+            files[fileName] <- v
+            v
+
+    member cache.GetProjectReferenceTimeStamp(projectReference: IProjectReference) =
         let ok, v = projects.TryGetValue projectReference
-        if ok then v else
-        let v = defaultArg (projectReference.TryGetLogicalTimeStamp cache) defaultTimeStamp
-        projects[projectReference] <- v
-        v
 
-and []
-    ProjectAssemblyDataResult =
+        if ok then
+            v
+        else
+            let v = defaultArg (projectReference.TryGetLogicalTimeStamp cache) defaultTimeStamp
+            projects[projectReference] <- v
+            v
+
+and [] ProjectAssemblyDataResult =
     | Available of IRawFSharpAssemblyData
     | Unavailable of useOnDiskInstead: bool
 
@@ -229,22 +264,24 @@ and IProjectReference =
     abstract TryGetLogicalTimeStamp: cache: TimeStampCache -> DateTime option
 
 type AssemblyReference =
-    | AssemblyReference of range * string * IProjectReference option
+    | AssemblyReference of range: range * text: string * projectReference: IProjectReference option
 
-    member x.Range = (let (AssemblyReference(m, _, _)) = x in m)
+    member x.Range = (let (AssemblyReference (m, _, _)) = x in m)
 
-    member x.Text = (let (AssemblyReference(_, text, _)) = x in text)
+    member x.Text = (let (AssemblyReference (_, text, _)) = x in text)
 
-    member x.ProjectReference = (let (AssemblyReference(_, _, contents)) = x in contents)
+    member x.ProjectReference = (let (AssemblyReference (_, _, contents)) = x in contents)
 
     member x.SimpleAssemblyNameIs name =
-        (String.Compare(FileSystemUtils.fileNameWithoutExtensionWithValidate false x.Text, name, StringComparison.OrdinalIgnoreCase) = 0) ||
-        not (x.Text.Contains "/") &&
-        not (x.Text.Contains "\\") &&
-        not (x.Text.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase)) &&
-        not (x.Text.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase)) &&
-        (try let aname = System.Reflection.AssemblyName x.Text in aname.Name = name
-         with _ -> false)
+        (String.Compare(FileSystemUtils.fileNameWithoutExtensionWithValidate false x.Text, name, StringComparison.OrdinalIgnoreCase) = 0)
+        || not (x.Text.Contains "/")
+           && not (x.Text.Contains "\\")
+           && not (x.Text.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
+           && not (x.Text.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
+           && (try
+                   let aname = System.Reflection.AssemblyName x.Text in aname.Name = name
+               with _ ->
+                   false)
 
     override x.ToString() = sprintf "AssemblyReference(%s)" x.Text
 
@@ -254,15 +291,17 @@ type ResolvedExtensionReference = ResolvedExtensionReference of string * Assembl
 #endif
 
 type ImportedAssembly =
-    { ILScopeRef: ILScopeRef
-      FSharpViewOfMetadata: CcuThunk
-      AssemblyAutoOpenAttributes: string list
-      AssemblyInternalsVisibleToAttributes: string list
+    {
+        ILScopeRef: ILScopeRef
+        FSharpViewOfMetadata: CcuThunk
+        AssemblyAutoOpenAttributes: string list
+        AssemblyInternalsVisibleToAttributes: string list
 #if !NO_TYPEPROVIDERS
-      IsProviderGenerated: bool
-      mutable TypeProviders: Tainted list
+        IsProviderGenerated: bool
+        mutable TypeProviders: Tainted list
 #endif
-      FSharpOptimizationData: Microsoft.FSharp.Control.Lazy> }
+        FSharpOptimizationData: Microsoft.FSharp.Control.Lazy>
+    }
 
 type AvailableImportedAssembly =
     | ResolvedImportedAssembly of ImportedAssembly
@@ -286,50 +325,61 @@ type TokenizeOption =
     | Unfiltered
 
 type PackageManagerLine =
-    { Directive: Directive
-      LineStatus: LStatus
-      Line: string
-      Range: range }
-
-    static member AddLineWithKey (packageKey: string) (directive:Directive) (line: string) (m: range) (packageManagerLines: Map): Map  =
+    {
+        Directive: Directive
+        LineStatus: LStatus
+        Line: string
+        Range: range
+    }
+
+    static member AddLineWithKey
+        (packageKey: string)
+        (directive: Directive)
+        (line: string)
+        (m: range)
+        (packageManagerLines: Map)
+        : Map =
         let path = PackageManagerLine.StripDependencyManagerKey packageKey line
-        let map =
-            let mutable found = false
-            let result =
-                packageManagerLines
-                |> Map.map(fun key lines ->
-                    if key = packageKey then
-                        found <- true
-                        lines |> List.append [{Directive=directive; LineStatus=LStatus.Unprocessed; Line=path; Range=m}]
-                    else
-                        lines)
-            if found then
-                result
-            else
-                result.Add(packageKey, [{Directive=directive; LineStatus=LStatus.Unprocessed; Line=path; Range=m}])
-        map
-
-    static member RemoveUnprocessedLines (packageKey: string) (packageManagerLines: Map): Map =
-        let map =
-            packageManagerLines
-            |> Map.map(fun key lines ->
-                if key = packageKey then
-                    lines |> List.filter(fun line -> line.LineStatus=LStatus.Processed)
-                else
-                    lines)
-        map
-
-    static member SetLinesAsProcessed (packageKey:string) (packageManagerLines: Map): Map =
-        let map =
-            packageManagerLines
-            |> Map.map(fun key lines ->
-                if key = packageKey then
-                    lines |> List.map(fun line -> {line with LineStatus = LStatus.Processed;})
-                else
-                    lines)
-        map
 
-    static member StripDependencyManagerKey (packageKey: string) (line: string): string =
+        let newLine =
+            {
+                Directive = directive
+                LineStatus = LStatus.Unprocessed
+                Line = path
+                Range = m
+            }
+
+        let oldLines = MultiMap.find packageKey packageManagerLines
+        let newLines = oldLines @ [ newLine ]
+        packageManagerLines.Add(packageKey, newLines)
+
+    static member RemoveUnprocessedLines
+        (packageKey: string)
+        (packageManagerLines: Map)
+        : Map =
+        let oldLines = MultiMap.find packageKey packageManagerLines
+
+        let newLines =
+            oldLines |> List.filter (fun line -> line.LineStatus = LStatus.Processed)
+
+        packageManagerLines.Add(packageKey, newLines)
+
+    static member SetLinesAsProcessed
+        (packageKey: string)
+        (packageManagerLines: Map)
+        : Map =
+        let oldLines = MultiMap.find packageKey packageManagerLines
+
+        let newLines =
+            oldLines
+            |> List.map (fun line ->
+                { line with
+                    LineStatus = LStatus.Processed
+                })
+
+        packageManagerLines.Add(packageKey, newLines)
+
+    static member StripDependencyManagerKey (packageKey: string) (line: string) : string =
         line.Substring(packageKey.Length + 1).Trim()
 
 []
@@ -341,186 +391,185 @@ type MetadataAssemblyGeneration =
 []
 type TcConfigBuilder =
     {
-      mutable primaryAssembly: PrimaryAssembly
-      mutable noFeedback: bool
-      mutable stackReserveSize: int32 option
-      mutable implicitIncludeDir: string (* normally "." *)
-      mutable openDebugInformationForLaterStaticLinking: bool (* only for --standalone *)
-      defaultFSharpBinariesDir: string
-      mutable compilingFSharpCore: bool
-      mutable useIncrementalBuilder: bool
-      mutable includes: string list
-      mutable implicitOpens: string list
-      mutable useFsiAuxLib: bool
-      mutable implicitlyReferenceDotNetAssemblies: bool
-      mutable resolutionEnvironment: LegacyResolutionEnvironment
-      mutable implicitlyResolveAssemblies: bool
-      mutable indentationAwareSyntax: bool option
-      mutable conditionalDefines: string list
-      mutable loadedSources: (range * string * string) list
-      mutable compilerToolPaths: string list
-      mutable referencedDLLs: AssemblyReference list
-      mutable packageManagerLines: Map
-      mutable projectReferences: IProjectReference list
-      mutable knownUnresolvedReferences: UnresolvedAssemblyReference list
-      reduceMemoryUsage: ReduceMemoryFlag
-      mutable subsystemVersion: int * int
-      mutable useHighEntropyVA: bool
-      mutable inputCodePage: int option
-      mutable embedResources: string list
-      mutable diagnosticsOptions: FSharpDiagnosticOptions
-      mutable mlCompatibility: bool
-      mutable checkOverflow: bool
-      mutable showReferenceResolutions: bool
-      mutable outputDir : string option
-      mutable outputFile: string option
-      mutable platform: ILPlatform option
-      mutable prefer32Bit: bool
-      mutable useSimpleResolution: bool
-      mutable target: CompilerTarget
-      mutable debuginfo: bool
-      mutable testFlagEmitFeeFeeAs100001: bool
-      mutable dumpDebugInfo: bool
-      mutable debugSymbolFile: string option
-      (* Backend configuration *)
-      mutable typeCheckOnly: bool
-      mutable parseOnly: bool
-      mutable importAllReferencesOnly: bool
-      mutable simulateException: string option
-      mutable printAst: bool
-      mutable tokenize: TokenizeOption
-      mutable testInteractionParser: bool
-      mutable reportNumDecls: bool
-      mutable printSignature: bool
-      mutable printSignatureFile: string
-      mutable printAllSignatureFiles: bool
-      mutable xmlDocOutputFile: string option
-      mutable stats: bool
-      mutable generateFilterBlocks: bool (* don't generate filter blocks due to bugs on Mono *)
-
-      mutable signer: string option
-      mutable container: string option
-
-      mutable delaysign: bool
-      mutable publicsign: bool
-      mutable version: VersionFlag
-      mutable metadataVersion: string option
-      mutable standalone: bool
-      mutable extraStaticLinkRoots: string list
-      mutable noSignatureData: bool
-      mutable onlyEssentialOptimizationData: bool
-      mutable useOptimizationDataFile: bool
-      mutable jitTracking: bool
-      mutable portablePDB: bool
-      mutable embeddedPDB: bool
-      mutable embedAllSource: bool
-      mutable embedSourceList: string list
-      mutable sourceLink: string
-
-      mutable ignoreSymbolStoreSequencePoints: bool
-      mutable internConstantStrings: bool
-      mutable extraOptimizationIterations: int
-
-      mutable win32icon: string
-      mutable win32res: string
-      mutable win32manifest: string
-      mutable includewin32manifest: bool
-      mutable linkResources: string list
-
-      mutable legacyReferenceResolver: LegacyReferenceResolver
-
-      mutable showFullPaths: bool
-      mutable diagnosticStyle: DiagnosticStyle
-      mutable utf8output: bool
-      mutable flatErrors: bool
-
-      mutable maxErrors: int
-      mutable abortOnError: bool (* intended for fsi scripts that should exit on first error *)
-      mutable baseAddress: int32 option
-      mutable checksumAlgorithm: HashAlgorithm
+        mutable primaryAssembly: PrimaryAssembly
+        mutable noFeedback: bool
+        mutable stackReserveSize: int32 option
+        mutable implicitIncludeDir: string (* normally "." *)
+        mutable openDebugInformationForLaterStaticLinking: bool (* only for --standalone *)
+        defaultFSharpBinariesDir: string
+        mutable compilingFSharpCore: bool
+        mutable useIncrementalBuilder: bool
+        mutable includes: string list
+        mutable implicitOpens: string list
+        mutable useFsiAuxLib: bool
+        mutable implicitlyReferenceDotNetAssemblies: bool
+        mutable resolutionEnvironment: LegacyResolutionEnvironment
+        mutable implicitlyResolveAssemblies: bool
+        mutable indentationAwareSyntax: bool option
+        mutable conditionalDefines: string list
+        mutable loadedSources: (range * string * string) list
+        mutable compilerToolPaths: string list
+        mutable referencedDLLs: AssemblyReference list
+        mutable packageManagerLines: Map
+        mutable projectReferences: IProjectReference list
+        mutable knownUnresolvedReferences: UnresolvedAssemblyReference list
+        reduceMemoryUsage: ReduceMemoryFlag
+        mutable subsystemVersion: int * int
+        mutable useHighEntropyVA: bool
+        mutable inputCodePage: int option
+        mutable embedResources: string list
+        mutable diagnosticsOptions: FSharpDiagnosticOptions
+        mutable mlCompatibility: bool
+        mutable checkOverflow: bool
+        mutable showReferenceResolutions: bool
+        mutable outputDir: string option
+        mutable outputFile: string option
+        mutable platform: ILPlatform option
+        mutable prefer32Bit: bool
+        mutable useSimpleResolution: bool
+        mutable target: CompilerTarget
+        mutable debuginfo: bool
+        mutable testFlagEmitFeeFeeAs100001: bool
+        mutable dumpDebugInfo: bool
+        mutable debugSymbolFile: string option
+        (* Backend configuration *)
+        mutable typeCheckOnly: bool
+        mutable parseOnly: bool
+        mutable importAllReferencesOnly: bool
+        mutable simulateException: string option
+        mutable printAst: bool
+        mutable tokenize: TokenizeOption
+        mutable testInteractionParser: bool
+        mutable reportNumDecls: bool
+        mutable printSignature: bool
+        mutable printSignatureFile: string
+        mutable printAllSignatureFiles: bool
+        mutable xmlDocOutputFile: string option
+        mutable stats: bool
+        mutable generateFilterBlocks: bool (* don't generate filter blocks due to bugs on Mono *)
+
+        mutable signer: string option
+        mutable container: string option
+
+        mutable delaysign: bool
+        mutable publicsign: bool
+        mutable version: VersionFlag
+        mutable metadataVersion: string option
+        mutable standalone: bool
+        mutable extraStaticLinkRoots: string list
+        mutable noSignatureData: bool
+        mutable onlyEssentialOptimizationData: bool
+        mutable useOptimizationDataFile: bool
+        mutable jitTracking: bool
+        mutable portablePDB: bool
+        mutable embeddedPDB: bool
+        mutable embedAllSource: bool
+        mutable embedSourceList: string list
+        mutable sourceLink: string
+
+        mutable ignoreSymbolStoreSequencePoints: bool
+        mutable internConstantStrings: bool
+        mutable extraOptimizationIterations: int
+
+        mutable win32icon: string
+        mutable win32res: string
+        mutable win32manifest: string
+        mutable includewin32manifest: bool
+        mutable linkResources: string list
+
+        mutable legacyReferenceResolver: LegacyReferenceResolver
+
+        mutable showFullPaths: bool
+        mutable diagnosticStyle: DiagnosticStyle
+        mutable utf8output: bool
+        mutable flatErrors: bool
+
+        mutable maxErrors: int
+        mutable abortOnError: bool (* intended for fsi scripts that should exit on first error *)
+        mutable baseAddress: int32 option
+        mutable checksumAlgorithm: HashAlgorithm
 #if DEBUG
-      mutable showOptimizationData: bool
+        mutable showOptimizationData: bool
 #endif
-      mutable showTerms: bool (* show terms between passes? *)
-      mutable writeTermsToFiles: bool (* show terms to files? *)
-      mutable doDetuple: bool (* run detuple pass? *)
-      mutable doTLR: bool (* run TLR pass? *)
-      mutable doFinalSimplify: bool (* do final simplification pass *)
-      mutable optsOn: bool (* optimizations are turned on *)
-      mutable optSettings: Optimizer.OptimizationSettings
-      mutable emitTailcalls: bool
-      mutable deterministic: bool
-      mutable concurrentBuild: bool
-      mutable emitMetadataAssembly: MetadataAssemblyGeneration
-      mutable preferredUiLang: string option
-      mutable lcid: int option
-      mutable productNameForBannerText: string
-      /// show the MS (c) notice, e.g. with help or fsi?
-      mutable showBanner: bool
-
-      /// show times between passes?
-      mutable showTimes: bool
-      mutable showLoadedAssemblies: bool
-      mutable continueAfterParseFailure: bool
+        mutable showTerms: bool (* show terms between passes? *)
+        mutable writeTermsToFiles: bool (* show terms to files? *)
+        mutable doDetuple: bool (* run detuple pass? *)
+        mutable doTLR: bool (* run TLR pass? *)
+        mutable doFinalSimplify: bool (* do final simplification pass *)
+        mutable optsOn: bool (* optimizations are turned on *)
+        mutable optSettings: Optimizer.OptimizationSettings
+        mutable emitTailcalls: bool
+        mutable deterministic: bool
+        mutable concurrentBuild: bool
+        mutable emitMetadataAssembly: MetadataAssemblyGeneration
+        mutable preferredUiLang: string option
+        mutable lcid: int option
+        mutable productNameForBannerText: string
+        /// show the MS (c) notice, e.g. with help or fsi?
+        mutable showBanner: bool
+
+        /// show times between passes?
+        mutable showTimes: bool
+        mutable showLoadedAssemblies: bool
+        mutable continueAfterParseFailure: bool
 
 #if !NO_TYPEPROVIDERS
-      /// show messages about extension type resolution?
-      mutable showExtensionTypeMessages: bool
+        /// show messages about extension type resolution?
+        mutable showExtensionTypeMessages: bool
 #endif
 
-      /// Pause between passes?
-      mutable pause: bool
-
-      /// Whenever possible, emit callvirt instead of call
-      mutable alwaysCallVirt: bool
+        /// Pause between passes?
+        mutable pause: bool
 
-      /// If true, strip away data that would not be of use to end users, but is useful to us for debugging
-      mutable noDebugAttributes: bool
+        /// Whenever possible, emit callvirt instead of call
+        mutable alwaysCallVirt: bool
 
-      /// If true, indicates all type checking and code generation is in the context of fsi.exe
-      isInteractive: bool
+        /// If true, strip away data that would not be of use to end users, but is useful to us for debugging
+        mutable noDebugAttributes: bool
 
-      isInvalidationSupported: bool
+        /// If true, indicates all type checking and code generation is in the context of fsi.exe
+        isInteractive: bool
 
-      /// If true - every expression in quotations will be augmented with full debug info (fileName, location in file)
-      mutable emitDebugInfoInQuotations: bool
+        isInvalidationSupported: bool
 
-      mutable exename: string option
+        /// If true - every expression in quotations will be augmented with full debug info (fileName, location in file)
+        mutable emitDebugInfoInQuotations: bool
 
-      // If true - the compiler will copy FSharp.Core.dll along the produced binaries
-      mutable copyFSharpCore: CopyFSharpCoreFlag
+        mutable exename: string option
 
-      /// When false FSI will lock referenced assemblies requiring process restart, false = disable Shadow Copy false (*default*)
-      mutable shadowCopyReferences: bool
+        // If true - the compiler will copy FSharp.Core.dll along the produced binaries
+        mutable copyFSharpCore: CopyFSharpCoreFlag
 
-      mutable useSdkRefs: bool
+        /// When false FSI will lock referenced assemblies requiring process restart, false = disable Shadow Copy false (*default*)
+        mutable shadowCopyReferences: bool
 
-      mutable fxResolver: FxResolver option
+        mutable useSdkRefs: bool
 
-      // Is F# Interactive using multi-assembly emit?
-      mutable fsiMultiAssemblyEmit: bool
+        mutable fxResolver: FxResolver option
 
-      /// specify the error range for FxResolver
-      rangeForErrors: range
+        // Is F# Interactive using multi-assembly emit?
+        mutable fsiMultiAssemblyEmit: bool
 
-      /// Override the SDK directory used by FxResolver, used for FCS only
-      sdkDirOverride: string option
+        /// specify the error range for FxResolver
+        rangeForErrors: range
 
-      /// A function to call to try to get an object that acts as a snapshot of the metadata section of a .NET binary,
-      /// and from which we can read the metadata. Only used when metadataOnly=true.
-      mutable tryGetMetadataSnapshot: ILReaderTryGetMetadataSnapshot
+        /// Override the SDK directory used by FxResolver, used for FCS only
+        sdkDirOverride: string option
 
-      mutable internalTestSpanStackReferring: bool
+        /// A function to call to try to get an object that acts as a snapshot of the metadata section of a .NET binary,
+        /// and from which we can read the metadata. Only used when metadataOnly=true.
+        mutable tryGetMetadataSnapshot: ILReaderTryGetMetadataSnapshot
 
-      mutable noConditionalErasure: bool
+        mutable internalTestSpanStackReferring: bool
 
-      mutable pathMap: PathMap
+        mutable noConditionalErasure: bool
 
-      mutable langVersion: LanguageVersion
+        mutable pathMap: PathMap
 
-      mutable xmlDocInfoLoader: IXmlDocumentationInfoLoader option
-      }
+        mutable langVersion: LanguageVersion
 
+        mutable xmlDocInfoLoader: IXmlDocumentationInfoLoader option
+    }
 
     // Directories to start probing in
     // Algorithm:
@@ -532,12 +581,12 @@ type TcConfigBuilder =
     //
     // NOTE: it is important this is a delayed IEnumerable sequence. It is recomputed
     // each time a resolution happens and additional paths may be added as a result.
-    member tcConfigB.GetNativeProbingRoots () =
+    member tcConfigB.GetNativeProbingRoots() =
         seq {
             yield! tcConfigB.includes
             yield! tcConfigB.compilerToolPaths
-            yield! (tcConfigB.referencedDLLs |> Seq.map(fun ref -> Path.GetDirectoryName(ref.Text)))
-            yield tcConfigB.implicitIncludeDir
+            yield! (tcConfigB.referencedDLLs |> Seq.map (fun ref -> Path.GetDirectoryName(ref.Text)))
+            tcConfigB.implicitIncludeDir
         }
         |> Seq.distinct
 
@@ -560,147 +609,147 @@ type TcConfigBuilder =
 
         // These are all default values, many can be overridden using the command line switch
         {
-          primaryAssembly = PrimaryAssembly.Mscorlib
-          indentationAwareSyntax = None
-          noFeedback = false
-          stackReserveSize = None
-          conditionalDefines = []
-          openDebugInformationForLaterStaticLinking = false
-          compilingFSharpCore = false
-          useIncrementalBuilder = false
-          implicitOpens = []
-          includes = []
-          resolutionEnvironment = LegacyResolutionEnvironment.EditingOrCompilation false
-          implicitlyReferenceDotNetAssemblies = true
-          implicitlyResolveAssemblies = true
-          compilerToolPaths = []
-          referencedDLLs = []
-          packageManagerLines = Map.empty
-          projectReferences = []
-          knownUnresolvedReferences = []
-          loadedSources = []
-          diagnosticsOptions = FSharpDiagnosticOptions.Default
-          embedResources = []
-          inputCodePage = None
-          subsystemVersion = 4, 0 // per spec for 357994
-          useHighEntropyVA = false
-          mlCompatibility = false
-          checkOverflow = false
-          showReferenceResolutions = false
-          outputDir = None
-          outputFile = None
-          platform = None
-          prefer32Bit = false
-          useSimpleResolution = runningOnMono
-          target = CompilerTarget.ConsoleExe
-          debuginfo = false
-          testFlagEmitFeeFeeAs100001 = false
-          dumpDebugInfo = false
-          debugSymbolFile = None
-
-          (* Backend configuration *)
-          typeCheckOnly = false
-          parseOnly = false
-          importAllReferencesOnly = false
-          simulateException = None
-          printAst = false
-          tokenize = TokenizeOption.AndCompile
-          testInteractionParser = false
-          reportNumDecls = false
-          printSignature = false
-          printSignatureFile = ""
-          printAllSignatureFiles = false
-          xmlDocOutputFile = None
-          stats = false
-          generateFilterBlocks = false (* don't generate filter blocks *)
-
-          signer = None
-          container = None
-          maxErrors = 100
-          abortOnError = false
-          baseAddress = None
-          checksumAlgorithm = HashAlgorithm.Sha256
-
-          delaysign = false
-          publicsign = false
-          version = VersionNone
-          metadataVersion = None
-          standalone = false
-          extraStaticLinkRoots = []
-          noSignatureData = false
-          onlyEssentialOptimizationData = false
-          useOptimizationDataFile = false
-          jitTracking = true
-          portablePDB = true
-          embeddedPDB = false
-          embedAllSource = false
-          embedSourceList = []
-          sourceLink = ""
-          ignoreSymbolStoreSequencePoints = false
-          internConstantStrings = true
-          extraOptimizationIterations = 0
-
-          win32icon = ""
-          win32res = ""
-          win32manifest = ""
-          includewin32manifest = true
-          linkResources = []
-          showFullPaths = false
-          diagnosticStyle = DiagnosticStyle.Default
-
-          utf8output = false
-          flatErrors = false
-
- #if DEBUG
-          showOptimizationData = false
- #endif
-          showTerms = false
-          writeTermsToFiles = false
-
-          doDetuple = false
-          doTLR = false
-          doFinalSimplify = false
-          optsOn = false
-          optSettings = Optimizer.OptimizationSettings.Defaults
-          emitTailcalls = true
-          deterministic = false
-          concurrentBuild = true
-          emitMetadataAssembly = MetadataAssemblyGeneration.None
-          preferredUiLang = None
-          lcid = None
-          productNameForBannerText = FSharpProductName
-          showBanner = true
-          showTimes = false
-          showLoadedAssemblies = false
-          continueAfterParseFailure = false
+            primaryAssembly = PrimaryAssembly.Mscorlib
+            indentationAwareSyntax = None
+            noFeedback = false
+            stackReserveSize = None
+            conditionalDefines = []
+            openDebugInformationForLaterStaticLinking = false
+            compilingFSharpCore = false
+            useIncrementalBuilder = false
+            implicitOpens = []
+            includes = []
+            resolutionEnvironment = LegacyResolutionEnvironment.EditingOrCompilation false
+            implicitlyReferenceDotNetAssemblies = true
+            implicitlyResolveAssemblies = true
+            compilerToolPaths = []
+            referencedDLLs = []
+            packageManagerLines = Map.empty
+            projectReferences = []
+            knownUnresolvedReferences = []
+            loadedSources = []
+            diagnosticsOptions = FSharpDiagnosticOptions.Default
+            embedResources = []
+            inputCodePage = None
+            subsystemVersion = 4, 0 // per spec for 357994
+            useHighEntropyVA = false
+            mlCompatibility = false
+            checkOverflow = false
+            showReferenceResolutions = false
+            outputDir = None
+            outputFile = None
+            platform = None
+            prefer32Bit = false
+            useSimpleResolution = runningOnMono
+            target = CompilerTarget.ConsoleExe
+            debuginfo = false
+            testFlagEmitFeeFeeAs100001 = false
+            dumpDebugInfo = false
+            debugSymbolFile = None
+
+            (* Backend configuration *)
+            typeCheckOnly = false
+            parseOnly = false
+            importAllReferencesOnly = false
+            simulateException = None
+            printAst = false
+            tokenize = TokenizeOption.AndCompile
+            testInteractionParser = false
+            reportNumDecls = false
+            printSignature = false
+            printSignatureFile = ""
+            printAllSignatureFiles = false
+            xmlDocOutputFile = None
+            stats = false
+            generateFilterBlocks = false (* don't generate filter blocks *)
+
+            signer = None
+            container = None
+            maxErrors = 100
+            abortOnError = false
+            baseAddress = None
+            checksumAlgorithm = HashAlgorithm.Sha256
+
+            delaysign = false
+            publicsign = false
+            version = VersionNone
+            metadataVersion = None
+            standalone = false
+            extraStaticLinkRoots = []
+            noSignatureData = false
+            onlyEssentialOptimizationData = false
+            useOptimizationDataFile = false
+            jitTracking = true
+            portablePDB = true
+            embeddedPDB = false
+            embedAllSource = false
+            embedSourceList = []
+            sourceLink = ""
+            ignoreSymbolStoreSequencePoints = false
+            internConstantStrings = true
+            extraOptimizationIterations = 0
+
+            win32icon = ""
+            win32res = ""
+            win32manifest = ""
+            includewin32manifest = true
+            linkResources = []
+            showFullPaths = false
+            diagnosticStyle = DiagnosticStyle.Default
+
+            utf8output = false
+            flatErrors = false
+
+#if DEBUG
+            showOptimizationData = false
+#endif
+            showTerms = false
+            writeTermsToFiles = false
+
+            doDetuple = false
+            doTLR = false
+            doFinalSimplify = false
+            optsOn = false
+            optSettings = Optimizer.OptimizationSettings.Defaults
+            emitTailcalls = true
+            deterministic = false
+            concurrentBuild = true
+            emitMetadataAssembly = MetadataAssemblyGeneration.None
+            preferredUiLang = None
+            lcid = None
+            productNameForBannerText = FSharpProductName
+            showBanner = true
+            showTimes = false
+            showLoadedAssemblies = false
+            continueAfterParseFailure = false
 #if !NO_TYPEPROVIDERS
-          showExtensionTypeMessages = false
+            showExtensionTypeMessages = false
 #endif
-          pause = false
-          alwaysCallVirt = true
-          noDebugAttributes = false
-          emitDebugInfoInQuotations = false
-          exename = None
-          shadowCopyReferences = false
-          useSdkRefs = true
-          fxResolver = None
-          fsiMultiAssemblyEmit = true
-          internalTestSpanStackReferring = false
-          noConditionalErasure = false
-          pathMap = PathMap.empty
-          langVersion = LanguageVersion.Default
-          implicitIncludeDir = implicitIncludeDir
-          defaultFSharpBinariesDir = defaultFSharpBinariesDir
-          reduceMemoryUsage = reduceMemoryUsage
-          legacyReferenceResolver = legacyReferenceResolver
-          isInteractive = isInteractive
-          isInvalidationSupported = isInvalidationSupported
-          copyFSharpCore = defaultCopyFSharpCore
-          tryGetMetadataSnapshot = tryGetMetadataSnapshot
-          useFsiAuxLib = isInteractive
-          rangeForErrors = rangeForErrors
-          sdkDirOverride = sdkDirOverride
-          xmlDocInfoLoader = None
+            pause = false
+            alwaysCallVirt = true
+            noDebugAttributes = false
+            emitDebugInfoInQuotations = false
+            exename = None
+            shadowCopyReferences = false
+            useSdkRefs = true
+            fxResolver = None
+            fsiMultiAssemblyEmit = true
+            internalTestSpanStackReferring = false
+            noConditionalErasure = false
+            pathMap = PathMap.empty
+            langVersion = LanguageVersion.Default
+            implicitIncludeDir = implicitIncludeDir
+            defaultFSharpBinariesDir = defaultFSharpBinariesDir
+            reduceMemoryUsage = reduceMemoryUsage
+            legacyReferenceResolver = legacyReferenceResolver
+            isInteractive = isInteractive
+            isInvalidationSupported = isInvalidationSupported
+            copyFSharpCore = defaultCopyFSharpCore
+            tryGetMetadataSnapshot = tryGetMetadataSnapshot
+            useFsiAuxLib = isInteractive
+            rangeForErrors = rangeForErrors
+            sdkDirOverride = sdkDirOverride
+            xmlDocInfoLoader = None
         }
 
     member tcConfigB.FxResolver =
@@ -709,7 +758,17 @@ type TcConfigBuilder =
         match tcConfigB.fxResolver with
         | None ->
             let useDotNetFramework = (tcConfigB.primaryAssembly = PrimaryAssembly.Mscorlib)
-            let fxResolver = FxResolver(useDotNetFramework, tcConfigB.implicitIncludeDir, rangeForErrors=tcConfigB.rangeForErrors, useSdkRefs=tcConfigB.useSdkRefs, isInteractive=tcConfigB.isInteractive, sdkDirOverride=tcConfigB.sdkDirOverride)
+
+            let fxResolver =
+                FxResolver(
+                    useDotNetFramework,
+                    tcConfigB.implicitIncludeDir,
+                    rangeForErrors = tcConfigB.rangeForErrors,
+                    useSdkRefs = tcConfigB.useSdkRefs,
+                    isInteractive = tcConfigB.isInteractive,
+                    sdkDirOverride = tcConfigB.sdkDirOverride
+                )
+
             tcConfigB.fxResolver <- Some fxResolver
             fxResolver
         | Some fxResolver -> fxResolver
@@ -724,93 +783,140 @@ type TcConfigBuilder =
 
     member tcConfigB.ResolveSourceFile(m, nm, pathLoadedFrom) =
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
-        let paths = seq { yield! tcConfigB.includes; yield pathLoadedFrom }
+
+        let paths =
+            seq {
+                yield! tcConfigB.includes
+                yield pathLoadedFrom
+            }
+
         ResolveFileUsingPaths(paths, m, nm)
 
     /// Decide names of output file, pdb and assembly
     member tcConfigB.DecideNames sourceFiles =
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
-        if sourceFiles = [] then errorR(Error(FSComp.SR.buildNoInputsSpecified(), rangeCmdArgs))
-        let ext() = match tcConfigB.target with CompilerTarget.Dll -> ".dll" | CompilerTarget.Module -> ".netmodule" | CompilerTarget.ConsoleExe | CompilerTarget.WinExe -> ".exe"
-        let implFiles = sourceFiles |> List.filter (fun fileName -> List.exists (FileSystemUtils.checkSuffix fileName) FSharpImplFileSuffixes)
+
+        if sourceFiles = [] then
+            errorR (Error(FSComp.SR.buildNoInputsSpecified (), rangeCmdArgs))
+
+        let ext () =
+            match tcConfigB.target with
+            | CompilerTarget.Dll -> ".dll"
+            | CompilerTarget.Module -> ".netmodule"
+            | CompilerTarget.ConsoleExe
+            | CompilerTarget.WinExe -> ".exe"
+
+        let implFiles =
+            sourceFiles
+            |> List.filter (fun fileName -> List.exists (FileSystemUtils.checkSuffix fileName) FSharpImplFileSuffixes)
+
         let outfile =
             match tcConfigB.outputFile, List.rev implFiles with
-            | None, [] -> "out" + ext()
+            | None, [] -> "out" + ext ()
             | None, h :: _ ->
                 let basic = FileSystemUtils.fileNameOfPath h
-                let modname = try FileSystemUtils.chopExtension basic with _ -> basic
-                modname+(ext())
+
+                let modname =
+                    try
+                        FileSystemUtils.chopExtension basic
+                    with _ ->
+                        basic
+
+                modname + (ext ())
             | Some f, _ -> f
+
         let assemblyName =
             let baseName = FileSystemUtils.fileNameOfPath outfile
             (FileSystemUtils.fileNameWithoutExtension baseName)
 
         let pdbfile =
             if tcConfigB.debuginfo then
-              Some (match tcConfigB.debugSymbolFile with
+                Some(
+                    match tcConfigB.debugSymbolFile with
                     | None -> getDebugFileName outfile tcConfigB.portablePDB
 #if ENABLE_MONO_SUPPORT
                     | Some _ when runningOnMono ->
                         // On Mono, the name of the debug file has to be ".mdb" so specifying it explicitly is an error
-                        warning(Error(FSComp.SR.ilwriteMDBFileNameCannotBeChangedWarning(), rangeCmdArgs))
+                        warning (Error(FSComp.SR.ilwriteMDBFileNameCannotBeChangedWarning (), rangeCmdArgs))
                         getDebugFileName outfile tcConfigB.portablePDB
 #endif
-                    | Some f -> f)
+                    | Some f -> f
+                )
             elif (tcConfigB.debugSymbolFile <> None) && (not tcConfigB.debuginfo) then
-                error(Error(FSComp.SR.buildPdbRequiresDebug(), rangeStartup))
+                error (Error(FSComp.SR.buildPdbRequiresDebug (), rangeStartup))
             else
                 None
+
         tcConfigB.outputFile <- Some outfile
         outfile, pdbfile, assemblyName
 
     member tcConfigB.TurnWarningOff(m, s: string) =
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
+
         match GetWarningNumber(m, s) with
         | None -> ()
         | Some n ->
             // nowarn:62 turns on mlCompatibility, e.g. shows ML compat items in intellisense menus
             if n = 62 then tcConfigB.mlCompatibility <- true
+
             tcConfigB.diagnosticsOptions <-
-                { tcConfigB.diagnosticsOptions with WarnOff = ListSet.insert (=) n tcConfigB.diagnosticsOptions.WarnOff }
+                { tcConfigB.diagnosticsOptions with
+                    WarnOff = ListSet.insert (=) n tcConfigB.diagnosticsOptions.WarnOff
+                }
 
     member tcConfigB.TurnWarningOn(m, s: string) =
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
+
         match GetWarningNumber(m, s) with
         | None -> ()
         | Some n ->
             // warnon 62 turns on mlCompatibility, e.g. shows ML compat items in intellisense menus
             if n = 62 then tcConfigB.mlCompatibility <- false
+
             tcConfigB.diagnosticsOptions <-
-                { tcConfigB.diagnosticsOptions with WarnOn = ListSet.insert (=) n tcConfigB.diagnosticsOptions.WarnOn }
+                { tcConfigB.diagnosticsOptions with
+                    WarnOn = ListSet.insert (=) n tcConfigB.diagnosticsOptions.WarnOn
+                }
 
-    member tcConfigB.AddIncludePath (m, path, pathIncludedFrom) =
+    member tcConfigB.AddIncludePath(m, path, pathIncludedFrom) =
         let absolutePath = ComputeMakePathAbsolute pathIncludedFrom path
 
         let ok =
             let existsOpt =
-                try Some(FileSystem.DirectoryExistsShim absolutePath)
-                with _ -> warning(Error(FSComp.SR.buildInvalidSearchDirectory path, m)); None
+                try
+                    Some(FileSystem.DirectoryExistsShim absolutePath)
+                with _ ->
+                    warning (Error(FSComp.SR.buildInvalidSearchDirectory path, m))
+                    None
 
             match existsOpt with
             | Some exists ->
-                if not exists then warning(Error(FSComp.SR.buildSearchDirectoryNotFound absolutePath, m))
+                if not exists then
+                    warning (Error(FSComp.SR.buildSearchDirectoryNotFound absolutePath, m))
+
                 exists
             | None -> false
 
         if ok && not (List.contains absolutePath tcConfigB.includes) then
-           tcConfigB.includes <- tcConfigB.includes ++ absolutePath
+            tcConfigB.includes <- tcConfigB.includes ++ absolutePath
 
     member tcConfigB.AddLoadedSource(m, originalPath, pathLoadedFrom) =
         if FileSystem.IsInvalidPathShim originalPath then
-            warning(Error(FSComp.SR.buildInvalidFilename originalPath, m))
+            warning (Error(FSComp.SR.buildInvalidFilename originalPath, m))
         else
             let path =
-                let paths = seq { yield! tcConfigB.includes; yield pathLoadedFrom }
+                let paths =
+                    seq {
+                        yield! tcConfigB.includes
+                        yield pathLoadedFrom
+                    }
+
                 match TryResolveFileUsingPaths(paths, m, originalPath) with
                 | Some path -> path
                 | None ->
-                        // File doesn't exist in the paths. Assume it will be in the load-ed from directory.
-                        ComputeMakePathAbsolute pathLoadedFrom originalPath
+                    // File doesn't exist in the paths. Assume it will be in the load-ed from directory.
+                    ComputeMakePathAbsolute pathLoadedFrom originalPath
+
             if not (List.contains path (List.map (fun (_, _, path) -> path) tcConfigB.loadedSources)) then
                 tcConfigB.loadedSources <- tcConfigB.loadedSources ++ (m, originalPath, path)
 
@@ -821,74 +927,92 @@ type TcConfigBuilder =
         tcConfigB.embedResources <- tcConfigB.embedResources ++ fileName
 
     member tcConfigB.AddCompilerToolsByPath path =
-        if not (tcConfigB.compilerToolPaths  |> List.exists (fun text -> path = text)) then // NOTE: We keep same paths if range is different.
-            let compilerToolPath = tcConfigB.compilerToolPaths |> List.tryPick (fun text -> if text = path then Some text else None)
+        if not (tcConfigB.compilerToolPaths |> List.exists (fun text -> path = text)) then // NOTE: We keep same paths if range is different.
+            let compilerToolPath =
+                tcConfigB.compilerToolPaths
+                |> List.tryPick (fun text -> if text = path then Some text else None)
+
             if compilerToolPath.IsNone then
                 tcConfigB.compilerToolPaths <- tcConfigB.compilerToolPaths ++ path
 
-    member tcConfigB.AddReferencedAssemblyByPath (m, path) =
+    member tcConfigB.AddReferencedAssemblyByPath(m, path) =
         if FileSystem.IsInvalidPathShim path then
-            warning(Error(FSComp.SR.buildInvalidAssemblyName(path), m))
-        elif not (tcConfigB.referencedDLLs |> List.exists (fun ar2 -> equals m ar2.Range && path=ar2.Text)) then // NOTE: We keep same paths if range is different.
-             let projectReference = tcConfigB.projectReferences |> List.tryPick (fun pr -> if pr.FileName = path then Some pr else None)
-             tcConfigB.referencedDLLs <- tcConfigB.referencedDLLs ++ AssemblyReference(m, path, projectReference)
-
-    member tcConfigB.AddDependencyManagerText (packageManager: IDependencyManagerProvider, lt, m, path: string) =
+            warning (Error(FSComp.SR.buildInvalidAssemblyName (path), m))
+        elif
+            not
+                (
+                    tcConfigB.referencedDLLs
+                    |> List.exists (fun ar2 -> equals m ar2.Range && path = ar2.Text)
+                )
+        then // NOTE: We keep same paths if range is different.
+            let projectReference =
+                tcConfigB.projectReferences
+                |> List.tryPick (fun pr -> if pr.FileName = path then Some pr else None)
+
+            tcConfigB.referencedDLLs <- tcConfigB.referencedDLLs ++ AssemblyReference(m, path, projectReference)
+
+    member tcConfigB.AddDependencyManagerText(packageManager: IDependencyManagerProvider, lt, m, path: string) =
         tcConfigB.packageManagerLines <- PackageManagerLine.AddLineWithKey packageManager.Key lt path m tcConfigB.packageManagerLines
 
-    member tcConfigB.AddReferenceDirective (dependencyProvider: DependencyProvider, m, path: string, directive) =
+    member tcConfigB.AddReferenceDirective(dependencyProvider: DependencyProvider, m, path: string, directive) =
         let output = tcConfigB.outputDir |> Option.defaultValue ""
 
         let reportError =
-            ResolvingErrorReport (fun errorType err msg ->
+            ResolvingErrorReport(fun errorType err msg ->
                 let error = err, msg
+
                 match errorType with
-                | ErrorReportType.Warning -> warning(Error(error, m))
-                | ErrorReportType.Error -> errorR(Error(error, m)))
+                | ErrorReportType.Warning -> warning (Error(error, m))
+                | ErrorReportType.Error -> errorR (Error(error, m)))
 
-        let dm = dependencyProvider.TryFindDependencyManagerInPath(tcConfigB.compilerToolPaths, output, reportError, path)
+        let dm =
+            dependencyProvider.TryFindDependencyManagerInPath(tcConfigB.compilerToolPaths, output, reportError, path)
 
         match dm with
         // #r "Assembly"
-        | NonNull path, Null ->
-            tcConfigB.AddReferencedAssemblyByPath (m, path)
+        | NonNull path, Null -> tcConfigB.AddReferencedAssemblyByPath(m, path)
 
         | _, NonNull dependencyManager ->
             if tcConfigB.langVersion.SupportsFeature(LanguageFeature.PackageManagement) then
-                tcConfigB.AddDependencyManagerText (dependencyManager, directive, m, path)
+                tcConfigB.AddDependencyManagerText(dependencyManager, directive, m, path)
             else
-                errorR(Error(FSComp.SR.packageManagementRequiresVFive(), m))
+                errorR (Error(FSComp.SR.packageManagementRequiresVFive (), m))
 
-        | Null, Null when directive = Directive.Include ->
-            errorR(Error(FSComp.SR.poundiNotSupportedByRegisteredDependencyManagers(), m))
+        | Null, Null when directive = Directive.Include -> errorR (Error(FSComp.SR.poundiNotSupportedByRegisteredDependencyManagers (), m))
 
-        | Null, Null ->
-           errorR(Error(FSComp.SR.buildInvalidHashrDirective(), m))
+        | Null, Null -> errorR (Error(FSComp.SR.buildInvalidHashrDirective (), m))
 
-    member tcConfigB.RemoveReferencedAssemblyByPath (m, path) =
-        tcConfigB.referencedDLLs <- tcConfigB.referencedDLLs |> List.filter (fun ar -> not (equals ar.Range m) || ar.Text <> path)
+    member tcConfigB.RemoveReferencedAssemblyByPath(m, path) =
+        tcConfigB.referencedDLLs <-
+            tcConfigB.referencedDLLs
+            |> List.filter (fun ar -> not (equals ar.Range m) || ar.Text <> path)
 
-    member tcConfigB.AddPathMapping (oldPrefix, newPrefix) =
+    member tcConfigB.AddPathMapping(oldPrefix, newPrefix) =
         tcConfigB.pathMap <- tcConfigB.pathMap |> PathMap.addMapping oldPrefix newPrefix
 
-    static member SplitCommandLineResourceInfo (ri: string) =
+    static member SplitCommandLineResourceInfo(ri: string) =
         let p = ri.IndexOf ','
+
         if p <> -1 then
             let file = String.sub ri 0 p
-            let rest = String.sub ri (p+1) (String.length ri - p - 1)
+            let rest = String.sub ri (p + 1) (String.length ri - p - 1)
             let p = rest.IndexOf ','
+
             if p <> -1 then
-                let name = String.sub rest 0 p+".resources"
-                let pubpri = String.sub rest (p+1) (rest.Length - p - 1)
-                if pubpri = "public" then file, name, ILResourceAccess.Public
-                elif pubpri = "private" then file, name, ILResourceAccess.Private
-                else error(Error(FSComp.SR.buildInvalidPrivacy pubpri, rangeStartup))
+                let name = String.sub rest 0 p + ".resources"
+                let pubpri = String.sub rest (p + 1) (rest.Length - p - 1)
+
+                if pubpri = "public" then
+                    file, name, ILResourceAccess.Public
+                elif pubpri = "private" then
+                    file, name, ILResourceAccess.Private
+                else
+                    error (Error(FSComp.SR.buildInvalidPrivacy pubpri, rangeStartup))
             else
                 file, rest, ILResourceAccess.Public
         else
             ri, FileSystemUtils.fileNameOfPath ri, ILResourceAccess.Public
 
-
 //----------------------------------------------------------------------------
 // TcConfig
 //--------------------------------------------------------------------------
@@ -899,33 +1023,51 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
 
     // Validate the inputs - this helps ensure errors in options are shown in visual studio rather than only when built
     // However we only validate a minimal number of options at the moment
-    do if validate then try data.version.GetVersionInfo(data.implicitIncludeDir) |> ignore with e -> errorR e
+    do
+        if validate then
+            try
+                data.version.GetVersionInfo(data.implicitIncludeDir) |> ignore
+            with e ->
+                errorR e
 
     // clone the input builder to ensure nobody messes with it.
     let data = { data with pause = data.pause }
 
     let computeKnownDllReference libraryName =
-        let defaultCoreLibraryReference = AssemblyReference(range0, libraryName+".dll", None)
+        let defaultCoreLibraryReference =
+            AssemblyReference(range0, libraryName + ".dll", None)
+
         let nameOfDll (assemRef: AssemblyReference) =
             let fileName = ComputeMakePathAbsolute data.implicitIncludeDir assemRef.Text
+
             if FileSystem.FileExistsShim fileName then
                 assemRef, Some fileName
             else
                 // If the file doesn't exist, let reference resolution logic report the error later...
-                defaultCoreLibraryReference, if equals assemRef.Range rangeStartup then Some fileName else None
-        match data.referencedDLLs |> List.filter (fun assemblyReference -> assemblyReference.SimpleAssemblyNameIs libraryName) with
+                defaultCoreLibraryReference,
+                if equals assemRef.Range rangeStartup then
+                    Some fileName
+                else
+                    None
+
+        match data.referencedDLLs
+              |> List.filter (fun assemblyReference -> assemblyReference.SimpleAssemblyNameIs libraryName)
+            with
         | [] -> defaultCoreLibraryReference, None
-        | [r]
+        | [ r ]
         | r :: _ -> nameOfDll r
 
     // Look for an explicit reference to mscorlib/netstandard.dll or System.Runtime.dll and use that to compute clrRoot and targetFrameworkVersion
-    let primaryAssemblyReference, primaryAssemblyExplicitFilenameOpt = computeKnownDllReference(data.primaryAssembly.Name)
+    let primaryAssemblyReference, primaryAssemblyExplicitFilenameOpt =
+        computeKnownDllReference (data.primaryAssembly.Name)
+
     let fslibReference =
         // Look for explicit FSharp.Core reference otherwise use version that was referenced by compiler
         let dllReference, fileNameOpt = computeKnownDllReference getFSharpCoreLibraryName
+
         match fileNameOpt with
         | Some _ -> dllReference
-        | None -> AssemblyReference(range0, getDefaultFSharpCoreLocation(), None)
+        | None -> AssemblyReference(range0, getDefaultFSharpCoreLocation (), None)
 
     // clrRoot: the location of the primary assembly (mscorlib.dll or netstandard.dll or System.Runtime.dll)
     //
@@ -938,13 +1080,15 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
     let clrRootValue, targetFrameworkVersionValue =
         match primaryAssemblyExplicitFilenameOpt with
         | Some primaryAssemblyFilename ->
-            let fileName = ComputeMakePathAbsolute data.implicitIncludeDir primaryAssemblyFilename
+            let fileName =
+                ComputeMakePathAbsolute data.implicitIncludeDir primaryAssemblyFilename
+
             try
                 let clrRoot = Some(Path.GetDirectoryName(FileSystem.GetFullPathShim fileName))
                 clrRoot, data.legacyReferenceResolver.Impl.HighestInstalledNetFrameworkVersion()
             with e ->
                 // We no longer expect the above to fail but leaving this just in case
-                error(Error(FSComp.SR.buildErrorOpeningBinaryFile(fileName, e.Message), rangeStartup))
+                error (Error(FSComp.SR.buildErrorOpeningBinaryFile (fileName, e.Message), rangeStartup))
         | None ->
 #if !ENABLE_MONO_SUPPORT
             // TODO: we have to get msbuild out of this
@@ -952,87 +1096,96 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
                 None, ""
             else
 #endif
-                None, data.legacyReferenceResolver.Impl.HighestInstalledNetFrameworkVersion()
+            None, data.legacyReferenceResolver.Impl.HighestInstalledNetFrameworkVersion()
 
     let makePathAbsolute path =
         ComputeMakePathAbsolute data.implicitIncludeDir path
 
     let targetFrameworkDirectories =
         try
-          [
-            // Check if we are given an explicit framework root - if so, use that
-            match clrRootValue with
-            | Some x ->
-                let clrRoot = makePathAbsolute x
-                yield clrRoot
-                let clrFacades = Path.Combine(clrRoot, "Facades")
-                if FileSystem.DirectoryExistsShim(clrFacades) then yield clrFacades
-
-            | None ->
-// "there is no really good notion of runtime directory on .NETCore"
+            [
+                // Check if we are given an explicit framework root - if so, use that
+                match clrRootValue with
+                | Some x ->
+                    let clrRoot = makePathAbsolute x
+                    yield clrRoot
+                    let clrFacades = Path.Combine(clrRoot, "Facades")
+
+                    if FileSystem.DirectoryExistsShim(clrFacades) then
+                        yield clrFacades
+
+                | None ->
+                    // "there is no really good notion of runtime directory on .NETCore"
 #if NETSTANDARD
-                let runtimeRoot = Path.GetDirectoryName(typeof.Assembly.Location)
+                    let runtimeRoot = Path.GetDirectoryName(typeof.Assembly.Location)
 #else
-                let runtimeRoot = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
+                    let runtimeRoot =
+                        System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
 #endif
-                let runtimeRootWithoutSlash = runtimeRoot.TrimEnd('/', '\\')
-                let runtimeRootFacades = Path.Combine(runtimeRootWithoutSlash, "Facades")
-                let runtimeRootWPF = Path.Combine(runtimeRootWithoutSlash, "WPF")
-
-                match data.resolutionEnvironment with
-                | LegacyResolutionEnvironment.CompilationAndEvaluation ->
-                    // Default compilation-and-execution-time references on .NET Framework and Mono, e.g. for F# Interactive
-                    //
-                    // In the current way of doing things, F# Interactive refers to implementation assemblies.
-                    yield runtimeRoot
-                    if FileSystem.DirectoryExistsShim runtimeRootFacades then
-                        yield runtimeRootFacades // System.Runtime.dll is in /usr/lib/mono/4.5/Facades
-                    if FileSystem.DirectoryExistsShim runtimeRootWPF then
-                        yield runtimeRootWPF // PresentationCore.dll is in C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF
-
-                    match data.FxResolver.GetFrameworkRefsPackDirectory() with
-                    | Some path when FileSystem.DirectoryExistsShim(path) ->
-                        yield path
-                    | _ -> ()
-
-                | LegacyResolutionEnvironment.EditingOrCompilation _ ->
-#if ENABLE_MONO_SUPPORT
-                    if runningOnMono then
-                        // Default compilation-time references on Mono
+                    let runtimeRootWithoutSlash = runtimeRoot.TrimEnd('/', '\\')
+                    let runtimeRootFacades = Path.Combine(runtimeRootWithoutSlash, "Facades")
+                    let runtimeRootWPF = Path.Combine(runtimeRootWithoutSlash, "WPF")
+
+                    match data.resolutionEnvironment with
+                    | LegacyResolutionEnvironment.CompilationAndEvaluation ->
+                        // Default compilation-and-execution-time references on .NET Framework and Mono, e.g. for F# Interactive
                         //
-                        // On Mono, the default references come from the implementation assemblies.
-                        // This is because we have had trouble reliably using MSBuild APIs to compute DotNetFrameworkReferenceAssembliesRootDirectory on Mono.
+                        // In the current way of doing things, F# Interactive refers to implementation assemblies.
                         yield runtimeRoot
+
                         if FileSystem.DirectoryExistsShim runtimeRootFacades then
                             yield runtimeRootFacades // System.Runtime.dll is in /usr/lib/mono/4.5/Facades
+
                         if FileSystem.DirectoryExistsShim runtimeRootWPF then
                             yield runtimeRootWPF // PresentationCore.dll is in C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF
-                        // On Mono we also add a default reference to the 4.5-api and 4.5-api/Facades directories.
-                        let runtimeRootApi = runtimeRootWithoutSlash + "-api"
-                        let runtimeRootApiFacades = Path.Combine(runtimeRootApi, "Facades")
-                        if FileSystem.DirectoryExistsShim runtimeRootApi then
-                            yield runtimeRootApi
-                        if FileSystem.DirectoryExistsShim runtimeRootApiFacades then
-                             yield runtimeRootApiFacades
-                    else
+
+                        match data.FxResolver.GetFrameworkRefsPackDirectory() with
+                        | Some path when FileSystem.DirectoryExistsShim(path) -> yield path
+                        | _ -> ()
+
+                    | LegacyResolutionEnvironment.EditingOrCompilation _ ->
+#if ENABLE_MONO_SUPPORT
+                        if runningOnMono then
+                            // Default compilation-time references on Mono
+                            //
+                            // On Mono, the default references come from the implementation assemblies.
+                            // This is because we have had trouble reliably using MSBuild APIs to compute DotNetFrameworkReferenceAssembliesRootDirectory on Mono.
+                            yield runtimeRoot
+
+                            if FileSystem.DirectoryExistsShim runtimeRootFacades then
+                                yield runtimeRootFacades // System.Runtime.dll is in /usr/lib/mono/4.5/Facades
+
+                            if FileSystem.DirectoryExistsShim runtimeRootWPF then
+                                yield runtimeRootWPF // PresentationCore.dll is in C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF
+                            // On Mono we also add a default reference to the 4.5-api and 4.5-api/Facades directories.
+                            let runtimeRootApi = runtimeRootWithoutSlash + "-api"
+                            let runtimeRootApiFacades = Path.Combine(runtimeRootApi, "Facades")
+
+                            if FileSystem.DirectoryExistsShim runtimeRootApi then
+                                yield runtimeRootApi
+
+                            if FileSystem.DirectoryExistsShim runtimeRootApiFacades then
+                                yield runtimeRootApiFacades
+                        else
 #endif
                         // Default compilation-time references on .NET Framework
                         //
                         // This is the normal case for "fsc.exe a.fs". We refer to the reference assemblies folder.
-                        let frameworkRoot = data.legacyReferenceResolver.Impl.DotNetFrameworkReferenceAssembliesRootDirectory
+                        let frameworkRoot =
+                            data.legacyReferenceResolver.Impl.DotNetFrameworkReferenceAssembliesRootDirectory
+
                         let frameworkRootVersion = Path.Combine(frameworkRoot, targetFrameworkVersionValue)
                         yield frameworkRootVersion
                         let facades = Path.Combine(frameworkRootVersion, "Facades")
-                        if FileSystem.DirectoryExistsShim facades then
-                            yield facades
+                        if FileSystem.DirectoryExistsShim facades then yield facades
+
                         match data.FxResolver.GetFrameworkRefsPackDirectory() with
-                        | Some path when FileSystem.DirectoryExistsShim(path) ->
-                            yield path
+                        | Some path when FileSystem.DirectoryExistsShim(path) -> yield path
                         | _ -> ()
-                  ]
+            ]
         with e ->
-            errorRecovery e range0; []
-
+            errorRecovery e range0
+            []
 
     member _.fsiMultiAssemblyEmit = data.fsiMultiAssemblyEmit
     member _.FxResolver = data.FxResolver
@@ -1040,7 +1193,10 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
     member _.noFeedback = data.noFeedback
     member _.stackReserveSize = data.stackReserveSize
     member _.implicitIncludeDir = data.implicitIncludeDir
-    member _.openDebugInformationForLaterStaticLinking = data.openDebugInformationForLaterStaticLinking
+
+    member _.openDebugInformationForLaterStaticLinking =
+        data.openDebugInformationForLaterStaticLinking
+
     member _.fsharpBinariesDir = data.defaultFSharpBinariesDir
     member _.compilingFSharpCore = data.compilingFSharpCore
     member _.useIncrementalBuilder = data.useIncrementalBuilder
@@ -1108,7 +1264,7 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
     member _.embedAllSource = data.embedAllSource
     member _.embedSourceList = data.embedSourceList
     member _.sourceLink = data.sourceLink
-    member _.packageManagerLines  = data.packageManagerLines
+    member _.packageManagerLines = data.packageManagerLines
     member _.ignoreSymbolStoreSequencePoints = data.ignoreSymbolStoreSequencePoints
     member _.internConstantStrings = data.internConstantStrings
     member _.extraOptimizationIterations = data.extraOptimizationIterations
@@ -1124,7 +1280,7 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
     member _.maxErrors = data.maxErrors
     member _.baseAddress = data.baseAddress
     member _.checksumAlgorithm = data.checksumAlgorithm
- #if DEBUG
+#if DEBUG
     member _.showOptimizationData = data.showOptimizationData
 #endif
     member _.showTerms = data.showTerms
@@ -1172,26 +1328,33 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
     member _.legacyReferenceResolver = data.legacyReferenceResolver
 
     member _.CloneToBuilder() =
-        { data with conditionalDefines=data.conditionalDefines }
+        { data with
+            conditionalDefines = data.conditionalDefines
+        }
 
     member tcConfig.ComputeCanContainEntryPoint(sourceFiles: string list) =
-        let n = sourceFiles.Length in
-        (sourceFiles |> List.mapi (fun i _ -> (i = n-1)), tcConfig.target.IsExe)
+        let n = sourceFiles.Length in (sourceFiles |> List.mapi (fun i _ -> (i = n - 1)), tcConfig.target.IsExe)
 
     // This call can fail if no CLR is found (this is the path to mscorlib)
-    member _.GetTargetFrameworkDirectories() =
-        targetFrameworkDirectories
+    member _.GetTargetFrameworkDirectories() = targetFrameworkDirectories
 
     member tcConfig.ComputeIndentationAwareSyntaxInitialStatus fileName =
         use _unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
-        let indentationAwareSyntaxOnByDefault = List.exists (FileSystemUtils.checkSuffix fileName) FSharpIndentationAwareSyntaxFileSuffixes
-        if indentationAwareSyntaxOnByDefault then (tcConfig.indentationAwareSyntax <> Some false) else (tcConfig.indentationAwareSyntax = Some true )
+
+        let indentationAwareSyntaxOnByDefault =
+            List.exists (FileSystemUtils.checkSuffix fileName) FSharpIndentationAwareSyntaxFileSuffixes
+
+        if indentationAwareSyntaxOnByDefault then
+            (tcConfig.indentationAwareSyntax <> Some false)
+        else
+            (tcConfig.indentationAwareSyntax = Some true)
 
     member tcConfig.GetAvailableLoadedSources() =
         use _unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
+
         let resolveLoadedSource (m, originalPath, path) =
             try
-                if not(FileSystem.FileExistsShim(path)) then
+                if not (FileSystem.FileExistsShim(path)) then
                     let secondTrial =
                         tcConfig.includes
                         |> List.tryPick (fun root ->
@@ -1199,27 +1362,29 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
                             if FileSystem.FileExistsShim(path) then Some path else None)
 
                     match secondTrial with
-                    | Some path -> Some(m,path)
+                    | Some path -> Some(m, path)
                     | None ->
-                        error(LoadedSourceNotFoundIgnoring(path,m))
+                        error (LoadedSourceNotFoundIgnoring(path, m))
                         None
-                else Some(m,path)
-            with e -> errorRecovery e m; None
+                else
+                    Some(m, path)
+            with e ->
+                errorRecovery e m
+                None
 
-        tcConfig.loadedSources
-        |> List.choose resolveLoadedSource
-        |> List.distinct
+        tcConfig.loadedSources |> List.choose resolveLoadedSource |> List.distinct
 
     // This is not the complete set of search paths, it is just the set
     // that is special to F# (as compared to MSBuild resolution)
     member tcConfig.GetSearchPathsForLibraryFiles() =
-        [ yield! tcConfig.GetTargetFrameworkDirectories()
-          yield! List.map tcConfig.MakePathAbsolute tcConfig.includes
-          yield tcConfig.implicitIncludeDir
-          yield tcConfig.fsharpBinariesDir ]
+        [
+            yield! tcConfig.GetTargetFrameworkDirectories()
+            yield! List.map tcConfig.MakePathAbsolute tcConfig.includes
+            tcConfig.implicitIncludeDir
+            tcConfig.fsharpBinariesDir
+        ]
 
-    member _.MakePathAbsolute path =
-        makePathAbsolute path
+    member _.MakePathAbsolute path = makePathAbsolute path
 
     member _.ResolveSourceFile(m, fileName, pathLoadedFrom) =
         data.ResolveSourceFile(m, fileName, pathLoadedFrom)
@@ -1239,20 +1404,23 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
     ///
     /// Returning true may mean that the file is locked and/or placed into the
     /// 'framework' reference set that is potentially shared across multiple compilations.
-    member tcConfig.IsSystemAssembly (fileName: string) =
+    member tcConfig.IsSystemAssembly(fileName: string) =
         try
-            FileSystem.FileExistsShim fileName &&
-            ((tcConfig.GetTargetFrameworkDirectories() |> List.exists (fun clrRoot -> clrRoot = Path.GetDirectoryName fileName)) ||
-             (tcConfig.FxResolver.GetSystemAssemblies().Contains (FileSystemUtils.fileNameWithoutExtension fileName)) ||
-             tcConfig.FxResolver.IsInReferenceAssemblyPackDirectory fileName)
+            FileSystem.FileExistsShim fileName
+            && ((tcConfig.GetTargetFrameworkDirectories()
+                 |> List.exists (fun clrRoot -> clrRoot = Path.GetDirectoryName fileName))
+                || (tcConfig
+                       .FxResolver
+                       .GetSystemAssemblies()
+                       .Contains(FileSystemUtils.fileNameWithoutExtension fileName))
+                || tcConfig.FxResolver.IsInReferenceAssemblyPackDirectory fileName)
         with _ ->
             false
 
     member tcConfig.GenerateSignatureData =
         not tcConfig.standalone && not tcConfig.noSignatureData
 
-    member tcConfig.GenerateOptimizationData =
-        tcConfig.GenerateSignatureData
+    member tcConfig.GenerateOptimizationData = tcConfig.GenerateSignatureData
 
     member tcConfig.assumeDotNetFramework =
         tcConfig.primaryAssembly = PrimaryAssembly.Mscorlib
@@ -1261,13 +1429,16 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
 /// but for F# Interactive it may be based on an underlying mutable TcConfigBuilder.
 type TcConfigProvider =
     | TcConfigProvider of (CompilationThreadToken -> TcConfig)
-    member x.Get ctok = (let (TcConfigProvider f) = x in f ctok)
+
+    member x.Get ctok =
+        (let (TcConfigProvider f) = x in f ctok)
 
     /// Get a TcConfigProvider which will return only the exact TcConfig.
     static member Constant tcConfig = TcConfigProvider(fun _ctok -> tcConfig)
 
     /// Get a TcConfigProvider which will continue to respect changes in the underlying
     /// TcConfigBuilder rather than delivering snapshots.
-    static member BasedOnMutableBuilder tcConfigB = TcConfigProvider(fun _ctok -> TcConfig.Create(tcConfigB, validate=false))
+    static member BasedOnMutableBuilder tcConfigB =
+        TcConfigProvider(fun _ctok -> TcConfig.Create(tcConfigB, validate = false))
 
 let GetFSharpCoreLibraryName () = getFSharpCoreLibraryName
diff --git a/src/Compiler/Driver/CompilerConfig.fsi b/src/Compiler/Driver/CompilerConfig.fsi
index bb8e859a010..202aa59b44e 100644
--- a/src/Compiler/Driver/CompilerConfig.fsi
+++ b/src/Compiler/Driver/CompilerConfig.fsi
@@ -99,7 +99,7 @@ and IProjectReference =
     abstract TryGetLogicalTimeStamp: cache: TimeStampCache -> DateTime option
 
 type AssemblyReference =
-    | AssemblyReference of range * string * IProjectReference option
+    | AssemblyReference of range: range * text: string * projectReference: IProjectReference option
 
     member Range: range
 
@@ -854,6 +854,4 @@ val FSharpScriptFileSuffixes: string list
 /// File suffixes where #light is the default
 val FSharpIndentationAwareSyntaxFileSuffixes: string list
 
-val doNotRequireNamespaceOrModuleSuffixes: string list
-
-val mlCompatSuffixes: string list
+val FSharpMLCompatFileSuffixes: string list
diff --git a/src/Compiler/Driver/CompilerDiagnostics.fs b/src/Compiler/Driver/CompilerDiagnostics.fs
index bc3e1b5c9ce..a26d232746f 100644
--- a/src/Compiler/Driver/CompilerDiagnostics.fs
+++ b/src/Compiler/Driver/CompilerDiagnostics.fs
@@ -44,7 +44,7 @@ open FSharp.Compiler.TypedTreeOps
 []
 module internal CompilerService =
     let showAssertForUnexpectedException = ref true
-#endif // DEBUG
+#endif
 
 /// This exception is an old-style way of reporting a diagnostic
 exception HashIncludeNotAllowedInNonScript of range
@@ -76,279 +76,273 @@ exception DeprecatedCommandLineOptionNoDescription of string * range
 /// This exception is an old-style way of reporting a diagnostic
 exception InternalCommandLineOption of string * range
 
-let GetRangeOfDiagnostic(diagnostic: PhasedDiagnostic) =
-  let rec RangeFromException exn =
-      match exn with
-      | ErrorFromAddingConstraint(_, exn2, _) -> RangeFromException exn2
+let GetRangeOfDiagnostic (diagnostic: PhasedDiagnostic) =
+    let rec RangeFromException exn =
+        match exn with
+        | ErrorFromAddingConstraint (_, exn2, _) -> RangeFromException exn2
 #if !NO_TYPEPROVIDERS
-      | TypeProviders.ProvidedTypeResolutionNoRange exn -> RangeFromException exn
-      | TypeProviders.ProvidedTypeResolution(m, _)
+        | TypeProviders.ProvidedTypeResolutionNoRange exn -> RangeFromException exn
+        | TypeProviders.ProvidedTypeResolution (m, _)
 #endif
-      | ReservedKeyword(_, m)
-      | IndentationProblem(_, m)
-      | ErrorFromAddingTypeEquation(_, _, _, _, _, m)
-      | ErrorFromApplyingDefault(_, _, _, _, _, m)
-      | ErrorsFromAddingSubsumptionConstraint(_, _, _, _, _, _, m)
-      | FunctionExpected(_, _, m)
-      | BakedInMemberConstraintName(_, m)
-      | StandardOperatorRedefinitionWarning(_, m)
-      | BadEventTransformation m
-      | ParameterlessStructCtor m
-      | FieldNotMutable (_, _, m)
-      | Recursion (_, _, _, _, m)
-      | InvalidRuntimeCoercion(_, _, _, m)
-      | IndeterminateRuntimeCoercion(_, _, _, m)
-      | IndeterminateStaticCoercion (_, _, _, m)
-      | StaticCoercionShouldUseBox (_, _, _, m)
-      | CoercionTargetSealed(_, _, m)
-      | UpcastUnnecessary m
-      | QuotationTranslator.IgnoringPartOfQuotedTermWarning (_, m)
-
-      | TypeTestUnnecessary m
-      | RuntimeCoercionSourceSealed(_, _, m)
-      | OverrideDoesntOverride(_, _, _, _, _, m)
-      | UnionPatternsBindDifferentNames m
-      | UnionCaseWrongArguments (_, _, _, m)
-      | TypeIsImplicitlyAbstract m
-      | RequiredButNotSpecified (_, _, _, _, m)
-      | FunctionValueUnexpected (_, _, m)
-      | UnitTypeExpected (_, _, m)
-      | UnitTypeExpectedWithEquality (_, _, m)
-      | UnitTypeExpectedWithPossiblePropertySetter (_, _, _, _, m)
-      | UnitTypeExpectedWithPossibleAssignment (_, _, _, _, m)
-      | UseOfAddressOfOperator m
-      | DeprecatedThreadStaticBindingWarning m
-      | NonUniqueInferredAbstractSlot (_, _, _, _, _, m)
-      | DefensiveCopyWarning (_, m)
-      | LetRecCheckedAtRuntime m
-      | UpperCaseIdentifierInPattern m
-      | NotUpperCaseConstructor m
-      | RecursiveUseCheckedAtRuntime (_, _, m)
-      | LetRecEvaluatedOutOfOrder (_, _, _, m)
-      | DiagnosticWithText (_, _, m)
-      | DiagnosticWithSuggestions (_, _, m, _, _)
-      | SyntaxError (_, m)
-      | InternalError (_, m)
-      | InterfaceNotRevealed(_, _, m)
-      | WrappedError (_, m)
-      | PatternMatchCompilation.MatchIncomplete (_, _, m)
-      | PatternMatchCompilation.EnumMatchIncomplete (_, _, m)
-      | PatternMatchCompilation.RuleNeverMatched m
-      | ValNotMutable(_, _, m)
-      | ValNotLocal(_, _, m)
-      | MissingFields(_, m)
-      | OverrideInIntrinsicAugmentation m
-      | IntfImplInIntrinsicAugmentation m
-      | OverrideInExtrinsicAugmentation m
-      | IntfImplInExtrinsicAugmentation m
-      | ValueRestriction(_, _, _, _, _, m)
-      | LetRecUnsound (_, _, m)
-      | ObsoleteError (_, m)
-      | ObsoleteWarning (_, m)
-      | Experimental (_, m)
-      | PossibleUnverifiableCode m
-      | UserCompilerMessage (_, _, m)
-      | Deprecated(_, m)
-      | LibraryUseOnly m
-      | FieldsFromDifferentTypes (_, _, _, m)
-      | IndeterminateType m
-      | TyconBadArgs(_, _, _, m) ->
-          Some m
-
-      | FieldNotContained(_, _, _, arf, _, _) -> Some arf.Range
-      | ValueNotContained(_, _, _, aval, _, _) -> Some aval.Range
-      | UnionCaseNotContained(_, _, _, aval, _, _) -> Some aval.Id.idRange
-      | FSharpExceptionNotContained(_, _, aexnc, _, _) -> Some aexnc.Range
-
-      | VarBoundTwice id
-      | UndefinedName(_, _, id, _) ->
-          Some id.idRange
-
-      | Duplicate(_, _, m)
-      | NameClash(_, _, _, m, _, _, _)
-      | UnresolvedOverloading(_, _, _, m)
-      | UnresolvedConversionOperator (_, _, _, m)
-      | VirtualAugmentationOnNullValuedType m
-      | NonVirtualAugmentationOnNullValuedType m
-      | NonRigidTypar(_, _, _, _, _, m)
-      | ConstraintSolverTupleDiffLengths(_, _, _, m, _)
-      | ConstraintSolverInfiniteTypes(_, _, _, _, m, _)
-      | ConstraintSolverMissingConstraint(_, _, _, m, _)
-      | ConstraintSolverTypesNotInEqualityRelation(_, _, _, m, _, _)
-      | ConstraintSolverError(_, m, _)
-      | ConstraintSolverTypesNotInSubsumptionRelation(_, _, _, m, _)
-      | SelfRefObjCtor(_, m) ->
-          Some m
-
-      | NotAFunction(_, _, mfun, _) ->
-          Some mfun
-
-      | NotAFunctionButIndexer(_, _, _, mfun, _, _) ->
-          Some mfun
-
-      | IllegalFileNameChar _ -> Some rangeCmdArgs
-
-      | UnresolvedReferenceError(_, m)
-      | UnresolvedPathReference(_, _, m)
-      | DeprecatedCommandLineOptionFull(_, m)
-      | DeprecatedCommandLineOptionForHtmlDoc(_, m)
-      | DeprecatedCommandLineOptionSuggestAlternative(_, _, m)
-      | DeprecatedCommandLineOptionNoDescription(_, m)
-      | InternalCommandLineOption(_, m)
-      | HashIncludeNotAllowedInNonScript m
-      | HashReferenceNotAllowedInNonScript m
-      | HashDirectiveNotAllowedInNonScript m
-      | FileNameNotResolved(_, _, m)
-      | LoadedSourceNotFoundIgnoring(_, m)
-      | MSBuildReferenceResolutionWarning(_, _, m)
-      | MSBuildReferenceResolutionError(_, _, m)
-      | AssemblyNotResolved(_, m)
-      | HashLoadedSourceHasIssues(_, _, _, m)
-      | HashLoadedScriptConsideredSource m ->
-          Some m
-      // Strip TargetInvocationException wrappers
-      | :? System.Reflection.TargetInvocationException as e ->
-          RangeFromException e.InnerException
+        | ReservedKeyword (_, m)
+        | IndentationProblem (_, m)
+        | ErrorFromAddingTypeEquation (_, _, _, _, _, m)
+        | ErrorFromApplyingDefault (_, _, _, _, _, m)
+        | ErrorsFromAddingSubsumptionConstraint (_, _, _, _, _, _, m)
+        | FunctionExpected (_, _, m)
+        | BakedInMemberConstraintName (_, m)
+        | StandardOperatorRedefinitionWarning (_, m)
+        | BadEventTransformation m
+        | ParameterlessStructCtor m
+        | FieldNotMutable (_, _, m)
+        | Recursion (_, _, _, _, m)
+        | InvalidRuntimeCoercion (_, _, _, m)
+        | IndeterminateRuntimeCoercion (_, _, _, m)
+        | IndeterminateStaticCoercion (_, _, _, m)
+        | StaticCoercionShouldUseBox (_, _, _, m)
+        | CoercionTargetSealed (_, _, m)
+        | UpcastUnnecessary m
+        | QuotationTranslator.IgnoringPartOfQuotedTermWarning (_, m)
+
+        | TypeTestUnnecessary m
+        | RuntimeCoercionSourceSealed (_, _, m)
+        | OverrideDoesntOverride (_, _, _, _, _, m)
+        | UnionPatternsBindDifferentNames m
+        | UnionCaseWrongArguments (_, _, _, m)
+        | TypeIsImplicitlyAbstract m
+        | RequiredButNotSpecified (_, _, _, _, m)
+        | FunctionValueUnexpected (_, _, m)
+        | UnitTypeExpected (_, _, m)
+        | UnitTypeExpectedWithEquality (_, _, m)
+        | UnitTypeExpectedWithPossiblePropertySetter (_, _, _, _, m)
+        | UnitTypeExpectedWithPossibleAssignment (_, _, _, _, m)
+        | UseOfAddressOfOperator m
+        | DeprecatedThreadStaticBindingWarning m
+        | NonUniqueInferredAbstractSlot (_, _, _, _, _, m)
+        | DefensiveCopyWarning (_, m)
+        | LetRecCheckedAtRuntime m
+        | UpperCaseIdentifierInPattern m
+        | NotUpperCaseConstructor m
+        | RecursiveUseCheckedAtRuntime (_, _, m)
+        | LetRecEvaluatedOutOfOrder (_, _, _, m)
+        | DiagnosticWithText (_, _, m)
+        | DiagnosticWithSuggestions (_, _, m, _, _)
+        | SyntaxError (_, m)
+        | InternalError (_, m)
+        | InterfaceNotRevealed (_, _, m)
+        | WrappedError (_, m)
+        | PatternMatchCompilation.MatchIncomplete (_, _, m)
+        | PatternMatchCompilation.EnumMatchIncomplete (_, _, m)
+        | PatternMatchCompilation.RuleNeverMatched m
+        | ValNotMutable (_, _, m)
+        | ValNotLocal (_, _, m)
+        | MissingFields (_, m)
+        | OverrideInIntrinsicAugmentation m
+        | IntfImplInIntrinsicAugmentation m
+        | OverrideInExtrinsicAugmentation m
+        | IntfImplInExtrinsicAugmentation m
+        | ValueRestriction (_, _, _, _, _, m)
+        | LetRecUnsound (_, _, m)
+        | ObsoleteError (_, m)
+        | ObsoleteWarning (_, m)
+        | Experimental (_, m)
+        | PossibleUnverifiableCode m
+        | UserCompilerMessage (_, _, m)
+        | Deprecated (_, m)
+        | LibraryUseOnly m
+        | FieldsFromDifferentTypes (_, _, _, m)
+        | IndeterminateType m
+        | TyconBadArgs (_, _, _, m) -> Some m
+
+        | FieldNotContained (_, _, _, arf, _, _) -> Some arf.Range
+        | ValueNotContained (_, _, _, aval, _, _) -> Some aval.Range
+        | UnionCaseNotContained (_, _, _, aval, _, _) -> Some aval.Id.idRange
+        | FSharpExceptionNotContained (_, _, aexnc, _, _) -> Some aexnc.Range
+
+        | VarBoundTwice id
+        | UndefinedName (_, _, id, _) -> Some id.idRange
+
+        | Duplicate (_, _, m)
+        | NameClash (_, _, _, m, _, _, _)
+        | UnresolvedOverloading (_, _, _, m)
+        | UnresolvedConversionOperator (_, _, _, m)
+        | VirtualAugmentationOnNullValuedType m
+        | NonVirtualAugmentationOnNullValuedType m
+        | NonRigidTypar (_, _, _, _, _, m)
+        | ConstraintSolverTupleDiffLengths (_, _, _, m, _)
+        | ConstraintSolverInfiniteTypes (_, _, _, _, m, _)
+        | ConstraintSolverMissingConstraint (_, _, _, m, _)
+        | ConstraintSolverTypesNotInEqualityRelation (_, _, _, m, _, _)
+        | ConstraintSolverError (_, m, _)
+        | ConstraintSolverTypesNotInSubsumptionRelation (_, _, _, m, _)
+        | SelfRefObjCtor (_, m) -> Some m
+
+        | NotAFunction (_, _, mfun, _) -> Some mfun
+
+        | NotAFunctionButIndexer (_, _, _, mfun, _, _) -> Some mfun
+
+        | IllegalFileNameChar _ -> Some rangeCmdArgs
+
+        | UnresolvedReferenceError (_, m)
+        | UnresolvedPathReference (_, _, m)
+        | DeprecatedCommandLineOptionFull (_, m)
+        | DeprecatedCommandLineOptionForHtmlDoc (_, m)
+        | DeprecatedCommandLineOptionSuggestAlternative (_, _, m)
+        | DeprecatedCommandLineOptionNoDescription (_, m)
+        | InternalCommandLineOption (_, m)
+        | HashIncludeNotAllowedInNonScript m
+        | HashReferenceNotAllowedInNonScript m
+        | HashDirectiveNotAllowedInNonScript m
+        | FileNameNotResolved (_, _, m)
+        | LoadedSourceNotFoundIgnoring (_, m)
+        | MSBuildReferenceResolutionWarning (_, _, m)
+        | MSBuildReferenceResolutionError (_, _, m)
+        | AssemblyNotResolved (_, m)
+        | HashLoadedSourceHasIssues (_, _, _, m)
+        | HashLoadedScriptConsideredSource m -> Some m
+        // Strip TargetInvocationException wrappers
+        | :? System.Reflection.TargetInvocationException as e -> RangeFromException e.InnerException
 #if !NO_TYPEPROVIDERS
-      | :? TypeProviderError as e -> e.Range |> Some
+        | :? TypeProviderError as e -> e.Range |> Some
 #endif
 
-      | _ -> None
-
-  RangeFromException diagnostic.Exception
-
-let GetDiagnosticNumber(diagnostic: PhasedDiagnostic) =
-    let rec GetFromException(exn: exn) =
-      match exn with
-      // DO NOT CHANGE THESE NUMBERS
-      | ErrorFromAddingTypeEquation _ -> 1
-      | FunctionExpected _ -> 2
-      | NotAFunctionButIndexer _ -> 3217
-      | NotAFunction _ -> 3
-      | FieldNotMutable _ -> 5
-      | Recursion _ -> 6
-      | InvalidRuntimeCoercion _ -> 7
-      | IndeterminateRuntimeCoercion _ -> 8
-      | PossibleUnverifiableCode _ -> 9
-      | SyntaxError _ -> 10
-      // 11 cannot be reused
-      // 12 cannot be reused
-      | IndeterminateStaticCoercion _ -> 13
-      | StaticCoercionShouldUseBox _ -> 14
-      // 15 cannot be reused
-      | RuntimeCoercionSourceSealed _ -> 16
-      | OverrideDoesntOverride _ -> 17
-      | UnionPatternsBindDifferentNames _ -> 18
-      | UnionCaseWrongArguments _ -> 19
-      | UnitTypeExpected _ -> 20
-      | UnitTypeExpectedWithEquality _ -> 20
-      | UnitTypeExpectedWithPossiblePropertySetter _ -> 20
-      | UnitTypeExpectedWithPossibleAssignment _ -> 20
-      | RecursiveUseCheckedAtRuntime _ -> 21
-      | LetRecEvaluatedOutOfOrder _ -> 22
-      | NameClash _ -> 23
-      // 24 cannot be reused
-      | PatternMatchCompilation.MatchIncomplete _ -> 25
-      | PatternMatchCompilation.RuleNeverMatched _ -> 26
-      | ValNotMutable _ -> 27
-      | ValNotLocal _ -> 28
-      | MissingFields _ -> 29
-      | ValueRestriction _ -> 30
-      | LetRecUnsound _ -> 31
-      | FieldsFromDifferentTypes _ -> 32
-      | TyconBadArgs _ -> 33
-      | ValueNotContained _ -> 34
-      | Deprecated _ -> 35
-      | UnionCaseNotContained _ -> 36
-      | Duplicate _ -> 37
-      | VarBoundTwice _ -> 38
-      | UndefinedName _ -> 39
-      | LetRecCheckedAtRuntime _ -> 40
-      | UnresolvedOverloading _ -> 41
-      | LibraryUseOnly _ -> 42
-      | ErrorFromAddingConstraint _ -> 43
-      | ObsoleteWarning _ -> 44
-      | ReservedKeyword _ -> 46
-      | SelfRefObjCtor _ -> 47
-      | VirtualAugmentationOnNullValuedType _ -> 48
-      | UpperCaseIdentifierInPattern _ -> 49
-      | InterfaceNotRevealed _ -> 50
-      | UseOfAddressOfOperator _ -> 51
-      | DefensiveCopyWarning _ -> 52
-      | NotUpperCaseConstructor _ -> 53
-      | TypeIsImplicitlyAbstract _ -> 54
-      // 55 cannot be reused
-      | DeprecatedThreadStaticBindingWarning _ -> 56
-      | Experimental _ -> 57
-      | IndentationProblem _ -> 58
-      | CoercionTargetSealed _ -> 59
-      | OverrideInIntrinsicAugmentation _ -> 60
-      | NonVirtualAugmentationOnNullValuedType _ -> 61
-      | UserCompilerMessage (_, n, _) -> n
-      | FSharpExceptionNotContained _ -> 63
-      | NonRigidTypar _ -> 64
-      // 65 cannot be reused
-      | UpcastUnnecessary _ -> 66
-      | TypeTestUnnecessary _ -> 67
-      | QuotationTranslator.IgnoringPartOfQuotedTermWarning _ -> 68
-      | IntfImplInIntrinsicAugmentation _ -> 69
-      | NonUniqueInferredAbstractSlot _ -> 70
-      | ErrorFromApplyingDefault _ -> 71
-      | IndeterminateType _ -> 72
-      | InternalError _ -> 73
-      | UnresolvedReferenceNoRange _
-      | UnresolvedReferenceError _
-      | UnresolvedPathReferenceNoRange _
-      | UnresolvedPathReference _ -> 74
-      | DeprecatedCommandLineOptionFull _
-      | DeprecatedCommandLineOptionForHtmlDoc _
-      | DeprecatedCommandLineOptionSuggestAlternative _
-      | DeprecatedCommandLineOptionNoDescription _
-      | InternalCommandLineOption _ -> 75
-      | HashIncludeNotAllowedInNonScript _
-      | HashReferenceNotAllowedInNonScript _
-      | HashDirectiveNotAllowedInNonScript _ -> 76
-      | BakedInMemberConstraintName _ -> 77
-      | FileNameNotResolved _ -> 78
-      | LoadedSourceNotFoundIgnoring _ -> 79
-      // 80 cannot be reused
-      | ParameterlessStructCtor _ -> 81
-      | MSBuildReferenceResolutionWarning _ -> 82
-      | MSBuildReferenceResolutionError _ -> 83
-      | AssemblyNotResolved _ -> 84
-      | HashLoadedSourceHasIssues _ -> 85
-      | StandardOperatorRedefinitionWarning _ -> 86
-      | InvalidInternalsVisibleToAssemblyName _ -> 87
-      // 88 cannot be reused
-      | OverrideInExtrinsicAugmentation _ -> 89
-      | IntfImplInExtrinsicAugmentation _ -> 90
-      | BadEventTransformation _ -> 91
-      | HashLoadedScriptConsideredSource _ -> 92
-      | UnresolvedConversionOperator _ -> 93
-      // avoid 94-100 for safety
-      | ObsoleteError _ -> 101
+        | _ -> None
+
+    RangeFromException diagnostic.Exception
+
+let GetDiagnosticNumber (diagnostic: PhasedDiagnostic) =
+    let rec GetFromException (exn: exn) =
+        match exn with
+        // DO NOT CHANGE THESE NUMBERS
+        | ErrorFromAddingTypeEquation _ -> 1
+        | FunctionExpected _ -> 2
+        | NotAFunctionButIndexer _ -> 3217
+        | NotAFunction _ -> 3
+        | FieldNotMutable _ -> 5
+        | Recursion _ -> 6
+        | InvalidRuntimeCoercion _ -> 7
+        | IndeterminateRuntimeCoercion _ -> 8
+        | PossibleUnverifiableCode _ -> 9
+        | SyntaxError _ -> 10
+        // 11 cannot be reused
+        // 12 cannot be reused
+        | IndeterminateStaticCoercion _ -> 13
+        | StaticCoercionShouldUseBox _ -> 14
+        // 15 cannot be reused
+        | RuntimeCoercionSourceSealed _ -> 16
+        | OverrideDoesntOverride _ -> 17
+        | UnionPatternsBindDifferentNames _ -> 18
+        | UnionCaseWrongArguments _ -> 19
+        | UnitTypeExpected _ -> 20
+        | UnitTypeExpectedWithEquality _ -> 20
+        | UnitTypeExpectedWithPossiblePropertySetter _ -> 20
+        | UnitTypeExpectedWithPossibleAssignment _ -> 20
+        | RecursiveUseCheckedAtRuntime _ -> 21
+        | LetRecEvaluatedOutOfOrder _ -> 22
+        | NameClash _ -> 23
+        // 24 cannot be reused
+        | PatternMatchCompilation.MatchIncomplete _ -> 25
+        | PatternMatchCompilation.RuleNeverMatched _ -> 26
+        | ValNotMutable _ -> 27
+        | ValNotLocal _ -> 28
+        | MissingFields _ -> 29
+        | ValueRestriction _ -> 30
+        | LetRecUnsound _ -> 31
+        | FieldsFromDifferentTypes _ -> 32
+        | TyconBadArgs _ -> 33
+        | ValueNotContained _ -> 34
+        | Deprecated _ -> 35
+        | UnionCaseNotContained _ -> 36
+        | Duplicate _ -> 37
+        | VarBoundTwice _ -> 38
+        | UndefinedName _ -> 39
+        | LetRecCheckedAtRuntime _ -> 40
+        | UnresolvedOverloading _ -> 41
+        | LibraryUseOnly _ -> 42
+        | ErrorFromAddingConstraint _ -> 43
+        | ObsoleteWarning _ -> 44
+        | ReservedKeyword _ -> 46
+        | SelfRefObjCtor _ -> 47
+        | VirtualAugmentationOnNullValuedType _ -> 48
+        | UpperCaseIdentifierInPattern _ -> 49
+        | InterfaceNotRevealed _ -> 50
+        | UseOfAddressOfOperator _ -> 51
+        | DefensiveCopyWarning _ -> 52
+        | NotUpperCaseConstructor _ -> 53
+        | TypeIsImplicitlyAbstract _ -> 54
+        // 55 cannot be reused
+        | DeprecatedThreadStaticBindingWarning _ -> 56
+        | Experimental _ -> 57
+        | IndentationProblem _ -> 58
+        | CoercionTargetSealed _ -> 59
+        | OverrideInIntrinsicAugmentation _ -> 60
+        | NonVirtualAugmentationOnNullValuedType _ -> 61
+        | UserCompilerMessage (_, n, _) -> n
+        | FSharpExceptionNotContained _ -> 63
+        | NonRigidTypar _ -> 64
+        // 65 cannot be reused
+        | UpcastUnnecessary _ -> 66
+        | TypeTestUnnecessary _ -> 67
+        | QuotationTranslator.IgnoringPartOfQuotedTermWarning _ -> 68
+        | IntfImplInIntrinsicAugmentation _ -> 69
+        | NonUniqueInferredAbstractSlot _ -> 70
+        | ErrorFromApplyingDefault _ -> 71
+        | IndeterminateType _ -> 72
+        | InternalError _ -> 73
+        | UnresolvedReferenceNoRange _
+        | UnresolvedReferenceError _
+        | UnresolvedPathReferenceNoRange _
+        | UnresolvedPathReference _ -> 74
+        | DeprecatedCommandLineOptionFull _
+        | DeprecatedCommandLineOptionForHtmlDoc _
+        | DeprecatedCommandLineOptionSuggestAlternative _
+        | DeprecatedCommandLineOptionNoDescription _
+        | InternalCommandLineOption _ -> 75
+        | HashIncludeNotAllowedInNonScript _
+        | HashReferenceNotAllowedInNonScript _
+        | HashDirectiveNotAllowedInNonScript _ -> 76
+        | BakedInMemberConstraintName _ -> 77
+        | FileNameNotResolved _ -> 78
+        | LoadedSourceNotFoundIgnoring _ -> 79
+        // 80 cannot be reused
+        | ParameterlessStructCtor _ -> 81
+        | MSBuildReferenceResolutionWarning _ -> 82
+        | MSBuildReferenceResolutionError _ -> 83
+        | AssemblyNotResolved _ -> 84
+        | HashLoadedSourceHasIssues _ -> 85
+        | StandardOperatorRedefinitionWarning _ -> 86
+        | InvalidInternalsVisibleToAssemblyName _ -> 87
+        // 88 cannot be reused
+        | OverrideInExtrinsicAugmentation _ -> 89
+        | IntfImplInExtrinsicAugmentation _ -> 90
+        | BadEventTransformation _ -> 91
+        | HashLoadedScriptConsideredSource _ -> 92
+        | UnresolvedConversionOperator _ -> 93
+        // avoid 94-100 for safety
+        | ObsoleteError _ -> 101
 #if !NO_TYPEPROVIDERS
-      | TypeProviders.ProvidedTypeResolutionNoRange _
-      | TypeProviders.ProvidedTypeResolution _ -> 103
+        | TypeProviders.ProvidedTypeResolutionNoRange _
+        | TypeProviders.ProvidedTypeResolution _ -> 103
 #endif
-      | PatternMatchCompilation.EnumMatchIncomplete _ -> 104
-       // DO NOT CHANGE THE NUMBERS
+        | PatternMatchCompilation.EnumMatchIncomplete _ -> 104
+        // DO NOT CHANGE THE NUMBERS
 
-      // Strip TargetInvocationException wrappers
-      | :? System.Reflection.TargetInvocationException as e ->
-          GetFromException e.InnerException
+        // Strip TargetInvocationException wrappers
+        | :? System.Reflection.TargetInvocationException as e -> GetFromException e.InnerException
 
-      | WrappedError(e, _) -> GetFromException e
+        | WrappedError (e, _) -> GetFromException e
 
-      | DiagnosticWithText (n, _, _) -> n
-      | DiagnosticWithSuggestions (n, _, _, _, _) -> n
-      | Failure _ -> 192
-      | IllegalFileNameChar(fileName, invalidChar) -> fst (FSComp.SR.buildUnexpectedFileNameCharacter(fileName, string invalidChar))
+        | DiagnosticWithText (n, _, _) -> n
+        | DiagnosticWithSuggestions (n, _, _, _, _) -> n
+        | Failure _ -> 192
+        | IllegalFileNameChar (fileName, invalidChar) -> fst (FSComp.SR.buildUnexpectedFileNameCharacter (fileName, string invalidChar))
 #if !NO_TYPEPROVIDERS
-      | :? TypeProviderError as e -> e.Number
+        | :? TypeProviderError as e -> e.Number
 #endif
-      | ErrorsFromAddingSubsumptionConstraint (_, _, _, _, _, ContextInfo.DowncastUsedInsteadOfUpcast _, _) -> fst (FSComp.SR.considerUpcast("", ""))
-      | _ -> 193
+        | ErrorsFromAddingSubsumptionConstraint (_, _, _, _, _, ContextInfo.DowncastUsedInsteadOfUpcast _, _) ->
+            fst (FSComp.SR.considerUpcast ("", ""))
+        | _ -> 193
+
     GetFromException diagnostic.Exception
 
 let GetWarningLevel diagnostic =
@@ -356,10 +350,10 @@ let GetWarningLevel diagnostic =
     // Level 5 warnings
     | RecursiveUseCheckedAtRuntime _
     | LetRecEvaluatedOutOfOrder _
-    | DefensiveCopyWarning _  -> 5
+    | DefensiveCopyWarning _ -> 5
 
-    | DiagnosticWithText(n, _, _)
-    | DiagnosticWithSuggestions(n, _, _, _, _) ->
+    | DiagnosticWithText (n, _, _)
+    | DiagnosticWithSuggestions (n, _, _, _, _) ->
         // 1178, tcNoComparisonNeeded1, "The struct, record or union type '%s' is not structurally comparable because the type parameter %s does not satisfy the 'comparison' constraint..."
         // 1178, tcNoComparisonNeeded2, "The struct, record or union type '%s' is not structurally comparable because the type '%s' does not satisfy the 'comparison' constraint...."
         // 1178, tcNoEqualityNeeded1, "The struct, record or union type '%s' does not support structural equality because the type parameter %s does not satisfy the 'equality' constraint..."
@@ -369,7 +363,8 @@ let GetWarningLevel diagnostic =
     | _ -> 2
 
 let IsWarningOrInfoEnabled (diagnostic, severity) n level specificWarnOn =
-    List.contains n specificWarnOn ||
+    List.contains n specificWarnOn
+    ||
     // Some specific warnings/informational are never on by default, i.e. unused variable warnings
     match n with
     | 1182 -> false // chkUnusedValue - off by default
@@ -381,198 +376,208 @@ let IsWarningOrInfoEnabled (diagnostic, severity) n level specificWarnOn =
     | 3389 -> false // tcBuiltInImplicitConversionUsed - off by default
     | 3390 -> false // xmlDocBadlyFormed - off by default
     | 3395 -> false // tcImplicitConversionUsedForMethodArg - off by default
-    | _ -> 
-        (severity = FSharpDiagnosticSeverity.Info) ||
-        (severity = FSharpDiagnosticSeverity.Warning && level >= GetWarningLevel diagnostic)
+    | _ ->
+        (severity = FSharpDiagnosticSeverity.Info)
+        || (severity = FSharpDiagnosticSeverity.Warning
+            && level >= GetWarningLevel diagnostic)
+
+let SplitRelatedDiagnostics (diagnostic: PhasedDiagnostic) : PhasedDiagnostic * PhasedDiagnostic list =
+    let ToPhased exn =
+        {
+            Exception = exn
+            Phase = diagnostic.Phase
+        }
 
-let SplitRelatedDiagnostics(diagnostic: PhasedDiagnostic) : PhasedDiagnostic * PhasedDiagnostic list =
-    let ToPhased exn = {Exception=exn; Phase = diagnostic.Phase}
     let rec SplitRelatedException exn =
-      match exn with
-      | ErrorFromAddingTypeEquation(g, denv, ty1, ty2, exn2, m) ->
-          let diag2, related = SplitRelatedException exn2
-          ErrorFromAddingTypeEquation(g, denv, ty1, ty2, diag2.Exception, m) |> ToPhased, related
-      | ErrorFromApplyingDefault(g, denv, tp, defaultType, exn2, m) ->
-          let diag2, related = SplitRelatedException exn2
-          ErrorFromApplyingDefault(g, denv, tp, defaultType, diag2.Exception, m) |> ToPhased, related
-      | ErrorsFromAddingSubsumptionConstraint(g, denv, ty1, ty2, exn2, contextInfo, m) ->
-          let diag2, related = SplitRelatedException exn2
-          ErrorsFromAddingSubsumptionConstraint(g, denv, ty1, ty2, diag2.Exception, contextInfo, m) |> ToPhased, related
-      | ErrorFromAddingConstraint(x, exn2, m) ->
-          let diag2, related = SplitRelatedException exn2
-          ErrorFromAddingConstraint(x, diag2.Exception, m) |> ToPhased, related
-      | WrappedError (exn2, m) ->
-          let diag2, related = SplitRelatedException exn2
-          WrappedError(diag2.Exception, m) |> ToPhased, related
-      // Strip TargetInvocationException wrappers
-      | :? TargetInvocationException as exn ->
-          SplitRelatedException exn.InnerException
-      | _ ->
-           ToPhased exn, []
-    SplitRelatedException diagnostic.Exception
+        match exn with
+        | ErrorFromAddingTypeEquation (g, denv, ty1, ty2, exn2, m) ->
+            let diag2, related = SplitRelatedException exn2
+            ErrorFromAddingTypeEquation(g, denv, ty1, ty2, diag2.Exception, m) |> ToPhased, related
+        | ErrorFromApplyingDefault (g, denv, tp, defaultType, exn2, m) ->
+            let diag2, related = SplitRelatedException exn2
+
+            ErrorFromApplyingDefault(g, denv, tp, defaultType, diag2.Exception, m)
+            |> ToPhased,
+            related
+        | ErrorsFromAddingSubsumptionConstraint (g, denv, ty1, ty2, exn2, contextInfo, m) ->
+            let diag2, related = SplitRelatedException exn2
+
+            ErrorsFromAddingSubsumptionConstraint(g, denv, ty1, ty2, diag2.Exception, contextInfo, m)
+            |> ToPhased,
+            related
+        | ErrorFromAddingConstraint (x, exn2, m) ->
+            let diag2, related = SplitRelatedException exn2
+            ErrorFromAddingConstraint(x, diag2.Exception, m) |> ToPhased, related
+        | WrappedError (exn2, m) ->
+            let diag2, related = SplitRelatedException exn2
+            WrappedError(diag2.Exception, m) |> ToPhased, related
+        // Strip TargetInvocationException wrappers
+        | :? TargetInvocationException as exn -> SplitRelatedException exn.InnerException
+        | _ -> ToPhased exn, []
 
+    SplitRelatedException diagnostic.Exception
 
-let DeclareMessage = DeclareResourceString
+let Message (name, format) = DeclareResourceString(name, format)
 
 do FSComp.SR.RunStartupValidation()
-let SeeAlsoE() = DeclareResourceString("SeeAlso", "%s")
-let ConstraintSolverTupleDiffLengthsE() = DeclareResourceString("ConstraintSolverTupleDiffLengths", "%d%d")
-let ConstraintSolverInfiniteTypesE() = DeclareResourceString("ConstraintSolverInfiniteTypes", "%s%s")
-let ConstraintSolverMissingConstraintE() = DeclareResourceString("ConstraintSolverMissingConstraint", "%s")
-let ConstraintSolverTypesNotInEqualityRelation1E() = DeclareResourceString("ConstraintSolverTypesNotInEqualityRelation1", "%s%s")
-let ConstraintSolverTypesNotInEqualityRelation2E() = DeclareResourceString("ConstraintSolverTypesNotInEqualityRelation2", "%s%s")
-let ConstraintSolverTypesNotInSubsumptionRelationE() = DeclareResourceString("ConstraintSolverTypesNotInSubsumptionRelation", "%s%s%s")
-let ErrorFromAddingTypeEquation1E() = DeclareResourceString("ErrorFromAddingTypeEquation1", "%s%s%s")
-let ErrorFromAddingTypeEquation2E() = DeclareResourceString("ErrorFromAddingTypeEquation2", "%s%s%s")
-let ErrorFromApplyingDefault1E() = DeclareResourceString("ErrorFromApplyingDefault1", "%s")
-let ErrorFromApplyingDefault2E() = DeclareResourceString("ErrorFromApplyingDefault2", "")
-let ErrorsFromAddingSubsumptionConstraintE() = DeclareResourceString("ErrorsFromAddingSubsumptionConstraint", "%s%s%s")
-let UpperCaseIdentifierInPatternE() = DeclareResourceString("UpperCaseIdentifierInPattern", "")
-let NotUpperCaseConstructorE() = DeclareResourceString("NotUpperCaseConstructor", "")
-let FunctionExpectedE() = DeclareResourceString("FunctionExpected", "")
-let BakedInMemberConstraintNameE() = DeclareResourceString("BakedInMemberConstraintName", "%s")
-let BadEventTransformationE() = DeclareResourceString("BadEventTransformation", "")
-let ParameterlessStructCtorE() = DeclareResourceString("ParameterlessStructCtor", "")
-let InterfaceNotRevealedE() = DeclareResourceString("InterfaceNotRevealed", "%s")
-let TyconBadArgsE() = DeclareResourceString("TyconBadArgs", "%s%d%d")
-let IndeterminateTypeE() = DeclareResourceString("IndeterminateType", "")
-let NameClash1E() = DeclareResourceString("NameClash1", "%s%s")
-let NameClash2E() = DeclareResourceString("NameClash2", "%s%s%s%s%s")
-let Duplicate1E() = DeclareResourceString("Duplicate1", "%s")
-let Duplicate2E() = DeclareResourceString("Duplicate2", "%s%s")
-let UndefinedName2E() = DeclareResourceString("UndefinedName2", "")
-let FieldNotMutableE() = DeclareResourceString("FieldNotMutable", "")
-let FieldsFromDifferentTypesE() = DeclareResourceString("FieldsFromDifferentTypes", "%s%s")
-let VarBoundTwiceE() = DeclareResourceString("VarBoundTwice", "%s")
-let RecursionE() = DeclareResourceString("Recursion", "%s%s%s%s")
-let InvalidRuntimeCoercionE() = DeclareResourceString("InvalidRuntimeCoercion", "%s%s%s")
-let IndeterminateRuntimeCoercionE() = DeclareResourceString("IndeterminateRuntimeCoercion", "%s%s")
-let IndeterminateStaticCoercionE() = DeclareResourceString("IndeterminateStaticCoercion", "%s%s")
-let StaticCoercionShouldUseBoxE() = DeclareResourceString("StaticCoercionShouldUseBox", "%s%s")
-let TypeIsImplicitlyAbstractE() = DeclareResourceString("TypeIsImplicitlyAbstract", "")
-let NonRigidTypar1E() = DeclareResourceString("NonRigidTypar1", "%s%s")
-let NonRigidTypar2E() = DeclareResourceString("NonRigidTypar2", "%s%s")
-let NonRigidTypar3E() = DeclareResourceString("NonRigidTypar3", "%s%s")
-let OBlockEndSentenceE() = DeclareResourceString("BlockEndSentence", "")
-let UnexpectedEndOfInputE() = DeclareResourceString("UnexpectedEndOfInput", "")
-let UnexpectedE() = DeclareResourceString("Unexpected", "%s")
-let NONTERM_interactionE() = DeclareResourceString("NONTERM.interaction", "")
-let NONTERM_hashDirectiveE() = DeclareResourceString("NONTERM.hashDirective", "")
-let NONTERM_fieldDeclE() = DeclareResourceString("NONTERM.fieldDecl", "")
-let NONTERM_unionCaseReprE() = DeclareResourceString("NONTERM.unionCaseRepr", "")
-let NONTERM_localBindingE() = DeclareResourceString("NONTERM.localBinding", "")
-let NONTERM_hardwhiteLetBindingsE() = DeclareResourceString("NONTERM.hardwhiteLetBindings", "")
-let NONTERM_classDefnMemberE() = DeclareResourceString("NONTERM.classDefnMember", "")
-let NONTERM_defnBindingsE() = DeclareResourceString("NONTERM.defnBindings", "")
-let NONTERM_classMemberSpfnE() = DeclareResourceString("NONTERM.classMemberSpfn", "")
-let NONTERM_valSpfnE() = DeclareResourceString("NONTERM.valSpfn", "")
-let NONTERM_tyconSpfnE() = DeclareResourceString("NONTERM.tyconSpfn", "")
-let NONTERM_anonLambdaExprE() = DeclareResourceString("NONTERM.anonLambdaExpr", "")
-let NONTERM_attrUnionCaseDeclE() = DeclareResourceString("NONTERM.attrUnionCaseDecl", "")
-let NONTERM_cPrototypeE() = DeclareResourceString("NONTERM.cPrototype", "")
-let NONTERM_objectImplementationMembersE() = DeclareResourceString("NONTERM.objectImplementationMembers", "")
-let NONTERM_ifExprCasesE() = DeclareResourceString("NONTERM.ifExprCases", "")
-let NONTERM_openDeclE() = DeclareResourceString("NONTERM.openDecl", "")
-let NONTERM_fileModuleSpecE() = DeclareResourceString("NONTERM.fileModuleSpec", "")
-let NONTERM_patternClausesE() = DeclareResourceString("NONTERM.patternClauses", "")
-let NONTERM_beginEndExprE() = DeclareResourceString("NONTERM.beginEndExpr", "")
-let NONTERM_recdExprE() = DeclareResourceString("NONTERM.recdExpr", "")
-let NONTERM_tyconDefnE() = DeclareResourceString("NONTERM.tyconDefn", "")
-let NONTERM_exconCoreE() = DeclareResourceString("NONTERM.exconCore", "")
-let NONTERM_typeNameInfoE() = DeclareResourceString("NONTERM.typeNameInfo", "")
-let NONTERM_attributeListE() = DeclareResourceString("NONTERM.attributeList", "")
-let NONTERM_quoteExprE() = DeclareResourceString("NONTERM.quoteExpr", "")
-let NONTERM_typeConstraintE() = DeclareResourceString("NONTERM.typeConstraint", "")
-let NONTERM_Category_ImplementationFileE() = DeclareResourceString("NONTERM.Category.ImplementationFile", "")
-let NONTERM_Category_DefinitionE() = DeclareResourceString("NONTERM.Category.Definition", "")
-let NONTERM_Category_SignatureFileE() = DeclareResourceString("NONTERM.Category.SignatureFile", "")
-let NONTERM_Category_PatternE() = DeclareResourceString("NONTERM.Category.Pattern", "")
-let NONTERM_Category_ExprE() = DeclareResourceString("NONTERM.Category.Expr", "")
-let NONTERM_Category_TypeE() = DeclareResourceString("NONTERM.Category.Type", "")
-let NONTERM_typeArgsActualE() = DeclareResourceString("NONTERM.typeArgsActual", "")
-let TokenName1E() = DeclareResourceString("TokenName1", "%s")
-let TokenName1TokenName2E() = DeclareResourceString("TokenName1TokenName2", "%s%s")
-let TokenName1TokenName2TokenName3E() = DeclareResourceString("TokenName1TokenName2TokenName3", "%s%s%s")
-let RuntimeCoercionSourceSealed1E() = DeclareResourceString("RuntimeCoercionSourceSealed1", "%s")
-let RuntimeCoercionSourceSealed2E() = DeclareResourceString("RuntimeCoercionSourceSealed2", "%s")
-let CoercionTargetSealedE() = DeclareResourceString("CoercionTargetSealed", "%s")
-let UpcastUnnecessaryE() = DeclareResourceString("UpcastUnnecessary", "")
-let TypeTestUnnecessaryE() = DeclareResourceString("TypeTestUnnecessary", "")
-let OverrideDoesntOverride1E() = DeclareResourceString("OverrideDoesntOverride1", "%s")
-let OverrideDoesntOverride2E() = DeclareResourceString("OverrideDoesntOverride2", "%s")
-let OverrideDoesntOverride3E() = DeclareResourceString("OverrideDoesntOverride3", "%s")
-let OverrideDoesntOverride4E() = DeclareResourceString("OverrideDoesntOverride4", "%s")
-let UnionCaseWrongArgumentsE() = DeclareResourceString("UnionCaseWrongArguments", "%d%d")
-let UnionPatternsBindDifferentNamesE() = DeclareResourceString("UnionPatternsBindDifferentNames", "")
-let RequiredButNotSpecifiedE() = DeclareResourceString("RequiredButNotSpecified", "%s%s%s")
-let UseOfAddressOfOperatorE() = DeclareResourceString("UseOfAddressOfOperator", "")
-let DefensiveCopyWarningE() = DeclareResourceString("DefensiveCopyWarning", "%s")
-let DeprecatedThreadStaticBindingWarningE() = DeclareResourceString("DeprecatedThreadStaticBindingWarning", "")
-let FunctionValueUnexpectedE() = DeclareResourceString("FunctionValueUnexpected", "%s")
-let UnitTypeExpectedE() = DeclareResourceString("UnitTypeExpected", "%s")
-let UnitTypeExpectedWithEqualityE() = DeclareResourceString("UnitTypeExpectedWithEquality", "%s")
-let UnitTypeExpectedWithPossiblePropertySetterE() = DeclareResourceString("UnitTypeExpectedWithPossiblePropertySetter", "%s%s%s")
-let UnitTypeExpectedWithPossibleAssignmentE() = DeclareResourceString("UnitTypeExpectedWithPossibleAssignment", "%s%s")
-let UnitTypeExpectedWithPossibleAssignmentToMutableE() = DeclareResourceString("UnitTypeExpectedWithPossibleAssignmentToMutable", "%s%s")
-let RecursiveUseCheckedAtRuntimeE() = DeclareResourceString("RecursiveUseCheckedAtRuntime", "")
-let LetRecUnsound1E() = DeclareResourceString("LetRecUnsound1", "%s")
-let LetRecUnsound2E() = DeclareResourceString("LetRecUnsound2", "%s%s")
-let LetRecUnsoundInnerE() = DeclareResourceString("LetRecUnsoundInner", "%s")
-let LetRecEvaluatedOutOfOrderE() = DeclareResourceString("LetRecEvaluatedOutOfOrder", "")
-let LetRecCheckedAtRuntimeE() = DeclareResourceString("LetRecCheckedAtRuntime", "")
-let SelfRefObjCtor1E() = DeclareResourceString("SelfRefObjCtor1", "")
-let SelfRefObjCtor2E() = DeclareResourceString("SelfRefObjCtor2", "")
-let VirtualAugmentationOnNullValuedTypeE() = DeclareResourceString("VirtualAugmentationOnNullValuedType", "")
-let NonVirtualAugmentationOnNullValuedTypeE() = DeclareResourceString("NonVirtualAugmentationOnNullValuedType", "")
-let NonUniqueInferredAbstractSlot1E() = DeclareResourceString("NonUniqueInferredAbstractSlot1", "%s")
-let NonUniqueInferredAbstractSlot2E() = DeclareResourceString("NonUniqueInferredAbstractSlot2", "")
-let NonUniqueInferredAbstractSlot3E() = DeclareResourceString("NonUniqueInferredAbstractSlot3", "%s%s")
-let NonUniqueInferredAbstractSlot4E() = DeclareResourceString("NonUniqueInferredAbstractSlot4", "")
-let Failure3E() = DeclareResourceString("Failure3", "%s")
-let Failure4E() = DeclareResourceString("Failure4", "%s")
-let MatchIncomplete1E() = DeclareResourceString("MatchIncomplete1", "")
-let MatchIncomplete2E() = DeclareResourceString("MatchIncomplete2", "%s")
-let MatchIncomplete3E() = DeclareResourceString("MatchIncomplete3", "%s")
-let MatchIncomplete4E() = DeclareResourceString("MatchIncomplete4", "")
-let RuleNeverMatchedE() = DeclareResourceString("RuleNeverMatched", "")
-let EnumMatchIncomplete1E() = DeclareResourceString("EnumMatchIncomplete1", "")
-let ValNotMutableE() = DeclareResourceString("ValNotMutable", "%s")
-let ValNotLocalE() = DeclareResourceString("ValNotLocal", "")
-let Obsolete1E() = DeclareResourceString("Obsolete1", "")
-let Obsolete2E() = DeclareResourceString("Obsolete2", "%s")
-let ExperimentalE() = DeclareResourceString("Experimental", "%s")
-let PossibleUnverifiableCodeE() = DeclareResourceString("PossibleUnverifiableCode", "")
-let DeprecatedE() = DeclareResourceString("Deprecated", "%s")
-let LibraryUseOnlyE() = DeclareResourceString("LibraryUseOnly", "")
-let MissingFieldsE() = DeclareResourceString("MissingFields", "%s")
-let ValueRestriction1E() = DeclareResourceString("ValueRestriction1", "%s%s%s")
-let ValueRestriction2E() = DeclareResourceString("ValueRestriction2", "%s%s%s")
-let ValueRestriction3E() = DeclareResourceString("ValueRestriction3", "%s")
-let ValueRestriction4E() = DeclareResourceString("ValueRestriction4", "%s%s%s")
-let ValueRestriction5E() = DeclareResourceString("ValueRestriction5", "%s%s%s")
-let RecoverableParseErrorE() = DeclareResourceString("RecoverableParseError", "")
-let ReservedKeywordE() = DeclareResourceString("ReservedKeyword", "%s")
-let IndentationProblemE() = DeclareResourceString("IndentationProblem", "%s")
-let OverrideInIntrinsicAugmentationE() = DeclareResourceString("OverrideInIntrinsicAugmentation", "")
-let OverrideInExtrinsicAugmentationE() = DeclareResourceString("OverrideInExtrinsicAugmentation", "")
-let IntfImplInIntrinsicAugmentationE() = DeclareResourceString("IntfImplInIntrinsicAugmentation", "")
-let IntfImplInExtrinsicAugmentationE() = DeclareResourceString("IntfImplInExtrinsicAugmentation", "")
-let UnresolvedReferenceNoRangeE() = DeclareResourceString("UnresolvedReferenceNoRange", "%s")
-let UnresolvedPathReferenceNoRangeE() = DeclareResourceString("UnresolvedPathReferenceNoRange", "%s%s")
-let HashIncludeNotAllowedInNonScriptE() = DeclareResourceString("HashIncludeNotAllowedInNonScript", "")
-let HashReferenceNotAllowedInNonScriptE() = DeclareResourceString("HashReferenceNotAllowedInNonScript", "")
-let HashDirectiveNotAllowedInNonScriptE() = DeclareResourceString("HashDirectiveNotAllowedInNonScript", "")
-let FileNameNotResolvedE() = DeclareResourceString("FileNameNotResolved", "%s%s")
-let AssemblyNotResolvedE() = DeclareResourceString("AssemblyNotResolved", "%s")
-let HashLoadedSourceHasIssues0E() = DeclareResourceString("HashLoadedSourceHasIssues0", "")
-let HashLoadedSourceHasIssues1E() = DeclareResourceString("HashLoadedSourceHasIssues1", "")
-let HashLoadedSourceHasIssues2E() = DeclareResourceString("HashLoadedSourceHasIssues2", "")
-let HashLoadedScriptConsideredSourceE() = DeclareResourceString("HashLoadedScriptConsideredSource", "")
-let InvalidInternalsVisibleToAssemblyName1E() = DeclareResourceString("InvalidInternalsVisibleToAssemblyName1", "%s%s")
-let InvalidInternalsVisibleToAssemblyName2E() = DeclareResourceString("InvalidInternalsVisibleToAssemblyName2", "%s")
-let LoadedSourceNotFoundIgnoringE() = DeclareResourceString("LoadedSourceNotFoundIgnoring", "%s")
-let MSBuildReferenceResolutionErrorE() = DeclareResourceString("MSBuildReferenceResolutionError", "%s%s")
-let TargetInvocationExceptionWrapperE() = DeclareResourceString("TargetInvocationExceptionWrapper", "%s")
+let SeeAlsoE () = Message("SeeAlso", "%s")
+let ConstraintSolverTupleDiffLengthsE () = Message("ConstraintSolverTupleDiffLengths", "%d%d")
+let ConstraintSolverInfiniteTypesE () = Message("ConstraintSolverInfiniteTypes", "%s%s")
+let ConstraintSolverMissingConstraintE () = Message("ConstraintSolverMissingConstraint", "%s")
+let ConstraintSolverTypesNotInEqualityRelation1E () = Message("ConstraintSolverTypesNotInEqualityRelation1", "%s%s")
+let ConstraintSolverTypesNotInEqualityRelation2E () = Message("ConstraintSolverTypesNotInEqualityRelation2", "%s%s")
+let ConstraintSolverTypesNotInSubsumptionRelationE () = Message("ConstraintSolverTypesNotInSubsumptionRelation", "%s%s%s")
+let ErrorFromAddingTypeEquation1E () = Message("ErrorFromAddingTypeEquation1", "%s%s%s")
+let ErrorFromAddingTypeEquation2E () = Message("ErrorFromAddingTypeEquation2", "%s%s%s")
+let ErrorFromApplyingDefault1E () = Message("ErrorFromApplyingDefault1", "%s")
+let ErrorFromApplyingDefault2E () = Message("ErrorFromApplyingDefault2", "")
+let ErrorsFromAddingSubsumptionConstraintE () = Message("ErrorsFromAddingSubsumptionConstraint", "%s%s%s")
+let UpperCaseIdentifierInPatternE () = Message("UpperCaseIdentifierInPattern", "")
+let NotUpperCaseConstructorE () = Message("NotUpperCaseConstructor", "")
+let FunctionExpectedE () = Message("FunctionExpected", "")
+let BakedInMemberConstraintNameE () = Message("BakedInMemberConstraintName", "%s")
+let BadEventTransformationE () = Message("BadEventTransformation", "")
+let ParameterlessStructCtorE () = Message("ParameterlessStructCtor", "")
+let InterfaceNotRevealedE () = Message("InterfaceNotRevealed", "%s")
+let TyconBadArgsE () = Message("TyconBadArgs", "%s%d%d")
+let IndeterminateTypeE () = Message("IndeterminateType", "")
+let NameClash1E () = Message("NameClash1", "%s%s")
+let NameClash2E () = Message("NameClash2", "%s%s%s%s%s")
+let Duplicate1E () = Message("Duplicate1", "%s")
+let Duplicate2E () = Message("Duplicate2", "%s%s")
+let UndefinedName2E () = Message("UndefinedName2", "")
+let FieldNotMutableE () = Message("FieldNotMutable", "")
+let FieldsFromDifferentTypesE () = Message("FieldsFromDifferentTypes", "%s%s")
+let VarBoundTwiceE () = Message("VarBoundTwice", "%s")
+let RecursionE () = Message("Recursion", "%s%s%s%s")
+let InvalidRuntimeCoercionE () = Message("InvalidRuntimeCoercion", "%s%s%s")
+let IndeterminateRuntimeCoercionE () = Message("IndeterminateRuntimeCoercion", "%s%s")
+let IndeterminateStaticCoercionE () = Message("IndeterminateStaticCoercion", "%s%s")
+let StaticCoercionShouldUseBoxE () = Message("StaticCoercionShouldUseBox", "%s%s")
+let TypeIsImplicitlyAbstractE () = Message("TypeIsImplicitlyAbstract", "")
+let NonRigidTypar1E () = Message("NonRigidTypar1", "%s%s")
+let NonRigidTypar2E () = Message("NonRigidTypar2", "%s%s")
+let NonRigidTypar3E () = Message("NonRigidTypar3", "%s%s")
+let OBlockEndSentenceE () = Message("BlockEndSentence", "")
+let UnexpectedEndOfInputE () = Message("UnexpectedEndOfInput", "")
+let UnexpectedE () = Message("Unexpected", "%s")
+let NONTERM_interactionE () = Message("NONTERM.interaction", "")
+let NONTERM_hashDirectiveE () = Message("NONTERM.hashDirective", "")
+let NONTERM_fieldDeclE () = Message("NONTERM.fieldDecl", "")
+let NONTERM_unionCaseReprE () = Message("NONTERM.unionCaseRepr", "")
+let NONTERM_localBindingE () = Message("NONTERM.localBinding", "")
+let NONTERM_hardwhiteLetBindingsE () = Message("NONTERM.hardwhiteLetBindings", "")
+let NONTERM_classDefnMemberE () = Message("NONTERM.classDefnMember", "")
+let NONTERM_defnBindingsE () = Message("NONTERM.defnBindings", "")
+let NONTERM_classMemberSpfnE () = Message("NONTERM.classMemberSpfn", "")
+let NONTERM_valSpfnE () = Message("NONTERM.valSpfn", "")
+let NONTERM_tyconSpfnE () = Message("NONTERM.tyconSpfn", "")
+let NONTERM_anonLambdaExprE () = Message("NONTERM.anonLambdaExpr", "")
+let NONTERM_attrUnionCaseDeclE () = Message("NONTERM.attrUnionCaseDecl", "")
+let NONTERM_cPrototypeE () = Message("NONTERM.cPrototype", "")
+let NONTERM_objectImplementationMembersE () = Message("NONTERM.objectImplementationMembers", "")
+let NONTERM_ifExprCasesE () = Message("NONTERM.ifExprCases", "")
+let NONTERM_openDeclE () = Message("NONTERM.openDecl", "")
+let NONTERM_fileModuleSpecE () = Message("NONTERM.fileModuleSpec", "")
+let NONTERM_patternClausesE () = Message("NONTERM.patternClauses", "")
+let NONTERM_beginEndExprE () = Message("NONTERM.beginEndExpr", "")
+let NONTERM_recdExprE () = Message("NONTERM.recdExpr", "")
+let NONTERM_tyconDefnE () = Message("NONTERM.tyconDefn", "")
+let NONTERM_exconCoreE () = Message("NONTERM.exconCore", "")
+let NONTERM_typeNameInfoE () = Message("NONTERM.typeNameInfo", "")
+let NONTERM_attributeListE () = Message("NONTERM.attributeList", "")
+let NONTERM_quoteExprE () = Message("NONTERM.quoteExpr", "")
+let NONTERM_typeConstraintE () = Message("NONTERM.typeConstraint", "")
+let NONTERM_Category_ImplementationFileE () = Message("NONTERM.Category.ImplementationFile", "")
+let NONTERM_Category_DefinitionE () = Message("NONTERM.Category.Definition", "")
+let NONTERM_Category_SignatureFileE () = Message("NONTERM.Category.SignatureFile", "")
+let NONTERM_Category_PatternE () = Message("NONTERM.Category.Pattern", "")
+let NONTERM_Category_ExprE () = Message("NONTERM.Category.Expr", "")
+let NONTERM_Category_TypeE () = Message("NONTERM.Category.Type", "")
+let NONTERM_typeArgsActualE () = Message("NONTERM.typeArgsActual", "")
+let TokenName1E () = Message("TokenName1", "%s")
+let TokenName1TokenName2E () = Message("TokenName1TokenName2", "%s%s")
+let TokenName1TokenName2TokenName3E () = Message("TokenName1TokenName2TokenName3", "%s%s%s")
+let RuntimeCoercionSourceSealed1E () = Message("RuntimeCoercionSourceSealed1", "%s")
+let RuntimeCoercionSourceSealed2E () = Message("RuntimeCoercionSourceSealed2", "%s")
+let CoercionTargetSealedE () = Message("CoercionTargetSealed", "%s")
+let UpcastUnnecessaryE () = Message("UpcastUnnecessary", "")
+let TypeTestUnnecessaryE () = Message("TypeTestUnnecessary", "")
+let OverrideDoesntOverride1E () = Message("OverrideDoesntOverride1", "%s")
+let OverrideDoesntOverride2E () = Message("OverrideDoesntOverride2", "%s")
+let OverrideDoesntOverride3E () = Message("OverrideDoesntOverride3", "%s")
+let OverrideDoesntOverride4E () = Message("OverrideDoesntOverride4", "%s")
+let UnionCaseWrongArgumentsE () = Message("UnionCaseWrongArguments", "%d%d")
+let UnionPatternsBindDifferentNamesE () = Message("UnionPatternsBindDifferentNames", "")
+let RequiredButNotSpecifiedE () = Message("RequiredButNotSpecified", "%s%s%s")
+let UseOfAddressOfOperatorE () = Message("UseOfAddressOfOperator", "")
+let DefensiveCopyWarningE () = Message("DefensiveCopyWarning", "%s")
+let DeprecatedThreadStaticBindingWarningE () = Message("DeprecatedThreadStaticBindingWarning", "")
+let FunctionValueUnexpectedE () = Message("FunctionValueUnexpected", "%s")
+let UnitTypeExpectedE () = Message("UnitTypeExpected", "%s")
+let UnitTypeExpectedWithEqualityE () = Message("UnitTypeExpectedWithEquality", "%s")
+let UnitTypeExpectedWithPossiblePropertySetterE () = Message("UnitTypeExpectedWithPossiblePropertySetter", "%s%s%s")
+let UnitTypeExpectedWithPossibleAssignmentE () = Message("UnitTypeExpectedWithPossibleAssignment", "%s%s")
+let UnitTypeExpectedWithPossibleAssignmentToMutableE () = Message("UnitTypeExpectedWithPossibleAssignmentToMutable", "%s%s")
+let RecursiveUseCheckedAtRuntimeE () = Message("RecursiveUseCheckedAtRuntime", "")
+let LetRecUnsound1E () = Message("LetRecUnsound1", "%s")
+let LetRecUnsound2E () = Message("LetRecUnsound2", "%s%s")
+let LetRecUnsoundInnerE () = Message("LetRecUnsoundInner", "%s")
+let LetRecEvaluatedOutOfOrderE () = Message("LetRecEvaluatedOutOfOrder", "")
+let LetRecCheckedAtRuntimeE () = Message("LetRecCheckedAtRuntime", "")
+let SelfRefObjCtor1E () = Message("SelfRefObjCtor1", "")
+let SelfRefObjCtor2E () = Message("SelfRefObjCtor2", "")
+let VirtualAugmentationOnNullValuedTypeE () = Message("VirtualAugmentationOnNullValuedType", "")
+let NonVirtualAugmentationOnNullValuedTypeE () = Message("NonVirtualAugmentationOnNullValuedType", "")
+let NonUniqueInferredAbstractSlot1E () = Message("NonUniqueInferredAbstractSlot1", "%s")
+let NonUniqueInferredAbstractSlot2E () = Message("NonUniqueInferredAbstractSlot2", "")
+let NonUniqueInferredAbstractSlot3E () = Message("NonUniqueInferredAbstractSlot3", "%s%s")
+let NonUniqueInferredAbstractSlot4E () = Message("NonUniqueInferredAbstractSlot4", "")
+let Failure3E () = Message("Failure3", "%s")
+let Failure4E () = Message("Failure4", "%s")
+let MatchIncomplete1E () = Message("MatchIncomplete1", "")
+let MatchIncomplete2E () = Message("MatchIncomplete2", "%s")
+let MatchIncomplete3E () = Message("MatchIncomplete3", "%s")
+let MatchIncomplete4E () = Message("MatchIncomplete4", "")
+let RuleNeverMatchedE () = Message("RuleNeverMatched", "")
+let EnumMatchIncomplete1E () = Message("EnumMatchIncomplete1", "")
+let ValNotMutableE () = Message("ValNotMutable", "%s")
+let ValNotLocalE () = Message("ValNotLocal", "")
+let Obsolete1E () = Message("Obsolete1", "")
+let Obsolete2E () = Message("Obsolete2", "%s")
+let ExperimentalE () = Message("Experimental", "%s")
+let PossibleUnverifiableCodeE () = Message("PossibleUnverifiableCode", "")
+let DeprecatedE () = Message("Deprecated", "%s")
+let LibraryUseOnlyE () = Message("LibraryUseOnly", "")
+let MissingFieldsE () = Message("MissingFields", "%s")
+let ValueRestriction1E () = Message("ValueRestriction1", "%s%s%s")
+let ValueRestriction2E () = Message("ValueRestriction2", "%s%s%s")
+let ValueRestriction3E () = Message("ValueRestriction3", "%s")
+let ValueRestriction4E () = Message("ValueRestriction4", "%s%s%s")
+let ValueRestriction5E () = Message("ValueRestriction5", "%s%s%s")
+let RecoverableParseErrorE () = Message("RecoverableParseError", "")
+let ReservedKeywordE () = Message("ReservedKeyword", "%s")
+let IndentationProblemE () = Message("IndentationProblem", "%s")
+let OverrideInIntrinsicAugmentationE () = Message("OverrideInIntrinsicAugmentation", "")
+let OverrideInExtrinsicAugmentationE () = Message("OverrideInExtrinsicAugmentation", "")
+let IntfImplInIntrinsicAugmentationE () = Message("IntfImplInIntrinsicAugmentation", "")
+let IntfImplInExtrinsicAugmentationE () = Message("IntfImplInExtrinsicAugmentation", "")
+let UnresolvedReferenceNoRangeE () = Message("UnresolvedReferenceNoRange", "%s")
+let UnresolvedPathReferenceNoRangeE () = Message("UnresolvedPathReferenceNoRange", "%s%s")
+let HashIncludeNotAllowedInNonScriptE () = Message("HashIncludeNotAllowedInNonScript", "")
+let HashReferenceNotAllowedInNonScriptE () = Message("HashReferenceNotAllowedInNonScript", "")
+let HashDirectiveNotAllowedInNonScriptE () = Message("HashDirectiveNotAllowedInNonScript", "")
+let FileNameNotResolvedE () = Message("FileNameNotResolved", "%s%s")
+let AssemblyNotResolvedE () = Message("AssemblyNotResolved", "%s")
+let HashLoadedSourceHasIssues0E () = Message("HashLoadedSourceHasIssues0", "")
+let HashLoadedSourceHasIssues1E () = Message("HashLoadedSourceHasIssues1", "")
+let HashLoadedSourceHasIssues2E () = Message("HashLoadedSourceHasIssues2", "")
+let HashLoadedScriptConsideredSourceE () = Message("HashLoadedScriptConsideredSource", "")
+let InvalidInternalsVisibleToAssemblyName1E () = Message("InvalidInternalsVisibleToAssemblyName1", "%s%s")
+let InvalidInternalsVisibleToAssemblyName2E () = Message("InvalidInternalsVisibleToAssemblyName2", "%s")
+let LoadedSourceNotFoundIgnoringE () = Message("LoadedSourceNotFoundIgnoring", "%s")
+let MSBuildReferenceResolutionErrorE () = Message("MSBuildReferenceResolutionError", "%s%s")
+let TargetInvocationExceptionWrapperE () = Message("TargetInvocationExceptionWrapper", "%s")
 
 #if DEBUG
 let mutable showParserStackOnParseError = false
@@ -580,744 +585,873 @@ let mutable showParserStackOnParseError = false
 
 let getErrorString key = SR.GetString key
 
-let (|InvalidArgument|_|) (exn: exn) = match exn with :? ArgumentException as e -> Some e.Message | _ -> None
+let (|InvalidArgument|_|) (exn: exn) =
+    match exn with
+    | :? ArgumentException as e -> Some e.Message
+    | _ -> None
 
 let OutputPhasedErrorR (os: StringBuilder) (diagnostic: PhasedDiagnostic) (canSuggestNames: bool) =
 
     let suggestNames suggestionsF idText =
         if canSuggestNames then
             let buffer = DiagnosticResolutionHints.SuggestionBuffer idText
+
             if not buffer.Disabled then
-              suggestionsF buffer.Add
-              if not buffer.IsEmpty then
-                  os.AppendString " "
-                  os.AppendString(FSComp.SR.undefinedNameSuggestionsIntro())
-                  for value in buffer do
-                      os.AppendLine() |> ignore
-                      os.AppendString "   "
-                      os.AppendString(DecompileOpName value)
+                suggestionsF buffer.Add
+
+                if not buffer.IsEmpty then
+                    os.AppendString " "
+                    os.AppendString(FSComp.SR.undefinedNameSuggestionsIntro ())
+
+                    for value in buffer do
+                        os.AppendLine() |> ignore
+                        os.AppendString "   "
+                        os.AppendString(DecompileOpName value)
 
     let rec OutputExceptionR (os: StringBuilder) error =
 
-      match error with
-      | ConstraintSolverTupleDiffLengths(_, tl1, tl2, m, m2) ->
-          os.AppendString(ConstraintSolverTupleDiffLengthsE().Format tl1.Length tl2.Length)
-          if m.StartLine <> m2.StartLine then
-             os.AppendString(SeeAlsoE().Format (stringOfRange m))
-
-      | ConstraintSolverInfiniteTypes(denv, contextInfo, ty1, ty2, m, m2) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(ConstraintSolverInfiniteTypesE().Format ty1 ty2)
-
-          match contextInfo with
-          | ContextInfo.ReturnInComputationExpression ->
-            os.AppendString(" " + FSComp.SR.returnUsedInsteadOfReturnBang())
-          | ContextInfo.YieldInComputationExpression ->
-            os.AppendString(" " + FSComp.SR.yieldUsedInsteadOfYieldBang())
-          | _ -> ()
-
-          if m.StartLine <> m2.StartLine then
-             os.AppendString(SeeAlsoE().Format (stringOfRange m))
-
-      | ConstraintSolverMissingConstraint(denv, tpr, tpc, m, m2) ->
-          os.AppendString(ConstraintSolverMissingConstraintE().Format (NicePrint.stringOfTyparConstraint denv (tpr, tpc)))
-          if m.StartLine <> m2.StartLine then
-             os.AppendString(SeeAlsoE().Format (stringOfRange m))
-
-      | ConstraintSolverTypesNotInEqualityRelation(denv, (TType_measure _ as ty1), (TType_measure _ as ty2), m, m2, _) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-
-          os.AppendString(ConstraintSolverTypesNotInEqualityRelation1E().Format ty1 ty2 )
-
-          if m.StartLine <> m2.StartLine then
-             os.AppendString(SeeAlsoE().Format (stringOfRange m))
-
-      | ConstraintSolverTypesNotInEqualityRelation(denv, ty1, ty2, m, m2, contextInfo) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-
-          match contextInfo with
-          | ContextInfo.IfExpression range when equals range m -> os.AppendString(FSComp.SR.ifExpression(ty1, ty2))
-          | ContextInfo.CollectionElement (isArray, range) when equals range m ->
-            if isArray then
-                os.AppendString(FSComp.SR.arrayElementHasWrongType(ty1, ty2))
-            else
-                os.AppendString(FSComp.SR.listElementHasWrongType(ty1, ty2))
-          | ContextInfo.OmittedElseBranch range when equals range m -> os.AppendString(FSComp.SR.missingElseBranch(ty2))
-          | ContextInfo.ElseBranchResult range when equals range m -> os.AppendString(FSComp.SR.elseBranchHasWrongType(ty1, ty2))
-          | ContextInfo.FollowingPatternMatchClause range when equals range m -> os.AppendString(FSComp.SR.followingPatternMatchClauseHasWrongType(ty1, ty2))
-          | ContextInfo.PatternMatchGuard range when equals range m -> os.AppendString(FSComp.SR.patternMatchGuardIsNotBool(ty2))
-          | _ -> os.AppendString(ConstraintSolverTypesNotInEqualityRelation2E().Format ty1 ty2)
-          if m.StartLine <> m2.StartLine then
-             os.AppendString(SeeAlsoE().Format (stringOfRange m))
-
-      | ConstraintSolverTypesNotInSubsumptionRelation(denv, ty1, ty2, m, m2) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(ConstraintSolverTypesNotInSubsumptionRelationE().Format ty2 ty1 cxs)
-          if m.StartLine <> m2.StartLine then
-             os.AppendString(SeeAlsoE().Format (stringOfRange m2))
-
-      | ConstraintSolverError(msg, m, m2) ->
-         os.AppendString msg
-         if m.StartLine <> m2.StartLine then
-            os.AppendString(SeeAlsoE().Format (stringOfRange m2))
-
-      | ErrorFromAddingTypeEquation(g, denv, ty1, ty2, ConstraintSolverTypesNotInEqualityRelation(_, ty1b, ty2b, m, _, contextInfo), _)
-         when typeEquiv g ty1 ty1b
-              && typeEquiv g ty2 ty2b ->
-          let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          match contextInfo with
-          | ContextInfo.IfExpression range when equals range m ->
-              os.AppendString(FSComp.SR.ifExpression(ty1, ty2))
-
-          | ContextInfo.CollectionElement (isArray, range) when equals range m ->
-              if isArray then
-                  os.AppendString(FSComp.SR.arrayElementHasWrongType(ty1, ty2))
-              else
-                  os.AppendString(FSComp.SR.listElementHasWrongType(ty1, ty2))
-
-          | ContextInfo.OmittedElseBranch range when equals range m ->
-              os.AppendString(FSComp.SR.missingElseBranch(ty2))
-
-          | ContextInfo.ElseBranchResult range when equals range m ->
-              os.AppendString(FSComp.SR.elseBranchHasWrongType(ty1, ty2))
-
-          | ContextInfo.FollowingPatternMatchClause range when equals range m ->
-              os.AppendString(FSComp.SR.followingPatternMatchClauseHasWrongType(ty1, ty2))
-
-          | ContextInfo.PatternMatchGuard range when equals range m ->
-              os.AppendString(FSComp.SR.patternMatchGuardIsNotBool(ty2))
-
-          | ContextInfo.TupleInRecordFields ->
-              os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
-              os.AppendString(Environment.NewLine + FSComp.SR.commaInsteadOfSemicolonInRecord())
-
-          | _ when ty2 = "bool" && ty1.EndsWithOrdinal(" ref") ->
-              os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
-              os.AppendString(Environment.NewLine + FSComp.SR.derefInsteadOfNot())
-
-          | _ ->
-              os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
-
-      | ErrorFromAddingTypeEquation(_, _, _, _, (ConstraintSolverTypesNotInEqualityRelation (_, _, _, _, _, contextInfo) as e), _)
-              when (match contextInfo with ContextInfo.NoContext -> false | _ -> true) ->
-          OutputExceptionR os e
-
-      | ErrorFromAddingTypeEquation(_, _, _, _, (ConstraintSolverTypesNotInSubsumptionRelation _ | ConstraintSolverError _ as e), _) ->
-          OutputExceptionR os e
-
-      | ErrorFromAddingTypeEquation(g, denv, ty1, ty2, e, _) ->
-          if not (typeEquiv g ty1 ty2) then
-              let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-              if ty1<>ty2 + tpcs then os.AppendString(ErrorFromAddingTypeEquation2E().Format ty1 ty2 tpcs)
-
-          OutputExceptionR os e
-
-      | ErrorFromApplyingDefault(_, denv, _, defaultType, e, _) ->
-          let defaultType = NicePrint.minimalStringOfType denv defaultType
-          os.AppendString(ErrorFromApplyingDefault1E().Format defaultType)
-          OutputExceptionR os e
-          os.AppendString(ErrorFromApplyingDefault2E().Format)
-
-      | ErrorsFromAddingSubsumptionConstraint(g, denv, ty1, ty2, e, contextInfo, _) ->
-          match contextInfo with
-          | ContextInfo.DowncastUsedInsteadOfUpcast isOperator ->
-              let ty1, ty2, _ = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-              if isOperator then
-                  os.AppendString(FSComp.SR.considerUpcastOperator(ty1, ty2) |> snd)
-              else
-                  os.AppendString(FSComp.SR.considerUpcast(ty1, ty2) |> snd)
-          | _ ->
-              if not (typeEquiv g ty1 ty2) then
-                  let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-                  if ty1 <> (ty2 + tpcs) then
-                      os.AppendString(ErrorsFromAddingSubsumptionConstraintE().Format ty2 ty1 tpcs)
-                  else
-                      OutputExceptionR os e
-              else
-                  OutputExceptionR os e
-
-      | UpperCaseIdentifierInPattern _ ->
-          os.AppendString(UpperCaseIdentifierInPatternE().Format)
-
-      | NotUpperCaseConstructor _ ->
-          os.AppendString(NotUpperCaseConstructorE().Format)
-
-      | ErrorFromAddingConstraint(_, e, _) ->
-          OutputExceptionR os e
+        match error with
+        | ConstraintSolverTupleDiffLengths (_, tl1, tl2, m, m2) ->
+            os.AppendString(ConstraintSolverTupleDiffLengthsE().Format tl1.Length tl2.Length)
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m))
+
+        | ConstraintSolverInfiniteTypes (denv, contextInfo, ty1, ty2, m, m2) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(ConstraintSolverInfiniteTypesE().Format ty1 ty2)
+
+            match contextInfo with
+            | ContextInfo.ReturnInComputationExpression -> os.AppendString(" " + FSComp.SR.returnUsedInsteadOfReturnBang ())
+            | ContextInfo.YieldInComputationExpression -> os.AppendString(" " + FSComp.SR.yieldUsedInsteadOfYieldBang ())
+            | _ -> ()
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m))
+
+        | ConstraintSolverMissingConstraint (denv, tpr, tpc, m, m2) ->
+            os.AppendString(
+                ConstraintSolverMissingConstraintE()
+                    .Format(NicePrint.stringOfTyparConstraint denv (tpr, tpc))
+            )
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m))
+
+        | ConstraintSolverTypesNotInEqualityRelation (denv, (TType_measure _ as ty1), (TType_measure _ as ty2), m, m2, _) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+
+            os.AppendString(ConstraintSolverTypesNotInEqualityRelation1E().Format ty1 ty2)
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m))
+
+        | ConstraintSolverTypesNotInEqualityRelation (denv, ty1, ty2, m, m2, contextInfo) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+
+            match contextInfo with
+            | ContextInfo.IfExpression range when equals range m -> os.AppendString(FSComp.SR.ifExpression (ty1, ty2))
+            | ContextInfo.CollectionElement (isArray, range) when equals range m ->
+                if isArray then
+                    os.AppendString(FSComp.SR.arrayElementHasWrongType (ty1, ty2))
+                else
+                    os.AppendString(FSComp.SR.listElementHasWrongType (ty1, ty2))
+            | ContextInfo.OmittedElseBranch range when equals range m -> os.AppendString(FSComp.SR.missingElseBranch (ty2))
+            | ContextInfo.ElseBranchResult range when equals range m -> os.AppendString(FSComp.SR.elseBranchHasWrongType (ty1, ty2))
+            | ContextInfo.FollowingPatternMatchClause range when equals range m ->
+                os.AppendString(FSComp.SR.followingPatternMatchClauseHasWrongType (ty1, ty2))
+            | ContextInfo.PatternMatchGuard range when equals range m -> os.AppendString(FSComp.SR.patternMatchGuardIsNotBool (ty2))
+            | _ -> os.AppendString(ConstraintSolverTypesNotInEqualityRelation2E().Format ty1 ty2)
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m))
+
+        | ConstraintSolverTypesNotInSubsumptionRelation (denv, ty1, ty2, m, m2) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(ConstraintSolverTypesNotInSubsumptionRelationE().Format ty2 ty1 cxs)
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m2))
+
+        | ConstraintSolverError (msg, m, m2) ->
+            os.AppendString msg
+
+            if m.StartLine <> m2.StartLine then
+                os.AppendString(SeeAlsoE().Format(stringOfRange m2))
+
+        | ErrorFromAddingTypeEquation (g, denv, ty1, ty2, ConstraintSolverTypesNotInEqualityRelation (_, ty1b, ty2b, m, _, contextInfo), _) when
+            typeEquiv g ty1 ty1b && typeEquiv g ty2 ty2b
+            ->
+            let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+
+            match contextInfo with
+            | ContextInfo.IfExpression range when equals range m -> os.AppendString(FSComp.SR.ifExpression (ty1, ty2))
+
+            | ContextInfo.CollectionElement (isArray, range) when equals range m ->
+                if isArray then
+                    os.AppendString(FSComp.SR.arrayElementHasWrongType (ty1, ty2))
+                else
+                    os.AppendString(FSComp.SR.listElementHasWrongType (ty1, ty2))
+
+            | ContextInfo.OmittedElseBranch range when equals range m -> os.AppendString(FSComp.SR.missingElseBranch (ty2))
+
+            | ContextInfo.ElseBranchResult range when equals range m -> os.AppendString(FSComp.SR.elseBranchHasWrongType (ty1, ty2))
+
+            | ContextInfo.FollowingPatternMatchClause range when equals range m ->
+                os.AppendString(FSComp.SR.followingPatternMatchClauseHasWrongType (ty1, ty2))
+
+            | ContextInfo.PatternMatchGuard range when equals range m -> os.AppendString(FSComp.SR.patternMatchGuardIsNotBool (ty2))
+
+            | ContextInfo.TupleInRecordFields ->
+                os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
+                os.AppendString(Environment.NewLine + FSComp.SR.commaInsteadOfSemicolonInRecord ())
+
+            | _ when ty2 = "bool" && ty1.EndsWithOrdinal(" ref") ->
+                os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
+                os.AppendString(Environment.NewLine + FSComp.SR.derefInsteadOfNot ())
+
+            | _ -> os.AppendString(ErrorFromAddingTypeEquation1E().Format ty2 ty1 tpcs)
+
+        | ErrorFromAddingTypeEquation (_, _, _, _, (ConstraintSolverTypesNotInEqualityRelation (_, _, _, _, _, contextInfo) as e), _) when
+            (match contextInfo with
+             | ContextInfo.NoContext -> false
+             | _ -> true)
+            ->
+            OutputExceptionR os e
+
+        | ErrorFromAddingTypeEquation (_,
+                                       _,
+                                       _,
+                                       _,
+                                       (ConstraintSolverTypesNotInSubsumptionRelation _
+                                       | ConstraintSolverError _ as e),
+                                       _) -> OutputExceptionR os e
+
+        | ErrorFromAddingTypeEquation (g, denv, ty1, ty2, e, _) ->
+            if not (typeEquiv g ty1 ty2) then
+                let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+
+                if ty1 <> ty2 + tpcs then
+                    os.AppendString(ErrorFromAddingTypeEquation2E().Format ty1 ty2 tpcs)
+
+            OutputExceptionR os e
+
+        | ErrorFromApplyingDefault (_, denv, _, defaultType, e, _) ->
+            let defaultType = NicePrint.minimalStringOfType denv defaultType
+            os.AppendString(ErrorFromApplyingDefault1E().Format defaultType)
+            OutputExceptionR os e
+            os.AppendString(ErrorFromApplyingDefault2E().Format)
+
+        | ErrorsFromAddingSubsumptionConstraint (g, denv, ty1, ty2, e, contextInfo, _) ->
+            match contextInfo with
+            | ContextInfo.DowncastUsedInsteadOfUpcast isOperator ->
+                let ty1, ty2, _ = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+
+                if isOperator then
+                    os.AppendString(FSComp.SR.considerUpcastOperator (ty1, ty2) |> snd)
+                else
+                    os.AppendString(FSComp.SR.considerUpcast (ty1, ty2) |> snd)
+            | _ ->
+                if not (typeEquiv g ty1 ty2) then
+                    let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+
+                    if ty1 <> (ty2 + tpcs) then
+                        os.AppendString(ErrorsFromAddingSubsumptionConstraintE().Format ty2 ty1 tpcs)
+                    else
+                        OutputExceptionR os e
+                else
+                    OutputExceptionR os e
+
+        | UpperCaseIdentifierInPattern _ -> os.AppendString(UpperCaseIdentifierInPatternE().Format)
+
+        | NotUpperCaseConstructor _ -> os.AppendString(NotUpperCaseConstructorE().Format)
+
+        | ErrorFromAddingConstraint (_, e, _) -> OutputExceptionR os e
 
 #if !NO_TYPEPROVIDERS
-      | TypeProviders.ProvidedTypeResolutionNoRange e
+        | TypeProviders.ProvidedTypeResolutionNoRange e
 
-      | TypeProviders.ProvidedTypeResolution(_, e) ->
-          OutputExceptionR os e
+        | TypeProviders.ProvidedTypeResolution (_, e) -> OutputExceptionR os e
 
-      | :? TypeProviderError as e ->
-          os.AppendString(e.ContextualErrorMessage)
+        | :? TypeProviderError as e -> os.AppendString(e.ContextualErrorMessage)
 #endif
 
-      | UnresolvedOverloading(denv, callerArgs, failure, m) ->
-
-          // extract eventual information (return type and type parameters)
-          // from ConstraintTraitInfo
-          let knownReturnType, genericParameterTypes =
-              match failure with
-              | NoOverloadsFound (cx=Some cx)
-              | PossibleCandidates (cx=Some cx) -> cx.ReturnType, cx.ArgumentTypes
-              | _ -> None, []
-
-          // prepare message parts (known arguments, known return type, known generic parameters)
-          let argsMessage, returnType, genericParametersMessage =
-
-              let retTy =
-                  knownReturnType
-                  |> Option.defaultValue (TType_var (Typar.NewUnlinked(), 0uy))
-
-              let argRepr =
-                  callerArgs.ArgumentNamesAndTypes
-                  |> List.map (fun (name,tTy) -> tTy, {ArgReprInfo.Name = name |> Option.map (fun name -> Ident(name, range.Zero)); ArgReprInfo.Attribs = []})
-
-              let argsL,retTyL,genParamTysL = NicePrint.prettyLayoutsOfUnresolvedOverloading denv argRepr retTy genericParameterTypes
-
-              match callerArgs.ArgumentNamesAndTypes with
-              | [] -> None, LayoutRender.showL retTyL, LayoutRender.showL genParamTysL
-              | items ->
-                  let args = LayoutRender.showL argsL
-                  let prefixMessage =
-                      match items with
-                      | [_] -> FSComp.SR.csNoOverloadsFoundArgumentsPrefixSingular
-                      | _ -> FSComp.SR.csNoOverloadsFoundArgumentsPrefixPlural
-                  Some (prefixMessage args),
-                  LayoutRender.showL retTyL,
-                  LayoutRender.showL genParamTysL
-
-          let knownReturnType =
-              match knownReturnType with
-              | None -> None
-              | Some _ -> Some (FSComp.SR.csNoOverloadsFoundReturnType returnType)
-
-          let genericParametersMessage =
-              match genericParameterTypes with
-              | [] -> None
-              | [_] -> Some (FSComp.SR.csNoOverloadsFoundTypeParametersPrefixSingular genericParametersMessage)
-              | _ -> Some (FSComp.SR.csNoOverloadsFoundTypeParametersPrefixPlural genericParametersMessage)
-
-          let overloadMethodInfo displayEnv m (x: OverloadInformation) =
-              let paramInfo =
-                  match x.error with
-                  | :? ArgDoesNotMatchError as x ->
-                      let nameOrOneBasedIndexMessage =
-                          x.calledArg.NameOpt
-                          |> Option.map (fun n -> FSComp.SR.csOverloadCandidateNamedArgumentTypeMismatch n.idText)
-                          |> Option.defaultValue (FSComp.SR.csOverloadCandidateIndexedArgumentTypeMismatch ((vsnd x.calledArg.Position) + 1)) //snd
-                      sprintf " // %s" nameOrOneBasedIndexMessage
-                  | _ -> ""
-
-              (NicePrint.stringOfMethInfo x.infoReader m displayEnv x.methodSlot.Method) + paramInfo
-
-          let nl = Environment.NewLine
-          let formatOverloads (overloads: OverloadInformation list) =
-              overloads
-              |> List.map (overloadMethodInfo denv m)
-              |> List.sort
-              |> List.map FSComp.SR.formatDashItem
-              |> String.concat nl
-
-          // assemble final message composing the parts
-          let msg =
-              let optionalParts =
-                [knownReturnType; genericParametersMessage; argsMessage]
-                |> List.choose id
-                |> String.concat (nl + nl)
-                |> function | "" -> nl
-                            | result -> nl + nl + result + nl + nl
-
-              match failure with
-              | NoOverloadsFound (methodName, overloads, _) ->
-                  FSComp.SR.csNoOverloadsFound methodName
-                      + optionalParts
-                      + (FSComp.SR.csAvailableOverloads (formatOverloads overloads))
-              | PossibleCandidates (methodName, [], _) ->
-                  FSComp.SR.csMethodIsOverloaded methodName
-              | PossibleCandidates (methodName, overloads, _) ->
-                  FSComp.SR.csMethodIsOverloaded methodName
-                      + optionalParts
-                      + FSComp.SR.csCandidates (formatOverloads overloads)
-
-          os.AppendString msg
-
-      | UnresolvedConversionOperator(denv, fromTy, toTy, _) ->
-          let ty1, ty2, _tpcs = NicePrint.minimalStringsOfTwoTypes denv fromTy toTy
-          os.AppendString(FSComp.SR.csTypeDoesNotSupportConversion(ty1, ty2))
-
-      | FunctionExpected _ ->
-          os.AppendString(FunctionExpectedE().Format)
-
-      | BakedInMemberConstraintName(nm, _) ->
-          os.AppendString(BakedInMemberConstraintNameE().Format nm)
-
-      | StandardOperatorRedefinitionWarning(msg, _) ->
-          os.AppendString msg
-
-      | BadEventTransformation _ ->
-         os.AppendString(BadEventTransformationE().Format)
-
-      | ParameterlessStructCtor _ ->
-         os.AppendString(ParameterlessStructCtorE().Format)
-
-      | InterfaceNotRevealed(denv, ity, _) ->
-          os.AppendString(InterfaceNotRevealedE().Format (NicePrint.minimalStringOfType denv ity))
-
-      | NotAFunctionButIndexer(_, _, name, _, _, old) ->
-          if old then
-              match name with
-              | Some name -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexerWithName name)
-              | _ -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexer())
-          else
-              match name with
-              | Some name -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexerWithName2 name)
-              | _ -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexer2())
-
-      | NotAFunction(_, _, _, marg) ->
-          if marg.StartColumn = 0 then
-              os.AppendString(FSComp.SR.notAFunctionButMaybeDeclaration())
-          else
-              os.AppendString(FSComp.SR.notAFunction())
-
-      | TyconBadArgs(_, tcref, d, _) ->
-          let exp = tcref.TyparsNoRange.Length
-          if exp = 0 then
-              os.AppendString(FSComp.SR.buildUnexpectedTypeArgs(fullDisplayTextOfTyconRef tcref, d))
-          else
-              os.AppendString(TyconBadArgsE().Format (fullDisplayTextOfTyconRef tcref) exp d)
-
-      | IndeterminateType _ ->
-          os.AppendString(IndeterminateTypeE().Format)
-
-      | NameClash(nm, k1, nm1, _, k2, nm2, _) ->
-          if nm = nm1 && nm1 = nm2 && k1 = k2 then
-              os.AppendString(NameClash1E().Format k1 nm1)
-          else
-              os.AppendString(NameClash2E().Format k1 nm1 nm k2 nm2)
-
-      | Duplicate(k, s, _) ->
-          if k = "member" then
-              os.AppendString(Duplicate1E().Format (DecompileOpName s))
-          else
-              os.AppendString(Duplicate2E().Format k (DecompileOpName s))
-
-      | UndefinedName(_, k, id, suggestionsF) ->
-          os.AppendString(k (DecompileOpName id.idText))
-          suggestNames suggestionsF id.idText
-
-      | InternalUndefinedItemRef(f, smr, ccuName, s) ->
-          let _, errs = f(smr, ccuName, s)
-          os.AppendString errs
-
-      | FieldNotMutable _ ->
-          os.AppendString(FieldNotMutableE().Format)
-
-      | FieldsFromDifferentTypes (_, fref1, fref2, _) ->
-          os.AppendString(FieldsFromDifferentTypesE().Format fref1.FieldName fref2.FieldName)
-
-      | VarBoundTwice id ->
-          os.AppendString(VarBoundTwiceE().Format (DecompileOpName id.idText))
-
-      | Recursion (denv, id, ty1, ty2, _) ->
-          let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(RecursionE().Format (DecompileOpName id.idText) ty1 ty2 tpcs)
-
-      | InvalidRuntimeCoercion(denv, ty1, ty2, _) ->
-          let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(InvalidRuntimeCoercionE().Format ty1 ty2 tpcs)
-
-      | IndeterminateRuntimeCoercion(denv, ty1, ty2, _) ->
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(IndeterminateRuntimeCoercionE().Format ty1 ty2)
-
-      | IndeterminateStaticCoercion(denv, ty1, ty2, _) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(IndeterminateStaticCoercionE().Format ty1 ty2)
-
-      | StaticCoercionShouldUseBox(denv, ty1, ty2, _) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(StaticCoercionShouldUseBoxE().Format ty1 ty2)
-
-      | TypeIsImplicitlyAbstract _ ->
-          os.AppendString(TypeIsImplicitlyAbstractE().Format)
-
-      | NonRigidTypar(denv, tpnmOpt, typarRange, ty1, ty2, _) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let (ty1, ty2), _cxs = PrettyTypes.PrettifyTypePair denv.g (ty1, ty2)
-          match tpnmOpt with
-          | None ->
-              os.AppendString(NonRigidTypar1E().Format (stringOfRange typarRange) (NicePrint.stringOfTy denv ty2))
-          | Some tpnm ->
-              match ty1 with
-              | TType_measure _ ->
-                os.AppendString(NonRigidTypar2E().Format tpnm (NicePrint.stringOfTy denv ty2))
-              | _ ->
-                os.AppendString(NonRigidTypar3E().Format tpnm (NicePrint.stringOfTy denv ty2))
-
-      | SyntaxError (ctxt, _) ->
-          let ctxt = unbox>(ctxt)
-
-          let (|EndOfStructuredConstructToken|_|) token =
-              match token with
-              | Parser.TOKEN_ODECLEND
-              | Parser.TOKEN_OBLOCKSEP
-              | Parser.TOKEN_OEND
-              | Parser.TOKEN_ORIGHT_BLOCK_END
-              | Parser.TOKEN_OBLOCKEND | Parser.TOKEN_OBLOCKEND_COMING_SOON | Parser.TOKEN_OBLOCKEND_IS_HERE -> Some()
-              | _ -> None
-
-          let tokenIdToText tid =
-              match tid with
-              | Parser.TOKEN_IDENT -> getErrorString("Parser.TOKEN.IDENT")
-              | Parser.TOKEN_BIGNUM
-              | Parser.TOKEN_INT8
-              | Parser.TOKEN_UINT8
-              | Parser.TOKEN_INT16
-              | Parser.TOKEN_UINT16
-              | Parser.TOKEN_INT32
-              | Parser.TOKEN_UINT32
-              | Parser.TOKEN_INT64
-              | Parser.TOKEN_UINT64
-              | Parser.TOKEN_UNATIVEINT
-              | Parser.TOKEN_NATIVEINT -> getErrorString("Parser.TOKEN.INT")
-              | Parser.TOKEN_IEEE32
-              | Parser.TOKEN_IEEE64 -> getErrorString("Parser.TOKEN.FLOAT")
-              | Parser.TOKEN_DECIMAL -> getErrorString("Parser.TOKEN.DECIMAL")
-              | Parser.TOKEN_CHAR -> getErrorString("Parser.TOKEN.CHAR")
-
-              | Parser.TOKEN_BASE -> getErrorString("Parser.TOKEN.BASE")
-              | Parser.TOKEN_LPAREN_STAR_RPAREN -> getErrorString("Parser.TOKEN.LPAREN.STAR.RPAREN")
-              | Parser.TOKEN_DOLLAR -> getErrorString("Parser.TOKEN.DOLLAR")
-              | Parser.TOKEN_INFIX_STAR_STAR_OP -> getErrorString("Parser.TOKEN.INFIX.STAR.STAR.OP")
-              | Parser.TOKEN_INFIX_COMPARE_OP -> getErrorString("Parser.TOKEN.INFIX.COMPARE.OP")
-              | Parser.TOKEN_COLON_GREATER -> getErrorString("Parser.TOKEN.COLON.GREATER")
-              | Parser.TOKEN_COLON_COLON ->getErrorString("Parser.TOKEN.COLON.COLON")
-              | Parser.TOKEN_PERCENT_OP -> getErrorString("Parser.TOKEN.PERCENT.OP")
-              | Parser.TOKEN_INFIX_AT_HAT_OP -> getErrorString("Parser.TOKEN.INFIX.AT.HAT.OP")
-              | Parser.TOKEN_INFIX_BAR_OP -> getErrorString("Parser.TOKEN.INFIX.BAR.OP")
-              | Parser.TOKEN_PLUS_MINUS_OP -> getErrorString("Parser.TOKEN.PLUS.MINUS.OP")
-              | Parser.TOKEN_PREFIX_OP -> getErrorString("Parser.TOKEN.PREFIX.OP")
-              | Parser.TOKEN_COLON_QMARK_GREATER -> getErrorString("Parser.TOKEN.COLON.QMARK.GREATER")
-              | Parser.TOKEN_INFIX_STAR_DIV_MOD_OP -> getErrorString("Parser.TOKEN.INFIX.STAR.DIV.MOD.OP")
-              | Parser.TOKEN_INFIX_AMP_OP -> getErrorString("Parser.TOKEN.INFIX.AMP.OP")
-              | Parser.TOKEN_AMP -> getErrorString("Parser.TOKEN.AMP")
-              | Parser.TOKEN_AMP_AMP -> getErrorString("Parser.TOKEN.AMP.AMP")
-              | Parser.TOKEN_BAR_BAR -> getErrorString("Parser.TOKEN.BAR.BAR")
-              | Parser.TOKEN_LESS -> getErrorString("Parser.TOKEN.LESS")
-              | Parser.TOKEN_GREATER -> getErrorString("Parser.TOKEN.GREATER")
-              | Parser.TOKEN_QMARK -> getErrorString("Parser.TOKEN.QMARK")
-              | Parser.TOKEN_QMARK_QMARK -> getErrorString("Parser.TOKEN.QMARK.QMARK")
-              | Parser.TOKEN_COLON_QMARK-> getErrorString("Parser.TOKEN.COLON.QMARK")
-              | Parser.TOKEN_INT32_DOT_DOT -> getErrorString("Parser.TOKEN.INT32.DOT.DOT")
-              | Parser.TOKEN_DOT_DOT -> getErrorString("Parser.TOKEN.DOT.DOT")
-              | Parser.TOKEN_DOT_DOT_HAT -> getErrorString("Parser.TOKEN.DOT.DOT")
-              | Parser.TOKEN_QUOTE -> getErrorString("Parser.TOKEN.QUOTE")
-              | Parser.TOKEN_STAR -> getErrorString("Parser.TOKEN.STAR")
-              | Parser.TOKEN_HIGH_PRECEDENCE_TYAPP -> getErrorString("Parser.TOKEN.HIGH.PRECEDENCE.TYAPP")
-              | Parser.TOKEN_COLON -> getErrorString("Parser.TOKEN.COLON")
-              | Parser.TOKEN_COLON_EQUALS -> getErrorString("Parser.TOKEN.COLON.EQUALS")
-              | Parser.TOKEN_LARROW -> getErrorString("Parser.TOKEN.LARROW")
-              | Parser.TOKEN_EQUALS -> getErrorString("Parser.TOKEN.EQUALS")
-              | Parser.TOKEN_GREATER_BAR_RBRACK -> getErrorString("Parser.TOKEN.GREATER.BAR.RBRACK")
-              | Parser.TOKEN_MINUS -> getErrorString("Parser.TOKEN.MINUS")
-              | Parser.TOKEN_ADJACENT_PREFIX_OP -> getErrorString("Parser.TOKEN.ADJACENT.PREFIX.OP")
-              | Parser.TOKEN_FUNKY_OPERATOR_NAME -> getErrorString("Parser.TOKEN.FUNKY.OPERATOR.NAME")
-              | Parser.TOKEN_COMMA-> getErrorString("Parser.TOKEN.COMMA")
-              | Parser.TOKEN_DOT -> getErrorString("Parser.TOKEN.DOT")
-              | Parser.TOKEN_BAR-> getErrorString("Parser.TOKEN.BAR")
-              | Parser.TOKEN_HASH -> getErrorString("Parser.TOKEN.HASH")
-              | Parser.TOKEN_UNDERSCORE -> getErrorString("Parser.TOKEN.UNDERSCORE")
-              | Parser.TOKEN_SEMICOLON -> getErrorString("Parser.TOKEN.SEMICOLON")
-              | Parser.TOKEN_SEMICOLON_SEMICOLON-> getErrorString("Parser.TOKEN.SEMICOLON.SEMICOLON")
-              | Parser.TOKEN_LPAREN-> getErrorString("Parser.TOKEN.LPAREN")
-              | Parser.TOKEN_RPAREN | Parser.TOKEN_RPAREN_COMING_SOON | Parser.TOKEN_RPAREN_IS_HERE -> getErrorString("Parser.TOKEN.RPAREN")
-              | Parser.TOKEN_LQUOTE -> getErrorString("Parser.TOKEN.LQUOTE")
-              | Parser.TOKEN_LBRACK -> getErrorString("Parser.TOKEN.LBRACK")
-              | Parser.TOKEN_LBRACE_BAR -> getErrorString("Parser.TOKEN.LBRACE.BAR")
-              | Parser.TOKEN_LBRACK_BAR -> getErrorString("Parser.TOKEN.LBRACK.BAR")
-              | Parser.TOKEN_LBRACK_LESS -> getErrorString("Parser.TOKEN.LBRACK.LESS")
-              | Parser.TOKEN_LBRACE -> getErrorString("Parser.TOKEN.LBRACE")
-              | Parser.TOKEN_BAR_RBRACK -> getErrorString("Parser.TOKEN.BAR.RBRACK")
-              | Parser.TOKEN_BAR_RBRACE -> getErrorString("Parser.TOKEN.BAR.RBRACE")
-              | Parser.TOKEN_GREATER_RBRACK -> getErrorString("Parser.TOKEN.GREATER.RBRACK")
-              | Parser.TOKEN_RQUOTE_DOT _
-              | Parser.TOKEN_RQUOTE -> getErrorString("Parser.TOKEN.RQUOTE")
-              | Parser.TOKEN_RBRACK -> getErrorString("Parser.TOKEN.RBRACK")
-              | Parser.TOKEN_RBRACE | Parser.TOKEN_RBRACE_COMING_SOON | Parser.TOKEN_RBRACE_IS_HERE -> getErrorString("Parser.TOKEN.RBRACE")
-              | Parser.TOKEN_PUBLIC -> getErrorString("Parser.TOKEN.PUBLIC")
-              | Parser.TOKEN_PRIVATE -> getErrorString("Parser.TOKEN.PRIVATE")
-              | Parser.TOKEN_INTERNAL -> getErrorString("Parser.TOKEN.INTERNAL")
-              | Parser.TOKEN_CONSTRAINT -> getErrorString("Parser.TOKEN.CONSTRAINT")
-              | Parser.TOKEN_INSTANCE -> getErrorString("Parser.TOKEN.INSTANCE")
-              | Parser.TOKEN_DELEGATE -> getErrorString("Parser.TOKEN.DELEGATE")
-              | Parser.TOKEN_INHERIT -> getErrorString("Parser.TOKEN.INHERIT")
-              | Parser.TOKEN_CONSTRUCTOR-> getErrorString("Parser.TOKEN.CONSTRUCTOR")
-              | Parser.TOKEN_DEFAULT -> getErrorString("Parser.TOKEN.DEFAULT")
-              | Parser.TOKEN_OVERRIDE-> getErrorString("Parser.TOKEN.OVERRIDE")
-              | Parser.TOKEN_ABSTRACT-> getErrorString("Parser.TOKEN.ABSTRACT")
-              | Parser.TOKEN_CLASS-> getErrorString("Parser.TOKEN.CLASS")
-              | Parser.TOKEN_MEMBER -> getErrorString("Parser.TOKEN.MEMBER")
-              | Parser.TOKEN_STATIC -> getErrorString("Parser.TOKEN.STATIC")
-              | Parser.TOKEN_NAMESPACE-> getErrorString("Parser.TOKEN.NAMESPACE")
-              | Parser.TOKEN_OBLOCKBEGIN -> getErrorString("Parser.TOKEN.OBLOCKBEGIN")
-              | EndOfStructuredConstructToken -> getErrorString("Parser.TOKEN.OBLOCKEND")
-              | Parser.TOKEN_THEN
-              | Parser.TOKEN_OTHEN -> getErrorString("Parser.TOKEN.OTHEN")
-              | Parser.TOKEN_ELSE
-              | Parser.TOKEN_OELSE -> getErrorString("Parser.TOKEN.OELSE")
-              | Parser.TOKEN_LET _
-              | Parser.TOKEN_OLET _ -> getErrorString("Parser.TOKEN.OLET")
-              | Parser.TOKEN_OBINDER
-              | Parser.TOKEN_BINDER -> getErrorString("Parser.TOKEN.BINDER")
-              | Parser.TOKEN_OAND_BANG
-              | Parser.TOKEN_AND_BANG -> getErrorString("Parser.TOKEN.AND.BANG")
-              | Parser.TOKEN_ODO -> getErrorString("Parser.TOKEN.ODO")
-              | Parser.TOKEN_OWITH -> getErrorString("Parser.TOKEN.OWITH")
-              | Parser.TOKEN_OFUNCTION -> getErrorString("Parser.TOKEN.OFUNCTION")
-              | Parser.TOKEN_OFUN -> getErrorString("Parser.TOKEN.OFUN")
-              | Parser.TOKEN_ORESET -> getErrorString("Parser.TOKEN.ORESET")
-              | Parser.TOKEN_ODUMMY -> getErrorString("Parser.TOKEN.ODUMMY")
-              | Parser.TOKEN_DO_BANG
-              | Parser.TOKEN_ODO_BANG -> getErrorString("Parser.TOKEN.ODO.BANG")
-              | Parser.TOKEN_YIELD -> getErrorString("Parser.TOKEN.YIELD")
-              | Parser.TOKEN_YIELD_BANG -> getErrorString("Parser.TOKEN.YIELD.BANG")
-              | Parser.TOKEN_OINTERFACE_MEMBER-> getErrorString("Parser.TOKEN.OINTERFACE.MEMBER")
-              | Parser.TOKEN_ELIF -> getErrorString("Parser.TOKEN.ELIF")
-              | Parser.TOKEN_RARROW -> getErrorString("Parser.TOKEN.RARROW")
-              | Parser.TOKEN_SIG -> getErrorString("Parser.TOKEN.SIG")
-              | Parser.TOKEN_STRUCT -> getErrorString("Parser.TOKEN.STRUCT")
-              | Parser.TOKEN_UPCAST -> getErrorString("Parser.TOKEN.UPCAST")
-              | Parser.TOKEN_DOWNCAST -> getErrorString("Parser.TOKEN.DOWNCAST")
-              | Parser.TOKEN_NULL -> getErrorString("Parser.TOKEN.NULL")
-              | Parser.TOKEN_RESERVED -> getErrorString("Parser.TOKEN.RESERVED")
-              | Parser.TOKEN_MODULE | Parser.TOKEN_MODULE_COMING_SOON | Parser.TOKEN_MODULE_IS_HERE -> getErrorString("Parser.TOKEN.MODULE")
-              | Parser.TOKEN_AND -> getErrorString("Parser.TOKEN.AND")
-              | Parser.TOKEN_AS -> getErrorString("Parser.TOKEN.AS")
-              | Parser.TOKEN_ASSERT -> getErrorString("Parser.TOKEN.ASSERT")
-              | Parser.TOKEN_OASSERT -> getErrorString("Parser.TOKEN.ASSERT")
-              | Parser.TOKEN_ASR-> getErrorString("Parser.TOKEN.ASR")
-              | Parser.TOKEN_DOWNTO -> getErrorString("Parser.TOKEN.DOWNTO")
-              | Parser.TOKEN_EXCEPTION -> getErrorString("Parser.TOKEN.EXCEPTION")
-              | Parser.TOKEN_FALSE -> getErrorString("Parser.TOKEN.FALSE")
-              | Parser.TOKEN_FOR -> getErrorString("Parser.TOKEN.FOR")
-              | Parser.TOKEN_FUN -> getErrorString("Parser.TOKEN.FUN")
-              | Parser.TOKEN_FUNCTION-> getErrorString("Parser.TOKEN.FUNCTION")
-              | Parser.TOKEN_FINALLY -> getErrorString("Parser.TOKEN.FINALLY")
-              | Parser.TOKEN_LAZY -> getErrorString("Parser.TOKEN.LAZY")
-              | Parser.TOKEN_OLAZY -> getErrorString("Parser.TOKEN.LAZY")
-              | Parser.TOKEN_MATCH -> getErrorString("Parser.TOKEN.MATCH")
-              | Parser.TOKEN_MATCH_BANG -> getErrorString("Parser.TOKEN.MATCH.BANG")
-              | Parser.TOKEN_MUTABLE -> getErrorString("Parser.TOKEN.MUTABLE")
-              | Parser.TOKEN_NEW -> getErrorString("Parser.TOKEN.NEW")
-              | Parser.TOKEN_OF -> getErrorString("Parser.TOKEN.OF")
-              | Parser.TOKEN_OPEN -> getErrorString("Parser.TOKEN.OPEN")
-              | Parser.TOKEN_OR -> getErrorString("Parser.TOKEN.OR")
-              | Parser.TOKEN_VOID -> getErrorString("Parser.TOKEN.VOID")
-              | Parser.TOKEN_EXTERN-> getErrorString("Parser.TOKEN.EXTERN")
-              | Parser.TOKEN_INTERFACE -> getErrorString("Parser.TOKEN.INTERFACE")
-              | Parser.TOKEN_REC -> getErrorString("Parser.TOKEN.REC")
-              | Parser.TOKEN_TO -> getErrorString("Parser.TOKEN.TO")
-              | Parser.TOKEN_TRUE -> getErrorString("Parser.TOKEN.TRUE")
-              | Parser.TOKEN_TRY -> getErrorString("Parser.TOKEN.TRY")
-              | Parser.TOKEN_TYPE | Parser.TOKEN_TYPE_COMING_SOON | Parser.TOKEN_TYPE_IS_HERE -> getErrorString("Parser.TOKEN.TYPE")
-              | Parser.TOKEN_VAL -> getErrorString("Parser.TOKEN.VAL")
-              | Parser.TOKEN_INLINE -> getErrorString("Parser.TOKEN.INLINE")
-              | Parser.TOKEN_WHEN -> getErrorString("Parser.TOKEN.WHEN")
-              | Parser.TOKEN_WHILE -> getErrorString("Parser.TOKEN.WHILE")
-              | Parser.TOKEN_WITH-> getErrorString("Parser.TOKEN.WITH")
-              | Parser.TOKEN_IF -> getErrorString("Parser.TOKEN.IF")
-              | Parser.TOKEN_DO -> getErrorString("Parser.TOKEN.DO")
-              | Parser.TOKEN_GLOBAL -> getErrorString("Parser.TOKEN.GLOBAL")
-              | Parser.TOKEN_DONE -> getErrorString("Parser.TOKEN.DONE")
-              | Parser.TOKEN_IN | Parser.TOKEN_JOIN_IN -> getErrorString("Parser.TOKEN.IN")
-              | Parser.TOKEN_HIGH_PRECEDENCE_PAREN_APP-> getErrorString("Parser.TOKEN.HIGH.PRECEDENCE.PAREN.APP")
-              | Parser.TOKEN_HIGH_PRECEDENCE_BRACK_APP-> getErrorString("Parser.TOKEN.HIGH.PRECEDENCE.BRACK.APP")
-              | Parser.TOKEN_BEGIN -> getErrorString("Parser.TOKEN.BEGIN")
-              | Parser.TOKEN_END -> getErrorString("Parser.TOKEN.END")
-              | Parser.TOKEN_HASH_LIGHT
-              | Parser.TOKEN_HASH_LINE
-              | Parser.TOKEN_HASH_IF
-              | Parser.TOKEN_HASH_ELSE
-              | Parser.TOKEN_HASH_ENDIF -> getErrorString("Parser.TOKEN.HASH.ENDIF")
-              | Parser.TOKEN_INACTIVECODE -> getErrorString("Parser.TOKEN.INACTIVECODE")
-              | Parser.TOKEN_LEX_FAILURE-> getErrorString("Parser.TOKEN.LEX.FAILURE")
-              | Parser.TOKEN_WHITESPACE -> getErrorString("Parser.TOKEN.WHITESPACE")
-              | Parser.TOKEN_COMMENT -> getErrorString("Parser.TOKEN.COMMENT")
-              | Parser.TOKEN_LINE_COMMENT -> getErrorString("Parser.TOKEN.LINE.COMMENT")
-              | Parser.TOKEN_STRING_TEXT -> getErrorString("Parser.TOKEN.STRING.TEXT")
-              | Parser.TOKEN_BYTEARRAY -> getErrorString("Parser.TOKEN.BYTEARRAY")
-              | Parser.TOKEN_STRING -> getErrorString("Parser.TOKEN.STRING")
-              | Parser.TOKEN_KEYWORD_STRING -> getErrorString("Parser.TOKEN.KEYWORD_STRING")
-              | Parser.TOKEN_EOF -> getErrorString("Parser.TOKEN.EOF")
-              | Parser.TOKEN_CONST -> getErrorString("Parser.TOKEN.CONST")
-              | Parser.TOKEN_FIXED -> getErrorString("Parser.TOKEN.FIXED")
-              | Parser.TOKEN_INTERP_STRING_BEGIN_END -> getErrorString("Parser.TOKEN.INTERP.STRING.BEGIN.END")
-              | Parser.TOKEN_INTERP_STRING_BEGIN_PART -> getErrorString("Parser.TOKEN.INTERP.STRING.BEGIN.PART")
-              | Parser.TOKEN_INTERP_STRING_PART -> getErrorString("Parser.TOKEN.INTERP.STRING.PART")
-              | Parser.TOKEN_INTERP_STRING_END -> getErrorString("Parser.TOKEN.INTERP.STRING.END")
-              | unknown ->
-                  Debug.Assert(false, "unknown token tag")
-                  let result = sprintf "%+A" unknown
-                  Debug.Assert(false, result)
-                  result
+        | UnresolvedOverloading (denv, callerArgs, failure, m) ->
+
+            // extract eventual information (return type and type parameters)
+            // from ConstraintTraitInfo
+            let knownReturnType, genericParameterTypes =
+                match failure with
+                | NoOverloadsFound(cx = Some cx)
+                | PossibleCandidates(cx = Some cx) -> cx.ReturnType, cx.ArgumentTypes
+                | _ -> None, []
+
+            // prepare message parts (known arguments, known return type, known generic parameters)
+            let argsMessage, returnType, genericParametersMessage =
+
+                let retTy =
+                    knownReturnType |> Option.defaultValue (TType_var(Typar.NewUnlinked(), 0uy))
+
+                let argRepr =
+                    callerArgs.ArgumentNamesAndTypes
+                    |> List.map (fun (name, tTy) ->
+                        tTy,
+                        {
+                            ArgReprInfo.Name = name |> Option.map (fun name -> Ident(name, range.Zero))
+                            ArgReprInfo.Attribs = []
+                        })
+
+                let argsL, retTyL, genParamTysL =
+                    NicePrint.prettyLayoutsOfUnresolvedOverloading denv argRepr retTy genericParameterTypes
+
+                match callerArgs.ArgumentNamesAndTypes with
+                | [] -> None, LayoutRender.showL retTyL, LayoutRender.showL genParamTysL
+                | items ->
+                    let args = LayoutRender.showL argsL
+
+                    let prefixMessage =
+                        match items with
+                        | [ _ ] -> FSComp.SR.csNoOverloadsFoundArgumentsPrefixSingular
+                        | _ -> FSComp.SR.csNoOverloadsFoundArgumentsPrefixPlural
+
+                    Some(prefixMessage args), LayoutRender.showL retTyL, LayoutRender.showL genParamTysL
+
+            let knownReturnType =
+                match knownReturnType with
+                | None -> None
+                | Some _ -> Some(FSComp.SR.csNoOverloadsFoundReturnType returnType)
+
+            let genericParametersMessage =
+                match genericParameterTypes with
+                | [] -> None
+                | [ _ ] -> Some(FSComp.SR.csNoOverloadsFoundTypeParametersPrefixSingular genericParametersMessage)
+                | _ -> Some(FSComp.SR.csNoOverloadsFoundTypeParametersPrefixPlural genericParametersMessage)
+
+            let overloadMethodInfo displayEnv m (x: OverloadInformation) =
+                let paramInfo =
+                    match x.error with
+                    | :? ArgDoesNotMatchError as x ->
+                        let nameOrOneBasedIndexMessage =
+                            x.calledArg.NameOpt
+                            |> Option.map (fun n -> FSComp.SR.csOverloadCandidateNamedArgumentTypeMismatch n.idText)
+                            |> Option.defaultValue (
+                                FSComp.SR.csOverloadCandidateIndexedArgumentTypeMismatch ((vsnd x.calledArg.Position) + 1)
+                            ) //snd
+
+                        sprintf " // %s" nameOrOneBasedIndexMessage
+                    | _ -> ""
+
+                (NicePrint.stringOfMethInfo x.infoReader m displayEnv x.methodSlot.Method)
+                + paramInfo
+
+            let nl = Environment.NewLine
+
+            let formatOverloads (overloads: OverloadInformation list) =
+                overloads
+                |> List.map (overloadMethodInfo denv m)
+                |> List.sort
+                |> List.map FSComp.SR.formatDashItem
+                |> String.concat nl
+
+            // assemble final message composing the parts
+            let msg =
+                let optionalParts =
+                    [ knownReturnType; genericParametersMessage; argsMessage ]
+                    |> List.choose id
+                    |> String.concat (nl + nl)
+                    |> function
+                        | "" -> nl
+                        | result -> nl + nl + result + nl + nl
+
+                match failure with
+                | NoOverloadsFound (methodName, overloads, _) ->
+                    FSComp.SR.csNoOverloadsFound methodName
+                    + optionalParts
+                    + (FSComp.SR.csAvailableOverloads (formatOverloads overloads))
+                | PossibleCandidates (methodName, [], _) -> FSComp.SR.csMethodIsOverloaded methodName
+                | PossibleCandidates (methodName, overloads, _) ->
+                    FSComp.SR.csMethodIsOverloaded methodName
+                    + optionalParts
+                    + FSComp.SR.csCandidates (formatOverloads overloads)
+
+            os.AppendString msg
+
+        | UnresolvedConversionOperator (denv, fromTy, toTy, _) ->
+            let ty1, ty2, _tpcs = NicePrint.minimalStringsOfTwoTypes denv fromTy toTy
+            os.AppendString(FSComp.SR.csTypeDoesNotSupportConversion (ty1, ty2))
+
+        | FunctionExpected _ -> os.AppendString(FunctionExpectedE().Format)
+
+        | BakedInMemberConstraintName (nm, _) -> os.AppendString(BakedInMemberConstraintNameE().Format nm)
+
+        | StandardOperatorRedefinitionWarning (msg, _) -> os.AppendString msg
+
+        | BadEventTransformation _ -> os.AppendString(BadEventTransformationE().Format)
+
+        | ParameterlessStructCtor _ -> os.AppendString(ParameterlessStructCtorE().Format)
+
+        | InterfaceNotRevealed (denv, ity, _) -> os.AppendString(InterfaceNotRevealedE().Format(NicePrint.minimalStringOfType denv ity))
+
+        | NotAFunctionButIndexer (_, _, name, _, _, old) ->
+            if old then
+                match name with
+                | Some name -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexerWithName name)
+                | _ -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexer ())
+            else
+                match name with
+                | Some name -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexerWithName2 name)
+                | _ -> os.AppendString(FSComp.SR.notAFunctionButMaybeIndexer2 ())
+
+        | NotAFunction (_, _, _, marg) ->
+            if marg.StartColumn = 0 then
+                os.AppendString(FSComp.SR.notAFunctionButMaybeDeclaration ())
+            else
+                os.AppendString(FSComp.SR.notAFunction ())
+
+        | TyconBadArgs (_, tcref, d, _) ->
+            let exp = tcref.TyparsNoRange.Length
+
+            if exp = 0 then
+                os.AppendString(FSComp.SR.buildUnexpectedTypeArgs (fullDisplayTextOfTyconRef tcref, d))
+            else
+                os.AppendString(TyconBadArgsE().Format (fullDisplayTextOfTyconRef tcref) exp d)
+
+        | IndeterminateType _ -> os.AppendString(IndeterminateTypeE().Format)
+
+        | NameClash (nm, k1, nm1, _, k2, nm2, _) ->
+            if nm = nm1 && nm1 = nm2 && k1 = k2 then
+                os.AppendString(NameClash1E().Format k1 nm1)
+            else
+                os.AppendString(NameClash2E().Format k1 nm1 nm k2 nm2)
+
+        | Duplicate (k, s, _) ->
+            if k = "member" then
+                os.AppendString(Duplicate1E().Format(DecompileOpName s))
+            else
+                os.AppendString(Duplicate2E().Format k (DecompileOpName s))
+
+        | UndefinedName (_, k, id, suggestionsF) ->
+            os.AppendString(k (DecompileOpName id.idText))
+            suggestNames suggestionsF id.idText
+
+        | InternalUndefinedItemRef (f, smr, ccuName, s) ->
+            let _, errs = f (smr, ccuName, s)
+            os.AppendString errs
+
+        | FieldNotMutable _ -> os.AppendString(FieldNotMutableE().Format)
+
+        | FieldsFromDifferentTypes (_, fref1, fref2, _) ->
+            os.AppendString(FieldsFromDifferentTypesE().Format fref1.FieldName fref2.FieldName)
+
+        | VarBoundTwice id -> os.AppendString(VarBoundTwiceE().Format(DecompileOpName id.idText))
+
+        | Recursion (denv, id, ty1, ty2, _) ->
+            let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(RecursionE().Format (DecompileOpName id.idText) ty1 ty2 tpcs)
+
+        | InvalidRuntimeCoercion (denv, ty1, ty2, _) ->
+            let ty1, ty2, tpcs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(InvalidRuntimeCoercionE().Format ty1 ty2 tpcs)
+
+        | IndeterminateRuntimeCoercion (denv, ty1, ty2, _) ->
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(IndeterminateRuntimeCoercionE().Format ty1 ty2)
+
+        | IndeterminateStaticCoercion (denv, ty1, ty2, _) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(IndeterminateStaticCoercionE().Format ty1 ty2)
+
+        | StaticCoercionShouldUseBox (denv, ty1, ty2, _) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(StaticCoercionShouldUseBoxE().Format ty1 ty2)
+
+        | TypeIsImplicitlyAbstract _ -> os.AppendString(TypeIsImplicitlyAbstractE().Format)
+
+        | NonRigidTypar (denv, tpnmOpt, typarRange, ty1, ty2, _) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let (ty1, ty2), _cxs = PrettyTypes.PrettifyTypePair denv.g (ty1, ty2)
+
+            match tpnmOpt with
+            | None -> os.AppendString(NonRigidTypar1E().Format (stringOfRange typarRange) (NicePrint.stringOfTy denv ty2))
+            | Some tpnm ->
+                match ty1 with
+                | TType_measure _ -> os.AppendString(NonRigidTypar2E().Format tpnm (NicePrint.stringOfTy denv ty2))
+                | _ -> os.AppendString(NonRigidTypar3E().Format tpnm (NicePrint.stringOfTy denv ty2))
+
+        | SyntaxError (ctxt, _) ->
+            let ctxt = unbox> (ctxt)
+
+            let (|EndOfStructuredConstructToken|_|) token =
+                match token with
+                | Parser.TOKEN_ODECLEND
+                | Parser.TOKEN_OBLOCKSEP
+                | Parser.TOKEN_OEND
+                | Parser.TOKEN_ORIGHT_BLOCK_END
+                | Parser.TOKEN_OBLOCKEND
+                | Parser.TOKEN_OBLOCKEND_COMING_SOON
+                | Parser.TOKEN_OBLOCKEND_IS_HERE -> Some()
+                | _ -> None
+
+            let tokenIdToText tid =
+                match tid with
+                | Parser.TOKEN_IDENT -> getErrorString ("Parser.TOKEN.IDENT")
+                | Parser.TOKEN_BIGNUM
+                | Parser.TOKEN_INT8
+                | Parser.TOKEN_UINT8
+                | Parser.TOKEN_INT16
+                | Parser.TOKEN_UINT16
+                | Parser.TOKEN_INT32
+                | Parser.TOKEN_UINT32
+                | Parser.TOKEN_INT64
+                | Parser.TOKEN_UINT64
+                | Parser.TOKEN_UNATIVEINT
+                | Parser.TOKEN_NATIVEINT -> getErrorString ("Parser.TOKEN.INT")
+                | Parser.TOKEN_IEEE32
+                | Parser.TOKEN_IEEE64 -> getErrorString ("Parser.TOKEN.FLOAT")
+                | Parser.TOKEN_DECIMAL -> getErrorString ("Parser.TOKEN.DECIMAL")
+                | Parser.TOKEN_CHAR -> getErrorString ("Parser.TOKEN.CHAR")
+
+                | Parser.TOKEN_BASE -> getErrorString ("Parser.TOKEN.BASE")
+                | Parser.TOKEN_LPAREN_STAR_RPAREN -> getErrorString ("Parser.TOKEN.LPAREN.STAR.RPAREN")
+                | Parser.TOKEN_DOLLAR -> getErrorString ("Parser.TOKEN.DOLLAR")
+                | Parser.TOKEN_INFIX_STAR_STAR_OP -> getErrorString ("Parser.TOKEN.INFIX.STAR.STAR.OP")
+                | Parser.TOKEN_INFIX_COMPARE_OP -> getErrorString ("Parser.TOKEN.INFIX.COMPARE.OP")
+                | Parser.TOKEN_COLON_GREATER -> getErrorString ("Parser.TOKEN.COLON.GREATER")
+                | Parser.TOKEN_COLON_COLON -> getErrorString ("Parser.TOKEN.COLON.COLON")
+                | Parser.TOKEN_PERCENT_OP -> getErrorString ("Parser.TOKEN.PERCENT.OP")
+                | Parser.TOKEN_INFIX_AT_HAT_OP -> getErrorString ("Parser.TOKEN.INFIX.AT.HAT.OP")
+                | Parser.TOKEN_INFIX_BAR_OP -> getErrorString ("Parser.TOKEN.INFIX.BAR.OP")
+                | Parser.TOKEN_PLUS_MINUS_OP -> getErrorString ("Parser.TOKEN.PLUS.MINUS.OP")
+                | Parser.TOKEN_PREFIX_OP -> getErrorString ("Parser.TOKEN.PREFIX.OP")
+                | Parser.TOKEN_COLON_QMARK_GREATER -> getErrorString ("Parser.TOKEN.COLON.QMARK.GREATER")
+                | Parser.TOKEN_INFIX_STAR_DIV_MOD_OP -> getErrorString ("Parser.TOKEN.INFIX.STAR.DIV.MOD.OP")
+                | Parser.TOKEN_INFIX_AMP_OP -> getErrorString ("Parser.TOKEN.INFIX.AMP.OP")
+                | Parser.TOKEN_AMP -> getErrorString ("Parser.TOKEN.AMP")
+                | Parser.TOKEN_AMP_AMP -> getErrorString ("Parser.TOKEN.AMP.AMP")
+                | Parser.TOKEN_BAR_BAR -> getErrorString ("Parser.TOKEN.BAR.BAR")
+                | Parser.TOKEN_LESS -> getErrorString ("Parser.TOKEN.LESS")
+                | Parser.TOKEN_GREATER -> getErrorString ("Parser.TOKEN.GREATER")
+                | Parser.TOKEN_QMARK -> getErrorString ("Parser.TOKEN.QMARK")
+                | Parser.TOKEN_QMARK_QMARK -> getErrorString ("Parser.TOKEN.QMARK.QMARK")
+                | Parser.TOKEN_COLON_QMARK -> getErrorString ("Parser.TOKEN.COLON.QMARK")
+                | Parser.TOKEN_INT32_DOT_DOT -> getErrorString ("Parser.TOKEN.INT32.DOT.DOT")
+                | Parser.TOKEN_DOT_DOT -> getErrorString ("Parser.TOKEN.DOT.DOT")
+                | Parser.TOKEN_DOT_DOT_HAT -> getErrorString ("Parser.TOKEN.DOT.DOT")
+                | Parser.TOKEN_QUOTE -> getErrorString ("Parser.TOKEN.QUOTE")
+                | Parser.TOKEN_STAR -> getErrorString ("Parser.TOKEN.STAR")
+                | Parser.TOKEN_HIGH_PRECEDENCE_TYAPP -> getErrorString ("Parser.TOKEN.HIGH.PRECEDENCE.TYAPP")
+                | Parser.TOKEN_COLON -> getErrorString ("Parser.TOKEN.COLON")
+                | Parser.TOKEN_COLON_EQUALS -> getErrorString ("Parser.TOKEN.COLON.EQUALS")
+                | Parser.TOKEN_LARROW -> getErrorString ("Parser.TOKEN.LARROW")
+                | Parser.TOKEN_EQUALS -> getErrorString ("Parser.TOKEN.EQUALS")
+                | Parser.TOKEN_GREATER_BAR_RBRACK -> getErrorString ("Parser.TOKEN.GREATER.BAR.RBRACK")
+                | Parser.TOKEN_MINUS -> getErrorString ("Parser.TOKEN.MINUS")
+                | Parser.TOKEN_ADJACENT_PREFIX_OP -> getErrorString ("Parser.TOKEN.ADJACENT.PREFIX.OP")
+                | Parser.TOKEN_FUNKY_OPERATOR_NAME -> getErrorString ("Parser.TOKEN.FUNKY.OPERATOR.NAME")
+                | Parser.TOKEN_COMMA -> getErrorString ("Parser.TOKEN.COMMA")
+                | Parser.TOKEN_DOT -> getErrorString ("Parser.TOKEN.DOT")
+                | Parser.TOKEN_BAR -> getErrorString ("Parser.TOKEN.BAR")
+                | Parser.TOKEN_HASH -> getErrorString ("Parser.TOKEN.HASH")
+                | Parser.TOKEN_UNDERSCORE -> getErrorString ("Parser.TOKEN.UNDERSCORE")
+                | Parser.TOKEN_SEMICOLON -> getErrorString ("Parser.TOKEN.SEMICOLON")
+                | Parser.TOKEN_SEMICOLON_SEMICOLON -> getErrorString ("Parser.TOKEN.SEMICOLON.SEMICOLON")
+                | Parser.TOKEN_LPAREN -> getErrorString ("Parser.TOKEN.LPAREN")
+                | Parser.TOKEN_RPAREN
+                | Parser.TOKEN_RPAREN_COMING_SOON
+                | Parser.TOKEN_RPAREN_IS_HERE -> getErrorString ("Parser.TOKEN.RPAREN")
+                | Parser.TOKEN_LQUOTE -> getErrorString ("Parser.TOKEN.LQUOTE")
+                | Parser.TOKEN_LBRACK -> getErrorString ("Parser.TOKEN.LBRACK")
+                | Parser.TOKEN_LBRACE_BAR -> getErrorString ("Parser.TOKEN.LBRACE.BAR")
+                | Parser.TOKEN_LBRACK_BAR -> getErrorString ("Parser.TOKEN.LBRACK.BAR")
+                | Parser.TOKEN_LBRACK_LESS -> getErrorString ("Parser.TOKEN.LBRACK.LESS")
+                | Parser.TOKEN_LBRACE -> getErrorString ("Parser.TOKEN.LBRACE")
+                | Parser.TOKEN_BAR_RBRACK -> getErrorString ("Parser.TOKEN.BAR.RBRACK")
+                | Parser.TOKEN_BAR_RBRACE -> getErrorString ("Parser.TOKEN.BAR.RBRACE")
+                | Parser.TOKEN_GREATER_RBRACK -> getErrorString ("Parser.TOKEN.GREATER.RBRACK")
+                | Parser.TOKEN_RQUOTE_DOT _
+                | Parser.TOKEN_RQUOTE -> getErrorString ("Parser.TOKEN.RQUOTE")
+                | Parser.TOKEN_RBRACK -> getErrorString ("Parser.TOKEN.RBRACK")
+                | Parser.TOKEN_RBRACE
+                | Parser.TOKEN_RBRACE_COMING_SOON
+                | Parser.TOKEN_RBRACE_IS_HERE -> getErrorString ("Parser.TOKEN.RBRACE")
+                | Parser.TOKEN_PUBLIC -> getErrorString ("Parser.TOKEN.PUBLIC")
+                | Parser.TOKEN_PRIVATE -> getErrorString ("Parser.TOKEN.PRIVATE")
+                | Parser.TOKEN_INTERNAL -> getErrorString ("Parser.TOKEN.INTERNAL")
+                | Parser.TOKEN_CONSTRAINT -> getErrorString ("Parser.TOKEN.CONSTRAINT")
+                | Parser.TOKEN_INSTANCE -> getErrorString ("Parser.TOKEN.INSTANCE")
+                | Parser.TOKEN_DELEGATE -> getErrorString ("Parser.TOKEN.DELEGATE")
+                | Parser.TOKEN_INHERIT -> getErrorString ("Parser.TOKEN.INHERIT")
+                | Parser.TOKEN_CONSTRUCTOR -> getErrorString ("Parser.TOKEN.CONSTRUCTOR")
+                | Parser.TOKEN_DEFAULT -> getErrorString ("Parser.TOKEN.DEFAULT")
+                | Parser.TOKEN_OVERRIDE -> getErrorString ("Parser.TOKEN.OVERRIDE")
+                | Parser.TOKEN_ABSTRACT -> getErrorString ("Parser.TOKEN.ABSTRACT")
+                | Parser.TOKEN_CLASS -> getErrorString ("Parser.TOKEN.CLASS")
+                | Parser.TOKEN_MEMBER -> getErrorString ("Parser.TOKEN.MEMBER")
+                | Parser.TOKEN_STATIC -> getErrorString ("Parser.TOKEN.STATIC")
+                | Parser.TOKEN_NAMESPACE -> getErrorString ("Parser.TOKEN.NAMESPACE")
+                | Parser.TOKEN_OBLOCKBEGIN -> getErrorString ("Parser.TOKEN.OBLOCKBEGIN")
+                | EndOfStructuredConstructToken -> getErrorString ("Parser.TOKEN.OBLOCKEND")
+                | Parser.TOKEN_THEN
+                | Parser.TOKEN_OTHEN -> getErrorString ("Parser.TOKEN.OTHEN")
+                | Parser.TOKEN_ELSE
+                | Parser.TOKEN_OELSE -> getErrorString ("Parser.TOKEN.OELSE")
+                | Parser.TOKEN_LET _
+                | Parser.TOKEN_OLET _ -> getErrorString ("Parser.TOKEN.OLET")
+                | Parser.TOKEN_OBINDER
+                | Parser.TOKEN_BINDER -> getErrorString ("Parser.TOKEN.BINDER")
+                | Parser.TOKEN_OAND_BANG
+                | Parser.TOKEN_AND_BANG -> getErrorString ("Parser.TOKEN.AND.BANG")
+                | Parser.TOKEN_ODO -> getErrorString ("Parser.TOKEN.ODO")
+                | Parser.TOKEN_OWITH -> getErrorString ("Parser.TOKEN.OWITH")
+                | Parser.TOKEN_OFUNCTION -> getErrorString ("Parser.TOKEN.OFUNCTION")
+                | Parser.TOKEN_OFUN -> getErrorString ("Parser.TOKEN.OFUN")
+                | Parser.TOKEN_ORESET -> getErrorString ("Parser.TOKEN.ORESET")
+                | Parser.TOKEN_ODUMMY -> getErrorString ("Parser.TOKEN.ODUMMY")
+                | Parser.TOKEN_DO_BANG
+                | Parser.TOKEN_ODO_BANG -> getErrorString ("Parser.TOKEN.ODO.BANG")
+                | Parser.TOKEN_YIELD -> getErrorString ("Parser.TOKEN.YIELD")
+                | Parser.TOKEN_YIELD_BANG -> getErrorString ("Parser.TOKEN.YIELD.BANG")
+                | Parser.TOKEN_OINTERFACE_MEMBER -> getErrorString ("Parser.TOKEN.OINTERFACE.MEMBER")
+                | Parser.TOKEN_ELIF -> getErrorString ("Parser.TOKEN.ELIF")
+                | Parser.TOKEN_RARROW -> getErrorString ("Parser.TOKEN.RARROW")
+                | Parser.TOKEN_SIG -> getErrorString ("Parser.TOKEN.SIG")
+                | Parser.TOKEN_STRUCT -> getErrorString ("Parser.TOKEN.STRUCT")
+                | Parser.TOKEN_UPCAST -> getErrorString ("Parser.TOKEN.UPCAST")
+                | Parser.TOKEN_DOWNCAST -> getErrorString ("Parser.TOKEN.DOWNCAST")
+                | Parser.TOKEN_NULL -> getErrorString ("Parser.TOKEN.NULL")
+                | Parser.TOKEN_RESERVED -> getErrorString ("Parser.TOKEN.RESERVED")
+                | Parser.TOKEN_MODULE
+                | Parser.TOKEN_MODULE_COMING_SOON
+                | Parser.TOKEN_MODULE_IS_HERE -> getErrorString ("Parser.TOKEN.MODULE")
+                | Parser.TOKEN_AND -> getErrorString ("Parser.TOKEN.AND")
+                | Parser.TOKEN_AS -> getErrorString ("Parser.TOKEN.AS")
+                | Parser.TOKEN_ASSERT -> getErrorString ("Parser.TOKEN.ASSERT")
+                | Parser.TOKEN_OASSERT -> getErrorString ("Parser.TOKEN.ASSERT")
+                | Parser.TOKEN_ASR -> getErrorString ("Parser.TOKEN.ASR")
+                | Parser.TOKEN_DOWNTO -> getErrorString ("Parser.TOKEN.DOWNTO")
+                | Parser.TOKEN_EXCEPTION -> getErrorString ("Parser.TOKEN.EXCEPTION")
+                | Parser.TOKEN_FALSE -> getErrorString ("Parser.TOKEN.FALSE")
+                | Parser.TOKEN_FOR -> getErrorString ("Parser.TOKEN.FOR")
+                | Parser.TOKEN_FUN -> getErrorString ("Parser.TOKEN.FUN")
+                | Parser.TOKEN_FUNCTION -> getErrorString ("Parser.TOKEN.FUNCTION")
+                | Parser.TOKEN_FINALLY -> getErrorString ("Parser.TOKEN.FINALLY")
+                | Parser.TOKEN_LAZY -> getErrorString ("Parser.TOKEN.LAZY")
+                | Parser.TOKEN_OLAZY -> getErrorString ("Parser.TOKEN.LAZY")
+                | Parser.TOKEN_MATCH -> getErrorString ("Parser.TOKEN.MATCH")
+                | Parser.TOKEN_MATCH_BANG -> getErrorString ("Parser.TOKEN.MATCH.BANG")
+                | Parser.TOKEN_MUTABLE -> getErrorString ("Parser.TOKEN.MUTABLE")
+                | Parser.TOKEN_NEW -> getErrorString ("Parser.TOKEN.NEW")
+                | Parser.TOKEN_OF -> getErrorString ("Parser.TOKEN.OF")
+                | Parser.TOKEN_OPEN -> getErrorString ("Parser.TOKEN.OPEN")
+                | Parser.TOKEN_OR -> getErrorString ("Parser.TOKEN.OR")
+                | Parser.TOKEN_VOID -> getErrorString ("Parser.TOKEN.VOID")
+                | Parser.TOKEN_EXTERN -> getErrorString ("Parser.TOKEN.EXTERN")
+                | Parser.TOKEN_INTERFACE -> getErrorString ("Parser.TOKEN.INTERFACE")
+                | Parser.TOKEN_REC -> getErrorString ("Parser.TOKEN.REC")
+                | Parser.TOKEN_TO -> getErrorString ("Parser.TOKEN.TO")
+                | Parser.TOKEN_TRUE -> getErrorString ("Parser.TOKEN.TRUE")
+                | Parser.TOKEN_TRY -> getErrorString ("Parser.TOKEN.TRY")
+                | Parser.TOKEN_TYPE
+                | Parser.TOKEN_TYPE_COMING_SOON
+                | Parser.TOKEN_TYPE_IS_HERE -> getErrorString ("Parser.TOKEN.TYPE")
+                | Parser.TOKEN_VAL -> getErrorString ("Parser.TOKEN.VAL")
+                | Parser.TOKEN_INLINE -> getErrorString ("Parser.TOKEN.INLINE")
+                | Parser.TOKEN_WHEN -> getErrorString ("Parser.TOKEN.WHEN")
+                | Parser.TOKEN_WHILE -> getErrorString ("Parser.TOKEN.WHILE")
+                | Parser.TOKEN_WITH -> getErrorString ("Parser.TOKEN.WITH")
+                | Parser.TOKEN_IF -> getErrorString ("Parser.TOKEN.IF")
+                | Parser.TOKEN_DO -> getErrorString ("Parser.TOKEN.DO")
+                | Parser.TOKEN_GLOBAL -> getErrorString ("Parser.TOKEN.GLOBAL")
+                | Parser.TOKEN_DONE -> getErrorString ("Parser.TOKEN.DONE")
+                | Parser.TOKEN_IN
+                | Parser.TOKEN_JOIN_IN -> getErrorString ("Parser.TOKEN.IN")
+                | Parser.TOKEN_HIGH_PRECEDENCE_PAREN_APP -> getErrorString ("Parser.TOKEN.HIGH.PRECEDENCE.PAREN.APP")
+                | Parser.TOKEN_HIGH_PRECEDENCE_BRACK_APP -> getErrorString ("Parser.TOKEN.HIGH.PRECEDENCE.BRACK.APP")
+                | Parser.TOKEN_BEGIN -> getErrorString ("Parser.TOKEN.BEGIN")
+                | Parser.TOKEN_END -> getErrorString ("Parser.TOKEN.END")
+                | Parser.TOKEN_HASH_LIGHT
+                | Parser.TOKEN_HASH_LINE
+                | Parser.TOKEN_HASH_IF
+                | Parser.TOKEN_HASH_ELSE
+                | Parser.TOKEN_HASH_ENDIF -> getErrorString ("Parser.TOKEN.HASH.ENDIF")
+                | Parser.TOKEN_INACTIVECODE -> getErrorString ("Parser.TOKEN.INACTIVECODE")
+                | Parser.TOKEN_LEX_FAILURE -> getErrorString ("Parser.TOKEN.LEX.FAILURE")
+                | Parser.TOKEN_WHITESPACE -> getErrorString ("Parser.TOKEN.WHITESPACE")
+                | Parser.TOKEN_COMMENT -> getErrorString ("Parser.TOKEN.COMMENT")
+                | Parser.TOKEN_LINE_COMMENT -> getErrorString ("Parser.TOKEN.LINE.COMMENT")
+                | Parser.TOKEN_STRING_TEXT -> getErrorString ("Parser.TOKEN.STRING.TEXT")
+                | Parser.TOKEN_BYTEARRAY -> getErrorString ("Parser.TOKEN.BYTEARRAY")
+                | Parser.TOKEN_STRING -> getErrorString ("Parser.TOKEN.STRING")
+                | Parser.TOKEN_KEYWORD_STRING -> getErrorString ("Parser.TOKEN.KEYWORD_STRING")
+                | Parser.TOKEN_EOF -> getErrorString ("Parser.TOKEN.EOF")
+                | Parser.TOKEN_CONST -> getErrorString ("Parser.TOKEN.CONST")
+                | Parser.TOKEN_FIXED -> getErrorString ("Parser.TOKEN.FIXED")
+                | Parser.TOKEN_INTERP_STRING_BEGIN_END -> getErrorString ("Parser.TOKEN.INTERP.STRING.BEGIN.END")
+                | Parser.TOKEN_INTERP_STRING_BEGIN_PART -> getErrorString ("Parser.TOKEN.INTERP.STRING.BEGIN.PART")
+                | Parser.TOKEN_INTERP_STRING_PART -> getErrorString ("Parser.TOKEN.INTERP.STRING.PART")
+                | Parser.TOKEN_INTERP_STRING_END -> getErrorString ("Parser.TOKEN.INTERP.STRING.END")
+                | unknown ->
+                    Debug.Assert(false, "unknown token tag")
+                    let result = sprintf "%+A" unknown
+                    Debug.Assert(false, result)
+                    result
 
 #if DEBUG
-          if showParserStackOnParseError then
-              printfn "parser stack:"
-              for rps in ctxt.ReducibleProductions do
-                  printfn "   ----"
-                  //printfn "   state %d" state
-                  for rp in rps do
-                      printfn "       non-terminal %+A (idx %d): ... " (Parser.prodIdxToNonTerminal rp) rp
+            if showParserStackOnParseError then
+                printfn "parser stack:"
+
+                for rps in ctxt.ReducibleProductions do
+                    printfn "   ----"
+                    //printfn "   state %d" state
+                    for rp in rps do
+                        printfn "       non-terminal %+A (idx %d): ... " (Parser.prodIdxToNonTerminal rp) rp
 #endif
 
-          match ctxt.CurrentToken with
-          | None -> os.AppendString(UnexpectedEndOfInputE().Format)
-          | Some token ->
-              let tokenId = token |> Parser.tagOfToken |> Parser.tokenTagToTokenId
-
-              match tokenId, token with
-              | EndOfStructuredConstructToken, _ -> os.AppendString(OBlockEndSentenceE().Format)
-              | Parser.TOKEN_LEX_FAILURE, Parser.LEX_FAILURE str -> os.AppendString str
-              | token, _ -> os.AppendString(UnexpectedE().Format (token |> tokenIdToText))
-
-              // Search for a state producing a single recognized non-terminal in the states on the stack
-              let foundInContext =
-
-                  // Merge a bunch of expression non terminals
-                  let (|NONTERM_Category_Expr|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_argExpr|Parser.NONTERM_minusExpr|Parser.NONTERM_parenExpr|Parser.NONTERM_atomicExpr
-                      | Parser.NONTERM_appExpr|Parser.NONTERM_tupleExpr|Parser.NONTERM_declExpr|Parser.NONTERM_braceExpr|Parser.NONTERM_braceBarExpr
-                      | Parser.NONTERM_typedSequentialExprBlock
-                      | Parser.NONTERM_interactiveExpr -> Some()
-                      | _ -> None
-
-                  // Merge a bunch of pattern non terminals
-                  let (|NONTERM_Category_Pattern|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_constrPattern|Parser.NONTERM_parenPattern|Parser.NONTERM_atomicPattern -> Some()
-                      | _ -> None
-
-                  // Merge a bunch of if/then/else non terminals
-                  let (|NONTERM_Category_IfThenElse|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_ifExprThen|Parser.NONTERM_ifExprElifs|Parser.NONTERM_ifExprCases -> Some()
-                      | _ -> None
-
-                  // Merge a bunch of non terminals
-                  let (|NONTERM_Category_SignatureFile|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_signatureFile|Parser.NONTERM_moduleSpfn|Parser.NONTERM_moduleSpfns -> Some()
-                      | _ -> None
-
-                  let (|NONTERM_Category_ImplementationFile|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_implementationFile|Parser.NONTERM_fileNamespaceImpl|Parser.NONTERM_fileNamespaceImpls -> Some()
-                      | _ -> None
-
-                  let (|NONTERM_Category_Definition|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_fileModuleImpl|Parser.NONTERM_moduleDefn|Parser.NONTERM_interactiveDefns
-                      | Parser.NONTERM_moduleDefns|Parser.NONTERM_moduleDefnsOrExpr -> Some()
-                      | _ -> None
-
-                  let (|NONTERM_Category_Type|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_typ|Parser.NONTERM_tupleType -> Some()
-                      | _ -> None
-
-                  let (|NONTERM_Category_Interaction|_|) nonTerminal =
-                      match nonTerminal with
-                      | Parser.NONTERM_interactiveItemsTerminator|Parser.NONTERM_interaction|Parser.NONTERM__startinteraction -> Some()
-                      | _ -> None
-
-                  // Canonicalize the categories and check for a unique category
-                  ctxt.ReducibleProductions |> List.exists (fun prods ->
-                      let prodIds =
-                          prods
-                          |> List.map Parser.prodIdxToNonTerminal
-                          |> List.map (fun nonTerminal ->
-                              match nonTerminal with
-                              | NONTERM_Category_Type -> Parser.NONTERM_typ
-                              | NONTERM_Category_Expr -> Parser.NONTERM_declExpr
-                              | NONTERM_Category_Pattern -> Parser.NONTERM_atomicPattern
-                              | NONTERM_Category_IfThenElse -> Parser.NONTERM_ifExprThen
-                              | NONTERM_Category_SignatureFile -> Parser.NONTERM_signatureFile
-                              | NONTERM_Category_ImplementationFile -> Parser.NONTERM_implementationFile
-                              | NONTERM_Category_Definition -> Parser.NONTERM_moduleDefn
-                              | NONTERM_Category_Interaction -> Parser.NONTERM_interaction
-                              | nt -> nt)
-                          |> Set.ofList
-                          |> Set.toList
-                      match prodIds with
-                      | [Parser.NONTERM_interaction] -> os.AppendString(NONTERM_interactionE().Format); true
-                      | [Parser.NONTERM_hashDirective] -> os.AppendString(NONTERM_hashDirectiveE().Format); true
-                      | [Parser.NONTERM_fieldDecl] -> os.AppendString(NONTERM_fieldDeclE().Format); true
-                      | [Parser.NONTERM_unionCaseRepr] -> os.AppendString(NONTERM_unionCaseReprE().Format); true
-                      | [Parser.NONTERM_localBinding] -> os.AppendString(NONTERM_localBindingE().Format); true
-                      | [Parser.NONTERM_hardwhiteLetBindings] -> os.AppendString(NONTERM_hardwhiteLetBindingsE().Format); true
-                      | [Parser.NONTERM_classDefnMember] -> os.AppendString(NONTERM_classDefnMemberE().Format); true
-                      | [Parser.NONTERM_defnBindings] -> os.AppendString(NONTERM_defnBindingsE().Format); true
-                      | [Parser.NONTERM_classMemberSpfn] -> os.AppendString(NONTERM_classMemberSpfnE().Format); true
-                      | [Parser.NONTERM_valSpfn] -> os.AppendString(NONTERM_valSpfnE().Format); true
-                      | [Parser.NONTERM_tyconSpfn] -> os.AppendString(NONTERM_tyconSpfnE().Format); true
-                      | [Parser.NONTERM_anonLambdaExpr] -> os.AppendString(NONTERM_anonLambdaExprE().Format); true
-                      | [Parser.NONTERM_attrUnionCaseDecl] -> os.AppendString(NONTERM_attrUnionCaseDeclE().Format); true
-                      | [Parser.NONTERM_cPrototype] -> os.AppendString(NONTERM_cPrototypeE().Format); true
-                      | [Parser.NONTERM_objExpr|Parser.NONTERM_objectImplementationMembers] -> os.AppendString(NONTERM_objectImplementationMembersE().Format); true
-                      | [Parser.NONTERM_ifExprThen|Parser.NONTERM_ifExprElifs|Parser.NONTERM_ifExprCases] -> os.AppendString(NONTERM_ifExprCasesE().Format); true
-                      | [Parser.NONTERM_openDecl] -> os.AppendString(NONTERM_openDeclE().Format); true
-                      | [Parser.NONTERM_fileModuleSpec] -> os.AppendString(NONTERM_fileModuleSpecE().Format); true
-                      | [Parser.NONTERM_patternClauses] -> os.AppendString(NONTERM_patternClausesE().Format); true
-                      | [Parser.NONTERM_beginEndExpr] -> os.AppendString(NONTERM_beginEndExprE().Format); true
-                      | [Parser.NONTERM_recdExpr] -> os.AppendString(NONTERM_recdExprE().Format); true
-                      | [Parser.NONTERM_tyconDefn] -> os.AppendString(NONTERM_tyconDefnE().Format); true
-                      | [Parser.NONTERM_exconCore] -> os.AppendString(NONTERM_exconCoreE().Format); true
-                      | [Parser.NONTERM_typeNameInfo] -> os.AppendString(NONTERM_typeNameInfoE().Format); true
-                      | [Parser.NONTERM_attributeList] -> os.AppendString(NONTERM_attributeListE().Format); true
-                      | [Parser.NONTERM_quoteExpr] -> os.AppendString(NONTERM_quoteExprE().Format); true
-                      | [Parser.NONTERM_typeConstraint] -> os.AppendString(NONTERM_typeConstraintE().Format); true
-                      | [NONTERM_Category_ImplementationFile] -> os.AppendString(NONTERM_Category_ImplementationFileE().Format); true
-                      | [NONTERM_Category_Definition] -> os.AppendString(NONTERM_Category_DefinitionE().Format); true
-                      | [NONTERM_Category_SignatureFile] -> os.AppendString(NONTERM_Category_SignatureFileE().Format); true
-                      | [NONTERM_Category_Pattern] -> os.AppendString(NONTERM_Category_PatternE().Format); true
-                      | [NONTERM_Category_Expr] -> os.AppendString(NONTERM_Category_ExprE().Format); true
-                      | [NONTERM_Category_Type] -> os.AppendString(NONTERM_Category_TypeE().Format); true
-                      | [Parser.NONTERM_typeArgsActual] -> os.AppendString(NONTERM_typeArgsActualE().Format); true
-                      | _ ->
-                          false)
+            match ctxt.CurrentToken with
+            | None -> os.AppendString(UnexpectedEndOfInputE().Format)
+            | Some token ->
+                let tokenId = token |> Parser.tagOfToken |> Parser.tokenTagToTokenId
+
+                match tokenId, token with
+                | EndOfStructuredConstructToken, _ -> os.AppendString(OBlockEndSentenceE().Format)
+                | Parser.TOKEN_LEX_FAILURE, Parser.LEX_FAILURE str -> os.AppendString str
+                | token, _ -> os.AppendString(UnexpectedE().Format(token |> tokenIdToText))
+
+                // Search for a state producing a single recognized non-terminal in the states on the stack
+                let foundInContext =
+
+                    // Merge a bunch of expression non terminals
+                    let (|NONTERM_Category_Expr|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_argExpr
+                        | Parser.NONTERM_minusExpr
+                        | Parser.NONTERM_parenExpr
+                        | Parser.NONTERM_atomicExpr
+                        | Parser.NONTERM_appExpr
+                        | Parser.NONTERM_tupleExpr
+                        | Parser.NONTERM_declExpr
+                        | Parser.NONTERM_braceExpr
+                        | Parser.NONTERM_braceBarExpr
+                        | Parser.NONTERM_typedSequentialExprBlock
+                        | Parser.NONTERM_interactiveExpr -> Some()
+                        | _ -> None
+
+                    // Merge a bunch of pattern non terminals
+                    let (|NONTERM_Category_Pattern|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_constrPattern
+                        | Parser.NONTERM_parenPattern
+                        | Parser.NONTERM_atomicPattern -> Some()
+                        | _ -> None
+
+                    // Merge a bunch of if/then/else non terminals
+                    let (|NONTERM_Category_IfThenElse|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_ifExprThen
+                        | Parser.NONTERM_ifExprElifs
+                        | Parser.NONTERM_ifExprCases -> Some()
+                        | _ -> None
+
+                    // Merge a bunch of non terminals
+                    let (|NONTERM_Category_SignatureFile|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_signatureFile
+                        | Parser.NONTERM_moduleSpfn
+                        | Parser.NONTERM_moduleSpfns -> Some()
+                        | _ -> None
+
+                    let (|NONTERM_Category_ImplementationFile|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_implementationFile
+                        | Parser.NONTERM_fileNamespaceImpl
+                        | Parser.NONTERM_fileNamespaceImpls -> Some()
+                        | _ -> None
+
+                    let (|NONTERM_Category_Definition|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_fileModuleImpl
+                        | Parser.NONTERM_moduleDefn
+                        | Parser.NONTERM_interactiveDefns
+                        | Parser.NONTERM_moduleDefns
+                        | Parser.NONTERM_moduleDefnsOrExpr -> Some()
+                        | _ -> None
+
+                    let (|NONTERM_Category_Type|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_typ
+                        | Parser.NONTERM_tupleType -> Some()
+                        | _ -> None
+
+                    let (|NONTERM_Category_Interaction|_|) nonTerminal =
+                        match nonTerminal with
+                        | Parser.NONTERM_interactiveItemsTerminator
+                        | Parser.NONTERM_interaction
+                        | Parser.NONTERM__startinteraction -> Some()
+                        | _ -> None
+
+                    // Canonicalize the categories and check for a unique category
+                    ctxt.ReducibleProductions
+                    |> List.exists (fun prods ->
+                        let prodIds =
+                            prods
+                            |> List.map Parser.prodIdxToNonTerminal
+                            |> List.map (fun nonTerminal ->
+                                match nonTerminal with
+                                | NONTERM_Category_Type -> Parser.NONTERM_typ
+                                | NONTERM_Category_Expr -> Parser.NONTERM_declExpr
+                                | NONTERM_Category_Pattern -> Parser.NONTERM_atomicPattern
+                                | NONTERM_Category_IfThenElse -> Parser.NONTERM_ifExprThen
+                                | NONTERM_Category_SignatureFile -> Parser.NONTERM_signatureFile
+                                | NONTERM_Category_ImplementationFile -> Parser.NONTERM_implementationFile
+                                | NONTERM_Category_Definition -> Parser.NONTERM_moduleDefn
+                                | NONTERM_Category_Interaction -> Parser.NONTERM_interaction
+                                | nt -> nt)
+                            |> Set.ofList
+                            |> Set.toList
+
+                        match prodIds with
+                        | [ Parser.NONTERM_interaction ] ->
+                            os.AppendString(NONTERM_interactionE().Format)
+                            true
+                        | [ Parser.NONTERM_hashDirective ] ->
+                            os.AppendString(NONTERM_hashDirectiveE().Format)
+                            true
+                        | [ Parser.NONTERM_fieldDecl ] ->
+                            os.AppendString(NONTERM_fieldDeclE().Format)
+                            true
+                        | [ Parser.NONTERM_unionCaseRepr ] ->
+                            os.AppendString(NONTERM_unionCaseReprE().Format)
+                            true
+                        | [ Parser.NONTERM_localBinding ] ->
+                            os.AppendString(NONTERM_localBindingE().Format)
+                            true
+                        | [ Parser.NONTERM_hardwhiteLetBindings ] ->
+                            os.AppendString(NONTERM_hardwhiteLetBindingsE().Format)
+                            true
+                        | [ Parser.NONTERM_classDefnMember ] ->
+                            os.AppendString(NONTERM_classDefnMemberE().Format)
+                            true
+                        | [ Parser.NONTERM_defnBindings ] ->
+                            os.AppendString(NONTERM_defnBindingsE().Format)
+                            true
+                        | [ Parser.NONTERM_classMemberSpfn ] ->
+                            os.AppendString(NONTERM_classMemberSpfnE().Format)
+                            true
+                        | [ Parser.NONTERM_valSpfn ] ->
+                            os.AppendString(NONTERM_valSpfnE().Format)
+                            true
+                        | [ Parser.NONTERM_tyconSpfn ] ->
+                            os.AppendString(NONTERM_tyconSpfnE().Format)
+                            true
+                        | [ Parser.NONTERM_anonLambdaExpr ] ->
+                            os.AppendString(NONTERM_anonLambdaExprE().Format)
+                            true
+                        | [ Parser.NONTERM_attrUnionCaseDecl ] ->
+                            os.AppendString(NONTERM_attrUnionCaseDeclE().Format)
+                            true
+                        | [ Parser.NONTERM_cPrototype ] ->
+                            os.AppendString(NONTERM_cPrototypeE().Format)
+                            true
+                        | [ Parser.NONTERM_objExpr | Parser.NONTERM_objectImplementationMembers ] ->
+                            os.AppendString(NONTERM_objectImplementationMembersE().Format)
+                            true
+                        | [ Parser.NONTERM_ifExprThen | Parser.NONTERM_ifExprElifs | Parser.NONTERM_ifExprCases ] ->
+                            os.AppendString(NONTERM_ifExprCasesE().Format)
+                            true
+                        | [ Parser.NONTERM_openDecl ] ->
+                            os.AppendString(NONTERM_openDeclE().Format)
+                            true
+                        | [ Parser.NONTERM_fileModuleSpec ] ->
+                            os.AppendString(NONTERM_fileModuleSpecE().Format)
+                            true
+                        | [ Parser.NONTERM_patternClauses ] ->
+                            os.AppendString(NONTERM_patternClausesE().Format)
+                            true
+                        | [ Parser.NONTERM_beginEndExpr ] ->
+                            os.AppendString(NONTERM_beginEndExprE().Format)
+                            true
+                        | [ Parser.NONTERM_recdExpr ] ->
+                            os.AppendString(NONTERM_recdExprE().Format)
+                            true
+                        | [ Parser.NONTERM_tyconDefn ] ->
+                            os.AppendString(NONTERM_tyconDefnE().Format)
+                            true
+                        | [ Parser.NONTERM_exconCore ] ->
+                            os.AppendString(NONTERM_exconCoreE().Format)
+                            true
+                        | [ Parser.NONTERM_typeNameInfo ] ->
+                            os.AppendString(NONTERM_typeNameInfoE().Format)
+                            true
+                        | [ Parser.NONTERM_attributeList ] ->
+                            os.AppendString(NONTERM_attributeListE().Format)
+                            true
+                        | [ Parser.NONTERM_quoteExpr ] ->
+                            os.AppendString(NONTERM_quoteExprE().Format)
+                            true
+                        | [ Parser.NONTERM_typeConstraint ] ->
+                            os.AppendString(NONTERM_typeConstraintE().Format)
+                            true
+                        | [ NONTERM_Category_ImplementationFile ] ->
+                            os.AppendString(NONTERM_Category_ImplementationFileE().Format)
+                            true
+                        | [ NONTERM_Category_Definition ] ->
+                            os.AppendString(NONTERM_Category_DefinitionE().Format)
+                            true
+                        | [ NONTERM_Category_SignatureFile ] ->
+                            os.AppendString(NONTERM_Category_SignatureFileE().Format)
+                            true
+                        | [ NONTERM_Category_Pattern ] ->
+                            os.AppendString(NONTERM_Category_PatternE().Format)
+                            true
+                        | [ NONTERM_Category_Expr ] ->
+                            os.AppendString(NONTERM_Category_ExprE().Format)
+                            true
+                        | [ NONTERM_Category_Type ] ->
+                            os.AppendString(NONTERM_Category_TypeE().Format)
+                            true
+                        | [ Parser.NONTERM_typeArgsActual ] ->
+                            os.AppendString(NONTERM_typeArgsActualE().Format)
+                            true
+                        | _ -> false)
 
 #if DEBUG
-              if not foundInContext then
-                  Printf.bprintf os ". (no 'in' context found: %+A)" (List.mapSquared Parser.prodIdxToNonTerminal ctxt.ReducibleProductions)
+                if not foundInContext then
+                    Printf.bprintf
+                        os
+                        ". (no 'in' context found: %+A)"
+                        (List.mapSquared Parser.prodIdxToNonTerminal ctxt.ReducibleProductions)
 #else
-              foundInContext |> ignore // suppress unused variable warning in RELEASE
+                foundInContext |> ignore // suppress unused variable warning in RELEASE
 #endif
-              let fix (s: string) = s.Replace(SR.GetString("FixKeyword"), "").Replace(SR.GetString("FixSymbol"), "").Replace(SR.GetString("FixReplace"), "")
-              let tokenNames =
-                  ctxt.ShiftTokens
-                  |> List.map Parser.tokenTagToTokenId
-                  |> List.filter (function Parser.TOKEN_error | Parser.TOKEN_EOF -> false | _ -> true)
-                  |> List.map tokenIdToText
-                  |> Set.ofList
-                  |> Set.toList
-
-              match tokenNames with
-              | [tokenName1] -> os.AppendString(TokenName1E().Format (fix tokenName1))
-              | [tokenName1;tokenName2] -> os.AppendString(TokenName1TokenName2E().Format (fix tokenName1) (fix tokenName2))
-              | [tokenName1;tokenName2;tokenName3] -> os.AppendString(TokenName1TokenName2TokenName3E().Format (fix tokenName1) (fix tokenName2) (fix tokenName3))
-              | _ -> ()
+                let fix (s: string) =
+                    s
+                        .Replace(SR.GetString("FixKeyword"), "")
+                        .Replace(SR.GetString("FixSymbol"), "")
+                        .Replace(SR.GetString("FixReplace"), "")
+
+                let tokenNames =
+                    ctxt.ShiftTokens
+                    |> List.map Parser.tokenTagToTokenId
+                    |> List.filter (function
+                        | Parser.TOKEN_error
+                        | Parser.TOKEN_EOF -> false
+                        | _ -> true)
+                    |> List.map tokenIdToText
+                    |> Set.ofList
+                    |> Set.toList
+
+                match tokenNames with
+                | [ tokenName1 ] -> os.AppendString(TokenName1E().Format(fix tokenName1))
+                | [ tokenName1; tokenName2 ] -> os.AppendString(TokenName1TokenName2E().Format (fix tokenName1) (fix tokenName2))
+                | [ tokenName1; tokenName2; tokenName3 ] ->
+                    os.AppendString(TokenName1TokenName2TokenName3E().Format (fix tokenName1) (fix tokenName2) (fix tokenName3))
+                | _ -> ()
         (*
               Printf.bprintf os ".\n\n    state = %A\n    token = %A\n    expect (shift) %A\n    expect (reduce) %A\n   prods=%A\n     non terminals: %A"
                   ctxt.StateStack
@@ -1328,395 +1462,415 @@ let OutputPhasedErrorR (os: StringBuilder) (diagnostic: PhasedDiagnostic) (canSu
                   (List.mapSquared Parser.prodIdxToNonTerminal ctxt.ReducibleProductions)
         *)
 
-      | RuntimeCoercionSourceSealed(denv, ty, _) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
-          if isTyparTy denv.g ty then
-              os.AppendString(RuntimeCoercionSourceSealed1E().Format (NicePrint.stringOfTy denv ty))
-          else
-              os.AppendString(RuntimeCoercionSourceSealed2E().Format (NicePrint.stringOfTy denv ty))
-
-      | CoercionTargetSealed(denv, ty, _) ->
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty, _cxs= PrettyTypes.PrettifyType denv.g ty
-          os.AppendString(CoercionTargetSealedE().Format (NicePrint.stringOfTy denv ty))
-
-      | UpcastUnnecessary _ ->
-          os.AppendString(UpcastUnnecessaryE().Format)
-
-      | TypeTestUnnecessary _ ->
-          os.AppendString(TypeTestUnnecessaryE().Format)
-
-      | QuotationTranslator.IgnoringPartOfQuotedTermWarning (msg, _) ->
-          Printf.bprintf os "%s" msg
-
-      | OverrideDoesntOverride(denv, impl, minfoVirtOpt, g, amap, m) ->
-          let sig1 = DispatchSlotChecking.FormatOverride denv impl
-          match minfoVirtOpt with
-          | None ->
-              os.AppendString(OverrideDoesntOverride1E().Format sig1)
-          | Some minfoVirt ->
-              // https://github.com/dotnet/fsharp/issues/35
-              // Improve error message when attempting to override generic return type with unit:
-              // we need to check if unit was used as a type argument
-              let hasUnitTType_app (types: TType list) =
-                  types |> List.exists (function
-                      | TType_app (maybeUnit, [], _) ->
-                          match maybeUnit.TypeAbbrev with
-                          | Some ttype when isUnitTy g ttype -> true
-                          | _ -> false
-                      | _ -> false)
-
-              match minfoVirt.ApparentEnclosingType with
-              | TType_app (tycon, tyargs, _) when tycon.IsFSharpInterfaceTycon && hasUnitTType_app tyargs ->
-                  // match abstract member with 'unit' passed as generic argument
-                  os.AppendString(OverrideDoesntOverride4E().Format sig1)
-              | _ ->
-                  os.AppendString(OverrideDoesntOverride2E().Format sig1)
-                  let sig2 = DispatchSlotChecking.FormatMethInfoSig g amap m denv minfoVirt
-                  if sig1 <> sig2 then
-                      os.AppendString(OverrideDoesntOverride3E().Format sig2)
-
-      | UnionCaseWrongArguments (_, n1, n2, _) ->
-          os.AppendString(UnionCaseWrongArgumentsE().Format n2 n1)
-
-      | UnionPatternsBindDifferentNames _ ->
-          os.AppendString(UnionPatternsBindDifferentNamesE().Format)
-
-      | ValueNotContained (denv, infoReader, mref, implVal, sigVal, f) ->
-          let text1, text2 = NicePrint.minimalStringsOfTwoValues denv infoReader (mkLocalValRef implVal) (mkLocalValRef sigVal)
-          os.AppendString(f((fullDisplayTextOfModRef mref), text1, text2))
-
-      | UnionCaseNotContained (denv, infoReader, enclosingTycon, v1, v2, f) ->
-          let enclosingTcref = mkLocalEntityRef enclosingTycon
-          os.AppendString(f((NicePrint.stringOfUnionCase denv infoReader enclosingTcref v1), (NicePrint.stringOfUnionCase denv infoReader enclosingTcref v2)))
-
-      | FSharpExceptionNotContained (denv, infoReader, v1, v2, f) ->
-          os.AppendString(f((NicePrint.stringOfExnDef denv infoReader (mkLocalEntityRef v1)), (NicePrint.stringOfExnDef denv infoReader (mkLocalEntityRef v2))))
-
-      | FieldNotContained (denv, infoReader, enclosingTycon, v1, v2, f) ->
-          let enclosingTcref = mkLocalEntityRef enclosingTycon
-          os.AppendString(f((NicePrint.stringOfRecdField denv infoReader enclosingTcref v1), (NicePrint.stringOfRecdField denv infoReader enclosingTcref v2)))
-
-      | RequiredButNotSpecified (_, mref, k, name, _) ->
-          let nsb = StringBuilder()
-          name nsb;
-          os.AppendString(RequiredButNotSpecifiedE().Format (fullDisplayTextOfModRef mref) k (nsb.ToString()))
-
-      | UseOfAddressOfOperator _ ->
-          os.AppendString(UseOfAddressOfOperatorE().Format)
-
-      | DefensiveCopyWarning(s, _) -> os.AppendString(DefensiveCopyWarningE().Format s)
-
-      | DeprecatedThreadStaticBindingWarning _ ->
-          os.AppendString(DeprecatedThreadStaticBindingWarningE().Format)
-
-      | FunctionValueUnexpected (denv, ty, _) ->
-          let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
-          let errorText = FunctionValueUnexpectedE().Format (NicePrint.stringOfTy denv ty)
-          os.AppendString errorText
-
-      | UnitTypeExpected (denv, ty, _) ->
-          let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
-          let warningText = UnitTypeExpectedE().Format (NicePrint.stringOfTy denv ty)
-          os.AppendString warningText
-
-      | UnitTypeExpectedWithEquality (denv, ty, _) ->
-          let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
-          let warningText = UnitTypeExpectedWithEqualityE().Format (NicePrint.stringOfTy denv ty)
-          os.AppendString warningText
-
-      | UnitTypeExpectedWithPossiblePropertySetter (denv, ty, bindingName, propertyName, _) ->
-          let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
-          let warningText = UnitTypeExpectedWithPossiblePropertySetterE().Format (NicePrint.stringOfTy denv ty) bindingName propertyName
-          os.AppendString warningText
-
-      | UnitTypeExpectedWithPossibleAssignment (denv, ty, isAlreadyMutable, bindingName, _) ->
-          let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
-          let warningText =
-            if isAlreadyMutable then
-                UnitTypeExpectedWithPossibleAssignmentToMutableE().Format (NicePrint.stringOfTy denv ty) bindingName
+        | RuntimeCoercionSourceSealed (denv, ty, _) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+
+            if isTyparTy denv.g ty then
+                os.AppendString(RuntimeCoercionSourceSealed1E().Format(NicePrint.stringOfTy denv ty))
             else
-                UnitTypeExpectedWithPossibleAssignmentE().Format (NicePrint.stringOfTy denv ty) bindingName
-          os.AppendString warningText
+                os.AppendString(RuntimeCoercionSourceSealed2E().Format(NicePrint.stringOfTy denv ty))
+
+        | CoercionTargetSealed (denv, ty, _) ->
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+            os.AppendString(CoercionTargetSealedE().Format(NicePrint.stringOfTy denv ty))
+
+        | UpcastUnnecessary _ -> os.AppendString(UpcastUnnecessaryE().Format)
+
+        | TypeTestUnnecessary _ -> os.AppendString(TypeTestUnnecessaryE().Format)
+
+        | QuotationTranslator.IgnoringPartOfQuotedTermWarning (msg, _) -> Printf.bprintf os "%s" msg
+
+        | OverrideDoesntOverride (denv, impl, minfoVirtOpt, g, amap, m) ->
+            let sig1 = DispatchSlotChecking.FormatOverride denv impl
+
+            match minfoVirtOpt with
+            | None -> os.AppendString(OverrideDoesntOverride1E().Format sig1)
+            | Some minfoVirt ->
+                // https://github.com/dotnet/fsharp/issues/35
+                // Improve error message when attempting to override generic return type with unit:
+                // we need to check if unit was used as a type argument
+                let hasUnitTType_app (types: TType list) =
+                    types
+                    |> List.exists (function
+                        | TType_app (maybeUnit, [], _) ->
+                            match maybeUnit.TypeAbbrev with
+                            | Some ttype when isUnitTy g ttype -> true
+                            | _ -> false
+                        | _ -> false)
+
+                match minfoVirt.ApparentEnclosingType with
+                | TType_app (tycon, tyargs, _) when tycon.IsFSharpInterfaceTycon && hasUnitTType_app tyargs ->
+                    // match abstract member with 'unit' passed as generic argument
+                    os.AppendString(OverrideDoesntOverride4E().Format sig1)
+                | _ ->
+                    os.AppendString(OverrideDoesntOverride2E().Format sig1)
+                    let sig2 = DispatchSlotChecking.FormatMethInfoSig g amap m denv minfoVirt
+
+                    if sig1 <> sig2 then
+                        os.AppendString(OverrideDoesntOverride3E().Format sig2)
+
+        | UnionCaseWrongArguments (_, n1, n2, _) -> os.AppendString(UnionCaseWrongArgumentsE().Format n2 n1)
+
+        | UnionPatternsBindDifferentNames _ -> os.AppendString(UnionPatternsBindDifferentNamesE().Format)
+
+        | ValueNotContained (denv, infoReader, mref, implVal, sigVal, f) ->
+            let text1, text2 =
+                NicePrint.minimalStringsOfTwoValues denv infoReader (mkLocalValRef implVal) (mkLocalValRef sigVal)
+
+            os.AppendString(f ((fullDisplayTextOfModRef mref), text1, text2))
+
+        | UnionCaseNotContained (denv, infoReader, enclosingTycon, v1, v2, f) ->
+            let enclosingTcref = mkLocalEntityRef enclosingTycon
+
+            os.AppendString(
+                f (
+                    (NicePrint.stringOfUnionCase denv infoReader enclosingTcref v1),
+                    (NicePrint.stringOfUnionCase denv infoReader enclosingTcref v2)
+                )
+            )
+
+        | FSharpExceptionNotContained (denv, infoReader, v1, v2, f) ->
+            os.AppendString(
+                f (
+                    (NicePrint.stringOfExnDef denv infoReader (mkLocalEntityRef v1)),
+                    (NicePrint.stringOfExnDef denv infoReader (mkLocalEntityRef v2))
+                )
+            )
 
-      | RecursiveUseCheckedAtRuntime _ ->
-          os.AppendString(RecursiveUseCheckedAtRuntimeE().Format)
+        | FieldNotContained (denv, infoReader, enclosingTycon, v1, v2, f) ->
+            let enclosingTcref = mkLocalEntityRef enclosingTycon
 
-      | LetRecUnsound (_, [v], _) ->
-          os.AppendString(LetRecUnsound1E().Format v.DisplayName)
+            os.AppendString(
+                f (
+                    (NicePrint.stringOfRecdField denv infoReader enclosingTcref v1),
+                    (NicePrint.stringOfRecdField denv infoReader enclosingTcref v2)
+                )
+            )
+
+        | RequiredButNotSpecified (_, mref, k, name, _) ->
+            let nsb = StringBuilder()
+            name nsb
+            os.AppendString(RequiredButNotSpecifiedE().Format (fullDisplayTextOfModRef mref) k (nsb.ToString()))
+
+        | UseOfAddressOfOperator _ -> os.AppendString(UseOfAddressOfOperatorE().Format)
+
+        | DefensiveCopyWarning (s, _) -> os.AppendString(DefensiveCopyWarningE().Format s)
+
+        | DeprecatedThreadStaticBindingWarning _ -> os.AppendString(DeprecatedThreadStaticBindingWarningE().Format)
+
+        | FunctionValueUnexpected (denv, ty, _) ->
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+            let errorText = FunctionValueUnexpectedE().Format(NicePrint.stringOfTy denv ty)
+            os.AppendString errorText
+
+        | UnitTypeExpected (denv, ty, _) ->
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+            let warningText = UnitTypeExpectedE().Format(NicePrint.stringOfTy denv ty)
+            os.AppendString warningText
+
+        | UnitTypeExpectedWithEquality (denv, ty, _) ->
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+
+            let warningText =
+                UnitTypeExpectedWithEqualityE().Format(NicePrint.stringOfTy denv ty)
+
+            os.AppendString warningText
+
+        | UnitTypeExpectedWithPossiblePropertySetter (denv, ty, bindingName, propertyName, _) ->
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+
+            let warningText =
+                UnitTypeExpectedWithPossiblePropertySetterE().Format (NicePrint.stringOfTy denv ty) bindingName propertyName
+
+            os.AppendString warningText
+
+        | UnitTypeExpectedWithPossibleAssignment (denv, ty, isAlreadyMutable, bindingName, _) ->
+            let ty, _cxs = PrettyTypes.PrettifyType denv.g ty
+
+            let warningText =
+                if isAlreadyMutable then
+                    UnitTypeExpectedWithPossibleAssignmentToMutableE().Format (NicePrint.stringOfTy denv ty) bindingName
+                else
+                    UnitTypeExpectedWithPossibleAssignmentE().Format (NicePrint.stringOfTy denv ty) bindingName
+
+            os.AppendString warningText
+
+        | RecursiveUseCheckedAtRuntime _ -> os.AppendString(RecursiveUseCheckedAtRuntimeE().Format)
+
+        | LetRecUnsound (_, [ v ], _) -> os.AppendString(LetRecUnsound1E().Format v.DisplayName)
+
+        | LetRecUnsound (_, path, _) ->
+            let bos = StringBuilder()
 
-      | LetRecUnsound (_, path, _) ->
-          let bos = StringBuilder()
-          (path.Tail @ [path.Head]) |> List.iter (fun (v: ValRef) -> bos.AppendString(LetRecUnsoundInnerE().Format v.DisplayName))
-          os.AppendString(LetRecUnsound2E().Format (List.head path).DisplayName (bos.ToString()))
+            (path.Tail @ [ path.Head ])
+            |> List.iter (fun (v: ValRef) -> bos.AppendString(LetRecUnsoundInnerE().Format v.DisplayName))
 
-      | LetRecEvaluatedOutOfOrder (_, _, _, _) ->
-          os.AppendString(LetRecEvaluatedOutOfOrderE().Format)
+            os.AppendString(LetRecUnsound2E().Format (List.head path).DisplayName (bos.ToString()))
 
-      | LetRecCheckedAtRuntime _ ->
-          os.AppendString(LetRecCheckedAtRuntimeE().Format)
+        | LetRecEvaluatedOutOfOrder (_, _, _, _) -> os.AppendString(LetRecEvaluatedOutOfOrderE().Format)
 
-      | SelfRefObjCtor(false, _) ->
-          os.AppendString(SelfRefObjCtor1E().Format)
+        | LetRecCheckedAtRuntime _ -> os.AppendString(LetRecCheckedAtRuntimeE().Format)
 
-      | SelfRefObjCtor(true, _) ->
-          os.AppendString(SelfRefObjCtor2E().Format)
+        | SelfRefObjCtor (false, _) -> os.AppendString(SelfRefObjCtor1E().Format)
 
-      | VirtualAugmentationOnNullValuedType _ ->
-          os.AppendString(VirtualAugmentationOnNullValuedTypeE().Format)
+        | SelfRefObjCtor (true, _) -> os.AppendString(SelfRefObjCtor2E().Format)
 
-      | NonVirtualAugmentationOnNullValuedType _ ->
-          os.AppendString(NonVirtualAugmentationOnNullValuedTypeE().Format)
+        | VirtualAugmentationOnNullValuedType _ -> os.AppendString(VirtualAugmentationOnNullValuedTypeE().Format)
 
-      | NonUniqueInferredAbstractSlot(_, denv, bindnm, bvirt1, bvirt2, _) ->
-          os.AppendString(NonUniqueInferredAbstractSlot1E().Format bindnm)
-          let ty1 = bvirt1.ApparentEnclosingType
-          let ty2 = bvirt2.ApparentEnclosingType
-          // REVIEW: consider if we need to show _cxs (the type parameter constraints)
-          let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
-          os.AppendString(NonUniqueInferredAbstractSlot2E().Format)
-          if ty1 <> ty2 then
-              os.AppendString(NonUniqueInferredAbstractSlot3E().Format ty1 ty2)
-          os.AppendString(NonUniqueInferredAbstractSlot4E().Format)
+        | NonVirtualAugmentationOnNullValuedType _ -> os.AppendString(NonVirtualAugmentationOnNullValuedTypeE().Format)
 
-      | DiagnosticWithText (_, s, _) -> os.AppendString s
+        | NonUniqueInferredAbstractSlot (_, denv, bindnm, bvirt1, bvirt2, _) ->
+            os.AppendString(NonUniqueInferredAbstractSlot1E().Format bindnm)
+            let ty1 = bvirt1.ApparentEnclosingType
+            let ty2 = bvirt2.ApparentEnclosingType
+            // REVIEW: consider if we need to show _cxs (the type parameter constraints)
+            let ty1, ty2, _cxs = NicePrint.minimalStringsOfTwoTypes denv ty1 ty2
+            os.AppendString(NonUniqueInferredAbstractSlot2E().Format)
 
-      | DiagnosticWithSuggestions (_, s, _, idText, suggestionF) ->
-          os.AppendString(DecompileOpName s)
-          suggestNames suggestionF idText
+            if ty1 <> ty2 then
+                os.AppendString(NonUniqueInferredAbstractSlot3E().Format ty1 ty2)
 
-      | InternalError (s, _)
+            os.AppendString(NonUniqueInferredAbstractSlot4E().Format)
 
-      | InvalidArgument s
+        | DiagnosticWithText (_, s, _) -> os.AppendString s
 
-      | Failure s as exn ->
-          ignore exn // use the argument, even in non DEBUG
-          let f1 = SR.GetString("Failure1")
-          let f2 = SR.GetString("Failure2")
-          match s with
-          | f when f = f1 -> os.AppendString(Failure3E().Format s)
-          | f when f = f2 -> os.AppendString(Failure3E().Format s)
-          | _ -> os.AppendString(Failure4E().Format s)
+        | DiagnosticWithSuggestions (_, s, _, idText, suggestionF) ->
+            os.AppendString(DecompileOpName s)
+            suggestNames suggestionF idText
+
+        | InternalError (s, _)
+
+        | InvalidArgument s
+
+        | Failure s as exn ->
+            ignore exn // use the argument, even in non DEBUG
+            let f1 = SR.GetString("Failure1")
+            let f2 = SR.GetString("Failure2")
+
+            match s with
+            | f when f = f1 -> os.AppendString(Failure3E().Format s)
+            | f when f = f2 -> os.AppendString(Failure3E().Format s)
+            | _ -> os.AppendString(Failure4E().Format s)
 #if DEBUG
-          Printf.bprintf os "\nStack Trace\n%s\n" (exn.ToString())
-          Debug.Assert(false, sprintf "Unexpected exception seen in compiler: %s\n%s" s (exn.ToString()))
+            Printf.bprintf os "\nStack Trace\n%s\n" (exn.ToString())
+            Debug.Assert(false, sprintf "Unexpected exception seen in compiler: %s\n%s" s (exn.ToString()))
 #endif
 
-      | WrappedError (exn, _) -> OutputExceptionR os exn
+        | WrappedError (exn, _) -> OutputExceptionR os exn
+
+        | PatternMatchCompilation.MatchIncomplete (isComp, cexOpt, _) ->
+            os.AppendString(MatchIncomplete1E().Format)
 
-      | PatternMatchCompilation.MatchIncomplete (isComp, cexOpt, _) ->
-          os.AppendString(MatchIncomplete1E().Format)
-          match cexOpt with
-          | None -> ()
-          | Some (cex, false) -> os.AppendString(MatchIncomplete2E().Format cex)
-          | Some (cex, true) -> os.AppendString(MatchIncomplete3E().Format cex)
-          if isComp then
-              os.AppendString(MatchIncomplete4E().Format)
+            match cexOpt with
+            | None -> ()
+            | Some (cex, false) -> os.AppendString(MatchIncomplete2E().Format cex)
+            | Some (cex, true) -> os.AppendString(MatchIncomplete3E().Format cex)
 
-      | PatternMatchCompilation.EnumMatchIncomplete (isComp, cexOpt, _) ->
-          os.AppendString(EnumMatchIncomplete1E().Format)
-          match cexOpt with
-          | None -> ()
-          | Some (cex, false) -> os.AppendString(MatchIncomplete2E().Format cex)
-          | Some (cex, true) -> os.AppendString(MatchIncomplete3E().Format cex)
-          if isComp then
-              os.AppendString(MatchIncomplete4E().Format)
+            if isComp then os.AppendString(MatchIncomplete4E().Format)
 
-      | PatternMatchCompilation.RuleNeverMatched _ -> os.AppendString(RuleNeverMatchedE().Format)
+        | PatternMatchCompilation.EnumMatchIncomplete (isComp, cexOpt, _) ->
+            os.AppendString(EnumMatchIncomplete1E().Format)
 
-      | ValNotMutable(_, valRef, _) -> os.AppendString(ValNotMutableE().Format(valRef.DisplayName))
+            match cexOpt with
+            | None -> ()
+            | Some (cex, false) -> os.AppendString(MatchIncomplete2E().Format cex)
+            | Some (cex, true) -> os.AppendString(MatchIncomplete3E().Format cex)
 
-      | ValNotLocal _ -> os.AppendString(ValNotLocalE().Format)
+            if isComp then os.AppendString(MatchIncomplete4E().Format)
 
-      | ObsoleteError (s, _)
+        | PatternMatchCompilation.RuleNeverMatched _ -> os.AppendString(RuleNeverMatchedE().Format)
 
-      | ObsoleteWarning (s, _) ->
+        | ValNotMutable (_, valRef, _) -> os.AppendString(ValNotMutableE().Format(valRef.DisplayName))
+
+        | ValNotLocal _ -> os.AppendString(ValNotLocalE().Format)
+
+        | ObsoleteError (s, _)
+
+        | ObsoleteWarning (s, _) ->
             os.AppendString(Obsolete1E().Format)
             if s <> "" then os.AppendString(Obsolete2E().Format s)
 
-      | Experimental (s, _) -> os.AppendString(ExperimentalE().Format s)
-
-      | PossibleUnverifiableCode _ -> os.AppendString(PossibleUnverifiableCodeE().Format)
-
-      | UserCompilerMessage (msg, _, _) -> os.AppendString msg
-
-      | Deprecated(s, _) -> os.AppendString(DeprecatedE().Format s)
-
-      | LibraryUseOnly _ -> os.AppendString(LibraryUseOnlyE().Format)
-
-      | MissingFields(sl, _) -> os.AppendString(MissingFieldsE().Format (String.concat "," sl + "."))
-
-      | ValueRestriction(denv, infoReader, hasSig, v, _, _) ->
-          let denv = { denv with showInferenceTyparAnnotations=true }
-          let tau = v.TauType
-          if hasSig then
-              if isFunTy denv.g tau && (arityOfVal v).HasNoArgs then
-                  let msg =
-                      ValueRestriction1E().Format
-                          v.DisplayName
-                          (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
-                          v.DisplayName
-                  os.AppendString msg
-              else
-                  let msg =
-                      ValueRestriction2E().Format
-                          v.DisplayName
-                          (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
-                          v.DisplayName
-                  os.AppendString msg
-          else
-              match v.MemberInfo with
-              | Some membInfo when
-                 (match membInfo.MemberFlags.MemberKind with
-                  | SynMemberKind.PropertyGet
-                  | SynMemberKind.PropertySet
-                  | SynMemberKind.Constructor -> true // can't infer extra polymorphism
-                  // can infer extra polymorphism
-                  | _ -> false) ->
-                  let msg = ValueRestriction3E().Format (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
-                  os.AppendString msg
-              | _ ->
+        | Experimental (s, _) -> os.AppendString(ExperimentalE().Format s)
+
+        | PossibleUnverifiableCode _ -> os.AppendString(PossibleUnverifiableCodeE().Format)
+
+        | UserCompilerMessage (msg, _, _) -> os.AppendString msg
+
+        | Deprecated (s, _) -> os.AppendString(DeprecatedE().Format s)
+
+        | LibraryUseOnly _ -> os.AppendString(LibraryUseOnlyE().Format)
+
+        | MissingFields (sl, _) -> os.AppendString(MissingFieldsE().Format(String.concat "," sl + "."))
+
+        | ValueRestriction (denv, infoReader, hasSig, v, _, _) ->
+            let denv =
+                { denv with
+                    showInferenceTyparAnnotations = true
+                }
+
+            let tau = v.TauType
+
+            if hasSig then
                 if isFunTy denv.g tau && (arityOfVal v).HasNoArgs then
                     let msg =
-                        ValueRestriction4E().Format
+                        ValueRestriction1E().Format
                             v.DisplayName
                             (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
                             v.DisplayName
+
                     os.AppendString msg
                 else
                     let msg =
-                        ValueRestriction5E().Format
+                        ValueRestriction2E().Format
                             v.DisplayName
                             (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
                             v.DisplayName
+
+                    os.AppendString msg
+            else
+                match v.MemberInfo with
+                | Some membInfo when
+                    (match membInfo.MemberFlags.MemberKind with
+                     | SynMemberKind.PropertyGet
+                     | SynMemberKind.PropertySet
+                     | SynMemberKind.Constructor -> true // can't infer extra polymorphism
+                     // can infer extra polymorphism
+                     | _ -> false)
+                    ->
+                    let msg =
+                        ValueRestriction3E()
+                            .Format(NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
+
                     os.AppendString msg
+                | _ ->
+                    if isFunTy denv.g tau && (arityOfVal v).HasNoArgs then
+                        let msg =
+                            ValueRestriction4E().Format
+                                v.DisplayName
+                                (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
+                                v.DisplayName
+
+                        os.AppendString msg
+                    else
+                        let msg =
+                            ValueRestriction5E().Format
+                                v.DisplayName
+                                (NicePrint.stringOfQualifiedValOrMember denv infoReader (mkLocalValRef v))
+                                v.DisplayName
 
-      | Parsing.RecoverableParseError ->
-          os.AppendString(RecoverableParseErrorE().Format)
+                        os.AppendString msg
 
-      | ReservedKeyword (s, _) ->
-          os.AppendString(ReservedKeywordE().Format s)
+        | Parsing.RecoverableParseError -> os.AppendString(RecoverableParseErrorE().Format)
 
-      | IndentationProblem (s, _) ->
-          os.AppendString(IndentationProblemE().Format s)
+        | ReservedKeyword (s, _) -> os.AppendString(ReservedKeywordE().Format s)
 
-      | OverrideInIntrinsicAugmentation _ ->
-         os.AppendString(OverrideInIntrinsicAugmentationE().Format)
+        | IndentationProblem (s, _) -> os.AppendString(IndentationProblemE().Format s)
 
-      | OverrideInExtrinsicAugmentation _ ->
-          os.AppendString(OverrideInExtrinsicAugmentationE().Format)
+        | OverrideInIntrinsicAugmentation _ -> os.AppendString(OverrideInIntrinsicAugmentationE().Format)
 
-      | IntfImplInIntrinsicAugmentation _ ->
-          os.AppendString(IntfImplInIntrinsicAugmentationE().Format)
+        | OverrideInExtrinsicAugmentation _ -> os.AppendString(OverrideInExtrinsicAugmentationE().Format)
 
-      | IntfImplInExtrinsicAugmentation _ ->
-          os.AppendString(IntfImplInExtrinsicAugmentationE().Format)
+        | IntfImplInIntrinsicAugmentation _ -> os.AppendString(IntfImplInIntrinsicAugmentationE().Format)
 
-      | UnresolvedReferenceError(assemblyName, _)
-      | UnresolvedReferenceNoRange assemblyName ->
-          os.AppendString(UnresolvedReferenceNoRangeE().Format assemblyName)
+        | IntfImplInExtrinsicAugmentation _ -> os.AppendString(IntfImplInExtrinsicAugmentationE().Format)
 
-      | UnresolvedPathReference(assemblyName, pathname, _)
+        | UnresolvedReferenceError (assemblyName, _)
+        | UnresolvedReferenceNoRange assemblyName -> os.AppendString(UnresolvedReferenceNoRangeE().Format assemblyName)
 
-      | UnresolvedPathReferenceNoRange(assemblyName, pathname) ->
-          os.AppendString(UnresolvedPathReferenceNoRangeE().Format pathname assemblyName)
+        | UnresolvedPathReference (assemblyName, pathname, _)
 
-      | DeprecatedCommandLineOptionFull(fullText, _) ->
-          os.AppendString fullText
+        | UnresolvedPathReferenceNoRange (assemblyName, pathname) ->
+            os.AppendString(UnresolvedPathReferenceNoRangeE().Format pathname assemblyName)
 
-      | DeprecatedCommandLineOptionForHtmlDoc(optionName, _) ->
-          os.AppendString(FSComp.SR.optsDCLOHtmlDoc optionName)
+        | DeprecatedCommandLineOptionFull (fullText, _) -> os.AppendString fullText
 
-      | DeprecatedCommandLineOptionSuggestAlternative(optionName, altOption, _) ->
-          os.AppendString(FSComp.SR.optsDCLODeprecatedSuggestAlternative(optionName, altOption))
+        | DeprecatedCommandLineOptionForHtmlDoc (optionName, _) -> os.AppendString(FSComp.SR.optsDCLOHtmlDoc optionName)
 
-      | InternalCommandLineOption(optionName, _) ->
-          os.AppendString(FSComp.SR.optsInternalNoDescription optionName)
+        | DeprecatedCommandLineOptionSuggestAlternative (optionName, altOption, _) ->
+            os.AppendString(FSComp.SR.optsDCLODeprecatedSuggestAlternative (optionName, altOption))
 
-      | DeprecatedCommandLineOptionNoDescription(optionName, _) ->
-          os.AppendString(FSComp.SR.optsDCLONoDescription optionName)
+        | InternalCommandLineOption (optionName, _) -> os.AppendString(FSComp.SR.optsInternalNoDescription optionName)
 
-      | HashIncludeNotAllowedInNonScript _ ->
-          os.AppendString(HashIncludeNotAllowedInNonScriptE().Format)
+        | DeprecatedCommandLineOptionNoDescription (optionName, _) -> os.AppendString(FSComp.SR.optsDCLONoDescription optionName)
 
-      | HashReferenceNotAllowedInNonScript _ ->
-          os.AppendString(HashReferenceNotAllowedInNonScriptE().Format)
+        | HashIncludeNotAllowedInNonScript _ -> os.AppendString(HashIncludeNotAllowedInNonScriptE().Format)
 
-      | HashDirectiveNotAllowedInNonScript _ ->
-          os.AppendString(HashDirectiveNotAllowedInNonScriptE().Format)
+        | HashReferenceNotAllowedInNonScript _ -> os.AppendString(HashReferenceNotAllowedInNonScriptE().Format)
 
-      | FileNameNotResolved(fileName, locations, _) ->
-          os.AppendString(FileNameNotResolvedE().Format fileName locations)
+        | HashDirectiveNotAllowedInNonScript _ -> os.AppendString(HashDirectiveNotAllowedInNonScriptE().Format)
 
-      | AssemblyNotResolved(originalName, _) ->
-          os.AppendString(AssemblyNotResolvedE().Format originalName)
+        | FileNameNotResolved (fileName, locations, _) -> os.AppendString(FileNameNotResolvedE().Format fileName locations)
 
-      | IllegalFileNameChar(fileName, invalidChar) ->
-          os.AppendString(FSComp.SR.buildUnexpectedFileNameCharacter(fileName, string invalidChar)|>snd)
+        | AssemblyNotResolved (originalName, _) -> os.AppendString(AssemblyNotResolvedE().Format originalName)
 
-      | HashLoadedSourceHasIssues(infos, warnings, errors, _) ->
-          let Emit(l: exn list) =
-              OutputExceptionR os (List.head l)
-          if isNil warnings && isNil errors then
-              os.AppendString(HashLoadedSourceHasIssues0E().Format)
-              Emit infos
-          elif isNil errors then
-              os.AppendString(HashLoadedSourceHasIssues1E().Format)
-              Emit warnings
-          else
-              os.AppendString(HashLoadedSourceHasIssues2E().Format)
-              Emit errors
+        | IllegalFileNameChar (fileName, invalidChar) ->
+            os.AppendString(FSComp.SR.buildUnexpectedFileNameCharacter (fileName, string invalidChar) |> snd)
 
-      | HashLoadedScriptConsideredSource _ ->
-          os.AppendString(HashLoadedScriptConsideredSourceE().Format)
+        | HashLoadedSourceHasIssues (infos, warnings, errors, _) ->
+            let Emit (l: exn list) = OutputExceptionR os (List.head l)
 
-      | InvalidInternalsVisibleToAssemblyName(badName, fileNameOption) ->
-          match fileNameOption with
-          | Some file -> os.AppendString(InvalidInternalsVisibleToAssemblyName1E().Format badName file)
-          | None -> os.AppendString(InvalidInternalsVisibleToAssemblyName2E().Format badName)
+            if isNil warnings && isNil errors then
+                os.AppendString(HashLoadedSourceHasIssues0E().Format)
+                Emit infos
+            elif isNil errors then
+                os.AppendString(HashLoadedSourceHasIssues1E().Format)
+                Emit warnings
+            else
+                os.AppendString(HashLoadedSourceHasIssues2E().Format)
+                Emit errors
+
+        | HashLoadedScriptConsideredSource _ -> os.AppendString(HashLoadedScriptConsideredSourceE().Format)
+
+        | InvalidInternalsVisibleToAssemblyName (badName, fileNameOption) ->
+            match fileNameOption with
+            | Some file -> os.AppendString(InvalidInternalsVisibleToAssemblyName1E().Format badName file)
+            | None -> os.AppendString(InvalidInternalsVisibleToAssemblyName2E().Format badName)
 
-      | LoadedSourceNotFoundIgnoring(fileName, _) ->
-          os.AppendString(LoadedSourceNotFoundIgnoringE().Format fileName)
+        | LoadedSourceNotFoundIgnoring (fileName, _) -> os.AppendString(LoadedSourceNotFoundIgnoringE().Format fileName)
 
-      | MSBuildReferenceResolutionWarning(code, message, _)
+        | MSBuildReferenceResolutionWarning (code, message, _)
 
-      | MSBuildReferenceResolutionError(code, message, _) ->
-          os.AppendString(MSBuildReferenceResolutionErrorE().Format message code)
+        | MSBuildReferenceResolutionError (code, message, _) -> os.AppendString(MSBuildReferenceResolutionErrorE().Format message code)
 
-      // Strip TargetInvocationException wrappers
-      | :? System.Reflection.TargetInvocationException as exn ->
-          OutputExceptionR os exn.InnerException
+        // Strip TargetInvocationException wrappers
+        | :? System.Reflection.TargetInvocationException as exn -> OutputExceptionR os exn.InnerException
 
-      | :? FileNotFoundException as exn -> Printf.bprintf os "%s" exn.Message
+        | :? FileNotFoundException as exn -> Printf.bprintf os "%s" exn.Message
 
-      | :? DirectoryNotFoundException as exn -> Printf.bprintf os "%s" exn.Message
+        | :? DirectoryNotFoundException as exn -> Printf.bprintf os "%s" exn.Message
 
-      | :? ArgumentException as exn -> Printf.bprintf os "%s" exn.Message
+        | :? ArgumentException as exn -> Printf.bprintf os "%s" exn.Message
 
-      | :? NotSupportedException as exn -> Printf.bprintf os "%s" exn.Message
+        | :? NotSupportedException as exn -> Printf.bprintf os "%s" exn.Message
 
-      | :? IOException as exn -> Printf.bprintf os "%s" exn.Message
+        | :? IOException as exn -> Printf.bprintf os "%s" exn.Message
 
-      | :? UnauthorizedAccessException as exn -> Printf.bprintf os "%s" exn.Message
+        | :? UnauthorizedAccessException as exn -> Printf.bprintf os "%s" exn.Message
 
-      | exn ->
-          os.AppendString(TargetInvocationExceptionWrapperE().Format exn.Message)
+        | exn ->
+            os.AppendString(TargetInvocationExceptionWrapperE().Format exn.Message)
 #if DEBUG
-          Printf.bprintf os "\nStack Trace\n%s\n" (exn.ToString())
-          if showAssertForUnexpectedException.Value then
-              Debug.Assert(false, sprintf "Unknown exception seen in compiler: %s" (exn.ToString()))
+            Printf.bprintf os "\nStack Trace\n%s\n" (exn.ToString())
+
+            if showAssertForUnexpectedException.Value then
+                Debug.Assert(false, sprintf "Unknown exception seen in compiler: %s" (exn.ToString()))
 #endif
 
     OutputExceptionR os diagnostic.Exception
 
-
 // remove any newlines and tabs
 let OutputPhasedDiagnostic (os: StringBuilder) (diagnostic: PhasedDiagnostic) (flattenErrors: bool) (suggestNames: bool) =
     let buf = StringBuilder()
 
     OutputPhasedErrorR buf diagnostic suggestNames
-    let text = if flattenErrors then NormalizeErrorString (buf.ToString()) else buf.ToString()
+
+    let text =
+        if flattenErrors then
+            NormalizeErrorString(buf.ToString())
+        else
+            buf.ToString()
 
     os.AppendString text
 
@@ -1731,32 +1885,38 @@ let SanitizeFileName fileName implicitIncludeDir =
         let currentDir = implicitIncludeDir
 
         // if the file name is not rooted in the current directory, return the full path
-        if not(fullPath.StartsWithOrdinal currentDir) then
+        if not (fullPath.StartsWithOrdinal currentDir) then
             fullPath
         // if the file name is rooted in the current directory, return the relative path
         else
-            fullPath.Replace(currentDir+"\\", "")
+            fullPath.Replace(currentDir + "\\", "")
     with _ ->
         fileName
 
 []
 type FormattedDiagnosticLocation =
-    { Range: range
-      File: string
-      TextRepresentation: string
-      IsEmpty: bool }
+    {
+        Range: range
+        File: string
+        TextRepresentation: string
+        IsEmpty: bool
+    }
 
 []
 type FormattedDiagnosticCanonicalInformation =
-    { ErrorNumber: int
-      Subcategory: string
-      TextRepresentation: string }
+    {
+        ErrorNumber: int
+        Subcategory: string
+        TextRepresentation: string
+    }
 
 []
 type FormattedDiagnosticDetailedInfo =
-    { Location: FormattedDiagnosticLocation option
-      Canonical: FormattedDiagnosticCanonicalInformation
-      Message: string }
+    {
+        Location: FormattedDiagnosticLocation option
+        Canonical: FormattedDiagnosticCanonicalInformation
+        Message: string
+    }
 
 []
 type FormattedDiagnostic =
@@ -1764,120 +1924,180 @@ type FormattedDiagnostic =
     | Long of FSharpDiagnosticSeverity * FormattedDiagnosticDetailedInfo
 
 /// returns sequence that contains Diagnostic for the given error + Diagnostic for all related errors
-let CollectFormattedDiagnostics (implicitIncludeDir, showFullPaths, flattenErrors, diagnosticStyle, severity: FSharpDiagnosticSeverity, diagnostic: PhasedDiagnostic, suggestNames: bool) =
-    let outputWhere (showFullPaths, diagnosticStyle) m: FormattedDiagnosticLocation =
+let CollectFormattedDiagnostics
+    (
+        implicitIncludeDir,
+        showFullPaths,
+        flattenErrors,
+        diagnosticStyle,
+        severity: FSharpDiagnosticSeverity,
+        diagnostic: PhasedDiagnostic,
+        suggestNames: bool
+    ) =
+    let outputWhere (showFullPaths, diagnosticStyle) m : FormattedDiagnosticLocation =
         if equals m rangeStartup || equals m rangeCmdArgs then
-            { Range = m; TextRepresentation = ""; IsEmpty = true; File = "" }
+            {
+                Range = m
+                TextRepresentation = ""
+                IsEmpty = true
+                File = ""
+            }
         else
             let file = m.FileName
-            let file = if showFullPaths then
-                            FileSystem.GetFullFilePathInDirectoryShim implicitIncludeDir file
-                       else
-                            SanitizeFileName file implicitIncludeDir
+
+            let file =
+                if showFullPaths then
+                    FileSystem.GetFullFilePathInDirectoryShim implicitIncludeDir file
+                else
+                    SanitizeFileName file implicitIncludeDir
+
             let text, m, file =
                 match diagnosticStyle with
-                  | DiagnosticStyle.Emacs ->
+                | DiagnosticStyle.Emacs ->
                     let file = file.Replace("\\", "/")
                     (sprintf "File \"%s\", line %d, characters %d-%d: " file m.StartLine m.StartColumn m.EndColumn), m, file
 
-                  // We're adjusting the columns here to be 1-based - both for parity with C# and for MSBuild, which assumes 1-based columns for error output
-                  | DiagnosticStyle.Default ->
+                // We're adjusting the columns here to be 1-based - both for parity with C# and for MSBuild, which assumes 1-based columns for error output
+                | DiagnosticStyle.Default ->
                     let file = file.Replace('/', Path.DirectorySeparatorChar)
                     let m = mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) m.End
                     (sprintf "%s(%d,%d): " file m.StartLine m.StartColumn), m, file
 
-                  // We may also want to change Test to be 1-based
-                  | DiagnosticStyle.Test ->
+                // We may also want to change Test to be 1-based
+                | DiagnosticStyle.Test ->
                     let file = file.Replace("/", "\\")
-                    let m = mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) (mkPos m.EndLine (m.EndColumn + 1) )
+
+                    let m =
+                        mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) (mkPos m.EndLine (m.EndColumn + 1))
+
                     sprintf "%s(%d,%d-%d,%d): " file m.StartLine m.StartColumn m.EndLine m.EndColumn, m, file
 
-                  | DiagnosticStyle.Gcc ->
+                | DiagnosticStyle.Gcc ->
                     let file = file.Replace('/', Path.DirectorySeparatorChar)
-                    let m = mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) (mkPos m.EndLine (m.EndColumn + 1) )
+
+                    let m =
+                        mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) (mkPos m.EndLine (m.EndColumn + 1))
+
                     sprintf "%s:%d:%d: " file m.StartLine m.StartColumn, m, file
 
-                  // Here, we want the complete range information so Project Systems can generate proper squiggles
-                  | DiagnosticStyle.VisualStudio ->
-                        // Show prefix only for real files. Otherwise, we just want a truncated error like:
-                        //      parse error FS0031: blah blah
-                        if not (equals m range0) && not (equals m rangeStartup) && not (equals m rangeCmdArgs) then
-                            let file = file.Replace("/", "\\")
-                            let m = mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) (mkPos m.EndLine (m.EndColumn + 1) )
-                            sprintf "%s(%d,%d,%d,%d): " file m.StartLine m.StartColumn m.EndLine m.EndColumn, m, file
-                        else
-                            "", m, file
-            { Range = m; TextRepresentation = text; IsEmpty = false; File = file }
+                // Here, we want the complete range information so Project Systems can generate proper squiggles
+                | DiagnosticStyle.VisualStudio ->
+                    // Show prefix only for real files. Otherwise, we just want a truncated error like:
+                    //      parse error FS0031: blah blah
+                    if
+                        not (equals m range0) && not (equals m rangeStartup)
+                        && not (equals m rangeCmdArgs)
+                    then
+                        let file = file.Replace("/", "\\")
+
+                        let m =
+                            mkRange m.FileName (mkPos m.StartLine (m.StartColumn + 1)) (mkPos m.EndLine (m.EndColumn + 1))
+
+                        sprintf "%s(%d,%d,%d,%d): " file m.StartLine m.StartColumn m.EndLine m.EndColumn, m, file
+                    else
+                        "", m, file
+
+            {
+                Range = m
+                TextRepresentation = text
+                IsEmpty = false
+                File = file
+            }
 
     match diagnostic.Exception with
     | ReportedError _ ->
         assert ("" = "Unexpected ReportedError") //  this should never happen
-        [| |]
+        [||]
     | StopProcessing ->
         assert ("" = "Unexpected StopProcessing") // this should never happen
-        [| |]
+        [||]
     | _ ->
         let errors = ResizeArray()
+
         let report diagnostic =
             let OutputWhere diagnostic =
                 match GetRangeOfDiagnostic diagnostic with
                 | Some m -> Some(outputWhere (showFullPaths, diagnosticStyle) m)
                 | None -> None
 
-            let OutputCanonicalInformation(subcategory, errorNumber) : FormattedDiagnosticCanonicalInformation =
+            let OutputCanonicalInformation (subcategory, errorNumber) : FormattedDiagnosticCanonicalInformation =
                 let message =
                     match severity with
                     | FSharpDiagnosticSeverity.Error -> "error"
                     | FSharpDiagnosticSeverity.Warning -> "warning"
                     | FSharpDiagnosticSeverity.Info
                     | FSharpDiagnosticSeverity.Hidden -> "info"
+
                 let text =
                     match diagnosticStyle with
                     // Show the subcategory for --vserrors so that we can fish it out in Visual Studio and use it to determine error stickiness.
                     | DiagnosticStyle.VisualStudio -> sprintf "%s %s FS%04d: " subcategory message errorNumber
                     | _ -> sprintf "%s FS%04d: " message errorNumber
-                { ErrorNumber = errorNumber; Subcategory = subcategory; TextRepresentation = text}
+
+                {
+                    ErrorNumber = errorNumber
+                    Subcategory = subcategory
+                    TextRepresentation = text
+                }
 
             let mainError, relatedErrors = SplitRelatedDiagnostics diagnostic
             let where = OutputWhere mainError
-            let canonical = OutputCanonicalInformation(diagnostic.Subcategory(), GetDiagnosticNumber mainError)
+
+            let canonical =
+                OutputCanonicalInformation(diagnostic.Subcategory(), GetDiagnosticNumber mainError)
+
             let message =
                 let os = StringBuilder()
                 OutputPhasedDiagnostic os mainError flattenErrors suggestNames
                 os.ToString()
 
-            let entry: FormattedDiagnosticDetailedInfo = { Location = where; Canonical = canonical; Message = message }
+            let entry: FormattedDiagnosticDetailedInfo =
+                {
+                    Location = where
+                    Canonical = canonical
+                    Message = message
+                }
 
-            errors.Add (FormattedDiagnostic.Long(severity, entry))
+            errors.Add(FormattedDiagnostic.Long(severity, entry))
 
-            let OutputRelatedError(diagnostic: PhasedDiagnostic) =
+            let OutputRelatedError (diagnostic: PhasedDiagnostic) =
                 match diagnosticStyle with
                 // Give a canonical string when --vserror.
                 | DiagnosticStyle.VisualStudio ->
                     let relWhere = OutputWhere mainError // mainError?
-                    let relCanonical = OutputCanonicalInformation(diagnostic.Subcategory(), GetDiagnosticNumber mainError) // Use main error for code
+
+                    let relCanonical =
+                        OutputCanonicalInformation(diagnostic.Subcategory(), GetDiagnosticNumber mainError) // Use main error for code
+
                     let relMessage =
                         let os = StringBuilder()
                         OutputPhasedDiagnostic os diagnostic flattenErrors suggestNames
                         os.ToString()
 
-                    let entry: FormattedDiagnosticDetailedInfo = { Location = relWhere; Canonical = relCanonical; Message = relMessage}
-                    errors.Add (FormattedDiagnostic.Long (severity, entry) )
+                    let entry: FormattedDiagnosticDetailedInfo =
+                        {
+                            Location = relWhere
+                            Canonical = relCanonical
+                            Message = relMessage
+                        }
+
+                    errors.Add(FormattedDiagnostic.Long(severity, entry))
 
                 | _ ->
                     let os = StringBuilder()
                     OutputPhasedDiagnostic os diagnostic flattenErrors suggestNames
-                    errors.Add (FormattedDiagnostic.Short(severity, os.ToString()) )
+                    errors.Add(FormattedDiagnostic.Short(severity, os.ToString()))
 
             relatedErrors |> List.iter OutputRelatedError
 
         match diagnostic with
 #if !NO_TYPEPROVIDERS
-        | {Exception = :? TypeProviderError as tpe} ->
-            tpe.Iter (fun exn ->
-                let newErr = {diagnostic with Exception = exn}
-                report newErr
-            )
+        | {
+              Exception = :? TypeProviderError as tpe
+          } ->
+            tpe.Iter(fun exn ->
+                let newErr = { diagnostic with Exception = exn }
+                report newErr)
 #endif
         | x -> report x
 
@@ -1888,16 +2108,19 @@ let CollectFormattedDiagnostics (implicitIncludeDir, showFullPaths, flattenError
 let rec OutputDiagnostic (implicitIncludeDir, showFullPaths, flattenErrors, diagnosticStyle, severity) os (diagnostic: PhasedDiagnostic) =
 
     // 'true' for "canSuggestNames" is passed last here because we want to report suggestions in fsc.exe and fsi.exe, just not in regular IDE usage.
-    let errors = CollectFormattedDiagnostics (implicitIncludeDir, showFullPaths, flattenErrors, diagnosticStyle, severity, diagnostic, true)
+    let errors =
+        CollectFormattedDiagnostics(implicitIncludeDir, showFullPaths, flattenErrors, diagnosticStyle, severity, diagnostic, true)
+
     for e in errors do
         Printf.bprintf os "\n"
+
         match e with
-        | FormattedDiagnostic.Short(_, txt) ->
-            os.AppendString txt |> ignore
-        | FormattedDiagnostic.Long(_, details) ->
+        | FormattedDiagnostic.Short (_, txt) -> os.AppendString txt |> ignore
+        | FormattedDiagnostic.Long (_, details) ->
             match details.Location with
             | Some l when not l.IsEmpty -> os.AppendString l.TextRepresentation
             | _ -> ()
+
             os.AppendString details.Canonical.TextRepresentation
             os.AppendString details.Message
 
@@ -1909,7 +2132,8 @@ let OutputDiagnosticContext prefix fileLineFunction os diagnostic =
         let lineA = m.StartLine
         let lineB = m.EndLine
         let line = fileLineFunction fileName lineA
-        if line<>"" then
+
+        if line <> "" then
             let iA = m.StartColumn
             let iB = m.EndColumn
             let iLen = if lineA = lineB then max (iB - iA) 1 else 1
@@ -1922,8 +2146,9 @@ let ReportDiagnosticAsInfo options (diagnostic, severity) =
     | FSharpDiagnosticSeverity.Warning -> false
     | FSharpDiagnosticSeverity.Info ->
         let n = GetDiagnosticNumber diagnostic
-        IsWarningOrInfoEnabled (diagnostic, severity) n options.WarnLevel options.WarnOn && 
-        not (List.contains n options.WarnOff)
+
+        IsWarningOrInfoEnabled (diagnostic, severity) n options.WarnLevel options.WarnOn
+        && not (List.contains n options.WarnOff)
     | FSharpDiagnosticSeverity.Hidden -> false
 
 let ReportDiagnosticAsWarning options (diagnostic, severity) =
@@ -1931,13 +2156,13 @@ let ReportDiagnosticAsWarning options (diagnostic, severity) =
     | FSharpDiagnosticSeverity.Error -> false
     | FSharpDiagnosticSeverity.Warning ->
         let n = GetDiagnosticNumber diagnostic
-        IsWarningOrInfoEnabled (diagnostic, severity) n options.WarnLevel options.WarnOn && 
-        not (List.contains n options.WarnOff)
+
+        IsWarningOrInfoEnabled (diagnostic, severity) n options.WarnLevel options.WarnOn
+        && not (List.contains n options.WarnOff)
     // Informational become warning if explicitly on and not explicitly off
     | FSharpDiagnosticSeverity.Info ->
         let n = GetDiagnosticNumber diagnostic
-        List.contains n options.WarnOn && 
-        not (List.contains n options.WarnOff)
+        List.contains n options.WarnOn && not (List.contains n options.WarnOff)
     | FSharpDiagnosticSeverity.Hidden -> false
 
 let ReportDiagnosticAsError options (diagnostic, severity) =
@@ -1946,10 +2171,11 @@ let ReportDiagnosticAsError options (diagnostic, severity) =
     // Warnings become errors in some situations
     | FSharpDiagnosticSeverity.Warning ->
         let n = GetDiagnosticNumber diagnostic
-        IsWarningOrInfoEnabled (diagnostic, severity) n options.WarnLevel options.WarnOn &&
-        not (List.contains n options.WarnAsWarn) &&
-        ((options.GlobalWarnAsError && not (List.contains n options.WarnOff)) ||
-         List.contains n options.WarnAsError)
+
+        IsWarningOrInfoEnabled (diagnostic, severity) n options.WarnLevel options.WarnOn
+        && not (List.contains n options.WarnAsWarn)
+        && ((options.GlobalWarnAsError && not (List.contains n options.WarnOff))
+            || List.contains n options.WarnAsError)
     // Informational become errors if explicitly WarnAsError
     | FSharpDiagnosticSeverity.Info ->
         let n = GetDiagnosticNumber diagnostic
@@ -1959,7 +2185,6 @@ let ReportDiagnosticAsError options (diagnostic, severity) =
 //----------------------------------------------------------------------------
 // Scoped #nowarn pragmas
 
-
 /// Build an DiagnosticsLogger that delegates to another DiagnosticsLogger but filters warnings turned off by the given pragma declarations
 //
 // NOTE: we allow a flag to turn of strict file checking. This is because file names sometimes don't match due to use of
@@ -1968,23 +2193,31 @@ let ReportDiagnosticAsError options (diagnostic, severity) =
 // However this is indicative of a more systematic problem where source-line
 // sensitive operations (lexfilter and warning filtering) do not always
 // interact well with #line directives.
-type DiagnosticsLoggerFilteringByScopedPragmas (checkFile, scopedPragmas, diagnosticOptions:FSharpDiagnosticOptions, diagnosticsLogger: DiagnosticsLogger) =
+type DiagnosticsLoggerFilteringByScopedPragmas
+    (
+        checkFile,
+        scopedPragmas,
+        diagnosticOptions: FSharpDiagnosticOptions,
+        diagnosticsLogger: DiagnosticsLogger
+    ) =
     inherit DiagnosticsLogger("DiagnosticsLoggerFilteringByScopedPragmas")
 
-    override _.DiagnosticSink (diagnostic, severity) =
+    override _.DiagnosticSink(diagnostic, severity) =
         if severity = FSharpDiagnosticSeverity.Error then
-            diagnosticsLogger.DiagnosticSink (diagnostic, severity)
+            diagnosticsLogger.DiagnosticSink(diagnostic, severity)
         else
             let report =
                 let warningNum = GetDiagnosticNumber diagnostic
+
                 match GetRangeOfDiagnostic diagnostic with
                 | Some m ->
                     scopedPragmas
                     |> List.exists (fun pragma ->
-                        let (ScopedPragma.WarningOff(pragmaRange, warningNumFromPragma)) = pragma
-                        warningNum = warningNumFromPragma &&
-                        (not checkFile || m.FileIndex = pragmaRange.FileIndex) &&
-                        posGeq m.Start pragmaRange.Start)
+                        let (ScopedPragma.WarningOff (pragmaRange, warningNumFromPragma)) = pragma
+
+                        warningNum = warningNumFromPragma
+                        && (not checkFile || m.FileIndex = pragmaRange.FileIndex)
+                        && posGeq m.Start pragmaRange.Start)
                     |> not
                 | None -> true
 
@@ -1998,5 +2231,5 @@ type DiagnosticsLoggerFilteringByScopedPragmas (checkFile, scopedPragmas, diagno
 
     override _.ErrorCount = diagnosticsLogger.ErrorCount
 
-let GetDiagnosticsLoggerFilteringByScopedPragmas(checkFile, scopedPragmas, diagnosticOptions, diagnosticsLogger) =
+let GetDiagnosticsLoggerFilteringByScopedPragmas (checkFile, scopedPragmas, diagnosticOptions, diagnosticsLogger) =
     DiagnosticsLoggerFilteringByScopedPragmas(checkFile, scopedPragmas, diagnosticOptions, diagnosticsLogger) :> DiagnosticsLogger
diff --git a/src/Compiler/Driver/CompilerImports.fs b/src/Compiler/Driver/CompilerImports.fs
index 21604212b79..227e23f5b32 100644
--- a/src/Compiler/Driver/CompilerImports.fs
+++ b/src/Compiler/Driver/CompilerImports.fs
@@ -45,48 +45,53 @@ open FSharp.Compiler.TypeProviders
 open FSharp.Core.CompilerServices
 #endif
 
-let (++) x s = x @ [s]
+let (++) x s = x @ [ s ]
 
 //----------------------------------------------------------------------------
 // Signature and optimization data blobs
 //--------------------------------------------------------------------------
 
 let IsSignatureDataResource (r: ILResource) =
-    r.Name.StartsWithOrdinal FSharpSignatureDataResourceName ||
-    r.Name.StartsWithOrdinal FSharpSignatureDataResourceName2
+    r.Name.StartsWithOrdinal FSharpSignatureDataResourceName
+    || r.Name.StartsWithOrdinal FSharpSignatureDataResourceName2
 
 let IsOptimizationDataResource (r: ILResource) =
-    r.Name.StartsWithOrdinal FSharpOptimizationDataResourceName||
-    r.Name.StartsWithOrdinal FSharpOptimizationDataResourceName2
+    r.Name.StartsWithOrdinal FSharpOptimizationDataResourceName
+    || r.Name.StartsWithOrdinal FSharpOptimizationDataResourceName2
 
 let GetSignatureDataResourceName (r: ILResource) =
     if r.Name.StartsWithOrdinal FSharpSignatureDataResourceName then
         String.dropPrefix r.Name FSharpSignatureDataResourceName
     elif r.Name.StartsWithOrdinal FSharpSignatureDataResourceName2 then
         String.dropPrefix r.Name FSharpSignatureDataResourceName2
-    else failwith "GetSignatureDataResourceName"
+    else
+        failwith "GetSignatureDataResourceName"
 
 let GetOptimizationDataResourceName (r: ILResource) =
     if r.Name.StartsWithOrdinal FSharpOptimizationDataResourceName then
         String.dropPrefix r.Name FSharpOptimizationDataResourceName
     elif r.Name.StartsWithOrdinal FSharpOptimizationDataResourceName2 then
         String.dropPrefix r.Name FSharpOptimizationDataResourceName2
-    else failwith "GetOptimizationDataResourceName"
+    else
+        failwith "GetOptimizationDataResourceName"
 
 let IsReflectedDefinitionsResource (r: ILResource) =
     r.Name.StartsWithOrdinal(QuotationPickler.SerializedReflectedDefinitionsResourceNameBase)
 
 let MakeILResource rName bytes =
-    { Name = rName
-      Location = ILResourceLocation.Local(ByteStorage.FromByteArray(bytes))
-      Access = ILResourceAccess.Public
-      CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
-      MetadataIndex = NoMetadataIdx }
+    {
+        Name = rName
+        Location = ILResourceLocation.Local(ByteStorage.FromByteArray(bytes))
+        Access = ILResourceAccess.Public
+        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+        MetadataIndex = NoMetadataIdx
+    }
 
 let PickleToResource inMem file (g: TcGlobals) scope rName p x =
     let file = PathMap.apply g.pathMap file
 
     let bytes = pickleObjWithDanglingCcus inMem file g scope p x
+
     let byteStorage =
         if inMem then
             ByteStorage.FromMemoryAndCopy(bytes.AsMemory(), useBackingMemoryMappedFile = true)
@@ -95,73 +100,109 @@ let PickleToResource inMem file (g: TcGlobals) scope rName p x =
 
     (bytes :> IDisposable).Dispose()
 
-    { Name = rName
-      Location = ILResourceLocation.Local(byteStorage)
-      Access = ILResourceAccess.Public
-      CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
-      MetadataIndex = NoMetadataIdx }
+    {
+        Name = rName
+        Location = ILResourceLocation.Local(byteStorage)
+        Access = ILResourceAccess.Public
+        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+        MetadataIndex = NoMetadataIdx
+    }
 
 let GetSignatureData (file, ilScopeRef, ilModule, byteReader) : PickledDataWithReferences =
-    unpickleObjWithDanglingCcus file ilScopeRef ilModule unpickleCcuInfo (byteReader())
+    unpickleObjWithDanglingCcus file ilScopeRef ilModule unpickleCcuInfo (byteReader ())
 
 let WriteSignatureData (tcConfig: TcConfig, tcGlobals, exportRemapping, ccu: CcuThunk, fileName, inMem) : ILResource =
     let mspec = ccu.Contents
     let mspec = ApplyExportRemappingToEntity tcGlobals exportRemapping mspec
     // For historical reasons, we use a different resource name for FSharp.Core, so older F# compilers
     // don't complain when they see the resource.
-    let rName = if ccu.AssemblyName = getFSharpCoreLibraryName then FSharpSignatureDataResourceName2 else FSharpSignatureDataResourceName
+    let rName =
+        if ccu.AssemblyName = getFSharpCoreLibraryName then
+            FSharpSignatureDataResourceName2
+        else
+            FSharpSignatureDataResourceName
 
     let includeDir =
-        if String.IsNullOrEmpty tcConfig.implicitIncludeDir then ""
+        if String.IsNullOrEmpty tcConfig.implicitIncludeDir then
+            ""
         else
             tcConfig.implicitIncludeDir
             |> FileSystem.GetFullPathShim
             |> PathMap.applyDir tcGlobals.pathMap
 
-    PickleToResource inMem fileName tcGlobals ccu (rName+ccu.AssemblyName) pickleCcuInfo
-        { mspec=mspec
-          compileTimeWorkingDir=includeDir
-          usesQuotations = ccu.UsesFSharp20PlusQuotations }
+    PickleToResource
+        inMem
+        fileName
+        tcGlobals
+        ccu
+        (rName + ccu.AssemblyName)
+        pickleCcuInfo
+        {
+            mspec = mspec
+            compileTimeWorkingDir = includeDir
+            usesQuotations = ccu.UsesFSharp20PlusQuotations
+        }
 
 let GetOptimizationData (file, ilScopeRef, ilModule, byteReader) =
-    unpickleObjWithDanglingCcus file ilScopeRef ilModule Optimizer.u_CcuOptimizationInfo (byteReader())
+    unpickleObjWithDanglingCcus file ilScopeRef ilModule Optimizer.u_CcuOptimizationInfo (byteReader ())
 
 let WriteOptimizationData (tcGlobals, fileName, inMem, ccu: CcuThunk, modulInfo) =
     // For historical reasons, we use a different resource name for FSharp.Core, so older F# compilers
     // don't complain when they see the resource.
-    let rName = if ccu.AssemblyName = getFSharpCoreLibraryName then FSharpOptimizationDataResourceName2 else FSharpOptimizationDataResourceName
-    PickleToResource inMem fileName tcGlobals ccu (rName+ccu.AssemblyName) Optimizer.p_CcuOptimizationInfo modulInfo
+    let rName =
+        if ccu.AssemblyName = getFSharpCoreLibraryName then
+            FSharpOptimizationDataResourceName2
+        else
+            FSharpOptimizationDataResourceName
 
-let EncodeSignatureData(tcConfig: TcConfig, tcGlobals, exportRemapping, generatedCcu, outfile, isIncrementalBuild) =
+    PickleToResource inMem fileName tcGlobals ccu (rName + ccu.AssemblyName) Optimizer.p_CcuOptimizationInfo modulInfo
+
+let EncodeSignatureData (tcConfig: TcConfig, tcGlobals, exportRemapping, generatedCcu, outfile, isIncrementalBuild) =
     if tcConfig.GenerateSignatureData then
-        let resource = WriteSignatureData (tcConfig, tcGlobals, exportRemapping, generatedCcu, outfile, isIncrementalBuild)
+        let resource =
+            WriteSignatureData(tcConfig, tcGlobals, exportRemapping, generatedCcu, outfile, isIncrementalBuild)
         // The resource gets written to a file for FSharp.Core
-        let useDataFiles = (tcConfig.useOptimizationDataFile || tcGlobals.compilingFSharpCore) && not isIncrementalBuild
+        let useDataFiles =
+            (tcConfig.useOptimizationDataFile || tcGlobals.compilingFSharpCore)
+            && not isIncrementalBuild
 
         if useDataFiles then
-            let sigDataFileName = (FileSystemUtils.chopExtension outfile)+".sigdata"
+            let sigDataFileName = (FileSystemUtils.chopExtension outfile) + ".sigdata"
             let bytes = resource.GetBytes()
-            use fileStream = FileSystem.OpenFileForWriteShim(sigDataFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
+
+            use fileStream =
+                FileSystem.OpenFileForWriteShim(sigDataFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
 
             bytes.CopyTo fileStream
-        let resources =
-            [ resource ]
-        let sigAttr = mkSignatureDataVersionAttr tcGlobals (parseILVersion FSharpBinaryMetadataFormatRevision)
-        [sigAttr], resources
-      else
+
+        let resources = [ resource ]
+
+        let sigAttr =
+            mkSignatureDataVersionAttr tcGlobals (parseILVersion FSharpBinaryMetadataFormatRevision)
+
+        [ sigAttr ], resources
+    else
         [], []
 
-let EncodeOptimizationData(tcGlobals, tcConfig: TcConfig, outfile, exportRemapping, data, isIncrementalBuild) =
+let EncodeOptimizationData (tcGlobals, tcConfig: TcConfig, outfile, exportRemapping, data, isIncrementalBuild) =
     if tcConfig.GenerateOptimizationData then
         let data = map2Of2 (Optimizer.RemapOptimizationInfo tcGlobals exportRemapping) data
         // As with the sigdata file, the optdata gets written to a file for FSharp.Core
-        let useDataFiles = (tcConfig.useOptimizationDataFile || tcGlobals.compilingFSharpCore) && not isIncrementalBuild
+        let useDataFiles =
+            (tcConfig.useOptimizationDataFile || tcGlobals.compilingFSharpCore)
+            && not isIncrementalBuild
 
         if useDataFiles then
             let ccu, modulInfo = data
-            let bytes = pickleObjWithDanglingCcus isIncrementalBuild outfile tcGlobals ccu Optimizer.p_CcuOptimizationInfo modulInfo
-            let optDataFileName = (FileSystemUtils.chopExtension outfile)+".optdata"
-            use fileStream = FileSystem.OpenFileForWriteShim(optDataFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
+
+            let bytes =
+                pickleObjWithDanglingCcus isIncrementalBuild outfile tcGlobals ccu Optimizer.p_CcuOptimizationInfo modulInfo
+
+            let optDataFileName = (FileSystemUtils.chopExtension outfile) + ".optdata"
+
+            use fileStream =
+                FileSystem.OpenFileForWriteShim(optDataFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
+
             fileStream.Write(bytes)
 
         let ccu, optData =
@@ -169,9 +210,10 @@ let EncodeOptimizationData(tcGlobals, tcConfig: TcConfig, outfile, exportRemappi
                 map2Of2 Optimizer.AbstractOptimizationInfoToEssentials data
             else
                 data
-        [ WriteOptimizationData (tcGlobals, outfile, isIncrementalBuild, ccu, optData) ]
+
+        [ WriteOptimizationData(tcGlobals, outfile, isIncrementalBuild, ccu, optData) ]
     else
-        [ ]
+        []
 
 exception AssemblyNotResolved of originalName: string * range: range
 
@@ -179,12 +221,14 @@ exception MSBuildReferenceResolutionWarning of message: string * warningCode: st
 
 exception MSBuildReferenceResolutionError of message: string * warningCode: string * range: range
 
-let OpenILBinary(fileName, reduceMemoryUsage, pdbDirPath, shadowCopyReferences, tryGetMetadataSnapshot) =
+let OpenILBinary (fileName, reduceMemoryUsage, pdbDirPath, shadowCopyReferences, tryGetMetadataSnapshot) =
     let opts: ILReaderOptions =
-        { metadataOnly = MetadataOnlyFlag.Yes
-          reduceMemoryUsage = reduceMemoryUsage
-          pdbDirPath = pdbDirPath
-          tryGetMetadataSnapshot = tryGetMetadataSnapshot }
+        {
+            metadataOnly = MetadataOnlyFlag.Yes
+            reduceMemoryUsage = reduceMemoryUsage
+            pdbDirPath = pdbDirPath
+            tryGetMetadataSnapshot = tryGetMetadataSnapshot
+        }
 
     let location =
 #if FX_NO_APP_DOMAINS
@@ -193,16 +237,20 @@ let OpenILBinary(fileName, reduceMemoryUsage, pdbDirPath, shadowCopyReferences,
         if shadowCopyReferences then
             try
                 System.Reflection.Assembly.ReflectionOnlyLoadFrom(fileName).Location
-            with _ -> fileName
+            with _ ->
+                fileName
         else
 #else
-            ignore shadowCopyReferences
+        ignore shadowCopyReferences
 #endif
-            fileName
+        fileName
+
     AssemblyReader.GetILModuleReader(location, opts)
 
 []
-type ResolveAssemblyReferenceMode = Speculative | ReportErrors
+type ResolveAssemblyReferenceMode =
+    | Speculative
+    | ReportErrors
 
 #if !NO_TYPEPROVIDERS
 type ResolvedExtensionReference = ResolvedExtensionReference of string * AssemblyReference list * Tainted list
@@ -212,22 +260,25 @@ type ResolvedExtensionReference = ResolvedExtensionReference of string * Assembl
 []
 #endif
 type AssemblyResolution =
-    {  /// The original reference to the assembly.
-       originalReference: AssemblyReference
+    {
+        /// The original reference to the assembly.
+        originalReference: AssemblyReference
+
+        /// Path to the resolvedFile
+        resolvedPath: string
 
-       /// Path to the resolvedFile
-       resolvedPath: string
+        /// Create the tooltip text for the assembly reference
+        prepareToolTip: unit -> string
 
-       /// Create the tooltip text for the assembly reference
-       prepareToolTip: unit -> string
+        /// Whether or not this is an installed system assembly (for example, System.dll)
+        sysdir: bool
 
-       /// Whether or not this is an installed system assembly (for example, System.dll)
-       sysdir: bool
+        /// Lazily populated ilAssemblyRef for this reference.
+        mutable ilAssemblyRef: ILAssemblyRef option
+    }
 
-       /// Lazily populated ilAssemblyRef for this reference.
-       mutable ilAssemblyRef: ILAssemblyRef option
-     }
-    override this.ToString() = sprintf "%s%s" (if this.sysdir then "[sys]" else "") this.resolvedPath
+    override this.ToString() =
+        sprintf "%s%s" (if this.sysdir then "[sys]" else "") this.resolvedPath
 
     member this.ProjectReference = this.originalReference.ProjectReference
 
@@ -245,36 +296,44 @@ type AssemblyResolution =
 
             let assemblyRef =
                 let readerSettings: ILReaderOptions =
-                    { pdbDirPath=None
-                      reduceMemoryUsage = reduceMemoryUsage
-                      metadataOnly = MetadataOnlyFlag.Yes
-                      tryGetMetadataSnapshot = tryGetMetadataSnapshot }
+                    {
+                        pdbDirPath = None
+                        reduceMemoryUsage = reduceMemoryUsage
+                        metadataOnly = MetadataOnlyFlag.Yes
+                        tryGetMetadataSnapshot = tryGetMetadataSnapshot
+                    }
+
                 use reader = OpenILModuleReader this.resolvedPath readerSettings
                 mkRefToILAssembly reader.ILModuleDef.ManifestOfAssembly
+
             this.ilAssemblyRef <- Some assemblyRef
             assemblyRef
 
 type ImportedBinary =
-    { FileName: string
-      RawMetadata: IRawFSharpAssemblyData
+    {
+        FileName: string
+        RawMetadata: IRawFSharpAssemblyData
 #if !NO_TYPEPROVIDERS
-      ProviderGeneratedAssembly: System.Reflection.Assembly option
-      IsProviderGenerated: bool
-      ProviderGeneratedStaticLinkMap: ProvidedAssemblyStaticLinkingMap option
+        ProviderGeneratedAssembly: System.Reflection.Assembly option
+        IsProviderGenerated: bool
+        ProviderGeneratedStaticLinkMap: ProvidedAssemblyStaticLinkingMap option
 #endif
-      ILAssemblyRefs: ILAssemblyRef list
-      ILScopeRef: ILScopeRef }
+        ILAssemblyRefs: ILAssemblyRef list
+        ILScopeRef: ILScopeRef
+    }
 
 type ImportedAssembly =
-    { ILScopeRef: ILScopeRef
-      FSharpViewOfMetadata: CcuThunk
-      AssemblyAutoOpenAttributes: string list
-      AssemblyInternalsVisibleToAttributes: string list
+    {
+        ILScopeRef: ILScopeRef
+        FSharpViewOfMetadata: CcuThunk
+        AssemblyAutoOpenAttributes: string list
+        AssemblyInternalsVisibleToAttributes: string list
 #if !NO_TYPEPROVIDERS
-      IsProviderGenerated: bool
-      mutable TypeProviders: Tainted list
+        IsProviderGenerated: bool
+        mutable TypeProviders: Tainted list
 #endif
-      FSharpOptimizationData: Microsoft.FSharp.Control.Lazy> }
+        FSharpOptimizationData: Microsoft.FSharp.Control.Lazy>
+    }
 
 type AvailableImportedAssembly =
     | ResolvedImportedAssembly of ImportedAssembly
@@ -285,87 +344,102 @@ type CcuLoadFailureAction =
     | ReturnNone
 
 type TcImportsLockToken() =
-   interface LockToken
+    interface LockToken
 
-type TcImportsLock = Lock   
+type TcImportsLock = Lock
 
 let RequireTcImportsLock (_tcitok: TcImportsLockToken, _thingProtected: 'T) = ()
 
+// if this is a #r reference (not from dummy range), make sure the directory of the declaring
+// file is included in the search path. This should ideally already be one of the search paths, but
+// during some global checks it won't be. We append to the end of the search list so that this is the last
+// place that is checked.
+let isHashRReference (r: range) =
+    not (equals r range0)
+    && not (equals r rangeStartup)
+    && not (equals r rangeCmdArgs)
+    && FileSystem.IsPathRootedShim r.FileName
+
+let IsNetModule fileName =
+    let ext = Path.GetExtension fileName
+    String.Compare(ext, ".netmodule", StringComparison.OrdinalIgnoreCase) = 0
+
+let IsDLL fileName =
+    let ext = Path.GetExtension fileName
+    String.Compare(ext, ".dll", StringComparison.OrdinalIgnoreCase) = 0
+
+let IsExe fileName =
+    let ext = Path.GetExtension fileName
+    String.Compare(ext, ".exe", StringComparison.OrdinalIgnoreCase) = 0
+
 type TcConfig with
 
-    member tcConfig.TryResolveLibWithDirectories (r: AssemblyReference) =
+    member tcConfig.TryResolveLibWithDirectories(r: AssemblyReference) =
         let m, nm = r.Range, r.Text
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
-        // Only want to resolve certain extensions (otherwise, 'System.Xml' is ambiguous).
-        // MSBuild resolution is limited to .exe and .dll so do the same here.
-        let ext = Path.GetExtension nm
-        let isNetModule = String.Compare(ext, ".netmodule", StringComparison.OrdinalIgnoreCase)=0
 
         // See if the language service has already produced the contents of the assembly for us, virtually
         match r.ProjectReference with
         | Some _ ->
             let resolved = r.Text
             let sysdir = tcConfig.IsSystemAssembly resolved
+
             Some
-                { originalReference = r
-                  resolvedPath = resolved
-                  prepareToolTip = (fun () -> resolved)
-                  sysdir = sysdir
-                  ilAssemblyRef = None }
+                {
+                    originalReference = r
+                    resolvedPath = resolved
+                    prepareToolTip = (fun () -> resolved)
+                    sysdir = sysdir
+                    ilAssemblyRef = None
+                }
         | None ->
 
-        if String.Compare(ext, ".dll", StringComparison.OrdinalIgnoreCase)=0
-           || String.Compare(ext, ".exe", StringComparison.OrdinalIgnoreCase)=0
-           || isNetModule then
-
-            let searchPaths =
-                seq {
-                    yield! tcConfig.GetSearchPathsForLibraryFiles()
-
-                    // if this is a #r reference (not from dummy range), make sure the directory of the declaring
-                    // file is included in the search path. This should ideally already be one of the search paths, but
-                    // during some global checks it won't be. We append to the end of the search list so that this is the last
-                    // place that is checked.
-                    let isPoundRReference (r: range) =
-                        not (equals r range0) &&
-                        not (equals r rangeStartup) &&
-                        not (equals r rangeCmdArgs) &&
-                        FileSystem.IsPathRootedShim r.FileName
-
-                    if isPoundRReference m then
-                        yield Path.GetDirectoryName(m.FileName)
-                }
+            // Only want to resolve certain extensions (otherwise, 'System.Xml' is ambiguous).
+            // MSBuild resolution is limited to .exe and .dll so do the same here.
+            if IsDLL nm || IsExe nm || IsNetModule nm then
 
-            let resolved = TryResolveFileUsingPaths(searchPaths, m, nm)
-            match resolved with
-            | Some resolved ->
-                let sysdir = tcConfig.IsSystemAssembly resolved
-                Some
-                    { originalReference = r
-                      resolvedPath = resolved
-                      prepareToolTip = (fun () ->
-                            let fusionName = System.Reflection.AssemblyName.GetAssemblyName(resolved).ToString()
-                            let line(append: string) = append.Trim([|' '|])+"\n"
-                            line resolved + line fusionName)
-                      sysdir = sysdir
-                      ilAssemblyRef = None }
-            | None -> None
-        else None
+                let searchPaths =
+                    seq {
+                        yield! tcConfig.GetSearchPathsForLibraryFiles()
+
+                        if isHashRReference m then Path.GetDirectoryName(m.FileName)
+                    }
 
-    member tcConfig.ResolveLibWithDirectories (ccuLoadFailureAction, r: AssemblyReference) =
+                let resolved = TryResolveFileUsingPaths(searchPaths, m, nm)
+
+                match resolved with
+                | Some resolved ->
+                    let sysdir = tcConfig.IsSystemAssembly resolved
+
+                    Some
+                        {
+                            originalReference = r
+                            resolvedPath = resolved
+                            prepareToolTip =
+                                (fun () ->
+                                    let fusionName = System.Reflection.AssemblyName.GetAssemblyName(resolved).ToString()
+                                    let line (append: string) = append.Trim(' ') + "\n"
+                                    line resolved + line fusionName)
+                            sysdir = sysdir
+                            ilAssemblyRef = None
+                        }
+                | None -> None
+            else
+                None
+
+    member tcConfig.ResolveLibWithDirectories(ccuLoadFailureAction, r: AssemblyReference) =
         let m, nm = r.Range, r.Text
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
-        // test for both libraries and executables
-        let ext = Path.GetExtension nm
-        let isExe = (String.Compare(ext, ".exe", StringComparison.OrdinalIgnoreCase) = 0)
-        let isDLL = (String.Compare(ext, ".dll", StringComparison.OrdinalIgnoreCase) = 0)
-        let isNetModule = (String.Compare(ext, ".netmodule", StringComparison.OrdinalIgnoreCase) = 0)
 
         let rs =
-            if isExe || isDLL || isNetModule then
-                [r]
+            if IsExe nm || IsDLL nm || IsNetModule nm then
+                [ r ]
             else
-                [AssemblyReference(m, nm+".dll", None);AssemblyReference(m, nm+".exe", None);AssemblyReference(m, nm+".netmodule", None)]
+                [
+                    AssemblyReference(m, nm + ".dll", None)
+                    AssemblyReference(m, nm + ".exe", None)
+                    AssemblyReference(m, nm + ".netmodule", None)
+                ]
 
         match rs |> List.tryPick (fun r -> tcConfig.TryResolveLibWithDirectories(r)) with
         | Some res -> Some res
@@ -376,29 +450,30 @@ type TcConfig with
                 raise (FileNameNotResolved(nm, searchMessage, m))
             | CcuLoadFailureAction.ReturnNone -> None
 
-    member tcConfig.MsBuildResolve (references, mode, errorAndWarningRange, showMessages) =
+    member tcConfig.MsBuildResolve(references, mode, errorAndWarningRange, showMessages) =
         let logMessage showMessages =
-            if showMessages && tcConfig.showReferenceResolutions then (fun (message: string)->dprintf "%s\n" message)
-            else ignore
+            if showMessages && tcConfig.showReferenceResolutions then
+                (fun (message: string) -> printfn "%s" message)
+            else
+                ignore
 
         let logDiagnostic showMessages =
-            (fun isError code message->
+            (fun isError code message ->
                 if showMessages && mode = ResolveAssemblyReferenceMode.ReportErrors then
-                  if isError then
-                    errorR(MSBuildReferenceResolutionError(code, message, errorAndWarningRange))
-                  else
-                    match code with
-                    // These are warnings that mean 'not resolved' for some assembly.
-                    // Note that we don't get to know the name of the assembly that couldn't be resolved.
-                    // Ignore these and rely on the logic below to emit an error for each unresolved reference.
-                    | "MSB3246" // Resolved file has a bad image, no metadata, or is otherwise inaccessible.
-                    | "MSB3106"
-                        -> ()
-                    | _ ->
-                        if code = "MSB3245" then
-                            errorR(MSBuildReferenceResolutionWarning(code, message, errorAndWarningRange))
-                        else
-                            warning(MSBuildReferenceResolutionWarning(code, message, errorAndWarningRange)))
+                    if isError then
+                        errorR (MSBuildReferenceResolutionError(code, message, errorAndWarningRange))
+                    else
+                        match code with
+                        // These are warnings that mean 'not resolved' for some assembly.
+                        // Note that we don't get to know the name of the assembly that couldn't be resolved.
+                        // Ignore these and rely on the logic below to emit an error for each unresolved reference.
+                        | "MSB3246" // Resolved file has a bad image, no metadata, or is otherwise inaccessible.
+                        | "MSB3106" -> ()
+                        | _ ->
+                            if code = "MSB3245" then
+                                errorR (MSBuildReferenceResolutionWarning(code, message, errorAndWarningRange))
+                            else
+                                warning (MSBuildReferenceResolutionWarning(code, message, errorAndWarningRange)))
 
         let targetProcessorArchitecture =
             match tcConfig.platform with
@@ -410,8 +485,8 @@ type TcConfig with
             | Some IA64 -> "ia64"
 
         try
-            tcConfig.legacyReferenceResolver.Impl.Resolve
-                (tcConfig.resolutionEnvironment,
+            tcConfig.legacyReferenceResolver.Impl.Resolve(
+                tcConfig.resolutionEnvironment,
                 references,
                 tcConfig.targetFrameworkVersion,
                 tcConfig.GetTargetFrameworkDirectories(),
@@ -419,226 +494,322 @@ type TcConfig with
                 tcConfig.fsharpBinariesDir, // FSharp binaries directory
                 tcConfig.includes, // Explicit include directories
                 tcConfig.implicitIncludeDir, // Implicit include directory (likely the project directory)
-                logMessage showMessages, logDiagnostic showMessages)
-        with
-            | LegacyResolutionFailure -> error(Error(FSComp.SR.buildAssemblyResolutionFailed(), errorAndWarningRange))
-
+                logMessage showMessages,
+                logDiagnostic showMessages
+            )
+        with LegacyResolutionFailure ->
+            error (Error(FSComp.SR.buildAssemblyResolutionFailed (), errorAndWarningRange))
 
     // NOTE!! if mode=Speculative then this method must not report ANY warnings or errors through 'warning' or 'error'. Instead
     // it must return warnings and errors as data
     //
     // NOTE!! if mode=ReportErrors then this method must not raise exceptions. It must just report the errors and recover
-    static member TryResolveLibsUsingMSBuildRules (tcConfig: TcConfig,
+    static member TryResolveLibsUsingMSBuildRules
+        (
+            tcConfig: TcConfig,
             originalReferences: AssemblyReference list,
             errorAndWarningRange: range,
-            mode: ResolveAssemblyReferenceMode) : AssemblyResolution list * UnresolvedAssemblyReference list =
+            mode: ResolveAssemblyReferenceMode
+        ) : AssemblyResolution list * UnresolvedAssemblyReference list =
 
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
+
         if tcConfig.useSimpleResolution then
             failwith "MSBuild resolution is not supported."
-        if originalReferences=[] then [], []
+
+        if originalReferences = [] then
+            [], []
         else
             // Group references by name with range values in the grouped value list.
             // In the grouped reference, store the index of the last use of the reference.
             let groupedReferences =
                 originalReferences
                 |> List.indexed
-                |> Seq.groupBy(fun (_, reference) -> reference.Text)
-                |> Seq.map(fun (assemblyName, assemblyAndIndexGroup)->
+                |> List.groupBy (fun (_, reference) -> reference.Text)
+                |> List.map (fun (assemblyName, assemblyAndIndexGroup) ->
                     let assemblyAndIndexGroup = assemblyAndIndexGroup |> List.ofSeq
                     let highestPosition = assemblyAndIndexGroup |> List.maxBy fst |> fst
                     let assemblyGroup = assemblyAndIndexGroup |> List.map snd
                     assemblyName, highestPosition, assemblyGroup)
-                |> Array.ofSeq
+                |> Array.ofList
 
             // First, try to resolve everything as a file using simple resolution
             let resolvedAsFile =
-                groupedReferences
-                |> Array.map(fun (_filename, maxIndexOfReference, references)->
-                                let assemblyResolution = references |> List.choose (fun r -> tcConfig.TryResolveLibWithDirectories r)
-                                (maxIndexOfReference, assemblyResolution))
-                |> Array.filter(fun (_, refs)->refs |> isNil |> not)
+                [|
+                    for (_filename, maxIndexOfReference, references) in groupedReferences do
+                        let assemblyResolution =
+                            references |> List.choose (fun r -> tcConfig.TryResolveLibWithDirectories r)
+
+                        if not assemblyResolution.IsEmpty then
+                            (maxIndexOfReference, assemblyResolution)
+                |]
+
+            let toMsBuild =
+                [|
+                    for i in 0 .. groupedReferences.Length - 1 do
+                        let ref, i0, _ = groupedReferences[i]
 
-            let toMsBuild = [|0..groupedReferences.Length-1|]
-                             |> Array.map(fun i->(p13 groupedReferences[i]), (p23 groupedReferences[i]), i)
-                             |> Array.filter (fun (_, i0, _)->resolvedAsFile|>Array.exists(fun (i1, _) -> i0=i1)|>not)
-                             |> Array.map(fun (ref, _, i)->ref, string i)
+                        if resolvedAsFile |> Array.exists (fun (i1, _) -> i0 = i1) |> not then
+                            ref, string i
+                |]
 
-            let resolutions = tcConfig.MsBuildResolve(toMsBuild, mode, errorAndWarningRange, (*showMessages*)true)
+            let resolutions =
+                tcConfig.MsBuildResolve(toMsBuild, mode, errorAndWarningRange, true)
 
             // Map back to original assembly resolutions.
             let resolvedByMsbuild =
-                resolutions
-                    |> Array.map(fun resolvedFile ->
-                                    let i = int resolvedFile.baggage
-                                    let _, maxIndexOfReference, ms = groupedReferences[i]
-                                    let assemblyResolutions =
-                                        ms|>List.map(fun originalReference ->
-                                                    Debug.Assert(FileSystem.IsPathRootedShim(resolvedFile.itemSpec), sprintf "msbuild-resolved path is not absolute: '%s'" resolvedFile.itemSpec)
-                                                    let canonicalItemSpec = FileSystem.GetFullPathShim(resolvedFile.itemSpec)
-                                                    { originalReference=originalReference
-                                                      resolvedPath=canonicalItemSpec
-                                                      prepareToolTip = (fun () -> resolvedFile.prepareToolTip (originalReference.Text, canonicalItemSpec))
-                                                      sysdir= tcConfig.IsSystemAssembly canonicalItemSpec
-                                                      ilAssemblyRef = None })
-                                    (maxIndexOfReference, assemblyResolutions))
+                [|
+                    for resolvedFile in resolutions do
+                        let i = int resolvedFile.baggage
+                        let _, maxIndexOfReference, ms = groupedReferences[i]
+
+                        let assemblyResolutions =
+                            [
+                                for originalReference in ms do
+                                    let canonicalItemSpec = FileSystem.GetFullPathShim(resolvedFile.itemSpec)
+
+                                    {
+                                        originalReference = originalReference
+                                        resolvedPath = canonicalItemSpec
+                                        prepareToolTip = (fun () -> resolvedFile.prepareToolTip (originalReference.Text, canonicalItemSpec))
+                                        sysdir = tcConfig.IsSystemAssembly canonicalItemSpec
+                                        ilAssemblyRef = None
+                                    }
+                            ]
+
+                        (maxIndexOfReference, assemblyResolutions)
+                |]
 
             // When calculating the resulting resolutions, we're going to use the index of the reference
             // in the original specification and resort it to match the ordering that we had.
             let resultingResolutions =
-                    [resolvedByMsbuild;resolvedAsFile]
-                    |> Array.concat
-                    |> Array.sortBy fst
-                    |> Array.map snd
-                    |> List.ofArray
-                    |> List.concat
+                [ resolvedByMsbuild; resolvedAsFile ]
+                |> Array.concat
+                |> Array.sortBy fst
+                |> Array.map snd
+                |> List.ofArray
+                |> List.concat
 
             // O(N^2) here over a small set of referenced assemblies.
-            let IsResolved(originalName: string) =
-                if resultingResolutions |> List.exists(fun resolution -> resolution.originalReference.Text = originalName) then true
+            let IsResolved (originalName: string) =
+                if resultingResolutions
+                   |> List.exists (fun resolution -> resolution.originalReference.Text = originalName) then
+                    true
                 else
                     // MSBuild resolution may have unified the result of two duplicate references. Try to re-resolve now.
                     // If re-resolution worked then this was a removed duplicate.
-                    tcConfig.MsBuildResolve([|originalName, ""|], mode, errorAndWarningRange, (*showMessages*)false).Length<>0
+                    let references = [| (originalName, "") |]
+
+                    let resolutions =
+                        tcConfig.MsBuildResolve(references, mode, errorAndWarningRange, false)
+
+                    resolutions.Length <> 0
 
             let unresolvedReferences =
-                    groupedReferences
-                    //|> Array.filter(p13 >> IsNotFileOrIsAssembly)
-                    |> Array.filter(p13 >> IsResolved >> not)
-                    |> List.ofArray
+                groupedReferences |> Array.filter (p13 >> IsResolved >> not) |> List.ofArray
+
+            let unresolved =
+                [
+                    for (name, _, r) in unresolvedReferences -> UnresolvedAssemblyReference(name, r)
+                ]
 
             // If mode=Speculative, then we haven't reported any errors.
             // We report the error condition by returning an empty list of resolutions
-            if mode = ResolveAssemblyReferenceMode.Speculative && (List.length unresolvedReferences) > 0 then
-                [], (List.ofArray groupedReferences) |> List.map (fun (name, _, r) -> (name, r)) |> List.map UnresolvedAssemblyReference
+            if mode = ResolveAssemblyReferenceMode.Speculative
+               && unresolvedReferences.Length > 0 then
+                [], unresolved
             else
-                resultingResolutions, unresolvedReferences |> List.map (fun (name, _, r) -> (name, r)) |> List.map UnresolvedAssemblyReference
+                resultingResolutions, unresolved
 
 []
 type TcAssemblyResolutions(tcConfig: TcConfig, results: AssemblyResolution list, unresolved: UnresolvedAssemblyReference list) =
 
-    let originalReferenceToResolution = results |> List.map (fun r -> r.originalReference.Text, r) |> Map.ofList
-    let resolvedPathToResolution = results |> List.map (fun r -> r.resolvedPath, r) |> Map.ofList
+    let originalReferenceToResolution =
+        results |> List.map (fun r -> r.originalReference.Text, r) |> Map.ofList
+
+    let resolvedPathToResolution =
+        results |> List.map (fun r -> r.resolvedPath, r) |> Map.ofList
 
     /// Add some resolutions to the map of resolution results.
-    member _.AddResolutionResults newResults = TcAssemblyResolutions(tcConfig, results @ newResults, unresolved)
+    member _.AddResolutionResults newResults =
+        TcAssemblyResolutions(tcConfig, results @ newResults, unresolved)
 
     /// Add some unresolved results.
-    member _.AddUnresolvedReferences newUnresolved = TcAssemblyResolutions(tcConfig, results, unresolved @ newUnresolved)
+    member _.AddUnresolvedReferences newUnresolved =
+        TcAssemblyResolutions(tcConfig, results, unresolved @ newUnresolved)
 
     /// Get information about referenced DLLs
     member _.GetAssemblyResolutions() = results
 
     member _.GetUnresolvedReferences() = unresolved
 
-    member _.TryFindByOriginalReference(assemblyReference: AssemblyReference) = originalReferenceToResolution.TryFind assemblyReference.Text
+    member _.TryFindByOriginalReference(assemblyReference: AssemblyReference) =
+        originalReferenceToResolution.TryFind assemblyReference.Text
 
     /// Only used by F# Interactive
     member _.TryFindByExactILAssemblyRef assemblyRef =
-        results |> List.tryFind (fun ar->
-            let r = ar.GetILAssemblyRef(tcConfig.reduceMemoryUsage, tcConfig.tryGetMetadataSnapshot)
+        results
+        |> List.tryFind (fun ar ->
+            let r =
+                ar.GetILAssemblyRef(tcConfig.reduceMemoryUsage, tcConfig.tryGetMetadataSnapshot)
+
             r = assemblyRef)
 
     /// Only used by F# Interactive
     member _.TryFindBySimpleAssemblyName simpleAssemName =
-        results |> List.tryFind (fun ar->
-            let r = ar.GetILAssemblyRef(tcConfig.reduceMemoryUsage, tcConfig.tryGetMetadataSnapshot)
+        results
+        |> List.tryFind (fun ar ->
+            let r =
+                ar.GetILAssemblyRef(tcConfig.reduceMemoryUsage, tcConfig.tryGetMetadataSnapshot)
+
             r.Name = simpleAssemName)
 
     member _.TryFindByResolvedPath nm = resolvedPathToResolution.TryFind nm
 
-    member _.TryFindByOriginalReferenceText nm = originalReferenceToResolution.TryFind nm
+    member _.TryFindByOriginalReferenceText nm =
+        originalReferenceToResolution.TryFind nm
 
-    static member ResolveAssemblyReferences (tcConfig: TcConfig, assemblyList: AssemblyReference list, knownUnresolved: UnresolvedAssemblyReference list) : TcAssemblyResolutions =
+    static member ResolveAssemblyReferences
+        (
+            tcConfig: TcConfig,
+            assemblyList: AssemblyReference list,
+            knownUnresolved: UnresolvedAssemblyReference list
+        ) : TcAssemblyResolutions =
         let resolved, unresolved =
             if tcConfig.useSimpleResolution then
                 let resolutions =
                     assemblyList
                     |> List.map (fun assemblyReference ->
-                           try
-                               Choice1Of2 (tcConfig.ResolveLibWithDirectories (CcuLoadFailureAction.RaiseError, assemblyReference) |> Option.get)
-                           with e ->
-                               errorRecovery e assemblyReference.Range
-                               Choice2Of2 assemblyReference)
-                let successes = resolutions |> List.choose (function Choice1Of2 x -> Some x | _ -> None)
-                let failures = resolutions |> List.choose (function Choice2Of2 x -> Some (UnresolvedAssemblyReference(x.Text, [x])) | _ -> None)
+                        try
+                            let resolutionOpt =
+                                tcConfig.ResolveLibWithDirectories(CcuLoadFailureAction.RaiseError, assemblyReference)
+
+                            Choice1Of2 resolutionOpt.Value
+                        with e ->
+                            errorRecovery e assemblyReference.Range
+                            Choice2Of2 assemblyReference)
+
+                let successes =
+                    resolutions
+                    |> List.choose (function
+                        | Choice1Of2 x -> Some x
+                        | _ -> None)
+
+                let failures =
+                    resolutions
+                    |> List.choose (function
+                        | Choice2Of2 x -> Some(UnresolvedAssemblyReference(x.Text, [ x ]))
+                        | _ -> None)
+
                 successes, failures
             else
                 // we don't want to do assembly resolution concurrently, we assume MSBuild doesn't handle this
-                TcConfig.TryResolveLibsUsingMSBuildRules (tcConfig, assemblyList, rangeStartup, ResolveAssemblyReferenceMode.ReportErrors)
+                TcConfig.TryResolveLibsUsingMSBuildRules(tcConfig, assemblyList, rangeStartup, ResolveAssemblyReferenceMode.ReportErrors)
+
         TcAssemblyResolutions(tcConfig, resolved, unresolved @ knownUnresolved)
 
-    static member GetAllDllReferences (tcConfig: TcConfig) = [
+    static member GetAllDllReferences(tcConfig: TcConfig) =
+        [
             let primaryReference = tcConfig.PrimaryAssemblyDllReference()
 
             let assumeDotNetFramework = primaryReference.SimpleAssemblyNameIs("mscorlib")
 
             if not tcConfig.compilingFSharpCore then
-                yield tcConfig.CoreLibraryDllReference()
+                tcConfig.CoreLibraryDllReference()
+
                 if assumeDotNetFramework then
                     // When building desktop then we need these additional dependencies
-                    yield AssemblyReference(rangeStartup, "System.Numerics.dll", None)
-                    yield AssemblyReference(rangeStartup, "System.dll", None)
+                    AssemblyReference(rangeStartup, "System.Numerics.dll", None)
+                    AssemblyReference(rangeStartup, "System.dll", None)
                     let asm = AssemblyReference(rangeStartup, "netstandard.dll", None)
+
                     let found =
                         if tcConfig.useSimpleResolution then
-                            match tcConfig.ResolveLibWithDirectories (CcuLoadFailureAction.ReturnNone, asm) with
+                            match tcConfig.ResolveLibWithDirectories(CcuLoadFailureAction.ReturnNone, asm) with
                             | Some _ -> true
                             | None -> false
                         else
-                            let resolutions = tcConfig.MsBuildResolve([|asm.Text, ""|], ResolveAssemblyReferenceMode.Speculative, rangeStartup, (*showMessages*)false)
+                            let references = [| (asm.Text, "") |]
+
+                            let resolutions =
+                                tcConfig.MsBuildResolve(references, ResolveAssemblyReferenceMode.Speculative, rangeStartup, false)
+
                             resolutions.Length = 1
-                    if found then yield asm
+
+                    if found then asm
 
             if tcConfig.implicitlyReferenceDotNetAssemblies then
-                let references, _useDotNetFramework = tcConfig.FxResolver.GetDefaultReferences(tcConfig.useFsiAuxLib)
+                let references, _useDotNetFramework =
+                    tcConfig.FxResolver.GetDefaultReferences(tcConfig.useFsiAuxLib)
+
                 for s in references do
-                    yield AssemblyReference(rangeStartup, (if s.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) then s else s+".dll"), None)
+                    let referenceText =
+                        if s.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) then
+                            s
+                        else
+                            s + ".dll"
+
+                    AssemblyReference(rangeStartup, referenceText, None)
 
             yield! tcConfig.referencedDLLs
         ]
 
-    static member SplitNonFoundationalResolutions (tcConfig: TcConfig) =
+    static member SplitNonFoundationalResolutions(tcConfig: TcConfig) =
         let assemblyList = TcAssemblyResolutions.GetAllDllReferences tcConfig
-        let resolutions = TcAssemblyResolutions.ResolveAssemblyReferences (tcConfig, assemblyList, tcConfig.knownUnresolvedReferences)
-        let frameworkDLLs, nonFrameworkReferences = resolutions.GetAssemblyResolutions() |> List.partition (fun r -> r.sysdir)
+
+        let resolutions =
+            TcAssemblyResolutions.ResolveAssemblyReferences(tcConfig, assemblyList, tcConfig.knownUnresolvedReferences)
+
+        let frameworkDLLs, nonFrameworkReferences =
+            resolutions.GetAssemblyResolutions() |> List.partition (fun r -> r.sysdir)
+
         let unresolved = resolutions.GetUnresolvedReferences()
 #if DEBUG
         let mutable itFailed = false
-        let addedText = "\nIf you want to debug this right now, attach a debugger, and put a breakpoint in 'CompileOps.fs' near the text '!itFailed', and you can re-step through the assembly resolution logic."
 
-        for UnresolvedAssemblyReference(referenceText, _ranges) in unresolved do
+        let addedText =
+            "\nIf you want to debug this right now, attach a debugger, and put a breakpoint in 'CompileOps.fs' near the text '!itFailed', and you can re-step through the assembly resolution logic."
+
+        for UnresolvedAssemblyReference (referenceText, _ranges) in unresolved do
             if referenceText.Contains("mscorlib") then
                 Debug.Assert(false, sprintf "whoops, did not resolve mscorlib: '%s'%s" referenceText addedText)
                 itFailed <- true
 
         for x in frameworkDLLs do
-            if not(FileSystem.IsPathRootedShim(x.resolvedPath)) then
+            if not (FileSystem.IsPathRootedShim(x.resolvedPath)) then
                 Debug.Assert(false, sprintf "frameworkDLL should be absolute path: '%s'%s" x.resolvedPath addedText)
                 itFailed <- true
 
         for x in nonFrameworkReferences do
-            if not(FileSystem.IsPathRootedShim(x.resolvedPath)) then
+            if not (FileSystem.IsPathRootedShim(x.resolvedPath)) then
                 Debug.Assert(false, sprintf "nonFrameworkReference should be absolute path: '%s'%s" x.resolvedPath addedText)
                 itFailed <- true
 
         if itFailed then
             // idea is, put a breakpoint here and then step through
             let assemblyList = TcAssemblyResolutions.GetAllDllReferences tcConfig
-            let resolutions = TcAssemblyResolutions.ResolveAssemblyReferences (tcConfig, assemblyList, [])
-            let _frameworkDLLs, _nonFrameworkReferences = resolutions.GetAssemblyResolutions() |> List.partition (fun r -> r.sysdir)
+
+            let resolutions =
+                TcAssemblyResolutions.ResolveAssemblyReferences(tcConfig, assemblyList, [])
+
+            let _frameworkDLLs, _nonFrameworkReferences =
+                resolutions.GetAssemblyResolutions() |> List.partition (fun r -> r.sysdir)
+
             ()
 #endif
         frameworkDLLs, nonFrameworkReferences, unresolved
 
-    static member BuildFromPriorResolutions (tcConfig: TcConfig, resolutions, knownUnresolved) =
+    static member BuildFromPriorResolutions(tcConfig: TcConfig, resolutions, knownUnresolved) =
         let references = resolutions |> List.map (fun r -> r.originalReference)
-        TcAssemblyResolutions.ResolveAssemblyReferences (tcConfig, references, knownUnresolved)
+        TcAssemblyResolutions.ResolveAssemblyReferences(tcConfig, references, knownUnresolved)
 
     static member GetAssemblyResolutionInformation(tcConfig: TcConfig) =
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
         let assemblyList = TcAssemblyResolutions.GetAllDllReferences tcConfig
-        let resolutions = TcAssemblyResolutions.ResolveAssemblyReferences (tcConfig, assemblyList, [])
+
+        let resolutions =
+            TcAssemblyResolutions.ResolveAssemblyReferences(tcConfig, assemblyList, [])
+
         resolutions.GetAssemblyResolutions(), resolutions.GetUnresolvedReferences()
 
 //----------------------------------------------------------------------------
@@ -651,159 +822,208 @@ let GetNameOfILModule (m: ILModuleDef) =
 
 let MakeScopeRefForILModule (ilModule: ILModuleDef) =
     match ilModule.Manifest with
-    | Some m -> ILScopeRef.Assembly (mkRefToILAssembly m)
-    | None -> ILScopeRef.Module (mkRefToILModule ilModule)
+    | Some m -> ILScopeRef.Assembly(mkRefToILAssembly m)
+    | None -> ILScopeRef.Module(mkRefToILModule ilModule)
 
 let GetCustomAttributesOfILModule (ilModule: ILModuleDef) =
-    (match ilModule.Manifest with Some m -> m.CustomAttrs | None -> ilModule.CustomAttrs).AsList()
+    let attrs =
+        match ilModule.Manifest with
+        | Some m -> m.CustomAttrs
+        | None -> ilModule.CustomAttrs
+
+    attrs.AsList()
 
 let GetAutoOpenAttributes ilModule =
     ilModule |> GetCustomAttributesOfILModule |> List.choose TryFindAutoOpenAttr
 
 let GetInternalsVisibleToAttributes ilModule =
-    ilModule |> GetCustomAttributesOfILModule |> List.choose TryFindInternalsVisibleToAttr
+    ilModule
+    |> GetCustomAttributesOfILModule
+    |> List.choose TryFindInternalsVisibleToAttr
+
+type RawFSharpAssemblyDataBackedByFileOnDisk(ilModule: ILModuleDef, ilAssemblyRefs) =
+    let externalSigAndOptData = [ "FSharp.Core" ]
 
-type RawFSharpAssemblyDataBackedByFileOnDisk (ilModule: ILModuleDef, ilAssemblyRefs) =
-    let externalSigAndOptData = ["FSharp.Core"]
     interface IRawFSharpAssemblyData with
 
-         member _.GetAutoOpenAttributes() = GetAutoOpenAttributes ilModule
+        member _.GetAutoOpenAttributes() = GetAutoOpenAttributes ilModule
 
-         member _.GetInternalsVisibleToAttributes() = GetInternalsVisibleToAttributes ilModule
+        member _.GetInternalsVisibleToAttributes() =
+            GetInternalsVisibleToAttributes ilModule
 
-         member _.TryGetILModuleDef() = Some ilModule
+        member _.TryGetILModuleDef() = Some ilModule
 
-         member _.GetRawFSharpSignatureData(m, ilShortAssemName, fileName) =
+        member _.GetRawFSharpSignatureData(m, ilShortAssemName, fileName) =
             let resources = ilModule.Resources.AsList()
+
             let sigDataReaders =
-                [ for iresource in resources do
-                    if IsSignatureDataResource iresource then
-                        let ccuName = GetSignatureDataResourceName iresource
-                        yield (ccuName, fun () -> iresource.GetBytes()) ]
+                [
+                    for iresource in resources do
+                        if IsSignatureDataResource iresource then
+                            let ccuName = GetSignatureDataResourceName iresource
+                            (ccuName, (fun () -> iresource.GetBytes()))
+                ]
 
             let sigDataReaders =
                 if sigDataReaders.IsEmpty && List.contains ilShortAssemName externalSigAndOptData then
                     let sigFileName = Path.ChangeExtension(fileName, "sigdata")
+
                     if not (FileSystem.FileExistsShim sigFileName) then
-                        error(Error(FSComp.SR.buildExpectedSigdataFile (FileSystem.GetFullPathShim sigFileName), m))
-                    [ (ilShortAssemName, fun () -> FileSystem.OpenFileForReadShim(sigFileName, useMemoryMappedFile=true, shouldShadowCopy=true).AsByteMemory().AsReadOnly())]
+                        error (Error(FSComp.SR.buildExpectedSigdataFile (FileSystem.GetFullPathShim sigFileName), m))
+
+                    [
+                        (ilShortAssemName,
+                         fun () ->
+                             FileSystem
+                                 .OpenFileForReadShim(sigFileName, useMemoryMappedFile = true, shouldShadowCopy = true)
+                                 .AsByteMemory()
+                                 .AsReadOnly())
+                    ]
                 else
                     sigDataReaders
+
             sigDataReaders
 
-         member _.GetRawFSharpOptimizationData(m, ilShortAssemName, fileName) =
+        member _.GetRawFSharpOptimizationData(m, ilShortAssemName, fileName) =
             let optDataReaders =
                 ilModule.Resources.AsList()
-                |> List.choose (fun r -> if IsOptimizationDataResource r then Some(GetOptimizationDataResourceName r, (fun () -> r.GetBytes())) else None)
+                |> List.choose (fun r ->
+                    if IsOptimizationDataResource r then
+                        Some(GetOptimizationDataResourceName r, (fun () -> r.GetBytes()))
+                    else
+                        None)
 
             // Look for optimization data in a file
             let optDataReaders =
                 if optDataReaders.IsEmpty && List.contains ilShortAssemName externalSigAndOptData then
                     let optDataFile = Path.ChangeExtension(fileName, "optdata")
+
                     if not (FileSystem.FileExistsShim optDataFile) then
-                        error(Error(FSComp.SR.buildExpectedFileAlongSideFSharpCore(optDataFile, FileSystem.GetFullPathShim optDataFile), m))
-                    [ (ilShortAssemName, (fun () -> FileSystem.OpenFileForReadShim(optDataFile, useMemoryMappedFile=true, shouldShadowCopy=true).AsByteMemory().AsReadOnly()))]
+                        let fullPath = FileSystem.GetFullPathShim optDataFile
+                        error (Error(FSComp.SR.buildExpectedFileAlongSideFSharpCore (optDataFile, fullPath), m))
+
+                    [
+                        (ilShortAssemName,
+                         (fun () ->
+                             FileSystem
+                                 .OpenFileForReadShim(optDataFile, useMemoryMappedFile = true, shouldShadowCopy = true)
+                                 .AsByteMemory()
+                                 .AsReadOnly()))
+                    ]
                 else
                     optDataReaders
+
             optDataReaders
 
-         member _.GetRawTypeForwarders() =
+        member _.GetRawTypeForwarders() =
             match ilModule.Manifest with
             | Some manifest -> manifest.ExportedTypes
             | None -> mkILExportedTypes []
 
-         member _.ShortAssemblyName = GetNameOfILModule ilModule
+        member _.ShortAssemblyName = GetNameOfILModule ilModule
 
-         member _.ILScopeRef = MakeScopeRefForILModule ilModule
+        member _.ILScopeRef = MakeScopeRefForILModule ilModule
 
-         member _.ILAssemblyRefs = ilAssemblyRefs
+        member _.ILAssemblyRefs = ilAssemblyRefs
 
-         member _.HasAnyFSharpSignatureDataAttribute =
+        member _.HasAnyFSharpSignatureDataAttribute =
             let attrs = GetCustomAttributesOfILModule ilModule
             List.exists IsSignatureDataVersionAttr attrs
 
-         member _.HasMatchingFSharpSignatureDataAttribute =
+        member _.HasMatchingFSharpSignatureDataAttribute =
             let attrs = GetCustomAttributesOfILModule ilModule
-            List.exists (IsMatchingSignatureDataVersionAttr (parseILVersion FSharpBinaryMetadataFormatRevision)) attrs
+            List.exists (IsMatchingSignatureDataVersionAttr(parseILVersion FSharpBinaryMetadataFormatRevision)) attrs
 
 []
-type RawFSharpAssemblyData (ilModule: ILModuleDef, ilAssemblyRefs) =
+type RawFSharpAssemblyData(ilModule: ILModuleDef, ilAssemblyRefs) =
 
     interface IRawFSharpAssemblyData with
 
-         member _.GetAutoOpenAttributes() = GetAutoOpenAttributes ilModule
+        member _.GetAutoOpenAttributes() = GetAutoOpenAttributes ilModule
 
-         member _.GetInternalsVisibleToAttributes() = GetInternalsVisibleToAttributes ilModule
+        member _.GetInternalsVisibleToAttributes() =
+            GetInternalsVisibleToAttributes ilModule
 
-         member _.TryGetILModuleDef() = Some ilModule
+        member _.TryGetILModuleDef() = Some ilModule
 
-         member _.GetRawFSharpSignatureData(_, _, _) =
+        member _.GetRawFSharpSignatureData(_, _, _) =
             let resources = ilModule.Resources.AsList()
-            [ for iresource in resources do
-                if IsSignatureDataResource iresource then
-                    let ccuName = GetSignatureDataResourceName iresource
-                    yield (ccuName, fun () -> iresource.GetBytes()) ]
 
-         member _.GetRawFSharpOptimizationData(_, _, _) =
+            [
+                for iresource in resources do
+                    if IsSignatureDataResource iresource then
+                        let ccuName = GetSignatureDataResourceName iresource
+                        (ccuName, (fun () -> iresource.GetBytes()))
+            ]
+
+        member _.GetRawFSharpOptimizationData(_, _, _) =
             ilModule.Resources.AsList()
-            |> List.choose (fun r -> if IsOptimizationDataResource r then Some(GetOptimizationDataResourceName r, (fun () -> r.GetBytes())) else None)
+            |> List.choose (fun r ->
+                if IsOptimizationDataResource r then
+                    Some(GetOptimizationDataResourceName r, (fun () -> r.GetBytes()))
+                else
+                    None)
 
-         member _.GetRawTypeForwarders() =
+        member _.GetRawTypeForwarders() =
             match ilModule.Manifest with
             | Some manifest -> manifest.ExportedTypes
             | None -> mkILExportedTypes []
 
-         member _.ShortAssemblyName = GetNameOfILModule ilModule
+        member _.ShortAssemblyName = GetNameOfILModule ilModule
 
-         member _.ILScopeRef = MakeScopeRefForILModule ilModule
+        member _.ILScopeRef = MakeScopeRefForILModule ilModule
 
-         member _.ILAssemblyRefs = ilAssemblyRefs
+        member _.ILAssemblyRefs = ilAssemblyRefs
 
-         member _.HasAnyFSharpSignatureDataAttribute =
+        member _.HasAnyFSharpSignatureDataAttribute =
             let attrs = GetCustomAttributesOfILModule ilModule
             List.exists IsSignatureDataVersionAttr attrs
 
-         member _.HasMatchingFSharpSignatureDataAttribute =
+        member _.HasMatchingFSharpSignatureDataAttribute =
             let attrs = GetCustomAttributesOfILModule ilModule
-            List.exists (IsMatchingSignatureDataVersionAttr (parseILVersion FSharpBinaryMetadataFormatRevision)) attrs
+            List.exists (IsMatchingSignatureDataVersionAttr(parseILVersion FSharpBinaryMetadataFormatRevision)) attrs
 
 //----------------------------------------------------------------------------
 // TcImports
 //--------------------------------------------------------------------------
 
 []
-type TcImportsSafeDisposal(tciLock: TcImportsLock, disposeActions: ResizeArray unit>,disposeTypeProviderActions: ResizeArray unit>) =
+type TcImportsSafeDisposal
+    (
+        tciLock: TcImportsLock,
+        disposeActions: ResizeArray unit>,
+        disposeTypeProviderActions: ResizeArray unit>
+    ) =
 
     let mutable isDisposed = false
 
     let dispose () =
-      tciLock.AcquireLock (fun tcitok -> 
+        tciLock.AcquireLock(fun tcitok ->
+
+            RequireTcImportsLock(tcitok, isDisposed)
+            RequireTcImportsLock(tcitok, disposeTypeProviderActions)
+            RequireTcImportsLock(tcitok, disposeActions)
 
-        RequireTcImportsLock (tcitok, isDisposed)
-        RequireTcImportsLock (tcitok, disposeTypeProviderActions)
-        RequireTcImportsLock (tcitok, disposeActions)
+            // disposing deliberately only closes this tcImports, not the ones up the chain
+            isDisposed <- true
 
-        // disposing deliberately only closes this tcImports, not the ones up the chain
-        isDisposed <- true
-        if verbose then
-            dprintf "disposing of TcImports, %d binaries\n" disposeActions.Count
-        
-        let actions1 = disposeTypeProviderActions |> Seq.toArray
-        let actions2 = disposeActions |> Seq.toArray
+            let actions1 = disposeTypeProviderActions |> Seq.toArray
+            let actions2 = disposeActions |> Seq.toArray
 
-        disposeTypeProviderActions.Clear()
-        disposeActions.Clear()
+            disposeTypeProviderActions.Clear()
+            disposeActions.Clear()
 
-        for action in actions1 do action()
-        for action in actions2 do action()
-      )
+            for action in actions1 do
+                action ()
 
-    override _.Finalize() =
-        dispose ()
+            for action in actions2 do
+                action ())
+
+    override _.Finalize() = dispose ()
 
     interface IDisposable with
 
-        member this.Dispose()  =
+        member this.Dispose() =
             if not isDisposed then
                 GC.SuppressFinalize this
                 dispose ()
@@ -814,43 +1034,42 @@ type TcImportsSafeDisposal(tciLock: TcImportsLock, disposeActions: ResizeArray) =
+and TcImportsWeakHack(tciLock: TcImportsLock, tcImports: WeakReference) =
     let mutable dllInfos: TcImportsDllInfoHack list = []
 
-    member _.SetDllInfos (value: ImportedBinary list) =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, dllInfos)
-        dllInfos <- value |> List.map (fun x -> { FileName = x.FileName })
+    member _.SetDllInfos(value: ImportedBinary list) =
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, dllInfos)
+            dllInfos <- value |> List.map (fun x -> { FileName = x.FileName }))
 
     member _.Base: TcImportsWeakHack option =
         match tcImports.TryGetTarget() with
         | true, strong ->
             match strong.Base with
-            | Some (baseTcImports: TcImports) ->
-                Some baseTcImports.Weak
-            | _ ->
-                None
-        | _ ->
-            None
+            | Some (baseTcImports: TcImports) -> Some baseTcImports.Weak
+            | _ -> None
+        | _ -> None
 
     member _.SystemRuntimeContainsType typeName =
-        match tcImports.TryGetTarget () with
+        match tcImports.TryGetTarget() with
         | true, strong -> strong.SystemRuntimeContainsType typeName
         | _ -> false
 #endif
 /// Represents a table of imported assemblies with their resolutions.
 /// Is a disposable object, but it is recommended not to explicitly call Dispose unless you absolutely know nothing will be using its contents after the disposal.
 /// Otherwise, simply allow the GC to collect this and it will properly call Dispose from the finalizer.
-and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAssemblyResolutions, importsBase: TcImports option, dependencyProviderOpt: DependencyProvider option)
+and [] TcImports
+    (
+        tcConfigP: TcConfigProvider,
+        initialResolutions: TcAssemblyResolutions,
+        importsBase: TcImports option,
+        dependencyProviderOpt: DependencyProvider option
+    ) as this
 #if !NO_TYPEPROVIDERS
-                         as this
 #endif
-       =
+ =
 
     let tciLock = TcImportsLock()
 
@@ -865,18 +1084,20 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
     let disposeTypeProviderActions = ResizeArray()
 
 #if !NO_TYPEPROVIDERS
-    let mutable generatedTypeRoots = Dictionary()
-    let tcImportsWeak = TcImportsWeakHack (tciLock, WeakReference<_> this)
+    let mutable generatedTypeRoots =
+        Dictionary()
+
+    let tcImportsWeak = TcImportsWeakHack(tciLock, WeakReference<_> this)
 #endif
 
-    let disposal = new TcImportsSafeDisposal(tciLock, disposeActions, disposeTypeProviderActions)
+    let disposal =
+        new TcImportsSafeDisposal(tciLock, disposeActions, disposeTypeProviderActions)
     //---- End protected by tciLock -------
 
     let mutable disposed = false // this doesn't need locking, it's only for debugging
     let mutable tcGlobals = None // this doesn't need locking, it's set during construction of the TcImports
 
-    let CheckDisposed() =
-        if disposed then assert false
+    let CheckDisposed () = if disposed then assert false
 
     let dispose () =
         CheckDisposed()
@@ -888,18 +1109,21 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
     //          If the thunk remains unresolved add it back to the ccuThunks dictionary for further processing
     //      If not then move on to the next thunk
     let fixupOrphanCcus () =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, ccuThunks)
-        let contents = ccuThunks |> Seq.toArray
-        let unsuccessful =
-            [ for ccuThunk, func in contents do
-                if ccuThunk.IsUnresolvedReference then
-                    func()
-                if ccuThunk.IsUnresolvedReference then
-                    yield (ccuThunk, func) ]
-        ccuThunks <- ResizeArray unsuccessful
-
-    let availableToOptionalCcu = function
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, ccuThunks)
+            let contents = ccuThunks |> Seq.toArray
+
+            let unsuccessful =
+                [
+                    for ccuThunk, func in contents do
+                        if ccuThunk.IsUnresolvedReference then func ()
+                        if ccuThunk.IsUnresolvedReference then (ccuThunk, func)
+                ]
+
+            ccuThunks <- ResizeArray unsuccessful)
+
+    let availableToOptionalCcu =
+        function
         | ResolvedCcu ccu -> Some ccu
         | UnresolvedCcu _ -> None
 
@@ -907,14 +1131,13 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
         let matchNameSpace (entityOpt: Entity option) n =
             match entityOpt with
             | None -> None
-            | Some entity ->
-                entity.ModuleOrNamespaceType.AllEntitiesByCompiledAndLogicalMangledNames.TryFind n
+            | Some entity -> entity.ModuleOrNamespaceType.AllEntitiesByCompiledAndLogicalMangledNames.TryFind n
 
         match (Some ccu.Contents, nsname) ||> List.fold matchNameSpace with
         | Some ns ->
-                match Map.tryFind tname ns.ModuleOrNamespaceType.TypesByMangledName with
-                | Some _ -> true
-                | None -> false
+            match Map.tryFind tname ns.ModuleOrNamespaceType.TypesByMangledName with
+            | Some _ -> true
+            | None -> false
         | None -> false
 
     member internal tcImports.Base =
@@ -922,62 +1145,65 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
         importsBase
 
     member tcImports.CcuTable =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, ccuTable)
-        CheckDisposed()
-        ccuTable
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, ccuTable)
+            CheckDisposed()
+            ccuTable)
 
     member tcImports.DllTable =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, dllTable)
-        CheckDisposed()
-        dllTable
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, dllTable)
+            CheckDisposed()
+            dllTable)
 
 #if !NO_TYPEPROVIDERS
     member tcImports.Weak =
-            CheckDisposed()
-            tcImportsWeak
+        CheckDisposed()
+        tcImportsWeak
 #endif
 
     member tcImports.RegisterCcu ccuInfo =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, ccuInfos)
-        RequireTcImportsLock(tcitok, ccuTable)
-        ccuInfos <- ccuInfos ++ ccuInfo
-        // Assembly Ref Resolution: remove this use of ccu.AssemblyName
-        ccuTable <- NameMap.add ccuInfo.FSharpViewOfMetadata.AssemblyName ccuInfo ccuTable
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, ccuInfos)
+            RequireTcImportsLock(tcitok, ccuTable)
+            ccuInfos <- ccuInfos ++ ccuInfo
+            // Assembly Ref Resolution: remove this use of ccu.AssemblyName
+            ccuTable <- NameMap.add ccuInfo.FSharpViewOfMetadata.AssemblyName ccuInfo ccuTable)
 
     member tcImports.RegisterDll dllInfo =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, dllInfos)
-        RequireTcImportsLock(tcitok, dllTable)
-        dllInfos <- dllInfos ++ dllInfo
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, dllInfos)
+            RequireTcImportsLock(tcitok, dllTable)
+            dllInfos <- dllInfos ++ dllInfo
 #if !NO_TYPEPROVIDERS
-        tcImportsWeak.SetDllInfos dllInfos
+            tcImportsWeak.SetDllInfos dllInfos
 #endif
-        dllTable <- NameMap.add (getNameOfScopeRef dllInfo.ILScopeRef) dllInfo dllTable
+            dllTable <- NameMap.add (getNameOfScopeRef dllInfo.ILScopeRef) dllInfo dllTable)
 
     member tcImports.GetDllInfos() : ImportedBinary list =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, dllInfos)
-        match importsBase with
-        | Some importsBase -> importsBase.GetDllInfos() @ dllInfos
-        | None -> dllInfos
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, dllInfos)
+
+            match importsBase with
+            | Some importsBase -> importsBase.GetDllInfos() @ dllInfos
+            | None -> dllInfos)
 
     member tcImports.AllAssemblyResolutions() =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, resolutions)
-        let ars = resolutions.GetAssemblyResolutions()
-        match importsBase with
-        | Some importsBase-> importsBase.AllAssemblyResolutions() @ ars
-        | None -> ars
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, resolutions)
+            let ars = resolutions.GetAssemblyResolutions()
+
+            match importsBase with
+            | Some importsBase -> importsBase.AllAssemblyResolutions() @ ars
+            | None -> ars)
 
-    member tcImports.TryFindDllInfo (ctok: CompilationThreadToken, m, assemblyName, lookupOnly) =
+    member tcImports.TryFindDllInfo(ctok: CompilationThreadToken, m, assemblyName, lookupOnly) =
         CheckDisposed()
+
         let rec look (t: TcImports) =
             match NameMap.tryFind assemblyName t.DllTable with
             | Some res -> Some res
@@ -985,75 +1211,82 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
                 match t.Base with
                 | Some t2 -> look t2
                 | None -> None
+
         match look tcImports with
         | Some res -> Some res
         | None ->
             tcImports.ImplicitLoadIfAllowed(ctok, m, assemblyName, lookupOnly)
             look tcImports
 
-    member tcImports.FindDllInfo (ctok, m, assemblyName) =
-        match tcImports.TryFindDllInfo (ctok, m, assemblyName, lookupOnly=false) with
+    member tcImports.FindDllInfo(ctok, m, assemblyName) =
+        match tcImports.TryFindDllInfo(ctok, m, assemblyName, lookupOnly = false) with
         | Some res -> res
-        | None -> error(Error(FSComp.SR.buildCouldNotResolveAssembly assemblyName, m))
+        | None -> error (Error(FSComp.SR.buildCouldNotResolveAssembly assemblyName, m))
 
     member tcImports.GetImportedAssemblies() =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, ccuInfos)
-        match importsBase with
-        | Some importsBase -> List.append (importsBase.GetImportedAssemblies()) ccuInfos
-        | None -> ccuInfos
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, ccuInfos)
+
+            match importsBase with
+            | Some importsBase -> List.append (importsBase.GetImportedAssemblies()) ccuInfos
+            | None -> ccuInfos)
 
     member tcImports.GetCcusExcludingBase() =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, ccuInfos)
-        ccuInfos |> List.map (fun x -> x.FSharpViewOfMetadata)
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, ccuInfos)
+            ccuInfos |> List.map (fun x -> x.FSharpViewOfMetadata))
 
     member tcImports.GetCcusInDeclOrder() =
         CheckDisposed()
         List.map (fun x -> x.FSharpViewOfMetadata) (tcImports.GetImportedAssemblies())
 
     // This is the main "assembly reference --> assembly" resolution routine.
-    member tcImports.FindCcuInfo (ctok, m, assemblyName, lookupOnly) =
+    member tcImports.FindCcuInfo(ctok, m, assemblyName, lookupOnly) =
         CheckDisposed()
+
         let rec look (t: TcImports) =
             match NameMap.tryFind assemblyName t.CcuTable with
             | Some res -> Some res
             | None ->
-                 match t.Base with
-                 | Some t2 -> look t2
-                 | None -> None
+                match t.Base with
+                | Some t2 -> look t2
+                | None -> None
 
         match look tcImports with
         | Some res -> ResolvedImportedAssembly res
         | None ->
             tcImports.ImplicitLoadIfAllowed(ctok, m, assemblyName, lookupOnly)
+
             match look tcImports with
             | Some res -> ResolvedImportedAssembly res
             | None -> UnresolvedImportedAssembly assemblyName
 
-    member tcImports.FindCcu (ctok, m, assemblyName, lookupOnly) =
+    member tcImports.FindCcu(ctok, m, assemblyName, lookupOnly) =
         CheckDisposed()
+
         match tcImports.FindCcuInfo(ctok, m, assemblyName, lookupOnly) with
         | ResolvedImportedAssembly importedAssembly -> ResolvedCcu(importedAssembly.FSharpViewOfMetadata)
         | UnresolvedImportedAssembly assemblyName -> UnresolvedCcu assemblyName
 
     member tcImports.FindCcuFromAssemblyRef(ctok, m, assemblyRef: ILAssemblyRef) =
         CheckDisposed()
-        match tcImports.FindCcuInfo(ctok, m, assemblyRef.Name, lookupOnly=false) with
+
+        match tcImports.FindCcuInfo(ctok, m, assemblyRef.Name, lookupOnly = false) with
         | ResolvedImportedAssembly importedAssembly -> ResolvedCcu(importedAssembly.FSharpViewOfMetadata)
         | UnresolvedImportedAssembly _ -> UnresolvedCcu(assemblyRef.QualifiedName)
 
     member tcImports.TryFindXmlDocumentationInfo(assemblyName: string) =
         CheckDisposed()
+
         let rec look (t: TcImports) =
             match NameMap.tryFind assemblyName t.CcuTable with
             | Some res -> Some res
             | None ->
-                 match t.Base with
-                 | Some t2 -> look t2
-                 | None -> None
+                match t.Base with
+                | Some t2 -> look t2
+                | None -> None
 
         match look tcImports with
         | Some res -> res.FSharpViewOfMetadata.Deref.XmlDocumentationInfo
@@ -1062,106 +1295,129 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
 #if !NO_TYPEPROVIDERS
     member tcImports.GetProvidedAssemblyInfo(ctok, m, assembly: Tainted) =
         match assembly with
-        | Tainted.Null -> false,None
+        | Tainted.Null -> false, None
         | Tainted.NonNull assembly ->
-        let aname = assembly.PUntaint((fun a -> a.GetName()), m)
-        let ilShortAssemName = aname.Name
+            let aname = assembly.PUntaint((fun a -> a.GetName()), m)
+            let ilShortAssemName = aname.Name
+
+            match tcImports.FindCcu(ctok, m, ilShortAssemName, lookupOnly = true) with
+            | ResolvedCcu ccu ->
+                if ccu.IsProviderGenerated then
+                    let dllinfo = tcImports.FindDllInfo(ctok, m, ilShortAssemName)
+                    true, dllinfo.ProviderGeneratedStaticLinkMap
+                else
+                    false, None
+
+            | UnresolvedCcu _ ->
+                let g = tcImports.GetTcGlobals()
+                let ilScopeRef = ILScopeRef.Assembly(ILAssemblyRef.FromAssemblyName aname)
+                let fileName = aname.Name + ".dll"
+
+                let bytes =
+                    assembly
+                        .PApplyWithProvider((fun (assembly, provider) -> assembly.GetManifestModuleContents provider), m)
+                        .PUntaint(id, m)
+
+                let tcConfig = tcConfigP.Get ctok
+
+                let ilModule, ilAssemblyRefs =
+                    let opts: ILReaderOptions =
+                        {
+                            reduceMemoryUsage = tcConfig.reduceMemoryUsage
+                            pdbDirPath = None
+                            metadataOnly = MetadataOnlyFlag.Yes
+                            tryGetMetadataSnapshot = tcConfig.tryGetMetadataSnapshot
+                        }
+
+                    let reader = OpenILModuleReaderFromBytes fileName bytes opts
+                    reader.ILModuleDef, reader.ILAssemblyRefs
+
+                let theActualAssembly = assembly.PUntaint((fun x -> x.Handle), m)
+
+                let dllinfo =
+                    {
+                        RawMetadata = RawFSharpAssemblyDataBackedByFileOnDisk(ilModule, ilAssemblyRefs)
+                        FileName = fileName
+                        ProviderGeneratedAssembly = Some theActualAssembly
+                        IsProviderGenerated = true
+                        ProviderGeneratedStaticLinkMap =
+                            if g.isInteractive then
+                                None
+                            else
+                                Some(ProvidedAssemblyStaticLinkingMap.CreateNew())
+                        ILScopeRef = ilScopeRef
+                        ILAssemblyRefs = ilAssemblyRefs
+                    }
 
-        match tcImports.FindCcu (ctok, m, ilShortAssemName, lookupOnly=true) with
-        | ResolvedCcu ccu ->
-            if ccu.IsProviderGenerated then
-                let dllinfo = tcImports.FindDllInfo(ctok, m, ilShortAssemName)
-                true, dllinfo.ProviderGeneratedStaticLinkMap
-            else
-                false, None
+                tcImports.RegisterDll dllinfo
 
-        | UnresolvedCcu _ ->
-            let g = tcImports.GetTcGlobals()
-            let ilScopeRef = ILScopeRef.Assembly (ILAssemblyRef.FromAssemblyName aname)
-            let fileName = aname.Name + ".dll"
-            let bytes = assembly.PApplyWithProvider((fun (assembly, provider) -> assembly.GetManifestModuleContents provider), m).PUntaint(id, m)
-            let tcConfig = tcConfigP.Get ctok
-            let ilModule, ilAssemblyRefs =
-                let opts: ILReaderOptions =
-                    { reduceMemoryUsage = tcConfig.reduceMemoryUsage
-                      pdbDirPath = None
-                      metadataOnly = MetadataOnlyFlag.Yes
-                      tryGetMetadataSnapshot = tcConfig.tryGetMetadataSnapshot }
-                let reader = OpenILModuleReaderFromBytes fileName bytes opts
-                reader.ILModuleDef, reader.ILAssemblyRefs
-
-            let theActualAssembly = assembly.PUntaint((fun x -> x.Handle), m)
-            let dllinfo =
-                { RawMetadata= RawFSharpAssemblyDataBackedByFileOnDisk (ilModule, ilAssemblyRefs)
-                  FileName=fileName
-                  ProviderGeneratedAssembly=Some theActualAssembly
-                  IsProviderGenerated=true
-                  ProviderGeneratedStaticLinkMap= if g.isInteractive then None else Some (ProvidedAssemblyStaticLinkingMap.CreateNew())
-                  ILScopeRef = ilScopeRef
-                  ILAssemblyRefs = ilAssemblyRefs }
-            tcImports.RegisterDll dllinfo
-
-            let ccuContents = Construct.NewCcuContents ilScopeRef m ilShortAssemName (Construct.NewEmptyModuleOrNamespaceType Namespace)
-
-            let ccuData: CcuData =
-              { IsFSharp=false
-                UsesFSharp20PlusQuotations=false
-                InvalidateEvent=(Event<_>()).Publish
-                IsProviderGenerated = true
-                QualifiedName= Some (assembly.PUntaint((fun a -> a.FullName), m))
-                Contents = ccuContents
-                ILScopeRef = ilScopeRef
-                Stamp = newStamp()
-                SourceCodeDirectory = ""
-                FileName = Some fileName
-                MemberSignatureEquality = (fun ty1 ty2 -> typeEquivAux EraseAll g ty1 ty2)
-                ImportProvidedType = (fun ty -> ImportProvidedType (tcImports.GetImportMap()) m ty)
-                TryGetILModuleDef = (fun () -> Some ilModule)
-                TypeForwarders = CcuTypeForwarderTable.Empty
-                XmlDocumentationInfo =
-                    match tcConfig.xmlDocInfoLoader with
-                    | Some xmlDocInfoLoader -> xmlDocInfoLoader.TryLoad(fileName)
-                    | _ -> None
-              }
-
-            let ccu = CcuThunk.Create(ilShortAssemName, ccuData)
-            let ccuinfo =
-                { FSharpViewOfMetadata=ccu
-                  ILScopeRef = ilScopeRef
-                  AssemblyAutoOpenAttributes = []
-                  AssemblyInternalsVisibleToAttributes = []
-                  IsProviderGenerated = true
-                  TypeProviders=[]
-                  FSharpOptimizationData = notlazy None }
-            tcImports.RegisterCcu ccuinfo
-            // Yes, it is generative
-            true, dllinfo.ProviderGeneratedStaticLinkMap
+                let ccuContents =
+                    Construct.NewCcuContents ilScopeRef m ilShortAssemName (Construct.NewEmptyModuleOrNamespaceType Namespace)
+
+                let ccuData: CcuData =
+                    {
+                        IsFSharp = false
+                        UsesFSharp20PlusQuotations = false
+                        InvalidateEvent = (Event<_>()).Publish
+                        IsProviderGenerated = true
+                        QualifiedName = Some(assembly.PUntaint((fun a -> a.FullName), m))
+                        Contents = ccuContents
+                        ILScopeRef = ilScopeRef
+                        Stamp = newStamp ()
+                        SourceCodeDirectory = ""
+                        FileName = Some fileName
+                        MemberSignatureEquality = (fun ty1 ty2 -> typeEquivAux EraseAll g ty1 ty2)
+                        ImportProvidedType = (fun ty -> ImportProvidedType (tcImports.GetImportMap()) m ty)
+                        TryGetILModuleDef = (fun () -> Some ilModule)
+                        TypeForwarders = CcuTypeForwarderTable.Empty
+                        XmlDocumentationInfo =
+                            match tcConfig.xmlDocInfoLoader with
+                            | Some xmlDocInfoLoader -> xmlDocInfoLoader.TryLoad(fileName)
+                            | _ -> None
+                    }
+
+                let ccu = CcuThunk.Create(ilShortAssemName, ccuData)
+
+                let ccuinfo =
+                    {
+                        FSharpViewOfMetadata = ccu
+                        ILScopeRef = ilScopeRef
+                        AssemblyAutoOpenAttributes = []
+                        AssemblyInternalsVisibleToAttributes = []
+                        IsProviderGenerated = true
+                        TypeProviders = []
+                        FSharpOptimizationData = notlazy None
+                    }
+
+                tcImports.RegisterCcu ccuinfo
+                // Yes, it is generative
+                true, dllinfo.ProviderGeneratedStaticLinkMap
 
     member tcImports.RecordGeneratedTypeRoot root =
-      tciLock.AcquireLock <| fun tcitok ->
-        // checking if given ProviderGeneratedType was already recorded before (probably for another set of static parameters)
-        let (ProviderGeneratedType(_, ilTyRef, _)) = root
-        let index =
-            RequireTcImportsLock(tcitok, generatedTypeRoots)
-            match generatedTypeRoots.TryGetValue ilTyRef with
-            | true, (index, _) -> index
-            | false, _ -> generatedTypeRoots.Count
-        generatedTypeRoots[ilTyRef] <- (index, root)
+        tciLock.AcquireLock(fun tcitok ->
+            // checking if given ProviderGeneratedType was already recorded before (probably for another set of static parameters)
+            let (ProviderGeneratedType (_, ilTyRef, _)) = root
+
+            let index =
+                RequireTcImportsLock(tcitok, generatedTypeRoots)
+
+                match generatedTypeRoots.TryGetValue ilTyRef with
+                | true, (index, _) -> index
+                | false, _ -> generatedTypeRoots.Count
+
+            generatedTypeRoots[ilTyRef] <- (index, root))
 
     member tcImports.ProviderGeneratedTypeRoots =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, generatedTypeRoots)
-        generatedTypeRoots.Values
-        |> Seq.sortBy fst
-        |> Seq.map snd
-        |> Seq.toList
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, generatedTypeRoots)
+            generatedTypeRoots.Values |> Seq.sortBy fst |> Seq.map snd |> Seq.toList)
 #endif
 
     member private tcImports.AttachDisposeAction action =
-      tciLock.AcquireLock <| fun tcitok ->
-        CheckDisposed()
-        RequireTcImportsLock(tcitok, disposeActions)
-        disposeActions.Add action
+        tciLock.AcquireLock(fun tcitok ->
+            CheckDisposed()
+            RequireTcImportsLock(tcitok, disposeActions)
+            disposeActions.Add action)
 
 #if !NO_TYPEPROVIDERS
     member private tcImports.AttachDisposeTypeProviderAction action =
@@ -1172,60 +1428,85 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
     // Note: the returned binary reader is associated with the tcImports, i.e. when the tcImports are closed
     // then the reader is closed.
     member tcImports.OpenILBinaryModule(ctok, fileName, m) =
-      try
-        CheckDisposed()
-        let tcConfig = tcConfigP.Get ctok
-        let pdbDirPath =
-            // We open the pdb file if one exists parallel to the binary we
-            // are reading, so that --standalone will preserve debug information.
-            if tcConfig.openDebugInformationForLaterStaticLinking then
-                let pdbDir = try FileSystem.GetDirectoryNameShim fileName with _ -> "."
-                let pdbFile = (try FileSystemUtils.chopExtension fileName with _ -> fileName) + ".pdb"
-
-                if FileSystem.FileExistsShim pdbFile then
-                    if verbose then dprintf "reading PDB file %s from directory %s\n" pdbFile pdbDir
-                    Some pdbDir
+        try
+            CheckDisposed()
+            let tcConfig = tcConfigP.Get ctok
+
+            let pdbDirPath =
+                // We open the pdb file if one exists parallel to the binary we
+                // are reading, so that --standalone will preserve debug information.
+                if tcConfig.openDebugInformationForLaterStaticLinking then
+                    let pdbDir =
+                        try
+                            FileSystem.GetDirectoryNameShim fileName
+                        with _ ->
+                            "."
+
+                    let pdbFile =
+                        (try
+                            FileSystemUtils.chopExtension fileName
+                         with _ ->
+                             fileName)
+                        + ".pdb"
+
+                    if FileSystem.FileExistsShim pdbFile then
+                        Some pdbDir
+                    else
+                        None
                 else
                     None
-            else
-                None
 
-        let ilILBinaryReader =
-            OpenILBinary (fileName, tcConfig.reduceMemoryUsage, pdbDirPath, tcConfig.shadowCopyReferences, tcConfig.tryGetMetadataSnapshot)
+            let ilILBinaryReader =
+                OpenILBinary(
+                    fileName,
+                    tcConfig.reduceMemoryUsage,
+                    pdbDirPath,
+                    tcConfig.shadowCopyReferences,
+                    tcConfig.tryGetMetadataSnapshot
+                )
 
-        tcImports.AttachDisposeAction(fun _ -> (ilILBinaryReader :> IDisposable).Dispose())
-        ilILBinaryReader.ILModuleDef, ilILBinaryReader.ILAssemblyRefs
-      with e ->
-        error(Error(FSComp.SR.buildErrorOpeningBinaryFile(fileName, e.Message), m))
+            tcImports.AttachDisposeAction(fun _ -> (ilILBinaryReader :> IDisposable).Dispose())
+            ilILBinaryReader.ILModuleDef, ilILBinaryReader.ILAssemblyRefs
+        with e ->
+            error (Error(FSComp.SR.buildErrorOpeningBinaryFile (fileName, e.Message), m))
 
     (* auxModTable is used for multi-module assemblies *)
     member tcImports.MkLoaderForMultiModuleILAssemblies ctok m =
         CheckDisposed()
         let auxModTable = HashMultiMap(10, HashIdentity.Structural)
+
         fun viewedScopeRef ->
 
             let tcConfig = tcConfigP.Get ctok
+
             match viewedScopeRef with
             | ILScopeRef.Module modref ->
                 let key = modref.Name
+
                 if not (auxModTable.ContainsKey key) then
-                    let resolution = tcConfig.ResolveLibWithDirectories (CcuLoadFailureAction.RaiseError, AssemblyReference(m, key, None)) |> Option.get
+                    let resolution =
+                        tcConfig.ResolveLibWithDirectories(CcuLoadFailureAction.RaiseError, AssemblyReference(m, key, None))
+                        |> Option.get
+
                     let ilModule, _ = tcImports.OpenILBinaryModule(ctok, resolution.resolvedPath, m)
                     auxModTable[key] <- ilModule
+
                 auxModTable[key]
 
-            | _ ->
-                error(InternalError("Unexpected ILScopeRef.Local or ILScopeRef.Assembly in exported type table", m))
+            | _ -> error (InternalError("Unexpected ILScopeRef.Local or ILScopeRef.Assembly in exported type table", m))
 
     member tcImports.IsAlreadyRegistered nm =
         CheckDisposed()
-        tcImports.GetDllInfos() |> List.exists (fun dll ->
+
+        tcImports.GetDllInfos()
+        |> List.exists (fun dll ->
             match dll.ILScopeRef with
             | ILScopeRef.Assembly a -> a.Name = nm
             | _ -> false)
 
     member _.DependencyProvider =
         CheckDisposed()
+
         match dependencyProviderOpt with
         | None ->
             Debug.Assert(false, "this should never be called on FrameworkTcImports")
@@ -1234,22 +1515,30 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
 
     member tcImports.GetImportMap() =
         CheckDisposed()
+
         let loaderInterface =
+#if NO_TYPEPROVIDERS
             { new AssemblyLoader with
-                 member _.FindCcuFromAssemblyRef (ctok, m, ilAssemblyRef) =
-                     tcImports.FindCcuFromAssemblyRef (ctok, m, ilAssemblyRef)
+                member _.FindCcuFromAssemblyRef(ctok, m, ilAssemblyRef) =
+                    tcImports.FindCcuFromAssemblyRef(ctok, m, ilAssemblyRef)
 
-                 member _.TryFindXmlDocumentationInfo assemblyName =
+                member _.TryFindXmlDocumentationInfo assemblyName =
                     tcImports.TryFindXmlDocumentationInfo(assemblyName)
+            }
+#else
+            { new AssemblyLoader with
+                member _.FindCcuFromAssemblyRef(ctok, m, ilAssemblyRef) =
+                    tcImports.FindCcuFromAssemblyRef(ctok, m, ilAssemblyRef)
 
-#if !NO_TYPEPROVIDERS
-                 member _.GetProvidedAssemblyInfo (ctok, m, assembly) =
-                     tcImports.GetProvidedAssemblyInfo (ctok, m, assembly)
+                member _.TryFindXmlDocumentationInfo assemblyName =
+                    tcImports.TryFindXmlDocumentationInfo(assemblyName)
+
+                member _.GetProvidedAssemblyInfo(ctok, m, assembly) =
+                    tcImports.GetProvidedAssemblyInfo(ctok, m, assembly)
 
-                 member _.RecordGeneratedTypeRoot root =
-                     tcImports.RecordGeneratedTypeRoot root
+                member _.RecordGeneratedTypeRoot root = tcImports.RecordGeneratedTypeRoot root
+            }
 #endif
-             }
         ImportMap(tcImports.GetTcGlobals(), loaderInterface)
 
     // Note the tcGlobals are only available once mscorlib and fslib have been established. For TcImports,
@@ -1262,6 +1551,7 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
     // ImportILAssembly had a tcGlobals available when it really needs it.
     member tcImports.GetTcGlobals() : TcGlobals =
         CheckDisposed()
+
         match tcGlobals with
         | Some g -> g
         | None ->
@@ -1275,26 +1565,59 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
 
 #if !NO_TYPEPROVIDERS
     member private tcImports.InjectProvidedNamespaceOrTypeIntoEntity
-            (typeProviderEnvironment,
-             tcConfig: TcConfig,
-             m, entity: Entity,
-             injectedNamespace, remainingNamespace,
-             provider,
-             st: Tainted option) =
+        (
+            typeProviderEnvironment,
+            tcConfig: TcConfig,
+            m,
+            entity: Entity,
+            injectedNamespace,
+            remainingNamespace,
+            provider,
+            st: Tainted option
+        ) =
         match remainingNamespace with
         | next :: rest ->
             // Inject the namespace entity
             match entity.ModuleOrNamespaceType.ModulesAndNamespacesByDemangledName.TryFind next with
             | Some childEntity ->
-                tcImports.InjectProvidedNamespaceOrTypeIntoEntity (typeProviderEnvironment, tcConfig, m, childEntity, next :: injectedNamespace, rest, provider, st)
+                tcImports.InjectProvidedNamespaceOrTypeIntoEntity(
+                    typeProviderEnvironment,
+                    tcConfig,
+                    m,
+                    childEntity,
+                    next :: injectedNamespace,
+                    rest,
+                    provider,
+                    st
+                )
             | None ->
                 // Build up the artificial namespace if there is not a real one.
-                let cpath = CompPath(ILScopeRef.Local, injectedNamespace |> List.rev |> List.map (fun n -> (n, ModuleOrNamespaceKind.Namespace)) )
+                let cpath =
+                    CompPath(
+                        ILScopeRef.Local,
+                        injectedNamespace
+                        |> List.rev
+                        |> List.map (fun n -> (n, ModuleOrNamespaceKind.Namespace))
+                    )
+
                 let mid = ident (next, rangeStartup)
                 let mty = Construct.NewEmptyModuleOrNamespaceType Namespace
-                let newNamespace = Construct.NewModuleOrNamespace (Some cpath) taccessPublic mid XmlDoc.Empty [] (MaybeLazy.Strict mty)
+
+                let newNamespace =
+                    Construct.NewModuleOrNamespace (Some cpath) taccessPublic mid XmlDoc.Empty [] (MaybeLazy.Strict mty)
+
                 entity.ModuleOrNamespaceType.AddModuleOrNamespaceByMutation newNamespace
-                tcImports.InjectProvidedNamespaceOrTypeIntoEntity (typeProviderEnvironment, tcConfig, m, newNamespace, next :: injectedNamespace, rest, provider, st)
+
+                tcImports.InjectProvidedNamespaceOrTypeIntoEntity(
+                    typeProviderEnvironment,
+                    tcConfig,
+                    m,
+                    newNamespace,
+                    next :: injectedNamespace,
+                    rest,
+                    provider,
+                    st
+                )
         | [] ->
             match st with
             | Some st ->
@@ -1302,21 +1625,26 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
                 //
                 // Generated types get properly injected into the provided (i.e. generated) assembly CCU in tc.fs
 
-                let importProvidedType t = ImportProvidedType (tcImports.GetImportMap()) m t
-                let isSuppressRelocate = tcConfig.isInteractive || st.PUntaint((fun st -> st.IsSuppressRelocate), m)
-                let newEntity = Construct.NewProvidedTycon(typeProviderEnvironment, st, importProvidedType, isSuppressRelocate, m)
+                let importProvidedType t =
+                    ImportProvidedType (tcImports.GetImportMap()) m t
+
+                let isSuppressRelocate =
+                    tcConfig.isInteractive || st.PUntaint((fun st -> st.IsSuppressRelocate), m)
+
+                let newEntity =
+                    Construct.NewProvidedTycon(typeProviderEnvironment, st, importProvidedType, isSuppressRelocate, m)
+
                 entity.ModuleOrNamespaceType.AddProvidedTypeEntity newEntity
             | None -> ()
 
             entity.entity_tycon_repr <-
                 match entity.TypeReprInfo with
                 // This is the first extension
-                | TNoRepr ->
-                    TProvidedNamespaceRepr(typeProviderEnvironment, [provider])
+                | TNoRepr -> TProvidedNamespaceRepr(typeProviderEnvironment, [ provider ])
 
                 // Add to the existing list of extensions
-                | TProvidedNamespaceRepr(resolutionFolder, prior) as repr ->
-                    if not(prior |> List.exists(fun r->Tainted.EqTainted r provider)) then
+                | TProvidedNamespaceRepr (resolutionFolder, prior) as repr ->
+                    if not (prior |> List.exists (fun r -> Tainted.EqTainted r provider)) then
                         TProvidedNamespaceRepr(resolutionFolder, provider :: prior)
                     else
                         repr
@@ -1324,11 +1652,16 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
                 | _ -> failwith "Unexpected representation in namespace entity referred to by a type provider"
 
     member tcImportsStrong.ImportTypeProviderExtensions
-               (ctok, tcConfig: TcConfig,
-                fileNameOfRuntimeAssembly,
-                ilScopeRefOfRuntimeAssembly,
-                runtimeAssemblyAttributes: ILAttribute list,
-                entityToInjectInto, invalidateCcu: Event<_>, m) =
+        (
+            ctok,
+            tcConfig: TcConfig,
+            fileNameOfRuntimeAssembly,
+            ilScopeRefOfRuntimeAssembly,
+            runtimeAssemblyAttributes: ILAttribute list,
+            entityToInjectInto,
+            invalidateCcu: Event<_>,
+            m
+        ) =
 
         let startingErrorCount = DiagnosticsThreadStatics.DiagnosticsLogger.ErrorCount
 
@@ -1338,27 +1671,38 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
             runtimeAssemblyAttributes
             |> List.choose TryDecodeTypeProviderAssemblyAttr
             // If no design-time assembly is specified, use the runtime assembly
-            |> List.map (function Null -> fileNameOfRuntimeAssembly | NonNull s -> s)
+            |> List.map (function
+                | Null -> fileNameOfRuntimeAssembly
+                | NonNull s -> s)
             // For each simple name of a design-time assembly, we take the first matching one in the order they are
             // specified in the attributes
-            |> List.distinctBy (fun s -> try Path.GetFileNameWithoutExtension s with _ -> s)
+            |> List.distinctBy (fun s ->
+                try
+                    Path.GetFileNameWithoutExtension s
+                with _ ->
+                    s)
 
         if not (List.isEmpty designTimeAssemblyNames) then
 
             // Find the SystemRuntimeAssemblyVersion value to report in the TypeProviderConfig.
             let primaryAssemblyVersion =
                 let primaryAssemblyRef = tcConfig.PrimaryAssemblyDllReference()
-                let resolution = tcConfig.ResolveLibWithDirectories (CcuLoadFailureAction.RaiseError, primaryAssemblyRef) |> Option.get
-                 // MSDN: this method causes the file to be opened and closed, but the assembly is not added to this domain
+
+                let resolution =
+                    tcConfig.ResolveLibWithDirectories(CcuLoadFailureAction.RaiseError, primaryAssemblyRef)
+                    |> Option.get
+                // MSDN: this method causes the file to be opened and closed, but the assembly is not added to this domain
                 let name = System.Reflection.AssemblyName.GetAssemblyName(resolution.resolvedPath)
                 name.Version
 
             let typeProviderEnvironment =
-                 { ResolutionFolder = tcConfig.implicitIncludeDir
-                   OutputFile = tcConfig.outputFile
-                   ShowResolutionMessages = tcConfig.showExtensionTypeMessages
-                   ReferencedAssemblies = Array.distinct [| for r in tcImportsStrong.AllAssemblyResolutions() -> r.resolvedPath |]
-                   TemporaryFolder = FileSystem.GetTempPathShim() }
+                {
+                    ResolutionFolder = tcConfig.implicitIncludeDir
+                    OutputFile = tcConfig.outputFile
+                    ShowResolutionMessages = tcConfig.showExtensionTypeMessages
+                    ReferencedAssemblies = Array.distinct [| for r in tcImportsStrong.AllAssemblyResolutions() -> r.resolvedPath |]
+                    TemporaryFolder = FileSystem.GetTempPathShim()
+                }
 
             // The type provider should not hold strong references to disposed
             // TcImport objects. So the callbacks provided in the type provider config
@@ -1367,22 +1711,32 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
             let systemRuntimeContainsType =
                 // NOTE: do not touch this, edit: but we did, we had no choice - TPs cannot hold a strong reference on TcImports "ever".
                 let tcImports = tcImportsWeak
-                let mutable systemRuntimeContainsTypeRef = fun typeName -> tcImports.SystemRuntimeContainsType typeName
-                tcImportsStrong.AttachDisposeTypeProviderAction(fun () -> systemRuntimeContainsTypeRef <- fun _ -> raise (ObjectDisposedException("The type provider has been disposed")))
+
+                let mutable systemRuntimeContainsTypeRef =
+                    fun typeName -> tcImports.SystemRuntimeContainsType typeName
+
+                tcImportsStrong.AttachDisposeTypeProviderAction(fun () ->
+                    systemRuntimeContainsTypeRef <- fun _ -> raise (ObjectDisposedException("The type provider has been disposed")))
+
                 fun arg -> systemRuntimeContainsTypeRef arg
 
-            let providers = [
-                for designTimeAssemblyName in designTimeAssemblyNames do
-                    yield! GetTypeProvidersOfAssembly(fileNameOfRuntimeAssembly,
-                                                                      ilScopeRefOfRuntimeAssembly,
-                                                                      designTimeAssemblyName,
-                                                                      typeProviderEnvironment,
-                                                                      tcConfig.isInvalidationSupported,
-                                                                      tcConfig.isInteractive,
-                                                                      systemRuntimeContainsType,
-                                                                      primaryAssemblyVersion,
-                                                                      tcConfig.compilerToolPaths,
-                                                                      m) ]
+            let providers =
+                [
+                    for designTimeAssemblyName in designTimeAssemblyNames do
+                        yield!
+                            GetTypeProvidersOfAssembly(
+                                fileNameOfRuntimeAssembly,
+                                ilScopeRefOfRuntimeAssembly,
+                                designTimeAssemblyName,
+                                typeProviderEnvironment,
+                                tcConfig.isInvalidationSupported,
+                                tcConfig.isInteractive,
+                                systemRuntimeContainsType,
+                                primaryAssemblyVersion,
+                                tcConfig.compilerToolPaths,
+                                m
+                            )
+                ]
             // Note, type providers are disposable objects. The TcImports owns the provider objects - when/if it is disposed, the providers are disposed.
             // We ignore all exceptions from provider disposal.
             for provider in providers do
@@ -1394,38 +1748,52 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
 
             // Add the invalidation signal handlers to each provider
             for provider in providers do
-                provider.PUntaint((fun tp ->
-
-                    // Register the type provider invalidation handler.
-                    //
-                    // We are explicit about what the handler closure captures to help reason about the
-                    // lifetime of captured objects, especially in case the type provider instance gets leaked
-                    // or keeps itself alive mistakenly, e.g. via some global state in the type provider instance.
-                    //
-                    // The closure captures
-                    //   1. an Event value, ultimately this is made available in all CCus as ccu.InvalidateEvent
-                    //   2. any handlers registered to ccu.InvalidateEvent
-                    //   3. a message string
-                    //
-                    // Note that the invalidation handler does not explicitly capture the TcImports.
-                    // The only place where handlers are registered is to ccu.InvalidateEvent is in IncrementalBuilder.fs.
-
-                    let capturedInvalidateCcu = invalidateCcu
-                    let capturedMessage = "The provider '" + fileNameOfRuntimeAssembly + "' reported a change"
-                    let handler = tp.Invalidate.Subscribe(fun _ -> capturedInvalidateCcu.Trigger capturedMessage)
-
-                    // When the TcImports is disposed we detach the invalidation callback
-                    tcImportsStrong.AttachDisposeTypeProviderAction(fun () -> try handler.Dispose() with _ -> ())), m)
+                provider.PUntaint(
+                    (fun tp ->
+
+                        // Register the type provider invalidation handler.
+                        //
+                        // We are explicit about what the handler closure captures to help reason about the
+                        // lifetime of captured objects, especially in case the type provider instance gets leaked
+                        // or keeps itself alive mistakenly, e.g. via some global state in the type provider instance.
+                        //
+                        // The closure captures
+                        //   1. an Event value, ultimately this is made available in all CCus as ccu.InvalidateEvent
+                        //   2. any handlers registered to ccu.InvalidateEvent
+                        //   3. a message string
+                        //
+                        // Note that the invalidation handler does not explicitly capture the TcImports.
+                        // The only place where handlers are registered is to ccu.InvalidateEvent is in IncrementalBuilder.fs.
+
+                        let capturedInvalidateCcu = invalidateCcu
+
+                        let capturedMessage =
+                            "The provider '" + fileNameOfRuntimeAssembly + "' reported a change"
+
+                        let handler =
+                            tp.Invalidate.Subscribe(fun _ -> capturedInvalidateCcu.Trigger capturedMessage)
+
+                        // When the TcImports is disposed we detach the invalidation callback
+                        tcImportsStrong.AttachDisposeTypeProviderAction(fun () ->
+                            try
+                                handler.Dispose()
+                            with _ ->
+                                ())),
+                    m
+                )
 
             match providers with
             | [] ->
-                warning(Error(FSComp.SR.etHostingAssemblyFoundWithoutHosts(fileNameOfRuntimeAssembly, typeof.FullName), m))
+                let typeName = typeof.FullName
+                warning (Error(FSComp.SR.etHostingAssemblyFoundWithoutHosts (fileNameOfRuntimeAssembly, typeName), m))
             | _ ->
 
 #if DEBUG
                 if typeProviderEnvironment.ShowResolutionMessages then
                     dprintfn "Found extension type hosting hosting assembly '%s' with the following extensions:" fileNameOfRuntimeAssembly
-                    providers |> List.iter(fun provider ->dprintfn " %s" (DisplayNameOfTypeProvider(provider.TypeProvider, m)))
+
+                    providers
+                    |> List.iter (fun provider -> dprintfn " %s" (DisplayNameOfTypeProvider(provider.TypeProvider, m)))
 #endif
 
                 for provider in providers do
@@ -1433,39 +1801,69 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
                         // Inject an entity for the namespace, or if one already exists, then record this as a provider
                         // for that namespace.
                         let rec loop (providedNamespace: Tainted) =
-                            let path = GetProvidedNamespaceAsPath(m, provider, providedNamespace.PUntaint((fun r -> r.NamespaceName), m))
-                            tcImportsStrong.InjectProvidedNamespaceOrTypeIntoEntity (typeProviderEnvironment, tcConfig, m, entityToInjectInto, [], path, provider, None)
+                            let path =
+                                GetProvidedNamespaceAsPath(m, provider, providedNamespace.PUntaint((fun r -> r.NamespaceName), m))
+
+                            tcImportsStrong.InjectProvidedNamespaceOrTypeIntoEntity(
+                                typeProviderEnvironment,
+                                tcConfig,
+                                m,
+                                entityToInjectInto,
+                                [],
+                                path,
+                                provider,
+                                None
+                            )
 
                             // Inject entities for the types returned by provider.GetTypes().
                             //
                             // NOTE: The types provided by GetTypes() are available for name resolution
                             // when the namespace is "opened". This is part of the specification of the language
                             // feature.
-                            let tys = providedNamespace.PApplyArray((fun provider -> provider.GetTypes()), "GetTypes", m)
-                            let ptys = [| for ty in tys -> ty.PApply((fun ty -> ty |> ProvidedType.CreateNoContext), m) |]
-                            for st in ptys do
-                                tcImportsStrong.InjectProvidedNamespaceOrTypeIntoEntity (typeProviderEnvironment, tcConfig, m, entityToInjectInto, [], path, provider, Some st)
+                            let tys =
+                                providedNamespace.PApplyArray((fun provider -> provider.GetTypes()), "GetTypes", m)
+
+                            let ptys =
+                                [|
+                                    for ty in tys -> ty.PApply((fun ty -> ty |> ProvidedType.CreateNoContext), m)
+                                |]
 
-                            for providedNestedNamespace in providedNamespace.PApplyArray((fun provider -> provider.GetNestedNamespaces()), "GetNestedNamespaces", m) do
+                            for st in ptys do
+                                tcImportsStrong.InjectProvidedNamespaceOrTypeIntoEntity(
+                                    typeProviderEnvironment,
+                                    tcConfig,
+                                    m,
+                                    entityToInjectInto,
+                                    [],
+                                    path,
+                                    provider,
+                                    Some st
+                                )
+
+                            for providedNestedNamespace in
+                                providedNamespace.PApplyArray((fun provider -> provider.GetNestedNamespaces()), "GetNestedNamespaces", m) do
                                 loop providedNestedNamespace
 
                         RequireCompilationThread ctok // IProvidedType.GetNamespaces is an example of a type provider call
-                        let providedNamespaces = provider.PApplyArray((fun r -> r.GetNamespaces()), "GetNamespaces", m)
+
+                        let providedNamespaces =
+                            provider.PApplyArray((fun r -> r.GetNamespaces()), "GetNamespaces", m)
 
                         for providedNamespace in providedNamespaces do
                             loop providedNamespace
                     with e ->
                         errorRecovery e m
 
-                if startingErrorCount Option.isSome
@@ -1477,43 +1875,55 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
     // Compact Framework binaries must use this. However it is not
     // clear when else it is required, e.g. for Mono.
 
-    member tcImports.PrepareToImportReferencedILAssembly (ctok, m, fileName, dllinfo: ImportedBinary) =
+    member tcImports.PrepareToImportReferencedILAssembly(ctok, m, fileName, dllinfo: ImportedBinary) =
         CheckDisposed()
         let tcConfig = tcConfigP.Get ctok
         assert dllinfo.RawMetadata.TryGetILModuleDef().IsSome
         let ilModule = dllinfo.RawMetadata.TryGetILModuleDef().Value
         let ilScopeRef = dllinfo.ILScopeRef
-        let aref =
-            match ilScopeRef with
-            | ILScopeRef.Assembly aref -> aref
-            | _ -> error(InternalError("PrepareToImportReferencedILAssembly: cannot reference .NET netmodules directly, reference the containing assembly instead", m))
-
-        let nm = aref.Name
-        if verbose then dprintn ("Converting IL assembly to F# data structures "+nm)
         let auxModuleLoader = tcImports.MkLoaderForMultiModuleILAssemblies ctok m
         let invalidateCcu = Event<_>()
-        let ccu = ImportILAssembly(tcImports.GetImportMap, m, auxModuleLoader, tcConfig.xmlDocInfoLoader, ilScopeRef, tcConfig.implicitIncludeDir, Some fileName, ilModule, invalidateCcu.Publish)
+
+        let ccu =
+            ImportILAssembly(
+                tcImports.GetImportMap,
+                m,
+                auxModuleLoader,
+                tcConfig.xmlDocInfoLoader,
+                ilScopeRef,
+                tcConfig.implicitIncludeDir,
+                Some fileName,
+                ilModule,
+                invalidateCcu.Publish
+            )
 
         let ccuinfo =
-            { FSharpViewOfMetadata=ccu
-              ILScopeRef = ilScopeRef
-              AssemblyAutoOpenAttributes = GetAutoOpenAttributes ilModule
-              AssemblyInternalsVisibleToAttributes = GetInternalsVisibleToAttributes ilModule
+            {
+                FSharpViewOfMetadata = ccu
+                ILScopeRef = ilScopeRef
+                AssemblyAutoOpenAttributes = GetAutoOpenAttributes ilModule
+                AssemblyInternalsVisibleToAttributes = GetInternalsVisibleToAttributes ilModule
 #if !NO_TYPEPROVIDERS
-              IsProviderGenerated = false
-              TypeProviders = []
+                IsProviderGenerated = false
+                TypeProviders = []
 #endif
-              FSharpOptimizationData = notlazy None }
+                FSharpOptimizationData = notlazy None
+            }
+
         tcImports.RegisterCcu ccuinfo
 
         let phase2 () =
 #if !NO_TYPEPROVIDERS
-            ccuinfo.TypeProviders <- tcImports.ImportTypeProviderExtensions (ctok, tcConfig, fileName, ilScopeRef, ilModule.ManifestOfAssembly.CustomAttrs.AsList(), ccu.Contents, invalidateCcu, m)
+            let attrs = ilModule.ManifestOfAssembly.CustomAttrs.AsList()
+
+            ccuinfo.TypeProviders <-
+                tcImports.ImportTypeProviderExtensions(ctok, tcConfig, fileName, ilScopeRef, attrs, ccu.Contents, invalidateCcu, m)
 #endif
-            [ResolvedImportedAssembly ccuinfo]
+            [ ResolvedImportedAssembly ccuinfo ]
+
         phase2
 
-    member tcImports.PrepareToImportReferencedFSharpAssembly (ctok, m, fileName, dllinfo: ImportedBinary) =
+    member tcImports.PrepareToImportReferencedFSharpAssembly(ctok, m, fileName, dllinfo: ImportedBinary) =
         CheckDisposed()
 #if !NO_TYPEPROVIDERS
         let tcConfig = tcConfigP.Get ctok
@@ -1521,14 +1931,15 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
         let ilModule = dllinfo.RawMetadata
         let ilScopeRef = dllinfo.ILScopeRef
         let ilShortAssemName = getNameOfScopeRef ilScopeRef
-        if verbose then dprintn ("Converting F# assembly to F# data structures "+(getNameOfScopeRef ilScopeRef))
-        if verbose then dprintn ("Relinking interface info from F# assembly "+ilShortAssemName)
-        let optDataReaders = ilModule.GetRawFSharpOptimizationData(m, ilShortAssemName, fileName)
+
+        let optDataReaders =
+            ilModule.GetRawFSharpOptimizationData(m, ilShortAssemName, fileName)
 
         let ccuRawDataAndInfos =
             ilModule.GetRawFSharpSignatureData(m, ilShortAssemName, fileName)
             |> List.map (fun (ccuName, sigDataReader) ->
-                let data = GetSignatureData (fileName, ilScopeRef, ilModule.TryGetILModuleDef(), sigDataReader)
+                let data =
+                    GetSignatureData(fileName, ilScopeRef, ilModule.TryGetILModuleDef(), sigDataReader)
 
                 let optDatas = Map.ofList optDataReaders
 
@@ -1540,27 +1951,30 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
 #endif
 
                 let codeDir = minfo.compileTimeWorkingDir
+
+                // note: for some fields we fix up this information later
                 let ccuData: CcuData =
-                    { ILScopeRef=ilScopeRef
-                      Stamp = newStamp()
-                      FileName = Some fileName
-                      QualifiedName= Some(ilScopeRef.QualifiedName)
-                      SourceCodeDirectory = codeDir (* note: in some cases we fix up this information later *)
-                      IsFSharp=true
-                      Contents = mspec
+                    {
+                        ILScopeRef = ilScopeRef
+                        Stamp = newStamp ()
+                        FileName = Some fileName
+                        QualifiedName = Some(ilScopeRef.QualifiedName)
+                        SourceCodeDirectory = codeDir
+                        IsFSharp = true
+                        Contents = mspec
 #if !NO_TYPEPROVIDERS
-                      InvalidateEvent=invalidateCcu.Publish
-                      IsProviderGenerated = false
-                      ImportProvidedType = (fun ty -> ImportProvidedType (tcImports.GetImportMap()) m ty)
+                        InvalidateEvent = invalidateCcu.Publish
+                        IsProviderGenerated = false
+                        ImportProvidedType = (fun ty -> ImportProvidedType (tcImports.GetImportMap()) m ty)
 #endif
-                      TryGetILModuleDef = ilModule.TryGetILModuleDef
-                      UsesFSharp20PlusQuotations = minfo.usesQuotations
-                      MemberSignatureEquality= (fun ty1 ty2 -> typeEquivAux EraseAll (tcImports.GetTcGlobals()) ty1 ty2)
-                      TypeForwarders = ImportILAssemblyTypeForwarders(tcImports.GetImportMap, m, ilModule.GetRawTypeForwarders())
-                      XmlDocumentationInfo =
-                        match tcConfig.xmlDocInfoLoader with
-                        | Some xmlDocInfoLoader -> xmlDocInfoLoader.TryLoad(fileName)
-                        | _ -> None
+                        TryGetILModuleDef = ilModule.TryGetILModuleDef
+                        UsesFSharp20PlusQuotations = minfo.usesQuotations
+                        MemberSignatureEquality = (fun ty1 ty2 -> typeEquivAux EraseAll (tcImports.GetTcGlobals()) ty1 ty2)
+                        TypeForwarders = ImportILAssemblyTypeForwarders(tcImports.GetImportMap, m, ilModule.GetRawTypeForwarders())
+                        XmlDocumentationInfo =
+                            match tcConfig.xmlDocInfoLoader with
+                            | Some xmlDocInfoLoader -> xmlDocInfoLoader.TryLoad(fileName)
+                            | _ -> None
                     }
 
                 let ccu = CcuThunk.Create(ccuName, ccuData)
@@ -1568,182 +1982,223 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
                 let optdata =
                     lazy
                         (match Map.tryFind ccuName optDatas with
-                         | None ->
-                            if verbose then dprintf "*** no optimization data for CCU %s, was DLL compiled with --no-optimization-data??\n" ccuName
-                            None
+                         | None -> None
                          | Some info ->
-                            let data = GetOptimizationData (fileName, ilScopeRef, ilModule.TryGetILModuleDef(), info)
-                            let fixupThunk () = data.OptionalFixup(fun nm -> availableToOptionalCcu(tcImports.FindCcu(ctok, m, nm, lookupOnly=false)))
-
-                            // Make a note of all ccuThunks that may still need to be fixed up when other dlls are loaded
-                            tciLock.AcquireLock (fun tcitok ->
-                                RequireTcImportsLock(tcitok, ccuThunks)
-                                for ccuThunk in data.FixupThunks do
-                                    if ccuThunk.IsUnresolvedReference then
-                                        ccuThunks.Add(ccuThunk, fun () -> fixupThunk () |> ignore)
-                            )
+                             let data =
+                                 GetOptimizationData(fileName, ilScopeRef, ilModule.TryGetILModuleDef(), info)
 
-                            if verbose then dprintf "found optimization data for CCU %s\n" ccuName
-                            Some (fixupThunk ()))
+                             let fixupThunk () =
+                                 data.OptionalFixup(fun nm -> availableToOptionalCcu (tcImports.FindCcu(ctok, m, nm, lookupOnly = false)))
+
+                             // Make a note of all ccuThunks that may still need to be fixed up when other dlls are loaded
+                             tciLock.AcquireLock(fun tcitok ->
+                                 RequireTcImportsLock(tcitok, ccuThunks)
+
+                                 for ccuThunk in data.FixupThunks do
+                                     if ccuThunk.IsUnresolvedReference then
+                                         ccuThunks.Add(ccuThunk, (fun () -> fixupThunk () |> ignore)))
+
+                             Some(fixupThunk ()))
 
                 let ccuinfo =
-                    { FSharpViewOfMetadata=ccu
-                      AssemblyAutoOpenAttributes = ilModule.GetAutoOpenAttributes()
-                      AssemblyInternalsVisibleToAttributes = ilModule.GetInternalsVisibleToAttributes()
-                      FSharpOptimizationData=optdata
+                    {
+                        FSharpViewOfMetadata = ccu
+                        AssemblyAutoOpenAttributes = ilModule.GetAutoOpenAttributes()
+                        AssemblyInternalsVisibleToAttributes = ilModule.GetInternalsVisibleToAttributes()
+                        FSharpOptimizationData = optdata
 #if !NO_TYPEPROVIDERS
-                      IsProviderGenerated = false
-                      TypeProviders = []
+                        IsProviderGenerated = false
+                        TypeProviders = []
 #endif
-                      ILScopeRef = ilScopeRef }
+                        ILScopeRef = ilScopeRef
+                    }
 
-                let phase2() =
+                let phase2 () =
 #if !NO_TYPEPROVIDERS
-                     match ilModule.TryGetILModuleDef() with
-                     | None -> () // no type providers can be used without a real IL Module present
-                     | Some ilModule ->
-                         let tps = tcImports.ImportTypeProviderExtensions (ctok, tcConfig, fileName, ilScopeRef, ilModule.ManifestOfAssembly.CustomAttrs.AsList(), ccu.Contents, invalidateCcu, m)
-                         ccuinfo.TypeProviders <- tps
+                    match ilModule.TryGetILModuleDef() with
+                    | None -> () // no type providers can be used without a real IL Module present
+                    | Some ilModule ->
+                        let attrs = ilModule.ManifestOfAssembly.CustomAttrs.AsList()
+
+                        let tps =
+                            tcImports.ImportTypeProviderExtensions(
+                                ctok,
+                                tcConfig,
+                                fileName,
+                                ilScopeRef,
+                                attrs,
+                                ccu.Contents,
+                                invalidateCcu,
+                                m
+                            )
+
+                        ccuinfo.TypeProviders <- tps
 #else
-                     ()
+                    ()
 #endif
                 data, ccuinfo, phase2)
 
         // Register all before relinking to cope with mutually-referential ccus
         ccuRawDataAndInfos |> List.iter (p23 >> tcImports.RegisterCcu)
+
         let phase2 () =
             // Relink
             ccuRawDataAndInfos
             |> List.iter (fun (data, _, _) ->
-                let fixupThunk () = data.OptionalFixup(fun nm -> availableToOptionalCcu(tcImports.FindCcu(ctok, m, nm, lookupOnly=false))) |> ignore
-                fixupThunk()
+                let fixupThunk () =
+                    data.OptionalFixup(fun nm -> availableToOptionalCcu (tcImports.FindCcu(ctok, m, nm, lookupOnly = false)))
+                    |> ignore
+
+                fixupThunk ()
+
                 for ccuThunk in data.FixupThunks do
                     if ccuThunk.IsUnresolvedReference then
-                      tciLock.AcquireLock <| fun tcitok ->
-                        RequireTcImportsLock(tcitok, ccuThunks)
-                        ccuThunks.Add(ccuThunk, fixupThunk)
-                )
+                        tciLock.AcquireLock(fun tcitok ->
+                            RequireTcImportsLock(tcitok, ccuThunks)
+                            ccuThunks.Add(ccuThunk, fixupThunk)))
 #if !NO_TYPEPROVIDERS
-            ccuRawDataAndInfos |> List.iter (fun (_, _, phase2) -> phase2())
+            ccuRawDataAndInfos |> List.iter (fun (_, _, phase2) -> phase2 ())
 #endif
             ccuRawDataAndInfos |> List.map p23 |> List.map ResolvedImportedAssembly
+
         phase2
 
     // NOTE: When used in the Language Service this can cause the transitive checking of projects. Hence it must be cancellable.
-    member tcImports.TryRegisterAndPrepareToImportReferencedDll (ctok, r: AssemblyResolution) : NodeCode<(_ * (unit -> AvailableImportedAssembly list)) option> =
-      node {
-        CheckDisposed()
-        let m = r.originalReference.Range
-        let fileName = r.resolvedPath
-        let! contentsOpt =
-          node {
-            match r.ProjectReference with
-            | Some ilb -> 
-                return! ilb.EvaluateRawContents()
-            | None -> 
-                return ProjectAssemblyDataResult.Unavailable true
-          }
-
-        // If we have a project reference but did not get any valid contents,
-        //     just return None and do not attempt to read elsewhere.
-        match contentsOpt with 
-        | ProjectAssemblyDataResult.Unavailable false ->
-            return None
-        | _ ->
-
-        let assemblyData =
+    member tcImports.TryRegisterAndPrepareToImportReferencedDll
+        (
+            ctok,
+            r: AssemblyResolution
+        ) : NodeCode<(_ * (unit -> AvailableImportedAssembly list)) option> =
+        node {
+            CheckDisposed()
+            let m = r.originalReference.Range
+            let fileName = r.resolvedPath
+
+            let! contentsOpt =
+                node {
+                    match r.ProjectReference with
+                    | Some ilb -> return! ilb.EvaluateRawContents()
+                    | None -> return ProjectAssemblyDataResult.Unavailable true
+                }
+
+            // If we have a project reference but did not get any valid contents,
+            //     just return None and do not attempt to read elsewhere.
             match contentsOpt with
-            | ProjectAssemblyDataResult.Available ilb -> ilb
-            | ProjectAssemblyDataResult.Unavailable _ ->
-                let ilModule, ilAssemblyRefs = tcImports.OpenILBinaryModule(ctok, fileName, m)
-                RawFSharpAssemblyDataBackedByFileOnDisk (ilModule, ilAssemblyRefs) :> IRawFSharpAssemblyData
-
-        let ilShortAssemName = assemblyData.ShortAssemblyName
-        let ilScopeRef = assemblyData.ILScopeRef
-
-        if tcImports.IsAlreadyRegistered ilShortAssemName then
-            let dllinfo = tcImports.FindDllInfo(ctok, m, ilShortAssemName)
-            let phase2() = [tcImports.FindCcuInfo(ctok, m, ilShortAssemName, lookupOnly=true)]
-            return Some(dllinfo, phase2)
-        else
-            let dllinfo =
-                { RawMetadata=assemblyData
-                  FileName=fileName
+            | ProjectAssemblyDataResult.Unavailable false -> return None
+            | _ ->
+
+                let assemblyData =
+                    match contentsOpt with
+                    | ProjectAssemblyDataResult.Available ilb -> ilb
+                    | ProjectAssemblyDataResult.Unavailable _ ->
+                        let ilModule, ilAssemblyRefs = tcImports.OpenILBinaryModule(ctok, fileName, m)
+                        RawFSharpAssemblyDataBackedByFileOnDisk(ilModule, ilAssemblyRefs) :> IRawFSharpAssemblyData
+
+                let ilShortAssemName = assemblyData.ShortAssemblyName
+                let ilScopeRef = assemblyData.ILScopeRef
+
+                if tcImports.IsAlreadyRegistered ilShortAssemName then
+                    let dllinfo = tcImports.FindDllInfo(ctok, m, ilShortAssemName)
+
+                    let phase2 () =
+                        [ tcImports.FindCcuInfo(ctok, m, ilShortAssemName, lookupOnly = true) ]
+
+                    return Some(dllinfo, phase2)
+                else
+                    let dllinfo =
+                        {
+                            RawMetadata = assemblyData
+                            FileName = fileName
 #if !NO_TYPEPROVIDERS
-                  ProviderGeneratedAssembly=None
-                  IsProviderGenerated=false
-                  ProviderGeneratedStaticLinkMap = None
+                            ProviderGeneratedAssembly = None
+                            IsProviderGenerated = false
+                            ProviderGeneratedStaticLinkMap = None
 #endif
-                  ILScopeRef = ilScopeRef
-                  ILAssemblyRefs = assemblyData.ILAssemblyRefs }
-            tcImports.RegisterDll dllinfo
-            let phase2 =
-                if assemblyData.HasAnyFSharpSignatureDataAttribute then
-                    if not assemblyData.HasMatchingFSharpSignatureDataAttribute then
-                        errorR(Error(FSComp.SR.buildDifferentVersionMustRecompile fileName, m))
-                        tcImports.PrepareToImportReferencedILAssembly (ctok, m, fileName, dllinfo)
-                    else
-                        try
-                        tcImports.PrepareToImportReferencedFSharpAssembly (ctok, m, fileName, dllinfo)
-                        with e -> error(Error(FSComp.SR.buildErrorOpeningBinaryFile(fileName, e.Message), m))
-                else
-                    tcImports.PrepareToImportReferencedILAssembly (ctok, m, fileName, dllinfo)
-            return Some(dllinfo, phase2)
-         }
+                            ILScopeRef = ilScopeRef
+                            ILAssemblyRefs = assemblyData.ILAssemblyRefs
+                        }
+
+                    tcImports.RegisterDll dllinfo
+
+                    let phase2 =
+                        if assemblyData.HasAnyFSharpSignatureDataAttribute then
+                            if not assemblyData.HasMatchingFSharpSignatureDataAttribute then
+                                errorR (Error(FSComp.SR.buildDifferentVersionMustRecompile fileName, m))
+                                tcImports.PrepareToImportReferencedILAssembly(ctok, m, fileName, dllinfo)
+                            else
+                                try
+                                    tcImports.PrepareToImportReferencedFSharpAssembly(ctok, m, fileName, dllinfo)
+                                with e ->
+                                    error (Error(FSComp.SR.buildErrorOpeningBinaryFile (fileName, e.Message), m))
+                        else
+                            tcImports.PrepareToImportReferencedILAssembly(ctok, m, fileName, dllinfo)
 
-    // NOTE: When used in the Language Service this can cause the transitive checking of projects. Hence it must be cancellable.
-    member tcImports.RegisterAndImportReferencedAssemblies (ctok, nms: AssemblyResolution list) =
-      node {
-        CheckDisposed()
+                    return Some(dllinfo, phase2)
+        }
 
-        let! results =
-            nms
-            |> List.map (fun nm -> 
-                node {
-                    try
-                         return! tcImports.TryRegisterAndPrepareToImportReferencedDll (ctok, nm)
-                    with e ->
-                         errorR(Error(FSComp.SR.buildProblemReadingAssembly(nm.resolvedPath, e.Message), nm.originalReference.Range))
-                         return None
-                }
-            )
-            |> NodeCode.Sequential
+    // NOTE: When used in the Language Service this can cause the transitive checking of projects. Hence it must be cancellable.
+    member tcImports.RegisterAndImportReferencedAssemblies(ctok, nms: AssemblyResolution list) =
+        node {
+            CheckDisposed()
 
-        let dllinfos, phase2s = results |> Array.choose id |> List.ofArray |> List.unzip
-        fixupOrphanCcus()
-        let ccuinfos = (List.collect (fun phase2 -> phase2()) phase2s)
-        return dllinfos, ccuinfos
-      }
+            let! results =
+                nms
+                |> List.map (fun nm ->
+                    node {
+                        try
+                            return! tcImports.TryRegisterAndPrepareToImportReferencedDll(ctok, nm)
+                        with e ->
+                            errorR (Error(FSComp.SR.buildProblemReadingAssembly (nm.resolvedPath, e.Message), nm.originalReference.Range))
+                            return None
+                    })
+                |> NodeCode.Sequential
+
+            let dllinfos, phase2s = results |> Array.choose id |> List.ofArray |> List.unzip
+            fixupOrphanCcus ()
+            let ccuinfos = (List.collect (fun phase2 -> phase2 ()) phase2s)
+            return dllinfos, ccuinfos
+        }
 
     /// Note that implicit loading is not used for compilations from MSBuild, which passes ``--noframework``
     /// Implicit loading is done in non-cancellation mode. Implicit loading is never used in the language service, so
     /// no cancellation is needed.
-    member tcImports.ImplicitLoadIfAllowed (ctok, m, assemblyName, lookupOnly) =
+    member tcImports.ImplicitLoadIfAllowed(ctok, m, assemblyName, lookupOnly) =
         CheckDisposed()
         // If the user is asking for the default framework then also try to resolve other implicit assemblies as they are discovered.
         // Using this flag to mean 'allow implicit discover of assemblies'.
         let tcConfig = tcConfigP.Get ctok
+
         if not lookupOnly && tcConfig.implicitlyResolveAssemblies then
             let tryFile speculativeFileName =
-                let foundFile = tcImports.TryResolveAssemblyReference (ctok, AssemblyReference (m, speculativeFileName, None), ResolveAssemblyReferenceMode.Speculative)
+                let foundFile =
+                    tcImports.TryResolveAssemblyReference(
+                        ctok,
+                        AssemblyReference(m, speculativeFileName, None),
+                        ResolveAssemblyReferenceMode.Speculative
+                    )
+
                 match foundFile with
                 | OkResult (warns, res) ->
                     ReportWarnings warns
-                    tcImports.RegisterAndImportReferencedAssemblies(ctok, res) 
-                    |> NodeCode.RunImmediateWithoutCancellation 
+
+                    tcImports.RegisterAndImportReferencedAssemblies(ctok, res)
+                    |> NodeCode.RunImmediateWithoutCancellation
                     |> ignore
+
                     true
                 | ErrorResult (_warns, _err) ->
                     // Throw away warnings and errors - this is speculative loading
                     false
 
-            if tryFile (assemblyName + ".dll") then ()
-            else tryFile (assemblyName + ".exe") |> ignore
+            if tryFile (assemblyName + ".dll") then
+                ()
+            else
+                tryFile (assemblyName + ".exe") |> ignore
 
 #if !NO_TYPEPROVIDERS
     member tcImports.TryFindProviderGeneratedAssemblyByName(ctok, assemblyName: string) : System.Reflection.Assembly option =
         // The assembly may not be in the resolutions, but may be in the load set including EST injected assemblies
-        match tcImports.TryFindDllInfo (ctok, range0, assemblyName, lookupOnly=true) with
+        match tcImports.TryFindDllInfo(ctok, range0, assemblyName, lookupOnly = true) with
         | Some res ->
             // Provider-generated assemblies don't necessarily have an on-disk representation we can load.
             res.ProviderGeneratedAssembly
@@ -1752,64 +2207,74 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
 
     /// Only used by F# Interactive
     member tcImports.TryFindExistingFullyQualifiedPathBySimpleAssemblyName simpleAssemName : string option =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, resolutions)
-        resolutions.TryFindBySimpleAssemblyName simpleAssemName |> Option.map (fun r -> r.resolvedPath)
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, resolutions)
+
+            resolutions.TryFindBySimpleAssemblyName simpleAssemName
+            |> Option.map (fun r -> r.resolvedPath))
 
     /// Only used by F# Interactive
     member tcImports.TryFindExistingFullyQualifiedPathByExactAssemblyRef(assemblyRef: ILAssemblyRef) : string option =
-      tciLock.AcquireLock <| fun tcitok ->
-        RequireTcImportsLock(tcitok, resolutions)
-        resolutions.TryFindByExactILAssemblyRef assemblyRef |> Option.map (fun r -> r.resolvedPath)
-
-    member tcImports.TryResolveAssemblyReference(ctok, assemblyReference: AssemblyReference, mode: ResolveAssemblyReferenceMode) : OperationResult =
-      tciLock.AcquireLock <| fun tcitok ->
-        let tcConfig = tcConfigP.Get ctok
+        tciLock.AcquireLock(fun tcitok ->
+            RequireTcImportsLock(tcitok, resolutions)
+
+            resolutions.TryFindByExactILAssemblyRef assemblyRef
+            |> Option.map (fun r -> r.resolvedPath))
+
+    member tcImports.TryResolveAssemblyReference
+        (
+            ctok,
+            assemblyReference: AssemblyReference,
+            mode: ResolveAssemblyReferenceMode
+        ) : OperationResult =
+        tciLock.AcquireLock(fun tcitok ->
+            let tcConfig = tcConfigP.Get ctok
 
-        RequireTcImportsLock(tcitok, resolutions)
-        // First try to lookup via the original reference text.
-        match resolutions.TryFindByOriginalReference assemblyReference with
-        | Some assemblyResolution ->
-            ResultD [assemblyResolution]
-        | None ->
+            RequireTcImportsLock(tcitok, resolutions)
+            // First try to lookup via the original reference text.
+            match resolutions.TryFindByOriginalReference assemblyReference with
+            | Some assemblyResolution -> ResultD [ assemblyResolution ]
+            | None ->
 #if NO_MSBUILD_REFERENCE_RESOLUTION
-           try
-               ResultD [tcConfig.ResolveLibWithDirectories assemblyReference]
-           with e ->
-               ErrorD e
+                try
+                    ResultD [ tcConfig.ResolveLibWithDirectories assemblyReference ]
+                with e ->
+                    ErrorD e
 #else
-            // Next try to lookup up by the exact full resolved path.
-            match resolutions.TryFindByResolvedPath assemblyReference.Text with
-            | Some assemblyResolution ->
-                ResultD [assemblyResolution]
-            | None ->
-                if tcConfigP.Get(ctok).useSimpleResolution then
-                    let action =
-                        match mode with
-                        | ResolveAssemblyReferenceMode.ReportErrors -> CcuLoadFailureAction.RaiseError
-                        | ResolveAssemblyReferenceMode.Speculative -> CcuLoadFailureAction.ReturnNone
-                    match tcConfig.ResolveLibWithDirectories (action, assemblyReference) with
-                    | Some resolved ->
-                        resolutions <- resolutions.AddResolutionResults [resolved]
-                        ResultD [resolved]
-                    | None ->
-                        ErrorD(AssemblyNotResolved(assemblyReference.Text, assemblyReference.Range))
-                else
-                    // This is a previously unencountered assembly. Resolve it and add it to the list.
-                    // But don't cache resolution failures because the assembly may appear on the disk later.
-                    let resolved, unresolved = TcConfig.TryResolveLibsUsingMSBuildRules(tcConfig, [ assemblyReference ], assemblyReference.Range, mode)
-                    match resolved, unresolved with
-                    | assemblyResolution :: _, _ ->
-                        resolutions <- resolutions.AddResolutionResults resolved
-                        ResultD [assemblyResolution]
-                    | _, _ :: _ ->
-                        resolutions <- resolutions.AddUnresolvedReferences unresolved
-                        ErrorD(AssemblyNotResolved(assemblyReference.Text, assemblyReference.Range))
-                    | [], [] ->
-                        // Note, if mode=ResolveAssemblyReferenceMode.Speculative and the resolution failed then TryResolveLibsUsingMSBuildRules returns
-                        // the empty list and we convert the failure into an AssemblyNotResolved here.
-                        ErrorD(AssemblyNotResolved(assemblyReference.Text, assemblyReference.Range))
+                // Next try to lookup up by the exact full resolved path.
+                match resolutions.TryFindByResolvedPath assemblyReference.Text with
+                | Some assemblyResolution -> ResultD [ assemblyResolution ]
+                | None ->
+                    if tcConfigP.Get(ctok).useSimpleResolution then
+                        let action =
+                            match mode with
+                            | ResolveAssemblyReferenceMode.ReportErrors -> CcuLoadFailureAction.RaiseError
+                            | ResolveAssemblyReferenceMode.Speculative -> CcuLoadFailureAction.ReturnNone
+
+                        match tcConfig.ResolveLibWithDirectories(action, assemblyReference) with
+                        | Some resolved ->
+                            resolutions <- resolutions.AddResolutionResults [ resolved ]
+                            ResultD [ resolved ]
+                        | None -> ErrorD(AssemblyNotResolved(assemblyReference.Text, assemblyReference.Range))
+                    else
+                        // This is a previously unencountered assembly. Resolve it and add it to the list.
+                        // But don't cache resolution failures because the assembly may appear on the disk later.
+                        let resolved, unresolved =
+                            TcConfig.TryResolveLibsUsingMSBuildRules(tcConfig, [ assemblyReference ], assemblyReference.Range, mode)
+
+                        match resolved, unresolved with
+                        | assemblyResolution :: _, _ ->
+                            resolutions <- resolutions.AddResolutionResults resolved
+                            ResultD [ assemblyResolution ]
+                        | _, _ :: _ ->
+                            resolutions <- resolutions.AddUnresolvedReferences unresolved
+                            ErrorD(AssemblyNotResolved(assemblyReference.Text, assemblyReference.Range))
+                        | [], [] ->
+                            // Note, if mode=ResolveAssemblyReferenceMode.Speculative and the resolution failed then TryResolveLibsUsingMSBuildRules returns
+                            // the empty list and we convert the failure into an AssemblyNotResolved here.
+                            ErrorD(AssemblyNotResolved(assemblyReference.Text, assemblyReference.Range))
 #endif
+        )
 
     member tcImports.ResolveAssemblyReference(ctok, assemblyReference, mode) : AssemblyResolution list =
         CommitOperationResult(tcImports.TryResolveAssemblyReference(ctok, assemblyReference, mode))
@@ -1817,181 +2282,258 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
     // Note: This returns a TcImports object. However, framework TcImports are not currently disposed. The only reason
     // we dispose TcImports is because we need to dispose type providers, and type providers are never included in the framework DLL set.
     // If a framework set ever includes type providers, you will not have to worry about explicitly calling Dispose as the Finalizer will handle it.
-    static member BuildFrameworkTcImports (tcConfigP: TcConfigProvider, frameworkDLLs, nonFrameworkDLLs) =
-      node {
-        let ctok = CompilationThreadToken()
-        let tcConfig = tcConfigP.Get ctok
-        let tcResolutions = TcAssemblyResolutions.BuildFromPriorResolutions(tcConfig, frameworkDLLs, [])
-        let tcAltResolutions = TcAssemblyResolutions.BuildFromPriorResolutions(tcConfig, nonFrameworkDLLs, [])
-
-        let frameworkTcImports = new TcImports(tcConfigP, tcResolutions, None, None)
-
-        // Fetch the primaryAssembly from the referenced assemblies otherwise
-        let primaryAssemblyReference =
-            let path = frameworkDLLs |> List.tryFind(fun dll -> String.Compare(Path.GetFileNameWithoutExtension(dll.resolvedPath), tcConfig.primaryAssembly.Name, StringComparison.OrdinalIgnoreCase) = 0)
-            match path with
-            | Some p -> AssemblyReference(range0, p.resolvedPath, None)
-            | None -> tcConfig.PrimaryAssemblyDllReference()
-
-        let primaryAssemblyResolution = frameworkTcImports.ResolveAssemblyReference(ctok, primaryAssemblyReference, ResolveAssemblyReferenceMode.ReportErrors)
-        let! primaryAssem = frameworkTcImports.RegisterAndImportReferencedAssemblies(ctok, primaryAssemblyResolution)
-        let primaryScopeRef =
-            match primaryAssem with
-              | _, [ResolvedImportedAssembly ccu] -> ccu.FSharpViewOfMetadata.ILScopeRef
-              | _ -> failwith "primaryScopeRef - unexpected"
-
-        let primaryAssemblyResolvedPath =
-            match primaryAssemblyResolution with
-            | [primaryAssemblyResolution] -> primaryAssemblyResolution.resolvedPath
-            | _ -> failwith "primaryAssemblyResolvedPath - unexpected"
-
-        let resolvedAssemblies = tcResolutions.GetAssemblyResolutions()
-
-        let readerSettings: ILReaderOptions =
-            { pdbDirPath=None
-              reduceMemoryUsage = tcConfig.reduceMemoryUsage
-              metadataOnly = MetadataOnlyFlag.Yes
-              tryGetMetadataSnapshot = tcConfig.tryGetMetadataSnapshot }
-
-        let tryFindAssemblyByExportedType manifest (exportedType: ILExportedTypeOrForwarder) =
-            match exportedType.ScopeRef, primaryScopeRef with
-            | ILScopeRef.Assembly aref1, ILScopeRef.Assembly aref2 when aref1.EqualsIgnoringVersion aref2 ->
-                mkRefToILAssembly manifest
-                |> Some
-            | _ ->
-                None
+    static member BuildFrameworkTcImports(tcConfigP: TcConfigProvider, frameworkDLLs, nonFrameworkDLLs) =
+        node {
+            let ctok = CompilationThreadToken()
+            let tcConfig = tcConfigP.Get ctok
 
-        let tryFindAssemblyThatForwardsToPrimaryAssembly manifest =
-            manifest.ExportedTypes.TryFindByName "System.Object"
-            |> Option.bind (tryFindAssemblyByExportedType manifest)
-
-        // Determine what other assemblies could have been the primary assembly
-        // by checking to see if "System.Object" is an exported type.
-        let assembliesThatForwardToPrimaryAssembly =
-            resolvedAssemblies
-            |> List.choose (fun resolvedAssembly ->
-                if primaryAssemblyResolvedPath <> resolvedAssembly.resolvedPath then
-                    let reader = OpenILModuleReader resolvedAssembly.resolvedPath readerSettings
-                    reader.ILModuleDef.Manifest
-                    |> Option.bind tryFindAssemblyThatForwardsToPrimaryAssembly
-                else
-                    None)
+            let tcResolutions =
+                TcAssemblyResolutions.BuildFromPriorResolutions(tcConfig, frameworkDLLs, [])
 
-        let! fslibCcu, fsharpCoreAssemblyScopeRef =
-            node {
-                if tcConfig.compilingFSharpCore then
-                    // When compiling FSharp.Core.dll, the fslibCcu reference to FSharp.Core.dll is a delayed ccu thunk fixed up during type checking
-                    return CcuThunk.CreateDelayed getFSharpCoreLibraryName, ILScopeRef.Local
-                else
-                    let coreLibraryReference = tcConfig.CoreLibraryDllReference()
+            let tcAltResolutions =
+                TcAssemblyResolutions.BuildFromPriorResolutions(tcConfig, nonFrameworkDLLs, [])
 
-                    let resolvedAssemblyRef =
-                        match tcResolutions.TryFindByOriginalReference coreLibraryReference with
-                        | Some resolution -> Some resolution
-                        | _ ->
-                            // Are we using a "non-canonical" FSharp.Core?
-                            match tcAltResolutions.TryFindByOriginalReference coreLibraryReference with
-                            | Some resolution -> Some resolution
-                            | _ -> tcResolutions.TryFindByOriginalReferenceText getFSharpCoreLibraryName  // was the ".dll" elided?
+            let frameworkTcImports = new TcImports(tcConfigP, tcResolutions, None, None)
 
-                    match resolvedAssemblyRef with
-                    | Some coreLibraryResolution ->
-                        match! frameworkTcImports.RegisterAndImportReferencedAssemblies(ctok, [coreLibraryResolution]) with
-                        | _, [ResolvedImportedAssembly fslibCcuInfo ] -> return fslibCcuInfo.FSharpViewOfMetadata, fslibCcuInfo.ILScopeRef
-                        | _ ->
-                            return error(InternalError("BuildFrameworkTcImports: no successful import of "+coreLibraryResolution.resolvedPath, coreLibraryResolution.originalReference.Range))
-                    | None ->
-                        return error(InternalError(sprintf "BuildFrameworkTcImports: no resolution of '%s'" coreLibraryReference.Text, rangeStartup))   
-            }
+            // Fetch the primaryAssembly from the referenced assemblies otherwise
+            let primaryAssemblyReference =
+                let path =
+                    frameworkDLLs
+                    |> List.tryFind (fun dll ->
+                        let baseName = Path.GetFileNameWithoutExtension(dll.resolvedPath)
 
-        // Load the rest of the framework DLLs all at once (they may be mutually recursive)
-        let! _assemblies = frameworkTcImports.RegisterAndImportReferencedAssemblies (ctok, resolvedAssemblies)
+                        let res =
+                            String.Compare(baseName, tcConfig.primaryAssembly.Name, StringComparison.OrdinalIgnoreCase)
 
-        // These are the DLLs we can search for well-known types
-        let sysCcus =
-             [| for ccu in frameworkTcImports.GetCcusInDeclOrder() do
-                   yield ccu |]
+                        res = 0)
 
-        let tryFindSysTypeCcu path typeName =
-            sysCcus |> Array.tryFind (fun ccu -> ccuHasType ccu path typeName)
+                match path with
+                | Some p -> AssemblyReference(range0, p.resolvedPath, None)
+                | None -> tcConfig.PrimaryAssemblyDllReference()
 
-        let ilGlobals = mkILGlobals (primaryScopeRef, assembliesThatForwardToPrimaryAssembly, fsharpCoreAssemblyScopeRef)
+            let primaryAssemblyResolution =
+                frameworkTcImports.ResolveAssemblyReference(ctok, primaryAssemblyReference, ResolveAssemblyReferenceMode.ReportErrors)
 
-        // OK, now we have both mscorlib.dll and FSharp.Core.dll we can create TcGlobals
-        let tcGlobals =
-            TcGlobals(tcConfig.compilingFSharpCore,
-                ilGlobals,
-                fslibCcu,
-                tcConfig.implicitIncludeDir,
-                tcConfig.mlCompatibility,
-                tcConfig.isInteractive,
-                tryFindSysTypeCcu,
-                tcConfig.emitDebugInfoInQuotations,
-                tcConfig.noDebugAttributes,
-                tcConfig.pathMap,
-                tcConfig.langVersion)
+            let! primaryAssem = frameworkTcImports.RegisterAndImportReferencedAssemblies(ctok, primaryAssemblyResolution)
+
+            let primaryScopeRef =
+                match primaryAssem with
+                | _, [ ResolvedImportedAssembly ccu ] -> ccu.FSharpViewOfMetadata.ILScopeRef
+                | _ -> failwith "primaryScopeRef - unexpected"
+
+            let primaryAssemblyResolvedPath =
+                match primaryAssemblyResolution with
+                | [ primaryAssemblyResolution ] -> primaryAssemblyResolution.resolvedPath
+                | _ -> failwith "primaryAssemblyResolvedPath - unexpected"
+
+            let resolvedAssemblies = tcResolutions.GetAssemblyResolutions()
+
+            let readerSettings: ILReaderOptions =
+                {
+                    pdbDirPath = None
+                    reduceMemoryUsage = tcConfig.reduceMemoryUsage
+                    metadataOnly = MetadataOnlyFlag.Yes
+                    tryGetMetadataSnapshot = tcConfig.tryGetMetadataSnapshot
+                }
+
+            let tryFindAssemblyByExportedType manifest (exportedType: ILExportedTypeOrForwarder) =
+                match exportedType.ScopeRef, primaryScopeRef with
+                | ILScopeRef.Assembly aref1, ILScopeRef.Assembly aref2 when aref1.EqualsIgnoringVersion aref2 ->
+                    mkRefToILAssembly manifest |> Some
+                | _ -> None
+
+            let tryFindAssemblyThatForwardsToPrimaryAssembly manifest =
+                manifest.ExportedTypes.TryFindByName "System.Object"
+                |> Option.bind (tryFindAssemblyByExportedType manifest)
+
+            // Determine what other assemblies could have been the primary assembly
+            // by checking to see if "System.Object" is an exported type.
+            let assembliesThatForwardToPrimaryAssembly =
+                resolvedAssemblies
+                |> List.choose (fun resolvedAssembly ->
+                    if primaryAssemblyResolvedPath <> resolvedAssembly.resolvedPath then
+                        let reader = OpenILModuleReader resolvedAssembly.resolvedPath readerSettings
+
+                        reader.ILModuleDef.Manifest
+                        |> Option.bind tryFindAssemblyThatForwardsToPrimaryAssembly
+                    else
+                        None)
+
+            let! fslibCcu, fsharpCoreAssemblyScopeRef =
+                node {
+                    if tcConfig.compilingFSharpCore then
+                        // When compiling FSharp.Core.dll, the fslibCcu reference to FSharp.Core.dll is a delayed ccu thunk fixed up during type checking
+                        return CcuThunk.CreateDelayed getFSharpCoreLibraryName, ILScopeRef.Local
+                    else
+                        let coreLibraryReference = tcConfig.CoreLibraryDllReference()
+
+                        let resolvedAssemblyRef =
+                            match tcResolutions.TryFindByOriginalReference coreLibraryReference with
+                            | Some resolution -> Some resolution
+                            | _ ->
+                                // Are we using a "non-canonical" FSharp.Core?
+                                match tcAltResolutions.TryFindByOriginalReference coreLibraryReference with
+                                | Some resolution -> Some resolution
+                                | _ -> tcResolutions.TryFindByOriginalReferenceText getFSharpCoreLibraryName // was the ".dll" elided?
+
+                        match resolvedAssemblyRef with
+                        | Some coreLibraryResolution ->
+                            match! frameworkTcImports.RegisterAndImportReferencedAssemblies(ctok, [ coreLibraryResolution ]) with
+                            | _, [ ResolvedImportedAssembly fslibCcuInfo ] ->
+                                return fslibCcuInfo.FSharpViewOfMetadata, fslibCcuInfo.ILScopeRef
+                            | _ ->
+                                return
+                                    error (
+                                        InternalError(
+                                            $"no import of {coreLibraryResolution.resolvedPath}",
+                                            coreLibraryResolution.originalReference.Range
+                                        )
+                                    )
+                        | None -> return error (InternalError($"no resolution of '{coreLibraryReference.Text}'", rangeStartup))
+                }
+
+            // Load the rest of the framework DLLs all at once (they may be mutually recursive)
+            let! _assemblies = frameworkTcImports.RegisterAndImportReferencedAssemblies(ctok, resolvedAssemblies)
+
+            // These are the DLLs we can search for well-known types
+            let sysCcus =
+                [|
+                    for ccu in frameworkTcImports.GetCcusInDeclOrder() do
+                        ccu
+                |]
+
+            let tryFindSysTypeCcu path typeName =
+                sysCcus |> Array.tryFind (fun ccu -> ccuHasType ccu path typeName)
+
+            let ilGlobals =
+                mkILGlobals (primaryScopeRef, assembliesThatForwardToPrimaryAssembly, fsharpCoreAssemblyScopeRef)
+
+            // OK, now we have both mscorlib.dll and FSharp.Core.dll we can create TcGlobals
+            let tcGlobals =
+                TcGlobals(
+                    tcConfig.compilingFSharpCore,
+                    ilGlobals,
+                    fslibCcu,
+                    tcConfig.implicitIncludeDir,
+                    tcConfig.mlCompatibility,
+                    tcConfig.isInteractive,
+                    tryFindSysTypeCcu,
+                    tcConfig.emitDebugInfoInQuotations,
+                    tcConfig.noDebugAttributes,
+                    tcConfig.pathMap,
+                    tcConfig.langVersion
+                )
 
 #if DEBUG
-        // the global_g reference cell is used only for debug printing
-        global_g <- Some tcGlobals
+            // the global_g reference cell is used only for debug printing
+            global_g <- Some tcGlobals
 #endif
-        frameworkTcImports.SetTcGlobals tcGlobals
-        return tcGlobals, frameworkTcImports
-      }
+            frameworkTcImports.SetTcGlobals tcGlobals
+            return tcGlobals, frameworkTcImports
+        }
 
     member tcImports.ReportUnresolvedAssemblyReferences knownUnresolved =
         // Report that an assembly was not resolved.
-        let reportAssemblyNotResolved(file, originalReferences: AssemblyReference list) =
-            originalReferences |> List.iter(fun originalReference -> errorR(AssemblyNotResolved(file, originalReference.Range)))
+        let reportAssemblyNotResolved (file, originalReferences: AssemblyReference list) =
+            originalReferences
+            |> List.iter (fun originalReference -> errorR (AssemblyNotResolved(file, originalReference.Range)))
+
         knownUnresolved
-        |> List.map (function UnresolvedAssemblyReference(file, originalReferences) -> file, originalReferences)
+        |> List.map (function
+            | UnresolvedAssemblyReference (file, originalReferences) -> file, originalReferences)
         |> List.iter reportAssemblyNotResolved
 
     static member BuildNonFrameworkTcImports
-       (tcConfigP: TcConfigProvider, baseTcImports,
-        nonFrameworkReferences, knownUnresolved, dependencyProvider) =
+        (
+            tcConfigP: TcConfigProvider,
+            baseTcImports,
+            nonFrameworkReferences,
+            knownUnresolved,
+            dependencyProvider
+        ) =
+
+        node {
+            let ctok = CompilationThreadToken()
+            let tcConfig = tcConfigP.Get ctok
 
-      node {
-        let ctok = CompilationThreadToken()
-        let tcConfig = tcConfigP.Get ctok
-        let tcResolutions = TcAssemblyResolutions.BuildFromPriorResolutions(tcConfig, nonFrameworkReferences, knownUnresolved)
-        let references = tcResolutions.GetAssemblyResolutions()
-        let tcImports = new TcImports(tcConfigP, tcResolutions, Some baseTcImports, Some dependencyProvider)
-        let! _assemblies = tcImports.RegisterAndImportReferencedAssemblies(ctok, references)
-        tcImports.ReportUnresolvedAssemblyReferences knownUnresolved
-        return tcImports
-      }
+            let tcResolutions =
+                TcAssemblyResolutions.BuildFromPriorResolutions(tcConfig, nonFrameworkReferences, knownUnresolved)
+
+            let references = tcResolutions.GetAssemblyResolutions()
+
+            let tcImports =
+                new TcImports(tcConfigP, tcResolutions, Some baseTcImports, Some dependencyProvider)
+
+            let! _assemblies = tcImports.RegisterAndImportReferencedAssemblies(ctok, references)
+            tcImports.ReportUnresolvedAssemblyReferences knownUnresolved
+            return tcImports
+        }
 
     static member BuildTcImports(tcConfigP: TcConfigProvider, dependencyProvider) =
-      node {
-        let ctok = CompilationThreadToken()
-        let tcConfig = tcConfigP.Get ctok
-        let frameworkDLLs, nonFrameworkReferences, knownUnresolved = TcAssemblyResolutions.SplitNonFoundationalResolutions(tcConfig)
-        let! tcGlobals, frameworkTcImports = TcImports.BuildFrameworkTcImports (tcConfigP, frameworkDLLs, nonFrameworkReferences)
-        let! tcImports = TcImports.BuildNonFrameworkTcImports(tcConfigP, frameworkTcImports, nonFrameworkReferences, knownUnresolved, dependencyProvider)
-        return tcGlobals, tcImports
-      }
+        node {
+            let ctok = CompilationThreadToken()
+            let tcConfig = tcConfigP.Get ctok
+
+            let frameworkDLLs, nonFrameworkReferences, knownUnresolved =
+                TcAssemblyResolutions.SplitNonFoundationalResolutions(tcConfig)
+
+            let! tcGlobals, frameworkTcImports = TcImports.BuildFrameworkTcImports(tcConfigP, frameworkDLLs, nonFrameworkReferences)
+
+            let! tcImports =
+                TcImports.BuildNonFrameworkTcImports(
+                    tcConfigP,
+                    frameworkTcImports,
+                    nonFrameworkReferences,
+                    knownUnresolved,
+                    dependencyProvider
+                )
+
+            return tcGlobals, tcImports
+        }
 
     interface IDisposable with
-        member tcImports.Dispose() =
-            dispose ()
+        member tcImports.Dispose() = dispose ()
 
     override tcImports.ToString() = "TcImports(...)"
 
 /// Process #r in F# Interactive.
 /// Adds the reference to the tcImports and add the ccu to the type checking environment.
 let RequireDLL (ctok, tcImports: TcImports, tcEnv, thisAssemblyName, referenceRange, file) =
-    let resolutions = CommitOperationResult(tcImports.TryResolveAssemblyReference(ctok, AssemblyReference(referenceRange, file, None), ResolveAssemblyReferenceMode.ReportErrors))
-    let dllinfos, ccuinfos = 
-        tcImports.RegisterAndImportReferencedAssemblies(ctok, resolutions) 
+    let resolutions =
+        CommitOperationResult(
+            tcImports.TryResolveAssemblyReference(
+                ctok,
+                AssemblyReference(referenceRange, file, None),
+                ResolveAssemblyReferenceMode.ReportErrors
+            )
+        )
+
+    let dllinfos, ccuinfos =
+        tcImports.RegisterAndImportReferencedAssemblies(ctok, resolutions)
         |> NodeCode.RunImmediateWithoutCancellation
 
     let asms =
-        ccuinfos |> List.map (function
+        ccuinfos
+        |> List.map (function
             | ResolvedImportedAssembly asm -> asm
-            | UnresolvedImportedAssembly assemblyName -> error(Error(FSComp.SR.buildCouldNotResolveAssemblyRequiredByFile(assemblyName, file), referenceRange)))
+            | UnresolvedImportedAssembly assemblyName ->
+                error (Error(FSComp.SR.buildCouldNotResolveAssemblyRequiredByFile (assemblyName, file), referenceRange)))
 
     let g = tcImports.GetTcGlobals()
     let amap = tcImports.GetImportMap()
-    let _openDecls, tcEnv = (tcEnv, asms) ||> List.collectFold (fun tcEnv asm -> AddCcuToTcEnv(g, amap, referenceRange, tcEnv, thisAssemblyName, asm.FSharpViewOfMetadata, asm.AssemblyAutoOpenAttributes, asm.AssemblyInternalsVisibleToAttributes))
+
+    let _openDecls, tcEnv =
+        (tcEnv, asms)
+        ||> List.collectFold (fun tcEnv asm ->
+            AddCcuToTcEnv(
+                g,
+                amap,
+                referenceRange,
+                tcEnv,
+                thisAssemblyName,
+                asm.FSharpViewOfMetadata,
+                asm.AssemblyAutoOpenAttributes,
+                asm.AssemblyInternalsVisibleToAttributes
+            ))
+
     tcEnv, (dllinfos, asms)
diff --git a/src/Compiler/Driver/CompilerOptions.fs b/src/Compiler/Driver/CompilerOptions.fs
index 1d211eee26a..b46754e88be 100644
--- a/src/Compiler/Driver/CompilerOptions.fs
+++ b/src/Compiler/Driver/CompilerOptions.fs
@@ -28,7 +28,7 @@ module Attributes =
 
     //[]
     []
-    do()
+    do ()
 
 //----------------------------------------------------------------------------
 // Compiler option parser
@@ -59,11 +59,20 @@ type OptionSpec =
     | OptionStringList of (string -> unit)
     | OptionStringListSwitch of (string -> OptionSwitch -> unit)
     | OptionUnit of (unit -> unit)
-    | OptionHelp of (CompilerOptionBlock list -> unit)                      // like OptionUnit, but given the "options"
+    | OptionHelp of (CompilerOptionBlock list -> unit) // like OptionUnit, but given the "options"
     | OptionGeneral of (string list -> bool) * (string list -> string list) // Applies? * (ApplyReturningResidualArgs)
 
-and  CompilerOption      = CompilerOption of name: string * argumentDescriptionString: string * actionSpec: OptionSpec * deprecationError: Option * helpText: string option
-and  CompilerOptionBlock = PublicOptions  of heading: string * options: CompilerOption list | PrivateOptions of options: CompilerOption list
+and CompilerOption =
+    | CompilerOption of
+        name: string *
+        argumentDescriptionString: string *
+        actionSpec: OptionSpec *
+        deprecationError: Option *
+        helpText: string option
+
+and CompilerOptionBlock =
+    | PublicOptions of heading: string * options: CompilerOption list
+    | PrivateOptions of options: CompilerOption list
 
 let GetOptionsOfBlock block =
     match block with
@@ -72,13 +81,21 @@ let GetOptionsOfBlock block =
 
 let FilterCompilerOptionBlock pred block =
     match block with
-    | PublicOptions(heading, opts) -> PublicOptions(heading, List.filter pred opts)
-    | PrivateOptions opts        -> PrivateOptions(List.filter pred opts)
+    | PublicOptions (heading, opts) -> PublicOptions(heading, List.filter pred opts)
+    | PrivateOptions opts -> PrivateOptions(List.filter pred opts)
+
+let compilerOptionUsage (CompilerOption (s, tag, spec, _, _)) =
+    let s =
+        if s = "--" then
+            ""
+        else
+            s (* s="flag" for "--flag" options. s="--" for "--" option. Adjust printing here for "--" case. *)
 
-let compilerOptionUsage (CompilerOption(s, tag, spec, _, _)) =
-    let s = if s="--" then "" else s (* s="flag" for "--flag" options. s="--" for "--" option. Adjust printing here for "--" case. *)
     match spec with
-    | OptionUnit _ | OptionSet _ | OptionClear _ | OptionHelp _ -> sprintf "--%s" s
+    | OptionUnit _
+    | OptionSet _
+    | OptionClear _
+    | OptionHelp _ -> sprintf "--%s" s
     | OptionStringList _ -> sprintf "--%s:%s" s tag
     | OptionIntList _ -> sprintf "--%s:%s" s tag
     | OptionSwitch _ -> sprintf "--%s[+|-]" s
@@ -86,285 +103,378 @@ let compilerOptionUsage (CompilerOption(s, tag, spec, _, _)) =
     | OptionIntListSwitch _ -> sprintf "--%s[+|-]:%s" s tag
     | OptionString _ -> sprintf "--%s:%s" s tag
     | OptionInt _ -> sprintf "--%s:%s" s tag
-    | OptionFloat _ ->  sprintf "--%s:%s" s tag
+    | OptionFloat _ -> sprintf "--%s:%s" s tag
     | OptionRest _ -> sprintf "--%s ..." s
-    | OptionGeneral _  -> if tag="" then sprintf "%s" s else sprintf "%s:%s" s tag (* still being decided *)
+    | OptionGeneral _ ->
+        if tag = "" then
+            sprintf "%s" s
+        else
+            sprintf "%s:%s" s tag (* still being decided *)
 
-let PrintCompilerOption (CompilerOption(_s, _tag, _spec, _, help) as compilerOption) =
+let PrintCompilerOption (CompilerOption (_s, _tag, _spec, _, help) as compilerOption) =
     let flagWidth = 42 // fixed width for printing of flags, e.g. --debug:{full|pdbonly|portable|embedded}
     let defaultLineWidth = 80 // the fallback width
+
     let lineWidth =
         try
             Console.BufferWidth
-        with e -> defaultLineWidth
-    let lineWidth = if lineWidth=0 then defaultLineWidth else lineWidth (* Have seen BufferWidth=0 on Linux/Mono *)
+        with e ->
+            defaultLineWidth
+
+    let lineWidth =
+        if lineWidth = 0 then
+            defaultLineWidth
+        else
+            lineWidth (* Have seen BufferWidth=0 on Linux/Mono *)
     // Lines have this form: 
     //   flagWidth chars - for flags description or padding on continuation lines.
     //   single space    - space.
     //   description     - words upto but excluding the final character of the line.
     printf "%-40s" (compilerOptionUsage compilerOption)
-    let printWord column (word:string) =
+
+    let printWord column (word: string) =
         // Have printed upto column.
         // Now print the next word including any preceding whitespace.
         // Returns the column printed to (suited to folding).
         if column + 1 (*space*) + word.Length >= lineWidth then // NOTE: "equality" ensures final character of the line is never printed
-          printfn "" (* newline *)
-          printf  "%-40s %s" ""(*<--flags*) word
-          flagWidth + 1 + word.Length
+            printfn "" (* newline *)
+            printf "%-40s %s" "" (*<--flags*) word
+            flagWidth + 1 + word.Length
         else
-          printf  " %s" word
-          column + 1 + word.Length
-    let words = match help with None -> [| |] | Some s -> s.Split [| ' ' |]
+            printf " %s" word
+            column + 1 + word.Length
+
+    let words =
+        match help with
+        | None -> [||]
+        | Some s -> s.Split [| ' ' |]
+
     let _finalColumn = Array.fold printWord flagWidth words
     printfn "" (* newline *)
 
 let PrintPublicOptions (heading, opts) =
-  if not (isNil opts) then
-    printfn ""
-    printfn ""
-    printfn "\t\t%s" heading
-    List.iter PrintCompilerOption opts
+    if not (isNil opts) then
+        printfn ""
+        printfn ""
+        printfn "\t\t%s" heading
+        List.iter PrintCompilerOption opts
 
 let PrintCompilerOptionBlocks blocks =
-  let equals x y = x=y
-  let publicBlocks = List.choose (function PrivateOptions _ -> None | PublicOptions (heading, opts) -> Some (heading, opts)) blocks
-  let consider doneHeadings (heading, _opts) =
-    if Set.contains heading doneHeadings then
-      doneHeadings
-    else
-      let headingOptions = List.filter (fst >> equals heading) publicBlocks |> List.collect snd
-      PrintPublicOptions (heading, headingOptions)
-      Set.add heading doneHeadings
-  List.fold consider Set.empty publicBlocks |> ignore>
+    let publicBlocks =
+        blocks
+        |> List.choose (function
+            | PrivateOptions _ -> None
+            | PublicOptions (heading, opts) -> Some(heading, opts))
+
+    let consider doneHeadings (heading, _opts) =
+        if Set.contains heading doneHeadings then
+            doneHeadings
+        else
+            let headingOptions =
+                publicBlocks |> List.filter (fun (h2, _) -> heading = h2) |> List.collect snd
+
+            PrintPublicOptions(heading, headingOptions)
+            Set.add heading doneHeadings
+
+    List.fold consider Set.empty publicBlocks |> ignore>
 
 (* For QA *)
-let dumpCompilerOption prefix (CompilerOption(str, _, spec, _, _)) =
+let dumpCompilerOption prefix (CompilerOption (str, _, spec, _, _)) =
     printf "section='%-25s' ! option=%-30s kind=" prefix str
+
     match spec with
-      | OptionUnit             _ -> printf "OptionUnit"
-      | OptionSet              _ -> printf "OptionSet"
-      | OptionClear            _ -> printf "OptionClear"
-      | OptionHelp             _ -> printf "OptionHelp"
-      | OptionStringList       _ -> printf "OptionStringList"
-      | OptionIntList          _ -> printf "OptionIntList"
-      | OptionSwitch           _ -> printf "OptionSwitch"
-      | OptionStringListSwitch _ -> printf "OptionStringListSwitch"
-      | OptionIntListSwitch    _ -> printf "OptionIntListSwitch"
-      | OptionString           _ -> printf "OptionString"
-      | OptionInt              _ -> printf "OptionInt"
-      | OptionFloat            _ -> printf "OptionFloat"
-      | OptionRest             _ -> printf "OptionRest"
-      | OptionGeneral          _ -> printf "OptionGeneral"
+    | OptionUnit _ -> printf "OptionUnit"
+    | OptionSet _ -> printf "OptionSet"
+    | OptionClear _ -> printf "OptionClear"
+    | OptionHelp _ -> printf "OptionHelp"
+    | OptionStringList _ -> printf "OptionStringList"
+    | OptionIntList _ -> printf "OptionIntList"
+    | OptionSwitch _ -> printf "OptionSwitch"
+    | OptionStringListSwitch _ -> printf "OptionStringListSwitch"
+    | OptionIntListSwitch _ -> printf "OptionIntListSwitch"
+    | OptionString _ -> printf "OptionString"
+    | OptionInt _ -> printf "OptionInt"
+    | OptionFloat _ -> printf "OptionFloat"
+    | OptionRest _ -> printf "OptionRest"
+    | OptionGeneral _ -> printf "OptionGeneral"
+
     printf "\n"
-let dumpCompilerOptionBlock = function
-  | PublicOptions (heading, opts) -> List.iter (dumpCompilerOption heading)     opts
-  | PrivateOptions opts          -> List.iter (dumpCompilerOption "NoSection") opts
-let DumpCompilerOptionBlocks blocks = List.iter dumpCompilerOptionBlock blocks
 
-let isSlashOpt (opt:string) =
-    opt[0] = '/' && (opt.Length = 1 || not (opt[1..].Contains "/"))
+let dumpCompilerOptionBlock =
+    function
+    | PublicOptions (heading, opts) -> List.iter (dumpCompilerOption heading) opts
+    | PrivateOptions opts -> List.iter (dumpCompilerOption "NoSection") opts
+
+let DumpCompilerOptionBlocks blocks =
+    List.iter dumpCompilerOptionBlock blocks
+
+let isSlashOpt (opt: string) =
+    opt[0] = '/' && (opt.Length = 1 || not (opt[ 1.. ].Contains "/"))
 
 module ResponseFile =
 
     type ResponseFileData = ResponseFileLine list
+
     and ResponseFileLine =
         | CompilerOptionSpec of string
         | Comment of string
 
-    let parseFile path: Choice =
+    let parseFile path : Choice =
         let parseLine (l: string) =
             match l with
             | s when String.IsNullOrWhiteSpace s -> None
-            | s when l.StartsWithOrdinal("#") -> Some (ResponseFileLine.Comment (s.TrimStart('#')))
-            | s -> Some (ResponseFileLine.CompilerOptionSpec (s.Trim()))
+            | s when l.StartsWithOrdinal("#") -> Some(ResponseFileLine.Comment(s.TrimStart('#')))
+            | s -> Some(ResponseFileLine.CompilerOptionSpec(s.Trim()))
 
         try
             use stream = FileSystem.OpenFileForReadShim(path)
             use reader = new StreamReader(stream, true)
+
             let data =
-                seq { while not reader.EndOfStream do yield reader.ReadLine () }
+                seq {
+                    while not reader.EndOfStream do
+                        reader.ReadLine()
+                }
                 |> Seq.choose parseLine
                 |> List.ofSeq
+
             Choice1Of2 data
         with e ->
             Choice2Of2 e
 
 let ParseCompilerOptions (collectOtherArgument: string -> unit, blocks: CompilerOptionBlock list, args) =
-  use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
+    use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
+
+    let specs = List.collect GetOptionsOfBlock blocks
+
+    // returns a tuple - the option token, the option argument string
+    let parseOption (s: string) =
+        // grab the option token
+        let opts = s.Split([| ':' |])
+        let mutable opt = opts[0]
+
+        if opt = "" then
+            ()
+        // if it doesn't start with a '-' or '/', reject outright
+        elif opt[0] <> '-' && opt[0] <> '/' then
+            opt <- ""
+        elif opt <> "--" then
+            // is it an abbreviated or MSFT-style option?
+            // if so, strip the first character and move on with your life
+            if opt.Length = 2 || isSlashOpt opt then
+                opt <- opt[1..]
+            // else, it should be a non-abbreviated option starting with "--"
+            elif opt.Length > 3 && opt.StartsWithOrdinal("--") then
+                opt <- opt[2..]
+            else
+                opt <- ""
+
+        // get the argument string
+        let optArgs = if opts.Length > 1 then String.Join(":", opts[1..]) else ""
+        opt, optArgs
+
+    let getOptionArg compilerOption (argString: string) =
+        if argString = "" then
+            errorR (Error(FSComp.SR.buildOptionRequiresParameter (compilerOptionUsage compilerOption), rangeCmdArgs))
+
+        argString
+
+    let getOptionArgList compilerOption (argString: string) =
+        if argString = "" then
+            errorR (Error(FSComp.SR.buildOptionRequiresParameter (compilerOptionUsage compilerOption), rangeCmdArgs))
+            []
+        else
+            argString.Split([| ','; ';' |]) |> List.ofArray
+
+    let getSwitchOpt (opt: string) =
+        // if opt is a switch, strip the  '+' or '-'
+        if opt <> "--"
+           && opt.Length > 1
+           && (opt.EndsWithOrdinal("+") || opt.EndsWithOrdinal("-")) then
+            opt[0 .. opt.Length - 2]
+        else
+            opt
 
-  let specs = List.collect GetOptionsOfBlock blocks
+    let getSwitch (s: string) =
+        let s = (s.Split([| ':' |]))[0]
 
-  // returns a tuple - the option token, the option argument string
-  let parseOption (s: string) =
-    // grab the option token
-    let opts = s.Split([|':'|])
-    let mutable opt = opts[0]
-    if opt = "" then
-        ()
-    // if it doesn't start with a '-' or '/', reject outright
-    elif opt[0] <> '-' && opt[0] <> '/' then
-      opt <- ""
-    elif opt <> "--" then
-      // is it an abbreviated or MSFT-style option?
-      // if so, strip the first character and move on with your life
-      if opt.Length = 2 || isSlashOpt opt then
-        opt <- opt[1 ..]
-      // else, it should be a non-abbreviated option starting with "--"
-      elif opt.Length > 3 && opt.StartsWithOrdinal("--") then
-        opt <- opt[2 ..]
-      else
-        opt <- ""
-
-    // get the argument string
-    let optArgs = if opts.Length > 1 then String.Join(":", opts[1 ..]) else ""
-    opt, optArgs
-
-  let getOptionArg compilerOption (argString: string) =
-    if argString = "" then
-      errorR(Error(FSComp.SR.buildOptionRequiresParameter(compilerOptionUsage compilerOption), rangeCmdArgs))
-    argString
-
-  let getOptionArgList compilerOption (argString: string) =
-    if argString = "" then
-      errorR(Error(FSComp.SR.buildOptionRequiresParameter(compilerOptionUsage compilerOption), rangeCmdArgs))
-      []
-    else
-      argString.Split([|',';';'|]) |> List.ofArray
+        if s <> "--" && s.EndsWithOrdinal("-") then
+            OptionSwitch.Off
+        else
+            OptionSwitch.On
+
+    let rec processArg args =
+        match args with
+        | [] -> ()
+        | rsp: string :: t when rsp.StartsWithOrdinal("@") ->
+            let responseFileOptions =
+                let fullpath =
+                    try
+                        Some(rsp.TrimStart('@') |> FileSystem.GetFullPathShim)
+                    with _ ->
+                        None
 
-  let getSwitchOpt (opt: string) =
-    // if opt is a switch, strip the  '+' or '-'
-    if opt <> "--" && opt.Length > 1 && (opt.EndsWithOrdinal("+") || opt.EndsWithOrdinal("-")) then
-      opt[0 .. opt.Length - 2]
-    else
-      opt
-
-  let getSwitch (s: string) =
-    let s = (s.Split([|':'|]))[0]
-    if s <> "--" && s.EndsWithOrdinal("-") then OptionSwitch.Off else OptionSwitch.On
-
-  let rec processArg args =
-    match args with
-    | [] -> ()
-    | rsp: string :: t when rsp.StartsWithOrdinal("@") ->
-        let responseFileOptions =
-            let fullpath =
-                try
-                    Some (rsp.TrimStart('@') |> FileSystem.GetFullPathShim)
-                with _ ->
-                    None
-
-            match fullpath with
-            | None ->
-                errorR(Error(FSComp.SR.optsResponseFileNameInvalid rsp, rangeCmdArgs))
-                []
-            | Some path when not (FileSystem.FileExistsShim path) ->
-                errorR(Error(FSComp.SR.optsResponseFileNotFound(rsp, path), rangeCmdArgs))
-                []
-            | Some path ->
-                match ResponseFile.parseFile path with
-                | Choice2Of2 _ ->
-                    errorR(Error(FSComp.SR.optsInvalidResponseFile(rsp, path), rangeCmdArgs))
+                match fullpath with
+                | None ->
+                    errorR (Error(FSComp.SR.optsResponseFileNameInvalid rsp, rangeCmdArgs))
+                    []
+                | Some path when not (FileSystem.FileExistsShim path) ->
+                    errorR (Error(FSComp.SR.optsResponseFileNotFound (rsp, path), rangeCmdArgs))
+                    []
+                | Some path ->
+                    match ResponseFile.parseFile path with
+                    | Choice2Of2 _ ->
+                        errorR (Error(FSComp.SR.optsInvalidResponseFile (rsp, path), rangeCmdArgs))
+                        []
+                    | Choice1Of2 rspData ->
+                        let onlyOptions l =
+                            match l with
+                            | ResponseFile.ResponseFileLine.Comment _ -> None
+                            | ResponseFile.ResponseFileLine.CompilerOptionSpec opt -> Some opt
+
+                        rspData |> List.choose onlyOptions
+
+            processArg (responseFileOptions @ t)
+        | opt :: t ->
+            let optToken, argString = parseOption opt
+
+            let reportDeprecatedOption errOpt =
+                match errOpt with
+                | Some e -> warning e
+                | None -> ()
+
+            let rec attempt l =
+                match l with
+                | CompilerOption (s, _, OptionHelp f, d, _) :: _ when optToken = s && argString = "" ->
+                    reportDeprecatedOption d
+                    f blocks
+                    t
+                | CompilerOption (s, _, OptionUnit f, d, _) :: _ when optToken = s && argString = "" ->
+                    reportDeprecatedOption d
+                    f ()
+                    t
+                | CompilerOption (s, _, OptionSwitch f, d, _) :: _ when getSwitchOpt optToken = s && argString = "" ->
+                    reportDeprecatedOption d
+                    f (getSwitch opt)
+                    t
+                | CompilerOption (s, _, OptionSet f, d, _) :: _ when optToken = s && argString = "" ->
+                    reportDeprecatedOption d
+                    f.Value <- true
+                    t
+                | CompilerOption (s, _, OptionClear f, d, _) :: _ when optToken = s && argString = "" ->
+                    reportDeprecatedOption d
+                    f.Value <- false
+                    t
+                | CompilerOption (s, _, OptionString f, d, _) as compilerOption :: _ when optToken = s ->
+                    reportDeprecatedOption d
+                    let oa = getOptionArg compilerOption argString
+                    if oa <> "" then f (getOptionArg compilerOption oa)
+                    t
+                | CompilerOption (s, _, OptionInt f, d, _) as compilerOption :: _ when optToken = s ->
+                    reportDeprecatedOption d
+                    let oa = getOptionArg compilerOption argString
+
+                    if oa <> "" then
+                        f (
+                            try
+                                int32 oa
+                            with _ ->
+                                errorR (Error(FSComp.SR.buildArgInvalidInt (getOptionArg compilerOption argString), rangeCmdArgs))
+                                0
+                        )
+
+                    t
+                | CompilerOption (s, _, OptionFloat f, d, _) as compilerOption :: _ when optToken = s ->
+                    reportDeprecatedOption d
+                    let oa = getOptionArg compilerOption argString
+
+                    if oa <> "" then
+                        f (
+                            try
+                                float oa
+                            with _ ->
+                                errorR (Error(FSComp.SR.buildArgInvalidFloat (getOptionArg compilerOption argString), rangeCmdArgs))
+                                0.0
+                        )
+
+                    t
+                | CompilerOption (s, _, OptionRest f, d, _) :: _ when optToken = s ->
+                    reportDeprecatedOption d
+                    List.iter f t
                     []
-                | Choice1Of2 rspData ->
-                    let onlyOptions l =
-                        match l with
-                        | ResponseFile.ResponseFileLine.Comment _ -> None
-                        | ResponseFile.ResponseFileLine.CompilerOptionSpec opt -> Some opt
-                    rspData |> List.choose onlyOptions
-
-        processArg (responseFileOptions @ t)
-    | opt :: t ->
-        let optToken, argString = parseOption opt
-
-        let reportDeprecatedOption errOpt =
-          match errOpt with
-          | Some e -> warning e
-          | None -> ()
-
-        let rec attempt l =
-          match l with
-          | CompilerOption(s, _, OptionHelp f, d, _) :: _ when optToken = s  && argString = "" ->
-              reportDeprecatedOption d
-              f blocks; t
-          | CompilerOption(s, _, OptionUnit f, d, _) :: _ when optToken = s  && argString = "" ->
-              reportDeprecatedOption d
-              f (); t
-          | CompilerOption(s, _, OptionSwitch f, d, _) :: _ when getSwitchOpt optToken = s && argString = "" ->
-              reportDeprecatedOption d
-              f (getSwitch opt); t
-          | CompilerOption(s, _, OptionSet f, d, _) :: _ when optToken = s && argString = "" ->
-              reportDeprecatedOption d
-              f.Value <- true; t
-          | CompilerOption(s, _, OptionClear f, d, _) :: _ when optToken = s && argString = "" ->
-              reportDeprecatedOption d
-              f.Value <- false; t
-          | CompilerOption(s, _, OptionString f, d, _) as compilerOption :: _ when optToken = s ->
-              reportDeprecatedOption d
-              let oa = getOptionArg compilerOption argString
-              if oa <> "" then
-                  f (getOptionArg compilerOption oa)
-              t
-          | CompilerOption(s, _, OptionInt f, d, _) as compilerOption :: _ when optToken = s ->
-              reportDeprecatedOption d
-              let oa = getOptionArg compilerOption argString
-              if oa <> "" then
-                  f (try int32 oa with _ ->
-                      errorR(Error(FSComp.SR.buildArgInvalidInt(getOptionArg compilerOption argString), rangeCmdArgs)); 0)
-              t
-          | CompilerOption(s, _, OptionFloat f, d, _) as compilerOption :: _ when optToken = s ->
-              reportDeprecatedOption d
-              let oa = getOptionArg compilerOption argString
-              if oa <> "" then
-                  f (try float oa with _ ->
-                      errorR(Error(FSComp.SR.buildArgInvalidFloat(getOptionArg compilerOption argString), rangeCmdArgs)); 0.0)
-              t
-          | CompilerOption(s, _, OptionRest f, d, _) :: _ when optToken = s ->
-              reportDeprecatedOption d
-              List.iter f t; []
-          | CompilerOption(s, _, OptionIntList f, d, _) as compilerOption :: _ when optToken = s ->
-              reportDeprecatedOption d
-              let al = getOptionArgList compilerOption argString
-              if al <> [] then
-                  List.iter (fun i -> f (try int32 i with _ -> errorR(Error(FSComp.SR.buildArgInvalidInt i, rangeCmdArgs)); 0)) al
-              t
-          | CompilerOption(s, _, OptionIntListSwitch f, d, _) as compilerOption :: _ when getSwitchOpt optToken = s ->
-              reportDeprecatedOption d
-              let al = getOptionArgList compilerOption argString
-              if al <> [] then
-                  let switch = getSwitch opt
-                  List.iter (fun i -> f (try int32 i with _ -> errorR(Error(FSComp.SR.buildArgInvalidInt i, rangeCmdArgs)); 0) switch) al
-              t
-              // here
-          | CompilerOption(s, _, OptionStringList f, d, _) as compilerOption :: _ when optToken = s ->
-              reportDeprecatedOption d
-              let al = getOptionArgList compilerOption argString
-              if al <> [] then
-                  List.iter f (getOptionArgList compilerOption argString)
-              t
-          | CompilerOption(s, _, OptionStringListSwitch f, d, _) as compilerOption :: _ when getSwitchOpt optToken = s ->
-              reportDeprecatedOption d
-              let al = getOptionArgList compilerOption argString
-              if al <> [] then
-                  let switch = getSwitch opt
-                  List.iter (fun s -> f s switch) (getOptionArgList compilerOption argString)
-              t
-          | CompilerOption(_, _, OptionGeneral (pred, exec), d, _) :: _ when pred args ->
-              reportDeprecatedOption d
-              let rest = exec args in rest // arguments taken, rest remaining
-          | _ :: more -> attempt more
-          | [] ->
-              if opt.Length = 0 || opt[0] = '-' || isSlashOpt opt
-               then
-                  // want the whole opt token - delimiter and all
-                  let unrecOpt = opt.Split([|':'|]).[0]
-                  errorR(Error(FSComp.SR.buildUnrecognizedOption unrecOpt, rangeCmdArgs))
-                  t
-              else
-                 (collectOtherArgument opt; t)
-        let rest = attempt specs
-        processArg rest
-
-  processArg args
+                | CompilerOption (s, _, OptionIntList f, d, _) as compilerOption :: _ when optToken = s ->
+                    reportDeprecatedOption d
+                    let al = getOptionArgList compilerOption argString
+
+                    if al <> [] then
+                        List.iter
+                            (fun i ->
+                                f (
+                                    try
+                                        int32 i
+                                    with _ ->
+                                        errorR (Error(FSComp.SR.buildArgInvalidInt i, rangeCmdArgs))
+                                        0
+                                ))
+                            al
+
+                    t
+                | CompilerOption (s, _, OptionIntListSwitch f, d, _) as compilerOption :: _ when getSwitchOpt optToken = s ->
+                    reportDeprecatedOption d
+                    let al = getOptionArgList compilerOption argString
+
+                    if al <> [] then
+                        let switch = getSwitch opt
+
+                        List.iter
+                            (fun i ->
+                                f
+                                    (try
+                                        int32 i
+                                     with _ ->
+                                         errorR (Error(FSComp.SR.buildArgInvalidInt i, rangeCmdArgs))
+                                         0)
+                                    switch)
+                            al
+
+                    t
+                // here
+                | CompilerOption (s, _, OptionStringList f, d, _) as compilerOption :: _ when optToken = s ->
+                    reportDeprecatedOption d
+                    let al = getOptionArgList compilerOption argString
+
+                    if al <> [] then
+                        List.iter f (getOptionArgList compilerOption argString)
+
+                    t
+                | CompilerOption (s, _, OptionStringListSwitch f, d, _) as compilerOption :: _ when getSwitchOpt optToken = s ->
+                    reportDeprecatedOption d
+                    let al = getOptionArgList compilerOption argString
+
+                    if al <> [] then
+                        let switch = getSwitch opt
+                        List.iter (fun s -> f s switch) (getOptionArgList compilerOption argString)
+
+                    t
+                | CompilerOption (_, _, OptionGeneral (pred, exec), d, _) :: _ when pred args ->
+                    reportDeprecatedOption d
+                    let rest = exec args in
+                    rest // arguments taken, rest remaining
+                | _ :: more -> attempt more
+                | [] ->
+                    if opt.Length = 0 || opt[0] = '-' || isSlashOpt opt then
+                        // want the whole opt token - delimiter and all
+                        let unrecOpt = opt.Split([| ':' |]).[0]
+                        errorR (Error(FSComp.SR.buildUnrecognizedOption unrecOpt, rangeCmdArgs))
+                        t
+                    else
+                        (collectOtherArgument opt
+                         t)
+
+            let rest = attempt specs
+            processArg rest
+
+    processArg args
 
 //----------------------------------------------------------------------------
 // Compiler options
@@ -379,26 +489,61 @@ let setFlag r n =
     | 1 -> r true
     | _ -> raise (Failure "expected 0/1")
 
-let SetOptimizeOff(tcConfigB: TcConfigBuilder) =
-    tcConfigB.optSettings <- { tcConfigB.optSettings with jitOptUser = Some false }
-    tcConfigB.optSettings <- { tcConfigB.optSettings with localOptUser = Some false }
-    tcConfigB.optSettings <- { tcConfigB.optSettings with crossAssemblyOptimizationUser = Some false }
-    tcConfigB.optSettings <- { tcConfigB.optSettings with lambdaInlineThreshold = 0 }
+let SetOptimizeOff (tcConfigB: TcConfigBuilder) =
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            jitOptUser = Some false
+        }
+
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            localOptUser = Some false
+        }
+
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            crossAssemblyOptimizationUser = Some false
+        }
+
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            lambdaInlineThreshold = 0
+        }
+
     tcConfigB.doDetuple <- false
     tcConfigB.doTLR <- false
     tcConfigB.doFinalSimplify <- false
 
-let SetOptimizeOn(tcConfigB: TcConfigBuilder) =
-    tcConfigB.optSettings <- { tcConfigB.optSettings with jitOptUser = Some true }
-    tcConfigB.optSettings <- { tcConfigB.optSettings with localOptUser = Some true }
-    tcConfigB.optSettings <- { tcConfigB.optSettings with crossAssemblyOptimizationUser = Some true }
-    tcConfigB.optSettings <- { tcConfigB.optSettings with lambdaInlineThreshold = 6 }
+let SetOptimizeOn (tcConfigB: TcConfigBuilder) =
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            jitOptUser = Some true
+        }
+
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            localOptUser = Some true
+        }
+
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            crossAssemblyOptimizationUser = Some true
+        }
+
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            lambdaInlineThreshold = 6
+        }
+
     tcConfigB.doDetuple <- true
     tcConfigB.doTLR <- true
     tcConfigB.doFinalSimplify <- true
 
 let SetOptimizeSwitch (tcConfigB: TcConfigBuilder) switch =
-    if (switch = OptionSwitch.On) then SetOptimizeOn tcConfigB else SetOptimizeOff tcConfigB
+    if (switch = OptionSwitch.On) then
+        SetOptimizeOn tcConfigB
+    else
+        SetOptimizeOff tcConfigB
 
 let SetTailcallSwitch (tcConfigB: TcConfigBuilder) switch =
     tcConfigB.emitTailcalls <- (switch = OptionSwitch.On)
@@ -409,38 +554,50 @@ let SetDeterministicSwitch (tcConfigB: TcConfigBuilder) switch =
 let SetReferenceAssemblyOnlySwitch (tcConfigB: TcConfigBuilder) switch =
     match tcConfigB.emitMetadataAssembly with
     | MetadataAssemblyGeneration.None ->
-        tcConfigB.emitMetadataAssembly <- if (switch = OptionSwitch.On) then MetadataAssemblyGeneration.ReferenceOnly else MetadataAssemblyGeneration.None
-    | _ ->
-        error(Error(FSComp.SR.optsInvalidRefAssembly(), rangeCmdArgs))
+        tcConfigB.emitMetadataAssembly <-
+            if (switch = OptionSwitch.On) then
+                MetadataAssemblyGeneration.ReferenceOnly
+            else
+                MetadataAssemblyGeneration.None
+    | _ -> error (Error(FSComp.SR.optsInvalidRefAssembly (), rangeCmdArgs))
 
 let SetReferenceAssemblyOutSwitch (tcConfigB: TcConfigBuilder) outputPath =
     match tcConfigB.emitMetadataAssembly with
-    | MetadataAssemblyGeneration.None ->      
+    | MetadataAssemblyGeneration.None ->
         if FileSystem.IsInvalidPathShim outputPath then
-            error(Error(FSComp.SR.optsInvalidRefOut(), rangeCmdArgs))
+            error (Error(FSComp.SR.optsInvalidRefOut (), rangeCmdArgs))
         else
             tcConfigB.emitMetadataAssembly <- MetadataAssemblyGeneration.ReferenceOut outputPath
-    | _ ->
-        error(Error(FSComp.SR.optsInvalidRefAssembly(), rangeCmdArgs))
+    | _ -> error (Error(FSComp.SR.optsInvalidRefAssembly (), rangeCmdArgs))
 
 let AddPathMapping (tcConfigB: TcConfigBuilder) (pathPair: string) =
-    match pathPair.Split([|'='|], 2) with
-    | [| oldPrefix; newPrefix |] ->
-        tcConfigB.AddPathMapping (oldPrefix, newPrefix)
-    | _ ->
-        error(Error(FSComp.SR.optsInvalidPathMapFormat(), rangeCmdArgs))
+    match pathPair.Split([| '=' |], 2) with
+    | [| oldPrefix; newPrefix |] -> tcConfigB.AddPathMapping(oldPrefix, newPrefix)
+    | _ -> error (Error(FSComp.SR.optsInvalidPathMapFormat (), rangeCmdArgs))
 
 let jitoptimizeSwitch (tcConfigB: TcConfigBuilder) switch =
-    tcConfigB.optSettings <- { tcConfigB.optSettings with jitOptUser = Some (switch = OptionSwitch.On) }
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            jitOptUser = Some(switch = OptionSwitch.On)
+        }
 
 let localoptimizeSwitch (tcConfigB: TcConfigBuilder) switch =
-    tcConfigB.optSettings <- { tcConfigB.optSettings with localOptUser = Some (switch = OptionSwitch.On) }
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            localOptUser = Some(switch = OptionSwitch.On)
+        }
 
 let crossOptimizeSwitch (tcConfigB: TcConfigBuilder) switch =
-    tcConfigB.optSettings <- { tcConfigB.optSettings with crossAssemblyOptimizationUser = Some (switch = OptionSwitch.On) }
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            crossAssemblyOptimizationUser = Some(switch = OptionSwitch.On)
+        }
 
 let splittingSwitch (tcConfigB: TcConfigBuilder) switch =
-    tcConfigB.optSettings <- { tcConfigB.optSettings with abstractBigTargets = switch = OptionSwitch.On }
+    tcConfigB.optSettings <-
+        { tcConfigB.optSettings with
+            abstractBigTargets = switch = OptionSwitch.On
+        }
 
 let callVirtSwitch (tcConfigB: TcConfigBuilder) switch =
     tcConfigB.alwaysCallVirt <- switch = OptionSwitch.On
@@ -449,73 +606,79 @@ let useHighEntropyVASwitch (tcConfigB: TcConfigBuilder) switch =
     tcConfigB.useHighEntropyVA <- switch = OptionSwitch.On
 
 let subSystemVersionSwitch (tcConfigB: TcConfigBuilder) (text: string) =
-    let fail() = error(Error(FSComp.SR.optsInvalidSubSystemVersion text, rangeCmdArgs))
+    let fail () =
+        error (Error(FSComp.SR.optsInvalidSubSystemVersion text, rangeCmdArgs))
 
     // per spec for 357994: Validate input string, should be two positive integers x.y when x>=4 and y>=0 and both <= 65535
     if String.IsNullOrEmpty text then
-       fail()
+        fail ()
     else
         match text.Split('.') with
-        | [| majorStr; minorStr|] ->
+        | [| majorStr; minorStr |] ->
             match (Int32.TryParse majorStr), (Int32.TryParse minorStr) with
-            | (true, major), (true, minor)
-                 when major >= 4 && major <= 65535
-                      && minor >=0 && minor <= 65535 ->
-                 tcConfigB.subsystemVersion <- (major, minor)
-            | _ -> fail()
-        | _ -> fail()
+            | (true, major), (true, minor) when major >= 4 && major <= 65535 && minor >= 0 && minor <= 65535 ->
+                tcConfigB.subsystemVersion <- (major, minor)
+            | _ -> fail ()
+        | _ -> fail ()
 
 let SetUseSdkSwitch (tcConfigB: TcConfigBuilder) switch =
     let useSdkRefs = (switch = OptionSwitch.On)
     tcConfigB.SetUseSdkRefs useSdkRefs
 
-let (++) x s = x @ [s]
+let (++) x s = x @ [ s ]
 
-let SetTarget (tcConfigB: TcConfigBuilder)(s: string) =
+let SetTarget (tcConfigB: TcConfigBuilder) (s: string) =
     match s.ToLowerInvariant() with
-    | "exe"     ->  tcConfigB.target <- CompilerTarget.ConsoleExe
-    | "winexe"  ->  tcConfigB.target <- CompilerTarget.WinExe
-    | "library" ->  tcConfigB.target <- CompilerTarget.Dll
-    | "module"  ->  tcConfigB.target <- CompilerTarget.Module
-    | _         ->  error(Error(FSComp.SR.optsUnrecognizedTarget s, rangeCmdArgs))
+    | "exe" -> tcConfigB.target <- CompilerTarget.ConsoleExe
+    | "winexe" -> tcConfigB.target <- CompilerTarget.WinExe
+    | "library" -> tcConfigB.target <- CompilerTarget.Dll
+    | "module" -> tcConfigB.target <- CompilerTarget.Module
+    | _ -> error (Error(FSComp.SR.optsUnrecognizedTarget s, rangeCmdArgs))
 
 let SetDebugSwitch (tcConfigB: TcConfigBuilder) (dtype: string option) (s: OptionSwitch) =
     match dtype with
     | Some s ->
-       match s with
-       | "portable" ->
-           tcConfigB.portablePDB <- true
-           tcConfigB.embeddedPDB <- false
-           tcConfigB.jitTracking <- true
-           tcConfigB.ignoreSymbolStoreSequencePoints <- true
-       | "pdbonly" ->
-           tcConfigB.portablePDB <- false
-           tcConfigB.embeddedPDB <- false
-           tcConfigB.jitTracking <- false
-       | "embedded" ->
-           tcConfigB.portablePDB <- true
-           tcConfigB.embeddedPDB <- true
-           tcConfigB.jitTracking <- true
-           tcConfigB.ignoreSymbolStoreSequencePoints <- true
+        match s with
+        | "portable" ->
+            tcConfigB.portablePDB <- true
+            tcConfigB.embeddedPDB <- false
+            tcConfigB.jitTracking <- true
+            tcConfigB.ignoreSymbolStoreSequencePoints <- true
+        | "pdbonly" ->
+            tcConfigB.portablePDB <- false
+            tcConfigB.embeddedPDB <- false
+            tcConfigB.jitTracking <- false
+        | "embedded" ->
+            tcConfigB.portablePDB <- true
+            tcConfigB.embeddedPDB <- true
+            tcConfigB.jitTracking <- true
+            tcConfigB.ignoreSymbolStoreSequencePoints <- true
 #if FX_NO_PDB_WRITER
-       // When building on the coreclr, full means portable
-       | "full" ->
-           tcConfigB.portablePDB <- true
-           tcConfigB.embeddedPDB <- false
-           tcConfigB.jitTracking <- true
+        // When building on the coreclr, full means portable
+        | "full" ->
+            tcConfigB.portablePDB <- true
+            tcConfigB.embeddedPDB <- false
+            tcConfigB.jitTracking <- true
 #else
-       | "full" ->
-           tcConfigB.portablePDB <- false
-           tcConfigB.embeddedPDB <- false
-           tcConfigB.jitTracking <- true
+        | "full" ->
+            tcConfigB.portablePDB <- false
+            tcConfigB.embeddedPDB <- false
+            tcConfigB.jitTracking <- true
 #endif
 
-       | _ -> error(Error(FSComp.SR.optsUnrecognizedDebugType s, rangeCmdArgs))
-    | None ->           tcConfigB.portablePDB <- false; tcConfigB.embeddedPDB <- false; tcConfigB.jitTracking <- s = OptionSwitch.On
+        | _ -> error (Error(FSComp.SR.optsUnrecognizedDebugType s, rangeCmdArgs))
+    | None ->
+        tcConfigB.portablePDB <- false
+        tcConfigB.embeddedPDB <- false
+        tcConfigB.jitTracking <- s = OptionSwitch.On
+
     tcConfigB.debuginfo <- s = OptionSwitch.On
 
 let SetEmbedAllSourceSwitch (tcConfigB: TcConfigBuilder) switch =
-    if (switch = OptionSwitch.On) then tcConfigB.embedAllSource <- true else tcConfigB.embedAllSource <- false
+    if (switch = OptionSwitch.On) then
+        tcConfigB.embedAllSource <- true
+    else
+        tcConfigB.embedAllSource <- false
 
 let setOutFileName tcConfigB path =
     let outputDir = Path.GetDirectoryName(path)
@@ -554,7 +717,7 @@ let tagLangVersionValues = "{?|version|latest|preview}"
 //----------------
 
 /// Print internal "option state" information for diagnostics and regression tests.
-let PrintOptionInfo (tcConfigB:TcConfigBuilder) =
+let PrintOptionInfo (tcConfigB: TcConfigBuilder) =
     printfn "  jitOptUser . . . . . . : %+A" tcConfigB.optSettings.jitOptUser
     printfn "  localOptUser . . . . . : %+A" tcConfigB.optSettings.localOptUser
     printfn "  crossAssemblyOptimizationUser . . : %+A" tcConfigB.optSettings.crossAssemblyOptimizationUser
@@ -573,28 +736,57 @@ let PrintOptionInfo (tcConfigB:TcConfigBuilder) =
     printfn "  resolutionEnvironment  : %+A" tcConfigB.resolutionEnvironment
     printfn "  product  . . . . . . . : %+A" tcConfigB.productNameForBannerText
     printfn "  copyFSharpCore . . . . : %+A" tcConfigB.copyFSharpCore
-    tcConfigB.includes |> List.sort
-                       |> List.iter (printfn "  include  . . . . . . . : %A")
+
+    tcConfigB.includes
+    |> List.sort
+    |> List.iter (printfn "  include  . . . . . . . : %A")
 
 // OptionBlock: Input files
 //-------------------------
 
-let inputFileFlagsBoth (tcConfigB : TcConfigBuilder) = [
-    CompilerOption("reference", tagFile, OptionString (fun s -> tcConfigB.AddReferencedAssemblyByPath (rangeStartup, s)), None, Some (FSComp.SR.optsReference()))
-    CompilerOption("compilertool", tagFile, OptionString (fun s -> tcConfigB.AddCompilerToolsByPath s), None, Some (FSComp.SR.optsCompilerTool()))
+let inputFileFlagsBoth (tcConfigB: TcConfigBuilder) =
+    [
+        CompilerOption(
+            "reference",
+            tagFile,
+            OptionString(fun s -> tcConfigB.AddReferencedAssemblyByPath(rangeStartup, s)),
+            None,
+            Some(FSComp.SR.optsReference ())
+        )
+        CompilerOption(
+            "compilertool",
+            tagFile,
+            OptionString(fun s -> tcConfigB.AddCompilerToolsByPath s),
+            None,
+            Some(FSComp.SR.optsCompilerTool ())
+        )
     ]
 
-let referenceFlagAbbrev (tcConfigB : TcConfigBuilder) =
-    CompilerOption("r", tagFile, OptionString (fun s -> tcConfigB.AddReferencedAssemblyByPath (rangeStartup, s)), None, Some(FSComp.SR.optsShortFormOf("--reference")))
+let referenceFlagAbbrev (tcConfigB: TcConfigBuilder) =
+    CompilerOption(
+        "r",
+        tagFile,
+        OptionString(fun s -> tcConfigB.AddReferencedAssemblyByPath(rangeStartup, s)),
+        None,
+        Some(FSComp.SR.optsShortFormOf ("--reference"))
+    )
 
-let compilerToolFlagAbbrev (tcConfigB : TcConfigBuilder) =
-    CompilerOption("t", tagFile, OptionString (fun s -> tcConfigB.AddCompilerToolsByPath s), None, Some(FSComp.SR.optsShortFormOf("--compilertool")))
+let compilerToolFlagAbbrev (tcConfigB: TcConfigBuilder) =
+    CompilerOption(
+        "t",
+        tagFile,
+        OptionString(fun s -> tcConfigB.AddCompilerToolsByPath s),
+        None,
+        Some(FSComp.SR.optsShortFormOf ("--compilertool"))
+    )
 
 let inputFileFlagsFsc tcConfigB = inputFileFlagsBoth tcConfigB
 
 let inputFileFlagsFsiBase (_tcConfigB: TcConfigBuilder) =
 #if NETSTANDARD
-    [ CompilerOption("usesdkrefs", tagNone, OptionSwitch (SetUseSdkSwitch _tcConfigB), None, Some (FSComp.SR.useSdkRefs())) ]
+    [
+        CompilerOption("usesdkrefs", tagNone, OptionSwitch(SetUseSdkSwitch _tcConfigB), None, Some(FSComp.SR.useSdkRefs ()))
+    ]
 #else
     List.empty
 #endif
@@ -606,49 +798,92 @@ let inputFileFlagsFsi (tcConfigB: TcConfigBuilder) =
 //---------------------------------
 
 let errorsAndWarningsFlags (tcConfigB: TcConfigBuilder) =
-    let trimFS (s:string) = if s.StartsWithOrdinal "FS" then s.Substring 2 else s
-    let trimFStoInt (s:string) =
-        match Int32.TryParse (trimFS s) with
-        | true, n ->  Some n
+    let trimFS (s: string) =
+        if s.StartsWithOrdinal "FS" then s.Substring 2 else s
+
+    let trimFStoInt (s: string) =
+        match Int32.TryParse(trimFS s) with
+        | true, n -> Some n
         | false, _ -> None
+
     [
-        CompilerOption("warnaserror", tagNone, OptionSwitch(fun switch ->
-            tcConfigB.diagnosticsOptions <-
-                { tcConfigB.diagnosticsOptions with
-                    GlobalWarnAsError = switch <> OptionSwitch.Off }), None, Some (FSComp.SR.optsWarnaserrorPM()))
-
-        CompilerOption("warnaserror", tagWarnList, OptionStringListSwitch (fun n switch ->
-            match trimFStoInt n with
-            | Some n ->
-                let options = tcConfigB.diagnosticsOptions
+        CompilerOption(
+            "warnaserror",
+            tagNone,
+            OptionSwitch(fun switch ->
                 tcConfigB.diagnosticsOptions <-
-                    if switch = OptionSwitch.Off then
-                        { options with
-                            WarnAsError = ListSet.remove (=) n options.WarnAsError
-                            WarnAsWarn = ListSet.insert (=) n options.WarnAsWarn }
-                    else
-                        { options with
-                            WarnAsError = ListSet.insert (=) n options.WarnAsError
-                            WarnAsWarn = ListSet.remove (=) n options.WarnAsWarn }
-            | None -> ()), None, Some (FSComp.SR.optsWarnaserror()))
-
-        CompilerOption("warn", tagInt, OptionInt (fun n ->
-                 tcConfigB.diagnosticsOptions <-
-                     { tcConfigB.diagnosticsOptions with
-                         WarnLevel = if (n >= 0 && n <= 5) then n else error(Error (FSComp.SR.optsInvalidWarningLevel n, rangeCmdArgs)) }
-            ), None, Some (FSComp.SR.optsWarn()))
-
-        CompilerOption("nowarn", tagWarnList, OptionStringList (fun n ->
-            tcConfigB.TurnWarningOff(rangeCmdArgs, trimFS n)), None, Some (FSComp.SR.optsNowarn()))
-
-        CompilerOption("warnon", tagWarnList, OptionStringList (fun n ->
-            tcConfigB.TurnWarningOn(rangeCmdArgs, trimFS n)), None, Some (FSComp.SR.optsWarnOn()))
-
-        CompilerOption("consolecolors", tagNone, OptionSwitch (fun switch ->
-            enableConsoleColoring <- switch = OptionSwitch.On), None, Some (FSComp.SR.optsConsoleColors()))
+                    { tcConfigB.diagnosticsOptions with
+                        GlobalWarnAsError = switch <> OptionSwitch.Off
+                    }),
+            None,
+            Some(FSComp.SR.optsWarnaserrorPM ())
+        )
+
+        CompilerOption(
+            "warnaserror",
+            tagWarnList,
+            OptionStringListSwitch(fun n switch ->
+                match trimFStoInt n with
+                | Some n ->
+                    let options = tcConfigB.diagnosticsOptions
+
+                    tcConfigB.diagnosticsOptions <-
+                        if switch = OptionSwitch.Off then
+                            { options with
+                                WarnAsError = ListSet.remove (=) n options.WarnAsError
+                                WarnAsWarn = ListSet.insert (=) n options.WarnAsWarn
+                            }
+                        else
+                            { options with
+                                WarnAsError = ListSet.insert (=) n options.WarnAsError
+                                WarnAsWarn = ListSet.remove (=) n options.WarnAsWarn
+                            }
+                | None -> ()),
+            None,
+            Some(FSComp.SR.optsWarnaserror ())
+        )
+
+        CompilerOption(
+            "warn",
+            tagInt,
+            OptionInt(fun n ->
+                tcConfigB.diagnosticsOptions <-
+                    { tcConfigB.diagnosticsOptions with
+                        WarnLevel =
+                            if (n >= 0 && n <= 5) then
+                                n
+                            else
+                                error (Error(FSComp.SR.optsInvalidWarningLevel n, rangeCmdArgs))
+                    }),
+            None,
+            Some(FSComp.SR.optsWarn ())
+        )
+
+        CompilerOption(
+            "nowarn",
+            tagWarnList,
+            OptionStringList(fun n -> tcConfigB.TurnWarningOff(rangeCmdArgs, trimFS n)),
+            None,
+            Some(FSComp.SR.optsNowarn ())
+        )
+
+        CompilerOption(
+            "warnon",
+            tagWarnList,
+            OptionStringList(fun n -> tcConfigB.TurnWarningOn(rangeCmdArgs, trimFS n)),
+            None,
+            Some(FSComp.SR.optsWarnOn ())
+        )
+
+        CompilerOption(
+            "consolecolors",
+            tagNone,
+            OptionSwitch(fun switch -> enableConsoleColoring <- switch = OptionSwitch.On),
+            None,
+            Some(FSComp.SR.optsConsoleColors ())
+        )
     ]
 
-
 // OptionBlock: Output files
 //--------------------------
 
@@ -656,54 +891,40 @@ let outputFileFlagsFsi (_tcConfigB: TcConfigBuilder) = []
 
 let outputFileFlagsFsc (tcConfigB: TcConfigBuilder) =
     [
-        CompilerOption
-           ("out", tagFile,
-            OptionString (setOutFileName tcConfigB), None,
-            Some (FSComp.SR.optsNameOfOutputFile()) )
-
-        CompilerOption
-           ("target", tagExe,
-            OptionString (SetTarget tcConfigB), None,
-            Some (FSComp.SR.optsBuildConsole()))
-
-        CompilerOption
-           ("target", tagWinExe,
-            OptionString (SetTarget tcConfigB), None,
-            Some (FSComp.SR.optsBuildWindows()))
-
-        CompilerOption
-           ("target", tagLibrary,
-            OptionString (SetTarget tcConfigB), None,
-            Some (FSComp.SR.optsBuildLibrary()))
-
-        CompilerOption
-           ("target", tagModule,
-            OptionString (SetTarget tcConfigB), None,
-            Some (FSComp.SR.optsBuildModule()))
-
-        CompilerOption
-           ("delaysign", tagNone,
-            OptionSwitch (fun s -> tcConfigB.delaysign <- (s = OptionSwitch.On)), None,
-            Some (FSComp.SR.optsDelaySign()))
-
-        CompilerOption
-           ("publicsign", tagNone,
-            OptionSwitch (fun s -> tcConfigB.publicsign <- (s = OptionSwitch.On)), None,
-            Some (FSComp.SR.optsPublicSign()))
-
-        CompilerOption
-           ("doc", tagFile,
-            OptionString (fun s -> tcConfigB.xmlDocOutputFile <- Some s), None,
-            Some (FSComp.SR.optsWriteXml()))
-
-        CompilerOption
-           ("keyfile", tagFile,
-            OptionString (fun s -> tcConfigB.signer <- Some s), None,
-            Some (FSComp.SR.optsStrongKeyFile()))
-
-        CompilerOption
-           ("platform", tagString,
-            OptionString (fun s ->
+        CompilerOption("out", tagFile, OptionString(setOutFileName tcConfigB), None, Some(FSComp.SR.optsNameOfOutputFile ()))
+
+        CompilerOption("target", tagExe, OptionString(SetTarget tcConfigB), None, Some(FSComp.SR.optsBuildConsole ()))
+
+        CompilerOption("target", tagWinExe, OptionString(SetTarget tcConfigB), None, Some(FSComp.SR.optsBuildWindows ()))
+
+        CompilerOption("target", tagLibrary, OptionString(SetTarget tcConfigB), None, Some(FSComp.SR.optsBuildLibrary ()))
+
+        CompilerOption("target", tagModule, OptionString(SetTarget tcConfigB), None, Some(FSComp.SR.optsBuildModule ()))
+
+        CompilerOption(
+            "delaysign",
+            tagNone,
+            OptionSwitch(fun s -> tcConfigB.delaysign <- (s = OptionSwitch.On)),
+            None,
+            Some(FSComp.SR.optsDelaySign ())
+        )
+
+        CompilerOption(
+            "publicsign",
+            tagNone,
+            OptionSwitch(fun s -> tcConfigB.publicsign <- (s = OptionSwitch.On)),
+            None,
+            Some(FSComp.SR.optsPublicSign ())
+        )
+
+        CompilerOption("doc", tagFile, OptionString(fun s -> tcConfigB.xmlDocOutputFile <- Some s), None, Some(FSComp.SR.optsWriteXml ()))
+
+        CompilerOption("keyfile", tagFile, OptionString(fun s -> tcConfigB.signer <- Some s), None, Some(FSComp.SR.optsStrongKeyFile ()))
+
+        CompilerOption(
+            "platform",
+            tagString,
+            OptionString(fun s ->
                 tcConfigB.platform <-
                     match s with
                     | "x86" -> Some X86
@@ -715,169 +936,187 @@ let outputFileFlagsFsc (tcConfigB: TcConfigBuilder) =
                         tcConfigB.prefer32Bit <- true
                         None
                     | "anycpu" -> None
-                    | _ -> error(Error(FSComp.SR.optsUnknownPlatform s, rangeCmdArgs))), None,
-            Some(FSComp.SR.optsPlatform()))
-
-        CompilerOption
-           ("nooptimizationdata", tagNone,
-            OptionUnit (fun () -> tcConfigB.onlyEssentialOptimizationData <- true), None,
-            Some (FSComp.SR.optsNoOpt()))
-
-        CompilerOption
-           ("nointerfacedata", tagNone,
-            OptionUnit (fun () -> tcConfigB.noSignatureData <- true), None,
-            Some (FSComp.SR.optsNoInterface()))
-
-        CompilerOption
-           ("sig", tagFile,
-            OptionString (setSignatureFile tcConfigB), None,
-            Some (FSComp.SR.optsSig()))
-
-        CompilerOption
-           ("allsigs", tagNone,
-            OptionUnit (setAllSignatureFiles tcConfigB), None,
-            Some (FSComp.SR.optsAllSigs()))
-
-        CompilerOption
-           ("nocopyfsharpcore", tagNone,
-            OptionUnit (fun () -> tcConfigB.copyFSharpCore <- CopyFSharpCoreFlag.No), None,
-            Some (FSComp.SR.optsNoCopyFsharpCore()))
-
-        CompilerOption
-           ("refonly", tagNone,
-            OptionSwitch (SetReferenceAssemblyOnlySwitch tcConfigB), None,
-            Some (FSComp.SR.optsRefOnly()))
-
-        CompilerOption
-           ("refout", tagFile,
-            OptionString (SetReferenceAssemblyOutSwitch tcConfigB), None,
-            Some (FSComp.SR.optsRefOut()))
+                    | _ -> error (Error(FSComp.SR.optsUnknownPlatform s, rangeCmdArgs))),
+            None,
+            Some(FSComp.SR.optsPlatform ())
+        )
+
+        CompilerOption(
+            "nooptimizationdata",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.onlyEssentialOptimizationData <- true),
+            None,
+            Some(FSComp.SR.optsNoOpt ())
+        )
+
+        CompilerOption(
+            "nointerfacedata",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.noSignatureData <- true),
+            None,
+            Some(FSComp.SR.optsNoInterface ())
+        )
+
+        CompilerOption("sig", tagFile, OptionString(setSignatureFile tcConfigB), None, Some(FSComp.SR.optsSig ()))
+
+        CompilerOption("allsigs", tagNone, OptionUnit(setAllSignatureFiles tcConfigB), None, Some(FSComp.SR.optsAllSigs ()))
+
+        CompilerOption(
+            "nocopyfsharpcore",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.copyFSharpCore <- CopyFSharpCoreFlag.No),
+            None,
+            Some(FSComp.SR.optsNoCopyFsharpCore ())
+        )
+
+        CompilerOption("refonly", tagNone, OptionSwitch(SetReferenceAssemblyOnlySwitch tcConfigB), None, Some(FSComp.SR.optsRefOnly ()))
+
+        CompilerOption("refout", tagFile, OptionString(SetReferenceAssemblyOutSwitch tcConfigB), None, Some(FSComp.SR.optsRefOut ()))
     ]
 
-
 // OptionBlock: Resources
 //-----------------------
 
 let resourcesFlagsFsi (_tcConfigB: TcConfigBuilder) = []
+
 let resourcesFlagsFsc (tcConfigB: TcConfigBuilder) =
     [
-        CompilerOption
-            ("win32icon", tagFile,
-             OptionString (fun s -> tcConfigB.win32icon <- s), None,
-             Some (FSComp.SR.optsWin32icon()))
-        CompilerOption
-           ("win32res", tagFile,
-            OptionString (fun s -> tcConfigB.win32res <- s), None,
-            Some (FSComp.SR.optsWin32res()))
-
-        CompilerOption
-           ("win32manifest", tagFile,
-            OptionString (fun s -> tcConfigB.win32manifest <- s), None,
-            Some (FSComp.SR.optsWin32manifest()))
-
-        CompilerOption
-           ("nowin32manifest", tagNone,
-            OptionUnit (fun () -> tcConfigB.includewin32manifest <- false), None,
-            Some (FSComp.SR.optsNowin32manifest()))
-
-        CompilerOption
-           ("resource", tagResInfo,
-            OptionString (fun s -> tcConfigB.AddEmbeddedResource s), None,
-            Some (FSComp.SR.optsResource()))
-
-        CompilerOption
-           ("linkresource", tagResInfo,
-            OptionString (fun s -> tcConfigB.linkResources <- tcConfigB.linkResources ++ s), None,
-            Some (FSComp.SR.optsLinkresource()))
+        CompilerOption("win32icon", tagFile, OptionString(fun s -> tcConfigB.win32icon <- s), None, Some(FSComp.SR.optsWin32icon ()))
+        CompilerOption("win32res", tagFile, OptionString(fun s -> tcConfigB.win32res <- s), None, Some(FSComp.SR.optsWin32res ()))
+
+        CompilerOption(
+            "win32manifest",
+            tagFile,
+            OptionString(fun s -> tcConfigB.win32manifest <- s),
+            None,
+            Some(FSComp.SR.optsWin32manifest ())
+        )
+
+        CompilerOption(
+            "nowin32manifest",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.includewin32manifest <- false),
+            None,
+            Some(FSComp.SR.optsNowin32manifest ())
+        )
+
+        CompilerOption(
+            "resource",
+            tagResInfo,
+            OptionString(fun s -> tcConfigB.AddEmbeddedResource s),
+            None,
+            Some(FSComp.SR.optsResource ())
+        )
+
+        CompilerOption(
+            "linkresource",
+            tagResInfo,
+            OptionString(fun s -> tcConfigB.linkResources <- tcConfigB.linkResources ++ s),
+            None,
+            Some(FSComp.SR.optsLinkresource ())
+        )
     ]
 
-
 // OptionBlock: Code generation
 //-----------------------------
 
 let codeGenerationFlags isFsi (tcConfigB: TcConfigBuilder) =
     let debug =
-        [ CompilerOption
-            ("debug", tagNone,
-             OptionSwitch (SetDebugSwitch tcConfigB None), None,
-             Some (FSComp.SR.optsDebugPM()))
-
-          CompilerOption
-             ("debug", tagFullPDBOnlyPortable,
-              OptionString (fun s -> SetDebugSwitch tcConfigB (Some s) OptionSwitch.On), None,
-              Some (FSComp.SR.optsDebug(if isFsi then "pdbonly" else "full")))
+        [
+            CompilerOption("debug", tagNone, OptionSwitch(SetDebugSwitch tcConfigB None), None, Some(FSComp.SR.optsDebugPM ()))
+
+            CompilerOption(
+                "debug",
+                tagFullPDBOnlyPortable,
+                OptionString(fun s -> SetDebugSwitch tcConfigB (Some s) OptionSwitch.On),
+                None,
+                Some(FSComp.SR.optsDebug (if isFsi then "pdbonly" else "full"))
+            )
         ]
+
     let embed =
-        [ CompilerOption
-            ("embed", tagNone,
-             OptionSwitch (SetEmbedAllSourceSwitch tcConfigB), None,
-             Some (FSComp.SR.optsEmbedAllSource()))
-
-          CompilerOption
-            ("embed", tagFileList,
-             OptionStringList (fun f -> tcConfigB.AddEmbeddedSourceFile f), None,
-             Some ( FSComp.SR.optsEmbedSource()))
-
-          CompilerOption
-            ("sourcelink", tagFile,
-             OptionString (fun f -> tcConfigB.sourceLink <- f), None,
-             Some ( FSComp.SR.optsSourceLink()))
+        [
+            CompilerOption("embed", tagNone, OptionSwitch(SetEmbedAllSourceSwitch tcConfigB), None, Some(FSComp.SR.optsEmbedAllSource ()))
+
+            CompilerOption(
+                "embed",
+                tagFileList,
+                OptionStringList(fun f -> tcConfigB.AddEmbeddedSourceFile f),
+                None,
+                Some(FSComp.SR.optsEmbedSource ())
+            )
+
+            CompilerOption("sourcelink", tagFile, OptionString(fun f -> tcConfigB.sourceLink <- f), None, Some(FSComp.SR.optsSourceLink ()))
         ]
 
     let codegen =
-        [ CompilerOption
-            ("optimize", tagNone,
-             OptionSwitch (SetOptimizeSwitch tcConfigB), None,
-             Some (FSComp.SR.optsOptimize()))
-
-          CompilerOption
-           ("tailcalls", tagNone,
-            OptionSwitch (SetTailcallSwitch tcConfigB), None,
-            Some (FSComp.SR.optsTailcalls()))
-
-          CompilerOption
-           ("deterministic", tagNone,
-            OptionSwitch (SetDeterministicSwitch tcConfigB), None,
-            Some (FSComp.SR.optsDeterministic()))
-
-          CompilerOption
-           ("pathmap", tagPathMap,
-            OptionStringList (AddPathMapping tcConfigB), None,
-            Some (FSComp.SR.optsPathMap()))
-
-          CompilerOption
-           ("crossoptimize", tagNone,
-            OptionSwitch (crossOptimizeSwitch tcConfigB), None,
-            Some (FSComp.SR.optsCrossoptimize()))
+        [
+            CompilerOption("optimize", tagNone, OptionSwitch(SetOptimizeSwitch tcConfigB), None, Some(FSComp.SR.optsOptimize ()))
+
+            CompilerOption("tailcalls", tagNone, OptionSwitch(SetTailcallSwitch tcConfigB), None, Some(FSComp.SR.optsTailcalls ()))
+
+            CompilerOption(
+                "deterministic",
+                tagNone,
+                OptionSwitch(SetDeterministicSwitch tcConfigB),
+                None,
+                Some(FSComp.SR.optsDeterministic ())
+            )
+
+            CompilerOption("pathmap", tagPathMap, OptionStringList(AddPathMapping tcConfigB), None, Some(FSComp.SR.optsPathMap ()))
+
+            CompilerOption(
+                "crossoptimize",
+                tagNone,
+                OptionSwitch(crossOptimizeSwitch tcConfigB),
+                None,
+                Some(FSComp.SR.optsCrossoptimize ())
+            )
 
         ]
-    if isFsi then debug @ codegen
-    else debug @ embed @ codegen
+
+    if isFsi then debug @ codegen else debug @ embed @ codegen
 
 // OptionBlock: Language
 //----------------------
 
-let defineSymbol tcConfigB s = tcConfigB.conditionalDefines <- s :: tcConfigB.conditionalDefines
+let defineSymbol tcConfigB s =
+    tcConfigB.conditionalDefines <- s :: tcConfigB.conditionalDefines
 
 let mlCompatibilityFlag (tcConfigB: TcConfigBuilder) =
-    CompilerOption
-       ("mlcompatibility", tagNone,
-        OptionUnit (fun () -> tcConfigB.mlCompatibility<-true; tcConfigB.TurnWarningOff(rangeCmdArgs, "62")), None,
-        Some (FSComp.SR.optsMlcompatibility()))
+    CompilerOption(
+        "mlcompatibility",
+        tagNone,
+        OptionUnit(fun () ->
+            tcConfigB.mlCompatibility <- true
+            tcConfigB.TurnWarningOff(rangeCmdArgs, "62")),
+        None,
+        Some(FSComp.SR.optsMlcompatibility ())
+    )
 
 /// LanguageVersion management
 let setLanguageVersion specifiedVersion =
 
     let languageVersion = LanguageVersion(specifiedVersion)
+
     let dumpAllowedValues () =
-        printfn "%s" (FSComp.SR.optsSupportedLangVersions())
-        for v in languageVersion.ValidOptions do printfn "%s" v
-        for v in languageVersion.ValidVersions do printfn "%s" v
+        printfn "%s" (FSComp.SR.optsSupportedLangVersions ())
+
+        for v in languageVersion.ValidOptions do
+            printfn "%s" v
+
+        for v in languageVersion.ValidVersions do
+            printfn "%s" v
+
         exit 0
 
-    if specifiedVersion = "?" then dumpAllowedValues ()
-    elif specifiedVersion.ToUpperInvariant() = "PREVIEW" then ()
-    elif not (languageVersion.ContainsVersion specifiedVersion) then error(Error(FSComp.SR.optsUnrecognizedLanguageVersion specifiedVersion, rangeCmdArgs))
+    if specifiedVersion = "?" then
+        dumpAllowedValues ()
+    elif specifiedVersion.ToUpperInvariant() = "PREVIEW" then
+        ()
+    elif not (languageVersion.ContainsVersion specifiedVersion) then
+        error (Error(FSComp.SR.optsUnrecognizedLanguageVersion specifiedVersion, rangeCmdArgs))
+
     languageVersion
 
 let languageFlags tcConfigB =
@@ -888,10 +1127,22 @@ let languageFlags tcConfigB =
         //                               'latest' (latest version, including minor versions),
         //                               'preview' (features for preview)
         //                               or specific versions like '4.7'
-        CompilerOption("langversion", tagLangVersionValues, OptionString (fun switch -> tcConfigB.langVersion <- setLanguageVersion(switch)), None, Some (FSComp.SR.optsLangVersion()))
-
-        CompilerOption("checked", tagNone, OptionSwitch (fun switch -> tcConfigB.checkOverflow <- (switch = OptionSwitch.On)), None, Some (FSComp.SR.optsChecked()))
-        CompilerOption("define", tagString, OptionString (defineSymbol tcConfigB), None, Some (FSComp.SR.optsDefine()))
+        CompilerOption(
+            "langversion",
+            tagLangVersionValues,
+            OptionString(fun switch -> tcConfigB.langVersion <- setLanguageVersion (switch)),
+            None,
+            Some(FSComp.SR.optsLangVersion ())
+        )
+
+        CompilerOption(
+            "checked",
+            tagNone,
+            OptionSwitch(fun switch -> tcConfigB.checkOverflow <- (switch = OptionSwitch.On)),
+            None,
+            Some(FSComp.SR.optsChecked ())
+        )
+        CompilerOption("define", tagString, OptionString(defineSymbol tcConfigB), None, Some(FSComp.SR.optsDefine ()))
         mlCompatibilityFlag tcConfigB
     ]
 
@@ -899,46 +1150,52 @@ let languageFlags tcConfigB =
 //-----------------------------------
 
 let libFlag (tcConfigB: TcConfigBuilder) =
-    CompilerOption
-        ("lib", tagDirList,
-         OptionStringList (fun s -> tcConfigB.AddIncludePath (rangeStartup, s, tcConfigB.implicitIncludeDir)), None,
-         Some (FSComp.SR.optsLib()))
+    CompilerOption(
+        "lib",
+        tagDirList,
+        OptionStringList(fun s -> tcConfigB.AddIncludePath(rangeStartup, s, tcConfigB.implicitIncludeDir)),
+        None,
+        Some(FSComp.SR.optsLib ())
+    )
 
 let codePageFlag (tcConfigB: TcConfigBuilder) =
-    CompilerOption
-        ("codepage", tagInt,
-         OptionInt (fun n ->
+    CompilerOption(
+        "codepage",
+        tagInt,
+        OptionInt(fun n ->
             try
                 System.Text.Encoding.GetEncoding n |> ignore
             with :? ArgumentException as err ->
-                error(Error(FSComp.SR.optsProblemWithCodepage(n, err.Message), rangeCmdArgs))
+                error (Error(FSComp.SR.optsProblemWithCodepage (n, err.Message), rangeCmdArgs))
 
-            tcConfigB.inputCodePage <- Some n), None,
-                Some (FSComp.SR.optsCodepage()))
+            tcConfigB.inputCodePage <- Some n),
+        None,
+        Some(FSComp.SR.optsCodepage ())
+    )
 
 let preferredUiLang (tcConfigB: TcConfigBuilder) =
-    CompilerOption
-        ("preferreduilang", tagString,
-         OptionString (fun s -> tcConfigB.preferredUiLang <- Some s), None,
-         Some(FSComp.SR.optsPreferredUiLang()))
+    CompilerOption(
+        "preferreduilang",
+        tagString,
+        OptionString(fun s -> tcConfigB.preferredUiLang <- Some s),
+        None,
+        Some(FSComp.SR.optsPreferredUiLang ())
+    )
 
 let utf8OutputFlag (tcConfigB: TcConfigBuilder) =
-    CompilerOption
-        ("utf8output", tagNone,
-         OptionUnit (fun () -> tcConfigB.utf8output <- true), None,
-         Some (FSComp.SR.optsUtf8output()))
+    CompilerOption("utf8output", tagNone, OptionUnit(fun () -> tcConfigB.utf8output <- true), None, Some(FSComp.SR.optsUtf8output ()))
 
-let fullPathsFlag  (tcConfigB: TcConfigBuilder)  =
-    CompilerOption
-        ("fullpaths", tagNone,
-         OptionUnit (fun () -> tcConfigB.showFullPaths <- true), None,
-         Some (FSComp.SR.optsFullpaths()))
+let fullPathsFlag (tcConfigB: TcConfigBuilder) =
+    CompilerOption("fullpaths", tagNone, OptionUnit(fun () -> tcConfigB.showFullPaths <- true), None, Some(FSComp.SR.optsFullpaths ()))
 
 let cliRootFlag (_tcConfigB: TcConfigBuilder) =
-    CompilerOption
-        ("cliroot", tagString,
-         OptionString (fun _  -> ()), Some(DeprecatedCommandLineOptionFull(FSComp.SR.optsClirootDeprecatedMsg(), rangeCmdArgs)),
-         Some(FSComp.SR.optsClirootDescription()))
+    CompilerOption(
+        "cliroot",
+        tagString,
+        OptionString(fun _ -> ()),
+        Some(DeprecatedCommandLineOptionFull(FSComp.SR.optsClirootDeprecatedMsg (), rangeCmdArgs)),
+        Some(FSComp.SR.optsClirootDescription ())
+    )
 
 let SetTargetProfile (tcConfigB: TcConfigBuilder) v =
     let primaryAssembly =
@@ -946,108 +1203,122 @@ let SetTargetProfile (tcConfigB: TcConfigBuilder) v =
         // Indicates we assume "mscorlib.dll", i.e .NET Framework, Mono and Profile 47
         | "mscorlib" -> PrimaryAssembly.Mscorlib
         // Indicates we assume "System.Runtime.dll", i.e .NET Standard 1.x, .NET Core App 1.x and above, and Profile 7/78/259
-        | "netcore"  -> PrimaryAssembly.System_Runtime
+        | "netcore" -> PrimaryAssembly.System_Runtime
         // Indicates we assume "netstandard.dll", i.e .NET Standard 2.0 and above
-        | "netstandard"  -> PrimaryAssembly.NetStandard
-        | _ -> error(Error(FSComp.SR.optsInvalidTargetProfile v, rangeCmdArgs))
-    tcConfigB.SetPrimaryAssembly  primaryAssembly
+        | "netstandard" -> PrimaryAssembly.NetStandard
+        | _ -> error (Error(FSComp.SR.optsInvalidTargetProfile v, rangeCmdArgs))
+
+    tcConfigB.SetPrimaryAssembly primaryAssembly
 
 let advancedFlagsBoth tcConfigB =
     [
-        yield codePageFlag tcConfigB
-        yield utf8OutputFlag tcConfigB
-        yield preferredUiLang tcConfigB
-        yield fullPathsFlag tcConfigB
-        yield libFlag tcConfigB
-        yield CompilerOption
-                 ("simpleresolution",
-                  tagNone,
-                  OptionUnit (fun () -> tcConfigB.useSimpleResolution<-true), None,
-                  Some (FSComp.SR.optsSimpleresolution()))
-
-        yield CompilerOption
-                 ("targetprofile", tagString,
-                  OptionString (SetTargetProfile tcConfigB), None,
-                  Some(FSComp.SR.optsTargetProfile()))
+        codePageFlag tcConfigB
+        utf8OutputFlag tcConfigB
+        preferredUiLang tcConfigB
+        fullPathsFlag tcConfigB
+        libFlag tcConfigB
+        CompilerOption(
+            "simpleresolution",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.useSimpleResolution <- true),
+            None,
+            Some(FSComp.SR.optsSimpleresolution ())
+        )
+
+        CompilerOption("targetprofile", tagString, OptionString(SetTargetProfile tcConfigB), None, Some(FSComp.SR.optsTargetProfile ()))
     ]
 
 let noFrameworkFlag isFsc tcConfigB =
-    CompilerOption
-        ("noframework", tagNone,
-         OptionUnit (fun () ->
+    CompilerOption(
+        "noframework",
+        tagNone,
+        OptionUnit(fun () ->
             tcConfigB.implicitlyReferenceDotNetAssemblies <- false
-            if isFsc then
-                tcConfigB.implicitlyResolveAssemblies <- false), None,
-         Some (FSComp.SR.optsNoframework()))
+            if isFsc then tcConfigB.implicitlyResolveAssemblies <- false),
+        None,
+        Some(FSComp.SR.optsNoframework ())
+    )
 
 let advancedFlagsFsi tcConfigB =
-    advancedFlagsBoth tcConfigB  @
-    [
-        yield noFrameworkFlag false tcConfigB
-    ]
+    advancedFlagsBoth tcConfigB @ [ noFrameworkFlag false tcConfigB ]
 
 let advancedFlagsFsc tcConfigB =
-    advancedFlagsBoth tcConfigB @
-    [
-        yield CompilerOption
-                  ("baseaddress", tagAddress,
-                   OptionString (fun s -> tcConfigB.baseAddress <- Some(int32 s)), None,
-                   Some (FSComp.SR.optsBaseaddress()))
-
-        yield CompilerOption
-                  ("checksumalgorithm", tagAlgorithm,
-                   OptionString (fun s ->
-                       tcConfigB.checksumAlgorithm <-
-                        match s.ToUpperInvariant() with
-                        | "SHA1" -> HashAlgorithm.Sha1
-                        | "SHA256" -> HashAlgorithm.Sha256
-                        | _ -> error(Error(FSComp.SR.optsUnknownChecksumAlgorithm s, rangeCmdArgs))), None,
-                        Some (FSComp.SR.optsChecksumAlgorithm()))
-
-        yield noFrameworkFlag true tcConfigB
-
-        yield CompilerOption
-                  ("standalone", tagNone,
-                   OptionUnit (fun _ ->
-                        tcConfigB.openDebugInformationForLaterStaticLinking <- true
-                        tcConfigB.standalone <- true
-                        tcConfigB.implicitlyResolveAssemblies <- true), None,
-                   Some (FSComp.SR.optsStandalone()))
-
-        yield CompilerOption
-                  ("staticlink", tagFile,
-                   OptionString (fun s ->
-                       tcConfigB.extraStaticLinkRoots <- tcConfigB.extraStaticLinkRoots @ [s]
-                       tcConfigB.implicitlyResolveAssemblies <- true), None,
-                   Some (FSComp.SR.optsStaticlink()))
+    advancedFlagsBoth tcConfigB
+    @ [
+        CompilerOption(
+            "baseaddress",
+            tagAddress,
+            OptionString(fun s -> tcConfigB.baseAddress <- Some(int32 s)),
+            None,
+            Some(FSComp.SR.optsBaseaddress ())
+        )
+
+        CompilerOption(
+            "checksumalgorithm",
+            tagAlgorithm,
+            OptionString(fun s ->
+                tcConfigB.checksumAlgorithm <-
+                    match s.ToUpperInvariant() with
+                    | "SHA1" -> HashAlgorithm.Sha1
+                    | "SHA256" -> HashAlgorithm.Sha256
+                    | _ -> error (Error(FSComp.SR.optsUnknownChecksumAlgorithm s, rangeCmdArgs))),
+            None,
+            Some(FSComp.SR.optsChecksumAlgorithm ())
+        )
+
+        noFrameworkFlag true tcConfigB
+
+        CompilerOption(
+            "standalone",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.openDebugInformationForLaterStaticLinking <- true
+                tcConfigB.standalone <- true
+                tcConfigB.implicitlyResolveAssemblies <- true),
+            None,
+            Some(FSComp.SR.optsStandalone ())
+        )
+
+        CompilerOption(
+            "staticlink",
+            tagFile,
+            OptionString(fun s ->
+                tcConfigB.extraStaticLinkRoots <- tcConfigB.extraStaticLinkRoots @ [ s ]
+                tcConfigB.implicitlyResolveAssemblies <- true),
+            None,
+            Some(FSComp.SR.optsStaticlink ())
+        )
 
 #if ENABLE_MONO_SUPPORT
         if runningOnMono then
-            yield CompilerOption
-                      ("resident", tagFile,
-                       OptionUnit (fun () -> ()), None,
-                       Some (FSComp.SR.optsResident()))
+            CompilerOption("resident", tagFile, OptionUnit(fun () -> ()), None, Some(FSComp.SR.optsResident ()))
 #endif
 
-        yield CompilerOption
-                  ("pdb", tagString,
-                   OptionString (fun s -> tcConfigB.debugSymbolFile <- Some s), None,
-                   Some (FSComp.SR.optsPdb()))
-
-        yield CompilerOption
-                  ("highentropyva", tagNone,
-                   OptionSwitch (useHighEntropyVASwitch tcConfigB), None,
-                   Some (FSComp.SR.optsUseHighEntropyVA()))
-
-        yield CompilerOption
-                  ("subsystemversion", tagString,
-                   OptionString (subSystemVersionSwitch tcConfigB), None,
-                   Some (FSComp.SR.optsSubSystemVersion()))
-
-        yield CompilerOption
-                  ("quotations-debug", tagNone,
-                   OptionSwitch(fun switch -> tcConfigB.emitDebugInfoInQuotations <- switch = OptionSwitch.On), None,
-                   Some(FSComp.SR.optsEmitDebugInfoInQuotations()))
+        CompilerOption("pdb", tagString, OptionString(fun s -> tcConfigB.debugSymbolFile <- Some s), None, Some(FSComp.SR.optsPdb ()))
+
+        CompilerOption(
+            "highentropyva",
+            tagNone,
+            OptionSwitch(useHighEntropyVASwitch tcConfigB),
+            None,
+            Some(FSComp.SR.optsUseHighEntropyVA ())
+        )
+
+        CompilerOption(
+            "subsystemversion",
+            tagString,
+            OptionString(subSystemVersionSwitch tcConfigB),
+            None,
+            Some(FSComp.SR.optsSubSystemVersion ())
+        )
+
+        CompilerOption(
+            "quotations-debug",
+            tagNone,
+            OptionSwitch(fun switch -> tcConfigB.emitDebugInfoInQuotations <- switch = OptionSwitch.On),
+            None,
+            Some(FSComp.SR.optsEmitDebugInfoInQuotations ())
+        )
 
     ]
 
@@ -1055,415 +1326,641 @@ let advancedFlagsFsc tcConfigB =
 //--------------------------------------------------
 
 let testFlag tcConfigB =
-        CompilerOption
-            ("test", tagString,
-             OptionString (fun s ->
-                match s with
-                | "StackSpan"        -> tcConfigB.internalTestSpanStackReferring <- true
-                | "ErrorRanges"      -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Test
-                | "Tracking"         -> tracking <- true (* general purpose on/off diagnostics flag *)
-                | "NoNeedToTailcall" -> tcConfigB.optSettings <- { tcConfigB.optSettings with reportNoNeedToTailcall = true }
-                | "FunctionSizes"    -> tcConfigB.optSettings <- { tcConfigB.optSettings with reportFunctionSizes = true }
-                | "TotalSizes"       -> tcConfigB.optSettings <- { tcConfigB.optSettings with reportTotalSizes = true }
-                | "HasEffect"        -> tcConfigB.optSettings <- { tcConfigB.optSettings with reportHasEffect = true }
-                | "NoErrorText"      -> FSComp.SR.SwallowResourceText <- true
-                | "EmitFeeFeeAs100001" -> tcConfigB.testFlagEmitFeeFeeAs100001 <- true
-                | "DumpDebugInfo"    -> tcConfigB.dumpDebugInfo <- true
-                | "ShowLoadedAssemblies" -> tcConfigB.showLoadedAssemblies <- true
-                | "ContinueAfterParseFailure" -> tcConfigB.continueAfterParseFailure <- true
-                | "ParallelOff" -> tcConfigB.concurrentBuild <- false
+    CompilerOption(
+        "test",
+        tagString,
+        OptionString(fun s ->
+            match s with
+            | "StackSpan" -> tcConfigB.internalTestSpanStackReferring <- true
+            | "ErrorRanges" -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Test
+            | "Tracking" -> tracking <- true (* general purpose on/off diagnostics flag *)
+            | "NoNeedToTailcall" ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        reportNoNeedToTailcall = true
+                    }
+            | "FunctionSizes" ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        reportFunctionSizes = true
+                    }
+            | "TotalSizes" ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        reportTotalSizes = true
+                    }
+            | "HasEffect" ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        reportHasEffect = true
+                    }
+            | "NoErrorText" -> FSComp.SR.SwallowResourceText <- true
+            | "EmitFeeFeeAs100001" -> tcConfigB.testFlagEmitFeeFeeAs100001 <- true
+            | "DumpDebugInfo" -> tcConfigB.dumpDebugInfo <- true
+            | "ShowLoadedAssemblies" -> tcConfigB.showLoadedAssemblies <- true
+            | "ContinueAfterParseFailure" -> tcConfigB.continueAfterParseFailure <- true
+            | "ParallelOff" -> tcConfigB.concurrentBuild <- false
 #if DEBUG
-                | "ShowParserStackOnParseError" -> showParserStackOnParseError <- true
+            | "ShowParserStackOnParseError" -> showParserStackOnParseError <- true
 #endif
-                | str                -> warning(Error(FSComp.SR.optsUnknownArgumentToTheTestSwitch str, rangeCmdArgs))), None,
-             None)
+            | str -> warning (Error(FSComp.SR.optsUnknownArgumentToTheTestSwitch str, rangeCmdArgs))),
+        None,
+        None
+    )
 
 // Not shown in fsc.exe help, no warning on use, motivation is for use from tooling.
 let editorSpecificFlags (tcConfigB: TcConfigBuilder) =
-  [ CompilerOption("vserrors", tagNone, OptionUnit (fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.VisualStudio), None, None)
-    CompilerOption("validate-type-providers", tagNone, OptionUnit id, None, None)  // preserved for compatibility's sake, no longer has any effect
-    CompilerOption("LCID", tagInt, OptionInt ignore, None, None)
-    CompilerOption("flaterrors", tagNone, OptionUnit (fun () -> tcConfigB.flatErrors <- true), None, None)
-    CompilerOption("sqmsessionguid", tagNone, OptionString ignore, None, None)
-    CompilerOption("gccerrors", tagNone, OptionUnit (fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Gcc), None, None)
-    CompilerOption("exename", tagNone, OptionString (fun s -> tcConfigB.exename <- Some s), None, None)
-    CompilerOption("maxerrors", tagInt, OptionInt (fun n -> tcConfigB.maxErrors <- n), None, None)
-    CompilerOption("noconditionalerasure", tagNone, OptionUnit (fun () -> tcConfigB.noConditionalErasure <- true), None, None)
-  ]
-
-let internalFlags (tcConfigB:TcConfigBuilder) =
-  [
-    CompilerOption
-       ("stamps", tagNone,
-        OptionUnit ignore,
-        Some(InternalCommandLineOption("--stamps", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("ranges", tagNone,
-        OptionSet DebugPrint.layoutRanges,
-        Some(InternalCommandLineOption("--ranges", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("terms", tagNone,
-        OptionUnit (fun () -> tcConfigB.showTerms <- true),
-        Some(InternalCommandLineOption("--terms", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("termsfile", tagNone,
-        OptionUnit (fun () -> tcConfigB.writeTermsToFiles <- true),
-        Some(InternalCommandLineOption("--termsfile", rangeCmdArgs)), None)
+    [
+        CompilerOption("vserrors", tagNone, OptionUnit(fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.VisualStudio), None, None)
+        CompilerOption("validate-type-providers", tagNone, OptionUnit id, None, None) // preserved for compatibility's sake, no longer has any effect
+        CompilerOption("LCID", tagInt, OptionInt ignore, None, None)
+        CompilerOption("flaterrors", tagNone, OptionUnit(fun () -> tcConfigB.flatErrors <- true), None, None)
+        CompilerOption("sqmsessionguid", tagNone, OptionString ignore, None, None)
+        CompilerOption("gccerrors", tagNone, OptionUnit(fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Gcc), None, None)
+        CompilerOption("exename", tagNone, OptionString(fun s -> tcConfigB.exename <- Some s), None, None)
+        CompilerOption("maxerrors", tagInt, OptionInt(fun n -> tcConfigB.maxErrors <- n), None, None)
+        CompilerOption("noconditionalerasure", tagNone, OptionUnit(fun () -> tcConfigB.noConditionalErasure <- true), None, None)
+    ]
+
+let internalFlags (tcConfigB: TcConfigBuilder) =
+    [
+        CompilerOption("stamps", tagNone, OptionUnit ignore, Some(InternalCommandLineOption("--stamps", rangeCmdArgs)), None)
+
+        CompilerOption(
+            "ranges",
+            tagNone,
+            OptionSet DebugPrint.layoutRanges,
+            Some(InternalCommandLineOption("--ranges", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "terms",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.showTerms <- true),
+            Some(InternalCommandLineOption("--terms", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "termsfile",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.writeTermsToFiles <- true),
+            Some(InternalCommandLineOption("--termsfile", rangeCmdArgs)),
+            None
+        )
 
 #if DEBUG
-    CompilerOption
-       ("debug-parse", tagNone,
-        OptionUnit (fun () -> Internal.Utilities.Text.Parsing.Flags.debug <- true),
-        Some(InternalCommandLineOption("--debug-parse", rangeCmdArgs)), None)
+        CompilerOption(
+            "debug-parse",
+            tagNone,
+            OptionUnit(fun () -> Internal.Utilities.Text.Parsing.Flags.debug <- true),
+            Some(InternalCommandLineOption("--debug-parse", rangeCmdArgs)),
+            None
+        )
 #endif
 
-    CompilerOption
-       ("pause", tagNone,
-        OptionUnit (fun () -> tcConfigB.pause <- true),
-        Some(InternalCommandLineOption("--pause", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("detuple", tagNone,
-        OptionInt (setFlag (fun v -> tcConfigB.doDetuple <- v)),
-        Some(InternalCommandLineOption("--detuple", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("simulateException", tagNone,
-        OptionString (fun s -> tcConfigB.simulateException <- Some s),
-        Some(InternalCommandLineOption("--simulateException", rangeCmdArgs)), Some "Simulate an exception from some part of the compiler")
-
-    CompilerOption
-       ("stackReserveSize", tagNone,
-        OptionString (fun s -> tcConfigB.stackReserveSize <- Some(int32 s)),
-        Some(InternalCommandLineOption("--stackReserveSize", rangeCmdArgs)), Some "for an exe, set stack reserve size")
-
-    CompilerOption
-       ("tlr", tagInt,
-        OptionInt (setFlag (fun v -> tcConfigB.doTLR <- v)),
-        Some(InternalCommandLineOption("--tlr", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("finalSimplify", tagInt,
-        OptionInt (setFlag (fun v -> tcConfigB.doFinalSimplify <- v)),
-        Some(InternalCommandLineOption("--finalSimplify", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("parseonly", tagNone,
-        OptionUnit (fun () -> tcConfigB.parseOnly <- true),
-        Some(InternalCommandLineOption("--parseonly", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("typecheckonly", tagNone,
-        OptionUnit (fun () -> tcConfigB.typeCheckOnly <- true),
-        Some(InternalCommandLineOption("--typecheckonly", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("ast", tagNone,
-        OptionUnit (fun () -> tcConfigB.printAst <- true),
-        Some(InternalCommandLineOption("--ast", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("tokenize", tagNone,
-        OptionUnit (fun () -> tcConfigB.tokenize <- TokenizeOption.Only),
-        Some(InternalCommandLineOption("--tokenize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("tokenize-unfiltered", tagNone,
-        OptionUnit (fun () -> tcConfigB.tokenize <- TokenizeOption.Unfiltered),
-        Some(InternalCommandLineOption("--tokenize-unfiltered", rangeCmdArgs)), None)
-    
-    CompilerOption
-       ("testInteractionParser", tagNone,
-        OptionUnit (fun () -> tcConfigB.testInteractionParser <- true),
-        Some(InternalCommandLineOption("--testInteractionParser", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("testparsererrorrecovery", tagNone,
-        OptionUnit (fun () -> tcConfigB.reportNumDecls <- true),
-        Some(InternalCommandLineOption("--testparsererrorrecovery", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("inlinethreshold", tagInt,
-        OptionInt (fun n -> tcConfigB.optSettings <- { tcConfigB.optSettings with lambdaInlineThreshold = n }),
-        Some(InternalCommandLineOption("--inlinethreshold", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("extraoptimizationloops", tagNone,
-        OptionInt (fun n -> tcConfigB.extraOptimizationIterations <- n),
-        Some(InternalCommandLineOption("--extraoptimizationloops", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("abortonerror", tagNone,
-        OptionUnit (fun () -> tcConfigB.abortOnError <- true),
-        Some(InternalCommandLineOption("--abortonerror", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("implicitresolution", tagNone,
-        OptionUnit (fun _ -> tcConfigB.implicitlyResolveAssemblies <- true),
-        Some(InternalCommandLineOption("--implicitresolution", rangeCmdArgs)), None)
-
-    // "Display assembly reference resolution information")
-    CompilerOption
-       ("resolutions", tagNone,
-        OptionUnit (fun () -> tcConfigB.showReferenceResolutions <- true),
-        Some(InternalCommandLineOption("", rangeCmdArgs)), None)
-
-    // "The base registry key to use for assembly resolution. This part in brackets here: HKEY_LOCAL_MACHINE\[SOFTWARE\Microsoft\.NETFramework]\v2.0.50727\AssemblyFoldersEx")
-    CompilerOption
-       ("resolutionframeworkregistrybase", tagString,
-        OptionString (fun _ -> ()),
-        Some(InternalCommandLineOption("", rangeCmdArgs)), None)
-
-    // "The base registry key to use for assembly resolution. This part in brackets here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727\[AssemblyFoldersEx]")
-    CompilerOption
-       ("resolutionassemblyfoldersuffix", tagString,
-        OptionString (fun _ -> ()),
-        Some(InternalCommandLineOption("resolutionassemblyfoldersuffix", rangeCmdArgs)), None)
-
-    // "Additional reference resolution conditions. For example \"OSVersion=5.1.2600.0, PlatformID=id")
-    CompilerOption
-       ("resolutionassemblyfoldersconditions", tagString,
-        OptionString (fun _ -> ()),
-        Some(InternalCommandLineOption("resolutionassemblyfoldersconditions", rangeCmdArgs)), None)
-
-    // "Resolve assembly references using MSBuild resolution rules rather than directory based (Default=true except when running fsc.exe under mono)")
-    CompilerOption
-       ("msbuildresolution", tagNone,
-        OptionUnit (fun () -> tcConfigB.useSimpleResolution <- false),
-        Some(InternalCommandLineOption("msbuildresolution", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("alwayscallvirt", tagNone,
-        OptionSwitch(callVirtSwitch tcConfigB),
-        Some(InternalCommandLineOption("alwayscallvirt", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("nodebugdata", tagNone,
-        OptionUnit (fun () -> tcConfigB.noDebugAttributes <- true),
-        Some(InternalCommandLineOption("nodebugdata", rangeCmdArgs)), None)
-
-    testFlag tcConfigB  ] @
-
-  editorSpecificFlags tcConfigB @
-  [ CompilerOption
-       ("jit", tagNone,
-        OptionSwitch (jitoptimizeSwitch tcConfigB),
-        Some(InternalCommandLineOption("jit", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("localoptimize", tagNone,
-        OptionSwitch(localoptimizeSwitch tcConfigB),
-        Some(InternalCommandLineOption("localoptimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("splitting", tagNone,
-        OptionSwitch(splittingSwitch tcConfigB),
-        Some(InternalCommandLineOption("splitting", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("versionfile", tagString,
-        OptionString (fun s -> tcConfigB.version <- VersionFile s),
-        Some(InternalCommandLineOption("versionfile", rangeCmdArgs)), None)
-
-    // "Display timing profiles for compilation"
-    CompilerOption
-       ("times", tagNone,
-        OptionUnit  (fun () -> tcConfigB.showTimes <- true),
-        Some(InternalCommandLineOption("times", rangeCmdArgs)), None)
+        CompilerOption(
+            "pause",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.pause <- true),
+            Some(InternalCommandLineOption("--pause", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "detuple",
+            tagNone,
+            OptionInt(setFlag (fun v -> tcConfigB.doDetuple <- v)),
+            Some(InternalCommandLineOption("--detuple", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "simulateException",
+            tagNone,
+            OptionString(fun s -> tcConfigB.simulateException <- Some s),
+            Some(InternalCommandLineOption("--simulateException", rangeCmdArgs)),
+            Some "Simulate an exception from some part of the compiler"
+        )
+
+        CompilerOption(
+            "stackReserveSize",
+            tagNone,
+            OptionString(fun s -> tcConfigB.stackReserveSize <- Some(int32 s)),
+            Some(InternalCommandLineOption("--stackReserveSize", rangeCmdArgs)),
+            Some "for an exe, set stack reserve size"
+        )
+
+        CompilerOption(
+            "tlr",
+            tagInt,
+            OptionInt(setFlag (fun v -> tcConfigB.doTLR <- v)),
+            Some(InternalCommandLineOption("--tlr", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "finalSimplify",
+            tagInt,
+            OptionInt(setFlag (fun v -> tcConfigB.doFinalSimplify <- v)),
+            Some(InternalCommandLineOption("--finalSimplify", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "parseonly",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.parseOnly <- true),
+            Some(InternalCommandLineOption("--parseonly", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "typecheckonly",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.typeCheckOnly <- true),
+            Some(InternalCommandLineOption("--typecheckonly", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "ast",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.printAst <- true),
+            Some(InternalCommandLineOption("--ast", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "tokenize",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.tokenize <- TokenizeOption.Only),
+            Some(InternalCommandLineOption("--tokenize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "tokenize-unfiltered",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.tokenize <- TokenizeOption.Unfiltered),
+            Some(InternalCommandLineOption("--tokenize-unfiltered", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "testInteractionParser",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.testInteractionParser <- true),
+            Some(InternalCommandLineOption("--testInteractionParser", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "testparsererrorrecovery",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.reportNumDecls <- true),
+            Some(InternalCommandLineOption("--testparsererrorrecovery", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "inlinethreshold",
+            tagInt,
+            OptionInt(fun n ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        lambdaInlineThreshold = n
+                    }),
+            Some(InternalCommandLineOption("--inlinethreshold", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "extraoptimizationloops",
+            tagNone,
+            OptionInt(fun n -> tcConfigB.extraOptimizationIterations <- n),
+            Some(InternalCommandLineOption("--extraoptimizationloops", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "abortonerror",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.abortOnError <- true),
+            Some(InternalCommandLineOption("--abortonerror", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "implicitresolution",
+            tagNone,
+            OptionUnit(fun _ -> tcConfigB.implicitlyResolveAssemblies <- true),
+            Some(InternalCommandLineOption("--implicitresolution", rangeCmdArgs)),
+            None
+        )
+
+        // "Display assembly reference resolution information")
+        CompilerOption(
+            "resolutions",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.showReferenceResolutions <- true),
+            Some(InternalCommandLineOption("", rangeCmdArgs)),
+            None
+        )
+
+        // "The base registry key to use for assembly resolution. This part in brackets here: HKEY_LOCAL_MACHINE\[SOFTWARE\Microsoft\.NETFramework]\v2.0.50727\AssemblyFoldersEx")
+        CompilerOption(
+            "resolutionframeworkregistrybase",
+            tagString,
+            OptionString(fun _ -> ()),
+            Some(InternalCommandLineOption("", rangeCmdArgs)),
+            None
+        )
+
+        // "The base registry key to use for assembly resolution. This part in brackets here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727\[AssemblyFoldersEx]")
+        CompilerOption(
+            "resolutionassemblyfoldersuffix",
+            tagString,
+            OptionString(fun _ -> ()),
+            Some(InternalCommandLineOption("resolutionassemblyfoldersuffix", rangeCmdArgs)),
+            None
+        )
+
+        // "Additional reference resolution conditions. For example \"OSVersion=5.1.2600.0, PlatformID=id")
+        CompilerOption(
+            "resolutionassemblyfoldersconditions",
+            tagString,
+            OptionString(fun _ -> ()),
+            Some(InternalCommandLineOption("resolutionassemblyfoldersconditions", rangeCmdArgs)),
+            None
+        )
+
+        // "Resolve assembly references using MSBuild resolution rules rather than directory based (Default=true except when running fsc.exe under mono)")
+        CompilerOption(
+            "msbuildresolution",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.useSimpleResolution <- false),
+            Some(InternalCommandLineOption("msbuildresolution", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "alwayscallvirt",
+            tagNone,
+            OptionSwitch(callVirtSwitch tcConfigB),
+            Some(InternalCommandLineOption("alwayscallvirt", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "nodebugdata",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.noDebugAttributes <- true),
+            Some(InternalCommandLineOption("nodebugdata", rangeCmdArgs)),
+            None
+        )
+
+        testFlag tcConfigB
+    ]
+    @
+
+    editorSpecificFlags tcConfigB
+    @ [
+        CompilerOption(
+            "jit",
+            tagNone,
+            OptionSwitch(jitoptimizeSwitch tcConfigB),
+            Some(InternalCommandLineOption("jit", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "localoptimize",
+            tagNone,
+            OptionSwitch(localoptimizeSwitch tcConfigB),
+            Some(InternalCommandLineOption("localoptimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "splitting",
+            tagNone,
+            OptionSwitch(splittingSwitch tcConfigB),
+            Some(InternalCommandLineOption("splitting", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "versionfile",
+            tagString,
+            OptionString(fun s -> tcConfigB.version <- VersionFile s),
+            Some(InternalCommandLineOption("versionfile", rangeCmdArgs)),
+            None
+        )
+
+        // "Display timing profiles for compilation"
+        CompilerOption(
+            "times",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.showTimes <- true),
+            Some(InternalCommandLineOption("times", rangeCmdArgs)),
+            None
+        )
 
 #if !NO_TYPEPROVIDERS
-    // "Display information about extension type resolution")
-    CompilerOption
-       ("showextensionresolution", tagNone,
-        OptionUnit  (fun () -> tcConfigB.showExtensionTypeMessages <- true),
-        Some(InternalCommandLineOption("showextensionresolution", rangeCmdArgs)), None)
+        // "Display information about extension type resolution")
+        CompilerOption(
+            "showextensionresolution",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.showExtensionTypeMessages <- true),
+            Some(InternalCommandLineOption("showextensionresolution", rangeCmdArgs)),
+            None
+        )
 #endif
 
-    CompilerOption
-       ("metadataversion", tagString,
-        OptionString (fun s -> tcConfigB.metadataVersion <- Some s),
-        Some(InternalCommandLineOption("metadataversion", rangeCmdArgs)), None)
-  ]
+        CompilerOption(
+            "metadataversion",
+            tagString,
+            OptionString(fun s -> tcConfigB.metadataVersion <- Some s),
+            Some(InternalCommandLineOption("metadataversion", rangeCmdArgs)),
+            None
+        )
+    ]
 
 // OptionBlock: Deprecated flags (fsc, service only)
 //--------------------------------------------------
 
 let compilingFsLibFlag (tcConfigB: TcConfigBuilder) =
-    CompilerOption
-        ("compiling-fslib", tagNone,
-         OptionUnit (fun () ->
+    CompilerOption(
+        "compiling-fslib",
+        tagNone,
+        OptionUnit(fun () ->
             tcConfigB.compilingFSharpCore <- true
             tcConfigB.TurnWarningOff(rangeStartup, "42")),
-         Some(InternalCommandLineOption("--compiling-fslib", rangeCmdArgs)), None)
+        Some(InternalCommandLineOption("--compiling-fslib", rangeCmdArgs)),
+        None
+    )
 
 let compilingFsLib20Flag =
-    CompilerOption ("compiling-fslib-20", tagNone, OptionString (fun _ -> () ), None, None)
+    CompilerOption("compiling-fslib-20", tagNone, OptionString(fun _ -> ()), None, None)
 
 let compilingFsLib40Flag =
-    CompilerOption ("compiling-fslib-40", tagNone, OptionUnit (fun () -> ()), None, None)
+    CompilerOption("compiling-fslib-40", tagNone, OptionUnit(fun () -> ()), None, None)
 
 let compilingFsLibNoBigIntFlag =
-    CompilerOption ("compiling-fslib-nobigint", tagNone, OptionUnit (fun () -> () ), None, None)
+    CompilerOption("compiling-fslib-nobigint", tagNone, OptionUnit(fun () -> ()), None, None)
 
 let mlKeywordsFlag =
-    CompilerOption
-        ("ml-keywords", tagNone,
-         OptionUnit (fun () -> ()),
-         Some(DeprecatedCommandLineOptionNoDescription("--ml-keywords", rangeCmdArgs)), None)
+    CompilerOption(
+        "ml-keywords",
+        tagNone,
+        OptionUnit(fun () -> ()),
+        Some(DeprecatedCommandLineOptionNoDescription("--ml-keywords", rangeCmdArgs)),
+        None
+    )
 
 let gnuStyleErrorsFlag tcConfigB =
-    CompilerOption
-        ("gnu-style-errors", tagNone,
-         OptionUnit (fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Emacs),
-         Some(DeprecatedCommandLineOptionNoDescription("--gnu-style-errors", rangeCmdArgs)), None)
+    CompilerOption(
+        "gnu-style-errors",
+        tagNone,
+        OptionUnit(fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Emacs),
+        Some(DeprecatedCommandLineOptionNoDescription("--gnu-style-errors", rangeCmdArgs)),
+        None
+    )
 
 let deprecatedFlagsBoth tcConfigB =
     [
-      CompilerOption
-         ("light", tagNone,
-          OptionUnit (fun () -> tcConfigB.indentationAwareSyntax <- Some true),
-          Some(DeprecatedCommandLineOptionNoDescription("--light", rangeCmdArgs)), None)
-
-      CompilerOption
-         ("indentation-syntax", tagNone,
-          OptionUnit (fun () -> tcConfigB.indentationAwareSyntax <- Some true),
-          Some(DeprecatedCommandLineOptionNoDescription("--indentation-syntax", rangeCmdArgs)), None)
-
-      CompilerOption
-         ("no-indentation-syntax", tagNone,
-          OptionUnit (fun () -> tcConfigB.indentationAwareSyntax <- Some false),
-          Some(DeprecatedCommandLineOptionNoDescription("--no-indentation-syntax", rangeCmdArgs)), None)
+        CompilerOption(
+            "light",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.indentationAwareSyntax <- Some true),
+            Some(DeprecatedCommandLineOptionNoDescription("--light", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "indentation-syntax",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.indentationAwareSyntax <- Some true),
+            Some(DeprecatedCommandLineOptionNoDescription("--indentation-syntax", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-indentation-syntax",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.indentationAwareSyntax <- Some false),
+            Some(DeprecatedCommandLineOptionNoDescription("--no-indentation-syntax", rangeCmdArgs)),
+            None
+        )
     ]
 
 let deprecatedFlagsFsi tcConfigB = deprecatedFlagsBoth tcConfigB
 
 let deprecatedFlagsFsc tcConfigB =
-    deprecatedFlagsBoth tcConfigB @
-    [
-    cliRootFlag tcConfigB
-    CompilerOption
-       ("jit-optimize", tagNone,
-        OptionUnit (fun _ -> tcConfigB.optSettings <- { tcConfigB.optSettings with jitOptUser = Some true }),
-        Some(DeprecatedCommandLineOptionNoDescription("--jit-optimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("no-jit-optimize", tagNone,
-        OptionUnit (fun _ -> tcConfigB.optSettings <- { tcConfigB.optSettings with jitOptUser = Some false }),
-        Some(DeprecatedCommandLineOptionNoDescription("--no-jit-optimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("jit-tracking", tagNone,
-        OptionUnit (fun _ -> tcConfigB.jitTracking <- true ),
-        Some(DeprecatedCommandLineOptionNoDescription("--jit-tracking", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("no-jit-tracking", tagNone,
-        OptionUnit (fun _ -> tcConfigB.jitTracking <- false ),
-        Some(DeprecatedCommandLineOptionNoDescription("--no-jit-tracking", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("progress", tagNone,
-        OptionUnit (fun () -> progress <- true),
-        Some(DeprecatedCommandLineOptionNoDescription("--progress", rangeCmdArgs)), None)
-
-    compilingFsLibFlag tcConfigB
-    compilingFsLib20Flag
-    compilingFsLib40Flag
-    compilingFsLibNoBigIntFlag
-
-    CompilerOption
-       ("version", tagString,
-        OptionString (fun s -> tcConfigB.version <- VersionString s),
-        Some(DeprecatedCommandLineOptionNoDescription("--version", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("local-optimize", tagNone,
-        OptionUnit (fun _ -> tcConfigB.optSettings <- { tcConfigB.optSettings with localOptUser = Some true }),
-        Some(DeprecatedCommandLineOptionNoDescription("--local-optimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("no-local-optimize", tagNone,
-        OptionUnit (fun _ -> tcConfigB.optSettings <- { tcConfigB.optSettings with localOptUser = Some false }),
-        Some(DeprecatedCommandLineOptionNoDescription("--no-local-optimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("cross-optimize", tagNone,
-        OptionUnit (fun _ -> tcConfigB.optSettings <- { tcConfigB.optSettings with crossAssemblyOptimizationUser = Some true }),
-        Some(DeprecatedCommandLineOptionNoDescription("--cross-optimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("no-cross-optimize", tagNone,
-        OptionUnit (fun _ -> tcConfigB.optSettings <- { tcConfigB.optSettings with crossAssemblyOptimizationUser = Some false }),
-        Some(DeprecatedCommandLineOptionNoDescription("--no-cross-optimize", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("no-string-interning", tagNone,
-        OptionUnit (fun () -> tcConfigB.internConstantStrings <- false),
-        Some(DeprecatedCommandLineOptionNoDescription("--no-string-interning", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("statistics", tagNone,
-        OptionUnit (fun () -> tcConfigB.stats <- true),
-        Some(DeprecatedCommandLineOptionNoDescription("--statistics", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("generate-filter-blocks", tagNone,
-        OptionUnit (fun () -> tcConfigB.generateFilterBlocks <- true),
-        Some(DeprecatedCommandLineOptionNoDescription("--generate-filter-blocks", rangeCmdArgs)), None)
-
-    //CompilerOption
-    //    ("no-generate-filter-blocks", tagNone,
-    //     OptionUnit (fun () -> tcConfigB.generateFilterBlocks <- false),
-    //     Some(DeprecatedCommandLineOptionNoDescription("--generate-filter-blocks", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("max-errors", tagInt,
-        OptionInt (fun n -> tcConfigB.maxErrors <- n),
-        Some(DeprecatedCommandLineOptionSuggestAlternative("--max-errors", "--maxerrors", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("debug-file", tagNone,
-        OptionString (fun s -> tcConfigB.debugSymbolFile <- Some s),
-        Some(DeprecatedCommandLineOptionSuggestAlternative("--debug-file", "--pdb", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("no-debug-file", tagNone,
-        OptionUnit (fun () -> tcConfigB.debuginfo <- false),
-        Some(DeprecatedCommandLineOptionSuggestAlternative("--no-debug-file", "--debug-", rangeCmdArgs)), None)
-
-    CompilerOption
-       ("Ooff", tagNone,
-        OptionUnit (fun () -> SetOptimizeOff tcConfigB),
-        Some(DeprecatedCommandLineOptionSuggestAlternative("-Ooff", "--optimize-", rangeCmdArgs)), None)
-
-
-    CompilerOption
-       ("keycontainer", tagString,
-        OptionString(fun s ->
-            if FSharpEnvironment.isRunningOnCoreClr then error(Error(FSComp.SR.containerSigningUnsupportedOnThisPlatform(), rangeCmdArgs))
-            else tcConfigB.container <- Some s),
-            if FSharpEnvironment.isRunningOnCoreClr then None
-            else Some(DeprecatedCommandLineOptionSuggestAlternative("--keycontainer", "--keyfile", rangeCmdArgs))
-        ,None)
-
-    mlKeywordsFlag
-    gnuStyleErrorsFlag tcConfigB ]
-
+    deprecatedFlagsBoth tcConfigB
+    @ [
+        cliRootFlag tcConfigB
+        CompilerOption(
+            "jit-optimize",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        jitOptUser = Some true
+                    }),
+            Some(DeprecatedCommandLineOptionNoDescription("--jit-optimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-jit-optimize",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        jitOptUser = Some false
+                    }),
+            Some(DeprecatedCommandLineOptionNoDescription("--no-jit-optimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "jit-tracking",
+            tagNone,
+            OptionUnit(fun _ -> tcConfigB.jitTracking <- true),
+            Some(DeprecatedCommandLineOptionNoDescription("--jit-tracking", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-jit-tracking",
+            tagNone,
+            OptionUnit(fun _ -> tcConfigB.jitTracking <- false),
+            Some(DeprecatedCommandLineOptionNoDescription("--no-jit-tracking", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "progress",
+            tagNone,
+            OptionUnit(fun () -> progress <- true),
+            Some(DeprecatedCommandLineOptionNoDescription("--progress", rangeCmdArgs)),
+            None
+        )
+
+        compilingFsLibFlag tcConfigB
+        compilingFsLib20Flag
+        compilingFsLib40Flag
+        compilingFsLibNoBigIntFlag
+
+        CompilerOption(
+            "version",
+            tagString,
+            OptionString(fun s -> tcConfigB.version <- VersionString s),
+            Some(DeprecatedCommandLineOptionNoDescription("--version", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "local-optimize",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        localOptUser = Some true
+                    }),
+            Some(DeprecatedCommandLineOptionNoDescription("--local-optimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-local-optimize",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        localOptUser = Some false
+                    }),
+            Some(DeprecatedCommandLineOptionNoDescription("--no-local-optimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "cross-optimize",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        crossAssemblyOptimizationUser = Some true
+                    }),
+            Some(DeprecatedCommandLineOptionNoDescription("--cross-optimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-cross-optimize",
+            tagNone,
+            OptionUnit(fun _ ->
+                tcConfigB.optSettings <-
+                    { tcConfigB.optSettings with
+                        crossAssemblyOptimizationUser = Some false
+                    }),
+            Some(DeprecatedCommandLineOptionNoDescription("--no-cross-optimize", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-string-interning",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.internConstantStrings <- false),
+            Some(DeprecatedCommandLineOptionNoDescription("--no-string-interning", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "statistics",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.stats <- true),
+            Some(DeprecatedCommandLineOptionNoDescription("--statistics", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "generate-filter-blocks",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.generateFilterBlocks <- true),
+            Some(DeprecatedCommandLineOptionNoDescription("--generate-filter-blocks", rangeCmdArgs)),
+            None
+        )
+
+        //CompilerOption
+        //    ("no-generate-filter-blocks", tagNone,
+        //     OptionUnit (fun () -> tcConfigB.generateFilterBlocks <- false),
+        //     Some(DeprecatedCommandLineOptionNoDescription("--generate-filter-blocks", rangeCmdArgs)), None)
+
+        CompilerOption(
+            "max-errors",
+            tagInt,
+            OptionInt(fun n -> tcConfigB.maxErrors <- n),
+            Some(DeprecatedCommandLineOptionSuggestAlternative("--max-errors", "--maxerrors", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "debug-file",
+            tagNone,
+            OptionString(fun s -> tcConfigB.debugSymbolFile <- Some s),
+            Some(DeprecatedCommandLineOptionSuggestAlternative("--debug-file", "--pdb", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "no-debug-file",
+            tagNone,
+            OptionUnit(fun () -> tcConfigB.debuginfo <- false),
+            Some(DeprecatedCommandLineOptionSuggestAlternative("--no-debug-file", "--debug-", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "Ooff",
+            tagNone,
+            OptionUnit(fun () -> SetOptimizeOff tcConfigB),
+            Some(DeprecatedCommandLineOptionSuggestAlternative("-Ooff", "--optimize-", rangeCmdArgs)),
+            None
+        )
+
+        CompilerOption(
+            "keycontainer",
+            tagString,
+            OptionString(fun s ->
+                if FSharpEnvironment.isRunningOnCoreClr then
+                    error (Error(FSComp.SR.containerSigningUnsupportedOnThisPlatform (), rangeCmdArgs))
+                else
+                    tcConfigB.container <- Some s),
+            (if FSharpEnvironment.isRunningOnCoreClr then
+                 None
+             else
+                 Some(DeprecatedCommandLineOptionSuggestAlternative("--keycontainer", "--keyfile", rangeCmdArgs))),
+            None
+        )
+
+        mlKeywordsFlag
+        gnuStyleErrorsFlag tcConfigB
+    ]
 
 // OptionBlock: Miscellaneous options
 //-----------------------------------
 
 let DisplayBannerText tcConfigB =
-    if tcConfigB.showBanner then (
-        printfn "%s" tcConfigB.productNameForBannerText
-        printfn "%s" (FSComp.SR.optsCopyright())
-    )
+    if tcConfigB.showBanner then
+        (printfn "%s" tcConfigB.productNameForBannerText
+         printfn "%s" (FSComp.SR.optsCopyright ()))
 
 /// FSC only help. (FSI has it's own help function).
-let displayHelpFsc tcConfigB (blocks:CompilerOptionBlock list) =
+let displayHelpFsc tcConfigB (blocks: CompilerOptionBlock list) =
     DisplayBannerText tcConfigB
     PrintCompilerOptionBlocks blocks
     exit 0
@@ -1473,103 +1970,130 @@ let displayVersion tcConfigB =
     exit 0
 
 let miscFlagsBoth tcConfigB =
-    [   CompilerOption("nologo", tagNone, OptionUnit (fun () -> tcConfigB.showBanner <- false), None, Some (FSComp.SR.optsNologo()))
-        CompilerOption("version", tagNone, OptionUnit (fun () -> displayVersion tcConfigB), None, Some (FSComp.SR.optsVersion()))
+    [
+        CompilerOption("nologo", tagNone, OptionUnit(fun () -> tcConfigB.showBanner <- false), None, Some(FSComp.SR.optsNologo ()))
+        CompilerOption("version", tagNone, OptionUnit(fun () -> displayVersion tcConfigB), None, Some(FSComp.SR.optsVersion ()))
     ]
 
 let miscFlagsFsc tcConfigB =
-    miscFlagsBoth tcConfigB @
-    [   CompilerOption("help", tagNone, OptionHelp (fun blocks -> displayHelpFsc tcConfigB blocks), None, Some (FSComp.SR.optsHelp()))
-        CompilerOption("@", tagNone, OptionUnit ignore, None, Some (FSComp.SR.optsResponseFile()))
+    miscFlagsBoth tcConfigB
+    @ [
+        CompilerOption("help", tagNone, OptionHelp(fun blocks -> displayHelpFsc tcConfigB blocks), None, Some(FSComp.SR.optsHelp ()))
+        CompilerOption("@", tagNone, OptionUnit ignore, None, Some(FSComp.SR.optsResponseFile ()))
     ]
-let miscFlagsFsi tcConfigB = miscFlagsBoth tcConfigB
 
+let miscFlagsFsi tcConfigB = miscFlagsBoth tcConfigB
 
 // OptionBlock: Abbreviations of existing options
 //-----------------------------------------------
 
 let abbreviatedFlagsBoth tcConfigB =
     [
-        CompilerOption("d", tagString, OptionString (defineSymbol tcConfigB), None, Some(FSComp.SR.optsShortFormOf("--define")))
-        CompilerOption("O", tagNone, OptionSwitch (SetOptimizeSwitch tcConfigB), None, Some(FSComp.SR.optsShortFormOf("--optimize[+|-]")))
-        CompilerOption("g", tagNone, OptionSwitch (SetDebugSwitch tcConfigB None), None, Some(FSComp.SR.optsShortFormOf("--debug")))
-        CompilerOption("i", tagString, OptionUnit (fun () -> tcConfigB.printSignature <- true), None, Some(FSComp.SR.optsShortFormOf("--sig")))
-        CompilerOption("r", tagFile, OptionString (fun s -> tcConfigB.AddReferencedAssemblyByPath (rangeStartup, s)),
-            None, Some(FSComp.SR.optsShortFormOf("--reference")))
-        CompilerOption("I", tagDirList, OptionStringList (fun s -> tcConfigB.AddIncludePath (rangeStartup, s, tcConfigB.implicitIncludeDir)),
-            None, Some (FSComp.SR.optsShortFormOf("--lib")))
+        CompilerOption("d", tagString, OptionString(defineSymbol tcConfigB), None, Some(FSComp.SR.optsShortFormOf ("--define")))
+        CompilerOption("O", tagNone, OptionSwitch(SetOptimizeSwitch tcConfigB), None, Some(FSComp.SR.optsShortFormOf ("--optimize[+|-]")))
+        CompilerOption("g", tagNone, OptionSwitch(SetDebugSwitch tcConfigB None), None, Some(FSComp.SR.optsShortFormOf ("--debug")))
+        CompilerOption(
+            "i",
+            tagString,
+            OptionUnit(fun () -> tcConfigB.printSignature <- true),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--sig"))
+        )
+        CompilerOption(
+            "r",
+            tagFile,
+            OptionString(fun s -> tcConfigB.AddReferencedAssemblyByPath(rangeStartup, s)),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--reference"))
+        )
+        CompilerOption(
+            "I",
+            tagDirList,
+            OptionStringList(fun s -> tcConfigB.AddIncludePath(rangeStartup, s, tcConfigB.implicitIncludeDir)),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--lib"))
+        )
     ]
 
 let abbreviatedFlagsFsi tcConfigB = abbreviatedFlagsBoth tcConfigB
 
 let abbreviatedFlagsFsc tcConfigB =
-    abbreviatedFlagsBoth tcConfigB @
-    [   // FSC only abbreviated options
-        CompilerOption
-            ("o", tagString,
-             OptionString (setOutFileName tcConfigB), None,
-             Some(FSComp.SR.optsShortFormOf("--out")))
-
-        CompilerOption
-            ("a", tagString,
-             OptionUnit (fun () -> tcConfigB.target <- CompilerTarget.Dll), None,
-             Some(FSComp.SR.optsShortFormOf("--target library")))
+    abbreviatedFlagsBoth tcConfigB
+    @ [ // FSC only abbreviated options
+        CompilerOption("o", tagString, OptionString(setOutFileName tcConfigB), None, Some(FSComp.SR.optsShortFormOf ("--out")))
+
+        CompilerOption(
+            "a",
+            tagString,
+            OptionUnit(fun () -> tcConfigB.target <- CompilerTarget.Dll),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--target library"))
+        )
 
         // FSC help abbreviations. FSI has it's own help options...
-        CompilerOption
-           ("?", tagNone,
-            OptionHelp (fun blocks -> displayHelpFsc tcConfigB blocks), None,
-            Some(FSComp.SR.optsShortFormOf("--help")))
-
-        CompilerOption
-            ("help", tagNone,
-             OptionHelp (fun blocks -> displayHelpFsc tcConfigB blocks), None,
-             Some(FSComp.SR.optsShortFormOf("--help")))
-
-        CompilerOption
-            ("full-help", tagNone,
-             OptionHelp (fun blocks -> displayHelpFsc tcConfigB blocks), None,
-             Some(FSComp.SR.optsShortFormOf("--help")))
+        CompilerOption(
+            "?",
+            tagNone,
+            OptionHelp(fun blocks -> displayHelpFsc tcConfigB blocks),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--help"))
+        )
+
+        CompilerOption(
+            "help",
+            tagNone,
+            OptionHelp(fun blocks -> displayHelpFsc tcConfigB blocks),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--help"))
+        )
+
+        CompilerOption(
+            "full-help",
+            tagNone,
+            OptionHelp(fun blocks -> displayHelpFsc tcConfigB blocks),
+            None,
+            Some(FSComp.SR.optsShortFormOf ("--help"))
+        )
     ]
 
 let GetAbbrevFlagSet tcConfigB isFsc =
     let mutable argList: string list = []
+
     for c in ((if isFsc then abbreviatedFlagsFsc else abbreviatedFlagsFsi) tcConfigB) do
         match c with
-        | CompilerOption(arg, _, OptionString _, _, _)
-        | CompilerOption(arg, _, OptionStringList _, _, _) -> argList <- argList @ ["-"+arg;"/"+arg]
+        | CompilerOption (arg, _, OptionString _, _, _)
+        | CompilerOption (arg, _, OptionStringList _, _, _) -> argList <- argList @ [ "-" + arg; "/" + arg ]
         | _ -> ()
+
     Set.ofList argList
 
 // check for abbreviated options that accept spaces instead of colons, and replace the spaces
 // with colons when necessary
-let PostProcessCompilerArgs (abbrevArgs: string Set) (args: string []) =
+let PostProcessCompilerArgs (abbrevArgs: string Set) (args: string[]) =
     let mutable i = 0
     let mutable idx = 0
     let len = args.Length
     let mutable arga: string[] = Array.create len ""
 
     while i < len do
-        if not(abbrevArgs.Contains(args[i])) || i = (len - 1)  then
+        if not (abbrevArgs.Contains(args[i])) || i = (len - 1) then
             arga[idx] <- args[i]
-            i <- i+1
+            i <- i + 1
         else
-            arga[idx] <- args[i] + ":" + args[i+1]
+            arga[idx] <- args[i] + ":" + args[i + 1]
             i <- i + 2
+
         idx <- idx + 1
+
     Array.toList arga[0 .. (idx - 1)]
 
 // OptionBlock: QA options
 //------------------------
 
 let testingAndQAFlags _tcConfigB =
-  [
-    CompilerOption
-       ("dumpAllCommandLineOptions", tagNone,
-        OptionHelp(fun blocks -> DumpCompilerOptionBlocks blocks),
-        None, None) // "Command line options")
-  ]
-
+    [
+        CompilerOption("dumpAllCommandLineOptions", tagNone, OptionHelp(fun blocks -> DumpCompilerOptionBlocks blocks), None, None) // "Command line options")
+    ]
 
 // Core compiler options, overview
 //--------------------------------
@@ -1605,64 +2129,89 @@ let testingAndQAFlags _tcConfigB =
 
 /// The core/common options used by fsc.exe. [not currently extended by fsc.fs].
 let GetCoreFscCompilerOptions (tcConfigB: TcConfigBuilder) =
-  [ PublicOptions(FSComp.SR.optsHelpBannerOutputFiles(), outputFileFlagsFsc        tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerInputFiles(), inputFileFlagsFsc         tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerResources(), resourcesFlagsFsc         tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerCodeGen(), codeGenerationFlags false tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerErrsAndWarns(), errorsAndWarningsFlags    tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerLanguage(), languageFlags             tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerMisc(), miscFlagsFsc              tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerAdvanced(), advancedFlagsFsc tcConfigB)
-    PrivateOptions(List.concat              [ internalFlags           tcConfigB
-                                              abbreviatedFlagsFsc     tcConfigB
-                                              deprecatedFlagsFsc      tcConfigB
-                                              testingAndQAFlags       tcConfigB])
-  ]
+    [
+        PublicOptions(FSComp.SR.optsHelpBannerOutputFiles (), outputFileFlagsFsc tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerInputFiles (), inputFileFlagsFsc tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerResources (), resourcesFlagsFsc tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerCodeGen (), codeGenerationFlags false tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerErrsAndWarns (), errorsAndWarningsFlags tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerLanguage (), languageFlags tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerMisc (), miscFlagsFsc tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerAdvanced (), advancedFlagsFsc tcConfigB)
+        PrivateOptions(
+            List.concat
+                [
+                    internalFlags tcConfigB
+                    abbreviatedFlagsFsc tcConfigB
+                    deprecatedFlagsFsc tcConfigB
+                    testingAndQAFlags tcConfigB
+                ]
+        )
+    ]
 
 /// The core/common options used by the F# VS Language Service.
 /// Filter out OptionHelp which does printing then exit. This is not wanted in the context of VS!!
-let GetCoreServiceCompilerOptions (tcConfigB:TcConfigBuilder) =
-  let isHelpOption = function CompilerOption(_, _, OptionHelp _, _, _) -> true | _ -> false
-  List.map (FilterCompilerOptionBlock (isHelpOption >> not)) (GetCoreFscCompilerOptions tcConfigB)
+let GetCoreServiceCompilerOptions (tcConfigB: TcConfigBuilder) =
+    let isHelpOption =
+        function
+        | CompilerOption (_, _, OptionHelp _, _, _) -> true
+        | _ -> false
+
+    List.map (FilterCompilerOptionBlock(isHelpOption >> not)) (GetCoreFscCompilerOptions tcConfigB)
 
 /// The core/common options used by fsi.exe. [note, some additional options are added in fsi.fs].
 let GetCoreFsiCompilerOptions (tcConfigB: TcConfigBuilder) =
-  [ PublicOptions(FSComp.SR.optsHelpBannerOutputFiles(), outputFileFlagsFsi       tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerInputFiles(), inputFileFlagsFsi        tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerResources(), resourcesFlagsFsi        tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerCodeGen(), codeGenerationFlags true tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerErrsAndWarns(), errorsAndWarningsFlags   tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerLanguage(), languageFlags            tcConfigB)
-    // Note: no HTML block for fsi.exe
-    PublicOptions(FSComp.SR.optsHelpBannerMisc(), miscFlagsFsi            tcConfigB)
-    PublicOptions(FSComp.SR.optsHelpBannerAdvanced(), advancedFlagsFsi        tcConfigB)
-    PrivateOptions(List.concat              [ internalFlags           tcConfigB
-                                              abbreviatedFlagsFsi     tcConfigB
-                                              deprecatedFlagsFsi      tcConfigB
-                                              testingAndQAFlags       tcConfigB])
-  ]
-
-let ApplyCommandLineArgs(tcConfigB: TcConfigBuilder, sourceFiles: string list, argv) =
+    [
+        PublicOptions(FSComp.SR.optsHelpBannerOutputFiles (), outputFileFlagsFsi tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerInputFiles (), inputFileFlagsFsi tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerResources (), resourcesFlagsFsi tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerCodeGen (), codeGenerationFlags true tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerErrsAndWarns (), errorsAndWarningsFlags tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerLanguage (), languageFlags tcConfigB)
+        // Note: no HTML block for fsi.exe
+        PublicOptions(FSComp.SR.optsHelpBannerMisc (), miscFlagsFsi tcConfigB)
+        PublicOptions(FSComp.SR.optsHelpBannerAdvanced (), advancedFlagsFsi tcConfigB)
+        PrivateOptions(
+            List.concat
+                [
+                    internalFlags tcConfigB
+                    abbreviatedFlagsFsi tcConfigB
+                    deprecatedFlagsFsi tcConfigB
+                    testingAndQAFlags tcConfigB
+                ]
+        )
+    ]
+
+let ApplyCommandLineArgs (tcConfigB: TcConfigBuilder, sourceFiles: string list, argv) =
     try
         let sourceFilesAcc = ResizeArray sourceFiles
-        let collect name = if not (FileSystemUtils.isDll name) then sourceFilesAcc.Add name
+
+        let collect name =
+            if not (FileSystemUtils.isDll name) then
+                sourceFilesAcc.Add name
+
         ParseCompilerOptions(collect, GetCoreServiceCompilerOptions tcConfigB, argv)
         ResizeArray.toList sourceFilesAcc
     with e ->
         errorRecovery e range0
         sourceFiles
 
-
 //----------------------------------------------------------------------------
 // PrintWholeAssemblyImplementation
 //----------------------------------------------------------------------------
 
 let mutable showTermFileCount = 0
-let PrintWholeAssemblyImplementation g (tcConfig:TcConfig) outfile header expr =
+
+let PrintWholeAssemblyImplementation g (tcConfig: TcConfig) outfile header expr =
     if tcConfig.showTerms then
         if tcConfig.writeTermsToFiles then
             let fileName = outfile + ".terms"
-            use f = FileSystem.OpenFileForWriteShim(fileName + "-" + string showTermFileCount + "-" + header, FileMode.Create).GetWriter()
+
+            use f =
+                FileSystem
+                    .OpenFileForWriteShim(fileName + "-" + string showTermFileCount + "-" + header, FileMode.Create)
+                    .GetWriter()
+
             showTermFileCount <- showTermFileCount + 1
             LayoutRender.outL f (Display.squashTo 192 (DebugPrint.implFilesL g expr))
         else
@@ -1676,7 +2225,8 @@ let PrintWholeAssemblyImplementation g (tcConfig:TcConfig) outfile header expr =
 
 let mutable tPrev = None
 let mutable nPrev = None
-let ReportTime (tcConfig:TcConfig) descr =
+
+let ReportTime (tcConfig: TcConfig) descr =
 
     match nPrev with
     | None -> ()
@@ -1686,87 +2236,105 @@ let ReportTime (tcConfig:TcConfig) descr =
             Console.ReadLine() |> ignore
         // Intentionally putting this right after the pause so a debugger can be attached.
         match tcConfig.simulateException with
-        | Some("fsc-oom") -> raise(OutOfMemoryException())
-        | Some("fsc-an") -> raise(ArgumentNullException("simulated"))
-        | Some("fsc-invop") -> raise(InvalidOperationException())
-        | Some("fsc-av") -> raise(AccessViolationException())
-        | Some("fsc-aor") -> raise(ArgumentOutOfRangeException())
-        | Some("fsc-dv0") -> raise(DivideByZeroException())
-        | Some("fsc-nfn") -> raise(NotFiniteNumberException())
-        | Some("fsc-oe") -> raise(OverflowException())
-        | Some("fsc-atmm") -> raise(ArrayTypeMismatchException())
-        | Some("fsc-bif") -> raise(BadImageFormatException())
-        | Some("fsc-knf") -> raise(System.Collections.Generic.KeyNotFoundException())
-        | Some("fsc-ior") -> raise(IndexOutOfRangeException())
-        | Some("fsc-ic") -> raise(InvalidCastException())
-        | Some("fsc-ip") -> raise(InvalidProgramException())
-        | Some("fsc-ma") -> raise(MemberAccessException())
-        | Some("fsc-ni") -> raise(NotImplementedException())
-        | Some("fsc-nr") -> raise(NullReferenceException())
-        | Some("fsc-oc") -> raise(OperationCanceledException())
-        | Some("fsc-fail") -> failwith "simulated"
+        | Some ("fsc-oom") -> raise (OutOfMemoryException())
+        | Some ("fsc-an") -> raise (ArgumentNullException("simulated"))
+        | Some ("fsc-invop") -> raise (InvalidOperationException())
+        | Some ("fsc-av") -> raise (AccessViolationException())
+        | Some ("fsc-aor") -> raise (ArgumentOutOfRangeException())
+        | Some ("fsc-dv0") -> raise (DivideByZeroException())
+        | Some ("fsc-nfn") -> raise (NotFiniteNumberException())
+        | Some ("fsc-oe") -> raise (OverflowException())
+        | Some ("fsc-atmm") -> raise (ArrayTypeMismatchException())
+        | Some ("fsc-bif") -> raise (BadImageFormatException())
+        | Some ("fsc-knf") -> raise (System.Collections.Generic.KeyNotFoundException())
+        | Some ("fsc-ior") -> raise (IndexOutOfRangeException())
+        | Some ("fsc-ic") -> raise (InvalidCastException())
+        | Some ("fsc-ip") -> raise (InvalidProgramException())
+        | Some ("fsc-ma") -> raise (MemberAccessException())
+        | Some ("fsc-ni") -> raise (NotImplementedException())
+        | Some ("fsc-nr") -> raise (NullReferenceException())
+        | Some ("fsc-oc") -> raise (OperationCanceledException())
+        | Some ("fsc-fail") -> failwith "simulated"
         | _ -> ()
 
-
-
-
     if (tcConfig.showTimes || verbose) then
         // Note that timing calls are relatively expensive on the startup path so we don't
         // make this call unless showTimes has been turned on.
-        let timeNow = System.Diagnostics.Process.GetCurrentProcess().UserProcessorTime.TotalSeconds
+        let timeNow =
+            System.Diagnostics.Process.GetCurrentProcess().UserProcessorTime.TotalSeconds
+
         let maxGen = GC.MaxGeneration
-        let gcNow = [| for i in 0 .. maxGen -> GC.CollectionCount i |]
+        let gcNow = [| for i in 0..maxGen -> GC.CollectionCount i |]
         let ptime = System.Diagnostics.Process.GetCurrentProcess()
-        let wsNow = ptime.WorkingSet64/1000000L
+        let wsNow = ptime.WorkingSet64 / 1000000L
 
         match tPrev, nPrev with
-        | Some (timePrev, gcPrev:int []), Some prevDescr ->
-            let spanGC = [| for i in 0 .. maxGen -> GC.CollectionCount i - gcPrev[i] |]
-            dprintf "TIME: %4.1f Delta: %4.1f Mem: %3d"
-                timeNow (timeNow - timePrev)
-                wsNow
-            dprintf " G0: %3d G1: %2d G2: %2d [%s]\n"
-                spanGC[Operators.min 0 maxGen] spanGC[Operators.min 1 maxGen] spanGC[Operators.min 2 maxGen]
+        | Some (timePrev, gcPrev: int[]), Some prevDescr ->
+            let spanGC = [| for i in 0..maxGen -> GC.CollectionCount i - gcPrev[i] |]
+            dprintf "TIME: %4.1f Delta: %4.1f Mem: %3d" timeNow (timeNow - timePrev) wsNow
+
+            dprintf
+                " G0: %3d G1: %2d G2: %2d [%s]\n"
+                spanGC[Operators.min 0 maxGen]
+                spanGC[Operators.min 1 maxGen]
+                spanGC[Operators.min 2 maxGen]
                 prevDescr
 
         | _ -> ()
-        tPrev <- Some (timeNow, gcNow)
+
+        tPrev <- Some(timeNow, gcNow)
 
     nPrev <- Some descr
 
-let ignoreFailureOnMono1_1_16 f = try f() with _ -> ()
+let ignoreFailureOnMono1_1_16 f =
+    try
+        f ()
+    with _ ->
+        ()
 
 let foreBackColor () =
     try
         let c = Console.ForegroundColor // may fail, perhaps on Mac, and maybe ForegroundColor is Black
         let b = Console.BackgroundColor // may fail, perhaps on Mac, and maybe BackgroundColor is White
-        Some (c, b)
-    with
-        e -> None
+        Some(c, b)
+    with e ->
+        None
 
 let DoWithColor newColor f =
-    match enableConsoleColoring, foreBackColor() with
+    match enableConsoleColoring, foreBackColor () with
     | false, _
     | true, None ->
         // could not get console colours, so no attempt to change colours, can not set them back
-        f()
+        f ()
     | true, Some (c, _) ->
         try
             ignoreFailureOnMono1_1_16 (fun () -> Console.ForegroundColor <- newColor)
-            f()
+            f ()
         finally
             ignoreFailureOnMono1_1_16 (fun () -> Console.ForegroundColor <- c)
 
 let DoWithDiagnosticColor severity f =
-    match foreBackColor() with
-    | None -> f()
+    match foreBackColor () with
+    | None -> f ()
     | Some (_, backColor) ->
-        let infoColor = if backColor = ConsoleColor.White then ConsoleColor.Blue else ConsoleColor.Green
-        let warnColor = if backColor = ConsoleColor.White then ConsoleColor.DarkBlue else ConsoleColor.Cyan
+        let infoColor =
+            if backColor = ConsoleColor.White then
+                ConsoleColor.Blue
+            else
+                ConsoleColor.Green
+
+        let warnColor =
+            if backColor = ConsoleColor.White then
+                ConsoleColor.DarkBlue
+            else
+                ConsoleColor.Cyan
+
         let errorColor = ConsoleColor.Red
+
         let color =
             match severity with
             | FSharpDiagnosticSeverity.Error -> errorColor
             | FSharpDiagnosticSeverity.Warning -> warnColor
             | _ -> infoColor
+
         DoWithColor color f
diff --git a/src/Compiler/Driver/CreateILModule.fs b/src/Compiler/Driver/CreateILModule.fs
index 5cb00a7f215..9039453478c 100644
--- a/src/Compiler/Driver/CreateILModule.fs
+++ b/src/Compiler/Driver/CreateILModule.fs
@@ -31,31 +31,32 @@ module AttributeHelpers =
 
     /// Try to find an attribute that takes a string argument
     let TryFindStringAttribute (g: TcGlobals) attrib attribs =
-      match g.TryFindSysAttrib attrib with
-      | None -> None
-      | Some attribRef ->
-        match TryFindFSharpAttribute g attribRef attribs with
-        | Some (Attrib(_, _, [ AttribStringArg s ], _, _, _, _))  -> Some s
-        | _ -> None
+        match g.TryFindSysAttrib attrib with
+        | None -> None
+        | Some attribRef ->
+            match TryFindFSharpAttribute g attribRef attribs with
+            | Some (Attrib (_, _, [ AttribStringArg s ], _, _, _, _)) -> Some s
+            | _ -> None
 
     let TryFindIntAttribute (g: TcGlobals) attrib attribs =
-      match g.TryFindSysAttrib attrib with
-      | None -> None
-      | Some attribRef ->
-        match TryFindFSharpAttribute g attribRef attribs with
-        | Some (Attrib(_, _, [ AttribInt32Arg i ], _, _, _, _)) -> Some i
-        | _ -> None
+        match g.TryFindSysAttrib attrib with
+        | None -> None
+        | Some attribRef ->
+            match TryFindFSharpAttribute g attribRef attribs with
+            | Some (Attrib (_, _, [ AttribInt32Arg i ], _, _, _, _)) -> Some i
+            | _ -> None
 
     let TryFindBoolAttribute (g: TcGlobals) attrib attribs =
-      match g.TryFindSysAttrib attrib with
-      | None -> None
-      | Some attribRef ->
-        match TryFindFSharpAttribute g attribRef attribs with
-        | Some (Attrib(_, _, [ AttribBoolArg p ], _, _, _, _)) -> Some p
-        | _ -> None
+        match g.TryFindSysAttrib attrib with
+        | None -> None
+        | Some attribRef ->
+            match TryFindFSharpAttribute g attribRef attribs with
+            | Some (Attrib (_, _, [ AttribBoolArg p ], _, _, _, _)) -> Some p
+            | _ -> None
 
     let (|ILVersion|_|) (versionString: string) =
-        try Some (parseILVersion versionString)
+        try
+            Some(parseILVersion versionString)
         with e ->
             None
 
@@ -67,20 +68,25 @@ module AttributeHelpers =
 type StrongNameSigningInfo = StrongNameSigningInfo of delaysign: bool * publicsign: bool * signer: string option * container: string option
 
 /// Validate the attributes and configuration settings used to perform strong-name signing
-let ValidateKeySigningAttributes (tcConfig : TcConfig, tcGlobals, topAttrs) =
-    let delaySignAttrib = AttributeHelpers.TryFindBoolAttribute tcGlobals "System.Reflection.AssemblyDelaySignAttribute" topAttrs.assemblyAttrs
-    let signerAttrib = AttributeHelpers.TryFindStringAttribute tcGlobals "System.Reflection.AssemblyKeyFileAttribute" topAttrs.assemblyAttrs
-    let containerAttrib = AttributeHelpers.TryFindStringAttribute tcGlobals "System.Reflection.AssemblyKeyNameAttribute" topAttrs.assemblyAttrs
+let ValidateKeySigningAttributes (tcConfig: TcConfig, tcGlobals, topAttrs) =
+    let delaySignAttrib =
+        AttributeHelpers.TryFindBoolAttribute tcGlobals "System.Reflection.AssemblyDelaySignAttribute" topAttrs.assemblyAttrs
+
+    let signerAttrib =
+        AttributeHelpers.TryFindStringAttribute tcGlobals "System.Reflection.AssemblyKeyFileAttribute" topAttrs.assemblyAttrs
+
+    let containerAttrib =
+        AttributeHelpers.TryFindStringAttribute tcGlobals "System.Reflection.AssemblyKeyNameAttribute" topAttrs.assemblyAttrs
 
     // if delaySign is set via an attribute, validate that it wasn't set via an option
     let delaysign =
         match delaySignAttrib with
         | Some delaysign ->
-          if tcConfig.delaysign then
-            warning(Error(FSComp.SR.fscDelaySignWarning(), rangeCmdArgs))
-            tcConfig.delaysign
-          else
-            delaysign
+            if tcConfig.delaysign then
+                warning (Error(FSComp.SR.fscDelaySignWarning (), rangeCmdArgs))
+                tcConfig.delaysign
+            else
+                delaysign
         | _ -> tcConfig.delaysign
 
     // if signer is set via an attribute, validate that it wasn't set via an option
@@ -88,7 +94,7 @@ let ValidateKeySigningAttributes (tcConfig : TcConfig, tcGlobals, topAttrs) =
         match signerAttrib with
         | Some signer ->
             if tcConfig.signer.IsSome && tcConfig.signer <> Some signer then
-                warning(Error(FSComp.SR.fscKeyFileWarning(), rangeCmdArgs))
+                warning (Error(FSComp.SR.fscKeyFileWarning (), rangeCmdArgs))
                 tcConfig.signer
             else
                 Some signer
@@ -101,35 +107,35 @@ let ValidateKeySigningAttributes (tcConfig : TcConfig, tcGlobals, topAttrs) =
         match containerAttrib with
         | Some container ->
             if not FSharpEnvironment.isRunningOnCoreClr then
-                warning(Error(FSComp.SR.containerDeprecated(), rangeCmdArgs))
+                warning (Error(FSComp.SR.containerDeprecated (), rangeCmdArgs))
+
             if tcConfig.container.IsSome && tcConfig.container <> Some container then
-              warning(Error(FSComp.SR.fscKeyNameWarning(), rangeCmdArgs))
-              tcConfig.container
+                warning (Error(FSComp.SR.fscKeyNameWarning (), rangeCmdArgs))
+                tcConfig.container
             else
-              Some container
+                Some container
         | None -> tcConfig.container
 
-    StrongNameSigningInfo (delaysign, tcConfig.publicsign, signer, container)
+    StrongNameSigningInfo(delaysign, tcConfig.publicsign, signer, container)
 
 /// Get the object used to perform strong-name signing
 let GetStrongNameSigner signingInfo =
-    let (StrongNameSigningInfo(delaysign, publicsign, signer, container)) = signingInfo
+    let (StrongNameSigningInfo (delaysign, publicsign, signer, container)) = signingInfo
     // REVIEW: favor the container over the key file - C# appears to do this
     match container with
-    | Some container ->
-        Some (ILStrongNameSigner.OpenKeyContainer container)
+    | Some container -> Some(ILStrongNameSigner.OpenKeyContainer container)
     | None ->
         match signer with
         | None -> None
         | Some s ->
             try
                 if publicsign || delaysign then
-                    Some (ILStrongNameSigner.OpenPublicKeyOptions s publicsign)
+                    Some(ILStrongNameSigner.OpenPublicKeyOptions s publicsign)
                 else
-                    Some (ILStrongNameSigner.OpenKeyPairFile s)
+                    Some(ILStrongNameSigner.OpenKeyPairFile s)
             with _ ->
                 // Note :: don't use errorR here since we really want to fail and not produce a binary
-                error(Error(FSComp.SR.fscKeyFileCouldNotBeOpened s, rangeCmdArgs))
+                error (Error(FSComp.SR.fscKeyFileCouldNotBeOpened s, rangeCmdArgs))
 
 //----------------------------------------------------------------------------
 // Building the contents of the finalized IL module
@@ -138,65 +144,87 @@ let GetStrongNameSigner signingInfo =
 module MainModuleBuilder =
 
     let injectedCompatTypes =
-      set [ "System.Tuple`1"
-            "System.Tuple`2"
-            "System.Tuple`3"
-            "System.Tuple`4"
-            "System.Tuple`5"
-            "System.Tuple`6"
-            "System.Tuple`7"
-            "System.Tuple`8"
-            "System.ITuple"
-            "System.Tuple"
-            "System.Collections.IStructuralComparable"
-            "System.Collections.IStructuralEquatable" ]
+        set
+            [
+                "System.Tuple`1"
+                "System.Tuple`2"
+                "System.Tuple`3"
+                "System.Tuple`4"
+                "System.Tuple`5"
+                "System.Tuple`6"
+                "System.Tuple`7"
+                "System.Tuple`8"
+                "System.ITuple"
+                "System.Tuple"
+                "System.Collections.IStructuralComparable"
+                "System.Collections.IStructuralEquatable"
+            ]
 
     let typesForwardedToMscorlib =
-      set [ "System.AggregateException"
-            "System.Threading.CancellationTokenRegistration"
-            "System.Threading.CancellationToken"
-            "System.Threading.CancellationTokenSource"
-            "System.Lazy`1"
-            "System.IObservable`1"
-            "System.IObserver`1" ]
-
-    let typesForwardedToSystemNumerics =
-      set [ "System.Numerics.BigInteger" ]
+        set
+            [
+                "System.AggregateException"
+                "System.Threading.CancellationTokenRegistration"
+                "System.Threading.CancellationToken"
+                "System.Threading.CancellationTokenSource"
+                "System.Lazy`1"
+                "System.IObservable`1"
+                "System.IObserver`1"
+            ]
+
+    let typesForwardedToSystemNumerics = set [ "System.Numerics.BigInteger" ]
 
     let createMscorlibExportList (tcGlobals: TcGlobals) =
-      // We want to write forwarders out for all injected types except for System.ITuple, which is internal
-      // Forwarding System.ITuple will cause FxCop failures on 4.0
-      Set.union (Set.filter (fun t -> t <> "System.ITuple") injectedCompatTypes) typesForwardedToMscorlib |>
-          Seq.map (fun t -> mkTypeForwarder tcGlobals.ilg.primaryAssemblyScopeRef t (mkILNestedExportedTypes List.empty) (mkILCustomAttrs List.empty) ILTypeDefAccess.Public )
-          |> Seq.toList
+        // We want to write forwarders out for all injected types except for System.ITuple, which is internal
+        // Forwarding System.ITuple will cause FxCop failures on 4.0
+        Set.union (Set.filter (fun t -> t <> "System.ITuple") injectedCompatTypes) typesForwardedToMscorlib
+        |> Seq.map (fun t ->
+            mkTypeForwarder
+                tcGlobals.ilg.primaryAssemblyScopeRef
+                t
+                (mkILNestedExportedTypes List.empty)
+                (mkILCustomAttrs List.empty)
+                ILTypeDefAccess.Public)
+        |> Seq.toList
 
     let createSystemNumericsExportList (tcConfig: TcConfig) (tcImports: TcImports) =
         let refNumericsDllName =
-            if (tcConfig.primaryAssembly.Name = "mscorlib") then "System.Numerics"
-            else "System.Runtime.Numerics"
+            if (tcConfig.primaryAssembly.Name = "mscorlib") then
+                "System.Numerics"
+            else
+                "System.Runtime.Numerics"
+
         let numericsAssemblyRef =
-            match tcImports.GetImportedAssemblies() |> List.tryFind(fun a -> a.FSharpViewOfMetadata.AssemblyName = refNumericsDllName) with
+            match tcImports.GetImportedAssemblies()
+                  |> List.tryFind (fun a -> a.FSharpViewOfMetadata.AssemblyName = refNumericsDllName)
+                with
             | Some asm ->
                 match asm.ILScopeRef with
                 | ILScopeRef.Assembly aref -> Some aref
                 | _ -> None
             | None -> None
+
         match numericsAssemblyRef with
         | Some aref ->
-            let systemNumericsAssemblyRef = ILAssemblyRef.Create(refNumericsDllName, aref.Hash, aref.PublicKey, aref.Retargetable, aref.Version, aref.Locale)
-            typesForwardedToSystemNumerics |>
-                Seq.map (fun t ->
-                            { ScopeRef = ILScopeRef.Assembly systemNumericsAssemblyRef
-                              Name = t
-                              Attributes = enum(0x00200000) ||| TypeAttributes.Public
-                              Nested = mkILNestedExportedTypes []
-                              CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
-                              MetadataIndex = NoMetadataIdx }) |>
-                Seq.toList
+            let systemNumericsAssemblyRef =
+                ILAssemblyRef.Create(refNumericsDllName, aref.Hash, aref.PublicKey, aref.Retargetable, aref.Version, aref.Locale)
+
+            typesForwardedToSystemNumerics
+            |> Seq.map (fun t ->
+                {
+                    ScopeRef = ILScopeRef.Assembly systemNumericsAssemblyRef
+                    Name = t
+                    Attributes = enum (0x00200000) ||| TypeAttributes.Public
+                    Nested = mkILNestedExportedTypes []
+                    CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+                    MetadataIndex = NoMetadataIdx
+                })
+            |> Seq.toList
         | None -> []
 
     let ComputeILFileVersion findStringAttr (assemblyVersion: ILVersionInfo) =
         let attrName = "System.Reflection.AssemblyFileVersionAttribute"
+
         match findStringAttr attrName with
         | None -> assemblyVersion
         | Some (AttributeHelpers.ILVersion v) -> v
@@ -206,69 +234,108 @@ module MainModuleBuilder =
 
     let ComputeProductVersion findStringAttr (fileVersion: ILVersionInfo) =
         let attrName = "System.Reflection.AssemblyInformationalVersionAttribute"
-        let toDotted (version: ILVersionInfo) = sprintf "%d.%d.%d.%d" version.Major version.Minor version.Build version.Revision
+
+        let toDotted (version: ILVersionInfo) =
+            sprintf "%d.%d.%d.%d" version.Major version.Minor version.Build version.Revision
+
         match findStringAttr attrName with
-        | None | Some "" -> fileVersion |> toDotted
+        | None
+        | Some "" -> fileVersion |> toDotted
         | Some (AttributeHelpers.ILVersion v) -> v |> toDotted
         | Some v ->
             // Warning will be reported by CheckExpressions.fs
             v
 
     let ConvertProductVersionToILVersionInfo (version: string) : ILVersionInfo =
-        let parseOrZero i (v:string) =
+        let parseOrZero i (v: string) =
             let v =
                 // When i = 3 then this is the 4th part of the version.  The last part of the version can be trailed by any characters so we trim them off
                 if i <> 3 then
                     v
                 else
                     ((false, ""), v)
-                    ||> Seq.fold(fun (finished, v) c ->
+                    ||> Seq.fold (fun (finished, v) c ->
                         match finished with
                         | false when Char.IsDigit(c) -> false, v + c.ToString()
                         | _ -> true, v)
                     |> snd
+
             match UInt16.TryParse v with
             | true, i -> i
             | false, _ -> 0us
+
         let validParts =
-            version.Split('.')
-            |> Array.mapi(fun i v -> parseOrZero i v)
-            |> Seq.toList
-        match validParts @ [0us; 0us; 0us; 0us] with
+            version.Split('.') |> Array.mapi (fun i v -> parseOrZero i v) |> Seq.toList
+
+        match validParts @ [ 0us; 0us; 0us; 0us ] with
         | major :: minor :: build :: rev :: _ -> ILVersionInfo(major, minor, build, rev)
         | x -> failwithf "error converting product version '%s' to binary, tried '%A' " version x
 
-
     let CreateMainModule
-            (ctok, tcConfig: TcConfig, tcGlobals, tcImports: TcImports,
-             pdbfile, assemblyName, outfile, topAttrs,
-             sigDataAttributes: ILAttribute list, sigDataResources: ILResource list, optDataResources: ILResource list,
-             codegenResults, assemVerFromAttrib, metadataVersion, secDecls) =
+        (
+            ctok,
+            tcConfig: TcConfig,
+            tcGlobals,
+            tcImports: TcImports,
+            pdbfile,
+            assemblyName,
+            outfile,
+            topAttrs,
+            sigDataAttributes: ILAttribute list,
+            sigDataResources: ILResource list,
+            optDataResources: ILResource list,
+            codegenResults,
+            assemVerFromAttrib,
+            metadataVersion,
+            secDecls
+        ) =
 
         RequireCompilationThread ctok
+
         let ilTypeDefs =
             //let topTypeDef = mkILTypeDefForGlobalFunctions tcGlobals.ilg (mkILMethods [], emptyILFields)
             mkILTypeDefs codegenResults.ilTypeDefs
 
         let mainModule =
-            let hashAlg = AttributeHelpers.TryFindIntAttribute tcGlobals "System.Reflection.AssemblyAlgorithmIdAttribute" topAttrs.assemblyAttrs
-            let locale = AttributeHelpers.TryFindStringAttribute tcGlobals "System.Reflection.AssemblyCultureAttribute" topAttrs.assemblyAttrs
-            let flags =  match AttributeHelpers.TryFindIntAttribute tcGlobals "System.Reflection.AssemblyFlagsAttribute" topAttrs.assemblyAttrs with | Some f -> f | _ -> 0x0
+            let hashAlg =
+                AttributeHelpers.TryFindIntAttribute tcGlobals "System.Reflection.AssemblyAlgorithmIdAttribute" topAttrs.assemblyAttrs
+
+            let locale =
+                AttributeHelpers.TryFindStringAttribute tcGlobals "System.Reflection.AssemblyCultureAttribute" topAttrs.assemblyAttrs
+
+            let flags =
+                match AttributeHelpers.TryFindIntAttribute tcGlobals "System.Reflection.AssemblyFlagsAttribute" topAttrs.assemblyAttrs with
+                | Some f -> f
+                | _ -> 0x0
 
             // You're only allowed to set a locale if the assembly is a library
             if (locale <> None && locale.Value <> "") && tcConfig.target <> CompilerTarget.Dll then
-              error(Error(FSComp.SR.fscAssemblyCultureAttributeError(), rangeCmdArgs))
+                error (Error(FSComp.SR.fscAssemblyCultureAttributeError (), rangeCmdArgs))
 
             // Add the type forwarders to any .NET DLL post-.NET-2.0, to give binary compatibility
             let exportedTypesList =
                 if tcConfig.compilingFSharpCore then
-                   List.append (createMscorlibExportList tcGlobals) (createSystemNumericsExportList tcConfig tcImports)
+                    List.append (createMscorlibExportList tcGlobals) (createSystemNumericsExportList tcConfig tcImports)
                 else
                     []
 
             let ilModuleName = GetGeneratedILModuleName tcConfig.target assemblyName
-            let isDLL = (tcConfig.target = CompilerTarget.Dll || tcConfig.target = CompilerTarget.Module)
-            mkILSimpleModule assemblyName ilModuleName isDLL tcConfig.subsystemVersion tcConfig.useHighEntropyVA ilTypeDefs hashAlg locale flags (mkILExportedTypes exportedTypesList) metadataVersion
+
+            let isDLL =
+                (tcConfig.target = CompilerTarget.Dll || tcConfig.target = CompilerTarget.Module)
+
+            mkILSimpleModule
+                assemblyName
+                ilModuleName
+                isDLL
+                tcConfig.subsystemVersion
+                tcConfig.useHighEntropyVA
+                ilTypeDefs
+                hashAlg
+                locale
+                flags
+                (mkILExportedTypes exportedTypesList)
+                metadataVersion
 
         let disableJitOptimizations = not tcConfig.optSettings.JitOptimizationsEnabled
 
@@ -277,74 +344,130 @@ module MainModuleBuilder =
         let reflectedDefinitionAttrs, reflectedDefinitionResources =
             codegenResults.quotationResourceInfo
             |> List.map (fun (referencedTypeDefs, reflectedDefinitionBytes) ->
-                let reflectedDefinitionResourceName = QuotationPickler.SerializedReflectedDefinitionsResourceNameBase+"-"+assemblyName+"-"+string(newUnique())+"-"+string(hash reflectedDefinitionBytes)
+                let reflectedDefinitionResourceName =
+                    QuotationPickler.SerializedReflectedDefinitionsResourceNameBase
+                    + "-"
+                    + assemblyName
+                    + "-"
+                    + string (newUnique ())
+                    + "-"
+                    + string (hash reflectedDefinitionBytes)
+
                 let reflectedDefinitionAttrs =
-                    let qf = QuotationTranslator.QuotationGenerationScope.ComputeQuotationFormat tcGlobals
+                    let qf =
+                        QuotationTranslator.QuotationGenerationScope.ComputeQuotationFormat tcGlobals
+
                     if qf.SupportsDeserializeEx then
-                        [ mkCompilationMappingAttrForQuotationResource tcGlobals (reflectedDefinitionResourceName, referencedTypeDefs) ]
+                        [
+                            mkCompilationMappingAttrForQuotationResource tcGlobals (reflectedDefinitionResourceName, referencedTypeDefs)
+                        ]
                     else
-                        [  ]
+                        []
+
                 let reflectedDefinitionResource =
-                  { Name=reflectedDefinitionResourceName
-                    Location = ILResourceLocation.Local(ByteStorage.FromByteArray(reflectedDefinitionBytes))
-                    Access= ILResourceAccess.Public
-                    CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
-                    MetadataIndex = NoMetadataIdx }
+                    {
+                        Name = reflectedDefinitionResourceName
+                        Location = ILResourceLocation.Local(ByteStorage.FromByteArray(reflectedDefinitionBytes))
+                        Access = ILResourceAccess.Public
+                        CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+                        MetadataIndex = NoMetadataIdx
+                    }
+
                 reflectedDefinitionAttrs, reflectedDefinitionResource)
             |> List.unzip
             |> (fun (attrs, resource) -> List.concat attrs, resource)
 
         let manifestAttrs =
             mkILCustomAttrs
-                 [ if not tcConfig.internConstantStrings then
-                       yield mkILCustomAttribute (tcGlobals.FindSysILTypeRef "System.Runtime.CompilerServices.CompilationRelaxationsAttribute", [tcGlobals.ilg.typ_Int32], [ILAttribElem.Int32( 8)], [])
-                   yield! sigDataAttributes
-                   yield! codegenResults.ilAssemAttrs
-
-                   if Option.isSome pdbfile then
-                       yield (tcGlobals.mkDebuggableAttributeV2 (tcConfig.jitTracking, tcConfig.ignoreSymbolStoreSequencePoints, disableJitOptimizations, false (* enableEnC *) ))
-                   yield! reflectedDefinitionAttrs ]
+                [
+                    if not tcConfig.internConstantStrings then
+                        mkILCustomAttribute (
+                            tcGlobals.FindSysILTypeRef "System.Runtime.CompilerServices.CompilationRelaxationsAttribute",
+                            [ tcGlobals.ilg.typ_Int32 ],
+                            [ ILAttribElem.Int32(8) ],
+                            []
+                        )
+                    yield! sigDataAttributes
+                    yield! codegenResults.ilAssemAttrs
+
+                    if Option.isSome pdbfile then
+                        tcGlobals.mkDebuggableAttributeV2 (
+                            tcConfig.jitTracking,
+                            tcConfig.ignoreSymbolStoreSequencePoints,
+                            disableJitOptimizations,
+                            false (* enableEnC *)
+                        )
+                    yield! reflectedDefinitionAttrs
+                ]
 
         // Make the manifest of the assembly
         let manifest =
-             if tcConfig.target = CompilerTarget.Module then None else
-             let man = mainModule.ManifestOfAssembly
-             let ver =
-                 match assemVerFromAttrib with
-                 | None -> tcVersion
-                 | Some v -> v
-             Some { man with Version= Some ver
-                             CustomAttrsStored = storeILCustomAttrs manifestAttrs
-                             DisableJitOptimizations=disableJitOptimizations
-                             JitTracking= tcConfig.jitTracking
-                             IgnoreSymbolStoreSequencePoints = tcConfig.ignoreSymbolStoreSequencePoints
-                             SecurityDeclsStored=storeILSecurityDecls secDecls }
+            if tcConfig.target = CompilerTarget.Module then
+                None
+            else
+                let man = mainModule.ManifestOfAssembly
+
+                let ver =
+                    match assemVerFromAttrib with
+                    | None -> tcVersion
+                    | Some v -> v
+
+                Some
+                    { man with
+                        Version = Some ver
+                        CustomAttrsStored = storeILCustomAttrs manifestAttrs
+                        DisableJitOptimizations = disableJitOptimizations
+                        JitTracking = tcConfig.jitTracking
+                        IgnoreSymbolStoreSequencePoints = tcConfig.ignoreSymbolStoreSequencePoints
+                        SecurityDeclsStored = storeILSecurityDecls secDecls
+                    }
 
         let resources =
-          mkILResources
-            [ for file in tcConfig.embedResources do
-                 let name, bytes, pub =
-                         let file, name, pub = TcConfigBuilder.SplitCommandLineResourceInfo file
-                         let file = tcConfig.ResolveSourceFile(rangeStartup, file, tcConfig.implicitIncludeDir)
-                         let bytes = FileSystem.OpenFileForReadShim(file).ReadAllBytes()
-                         name, bytes, pub
-                 yield { Name=name
-                         // TODO: We probably can directly convert ByteMemory to ByteStorage, without reading all bytes.
-                         Location=ILResourceLocation.Local(ByteStorage.FromByteArray(bytes))
-                         Access=pub
-                         CustomAttrsStored=storeILCustomAttrs emptyILCustomAttrs
-                         MetadataIndex = NoMetadataIdx }
-
-              yield! reflectedDefinitionResources
-              yield! sigDataResources
-              yield! optDataResources
-              for ri in tcConfig.linkResources do
-                 let file, name, pub = TcConfigBuilder.SplitCommandLineResourceInfo ri
-                 yield { Name=name
-                         Location=ILResourceLocation.File(ILModuleRef.Create(name=file, hasMetadata=false, hash=Some (sha1HashBytes (FileSystem.OpenFileForReadShim(file).ReadAllBytes()))), 0)
-                         Access=pub
-                         CustomAttrsStored=storeILCustomAttrs emptyILCustomAttrs
-                         MetadataIndex = NoMetadataIdx } ]
+            mkILResources
+                [
+                    for file in tcConfig.embedResources do
+                        let name, bytes, pub =
+                            let file, name, pub = TcConfigBuilder.SplitCommandLineResourceInfo file
+
+                            let file =
+                                tcConfig.ResolveSourceFile(rangeStartup, file, tcConfig.implicitIncludeDir)
+
+                            let bytes = FileSystem.OpenFileForReadShim(file).ReadAllBytes()
+                            name, bytes, pub
+
+                        {
+                            Name = name
+                            // TODO: We probably can directly convert ByteMemory to ByteStorage, without reading all bytes.
+                            Location = ILResourceLocation.Local(ByteStorage.FromByteArray(bytes))
+                            Access = pub
+                            CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+                            MetadataIndex = NoMetadataIdx
+                        }
+
+                    yield! reflectedDefinitionResources
+                    yield! sigDataResources
+                    yield! optDataResources
+                    for ri in tcConfig.linkResources do
+                        let file, name, pub = TcConfigBuilder.SplitCommandLineResourceInfo ri
+
+                        let location =
+                            ILResourceLocation.File(
+                                ILModuleRef.Create(
+                                    name = file,
+                                    hasMetadata = false,
+                                    hash = Some(sha1HashBytes (FileSystem.OpenFileForReadShim(file).ReadAllBytes()))
+                                ),
+                                0
+                            )
+
+                        {
+                            Name = name
+                            Location = location
+                            Access = pub
+                            CustomAttrsStored = storeILCustomAttrs emptyILCustomAttrs
+                            MetadataIndex = NoMetadataIdx
+                        }
+                ]
 
         let assemblyVersion =
             match tcConfig.version with
@@ -362,7 +485,7 @@ module MainModuleBuilder =
             | Some assemblyVersion ->
                 let FindAttribute key attrib =
                     match findAttribute attrib with
-                    | Some text  -> [(key, text)]
+                    | Some text -> [ (key, text) ]
                     | _ -> []
 
                 let fileVersionInfo = ComputeILFileVersion findAttribute assemblyVersion
@@ -370,26 +493,33 @@ module MainModuleBuilder =
                 let productVersionString = ComputeProductVersion findAttribute fileVersionInfo
 
                 let stringFileInfo =
-                     // 000004b0:
-                     // Specifies an 8-digit hexadecimal number stored as a Unicode string. The
-                     // four most significant digits represent the language identifier. The four least
-                     // significant digits represent the code page for which the data is formatted.
-                     // Each Microsoft Standard Language identifier contains two parts: the low-order 10 bits
-                     // specify the major language, and the high-order 6 bits specify the sublanguage.
-                     // For a table of valid identifiers see Language Identifiers.                                           //
-                     // see e.g. http://msdn.microsoft.com/en-us/library/aa912040.aspx 0000 is neutral and 04b0(hex)=1252(dec) is the code page.
-                      [ ("000004b0", [ yield ("Assembly Version", (sprintf "%d.%d.%d.%d" assemblyVersion.Major assemblyVersion.Minor assemblyVersion.Build assemblyVersion.Revision))
-                                       yield ("FileVersion", (sprintf "%d.%d.%d.%d" fileVersionInfo.Major fileVersionInfo.Minor fileVersionInfo.Build fileVersionInfo.Revision))
-                                       yield ("ProductVersion", productVersionString)
-                                       match tcConfig.outputFile with
-                                       | Some f -> yield ("OriginalFilename", Path.GetFileName f)
-                                       | None -> ()
-                                       yield! FindAttribute "Comments" "System.Reflection.AssemblyDescriptionAttribute"
-                                       yield! FindAttribute "FileDescription" "System.Reflection.AssemblyTitleAttribute"
-                                       yield! FindAttribute "ProductName" "System.Reflection.AssemblyProductAttribute"
-                                       yield! FindAttribute "CompanyName" "System.Reflection.AssemblyCompanyAttribute"
-                                       yield! FindAttribute "LegalCopyright" "System.Reflection.AssemblyCopyrightAttribute"
-                                       yield! FindAttribute "LegalTrademarks" "System.Reflection.AssemblyTrademarkAttribute" ]) ]
+                    // 000004b0:
+                    // Specifies an 8-digit hexadecimal number stored as a Unicode string. The
+                    // four most significant digits represent the language identifier. The four least
+                    // significant digits represent the code page for which the data is formatted.
+                    // Each Microsoft Standard Language identifier contains two parts: the low-order 10 bits
+                    // specify the major language, and the high-order 6 bits specify the sublanguage.
+                    // For a table of valid identifiers see Language Identifiers.                                           //
+                    // see e.g. http://msdn.microsoft.com/en-us/library/aa912040.aspx 0000 is neutral and 04b0(hex)=1252(dec) is the code page.
+                    [
+                        ("000004b0",
+                         [
+                             ("Assembly Version",
+                              $"%d{assemblyVersion.Major}.%d{assemblyVersion.Minor}.%d{assemblyVersion.Build}.%d{assemblyVersion.Revision}")
+                             ("FileVersion",
+                              $"%d{fileVersionInfo.Major}.%d{fileVersionInfo.Minor}.%d{fileVersionInfo.Build}.%d{fileVersionInfo.Revision}")
+                             ("ProductVersion", productVersionString)
+                             match tcConfig.outputFile with
+                             | Some f -> ("OriginalFilename", Path.GetFileName f)
+                             | None -> ()
+                             yield! FindAttribute "Comments" "System.Reflection.AssemblyDescriptionAttribute"
+                             yield! FindAttribute "FileDescription" "System.Reflection.AssemblyTitleAttribute"
+                             yield! FindAttribute "ProductName" "System.Reflection.AssemblyProductAttribute"
+                             yield! FindAttribute "CompanyName" "System.Reflection.AssemblyCompanyAttribute"
+                             yield! FindAttribute "LegalCopyright" "System.Reflection.AssemblyCopyrightAttribute"
+                             yield! FindAttribute "LegalTrademarks" "System.Reflection.AssemblyTrademarkAttribute"
+                         ])
+                    ]
 
                 // These entries listed in the MSDN documentation as "standard" string entries are not yet settable
 
@@ -421,7 +551,7 @@ module MainModuleBuilder =
                 // Either high-order or low-order word can be zero, indicating that
                 // the file is language or code page independent. If the Var structure is
                 // omitted, the file will be interpreted as both language and code page independent. "
-                let varFileInfo = [ (0x0, 0x04b0)  ]
+                let varFileInfo = [ (0x0, 0x04b0) ]
 
                 let fixedFileInfo =
                     let dwFileFlagsMask = 0x3f // REVIEW: HARDWIRED
@@ -430,71 +560,127 @@ module MainModuleBuilder =
                     let dwFileType = 0x01 // REVIEW: HARDWIRED
                     let dwFileSubtype = 0x00 // REVIEW: HARDWIRED
                     let lwFileDate = 0x00L // REVIEW: HARDWIRED
-                    (fileVersionInfo, productVersionString |> ConvertProductVersionToILVersionInfo, dwFileFlagsMask, dwFileFlags, dwFileOS, dwFileType, dwFileSubtype, lwFileDate)
+                    let ilProductVersion = productVersionString |> ConvertProductVersionToILVersionInfo
+                    (fileVersionInfo, ilProductVersion, dwFileFlagsMask, dwFileFlags, dwFileOS, dwFileType, dwFileSubtype, lwFileDate)
 
                 let vsVersionInfoResource =
                     VersionResourceFormat.VS_VERSION_INFO_RESOURCE(fixedFileInfo, stringFileInfo, varFileInfo)
 
                 let resource =
-                    [| yield! ResFileFormat.ResFileHeader()
-                       yield! vsVersionInfoResource |]
+                    [| yield! ResFileFormat.ResFileHeader(); yield! vsVersionInfoResource |]
 
                 [ resource ]
 
         // a user cannot specify both win32res and win32manifest
-        if not(tcConfig.win32manifest = "") && not(tcConfig.win32res = "") then
-            error(Error(FSComp.SR.fscTwoResourceManifests(), rangeCmdArgs))
+        if not (tcConfig.win32manifest = "") && not (tcConfig.win32res = "") then
+            error (Error(FSComp.SR.fscTwoResourceManifests (), rangeCmdArgs))
 
         let win32Manifest =
             // use custom manifest if provided
-            if not(tcConfig.win32manifest = "") then tcConfig.win32manifest
+            if not (tcConfig.win32manifest = "") then
+                tcConfig.win32manifest
 
             // don't embed a manifest if target is not an exe, if manifest is specifically excluded, if another native resource is being included, or if running on mono
-            elif not(tcConfig.target.IsExe) || not(tcConfig.includewin32manifest) || not(tcConfig.win32res = "") || runningOnMono then ""
+            elif not (tcConfig.target.IsExe)
+                 || not (tcConfig.includewin32manifest)
+                 || not (tcConfig.win32res = "")
+                 || runningOnMono then
+                ""
             // otherwise, include the default manifest
             else
-                let path=Path.Combine(FSharpEnvironment.getFSharpCompilerLocation(), @"default.win32manifest")
+                let path =
+                    Path.Combine(FSharpEnvironment.getFSharpCompilerLocation (), @"default.win32manifest")
+
                 if FileSystem.FileExistsShim(path) then
                     path
                 else
                     let path = Path.Combine(AppContext.BaseDirectory, @"default.win32manifest")
+
                     if FileSystem.FileExistsShim(path) then
                         path
                     else
                         Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), @"default.win32manifest")
 
         let nativeResources =
-            [ for av in assemblyVersionResources assemblyVersion do
-                  yield ILNativeResource.Out av
-              if not(tcConfig.win32res = "") then
-                  yield ILNativeResource.Out (FileSystem.OpenFileForReadShim(tcConfig.win32res).ReadAllBytes())
-              if tcConfig.includewin32manifest && not(win32Manifest = "") && not runningOnMono then
-                  yield  ILNativeResource.Out [| yield! ResFileFormat.ResFileHeader()
-                                                 yield! (ManifestResourceFormat.VS_MANIFEST_RESOURCE((FileSystem.OpenFileForReadShim(win32Manifest).ReadAllBytes()), tcConfig.target = CompilerTarget.Dll)) |]
-              if tcConfig.win32res = "" && tcConfig.win32icon <> "" && tcConfig.target <> CompilerTarget.Dll then
-                  use ms = new MemoryStream()
-                  use iconStream = FileSystem.OpenFileForReadShim(tcConfig.win32icon)
-                  Win32ResourceConversions.AppendIconToResourceStream(ms, iconStream)
-                  yield ILNativeResource.Out [| yield! ResFileFormat.ResFileHeader()
-                                                yield! ms.ToArray() |] ]
-
+            [
+                for av in assemblyVersionResources assemblyVersion do
+                    ILNativeResource.Out av
+                if not (tcConfig.win32res = "") then
+                    ILNativeResource.Out(FileSystem.OpenFileForReadShim(tcConfig.win32res).ReadAllBytes())
+                if tcConfig.includewin32manifest && not (win32Manifest = "") && not runningOnMono then
+                    ILNativeResource.Out
+                        [|
+                            yield! ResFileFormat.ResFileHeader()
+                            yield!
+                                (ManifestResourceFormat.VS_MANIFEST_RESOURCE(
+                                    (FileSystem.OpenFileForReadShim(win32Manifest).ReadAllBytes()),
+                                    tcConfig.target = CompilerTarget.Dll
+                                ))
+                        |]
+                if tcConfig.win32res = ""
+                   && tcConfig.win32icon <> ""
+                   && tcConfig.target <> CompilerTarget.Dll then
+                    use ms = new MemoryStream()
+                    use iconStream = FileSystem.OpenFileForReadShim(tcConfig.win32icon)
+                    Win32ResourceConversions.AppendIconToResourceStream(ms, iconStream)
+                    ILNativeResource.Out [| yield! ResFileFormat.ResFileHeader(); yield! ms.ToArray() |]
+            ]
+
+        let name =
+            if tcConfig.target = CompilerTarget.Module then
+                FileSystemUtils.fileNameOfPath outfile
+            else
+                mainModule.Name
+
+        let imageBase =
+            match tcConfig.baseAddress with
+            | None -> 0x00400000l
+            | Some b -> b
+
+        let isDLL =
+            (tcConfig.target = CompilerTarget.Dll || tcConfig.target = CompilerTarget.Module)
+
+        let is32bit =
+            match tcConfig.platform with
+            | Some X86
+            | Some ARM -> true
+            | _ -> false
+
+        let is64bit =
+            match tcConfig.platform with
+            | Some AMD64
+            | Some IA64
+            | Some ARM64 -> true
+            | _ -> false
+
+        let is32BitPreferred =
+            if tcConfig.prefer32Bit && not tcConfig.target.IsExe then
+                (error (Error(FSComp.SR.invalidPlatformTarget (), rangeCmdArgs)))
+            else
+                tcConfig.prefer32Bit
+
+        let attribs =
+            storeILCustomAttrs (
+                mkILCustomAttrs
+                    [
+                        if tcConfig.target = CompilerTarget.Module then
+                            yield! sigDataAttributes
+                        yield! codegenResults.ilNetModuleAttrs
+                    ]
+            )
         // Add attributes, version number, resources etc.
-        {mainModule with
-              StackReserveSize = tcConfig.stackReserveSize
-              Name = (if tcConfig.target = CompilerTarget.Module then FileSystemUtils.fileNameOfPath outfile else mainModule.Name)
-              SubSystemFlags = (if tcConfig.target = CompilerTarget.WinExe then 2 else 3)
-              Resources= resources
-              ImageBase = (match tcConfig.baseAddress with None -> 0x00400000l | Some b -> b)
-              IsDLL=(tcConfig.target = CompilerTarget.Dll || tcConfig.target=CompilerTarget.Module)
-              Platform = tcConfig.platform
-              Is32Bit=(match tcConfig.platform with Some X86 | Some ARM -> true | _ -> false)
-              Is64Bit=(match tcConfig.platform with Some AMD64 | Some IA64 | Some ARM64 -> true | _ -> false)
-              Is32BitPreferred = if tcConfig.prefer32Bit && not tcConfig.target.IsExe then (error(Error(FSComp.SR.invalidPlatformTarget(), rangeCmdArgs))) else tcConfig.prefer32Bit
-              CustomAttrsStored=
-                  storeILCustomAttrs
-                    (mkILCustomAttrs
-                      [ if tcConfig.target = CompilerTarget.Module then
-                           yield! sigDataAttributes
-                        yield! codegenResults.ilNetModuleAttrs ])
-              NativeResources=nativeResources
-              Manifest = manifest }
+        { mainModule with
+            StackReserveSize = tcConfig.stackReserveSize
+            Name = name
+            SubSystemFlags = (if tcConfig.target = CompilerTarget.WinExe then 2 else 3)
+            Resources = resources
+            ImageBase = imageBase
+            IsDLL = isDLL
+            Platform = tcConfig.platform
+            Is32Bit = is32bit
+            Is64Bit = is64bit
+            Is32BitPreferred = is32BitPreferred
+            CustomAttrsStored = attribs
+            NativeResources = nativeResources
+            Manifest = manifest
+        }
diff --git a/src/Compiler/Driver/FxResolver.fs b/src/Compiler/Driver/FxResolver.fs
index ff75963c159..2d1eb4ce7ae 100644
--- a/src/Compiler/Driver/FxResolver.fs
+++ b/src/Compiler/Driver/FxResolver.fs
@@ -19,9 +19,9 @@ open FSharp.Compiler.Text
 open FSharp.Compiler.IO
 
 type internal FxResolverLockToken() =
-   interface LockToken
+    interface LockToken
 
-type internal FxResolverLock = Lock   
+type internal FxResolverLock = Lock
 
 /// Resolves the references for a chosen or currently-executing framework, for
 ///   - script execution
@@ -30,19 +30,28 @@ type internal FxResolverLock = Lock
 ///   - out-of-project sources editing
 ///   - default references for fsc.exe
 ///   - default references for fsi.exe
-type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdkRefs: bool, isInteractive: bool, rangeForErrors: range, sdkDirOverride: string option) =
+type internal FxResolver
+    (
+        assumeDotNetFramework: bool,
+        projectDir: string,
+        useSdkRefs: bool,
+        isInteractive: bool,
+        rangeForErrors: range,
+        sdkDirOverride: string option
+    ) =
 
     let fxlock = FxResolverLock()
 
     static let RequireFxResolverLock (_fxtok: FxResolverLockToken, _thingProtected: 'T) = ()
 
-    /// We only try once for each directory (cleared on solution unload) to prevent conditions where 
+    /// We only try once for each directory (cleared on solution unload) to prevent conditions where
     /// we repeatedly try to run dotnet.exe on every keystroke for a script
-    static let desiredDotNetSdkVersionForDirectoryCache = ConcurrentDictionary>()
+    static let desiredDotNetSdkVersionForDirectoryCache =
+        ConcurrentDictionary>()
 
     // Execute the process pathToExe passing the arguments: arguments with the working directory: workingDir timeout after timeout milliseconds -1 = wait forever
     // returns exit code, stdio and stderr as string arrays
-    let executeProcess pathToExe arguments (workingDir:string option) timeout =
+    let executeProcess pathToExe arguments (workingDir: string option) timeout =
         if not (String.IsNullOrEmpty pathToExe) then
             let errorsList = ResizeArray()
             let outputList = ResizeArray()
@@ -52,44 +61,50 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
             let outputDataReceived (message: string MaybeNull) =
                 match message with
                 | Null -> ()
-                | NonNull message ->
-                    lock outputlock (fun () -> outputList.Add(message))
+                | NonNull message -> lock outputlock (fun () -> outputList.Add(message))
 
             let errorDataReceived (message: string MaybeNull) =
                 match message with
                 | Null -> ()
-                | NonNull message ->
-                    lock errorslock (fun () -> errorsList.Add(message))
+                | NonNull message -> lock errorslock (fun () -> errorsList.Add(message))
 
             let psi = ProcessStartInfo()
             psi.FileName <- pathToExe
-            if workingDir.IsSome  then
+
+            if workingDir.IsSome then
                 psi.WorkingDirectory <- workingDir.Value
+
             psi.RedirectStandardOutput <- true
             psi.RedirectStandardError <- true
             psi.Arguments <- arguments
             psi.CreateNoWindow <- true
-            psi.EnvironmentVariables.Remove("MSBuildSDKsPath")          // Host can sometimes add this, and it can break things
+            psi.EnvironmentVariables.Remove("MSBuildSDKsPath") // Host can sometimes add this, and it can break things
             psi.UseShellExecute <- false
 
             use p = new Process()
             p.StartInfo <- psi
 
             p.OutputDataReceived.Add(fun a -> outputDataReceived a.Data)
-            p.ErrorDataReceived.Add(fun a ->  errorDataReceived a.Data)
+            p.ErrorDataReceived.Add(fun a -> errorDataReceived a.Data)
 
             if p.Start() then
                 p.BeginOutputReadLine()
                 p.BeginErrorReadLine()
-                if not(p.WaitForExit(timeout)) then
+
+                if not (p.WaitForExit(timeout)) then
                     // Timed out resolving throw a diagnostic.
                     raise (TimeoutException(sprintf "Timeout executing command '%s' '%s'" psi.FileName psi.Arguments))
                 else
                     p.WaitForExit()
 #if DEBUG
             if workingDir.IsSome then
-                FileSystem.OpenFileForWriteShim(Path.Combine(workingDir.Value, "StandardOutput.txt")).WriteAllLines(outputList)
-                FileSystem.OpenFileForWriteShim(Path.Combine(workingDir.Value, "StandardError.txt")).WriteAllLines(errorsList)
+                FileSystem
+                    .OpenFileForWriteShim(Path.Combine(workingDir.Value, "StandardOutput.txt"))
+                    .WriteAllLines(outputList)
+
+                FileSystem
+                    .OpenFileForWriteShim(Path.Combine(workingDir.Value, "StandardError.txt"))
+                    .WriteAllLines(errorsList)
 #endif
             p.ExitCode, outputList.ToArray(), errorsList.ToArray()
         else
@@ -97,146 +112,184 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
 
     /// Find the relevant sdk version by running `dotnet --version` in the script/project location,
     /// taking into account any global.json
-    let tryGetDesiredDotNetSdkVersionForDirectoryInfo() =
-        desiredDotNetSdkVersionForDirectoryCache.GetOrAdd(projectDir, (fun _ -> 
-            match getDotnetHostPath() with
-            | Some dotnetHostPath ->
-                try
-                    let workingDir =
-                        if FileSystem.DirectoryExistsShim(projectDir) then
-                            Some projectDir
+    let tryGetDesiredDotNetSdkVersionForDirectoryInfo () =
+        desiredDotNetSdkVersionForDirectoryCache.GetOrAdd(
+            projectDir,
+            (fun _ ->
+                match getDotnetHostPath () with
+                | Some dotnetHostPath ->
+                    try
+                        let workingDir =
+                            if FileSystem.DirectoryExistsShim(projectDir) then
+                                Some projectDir
+                            else
+                                None
+
+                        let exitCode, output, errors =
+                            executeProcess dotnetHostPath "--version" workingDir 30000
+
+                        if exitCode <> 0 then
+                            Result.Error(
+                                Error(
+                                    FSComp.SR.scriptSdkNotDetermined (dotnetHostPath, projectDir, (errors |> String.concat "\n"), exitCode),
+                                    rangeForErrors
+                                )
+                            )
                         else
-                            None
-                    let exitCode, output, errors = executeProcess dotnetHostPath "--version" workingDir 30000
-                    if exitCode <> 0 then
-                        Result.Error (Error(FSComp.SR.scriptSdkNotDetermined(dotnetHostPath, projectDir, (errors |> String.concat "\n"), exitCode), rangeForErrors))
-                    else
-                        Result.Ok (output |> String.concat "\n")
-                with err ->
-                    Result.Error (Error(FSComp.SR.scriptSdkNotDetermined(dotnetHostPath, projectDir, err.Message, 1), rangeForErrors))
-            | _ -> Result.Error (Error(FSComp.SR.scriptSdkNotDeterminedNoHost(), rangeForErrors))))
+                            Result.Ok(output |> String.concat "\n")
+                    with err ->
+                        Result.Error(Error(FSComp.SR.scriptSdkNotDetermined (dotnetHostPath, projectDir, err.Message, 1), rangeForErrors))
+                | _ -> Result.Error(Error(FSComp.SR.scriptSdkNotDeterminedNoHost (), rangeForErrors)))
+        )
 
     // We need to make sure the warning gets replayed each time, despite the lazy computations
     // To do this we pass it back as data and eventually replay it at the entry points to FxResolver.
-    let tryGetDesiredDotNetSdkVersionForDirectory() =
-        match tryGetDesiredDotNetSdkVersionForDirectoryInfo() with
+    let tryGetDesiredDotNetSdkVersionForDirectory () =
+        match tryGetDesiredDotNetSdkVersionForDirectoryInfo () with
         | Result.Ok res -> Some res, []
-        | Result.Error exn -> None, [exn]
+        | Result.Error exn -> None, [ exn ]
 
     // This is used to replay the warnings generated in the function above.
     // It should not be used under the lazy on-demand computations in this type, nor should the warnings be explicitly ignored
     let replayWarnings (res, warnings: exn list) =
-        for exn in warnings do warning exn
+        for exn in warnings do
+            warning exn
+
         res
 
     /// Compute the .NET Core SDK directory relevant to projectDir, used to infer the default target framework assemblies.
     ///
     /// On-demand because (a) some FxResolver are ephemeral (b) we want to avoid recomputation
     let trySdkDir =
-      lazy
-        // This path shouldn't be used with reflective processes
-        assert not isInteractive
-        match assumeDotNetFramework with
-        | true -> None, []
-        | _ when not useSdkRefs -> None, []
-        | _ ->
-        match sdkDirOverride with
-        | Some sdkDir -> Some sdkDir, []
-        | None ->
-            let sdksDir = 
-                match getDotnetHostDirectory() with
-                | Some dotnetDir ->
-                    let candidate = FileSystem.GetFullPathShim(Path.Combine(dotnetDir, "sdk"))
-                    if FileSystem.DirectoryExistsShim(candidate) then Some candidate else None
-                | None -> None
-
-            match sdksDir with
-            | Some sdksDir ->
-                // Find the sdk version by running `dotnet --version` in the script/project location
-                let desiredSdkVer, warnings = tryGetDesiredDotNetSdkVersionForDirectory()
-
-                let sdkDir =
-                    DirectoryInfo(sdksDir).GetDirectories()
-                    // Filter to the version reported by `dotnet --version` in the location, if that succeeded
-                    // If it didn't succeed we will revert back to implementation assemblies, but still need an SDK
-                    // to use, so we find the SDKs by looking for dotnet.runtimeconfig.json
-                    |> Array.filter (fun di ->
-                        match desiredSdkVer with
-                        | None -> FileSystem.FileExistsShim(Path.Combine(di.FullName,"dotnet.runtimeconfig.json"))
-                        | Some v -> di.Name = v)
-                    |> Array.sortBy (fun di -> di.FullName)
-                    |> Array.tryLast
-                    |> Option.map (fun di -> di.FullName)
-                sdkDir, warnings
+        lazy
+            // This path shouldn't be used with reflective processes
+            assert not isInteractive
+
+            match assumeDotNetFramework with
+            | true -> None, []
+            | _ when not useSdkRefs -> None, []
             | _ ->
-                None, []
+                match sdkDirOverride with
+                | Some sdkDir -> Some sdkDir, []
+                | None ->
+                    let sdksDir =
+                        match getDotnetHostDirectory () with
+                        | Some dotnetDir ->
+                            let candidate = FileSystem.GetFullPathShim(Path.Combine(dotnetDir, "sdk"))
 
-    let tryGetSdkDir() = trySdkDir.Force()
+                            if FileSystem.DirectoryExistsShim(candidate) then
+                                Some candidate
+                            else
+                                None
+                        | None -> None
+
+                    match sdksDir with
+                    | Some sdksDir ->
+                        // Find the sdk version by running `dotnet --version` in the script/project location
+                        let desiredSdkVer, warnings = tryGetDesiredDotNetSdkVersionForDirectory ()
+
+                        let sdkDir =
+                            DirectoryInfo(sdksDir).GetDirectories()
+                            // Filter to the version reported by `dotnet --version` in the location, if that succeeded
+                            // If it didn't succeed we will revert back to implementation assemblies, but still need an SDK
+                            // to use, so we find the SDKs by looking for dotnet.runtimeconfig.json
+                            |> Array.filter (fun di ->
+                                match desiredSdkVer with
+                                | None -> FileSystem.FileExistsShim(Path.Combine(di.FullName, "dotnet.runtimeconfig.json"))
+                                | Some v -> di.Name = v)
+                            |> Array.sortBy (fun di -> di.FullName)
+                            |> Array.tryLast
+                            |> Option.map (fun di -> di.FullName)
+
+                        sdkDir, warnings
+                    | _ -> None, []
+
+    let tryGetSdkDir () = trySdkDir.Force()
 
     /// Get the framework implementation directory of the currently running process
-    let getRunningImplementationAssemblyDir() =
+    let getRunningImplementationAssemblyDir () =
         let fileName = Path.GetDirectoryName(typeof.Assembly.Location)
-        if String.IsNullOrWhiteSpace fileName then getFSharpCompilerLocation() else fileName
+
+        if String.IsNullOrWhiteSpace fileName then
+            getFSharpCompilerLocation ()
+        else
+            fileName
 
     // Compute the framework implementation directory, either of the selected SDK or the currently running process as a backup
     // F# interactive/reflective scenarios use the implementation directory of the currently running process
     //
     // On-demand because (a) some FxResolver are ephemeral (b) we want to avoid recomputation
     let implementationAssemblyDir =
-      lazy
-        if isInteractive then
-            getRunningImplementationAssemblyDir(), []
-        else
-            let sdkDir, warnings = tryGetSdkDir()
-            match sdkDir with
-            | Some dir ->
-                try
-                    let dotnetConfigFile = Path.Combine(dir, "dotnet.runtimeconfig.json")
-                    use stream = FileSystem.OpenFileForReadShim(dotnetConfigFile)
-                    let dotnetConfig = stream.ReadAllText()
-                    let pattern = "\"version\": \""
-                    let startPos = dotnetConfig.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) + pattern.Length
-                    let endPos = dotnetConfig.IndexOf("\"", startPos)
-                    let ver = dotnetConfig[startPos..endPos-1]
-                    let path = FileSystem.GetFullPathShim(Path.Combine(dir, "..", "..", "shared", "Microsoft.NETCore.App", ver))
-                    if FileSystem.DirectoryExistsShim(path) then
-                        path, warnings
-                    else
-                        getRunningImplementationAssemblyDir(), warnings
-                with e ->
-                    let warn = Error(FSComp.SR.scriptSdkNotDeterminedUnexpected(e.Message), rangeForErrors)
-                    let path = getRunningImplementationAssemblyDir()
-                    path, [warn]
-            | _ ->
-                let path = getRunningImplementationAssemblyDir()
-                path, []
+        lazy
+            if isInteractive then
+                getRunningImplementationAssemblyDir (), []
+            else
+                let sdkDir, warnings = tryGetSdkDir ()
+
+                match sdkDir with
+                | Some dir ->
+                    try
+                        let dotnetConfigFile = Path.Combine(dir, "dotnet.runtimeconfig.json")
+                        use stream = FileSystem.OpenFileForReadShim(dotnetConfigFile)
+                        let dotnetConfig = stream.ReadAllText()
+                        let pattern = "\"version\": \""
+
+                        let startPos =
+                            dotnetConfig.IndexOf(pattern, StringComparison.OrdinalIgnoreCase)
+                            + pattern.Length
+
+                        let endPos = dotnetConfig.IndexOf("\"", startPos)
+                        let ver = dotnetConfig[startPos .. endPos - 1]
 
-    let getImplementationAssemblyDir() = implementationAssemblyDir.Force()
+                        let path =
+                            FileSystem.GetFullPathShim(Path.Combine(dir, "..", "..", "shared", "Microsoft.NETCore.App", ver))
+
+                        if FileSystem.DirectoryExistsShim(path) then
+                            path, warnings
+                        else
+                            getRunningImplementationAssemblyDir (), warnings
+                    with e ->
+                        let warn =
+                            Error(FSComp.SR.scriptSdkNotDeterminedUnexpected (e.Message), rangeForErrors)
+
+                        let path = getRunningImplementationAssemblyDir ()
+                        path, [ warn ]
+                | _ ->
+                    let path = getRunningImplementationAssemblyDir ()
+                    path, []
+
+    let getImplementationAssemblyDir () = implementationAssemblyDir.Force()
 
     let getFSharpCoreLibraryName = "FSharp.Core"
 
     let getFsiLibraryName = "FSharp.Compiler.Interactive.Settings"
 
     // Use the FSharp.Core that is executing with the compiler as a backup reference
-    let getFSharpCoreImplementationReference() = Path.Combine(getFSharpCompilerLocation(), getFSharpCoreLibraryName + ".dll")
+    let getFSharpCoreImplementationReference () =
+        Path.Combine(getFSharpCompilerLocation (), getFSharpCoreLibraryName + ".dll")
 
     // Use the FSharp.Compiler.Interactive.Settings executing with the compiler as a backup reference
-    let getFsiLibraryImplementationReference() = Path.Combine(getFSharpCompilerLocation(), getFsiLibraryName + ".dll")
+    let getFsiLibraryImplementationReference () =
+        Path.Combine(getFSharpCompilerLocation (), getFsiLibraryName + ".dll")
 
     // Use the ValueTuple that is executing with the compiler if it is from System.ValueTuple
     // or the System.ValueTuple.dll that sits alongside the compiler.  (Note we always ship one with the compiler)
-    let getSystemValueTupleImplementationReference() =
-        let implDir = getImplementationAssemblyDir() |> replayWarnings
+    let getSystemValueTupleImplementationReference () =
+        let implDir = getImplementationAssemblyDir () |> replayWarnings
         let probeFile = Path.Combine(implDir, "System.ValueTuple.dll")
+
         if FileSystem.FileExistsShim(probeFile) then
             Some probeFile
         else
             try
                 let asm = typeof>.Assembly
+
                 if asm.FullName.StartsWith("System.ValueTuple", StringComparison.OrdinalIgnoreCase) then
                     Some asm.Location
                 else
-                    let valueTuplePath = Path.Combine(getFSharpCompilerLocation(), "System.ValueTuple.dll")
+                    let valueTuplePath =
+                        Path.Combine(getFSharpCompilerLocation (), "System.ValueTuple.dll")
+
                     if FileSystem.FileExistsShim(valueTuplePath) then
                         Some valueTuplePath
                     else
@@ -251,13 +304,14 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
     //          it may be a subdirectory of a locally xcopied sdk or the global sdk
     //     version is nuget format version id e.g 5.0.1-preview-4.3
     //
-    let tryGetVersionedSubDirectory (path:string) (version:string) =
+    let tryGetVersionedSubDirectory (path: string) (version: string) =
         let zeroVersion = Version("0.0.0.0")
 
         // Split the version into a number + it's suffix
         let computeVersion (version: string) =
             let ver, suffix =
                 let suffixPos = version.IndexOf('-')
+
                 if suffixPos >= 0 then
                     version.Substring(0, suffixPos), version.Substring(suffixPos + 1)
                 else
@@ -267,8 +321,9 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
             | true, v -> v, suffix
             | false, _ -> zeroVersion, suffix
 
-        let compareVersion (v1:Version * string) (v2:Version * string) =
+        let compareVersion (v1: Version * string) (v2: Version * string) =
             let fstCompare = (fst v1).CompareTo(fst v2)
+
             if fstCompare <> 0 then
                 fstCompare
             else
@@ -280,8 +335,8 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
         if directories.Length > 0 then
             directories
             |> Array.map (fun di -> computeVersion di.Name, di)
-            |> Array.filter(fun (v, _) -> (compareVersion v targetVersion) <= 0)
-            |> Array.sortWith (fun (v1,_) (v2,_) -> compareVersion v1 v2)
+            |> Array.filter (fun (v, _) -> (compareVersion v targetVersion) <= 0)
+            |> Array.sortWith (fun (v1, _) (v2, _) -> compareVersion v1 v2)
             |> Array.map snd
             |> Array.tryLast
         else
@@ -302,187 +357,219 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
     //
     // On-demand because (a) some FxResolver are ephemeral (b) we want to avoid recomputation
     let tryNetCoreRefsPackDirectoryRoot =
-      lazy
-        try
-            //     Use the reference assemblies for the highest netcoreapp tfm that we find in that location that is
-            //     lower than or equal to the implementation version.
-            let implDir, warnings = getImplementationAssemblyDir()
-            let version = DirectoryInfo(implDir).Name
-            if version.StartsWith("x") then
-                // Is running on the desktop
-                (None, None), warnings
-            else
-                let di = tryGetVersionedSubDirectory "packs/Microsoft.NETCore.App.Ref" version
-                match di with
-                | Some di -> (Some(di.Name), Some(di.Parent.FullName)), warnings
-                | None -> (None, None), warnings
+        lazy
+            try
+                //     Use the reference assemblies for the highest netcoreapp tfm that we find in that location that is
+                //     lower than or equal to the implementation version.
+                let implDir, warnings = getImplementationAssemblyDir ()
+                let version = DirectoryInfo(implDir).Name
+
+                if version.StartsWith("x") then
+                    // Is running on the desktop
+                    (None, None), warnings
+                else
+                    let di = tryGetVersionedSubDirectory "packs/Microsoft.NETCore.App.Ref" version
+
+                    match di with
+                    | Some di -> (Some(di.Name), Some(di.Parent.FullName)), warnings
+                    | None -> (None, None), warnings
             with e ->
-            let warn = Error(FSComp.SR.scriptSdkNotDeterminedUnexpected(e.Message), rangeForErrors)
-            // This is defensive coding, we don't expect this exception to happen
-            // NOTE: consider reporting this exception as a warning
-            (None, None), [warn]
+                let warn =
+                    Error(FSComp.SR.scriptSdkNotDeterminedUnexpected (e.Message), rangeForErrors)
+                // This is defensive coding, we don't expect this exception to happen
+                // NOTE: consider reporting this exception as a warning
+                (None, None), [ warn ]
 
-    let tryGetNetCoreRefsPackDirectoryRoot() = tryNetCoreRefsPackDirectoryRoot.Force()
+    let tryGetNetCoreRefsPackDirectoryRoot () = tryNetCoreRefsPackDirectoryRoot.Force()
 
     // Tries to figure out the tfm for the compiler instance.
     // On coreclr it uses the deps.json file
     //
     // On-demand because (a) some FxResolver are ephemeral (b) we want to avoid recomputation
     let tryRunningDotNetCoreTfm =
-      lazy
-        let file =
-            try
-                let asm = Assembly.GetEntryAssembly()
-                match asm with
-                | Null -> ""
-                | NonNull asm ->
-                    let depsJsonPath = Path.ChangeExtension(asm.Location, "deps.json")
-                    if FileSystem.FileExistsShim(depsJsonPath) then
-                        use stream = FileSystem.OpenFileForReadShim(depsJsonPath)
-                        stream.ReadAllText()
-                    else
-                        ""
-            with _ ->
-                // This is defensive coding, we don't expect this exception to happen
-                // NOTE: consider reporting this exception as a warning
-                ""
-
-        let tfmPrefix=".NETCoreApp,Version=v"
-        let pattern = "\"name\": \"" + tfmPrefix
-        let startPos =
-            let startPos = file.IndexOf(pattern, StringComparison.OrdinalIgnoreCase)
-            if startPos >= 0  then startPos + pattern.Length else startPos
-        let length =
-            if startPos >= 0 then
-                let ep = file.IndexOf("\"", startPos)
-                if ep >= 0 then ep - startPos else ep
-            else -1
-        match startPos, length with
-        | -1, _
-        | _, -1 ->
-            if isRunningOnCoreClr then
-                // Running on coreclr but no deps.json was deployed with the host so default to 6.0
-                Some "net6.0"
-            else
-                // Running on desktop
-                None
-        | pos, length ->
-            // use value from the deps.json file
-            let suffix = file.Substring(pos, length)
-            let prefix =
-                match Double.TryParse(suffix) with
-                | true, value when value < 5.0 -> "netcoreapp"
-                | _ -> "net"
-            Some (prefix + suffix)
+        lazy
+            let file =
+                try
+                    let asm = Assembly.GetEntryAssembly()
+
+                    match asm with
+                    | Null -> ""
+                    | NonNull asm ->
+                        let depsJsonPath = Path.ChangeExtension(asm.Location, "deps.json")
+
+                        if FileSystem.FileExistsShim(depsJsonPath) then
+                            use stream = FileSystem.OpenFileForReadShim(depsJsonPath)
+                            stream.ReadAllText()
+                        else
+                            ""
+                with _ ->
+                    // This is defensive coding, we don't expect this exception to happen
+                    // NOTE: consider reporting this exception as a warning
+                    ""
 
-    let tryGetRunningDotNetCoreTfm() = tryRunningDotNetCoreTfm.Force()
+            let tfmPrefix = ".NETCoreApp,Version=v"
+            let pattern = "\"name\": \"" + tfmPrefix
+
+            let startPos =
+                let startPos = file.IndexOf(pattern, StringComparison.OrdinalIgnoreCase)
+
+                if startPos >= 0 then
+                    startPos + pattern.Length
+                else
+                    startPos
+
+            let length =
+                if startPos >= 0 then
+                    let ep = file.IndexOf("\"", startPos)
+                    if ep >= 0 then ep - startPos else ep
+                else
+                    -1
+
+            match startPos, length with
+            | -1, _
+            | _, -1 ->
+                if isRunningOnCoreClr then
+                    // Running on coreclr but no deps.json was deployed with the host so default to 6.0
+                    Some "net6.0"
+                else
+                    // Running on desktop
+                    None
+            | pos, length ->
+                // use value from the deps.json file
+                let suffix = file.Substring(pos, length)
+
+                let prefix =
+                    match Double.TryParse(suffix) with
+                    | true, value when value < 5.0 -> "netcoreapp"
+                    | _ -> "net"
+
+                Some(prefix + suffix)
+
+    let tryGetRunningDotNetCoreTfm () = tryRunningDotNetCoreTfm.Force()
 
     // Tries to figure out the tfm for the compiler instance on the Windows desktop
     // On full clr it uses the mscorlib version number
     let getRunningDotNetFrameworkTfm () =
-        let defaultMscorlibVersion = 4,8,3815,0
-        let desktopProductVersionMonikers = [|
-            // major, minor, build, revision, moniker
-               4,     8,      3815,     0,    "net48"
-               4,     8,      3761,     0,    "net48"
-               4,     7,      3190,     0,    "net472"
-               4,     7,      3062,     0,    "net472"
-               4,     7,      2600,     0,    "net471"
-               4,     7,      2558,     0,    "net471"
-               4,     7,      2053,     0,    "net47"
-               4,     7,      2046,     0,    "net47"
-               4,     6,      1590,     0,    "net462"
-               4,     6,        57,     0,    "net462"
-               4,     6,      1055,     0,    "net461"
-               4,     6,        81,     0,    "net46"
-               4,     0,     30319, 34209,    "net452"
-               4,     0,     30319, 17020,    "net452"
-               4,     0,     30319, 18408,    "net451"
-               4,     0,     30319, 17929,    "net45"
-               4,     0,     30319,     1,    "net4"
+        let defaultMscorlibVersion = 4, 8, 3815, 0
+
+        let desktopProductVersionMonikers =
+            [|
+                // major, minor, build, revision, moniker
+                4, 8, 3815, 0, "net48"
+                4, 8, 3761, 0, "net48"
+                4, 7, 3190, 0, "net472"
+                4, 7, 3062, 0, "net472"
+                4, 7, 2600, 0, "net471"
+                4, 7, 2558, 0, "net471"
+                4, 7, 2053, 0, "net47"
+                4, 7, 2046, 0, "net47"
+                4, 6, 1590, 0, "net462"
+                4, 6, 57, 0, "net462"
+                4, 6, 1055, 0, "net461"
+                4, 6, 81, 0, "net46"
+                4, 0, 30319, 34209, "net452"
+                4, 0, 30319, 17020, "net452"
+                4, 0, 30319, 18408, "net451"
+                4, 0, 30319, 17929, "net45"
+                4, 0, 30319, 1, "net4"
             |]
 
-        let majorPart, minorPart, buildPart, privatePart=
+        let majorPart, minorPart, buildPart, privatePart =
             try
-                let attrOpt = typeof.Assembly.GetCustomAttributes(typeof) |> Seq.tryHead
+                let attrOpt =
+                    typeof.Assembly.GetCustomAttributes (typeof)
+                    |> Seq.tryHead
+
                 match attrOpt with
                 | Some attr ->
-                    let fv = (downcast attr : AssemblyFileVersionAttribute).Version.Split([|'.'|]) |> Array.map(fun e ->  Int32.Parse(e))
+                    let fv =
+                        (downcast attr: AssemblyFileVersionAttribute).Version.Split([| '.' |])
+                        |> Array.map (fun e -> Int32.Parse(e))
+
                     fv[0], fv[1], fv[2], fv[3]
                 | _ -> defaultMscorlibVersion
-            with _ -> defaultMscorlibVersion
+            with _ ->
+                defaultMscorlibVersion
 
-        // Get the ProductVersion of this framework compare with table yield compatible monikers
+        // Get the ProductVersion of this framework compare with table compatible monikers
         match desktopProductVersionMonikers
               |> Array.tryFind (fun (major, minor, build, revision, _) ->
-                    (majorPart >= major) &&
-                    (minorPart >= minor) &&
-                    (buildPart >= build) &&
-                    (privatePart >= revision)) with
-        | Some (_,_,_,_,moniker) ->
-            moniker
+                  (majorPart >= major)
+                  && (minorPart >= minor)
+                  && (buildPart >= build)
+                  && (privatePart >= revision))
+            with
+        | Some (_, _, _, _, moniker) -> moniker
         | None ->
             // no TFM could be found, assume latest stable?
             "net48"
 
     let trySdkRefsPackDirectory =
-      lazy
-        let tfmPrefix = "netcoreapp"
-        let tfmCompare c1 c2 =
-            let deconstructTfmApp (netcoreApp: DirectoryInfo) =
-                let name = netcoreApp.Name
-                try
-                    if name.StartsWith(tfmPrefix, StringComparison.InvariantCultureIgnoreCase) then
-                        Some (Double.Parse(name.Substring(tfmPrefix.Length), NumberStyles.AllowDecimalPoint,  CultureInfo.InvariantCulture))
-                    else
+        lazy
+            let tfmPrefix = "netcoreapp"
+
+            let tfmCompare c1 c2 =
+                let deconstructTfmApp (netcoreApp: DirectoryInfo) =
+                    let name = netcoreApp.Name
+
+                    try
+                        if name.StartsWith(tfmPrefix, StringComparison.InvariantCultureIgnoreCase) then
+                            Some(
+                                Double.Parse(name.Substring(tfmPrefix.Length), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture)
+                            )
+                        else
+                            None
+                    with _ ->
+                        // This is defensive coding, we don't expect this exception to happen
+                        // NOTE: consider reporting this exception as a warning
                         None
-                with _ ->
-                    // This is defensive coding, we don't expect this exception to happen
-                   // NOTE: consider reporting this exception as a warning
-                    None
 
-            if c1 = c2 then 0
-            else
-                match (deconstructTfmApp c1), (deconstructTfmApp c2) with
-                | Some c1, Some c2 -> int(c1 - c2)
-                | None, Some _ -> -1
-                | Some _, None -> 1
-                | _ -> 0
-
-        match tryGetNetCoreRefsPackDirectoryRoot() with
-        | (Some version, Some root), warnings ->
-            try
-                let ref = Path.Combine(root, version, "ref")
-                let highestTfm =
-                    DirectoryInfo(ref).GetDirectories()
-                    |> Array.sortWith tfmCompare
-                    |> Array.tryLast
-
-                match highestTfm with
-                | Some tfm -> Some (Path.Combine(ref, tfm.Name)), warnings
-                | None -> None, warnings
-            with e ->
-                let warn = Error(FSComp.SR.scriptSdkNotDeterminedUnexpected(e.Message), rangeForErrors)
-                // This is defensive coding, we don't expect this exception to happen
-                // NOTE: consider reporting this exception as a warning
-                None, warnings @ [warn]
-        | _ -> None, []
+                if c1 = c2 then
+                    0
+                else
+                    match (deconstructTfmApp c1), (deconstructTfmApp c2) with
+                    | Some c1, Some c2 -> int (c1 - c2)
+                    | None, Some _ -> -1
+                    | Some _, None -> 1
+                    | _ -> 0
+
+            match tryGetNetCoreRefsPackDirectoryRoot () with
+            | (Some version, Some root), warnings ->
+                try
+                    let ref = Path.Combine(root, version, "ref")
 
-    let tryGetSdkRefsPackDirectory() = trySdkRefsPackDirectory.Force()
+                    let highestTfm =
+                        DirectoryInfo(ref).GetDirectories()
+                        |> Array.sortWith tfmCompare
+                        |> Array.tryLast
+
+                    match highestTfm with
+                    | Some tfm -> Some(Path.Combine(ref, tfm.Name)), warnings
+                    | None -> None, warnings
+                with e ->
+                    let warn =
+                        Error(FSComp.SR.scriptSdkNotDeterminedUnexpected (e.Message), rangeForErrors)
+                    // This is defensive coding, we don't expect this exception to happen
+                    // NOTE: consider reporting this exception as a warning
+                    None, warnings @ [ warn ]
+            | _ -> None, []
+
+    let tryGetSdkRefsPackDirectory () = trySdkRefsPackDirectory.Force()
 
     let getDependenciesOf assemblyReferences =
         let assemblies = Dictionary()
 
         // Identify path to a dll in the framework directory from a simple name
         let frameworkPathFromSimpleName simpleName =
-            let implDir = getImplementationAssemblyDir() |> replayWarnings
+            let implDir = getImplementationAssemblyDir () |> replayWarnings
             let root = Path.Combine(implDir, simpleName)
+
             let pathOpt =
                 [| ""; ".dll"; ".exe" |]
-                |> Seq.tryPick(fun ext ->
+                |> Seq.tryPick (fun ext ->
                     let path = root + ext
-                    if FileSystem.FileExistsShim(path) then Some path
-                    else None)
+                    if FileSystem.FileExistsShim(path) then Some path else None)
+
             match pathOpt with
             | Some path -> path
             | None -> root
@@ -525,19 +612,24 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
                         | _ ->
                             try
                                 let opts =
-                                    { metadataOnly = MetadataOnlyFlag.Yes // turn this off here as we need the actual IL code
-                                      reduceMemoryUsage = ReduceMemoryFlag.Yes
-                                      pdbDirPath = None
-                                      tryGetMetadataSnapshot = (fun _ -> None) (* tryGetMetadataSnapshot *) }
+                                    {
+                                        metadataOnly = MetadataOnlyFlag.Yes // turn this off here as we need the actual IL code
+                                        reduceMemoryUsage = ReduceMemoryFlag.Yes
+                                        pdbDirPath = None
+                                        tryGetMetadataSnapshot = (fun _ -> None) (* tryGetMetadataSnapshot *)
+                                    }
 
                                 let reader = OpenILModuleReader path opts
                                 assemblies.Add(referenceName, path)
+
                                 for reference in reader.ILAssemblyRefs do
                                     traverseDependencies reference.Name
 
                             // There are many native assemblies which can't be cracked, raising exceptions
-                            with _ -> ()
-                with _ -> ()
+                            with _ ->
+                                ()
+                with _ ->
+                    ()
 
         assemblyReferences |> List.iter traverseDependencies
         assemblies
@@ -548,332 +640,352 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
     //    (a) included in the environment used for all .fsx files (see service.fs)
     //    (b) included in environment for files 'orphaned' from a project context
     //            -- for orphaned files (files in VS without a project context)
-    let getDotNetFrameworkDefaultReferences useFsiAuxLib = [
-        yield "mscorlib"
-        yield "System"
-        yield "System.Xml"
-        yield "System.Runtime.Remoting"
-        yield "System.Runtime.Serialization.Formatters.Soap"
-        yield "System.Data"
-        yield "System.Drawing"
-        yield "System.Core"
-        yield "System.Configuration"
-
-        yield getFSharpCoreLibraryName
-        if useFsiAuxLib then yield fsiLibraryName
-
-        // always include a default reference to System.ValueTuple.dll in scripts and out-of-project sources
-        match getSystemValueTupleImplementationReference () with
-        | None -> ()
-        | Some v -> yield v
-
-        // These are the Portable-profile and .NET Standard 1.6 dependencies of FSharp.Core.dll.  These are needed
-        // when an F# script references an F# profile 7, 78, 259 or .NET Standard 1.6 component which in turn refers
-        // to FSharp.Core for profile 7, 78, 259 or .NET Standard.
-        yield "netstandard"
-        yield "System.Runtime"          // lots of types
-        yield "System.Linq"             // System.Linq.Expressions.Expression
-        yield "System.Reflection"       // System.Reflection.ParameterInfo
-        yield "System.Linq.Expressions" // System.Linq.IQueryable
-        yield "System.Threading.Tasks"  // valuetype [System.Threading.Tasks]System.Threading.CancellationToken
-        yield "System.IO"               //  System.IO.TextWriter
-        yield "System.Net.Requests"     //  System.Net.WebResponse etc.
-        yield "System.Collections"      // System.Collections.Generic.List
-        yield "System.Runtime.Numerics" // BigInteger
-        yield "System.Threading"        // OperationCanceledException
-        yield "System.Web"
-        yield "System.Web.Services"
-        yield "System.Windows.Forms"
-        yield "System.Numerics"
-    ]
+    let getDotNetFrameworkDefaultReferences useFsiAuxLib =
+        [
+            "mscorlib"
+            "System"
+            "System.Xml"
+            "System.Runtime.Remoting"
+            "System.Runtime.Serialization.Formatters.Soap"
+            "System.Data"
+            "System.Drawing"
+            "System.Core"
+            "System.Configuration"
+
+            getFSharpCoreLibraryName
+            if useFsiAuxLib then fsiLibraryName
+
+            // always include a default reference to System.ValueTuple.dll in scripts and out-of-project sources
+            match getSystemValueTupleImplementationReference () with
+            | None -> ()
+            | Some v -> v
+
+            // These are the Portable-profile and .NET Standard 1.6 dependencies of FSharp.Core.dll.  These are needed
+            // when an F# script references an F# profile 7, 78, 259 or .NET Standard 1.6 component which in turn refers
+            // to FSharp.Core for profile 7, 78, 259 or .NET Standard.
+            "netstandard"
+            "System.Runtime" // lots of types
+            "System.Linq" // System.Linq.Expressions.Expression
+            "System.Reflection" // System.Reflection.ParameterInfo
+            "System.Linq.Expressions" // System.Linq.IQueryable
+            "System.Threading.Tasks" // valuetype [System.Threading.Tasks]System.Threading.CancellationToken
+            "System.IO" //  System.IO.TextWriter
+            "System.Net.Requests" //  System.Net.WebResponse etc.
+            "System.Collections" // System.Collections.Generic.List
+            "System.Runtime.Numerics" // BigInteger
+            "System.Threading" // OperationCanceledException
+            "System.Web"
+            "System.Web.Services"
+            "System.Windows.Forms"
+            "System.Numerics"
+        ]
 
     let getDotNetCoreImplementationReferences useFsiAuxLib =
-        let implDir = getImplementationAssemblyDir()  |> replayWarnings
+        let implDir = getImplementationAssemblyDir () |> replayWarnings
+
         let roots =
-            [ yield! Directory.GetFiles(implDir, "*.dll")
-              yield getFSharpCoreImplementationReference()
-              if useFsiAuxLib then yield getFsiLibraryImplementationReference() ]
+            [
+                yield! Directory.GetFiles(implDir, "*.dll")
+                getFSharpCoreImplementationReference ()
+                if useFsiAuxLib then getFsiLibraryImplementationReference ()
+            ]
+
         (getDependenciesOf roots).Values |> Seq.toList
 
     // A set of assemblies to always consider to be system assemblies.  A common set of these can be used a shared
     // resources between projects in the compiler services.  Also all assemblies where well-known system types exist
     // referenced from TcGlobals must be listed here.
     let systemAssemblies =
-        HashSet [
-            // NOTE: duplicates are ok in this list
-
-            // .NET Framework list
-            yield "mscorlib"
-            yield "netstandard"
-            yield "System"
-            yield getFSharpCoreLibraryName
-            yield "FSharp.Compiler.Interactive.Settings"
-            yield "Microsoft.CSharp"
-            yield "Microsoft.VisualBasic"
-            yield "Microsoft.VisualBasic.Core"
-            yield "Microsoft.Win32.Primitives"
-            yield "Microsoft.Win32.Registry"
-            yield "System.AppContext"
-            yield "System.Buffers"
-            yield "System.Collections"
-            yield "System.Collections.Concurrent"
-            yield "System.Collections.Immutable"
-            yield "System.Collections.NonGeneric"
-            yield "System.Collections.Specialized"
-            yield "System.ComponentModel"
-            yield "System.ComponentModel.Annotations"
-            yield "System.ComponentModel.DataAnnotations"
-            yield "System.ComponentModel.EventBasedAsync"
-            yield "System.ComponentModel.Primitives"
-            yield "System.ComponentModel.TypeConverter"
-            yield "System.Configuration"
-            yield "System.Console"
-            yield "System.Core"
-            yield "System.Data"
-            yield "System.Data.Common"
-            yield "System.Data.DataSetExtensions"
-            yield "System.Deployment"
-            yield "System.Design"
-            yield "System.Diagnostics.Contracts"
-            yield "System.Diagnostics.Debug"
-            yield "System.Diagnostics.DiagnosticSource"
-            yield "System.Diagnostics.FileVersionInfo"
-            yield "System.Diagnostics.Process"
-            yield "System.Diagnostics.StackTrace"
-            yield "System.Diagnostics.TextWriterTraceListener"
-            yield "System.Diagnostics.Tools"
-            yield "System.Diagnostics.TraceSource"
-            yield "System.Diagnostics.Tracing"
-            yield "System.Drawing"
-            yield "System.Drawing.Primitives"
-            yield "System.Dynamic.Runtime"
-            yield "System.Formats.Asn1"
-            yield "System.Globalization"
-            yield "System.Globalization.Calendars"
-            yield "System.Globalization.Extensions"
-            yield "System.IO"
-            yield "System.IO.Compression"
-            yield "System.IO.Compression.Brotli"
-            yield "System.IO.Compression.FileSystem"
-            yield "System.IO.Compression.ZipFile"
-            yield "System.IO.FileSystem"
-            yield "System.IO.FileSystem.DriveInfo"
-            yield "System.IO.FileSystem.Primitives"
-            yield "System.IO.FileSystem.Watcher"
-            yield "System.IO.IsolatedStorage"
-            yield "System.IO.MemoryMappedFiles"
-            yield "System.IO.Pipes"
-            yield "System.IO.UnmanagedMemoryStream"
-            yield "System.Linq"
-            yield "System.Linq.Expressions"
-            yield "System.Linq.Expressions"
-            yield "System.Linq.Parallel"
-            yield "System.Linq.Queryable"
-            yield "System.Memory"
-            yield "System.Messaging"
-            yield "System.Net"
-            yield "System.Net.Http"
-            yield "System.Net.Http.Json"
-            yield "System.Net.HttpListener"
-            yield "System.Net.Mail"
-            yield "System.Net.NameResolution"
-            yield "System.Net.NetworkInformation"
-            yield "System.Net.Ping"
-            yield "System.Net.Primitives"
-            yield "System.Net.Requests"
-            yield "System.Net.Security"
-            yield "System.Net.ServicePoint"
-            yield "System.Net.Sockets"
-            yield "System.Net.WebClient"
-            yield "System.Net.WebHeaderCollection"
-            yield "System.Net.WebProxy"
-            yield "System.Net.WebSockets"
-            yield "System.Net.WebSockets.Client"
-            yield "System.Numerics"
-            yield "System.Numerics.Vectors"
-            yield "System.ObjectModel"
-            yield "System.Observable"
-            yield "System.Private.Uri"
-            yield "System.Reflection"
-            yield "System.Reflection.DispatchProxy"
-            yield "System.Reflection.Emit"
-            yield "System.Reflection.Emit.ILGeneration"
-            yield "System.Reflection.Emit.Lightweight"
-            yield "System.Reflection.Extensions"
-            yield "System.Reflection.Metadata"
-            yield "System.Reflection.Primitives"
-            yield "System.Reflection.TypeExtensions"
-            yield "System.Resources.Reader"
-            yield "System.Resources.ResourceManager"
-            yield "System.Resources.Writer"
-            yield "System.Runtime"
-            yield "System.Runtime.CompilerServices.Unsafe"
-            yield "System.Runtime.CompilerServices.VisualC"
-            yield "System.Runtime.Extensions"
-            yield "System.Runtime.Handles"
-            yield "System.Runtime.InteropServices"
-            yield "System.Runtime.InteropServices.PInvoke"
-            yield "System.Runtime.InteropServices.RuntimeInformation"
-            yield "System.Runtime.InteropServices.WindowsRuntime"
-            yield "System.Runtime.Intrinsics"
-            yield "System.Runtime.Loader"
-            yield "System.Runtime.Numerics"
-            yield "System.Runtime.Remoting"
-            yield "System.Runtime.Serialization"
-            yield "System.Runtime.Serialization.Formatters"
-            yield "System.Runtime.Serialization.Formatters.Soap"
-            yield "System.Runtime.Serialization.Json"
-            yield "System.Runtime.Serialization.Primitives"
-            yield "System.Runtime.Serialization.Xml"
-            yield "System.Security"
-            yield "System.Security.Claims"
-            yield "System.Security.Cryptography.Algorithms"
-            yield "System.Security.Cryptography.Cng"
-            yield "System.Security.Cryptography.Csp"
-            yield "System.Security.Cryptography.Encoding"
-            yield "System.Security.Cryptography.OpenSsl"
-            yield "System.Security.Cryptography.Primitives"
-            yield "System.Security.Cryptography.X509Certificates"
-            yield "System.Security.Principal"
-            yield "System.Security.Principal.Windows"
-            yield "System.Security.SecureString"
-            yield "System.ServiceModel.Web"
-            yield "System.ServiceProcess"
-            yield "System.Text.Encoding"
-            yield "System.Text.Encoding.CodePages"
-            yield "System.Text.Encoding.Extensions"
-            yield "System.Text.Encodings.Web"
-            yield "System.Text.Json"
-            yield "System.Text.RegularExpressions"
-            yield "System.Threading"
-            yield "System.Threading.Channels"
-            yield "System.Threading.Overlapped"
-            yield "System.Threading.Tasks"
-            yield "System.Threading.Tasks.Dataflow"
-            yield "System.Threading.Tasks.Extensions"
-            yield "System.Threading.Tasks.Parallel"
-            yield "System.Threading.Thread"
-            yield "System.Threading.ThreadPool"
-            yield "System.Threading.Timer"
-            yield "System.Transactions"
-            yield "System.Transactions.Local"
-            yield "System.ValueTuple"
-            yield "System.Web"
-            yield "System.Web.HttpUtility"
-            yield "System.Web.Services"
-            yield "System.Windows"
-            yield "System.Windows.Forms"
-            yield "System.Xml"
-            yield "System.Xml.Linq"
-            yield "System.Xml.ReaderWriter"
-            yield "System.Xml.Serialization"
-            yield "System.Xml.XDocument"
-            yield "System.Xml.XmlDocument"
-            yield "System.Xml.XmlSerializer"
-            yield "System.Xml.XPath"
-            yield "System.Xml.XPath.XDocument"
-            yield "WindowsBase"
-        ]
+        HashSet
+            [
+                // NOTE: duplicates are ok in this list
+
+                // .NET Framework list
+                "mscorlib"
+                "netstandard"
+                "System"
+                getFSharpCoreLibraryName
+                "FSharp.Compiler.Interactive.Settings"
+                "Microsoft.CSharp"
+                "Microsoft.VisualBasic"
+                "Microsoft.VisualBasic.Core"
+                "Microsoft.Win32.Primitives"
+                "Microsoft.Win32.Registry"
+                "System.AppContext"
+                "System.Buffers"
+                "System.Collections"
+                "System.Collections.Concurrent"
+                "System.Collections.Immutable"
+                "System.Collections.NonGeneric"
+                "System.Collections.Specialized"
+                "System.ComponentModel"
+                "System.ComponentModel.Annotations"
+                "System.ComponentModel.DataAnnotations"
+                "System.ComponentModel.EventBasedAsync"
+                "System.ComponentModel.Primitives"
+                "System.ComponentModel.TypeConverter"
+                "System.Configuration"
+                "System.Console"
+                "System.Core"
+                "System.Data"
+                "System.Data.Common"
+                "System.Data.DataSetExtensions"
+                "System.Deployment"
+                "System.Design"
+                "System.Diagnostics.Contracts"
+                "System.Diagnostics.Debug"
+                "System.Diagnostics.DiagnosticSource"
+                "System.Diagnostics.FileVersionInfo"
+                "System.Diagnostics.Process"
+                "System.Diagnostics.StackTrace"
+                "System.Diagnostics.TextWriterTraceListener"
+                "System.Diagnostics.Tools"
+                "System.Diagnostics.TraceSource"
+                "System.Diagnostics.Tracing"
+                "System.Drawing"
+                "System.Drawing.Primitives"
+                "System.Dynamic.Runtime"
+                "System.Formats.Asn1"
+                "System.Globalization"
+                "System.Globalization.Calendars"
+                "System.Globalization.Extensions"
+                "System.IO"
+                "System.IO.Compression"
+                "System.IO.Compression.Brotli"
+                "System.IO.Compression.FileSystem"
+                "System.IO.Compression.ZipFile"
+                "System.IO.FileSystem"
+                "System.IO.FileSystem.DriveInfo"
+                "System.IO.FileSystem.Primitives"
+                "System.IO.FileSystem.Watcher"
+                "System.IO.IsolatedStorage"
+                "System.IO.MemoryMappedFiles"
+                "System.IO.Pipes"
+                "System.IO.UnmanagedMemoryStream"
+                "System.Linq"
+                "System.Linq.Expressions"
+                "System.Linq.Expressions"
+                "System.Linq.Parallel"
+                "System.Linq.Queryable"
+                "System.Memory"
+                "System.Messaging"
+                "System.Net"
+                "System.Net.Http"
+                "System.Net.Http.Json"
+                "System.Net.HttpListener"
+                "System.Net.Mail"
+                "System.Net.NameResolution"
+                "System.Net.NetworkInformation"
+                "System.Net.Ping"
+                "System.Net.Primitives"
+                "System.Net.Requests"
+                "System.Net.Security"
+                "System.Net.ServicePoint"
+                "System.Net.Sockets"
+                "System.Net.WebClient"
+                "System.Net.WebHeaderCollection"
+                "System.Net.WebProxy"
+                "System.Net.WebSockets"
+                "System.Net.WebSockets.Client"
+                "System.Numerics"
+                "System.Numerics.Vectors"
+                "System.ObjectModel"
+                "System.Observable"
+                "System.Private.Uri"
+                "System.Reflection"
+                "System.Reflection.DispatchProxy"
+                "System.Reflection.Emit"
+                "System.Reflection.Emit.ILGeneration"
+                "System.Reflection.Emit.Lightweight"
+                "System.Reflection.Extensions"
+                "System.Reflection.Metadata"
+                "System.Reflection.Primitives"
+                "System.Reflection.TypeExtensions"
+                "System.Resources.Reader"
+                "System.Resources.ResourceManager"
+                "System.Resources.Writer"
+                "System.Runtime"
+                "System.Runtime.CompilerServices.Unsafe"
+                "System.Runtime.CompilerServices.VisualC"
+                "System.Runtime.Extensions"
+                "System.Runtime.Handles"
+                "System.Runtime.InteropServices"
+                "System.Runtime.InteropServices.PInvoke"
+                "System.Runtime.InteropServices.RuntimeInformation"
+                "System.Runtime.InteropServices.WindowsRuntime"
+                "System.Runtime.Intrinsics"
+                "System.Runtime.Loader"
+                "System.Runtime.Numerics"
+                "System.Runtime.Remoting"
+                "System.Runtime.Serialization"
+                "System.Runtime.Serialization.Formatters"
+                "System.Runtime.Serialization.Formatters.Soap"
+                "System.Runtime.Serialization.Json"
+                "System.Runtime.Serialization.Primitives"
+                "System.Runtime.Serialization.Xml"
+                "System.Security"
+                "System.Security.Claims"
+                "System.Security.Cryptography.Algorithms"
+                "System.Security.Cryptography.Cng"
+                "System.Security.Cryptography.Csp"
+                "System.Security.Cryptography.Encoding"
+                "System.Security.Cryptography.OpenSsl"
+                "System.Security.Cryptography.Primitives"
+                "System.Security.Cryptography.X509Certificates"
+                "System.Security.Principal"
+                "System.Security.Principal.Windows"
+                "System.Security.SecureString"
+                "System.ServiceModel.Web"
+                "System.ServiceProcess"
+                "System.Text.Encoding"
+                "System.Text.Encoding.CodePages"
+                "System.Text.Encoding.Extensions"
+                "System.Text.Encodings.Web"
+                "System.Text.Json"
+                "System.Text.RegularExpressions"
+                "System.Threading"
+                "System.Threading.Channels"
+                "System.Threading.Overlapped"
+                "System.Threading.Tasks"
+                "System.Threading.Tasks.Dataflow"
+                "System.Threading.Tasks.Extensions"
+                "System.Threading.Tasks.Parallel"
+                "System.Threading.Thread"
+                "System.Threading.ThreadPool"
+                "System.Threading.Timer"
+                "System.Transactions"
+                "System.Transactions.Local"
+                "System.ValueTuple"
+                "System.Web"
+                "System.Web.HttpUtility"
+                "System.Web.Services"
+                "System.Windows"
+                "System.Windows.Forms"
+                "System.Xml"
+                "System.Xml.Linq"
+                "System.Xml.ReaderWriter"
+                "System.Xml.Serialization"
+                "System.Xml.XDocument"
+                "System.Xml.XmlDocument"
+                "System.Xml.XmlSerializer"
+                "System.Xml.XPath"
+                "System.Xml.XPath.XDocument"
+                "WindowsBase"
+            ]
 
     member _.GetSystemAssemblies() = systemAssemblies
 
     member _.IsInReferenceAssemblyPackDirectory fileName =
-      fxlock.AcquireLock <| fun fxtok -> 
-        RequireFxResolverLock(fxtok, "assuming all member require lock")
+        fxlock.AcquireLock(fun fxtok ->
+            RequireFxResolverLock(fxtok, "assuming all member require lock")
 
-        match tryGetNetCoreRefsPackDirectoryRoot() |> replayWarnings with
-        | _, Some root ->
-            let path = Path.GetDirectoryName(fileName)
-            path.StartsWith(root, StringComparison.OrdinalIgnoreCase)
-        | _ -> false
+            match tryGetNetCoreRefsPackDirectoryRoot () |> replayWarnings with
+            | _, Some root ->
+                let path = Path.GetDirectoryName(fileName)
+                path.StartsWith(root, StringComparison.OrdinalIgnoreCase)
+            | _ -> false)
 
     member _.TryGetSdkDir() =
-      fxlock.AcquireLock <| fun fxtok -> 
-        RequireFxResolverLock(fxtok, "assuming all member require lock")
-        tryGetSdkDir() |> replayWarnings
+        fxlock.AcquireLock(fun fxtok ->
+            RequireFxResolverLock(fxtok, "assuming all member require lock")
+            tryGetSdkDir () |> replayWarnings)
 
     /// Gets the selected target framework moniker, e.g netcore3.0, net472, and the running rid of the current machine
     member _.GetTfmAndRid() =
-      fxlock.AcquireLock <| fun fxtok -> 
-        RequireFxResolverLock(fxtok, "assuming all member require lock")
-        // Interactive processes read their own configuration to find the running tfm
+        fxlock.AcquireLock(fun fxtok ->
+            RequireFxResolverLock(fxtok, "assuming all member require lock")
+            // Interactive processes read their own configuration to find the running tfm
+
+            let tfm =
+                if isInteractive then
+                    match tryGetRunningDotNetCoreTfm () with
+                    | Some tfm -> tfm
+                    | _ -> getRunningDotNetFrameworkTfm ()
+                else
+                    let sdkDir = tryGetSdkDir () |> replayWarnings
 
-        let tfm =
-            if isInteractive then
-                match tryGetRunningDotNetCoreTfm() with
-                | Some tfm -> tfm
-                | _ -> getRunningDotNetFrameworkTfm ()
-            else
-                let sdkDir = tryGetSdkDir() |> replayWarnings
-                match sdkDir with
-                | Some dir ->
-                    let dotnetConfigFile = Path.Combine(dir, "dotnet.runtimeconfig.json")
-                    use stream = FileSystem.OpenFileForReadShim(dotnetConfigFile)
-                    let dotnetConfig = stream.ReadAllText()
-                    let pattern = "\"tfm\": \""
-                    let startPos = dotnetConfig.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) + pattern.Length
-                    let endPos = dotnetConfig.IndexOf("\"", startPos)
-                    let tfm = dotnetConfig[startPos..endPos-1]
-                    //printfn "GetTfmAndRid, tfm = '%s'" tfm
-                    tfm
-                | None ->
-                match tryGetRunningDotNetCoreTfm() with
-                | Some tfm -> tfm
-                | _ -> getRunningDotNetFrameworkTfm ()
-
-        // Computer valid dotnet-rids for this environment:
-        //      https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
-        //
-        // Where rid is: win, win-x64, win-x86, osx-x64, linux-x64 etc ...
-        let runningRid =
-            let processArchitecture = RuntimeInformation.ProcessArchitecture
-            let baseRid =
-                if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then "win"
-                elif RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then "osx"
-                else "linux"
-            match processArchitecture with
-            | Architecture.X64 ->  baseRid + "-x64"
-            | Architecture.X86 -> baseRid + "-x86"
-            | Architecture.Arm64 -> baseRid + "-arm64"
-            | _ -> baseRid + "-arm"
-
-        tfm, runningRid
+                    match sdkDir with
+                    | Some dir ->
+                        let dotnetConfigFile = Path.Combine(dir, "dotnet.runtimeconfig.json")
+                        use stream = FileSystem.OpenFileForReadShim(dotnetConfigFile)
+                        let dotnetConfig = stream.ReadAllText()
+                        let pattern = "\"tfm\": \""
+
+                        let startPos =
+                            dotnetConfig.IndexOf(pattern, StringComparison.OrdinalIgnoreCase)
+                            + pattern.Length
+
+                        let endPos = dotnetConfig.IndexOf("\"", startPos)
+                        let tfm = dotnetConfig[startPos .. endPos - 1]
+                        //printfn "GetTfmAndRid, tfm = '%s'" tfm
+                        tfm
+                    | None ->
+                        match tryGetRunningDotNetCoreTfm () with
+                        | Some tfm -> tfm
+                        | _ -> getRunningDotNetFrameworkTfm ()
+
+            // Computer valid dotnet-rids for this environment:
+            //      https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
+            //
+            // Where rid is: win, win-x64, win-x86, osx-x64, linux-x64 etc ...
+            let runningRid =
+                let processArchitecture = RuntimeInformation.ProcessArchitecture
+
+                let baseRid =
+                    if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
+                        "win"
+                    elif RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then
+                        "osx"
+                    else
+                        "linux"
+
+                match processArchitecture with
+                | Architecture.X64 -> baseRid + "-x64"
+                | Architecture.X86 -> baseRid + "-x86"
+                | Architecture.Arm64 -> baseRid + "-arm64"
+                | _ -> baseRid + "-arm"
+
+            tfm, runningRid)
 
     static member ClearStaticCaches() =
         desiredDotNetSdkVersionForDirectoryCache.Clear()
 
     member _.GetFrameworkRefsPackDirectory() =
-      fxlock.AcquireLock <| fun fxtok -> 
-        RequireFxResolverLock(fxtok, "assuming all member require lock")
-        tryGetSdkRefsPackDirectory() |> replayWarnings
+        fxlock.AcquireLock(fun fxtok ->
+            RequireFxResolverLock(fxtok, "assuming all member require lock")
+            tryGetSdkRefsPackDirectory () |> replayWarnings)
 
     member _.TryGetDesiredDotNetSdkVersionForDirectory() =
-      fxlock.AcquireLock <| fun fxtok -> 
-        RequireFxResolverLock(fxtok, "assuming all member require lock")
-        tryGetDesiredDotNetSdkVersionForDirectoryInfo()
+        fxlock.AcquireLock(fun fxtok ->
+            RequireFxResolverLock(fxtok, "assuming all member require lock")
+            tryGetDesiredDotNetSdkVersionForDirectoryInfo ())
 
     // The set of references entered into the TcConfigBuilder for scripts prior to computing the load closure.
     member _.GetDefaultReferences useFsiAuxLib =
-      fxlock.AcquireLock <| fun fxtok -> 
-        RequireFxResolverLock(fxtok, "assuming all member require lock")
-        let defaultReferences =
-            if assumeDotNetFramework then
-                getDotNetFrameworkDefaultReferences useFsiAuxLib, assumeDotNetFramework
-            else
-                if useSdkRefs then
+        fxlock.AcquireLock(fun fxtok ->
+            RequireFxResolverLock(fxtok, "assuming all member require lock")
+
+            let defaultReferences =
+                if assumeDotNetFramework then
+                    getDotNetFrameworkDefaultReferences useFsiAuxLib, assumeDotNetFramework
+                else if useSdkRefs then
                     // Go fetch references
-                    let sdkDir = tryGetSdkRefsPackDirectory() |> replayWarnings
+                    let sdkDir = tryGetSdkRefsPackDirectory () |> replayWarnings
+
                     match sdkDir with
                     | Some path ->
                         try
                             let sdkReferences =
-                                [ yield! Directory.GetFiles(path, "*.dll")
-                                  yield getFSharpCoreImplementationReference()
-                                  if useFsiAuxLib then yield getFsiLibraryImplementationReference()
-                                ] |> List.filter(fun f -> systemAssemblies.Contains(Path.GetFileNameWithoutExtension(f)))
+                                [
+                                    yield! Directory.GetFiles(path, "*.dll")
+                                    getFSharpCoreImplementationReference ()
+                                    if useFsiAuxLib then getFsiLibraryImplementationReference ()
+                                ]
+                                |> List.filter (fun f -> systemAssemblies.Contains(Path.GetFileNameWithoutExtension(f)))
+
                             sdkReferences, false
                         with e ->
-                            warning (Error(FSComp.SR.scriptSdkNotDeterminedUnexpected(e.Message), rangeForErrors))
+                            warning (Error(FSComp.SR.scriptSdkNotDeterminedUnexpected (e.Message), rangeForErrors))
                             // This is defensive coding, we don't expect this exception to happen
                             if isRunningOnCoreClr then
                                 // If running on .NET Core and something goes wrong with getting the
@@ -894,4 +1006,5 @@ type internal FxResolver(assumeDotNetFramework: bool, projectDir: string, useSdk
                             getDotNetFrameworkDefaultReferences useFsiAuxLib, true
                 else
                     getDotNetCoreImplementationReferences useFsiAuxLib, assumeDotNetFramework
-        defaultReferences
+
+            defaultReferences)
diff --git a/src/Compiler/Driver/OptimizeInputs.fs b/src/Compiler/Driver/OptimizeInputs.fs
index 54f800b2a46..f2ce410199b 100644
--- a/src/Compiler/Driver/OptimizeInputs.fs
+++ b/src/Compiler/Driver/OptimizeInputs.fs
@@ -20,11 +20,16 @@ open FSharp.Compiler.TypedTreeOps
 
 let mutable showTermFileCount = 0
 
-let PrintWholeAssemblyImplementation g (tcConfig:TcConfig) outfile header expr =
+let PrintWholeAssemblyImplementation g (tcConfig: TcConfig) outfile header expr =
     if tcConfig.showTerms then
         if tcConfig.writeTermsToFiles then
             let fileName = outfile + ".terms"
-            use f = FileSystem.OpenFileForWriteShim(fileName + "-" + string showTermFileCount + "-" + header, FileMode.Create).GetWriter()
+
+            use f =
+                FileSystem
+                    .OpenFileForWriteShim(fileName + "-" + string showTermFileCount + "-" + header, FileMode.Create)
+                    .GetWriter()
+
             showTermFileCount <- showTermFileCount + 1
             LayoutRender.outL f (Display.squashTo 192 (DebugPrint.implFilesL g expr))
         else
@@ -37,13 +42,24 @@ let AddExternalCcuToOptimizationEnv tcGlobals optEnv (ccuinfo: ImportedAssembly)
     | None -> optEnv
     | Some data -> Optimizer.BindCcu ccuinfo.FSharpViewOfMetadata data optEnv tcGlobals
 
-let GetInitialOptimizationEnv (tcImports:TcImports, tcGlobals:TcGlobals) =
+let GetInitialOptimizationEnv (tcImports: TcImports, tcGlobals: TcGlobals) =
     let ccuinfos = tcImports.GetImportedAssemblies()
     let optEnv = Optimizer.IncrementalOptimizationEnv.Empty
     let optEnv = List.fold (AddExternalCcuToOptimizationEnv tcGlobals) optEnv ccuinfos
     optEnv
 
-let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importMap, isIncrementalFragment, optEnv, ccu:CcuThunk, implFiles) =
+let ApplyAllOptimizations
+    (
+        tcConfig: TcConfig,
+        tcGlobals,
+        tcVal,
+        outfile,
+        importMap,
+        isIncrementalFragment,
+        optEnv,
+        ccu: CcuThunk,
+        implFiles
+    ) =
     // NOTE: optEnv - threads through
     //
     // Always optimize once - the results of this step give the x-module optimization
@@ -52,7 +68,9 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
     PrintWholeAssemblyImplementation tcGlobals tcConfig outfile "pass-start" implFiles
 #if DEBUG
     if tcConfig.showOptimizationData then
-        dprintf "Expression prior to optimization:\n%s\n" (LayoutRender.showL (Display.squashTo 192 (DebugPrint.implFilesL tcGlobals implFiles)))
+        dprintf
+            "Expression prior to optimization:\n%s\n"
+            (LayoutRender.showL (Display.squashTo 192 (DebugPrint.implFilesL tcGlobals implFiles)))
 
     if tcConfig.showOptimizationData then
         dprintf "CCU prior to optimization:\n%s\n" (LayoutRender.showL (Display.squashTo 192 (DebugPrint.entityL tcGlobals ccu.Contents)))
@@ -63,8 +81,16 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
 
     // Only do abstract_big_targets on the first pass!  Only do it when TLR is on!
     let optSettings = tcConfig.optSettings
-    let optSettings = { optSettings with abstractBigTargets = tcConfig.doTLR }
-    let optSettings = { optSettings with reportingPhase = true }
+
+    let optSettings =
+        { optSettings with
+            abstractBigTargets = tcConfig.doTLR
+        }
+
+    let optSettings =
+        { optSettings with
+            reportingPhase = true
+        }
 
     let results, (optEnvFirstLoop, _, _, _) =
         ((optEnv0, optEnv0, optEnv0, SignatureHidingInfo.Empty), implFiles)
@@ -73,18 +99,33 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
 
             //ReportTime tcConfig ("Initial simplify")
             let (optEnvFirstLoop, implFile, implFileOptData, hidden), optimizeDuringCodeGen =
-                Optimizer.OptimizeImplFile
-                   (optSettings, ccu, tcGlobals, tcVal, importMap,
-                    optEnvFirstLoop, isIncrementalFragment, tcConfig.fsiMultiAssemblyEmit,
-                    tcConfig.emitTailcalls, hidden, implFile)
+                Optimizer.OptimizeImplFile(
+                    optSettings,
+                    ccu,
+                    tcGlobals,
+                    tcVal,
+                    importMap,
+                    optEnvFirstLoop,
+                    isIncrementalFragment,
+                    tcConfig.fsiMultiAssemblyEmit,
+                    tcConfig.emitTailcalls,
+                    hidden,
+                    implFile
+                )
 
             let implFile = LowerLocalMutables.TransformImplFile tcGlobals importMap implFile
 
             // Only do this on the first pass!
-            let optSettings = { optSettings with abstractBigTargets = false; reportingPhase = false }
+            let optSettings =
+                { optSettings with
+                    abstractBigTargets = false
+                    reportingPhase = false
+                }
 #if DEBUG
             if tcConfig.showOptimizationData then
-                dprintf "Optimization implFileOptData:\n%s\n" (LayoutRender.showL (Display.squashTo 192 (Optimizer.moduleInfoL tcGlobals implFileOptData)))
+                dprintf
+                    "Optimization implFileOptData:\n%s\n"
+                    (LayoutRender.showL (Display.squashTo 192 (Optimizer.moduleInfoL tcGlobals implFileOptData)))
 #endif
 
             let implFile, optEnvExtraLoop =
@@ -92,10 +133,19 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
 
                     //ReportTime tcConfig ("Extra simplification loop")
                     let (optEnvExtraLoop, implFile, _, _), _ =
-                        Optimizer.OptimizeImplFile
-                           (optSettings, ccu, tcGlobals, tcVal, importMap,
-                            optEnvExtraLoop, isIncrementalFragment, tcConfig.fsiMultiAssemblyEmit,
-                            tcConfig.emitTailcalls, hidden, implFile)
+                        Optimizer.OptimizeImplFile(
+                            optSettings,
+                            ccu,
+                            tcGlobals,
+                            tcVal,
+                            importMap,
+                            optEnvExtraLoop,
+                            isIncrementalFragment,
+                            tcConfig.fsiMultiAssemblyEmit,
+                            tcConfig.emitTailcalls,
+                            hidden,
+                            implFile
+                        )
 
                     //PrintWholeAssemblyImplementation tcConfig outfile (sprintf "extra-loop-%d" n) implFile
                     implFile, optEnvExtraLoop
@@ -108,24 +158,36 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
                     let implFile = implFile |> Detuple.DetupleImplFile ccu tcGlobals
                     //PrintWholeAssemblyImplementation tcConfig outfile "post-detuple" implFile
                     implFile
-                else implFile
+                else
+                    implFile
 
             let implFile =
                 if tcConfig.doTLR then
-                    implFile |> InnerLambdasToTopLevelFuncs.MakeTopLevelRepresentationDecisions ccu tcGlobals
-                else implFile
+                    implFile
+                    |> InnerLambdasToTopLevelFuncs.MakeTopLevelRepresentationDecisions ccu tcGlobals
+                else
+                    implFile
 
-            let implFile =
-                LowerCalls.LowerImplFile tcGlobals implFile
+            let implFile = LowerCalls.LowerImplFile tcGlobals implFile
 
             let implFile, optEnvFinalSimplify =
                 if tcConfig.doFinalSimplify then
 
                     //ReportTime tcConfig ("Final simplify pass")
                     let (optEnvFinalSimplify, implFile, _, _), _ =
-                        Optimizer.OptimizeImplFile
-                           (optSettings, ccu, tcGlobals, tcVal, importMap, optEnvFinalSimplify,
-                            isIncrementalFragment, tcConfig.fsiMultiAssemblyEmit, tcConfig.emitTailcalls, hidden, implFile)
+                        Optimizer.OptimizeImplFile(
+                            optSettings,
+                            ccu,
+                            tcGlobals,
+                            tcVal,
+                            importMap,
+                            optEnvFinalSimplify,
+                            isIncrementalFragment,
+                            tcConfig.fsiMultiAssemblyEmit,
+                            tcConfig.emitTailcalls,
+                            hidden,
+                            implFile
+                        )
 
                     //PrintWholeAssemblyImplementation tcConfig outfile "post-rec-opt" implFile
                     implFile, optEnvFinalSimplify
@@ -133,8 +195,10 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
                     implFile, optEnvFinalSimplify
 
             let implFile =
-                { ImplFile = implFile
-                  OptimizeDuringCodeGen = optimizeDuringCodeGen }
+                {
+                    ImplFile = implFile
+                    OptimizeDuringCodeGen = optimizeDuringCodeGen
+                }
 
             (implFile, implFileOptData), (optEnvFirstLoop, optEnvExtraLoop, optEnvFinalSimplify, hidden))
 
@@ -149,17 +213,20 @@ let ApplyAllOptimizations (tcConfig:TcConfig, tcGlobals, tcVal, outfile, importM
 // ILX generation
 //----------------------------------------------------------------------------
 
-let CreateIlxAssemblyGenerator (_tcConfig:TcConfig, tcImports:TcImports, tcGlobals, tcVal, generatedCcu) =
-    let ilxGenerator = IlxAssemblyGenerator(tcImports.GetImportMap(), tcGlobals, tcVal, generatedCcu)
+let CreateIlxAssemblyGenerator (_tcConfig: TcConfig, tcImports: TcImports, tcGlobals, tcVal, generatedCcu) =
+    let ilxGenerator =
+        IlxAssemblyGenerator(tcImports.GetImportMap(), tcGlobals, tcVal, generatedCcu)
+
     let ccus = tcImports.GetCcusInDeclOrder()
     ilxGenerator.AddExternalCcus ccus
     ilxGenerator
 
-let GenerateIlxCode (
+let GenerateIlxCode
+    (
         ilxBackend,
         isInteractiveItExpr,
         isInteractiveOnMono,
-        tcConfig:TcConfig,
+        tcConfig: TcConfig,
         topAttrs: TopAttribs,
         optimizedImpls,
         fragName,
@@ -167,35 +234,39 @@ let GenerateIlxCode (
     ) =
 
     let mainMethodInfo =
-        if (tcConfig.target = CompilerTarget.Dll) || (tcConfig.target = CompilerTarget.Module) then
-           None
-        else Some topAttrs.mainMethodAttrs
+        if (tcConfig.target = CompilerTarget.Dll)
+           || (tcConfig.target = CompilerTarget.Module) then
+            None
+        else
+            Some topAttrs.mainMethodAttrs
 
     let ilxGenOpts: IlxGenOptions =
-        { generateFilterBlocks = tcConfig.generateFilterBlocks
-          emitConstantArraysUsingStaticDataBlobs = not isInteractiveOnMono
-          workAroundReflectionEmitBugs = tcConfig.isInteractive
-          generateDebugSymbols = tcConfig.debuginfo
-          fragName = fragName
-          localOptimizationsEnabled= tcConfig.optSettings.LocalOptimizationsEnabled
-          testFlagEmitFeeFeeAs100001 = tcConfig.testFlagEmitFeeFeeAs100001
-          mainMethodInfo= mainMethodInfo
-          ilxBackend = ilxBackend
-          fsiMultiAssemblyEmit = tcConfig.fsiMultiAssemblyEmit
-          isInteractive = tcConfig.isInteractive
-          isInteractiveItExpr = isInteractiveItExpr
-          alwaysCallVirt = tcConfig.alwaysCallVirt }
-
-    ilxGenerator.GenerateCode (ilxGenOpts, optimizedImpls, topAttrs.assemblyAttrs, topAttrs.netModuleAttrs)
+        {
+            generateFilterBlocks = tcConfig.generateFilterBlocks
+            emitConstantArraysUsingStaticDataBlobs = not isInteractiveOnMono
+            workAroundReflectionEmitBugs = tcConfig.isInteractive
+            generateDebugSymbols = tcConfig.debuginfo
+            fragName = fragName
+            localOptimizationsEnabled = tcConfig.optSettings.LocalOptimizationsEnabled
+            testFlagEmitFeeFeeAs100001 = tcConfig.testFlagEmitFeeFeeAs100001
+            mainMethodInfo = mainMethodInfo
+            ilxBackend = ilxBackend
+            fsiMultiAssemblyEmit = tcConfig.fsiMultiAssemblyEmit
+            isInteractive = tcConfig.isInteractive
+            isInteractiveItExpr = isInteractiveItExpr
+            alwaysCallVirt = tcConfig.alwaysCallVirt
+        }
+
+    ilxGenerator.GenerateCode(ilxGenOpts, optimizedImpls, topAttrs.assemblyAttrs, topAttrs.netModuleAttrs)
 
 //----------------------------------------------------------------------------
 // Assembly ref normalization: make sure all assemblies are referred to
 // by the same references. Only used for static linking.
 //----------------------------------------------------------------------------
 
-let NormalizeAssemblyRefs (ctok, ilGlobals: ILGlobals, tcImports:TcImports) scoref =
+let NormalizeAssemblyRefs (ctok, ilGlobals: ILGlobals, tcImports: TcImports) scoref =
     let normalizeAssemblyRefByName nm =
-        match tcImports.TryFindDllInfo (ctok, Range.rangeStartup, nm, lookupOnly=false) with
+        match tcImports.TryFindDllInfo(ctok, Range.rangeStartup, nm, lookupOnly = false) with
         | Some dllInfo -> dllInfo.ILScopeRef
         | None -> scoref
 
@@ -205,7 +276,12 @@ let NormalizeAssemblyRefs (ctok, ilGlobals: ILGlobals, tcImports:TcImports) scor
     | ILScopeRef.PrimaryAssembly -> normalizeAssemblyRefByName ilGlobals.primaryAssemblyName
     | ILScopeRef.Assembly aref -> normalizeAssemblyRefByName aref.Name
 
-let GetGeneratedILModuleName (t:CompilerTarget) (s:string) =
+let GetGeneratedILModuleName (t: CompilerTarget) (s: string) =
     // return the name of the file as a module name
-    let ext = match t with CompilerTarget.Dll -> "dll" | CompilerTarget.Module -> "netmodule" | _ -> "exe"
+    let ext =
+        match t with
+        | CompilerTarget.Dll -> "dll"
+        | CompilerTarget.Module -> "netmodule"
+        | _ -> "exe"
+
     s + "." + ext
diff --git a/src/Compiler/Driver/ParseAndCheckInputs.fs b/src/Compiler/Driver/ParseAndCheckInputs.fs
index b98425605d1..889abccff5f 100644
--- a/src/Compiler/Driver/ParseAndCheckInputs.fs
+++ b/src/Compiler/Driver/ParseAndCheckInputs.fs
@@ -42,11 +42,20 @@ open FSharp.Compiler.TcGlobals
 
 let CanonicalizeFilename fileName =
     let basic = FileSystemUtils.fileNameOfPath fileName
-    String.capitalize (try FileSystemUtils.chopExtension basic with _ -> basic)
+
+    String.capitalize (
+        try
+            FileSystemUtils.chopExtension basic
+        with _ ->
+            basic
+    )
 
 let IsScript fileName =
     FSharpScriptFileSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName)
 
+let IsMLCompatFile fileName =
+    FSharpMLCompatFileSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName)
+
 // Give a unique name to the different kinds of inputs. Used to correlate signature and implementation files
 //   QualFileNameOfModuleName - files with a single module declaration or an anonymous module
 let QualFileNameOfModuleName m fileName modname =
@@ -61,186 +70,291 @@ let ComputeQualifiedNameOfFileFromUniquePath (m, p: string list) =
 
 let QualFileNameOfSpecs fileName specs =
     match specs with
-    | [SynModuleOrNamespaceSig(longId = modname; kind = kind; range = m)] when kind.IsModule -> QualFileNameOfModuleName m fileName modname
-    | [SynModuleOrNamespaceSig(kind = kind; range = m)] when not kind.IsModule -> QualFileNameOfFilename m fileName
+    | [ SynModuleOrNamespaceSig (longId = modname; kind = kind; range = m) ] when kind.IsModule ->
+        QualFileNameOfModuleName m fileName modname
+    | [ SynModuleOrNamespaceSig (kind = kind; range = m) ] when not kind.IsModule -> QualFileNameOfFilename m fileName
     | _ -> QualFileNameOfFilename (mkRange fileName pos0 pos0) fileName
 
 let QualFileNameOfImpls fileName specs =
     match specs with
-    | [SynModuleOrNamespace(longId = modname; kind = kind; range = m)] when kind.IsModule -> QualFileNameOfModuleName m fileName modname
-    | [SynModuleOrNamespace(kind = kind; range = m)] when not kind.IsModule -> QualFileNameOfFilename m fileName
+    | [ SynModuleOrNamespace (longId = modname; kind = kind; range = m) ] when kind.IsModule -> QualFileNameOfModuleName m fileName modname
+    | [ SynModuleOrNamespace (kind = kind; range = m) ] when not kind.IsModule -> QualFileNameOfFilename m fileName
     | _ -> QualFileNameOfFilename (mkRange fileName pos0 pos0) fileName
 
 let PrependPathToQualFileName x (QualifiedNameOfFile q) =
-    ComputeQualifiedNameOfFileFromUniquePath (q.idRange, pathOfLid x@[q.idText])
+    ComputeQualifiedNameOfFileFromUniquePath(q.idRange, pathOfLid x @ [ q.idText ])
 
-let PrependPathToImpl x (SynModuleOrNamespace(longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)) =
-    SynModuleOrNamespace(x@longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)
+let PrependPathToImpl x (SynModuleOrNamespace (longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)) =
+    SynModuleOrNamespace(x @ longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)
 
-let PrependPathToSpec x (SynModuleOrNamespaceSig(longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)) =
-    SynModuleOrNamespaceSig(x@longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)
+let PrependPathToSpec x (SynModuleOrNamespaceSig (longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)) =
+    SynModuleOrNamespaceSig(x @ longId, isRecursive, kind, decls, xmlDoc, attribs, accessibility, range, trivia)
 
 let PrependPathToInput x inp =
     match inp with
     | ParsedInput.ImplFile (ParsedImplFileInput (b, c, q, d, hd, impls, e, trivia)) ->
-        ParsedInput.ImplFile (ParsedImplFileInput (b, c, PrependPathToQualFileName x q, d, hd, List.map (PrependPathToImpl x) impls, e, trivia))
+        ParsedInput.ImplFile(
+            ParsedImplFileInput(b, c, PrependPathToQualFileName x q, d, hd, List.map (PrependPathToImpl x) impls, e, trivia)
+        )
 
     | ParsedInput.SigFile (ParsedSigFileInput (b, q, d, hd, specs, trivia)) ->
-        ParsedInput.SigFile (ParsedSigFileInput (b, PrependPathToQualFileName x q, d, hd, List.map (PrependPathToSpec x) specs, trivia))
+        ParsedInput.SigFile(ParsedSigFileInput(b, PrependPathToQualFileName x q, d, hd, List.map (PrependPathToSpec x) specs, trivia))
+
+let IsValidAnonModuleName (modname: string) =
+    modname |> String.forall (fun c -> Char.IsLetterOrDigit c || c = '_')
 
 let ComputeAnonModuleName check defaultNamespace fileName (m: range) =
     let modname = CanonicalizeFilename fileName
-    if check && not (modname |> String.forall (fun c -> Char.IsLetterOrDigit c || c = '_')) then
-          if not (fileName.EndsWith("fsx", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith("fsscript", StringComparison.OrdinalIgnoreCase)) then
-              warning(Error(FSComp.SR.buildImplicitModuleIsNotLegalIdentifier(modname, (FileSystemUtils.fileNameOfPath fileName)), m))
+
+    if check && not (IsValidAnonModuleName modname) && not (IsScript fileName) then
+        warning (Error(FSComp.SR.buildImplicitModuleIsNotLegalIdentifier (modname, (FileSystemUtils.fileNameOfPath fileName)), m))
+
     let combined =
-      match defaultNamespace with
-      | None -> modname
-      | Some ns -> textOfPath [ns;modname]
+        match defaultNamespace with
+        | None -> modname
+        | Some ns -> textOfPath [ ns; modname ]
 
     let anonymousModuleNameRange =
         let fileName = m.FileName
         mkRange fileName pos0 pos0
+
     pathToSynLid anonymousModuleNameRange (splitNamespace combined)
 
+let FileRequiresModuleOrNamespaceDecl isLast isExe fileName =
+    not (isLast && isExe) && not (IsScript fileName || IsMLCompatFile fileName)
+
 let PostParseModuleImpl (_i, defaultNamespace, isLastCompiland, fileName, impl) =
     match impl with
-    | ParsedImplFileFragment.NamedModule(SynModuleOrNamespace(lid, isRec, kind, decls, xmlDoc, attribs, access, m, trivia)) ->
+    | ParsedImplFileFragment.NamedModule (SynModuleOrNamespace (lid, isRec, kind, decls, xmlDoc, attribs, access, m, trivia)) ->
         let lid =
             match lid with
-            | [id] when kind.IsModule && id.idText = MangledGlobalName ->
-                error(Error(FSComp.SR.buildInvalidModuleOrNamespaceName(), id.idRange))
+            | [ id ] when kind.IsModule && id.idText = MangledGlobalName ->
+                error (Error(FSComp.SR.buildInvalidModuleOrNamespaceName (), id.idRange))
             | id :: rest when id.idText = MangledGlobalName -> rest
             | _ -> lid
+
         SynModuleOrNamespace(lid, isRec, kind, decls, xmlDoc, attribs, access, m, trivia)
 
-    | ParsedImplFileFragment.AnonModule (defs, m)->
+    | ParsedImplFileFragment.AnonModule (defs, m) ->
         let isLast, isExe = isLastCompiland
-        if not (isLast && isExe) && not (doNotRequireNamespaceOrModuleSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName)) then
+
+        if FileRequiresModuleOrNamespaceDecl isLast isExe fileName then
             match defs with
-            | SynModuleDecl.NestedModule _ :: _ -> errorR(Error(FSComp.SR.noEqualSignAfterModule(), trimRangeToLine m))
-            | _ -> errorR(Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule(), trimRangeToLine m))
+            | SynModuleDecl.NestedModule _ :: _ -> errorR (Error(FSComp.SR.noEqualSignAfterModule (), trimRangeToLine m))
+            | _ -> errorR (Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule (), trimRangeToLine m))
+
+        let modname =
+            ComputeAnonModuleName (not (isNil defs)) defaultNamespace fileName (trimRangeToLine m)
+
+        let trivia: SynModuleOrNamespaceTrivia =
+            {
+                ModuleKeyword = None
+                NamespaceKeyword = None
+            }
 
-        let modname = ComputeAnonModuleName (not (isNil defs)) defaultNamespace fileName (trimRangeToLine m)
-        let trivia: SynModuleOrNamespaceTrivia = { ModuleKeyword = None; NamespaceKeyword = None }
         SynModuleOrNamespace(modname, false, SynModuleOrNamespaceKind.AnonModule, defs, PreXmlDoc.Empty, [], None, m, trivia)
 
-    | ParsedImplFileFragment.NamespaceFragment (lid, isRecursive, kind, decls, xmlDoc, attributes, range, trivia)->
+    | ParsedImplFileFragment.NamespaceFragment (lid, isRecursive, kind, decls, xmlDoc, attributes, range, trivia) ->
         let lid, kind =
             match lid with
             | id :: rest when id.idText = MangledGlobalName ->
-                rest, if List.isEmpty rest then SynModuleOrNamespaceKind.GlobalNamespace else kind
+                let kind =
+                    if rest.IsEmpty then
+                        SynModuleOrNamespaceKind.GlobalNamespace
+                    else
+                        kind
+
+                rest, kind
             | _ -> lid, kind
+
         SynModuleOrNamespace(lid, isRecursive, kind, decls, xmlDoc, attributes, None, range, trivia)
 
 let PostParseModuleSpec (_i, defaultNamespace, isLastCompiland, fileName, intf) =
     match intf with
-    | ParsedSigFileFragment.NamedModule(SynModuleOrNamespaceSig(lid, isRec, kind, decls, xmlDoc, attribs, access, m, trivia)) ->
+    | ParsedSigFileFragment.NamedModule (SynModuleOrNamespaceSig (lid, isRec, kind, decls, xmlDoc, attribs, access, m, trivia)) ->
         let lid =
             match lid with
-            | [id] when kind.IsModule && id.idText = MangledGlobalName ->
-                error(Error(FSComp.SR.buildInvalidModuleOrNamespaceName(), id.idRange))
+            | [ id ] when kind.IsModule && id.idText = MangledGlobalName ->
+                error (Error(FSComp.SR.buildInvalidModuleOrNamespaceName (), id.idRange))
             | id :: rest when id.idText = MangledGlobalName -> rest
             | _ -> lid
+
         SynModuleOrNamespaceSig(lid, isRec, SynModuleOrNamespaceKind.NamedModule, decls, xmlDoc, attribs, access, m, trivia)
 
     | ParsedSigFileFragment.AnonModule (defs, m) ->
         let isLast, isExe = isLastCompiland
-        if not (isLast && isExe) && not (doNotRequireNamespaceOrModuleSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName)) then
+
+        if FileRequiresModuleOrNamespaceDecl isLast isExe fileName then
             match defs with
-            | SynModuleSigDecl.NestedModule _ :: _ -> errorR(Error(FSComp.SR.noEqualSignAfterModule(), m))
-            | _ -> errorR(Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule(), m))
+            | SynModuleSigDecl.NestedModule _ :: _ -> errorR (Error(FSComp.SR.noEqualSignAfterModule (), m))
+            | _ -> errorR (Error(FSComp.SR.buildMultiFileRequiresNamespaceOrModule (), m))
+
+        let modname =
+            ComputeAnonModuleName (not (isNil defs)) defaultNamespace fileName (trimRangeToLine m)
+
+        let trivia: SynModuleOrNamespaceSigTrivia =
+            {
+                ModuleKeyword = None
+                NamespaceKeyword = None
+            }
 
-        let modname = ComputeAnonModuleName (not (isNil defs)) defaultNamespace fileName (trimRangeToLine m)
-        let trivia: SynModuleOrNamespaceSigTrivia = { ModuleKeyword = None; NamespaceKeyword = None }
         SynModuleOrNamespaceSig(modname, false, SynModuleOrNamespaceKind.AnonModule, defs, PreXmlDoc.Empty, [], None, m, trivia)
 
-    | ParsedSigFileFragment.NamespaceFragment (lid, isRecursive, kind, decls, xmlDoc, attributes, range, trivia)->
+    | ParsedSigFileFragment.NamespaceFragment (lid, isRecursive, kind, decls, xmlDoc, attributes, range, trivia) ->
         let lid, kind =
             match lid with
             | id :: rest when id.idText = MangledGlobalName ->
-                rest, if List.isEmpty rest then SynModuleOrNamespaceKind.GlobalNamespace else kind
+                let kind =
+                    if rest.IsEmpty then
+                        SynModuleOrNamespaceKind.GlobalNamespace
+                    else
+                        kind
+
+                rest, kind
             | _ -> lid, kind
+
         SynModuleOrNamespaceSig(lid, isRecursive, kind, decls, xmlDoc, attributes, None, range, trivia)
 
 let GetScopedPragmasForInput input =
     match input with
-    | ParsedInput.SigFile (ParsedSigFileInput (scopedPragmas=pragmas)) -> pragmas
-    | ParsedInput.ImplFile (ParsedImplFileInput (scopedPragmas=pragmas)) -> pragmas
+    | ParsedInput.SigFile (ParsedSigFileInput (scopedPragmas = pragmas)) -> pragmas
+    | ParsedInput.ImplFile (ParsedImplFileInput (scopedPragmas = pragmas)) -> pragmas
 
 let GetScopedPragmasForHashDirective hd =
-    [ match hd with
-      | ParsedHashDirective("nowarn", numbers, m) ->
-          for s in numbers do
-              match s with
-              | ParsedHashDirectiveArgument.SourceIdentifier _ -> ()
-              | ParsedHashDirectiveArgument.String (s, _, _) ->
-                  match GetWarningNumber(m, s) with
-                  | None -> ()
-                  | Some n -> yield ScopedPragma.WarningOff(m, n)
-      | _ -> () ]
+    [
+        match hd with
+        | ParsedHashDirective ("nowarn", numbers, m) ->
+            for s in numbers do
+                match s with
+                | ParsedHashDirectiveArgument.SourceIdentifier _ -> ()
+                | ParsedHashDirectiveArgument.String (s, _, _) ->
+                    match GetWarningNumber(m, s) with
+                    | None -> ()
+                    | Some n -> ScopedPragma.WarningOff(m, n)
+        | _ -> ()
+    ]
 
 let private collectCodeComments (lexbuf: UnicodeLexing.Lexbuf) (tripleSlashComments: range list) =
-    [ yield! LexbufCommentStore.GetComments(lexbuf); yield! (List.map CommentTrivia.LineComment tripleSlashComments) ]
+    [
+        yield! LexbufCommentStore.GetComments(lexbuf)
+        yield! (List.map CommentTrivia.LineComment tripleSlashComments)
+    ]
     |> List.sortBy (function
         | CommentTrivia.LineComment r
         | CommentTrivia.BlockComment r -> r.StartLine, r.StartColumn)
 
-let PostParseModuleImpls (defaultNamespace, fileName, isLastCompiland, ParsedImplFile (hashDirectives, impls), lexbuf: UnicodeLexing.Lexbuf, tripleSlashComments: range list) =
-    match impls |> List.rev |> List.tryPick (function ParsedImplFileFragment.NamedModule(SynModuleOrNamespace(longId = lid)) -> Some lid | _ -> None) with
-    | Some lid when impls.Length > 1 ->
-        errorR(Error(FSComp.SR.buildMultipleToplevelModules(), rangeOfLid lid))
-    | _ ->
-        ()
-    let impls = impls |> List.mapi (fun i x -> PostParseModuleImpl (i, defaultNamespace, isLastCompiland, fileName, x))
+let PostParseModuleImpls
+    (
+        defaultNamespace,
+        fileName,
+        isLastCompiland,
+        ParsedImplFile (hashDirectives, impls),
+        lexbuf: UnicodeLexing.Lexbuf,
+        tripleSlashComments: range list
+    ) =
+    let othersWithSameName =
+        impls
+        |> List.rev
+        |> List.tryPick (function
+            | ParsedImplFileFragment.NamedModule (SynModuleOrNamespace (longId = lid)) -> Some lid
+            | _ -> None)
+
+    match othersWithSameName with
+    | Some lid when impls.Length > 1 -> errorR (Error(FSComp.SR.buildMultipleToplevelModules (), rangeOfLid lid))
+    | _ -> ()
+
+    let impls =
+        impls
+        |> List.mapi (fun i x -> PostParseModuleImpl(i, defaultNamespace, isLastCompiland, fileName, x))
+
     let qualName = QualFileNameOfImpls fileName impls
     let isScript = IsScript fileName
 
     let scopedPragmas =
-        [ for SynModuleOrNamespace(decls = decls) in impls do
-            for d in decls do
-                match d with
-                | SynModuleDecl.HashDirective (hd, _) -> yield! GetScopedPragmasForHashDirective hd
-                | _ -> ()
-          for hd in hashDirectives do
-              yield! GetScopedPragmasForHashDirective hd ]
+        [
+            for SynModuleOrNamespace (decls = decls) in impls do
+                for d in decls do
+                    match d with
+                    | SynModuleDecl.HashDirective (hd, _) -> yield! GetScopedPragmasForHashDirective hd
+                    | _ -> ()
+            for hd in hashDirectives do
+                yield! GetScopedPragmasForHashDirective hd
+        ]
 
     let conditionalDirectives = LexbufIfdefStore.GetTrivia(lexbuf)
     let codeComments = collectCodeComments lexbuf tripleSlashComments
-    let trivia: ParsedImplFileInputTrivia = { ConditionalDirectives = conditionalDirectives; CodeComments = codeComments }
-    
-    ParsedInput.ImplFile (ParsedImplFileInput (fileName, isScript, qualName, scopedPragmas, hashDirectives, impls, isLastCompiland, trivia))
-
-let PostParseModuleSpecs (defaultNamespace, fileName, isLastCompiland, ParsedSigFile (hashDirectives, specs), lexbuf: UnicodeLexing.Lexbuf, tripleSlashComments: range list) =
-    match specs |> List.rev |> List.tryPick (function ParsedSigFileFragment.NamedModule(SynModuleOrNamespaceSig(longId = lid)) -> Some lid | _ -> None) with
-    | Some lid when specs.Length > 1 ->
-        errorR(Error(FSComp.SR.buildMultipleToplevelModules(), rangeOfLid lid))
-    | _ ->
-        ()
 
-    let specs = specs |> List.mapi (fun i x -> PostParseModuleSpec(i, defaultNamespace, isLastCompiland, fileName, x))
+    let trivia: ParsedImplFileInputTrivia =
+        {
+            ConditionalDirectives = conditionalDirectives
+            CodeComments = codeComments
+        }
+
+    ParsedInput.ImplFile(ParsedImplFileInput(fileName, isScript, qualName, scopedPragmas, hashDirectives, impls, isLastCompiland, trivia))
+
+let PostParseModuleSpecs
+    (
+        defaultNamespace,
+        fileName,
+        isLastCompiland,
+        ParsedSigFile (hashDirectives, specs),
+        lexbuf: UnicodeLexing.Lexbuf,
+        tripleSlashComments: range list
+    ) =
+    let othersWithSameName =
+        specs
+        |> List.rev
+        |> List.tryPick (function
+            | ParsedSigFileFragment.NamedModule (SynModuleOrNamespaceSig (longId = lid)) -> Some lid
+            | _ -> None)
+
+    match othersWithSameName with
+    | Some lid when specs.Length > 1 -> errorR (Error(FSComp.SR.buildMultipleToplevelModules (), rangeOfLid lid))
+    | _ -> ()
+
+    let specs =
+        specs
+        |> List.mapi (fun i x -> PostParseModuleSpec(i, defaultNamespace, isLastCompiland, fileName, x))
+
     let qualName = QualFileNameOfSpecs fileName specs
+
     let scopedPragmas =
-        [ for SynModuleOrNamespaceSig(decls = decls) in specs do
-            for d in decls do
-                match d with
-                | SynModuleSigDecl.HashDirective(hd, _) -> yield! GetScopedPragmasForHashDirective hd
-                | _ -> ()
-          for hd in hashDirectives do
-              yield! GetScopedPragmasForHashDirective hd ]
+        [
+            for SynModuleOrNamespaceSig (decls = decls) in specs do
+                for d in decls do
+                    match d with
+                    | SynModuleSigDecl.HashDirective (hd, _) -> yield! GetScopedPragmasForHashDirective hd
+                    | _ -> ()
+            for hd in hashDirectives do
+                yield! GetScopedPragmasForHashDirective hd
+        ]
 
     let conditionalDirectives = LexbufIfdefStore.GetTrivia(lexbuf)
     let codeComments = collectCodeComments lexbuf tripleSlashComments
-    let trivia: ParsedSigFileInputTrivia = { ConditionalDirectives = conditionalDirectives; CodeComments = codeComments }
 
-    ParsedInput.SigFile (ParsedSigFileInput (fileName, qualName, scopedPragmas, hashDirectives, specs, trivia))
+    let trivia: ParsedSigFileInputTrivia =
+        {
+            ConditionalDirectives = conditionalDirectives
+            CodeComments = codeComments
+        }
+
+    ParsedInput.SigFile(ParsedSigFileInput(fileName, qualName, scopedPragmas, hashDirectives, specs, trivia))
 
-type ModuleNamesDict = Map>
+type ModuleNamesDict = Map>
 
 /// Checks if a module name is already given and deduplicates the name if needed.
 let DeduplicateModuleName (moduleNamesDict: ModuleNamesDict) fileName (qualNameOfFile: QualifiedNameOfFile) =
     let path = Path.GetDirectoryName fileName
-    let path = if FileSystem.IsPathRootedShim path then try FileSystem.GetFullPathShim path with _ -> path else path
+
+    let path =
+        if FileSystem.IsPathRootedShim path then
+            try
+                FileSystem.GetFullPathShim path
+            with _ ->
+                path
+        else
+            path
+
     match moduleNamesDict.TryGetValue qualNameOfFile.Text with
     | true, paths ->
         if paths.ContainsKey path then
@@ -248,26 +362,61 @@ let DeduplicateModuleName (moduleNamesDict: ModuleNamesDict) fileName (qualNameO
         else
             let count = paths.Count + 1
             let id = qualNameOfFile.Id
-            let qualNameOfFileT = if count = 1 then qualNameOfFile else QualifiedNameOfFile(Ident(id.idText + "___" + count.ToString(), id.idRange))
-            let moduleNamesDictT = moduleNamesDict.Add(qualNameOfFile.Text, paths.Add(path, qualNameOfFileT))
+
+            let qualNameOfFileT =
+                if count = 1 then
+                    qualNameOfFile
+                else
+                    QualifiedNameOfFile(Ident(id.idText + "___" + count.ToString(), id.idRange))
+
+            let moduleNamesDictT =
+                moduleNamesDict.Add(qualNameOfFile.Text, paths.Add(path, qualNameOfFileT))
+
             qualNameOfFileT, moduleNamesDictT
     | _ ->
-        let moduleNamesDictT = moduleNamesDict.Add(qualNameOfFile.Text, Map.empty.Add(path, qualNameOfFile))
+        let moduleNamesDictT =
+            moduleNamesDict.Add(qualNameOfFile.Text, Map.empty.Add(path, qualNameOfFile))
+
         qualNameOfFile, moduleNamesDictT
 
 /// Checks if a ParsedInput is using a module name that was already given and deduplicates the name if needed.
 let DeduplicateParsedInputModuleName (moduleNamesDict: ModuleNamesDict) input =
     match input with
-    | ParsedInput.ImplFile (ParsedImplFileInput.ParsedImplFileInput (fileName, isScript, qualNameOfFile, scopedPragmas, hashDirectives, modules, (isLastCompiland, isExe), trivia)) ->
-        let qualNameOfFileT, moduleNamesDictT = DeduplicateModuleName moduleNamesDict fileName qualNameOfFile
-        let inputT = ParsedInput.ImplFile (ParsedImplFileInput.ParsedImplFileInput (fileName, isScript, qualNameOfFileT, scopedPragmas, hashDirectives, modules, (isLastCompiland, isExe), trivia))
-        inputT, moduleNamesDictT
-    | ParsedInput.SigFile (ParsedSigFileInput.ParsedSigFileInput (fileName, qualNameOfFile, scopedPragmas, hashDirectives, modules, trivia)) ->
-        let qualNameOfFileT, moduleNamesDictT = DeduplicateModuleName moduleNamesDict fileName qualNameOfFile
-        let inputT = ParsedInput.SigFile (ParsedSigFileInput.ParsedSigFileInput (fileName, qualNameOfFileT, scopedPragmas, hashDirectives, modules, trivia))
-        inputT, moduleNamesDictT
-
-let ParseInput (lexer, diagnosticOptions:FSharpDiagnosticOptions, diagnosticsLogger: DiagnosticsLogger, lexbuf: UnicodeLexing.Lexbuf, defaultNamespace, fileName, isLastCompiland) =
+    | ParsedInput.ImplFile implFile ->
+        let (ParsedImplFileInput (fileName, isScript, qualNameOfFile, scopedPragmas, hashDirectives, modules, flags, trivia)) =
+            implFile
+
+        let qualNameOfFileR, moduleNamesDictR =
+            DeduplicateModuleName moduleNamesDict fileName qualNameOfFile
+
+        let implFileR =
+            ParsedImplFileInput(fileName, isScript, qualNameOfFileR, scopedPragmas, hashDirectives, modules, flags, trivia)
+
+        let inputR = ParsedInput.ImplFile implFileR
+        inputR, moduleNamesDictR
+    | ParsedInput.SigFile sigFile ->
+        let (ParsedSigFileInput (fileName, qualNameOfFile, scopedPragmas, hashDirectives, modules, trivia)) =
+            sigFile
+
+        let qualNameOfFileR, moduleNamesDictR =
+            DeduplicateModuleName moduleNamesDict fileName qualNameOfFile
+
+        let sigFileR =
+            ParsedSigFileInput(fileName, qualNameOfFileR, scopedPragmas, hashDirectives, modules, trivia)
+
+        let inputT = ParsedInput.SigFile sigFileR
+        inputT, moduleNamesDictR
+
+let ParseInput
+    (
+        lexer,
+        diagnosticOptions: FSharpDiagnosticOptions,
+        diagnosticsLogger: DiagnosticsLogger,
+        lexbuf: UnicodeLexing.Lexbuf,
+        defaultNamespace,
+        fileName,
+        isLastCompiland
+    ) =
     // The assert below is almost ok, but it fires in two cases:
     //  - fsi.exe sometimes passes "stdin" as a dummy file name
     //  - if you have a #line directive, e.g.
@@ -277,39 +426,46 @@ let ParseInput (lexer, diagnosticOptions:FSharpDiagnosticOptions, diagnosticsLog
     // Delay sending errors and warnings until after the file is parsed. This gives us a chance to scrape the
     // #nowarn declarations for the file
     let delayLogger = CapturingDiagnosticsLogger("Parsing")
-    use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> delayLogger)
+    use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> delayLogger)
     use unwindBP = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
 
     let mutable scopedPragmas = []
+
     try
         let input =
-            if mlCompatSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName) then
+            if FSharpMLCompatFileSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName) then
                 if lexbuf.SupportsFeature LanguageFeature.MLCompatRevisions then
-                    errorR(Error(FSComp.SR.buildInvalidSourceFileExtensionML fileName, rangeStartup))
+                    errorR (Error(FSComp.SR.buildInvalidSourceFileExtensionML fileName, rangeStartup))
                 else
-                    mlCompatWarning (FSComp.SR.buildCompilingExtensionIsForML()) rangeStartup
+                    mlCompatWarning (FSComp.SR.buildCompilingExtensionIsForML ()) rangeStartup
 
             // Call the appropriate parser - for signature files or implementation files
             if FSharpImplFileSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName) then
                 let impl = Parser.implementationFile lexer lexbuf
-                let tripleSlashComments = LexbufLocalXmlDocStore.ReportInvalidXmlDocPositions(lexbuf)
-                PostParseModuleImpls (defaultNamespace, fileName, isLastCompiland, impl, lexbuf, tripleSlashComments)
+
+                let tripleSlashComments =
+                    LexbufLocalXmlDocStore.ReportInvalidXmlDocPositions(lexbuf)
+
+                PostParseModuleImpls(defaultNamespace, fileName, isLastCompiland, impl, lexbuf, tripleSlashComments)
             elif FSharpSigFileSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName) then
                 let intfs = Parser.signatureFile lexer lexbuf
-                let tripleSlashComments =  LexbufLocalXmlDocStore.ReportInvalidXmlDocPositions(lexbuf)
-                PostParseModuleSpecs (defaultNamespace, fileName, isLastCompiland, intfs, lexbuf, tripleSlashComments)
-            else
-                if lexbuf.SupportsFeature LanguageFeature.MLCompatRevisions then
-                    error(Error(FSComp.SR.buildInvalidSourceFileExtensionUpdated fileName, rangeStartup))
-                else
-                    error(Error(FSComp.SR.buildInvalidSourceFileExtension fileName, rangeStartup))
 
+                let tripleSlashComments =
+                    LexbufLocalXmlDocStore.ReportInvalidXmlDocPositions(lexbuf)
+
+                PostParseModuleSpecs(defaultNamespace, fileName, isLastCompiland, intfs, lexbuf, tripleSlashComments)
+            else if lexbuf.SupportsFeature LanguageFeature.MLCompatRevisions then
+                error (Error(FSComp.SR.buildInvalidSourceFileExtensionUpdated fileName, rangeStartup))
+            else
+                error (Error(FSComp.SR.buildInvalidSourceFileExtension fileName, rangeStartup))
 
         scopedPragmas <- GetScopedPragmasForInput input
         input
     finally
         // OK, now commit the errors, since the ScopedPragmas will (hopefully) have been scraped
-        let filteringDiagnosticsLogger = GetDiagnosticsLoggerFilteringByScopedPragmas(false, scopedPragmas, diagnosticOptions, diagnosticsLogger)
+        let filteringDiagnosticsLogger =
+            GetDiagnosticsLoggerFilteringByScopedPragmas(false, scopedPragmas, diagnosticOptions, diagnosticsLogger)
+
         delayLogger.CommitDelayedDiagnostics filteringDiagnosticsLogger
 
 type Tokenizer = unit -> Parser.token
@@ -320,35 +476,47 @@ let ShowAllTokensAndExit (shortFilename, tokenizer: Tokenizer, lexbuf: LexBuffer
         printf "tokenize - getting one token from %s\n" shortFilename
         let t = tokenizer ()
         printf "tokenize - got %s @ %a\n" (Parser.token_to_string t) outputRange lexbuf.LexemeRange
+
         match t with
         | Parser.EOF _ -> exit 0
         | _ -> ()
-        if lexbuf.IsPastEndOfStream then printf "!!! at end of stream\n"
+
+        if lexbuf.IsPastEndOfStream then
+            printf "!!! at end of stream\n"
 
 // Test one of the parser entry points, just for testing purposes
 let TestInteractionParserAndExit (tokenizer: Tokenizer, lexbuf: LexBuffer) =
     while true do
         match (Parser.interaction (fun _ -> tokenizer ()) lexbuf) with
-        | ParsedScriptInteraction.Definitions(l, m) -> printfn "Parsed OK, got %d defs @ %a" l.Length outputRange m
-        | ParsedScriptInteraction.HashDirective(_, m) -> printfn "Parsed OK, got hash @ %a" outputRange m
+        | ParsedScriptInteraction.Definitions (l, m) -> printfn "Parsed OK, got %d defs @ %a" l.Length outputRange m
+        | ParsedScriptInteraction.HashDirective (_, m) -> printfn "Parsed OK, got hash @ %a" outputRange m
+
     exit 0
 
 // Report the statistics for testing purposes
 let ReportParsingStatistics res =
     let rec flattenSpecs specs =
-            specs |> List.collect (function SynModuleSigDecl.NestedModule (moduleDecls=subDecls) -> flattenSpecs subDecls | spec -> [spec])
+        specs
+        |> List.collect (function
+            | SynModuleSigDecl.NestedModule (moduleDecls = subDecls) -> flattenSpecs subDecls
+            | spec -> [ spec ])
+
     let rec flattenDefns specs =
-            specs |> List.collect (function SynModuleDecl.NestedModule (decls=subDecls) -> flattenDefns subDecls | defn -> [defn])
+        specs
+        |> List.collect (function
+            | SynModuleDecl.NestedModule (decls = subDecls) -> flattenDefns subDecls
+            | defn -> [ defn ])
+
+    let flattenModSpec (SynModuleOrNamespaceSig (decls = decls)) = flattenSpecs decls
+    let flattenModImpl (SynModuleOrNamespace (decls = decls)) = flattenDefns decls
 
-    let flattenModSpec (SynModuleOrNamespaceSig(decls = decls)) = flattenSpecs decls
-    let flattenModImpl (SynModuleOrNamespace(decls = decls)) = flattenDefns decls
     match res with
     | ParsedInput.SigFile (ParsedSigFileInput (modules = specs)) ->
         printfn "parsing yielded %d specs" (List.collect flattenModSpec specs).Length
     | ParsedInput.ImplFile (ParsedImplFileInput (modules = impls)) ->
         printfn "parsing yielded %d definitions" (List.collect flattenModImpl impls).Length
 
-let EmptyParsedInput(fileName, isLastCompiland) =
+let EmptyParsedInput (fileName, isLastCompiland) =
     if FSharpSigFileSuffixes |> List.exists (FileSystemUtils.checkSuffix fileName) then
         ParsedInput.SigFile(
             ParsedSigFileInput(
@@ -357,7 +525,10 @@ let EmptyParsedInput(fileName, isLastCompiland) =
                 [],
                 [],
                 [],
-                { ConditionalDirectives = []; CodeComments = [] }
+                {
+                    ConditionalDirectives = []
+                    CodeComments = []
+                }
             )
         )
     else
@@ -370,23 +541,29 @@ let EmptyParsedInput(fileName, isLastCompiland) =
                 [],
                 [],
                 isLastCompiland,
-                { ConditionalDirectives = []; CodeComments = [] }
+                {
+                    ConditionalDirectives = []
+                    CodeComments = []
+                }
             )
         )
 
 /// Parse an input, drawing tokens from the LexBuffer
 let ParseOneInputLexbuf (tcConfig: TcConfig, lexResourceManager, lexbuf, fileName, isLastCompiland, diagnosticsLogger) =
     use unwindbuildphase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
+
     try
 
         // Don't report whitespace from lexer
         let skipWhitespaceTokens = true
 
         // Set up the initial status for indentation-aware processing
-        let indentationSyntaxStatus = IndentationAwareSyntaxStatus (tcConfig.ComputeIndentationAwareSyntaxInitialStatus fileName, true)
+        let indentationSyntaxStatus =
+            IndentationAwareSyntaxStatus(tcConfig.ComputeIndentationAwareSyntaxInitialStatus fileName, true)
 
         // Set up the initial lexer arguments
-        let lexargs = mkLexargs (tcConfig.conditionalDefines, indentationSyntaxStatus, lexResourceManager, [], diagnosticsLogger, tcConfig.pathMap)
+        let lexargs =
+            mkLexargs (tcConfig.conditionalDefines, indentationSyntaxStatus, lexResourceManager, [], diagnosticsLogger, tcConfig.pathMap)
 
         // Set up the initial lexer arguments
         let shortFilename = SanitizeFileName fileName tcConfig.implicitIncludeDir
@@ -395,14 +572,29 @@ let ParseOneInputLexbuf (tcConfig: TcConfig, lexResourceManager, lexbuf, fileNam
             usingLexbufForParsing (lexbuf, fileName) (fun lexbuf ->
 
                 // Set up the LexFilter over the token stream
-                let tokenizer,tokenizeOnly =
+                let tokenizer, tokenizeOnly =
                     match tcConfig.tokenize with
-                    | TokenizeOption.Unfiltered ->
-                        (fun () -> Lexer.token lexargs skipWhitespaceTokens lexbuf), true
+                    | TokenizeOption.Unfiltered -> (fun () -> Lexer.token lexargs skipWhitespaceTokens lexbuf), true
                     | TokenizeOption.Only ->
-                        LexFilter.LexFilter(indentationSyntaxStatus, tcConfig.compilingFSharpCore, Lexer.token lexargs skipWhitespaceTokens, lexbuf).GetToken, true
+                        LexFilter
+                            .LexFilter(
+                                indentationSyntaxStatus,
+                                tcConfig.compilingFSharpCore,
+                                Lexer.token lexargs skipWhitespaceTokens,
+                                lexbuf
+                            )
+                            .GetToken,
+                        true
                     | _ ->
-                        LexFilter.LexFilter(indentationSyntaxStatus, tcConfig.compilingFSharpCore, Lexer.token lexargs skipWhitespaceTokens, lexbuf).GetToken, false
+                        LexFilter
+                            .LexFilter(
+                                indentationSyntaxStatus,
+                                tcConfig.compilingFSharpCore,
+                                Lexer.token lexargs skipWhitespaceTokens,
+                                lexbuf
+                            )
+                            .GetToken,
+                        false
 
                 // If '--tokenize' then show the tokens now and exit
                 if tokenizeOnly then
@@ -410,44 +602,71 @@ let ParseOneInputLexbuf (tcConfig: TcConfig, lexResourceManager, lexbuf, fileNam
 
                 // Test hook for one of the parser entry points
                 if tcConfig.testInteractionParser then
-                    TestInteractionParserAndExit (tokenizer, lexbuf)
+                    TestInteractionParserAndExit(tokenizer, lexbuf)
 
                 // Parse the input
-                let res = ParseInput((fun _ -> tokenizer ()), tcConfig.diagnosticsOptions, diagnosticsLogger, lexbuf, None, fileName, isLastCompiland)
+                let res =
+                    ParseInput(
+                        (fun _ -> tokenizer ()),
+                        tcConfig.diagnosticsOptions,
+                        diagnosticsLogger,
+                        lexbuf,
+                        None,
+                        fileName,
+                        isLastCompiland
+                    )
 
                 // Report the statistics for testing purposes
-                if tcConfig.reportNumDecls then
-                    ReportParsingStatistics res
+                if tcConfig.reportNumDecls then ReportParsingStatistics res
+
+                res)
 
-                res
-            )
         input
 
     with exn ->
         errorRecovery exn rangeStartup
         EmptyParsedInput(fileName, isLastCompiland)
 
-let ValidSuffixes = FSharpSigFileSuffixes@FSharpImplFileSuffixes
+let ValidSuffixes = FSharpSigFileSuffixes @ FSharpImplFileSuffixes
 
 let checkInputFile (tcConfig: TcConfig) fileName =
     if List.exists (FileSystemUtils.checkSuffix fileName) ValidSuffixes then
-        if not(FileSystem.FileExistsShim fileName) then
-            error(Error(FSComp.SR.buildCouldNotFindSourceFile fileName, rangeStartup))
+        if not (FileSystem.FileExistsShim fileName) then
+            error (Error(FSComp.SR.buildCouldNotFindSourceFile fileName, rangeStartup))
     else
-        error(Error(FSComp.SR.buildInvalidSourceFileExtension(SanitizeFileName fileName tcConfig.implicitIncludeDir), rangeStartup))
+        error (Error(FSComp.SR.buildInvalidSourceFileExtension (SanitizeFileName fileName tcConfig.implicitIncludeDir), rangeStartup))
 
-let parseInputStreamAux (tcConfig: TcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked, stream: Stream) =
+let parseInputStreamAux
+    (
+        tcConfig: TcConfig,
+        lexResourceManager,
+        fileName,
+        isLastCompiland,
+        diagnosticsLogger,
+        retryLocked,
+        stream: Stream
+    ) =
     use reader = stream.GetReader(tcConfig.inputCodePage, retryLocked)
 
     // Set up the LexBuffer for the file
-    let lexbuf = UnicodeLexing.StreamReaderAsLexbuf(not tcConfig.compilingFSharpCore, tcConfig.langVersion, reader)
+    let lexbuf =
+        UnicodeLexing.StreamReaderAsLexbuf(not tcConfig.compilingFSharpCore, tcConfig.langVersion, reader)
 
     // Parse the file drawing tokens from the lexbuf
     ParseOneInputLexbuf(tcConfig, lexResourceManager, lexbuf, fileName, isLastCompiland, diagnosticsLogger)
 
-let parseInputSourceTextAux (tcConfig: TcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, sourceText: ISourceText) =
+let parseInputSourceTextAux
+    (
+        tcConfig: TcConfig,
+        lexResourceManager,
+        fileName,
+        isLastCompiland,
+        diagnosticsLogger,
+        sourceText: ISourceText
+    ) =
     // Set up the LexBuffer for the file
-    let lexbuf = UnicodeLexing.SourceTextAsLexbuf(not tcConfig.compilingFSharpCore, tcConfig.langVersion, sourceText)
+    let lexbuf =
+        UnicodeLexing.SourceTextAsLexbuf(not tcConfig.compilingFSharpCore, tcConfig.langVersion, sourceText)
 
     // Parse the file drawing tokens from the lexbuf
     ParseOneInputLexbuf(tcConfig, lexResourceManager, lexbuf, fileName, isLastCompiland, diagnosticsLogger)
@@ -458,23 +677,41 @@ let parseInputFileAux (tcConfig: TcConfig, lexResourceManager, fileName, isLastC
     use reader = fileStream.GetReader(tcConfig.inputCodePage, retryLocked)
 
     // Set up the LexBuffer for the file
-    let lexbuf = UnicodeLexing.StreamReaderAsLexbuf(not tcConfig.compilingFSharpCore, tcConfig.langVersion, reader)
+    let lexbuf =
+        UnicodeLexing.StreamReaderAsLexbuf(not tcConfig.compilingFSharpCore, tcConfig.langVersion, reader)
 
     // Parse the file drawing tokens from the lexbuf
     ParseOneInputLexbuf(tcConfig, lexResourceManager, lexbuf, fileName, isLastCompiland, diagnosticsLogger)
 
 /// Parse an input from stream
-let ParseOneInputStream (tcConfig: TcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked, stream: Stream) =
+let ParseOneInputStream
+    (
+        tcConfig: TcConfig,
+        lexResourceManager,
+        fileName,
+        isLastCompiland,
+        diagnosticsLogger,
+        retryLocked,
+        stream: Stream
+    ) =
     try
-       parseInputStreamAux(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked, stream)
+        parseInputStreamAux (tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked, stream)
     with exn ->
         errorRecovery exn rangeStartup
         EmptyParsedInput(fileName, isLastCompiland)
 
 /// Parse an input from source text
-let ParseOneInputSourceText (tcConfig: TcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, sourceText: ISourceText) =
+let ParseOneInputSourceText
+    (
+        tcConfig: TcConfig,
+        lexResourceManager,
+        fileName,
+        isLastCompiland,
+        diagnosticsLogger,
+        sourceText: ISourceText
+    ) =
     try
-       parseInputSourceTextAux(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, sourceText)
+        parseInputSourceTextAux (tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, sourceText)
     with exn ->
         errorRecovery exn rangeStartup
         EmptyParsedInput(fileName, isLastCompiland)
@@ -482,31 +719,43 @@ let ParseOneInputSourceText (tcConfig: TcConfig, lexResourceManager, fileName, i
 /// Parse an input from disk
 let ParseOneInputFile (tcConfig: TcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked) =
     try
-       checkInputFile tcConfig fileName
-       parseInputFileAux(tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked)
+        checkInputFile tcConfig fileName
+        parseInputFileAux (tcConfig, lexResourceManager, fileName, isLastCompiland, diagnosticsLogger, retryLocked)
     with exn ->
         errorRecovery exn rangeStartup
         EmptyParsedInput(fileName, isLastCompiland)
 
 /// Parse multiple input files from disk
-let ParseInputFiles (tcConfig: TcConfig, lexResourceManager, sourceFiles, diagnosticsLogger: DiagnosticsLogger, exiter: Exiter, createDiagnosticsLogger: Exiter -> CapturingDiagnosticsLogger, retryLocked) =
+let ParseInputFiles
+    (
+        tcConfig: TcConfig,
+        lexResourceManager,
+        sourceFiles,
+        diagnosticsLogger: DiagnosticsLogger,
+        exiter: Exiter,
+        createDiagnosticsLogger: Exiter -> CapturingDiagnosticsLogger,
+        retryLocked
+    ) =
     try
         let isLastCompiland, isExe = sourceFiles |> tcConfig.ComputeCanContainEntryPoint
         let sourceFiles = isLastCompiland |> List.zip sourceFiles |> Array.ofList
 
         if tcConfig.concurrentBuild then
             let mutable exitCode = 0
+
             let delayedExiter =
                 { new Exiter with
-                    member _.Exit n = exitCode <- n; raise StopProcessing }
+                    member _.Exit n =
+                        exitCode <- n
+                        raise StopProcessing
+                }
 
             // Check input files and create delayed error loggers before we try to parallel parse.
             let delayedDiagnosticsLoggers =
                 sourceFiles
                 |> Array.map (fun (fileName, _) ->
                     checkInputFile tcConfig fileName
-                    createDiagnosticsLogger(delayedExiter)
-                )
+                    createDiagnosticsLogger (delayedExiter))
 
             let results =
                 try
@@ -516,25 +765,33 @@ let ParseInputFiles (tcConfig: TcConfig, lexResourceManager, sourceFiles, diagno
                             let delayedDiagnosticsLogger = delayedDiagnosticsLoggers[i]
 
                             let directoryName = Path.GetDirectoryName fileName
-                            let input = parseInputFileAux(tcConfig, lexResourceManager, fileName, (isLastCompiland, isExe), delayedDiagnosticsLogger, retryLocked)
-                            (input, directoryName)
-                        )
+
+                            let input =
+                                parseInputFileAux (
+                                    tcConfig,
+                                    lexResourceManager,
+                                    fileName,
+                                    (isLastCompiland, isExe),
+                                    delayedDiagnosticsLogger,
+                                    retryLocked
+                                )
+
+                            (input, directoryName))
                     finally
                         delayedDiagnosticsLoggers
-                        |> Array.iter (fun delayedDiagnosticsLogger ->
-                            delayedDiagnosticsLogger.CommitDelayedDiagnostics diagnosticsLogger
-                        )
-                with
-                | StopProcessing ->
+                        |> Array.iter (fun delayedDiagnosticsLogger -> delayedDiagnosticsLogger.CommitDelayedDiagnostics diagnosticsLogger)
+                with StopProcessing ->
                     exiter.Exit exitCode
 
-            results
-            |> List.ofArray
+            results |> List.ofArray
         else
             sourceFiles
             |> Array.map (fun (fileName, isLastCompiland) ->
                 let directoryName = Path.GetDirectoryName fileName
-                let input = ParseOneInputFile(tcConfig, lexResourceManager, fileName, (isLastCompiland, isExe), diagnosticsLogger, retryLocked)
+
+                let input =
+                    ParseOneInputFile(tcConfig, lexResourceManager, fileName, (isLastCompiland, isExe), diagnosticsLogger, retryLocked)
+
                 (input, directoryName))
             |> List.ofArray
 
@@ -543,13 +800,11 @@ let ParseInputFiles (tcConfig: TcConfig, lexResourceManager, sourceFiles, diagno
         exiter.Exit 1
 
 let ProcessMetaCommandsFromInput
-     (nowarnF: 'state -> range * string -> 'state,
-      hashReferenceF: 'state -> range * string * Directive -> 'state,
-      loadSourceF: 'state -> range * string -> unit)
-     (tcConfig:TcConfigBuilder,
-      inp: ParsedInput,
-      pathOfMetaCommandSource,
-      state0) =
+    (nowarnF: 'state -> range * string -> 'state,
+     hashReferenceF: 'state -> range * string * Directive -> 'state,
+     loadSourceF: 'state -> range * string -> unit)
+    (tcConfig: TcConfigBuilder, inp: ParsedInput, pathOfMetaCommandSource, state0)
+    =
 
     use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
 
@@ -560,105 +815,118 @@ let ProcessMetaCommandsFromInput
 
     let ProcessDependencyManagerDirective directive args m state =
         if not canHaveScriptMetaCommands then
-            errorR(HashReferenceNotAllowedInNonScript m)
+            errorR (HashReferenceNotAllowedInNonScript m)
 
         match args with
-        | [path] ->
-            let p =
-                if String.IsNullOrWhiteSpace(path) then ""
-                else path
+        | [ path ] ->
+            let p = if String.IsNullOrWhiteSpace(path) then "" else path
 
             hashReferenceF state (m, p, directive)
 
         | _ ->
-            errorR(Error(FSComp.SR.buildInvalidHashrDirective(), m))
+            errorR (Error(FSComp.SR.buildInvalidHashrDirective (), m))
             state
 
     let ProcessMetaCommand state hash =
         let mutable matchedm = range0
+
         try
             match hash with
-            | ParsedHashDirective("I", ParsedHashDirectiveArguments args, m) ->
+            | ParsedHashDirective ("I", ParsedHashDirectiveArguments args, m) ->
                 if not canHaveScriptMetaCommands then
-                    errorR(HashIncludeNotAllowedInNonScript m)
+                    errorR (HashIncludeNotAllowedInNonScript m)
+
                 match args with
-                | [path] ->
+                | [ path ] ->
                     matchedm <- m
                     tcConfig.AddIncludePath(m, path, pathOfMetaCommandSource)
                     state
                 | _ ->
-                    errorR(Error(FSComp.SR.buildInvalidHashIDirective(), m))
+                    errorR (Error(FSComp.SR.buildInvalidHashIDirective (), m))
                     state
-            | ParsedHashDirective("nowarn", ParsedHashDirectiveArguments numbers,m) ->
-                List.fold (fun state d -> nowarnF state (m,d)) state numbers
-
-            | ParsedHashDirective(("reference" | "r"), ParsedHashDirectiveArguments args, m) ->
-                matchedm<-m
+            | ParsedHashDirective ("nowarn", ParsedHashDirectiveArguments numbers, m) ->
+                List.fold (fun state d -> nowarnF state (m, d)) state numbers
+
+            | ParsedHashDirective (("reference"
+                                   | "r"),
+                                   ParsedHashDirectiveArguments args,
+                                   m) ->
+                matchedm <- m
                 ProcessDependencyManagerDirective Directive.Resolution args m state
 
-            | ParsedHashDirective("i", ParsedHashDirectiveArguments args, m) ->
-                matchedm<-m
+            | ParsedHashDirective ("i", ParsedHashDirectiveArguments args, m) ->
+                matchedm <- m
                 ProcessDependencyManagerDirective Directive.Include args m state
 
-            | ParsedHashDirective("load", ParsedHashDirectiveArguments args, m) ->
+            | ParsedHashDirective ("load", ParsedHashDirectiveArguments args, m) ->
                 if not canHaveScriptMetaCommands then
-                    errorR(HashDirectiveNotAllowedInNonScript m)
+                    errorR (HashDirectiveNotAllowedInNonScript m)
+
                 match args with
                 | _ :: _ ->
-                   matchedm<-m
-                   args |> List.iter (fun path -> loadSourceF state (m, path))
-                | _ ->
-                   errorR(Error(FSComp.SR.buildInvalidHashloadDirective(), m))
+                    matchedm <- m
+                    args |> List.iter (fun path -> loadSourceF state (m, path))
+                | _ -> errorR (Error(FSComp.SR.buildInvalidHashloadDirective (), m))
+
                 state
-            | ParsedHashDirective("time", ParsedHashDirectiveArguments args, m) ->
+            | ParsedHashDirective ("time", ParsedHashDirectiveArguments args, m) ->
                 if not canHaveScriptMetaCommands then
-                    errorR(HashDirectiveNotAllowedInNonScript m)
+                    errorR (HashDirectiveNotAllowedInNonScript m)
+
                 match args with
-                | [] ->
-                     ()
-                | ["on" | "off"] ->
-                    ()
-                | _ ->
-                    errorR(Error(FSComp.SR.buildInvalidHashtimeDirective(), m))
+                | [] -> ()
+                | [ "on" | "off" ] -> ()
+                | _ -> errorR (Error(FSComp.SR.buildInvalidHashtimeDirective (), m))
+
                 state
 
             | _ ->
 
                 (* warning(Error("This meta-command has been ignored", m)) *)
                 state
-        with e -> errorRecovery e matchedm; state
+        with e ->
+            errorRecovery e matchedm
+            state
 
     let rec WarnOnIgnoredSpecDecls decls =
-        decls |> List.iter (fun d ->
+        decls
+        |> List.iter (fun d ->
             match d with
-            | SynModuleSigDecl.HashDirective (_, m) -> warning(Error(FSComp.SR.buildDirectivesInModulesAreIgnored(), m))
-            | SynModuleSigDecl.NestedModule (moduleDecls=subDecls) -> WarnOnIgnoredSpecDecls subDecls
+            | SynModuleSigDecl.HashDirective (_, m) -> warning (Error(FSComp.SR.buildDirectivesInModulesAreIgnored (), m))
+            | SynModuleSigDecl.NestedModule (moduleDecls = subDecls) -> WarnOnIgnoredSpecDecls subDecls
             | _ -> ())
 
     let rec WarnOnIgnoredImplDecls decls =
-        decls |> List.iter (fun d ->
+        decls
+        |> List.iter (fun d ->
             match d with
-            | SynModuleDecl.HashDirective (_, m) -> warning(Error(FSComp.SR.buildDirectivesInModulesAreIgnored(), m))
-            | SynModuleDecl.NestedModule (decls=subDecls) -> WarnOnIgnoredImplDecls subDecls
+            | SynModuleDecl.HashDirective (_, m) -> warning (Error(FSComp.SR.buildDirectivesInModulesAreIgnored (), m))
+            | SynModuleDecl.NestedModule (decls = subDecls) -> WarnOnIgnoredImplDecls subDecls
             | _ -> ())
 
-    let ProcessMetaCommandsFromModuleSpec state (SynModuleOrNamespaceSig(decls = decls)) =
-        List.fold (fun s d ->
-            match d with
-            | SynModuleSigDecl.HashDirective (h, _) -> ProcessMetaCommand s h
-            | SynModuleSigDecl.NestedModule (moduleDecls=subDecls) -> WarnOnIgnoredSpecDecls subDecls; s
-            | _ -> s)
-         state
-         decls
-
-    let ProcessMetaCommandsFromModuleImpl state (SynModuleOrNamespace(decls = decls)) =
-        List.fold (fun s d ->
-            match d with
-            | SynModuleDecl.HashDirective (h, _) -> ProcessMetaCommand s h
-            | SynModuleDecl.NestedModule (decls=subDecls) -> WarnOnIgnoredImplDecls subDecls; s
-            | _ -> s)
-         state
-         decls
+    let ProcessMetaCommandsFromModuleSpec state (SynModuleOrNamespaceSig (decls = decls)) =
+        List.fold
+            (fun s d ->
+                match d with
+                | SynModuleSigDecl.HashDirective (h, _) -> ProcessMetaCommand s h
+                | SynModuleSigDecl.NestedModule (moduleDecls = subDecls) ->
+                    WarnOnIgnoredSpecDecls subDecls
+                    s
+                | _ -> s)
+            state
+            decls
+
+    let ProcessMetaCommandsFromModuleImpl state (SynModuleOrNamespace (decls = decls)) =
+        List.fold
+            (fun s d ->
+                match d with
+                | SynModuleDecl.HashDirective (h, _) -> ProcessMetaCommand s h
+                | SynModuleDecl.NestedModule (decls = subDecls) ->
+                    WarnOnIgnoredImplDecls subDecls
+                    s
+                | _ -> s)
+            state
+            decls
 
     match inp with
     | ParsedInput.SigFile (ParsedSigFileInput (hashDirectives = hashDirectives; modules = specs)) ->
@@ -673,24 +941,25 @@ let ProcessMetaCommandsFromInput
 let ApplyNoWarnsToTcConfig (tcConfig: TcConfig, inp: ParsedInput, pathOfMetaCommandSource) =
     // Clone
     let tcConfigB = tcConfig.CloneToBuilder()
-    let addNoWarn = fun () (m,s) -> tcConfigB.TurnWarningOff(m, s)
+    let addNoWarn = fun () (m, s) -> tcConfigB.TurnWarningOff(m, s)
     let addReference = fun () (_m, _s, _) -> ()
     let addLoadedSource = fun () (_m, _s) -> ()
-    ProcessMetaCommandsFromInput
-        (addNoWarn, addReference, addLoadedSource)
-        (tcConfigB, inp, pathOfMetaCommandSource, ())
-    TcConfig.Create(tcConfigB, validate=false)
+    ProcessMetaCommandsFromInput (addNoWarn, addReference, addLoadedSource) (tcConfigB, inp, pathOfMetaCommandSource, ())
+    TcConfig.Create(tcConfigB, validate = false)
 
 let ApplyMetaCommandsFromInputToTcConfig (tcConfig: TcConfig, inp: ParsedInput, pathOfMetaCommandSource, dependencyProvider) =
     // Clone
     let tcConfigB = tcConfig.CloneToBuilder()
     let getWarningNumber = fun () _ -> ()
-    let addReferenceDirective = fun () (m, path, directive) -> tcConfigB.AddReferenceDirective(dependencyProvider, m, path, directive)
-    let addLoadedSource = fun () (m,s) -> tcConfigB.AddLoadedSource(m,s,pathOfMetaCommandSource)
-    ProcessMetaCommandsFromInput
-        (getWarningNumber, addReferenceDirective, addLoadedSource)
-        (tcConfigB, inp, pathOfMetaCommandSource, ())
-    TcConfig.Create(tcConfigB, validate=false)
+
+    let addReferenceDirective =
+        fun () (m, path, directive) -> tcConfigB.AddReferenceDirective(dependencyProvider, m, path, directive)
+
+    let addLoadedSource =
+        fun () (m, s) -> tcConfigB.AddLoadedSource(m, s, pathOfMetaCommandSource)
+
+    ProcessMetaCommandsFromInput (getWarningNumber, addReferenceDirective, addLoadedSource) (tcConfigB, inp, pathOfMetaCommandSource, ())
+    TcConfig.Create(tcConfigB, validate = false)
 
 /// Build the initial type checking environment
 let GetInitialTcEnv (assemblyName: string, initm: range, tcConfig: TcConfig, tcImports: TcImports, tcGlobals) =
@@ -702,12 +971,17 @@ let GetInitialTcEnv (assemblyName: string, initm: range, tcConfig: TcConfig, tcI
 
     let amap = tcImports.GetImportMap()
 
-    let openDecls0, tcEnv = CreateInitialTcEnv(tcGlobals, amap, initm, assemblyName, ccus)
+    let openDecls0, tcEnv =
+        CreateInitialTcEnv(tcGlobals, amap, initm, assemblyName, ccus)
 
     if tcConfig.checkOverflow then
-        try 
-            let checkOperatorsModule = pathToSynLid initm (splitNamespace CoreOperatorsCheckedName)
-            let tcEnv, openDecls1 = TcOpenModuleOrNamespaceDecl TcResultsSink.NoSink tcGlobals amap initm tcEnv (checkOperatorsModule, initm)
+        try
+            let checkOperatorsModule =
+                pathToSynLid initm (splitNamespace CoreOperatorsCheckedName)
+
+            let tcEnv, openDecls1 =
+                TcOpenModuleOrNamespaceDecl TcResultsSink.NoSink tcGlobals amap initm tcEnv (checkOperatorsModule, initm)
+
             tcEnv, openDecls0 @ openDecls1
         with e ->
             errorRecovery e initm
@@ -716,27 +990,27 @@ let GetInitialTcEnv (assemblyName: string, initm: range, tcConfig: TcConfig, tcI
         tcEnv, openDecls0
 
 /// Inject faults into checking
-let CheckSimulateException(tcConfig: TcConfig) =
+let CheckSimulateException (tcConfig: TcConfig) =
     match tcConfig.simulateException with
-    | Some("tc-oom") -> raise(OutOfMemoryException())
-    | Some("tc-an") -> raise(ArgumentNullException("simulated"))
-    | Some("tc-invop") -> raise(InvalidOperationException())
-    | Some("tc-av") -> raise(AccessViolationException())
-    | Some("tc-nfn") -> raise(NotFiniteNumberException())
-    | Some("tc-aor") -> raise(ArgumentOutOfRangeException())
-    | Some("tc-dv0") -> raise(DivideByZeroException())
-    | Some("tc-oe") -> raise(OverflowException())
-    | Some("tc-atmm") -> raise(ArrayTypeMismatchException())
-    | Some("tc-bif") -> raise(BadImageFormatException())
-    | Some("tc-knf") -> raise(KeyNotFoundException())
-    | Some("tc-ior") -> raise(IndexOutOfRangeException())
-    | Some("tc-ic") -> raise(InvalidCastException())
-    | Some("tc-ip") -> raise(InvalidProgramException())
-    | Some("tc-ma") -> raise(MemberAccessException())
-    | Some("tc-ni") -> raise(NotImplementedException())
-    | Some("tc-nr") -> raise(NullReferenceException())
-    | Some("tc-oc") -> raise(OperationCanceledException())
-    | Some("tc-fail") -> failwith "simulated"
+    | Some ("tc-oom") -> raise (OutOfMemoryException())
+    | Some ("tc-an") -> raise (ArgumentNullException("simulated"))
+    | Some ("tc-invop") -> raise (InvalidOperationException())
+    | Some ("tc-av") -> raise (AccessViolationException())
+    | Some ("tc-nfn") -> raise (NotFiniteNumberException())
+    | Some ("tc-aor") -> raise (ArgumentOutOfRangeException())
+    | Some ("tc-dv0") -> raise (DivideByZeroException())
+    | Some ("tc-oe") -> raise (OverflowException())
+    | Some ("tc-atmm") -> raise (ArrayTypeMismatchException())
+    | Some ("tc-bif") -> raise (BadImageFormatException())
+    | Some ("tc-knf") -> raise (KeyNotFoundException())
+    | Some ("tc-ior") -> raise (IndexOutOfRangeException())
+    | Some ("tc-ic") -> raise (InvalidCastException())
+    | Some ("tc-ip") -> raise (InvalidProgramException())
+    | Some ("tc-ma") -> raise (MemberAccessException())
+    | Some ("tc-ni") -> raise (NotImplementedException())
+    | Some ("tc-nr") -> raise (NullReferenceException())
+    | Some ("tc-oc") -> raise (OperationCanceledException())
+    | Some ("tc-fail") -> failwith "simulated"
     | _ -> ()
 
 //----------------------------------------------------------------------------
@@ -745,24 +1019,24 @@ let CheckSimulateException(tcConfig: TcConfig) =
 
 type RootSigs = Zmap
 
-type RootImpls = Zset
+type RootImpls = Zset
 
 let qnameOrder = Order.orderBy (fun (q: QualifiedNameOfFile) -> q.Text)
 
 type TcState =
     {
-      tcsCcu: CcuThunk
-      tcsCcuType: ModuleOrNamespace
-      tcsNiceNameGen: NiceNameGenerator
-      tcsTcSigEnv: TcEnv
-      tcsTcImplEnv: TcEnv
-      tcsCreatesGeneratedProvidedTypes: bool
-      tcsRootSigs: RootSigs
-      tcsRootImpls: RootImpls
-      tcsCcuSig: ModuleOrNamespaceType
-      
-      /// The collected open declarations implied by '/checked' flag and processing F# interactive fragments that have an implied module.
-      tcsImplicitOpenDeclarations: OpenDeclaration list
+        tcsCcu: CcuThunk
+        tcsCcuType: ModuleOrNamespace
+        tcsNiceNameGen: NiceNameGenerator
+        tcsTcSigEnv: TcEnv
+        tcsTcImplEnv: TcEnv
+        tcsCreatesGeneratedProvidedTypes: bool
+        tcsRootSigs: RootSigs
+        tcsRootImpls: RootImpls
+        tcsCcuSig: ModuleOrNamespaceType
+
+        /// The collected open declarations implied by '/checked' flag and processing F# interactive fragments that have an implied module.
+        tcsImplicitOpenDeclarations: OpenDeclaration list
     }
 
     member x.NiceNameGenerator = x.tcsNiceNameGen
@@ -782,35 +1056,39 @@ type TcState =
     member x.CcuSig = x.tcsCcuSig
 
     member x.NextStateAfterIncrementalFragment tcEnvAtEndOfLastInput =
-        { x with tcsTcSigEnv = tcEnvAtEndOfLastInput
-                 tcsTcImplEnv = tcEnvAtEndOfLastInput }
-
+        { x with
+            tcsTcSigEnv = tcEnvAtEndOfLastInput
+            tcsTcImplEnv = tcEnvAtEndOfLastInput
+        }
 
 /// Create the initial type checking state for compiling an assembly
-let GetInitialTcState(m, ccuName, tcConfig: TcConfig, tcGlobals, tcImports: TcImports, niceNameGen, tcEnv0, openDecls0) =
+let GetInitialTcState (m, ccuName, tcConfig: TcConfig, tcGlobals, tcImports: TcImports, niceNameGen, tcEnv0, openDecls0) =
     ignore tcImports
 
     // Create a ccu to hold all the results of compilation
-    let ccuContents = Construct.NewCcuContents ILScopeRef.Local m ccuName (Construct.NewEmptyModuleOrNamespaceType Namespace)
+    let ccuContents =
+        Construct.NewCcuContents ILScopeRef.Local m ccuName (Construct.NewEmptyModuleOrNamespaceType Namespace)
 
     let ccuData: CcuData =
-        { IsFSharp=true
-          UsesFSharp20PlusQuotations=false
+        {
+            IsFSharp = true
+            UsesFSharp20PlusQuotations = false
 #if !NO_TYPEPROVIDERS
-          InvalidateEvent=(Event<_>()).Publish
-          IsProviderGenerated = false
-          ImportProvidedType = (fun ty -> Import.ImportProvidedType (tcImports.GetImportMap()) m ty)
+            InvalidateEvent = (Event<_>()).Publish
+            IsProviderGenerated = false
+            ImportProvidedType = (fun ty -> Import.ImportProvidedType (tcImports.GetImportMap()) m ty)
 #endif
-          TryGetILModuleDef = (fun () -> None)
-          FileName=None
-          Stamp = newStamp()
-          QualifiedName= None
-          SourceCodeDirectory = tcConfig.implicitIncludeDir
-          ILScopeRef=ILScopeRef.Local
-          Contents=ccuContents
-          MemberSignatureEquality= typeEquivAux EraseAll tcGlobals
-          TypeForwarders= CcuTypeForwarderTable.Empty
-          XmlDocumentationInfo = None }
+            TryGetILModuleDef = (fun () -> None)
+            FileName = None
+            Stamp = newStamp ()
+            QualifiedName = None
+            SourceCodeDirectory = tcConfig.implicitIncludeDir
+            ILScopeRef = ILScopeRef.Local
+            Contents = ccuContents
+            MemberSignatureEquality = typeEquivAux EraseAll tcGlobals
+            TypeForwarders = CcuTypeForwarderTable.Empty
+            XmlDocumentationInfo = None
+        }
 
     let ccu = CcuThunk.Create(ccuName, ccuData)
 
@@ -818,16 +1096,17 @@ let GetInitialTcState(m, ccuName, tcConfig: TcConfig, tcGlobals, tcImports: TcIm
     if tcConfig.compilingFSharpCore then
         tcGlobals.fslibCcu.Fixup ccu
 
-    { tcsCcu= ccu
-      tcsCcuType=ccuContents
-      tcsNiceNameGen=niceNameGen
-      tcsTcSigEnv=tcEnv0
-      tcsTcImplEnv=tcEnv0
-      tcsCreatesGeneratedProvidedTypes=false
-      tcsRootSigs = Zmap.empty qnameOrder
-      tcsRootImpls = Zset.empty qnameOrder
-      tcsCcuSig = Construct.NewEmptyModuleOrNamespaceType Namespace 
-      tcsImplicitOpenDeclarations = openDecls0
+    {
+        tcsCcu = ccu
+        tcsCcuType = ccuContents
+        tcsNiceNameGen = niceNameGen
+        tcsTcSigEnv = tcEnv0
+        tcsTcImplEnv = tcEnv0
+        tcsCreatesGeneratedProvidedTypes = false
+        tcsRootSigs = Zmap.empty qnameOrder
+        tcsRootImpls = Zset.empty qnameOrder
+        tcsCcuSig = Construct.NewEmptyModuleOrNamespaceType Namespace
+        tcsImplicitOpenDeclarations = openDecls0
     }
 
 /// Dummy typed impl file that contains no definitions and is not used for emitting any kind of assembly.
@@ -850,115 +1129,151 @@ let CheckOneInput
 
     cancellable {
         try
-          CheckSimulateException tcConfig
-
-          let m = inp.Range
-          let amap = tcImports.GetImportMap()
-          match inp with
-          | ParsedInput.SigFile (ParsedSigFileInput (qualifiedNameOfFile = qualNameOfFile) as file) ->
-
-              // Check if we've seen this top module signature before.
-              if Zmap.mem qualNameOfFile tcState.tcsRootSigs then
-                  errorR(Error(FSComp.SR.buildSignatureAlreadySpecified(qualNameOfFile.Text), m.StartRange))
-
-              // Check if the implementation came first in compilation order
-              if Zset.contains qualNameOfFile tcState.tcsRootImpls then
-                  errorR(Error(FSComp.SR.buildImplementationAlreadyGivenDetail(qualNameOfFile.Text), m))
-
-              let conditionalDefines =
-                  if tcConfig.noConditionalErasure then None else Some tcConfig.conditionalDefines
-
-              // Typecheck the signature file
-              let! tcEnv, sigFileType, createsGeneratedProvidedTypes =
-                  CheckOneSigFile (tcGlobals, tcState.tcsNiceNameGen, amap, tcState.tcsCcu, checkForErrors, conditionalDefines, tcSink, tcConfig.internalTestSpanStackReferring) tcState.tcsTcSigEnv file
-
-              let rootSigs = Zmap.add qualNameOfFile sigFileType tcState.tcsRootSigs
-
-              // Add the signature to the signature env (unless it had an explicit signature)
-              let ccuSigForFile = CombineCcuContentFragments m [sigFileType; tcState.tcsCcuSig]
-
-              // Open the prefixPath for fsi.exe
-              let tcEnv, _openDecls1 =
-                  match prefixPathOpt with
-                  | None -> tcEnv, []
-                  | Some prefixPath ->
-                      let m = qualNameOfFile.Range
-                      TcOpenModuleOrNamespaceDecl tcSink tcGlobals amap m tcEnv (prefixPath, m)
-
-              let tcState =
-                   { tcState with
-                        tcsTcSigEnv=tcEnv
-                        tcsTcImplEnv=tcState.tcsTcImplEnv
-                        tcsRootSigs=rootSigs
-                        tcsCreatesGeneratedProvidedTypes=tcState.tcsCreatesGeneratedProvidedTypes || createsGeneratedProvidedTypes}
-
-              return (tcEnv, EmptyTopAttrs, None, ccuSigForFile), tcState
-
-          | ParsedInput.ImplFile (ParsedImplFileInput (qualifiedNameOfFile = qualNameOfFile) as file) ->
-
-              // Check if we've got an interface for this fragment
-              let rootSigOpt = tcState.tcsRootSigs.TryFind qualNameOfFile
-
-              // Check if we've already seen an implementation for this fragment
-              if Zset.contains qualNameOfFile tcState.tcsRootImpls then
-                  errorR(Error(FSComp.SR.buildImplementationAlreadyGiven(qualNameOfFile.Text), m))
-
-              let tcImplEnv = tcState.tcsTcImplEnv
-
-              let conditionalDefines =
-                  if tcConfig.noConditionalErasure then None else Some tcConfig.conditionalDefines
-
-              let hadSig = rootSigOpt.IsSome
+            CheckSimulateException tcConfig
+
+            let m = inp.Range
+            let amap = tcImports.GetImportMap()
+
+            match inp with
+            | ParsedInput.SigFile (ParsedSigFileInput (qualifiedNameOfFile = qualNameOfFile) as file) ->
+
+                // Check if we've seen this top module signature before.
+                if Zmap.mem qualNameOfFile tcState.tcsRootSigs then
+                    errorR (Error(FSComp.SR.buildSignatureAlreadySpecified (qualNameOfFile.Text), m.StartRange))
+
+                // Check if the implementation came first in compilation order
+                if Zset.contains qualNameOfFile tcState.tcsRootImpls then
+                    errorR (Error(FSComp.SR.buildImplementationAlreadyGivenDetail (qualNameOfFile.Text), m))
+
+                let conditionalDefines =
+                    if tcConfig.noConditionalErasure then
+                        None
+                    else
+                        Some tcConfig.conditionalDefines
+
+                // Typecheck the signature file
+                let! tcEnv, sigFileType, createsGeneratedProvidedTypes =
+                    CheckOneSigFile
+                        (tcGlobals,
+                         tcState.tcsNiceNameGen,
+                         amap,
+                         tcState.tcsCcu,
+                         checkForErrors,
+                         conditionalDefines,
+                         tcSink,
+                         tcConfig.internalTestSpanStackReferring)
+                        tcState.tcsTcSigEnv
+                        file
+
+                let rootSigs = Zmap.add qualNameOfFile sigFileType tcState.tcsRootSigs
+
+                // Add the signature to the signature env (unless it had an explicit signature)
+                let ccuSigForFile = CombineCcuContentFragments m [ sigFileType; tcState.tcsCcuSig ]
+
+                // Open the prefixPath for fsi.exe
+                let tcEnv, _openDecls1 =
+                    match prefixPathOpt with
+                    | None -> tcEnv, []
+                    | Some prefixPath ->
+                        let m = qualNameOfFile.Range
+                        TcOpenModuleOrNamespaceDecl tcSink tcGlobals amap m tcEnv (prefixPath, m)
+
+                let tcState =
+                    { tcState with
+                        tcsTcSigEnv = tcEnv
+                        tcsTcImplEnv = tcState.tcsTcImplEnv
+                        tcsRootSigs = rootSigs
+                        tcsCreatesGeneratedProvidedTypes = tcState.tcsCreatesGeneratedProvidedTypes || createsGeneratedProvidedTypes
+                    }
 
-              // Typecheck the implementation file
-              let typeCheckOne =
-                  if skipImplIfSigExists && hadSig then
-                    (EmptyTopAttrs, CreateEmptyDummyImplFile qualNameOfFile rootSigOpt.Value, Unchecked.defaultof<_>, tcImplEnv, false)
-                    |> Cancellable.ret
-                  else
-                    CheckOneImplFile (tcGlobals, tcState.tcsNiceNameGen, amap, tcState.tcsCcu, tcState.tcsImplicitOpenDeclarations, checkForErrors, conditionalDefines, tcSink, tcConfig.internalTestSpanStackReferring, tcImplEnv, rootSigOpt, file)
+                return (tcEnv, EmptyTopAttrs, None, ccuSigForFile), tcState
+
+            | ParsedInput.ImplFile (ParsedImplFileInput (qualifiedNameOfFile = qualNameOfFile) as file) ->
+
+                // Check if we've got an interface for this fragment
+                let rootSigOpt = tcState.tcsRootSigs.TryFind qualNameOfFile
+
+                // Check if we've already seen an implementation for this fragment
+                if Zset.contains qualNameOfFile tcState.tcsRootImpls then
+                    errorR (Error(FSComp.SR.buildImplementationAlreadyGiven (qualNameOfFile.Text), m))
+
+                let tcImplEnv = tcState.tcsTcImplEnv
+
+                let conditionalDefines =
+                    if tcConfig.noConditionalErasure then
+                        None
+                    else
+                        Some tcConfig.conditionalDefines
+
+                let hadSig = rootSigOpt.IsSome
+
+                // Typecheck the implementation file
+                let typeCheckOne =
+                    if skipImplIfSigExists && hadSig then
+                        (EmptyTopAttrs, CreateEmptyDummyImplFile qualNameOfFile rootSigOpt.Value, Unchecked.defaultof<_>, tcImplEnv, false)
+                        |> Cancellable.ret
+                    else
+                        CheckOneImplFile(
+                            tcGlobals,
+                            tcState.tcsNiceNameGen,
+                            amap,
+                            tcState.tcsCcu,
+                            tcState.tcsImplicitOpenDeclarations,
+                            checkForErrors,
+                            conditionalDefines,
+                            tcSink,
+                            tcConfig.internalTestSpanStackReferring,
+                            tcImplEnv,
+                            rootSigOpt,
+                            file
+                        )
 
-              let! topAttrs, implFile, _implFileHiddenType, tcEnvAtEnd, createsGeneratedProvidedTypes = typeCheckOne
+                let! topAttrs, implFile, _implFileHiddenType, tcEnvAtEnd, createsGeneratedProvidedTypes = typeCheckOne
 
-              let implFileSigType = implFile.Signature
+                let implFileSigType = implFile.Signature
 
-              let rootImpls = Zset.add qualNameOfFile tcState.tcsRootImpls
+                let rootImpls = Zset.add qualNameOfFile tcState.tcsRootImpls
 
-              // Only add it to the environment if it didn't have a signature
-              let m = qualNameOfFile.Range
+                // Only add it to the environment if it didn't have a signature
+                let m = qualNameOfFile.Range
 
-              // Add the implementation as to the implementation env
-              let tcImplEnv = AddLocalRootModuleOrNamespace TcResultsSink.NoSink tcGlobals amap m tcImplEnv implFileSigType
+                // Add the implementation as to the implementation env
+                let tcImplEnv =
+                    AddLocalRootModuleOrNamespace TcResultsSink.NoSink tcGlobals amap m tcImplEnv implFileSigType
 
-              // Add the implementation as to the signature env (unless it had an explicit signature)
-              let tcSigEnv =
-                  if hadSig then tcState.tcsTcSigEnv
-                  else AddLocalRootModuleOrNamespace TcResultsSink.NoSink tcGlobals amap m tcState.tcsTcSigEnv implFileSigType
+                // Add the implementation as to the signature env (unless it had an explicit signature)
+                let tcSigEnv =
+                    if hadSig then
+                        tcState.tcsTcSigEnv
+                    else
+                        AddLocalRootModuleOrNamespace TcResultsSink.NoSink tcGlobals amap m tcState.tcsTcSigEnv implFileSigType
 
-              // Open the prefixPath for fsi.exe (tcImplEnv)
-              let tcImplEnv, openDecls =
-                  match prefixPathOpt with
-                  | Some prefixPath -> TcOpenModuleOrNamespaceDecl tcSink tcGlobals amap m tcImplEnv (prefixPath, m)
-                  | _ -> tcImplEnv, []
+                // Open the prefixPath for fsi.exe (tcImplEnv)
+                let tcImplEnv, openDecls =
+                    match prefixPathOpt with
+                    | Some prefixPath -> TcOpenModuleOrNamespaceDecl tcSink tcGlobals amap m tcImplEnv (prefixPath, m)
+                    | _ -> tcImplEnv, []
 
-              // Open the prefixPath for fsi.exe (tcSigEnv)
-              let tcSigEnv, _ =
-                  match prefixPathOpt with
-                  | Some prefixPath when not hadSig -> TcOpenModuleOrNamespaceDecl tcSink tcGlobals amap m tcSigEnv (prefixPath, m)
-                  | _ -> tcSigEnv, []
+                // Open the prefixPath for fsi.exe (tcSigEnv)
+                let tcSigEnv, _ =
+                    match prefixPathOpt with
+                    | Some prefixPath when not hadSig -> TcOpenModuleOrNamespaceDecl tcSink tcGlobals amap m tcSigEnv (prefixPath, m)
+                    | _ -> tcSigEnv, []
 
-              let ccuSigForFile = CombineCcuContentFragments m [implFileSigType; tcState.tcsCcuSig]
+                let ccuSigForFile =
+                    CombineCcuContentFragments m [ implFileSigType; tcState.tcsCcuSig ]
 
-              let tcState =
-                   { tcState with
-                        tcsTcSigEnv=tcSigEnv
-                        tcsTcImplEnv=tcImplEnv
-                        tcsRootImpls=rootImpls
-                        tcsCcuSig=ccuSigForFile
-                        tcsCreatesGeneratedProvidedTypes=tcState.tcsCreatesGeneratedProvidedTypes || createsGeneratedProvidedTypes
+                let tcState =
+                    { tcState with
+                        tcsTcSigEnv = tcSigEnv
+                        tcsTcImplEnv = tcImplEnv
+                        tcsRootImpls = rootImpls
+                        tcsCcuSig = ccuSigForFile
+                        tcsCreatesGeneratedProvidedTypes = tcState.tcsCreatesGeneratedProvidedTypes || createsGeneratedProvidedTypes
                         tcsImplicitOpenDeclarations = tcState.tcsImplicitOpenDeclarations @ openDecls
                     }
-              return (tcEnvAtEnd, topAttrs, Some implFile, ccuSigForFile), tcState
+
+                return (tcEnvAtEnd, topAttrs, Some implFile, ccuSigForFile), tcState
 
         with e ->
             errorRecovery e range0
@@ -966,48 +1281,65 @@ let CheckOneInput
     }
 
 /// Typecheck a single file (or interactive entry into F# Interactive)
-let TypeCheckOneInputEntry (ctok, checkForErrors, tcConfig:TcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp =
+let TypeCheckOneInputEntry (ctok, checkForErrors, tcConfig: TcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp =
     // 'use' ensures that the warning handler is restored at the end
-    use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind(fun oldLogger -> GetDiagnosticsLoggerFilteringByScopedPragmas(false, GetScopedPragmasForInput inp, tcConfig.diagnosticsOptions, oldLogger) )
+    use unwindEL =
+        PushDiagnosticsLoggerPhaseUntilUnwind(fun oldLogger ->
+            GetDiagnosticsLoggerFilteringByScopedPragmas(false, GetScopedPragmasForInput inp, tcConfig.diagnosticsOptions, oldLogger))
+
     use unwindBP = PushThreadBuildPhaseUntilUnwind BuildPhase.TypeCheck
 
     RequireCompilationThread ctok
-    CheckOneInput (checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, TcResultsSink.NoSink, tcState, inp, false)
-        |> Cancellable.runWithoutCancellation
+
+    CheckOneInput(checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, TcResultsSink.NoSink, tcState, inp, false)
+    |> Cancellable.runWithoutCancellation
 
 /// Finish checking multiple files (or one interactive entry into F# Interactive)
-let CheckMultipleInputsFinish(results, tcState: TcState) =
+let CheckMultipleInputsFinish (results, tcState: TcState) =
     let tcEnvsAtEndFile, topAttrs, implFiles, ccuSigsForFiles = List.unzip4 results
     let topAttrs = List.foldBack CombineTopAttrs topAttrs EmptyTopAttrs
     let implFiles = List.choose id implFiles
     // This is the environment required by fsi.exe when incrementally adding definitions
-    let tcEnvAtEndOfLastFile = (match tcEnvsAtEndFile with h :: _ -> h | _ -> tcState.TcEnvFromSignatures)
+    let tcEnvAtEndOfLastFile =
+        (match tcEnvsAtEndFile with
+         | h :: _ -> h
+         | _ -> tcState.TcEnvFromSignatures)
+
     (tcEnvAtEndOfLastFile, topAttrs, implFiles, ccuSigsForFiles), tcState
 
-let CheckOneInputAndFinish(checkForErrors, tcConfig: TcConfig, tcImports, tcGlobals, prefixPathOpt, tcSink, tcState, input) =
+let CheckOneInputAndFinish (checkForErrors, tcConfig: TcConfig, tcImports, tcGlobals, prefixPathOpt, tcSink, tcState, input) =
     cancellable {
         Logger.LogBlockStart LogCompilerFunctionId.CompileOps_TypeCheckOneInputAndFinishEventually
         let! results, tcState = CheckOneInput(checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, tcSink, tcState, input, false)
-        let result = CheckMultipleInputsFinish([results], tcState)
+        let result = CheckMultipleInputsFinish([ results ], tcState)
         Logger.LogBlockStop LogCompilerFunctionId.CompileOps_TypeCheckOneInputAndFinishEventually
         return result
     }
 
 let CheckClosedInputSetFinish (declaredImpls: CheckedImplFile list, tcState) =
     // Latest contents to the CCU
-    let ccuContents = Construct.NewCcuContents ILScopeRef.Local range0 tcState.tcsCcu.AssemblyName tcState.tcsCcuSig
+    let ccuContents =
+        Construct.NewCcuContents ILScopeRef.Local range0 tcState.tcsCcu.AssemblyName tcState.tcsCcuSig
 
     // Check all interfaces have implementations
-    tcState.tcsRootSigs |> Zmap.iter (fun qualNameOfFile _ ->
-      if not (Zset.contains qualNameOfFile tcState.tcsRootImpls) then
-        errorR(Error(FSComp.SR.buildSignatureWithoutImplementation(qualNameOfFile.Text), qualNameOfFile.Range)))
+    tcState.tcsRootSigs
+    |> Zmap.iter (fun qualNameOfFile _ ->
+        if not (Zset.contains qualNameOfFile tcState.tcsRootImpls) then
+            errorR (Error(FSComp.SR.buildSignatureWithoutImplementation (qualNameOfFile.Text), qualNameOfFile.Range)))
 
     tcState, declaredImpls, ccuContents
 
 let CheckClosedInputSet (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, tcState, inputs) =
     // tcEnvAtEndOfLastFile is the environment required by fsi.exe when incrementally adding definitions
-    let results, tcState = (tcState, inputs) ||> List.mapFold (TypeCheckOneInputEntry (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt))
-    let (tcEnvAtEndOfLastFile, topAttrs, implFiles, _), tcState = CheckMultipleInputsFinish(results, tcState)
-    let tcState, declaredImpls, ccuContents = CheckClosedInputSetFinish (implFiles, tcState)
+    let results, tcState =
+        (tcState, inputs)
+        ||> List.mapFold (TypeCheckOneInputEntry(ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt))
+
+    let (tcEnvAtEndOfLastFile, topAttrs, implFiles, _), tcState =
+        CheckMultipleInputsFinish(results, tcState)
+
+    let tcState, declaredImpls, ccuContents =
+        CheckClosedInputSetFinish(implFiles, tcState)
+
     tcState.Ccu.Deref.Contents <- ccuContents
     tcState, topAttrs, declaredImpls, tcEnvAtEndOfLastFile
diff --git a/src/Compiler/Driver/ScriptClosure.fs b/src/Compiler/Driver/ScriptClosure.fs
index 7351edde58f..67adfb1e8de 100644
--- a/src/Compiler/Driver/ScriptClosure.fs
+++ b/src/Compiler/Driver/ScriptClosure.fs
@@ -73,26 +73,20 @@ type LoadClosure =
         LoadClosureRootFileDiagnostics: (PhasedDiagnostic * FSharpDiagnosticSeverity) list
     }
 
-
 []
 type CodeContext =
     | CompilationAndEvaluation // in fsi.exe
-    | Compilation  // in fsc.exe
+    | Compilation // in fsc.exe
     | Editing // in VS
 
 module ScriptPreprocessClosure =
 
     /// Represents an input to the closure finding process
-    type ClosureSource =
-        ClosureSource of
-            fileName: string *
-            referenceRange: range *
-            sourceText: ISourceText *
-            parseRequired: bool
+    type ClosureSource = ClosureSource of fileName: string * referenceRange: range * sourceText: ISourceText * parseRequired: bool
 
     /// Represents an output of the closure finding process
     type ClosureFile =
-        ClosureFile of
+        | ClosureFile of
             fileName: string *
             range: range *
             parsedInput: ParsedInput option *
@@ -102,12 +96,11 @@ module ScriptPreprocessClosure =
 
     type Observed() =
         let seen = Dictionary<_, bool>()
+
         member _.SetSeen check =
-            if not(seen.ContainsKey check) then
-                seen.Add(check, true)
+            if not (seen.ContainsKey check) then seen.Add(check, true)
 
-        member _.HaveSeen check =
-            seen.ContainsKey check
+        member _.HaveSeen check = seen.ContainsKey check
 
     /// Parse a script file (or any input file referenced by '#load')
     let ParseScriptClosureInput
@@ -127,19 +120,25 @@ module ScriptPreprocessClosure =
         //     .fsx -- EDITING + !COMPILED\INTERACTIVE
         let defines =
             match codeContext with
-            | CodeContext.CompilationAndEvaluation -> ["INTERACTIVE"]
-            | CodeContext.Compilation -> ["COMPILED"]
-            | CodeContext.Editing -> "EDITING" :: (if IsScript fileName then ["INTERACTIVE"] else ["COMPILED"])
+            | CodeContext.CompilationAndEvaluation -> [ "INTERACTIVE" ]
+            | CodeContext.Compilation -> [ "COMPILED" ]
+            | CodeContext.Editing ->
+                "EDITING"
+                :: (if IsScript fileName then
+                        [ "INTERACTIVE" ]
+                    else
+                        [ "COMPILED" ])
 
-        let tcConfigB = tcConfig.CloneToBuilder() 
+        let tcConfigB = tcConfig.CloneToBuilder()
         tcConfigB.conditionalDefines <- defines @ tcConfig.conditionalDefines
         let tcConfig = TcConfig.Create(tcConfigB, false)
-        
-        let lexbuf = UnicodeLexing.SourceTextAsLexbuf(true, tcConfig.langVersion, sourceText)
+
+        let lexbuf =
+            UnicodeLexing.SourceTextAsLexbuf(true, tcConfig.langVersion, sourceText)
 
         // The root compiland is last in the list of compilands.
         let isLastCompiland = (IsScript fileName, tcConfig.target.IsExe)
-        ParseOneInputLexbuf (tcConfig, lexResourceManager, lexbuf, fileName, isLastCompiland, diagnosticsLogger)
+        ParseOneInputLexbuf(tcConfig, lexResourceManager, lexbuf, fileName, isLastCompiland, diagnosticsLogger)
 
     /// Create a TcConfig for load closure starting from a single .fsx file
     let CreateScriptTextTcConfig
@@ -164,8 +163,10 @@ module ScriptPreprocessClosure =
         let isInvalidationSupported = (codeContext = CodeContext.Editing)
 
         let rangeForErrors = mkFirstLineOfFile fileName
+
         let tcConfigB =
-            TcConfigBuilder.CreateNew(legacyReferenceResolver,
+            TcConfigBuilder.CreateNew(
+                legacyReferenceResolver,
                 defaultFSharpBinariesDir,
                 reduceMemoryUsage,
                 projectDir,
@@ -174,8 +175,16 @@ module ScriptPreprocessClosure =
                 CopyFSharpCoreFlag.No,
                 tryGetMetadataSnapshot,
                 sdkDirOverride,
-                rangeForErrors)
-        tcConfigB.SetPrimaryAssembly (if assumeDotNetFramework then PrimaryAssembly.Mscorlib else PrimaryAssembly.System_Runtime)
+                rangeForErrors
+            )
+
+        let primaryAssembly =
+            if assumeDotNetFramework then
+                PrimaryAssembly.Mscorlib
+            else
+                PrimaryAssembly.System_Runtime
+
+        tcConfigB.SetPrimaryAssembly primaryAssembly
         tcConfigB.SetUseSdkRefs useSdkRefs
 
         applyCommandLineArgs tcConfigB
@@ -186,13 +195,21 @@ module ScriptPreprocessClosure =
             match basicReferences with
             | None ->
                 let diagnosticsLogger = CapturingDiagnosticsLogger("ScriptDefaultReferences")
-                use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> diagnosticsLogger)
-                let references, useDotNetFramework = tcConfigB.FxResolver.GetDefaultReferences useFsiAuxLib
+                use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
+
+                let references, useDotNetFramework =
+                    tcConfigB.FxResolver.GetDefaultReferences useFsiAuxLib
 
                 // If the user requested .NET Core scripting but something went wrong and we reverted to
                 // .NET Framework scripting then we must adjust both the primaryAssembly and fxResolver
                 if useDotNetFramework <> assumeDotNetFramework then
-                    tcConfigB.SetPrimaryAssembly (if useDotNetFramework then PrimaryAssembly.Mscorlib else PrimaryAssembly.System_Runtime)
+                    let primaryAssembly =
+                        if useDotNetFramework then
+                            PrimaryAssembly.Mscorlib
+                        else
+                            PrimaryAssembly.System_Runtime
+
+                    tcConfigB.SetPrimaryAssembly primaryAssembly
 
                 // Add script references
                 for reference in references do
@@ -203,6 +220,7 @@ module ScriptPreprocessClosure =
             | Some (rs, diagnostics) ->
                 for m, reference in rs do
                     tcConfigB.AddReferencedAssemblyByPath(m, reference)
+
                 diagnostics
 
         tcConfigB.resolutionEnvironment <-
@@ -221,18 +239,20 @@ module ScriptPreprocessClosure =
 
         tcConfigB.SetUseSdkRefs useSdkRefs
 
-        TcConfig.Create(tcConfigB, validate=true), scriptDefaultReferencesDiagnostics
+        TcConfig.Create(tcConfigB, validate = true), scriptDefaultReferencesDiagnostics
 
-    let ClosureSourceOfFilename(fileName, m, inputCodePage, parseRequired) =
+    let ClosureSourceOfFilename (fileName, m, inputCodePage, parseRequired) =
         try
             let fileName = FileSystem.GetFullPathShim fileName
             use stream = FileSystem.OpenFileForReadShim(fileName)
+
             use reader =
                 match inputCodePage with
                 | None -> new StreamReader(stream, true)
                 | Some (n: int) -> new StreamReader(stream, Encoding.GetEncoding n)
+
             let source = reader.ReadToEnd()
-            [ClosureSource(fileName, m, SourceText.ofString source, parseRequired)]
+            [ ClosureSource(fileName, m, SourceText.ofString source, parseRequired) ]
         with exn ->
             errorRecovery exn m
             []
@@ -247,27 +267,39 @@ module ScriptPreprocessClosure =
 
         let tcConfigB = tcConfig.CloneToBuilder()
         let mutable nowarns = []
-        let getWarningNumber = fun () (m, s) -> nowarns <- (s, m) :: nowarns
-        let addReferenceDirective = fun () (m, s, directive) -> tcConfigB.AddReferenceDirective(dependencyProvider, m, s, directive)
-        let addLoadedSource = fun () (m, s) -> tcConfigB.AddLoadedSource(m, s, pathOfMetaCommandSource)
+        let getWarningNumber () (m, s) = nowarns <- (s, m) :: nowarns
+
+        let addReferenceDirective () (m, s, directive) =
+            tcConfigB.AddReferenceDirective(dependencyProvider, m, s, directive)
+
+        let addLoadedSource () (m, s) =
+            tcConfigB.AddLoadedSource(m, s, pathOfMetaCommandSource)
+
         try
-            ProcessMetaCommandsFromInput (getWarningNumber, addReferenceDirective, addLoadedSource) (tcConfigB, inp, pathOfMetaCommandSource, ())
+            ProcessMetaCommandsFromInput
+                (getWarningNumber, addReferenceDirective, addLoadedSource)
+                (tcConfigB, inp, pathOfMetaCommandSource, ())
         with ReportedError _ ->
             // Recover by using whatever did end up in the tcConfig
             ()
 
         try
-            TcConfig.Create(tcConfigB, validate=false), nowarns
+            TcConfig.Create(tcConfigB, validate = false), nowarns
         with ReportedError _ ->
             // Recover by using a default TcConfig.
             let tcConfigB = tcConfig.CloneToBuilder()
-            TcConfig.Create(tcConfigB, validate=false), nowarns
+            TcConfig.Create(tcConfigB, validate = false), nowarns
+
+    let getDirective d =
+        match d with
+        | Directive.Resolution -> "r"
+        | Directive.Include -> "i"
 
     let FindClosureFiles
         (
             mainFile,
             closureSources,
-            origTcConfig:TcConfig,
+            origTcConfig: TcConfig,
             codeContext,
             lexResourceManager: Lexhelp.LexResourceManager,
             dependencyProvider: DependencyProvider
@@ -281,165 +313,273 @@ module ScriptPreprocessClosure =
 
         // Resolve the packages
         let rec resolveDependencyManagerSources scriptName =
-            if not (loadScripts.Contains scriptName) then
-                [ for kv in tcConfig.packageManagerLines do
-                    let packageManagerKey, packageManagerLines = kv.Key, kv.Value
-                    match packageManagerLines with
-                    | [] -> ()
-                    | { Directive=_; LineStatus=_; Line=_; Range=m } :: _ ->
-                        let reportError =
-                            ResolvingErrorReport (fun errorType err msg ->
-                                let error = err, msg
-                                match errorType with
-                                | ErrorReportType.Warning -> warning(Error(error, m))
-                                | ErrorReportType.Error -> errorR(Error(error, m)))
-
-                        match origTcConfig.packageManagerLines |> Map.tryFind packageManagerKey with
-                        | Some oldDependencyManagerLines when oldDependencyManagerLines = packageManagerLines -> ()
-                        | _ ->
-                            let outputDir =  tcConfig.outputDir |> Option.defaultValue ""
-                            match dependencyProvider.TryFindDependencyManagerByKey(tcConfig.compilerToolPaths, outputDir, reportError, packageManagerKey) with
-                            | Null ->
-                                errorR(Error(dependencyProvider.CreatePackageManagerUnknownError(tcConfig.compilerToolPaths, outputDir, packageManagerKey, reportError), m))
-
-                            | NonNull dependencyManager ->
-                                let directive d =
-                                    match d with
-                                    | Directive.Resolution -> "r"
-                                    | Directive.Include -> "i"
-
-                                let packageManagerTextLines = packageManagerLines |> List.map(fun l -> directive l.Directive, l.Line)
-                                let tfm, rid = tcConfig.FxResolver.GetTfmAndRid()
-                                let result = dependencyProvider.Resolve(dependencyManager, ".fsx", packageManagerTextLines, reportError, tfm, rid, tcConfig.implicitIncludeDir, mainFile, scriptName)
-                                if result.Success then
-                                    // Resolution produced no errors
-                                    //Write outputs in F# Interactive and compiler
-                                    if codeContext <> CodeContext.Editing then
-                                        for line in result.StdOut do Console.Out.WriteLine(line)
-                                        for line in result.StdError do Console.Error.WriteLine(line)
-
-                                    packageReferences[m] <- [ for script in result.SourceFiles do yield! FileSystem.OpenFileForReadShim(script).ReadLines() ]
-                                    if not (Seq.isEmpty result.Roots) then
-                                        let tcConfigB = tcConfig.CloneToBuilder()
-                                        for folder in result.Roots do
-                                            tcConfigB.AddIncludePath(m, folder, "")
-                                        tcConfigB.packageManagerLines <- PackageManagerLine.SetLinesAsProcessed packageManagerKey tcConfigB.packageManagerLines
-                                        tcConfig <- TcConfig.Create(tcConfigB, validate=false)
-
-                                    if not (Seq.isEmpty result.Resolutions) then
-                                        let tcConfigB = tcConfig.CloneToBuilder()
-                                        for resolution in result.Resolutions do
-                                            tcConfigB.AddReferencedAssemblyByPath(m, resolution)
-                                        tcConfig <- TcConfig.Create(tcConfigB, validate = false)
-
-                                    for script in result.SourceFiles do
-                                        use stream = FileSystem.OpenFileForReadShim(script)
-                                        let scriptText = stream.ReadAllText()
-                                        loadScripts.Add script |> ignore
-                                        let iSourceText = SourceText.ofString scriptText
-                                        yield! loop (ClosureSource(script, m, iSourceText, true))
-
-                                else
-                                    // Send outputs via diagnostics
-                                    if (result.StdOut.Length > 0 || result.StdError.Length > 0) then
-                                        for line in Array.append result.StdOut result.StdError do
-                                            errorR(Error(FSComp.SR.packageManagerError(line), m))
-                                    // Resolution produced errors update packagerManagerLines entries to note these failure
-                                    // failed resolutions will no longer be considered
-                                    let tcConfigB = tcConfig.CloneToBuilder()
-                                    tcConfigB.packageManagerLines <- PackageManagerLine.RemoveUnprocessedLines packageManagerKey tcConfigB.packageManagerLines
-                                    tcConfig <- TcConfig.Create(tcConfigB, validate=false)]
-            else []
-
-        and loop (ClosureSource(fileName, m, sourceText, parseRequired)) =
-            [   if not (observedSources.HaveSeen(fileName)) then
+            [
+                if not (loadScripts.Contains scriptName) then
+                    for kv in tcConfig.packageManagerLines do
+                        let packageManagerKey, packageManagerLines = kv.Key, kv.Value
+
+                        match packageManagerLines with
+                        | [] -> ()
+                        | packageManagerLine :: _ ->
+                            let m = packageManagerLine.Range
+                            yield! processPackageManagerLines m packageManagerLines scriptName packageManagerKey
+            ]
+
+        and reportError m =
+            ResolvingErrorReport(fun errorType err msg ->
+                let error = err, msg
+
+                match errorType with
+                | ErrorReportType.Warning -> warning (Error(error, m))
+                | ErrorReportType.Error -> errorR (Error(error, m)))
+
+        and processPackageManagerLines m packageManagerLines scriptName packageManagerKey =
+            [
+
+                match origTcConfig.packageManagerLines |> Map.tryFind packageManagerKey with
+                | Some oldDependencyManagerLines when oldDependencyManagerLines = packageManagerLines -> ()
+                | _ ->
+                    let outputDir = tcConfig.outputDir |> Option.defaultValue ""
+
+                    let managerOpt =
+                        dependencyProvider.TryFindDependencyManagerByKey(
+                            tcConfig.compilerToolPaths,
+                            outputDir,
+                            reportError m,
+                            packageManagerKey
+                        )
+
+                    match managerOpt with
+                    | Null ->
+                        let err =
+                            dependencyProvider.CreatePackageManagerUnknownError(
+                                tcConfig.compilerToolPaths,
+                                outputDir,
+                                packageManagerKey,
+                                reportError m
+                            )
+
+                        errorR (Error(err, m))
+
+                    | NonNull dependencyManager ->
+                        yield! resolvePackageManagerLines m packageManagerLines scriptName packageManagerKey dependencyManager
+            ]
+
+        and resolvePackageManagerLines m packageManagerLines scriptName packageManagerKey dependencyManager =
+            [
+                let packageManagerTextLines =
+                    packageManagerLines |> List.map (fun l -> getDirective l.Directive, l.Line)
+
+                let tfm, rid = tcConfig.FxResolver.GetTfmAndRid()
+
+                let result =
+                    dependencyProvider.Resolve(
+                        dependencyManager,
+                        ".fsx",
+                        packageManagerTextLines,
+                        reportError m,
+                        tfm,
+                        rid,
+                        tcConfig.implicitIncludeDir,
+                        mainFile,
+                        scriptName
+                    )
+
+                if result.Success then
+                    // Resolution produced no errors
+                    //Write outputs in F# Interactive and compiler
+                    if codeContext <> CodeContext.Editing then
+                        for line in result.StdOut do
+                            Console.Out.WriteLine(line)
+
+                        for line in result.StdError do
+                            Console.Error.WriteLine(line)
+
+                    packageReferences[m] <-
+                        [
+                            for script in result.SourceFiles do
+                                yield! FileSystem.OpenFileForReadShim(script).ReadLines()
+                        ]
+
+                    if not (Seq.isEmpty result.Roots) then
+                        let tcConfigB = tcConfig.CloneToBuilder()
+
+                        for folder in result.Roots do
+                            tcConfigB.AddIncludePath(m, folder, "")
+
+                        tcConfigB.packageManagerLines <-
+                            PackageManagerLine.SetLinesAsProcessed packageManagerKey tcConfigB.packageManagerLines
+
+                        tcConfig <- TcConfig.Create(tcConfigB, validate = false)
+
+                    if not (Seq.isEmpty result.Resolutions) then
+                        let tcConfigB = tcConfig.CloneToBuilder()
+
+                        for resolution in result.Resolutions do
+                            tcConfigB.AddReferencedAssemblyByPath(m, resolution)
+
+                        tcConfig <- TcConfig.Create(tcConfigB, validate = false)
+
+                    for script in result.SourceFiles do
+                        use stream = FileSystem.OpenFileForReadShim(script)
+                        let scriptText = stream.ReadAllText()
+                        loadScripts.Add script |> ignore
+                        let iSourceText = SourceText.ofString scriptText
+                        yield! processClosureSource (ClosureSource(script, m, iSourceText, true))
+
+                else
+                    // Send outputs via diagnostics
+                    if (result.StdOut.Length > 0 || result.StdError.Length > 0) then
+                        for line in Array.append result.StdOut result.StdError do
+                            errorR (Error(FSComp.SR.packageManagerError (line), m))
+                    // Resolution produced errors update packagerManagerLines entries to note these failure
+                    // failed resolutions will no longer be considered
+                    let tcConfigB = tcConfig.CloneToBuilder()
+
+                    tcConfigB.packageManagerLines <-
+                        PackageManagerLine.RemoveUnprocessedLines packageManagerKey tcConfigB.packageManagerLines
+
+                    tcConfig <- TcConfig.Create(tcConfigB, validate = false)
+            ]
+
+        and processClosureSource (ClosureSource (fileName, m, sourceText, parseRequired)) =
+            [
+                if not (observedSources.HaveSeen(fileName)) then
                     observedSources.SetSeen(fileName)
                     //printfn "visiting %s" fileName
                     if IsScript fileName || parseRequired then
                         let parseResult, parseDiagnostics =
                             let diagnosticsLogger = CapturingDiagnosticsLogger("FindClosureParse")
-                            use _unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> diagnosticsLogger)
-                            let result = ParseScriptClosureInput (fileName, sourceText, tcConfig, codeContext, lexResourceManager, diagnosticsLogger)
+                            use _unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
+
+                            let result =
+                                ParseScriptClosureInput(fileName, sourceText, tcConfig, codeContext, lexResourceManager, diagnosticsLogger)
+
                             result, diagnosticsLogger.Diagnostics
 
                         let diagnosticsLogger = CapturingDiagnosticsLogger("FindClosureMetaCommands")
-                        use _unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> diagnosticsLogger)
+                        use _unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
                         let pathOfMetaCommandSource = Path.GetDirectoryName fileName
                         let preSources = tcConfig.GetAvailableLoadedSources()
 
-                        let tcConfigResult, noWarns = ApplyMetaCommandsFromInputToTcConfigAndGatherNoWarn (tcConfig, parseResult, pathOfMetaCommandSource, dependencyProvider)
+                        let tcConfigResult, noWarns =
+                            ApplyMetaCommandsFromInputToTcConfigAndGatherNoWarn(
+                                tcConfig,
+                                parseResult,
+                                pathOfMetaCommandSource,
+                                dependencyProvider
+                            )
+
                         tcConfig <- tcConfigResult // We accumulate the tcConfig in order to collect assembly references
 
                         yield! resolveDependencyManagerSources fileName
 
                         let postSources = tcConfig.GetAvailableLoadedSources()
-                        let sources = if preSources.Length < postSources.Length then postSources[preSources.Length..] else []
+
+                        let sources =
+                            if preSources.Length < postSources.Length then
+                                postSources[preSources.Length ..]
+                            else
+                                []
 
                         yield! resolveDependencyManagerSources fileName
+
                         for m, subFile in sources do
                             if IsScript subFile then
                                 for subSource in ClosureSourceOfFilename(subFile, m, tcConfigResult.inputCodePage, false) do
-                                    yield! loop subSource
+                                    yield! processClosureSource subSource
                             else
-                                yield ClosureFile(subFile, m, None, [], [], [])
-                        yield ClosureFile(fileName, m, Some parseResult, parseDiagnostics, diagnosticsLogger.Diagnostics, noWarns)
+                                ClosureFile(subFile, m, None, [], [], [])
+
+                        ClosureFile(fileName, m, Some parseResult, parseDiagnostics, diagnosticsLogger.Diagnostics, noWarns)
 
                     else
                         // Don't traverse into .fs leafs.
                         printfn "yielding non-script source %s" fileName
-                        yield ClosureFile(fileName, m, None, [], [], []) ]
+                        ClosureFile(fileName, m, None, [], [], [])
+            ]
+
+        let sources = closureSources |> List.collect processClosureSource
+
+        let packageReferences =
+            packageReferences |> Seq.map (fun kvp -> kvp.Key, kvp.Value) |> Seq.toArray
 
-        let sources = closureSources |> List.collect loop
-        let packageReferences = packageReferences |> Seq.map (fun kvp -> kvp.Key, kvp.Value) |> Seq.toArray
         sources, tcConfig, packageReferences
 
+    /// Mark the last file as isLastCompiland.
+    let MarkLastCompiland (tcConfig: TcConfig, lastClosureFile) =
+        let (ClosureFile (fileName, m, lastParsedInput, parseDiagnostics, metaDiagnostics, nowarns)) =
+            lastClosureFile
+
+        match lastParsedInput with
+        | Some (ParsedInput.ImplFile lastParsedImplFile) ->
+
+            let (ParsedImplFileInput (name, isScript, qualNameOfFile, scopedPragmas, hashDirectives, implFileFlags, _, trivia)) =
+                lastParsedImplFile
+
+            let isLastCompiland = (true, tcConfig.target.IsExe)
+
+            let lastParsedImplFileR =
+                ParsedImplFileInput(name, isScript, qualNameOfFile, scopedPragmas, hashDirectives, implFileFlags, isLastCompiland, trivia)
+
+            let lastClosureFileR =
+                ClosureFile(fileName, m, Some(ParsedInput.ImplFile lastParsedImplFileR), parseDiagnostics, metaDiagnostics, nowarns)
+
+            lastClosureFileR
+        | _ -> lastClosureFile
+
     /// Reduce the full directive closure into LoadClosure
-    let GetLoadClosure(rootFilename, closureFiles, tcConfig: TcConfig, codeContext, packageReferences, earlierDiagnostics) =
+    let GetLoadClosure (rootFilename, closureFiles, tcConfig: TcConfig, codeContext, packageReferences, earlierDiagnostics) =
 
         // Mark the last file as isLastCompiland.
         let closureFiles =
-            if isNil closureFiles then
-                closureFiles
-            else
-                match List.frontAndBack closureFiles with
-                | rest, ClosureFile
-                           (fileName, m,
-                            Some(ParsedInput.ImplFile (ParsedImplFileInput (name, isScript, qualNameOfFile, scopedPragmas, hashDirectives, implFileFlags, _, trivia))),
-                            parseDiagnostics, metaDiagnostics, nowarns) ->
-
-                    let isLastCompiland = (true, tcConfig.target.IsExe)
-                    rest @ [ClosureFile
-                                (fileName, m,
-                                 Some(ParsedInput.ImplFile (ParsedImplFileInput (name, isScript, qualNameOfFile, scopedPragmas, hashDirectives, implFileFlags, isLastCompiland, trivia))),
-                                 parseDiagnostics, metaDiagnostics, nowarns)]
-
-                | _ -> closureFiles
+            match List.tryFrontAndBack closureFiles with
+            | None -> closureFiles
+            | Some (rest, lastClosureFile) ->
+                let lastClosureFileR = MarkLastCompiland(tcConfig, lastClosureFile)
+                rest @ [ lastClosureFileR ]
 
         // Get all source files.
-        let sourceFiles = [ for ClosureFile(fileName, m, _, _, _, _) in closureFiles -> (fileName, m) ]
+        let sourceFiles =
+            [ for ClosureFile (fileName, m, _, _, _, _) in closureFiles -> (fileName, m) ]
 
         let sourceInputs =
-            [  for ClosureFile(fileName, _, input, parseDiagnostics, metaDiagnostics, _nowarns) in closureFiles ->
-                   ({ FileName=fileName
-                      SyntaxTree=input
-                      ParseDiagnostics=parseDiagnostics
-                      MetaCommandDiagnostics=metaDiagnostics } : LoadClosureInput) ]
-
-        let globalNoWarns = closureFiles |> List.collect (fun (ClosureFile(_, _, _, _, _, noWarns)) -> noWarns)
+            [
+                for closureFile in closureFiles ->
+                    let (ClosureFile (fileName, _, input, parseDiagnostics, metaDiagnostics, _nowarns)) =
+                        closureFile
+
+                    let closureInput: LoadClosureInput =
+                        {
+                            FileName = fileName
+                            SyntaxTree = input
+                            ParseDiagnostics = parseDiagnostics
+                            MetaCommandDiagnostics = metaDiagnostics
+                        }
+
+                    closureInput
+            ]
+
+        let globalNoWarns =
+            closureFiles
+            |> List.collect (fun (ClosureFile (_, _, _, _, _, noWarns)) -> noWarns)
 
         // Resolve all references.
         let references, unresolvedReferences, resolutionDiagnostics =
             let diagnosticsLogger = CapturingDiagnosticsLogger("GetLoadClosure")
 
-            use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> diagnosticsLogger)
-            let references, unresolvedReferences = TcAssemblyResolutions.GetAssemblyResolutionInformation(tcConfig)
+            use unwindEL = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
+
+            let references, unresolvedReferences =
+                TcAssemblyResolutions.GetAssemblyResolutionInformation(tcConfig)
+
             let references = references |> List.map (fun ar -> ar.resolvedPath, ar)
             references, unresolvedReferences, diagnosticsLogger.Diagnostics
 
         // Root errors and warnings - look at the last item in the closureFiles list
         let loadClosureRootDiagnostics, allRootDiagnostics =
             match List.rev closureFiles with
-            | ClosureFile(_, _, _, parseDiagnostics, metaDiagnostics, _) :: _ ->
+            | ClosureFile (_, _, _, parseDiagnostics, metaDiagnostics, _) :: _ ->
                 (earlierDiagnostics @ metaDiagnostics @ resolutionDiagnostics),
                 (parseDiagnostics @ earlierDiagnostics @ metaDiagnostics @ resolutionDiagnostics)
             | _ -> [], [] // When no file existed.
@@ -448,8 +588,13 @@ module ScriptPreprocessClosure =
             match GetRangeOfDiagnostic exn with
             | Some m ->
                 // Return true if the error was *not* from a #load-ed file.
-                let isArgParameterWhileNotEditing = (codeContext <> CodeContext.Editing) && (equals m range0 || equals m rangeStartup || equals m rangeCmdArgs)
-                let isThisFileName = (0 = String.Compare(rootFilename, m.FileName, StringComparison.OrdinalIgnoreCase))
+                let isArgParameterWhileNotEditing =
+                    (codeContext <> CodeContext.Editing)
+                    && (equals m range0 || equals m rangeStartup || equals m rangeCmdArgs)
+
+                let isThisFileName =
+                    (0 = String.Compare(rootFilename, m.FileName, StringComparison.OrdinalIgnoreCase))
+
                 isArgParameterWhileNotEditing || isThisFileName
             | None -> true
 
@@ -457,18 +602,20 @@ module ScriptPreprocessClosure =
         let allRootDiagnostics = allRootDiagnostics |> List.filter (fst >> isRootRange)
 
         let result: LoadClosure =
-            { SourceFiles = List.groupBy fst sourceFiles |> List.map (map2Of2 (List.map snd))
-              References = List.groupBy fst references |> List.map (map2Of2 (List.map snd))
-              PackageReferences = packageReferences
-              UseDesktopFramework = (tcConfig.primaryAssembly = PrimaryAssembly.Mscorlib)
-              SdkDirOverride = tcConfig.sdkDirOverride
-              UnresolvedReferences = unresolvedReferences
-              Inputs = sourceInputs
-              NoWarns = List.groupBy fst globalNoWarns |> List.map (map2Of2 (List.map snd))
-              OriginalLoadReferences = tcConfig.loadedSources
-              ResolutionDiagnostics = resolutionDiagnostics
-              AllRootFileDiagnostics = allRootDiagnostics
-              LoadClosureRootFileDiagnostics = loadClosureRootDiagnostics }
+            {
+                SourceFiles = List.groupBy fst sourceFiles |> List.map (map2Of2 (List.map snd))
+                References = List.groupBy fst references |> List.map (map2Of2 (List.map snd))
+                PackageReferences = packageReferences
+                UseDesktopFramework = (tcConfig.primaryAssembly = PrimaryAssembly.Mscorlib)
+                SdkDirOverride = tcConfig.sdkDirOverride
+                UnresolvedReferences = unresolvedReferences
+                Inputs = sourceInputs
+                NoWarns = List.groupBy fst globalNoWarns |> List.map (map2Of2 (List.map snd))
+                OriginalLoadReferences = tcConfig.loadedSources
+                ResolutionDiagnostics = resolutionDiagnostics
+                AllRootFileDiagnostics = allRootDiagnostics
+                LoadClosureRootFileDiagnostics = loadClosureRootDiagnostics
+            }
 
         result
 
@@ -498,42 +645,81 @@ module ScriptPreprocessClosure =
         // first, then #I and other directives are processed.
         let references0, assumeDotNetFramework, scriptDefaultReferencesDiagnostics =
             let tcConfig, scriptDefaultReferencesDiagnostics =
-                CreateScriptTextTcConfig(legacyReferenceResolver, defaultFSharpBinariesDir,
-                    fileName, codeContext, useSimpleResolution,
-                    useFsiAuxLib, None, applyCommandLineArgs, assumeDotNetFramework,
-                    useSdkRefs, sdkDirOverride, tryGetMetadataSnapshot, reduceMemoryUsage)
+                CreateScriptTextTcConfig(
+                    legacyReferenceResolver,
+                    defaultFSharpBinariesDir,
+                    fileName,
+                    codeContext,
+                    useSimpleResolution,
+                    useFsiAuxLib,
+                    None,
+                    applyCommandLineArgs,
+                    assumeDotNetFramework,
+                    useSdkRefs,
+                    sdkDirOverride,
+                    tryGetMetadataSnapshot,
+                    reduceMemoryUsage
+                )
+
+            let resolutions0, _unresolvedReferences =
+                TcAssemblyResolutions.GetAssemblyResolutionInformation(tcConfig)
+
+            let references0 =
+                resolutions0
+                |> List.map (fun r -> r.originalReference.Range, r.resolvedPath)
+                |> Seq.distinct
+                |> List.ofSeq
 
-            let resolutions0, _unresolvedReferences = TcAssemblyResolutions.GetAssemblyResolutionInformation(tcConfig)
-            let references0 = resolutions0 |> List.map (fun r->r.originalReference.Range, r.resolvedPath) |> Seq.distinct |> List.ofSeq
             references0, tcConfig.assumeDotNetFramework, scriptDefaultReferencesDiagnostics
 
         let tcConfig, scriptDefaultReferencesDiagnostics =
-            CreateScriptTextTcConfig(legacyReferenceResolver, defaultFSharpBinariesDir, fileName,
-                 codeContext, useSimpleResolution, useFsiAuxLib, Some (references0, scriptDefaultReferencesDiagnostics),
-                 applyCommandLineArgs, assumeDotNetFramework, useSdkRefs, sdkDirOverride,
-                 tryGetMetadataSnapshot, reduceMemoryUsage)
+            CreateScriptTextTcConfig(
+                legacyReferenceResolver,
+                defaultFSharpBinariesDir,
+                fileName,
+                codeContext,
+                useSimpleResolution,
+                useFsiAuxLib,
+                Some(references0, scriptDefaultReferencesDiagnostics),
+                applyCommandLineArgs,
+                assumeDotNetFramework,
+                useSdkRefs,
+                sdkDirOverride,
+                tryGetMetadataSnapshot,
+                reduceMemoryUsage
+            )
+
+        let closureSources = [ ClosureSource(fileName, range0, sourceText, true) ]
+
+        let closureFiles, tcConfig, packageReferences =
+            FindClosureFiles(fileName, closureSources, tcConfig, codeContext, lexResourceManager, dependencyProvider)
 
-        let closureSources = [ClosureSource(fileName, range0, sourceText, true)]
-        let closureFiles, tcConfig, packageReferences = FindClosureFiles(fileName, closureSources, tcConfig, codeContext, lexResourceManager, dependencyProvider)
         GetLoadClosure(fileName, closureFiles, tcConfig, codeContext, packageReferences, scriptDefaultReferencesDiagnostics)
 
     /// Given source file fileName, find the full load closure
     /// Used from fsi.fs and fsc.fs, for #load and command line
     let GetFullClosureOfScriptFiles
         (
-            tcConfig:TcConfig,
-            files:(string*range) list,
+            tcConfig: TcConfig,
+            files: (string * range) list,
             codeContext,
             lexResourceManager: Lexhelp.LexResourceManager,
             dependencyProvider
         ) =
 
         let mainFile, _mainFileRange = List.last files
-        let closureSources = files |> List.collect (fun (fileName, m) -> ClosureSourceOfFilename(fileName, m,tcConfig.inputCodePage,true))
-        let closureFiles, tcConfig, packageReferences = FindClosureFiles(mainFile, closureSources, tcConfig, codeContext, lexResourceManager, dependencyProvider)
+
+        let closureSources =
+            files
+            |> List.collect (fun (fileName, m) -> ClosureSourceOfFilename(fileName, m, tcConfig.inputCodePage, true))
+
+        let closureFiles, tcConfig, packageReferences =
+            FindClosureFiles(mainFile, closureSources, tcConfig, codeContext, lexResourceManager, dependencyProvider)
+
         GetLoadClosure(mainFile, closureFiles, tcConfig, codeContext, packageReferences, [])
 
 type LoadClosure with
+
     /// Analyze a script text and find the closure of its references.
     /// Used from FCS, when editing a script file.
     ///
@@ -559,20 +745,34 @@ type LoadClosure with
         ) =
 
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
-        ScriptPreprocessClosure.GetFullClosureOfScriptText
-            (legacyReferenceResolver, defaultFSharpBinariesDir, fileName, sourceText,
-             implicitDefines, useSimpleResolution, useFsiAuxLib, useSdkRefs, sdkDir, lexResourceManager,
-             applyCompilerOptions, assumeDotNetFramework, tryGetMetadataSnapshot, reduceMemoryUsage, dependencyProvider)
+
+        ScriptPreprocessClosure.GetFullClosureOfScriptText(
+            legacyReferenceResolver,
+            defaultFSharpBinariesDir,
+            fileName,
+            sourceText,
+            implicitDefines,
+            useSimpleResolution,
+            useFsiAuxLib,
+            useSdkRefs,
+            sdkDir,
+            lexResourceManager,
+            applyCompilerOptions,
+            assumeDotNetFramework,
+            tryGetMetadataSnapshot,
+            reduceMemoryUsage,
+            dependencyProvider
+        )
 
     /// Analyze a set of script files and find the closure of their references.
     static member ComputeClosureOfScriptFiles
         (
             tcConfig: TcConfig,
-            files:(string*range) list,
+            files: (string * range) list,
             implicitDefines,
             lexResourceManager: Lexhelp.LexResourceManager,
             dependencyProvider
         ) =
 
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
-        ScriptPreprocessClosure.GetFullClosureOfScriptFiles (tcConfig, files, implicitDefines, lexResourceManager, dependencyProvider)
+        ScriptPreprocessClosure.GetFullClosureOfScriptFiles(tcConfig, files, implicitDefines, lexResourceManager, dependencyProvider)
diff --git a/src/Compiler/Driver/StaticLinking.fs b/src/Compiler/Driver/StaticLinking.fs
index 94d3c7f3f33..6c23eb9b024 100644
--- a/src/Compiler/Driver/StaticLinking.fs
+++ b/src/Compiler/Driver/StaticLinking.fs
@@ -25,7 +25,7 @@ open FSharp.Compiler.TypeProviders
 #endif
 
 // Handles TypeForwarding for the generated IL model
-type TypeForwarding (tcImports: TcImports) =
+type TypeForwarding(tcImports: TcImports) =
 
     // Make a dictionary of ccus passed to the compiler will be looked up by qualified assembly name
     let ccuThunksQualifiedName =
@@ -40,18 +40,20 @@ type TypeForwarding (tcImports: TcImports) =
             if String.IsNullOrEmpty(ccuThunk.AssemblyName) then
                 None
             else
-                Some (ccuThunk.AssemblyName, ccuThunk))
+                Some(ccuThunk.AssemblyName, ccuThunk))
         |> dict
 
-    let followTypeForwardForILTypeRef (tref:ILTypeRef) =
+    let followTypeForwardForILTypeRef (tref: ILTypeRef) =
         let typename =
-            let parts =  tref.FullName.Split([|'.'|])
+            let parts = tref.FullName.Split([| '.' |])
+
             match parts.Length with
             | 0 -> None
-            | 1 -> Some (Array.empty, parts[0])
-            | n -> Some (parts[0..n-2], parts[n-1])
+            | 1 -> Some(Array.empty, parts[0])
+            | n -> Some(parts[0 .. n - 2], parts[n - 1])
+
+        let scoref = tref.Scope
 
-        let  scoref = tref.Scope
         match scoref with
         | ILScopeRef.Assembly scope ->
             match ccuThunksQualifiedName.TryGetValue(scope.QualifiedName) with
@@ -59,10 +61,12 @@ type TypeForwarding (tcImports: TcImports) =
                 match typename with
                 | Some (parts, name) ->
                     let forwarded = ccu.TryForward(parts, name)
+
                     let result =
                         match forwarded with
                         | Some fwd -> fwd.CompilationPath.ILScopeRef
                         | None -> scoref
+
                     result
                 | None -> scoref
             | false, _ ->
@@ -72,10 +76,12 @@ type TypeForwarding (tcImports: TcImports) =
                     match typename with
                     | Some (parts, name) ->
                         let forwarded = ccu.TryForward(parts, name)
+
                         let result =
                             match forwarded with
                             | Some fwd -> fwd.CompilationPath.ILScopeRef
                             | None -> scoref
+
                         result
                     | None -> scoref
                 | false, _ -> scoref
@@ -84,55 +90,83 @@ type TypeForwarding (tcImports: TcImports) =
     let typeForwardILTypeRef (tref: ILTypeRef) =
         let scoref1 = tref.Scope
         let scoref2 = followTypeForwardForILTypeRef tref
-        if scoref1 === scoref2 then tref
-        else ILTypeRef.Create (scoref2, tref.Enclosing, tref.Name)
+
+        if scoref1 === scoref2 then
+            tref
+        else
+            ILTypeRef.Create(scoref2, tref.Enclosing, tref.Name)
 
     member _.TypeForwardILTypeRef tref = typeForwardILTypeRef tref
 
 let debugStaticLinking = condition "FSHARP_DEBUG_STATIC_LINKING"
 
-let StaticLinkILModules (tcConfig:TcConfig, ilGlobals, tcImports, ilxMainModule, dependentILModules: (CcuThunk option * ILModuleDef) list) =
+let StaticLinkILModules
+    (
+        tcConfig: TcConfig,
+        ilGlobals,
+        tcImports,
+        ilxMainModule,
+        dependentILModules: (CcuThunk option * ILModuleDef) list
+    ) =
     if isNil dependentILModules then
         ilxMainModule, id
     else
         let typeForwarding = TypeForwarding(tcImports)
 
         // Check no dependent assemblies use quotations
-        let dependentCcuUsingQuotations = dependentILModules |> List.tryPick (function Some ccu, _ when ccu.UsesFSharp20PlusQuotations -> Some ccu | _ -> None)
+        let dependentCcuUsingQuotations =
+            dependentILModules
+            |> List.tryPick (function
+                | Some ccu, _ when ccu.UsesFSharp20PlusQuotations -> Some ccu
+                | _ -> None)
+
         match dependentCcuUsingQuotations with
-        | Some ccu -> error(Error(FSComp.SR.fscQuotationLiteralsStaticLinking(ccu.AssemblyName), rangeStartup))
+        | Some ccu -> error (Error(FSComp.SR.fscQuotationLiteralsStaticLinking (ccu.AssemblyName), rangeStartup))
         | None -> ()
 
         // Check we're not static linking a .EXE
-        if dependentILModules |> List.exists (fun (_, x) -> not x.IsDLL)  then
-            error(Error(FSComp.SR.fscStaticLinkingNoEXE(), rangeStartup))
+        if dependentILModules |> List.exists (fun (_, x) -> not x.IsDLL) then
+            error (Error(FSComp.SR.fscStaticLinkingNoEXE (), rangeStartup))
 
         // Check we're not static linking something that is not pure IL
-        if dependentILModules |> List.exists (fun (_, x) -> not x.IsILOnly)  then
-            error(Error(FSComp.SR.fscStaticLinkingNoMixedDLL(), rangeStartup))
+        if dependentILModules |> List.exists (fun (_, x) -> not x.IsILOnly) then
+            error (Error(FSComp.SR.fscStaticLinkingNoMixedDLL (), rangeStartup))
 
         // The set of short names for the all dependent assemblies
         let assems =
-            set [ for _, m in dependentILModules  do
-                    match m.Manifest with
-                    | Some m -> yield m.Name
-                    | _ -> () ]
+            set
+                [
+                    for _, m in dependentILModules do
+                        match m.Manifest with
+                        | Some m -> m.Name
+                        | _ -> ()
+                ]
 
         // A rewriter which rewrites scope references to things in dependent assemblies to be local references
         let rewriteExternalRefsToLocalRefs x =
-            if assems.Contains (getNameOfScopeRef x) then ILScopeRef.Local else x
+            if assems.Contains(getNameOfScopeRef x) then
+                ILScopeRef.Local
+            else
+                x
 
         let savedManifestAttrs =
-            [ for _, depILModule in dependentILModules do
-                match depILModule.Manifest with
-                | Some m ->
-                    for ca in m.CustomAttrs.AsArray() do
-                        if ca.Method.MethodRef.DeclaringTypeRef.FullName = typeof.FullName then
-                            yield ca
-                | _ -> () ]
+            [
+                for _, depILModule in dependentILModules do
+                    match depILModule.Manifest with
+                    | Some m ->
+                        for ca in m.CustomAttrs.AsArray() do
+                            if ca.Method.MethodRef.DeclaringTypeRef.FullName = typeof.FullName then
+                                ca
+                    | _ -> ()
+            ]
 
         let savedResources =
-            let allResources = [ for ccu, m in dependentILModules do for r in m.Resources.AsList() do yield (ccu, r) ]
+            let allResources =
+                [
+                    for ccu, m in dependentILModules do
+                        for r in m.Resources.AsList() do
+                            (ccu, r)
+                ]
             // Don't save interface, optimization or resource definitions for provider-generated assemblies.
             // These are "fake".
             let isProvided (ccu: CcuThunk option) =
@@ -146,21 +180,29 @@ let StaticLinkILModules (tcConfig:TcConfig, ilGlobals, tcImports, ilxMainModule,
 #endif
 
             // Save only the interface/optimization attributes of generated data
-            let intfDataResources, others = allResources |> List.partition (snd >> IsSignatureDataResource)
+            let intfDataResources, others =
+                allResources |> List.partition (snd >> IsSignatureDataResource)
+
             let intfDataResources =
-                [ for ccu, r in intfDataResources do
-                     if tcConfig.GenerateSignatureData && not (isProvided ccu) then
-                         yield r ]
+                [
+                    for ccu, r in intfDataResources do
+                        if tcConfig.GenerateSignatureData && not (isProvided ccu) then
+                            r
+                ]
+
+            let optDataResources, others =
+                others |> List.partition (snd >> IsOptimizationDataResource)
 
-            let optDataResources, others = others |> List.partition (snd >> IsOptimizationDataResource)
             let optDataResources =
-                [ for ccu, r in optDataResources do
-                    if tcConfig.GenerateOptimizationData && not (isProvided ccu) then
-                        yield r ]
+                [
+                    for ccu, r in optDataResources do
+                        if tcConfig.GenerateOptimizationData && not (isProvided ccu) then
+                            r
+                ]
 
             let otherResources = others |> List.map snd
 
-            let result = intfDataResources@optDataResources@otherResources
+            let result = intfDataResources @ optDataResources @ otherResources
             result
 
         let moduls = ilxMainModule :: (List.map snd dependentILModules)
@@ -168,39 +210,62 @@ let StaticLinkILModules (tcConfig:TcConfig, ilGlobals, tcImports, ilxMainModule,
         let savedNativeResources =
             [ //yield! ilxMainModule.NativeResources
                 for m in moduls do
-                    yield! m.NativeResources ]
+                    yield! m.NativeResources
+            ]
 
         let topTypeDefs, normalTypeDefs =
             moduls
-            |> List.map (fun m -> m.TypeDefs.AsList() |> List.partition (fun td -> isTypeNameForGlobalFunctions td.Name))
+            |> List.map (fun m ->
+                m.TypeDefs.AsList()
+                |> List.partition (fun td -> isTypeNameForGlobalFunctions td.Name))
             |> List.unzip
 
         let topTypeDef =
             let topTypeDefs = List.concat topTypeDefs
-            mkILTypeDefForGlobalFunctions ilGlobals
+
+            mkILTypeDefForGlobalFunctions
+                ilGlobals
                 (mkILMethods (topTypeDefs |> List.collect (fun td -> td.Methods.AsList())),
-                mkILFields (topTypeDefs |> List.collect (fun td -> td.Fields.AsList())))
+                 mkILFields (topTypeDefs |> List.collect (fun td -> td.Fields.AsList())))
+
+        let oldManifest = ilxMainModule.ManifestOfAssembly
+
+        let newManifest =
+            { oldManifest with
+                CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs (oldManifest.CustomAttrs.AsList() @ savedManifestAttrs))
+            }
 
         let ilxMainModule =
             let main =
                 { ilxMainModule with
-                    Manifest = (let m = ilxMainModule.ManifestOfAssembly in Some {m with CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs (m.CustomAttrs.AsList() @ savedManifestAttrs)) })
-                    CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs [ for m in moduls do yield! m.CustomAttrs.AsArray() ])
+                    Manifest = Some newManifest
+                    CustomAttrsStored =
+                        storeILCustomAttrs (
+                            mkILCustomAttrs
+                                [
+                                    for m in moduls do
+                                        yield! m.CustomAttrs.AsArray()
+                                ]
+                        )
                     TypeDefs = mkILTypeDefs (topTypeDef :: List.concat normalTypeDefs)
                     Resources = mkILResources (savedResources @ ilxMainModule.Resources.AsList())
-                    NativeResources = savedNativeResources }
+                    NativeResources = savedNativeResources
+                }
+
             Morphs.morphILTypeRefsInILModuleMemoized typeForwarding.TypeForwardILTypeRef main
 
         ilxMainModule, rewriteExternalRefsToLocalRefs
 
 []
 type Node =
-    { name: string
-      data: ILModuleDef
-      ccu: CcuThunk option
-      refs: ILReferences
-      mutable edges: Node list
-      mutable visited: bool }
+    {
+        name: string
+        data: ILModuleDef
+        ccu: CcuThunk option
+        refs: ILReferences
+        mutable edges: Node list
+        mutable visited: bool
+    }
 
 // Find all IL modules that are to be statically linked given the static linking roots.
 let FindDependentILModulesForStaticLinking (ctok, tcConfig: TcConfig, tcImports: TcImports, ilGlobals: ILGlobals, ilxMainModule) =
@@ -209,130 +274,217 @@ let FindDependentILModulesForStaticLinking (ctok, tcConfig: TcConfig, tcImports:
     else
         // Recursively find all referenced modules and add them to a module graph
         let depModuleTable = HashMultiMap(0, HashIdentity.Structural)
+
         let dummyEntry nm =
-            { refs = emptyILRefs
-              name=nm
-              ccu=None
-              data=ilxMainModule // any old module
-              edges = []
-              visited = true }
-        let assumedIndependentSet = set [ "mscorlib";  "System"; "System.Core"; "System.Xml"; "Microsoft.Build.Framework"; "Microsoft.Build.Utilities"; "netstandard" ]
-
-        begin
-            let mutable remaining = (computeILRefs ilGlobals ilxMainModule).AssemblyReferences |> Array.toList
-            while not (isNil remaining) do
-                let ilAssemRef = List.head remaining
-                remaining <- List.tail remaining
-                if assumedIndependentSet.Contains ilAssemRef.Name || (ilAssemRef.PublicKey = Some ecmaPublicKey) then
-                    depModuleTable[ilAssemRef.Name] <- dummyEntry ilAssemRef.Name
-                else
-                    if not (depModuleTable.ContainsKey ilAssemRef.Name) then
-                        match tcImports.TryFindDllInfo(ctok, rangeStartup, ilAssemRef.Name, lookupOnly=false) with
-                        | Some dllInfo ->
-                            let ccu =
-                                match tcImports.FindCcuFromAssemblyRef (ctok, rangeStartup, ilAssemRef) with
-                                | ResolvedCcu ccu -> Some ccu
-                                | UnresolvedCcu(_ccuName) -> None
-
-                            let fileName = dllInfo.FileName
-                            let modul =
-                                let pdbDirPathOption =
-                                    // We open the pdb file if one exists parallel to the binary we
-                                    // are reading, so that --standalone will preserve debug information.
-                                    if tcConfig.openDebugInformationForLaterStaticLinking then
-                                        let pdbDir = (try FileSystem.GetDirectoryNameShim fileName with _ -> ".")
-                                        let pdbFile = (try FileSystemUtils.chopExtension fileName with _ -> fileName)+".pdb"
-                                        if FileSystem.FileExistsShim pdbFile then
-                                            Some pdbDir
-                                        else
-                                            None
-                                    else
-                                        None
-
-                                let opts : ILReaderOptions =
-                                    { metadataOnly = MetadataOnlyFlag.No // turn this off here as we need the actual IL code
-                                      reduceMemoryUsage = tcConfig.reduceMemoryUsage
-                                      pdbDirPath = pdbDirPathOption
-                                      tryGetMetadataSnapshot = (fun _ -> None) }
-
-                                let reader = OpenILModuleReader dllInfo.FileName opts
-                                reader.ILModuleDef
-
-                            let refs =
-                                if ilAssemRef.Name = GetFSharpCoreLibraryName() then
-                                    emptyILRefs
-                                elif not modul.IsILOnly then
-                                    warning(Error(FSComp.SR.fscIgnoringMixedWhenLinking ilAssemRef.Name, rangeStartup))
-                                    emptyILRefs
+            {
+                refs = emptyILRefs
+                name = nm
+                ccu = None
+                data = ilxMainModule // any old module
+                edges = []
+                visited = true
+            }
+
+        let assumedIndependentSet =
+            set
+                [
+                    "mscorlib"
+                    "System"
+                    "System.Core"
+                    "System.Xml"
+                    "Microsoft.Build.Framework"
+                    "Microsoft.Build.Utilities"
+                    "netstandard"
+                ]
+
+        let mutable remaining =
+            (computeILRefs ilGlobals ilxMainModule).AssemblyReferences |> Array.toList
+
+        while not (isNil remaining) do
+            let ilAssemRef = List.head remaining
+            remaining <- List.tail remaining
+
+            if assumedIndependentSet.Contains ilAssemRef.Name
+               || (ilAssemRef.PublicKey = Some ecmaPublicKey) then
+                depModuleTable[ilAssemRef.Name] <- dummyEntry ilAssemRef.Name
+            else if not (depModuleTable.ContainsKey ilAssemRef.Name) then
+                match tcImports.TryFindDllInfo(ctok, rangeStartup, ilAssemRef.Name, lookupOnly = false) with
+                | Some dllInfo ->
+                    let ccu =
+                        match tcImports.FindCcuFromAssemblyRef(ctok, rangeStartup, ilAssemRef) with
+                        | ResolvedCcu ccu -> Some ccu
+                        | UnresolvedCcu (_ccuName) -> None
+
+                    let fileName = dllInfo.FileName
+
+                    let modul =
+                        let pdbDirPathOption =
+                            // We open the pdb file if one exists parallel to the binary we
+                            // are reading, so that --standalone will preserve debug information.
+                            if tcConfig.openDebugInformationForLaterStaticLinking then
+                                let pdbDir =
+                                    (try
+                                        FileSystem.GetDirectoryNameShim fileName
+                                     with _ ->
+                                         ".")
+
+                                let pdbFile =
+                                    (try
+                                        FileSystemUtils.chopExtension fileName
+                                     with _ ->
+                                         fileName)
+                                    + ".pdb"
+
+                                if FileSystem.FileExistsShim pdbFile then
+                                    Some pdbDir
                                 else
-                                    { AssemblyReferences = dllInfo.ILAssemblyRefs |> List.toArray
-                                      ModuleReferences = [| |]
-                                      TypeReferences = [| |]
-                                      MethodReferences = [| |]
-                                      FieldReferences = [||] }
-
-                            depModuleTable[ilAssemRef.Name] <-
-                                { refs=refs
-                                  name=ilAssemRef.Name
-                                  ccu=ccu
-                                  data=modul
-                                  edges = []
-                                  visited = false }
-
-                            // Push the new work items
-                            remaining <- Array.toList refs.AssemblyReferences @ remaining
-
-                        | None ->
-                            warning(Error(FSComp.SR.fscAssumeStaticLinkContainsNoDependencies(ilAssemRef.Name), rangeStartup))
-                            depModuleTable[ilAssemRef.Name] <- dummyEntry ilAssemRef.Name
-            done
-        end
+                                    None
+                            else
+                                None
+
+                        let opts: ILReaderOptions =
+                            {
+                                metadataOnly = MetadataOnlyFlag.No // turn this off here as we need the actual IL code
+                                reduceMemoryUsage = tcConfig.reduceMemoryUsage
+                                pdbDirPath = pdbDirPathOption
+                                tryGetMetadataSnapshot = (fun _ -> None)
+                            }
+
+                        let reader = OpenILModuleReader dllInfo.FileName opts
+                        reader.ILModuleDef
+
+                    let refs =
+                        if ilAssemRef.Name = GetFSharpCoreLibraryName() then
+                            emptyILRefs
+                        elif not modul.IsILOnly then
+                            warning (Error(FSComp.SR.fscIgnoringMixedWhenLinking ilAssemRef.Name, rangeStartup))
+                            emptyILRefs
+                        else
+                            {
+                                AssemblyReferences = dllInfo.ILAssemblyRefs |> List.toArray
+                                ModuleReferences = [||]
+                                TypeReferences = [||]
+                                MethodReferences = [||]
+                                FieldReferences = [||]
+                            }
+
+                    depModuleTable[ilAssemRef.Name] <-
+                        {
+                            refs = refs
+                            name = ilAssemRef.Name
+                            ccu = ccu
+                            data = modul
+                            edges = []
+                            visited = false
+                        }
+
+                    // Push the new work items
+                    remaining <- Array.toList refs.AssemblyReferences @ remaining
+
+                | None ->
+                    warning (Error(FSComp.SR.fscAssumeStaticLinkContainsNoDependencies (ilAssemRef.Name), rangeStartup))
+                    depModuleTable[ilAssemRef.Name] <- dummyEntry ilAssemRef.Name
 
         ReportTime tcConfig "Find dependencies"
 
         // Add edges from modules to the modules that depend on them
-        for KeyValue(_, n) in depModuleTable do
+        for KeyValue (_, n) in depModuleTable do
             for aref in n.refs.AssemblyReferences do
                 let n2 = depModuleTable[aref.Name]
                 n2.edges <- n :: n2.edges
 
         // Find everything that depends on FSharp.Core
         let roots =
-            [ if tcConfig.standalone && depModuleTable.ContainsKey (GetFSharpCoreLibraryName()) then
-                  yield depModuleTable[GetFSharpCoreLibraryName()]
-              for n in tcConfig.extraStaticLinkRoots  do
-                  match depModuleTable.TryFind n with
-                  | Some x -> yield x
-                  | None -> error(Error(FSComp.SR.fscAssemblyNotFoundInDependencySet n, rangeStartup))
+            [
+                if tcConfig.standalone && depModuleTable.ContainsKey(GetFSharpCoreLibraryName()) then
+                    depModuleTable[GetFSharpCoreLibraryName()]
+                for n in tcConfig.extraStaticLinkRoots do
+                    match depModuleTable.TryFind n with
+                    | Some x -> x
+                    | None -> error (Error(FSComp.SR.fscAssemblyNotFoundInDependencySet n, rangeStartup))
             ]
 
         let mutable remaining = roots
-        [ while not (isNil remaining) do
-            let n = List.head remaining
-            remaining <- List.tail remaining
-            if not n.visited then
-                n.visited <- true
-                remaining <- n.edges @ remaining
-                yield (n.ccu, n.data)  ]
+
+        [
+            while not (isNil remaining) do
+                let n = List.head remaining
+                remaining <- List.tail remaining
+
+                if not n.visited then
+                    n.visited <- true
+                    remaining <- n.edges @ remaining
+                    (n.ccu, n.data)
+        ]
 
 // Add all provider-generated assemblies into the static linking set
 let FindProviderGeneratedILModules (ctok, tcImports: TcImports, providerGeneratedAssemblies: (ImportedBinary * _) list) =
-    [ for importedBinary, provAssemStaticLinkInfo in providerGeneratedAssemblies do
-        let ilAssemRef =
-            match importedBinary.ILScopeRef with
-            | ILScopeRef.Assembly aref -> aref
-            | _ -> failwith "Invalid ILScopeRef, expected ILScopeRef.Assembly"
-        if debugStaticLinking then printfn "adding provider-generated assembly '%s' into static linking set" ilAssemRef.Name
-        match tcImports.TryFindDllInfo(ctok, rangeStartup, ilAssemRef.Name, lookupOnly=false) with
-        | Some dllInfo ->
-            let ccu =
-                match tcImports.FindCcuFromAssemblyRef (ctok, rangeStartup, ilAssemRef) with
-                | ResolvedCcu ccu -> Some ccu
-                | UnresolvedCcu(_ccuName) -> None
-
-            let modul = dllInfo.RawMetadata.TryGetILModuleDef().Value
-            yield (ccu, dllInfo.ILScopeRef, modul), (ilAssemRef.Name, provAssemStaticLinkInfo)
-        | None -> () ]
+    [
+        for importedBinary, provAssemStaticLinkInfo in providerGeneratedAssemblies do
+            let ilAssemRef =
+                match importedBinary.ILScopeRef with
+                | ILScopeRef.Assembly aref -> aref
+                | _ -> failwith "Invalid ILScopeRef, expected ILScopeRef.Assembly"
+
+            if debugStaticLinking then
+                printfn "adding provider-generated assembly '%s' into static linking set" ilAssemRef.Name
+
+            match tcImports.TryFindDllInfo(ctok, rangeStartup, ilAssemRef.Name, lookupOnly = false) with
+            | Some dllInfo ->
+                let ccu =
+                    match tcImports.FindCcuFromAssemblyRef(ctok, rangeStartup, ilAssemRef) with
+                    | ResolvedCcu ccu -> Some ccu
+                    | UnresolvedCcu (_ccuName) -> None
+
+                let modul = dllInfo.RawMetadata.TryGetILModuleDef().Value
+                (ccu, dllInfo.ILScopeRef, modul), (ilAssemRef.Name, provAssemStaticLinkInfo)
+            | None -> ()
+    ]
+
+/// Split the list into left, middle and right parts at the first element satisfying 'p'. If no element matches return
+/// 'None' for the middle part.
+let trySplitFind p xs =
+    let rec loop xs acc =
+        match xs with
+        | [] -> List.rev acc, None, []
+        | h :: t -> if p h then List.rev acc, Some h, t else loop t (h :: acc)
+
+    loop xs []
+
+/// Implant the (nested) type definition 'td' at path 'enc' in 'tdefs'.
+let rec implantTypeDef ilGlobals isNested (tdefs: ILTypeDefs) (enc: string list) (td: ILTypeDef) =
+    match enc with
+    | [] -> addILTypeDef td tdefs
+    | h :: t ->
+        let tdefs = tdefs.AsList()
+
+        let ltdefs, htd, rtdefs =
+            match tdefs |> trySplitFind (fun td -> td.Name = h) with
+            | ltdefs, None, rtdefs ->
+                let access =
+                    if isNested then
+                        ILTypeDefAccess.Nested ILMemberAccess.Public
+                    else
+                        ILTypeDefAccess.Public
+
+                let fresh =
+                    mkILSimpleClass
+                        ilGlobals
+                        (h,
+                         access,
+                         emptyILMethods,
+                         emptyILFields,
+                         emptyILTypeDefs,
+                         emptyILProperties,
+                         emptyILEvents,
+                         emptyILCustomAttrs,
+                         ILTypeInit.OnAny)
+
+                (ltdefs, fresh, rtdefs)
+            | ltdefs, Some htd, rtdefs -> (ltdefs, htd, rtdefs)
+
+        let htd = htd.With(nestedTypes = implantTypeDef ilGlobals true htd.NestedTypes t td)
+        mkILTypeDefs (ltdefs @ [ htd ] @ rtdefs)
 
 // Compute a static linker. This only captures tcImports (a large data structure) if
 // static linking is enabled. Normally this is not the case, which lets us collect tcImports
@@ -343,181 +495,231 @@ let StaticLink (ctok, tcConfig: TcConfig, tcImports: TcImports, ilGlobals: ILGlo
     let providerGeneratedAssemblies =
 
         [ // Add all EST-generated assemblies into the static linking set
-            for KeyValue(_, importedBinary: ImportedBinary) in tcImports.DllTable do
+            for KeyValue (_, importedBinary: ImportedBinary) in tcImports.DllTable do
                 if importedBinary.IsProviderGenerated then
                     match importedBinary.ProviderGeneratedStaticLinkMap with
                     | None -> ()
-                    | Some provAssemStaticLinkInfo -> yield (importedBinary, provAssemStaticLinkInfo) ]
+                    | Some provAssemStaticLinkInfo -> (importedBinary, provAssemStaticLinkInfo)
+        ]
 #endif
-    if not tcConfig.standalone && tcConfig.extraStaticLinkRoots.IsEmpty
+    if not tcConfig.standalone
+       && tcConfig.extraStaticLinkRoots.IsEmpty
 #if !NO_TYPEPROVIDERS
-            && providerGeneratedAssemblies.IsEmpty
+       && providerGeneratedAssemblies.IsEmpty
 #endif
-            then
+    then
         id
     else
-        (fun ilxMainModule  ->
+        (fun ilxMainModule ->
             match tcConfig.emitMetadataAssembly with
             | MetadataAssemblyGeneration.None -> ()
-            | _ ->
-                error(Error(FSComp.SR.optsInvalidRefAssembly(), rangeCmdArgs))
+            | _ -> error (Error(FSComp.SR.optsInvalidRefAssembly (), rangeCmdArgs))
 
             ReportTime tcConfig "Find assembly references"
 
-            let dependentILModules = FindDependentILModulesForStaticLinking (ctok, tcConfig, tcImports, ilGlobals, ilxMainModule)
+            let dependentILModules =
+                FindDependentILModulesForStaticLinking(ctok, tcConfig, tcImports, ilGlobals, ilxMainModule)
 
             ReportTime tcConfig "Static link"
 
 #if !NO_TYPEPROVIDERS
-            Morphs.enableMorphCustomAttributeData()
-            let providerGeneratedILModules =  FindProviderGeneratedILModules (ctok, tcImports, providerGeneratedAssemblies)
+            Morphs.enableMorphCustomAttributeData ()
+
+            let providerGeneratedILModules =
+                FindProviderGeneratedILModules(ctok, tcImports, providerGeneratedAssemblies)
 
             // Transform the ILTypeRefs references in the IL of all provider-generated assemblies so that the references
             // are now local.
             let providerGeneratedILModules =
 
-                providerGeneratedILModules |> List.map (fun ((ccu, ilOrigScopeRef, ilModule), (_, localProvAssemStaticLinkInfo)) ->
+                providerGeneratedILModules
+                |> List.map (fun ((ccu, ilOrigScopeRef, ilModule), (_, localProvAssemStaticLinkInfo)) ->
                     let ilAssemStaticLinkMap =
-                        dict [ for _, (_, provAssemStaticLinkInfo) in providerGeneratedILModules do
-                                    for KeyValue(k, v) in provAssemStaticLinkInfo.ILTypeMap do
-                                        yield (k, v)
-                               for KeyValue(k, v) in localProvAssemStaticLinkInfo.ILTypeMap do
-                                   yield (ILTypeRef.Create(ILScopeRef.Local, k.Enclosing, k.Name), v) ]
+                        dict
+                            [
+                                for _, (_, provAssemStaticLinkInfo) in providerGeneratedILModules do
+                                    for KeyValue (k, v) in provAssemStaticLinkInfo.ILTypeMap do
+                                        (k, v)
+                                for KeyValue (k, v) in localProvAssemStaticLinkInfo.ILTypeMap do
+                                    (ILTypeRef.Create(ILScopeRef.Local, k.Enclosing, k.Name), v)
+                            ]
 
                     let ilModule =
-                        ilModule |> Morphs.morphILTypeRefsInILModuleMemoized (fun tref ->
-                                if debugStaticLinking then printfn "deciding whether to rewrite type ref %A" tref.QualifiedName
-                                let ok, v = ilAssemStaticLinkMap.TryGetValue tref
-                                if ok then
-                                    if debugStaticLinking then printfn "rewriting type ref %A to %A" tref.QualifiedName v.QualifiedName
-                                    v
-                                else
-                                    tref)
+                        ilModule
+                        |> Morphs.morphILTypeRefsInILModuleMemoized (fun tref ->
+                            if debugStaticLinking then
+                                printfn "deciding whether to rewrite type ref %A" tref.QualifiedName
+
+                            match ilAssemStaticLinkMap.TryGetValue tref with
+                            | true, v ->
+                                if debugStaticLinking then
+                                    printfn "rewriting type ref %A to %A" tref.QualifiedName v.QualifiedName
+
+                                v
+                            | _ -> tref)
+
                     (ccu, ilOrigScopeRef, ilModule))
 
             // Relocate provider generated type definitions into the expected shape for the [] declarations in an assembly
             let providerGeneratedILModules, ilxMainModule =
-                  // Build a dictionary of all remapped IL type defs
-                  let ilOrigTyRefsForProviderGeneratedTypesToRelocate =
-                      let rec walk acc (ProviderGeneratedType(ilOrigTyRef, _, xs) as node) = List.fold walk ((ilOrigTyRef, node) :: acc) xs
-                      dict (Seq.fold walk [] tcImports.ProviderGeneratedTypeRoots)
-
-                  // Build a dictionary of all IL type defs, mapping ilOrigTyRef --> ilTypeDef
-                  let allTypeDefsInProviderGeneratedAssemblies =
-                      let rec loop ilOrigTyRef (ilTypeDef: ILTypeDef) =
-                          seq { yield (ilOrigTyRef, ilTypeDef)
-                                for ntdef in ilTypeDef.NestedTypes do
-                                    yield! loop (mkILTyRefInTyRef (ilOrigTyRef, ntdef.Name)) ntdef }
-                      dict [
-                          for _ccu, ilOrigScopeRef, ilModule in providerGeneratedILModules do
-                              for td in ilModule.TypeDefs do
-                                  yield! loop (mkILTyRef (ilOrigScopeRef, td.Name)) td ]
-
-
-                  // Debugging output
-                  if debugStaticLinking then
-                      for ProviderGeneratedType(ilOrigTyRef, _, _) in tcImports.ProviderGeneratedTypeRoots do
-                          printfn "Have [] root '%s'" ilOrigTyRef.QualifiedName
-
-                  // Build the ILTypeDefs for generated types, starting with the roots
-                  let generatedILTypeDefs =
-                      let rec buildRelocatedGeneratedType (ProviderGeneratedType(ilOrigTyRef, ilTgtTyRef, ch)) =
-                          let isNested = not (isNil ilTgtTyRef.Enclosing)
-                          match allTypeDefsInProviderGeneratedAssemblies.TryGetValue ilOrigTyRef with
-                          | true, ilOrigTypeDef ->
-                              if debugStaticLinking then printfn "Relocating %s to %s " ilOrigTyRef.QualifiedName ilTgtTyRef.QualifiedName
-                              let ilOrigTypeDef =
+                // Build a dictionary of all remapped IL type defs
+                let ilOrigTyRefsForProviderGeneratedTypesToRelocate =
+                    let rec walk acc (ProviderGeneratedType (ilOrigTyRef, _, xs) as node) =
+                        List.fold walk ((ilOrigTyRef, node) :: acc) xs
+
+                    dict (Seq.fold walk [] tcImports.ProviderGeneratedTypeRoots)
+
+                // Build a dictionary of all IL type defs, mapping ilOrigTyRef --> ilTypeDef
+                let allTypeDefsInProviderGeneratedAssemblies =
+                    let rec loop ilOrigTyRef (ilTypeDef: ILTypeDef) =
+                        seq {
+                            (ilOrigTyRef, ilTypeDef)
+
+                            for ntdef in ilTypeDef.NestedTypes do
+                                yield! loop (mkILTyRefInTyRef (ilOrigTyRef, ntdef.Name)) ntdef
+                        }
+
+                    dict
+                        [
+                            for _ccu, ilOrigScopeRef, ilModule in providerGeneratedILModules do
+                                for td in ilModule.TypeDefs do
+                                    yield! loop (mkILTyRef (ilOrigScopeRef, td.Name)) td
+                        ]
+
+                // Debugging output
+                if debugStaticLinking then
+                    for ProviderGeneratedType (ilOrigTyRef, _, _) in tcImports.ProviderGeneratedTypeRoots do
+                        printfn "Have [] root '%s'" ilOrigTyRef.QualifiedName
+
+                // Build the ILTypeDefs for generated types, starting with the roots
+                let generatedILTypeDefs =
+                    let rec buildRelocatedGeneratedType (ProviderGeneratedType (ilOrigTyRef, ilTgtTyRef, ch)) =
+                        let isNested = not (isNil ilTgtTyRef.Enclosing)
+
+                        match allTypeDefsInProviderGeneratedAssemblies.TryGetValue ilOrigTyRef with
+                        | true, ilOrigTypeDef ->
+                            if debugStaticLinking then
+                                printfn "Relocating %s to %s " ilOrigTyRef.QualifiedName ilTgtTyRef.QualifiedName
+
+                            let ilOrigTypeDef =
                                 if isNested then
+                                    ilOrigTypeDef.WithAccess(
+                                        match ilOrigTypeDef.Access with
+                                        | ILTypeDefAccess.Public -> ILTypeDefAccess.Nested ILMemberAccess.Public
+                                        | ILTypeDefAccess.Private -> ILTypeDefAccess.Nested ILMemberAccess.Private
+                                        | _ -> ilOrigTypeDef.Access
+                                    )
+                                else
                                     ilOrigTypeDef
-                                        .WithAccess(match ilOrigTypeDef.Access with
-                                                    | ILTypeDefAccess.Public -> ILTypeDefAccess.Nested ILMemberAccess.Public
-                                                    | ILTypeDefAccess.Private -> ILTypeDefAccess.Nested ILMemberAccess.Private
-                                                    | _ -> ilOrigTypeDef.Access)
-                                else ilOrigTypeDef
-                              ilOrigTypeDef.With(name = ilTgtTyRef.Name,
-                                                 nestedTypes = mkILTypeDefs (List.map buildRelocatedGeneratedType ch))
-                          | _ ->
-                              // If there is no matching IL type definition, then make a simple container class
-                              if debugStaticLinking then
-                                  printfn "Generating simple class '%s' because we didn't find an original type '%s' in a provider generated assembly"
-                                      ilTgtTyRef.QualifiedName ilOrigTyRef.QualifiedName
-
-                              let access = (if isNested  then ILTypeDefAccess.Nested ILMemberAccess.Public else ILTypeDefAccess.Public)
-                              let tdefs = mkILTypeDefs (List.map buildRelocatedGeneratedType ch)
-                              mkILSimpleClass ilGlobals (ilTgtTyRef.Name, access, emptyILMethods, emptyILFields, tdefs, emptyILProperties, emptyILEvents, emptyILCustomAttrs, ILTypeInit.OnAny)
-
-                      [ for ProviderGeneratedType(_, ilTgtTyRef, _) as node in tcImports.ProviderGeneratedTypeRoots  do
-                           yield (ilTgtTyRef, buildRelocatedGeneratedType node) ]
-
-                  // Implant all the generated type definitions into the ilxMainModule (generating a new ilxMainModule)
-                  let ilxMainModule =
-
-                      /// Split the list into left, middle and right parts at the first element satisfying 'p'. If no element matches return
-                      /// 'None' for the middle part.
-                      let trySplitFind p xs =
-                          let rec loop xs acc =
-                              match xs with
-                              | [] -> List.rev acc, None, []
-                              | h :: t -> if p h then List.rev acc, Some h, t else loop t (h :: acc)
-                          loop xs []
-
-                      /// Implant the (nested) type definition 'td' at path 'enc' in 'tdefs'.
-                      let rec implantTypeDef isNested (tdefs: ILTypeDefs) (enc: string list) (td: ILTypeDef) =
-                          match enc with
-                          | [] -> addILTypeDef td tdefs
-                          | h :: t ->
-                               let tdefs = tdefs.AsList()
-                               let ltdefs, htd, rtdefs =
-                                   match tdefs |> trySplitFind (fun td -> td.Name = h) with
-                                   | ltdefs, None, rtdefs ->
-                                       let access = if isNested  then ILTypeDefAccess.Nested ILMemberAccess.Public else ILTypeDefAccess.Public
-                                       let fresh = mkILSimpleClass ilGlobals (h, access, emptyILMethods, emptyILFields, emptyILTypeDefs, emptyILProperties, emptyILEvents, emptyILCustomAttrs, ILTypeInit.OnAny)
-                                       (ltdefs, fresh, rtdefs)
-                                   | ltdefs, Some htd, rtdefs ->
-                                       (ltdefs, htd, rtdefs)
-                               let htd = htd.With(nestedTypes = implantTypeDef true htd.NestedTypes t td)
-                               mkILTypeDefs (ltdefs @ [htd] @ rtdefs)
-
-                      let newTypeDefs =
-                          (ilxMainModule.TypeDefs, generatedILTypeDefs) ||> List.fold (fun acc (ilTgtTyRef, td) ->
-                              if debugStaticLinking then printfn "implanting '%s' at '%s'" td.Name ilTgtTyRef.QualifiedName
-                              implantTypeDef false acc ilTgtTyRef.Enclosing td)
-                      { ilxMainModule with TypeDefs = newTypeDefs }
-
-                  // Remove any ILTypeDefs from the provider generated modules if they have been relocated because of a [] declaration.
-                  let providerGeneratedILModules =
-                      providerGeneratedILModules |> List.map (fun (ccu, ilOrigScopeRef, ilModule) ->
-                          let ilTypeDefsAfterRemovingRelocatedTypes =
-                              let rec rw enc (tdefs: ILTypeDefs) =
-                                  mkILTypeDefs
-                                   [ for tdef in tdefs do
-                                        let ilOrigTyRef = mkILNestedTyRef (ilOrigScopeRef, enc, tdef.Name)
-                                        if  not (ilOrigTyRefsForProviderGeneratedTypesToRelocate.ContainsKey ilOrigTyRef) then
-                                          if debugStaticLinking then printfn "Keep provided type %s in place because it wasn't relocated" ilOrigTyRef.QualifiedName
-                                          yield tdef.With(nestedTypes = rw (enc@[tdef.Name]) tdef.NestedTypes) ]
-                              rw [] ilModule.TypeDefs
-                          (ccu, { ilModule with TypeDefs = ilTypeDefsAfterRemovingRelocatedTypes }))
-
-                  providerGeneratedILModules, ilxMainModule
-
-            Morphs.disableMorphCustomAttributeData()
+
+                            ilOrigTypeDef.With(name = ilTgtTyRef.Name, nestedTypes = mkILTypeDefs (List.map buildRelocatedGeneratedType ch))
+                        | _ ->
+                            // If there is no matching IL type definition, then make a simple container class
+                            if debugStaticLinking then
+                                printfn
+                                    "Generating simple class '%s' because we didn't find an original type '%s' in a provider generated assembly"
+                                    ilTgtTyRef.QualifiedName
+                                    ilOrigTyRef.QualifiedName
+
+                            let access =
+                                (if isNested then
+                                     ILTypeDefAccess.Nested ILMemberAccess.Public
+                                 else
+                                     ILTypeDefAccess.Public)
+
+                            let tdefs = mkILTypeDefs (List.map buildRelocatedGeneratedType ch)
+
+                            mkILSimpleClass
+                                ilGlobals
+                                (ilTgtTyRef.Name,
+                                 access,
+                                 emptyILMethods,
+                                 emptyILFields,
+                                 tdefs,
+                                 emptyILProperties,
+                                 emptyILEvents,
+                                 emptyILCustomAttrs,
+                                 ILTypeInit.OnAny)
+
+                    [
+                        for ProviderGeneratedType (_, ilTgtTyRef, _) as node in tcImports.ProviderGeneratedTypeRoots do
+                            (ilTgtTyRef, buildRelocatedGeneratedType node)
+                    ]
+
+                // Implant all the generated type definitions into the ilxMainModule (generating a new ilxMainModule)
+                let ilxMainModule =
+
+                    let newTypeDefs =
+                        (ilxMainModule.TypeDefs, generatedILTypeDefs)
+                        ||> List.fold (fun acc (ilTgtTyRef, td) ->
+                            if debugStaticLinking then
+                                printfn "implanting '%s' at '%s'" td.Name ilTgtTyRef.QualifiedName
+
+                            implantTypeDef ilGlobals false acc ilTgtTyRef.Enclosing td)
+
+                    { ilxMainModule with
+                        TypeDefs = newTypeDefs
+                    }
+
+                // Remove any ILTypeDefs from the provider generated modules if they have been relocated because of a [] declaration.
+                let providerGeneratedILModules =
+                    providerGeneratedILModules
+                    |> List.map (fun (ccu, ilOrigScopeRef, ilModule) ->
+                        let ilTypeDefsAfterRemovingRelocatedTypes =
+                            let rec rw enc (tdefs: ILTypeDefs) =
+                                mkILTypeDefs
+                                    [
+                                        for tdef in tdefs do
+                                            let ilOrigTyRef = mkILNestedTyRef (ilOrigScopeRef, enc, tdef.Name)
+
+                                            if not (ilOrigTyRefsForProviderGeneratedTypesToRelocate.ContainsKey ilOrigTyRef) then
+                                                if debugStaticLinking then
+                                                    printfn
+                                                        "Keep provided type %s in place because it wasn't relocated"
+                                                        ilOrigTyRef.QualifiedName
+
+                                                tdef.With(nestedTypes = rw (enc @ [ tdef.Name ]) tdef.NestedTypes)
+                                    ]
+
+                            rw [] ilModule.TypeDefs
+
+                        (ccu,
+                         { ilModule with
+                             TypeDefs = ilTypeDefsAfterRemovingRelocatedTypes
+                         }))
+
+                providerGeneratedILModules, ilxMainModule
+
+            Morphs.disableMorphCustomAttributeData ()
 #else
             let providerGeneratedILModules = []
 #endif
 
             // Glue all this stuff into ilxMainModule
             let ilxMainModule, rewriteExternalRefsToLocalRefs =
-                  StaticLinkILModules (tcConfig, ilGlobals, tcImports, ilxMainModule, dependentILModules @ providerGeneratedILModules)
+                StaticLinkILModules(tcConfig, ilGlobals, tcImports, ilxMainModule, dependentILModules @ providerGeneratedILModules)
 
             // Rewrite type and assembly references
             let ilxMainModule =
-                  let isMscorlib = ilGlobals.primaryAssemblyName = PrimaryAssembly.Mscorlib.Name
-                  let validateTargetPlatform (scopeRef : ILScopeRef) =
-                      let name = getNameOfScopeRef scopeRef
-                      if (not isMscorlib && name = PrimaryAssembly.Mscorlib.Name) then
-                          error (Error(FSComp.SR.fscStaticLinkingNoProfileMismatches(), rangeCmdArgs))
-                      scopeRef
-                  let rewriteAssemblyRefsToMatchLibraries = NormalizeAssemblyRefs (ctok, ilGlobals, tcImports)
-                  Morphs.morphILTypeRefsInILModuleMemoized (Morphs.morphILScopeRefsInILTypeRef (validateTargetPlatform >> rewriteExternalRefsToLocalRefs >> rewriteAssemblyRefsToMatchLibraries)) ilxMainModule
+                let isMscorlib = ilGlobals.primaryAssemblyName = PrimaryAssembly.Mscorlib.Name
+
+                let validateTargetPlatform (scopeRef: ILScopeRef) =
+                    let name = getNameOfScopeRef scopeRef
+
+                    if (not isMscorlib && name = PrimaryAssembly.Mscorlib.Name) then
+                        error (Error(FSComp.SR.fscStaticLinkingNoProfileMismatches (), rangeCmdArgs))
+
+                    scopeRef
+
+                let rewriteAssemblyRefsToMatchLibraries =
+                    NormalizeAssemblyRefs(ctok, ilGlobals, tcImports)
+
+                Morphs.morphILTypeRefsInILModuleMemoized
+                    (Morphs.morphILScopeRefsInILTypeRef (
+                        validateTargetPlatform
+                        >> rewriteExternalRefsToLocalRefs
+                        >> rewriteAssemblyRefsToMatchLibraries
+                    ))
+                    ilxMainModule
 
             ilxMainModule)
diff --git a/src/Compiler/Driver/XmlDocFileWriter.fs b/src/Compiler/Driver/XmlDocFileWriter.fs
index 813dad3163c..97125744ace 100644
--- a/src/Compiler/Driver/XmlDocFileWriter.fs
+++ b/src/Compiler/Driver/XmlDocFileWriter.fs
@@ -25,31 +25,31 @@ module XmlDocWriter =
 
         let doTyconSig ptext (tc: Tycon) =
             if hasDoc tc.XmlDoc then
-                tc.XmlDocSig <- XmlDocSigOfTycon [ptext; tc.CompiledName]
+                tc.XmlDocSig <- XmlDocSigOfTycon [ ptext; tc.CompiledName ]
 
             for vref in tc.MembersOfFSharpTyconSorted do
                 doValSig ptext vref.Deref
 
             for uc in tc.UnionCasesArray do
                 if hasDoc uc.XmlDoc then
-                    uc.XmlDocSig <- XmlDocSigOfUnionCase [ptext; tc.CompiledName; uc.Id.idText]
+                    uc.XmlDocSig <- XmlDocSigOfUnionCase [ ptext; tc.CompiledName; uc.Id.idText ]
 
                 for field in uc.RecdFieldsArray do
                     if hasDoc field.XmlDoc then
                         // union case fields are exposed as properties
-                        field.XmlDocSig <- XmlDocSigOfProperty [ptext; tc.CompiledName; uc.Id.idText; field.Id.idText]
+                        field.XmlDocSig <- XmlDocSigOfProperty [ ptext; tc.CompiledName; uc.Id.idText; field.Id.idText ]
 
             for rf in tc.AllFieldsArray do
                 if hasDoc rf.XmlDoc then
                     rf.XmlDocSig <-
                         if tc.IsRecordTycon && not rf.IsStatic then
                             // represents a record field, which is exposed as a property
-                            XmlDocSigOfProperty [ptext; tc.CompiledName; rf.Id.idText]
+                            XmlDocSigOfProperty [ ptext; tc.CompiledName; rf.Id.idText ]
                         else
-                            XmlDocSigOfField [ptext; tc.CompiledName; rf.Id.idText]
+                            XmlDocSigOfField [ ptext; tc.CompiledName; rf.Id.idText ]
 
         let doModuleMemberSig path (m: ModuleOrNamespace) =
-            m.XmlDocSig <- XmlDocSigOfSubModul [path]
+            m.XmlDocSig <- XmlDocSigOfSubModul [ path ]
 
         let rec doModuleSig path (mspec: ModuleOrNamespace) =
             let mtype = mspec.ModuleOrNamespaceType
@@ -59,17 +59,16 @@ module XmlDocWriter =
                 match path with
                 | None -> Some ""
                 | Some "" -> Some mspec.LogicalName
-                | Some p -> Some (p+"."+mspec.LogicalName)
+                | Some p -> Some(p + "." + mspec.LogicalName)
 
             let ptext = defaultArg path ""
 
-            if mspec.IsModule then
-                doModuleMemberSig ptext mspec
+            if mspec.IsModule then doModuleMemberSig ptext mspec
 
             let vals =
                 mtype.AllValsAndMembers
                 |> Seq.toList
-                |> List.filter (fun x  -> not x.IsCompilerGenerated)
+                |> List.filter (fun x -> not x.IsCompilerGenerated)
                 |> List.filter (fun x -> x.MemberInfo.IsNone || x.IsExtensionMember)
 
             mtype.ModuleAndNamespaceDefinitions |> List.iter (doModuleSig path)
@@ -80,8 +79,8 @@ module XmlDocWriter =
         doModuleSig None generatedCcu.Contents
 
     let WriteXmlDocFile (g, assemblyName, generatedCcu: CcuThunk, xmlFile) =
-        if not (FileSystemUtils.checkSuffix xmlFile "xml" ) then
-            error(Error(FSComp.SR.docfileNoXmlSuffix(), Range.rangeStartup))
+        if not (FileSystemUtils.checkSuffix xmlFile "xml") then
+            error (Error(FSComp.SR.docfileNoXmlSuffix (), Range.rangeStartup))
 
         let mutable members = []
 
@@ -90,18 +89,17 @@ module XmlDocWriter =
                 let doc = xmlDoc.GetXmlText()
                 members <- (id, doc) :: members
 
-        let doVal (v: Val) =
-            addMember v.XmlDocSig v.XmlDoc
+        let doVal (v: Val) = addMember v.XmlDocSig v.XmlDoc
 
-        let doField (rf: RecdField) =
-            addMember rf.XmlDocSig rf.XmlDoc
+        let doField (rf: RecdField) = addMember rf.XmlDocSig rf.XmlDoc
 
         let doUnionCase (uc: UnionCase) =
             addMember uc.XmlDocSig uc.XmlDoc
+
             for field in uc.RecdFieldsArray do
                 addMember field.XmlDocSig field.XmlDoc
 
-        let doTycon (tc: Tycon) = 
+        let doTycon (tc: Tycon) =
             addMember tc.XmlDocSig tc.XmlDoc
 
             for vref in tc.MembersOfFSharpTyconSorted do
@@ -114,18 +112,16 @@ module XmlDocWriter =
             for rf in tc.AllFieldsArray do
                 doField rf
 
-        let modulMember (m: ModuleOrNamespace) =
-            addMember m.XmlDocSig m.XmlDoc
+        let modulMember (m: ModuleOrNamespace) = addMember m.XmlDocSig m.XmlDoc
 
         let rec doModule (mspec: ModuleOrNamespace) =
             let mtype = mspec.ModuleOrNamespaceType
-            if mspec.IsModule then
-                modulMember mspec
+            if mspec.IsModule then modulMember mspec
 
             let vals =
                 mtype.AllValsAndMembers
                 |> Seq.toList
-                |> List.filter (fun x  -> not x.IsCompilerGenerated)
+                |> List.filter (fun x -> not x.IsCompilerGenerated)
                 |> List.filter (fun x -> x.MemberInfo.IsNone || x.IsExtensionMember)
 
             List.iter doModule mtype.ModuleAndNamespaceDefinitions
@@ -143,9 +139,9 @@ module XmlDocWriter =
         fprintfn os ""
 
         for (nm, doc) in members do
-            fprintfn os  "" nm
-            fprintfn os  "%s" doc
-            fprintfn os  ""
+            fprintfn os "" nm
+            fprintfn os "%s" doc
+            fprintfn os ""
 
         fprintfn os ""
         fprintfn os ""
diff --git a/src/Compiler/Driver/fsc.fs b/src/Compiler/Driver/fsc.fs
index 7b484a41fee..c6ae4d83b67 100644
--- a/src/Compiler/Driver/fsc.fs
+++ b/src/Compiler/Driver/fsc.fs
@@ -76,41 +76,50 @@ type DiagnosticsLoggerUpToMaxErrors(tcConfigB: TcConfigBuilder, exiter: Exiter,
     override _.ErrorCount = errors
 
     override x.DiagnosticSink(diagnostic, severity) =
-      if ReportDiagnosticAsError tcConfigB.diagnosticsOptions (diagnostic, severity) then
-        if errors >= tcConfigB.maxErrors then
-            x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors())
-            exiter.Exit 1
-
-        x.HandleIssue(tcConfigB, diagnostic, FSharpDiagnosticSeverity.Error)
+        if ReportDiagnosticAsError tcConfigB.diagnosticsOptions (diagnostic, severity) then
+            if errors >= tcConfigB.maxErrors then
+                x.HandleTooManyErrors(FSComp.SR.fscTooManyErrors ())
+                exiter.Exit 1
 
-        errors <- errors + 1
+            x.HandleIssue(tcConfigB, diagnostic, FSharpDiagnosticSeverity.Error)
 
-        match diagnostic.Exception, tcConfigB.simulateException with
-        | InternalError (msg, _), None
-        | Failure msg, None -> Debug.Assert(false, sprintf "Bug in compiler: %s\n%s" msg (diagnostic.Exception.ToString()))
-        | :? KeyNotFoundException, None -> Debug.Assert(false, sprintf "Lookup exception in compiler: %s" (diagnostic.Exception.ToString()))
-        | _ ->  ()
+            errors <- errors + 1
 
-      elif ReportDiagnosticAsWarning tcConfigB.diagnosticsOptions (diagnostic, severity) then
-          x.HandleIssue(tcConfigB, diagnostic, FSharpDiagnosticSeverity.Warning)
+            match diagnostic.Exception, tcConfigB.simulateException with
+            | InternalError (msg, _), None
+            | Failure msg, None -> Debug.Assert(false, sprintf "Bug in compiler: %s\n%s" msg (diagnostic.Exception.ToString()))
+            | :? KeyNotFoundException, None ->
+                Debug.Assert(false, sprintf "Lookup exception in compiler: %s" (diagnostic.Exception.ToString()))
+            | _ -> ()
 
-      elif ReportDiagnosticAsInfo tcConfigB.diagnosticsOptions (diagnostic, severity) then
-          x.HandleIssue(tcConfigB, diagnostic, severity)
+        elif ReportDiagnosticAsWarning tcConfigB.diagnosticsOptions (diagnostic, severity) then
+            x.HandleIssue(tcConfigB, diagnostic, FSharpDiagnosticSeverity.Warning)
 
+        elif ReportDiagnosticAsInfo tcConfigB.diagnosticsOptions (diagnostic, severity) then
+            x.HandleIssue(tcConfigB, diagnostic, severity)
 
 /// Create an error logger that counts and prints errors
-let ConsoleDiagnosticsLoggerUpToMaxErrors (tcConfigB: TcConfigBuilder, exiter : Exiter) =
+let ConsoleDiagnosticsLoggerUpToMaxErrors (tcConfigB: TcConfigBuilder, exiter: Exiter) =
     { new DiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter, "ConsoleDiagnosticsLoggerUpToMaxErrors") with
 
-            member _.HandleTooManyErrors(text : string) =
-                DoWithDiagnosticColor FSharpDiagnosticSeverity.Warning (fun () -> Printf.eprintfn "%s" text)
+        member _.HandleTooManyErrors(text: string) =
+            DoWithDiagnosticColor FSharpDiagnosticSeverity.Warning (fun () -> Printf.eprintfn "%s" text)
+
+        member _.HandleIssue(tcConfigB, err, severity) =
+            DoWithDiagnosticColor severity (fun () ->
+                let diagnostic =
+                    OutputDiagnostic(
+                        tcConfigB.implicitIncludeDir,
+                        tcConfigB.showFullPaths,
+                        tcConfigB.flatErrors,
+                        tcConfigB.diagnosticStyle,
+                        severity
+                    )
 
-            member _.HandleIssue(tcConfigB, err, severity) =
-                DoWithDiagnosticColor severity (fun () ->
-                    let diagnostic = OutputDiagnostic (tcConfigB.implicitIncludeDir, tcConfigB.showFullPaths, tcConfigB.flatErrors, tcConfigB.diagnosticStyle, severity)
-                    writeViaBuffer stderr diagnostic err
-                    stderr.WriteLine())
-    } :> DiagnosticsLogger
+                writeViaBuffer stderr diagnostic err
+                stderr.WriteLine())
+    }
+    :> DiagnosticsLogger
 
 /// This error logger delays the messages it receives. At the end, call ForwardDelayedDiagnostics
 /// to send the held messages.
@@ -118,13 +127,15 @@ type DelayAndForwardDiagnosticsLogger(exiter: Exiter, diagnosticsLoggerProvider:
     inherit CapturingDiagnosticsLogger("DelayAndForwardDiagnosticsLogger")
 
     member x.ForwardDelayedDiagnostics(tcConfigB: TcConfigBuilder) =
-        let diagnosticsLogger =  diagnosticsLoggerProvider.CreateDiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter)
+        let diagnosticsLogger =
+            diagnosticsLoggerProvider.CreateDiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter)
+
         x.CommitDelayedDiagnostics diagnosticsLogger
 
-and []
-    DiagnosticsLoggerProvider() =
+and [] DiagnosticsLoggerProvider() =
 
-    member this.CreateDelayAndForwardLogger exiter = DelayAndForwardDiagnosticsLogger(exiter, this)
+    member this.CreateDelayAndForwardLogger exiter =
+        DelayAndForwardDiagnosticsLogger(exiter, this)
 
     abstract CreateDiagnosticsLoggerUpToMaxErrors: tcConfigBuilder: TcConfigBuilder * exiter: Exiter -> DiagnosticsLogger
 
@@ -137,16 +148,42 @@ type ConsoleLoggerProvider() =
         ConsoleDiagnosticsLoggerUpToMaxErrors(tcConfigBuilder, exiter)
 
 /// Notify the exiter if any error has occurred
-let AbortOnError (diagnosticsLogger: DiagnosticsLogger, exiter : Exiter) =
-    if diagnosticsLogger.ErrorCount > 0 then
-        exiter.Exit 1
-
-let TypeCheck (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: DiagnosticsLogger, assemblyName, niceNameGen, tcEnv0, openDecls0, inputs, exiter: Exiter) =
+let AbortOnError (diagnosticsLogger: DiagnosticsLogger, exiter: Exiter) =
+    if diagnosticsLogger.ErrorCount > 0 then exiter.Exit 1
+
+let TypeCheck
+    (
+        ctok,
+        tcConfig,
+        tcImports,
+        tcGlobals,
+        diagnosticsLogger: DiagnosticsLogger,
+        assemblyName,
+        niceNameGen,
+        tcEnv0,
+        openDecls0,
+        inputs,
+        exiter: Exiter
+    ) =
     try
-        if isNil inputs then error(Error(FSComp.SR.fscNoImplementationFiles(), rangeStartup))
+        if isNil inputs then
+            error (Error(FSComp.SR.fscNoImplementationFiles (), rangeStartup))
+
         let ccuName = assemblyName
-        let tcInitialState = GetInitialTcState (rangeStartup, ccuName, tcConfig, tcGlobals, tcImports, niceNameGen, tcEnv0, openDecls0)
-        CheckClosedInputSet (ctok, (fun () -> diagnosticsLogger.ErrorCount > 0), tcConfig, tcImports, tcGlobals, None, tcInitialState, inputs)
+
+        let tcInitialState =
+            GetInitialTcState(rangeStartup, ccuName, tcConfig, tcGlobals, tcImports, niceNameGen, tcEnv0, openDecls0)
+
+        CheckClosedInputSet(
+            ctok,
+            (fun () -> diagnosticsLogger.ErrorCount > 0),
+            tcConfig,
+            tcImports,
+            tcGlobals,
+            None,
+            tcInitialState,
+            inputs
+        )
     with exn ->
         errorRecovery exn rangeStartup
         exiter.Exit 1
@@ -162,34 +199,38 @@ let TypeCheck (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: Diagnost
 /// copied to the output folder, for example (except perhaps FSharp.Core.dll).
 ///
 /// NOTE: there is similar code in IncrementalBuilder.fs and this code should really be reconciled with that
-let AdjustForScriptCompile(tcConfigB: TcConfigBuilder, commandLineSourceFiles, lexResourceManager, dependencyProvider) =
+let AdjustForScriptCompile (tcConfigB: TcConfigBuilder, commandLineSourceFiles, lexResourceManager, dependencyProvider) =
 
     let combineFilePath file =
         try
-            if FileSystem.IsPathRootedShim file then file
-            else Path.Combine(tcConfigB.implicitIncludeDir, file)
+            if FileSystem.IsPathRootedShim file then
+                file
+            else
+                Path.Combine(tcConfigB.implicitIncludeDir, file)
         with _ ->
             error (Error(FSComp.SR.pathIsInvalid file, rangeStartup))
 
-    let commandLineSourceFiles =
-        commandLineSourceFiles
-        |> List.map combineFilePath
+    let commandLineSourceFiles = commandLineSourceFiles |> List.map combineFilePath
 
     // Script compilation is active if the last item being compiled is a script and --noframework has not been specified
     let mutable allSources = []
 
-    let tcConfig = TcConfig.Create(tcConfigB, validate=false)
+    let tcConfig = TcConfig.Create(tcConfigB, validate = false)
 
     let AddIfNotPresent (fileName: string) =
-        if not(allSources |> List.contains fileName) then
+        if not (allSources |> List.contains fileName) then
             allSources <- fileName :: allSources
 
     let AppendClosureInformation fileName =
         if IsScript fileName then
             let closure =
-                LoadClosure.ComputeClosureOfScriptFiles
-                   (tcConfig, [fileName, rangeStartup], CodeContext.Compilation,
-                    lexResourceManager, dependencyProvider)
+                LoadClosure.ComputeClosureOfScriptFiles(
+                    tcConfig,
+                    [ fileName, rangeStartup ],
+                    CodeContext.Compilation,
+                    lexResourceManager,
+                    dependencyProvider
+                )
 
             // Record the new references (non-framework) references from the analysis of the script. (The full resolutions are recorded
             // as the corresponding #I paths used to resolve them are local to the scripts and not added to the tcConfigB - they are
@@ -197,24 +238,39 @@ let AdjustForScriptCompile(tcConfigB: TcConfigBuilder, commandLineSourceFiles, l
             let references =
                 closure.References
                 |> List.collect snd
-                |> List.filter (fun r -> not (equals r.originalReference.Range range0) && not (equals r.originalReference.Range rangeStartup))
+                |> List.filter (fun r ->
+                    not (equals r.originalReference.Range range0)
+                    && not (equals r.originalReference.Range rangeStartup))
 
-            references |> List.iter (fun r -> tcConfigB.AddReferencedAssemblyByPath(r.originalReference.Range, r.resolvedPath))
+            references
+            |> List.iter (fun r -> tcConfigB.AddReferencedAssemblyByPath(r.originalReference.Range, r.resolvedPath))
 
             // Also record the other declarations from the script.
-            closure.NoWarns |> List.collect (fun (n, ms) -> ms|>List.map(fun m->m, n)) |> List.iter (fun (x,m) -> tcConfigB.TurnWarningOff(x, m))
+            closure.NoWarns
+            |> List.collect (fun (n, ms) -> ms |> List.map (fun m -> m, n))
+            |> List.iter (fun (x, m) -> tcConfigB.TurnWarningOff(x, m))
+
             closure.SourceFiles |> List.map fst |> List.iter AddIfNotPresent
             closure.AllRootFileDiagnostics |> List.iter diagnosticSink
 
             // If there is a target framework for the script then push that as a requirement into the overall compilation and add all the framework references implied
             // by the script too.
-            tcConfigB.SetPrimaryAssembly (if closure.UseDesktopFramework then PrimaryAssembly.Mscorlib else PrimaryAssembly.System_Runtime)
+            let primaryAssembly =
+                if closure.UseDesktopFramework then
+                    PrimaryAssembly.Mscorlib
+                else
+                    PrimaryAssembly.System_Runtime
+
+            tcConfigB.SetPrimaryAssembly primaryAssembly
 
             if tcConfigB.implicitlyReferenceDotNetAssemblies then
                 let references = closure.References |> List.collect snd
-                references |> List.iter (fun r -> tcConfigB.AddReferencedAssemblyByPath(r.originalReference.Range, r.resolvedPath))
 
-        else AddIfNotPresent fileName
+                for reference in references do
+                    tcConfigB.AddReferencedAssemblyByPath(reference.originalReference.Range, reference.resolvedPath)
+
+        else
+            AddIfNotPresent fileName
 
     // Find closure of .fsx files.
     commandLineSourceFiles |> List.iter AppendClosureInformation
@@ -225,33 +281,37 @@ let SetProcessThreadLocals tcConfigB =
     match tcConfigB.preferredUiLang with
     | Some s -> Thread.CurrentThread.CurrentUICulture <- CultureInfo(s)
     | None -> ()
+
     if tcConfigB.utf8output then
         Console.OutputEncoding <- Encoding.UTF8
 
 let ProcessCommandLineFlags (tcConfigB: TcConfigBuilder, lcidFromCodePage, argv) =
     let mutable inputFilesRef = []
+
     let collect name =
-        if List.exists (FileSystemUtils.checkSuffix name) [".resx"]  then
-            error(Error(FSComp.SR.fscResxSourceFileDeprecated name, rangeStartup))
+        if List.exists (FileSystemUtils.checkSuffix name) [ ".resx" ] then
+            error (Error(FSComp.SR.fscResxSourceFileDeprecated name, rangeStartup))
         else
             inputFilesRef <- name :: inputFilesRef
+
     let abbrevArgs = GetAbbrevFlagSet tcConfigB true
 
     // This is where flags are interpreted by the command line fsc.exe.
-    ParseCompilerOptions (collect, GetCoreFscCompilerOptions tcConfigB, List.tail (PostProcessCompilerArgs abbrevArgs argv))
+    ParseCompilerOptions(collect, GetCoreFscCompilerOptions tcConfigB, List.tail (PostProcessCompilerArgs abbrevArgs argv))
 
     if not (tcConfigB.portablePDB || tcConfigB.embeddedPDB) then
         if tcConfigB.embedAllSource || (tcConfigB.embedSourceList |> isNil |> not) then
-            error(Error(FSComp.SR.optsEmbeddedSourceRequirePortablePDBs(), rangeCmdArgs))
+            error (Error(FSComp.SR.optsEmbeddedSourceRequirePortablePDBs (), rangeCmdArgs))
+
         if not (String.IsNullOrEmpty(tcConfigB.sourceLink)) then
-            error(Error(FSComp.SR.optsSourceLinkRequirePortablePDBs(), rangeCmdArgs))
+            error (Error(FSComp.SR.optsSourceLinkRequirePortablePDBs (), rangeCmdArgs))
 
     if tcConfigB.debuginfo && not tcConfigB.portablePDB then
         if tcConfigB.deterministic then
-            error(Error(FSComp.SR.fscDeterministicDebugRequiresPortablePdb(), rangeCmdArgs))
+            error (Error(FSComp.SR.fscDeterministicDebugRequiresPortablePdb (), rangeCmdArgs))
 
         if tcConfigB.pathMap <> PathMap.empty then
-            error(Error(FSComp.SR.fscPathMapDebugRequiresPortablePdb(), rangeCmdArgs))
+            error (Error(FSComp.SR.fscPathMapDebugRequiresPortablePdb (), rangeCmdArgs))
 
     let inputFiles = List.rev inputFilesRef
 
@@ -263,12 +323,18 @@ let ProcessCommandLineFlags (tcConfigB: TcConfigBuilder, lcidFromCodePage, argv)
     SetProcessThreadLocals tcConfigB
 
     (* step - get dll references *)
-    let dllFiles, sourceFiles = inputFiles |> List.map(fun p -> FileSystemUtils.trimQuotes p) |> List.partition FileSystemUtils.isDll
+    let dllFiles, sourceFiles =
+        inputFiles
+        |> List.map (fun p -> FileSystemUtils.trimQuotes p)
+        |> List.partition FileSystemUtils.isDll
+
     match dllFiles with
     | [] -> ()
     | h :: _ -> errorR (Error(FSComp.SR.fscReferenceOnCommandLine h, rangeStartup))
 
-    dllFiles |> List.iter (fun f->tcConfigB.AddReferencedAssemblyByPath(rangeStartup, f))
+    dllFiles
+    |> List.iter (fun f -> tcConfigB.AddReferencedAssemblyByPath(rangeStartup, f))
+
     sourceFiles
 
 /// Write a .fsi file for the --sig option
@@ -278,14 +344,26 @@ module InterfaceFileWriter =
         // * write one unified sig file to a given path, or
         // * write individual sig files to paths matching their impl files
         let denv = DisplayEnv.InitialForSigFileGeneration tcGlobals
-        let denv = { denv with shrinkOverloads = false; printVerboseSignatures = true }
 
-        let writeToFile os (CheckedImplFile (contents=mexpr)) =
-          writeViaBuffer os (fun os s -> Printf.bprintf os "%s\n\n" s)
-            (NicePrint.layoutImpliedSignatureOfModuleOrNamespace true denv infoReader AccessibleFromSomewhere range0 mexpr |> Display.squashTo 80 |> LayoutRender.showL)
+        let denv =
+            { denv with
+                shrinkOverloads = false
+                printVerboseSignatures = true
+            }
+
+        let writeToFile os (CheckedImplFile (contents = mexpr)) =
+            writeViaBuffer
+                os
+                (fun os s -> Printf.bprintf os "%s\n\n" s)
+                (NicePrint.layoutImpliedSignatureOfModuleOrNamespace true denv infoReader AccessibleFromSomewhere range0 mexpr
+                 |> Display.squashTo 80
+                 |> LayoutRender.showL)
 
         let writeHeader filePath os =
-            if filePath <> "" && not (List.exists (FileSystemUtils.checkSuffix filePath) FSharpIndentationAwareSyntaxFileSuffixes) then
+            if
+                filePath <> ""
+                && not (List.exists (FileSystemUtils.checkSuffix filePath) FSharpIndentationAwareSyntaxFileSuffixes)
+            then
                 fprintfn os "#light"
                 fprintfn os ""
 
@@ -295,7 +373,9 @@ module InterfaceFileWriter =
                 if tcConfig.printSignatureFile = "" then
                     Console.Out
                 else
-                    FileSystem.OpenFileForWriteShim(tcConfig.printSignatureFile, FileMode.Create).GetWriter()
+                    FileSystem
+                        .OpenFileForWriteShim(tcConfig.printSignatureFile, FileMode.Create)
+                        .GetWriter()
 
             writeHeader tcConfig.printSignatureFile os
 
@@ -305,14 +385,16 @@ module InterfaceFileWriter =
             if tcConfig.printSignatureFile <> "" then os.Dispose()
 
         let extensionForFile (filePath: string) =
-            if (List.exists (FileSystemUtils.checkSuffix filePath) mlCompatSuffixes) then
+            if (List.exists (FileSystemUtils.checkSuffix filePath) FSharpMLCompatFileSuffixes) then
                 ".mli"
             else
                 ".fsi"
 
         let writeToSeparateFiles (declaredImpls: CheckedImplFile list) =
-            for CheckedImplFile (qualifiedNameOfFile=name) as impl in declaredImpls do
-                let fileName = Path.ChangeExtension(name.Range.FileName, extensionForFile name.Range.FileName)
+            for CheckedImplFile (qualifiedNameOfFile = name) as impl in declaredImpls do
+                let fileName =
+                    Path.ChangeExtension(name.Range.FileName, extensionForFile name.Range.FileName)
+
                 printfn "writing impl file to %s" fileName
                 use os = FileSystem.OpenFileForWriteShim(fileName, FileMode.Create).GetWriter()
                 writeHeader fileName os
@@ -331,36 +413,49 @@ module InterfaceFileWriter =
 // 1) Look into the referenced assemblies, if FSharp.Core.dll is specified, it will copy it to output directory.
 // 2) If not, but FSharp.Core.dll exists beside the compiler binaries, it will copy it to output directory.
 // 3) If not, it will produce an error.
-let CopyFSharpCore(outFile: string, referencedDlls: AssemblyReference list) =
+let CopyFSharpCore (outFile: string, referencedDlls: AssemblyReference list) =
     let outDir = Path.GetDirectoryName outFile
     let fsharpCoreAssemblyName = GetFSharpCoreLibraryName() + ".dll"
     let fsharpCoreDestinationPath = Path.Combine(outDir, fsharpCoreAssemblyName)
+
     let copyFileIfDifferent src dest =
-        if not (FileSystem.FileExistsShim dest) || (FileSystem.GetCreationTimeShim src <> FileSystem.GetCreationTimeShim dest) then
+        if
+            not (FileSystem.FileExistsShim dest)
+            || (FileSystem.GetCreationTimeShim src <> FileSystem.GetCreationTimeShim dest)
+        then
             FileSystem.CopyShim(src, dest, true)
 
-    match referencedDlls |> Seq.tryFind (fun dll -> String.Equals(Path.GetFileName(dll.Text), fsharpCoreAssemblyName, StringComparison.CurrentCultureIgnoreCase)) with
+    let fsharpCoreReferences =
+        referencedDlls
+        |> Seq.tryFind (fun dll ->
+            String.Equals(Path.GetFileName(dll.Text), fsharpCoreAssemblyName, StringComparison.CurrentCultureIgnoreCase))
+
+    match fsharpCoreReferences with
     | Some referencedFsharpCoreDll -> copyFileIfDifferent referencedFsharpCoreDll.Text fsharpCoreDestinationPath
     | None ->
-        let executionLocation =
-            Assembly.GetExecutingAssembly().Location
+        let executionLocation = Assembly.GetExecutingAssembly().Location
         let compilerLocation = Path.GetDirectoryName executionLocation
-        let compilerFsharpCoreDllPath = Path.Combine(compilerLocation, fsharpCoreAssemblyName)
+
+        let compilerFsharpCoreDllPath =
+            Path.Combine(compilerLocation, fsharpCoreAssemblyName)
+
         if FileSystem.FileExistsShim compilerFsharpCoreDllPath then
             copyFileIfDifferent compilerFsharpCoreDllPath fsharpCoreDestinationPath
         else
-            errorR(Error(FSComp.SR.fsharpCoreNotFoundToBeCopied(), rangeCmdArgs))
+            errorR (Error(FSComp.SR.fsharpCoreNotFoundToBeCopied (), rangeCmdArgs))
 
 // Try to find an AssemblyVersion attribute
 let TryFindVersionAttribute g attrib attribName attribs deterministic =
     match AttributeHelpers.TryFindStringAttribute g attrib attribs with
     | Some versionString ->
-         if deterministic && versionString.Contains("*") then
-             errorR(Error(FSComp.SR.fscAssemblyWildcardAndDeterminism(attribName, versionString), rangeStartup))
-         try Some (parseILVersion versionString)
-         with e ->
-             // Warning will be reported by CheckExpressions.fs
-             None
+        if deterministic && versionString.Contains("*") then
+            errorR (Error(FSComp.SR.fscAssemblyWildcardAndDeterminism (attribName, versionString), rangeStartup))
+
+        try
+            Some(parseILVersion versionString)
+        with e ->
+            // Warning will be reported by CheckExpressions.fs
+            None
     | _ -> None
 
 //----------------------------------------------------------------------------
@@ -370,7 +465,7 @@ let TryFindVersionAttribute g attrib attribName attribs deterministic =
 //-----------------------------------------------------------------------------
 
 []
-type Args<'T> = Args  of 'T
+type Args<'T> = Args of 'T
 
 /// First phase of compilation.
 ///   - Set up console encoding and code page settings
@@ -379,17 +474,28 @@ type Args<'T> = Args  of 'T
 ///   - Import assemblies
 ///   - Parse source files
 ///   - Check the inputs
-let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
-          reduceMemoryUsage: ReduceMemoryFlag, defaultCopyFSharpCore: CopyFSharpCoreFlag,
-          exiter: Exiter, diagnosticsLoggerProvider: DiagnosticsLoggerProvider, disposables: DisposablesTracker) =
+let main1
+    (
+        ctok,
+        argv,
+        legacyReferenceResolver,
+        bannerAlreadyPrinted,
+        reduceMemoryUsage: ReduceMemoryFlag,
+        defaultCopyFSharpCore: CopyFSharpCoreFlag,
+        exiter: Exiter,
+        diagnosticsLoggerProvider: DiagnosticsLoggerProvider,
+        disposables: DisposablesTracker
+    ) =
 
     // See Bug 735819
     let lcidFromCodePage =
-        if (Console.OutputEncoding.CodePage <> 65001) &&
-           (Console.OutputEncoding.CodePage <> Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage) &&
-           (Console.OutputEncoding.CodePage <> Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage) then
-                Thread.CurrentThread.CurrentUICulture <- CultureInfo("en-US")
-                Some 1033
+        if (Console.OutputEncoding.CodePage <> 65001)
+           && (Console.OutputEncoding.CodePage
+               <> Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage)
+           && (Console.OutputEncoding.CodePage
+               <> Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage) then
+            Thread.CurrentThread.CurrentUICulture <- CultureInfo("en-US")
+            Some 1033
         else
             None
 
@@ -397,29 +503,34 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
 
     let tryGetMetadataSnapshot = (fun _ -> None)
 
-    let defaultFSharpBinariesDir = FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(None).Value
+    let defaultFSharpBinariesDir =
+        FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(None).Value
 
     let tcConfigB =
-       TcConfigBuilder.CreateNew(legacyReferenceResolver,
+        TcConfigBuilder.CreateNew(
+            legacyReferenceResolver,
             defaultFSharpBinariesDir,
-            reduceMemoryUsage=reduceMemoryUsage,
-            implicitIncludeDir=directoryBuildingFrom,
-            isInteractive=false,
-            isInvalidationSupported=false,
-            defaultCopyFSharpCore=defaultCopyFSharpCore,
-            tryGetMetadataSnapshot=tryGetMetadataSnapshot,
-            sdkDirOverride=None,
-            rangeForErrors=range0)
+            reduceMemoryUsage = reduceMemoryUsage,
+            implicitIncludeDir = directoryBuildingFrom,
+            isInteractive = false,
+            isInvalidationSupported = false,
+            defaultCopyFSharpCore = defaultCopyFSharpCore,
+            tryGetMetadataSnapshot = tryGetMetadataSnapshot,
+            sdkDirOverride = None,
+            rangeForErrors = range0
+        )
 
     // Preset: --optimize+ -g --tailcalls+ (see 4505)
     SetOptimizeSwitch tcConfigB OptionSwitch.On
-    SetDebugSwitch    tcConfigB None OptionSwitch.Off
+    SetDebugSwitch tcConfigB None OptionSwitch.Off
     SetTailcallSwitch tcConfigB OptionSwitch.On
 
     // Now install a delayed logger to hold all errors from flags until after all flags have been parsed (for example, --vserrors)
-    let delayForFlagsLogger =  diagnosticsLoggerProvider.CreateDelayAndForwardLogger exiter
+    let delayForFlagsLogger =
+        diagnosticsLoggerProvider.CreateDelayAndForwardLogger exiter
 
-    let _unwindEL_1 = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> delayForFlagsLogger)
+    let _unwindEL_1 =
+        PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> delayForFlagsLogger)
 
     // Share intern'd strings across all lexing/parsing
     let lexResourceManager = Lexhelp.LexResourceManager()
@@ -432,7 +543,7 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
         // The ParseCompilerOptions function calls imperative function to process "real" args
         // Rather than start processing, just collect names, then process them.
         try
-            let files = ProcessCommandLineFlags (tcConfigB, lcidFromCodePage, argv)
+            let files = ProcessCommandLineFlags(tcConfigB, lcidFromCodePage, argv)
             AdjustForScriptCompile(tcConfigB, files, lexResourceManager, dependencyProvider)
         with e ->
             errorRecovery e rangeStartup
@@ -442,8 +553,7 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
     tcConfigB.conditionalDefines <- "COMPILED" :: tcConfigB.conditionalDefines
 
     // Display the banner text, if necessary
-    if not bannerAlreadyPrinted then
-        DisplayBannerText tcConfigB
+    if not bannerAlreadyPrinted then DisplayBannerText tcConfigB
 
     // Create tcGlobals and frameworkTcImports
     let outfile, pdbfile, assemblyName =
@@ -462,16 +572,17 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
     // If there's a problem building TcConfig, abort
     let tcConfig =
         try
-            TcConfig.Create(tcConfigB, validate=false)
+            TcConfig.Create(tcConfigB, validate = false)
         with e ->
             errorRecovery e rangeStartup
             delayForFlagsLogger.ForwardDelayedDiagnostics tcConfigB
             exiter.Exit 1
 
-    let diagnosticsLogger =  diagnosticsLoggerProvider.CreateDiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter)
+    let diagnosticsLogger =
+        diagnosticsLoggerProvider.CreateDiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter)
 
     // Install the global error logger and never remove it. This logger does have all command-line flags considered.
-    let _unwindEL_2 = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> diagnosticsLogger)
+    let _unwindEL_2 = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
 
     // Forward all errors from flags
     delayForFlagsLogger.CommitDelayedDiagnostics diagnosticsLogger
@@ -483,14 +594,18 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
     ReportTime tcConfig "Import mscorlib and FSharp.Core.dll"
     let foundationalTcConfigP = TcConfigProvider.Constant tcConfig
 
-    let sysRes, otherRes, knownUnresolved = TcAssemblyResolutions.SplitNonFoundationalResolutions(tcConfig)
+    let sysRes, otherRes, knownUnresolved =
+        TcAssemblyResolutions.SplitNonFoundationalResolutions(tcConfig)
 
     // Import basic assemblies
-    let tcGlobals, frameworkTcImports = 
-        TcImports.BuildFrameworkTcImports (foundationalTcConfigP, sysRes, otherRes)
+    let tcGlobals, frameworkTcImports =
+        TcImports.BuildFrameworkTcImports(foundationalTcConfigP, sysRes, otherRes)
         |> NodeCode.RunImmediateWithoutCancellation
 
-    let ilSourceDocs = [ for sourceFile in sourceFiles -> tcGlobals.memoize_file (FileIndex.fileIndexOfFile sourceFile)]
+    let ilSourceDocs =
+        [
+            for sourceFile in sourceFiles -> tcGlobals.memoize_file (FileIndex.fileIndexOfFile sourceFile)
+        ]
 
     // Register framework tcImports to be disposed in future
     disposables.Register frameworkTcImports
@@ -499,12 +614,23 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
     ReportTime tcConfig "Parse inputs"
     use unwindParsePhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
 
-    let createDiagnosticsLogger = (fun exiter -> diagnosticsLoggerProvider.CreateDelayAndForwardLogger(exiter) :> CapturingDiagnosticsLogger)
+    let createDiagnosticsLogger =
+        (fun exiter -> diagnosticsLoggerProvider.CreateDelayAndForwardLogger(exiter) :> CapturingDiagnosticsLogger)
 
-    let inputs = ParseInputFiles(tcConfig, lexResourceManager, sourceFiles, diagnosticsLogger, exiter, createDiagnosticsLogger, (*retryLocked*)false)
+    let inputs =
+        ParseInputFiles(
+            tcConfig,
+            lexResourceManager,
+            sourceFiles,
+            diagnosticsLogger,
+            exiter,
+            createDiagnosticsLogger (*retryLocked*) ,
+            false
+        )
 
     let inputs, _ =
-        (Map.empty, inputs) ||> List.mapFold (fun state (input, x) ->
+        (Map.empty, inputs)
+        ||> List.mapFold (fun state (input, x) ->
             let inputT, stateT = DeduplicateParsedInputModuleName state input
             (inputT, x), stateT)
 
@@ -522,7 +648,8 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
 
     // Apply any nowarn flags
     let tcConfig =
-        (tcConfig, inputs) ||> List.fold (fun z (input, sourceFileDirectory) ->
+        (tcConfig, inputs)
+        ||> List.fold (fun z (input, sourceFileDirectory) ->
             ApplyMetaCommandsFromInputToTcConfig(z, input, sourceFileDirectory, dependencyProvider))
 
     let tcConfigP = TcConfigProvider.Constant tcConfig
@@ -547,50 +674,99 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
 
     use unwindParsePhase = PushThreadBuildPhaseUntilUnwind BuildPhase.TypeCheck
 
-    let tcEnv0, openDecls0 = GetInitialTcEnv (assemblyName, rangeStartup, tcConfig, tcImports, tcGlobals)
+    let tcEnv0, openDecls0 =
+        GetInitialTcEnv(assemblyName, rangeStartup, tcConfig, tcImports, tcGlobals)
 
     // Type check the inputs
     let inputs = inputs |> List.map fst
 
     let tcState, topAttrs, typedAssembly, _tcEnvAtEnd =
-        TypeCheck(ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, assemblyName, NiceNameGenerator(), tcEnv0, openDecls0, inputs, exiter)
+        TypeCheck(
+            ctok,
+            tcConfig,
+            tcImports,
+            tcGlobals,
+            diagnosticsLogger,
+            assemblyName,
+            NiceNameGenerator(),
+            tcEnv0,
+            openDecls0,
+            inputs,
+            exiter
+        )
 
     AbortOnError(diagnosticsLogger, exiter)
     ReportTime tcConfig "Typechecked"
 
-    Args (ctok, tcGlobals, tcImports, frameworkTcImports, tcState.Ccu, typedAssembly, topAttrs, tcConfig, outfile, pdbfile, assemblyName, diagnosticsLogger, exiter, ilSourceDocs)
+    Args(
+        ctok,
+        tcGlobals,
+        tcImports,
+        frameworkTcImports,
+        tcState.Ccu,
+        typedAssembly,
+        topAttrs,
+        tcConfig,
+        outfile,
+        pdbfile,
+        assemblyName,
+        diagnosticsLogger,
+        exiter,
+        ilSourceDocs
+    )
 
 /// Alternative first phase of compilation.  This is for the compile-from-AST feature of FCS.
 ///   - Import assemblies
 ///   - Check the inputs
 let main1OfAst
-       (ctok, legacyReferenceResolver, reduceMemoryUsage, assemblyName, target,
-        outfile, pdbFile, dllReferences,
-        noframework, exiter: Exiter,
+    (
+        ctok,
+        legacyReferenceResolver,
+        reduceMemoryUsage,
+        assemblyName,
+        target,
+        outfile,
+        pdbFile,
+        dllReferences,
+        noframework,
+        exiter: Exiter,
         diagnosticsLoggerProvider: DiagnosticsLoggerProvider,
         disposables: DisposablesTracker,
-        inputs: ParsedInput list) =
+        inputs: ParsedInput list
+    ) =
 
     let tryGetMetadataSnapshot = (fun _ -> None)
 
     let directoryBuildingFrom = Directory.GetCurrentDirectory()
 
-    let defaultFSharpBinariesDir = FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(None).Value
+    let defaultFSharpBinariesDir =
+        FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(None).Value
 
     let tcConfigB =
-        TcConfigBuilder.CreateNew(legacyReferenceResolver, defaultFSharpBinariesDir,
-            reduceMemoryUsage=reduceMemoryUsage, implicitIncludeDir=directoryBuildingFrom,
-            isInteractive=false, isInvalidationSupported=false,
-            defaultCopyFSharpCore=CopyFSharpCoreFlag.No,
-            tryGetMetadataSnapshot=tryGetMetadataSnapshot,
-            sdkDirOverride=None,
-            rangeForErrors=range0)
+        TcConfigBuilder.CreateNew(
+            legacyReferenceResolver,
+            defaultFSharpBinariesDir,
+            reduceMemoryUsage = reduceMemoryUsage,
+            implicitIncludeDir = directoryBuildingFrom,
+            isInteractive = false,
+            isInvalidationSupported = false,
+            defaultCopyFSharpCore = CopyFSharpCoreFlag.No,
+            tryGetMetadataSnapshot = tryGetMetadataSnapshot,
+            sdkDirOverride = None,
+            rangeForErrors = range0
+        )
 
     let primaryAssembly =
         // temporary workaround until https://github.com/dotnet/fsharp/pull/8043 is merged:
         // pick a primary assembly based on whether the developer included System>Runtime in the list of reference assemblies.
         // It's an ugly compromise used to avoid exposing primaryAssembly in the public api for this function.
-        let includesSystem_Runtime = dllReferences |> Seq.exists(fun f -> Path.GetFileName(f).Equals("system.runtime.dll",StringComparison.InvariantCultureIgnoreCase))
+        let includesSystem_Runtime =
+            dllReferences
+            |> Seq.exists (fun f ->
+                Path
+                    .GetFileName(f)
+                    .Equals("system.runtime.dll", StringComparison.InvariantCultureIgnoreCase))
+
         if includesSystem_Runtime then
             PrimaryAssembly.System_Runtime
         else
@@ -605,34 +781,44 @@ let main1OfAst
 
     // Preset: --optimize+ -g --tailcalls+ (see 4505)
     SetOptimizeSwitch tcConfigB OptionSwitch.On
-    SetDebugSwitch    tcConfigB None (
-        match pdbFile with
-        | Some _ -> OptionSwitch.On
-        | None -> OptionSwitch.Off)
+
+    SetDebugSwitch
+        tcConfigB
+        None
+        (match pdbFile with
+         | Some _ -> OptionSwitch.On
+         | None -> OptionSwitch.Off)
+
     SetTailcallSwitch tcConfigB OptionSwitch.On
 
     // Now install a delayed logger to hold all errors from flags until after all flags have been parsed (for example, --vserrors)
-    let delayForFlagsLogger =  diagnosticsLoggerProvider.CreateDelayAndForwardLogger exiter
-    let _unwindEL_1 = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> delayForFlagsLogger)
+    let delayForFlagsLogger =
+        diagnosticsLoggerProvider.CreateDelayAndForwardLogger exiter
+
+    let _unwindEL_1 =
+        PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> delayForFlagsLogger)
 
     tcConfigB.conditionalDefines <- "COMPILED" :: tcConfigB.conditionalDefines
 
     // append assembly dependencies
-    dllReferences |> List.iter (fun ref -> tcConfigB.AddReferencedAssemblyByPath(rangeStartup,ref))
+    dllReferences
+    |> List.iter (fun ref -> tcConfigB.AddReferencedAssemblyByPath(rangeStartup, ref))
 
     // If there's a problem building TcConfig, abort
     let tcConfig =
         try
-            TcConfig.Create(tcConfigB,validate=false)
+            TcConfig.Create(tcConfigB, validate = false)
         with e ->
             delayForFlagsLogger.ForwardDelayedDiagnostics tcConfigB
             exiter.Exit 1
 
     let dependencyProvider = new DependencyProvider()
-    let diagnosticsLogger =  diagnosticsLoggerProvider.CreateDiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter)
+
+    let diagnosticsLogger =
+        diagnosticsLoggerProvider.CreateDiagnosticsLoggerUpToMaxErrors(tcConfigB, exiter)
 
     // Install the global error logger and never remove it. This logger does have all command-line flags considered.
-    let _unwindEL_2 = PushDiagnosticsLoggerPhaseUntilUnwind (fun _ -> diagnosticsLogger)
+    let _unwindEL_2 = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
 
     // Forward all errors from flags
     delayForFlagsLogger.CommitDelayedDiagnostics diagnosticsLogger
@@ -640,11 +826,13 @@ let main1OfAst
     // Resolve assemblies
     ReportTime tcConfig "Import mscorlib and FSharp.Core.dll"
     let foundationalTcConfigP = TcConfigProvider.Constant tcConfig
-    let sysRes, otherRes, knownUnresolved = TcAssemblyResolutions.SplitNonFoundationalResolutions(tcConfig)
+
+    let sysRes, otherRes, knownUnresolved =
+        TcAssemblyResolutions.SplitNonFoundationalResolutions(tcConfig)
 
     // Import basic assemblies
-    let tcGlobals, frameworkTcImports = 
-        TcImports.BuildFrameworkTcImports (foundationalTcConfigP, sysRes, otherRes) 
+    let tcGlobals, frameworkTcImports =
+        TcImports.BuildFrameworkTcImports(foundationalTcConfigP, sysRes, otherRes)
         |> NodeCode.RunImmediateWithoutCancellation
 
     // Register framework tcImports to be disposed in future
@@ -653,14 +841,18 @@ let main1OfAst
     use unwindParsePhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parse
 
     let meta = Directory.GetCurrentDirectory()
-    let tcConfig = (tcConfig,inputs) ||> List.fold (fun tcc inp -> ApplyMetaCommandsFromInputToTcConfig (tcc, inp, meta, dependencyProvider))
+
+    let tcConfig =
+        (tcConfig, inputs)
+        ||> List.fold (fun tcc inp -> ApplyMetaCommandsFromInputToTcConfig(tcc, inp, meta, dependencyProvider))
+
     let tcConfigP = TcConfigProvider.Constant tcConfig
 
     // Import other assemblies
     ReportTime tcConfig "Import non-system references"
 
-    let tcImports = 
-        TcImports.BuildNonFrameworkTcImports(tcConfigP, frameworkTcImports, otherRes, knownUnresolved, dependencyProvider) 
+    let tcImports =
+        TcImports.BuildNonFrameworkTcImports(tcConfigP, frameworkTcImports, otherRes, knownUnresolved, dependencyProvider)
         |> NodeCode.RunImmediateWithoutCancellation
 
     // register tcImports to be disposed in future
@@ -669,76 +861,171 @@ let main1OfAst
     // Build the initial type checking environment
     ReportTime tcConfig "Typecheck"
     use unwindParsePhase = PushThreadBuildPhaseUntilUnwind BuildPhase.TypeCheck
-    let tcEnv0, openDecls0 = GetInitialTcEnv (assemblyName, rangeStartup, tcConfig, tcImports, tcGlobals)
+
+    let tcEnv0, openDecls0 =
+        GetInitialTcEnv(assemblyName, rangeStartup, tcConfig, tcImports, tcGlobals)
 
     // Type check the inputs
     let tcState, topAttrs, typedAssembly, _tcEnvAtEnd =
-        TypeCheck(ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, assemblyName, NiceNameGenerator(), tcEnv0, openDecls0, inputs, exiter)
+        TypeCheck(
+            ctok,
+            tcConfig,
+            tcImports,
+            tcGlobals,
+            diagnosticsLogger,
+            assemblyName,
+            NiceNameGenerator(),
+            tcEnv0,
+            openDecls0,
+            inputs,
+            exiter
+        )
 
     AbortOnError(diagnosticsLogger, exiter)
     ReportTime tcConfig "Typechecked"
 
-    Args (ctok, tcGlobals, tcImports, frameworkTcImports, tcState.Ccu, typedAssembly, topAttrs, tcConfig, outfile, pdbFile, assemblyName, diagnosticsLogger, exiter, [])
+    Args(
+        ctok,
+        tcGlobals,
+        tcImports,
+        frameworkTcImports,
+        tcState.Ccu,
+        typedAssembly,
+        topAttrs,
+        tcConfig,
+        outfile,
+        pdbFile,
+        assemblyName,
+        diagnosticsLogger,
+        exiter,
+        []
+    )
 
 /// Second phase of compilation.
 ///   - Write the signature file, check some attributes
-let main2(Args (ctok, tcGlobals, tcImports: TcImports, frameworkTcImports, generatedCcu: CcuThunk, typedImplFiles, topAttrs, tcConfig: TcConfig, outfile, pdbfile, assemblyName, diagnosticsLogger, exiter: Exiter, ilSourceDocs)) =
+let main2
+    (Args (ctok,
+           tcGlobals,
+           tcImports: TcImports,
+           frameworkTcImports,
+           generatedCcu: CcuThunk,
+           typedImplFiles,
+           topAttrs,
+           tcConfig: TcConfig,
+           outfile,
+           pdbfile,
+           assemblyName,
+           diagnosticsLogger,
+           exiter: Exiter,
+           ilSourceDocs))
+    =
 
     if tcConfig.typeCheckOnly then exiter.Exit 0
 
     generatedCcu.Contents.SetAttribs(generatedCcu.Contents.Attribs @ topAttrs.assemblyAttrs)
 
     use unwindPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.CodeGen
-    let signingInfo = ValidateKeySigningAttributes (tcConfig, tcGlobals, topAttrs)
+    let signingInfo = ValidateKeySigningAttributes(tcConfig, tcGlobals, topAttrs)
 
     AbortOnError(diagnosticsLogger, exiter)
 
     // Build an updated diagnosticsLogger that filters according to the scopedPragmas. Then install
     // it as the updated global error logger and never remove it
     let oldLogger = diagnosticsLogger
+
     let diagnosticsLogger =
-        let scopedPragmas = [ for CheckedImplFile (pragmas=pragmas) in typedImplFiles do yield! pragmas ]
+        let scopedPragmas =
+            [
+                for CheckedImplFile (pragmas = pragmas) in typedImplFiles do
+                    yield! pragmas
+            ]
+
         GetDiagnosticsLoggerFilteringByScopedPragmas(true, scopedPragmas, tcConfig.diagnosticsOptions, oldLogger)
 
     let _unwindEL_3 = PushDiagnosticsLoggerPhaseUntilUnwind(fun _ -> diagnosticsLogger)
 
     // Try to find an AssemblyVersion attribute
     let assemVerFromAttrib =
-        match TryFindVersionAttribute tcGlobals "System.Reflection.AssemblyVersionAttribute" "AssemblyVersionAttribute" topAttrs.assemblyAttrs tcConfig.deterministic with
+        match
+            TryFindVersionAttribute
+                tcGlobals
+                "System.Reflection.AssemblyVersionAttribute"
+                "AssemblyVersionAttribute"
+                topAttrs.assemblyAttrs
+                tcConfig.deterministic
+            with
         | Some v ->
-           match tcConfig.version with
-           | VersionNone -> Some v
-           | _ -> warning(Error(FSComp.SR.fscAssemblyVersionAttributeIgnored(), rangeStartup)); None
+            match tcConfig.version with
+            | VersionNone -> Some v
+            | _ ->
+                warning (Error(FSComp.SR.fscAssemblyVersionAttributeIgnored (), rangeStartup))
+                None
         | _ ->
             match tcConfig.version with
-            | VersionNone -> Some (ILVersionInfo (0us,0us,0us,0us))               //If no attribute was specified in source then version is 0.0.0.0
-            | _ -> Some (tcConfig.version.GetVersionInfo tcConfig.implicitIncludeDir)
+            | VersionNone -> Some(ILVersionInfo(0us, 0us, 0us, 0us)) //If no attribute was specified in source then version is 0.0.0.0
+            | _ -> Some(tcConfig.version.GetVersionInfo tcConfig.implicitIncludeDir)
 
     // write interface, xmldoc
     ReportTime tcConfig "Write Interface File"
     use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Output
-    if tcConfig.printSignature || tcConfig.printAllSignatureFiles then InterfaceFileWriter.WriteInterfaceFile (tcGlobals, tcConfig, InfoReader(tcGlobals, tcImports.GetImportMap()), typedImplFiles)
+
+    if tcConfig.printSignature || tcConfig.printAllSignatureFiles then
+        InterfaceFileWriter.WriteInterfaceFile(tcGlobals, tcConfig, InfoReader(tcGlobals, tcImports.GetImportMap()), typedImplFiles)
 
     ReportTime tcConfig "Write XML document signatures"
+
     if tcConfig.xmlDocOutputFile.IsSome then
-        XmlDocWriter.ComputeXmlDocSigs (tcGlobals, generatedCcu)
+        XmlDocWriter.ComputeXmlDocSigs(tcGlobals, generatedCcu)
 
     ReportTime tcConfig "Write XML docs"
-    tcConfig.xmlDocOutputFile |> Option.iter (fun xmlFile ->
+
+    tcConfig.xmlDocOutputFile
+    |> Option.iter (fun xmlFile ->
         let xmlFile = tcConfig.MakePathAbsolute xmlFile
-        XmlDocWriter.WriteXmlDocFile (tcGlobals, assemblyName, generatedCcu, xmlFile))
+        XmlDocWriter.WriteXmlDocFile(tcGlobals, assemblyName, generatedCcu, xmlFile))
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, frameworkTcImports, tcGlobals, diagnosticsLogger, generatedCcu, outfile, typedImplFiles, topAttrs, pdbfile, assemblyName, assemVerFromAttrib, signingInfo, exiter, ilSourceDocs)
-
+    Args(
+        ctok,
+        tcConfig,
+        tcImports,
+        frameworkTcImports,
+        tcGlobals,
+        diagnosticsLogger,
+        generatedCcu,
+        outfile,
+        typedImplFiles,
+        topAttrs,
+        pdbfile,
+        assemblyName,
+        assemVerFromAttrib,
+        signingInfo,
+        exiter,
+        ilSourceDocs
+    )
 
 /// Third phase of compilation.
 ///   - encode signature data
 ///   - optimize
 ///   - encode optimization data
-let main3(Args (ctok, tcConfig, tcImports, frameworkTcImports: TcImports, tcGlobals,
-                 diagnosticsLogger: DiagnosticsLogger, generatedCcu: CcuThunk, outfile, typedImplFiles,
-                 topAttrs, pdbfile, assemblyName, assemVerFromAttrib, signingInfo, exiter: Exiter, ilSourceDocs)) =
+let main3
+    (Args (ctok,
+           tcConfig,
+           tcImports,
+           frameworkTcImports: TcImports,
+           tcGlobals,
+           diagnosticsLogger: DiagnosticsLogger,
+           generatedCcu: CcuThunk,
+           outfile,
+           typedImplFiles,
+           topAttrs,
+           pdbfile,
+           assemblyName,
+           assemVerFromAttrib,
+           signingInfo,
+           exiter: Exiter,
+           ilSourceDocs))
+    =
 
     // Encode the signature data
     ReportTime tcConfig "Encode Interface Data"
@@ -756,21 +1043,29 @@ let main3(Args (ctok, tcConfig, tcImports, frameworkTcImports: TcImports, tcGlob
         | Some v -> v
         | _ ->
             match frameworkTcImports.DllTable.TryFind tcConfig.primaryAssembly.Name with
-             | Some ib -> ib.RawMetadata.TryGetILModuleDef().Value.MetadataVersion
-             | _ -> ""
+            | Some ib -> ib.RawMetadata.TryGetILModuleDef().Value.MetadataVersion
+            | _ -> ""
 
     let optimizedImpls, optDataResources =
         // Perform optimization
         use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Optimize
 
-        let optEnv0 = GetInitialOptimizationEnv (tcImports, tcGlobals)
+        let optEnv0 = GetInitialOptimizationEnv(tcImports, tcGlobals)
 
         let importMap = tcImports.GetImportMap()
 
         let optimizedImpls, optimizationData, _ =
-            ApplyAllOptimizations
-                (tcConfig, tcGlobals, (LightweightTcValForUsingInBuildMethodCall tcGlobals), outfile,
-                 importMap, false, optEnv0, generatedCcu, typedImplFiles)
+            ApplyAllOptimizations(
+                tcConfig,
+                tcGlobals,
+                (LightweightTcValForUsingInBuildMethodCall tcGlobals),
+                outfile,
+                importMap,
+                false,
+                optEnv0,
+                generatedCcu,
+                typedImplFiles
+            )
 
         AbortOnError(diagnosticsLogger, exiter)
 
@@ -780,18 +1075,53 @@ let main3(Args (ctok, tcConfig, tcImports, frameworkTcImports: TcImports, tcGlob
         optimizedImpls, EncodeOptimizationData(tcGlobals, tcConfig, outfile, exportRemapping, (generatedCcu, optimizationData), false)
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger,
-          generatedCcu, outfile, optimizedImpls, topAttrs, pdbfile, assemblyName,
-          sigDataAttributes, sigDataResources, optDataResources, assemVerFromAttrib, signingInfo, metadataVersion, exiter, ilSourceDocs)
+    Args(
+        ctok,
+        tcConfig,
+        tcImports,
+        tcGlobals,
+        diagnosticsLogger,
+        generatedCcu,
+        outfile,
+        optimizedImpls,
+        topAttrs,
+        pdbfile,
+        assemblyName,
+        sigDataAttributes,
+        sigDataResources,
+        optDataResources,
+        assemVerFromAttrib,
+        signingInfo,
+        metadataVersion,
+        exiter,
+        ilSourceDocs
+    )
 
 /// Fourth phase of compilation.
 ///   -  Static linking
 ///   -  IL code generation
 let main4
-      (tcImportsCapture,dynamicAssemblyCreator)
-      (Args (ctok, tcConfig: TcConfig, tcImports, tcGlobals: TcGlobals, diagnosticsLogger,
-             generatedCcu: CcuThunk, outfile, optimizedImpls, topAttrs, pdbfile, assemblyName,
-             sigDataAttributes, sigDataResources, optDataResources, assemVerFromAttrib, signingInfo, metadataVersion, exiter: Exiter, ilSourceDocs)) =
+    (tcImportsCapture, dynamicAssemblyCreator)
+    (Args (ctok,
+           tcConfig: TcConfig,
+           tcImports,
+           tcGlobals: TcGlobals,
+           diagnosticsLogger,
+           generatedCcu: CcuThunk,
+           outfile,
+           optimizedImpls,
+           topAttrs,
+           pdbfile,
+           assemblyName,
+           sigDataAttributes,
+           sigDataResources,
+           optDataResources,
+           assemVerFromAttrib,
+           signingInfo,
+           metadataVersion,
+           exiter: Exiter,
+           ilSourceDocs))
+    =
 
     match tcImportsCapture with
     | None -> ()
@@ -799,50 +1129,110 @@ let main4
 
     // Compute a static linker, it gets called later.
     let ilGlobals = tcGlobals.ilg
+
     if tcConfig.standalone && generatedCcu.UsesFSharp20PlusQuotations then
-        error(Error(FSComp.SR.fscQuotationLiteralsStaticLinking0(), rangeStartup))
+        error (Error(FSComp.SR.fscQuotationLiteralsStaticLinking0 (), rangeStartup))
 
-    let staticLinker = StaticLink (ctok, tcConfig, tcImports, ilGlobals)
+    let staticLinker = StaticLink(ctok, tcConfig, tcImports, ilGlobals)
 
     ReportTime tcConfig "TAST -> IL"
     use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.IlxGen
 
     // Create the Abstract IL generator
-    let ilxGenerator = CreateIlxAssemblyGenerator (tcConfig, tcImports, tcGlobals, (LightweightTcValForUsingInBuildMethodCall tcGlobals), generatedCcu)
+    let ilxGenerator =
+        CreateIlxAssemblyGenerator(tcConfig, tcImports, tcGlobals, (LightweightTcValForUsingInBuildMethodCall tcGlobals), generatedCcu)
 
-    let codegenBackend = (if Option.isSome dynamicAssemblyCreator then IlReflectBackend else IlWriteBackend)
+    let codegenBackend =
+        (if Option.isSome dynamicAssemblyCreator then
+             IlReflectBackend
+         else
+             IlWriteBackend)
 
     // Generate the Abstract IL Code
-    let codegenResults = GenerateIlxCode (codegenBackend, Option.isSome dynamicAssemblyCreator, false, tcConfig, topAttrs, optimizedImpls, generatedCcu.AssemblyName, ilxGenerator)
+    let codegenResults =
+        GenerateIlxCode(
+            codegenBackend,
+            Option.isSome dynamicAssemblyCreator,
+            false,
+            tcConfig,
+            topAttrs,
+            optimizedImpls,
+            generatedCcu.AssemblyName,
+            ilxGenerator
+        )
 
     // Build the Abstract IL view of the final main module, prior to static linking
 
     let topAssemblyAttrs = codegenResults.topAssemblyAttrs
-    let topAttrs = {topAttrs with assemblyAttrs=topAssemblyAttrs}
+
+    let topAttrs =
+        { topAttrs with
+            assemblyAttrs = topAssemblyAttrs
+        }
+
     let permissionSets = codegenResults.permissionSets
     let secDecls = mkILSecurityDecls permissionSets
 
     let ilxMainModule =
-        MainModuleBuilder.CreateMainModule
-            (ctok, tcConfig, tcGlobals, tcImports,
-             pdbfile, assemblyName, outfile, topAttrs,
-             sigDataAttributes, sigDataResources, optDataResources,
-             codegenResults, assemVerFromAttrib, metadataVersion, secDecls)
+        MainModuleBuilder.CreateMainModule(
+            ctok,
+            tcConfig,
+            tcGlobals,
+            tcImports,
+            pdbfile,
+            assemblyName,
+            outfile,
+            topAttrs,
+            sigDataAttributes,
+            sigDataResources,
+            optDataResources,
+            codegenResults,
+            assemVerFromAttrib,
+            metadataVersion,
+            secDecls
+        )
 
     AbortOnError(diagnosticsLogger, exiter)
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, staticLinker, outfile, pdbfile, ilxMainModule, signingInfo, exiter, ilSourceDocs)
+    Args(
+        ctok,
+        tcConfig,
+        tcImports,
+        tcGlobals,
+        diagnosticsLogger,
+        staticLinker,
+        outfile,
+        pdbfile,
+        ilxMainModule,
+        signingInfo,
+        exiter,
+        ilSourceDocs
+    )
 
 /// Fifth phase of compilation.
 ///   -  static linking
-let main5(Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: DiagnosticsLogger, staticLinker, outfile, pdbfile, ilxMainModule, signingInfo, exiter: Exiter, ilSourceDocs)) =
+let main5
+    (Args (ctok,
+           tcConfig,
+           tcImports,
+           tcGlobals,
+           diagnosticsLogger: DiagnosticsLogger,
+           staticLinker,
+           outfile,
+           pdbfile,
+           ilxMainModule,
+           signingInfo,
+           exiter: Exiter,
+           ilSourceDocs))
+    =
 
     use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Output
 
     // Static linking, if any
     let ilxMainModule =
-        try  staticLinker ilxMainModule
+        try
+            staticLinker ilxMainModule
         with e ->
             errorRecoveryNoRange e
             exiter.Exit 1
@@ -850,25 +1240,37 @@ let main5(Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger: Diagnos
     AbortOnError(diagnosticsLogger, exiter)
 
     // Pass on only the minimum information required for the next phase
-    Args (ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, ilxMainModule, outfile, pdbfile, signingInfo, exiter, ilSourceDocs)
+    Args(ctok, tcConfig, tcImports, tcGlobals, diagnosticsLogger, ilxMainModule, outfile, pdbfile, signingInfo, exiter, ilSourceDocs)
 
 /// Sixth phase of compilation.
 ///   -  write the binaries
-let main6 dynamicAssemblyCreator (Args (ctok, tcConfig,  tcImports: TcImports, tcGlobals: TcGlobals,
-                                        diagnosticsLogger: DiagnosticsLogger, ilxMainModule, outfile, pdbfile,
-                                        signingInfo, exiter: Exiter, ilSourceDocs)) =
+let main6
+    dynamicAssemblyCreator
+    (Args (ctok,
+           tcConfig,
+           tcImports: TcImports,
+           tcGlobals: TcGlobals,
+           diagnosticsLogger: DiagnosticsLogger,
+           ilxMainModule,
+           outfile,
+           pdbfile,
+           signingInfo,
+           exiter: Exiter,
+           ilSourceDocs))
+    =
 
     ReportTime tcConfig "Write .NET Binary"
 
     use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Output
     let outfile = tcConfig.MakePathAbsolute outfile
 
-    DoesNotRequireCompilerThreadTokenAndCouldPossiblyBeMadeConcurrent  ctok
+    DoesNotRequireCompilerThreadTokenAndCouldPossiblyBeMadeConcurrent ctok
 
-    let pdbfile = pdbfile |> Option.map (tcConfig.MakePathAbsolute >> FileSystem.GetFullPathShim)
+    let pdbfile =
+        pdbfile |> Option.map (tcConfig.MakePathAbsolute >> FileSystem.GetFullPathShim)
 
     let normalizeAssemblyRefs (aref: ILAssemblyRef) =
-        match tcImports.TryFindDllInfo (ctok, rangeStartup, aref.Name, lookupOnly=false) with
+        match tcImports.TryFindDllInfo(ctok, rangeStartup, aref.Name, lookupOnly = false) with
         | Some dllInfo ->
             match dllInfo.ILScopeRef with
             | ILScopeRef.Assembly ref -> ref
@@ -885,66 +1287,70 @@ let main6 dynamicAssemblyCreator (Args (ctok, tcConfig,  tcImports: TcImports, t
                     match tcConfig.emitMetadataAssembly with
                     | MetadataAssemblyGeneration.ReferenceOut outputPath -> outputPath
                     | _ -> outfile
+
                 let referenceAssemblyAttribOpt =
                     tcGlobals.iltyp_ReferenceAssemblyAttributeOpt
-                    |> Option.map (fun ilTy ->
-                        mkILCustomAttribute (ilTy.TypeRef, [], [], [])
-                    )
+                    |> Option.map (fun ilTy -> mkILCustomAttribute (ilTy.TypeRef, [], [], []))
+
                 try
                     // We want to write no PDB info.
-                    ILBinaryWriter.WriteILBinaryFile
-                     ({ ilg = tcGlobals.ilg
-                        outfile = outfile
-                        pdbfile = None
-                        emitTailcalls = tcConfig.emitTailcalls
-                        deterministic = tcConfig.deterministic
-                        showTimes = tcConfig.showTimes
-                        portablePDB = false
-                        embeddedPDB = false
-                        embedAllSource = tcConfig.embedAllSource
-                        embedSourceList = tcConfig.embedSourceList
-                        allGivenSources = ilSourceDocs
-                        sourceLink = tcConfig.sourceLink
-                        checksumAlgorithm = tcConfig.checksumAlgorithm
-                        signer = GetStrongNameSigner signingInfo
-                        dumpDebugInfo = tcConfig.dumpDebugInfo
-                        referenceAssemblyOnly = true
-                        referenceAssemblyAttribOpt = referenceAssemblyAttribOpt
-                        pathMap = tcConfig.pathMap },
-                      ilxMainModule,
-                      normalizeAssemblyRefs
-                      )
+                    ILBinaryWriter.WriteILBinaryFile(
+                        {
+                            ilg = tcGlobals.ilg
+                            outfile = outfile
+                            pdbfile = None
+                            emitTailcalls = tcConfig.emitTailcalls
+                            deterministic = tcConfig.deterministic
+                            showTimes = tcConfig.showTimes
+                            portablePDB = false
+                            embeddedPDB = false
+                            embedAllSource = tcConfig.embedAllSource
+                            embedSourceList = tcConfig.embedSourceList
+                            allGivenSources = ilSourceDocs
+                            sourceLink = tcConfig.sourceLink
+                            checksumAlgorithm = tcConfig.checksumAlgorithm
+                            signer = GetStrongNameSigner signingInfo
+                            dumpDebugInfo = tcConfig.dumpDebugInfo
+                            referenceAssemblyOnly = true
+                            referenceAssemblyAttribOpt = referenceAssemblyAttribOpt
+                            pathMap = tcConfig.pathMap
+                        },
+                        ilxMainModule,
+                        normalizeAssemblyRefs
+                    )
                 with Failure msg ->
-                    error(Error(FSComp.SR.fscProblemWritingBinary(outfile, msg), rangeCmdArgs))
+                    error (Error(FSComp.SR.fscProblemWritingBinary (outfile, msg), rangeCmdArgs))
 
             match tcConfig.emitMetadataAssembly with
             | MetadataAssemblyGeneration.ReferenceOnly -> ()
             | _ ->
                 try
-                    ILBinaryWriter.WriteILBinaryFile
-                     ({ ilg = tcGlobals.ilg
-                        outfile = outfile
-                        pdbfile = pdbfile
-                        emitTailcalls = tcConfig.emitTailcalls
-                        deterministic = tcConfig.deterministic
-                        showTimes = tcConfig.showTimes
-                        portablePDB = tcConfig.portablePDB
-                        embeddedPDB = tcConfig.embeddedPDB
-                        embedAllSource = tcConfig.embedAllSource
-                        embedSourceList = tcConfig.embedSourceList
-                        allGivenSources = ilSourceDocs
-                        sourceLink = tcConfig.sourceLink
-                        checksumAlgorithm = tcConfig.checksumAlgorithm
-                        signer = GetStrongNameSigner signingInfo
-                        dumpDebugInfo = tcConfig.dumpDebugInfo
-                        referenceAssemblyOnly = false
-                        referenceAssemblyAttribOpt = None
-                        pathMap = tcConfig.pathMap },
-                      ilxMainModule,
-                      normalizeAssemblyRefs
-                      )
+                    ILBinaryWriter.WriteILBinaryFile(
+                        {
+                            ilg = tcGlobals.ilg
+                            outfile = outfile
+                            pdbfile = pdbfile
+                            emitTailcalls = tcConfig.emitTailcalls
+                            deterministic = tcConfig.deterministic
+                            showTimes = tcConfig.showTimes
+                            portablePDB = tcConfig.portablePDB
+                            embeddedPDB = tcConfig.embeddedPDB
+                            embedAllSource = tcConfig.embedAllSource
+                            embedSourceList = tcConfig.embedSourceList
+                            allGivenSources = ilSourceDocs
+                            sourceLink = tcConfig.sourceLink
+                            checksumAlgorithm = tcConfig.checksumAlgorithm
+                            signer = GetStrongNameSigner signingInfo
+                            dumpDebugInfo = tcConfig.dumpDebugInfo
+                            referenceAssemblyOnly = false
+                            referenceAssemblyAttribOpt = None
+                            pathMap = tcConfig.pathMap
+                        },
+                        ilxMainModule,
+                        normalizeAssemblyRefs
+                    )
                 with Failure msg ->
-                    error(Error(FSComp.SR.fscProblemWritingBinary(outfile, msg), rangeCmdArgs))
+                    error (Error(FSComp.SR.fscProblemWritingBinary (outfile, msg), rangeCmdArgs))
         with e ->
             errorRecoveryNoRange e
             exiter.Exit 1
@@ -953,40 +1359,93 @@ let main6 dynamicAssemblyCreator (Args (ctok, tcConfig,  tcImports: TcImports, t
     AbortOnError(diagnosticsLogger, exiter)
 
     // Don't copy referenced FSharp.core.dll if we are building FSharp.Core.dll
-    if (tcConfig.copyFSharpCore = CopyFSharpCoreFlag.Yes) && not tcConfig.compilingFSharpCore && not tcConfig.standalone then
+    if (tcConfig.copyFSharpCore = CopyFSharpCoreFlag.Yes)
+       && not tcConfig.compilingFSharpCore
+       && not tcConfig.standalone then
         CopyFSharpCore(outfile, tcConfig.referencedDLLs)
 
     ReportTime tcConfig "Exiting"
 
 /// The main (non-incremental) compilation entry point used by fsc.exe
 let CompileFromCommandLineArguments
-       (ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted, reduceMemoryUsage,
-        defaultCopyFSharpCore, exiter: Exiter, loggerProvider, tcImportsCapture, dynamicAssemblyCreator) =
+    (
+        ctok,
+        argv,
+        legacyReferenceResolver,
+        bannerAlreadyPrinted,
+        reduceMemoryUsage,
+        defaultCopyFSharpCore,
+        exiter: Exiter,
+        loggerProvider,
+        tcImportsCapture,
+        dynamicAssemblyCreator
+    ) =
 
     use disposables = new DisposablesTracker()
     let savedOut = Console.Out
+
     use _ =
         { new IDisposable with
             member _.Dispose() =
                 try
                     Console.SetOut(savedOut)
-                with _ -> ()}
-
-    main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted, reduceMemoryUsage, defaultCopyFSharpCore, exiter, loggerProvider, disposables)
+                with _ ->
+                    ()
+        }
+
+    main1 (
+        ctok,
+        argv,
+        legacyReferenceResolver,
+        bannerAlreadyPrinted,
+        reduceMemoryUsage,
+        defaultCopyFSharpCore,
+        exiter,
+        loggerProvider,
+        disposables
+    )
     |> main2
     |> main3
-    |> main4 (tcImportsCapture,dynamicAssemblyCreator)
+    |> main4 (tcImportsCapture, dynamicAssemblyCreator)
     |> main5
     |> main6 dynamicAssemblyCreator
 
 /// An additional compilation entry point used by FSharp.Compiler.Service taking syntax trees as input
 let CompileFromSyntaxTrees
-       (ctok, legacyReferenceResolver, reduceMemoryUsage, assemblyName, target,
-        targetDll, targetPdb, dependencies, noframework, exiter, loggerProvider, inputs, tcImportsCapture, dynamicAssemblyCreator) =
+    (
+        ctok,
+        legacyReferenceResolver,
+        reduceMemoryUsage,
+        assemblyName,
+        target,
+        targetDll,
+        targetPdb,
+        dependencies,
+        noframework,
+        exiter,
+        loggerProvider,
+        inputs,
+        tcImportsCapture,
+        dynamicAssemblyCreator
+    ) =
 
     use disposables = new DisposablesTracker()
-    main1OfAst (ctok, legacyReferenceResolver, reduceMemoryUsage, assemblyName, target, targetDll, targetPdb,
-                dependencies, noframework, exiter, loggerProvider, disposables, inputs)
+
+    main1OfAst (
+        ctok,
+        legacyReferenceResolver,
+        reduceMemoryUsage,
+        assemblyName,
+        target,
+        targetDll,
+        targetPdb,
+        dependencies,
+        noframework,
+        exiter,
+        loggerProvider,
+        disposables,
+        inputs
+    )
     |> main2
     |> main3
     |> main4 (tcImportsCapture, dynamicAssemblyCreator)
diff --git a/src/Compiler/Interactive/fsi.fs b/src/Compiler/Interactive/fsi.fs
index 797f098f585..0cbade7c234 100644
--- a/src/Compiler/Interactive/fsi.fs
+++ b/src/Compiler/Interactive/fsi.fs
@@ -1906,7 +1906,8 @@ type internal FsiDynamicCompiler(
 
                 match fsiOptions.DependencyProvider.TryFindDependencyManagerByKey(tcConfigB.compilerToolPaths, getOutputDir tcConfigB, reportError m, packageManagerKey) with
                 | Null ->
-                    errorR(Error(fsiOptions.DependencyProvider.CreatePackageManagerUnknownError(tcConfigB.compilerToolPaths, outputDir, packageManagerKey, reportError m), m))
+                    let err = fsiOptions.DependencyProvider.CreatePackageManagerUnknownError(tcConfigB.compilerToolPaths, outputDir, packageManagerKey, reportError m)
+                    errorR(Error(err, m))
                     istate
                 | NonNull dependencyManager ->
                     let directive d =
diff --git a/src/Compiler/Utilities/EditDistance.fs b/src/Compiler/Utilities/EditDistance.fs
index 8f5a76ae152..10595b0b999 100644
--- a/src/Compiler/Utilities/EditDistance.fs
+++ b/src/Compiler/Utilities/EditDistance.fs
@@ -80,10 +80,7 @@ let jaro (s1: string) (s2: string) =
         / 3.0
 
     // This is for cases where |s1|, |s2| or m are zero
-    if Double.IsNaN result then
-        0.0
-    else
-        result
+    if Double.IsNaN result then 0.0 else result
 
 /// Calculates the Jaro-Winkler edit distance between two strings.
 /// The edit distance is a metric that allows to measure the amount of similarity between two strings.
diff --git a/src/Compiler/Utilities/FileSystem.fs b/src/Compiler/Utilities/FileSystem.fs
index bdd64e87b69..465adeac51d 100644
--- a/src/Compiler/Utilities/FileSystem.fs
+++ b/src/Compiler/Utilities/FileSystem.fs
@@ -127,8 +127,7 @@ type ByteArrayMemory(bytes: byte[], offset, length) =
             ByteArrayMemory(Array.empty, 0, 0) :> ByteMemory
 
     override _.CopyTo stream =
-        if length > 0 then
-            stream.Write(bytes, offset, length)
+        if length > 0 then stream.Write(bytes, offset, length)
 
     override _.Copy(srcOffset, dest, destOffset, count) =
         checkCount count
@@ -413,8 +412,7 @@ module internal FileSystemUtils =
     let checkSuffix (path: string) (suffix: string) = path.EndsWithOrdinalIgnoreCase(suffix)
 
     let hasExtensionWithValidate (validate: bool) (s: string) =
-        if validate then
-            (checkPathForIllegalChars s)
+        if validate then (checkPathForIllegalChars s)
 
         let sLen = s.Length
 
@@ -439,8 +437,7 @@ module internal FileSystemUtils =
         Path.GetFileName(path)
 
     let fileNameWithoutExtensionWithValidate (validate: bool) path =
-        if validate then
-            checkPathForIllegalChars path
+        if validate then checkPathForIllegalChars path
 
         Path.GetFileNameWithoutExtension(path)
 
@@ -566,8 +563,7 @@ type DefaultFileSystem() as this =
 
             let stream = new MemoryMappedStream(mmf, length)
 
-            if not stream.CanRead then
-                invalidOp "Cannot read file"
+            if not stream.CanRead then invalidOp "Cannot read file"
 
             stream :> Stream
 
@@ -885,8 +881,7 @@ type internal ByteStream =
     }
 
     member b.ReadByte() =
-        if b.pos >= b.max then
-            failwith "end of stream"
+        if b.pos >= b.max then failwith "end of stream"
 
         let res = b.bytes[b.pos]
         b.pos <- b.pos + 1
@@ -953,8 +948,7 @@ type internal ByteBuffer =
 
             Bytes.blit old 0 buf.bbArray 0 buf.bbCurrent
 
-            if buf.useArrayPool then
-                ArrayPool.Shared.Return old
+            if buf.useArrayPool then ArrayPool.Shared.Return old
 
     member buf.AsMemory() =
         buf.CheckDisposed()
diff --git a/src/Compiler/Utilities/HashMultiMap.fs b/src/Compiler/Utilities/HashMultiMap.fs
index b88af5d77eb..cb750676fe3 100644
--- a/src/Compiler/Utilities/HashMultiMap.fs
+++ b/src/Compiler/Utilities/HashMultiMap.fs
@@ -170,8 +170,7 @@ type internal HashMultiMap<'Key, 'Value>(size: int, comparer: IEqualityComparer<
         member s.Remove(x) =
             match s.TryFind x.Key with
             | Some v ->
-                if Unchecked.equals v x.Value then
-                    s.Remove(x.Key)
+                if Unchecked.equals v x.Value then s.Remove(x.Key)
 
                 true
             | _ -> false
diff --git a/src/Compiler/Utilities/ImmutableArray.fs b/src/Compiler/Utilities/ImmutableArray.fs
index e32ce5391b6..5311efa5e0c 100644
--- a/src/Compiler/Utilities/ImmutableArray.fs
+++ b/src/Compiler/Utilities/ImmutableArray.fs
@@ -18,8 +18,7 @@ module ImmutableArray =
         | 0 -> ImmutableArray.Empty
         | 1 -> ImmutableArray.Create(f 0)
         | n ->
-            if n < 0 then
-                invalidArg "n" "Below zero."
+            if n < 0 then invalidArg "n" "Below zero."
 
             let builder = ImmutableArray.CreateBuilder(n)
 
@@ -144,12 +143,9 @@ module ImmutableArray =
 
     let tryFind predicate (arr: ImmutableArray<'T>) =
         let rec loop i =
-            if i >= arr.Length then
-                None
-            else if predicate arr[i] then
-                Some arr[i]
-            else
-                loop (i + 1)
+            if i >= arr.Length then None
+            else if predicate arr[i] then Some arr[i]
+            else loop (i + 1)
 
         loop 0
 
@@ -184,8 +180,7 @@ module ImmutableArray =
         let builder = ImmutableArray.CreateBuilder(arr.Length)
 
         for i = 0 to arr.Length - 1 do
-            if predicate arr[i] then
-                builder.Add(arr[i])
+            if predicate arr[i] then builder.Add(arr[i])
 
         builder.Capacity <- builder.Count
         builder.MoveToImmutable()
@@ -204,8 +199,7 @@ module ImmutableArray =
         for i = 0 to arr.Length - 1 do
             let result = chooser arr[i]
 
-            if result.IsSome then
-                builder.Add(result.Value)
+            if result.IsSome then builder.Add(result.Value)
 
         builder.Capacity <- builder.Count
         builder.MoveToImmutable()
diff --git a/src/Compiler/Utilities/InternalCollections.fs b/src/Compiler/Utilities/InternalCollections.fs
index 8ef878530d0..96aaf9b684f 100755
--- a/src/Compiler/Utilities/InternalCollections.fs
+++ b/src/Compiler/Utilities/InternalCollections.fs
@@ -142,11 +142,7 @@ type internal AgedLookup<'Token, 'Key, 'Value when 'Value: not struct>(keepStron
     member al.Put(tok, key, value) =
         let data = FilterAndHold(tok)
 
-        let data =
-            if Exists(data, key) then
-                RemoveImpl(data, key)
-            else
-                data
+        let data = if Exists(data, key) then RemoveImpl(data, key) else data
 
         let data = Add(data, key, value)
         AssignWithStrength(tok, data) // This will remove extras
@@ -201,11 +197,7 @@ type internal MruCache<'Token, 'Key, 'Value when 'Value: not struct>
 
     member bc.TryGetAny(tok, key) =
         match cache.TryPeekKeyValue(tok, key) with
-        | Some (similarKey, value) ->
-            if areSame (similarKey, key) then
-                Some(value)
-            else
-                None
+        | Some (similarKey, value) -> if areSame (similarKey, key) then Some(value) else None
         | None -> None
 
     member bc.TryGet(tok, key) =
@@ -224,11 +216,7 @@ type internal MruCache<'Token, 'Key, 'Value when 'Value: not struct>
 
     member bc.TryGetSimilar(tok, key) =
         match cache.TryGetKeyValue(tok, key) with
-        | Some (_, value) ->
-            if isStillValid (key, value) then
-                Some value
-            else
-                None
+        | Some (_, value) -> if isStillValid (key, value) then Some value else None
         | None -> None
 
     member bc.Set(tok, key: 'Key, value: 'Value) = cache.Put(tok, key, value)
diff --git a/src/Compiler/Utilities/QueueList.fs b/src/Compiler/Utilities/QueueList.fs
index 2cab0fe0b70..2c6852f8fc7 100644
--- a/src/Compiler/Utilities/QueueList.fs
+++ b/src/Compiler/Utilities/QueueList.fs
@@ -27,10 +27,7 @@ type internal QueueList<'T>(firstElementsIn: 'T list, lastElementsRevIn: 'T list
 
     // Compute the last elements on demand.
     let lastElements () =
-        if push then
-            []
-        else
-            List.rev lastElementsRev
+        if push then [] else List.rev lastElementsRev
 
     static let empty = QueueList<'T>([], [], 0)
 
diff --git a/src/Compiler/Utilities/ResizeArray.fs b/src/Compiler/Utilities/ResizeArray.fs
index a54521525f5..e96c775f972 100644
--- a/src/Compiler/Utilities/ResizeArray.fs
+++ b/src/Compiler/Utilities/ResizeArray.fs
@@ -26,8 +26,7 @@ module internal ResizeArray =
         if start2 < 0 then
             invalidArg "start2" "index must be positive"
 
-        if len < 0 then
-            invalidArg "len" "length must be positive"
+        if len < 0 then invalidArg "len" "length must be positive"
 
         if start1 + len > length arr1 then
             invalidArg "start1" "(start1+len) out of range"
@@ -53,8 +52,7 @@ module internal ResizeArray =
         if start < 0 then
             invalidArg "start" "index must be positive"
 
-        if len < 0 then
-            invalidArg "len" "length must be positive"
+        if len < 0 then invalidArg "len" "length must be positive"
 
         if start + len > length arr then
             invalidArg "len" "length must be positive"
@@ -65,8 +63,7 @@ module internal ResizeArray =
         if start < 0 then
             invalidArg "start" "index must be positive"
 
-        if len < 0 then
-            invalidArg "len" "length must be positive"
+        if len < 0 then invalidArg "len" "length must be positive"
 
         if start + len > length arr then
             invalidArg "len" "length must be positive"
diff --git a/src/Compiler/Utilities/TaggedCollections.fs b/src/Compiler/Utilities/TaggedCollections.fs
index 00e0ceb8d1d..c36cf0afb4f 100644
--- a/src/Compiler/Utilities/TaggedCollections.fs
+++ b/src/Compiler/Utilities/TaggedCollections.fs
@@ -106,12 +106,9 @@ module SetTree =
                 // nb. no check for rebalance needed for small trees, also be sure to reuse node already allocated
                 let c = comparer.Compare(k, t.Key)
 
-                if c < 0 then
-                    SetTreeNode(k, empty, t, 2) :> SetTree<'T>
-                elif c = 0 then
-                    t
-                else
-                    SetTreeNode(k, t, empty, 2) :> SetTree<'T>
+                if c < 0 then SetTreeNode(k, empty, t, 2) :> SetTree<'T>
+                elif c = 0 then t
+                else SetTreeNode(k, t, empty, 2) :> SetTree<'T>
 
     let rec balance comparer (t1: SetTree<'T>) k (t2: SetTree<'T>) =
         // Given t1 < k < t2 where t1 and t2 are "balanced",
@@ -211,12 +208,9 @@ module SetTree =
 
             match t with
             | :? SetTreeNode<'T> as tn ->
-                if c < 0 then
-                    contains comparer k tn.Left
-                elif c = 0 then
-                    true
-                else
-                    contains comparer k tn.Right
+                if c < 0 then contains comparer k tn.Left
+                elif c = 0 then true
+                else contains comparer k tn.Right
             | _ -> (c = 0)
 
     let rec iter f (t: SetTree<'T>) =
@@ -266,18 +260,10 @@ module SetTree =
         else
             match t with
             | :? SetTreeNode<'T> as tn ->
-                let acc =
-                    if f tn.Key then
-                        add comparer tn.Key acc
-                    else
-                        acc
+                let acc = if f tn.Key then add comparer tn.Key acc else acc
 
                 filterAux comparer f tn.Left (filterAux comparer f tn.Right acc)
-            | _ ->
-                if f t.Key then
-                    add comparer t.Key acc
-                else
-                    acc
+            | _ -> if f t.Key then add comparer t.Key acc else acc
 
     let filter comparer f s = filterAux comparer f s empty
 
@@ -495,10 +481,7 @@ module SetTree =
         | _, [] -> 1
         | x1 :: t1, x2 :: t2 ->
             if isEmpty x1 then
-                if isEmpty x2 then
-                    compareStacks comparer t1 t2
-                else
-                    cont ()
+                if isEmpty x2 then compareStacks comparer t1 t2 else cont ()
             elif isEmpty x2 then
                 cont ()
             else
@@ -540,10 +523,7 @@ module SetTree =
                     | _ ->
                         let c = comparer.Compare(x1.Key, x2.Key)
 
-                        if c <> 0 then
-                            c
-                        else
-                            compareStacks comparer t1 t2
+                        if c <> 0 then c else compareStacks comparer t1 t2
 
     let compare comparer (t1: SetTree<'T>) (t2: SetTree<'T>) =
         if isEmpty t1 then
@@ -648,20 +628,14 @@ type internal Set<'T, 'ComparerTag> when 'ComparerTag :> IComparer<'T>(comparer:
             SetTree.intersection a.Comparer a.Tree b.Tree |> refresh a
 
     static member Union(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) : Set<'T, 'ComparerTag> =
-        if SetTree.isEmpty b.Tree then
-            a (* A U 0 = A *)
-        else if SetTree.isEmpty a.Tree then
-            b (* 0 U B = B *)
-        else
-            SetTree.union a.Comparer a.Tree b.Tree |> refresh a
+        if SetTree.isEmpty b.Tree then a (* A U 0 = A *)
+        else if SetTree.isEmpty a.Tree then b (* 0 U B = B *)
+        else SetTree.union a.Comparer a.Tree b.Tree |> refresh a
 
     static member Difference(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) : Set<'T, 'ComparerTag> =
-        if SetTree.isEmpty a.Tree then
-            a (* 0 - B = 0 *)
-        else if SetTree.isEmpty b.Tree then
-            a (* A - 0 = A *)
-        else
-            SetTree.diff a.Comparer a.Tree b.Tree |> refresh a
+        if SetTree.isEmpty a.Tree then a (* 0 - B = 0 *)
+        else if SetTree.isEmpty b.Tree then a (* A - 0 = A *)
+        else SetTree.diff a.Comparer a.Tree b.Tree |> refresh a
 
     static member Equality(a: Set<'T, 'ComparerTag>, b: Set<'T, 'ComparerTag>) =
         (SetTree.compare a.Comparer a.Tree b.Tree = 0)
@@ -852,18 +826,12 @@ module MapTree =
     let find (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) =
         let mutable v = Unchecked.defaultof<'Value>
 
-        if tryGetValue comparer k &v m then
-            v
-        else
-            indexNotFound ()
+        if tryGetValue comparer k &v m then v else indexNotFound ()
 
     let tryFind (comparer: IComparer<'Key>) k (m: MapTree<'Key, 'Value>) =
         let mutable v = Unchecked.defaultof<'Value>
 
-        if tryGetValue comparer k &v m then
-            Some v
-        else
-            None
+        if tryGetValue comparer k &v m then Some v else None
 
     let partition1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v (acc1, acc2) =
         if f.Invoke(k, v) then
@@ -886,10 +854,7 @@ module MapTree =
         partitionAux comparer (OptimizedClosures.FSharpFunc<_, _, _>.Adapt f) m (empty, empty)
 
     let filter1 (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) k v acc =
-        if f.Invoke(k, v) then
-            add comparer k v acc
-        else
-            acc
+        if f.Invoke(k, v) then add comparer k v acc else acc
 
     let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc =
         if isEmpty m then
@@ -1061,11 +1026,7 @@ module MapTree =
                     let cLoKey = comparer.Compare(lo, mn.Key)
                     let cKeyHi = comparer.Compare(mn.Key, hi)
 
-                    let x =
-                        if cLoKey < 0 then
-                            foldFromTo f mn.Left x
-                        else
-                            x
+                    let x = if cLoKey < 0 then foldFromTo f mn.Left x else x
 
                     let x =
                         if cLoKey <= 0 && cKeyHi <= 0 then
@@ -1073,11 +1034,7 @@ module MapTree =
                         else
                             x
 
-                    let x =
-                        if cKeyHi < 0 then
-                            foldFromTo f mn.Right x
-                        else
-                            x
+                    let x = if cKeyHi < 0 then foldFromTo f mn.Right x else x
 
                     x
                 | _ ->
@@ -1092,10 +1049,7 @@ module MapTree =
 
                     x
 
-        if comparer.Compare(lo, hi) = 1 then
-            x
-        else
-            foldFromTo f m x
+        if comparer.Compare(lo, hi) = 1 then x else foldFromTo f m x
 
     let foldSection (comparer: IComparer<'Key>) lo hi f m x =
         foldSectionOpt comparer lo hi (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) m x
diff --git a/src/Compiler/Utilities/illib.fs b/src/Compiler/Utilities/illib.fs
index 85e8b8d9d98..b5e0a2d5218 100644
--- a/src/Compiler/Utilities/illib.fs
+++ b/src/Compiler/Utilities/illib.fs
@@ -196,8 +196,7 @@ module Array =
             let mutable i = 0
 
             while eq && i < len do
-                if not (inp[i] === res[i]) then
-                    eq <- false
+                if not (inp[i] === res[i]) then eq <- false
 
                 i <- i + 1
 
@@ -403,11 +402,7 @@ module List =
     let rec findi n f l =
         match l with
         | [] -> None
-        | h :: t ->
-            if f h then
-                Some(h, n)
-            else
-                findi (n + 1) f t
+        | h :: t -> if f h then Some(h, n) else findi (n + 1) f t
 
     let splitChoose select l =
         let rec ch acc1 acc2 l =
@@ -437,10 +432,7 @@ module List =
             let h2a = f h1a
             let h2b = f h1b
 
-            if h1a === h2a && h1b === h2b then
-                inp
-            else
-                [ h2a; h2b ]
+            if h1a === h2a && h1b === h2b then inp else [ h2a; h2b ]
         | [ h1a; h1b; h1c ] ->
             let h2a = f h1a
             let h2b = f h1b
@@ -465,15 +457,16 @@ module List =
 
         loop [] l
 
+    let tryFrontAndBack l =
+        match l with
+        | [] -> None
+        | _ -> Some(frontAndBack l)
+
     let tryRemove f inp =
         let rec loop acc l =
             match l with
             | [] -> None
-            | h :: t ->
-                if f h then
-                    Some(h, List.rev acc @ t)
-                else
-                    loop (h :: acc) t
+            | h :: t -> if f h then Some(h, List.rev acc @ t) else loop (h :: acc) t
 
         loop [] inp
 
@@ -498,11 +491,7 @@ module List =
         let rec loop acc l =
             match l with
             | [] -> List.rev acc, []
-            | x :: xs ->
-                if p x then
-                    List.rev acc, l
-                else
-                    loop (x :: acc) xs
+            | x :: xs -> if p x then List.rev acc, l else loop (x :: acc) xs
 
         loop [] l
 
@@ -543,11 +532,7 @@ module List =
         let rec mn i =
             function
             | [] -> []
-            | x :: xs ->
-                if i = n then
-                    f x :: xs
-                else
-                    x :: mn (i + 1) xs
+            | x :: xs -> if i = n then f x :: xs else x :: mn (i + 1) xs
 
         mn 0 xs
 
@@ -745,20 +730,14 @@ module String =
     let split options (separator: string[]) (value: string) = value.Split(separator, options)
 
     let (|StartsWith|_|) pattern value =
-        if String.IsNullOrWhiteSpace value then
-            None
-        elif value.StartsWithOrdinal pattern then
-            Some()
-        else
-            None
+        if String.IsNullOrWhiteSpace value then None
+        elif value.StartsWithOrdinal pattern then Some()
+        else None
 
     let (|Contains|_|) pattern value =
-        if String.IsNullOrWhiteSpace value then
-            None
-        elif value.Contains pattern then
-            Some()
-        else
-            None
+        if String.IsNullOrWhiteSpace value then None
+        elif value.Contains pattern then Some()
+        else None
 
     let getLines (str: string) =
         use reader = new StringReader(str)
diff --git a/src/Compiler/Utilities/illib.fsi b/src/Compiler/Utilities/illib.fsi
index 4d7ca7a57d6..f2518c2cfb3 100644
--- a/src/Compiler/Utilities/illib.fsi
+++ b/src/Compiler/Utilities/illib.fsi
@@ -154,6 +154,8 @@ module internal List =
 
     val frontAndBack: l: 'a list -> 'a list * 'a
 
+    val tryFrontAndBack: l: 'a list -> ('a list * 'a) option
+
     val tryRemove: f: ('a -> bool) -> inp: 'a list -> ('a * 'a list) option
 
     val zip4: l1: 'a list -> l2: 'b list -> l3: 'c list -> l4: 'd list -> ('a * 'b * 'c * 'd) list
diff --git a/src/Compiler/Utilities/rational.fs b/src/Compiler/Utilities/rational.fs
index 563c49cb60c..13aca52dd62 100644
--- a/src/Compiler/Utilities/rational.fs
+++ b/src/Compiler/Utilities/rational.fs
@@ -12,10 +12,7 @@ type Rational =
     }
 
 let rec gcd a (b: BigInteger) =
-    if b = BigInteger.Zero then
-        a
-    else
-        gcd b (a % b)
+    if b = BigInteger.Zero then a else gcd b (a % b)
 
 let lcm a b = (a * b) / (gcd a b)
 
@@ -27,11 +24,7 @@ let mkRational p q =
         let g = gcd q p in
         p / g, q / g
 
-    let p, q =
-        if q > BigInteger.Zero then
-            p, q
-        else
-            -p, -q
+    let p, q = if q > BigInteger.Zero then p, q else -p, -q
 
      in
 
@@ -73,9 +66,6 @@ let GetNumerator p = int p.numerator
 let GetDenominator p = int p.denominator
 
 let SignRational p =
-    if p.numerator < BigInteger.Zero then
-        -1
-    else if p.numerator > BigInteger.Zero then
-        1
-    else
-        0
+    if p.numerator < BigInteger.Zero then -1
+    else if p.numerator > BigInteger.Zero then 1
+    else 0
diff --git a/src/Compiler/Utilities/sformat.fs b/src/Compiler/Utilities/sformat.fs
index 1c9a3dd5c51..e9ff2aecd26 100644
--- a/src/Compiler/Utilities/sformat.fs
+++ b/src/Compiler/Utilities/sformat.fs
@@ -630,16 +630,11 @@ module Display =
         Breaks(next + 1, outer, stack)
 
     let popBreak (Breaks (next, outer, stack)) =
-        if next = 0 then
-            raise (Failure "popBreak: underflow")
+        if next = 0 then raise (Failure "popBreak: underflow")
 
         let topBroke = stack[next - 1] < 0
 
-        let outer =
-            if outer = next then
-                outer - 1
-            else
-                outer // if all broken, unwind
+        let outer = if outer = next then outer - 1 else outer // if all broken, unwind
 
         let next = next - 1
         Breaks(next, outer, stack), topBroke
@@ -977,10 +972,7 @@ module Display =
         let exceededPrintSize () = size <= 0
 
         let countNodes n =
-            if size > 0 then
-                size <- size - n
-            else
-                () // no need to keep decrementing (and avoid wrap around)
+            if size > 0 then size <- size - n else () // no need to keep decrementing (and avoid wrap around)
 
         let stopShort _ = exceededPrintSize () // for unfoldL
 
@@ -1295,10 +1287,7 @@ module Display =
                     |> makeListL
 
                 let project1 x =
-                    if x >= (b1 + n1) then
-                        None
-                    else
-                        Some(x, x + 1)
+                    if x >= (b1 + n1) then None else Some(x, x + 1)
 
                 let rowsL = boundedUnfoldL rowL project1 stopShort b1 opts.PrintLength
 
@@ -1340,11 +1329,7 @@ module Display =
                 let itemLs =
                     boundedUnfoldL
                         possibleKeyValueL
-                        (fun () ->
-                            if it.MoveNext() then
-                                Some(it.Current, ())
-                            else
-                                None)
+                        (fun () -> if it.MoveNext() then Some(it.Current, ()) else None)
                         stopShort
                         ()
                         (1 + opts.PrintLength / 12)
diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs
index 1e0aeebcb7b..faa0c126948 100644
--- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs
+++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.Utilities.fs
@@ -35,10 +35,7 @@ module internal Utilities =
             let pos = option.IndexOf('=')
 
             let stringAsOpt text =
-                if String.IsNullOrEmpty(text) then
-                    None
-                else
-                    Some text
+                if String.IsNullOrEmpty(text) then None else Some text
 
             let nameOpt =
                 if pos <= 0 then
@@ -48,12 +45,9 @@ module internal Utilities =
 
             let valueOpt =
                 let valueText =
-                    if pos < 0 then
-                        option
-                    else if pos < option.Length then
-                        option.Substring(pos + 1)
-                    else
-                        ""
+                    if pos < 0 then option
+                    else if pos < option.Length then option.Substring(pos + 1)
+                    else ""
 
                 stringAsOpt (valueText.Trim(trimChars))
 
@@ -210,6 +204,5 @@ module internal Utilities =
                     // So strip off the flags
                     let pos = source.IndexOf(" ")
 
-                    if pos >= 0 then
-                        yield ("i", source.Substring(pos).Trim())
+                    if pos >= 0 then yield ("i", source.Substring(pos).Trim())
         }
diff --git a/src/fsi/console.fs b/src/fsi/console.fs
index aede59c3889..05f242990b2 100644
--- a/src/fsi/console.fs
+++ b/src/fsi/console.fs
@@ -209,10 +209,7 @@ type internal ReadLineConsole() =
         | _ -> "^?"
 
     member x.GetCharacterSize(c) =
-        if Char.IsControl(c) then
-            x.MapCharacter(c).Length
-        else
-            1
+        if Char.IsControl(c) then x.MapCharacter(c).Length else 1
 
     static member TabSize = 4
 
@@ -223,12 +220,7 @@ type internal ReadLineConsole() =
 
             if currLeft < x.Inset then
                 if currLeft = 0 then
-                    Console.Write(
-                        if prompt then
-                            x.Prompt2
-                        else
-                            String(' ', x.Inset)
-                    )
+                    Console.Write(if prompt then x.Prompt2 else String(' ', x.Inset))
 
                 Utils.guard (fun () ->
                     Console.CursorTop <- min Console.CursorTop (Console.BufferHeight - 1)
@@ -286,8 +278,7 @@ type internal ReadLineConsole() =
             let mutable position = -1
 
             for i = 0 to input.Length - 1 do
-                if (i = curr) then
-                    position <- output.Length
+                if (i = curr) then position <- output.Length
 
                 let c = input.Chars(i)
 
@@ -296,8 +287,7 @@ type internal ReadLineConsole() =
                 else
                     output.Append(c) |> ignore
 
-            if (curr = input.Length) then
-                position <- output.Length
+            if (curr = input.Length) then position <- output.Length
 
             // render the current text, computing a new value for "rendered"
             let old_rendered = rendered
@@ -376,11 +366,7 @@ type internal ReadLineConsole() =
             optionsCache <- opts
 
             if (opts.Count > 0) then
-                let part =
-                    if shift then
-                        opts.Previous()
-                    else
-                        opts.Next()
+                let part = if shift then opts.Previous() else opts.Next()
 
                 setInput (opts.Root + part)
             else if (prefix) then
@@ -416,11 +402,7 @@ type internal ReadLineConsole() =
             // REVIEW: is this F6 rewrite required? 0x1A looks like Ctrl-Z.
             // REVIEW: the Ctrl-Z code is not recognised as EOF by the lexer.
             // REVIEW: looks like a relic of the port of readline, which is currently removable.
-            let c =
-                if (key.Key = ConsoleKey.F6) then
-                    '\x1A'
-                else
-                    key.KeyChar
+            let c = if (key.Key = ConsoleKey.F6) then '\x1A' else key.KeyChar
 
             insertChar (c)
 
@@ -437,8 +419,7 @@ type internal ReadLineConsole() =
             if (line = "\x1A") then
                 null
             else
-                if (line.Length > 0) then
-                    history.AddLast(line)
+                if (line.Length > 0) then history.AddLast(line)
 
                 line
 
diff --git a/src/fsi/fsimain.fs b/src/fsi/fsimain.fs
index 6bd2000f9a3..b94a152c171 100644
--- a/src/fsi/fsimain.fs
+++ b/src/fsi/fsimain.fs
@@ -278,11 +278,7 @@ let evaluateSession (argv: string[]) =
 
                 member _.EventLoopRun() =
 #if !FX_NO_WINFORMS
-                    match (if fsiSession.IsGui then
-                               fsiWinFormsLoop.Value
-                           else
-                               None)
-                        with
+                    match (if fsiSession.IsGui then fsiWinFormsLoop.Value else None) with
                     | Some l -> (l :> IEventLoop).Run()
                     | _ ->
 #endif
@@ -290,11 +286,7 @@ let evaluateSession (argv: string[]) =
 
                 member _.EventLoopInvoke(f) =
 #if !FX_NO_WINFORMS
-                    match (if fsiSession.IsGui then
-                               fsiWinFormsLoop.Value
-                           else
-                               None)
-                        with
+                    match (if fsiSession.IsGui then fsiWinFormsLoop.Value else None) with
                     | Some l -> (l :> IEventLoop).Invoke(f)
                     | _ ->
 #endif
@@ -302,11 +294,7 @@ let evaluateSession (argv: string[]) =
 
                 member _.EventLoopScheduleRestart() =
 #if !FX_NO_WINFORMS
-                    match (if fsiSession.IsGui then
-                               fsiWinFormsLoop.Value
-                           else
-                               None)
-                        with
+                    match (if fsiSession.IsGui then fsiWinFormsLoop.Value else None) with
                     | Some l -> (l :> IEventLoop).ScheduleRestart()
                     | _ ->
 #endif

From 5b1a3ae5d16fe93af1f8b055ab0be5bb5e47ff20 Mon Sep 17 00:00:00 2001
From: Don Syme 
Date: Tue, 31 May 2022 09:26:12 +0100
Subject: [PATCH 20/20] apply formatting (fix build) (#13209)

* preformat

* apply formatting
---
 src/FSharp.Core/.editorconfig             |   4 +
 src/FSharp.Core/QueryExtensions.fs        |  44 +++---
 src/FSharp.Core/array.fs                  |  30 +++-
 src/FSharp.Core/async.fs                  |  69 ++++++--
 src/FSharp.Core/collections.fs            |   3 +-
 src/FSharp.Core/event.fs                  |  11 +-
 src/FSharp.Core/fslib-extra-pervasives.fs |  15 +-
 src/FSharp.Core/list.fs                   |  30 +++-
 src/FSharp.Core/mailbox.fs                |   9 +-
 src/FSharp.Core/map.fs                    |  43 ++++-
 src/FSharp.Core/quotations.fs             |  77 ++++++---
 src/FSharp.Core/reflect.fs                | 182 +++++++++++++++-------
 src/FSharp.Core/resumable.fs              |  61 ++++++--
 src/FSharp.Core/seq.fs                    |  13 +-
 src/FSharp.Core/set.fs                    |  12 +-
 src/FSharp.Core/tasks.fs                  |  43 ++++-
 16 files changed, 484 insertions(+), 162 deletions(-)

diff --git a/src/FSharp.Core/.editorconfig b/src/FSharp.Core/.editorconfig
index 28218c4a78e..744ee2347a9 100644
--- a/src/FSharp.Core/.editorconfig
+++ b/src/FSharp.Core/.editorconfig
@@ -1,2 +1,6 @@
+# FSharp.Core uses more "conservative" settings - more lines etc.
+
 [*.fs]
+max_line_length=120
 fsharp_max_function_binding_width=1
+fsharp_max_if_then_else_short_width=40
\ No newline at end of file
diff --git a/src/FSharp.Core/QueryExtensions.fs b/src/FSharp.Core/QueryExtensions.fs
index c2e86390c94..f9d0ffd72fb 100644
--- a/src/FSharp.Core/QueryExtensions.fs
+++ b/src/FSharp.Core/QueryExtensions.fs
@@ -11,31 +11,33 @@ open Microsoft.FSharp.Quotations
 open Microsoft.FSharp.Quotations.DerivedPatterns
 open Microsoft.FSharp.Reflection
 open Microsoft.FSharp.Linq.RuntimeHelpers
+open System.Collections
+open System.Collections.Concurrent
 open System.Collections.Generic
 open System.Linq
 open System.Linq.Expressions
+open System.Reflection
 
 // ----------------------------------------------------------------------------
 
 /// A type used to reconstruct a grouping after applying a mutable->immutable mapping transformation
 /// on a result of a query.
 type Grouping<'K, 'T>(key: 'K, values: seq<'T>) =
-    interface System.Linq.IGrouping<'K, 'T> with
+    interface IGrouping<'K, 'T> with
         member _.Key = key
 
-    interface System.Collections.IEnumerable with
+    interface IEnumerable with
         member _.GetEnumerator() =
-            values.GetEnumerator() :> System.Collections.IEnumerator
+            values.GetEnumerator() :> IEnumerator
 
-    interface System.Collections.Generic.IEnumerable<'T> with
+    interface Generic.IEnumerable<'T> with
         member _.GetEnumerator() =
             values.GetEnumerator()
 
 module internal Adapters =
 
     let memoize f =
-        let d =
-            new System.Collections.Concurrent.ConcurrentDictionary(HashIdentity.Structural)
+        let d = new ConcurrentDictionary(HashIdentity.Structural)
 
         fun x -> d.GetOrAdd(x, (fun r -> f r))
 
@@ -46,13 +48,13 @@ module internal Adapters =
 
     let MemberInitializationHelperMeth =
         methodhandleof (fun x -> LeafExpressionConverter.MemberInitializationHelper x)
-        |> System.Reflection.MethodInfo.GetMethodFromHandle
-        :?> System.Reflection.MethodInfo
+        |> MethodInfo.GetMethodFromHandle
+        :?> MethodInfo
 
     let NewAnonymousObjectHelperMeth =
         methodhandleof (fun x -> LeafExpressionConverter.NewAnonymousObjectHelper x)
-        |> System.Reflection.MethodInfo.GetMethodFromHandle
-        :?> System.Reflection.MethodInfo
+        |> MethodInfo.GetMethodFromHandle
+        :?> MethodInfo
 
     // The following patterns are used to recognize object construction
     // using the 'new O(Prop1 = , Prop2 = )' syntax
@@ -73,7 +75,8 @@ module internal Adapters =
         let rec propSetList acc x =
             match x with
             // detect " v.X <- y"
-            | ((Patterns.PropertySet (Some (Patterns.Var var), _, _, _)) as p) :: xs when var = varArg -> propSetList (p :: acc) xs
+            | ((Patterns.PropertySet (Some (Patterns.Var var), _, _, _)) as p) :: xs when var = varArg ->
+                propSetList (p :: acc) xs
             // skip unit values
             | (Patterns.Value (v, _)) :: xs when v = null -> propSetList acc xs
             // detect "v"
@@ -190,8 +193,7 @@ module internal Adapters =
             let fields =
                 Microsoft.FSharp.Reflection.FSharpType.GetRecordFields(
                     typ,
-                    System.Reflection.BindingFlags.Public
-                    ||| System.Reflection.BindingFlags.NonPublic
+                    BindingFlags.Public ||| BindingFlags.NonPublic
                 )
 
             match fields |> Array.tryFindIndex (fun p -> p = propInfo) with
@@ -223,7 +225,8 @@ module internal Adapters =
 
             match convs with
             | x1 :: x2 :: x3 :: x4 :: x5 :: x6 :: x7 :: x8 :: tail ->
-                RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType [ x1; x2; x3; x4; x5; x6; x7; TupleConv(x8 :: tail) ])
+                let els = [ x1; x2; x3; x4; x5; x6; x7; TupleConv(x8 :: tail) ]
+                RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType els)
             | _ -> RewriteTupleType ty (List.map2 ConvImmutableTypeToMutableType convs)
         | RecordConv (_, convs) ->
             assert (isPartiallyImmutableRecord ty)
@@ -231,10 +234,10 @@ module internal Adapters =
             ConvImmutableTypeToMutableType (TupleConv convs) (FSharpType.MakeTupleType types)
         | GroupingConv (_keyTy, _elemTy, conv) ->
             assert ty.IsGenericType
-            assert (ty.GetGenericTypeDefinition() = typedefof>)
+            assert (ty.GetGenericTypeDefinition() = typedefof>)
             let keyt1 = ty.GetGenericArguments().[0]
             let valt1 = ty.GetGenericArguments().[1]
-            typedefof>.MakeGenericType [| keyt1; ConvImmutableTypeToMutableType conv valt1 |]
+            typedefof>.MakeGenericType [| keyt1; ConvImmutableTypeToMutableType conv valt1 |]
         | SeqConv conv ->
             assert ty.IsGenericType
             let isIQ = ty.GetGenericTypeDefinition() = typedefof>
@@ -256,14 +259,14 @@ module internal Adapters =
         let mhandle =
             (methodhandleof (fun x -> LeafExpressionConverter.NewAnonymousObjectHelper x))
 
-        let minfo =
-            (System.Reflection.MethodInfo.GetMethodFromHandle mhandle) :?> System.Reflection.MethodInfo
+        let minfo = (MethodInfo.GetMethodFromHandle mhandle) :?> MethodInfo
 
         let gmd = minfo.GetGenericMethodDefinition()
 
         (fun tm ->
             match tm with
-            | Patterns.Call (_obj, minfo2, _args) -> minfo2.IsGenericMethod && (gmd = minfo2.GetGenericMethodDefinition())
+            | Patterns.Call (_obj, minfo2, _args) ->
+                minfo2.IsGenericMethod && (gmd = minfo2.GetGenericMethodDefinition())
             | _ -> false)
 
     /// Cleanup the use of property-set object constructions in leaf expressions that form parts of F# queries.
@@ -305,7 +308,8 @@ module internal Adapters =
         // rewrite bottom-up
         let e =
             match e with
-            | ExprShape.ShapeCombination (comb, args) -> ExprShape.RebuildShapeCombination(comb, List.map SimplifyConsumingExpr args)
+            | ExprShape.ShapeCombination (comb, args) ->
+                ExprShape.RebuildShapeCombination(comb, List.map SimplifyConsumingExpr args)
             | ExprShape.ShapeLambda (v, body) -> Expr.Lambda(v, SimplifyConsumingExpr body)
             | ExprShape.ShapeVar _ -> e
 
diff --git a/src/FSharp.Core/array.fs b/src/FSharp.Core/array.fs
index 56465b1be47..1dff8103bda 100644
--- a/src/FSharp.Core/array.fs
+++ b/src/FSharp.Core/array.fs
@@ -244,7 +244,11 @@ module Array =
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let countByRefType (projection: 'T -> 'Key) (array: 'T[]) =
-        countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(projection t)) (fun sb -> sb.Value) array
+        countByImpl
+            RuntimeHelpers.StructBox<'Key>.Comparer
+            (fun t -> RuntimeHelpers.StructBox(projection t))
+            (fun sb -> sb.Value)
+            array
 
     []
     let countBy (projection: 'T -> 'Key) (array: 'T[]) =
@@ -570,7 +574,11 @@ module Array =
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let groupByRefType (keyf: 'T -> 'Key) (array: 'T[]) =
-        groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value) array
+        groupByImpl
+            RuntimeHelpers.StructBox<'Key>.Comparer
+            (fun t -> RuntimeHelpers.StructBox(keyf t))
+            (fun sb -> sb.Value)
+            array
 
     []
     let groupBy (projection: 'T -> 'Key) (array: 'T[]) =
@@ -822,7 +830,12 @@ module Array =
 
             count
 
-        let private createMask<'a> (f: 'a -> bool) (src: array<'a>) (maskArrayOut: byref>) (leftoverMaskOut: byref) =
+        let private createMask<'a>
+            (f: 'a -> bool)
+            (src: array<'a>)
+            (maskArrayOut: byref>)
+            (leftoverMaskOut: byref)
+            =
             let maskArrayLength = src.Length / 0x20
 
             // null when there are less than 32 items in src array.
@@ -1208,7 +1221,10 @@ module Array =
 
             if len % chunkSize <> 0 then
                 res.[chunkCount - 1] <-
-                    Microsoft.FSharp.Primitives.Basics.Array.subUnchecked ((chunkCount - 1) * chunkSize) (len % chunkSize) array
+                    Microsoft.FSharp.Primitives.Basics.Array.subUnchecked
+                        ((chunkCount - 1) * chunkSize)
+                        (len % chunkSize)
+                        array
 
             res
 
@@ -1776,7 +1792,11 @@ module Array =
 
             for j in 1 .. len - 1 do
                 if lenInner <> array.[j].Length then
-                    invalidArgDifferentArrayLength "array.[0]" lenInner (String.Format("array.[{0}]", j)) array.[j].Length
+                    invalidArgDifferentArrayLength
+                        "array.[0]"
+                        lenInner
+                        (String.Format("array.[{0}]", j))
+                        array.[j].Length
 
             let result: 'T[][] =
                 Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked lenInner
diff --git a/src/FSharp.Core/async.fs b/src/FSharp.Core/async.fs
index 977f071d496..46cd93a84ee 100644
--- a/src/FSharp.Core/async.fs
+++ b/src/FSharp.Core/async.fs
@@ -870,13 +870,15 @@ module AsyncPrimitives =
     ///   - Create Thread and call Start() with exception protection. We don't expect this
     ///     to fail but protect nevertheless.
     let CreateSwitchToNewThreadAsync () =
-        MakeAsyncWithCancelCheck(fun ctxt -> ctxt.ProtectCode(fun () -> ctxt.trampolineHolder.StartThreadWithTrampoline ctxt.cont))
+        MakeAsyncWithCancelCheck(fun ctxt ->
+            ctxt.ProtectCode(fun () -> ctxt.trampolineHolder.StartThreadWithTrampoline ctxt.cont))
 
     ///   - Initial cancellation check
     ///   - Call ThreadPool.QueueUserWorkItem with exception protection. We don't expect this
     ///     to fail but protect nevertheless.
     let CreateSwitchToThreadPoolAsync () =
-        MakeAsyncWithCancelCheck(fun ctxt -> ctxt.ProtectCode(fun () -> ctxt.trampolineHolder.QueueWorkItemWithTrampoline ctxt.cont))
+        MakeAsyncWithCancelCheck(fun ctxt ->
+            ctxt.ProtectCode(fun () -> ctxt.trampolineHolder.QueueWorkItemWithTrampoline ctxt.cont))
 
     /// Post back to the sync context regardless of which continuation is taken
     ///   - Call syncCtxt.Post with exception protection
@@ -917,7 +919,8 @@ module AsyncPrimitives =
             // This logic was added in F# 2.0 though is incorrect from the perspective of
             // how SynchronizationContext is meant to work. However the logic works for
             // mainline scenarios (WinForms/WPF) and for compatibility reasons we won't change it.
-            | _ when Object.Equals(syncCtxt, currentSyncCtxt) && thread.Equals Thread.CurrentThread -> executeImmediately ()
+            | _ when Object.Equals(syncCtxt, currentSyncCtxt) && thread.Equals Thread.CurrentThread ->
+                executeImmediately ()
             | _ -> trampolineHolder.PostOrQueueWithTrampoline syncCtxt action
 
         member _.PostOrQueueWithTrampoline res =
@@ -1074,7 +1077,7 @@ module AsyncPrimitives =
                 (typeof>)
                     .GetMethod("Invoke", BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance)
 
-            System.Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeMeth) :?> 'Delegate
+            Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeMeth) :?> 'Delegate
 
     []
     let QueueAsync cancellationToken cont econt ccont computation =
@@ -1429,7 +1432,9 @@ type Async =
     static member CancelCheck() =
         unitAsync
 
-    static member FromContinuations(callback: ('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) : Async<'T> =
+    static member FromContinuations
+        (callback: ('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit)
+        : Async<'T> =
         MakeAsyncWithCancelCheck(fun ctxt ->
             let mutable underCurrentThreadStack = true
             let mutable contToTailCall = None
@@ -1451,7 +1456,11 @@ type Async =
                     ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> cont x) |> unfake
 
             try
-                callback (once ctxt.cont, (fun exn -> once ctxt.econt (ExceptionDispatchInfo.RestoreOrCapture exn)), once ctxt.ccont)
+                callback (
+                    once ctxt.cont,
+                    (fun exn -> once ctxt.econt (ExceptionDispatchInfo.RestoreOrCapture exn)),
+                    once ctxt.ccont
+                )
             with exn ->
                 if not (latch.Enter()) then
                     invalidOp (SR.GetString(SR.controlContinuationInvokedMultipleTimes))
@@ -1518,7 +1527,12 @@ type Async =
     static member Parallel(computations: seq>, ?maxDegreeOfParallelism: int) =
         match maxDegreeOfParallelism with
         | Some x when x < 1 ->
-            raise (System.ArgumentException(String.Format(SR.GetString(SR.maxDegreeOfParallelismNotPositive), x), "maxDegreeOfParallelism"))
+            raise (
+                System.ArgumentException(
+                    String.Format(SR.GetString(SR.maxDegreeOfParallelismNotPositive), x),
+                    "maxDegreeOfParallelism"
+                )
+            )
         | _ -> ()
 
         MakeAsyncWithCancelCheck(fun ctxt ->
@@ -1547,8 +1561,10 @@ type Async =
 
                             match firstExn with
                             | None -> ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.cont results)
-                            | Some (Choice1Of2 exn) -> ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.econt exn)
-                            | Some (Choice2Of2 cexn) -> ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont cexn)
+                            | Some (Choice1Of2 exn) ->
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.econt exn)
+                            | Some (Choice2Of2 cexn) ->
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont cexn)
                         else
                             fake ()
 
@@ -1724,7 +1740,12 @@ type Async =
         let cancellationToken =
             defaultArg cancellationToken defaultCancellationTokenSource.Token
 
-        AsyncPrimitives.StartWithContinuations cancellationToken computation continuation exceptionContinuation cancellationContinuation
+        AsyncPrimitives.StartWithContinuations
+            cancellationToken
+            computation
+            continuation
+            exceptionContinuation
+            cancellationContinuation
 
     static member StartWithContinuations
         (
@@ -1781,7 +1802,8 @@ type Async =
                             DisposeCancellationRegistration ®istration
                             DisposeTimer &timer
 
-                            ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont (OperationCanceledException(ctxt.token)))
+                            ctxt.trampolineHolder.ExecuteWithTrampoline(fun () ->
+                                ctxt.ccont (OperationCanceledException(ctxt.token)))
                             |> unfake)
                 )
                 |> Some
@@ -1847,7 +1869,8 @@ type Async =
                                 UnregisterWaitHandle &rwh
 
                                 // Call the cancellation continuation
-                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () -> ctxt.ccont (OperationCanceledException(ctxt.token)))
+                                ctxt.trampolineHolder.ExecuteWithTrampoline(fun () ->
+                                    ctxt.ccont (OperationCanceledException(ctxt.token)))
                                 |> unfake)
                     )
                     |> Some
@@ -1929,7 +1952,11 @@ type Async =
                         let res = resultCell.GrabResult()
                         return res.Commit()
                     else
-                        let! ok = Async.AwaitWaitHandle(resultCell.GetWaitHandle(), ?millisecondsTimeout = millisecondsTimeout)
+                        let! ok =
+                            Async.AwaitWaitHandle(
+                                resultCell.GetWaitHandle(),
+                                ?millisecondsTimeout = millisecondsTimeout
+                            )
 
                         if ok then
                             let res = resultCell.GrabResult()
@@ -2009,10 +2036,18 @@ type Async =
         Async.FromBeginEnd((fun (iar, state) -> beginAction (arg, iar, state)), endAction, ?cancelAction = cancelAction)
 
     static member FromBeginEnd(arg1, arg2, beginAction, endAction, ?cancelAction) : Async<'T> =
-        Async.FromBeginEnd((fun (iar, state) -> beginAction (arg1, arg2, iar, state)), endAction, ?cancelAction = cancelAction)
+        Async.FromBeginEnd(
+            (fun (iar, state) -> beginAction (arg1, arg2, iar, state)),
+            endAction,
+            ?cancelAction = cancelAction
+        )
 
     static member FromBeginEnd(arg1, arg2, arg3, beginAction, endAction, ?cancelAction) : Async<'T> =
-        Async.FromBeginEnd((fun (iar, state) -> beginAction (arg1, arg2, arg3, iar, state)), endAction, ?cancelAction = cancelAction)
+        Async.FromBeginEnd(
+            (fun (iar, state) -> beginAction (arg1, arg2, arg3, iar, state)),
+            endAction,
+            ?cancelAction = cancelAction
+        )
 
     static member AsBeginEnd<'Arg, 'T>
         (computation: ('Arg -> Async<'T>))
@@ -2267,7 +2302,9 @@ module WebExtensions =
             )
             |> CreateTryWithFilterAsync(fun exn ->
                 match exn with
-                | :? System.Net.WebException as webExn when webExn.Status = System.Net.WebExceptionStatus.RequestCanceled && canceled ->
+                | :? System.Net.WebException as webExn when
+                    webExn.Status = System.Net.WebExceptionStatus.RequestCanceled && canceled
+                    ->
 
                     Some(CreateAsyncResultAsync(AsyncResult.Canceled(OperationCanceledException webExn.Message)))
                 | _ -> None)
diff --git a/src/FSharp.Core/collections.fs b/src/FSharp.Core/collections.fs
index 3094b9977e3..fe26460fbc5 100644
--- a/src/FSharp.Core/collections.fs
+++ b/src/FSharp.Core/collections.fs
@@ -50,7 +50,8 @@ module ComparisonIdentity =
     let inline Structural<'T when 'T: comparison> : IComparer<'T> =
         LanguagePrimitives.FastGenericComparer<'T>
 
-    let inline NonStructural<'T when 'T: (static member (<): 'T * 'T -> bool) and 'T: (static member (>): 'T * 'T -> bool)> : IComparer<'T> =
+    let inline NonStructural<'T
+        when 'T: (static member (<): 'T * 'T -> bool) and 'T: (static member (>): 'T * 'T -> bool)> : IComparer<'T> =
         { new IComparer<'T> with
             member _.Compare(x, y) =
                 NonStructuralComparison.compare x y
diff --git a/src/FSharp.Core/event.fs b/src/FSharp.Core/event.fs
index 108dd9b88eb..300434cdc97 100644
--- a/src/FSharp.Core/event.fs
+++ b/src/FSharp.Core/event.fs
@@ -8,8 +8,9 @@ open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
 open Microsoft.FSharp.Core.Operators
 open Microsoft.FSharp.Collections
 open Microsoft.FSharp.Control
-open System.Reflection
+open System
 open System.Diagnostics
+open System.Reflection
 
 module private Atomic =
     open System.Threading
@@ -79,7 +80,8 @@ type EventDelegee<'Args>(observer: System.IObserver<'Args>) =
 type EventWrapper<'Delegate, 'Args> = delegate of 'Delegate * obj * 'Args -> unit
 
 []
-type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct>() =
+type Event<'Delegate, 'Args
+    when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct>() =
 
     let mutable multicast: 'Delegate = Unchecked.defaultof<_>
 
@@ -98,7 +100,7 @@ type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate
     // CreateDelegate creates a delegate that is fast to invoke.
     static let invoker =
         if argTypes.Length = 1 then
-            (System.Delegate.CreateDelegate(typeof>, mi) :?> EventWrapper<'Delegate, 'Args>)
+            (Delegate.CreateDelegate(typeof>, mi) :?> EventWrapper<'Delegate, 'Args>)
         else
             null
 
@@ -152,8 +154,7 @@ type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate
               member e.Subscribe(observer) =
                   let obj = new EventDelegee<'Args>(observer)
 
-                  let h =
-                      System.Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeInfo) :?> 'Delegate
+                  let h = Delegate.CreateDelegate(typeof<'Delegate>, obj, invokeInfo) :?> 'Delegate
 
                   (e :?> IDelegateEvent<'Delegate>).AddHandler(h)
 
diff --git a/src/FSharp.Core/fslib-extra-pervasives.fs b/src/FSharp.Core/fslib-extra-pervasives.fs
index f7699ce66be..c78b71e48e0 100644
--- a/src/FSharp.Core/fslib-extra-pervasives.fs
+++ b/src/FSharp.Core/fslib-extra-pervasives.fs
@@ -37,12 +37,20 @@ module ExtraTopLevelOperators =
         dummyArray.Length |> ignore // pretty stupid way to avoid tail call, would be better if attribute existed, but this should be inlineable by the JIT
         result
 
-    let inline ICollection_Contains<'collection, 'item when 'collection :> ICollection<'item>> (collection: 'collection) (item: 'item) =
+    let inline ICollection_Contains<'collection, 'item when 'collection :> ICollection<'item>>
+        (collection: 'collection)
+        (item: 'item)
+        =
         collection.Contains item
 
     []
     [>)>]
-    type DictImpl<'SafeKey, 'Key, 'T>(t: Dictionary<'SafeKey, 'T>, makeSafeKey: 'Key -> 'SafeKey, getKey: 'SafeKey -> 'Key) =
+    type DictImpl<'SafeKey, 'Key, 'T>
+        (
+            t: Dictionary<'SafeKey, 'T>,
+            makeSafeKey: 'Key -> 'SafeKey,
+            getKey: 'SafeKey -> 'Key
+        ) =
 #if NETSTANDARD
         static let emptyEnumerator =
             (Array.empty> :> seq<_>).GetEnumerator()
@@ -512,7 +520,8 @@ type ITypeProvider =
 
     abstract GetStaticParameters: typeWithoutArguments: Type -> ParameterInfo[]
 
-    abstract ApplyStaticArguments: typeWithoutArguments: Type * typePathWithArguments: string[] * staticArguments: obj[] -> Type
+    abstract ApplyStaticArguments:
+        typeWithoutArguments: Type * typePathWithArguments: string[] * staticArguments: obj[] -> Type
 
     abstract GetInvokerExpression: syntheticMethodBase: MethodBase * parameters: Expr[] -> Expr
 
diff --git a/src/FSharp.Core/list.fs b/src/FSharp.Core/list.fs
index bbefce3003f..fbf8089610d 100644
--- a/src/FSharp.Core/list.fs
+++ b/src/FSharp.Core/list.fs
@@ -75,7 +75,11 @@ module List =
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let countByRefType (projection: 'T -> 'Key) (list: 'T list) =
-        countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(projection t)) (fun sb -> sb.Value) list
+        countByImpl
+            RuntimeHelpers.StructBox<'Key>.Comparer
+            (fun t -> RuntimeHelpers.StructBox(projection t))
+            (fun sb -> sb.Value)
+            list
 
     []
     let countBy (projection: 'T -> 'Key) (list: 'T list) =
@@ -358,7 +362,13 @@ module List =
             let arrn = arr.Length
             foldArraySubRight f arr 0 (arrn - 2) arr.[arrn - 1]
 
-    let scanArraySubRight<'T, 'State> (f: OptimizedClosures.FSharpFunc<'T, 'State, 'State>) (arr: _[]) start fin initState =
+    let scanArraySubRight<'T, 'State>
+        (f: OptimizedClosures.FSharpFunc<'T, 'State, 'State>)
+        (arr: _[])
+        start
+        fin
+        initState
+        =
         let mutable state = initState
         let mutable res = [ state ]
 
@@ -411,7 +421,8 @@ module List =
             | [], [] -> f.Invoke(h1, k1, state)
             | [ h2 ], [ k2 ] -> f.Invoke(h1, k1, f.Invoke(h2, k2, state))
             | [ h2; h3 ], [ k2; k3 ] -> f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, state)))
-            | [ h2; h3; h4 ], [ k2; k3; k4 ] -> f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, f.Invoke(h4, k4, state))))
+            | [ h2; h3; h4 ], [ k2; k3; k4 ] ->
+                f.Invoke(h1, k1, f.Invoke(h2, k2, f.Invoke(h3, k3, f.Invoke(h4, k4, state))))
             | _ -> foldBack2UsingArrays f list1 list2 state
         | [], xs2 -> invalidArgDifferentListLength "list1" "list2" xs2.Length
         | xs1, [] -> invalidArgDifferentListLength "list2" "list1" xs1.Length
@@ -528,7 +539,12 @@ module List =
     let where predicate list =
         Microsoft.FSharp.Primitives.Basics.List.filter predicate list
 
-    let inline groupByImpl (comparer: IEqualityComparer<'SafeKey>) (keyf: 'T -> 'SafeKey) (getKey: 'SafeKey -> 'Key) (list: 'T list) =
+    let inline groupByImpl
+        (comparer: IEqualityComparer<'SafeKey>)
+        (keyf: 'T -> 'SafeKey)
+        (getKey: 'SafeKey -> 'Key)
+        (list: 'T list)
+        =
         Microsoft.FSharp.Primitives.Basics.List.groupBy comparer keyf getKey list
 
     // We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
@@ -537,7 +553,11 @@ module List =
 
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let groupByRefType (keyf: 'T -> 'Key) (list: 'T list) =
-        groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value) list
+        groupByImpl
+            RuntimeHelpers.StructBox<'Key>.Comparer
+            (fun t -> RuntimeHelpers.StructBox(keyf t))
+            (fun sb -> sb.Value)
+            list
 
     []
     let groupBy (projection: 'T -> 'Key) (list: 'T list) =
diff --git a/src/FSharp.Core/mailbox.fs b/src/FSharp.Core/mailbox.fs
index 15e2b639f91..91c60d25cb3 100644
--- a/src/FSharp.Core/mailbox.fs
+++ b/src/FSharp.Core/mailbox.fs
@@ -203,7 +203,8 @@ type Mailbox<'Msg>(cancellationSupported: bool) =
 
                     match ok with
                     | Choice1Of2 true -> return! scan timeoutAsync timeoutCts
-                    | Choice1Of2 false -> return failwith "should not happen - waitOneNoTimeoutOrCancellation always returns true"
+                    | Choice1Of2 false ->
+                        return failwith "should not happen - waitOneNoTimeoutOrCancellation always returns true"
                     | Choice2Of2 () ->
                         lock syncRoot (fun () ->
                             // Cancel the outstanding wait for messages installed by waitOneWithCancellation
@@ -454,8 +455,10 @@ type MailboxProcessor<'Msg>(body, ?cancellationToken) =
             // Nothing to dispose, no wait handles used
             let resultCell = new ResultCell<_>()
 
-            let msg =
-                buildMessage (new AsyncReplyChannel<_>(fun reply -> resultCell.RegisterResult(reply, reuseThread = false) |> ignore))
+            let channel =
+                AsyncReplyChannel<_>(fun reply -> resultCell.RegisterResult(reply, reuseThread = false) |> ignore)
+
+            let msg = buildMessage channel
 
             mailbox.Post msg
             resultCell.AwaitResult_NoDirectCancelOrTimeout
diff --git a/src/FSharp.Core/map.fs b/src/FSharp.Core/map.fs
index 70f90a72ddd..0d510239f32 100644
--- a/src/FSharp.Core/map.fs
+++ b/src/FSharp.Core/map.fs
@@ -22,7 +22,14 @@ type internal MapTree<'Key, 'Value>(k: 'Key, v: 'Value, h: int) =
 []
 []
 []
-type internal MapTreeNode<'Key, 'Value>(k: 'Key, v: 'Value, left: MapTree<'Key, 'Value>, right: MapTree<'Key, 'Value>, h: int) =
+type internal MapTreeNode<'Key, 'Value>
+    (
+        k: 'Key,
+        v: 'Value,
+        left: MapTree<'Key, 'Value>,
+        right: MapTree<'Key, 'Value>,
+        h: int
+    ) =
     inherit MapTree<'Key, 'Value>(k, v, h)
     member _.Left = left
     member _.Right = right
@@ -68,7 +75,7 @@ module MapTree =
         traceCount <- traceCount + 1
 
         if traceCount % 1000000 = 0 then
-            System.Console.WriteLine(
+            Console.WriteLine(
                 "#MapOne = {0}, #MapNode = {1}, #Add = {2}, #Remove = {3}, #Unions = {4}, #Lookups = {5}, avMapTreeSizeOnNodeCreation = {6}, avMapSizeOnCreation = {7}, avMapSizeOnLookup = {8}",
                 numOnes,
                 numNodes,
@@ -81,7 +88,7 @@ module MapTree =
                 (totalSizeOnMapLookup / float numLookups)
             )
 
-            System.Console.WriteLine("#largestMapSize = {0}, largestMapStackTrace = {1}", largestMapSize, largestMapStackTrace)
+            Console.WriteLine("#largestMapSize = {0}, largestMapStackTrace = {1}", largestMapSize, largestMapStackTrace)
 
     let MapTree (k, v) =
         report ()
@@ -202,7 +209,12 @@ module MapTree =
         else
             (acc1, add comparer k v acc2)
 
-    let rec partitionAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc =
+    let rec partitionAux
+        (comparer: IComparer<'Key>)
+        (f: OptimizedClosures.FSharpFunc<_, _, _>)
+        (m: MapTree<'Key, 'Value>)
+        acc
+        =
         if isEmpty m then
             acc
         else if m.Height = 1 then
@@ -222,7 +234,12 @@ module MapTree =
         else
             acc
 
-    let rec filterAux (comparer: IComparer<'Key>) (f: OptimizedClosures.FSharpFunc<_, _, _>) (m: MapTree<'Key, 'Value>) acc =
+    let rec filterAux
+        (comparer: IComparer<'Key>)
+        (f: OptimizedClosures.FSharpFunc<_, _, _>)
+        (m: MapTree<'Key, 'Value>)
+        acc
+        =
         if isEmpty m then
             acc
         else if m.Height = 1 then
@@ -273,7 +290,12 @@ module MapTree =
                 else
                     rebalance mn.Left mn.Key mn.Value (remove comparer k mn.Right)
 
-    let rec change (comparer: IComparer<'Key>) k (u: 'Value option -> 'Value option) (m: MapTree<'Key, 'Value>) : MapTree<'Key, 'Value> =
+    let rec change
+        (comparer: IComparer<'Key>)
+        k
+        (u: 'Value option -> 'Value option)
+        (m: MapTree<'Key, 'Value>)
+        : MapTree<'Key, 'Value> =
         if isEmpty m then
             match u None with
             | None -> m
@@ -440,7 +462,14 @@ module MapTree =
     let fold f x m =
         foldOpt (OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt f) x m
 
-    let foldSectionOpt (comparer: IComparer<'Key>) lo hi (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x =
+    let foldSectionOpt
+        (comparer: IComparer<'Key>)
+        lo
+        hi
+        (f: OptimizedClosures.FSharpFunc<_, _, _, _>)
+        (m: MapTree<'Key, 'Value>)
+        x
+        =
         let rec foldFromTo (f: OptimizedClosures.FSharpFunc<_, _, _, _>) (m: MapTree<'Key, 'Value>) x =
             if isEmpty m then
                 x
diff --git a/src/FSharp.Core/quotations.fs b/src/FSharp.Core/quotations.fs
index 389ad12b79a..a309d9c1816 100644
--- a/src/FSharp.Core/quotations.fs
+++ b/src/FSharp.Core/quotations.fs
@@ -256,12 +256,16 @@ and [] Expr(
                     eq t1 (CombTerm(InstanceMethodCallOp(minfo2), obj2 :: args2WithoutWitnesses))
 
                 // We strip off StaticMethodCallWOp to ensure that CallWithWitness = Call
-                | CombTerm (StaticMethodCallWOp (minfo1, _minfoW1, nWitnesses1), args1), _ when nWitnesses1 <= args1.Length ->
+                | CombTerm (StaticMethodCallWOp (minfo1, _minfoW1, nWitnesses1), args1), _ when
+                    nWitnesses1 <= args1.Length
+                    ->
                     let argsWithoutWitnesses1 = List.skip nWitnesses1 args1
                     eq (CombTerm(StaticMethodCallOp(minfo1), argsWithoutWitnesses1)) t2
 
                 // We strip off StaticMethodCallWOp to ensure that CallWithWitness = Call
-                | _, CombTerm (StaticMethodCallWOp (minfo2, _minfoW2, nWitnesses2), args2) when nWitnesses2 <= args2.Length ->
+                | _, CombTerm (StaticMethodCallWOp (minfo2, _minfoW2, nWitnesses2), args2) when
+                    nWitnesses2 <= args2.Length
+                    ->
                     let argsWithoutWitnesses2 = List.skip nWitnesses2 args2
                     eq t1 (CombTerm(StaticMethodCallOp(minfo2), argsWithoutWitnesses2))
 
@@ -383,11 +387,14 @@ and [] Expr(
         | CombTerm (ValueOp (v, _, None), []) -> combL "Value" [ objL v ]
         | CombTerm (WithValueOp (v, _), [ defn ]) -> combL "WithValue" [ objL v; expr defn ]
 
-        | CombTerm (InstanceMethodCallOp (minfo), obj :: args) -> combL "Call" [ someL obj; minfoL minfo; listL (exprs args) ]
+        | CombTerm (InstanceMethodCallOp (minfo), obj :: args) ->
+            combL "Call" [ someL obj; minfoL minfo; listL (exprs args) ]
 
         | CombTerm (StaticMethodCallOp (minfo), args) -> combL "Call" [ noneL; minfoL minfo; listL (exprs args) ]
 
-        | CombTerm (InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj :: argsWithoutObj) when nWitnesses <= argsWithoutObj.Length ->
+        | CombTerm (InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj :: argsWithoutObj) when
+            nWitnesses <= argsWithoutObj.Length
+            ->
             let argsWithoutWitnesses = List.skip nWitnesses argsWithoutObj
             combL "Call" [ someL obj; minfoL minfo; listL (exprs argsWithoutWitnesses) ]
 
@@ -395,9 +402,11 @@ and [] Expr(
             let argsWithoutWitnesses = List.skip nWitnesses args
             combL "Call" [ noneL; minfoL minfo; listL (exprs argsWithoutWitnesses) ]
 
-        | CombTerm (InstancePropGetOp (pinfo), (obj :: args)) -> combL "PropertyGet" [ someL obj; pinfoL pinfo; listL (exprs args) ]
+        | CombTerm (InstancePropGetOp (pinfo), (obj :: args)) ->
+            combL "PropertyGet" [ someL obj; pinfoL pinfo; listL (exprs args) ]
         | CombTerm (StaticPropGetOp (pinfo), args) -> combL "PropertyGet" [ noneL; pinfoL pinfo; listL (exprs args) ]
-        | CombTerm (InstancePropSetOp (pinfo), (obj :: args)) -> combL "PropertySet" [ someL obj; pinfoL pinfo; listL (exprs args) ]
+        | CombTerm (InstancePropSetOp (pinfo), (obj :: args)) ->
+            combL "PropertySet" [ someL obj; pinfoL pinfo; listL (exprs args) ]
         | CombTerm (StaticPropSetOp (pinfo), args) -> combL "PropertySet" [ noneL; pinfoL pinfo; listL (exprs args) ]
         | CombTerm (InstanceFieldGetOp (finfo), [ obj ]) -> combL "FieldGet" [ someL obj; finfoL finfo ]
         | CombTerm (StaticFieldGetOp (finfo), []) -> combL "FieldGet" [ noneL; finfoL finfo ]
@@ -415,7 +424,8 @@ and [] Expr(
             combL "ForIntegerRangeLoop" [ varL v; expr e1; expr e2; expr e3 ]
         | CombTerm (WhileLoopOp, args) -> combL "WhileLoop" (exprs args)
         | CombTerm (TryFinallyOp, args) -> combL "TryFinally" (exprs args)
-        | CombTerm (TryWithOp, [ e1; Lambda (v1, e2); Lambda (v2, e3) ]) -> combL "TryWith" [ expr e1; varL v1; expr e2; varL v2; expr e3 ]
+        | CombTerm (TryWithOp, [ e1; Lambda (v1, e2); Lambda (v2, e3) ]) ->
+            combL "TryWith" [ expr e1; varL v1; expr e2; varL v2; expr e3 ]
         | CombTerm (SequentialOp, args) -> combL "Sequential" (exprs args)
 
         | CombTerm (NewDelegateOp ty, [ e ]) ->
@@ -764,7 +774,9 @@ module Patterns =
             Some(None, minfo, List.skip nWitnesses args)
 
         // A InstanceMethodCallWOp matches as if it were a InstanceMethodCallOp
-        | E (CombTerm (InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj :: argsWithoutObj)) when nWitnesses <= argsWithoutObj.Length ->
+        | E (CombTerm (InstanceMethodCallWOp (minfo, _minfoW, nWitnesses), obj :: argsWithoutObj)) when
+            nWitnesses <= argsWithoutObj.Length
+            ->
             let argsWithoutWitnesses = List.skip nWitnesses argsWithoutObj
             Some(Some obj, minfo, argsWithoutWitnesses)
 
@@ -854,7 +866,8 @@ module Patterns =
 
         match cases |> Array.tryFind (fun ucase -> ucase.Name = unionCaseName) with
         | Some case -> case
-        | _ -> invalidArg "unionCaseName" (String.Format(SR.GetString(SR.QmissingUnionCase), ty.FullName, unionCaseName))
+        | _ ->
+            invalidArg "unionCaseName" (String.Format(SR.GetString(SR.QmissingUnionCase), ty.FullName, unionCaseName))
 
     let getUnionCaseInfoField (unionCase: UnionCaseInfo, index) =
         let fields = unionCase.GetFields()
@@ -981,7 +994,8 @@ module Patterns =
             invalidArg "args" (SR.GetString(SR.QincorrectNumArgs))
 
         List.iter2
-            (fun (p: ParameterInfo) a -> checkTypesWeakSR p.ParameterType (typeOf a) "args" (SR.GetString(SR.QtmmInvalidParam)))
+            (fun (p: ParameterInfo) a ->
+                checkTypesWeakSR p.ParameterType (typeOf a) "args" (SR.GetString(SR.QtmmInvalidParam)))
             (paramInfos |> Array.toList)
             args
     // todo: shouldn't this be "strong" type check? sometimes?
@@ -1140,7 +1154,8 @@ module Patterns =
             invalidArg "args" (SR.GetString(SR.QincompatibleRecordLength))
 
         List.iter2
-            (fun (minfo: PropertyInfo) a -> checkTypesSR minfo.PropertyType (typeOf a) "recd" (SR.GetString(SR.QtmmIncorrectArgForRecord)))
+            (fun (minfo: PropertyInfo) a ->
+                checkTypesSR minfo.PropertyType (typeOf a) "recd" (SR.GetString(SR.QtmmIncorrectArgForRecord)))
             (Array.toList mems)
             args
 
@@ -1157,7 +1172,8 @@ module Patterns =
             invalidArg "args" (SR.GetString(SR.QunionNeedsDiffNumArgs))
 
         List.iter2
-            (fun (minfo: PropertyInfo) a -> checkTypesSR minfo.PropertyType (typeOf a) "sum" (SR.GetString(SR.QtmmIncorrectArgForUnion)))
+            (fun (minfo: PropertyInfo) a ->
+                checkTypesSR minfo.PropertyType (typeOf a) "sum" (SR.GetString(SR.QtmmIncorrectArgForUnion)))
             (Array.toList sargs)
             args
 
@@ -1894,7 +1910,10 @@ module Patterns =
             // For some reason we can get 'null' returned here even when a type with the right name exists... Hence search the slow way...
             match (assembly.GetTypes() |> Array.tryFind (fun a -> a.FullName = tcName)) with
             | Some ty -> ty
-            | None -> invalidArg "tcName" (String.Format(SR.GetString(SR.QfailedToBindTypeInAssembly), tcName, assembly.FullName))
+            | None ->
+                invalidArg
+                    "tcName"
+                    (String.Format(SR.GetString(SR.QfailedToBindTypeInAssembly), tcName, assembly.FullName))
         | ty -> ty
 
     let decodeNamedTy genericType tsR =
@@ -2151,7 +2170,8 @@ module Patterns =
     and instModuleDefnOp r tyargs _ =
         match r with
         | StaticMethodCallOp (minfo) -> StaticMethodCallOp(instMeth (minfo, tyargs))
-        | StaticMethodCallWOp (minfo, minfoW, n) -> StaticMethodCallWOp(instMeth (minfo, tyargs), instMeth (minfoW, tyargs), n)
+        | StaticMethodCallWOp (minfo, minfoW, n) ->
+            StaticMethodCallWOp(instMeth (minfo, tyargs), instMeth (minfoW, tyargs), n)
         // OK to throw away the tyargs here since this only non-generic values in modules get represented by static properties
         | x -> x
 
@@ -2313,7 +2333,8 @@ module Patterns =
             let h = l.[idx]
 
             match typeOf h with
-            | expected when expected <> ty -> invalidArg "receivedType" (String.Format(SR.GetString(SR.QtmmRaw), expected, ty))
+            | expected when expected <> ty ->
+                invalidArg "receivedType" (String.Format(SR.GetString(SR.QtmmRaw), expected, ty))
             | _ -> h
 
     let rec freeInExprAcc bvs acc (E t) =
@@ -2373,7 +2394,8 @@ module Patterns =
                 if v = bv then
                     let v2 = new Var(v.Name, v.Type)
                     let v2exp = E(VarTerm v2)
-                    EA(LambdaTerm(v2, substituteInExpr bvs (fun v -> if v = bv then Some v2exp else tmsubst v) b), e.CustomAttributes)
+                    let b2 = substituteInExpr bvs (fun v -> if v = bv then Some v2exp else tmsubst v) b
+                    EA(LambdaTerm(v2, b2), e.CustomAttributes)
                 else
                     reraise ()
         | HoleTerm _ -> e
@@ -2417,6 +2439,9 @@ module Patterns =
 
         decodedTopResources.Add((assem, resourceName), 0)
 
+    let isReflectedDefinitionResourceName (resourceName: string) =
+        resourceName.StartsWith(ReflectedDefinitionsResourceNameBase, StringComparison.Ordinal)
+
     /// Get the reflected definition at the given (always generic) instantiation
     let tryGetReflectedDefinition (methodBase: MethodBase, tyargs: Type[]) =
         checkNonNull "methodBase" methodBase
@@ -2448,7 +2473,7 @@ module Patterns =
                         [
                             for resourceName in resources do
                                 if
-                                    resourceName.StartsWith(ReflectedDefinitionsResourceNameBase, StringComparison.Ordinal)
+                                    isReflectedDefinitionResourceName resourceName
                                     && not (decodedTopResources.ContainsKey((assem, resourceName)))
                                 then
 
@@ -2459,7 +2484,8 @@ module Patterns =
                                         | x -> x)
                                         |> Array.tryPick (fun ca ->
                                             match ca with
-                                            | :? CompilationMappingAttribute as cma when cma.ResourceName = resourceName -> Some cma
+                                            | :? CompilationMappingAttribute as cma when cma.ResourceName = resourceName ->
+                                                Some cma
                                             | _ -> None)
 
                                     let resourceBytes = readToEnd (assem.GetManifestResourceStream resourceName)
@@ -2481,7 +2507,8 @@ module Patterns =
                             |> List.iter (fun (resourceName, defns) ->
                                 defns
                                 |> List.iter (fun (methodBase, exprBuilder) ->
-                                    reflectedDefinitionTable.[ReflectedDefinitionTableKey.GetKey methodBase] <- Entry exprBuilder)
+                                    reflectedDefinitionTable.[ReflectedDefinitionTableKey.GetKey methodBase] <-
+                                        Entry exprBuilder)
 
                                 decodedTopResources.[(assem, resourceName)] <- 0)
                         // we know it's in the table now, if it's ever going to be there
@@ -2581,7 +2608,14 @@ type Expr with
         checkNonNull "methodInfoWithWitnesses" methodInfoWithWitnesses
         mkStaticMethodCallW (methodInfo, methodInfoWithWitnesses, List.length witnesses, witnesses @ arguments)
 
-    static member CallWithWitnesses(obj: Expr, methodInfo: MethodInfo, methodInfoWithWitnesses: MethodInfo, witnesses, arguments) =
+    static member CallWithWitnesses
+        (
+            obj: Expr,
+            methodInfo: MethodInfo,
+            methodInfoWithWitnesses: MethodInfo,
+            witnesses,
+            arguments
+        ) =
         checkNonNull "methodInfo" methodInfo
         checkNonNull "methodInfoWithWitnesses" methodInfoWithWitnesses
         mkInstanceMethodCallW (obj, methodInfo, methodInfoWithWitnesses, List.length witnesses, witnesses @ arguments)
@@ -2988,7 +3022,8 @@ module ExprShape =
             | StaticMethodCallOp minfo, _ -> mkStaticMethodCall (minfo, arguments)
             | InstanceMethodCallOp minfo, obj :: args -> mkInstanceMethodCall (obj, minfo, args)
             | StaticMethodCallWOp (minfo, minfoW, n), _ -> mkStaticMethodCallW (minfo, minfoW, n, arguments)
-            | InstanceMethodCallWOp (minfo, minfoW, n), obj :: args -> mkInstanceMethodCallW (obj, minfo, minfoW, n, args)
+            | InstanceMethodCallWOp (minfo, minfoW, n), obj :: args ->
+                mkInstanceMethodCallW (obj, minfo, minfoW, n, args)
             | CoerceOp ty, [ arg ] -> mkCoerce (ty, arg)
             | NewArrayOp ty, _ -> mkNewArray (ty, arguments)
             | NewDelegateOp ty, [ arg ] -> mkNewDelegate (ty, arg)
diff --git a/src/FSharp.Core/reflect.fs b/src/FSharp.Core/reflect.fs
index 0a6fdd12e27..4bc089765fb 100644
--- a/src/FSharp.Core/reflect.fs
+++ b/src/FSharp.Core/reflect.fs
@@ -83,11 +83,11 @@ module internal Impl =
     let compilePropGetterFunc (prop: PropertyInfo) =
         let param = Expression.Parameter(typeof, "param")
 
+        let propExpr =
+            Expression.Property(Expression.Convert(param, prop.DeclaringType), prop)
+
         let expr =
-            Expression.Lambda>(
-                Expression.Convert(Expression.Property(Expression.Convert(param, prop.DeclaringType), prop), typeof),
-                param
-            )
+            Expression.Lambda>(Expression.Convert(propExpr, typeof), param)
 
         expr.Compile()
 
@@ -103,7 +103,8 @@ module internal Impl =
                     Expression.NewArrayInit(
                         typeof,
                         [
-                            for prop in props -> Expression.Convert(Expression.Property(typedParam, prop), typeof) :> Expression
+                            for prop in props ->
+                                Expression.Convert(Expression.Property(typedParam, prop), typeof) :> Expression
                         ]
                     )
                 ),
@@ -125,8 +126,8 @@ module internal Impl =
                             for paramIndex in 0 .. ctorParams.Length - 1 do
                                 let p = ctorParams.[paramIndex]
 
-                                Expression.Convert(Expression.ArrayAccess(paramArray, Expression.Constant paramIndex), p.ParameterType)
-                                :> Expression
+                                let accessExpr = Expression.ArrayAccess(paramArray, Expression.Constant paramIndex)
+                                Expression.Convert(accessExpr, p.ParameterType) :> Expression
                         ]
                     ),
                     typeof
@@ -149,8 +150,8 @@ module internal Impl =
                             for paramIndex in 0 .. methodParams.Length - 1 do
                                 let p = methodParams.[paramIndex]
 
-                                Expression.Convert(Expression.ArrayAccess(paramArray, Expression.Constant paramIndex), p.ParameterType)
-                                :> Expression
+                                let accessExpr = Expression.ArrayAccess(paramArray, Expression.Constant paramIndex)
+                                Expression.Convert(accessExpr, p.ParameterType) :> Expression
                         ]
                     ),
                     typeof
@@ -184,14 +185,20 @@ module internal Impl =
                         if paramIndex = tupleEncField then
                             constituentTuple genericArg elements (startIndex + paramIndex) :> Expression
                         else
-                            Expression.Convert(Expression.ArrayAccess(elements, Expression.Constant(startIndex + paramIndex)), genericArg)
+                            Expression.Convert(
+                                Expression.ArrayAccess(elements, Expression.Constant(startIndex + paramIndex)),
+                                genericArg
+                            )
                 ]
             )
 
         let elements = Expression.Parameter(typeof, "elements")
 
         let expr =
-            Expression.Lambda>(Expression.Convert(constituentTuple typ elements 0, typeof), elements)
+            Expression.Lambda>(
+                Expression.Convert(constituentTuple typ elements 0, typeof),
+                elements
+            )
 
         expr.Compile()
 
@@ -201,9 +208,11 @@ module internal Impl =
                 let elements =
                     match getTupleElementAccessors typ with
                     // typ is a struct tuple and its elements are accessed via fields
-                    | Choice1Of2 (fi: FieldInfo[]) -> fi |> Array.map (fun fi -> Expression.Field(tuple, fi), fi.FieldType)
+                    | Choice1Of2 (fi: FieldInfo[]) ->
+                        fi |> Array.map (fun fi -> Expression.Field(tuple, fi), fi.FieldType)
                     // typ is a class tuple and its elements are accessed via properties
-                    | Choice2Of2 (pi: PropertyInfo[]) -> pi |> Array.map (fun pi -> Expression.Property(tuple, pi), pi.PropertyType)
+                    | Choice2Of2 (pi: PropertyInfo[]) ->
+                        pi |> Array.map (fun pi -> Expression.Property(tuple, pi), pi.PropertyType)
 
                 for index, (element, elementType) in elements |> Array.indexed do
                     if index = tupleEncField then
@@ -241,14 +250,12 @@ module internal Impl =
                 Expression.Block(
                     [ outputArray ],
                     [
-                        yield
-                            Expression.Assign(
-                                outputArray,
-                                Expression.NewArrayBounds(typeof, Expression.Constant(outputLength tupleEncField typ))
-                            )
-                            :> Expression
+                        let arrayBounds =
+                            Expression.NewArrayBounds(typeof, Expression.Constant(outputLength tupleEncField typ))
+
+                        Expression.Assign(outputArray, arrayBounds) :> Expression
                         yield! writeTupleIntoArray typ (Expression.Convert(param, typ)) outputArray 0
-                        yield outputArray :> Expression
+                        outputArray :> Expression
                     ]
                 ),
                 param
@@ -263,7 +270,9 @@ module internal Impl =
         match attrs with
         | null
         | [||] -> None
-        | [| res |] -> let a = (res :?> CompilationMappingAttribute) in Some(a.SourceConstructFlags, a.SequenceNumber, a.VariantNumber)
+        | [| res |] ->
+            let a = (res :?> CompilationMappingAttribute)
+            Some(a.SourceConstructFlags, a.SequenceNumber, a.VariantNumber)
         | _ -> invalidOp (SR.GetString(SR.multipleCompilationMappings))
 
     let findCompilationMappingAttribute (attrs: obj[]) =
@@ -287,12 +296,24 @@ module internal Impl =
 
                     let flags =
                         match args.Count with
-                        | 1 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), 0, 0)
-                        | 2 -> ((let x = args.[0] in x.Value :?> SourceConstructFlags), (let x = args.[1] in x.Value :?> int), 0)
+                        | 1 ->
+                            let arg0 = args.[0]
+                            let v0 = arg0.Value :?> SourceConstructFlags
+                            (v0, 0, 0)
+                        | 2 ->
+                            let arg0 = args.[0]
+                            let v0 = arg0.Value :?> SourceConstructFlags
+                            let arg1 = args.[1]
+                            let v1 = arg1.Value :?> int
+                            (v0, v1, 0)
                         | 3 ->
-                            ((let x = args.[0] in x.Value :?> SourceConstructFlags),
-                             (let x = args.[1] in x.Value :?> int),
-                             (let x = args.[2] in x.Value :?> int))
+                            let arg0 = args.[0]
+                            let v0 = arg0.Value :?> SourceConstructFlags
+                            let arg1 = args.[1]
+                            let v1 = arg1.Value :?> int
+                            let arg2 = args.[2]
+                            let v2 = arg2.Value :?> int
+                            (v0, v1, v2)
                         | _ -> (enum 0, 0, 0)
 
                     res <- Some flags
@@ -579,12 +600,16 @@ module internal Impl =
                 "New" + constrname
 
         match typ.GetMethod(methname, BindingFlags.Static ||| bindingFlags) with
-        | null -> invalidOp (String.Format(SR.GetString(SR.constructorForUnionCaseNotFound), methname))
+        | null ->
+            let msg = String.Format(SR.GetString(SR.constructorForUnionCaseNotFound), methname)
+            invalidOp msg
         | meth -> meth
 
     let getUnionCaseConstructor (typ: Type, tag: int, bindingFlags) =
         let meth = getUnionCaseConstructorMethod (typ, tag, bindingFlags)
-        (fun args -> meth.Invoke(null, BindingFlags.Static ||| BindingFlags.InvokeMethod ||| bindingFlags, null, args, null))
+
+        (fun args ->
+            meth.Invoke(null, BindingFlags.Static ||| BindingFlags.InvokeMethod ||| bindingFlags, null, args, null))
 
     let getUnionCaseConstructorCompiled (typ: Type, tag: int, bindingFlags) =
         let meth = getUnionCaseConstructorMethod (typ, tag, bindingFlags)
@@ -595,9 +620,11 @@ module internal Impl =
 
         if not (isUnionType (unionType, bindingFlags)) then
             if isUnionType (unionType, bindingFlags ||| BindingFlags.NonPublic) then
-                invalidArg "unionType" (String.Format(SR.GetString(SR.privateUnionType), unionType.FullName))
+                let msg = String.Format(SR.GetString(SR.privateUnionType), unionType.FullName)
+                invalidArg "unionType" msg
             else
-                invalidArg "unionType" (String.Format(SR.GetString(SR.notAUnionType), unionType.FullName))
+                let msg = String.Format(SR.GetString(SR.notAUnionType), unionType.FullName)
+                invalidArg "unionType" msg
 
     //-----------------------------------------------------------------
     // TUPLE DECOMPILATION
@@ -718,7 +745,8 @@ module internal Impl =
 
     let rec getTupleTypeInfo (typ: Type) =
         if not (isTupleType typ) then
-            invalidArg "typ" (String.Format(SR.GetString(SR.notATupleType), typ.FullName))
+            let msg = String.Format(SR.GetString(SR.notATupleType), typ.FullName)
+            invalidArg "typ" msg
 
         let tyargs = typ.GetGenericArguments()
 
@@ -801,20 +829,35 @@ module internal Impl =
                 let fields =
                     typ.GetFields(instanceFieldFlags ||| BindingFlags.Public) |> orderTupleFields
 
-                typ.GetConstructor(BindingFlags.Public ||| BindingFlags.Instance, null, fields |> Array.map (fun fi -> fi.FieldType), null)
+                typ.GetConstructor(
+                    BindingFlags.Public ||| BindingFlags.Instance,
+                    null,
+                    fields |> Array.map (fun fi -> fi.FieldType),
+                    null
+                )
             else
                 let props = typ.GetProperties() |> orderTupleProperties
-                typ.GetConstructor(BindingFlags.Public ||| BindingFlags.Instance, null, props |> Array.map (fun p -> p.PropertyType), null)
+
+                typ.GetConstructor(
+                    BindingFlags.Public ||| BindingFlags.Instance,
+                    null,
+                    props |> Array.map (fun p -> p.PropertyType),
+                    null
+                )
 
         match ctor with
-        | null -> raise (ArgumentException(String.Format(SR.GetString(SR.invalidTupleTypeConstructorNotDefined), typ.FullName)))
+        | null ->
+            let msg = String.Format(SR.GetString(SR.invalidTupleTypeConstructorNotDefined))
+            raise (ArgumentException(msg, typ.FullName))
         | _ -> ()
 
         ctor
 
     let getTupleCtor (typ: Type) =
         let ctor = getTupleConstructorMethod typ
-        (fun (args: obj[]) -> ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public, null, args, null))
+
+        (fun (args: obj[]) ->
+            ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public, null, args, null))
 
     let getTupleElementAccessors (typ: Type) =
         if typ.IsValueType then
@@ -869,7 +912,10 @@ module internal Impl =
 
     let getTupleReaderInfo (typ: Type, index: int) =
         if index < 0 then
-            invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+            let msg =
+                String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString())
+
+            invalidArg "index" msg
 
         let get index =
             if typ.IsValueType then
@@ -878,7 +924,10 @@ module internal Impl =
                     |> orderTupleProperties
 
                 if index >= props.Length then
-                    invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+                    let msg =
+                        String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString())
+
+                    invalidArg "index" msg
 
                 props.[index]
             else
@@ -887,7 +936,10 @@ module internal Impl =
                     |> orderTupleProperties
 
                 if index >= props.Length then
-                    invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString()))
+                    let msg =
+                        String.Format(SR.GetString(SR.tupleIndexOutOfRange), typ.FullName, index.ToString())
+
+                    invalidArg "index" msg
 
                 props.[index]
 
@@ -944,19 +996,28 @@ module internal Impl =
         let props = fieldPropsOfRecordType (typ, bindingFlags)
 
         let ctor =
-            typ.GetConstructor(BindingFlags.Instance ||| bindingFlags, null, props |> Array.map (fun p -> p.PropertyType), null)
+            typ.GetConstructor(
+                BindingFlags.Instance ||| bindingFlags,
+                null,
+                props |> Array.map (fun p -> p.PropertyType),
+                null
+            )
 
         match ctor with
         | null ->
-            raise
-            <| ArgumentException(String.Format(SR.GetString(SR.invalidRecordTypeConstructorNotDefined), typ.FullName))
+            let msg =
+                String.Format(SR.GetString(SR.invalidRecordTypeConstructorNotDefined), typ.FullName)
+
+            raise (ArgumentException(msg))
         | _ -> ()
 
         ctor
 
     let getRecordConstructor (typ: Type, bindingFlags) =
         let ctor = getRecordConstructorMethod (typ, bindingFlags)
-        (fun (args: obj[]) -> ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| bindingFlags, null, args, null))
+
+        (fun (args: obj[]) ->
+            ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| bindingFlags, null, args, null))
 
     let getRecordConstructorCompiled (typ: Type, bindingFlags) =
         let ctor = getRecordConstructorMethod (typ, bindingFlags)
@@ -1001,24 +1062,31 @@ module internal Impl =
     let checkExnType (exceptionType, bindingFlags) =
         if not (isExceptionRepr (exceptionType, bindingFlags)) then
             if isExceptionRepr (exceptionType, bindingFlags ||| BindingFlags.NonPublic) then
-                invalidArg "exceptionType" (String.Format(SR.GetString(SR.privateExceptionType), exceptionType.FullName))
+                let msg =
+                    String.Format(SR.GetString(SR.privateExceptionType), exceptionType.FullName)
+
+                invalidArg "exceptionType" msg
             else
-                invalidArg "exceptionType" (String.Format(SR.GetString(SR.notAnExceptionType), exceptionType.FullName))
+                let msg = String.Format(SR.GetString(SR.notAnExceptionType), exceptionType.FullName)
+                invalidArg "exceptionType" msg
 
     let checkRecordType (argName, recordType, bindingFlags) =
         checkNonNull argName recordType
 
         if not (isRecordType (recordType, bindingFlags)) then
             if isRecordType (recordType, bindingFlags ||| BindingFlags.NonPublic) then
-                invalidArg argName (String.Format(SR.GetString(SR.privateRecordType), recordType.FullName))
+                let msg = String.Format(SR.GetString(SR.privateRecordType), recordType.FullName)
+                invalidArg argName msg
             else
-                invalidArg argName (String.Format(SR.GetString(SR.notARecordType), recordType.FullName))
+                let msg = String.Format(SR.GetString(SR.notARecordType), recordType.FullName)
+                invalidArg argName msg
 
     let checkTupleType (argName, (tupleType: Type)) =
         checkNonNull argName tupleType
 
         if not (isTupleType tupleType) then
-            invalidArg argName (String.Format(SR.GetString(SR.notATupleType), tupleType.FullName))
+            let msg = String.Format(SR.GetString(SR.notATupleType), tupleType.FullName)
+            invalidArg argName msg
 
 []
 type UnionCaseInfo(typ: System.Type, tag: int) =
@@ -1228,7 +1296,8 @@ type FSharpValue =
         checkNonNull "functionType" functionType
 
         if not (isFunctionType functionType) then
-            invalidArg "functionType" (String.Format(SR.GetString(SR.notAFunctionType), functionType.FullName))
+            let msg = String.Format(SR.GetString(SR.notAFunctionType), functionType.FullName)
+            invalidArg "functionType" msg
 
         checkNonNull "implementation" implementation
         let domain, range = getFunctionTypeInfo functionType
@@ -1248,7 +1317,8 @@ type FSharpValue =
         let typ = tuple.GetType()
 
         if not (isTupleType typ) then
-            invalidArg "tuple" (String.Format(SR.GetString(SR.notATupleType), tuple.GetType().FullName))
+            let msg = String.Format(SR.GetString(SR.notATupleType), tuple.GetType().FullName)
+            invalidArg "tuple" msg
 
         getTupleReader typ tuple
 
@@ -1257,12 +1327,16 @@ type FSharpValue =
         let typ = tuple.GetType()
 
         if not (isTupleType typ) then
-            invalidArg "tuple" (String.Format(SR.GetString(SR.notATupleType), tuple.GetType().FullName))
+            let msg = String.Format(SR.GetString(SR.notATupleType), tuple.GetType().FullName)
+            invalidArg "tuple" msg
 
         let fields = getTupleReader typ tuple
 
         if index < 0 || index >= fields.Length then
-            invalidArg "index" (String.Format(SR.GetString(SR.tupleIndexOutOfRange), tuple.GetType().FullName, index.ToString()))
+            let msg =
+                String.Format(SR.GetString(SR.tupleIndexOutOfRange), tuple.GetType().FullName, index.ToString())
+
+            invalidArg "index" msg
 
         fields.[index]
 
@@ -1421,7 +1495,11 @@ module FSharpReflectionExtensions =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
             FSharpValue.PreComputeUnionTagReader(unionType, bindingFlags)
 
-        static member PreComputeUnionReader(unionCase: UnionCaseInfo, ?allowAccessToPrivateRepresentation) : (obj -> obj[]) =
+        static member PreComputeUnionReader
+            (
+                unionCase: UnionCaseInfo,
+                ?allowAccessToPrivateRepresentation
+            ) : (obj -> obj[]) =
             let bindingFlags = getBindingFlags allowAccessToPrivateRepresentation
             FSharpValue.PreComputeUnionReader(unionCase, bindingFlags)
 
diff --git a/src/FSharp.Core/resumable.fs b/src/FSharp.Core/resumable.fs
index 3fd88f52c95..ee762d3bf17 100644
--- a/src/FSharp.Core/resumable.fs
+++ b/src/FSharp.Core/resumable.fs
@@ -92,12 +92,15 @@ module StateMachineHelpers =
 
     []
     let __resumableEntry () : int option =
-        failwith "__resumableEntry should always be guarded by __useResumableCode and only used in valid state machine implementations"
+        failwith
+            "__resumableEntry should always be guarded by __useResumableCode and only used in valid state machine implementations"
 
     []
     let __resumeAt<'T> (programLabel: int) : 'T =
         ignore programLabel
-        failwith "__resumeAt should always be guarded by __useResumableCode and only used in valid state machine implementations"
+
+        failwith
+            "__resumeAt should always be guarded by __useResumableCode and only used in valid state machine implementations"
 
     []
     let __stateMachine<'Data, 'Result>
@@ -108,7 +111,9 @@ module StateMachineHelpers =
         ignore moveNextMethod
         ignore setStateMachineMethod
         ignore afterCode
-        failwith "__stateMachine should always be guarded by __useResumableCode and only used in valid state machine implementations"
+
+        failwith
+            "__stateMachine should always be guarded by __useResumableCode and only used in valid state machine implementations"
 
 module ResumableCode =
 
@@ -167,13 +172,21 @@ module ResumableCode =
             else
                 CombineDynamic(&sm, code1, code2))
 
-    let rec WhileDynamic (sm: byref>, condition: unit -> bool, body: ResumableCode<'Data, unit>) : bool =
+    let rec WhileDynamic
+        (
+            sm: byref>,
+            condition: unit -> bool,
+            body: ResumableCode<'Data, unit>
+        ) : bool =
         if condition () then
             if body.Invoke(&sm) then
                 WhileDynamic(&sm, condition, body)
             else
                 let rf = GetResumptionFunc &sm
-                sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf)))
+
+                sm.ResumptionDynamicInfo.ResumptionFunc <-
+                    (ResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf)))
+
                 false
         else
             true
@@ -189,11 +202,18 @@ module ResumableCode =
             WhileDynamic(&sm, condition, body)
         else
             let rf = GetResumptionFunc &sm
-            sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf)))
+
+            sm.ResumptionDynamicInfo.ResumptionFunc <-
+                (ResumptionFunc<'Data>(fun sm -> WhileBodyDynamicAux(&sm, condition, body, rf)))
+
             false
 
     /// Builds a step that executes the body while the condition predicate is true.
-    let inline While ([] condition: unit -> bool, body: ResumableCode<'Data, unit>) : ResumableCode<'Data, unit> =
+    let inline While
+        (
+            [] condition: unit -> bool,
+            body: ResumableCode<'Data, unit>
+        ) : ResumableCode<'Data, unit> =
         ResumableCode<'Data, unit>(fun sm ->
             if __useResumableCode then
                 //-- RESUMABLE CODE START
@@ -225,7 +245,8 @@ module ResumableCode =
                 let rf = GetResumptionFunc &sm
 
                 sm.ResumptionDynamicInfo.ResumptionFunc <-
-                    (ResumptionFunc<'Data>(fun sm -> TryWithDynamic(&sm, ResumableCode<'Data, 'T>(fun sm -> rf.Invoke(&sm)), handler)))
+                    (ResumptionFunc<'Data>(fun sm ->
+                        TryWithDynamic(&sm, ResumableCode<'Data, 'T>(fun sm -> rf.Invoke(&sm)), handler)))
 
                 false
         with exn ->
@@ -233,7 +254,11 @@ module ResumableCode =
 
     /// Wraps a step in a try/with. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    let inline TryWith (body: ResumableCode<'Data, 'T>, catch: exn -> ResumableCode<'Data, 'T>) : ResumableCode<'Data, 'T> =
+    let inline TryWith
+        (
+            body: ResumableCode<'Data, 'T>,
+            catch: exn -> ResumableCode<'Data, 'T>
+        ) : ResumableCode<'Data, 'T> =
         ResumableCode<'Data, 'T>(fun sm ->
             if __useResumableCode then
                 //-- RESUMABLE CODE START
@@ -260,7 +285,12 @@ module ResumableCode =
             else
                 TryWithDynamic(&sm, body, catch))
 
-    let rec TryFinallyCompensateDynamic (sm: byref>, mf: ResumptionFunc<'Data>, savedExn: exn option) : bool =
+    let rec TryFinallyCompensateDynamic
+        (
+            sm: byref>,
+            mf: ResumptionFunc<'Data>,
+            savedExn: exn option
+        ) : bool =
         let mutable fin = false
         fin <- mf.Invoke(&sm)
 
@@ -271,7 +301,10 @@ module ResumableCode =
             | Some exn -> raise exn
         else
             let rf = GetResumptionFunc &sm
-            sm.ResumptionDynamicInfo.ResumptionFunc <- (ResumptionFunc<'Data>(fun sm -> TryFinallyCompensateDynamic(&sm, rf, savedExn)))
+
+            sm.ResumptionDynamicInfo.ResumptionFunc <-
+                (ResumptionFunc<'Data>(fun sm -> TryFinallyCompensateDynamic(&sm, rf, savedExn)))
+
             false
 
     let rec TryFinallyAsyncDynamic
@@ -328,7 +361,11 @@ module ResumableCode =
 
     /// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    let inline TryFinallyAsync (body: ResumableCode<'Data, 'T>, compensation: ResumableCode<'Data, unit>) : ResumableCode<'Data, 'T> =
+    let inline TryFinallyAsync
+        (
+            body: ResumableCode<'Data, 'T>,
+            compensation: ResumableCode<'Data, unit>
+        ) : ResumableCode<'Data, 'T> =
         ResumableCode<'Data, 'T>(fun sm ->
             if __useResumableCode then
                 //-- RESUMABLE CODE START
diff --git a/src/FSharp.Core/seq.fs b/src/FSharp.Core/seq.fs
index 7b867e661e4..b04ecfa3ec8 100644
--- a/src/FSharp.Core/seq.fs
+++ b/src/FSharp.Core/seq.fs
@@ -443,7 +443,8 @@ module Internal =
 
             static member Bind(g: Generator<'T>, cont) =
                 match g with
-                | :? GenerateThen<'T> as g -> GenerateThen<_>.Bind (g.Generator, (fun () -> GenerateThen<_>.Bind (g.Cont(), cont)))
+                | :? GenerateThen<'T> as g ->
+                    GenerateThen<_>.Bind (g.Generator, (fun () -> GenerateThen<_>.Bind (g.Cont(), cont)))
                 | g -> (new GenerateThen<'T>(g, cont) :> Generator<'T>)
 
         let bindG g cont =
@@ -1290,7 +1291,10 @@ module Seq =
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let groupByRefType (keyf: 'T -> 'Key) (seq: seq<'T>) =
         seq
-        |> groupByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value)
+        |> groupByImpl
+            RuntimeHelpers.StructBox<'Key>.Comparer
+            (fun t -> RuntimeHelpers.StructBox(keyf t))
+            (fun sb -> sb.Value)
 
     []
     let groupBy (projection: 'T -> 'Key) (source: seq<'T>) =
@@ -1402,7 +1406,10 @@ module Seq =
     // Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
     let countByRefType (keyf: 'T -> 'Key) (seq: seq<'T>) =
         seq
-        |> countByImpl RuntimeHelpers.StructBox<'Key>.Comparer (fun t -> RuntimeHelpers.StructBox(keyf t)) (fun sb -> sb.Value)
+        |> countByImpl
+            RuntimeHelpers.StructBox<'Key>.Comparer
+            (fun t -> RuntimeHelpers.StructBox(keyf t))
+            (fun sb -> sb.Value)
 
     []
     let countBy (projection: 'T -> 'Key) (source: seq<'T>) =
diff --git a/src/FSharp.Core/set.fs b/src/FSharp.Core/set.fs
index 1734dc1a54d..f04cc12433e 100644
--- a/src/FSharp.Core/set.fs
+++ b/src/FSharp.Core/set.fs
@@ -571,13 +571,21 @@ module internal SetTree =
                     compareStacks comparer (empty :: SetTree x1.Key :: t1) l2
                 else
                     let x1n = asNode x1
-                    compareStacks comparer (x1n.Left :: (SetTreeNode(x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1) l2
+
+                    compareStacks
+                        comparer
+                        (x1n.Left :: (SetTreeNode(x1n.Key, empty, x1n.Right, 0) :> SetTree<'T>) :: t1)
+                        l2
             | _, (x2 :: t2) when not (isEmpty x2) ->
                 if x2.Height = 1 then
                     compareStacks comparer l1 (empty :: SetTree x2.Key :: t2)
                 else
                     let x2n = asNode x2
-                    compareStacks comparer l1 (x2n.Left :: (SetTreeNode(x2n.Key, empty, x2n.Right, 0) :> SetTree<'T>) :: t2)
+
+                    compareStacks
+                        comparer
+                        l1
+                        (x2n.Left :: (SetTreeNode(x2n.Key, empty, x2n.Right, 0) :> SetTree<'T>) :: t2)
             | _ -> unexpectedstateInSetTreeCompareStacks ()
 
         match l1, l2 with
diff --git a/src/FSharp.Core/tasks.fs b/src/FSharp.Core/tasks.fs
index b348121149c..a4a0e22b575 100644
--- a/src/FSharp.Core/tasks.fs
+++ b/src/FSharp.Core/tasks.fs
@@ -58,21 +58,37 @@ type TaskBuilderBase() =
     /// Chains together a step with its following step.
     /// Note that this requires that the first step has no result.
     /// This prevents constructs like `task { return 1; return 2; }`.
-    member inline _.Combine(task1: TaskCode<'TOverall, unit>, task2: TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
+    member inline _.Combine
+        (
+            task1: TaskCode<'TOverall, unit>,
+            task2: TaskCode<'TOverall, 'T>
+        ) : TaskCode<'TOverall, 'T> =
         ResumableCode.Combine(task1, task2)
 
     /// Builds a step that executes the body while the condition predicate is true.
-    member inline _.While([] condition: unit -> bool, body: TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
+    member inline _.While
+        (
+            [] condition: unit -> bool,
+            body: TaskCode<'TOverall, unit>
+        ) : TaskCode<'TOverall, unit> =
         ResumableCode.While(condition, body)
 
     /// Wraps a step in a try/with. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    member inline _.TryWith(body: TaskCode<'TOverall, 'T>, catch: exn -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
+    member inline _.TryWith
+        (
+            body: TaskCode<'TOverall, 'T>,
+            catch: exn -> TaskCode<'TOverall, 'T>
+        ) : TaskCode<'TOverall, 'T> =
         ResumableCode.TryWith(body, catch)
 
     /// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
     /// to retrieve the step, and in the continuation of the step (if any).
-    member inline _.TryFinally(body: TaskCode<'TOverall, 'T>, [] compensation: unit -> unit) : TaskCode<'TOverall, 'T> =
+    member inline _.TryFinally
+        (
+            body: TaskCode<'TOverall, 'T>,
+            [] compensation: unit -> unit
+        ) : TaskCode<'TOverall, 'T> =
         ResumableCode.TryFinally(
             body,
             ResumableCode<_, _>(fun _sm ->
@@ -84,7 +100,11 @@ type TaskBuilderBase() =
         ResumableCode.For(sequence, body)
 
 #if NETSTANDARD2_1
-    member inline internal this.TryFinallyAsync(body: TaskCode<'TOverall, 'T>, compensation: unit -> ValueTask) : TaskCode<'TOverall, 'T> =
+    member inline internal this.TryFinallyAsync
+        (
+            body: TaskCode<'TOverall, 'T>,
+            compensation: unit -> ValueTask
+        ) : TaskCode<'TOverall, 'T> =
         ResumableCode.TryFinallyAsync(
             body,
             ResumableCode<_, _>(fun sm ->
@@ -344,7 +364,11 @@ module LowPriority =
                         sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
                         false
                 else
-                    TaskBuilderBase.BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall>(&sm, task, continuation)
+                    TaskBuilderBase.BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall>(
+                        &sm,
+                        task,
+                        continuation
+                    )
             //-- RESUMABLE CODE END
             )
 
@@ -370,7 +394,12 @@ module HighPriority =
     // High priority extensions
     type TaskBuilderBase with
 
-        static member BindDynamic(sm: byref<_>, task: Task<'TResult1>, continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)) : bool =
+        static member BindDynamic
+            (
+                sm: byref<_>,
+                task: Task<'TResult1>,
+                continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
+            ) : bool =
             let mutable awaiter = task.GetAwaiter()
 
             let cont =