Skip to content

Commit

Permalink
#55 Incorrect source generated
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed May 20, 2024
1 parent 88f5808 commit 98709f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
31 changes: 4 additions & 27 deletions src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,8 @@ public void Build(BuildContext ctx, in Block block)
}
}

private static bool IsNewInstanceRequired(Variable variable)
{
if (variable.Node.Lifetime == Lifetime.Transient)
{
return true;
}

if (variable.Current.HasCycle)
{
return false;
}

var owners = variable
.GetPath()
.Skip(1)
.TakeWhile(i => !i.Current.IsLazy)
.OfType<Block>()
.ToArray();

if (variable.Info.Owners.Intersect(owners).Any())
{
return false;
}

variable.Info.Owners.Add(owners.FirstOrDefault());
return true;
}
private static bool IsNewInstanceRequired(Variable variable) =>
variable.Node.Lifetime == Lifetime.Transient
|| !variable.Current.HasCycle
&& variable.Info.ParentBlocks.Add(variable.ParentBlock);
}
12 changes: 6 additions & 6 deletions src/Pure.DI.Core/Core/Code/VariableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
internal class VariableInfo
{
private readonly HashSet<int> _perBlockRefCounts = [];
public readonly HashSet<Block> Owners = [];
public readonly HashSet<Block> Created = [];
private readonly HashSet<Block> _created = [];
public readonly HashSet<Block> ParentBlocks = [];
public bool HasLocalMethod;

public int RefCount { get; private set; } = 1;
Expand All @@ -19,19 +19,19 @@ public void AddRef(Block parentBlock)

public bool MarkAsCreated(Block block)
{
return Created.Add(block);
return _created.Add(block);
}

public bool IsCreated(Block block)
{
return Created.Contains(block);
return _created.Contains(block);
}

public void Reset()
{
_perBlockRefCounts.Clear();
Owners.Clear();
Created.Clear();
ParentBlocks.Clear();
_created.Clear();
RefCount = 1;
HasLocalMethod = false;
}
Expand Down

0 comments on commit 98709f1

Please sign in to comment.