@@ -101,7 +101,7 @@ public void Build(BuildContext ctx, in DpFactory factory)
101101 var argType = typeConstructor . ConstructReversed ( typeArg ) ;
102102 if ( binding . TypeConstructor is { } bindingTypeConstructor )
103103 {
104- argType = bindingTypeConstructor . Construct ( setup , binding . SemanticModel . Compilation , argType ) ;
104+ argType = bindingTypeConstructor . Construct ( setup , argType ) ;
105105 }
106106
107107 var typeName = symbolNames . GetGlobalName ( argType ) ;
@@ -224,14 +224,16 @@ public void Build(BuildContext ctx, in DpFactory factory)
224224 . GetEnumerator ( ) ;
225225
226226 var initializationArgsEnum = initializationArgs . Select ( i => i . Current ) . GetEnumerator ( ) ;
227+ ctx = ctx with { LockIsRequired = lockIsRequired , Level = level } ;
227228 var injectionsCtx = ctx ;
228229 if ( variable . IsLazy && variable . Node . Accumulators . Count > 0 )
229230 {
230231 injectionsCtx = injectionsCtx with
231232 {
232- Accumulators = injectionsCtx . Accumulators . AddRange (
233- variable . Node . Accumulators
234- . Select ( accumulator => accumulator with { IsDeclared = false } ) )
233+ Accumulators = injectionsCtx . Accumulators
234+ . AsEnumerable ( )
235+ . Select ( i => i with { IsDeclared = i . IsRoot && i . IsDeclared } )
236+ . ToImmutableArray ( )
235237 } ;
236238 }
237239
@@ -272,7 +274,12 @@ public void Build(BuildContext ctx, in DpFactory factory)
272274 code . AppendLine ( $ "{ variableNameProvider . GetOverrideVariableName ( @override . Source ) } = { @override . Source . ValueExpression } ;") ;
273275 }
274276
275- ctx . StatementBuilder . Build ( injectionsCtx with { Level = level , Variable = argument . Current , LockIsRequired = lockIsRequired , IsFactory = true } , argument ) ;
277+ foreach ( var argStatement in GetArgsStatements ( argument ) )
278+ {
279+ ctx . StatementBuilder . Build ( ctx with { Variable = argStatement . Current } , argStatement ) ;
280+ }
281+
282+ ctx . StatementBuilder . Build ( injectionsCtx with { Variable = argument . Current } , argument ) ;
276283 code . AppendLine ( $ "{ ( injection . DeclarationRequired ? $ "{ typeResolver . Resolve ( ctx . DependencyGraph . Source , argument . Current . Injection . Type ) } " : "" ) } { injection . VariableName } = { ctx . BuildTools . OnInjected ( ctx , argument . Current ) } ;") ;
277284 }
278285
@@ -287,6 +294,12 @@ public void Build(BuildContext ctx, in DpFactory factory)
287294 code . AppendLine ( $ "{ variableNameProvider . GetOverrideVariableName ( @override . Source ) } = { @override . Source . ValueExpression } ;") ;
288295 }
289296
297+ foreach ( var argument in initializationArgs )
298+ foreach ( var argStatement in GetArgsStatements ( argument ) )
299+ {
300+ ctx . StatementBuilder . Build ( ctx with { Variable = argStatement . Current } , argStatement ) ;
301+ }
302+
290303 var initializersWalker = initializersWalkerFactory ( ) . Ininitialize ( initialization . VariableName , initializationArgsEnum ) ;
291304 initializersWalker . VisitInitializer ( injectionsCtx , initializer ) ;
292305 continue ;
@@ -313,4 +326,27 @@ public void Build(BuildContext ctx, in DpFactory factory)
313326
314327 ctx . Code . AppendLines ( ctx . BuildTools . OnCreated ( ctx , variable ) ) ;
315328 }
329+
330+ private static IEnumerable < IStatement > GetArgsStatements ( IStatement statement )
331+ {
332+ var result = new Stack < IStatement > ( ) ;
333+ var statements = new Stack < IStatement > ( ) ;
334+ statements . Push ( statement ) ;
335+ while ( statements . TryPop ( out var nextStatement ) )
336+ {
337+ var variable = nextStatement . Current ;
338+ if ( variable . HasCycledReference && variable . Node . Lifetime == Lifetime . PerBlock )
339+ {
340+ continue ;
341+ }
342+
343+ result . Push ( nextStatement ) ;
344+ foreach ( var arg in variable . Args )
345+ {
346+ statements . Push ( arg ) ;
347+ }
348+ }
349+
350+ return result ;
351+ }
316352}
0 commit comments