Detect and error on partially-lazy dependency cycles#220
Merged
Conversation
A partially-lazy cycle (constant + Instantiator hops) generates code with forward variable references that Swift rejects. Previously this was silently allowed. Now emits partiallyLazyDependencyCycleDetected with guidance to make all hops lazy. Moved the broken production test to the error test file. Created SafeDIToolMockGenerationErrorTests.swift for mock error tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #220 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 40 40
Lines 6055 6127 +72
=========================================
+ Hits 6055 6127 +72
🚀 New features to boost your workflow:
|
Adds 6 additional error tests to SafeDIToolMockGenerationErrorTests: - Constant dependency cycle - Unfulfillable @Instantiated property - Unfulfillable @received property - Self-referencing instantiation - @forwarded property on non-Instantiator child - @received Instantiator dependency cycle Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The partially-lazy cycle check was only reached through root validation. Mock-only types (generateMock: true without isRoot) bypassed it. Now adds a separate validateMockScopeForCycles pass in generateMockCode that checks for constant and partially-lazy cycles without also rejecting unfulfillable properties (which are expected in mock-only types as they become required mock parameters). The mock error test no longer includes a root Parent — it exercises the mock-only validation path directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replaces the separate validateMockScopeForCycles function with a cyclesOnly parameter on the shared validatePropertiesAreFulfillable. When cyclesOnly is true, unfulfillable property collection is skipped and mock-only types are added as entry points. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
dfed
commented
Apr 6, 2026
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The pre-promotion scope map doesn't show cycles that appear after @received dependencies are promoted to the mock root. Now validates each mock root scope after createMockRootScopeGenerator builds it, catching received-Instantiator cycles that only exist in the promoted mock tree. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…cation duplication Three copies of the cycle type classification (constant vs partially-lazy vs received-Instantiator) are replaced by a single throwIfInvalidCycle static method. Fully-lazy cycles (valid) fall through without throwing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
dfed
added a commit
that referenced
this pull request
Apr 6, 2026
This function was added by #220 on main after the original PR branch diverged, so it retained OrderedSet references that the auto-merge did not convert. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Detects and errors on dependency cycles that would generate uncompilable Swift code, fixing a latent bug in both production and mock code generation. Also creates a dedicated mock error test file and adds comprehensive mock error test coverage.
Partially-lazy cycle detection
A partially-lazy cycle (mix of constant and
Instantiatorhops, e.g. A → B → Instantiator<C> → C → A) generates code with forward variable references that Swift rejects:closure captures 'X' before it is declared. This was a latent production bug — the existing test only did string comparison and never compiled the output.New error case
partiallyLazyDependencyCycleDetectedwith guidance to make all hops lazy:Two-phase mock cycle validation
Mock-only types (
generateMock: truewithoutisRoot) need their own cycle validation because:Pre-promotion validation (
cyclesOnly: trueon sharedvalidatePropertiesAreFulfillable): Catches cycles visible in the pre-promotion scope map. UsescyclesOnlyflag to skip unfulfillable property checks (mock-only types have unfulfillable received properties by design — they become mock parameters). Non-root mock types are added as entry points alongside root types.Post-promotion validation (
validateMockRootScopeForCycles): Runs on each mock root scope AFTERcreateMockRootScopeGeneratorpromotes@Receiveddependencies. Catches cycles that only become visible after promotion (e.g., B@ReceivesInstantiator<A> where A instantiates B — promotion creates a cycle in the mock root scope that isn't visible pre-promotion).No code duplication
The pre-promotion cycle check reuses the shared
validatePropertiesAreFulfillablewith acyclesOnly: Boolparameter (no default — callers must be explicit). The post-promotion check is a separatevalidateMockRootScopeForCyclesthat walks the promoted scope tree checking bothpropertiesToGenerateanddependencies.New
SafeDIToolMockGenerationErrorTests.swiftDedicated file for mock generation error tests (the mock test file was 12,725 lines). Contains 9 tests:
mock_throwsError_whenPartiallyLazyCycleThroughInstantiatorBoundarymock_throwsError_whenConstantDependencyCycleExistsmock_throwsError_whenUnfulfillableInstantiatedPropertyExistsmock_throwsError_whenReceivedPropertyIsUnfulfillablemock_throwsError_whenInstantiatedPropertyRefersToSelfmock_throwsError_whenInstantiatedPropertyHasForwardedArgumentmock_throwsError_whenReceivedInstantiatorDependencyCycleExists_withRootmock_throwsError_whenReceivedInstantiatorDependencyCycleExists_withoutRootProduction test changes
run_writesConvenienceExtensionOnRootOfTree_whenPartiallyLazyInstantiationCycleExiststo error test fileTest plan
🤖 Generated with Claude Code