Skip to content

Commit 4fad500

Browse files
#140 Incorrect generated code for the PerBlock lifetime: a local or parameter named ... cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
1 parent 2c23cc6 commit 4fad500

3 files changed

Lines changed: 1932 additions & 2 deletions

File tree

src/Pure.DI.Core/Core/Code/VarsMap.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,41 @@ public IDisposable Lazy(Var var, Lines lines)
176176
public IDisposable Block(Var var, Lines lines)
177177
{
178178
var scope = EnterScope(var.AbstractNode.BindingId);
179+
180+
// Per-block variables should be isolated between blocks.
181+
List<KeyValuePair<int, Var>>? removed = null;
182+
foreach (var bindingId in _perBlockBindingIds)
183+
{
184+
if (!_map.TryGetValue(bindingId, out var perBlockVar))
185+
{
186+
continue;
187+
}
188+
189+
#if DEBUG
190+
lines.AppendLine($"// {perBlockVar.Declaration.Name}: remove ({nameof(Block)})");
191+
#endif
192+
(removed ??= new List<KeyValuePair<int, Var>>(_perBlockBindingIds.Count)).Add(new KeyValuePair<int, Var>(bindingId, perBlockVar));
193+
_map.Remove(bindingId);
194+
}
195+
179196
return Disposables.Create(() => {
180197
_suppressedTrackingCount++;
181198
try
182199
{
183200
RemoveNewNonPersistentVars(var, scope, lines, nameof(Block));
184201
RestoreState(scope, lines, nameof(Block), false);
202+
if (removed is null)
203+
{
204+
return;
205+
}
206+
207+
foreach (var item in removed)
208+
{
209+
#if DEBUG
210+
lines.AppendLine($"// {item.Value.Declaration.Name}: rollback ({nameof(Block)})");
211+
#endif
212+
_map[item.Key] = item.Value;
213+
}
185214
}
186215
finally
187216
{

0 commit comments

Comments
 (0)