Fix: handle array / pointer / dynamic type arguments in nested constraint chains#62
Open
waynebrantley wants to merge 1 commit into
Open
Conversation
…chains
MatchedTypeSatisfiesConstraints previously required every candidate type
argument to be an INamedTypeSymbol. That silently rejected any handler whose
bound generic contains an array (byte[], string[], T[][]) or other non-named
type when the callback's constraint chain recurses through a nested
IInterface<TOtherParam> where TOtherParam is bound to the array type.
Example that was dropped without diagnostic:
interface IQuery<T> { }
interface IHandler<Q, R> where Q : IQuery<R> { }
class BytesQuery : IQuery<byte[]> { }
class BytesHandler : IHandler<BytesQuery, byte[]> { }
[GenerateServiceRegistrations(AssignableTo = typeof(IHandler<,>),
CustomHandler = nameof(AddHandler))]
...
void AddHandler<THandler, TQuery, TResult>()
where THandler : class, IHandler<TQuery, TResult>
where TQuery : class, IQuery<TResult>;
BytesHandler was skipped because the inner recursion hit IQuery<byte[]> whose
type argument byte[] is an IArrayTypeSymbol, not INamedTypeSymbol.
Widen the check to ITypeSymbol and branch on shape:
- INamedTypeSymbol: recurse into SatisfiesGenericConstraints as before.
- Other ITypeSymbol (arrays / pointers / dynamic): can't recurse, but the
simple constraints on the type parameter still apply — reject value-type /
unmanaged / ctor constraints, honor reference-type, and walk AllInterfaces
+ BaseType for the type constraints. Arrays are reference types
implementing IList<T> etc., so this covers the common cases.
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
MatchedTypeSatisfiesConstraintsinDependencyInjectionGenerator.FilterTypes.csrequires every candidate type argument in a nested constraint chain to be anINamedTypeSymbol. When the recursion reaches a constructed generic whose type argument is an array (T[],T[][]), pointer, ordynamic, the pattern-match fails and the whole handler is silently dropped from generation — no diagnostic, no warning.Repro (fails on
main)Expected emit: all three
AddHandler<...>()calls.Actual emit: only
AddHandler<ScalarHandler, ScalarQuery, int>();— the two array-result handlers are silently dropped.Root cause
MatchedTypeSatisfiesConstraintswalksTHandler's constraint chain recursively. On the outer step it matchesTHandler : IHandler<TQuery, TResult>fine. Then it recurses intoTQuery : IQuery<TResult>by walkingBytesQuery.AllInterfacesforIQuery<byte[]>. That step compares constraint type args with matched-interface type args:matchedType.TypeArguments[0] = byte[]is anIArrayTypeSymbol, notINamedTypeSymbol, so the pattern-match fails and the whole outer handler is rejected.Fix
Widen the check to
ITypeSymboland branch on shape:INamedTypeSymbolcandidate → recurse intoSatisfiesGenericConstraintsas before.ITypeSymbol(array, pointer, dynamic) → can't recurse into nested constraints, but the simple constraints (class/struct/new()/ interface / base-class) on the type parameter still apply. Rejectstruct,unmanaged,new(); honorclass; and check interface / base-class constraint types against the candidate'sAllInterfaces+BaseType.Test
CustomHandlerTests.CustomHandler_HandlesArrayTypeArgumentInNestedConstraintcoversbyte[],byte[][],string[], andint. Verified: fails onmain, passes with the fix; all 94 existing tests still pass.Impact
Any consumer using
CustomHandler(orScanForTypes+Handler) with a nested-generic constraint chain and array-typed generic arguments — I hit it in production migrating a large CQRS codebase whereIAsyncQueryHandler<Q, byte[]>handlers silently disappeared from DI after switching to source-gen.