Fail fast when a mixed-lifetime IEnumerable<T> singleton mirror is missing (#381)#382
Merged
Merged
Conversation
…ssing (#381) The mixed-lifetime IEnumerable<T> codegen routes each singleton element through a keyed "mirror" registration created by AddJasperFxEnumerableSingletonSupport(). If a consumer never calls that method (or registers a mixed-lifetime family after it runs), the mirror does not exist and the singleton element was injected as null with no error — the enumerable silently contained a null that surfaced far away as an NRE (the footgun behind #380). CreateArrayFrame now emits a null guard before constructing the element array for any singleton element sourced from a keyed mirror. When the mirror is absent the generated code throws an actionable InvalidOperationException naming the missing key and pointing at AddJasperFxEnumerableSingletonSupport(), instead of yielding a null element. - EnumerableSingletons: KeyPrefix constant, IsMirrorKey() detector, and MissingMirrorMessage() builder. - CreateArrayFrame.GenerateCode: emit the guard for mirror-sourced singletons. - Tests cover the generated guard text, the runtime throw under optional keyed resolution (the silent-null path), and a no-regression case with support registered. Bumps JasperFxVersion 2.1.3 -> 2.1.4. Co-Authored-By: Claude Opus 4.7 (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
Fixes #381 — the mixed-lifetime
IEnumerable<T>codegen routes each singleton element through a keyed "mirror" registration created byAddJasperFxEnumerableSingletonSupport(). If a consumer never calls that method (or registers a mixed-lifetime family after it runs), the mirror doesn't exist and the singleton element was injected asnullwith no error — the enumerable silently contained a null that surfaced far away as an NRE inside a handler (the footgun behind #380).This implements option 2 from the issue: a small guard in the generated enumerable code.
What changed
CreateArrayFrame.GenerateCodenow emits a null guard before thenew T[]{…}array literal for any singleton element sourced from a keyed mirror. When the mirror is absent, the generated code throws an actionableInvalidOperationExceptioninstead of yielding a null element:EnumerableSingletonsgains aKeyPrefixconstant, anIsMirrorKey()detector, and aMissingMirrorMessage()builder.The guard is robust regardless of how the constructor parameter was populated (MS DI's keyed resolution, or JasperFx's
QuickBuildoptionalGetKeyedServicepath that produced the silent null).Tests
Added to
inline_enumerable_with_mixed_lifetimes:missing_mirror_emits_a_failfast_guard_in_the_generated_code— asserts the guard textmissing_mirror_throws_actionable_error_instead_of_injecting_null— exercises the silent-null path (optional keyed resolution returns null) and asserts the actionable throwregistered_mirror_does_not_trip_the_guard— no-regression case with support registeredFull CodegenTests (359) and CoreTests (439) pass on net9.0 and net10.0.
Version
Bumps
JasperFxVersion2.1.3 → 2.1.4.🤖 Generated with Claude Code