@@ -826,8 +826,15 @@ and IlxGenEnv =
826826
827827 /// Are we under the scope of a try, catch or finally? If so we can't tailcall. SEH = structured exception handling
828828 withinSEH: bool
829+
830+ /// Are we inside of a recursive let binding, while loop, or a for loop?
831+ isInLoop: bool
829832 }
830833
834+ let SetIsInLoop isInLoop eenv =
835+ if eenv.isInLoop = isInLoop then eenv
836+ else { eenv with isInLoop = isInLoop }
837+
831838let ReplaceTyenv tyenv ( eenv : IlxGenEnv ) = { eenv with tyenv = tyenv }
832839
833840let EnvForTypars tps eenv = { eenv with tyenv = TypeReprEnv.ForTypars tps }
@@ -3369,6 +3376,7 @@ and GenTryFinally cenv cgbuf eenv (bodyExpr, handlerExpr, m, resty, spTry, spFin
33693376//--------------------------------------------------------------------------
33703377
33713378and GenForLoop cenv cgbuf eenv ( spFor , v , e1 , dir , e2 , loopBody , m ) sequel =
3379+ let eenv = SetIsInLoop true eenv
33723380 let g = cenv.g
33733381
33743382 // The JIT/NGen eliminate array-bounds checks for C# loops of form:
@@ -3459,6 +3467,7 @@ and GenForLoop cenv cgbuf eenv (spFor, v, e1, dir, e2, loopBody, m) sequel =
34593467//--------------------------------------------------------------------------
34603468
34613469and GenWhileLoop cenv cgbuf eenv ( spWhile , e1 , e2 , m ) sequel =
3470+ let eenv = SetIsInLoop true eenv
34623471 let finish = CG.GenerateDelayMark cgbuf " while_finish"
34633472 let startTest = CG.GenerateMark cgbuf " startTest"
34643473
@@ -5083,6 +5092,7 @@ and GenLetRecFixup cenv cgbuf eenv (ilxCloSpec: IlxClosureSpec, e, ilField: ILFi
50835092
50845093/// Generate letrec bindings
50855094and GenLetRecBindings cenv ( cgbuf : CodeGenBuffer ) eenv ( allBinds : Bindings , m ) =
5095+ let eenv = SetIsInLoop true eenv
50865096 // Fix up recursion for non-toplevel recursive bindings
50875097 let bindsPossiblyRequiringFixup =
50885098 allBinds |> List.filter ( fun b ->
@@ -5324,8 +5334,8 @@ and GenBindingAfterSequencePoint cenv cgbuf eenv sp (TBind(vspec, rhsExpr, _)) s
53245334 | _ ->
53255335 let storage = StorageForVal cenv.g m vspec eenv
53265336 match storage, rhsExpr with
5327- // locals are zero-init, no need to initialize them
5328- | Local (_, realloc, _), Expr.Const ( Const.Zero, _, _) when not realloc ->
5337+ // locals are zero-init, no need to initialize them, except if you are in a loop and the local is mutable.
5338+ | Local (_, realloc, _), Expr.Const ( Const.Zero, _, _) when not realloc && not ( eenv.isInLoop && vspec.IsMutable ) ->
53295339 CommitStartScope cgbuf startScopeMarkOpt
53305340 | _ ->
53315341 GenBindingRhs cenv cgbuf eenv SPSuppress vspec rhsExpr
@@ -7463,7 +7473,8 @@ let GetEmptyIlxGenEnv (ilg: ILGlobals) ccu =
74637473 liveLocals= IntMap.empty()
74647474 innerVals = []
74657475 sigToImplRemapInfo = [] (* "module remap info" *)
7466- withinSEH = false }
7476+ withinSEH = false
7477+ isInLoop = false }
74677478
74687479type IlxGenResults =
74697480 { ilTypeDefs: ILTypeDef list
0 commit comments