diff --git a/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs b/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs index 8b27cf293..242c83f84 100644 --- a/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs @@ -14,15 +14,9 @@ public void Build(BuildContext ctx, in Block block) } var info = block.Current.Info; - if (info.HasCode) - { - ctx.Code.AppendLines(info.Code.Lines); - return; - } - + var code = new LinesBuilder(); try { - var code = info.Code; var level = ctx.Level; var isThreadSafe = ctx.DependencyGraph.Source.Hints.IsThreadSafeEnabled; var lockIsRequired = ctx.LockIsRequired ?? isThreadSafe; @@ -118,9 +112,9 @@ variable.Node.Lifetime is Lifetime.Singleton or Lifetime.Scoped } finally { - info.HasCode = true; if (block.Parent is not null - && info is { PerBlockRefCount: > 1, Code.Lines.Count: > 11 }) + && info is { PerBlockRefCount: > 1 } + && code.Count > 11) { var localFunctionsCode = ctx.LocalFunctionsCode; var localMethodName = $"{Names.EnsureExistsMethodNamePrefix}_{variable.VariableDeclarationName}".Replace("__", "_"); @@ -133,15 +127,15 @@ variable.Node.Lifetime is Lifetime.Singleton or Lifetime.Scoped localFunctionsCode.AppendLine("{"); using (localFunctionsCode.Indent()) { - localFunctionsCode.AppendLines(info.Code.Lines); + localFunctionsCode.AppendLines(code.Lines); } localFunctionsCode.AppendLine("}"); - info.Code = new LinesBuilder(); - info.Code.AppendLine($"{localMethodName}();"); + code = new LinesBuilder(); + code.AppendLine($"{localMethodName}();"); } - ctx.Code.AppendLines(info.Code.Lines); + ctx.Code.AppendLines(code.Lines); } } diff --git a/src/Pure.DI.Core/Core/Code/BuildTools.cs b/src/Pure.DI.Core/Core/Code/BuildTools.cs index 88d938133..8b0813e39 100644 --- a/src/Pure.DI.Core/Core/Code/BuildTools.cs +++ b/src/Pure.DI.Core/Core/Code/BuildTools.cs @@ -23,7 +23,7 @@ public string OnInjected(BuildContext ctx, Variable variable) var variableCode = variable.VariableCode; if (variableCode == variable.VariableName) { - var skipNotNullCheck = (variable.HasCycle || variable.Info.HasCode) + var skipNotNullCheck = (variable.HasCycle || variable.IsDeclared) && variable.InstanceType.IsReferenceType && ctx.DependencyGraph.Source.SemanticModel.Compilation.Options.NullableContextOptions != NullableContextOptions.Disable; diff --git a/src/Pure.DI.Core/Core/Code/VariableInfo.cs b/src/Pure.DI.Core/Core/Code/VariableInfo.cs index 620fa18e5..7bd7ba719 100644 --- a/src/Pure.DI.Core/Core/Code/VariableInfo.cs +++ b/src/Pure.DI.Core/Core/Code/VariableInfo.cs @@ -5,8 +5,6 @@ internal class VariableInfo private readonly HashSet _perBlockRefCounts = []; public readonly HashSet Owners = []; public bool IsCreated; - public bool HasCode; - public LinesBuilder Code = new(); public int RefCount { get; private set; } = 1; @@ -24,7 +22,5 @@ public void Reset() Owners.Clear(); RefCount = 1; IsCreated = false; - HasCode = false; - Code = new LinesBuilder(); } } \ No newline at end of file