Repository navigation
fix(generic): share generic inference and constructor-row binding across source-gen, reflection and TUnit0001 - #7883
Conversation
…structor partitions The CLR assigns arrays whose elements reduce to the same type (an enum to its underlying type, and signed and unsigned integers of one size), so int[] is a uint[] and List<int[]> is an IEnumerable<uint[]>. Partition matching only recognised the enum case, so a compatible row behind a null row was dropped once filtering turned on. Addresses the two open P1 review findings on #7599.
…flection adapters GenericTypeUnifier is linked into the generator, the analyzers and the engine (like the existing inference policy files) and sees types only through ITypeModel<TType>. RoslynTypeModel adapts ITypeSymbol for the generator and analyzers; ReflectionTypeModel adapts System.Type for the engine. Both classify implicit conversions identically (no user-defined or tuple conversions). The unifier follows C# method type inference: bare values give lower bounds, type arguments and array elements give exact bounds, and a repeated type parameter takes the bound every other bound converts to. No consumer uses it yet; the same cases are pinned through both adapters. Refs #7377
…ate instance data source provider (#7505)
…ncrete test class (#7551)
… of display string (#7380)
…he analyzer A class data row binds to the [TestConstructor], else the first public constructor (in declaration order) its values fit, and the same constructor infers the class type arguments, reads the source's rows and builds the instance. The rule lives in the shared ConstructorRowInference, used by reflection discovery and instance creation, by the generator's class-row, own-member, typed-source and factory paths, and by TUnit0001's class checks. - Generated factories for classes with several public constructors pick one per row through ConstructorSelector.Fits instead of always calling the first; reflection picks the same way instead of preferring a parameterless constructor. - A repeated bare T infers the type the other values convert to (1, 2L gives long) in both modes, where the generator took the last value and reflection the first. - The generator closes a generic class over the types an own-class [MethodDataSource] member's declared rows infer, as reflection does. - A row that fits no constructor fails with a message naming the row. Fixes #7365, #7366. Refs #7333, #7446, #7377
…ared row rules - Class rows unwrap against the constructor's parameters, as method rows do against the method's, so a tuple row stays whole for a sole tuple constructor parameter, including a TestDataRow-wrapped tuple and a (T, string) parameter that generated metadata erases to object (#7367, #7680). - TUnit0001 accepts the row readings both modes bind and its older shape checks rejected: an omitted plain trailing array, extra tuple columns collected by a params or trailing array, and a whole tuple for a type parameter that [GenerateGenericTest] closes over it (#7587, #7622). Types are inferred with the shared unifier and must satisfy their constraints; one-to-one readings keep the existing checks and explanations. #7489 was already fixed on main; its exact repro is now a regression test. - An own-class member that closes a generic class keeps its rows unpartitioned on that instantiation; members joining an [Arguments] row's instantiation, or whose rows infer other types, keep main's partitioning and exclusion. - A declared row column naming the class's own type parameters is unknown, as before, rather than contradicting the concrete columns. - Params values infer T as C# does: (1, 2L) gives long in both modes. The _7209 fixtures that asserted the first value's type are updated. - Documents the shared class-row and row-shape rules. Fixes #7367, #7680, #7587, #7622. Refs #7489, #7333, #7446, #7377
…sion in both modes A tuple row's elements are columns the binder converts one by one, so in the shared unifier they keep the tuple's own bound kind instead of being exact: (T, T) with (int, long) or (long, int) infers long, while List<(T, T)> still needs exact element types. Reflection's value inference now resolves all of a row's bounds together through the unifier, and the declared-row paths in both modes (ProcessTypeForGenerics, MatchesOpenRowType, MapGenericParameters and SoleTupleInference) keep the candidate the other converts to rather than the last one. The preserved tuple is converted for the call by the existing binding from #7487. Fixes #7481
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
Walkthrough
Merge Risk:
|
Review of #7883 (shared generic inference / constructor-row binding)I reviewed the shared inference code ( Design: good. The generator, reflection mode and TUnit0001 each had their own copy of this logic. Moving it behind Concerns
Validation. The reported results are thorough, with both modes compared against a No blocking correctness bug found in what I read. The main requests are the allocation cost in item 3, the AOT confirmation in item 6, and splitting or documenting the breaking changes. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 24fd45e3ba
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…a class-level member source A class-level [MethodDataSource] whose declared rows infer the class type arguments, whether a member of the class itself or of a provider named with typeof, now closes the class for a generic test method too. Each inferred class is paired with every method instantiation, including [GenerateGenericTest] arity failures, as reflection mode does. Fixes #7369
Summary
This consolidates the generic-inference and source-generator backlog into one PR. The source generator, reflection mode and TUnit0001 each had their own copy of generic type inference and constructor-row binding, so every single-issue fix opened a new gap between them. This PR moves those algorithms into shared code that all three call, then fixes the backlog on top of it. The hardest class-row issues are left for follow-up PRs built on this component (listed below).
Shared inference (
src/TUnit.Core.SourceGenerator/Helpers/Inference/)This code is compiled into the generator (every Roslyn variant), the analyzers and
TUnit.Core, the same way the existing inference policy files are shared. It sees types only throughITypeModel<TType>. The generator and analyzers useRoslynTypeModel; the engine and generated factories useReflectionTypeModel. The rules are documented indocs/internal/generic-method-inference.mdunder "Class rows and row shape".GenericTypeUnifierfollows C# method type inference:ConstructorRowInferencepicks the constructor for a class row:[TestConstructor], otherwise the first public constructor (in declaration order) whose parameters suit the row's value count. Optional parameters may be omitted, and a trailing array orparamscollection collects loose values. The values' types never change the choice, so every mode picks the same constructor. That constructor alone infers the class type arguments, reads the source's rows and builds the instance.args.Lengthin declaration order.Behaviour changes
Tinfers the common type in both modes:[Arguments(1, 2L)]onC<T>(T a, T b)giveslong. Before, the generator took the last value's type and reflection the first. Looseparams T[]values follow the same rule, so two_7209fixtures that assertedintnow assertlong. An explicitly suppliedT[]still fixesTexactly.[TestConstructor]builds each row with the first constructor whose parameters suit the row's value count. Before, the generator always called the first constructor and reflection preferred a parameterless one. Constructors of the same arity still resolve to the first declared: choosing among them by value type needed every mode to predict three binders' conversions identically, and review kept finding cases where they differed. Use[TestConstructor]to pick another. A row whose value count fits no constructor now fails with a message naming its count.Folded in
int[]for auint[]parameter orList<int[]>forIEnumerable<uint[]>.Issues
Closes #7596, #7365, #7366, #7367, #7680, #7481, #7587, #7622, #7520, #7369, #7334, #7490, #7545, #7386, #7602, #7553, #7740, #7741, #7505, #7551, #7380, #7785, #7178, #7430, #7654
Partly fixed (left open):
[MethodDataSource]member's declared rows (own-class ortypeof(Provider)), as reflection does. The sources that can't be inferred are covered by the follow-ups below.GenerateGenericTestArityMessages.Validate, violation entries are looked up through a dictionary, and the arity pass reads the same class rows as the pairing. The post-hoc sort is still there.Already fixed on
main, now closed, with regression tests here:Follow-ups: #7358, #7362, #7390, #7224, #7597, #7543 and #7463 still reproduce on this branch. Each issue has a comment with counts for both modes, and the fixtures are on
followup/class-row-repro-fixtures.Out of scope:
New issues filed from findings in this PR: #7884, #7885 and #7886.
Also in this PR
object, an interface or an unsealed class. Both modes now read its value as rows (Source-gen treats an object-typed data source holding a collection as one row; reflection mode yields one row per element #7178).Validation (local, net10.0, macOS)
.verified.txtwas reviewed. The diffs are the per-row constructor dispatch on value count, new instantiations, line numbers, and the new tests.main.Bugs/_7xxxgeneric fixture folders were run in both modes, one filter per run, and compared with anorigin/mainbaseline. Every folder matchesmainor improves on it (_7208source-generated mode: 4 failures down to 1). The new fixtures pass in both modes with the same test counts.Summary by CodeRabbit