Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d0ae390
Preserve generic class runtime row binding and constructor partitions…
thomhurst Nov 29, 2026
1b615bb
fix(core): accept CLR signed/unsigned array assignment in generic con…
thomhurst Nov 29, 2026
23012d9
feat(inference): add a shared generic type unifier with Roslyn and re…
thomhurst Nov 29, 2026
710c755
fix(core): exclude indexers from reflection class property metadata (…
thomhurst Nov 29, 2026
c8fb3a7
fix(core): recognise tuples with more than seven elements on netstand…
thomhurst Nov 29, 2026
9f01917
fix(core): keep enum and array element types in netstandard2.0 attrib…
thomhurst Nov 29, 2026
92a2cda
fix(analyzers): require a public parameterless constructor on a separ…
thomhurst Nov 29, 2026
80fdc96
fix(analyzers): resolve abstract base property data sources on the co…
thomhurst Nov 29, 2026
2f22a73
fix(analyzers): detect Func<T> data source wrappers by symbol instead…
thomhurst Nov 29, 2026
c64e376
fix(generic): one constructor rule for class rows in both modes and t…
thomhurst Nov 29, 2026
2fd0936
fix(sourcegen): inherit attributes from overridden test methods (#7785)
thomhurst Nov 29, 2026
372f3c8
fix(sourcegen): split object-typed data source collections into rows …
thomhurst Nov 29, 2026
a0950a8
fix(sourcegen): bind open generic instance hooks without reflecting o…
thomhurst Nov 29, 2026
856eded
fix(sourcegen): generate factories for custom derived method data sou…
thomhurst Nov 29, 2026
48cc965
fix(generic): bind tuple class rows and TUnit0001 rows through the sh…
thomhurst Nov 29, 2026
24fd45e
fix(generic): infer a repeated tuple-element type parameter by conver…
thomhurst Nov 29, 2026
9f6dda8
fix(sourcegen): close a generic class for a generic test method from …
thomhurst Nov 29, 2026
caca1ee
test(generic): cover tuple class rows that instantiate a class with s…
thomhurst Nov 29, 2026
c32f257
fix(analyzers): report nested constraint violations and defer run-tim…
thomhurst Nov 29, 2026
bce1bd6
refactor(sourcegen): remove unreachable metadata helpers
thomhurst Nov 29, 2026
1083aa3
refactor(generic): share the GenerateGenericTest arity rule and index…
thomhurst Nov 29, 2026
59f7fab
test(generic): one typed-row inference table run through both modes
thomhurst Nov 29, 2026
7ddfc1a
test(generic): cover inherited class rows across several ancestors
thomhurst Nov 29, 2026
d73d053
test: restore the existing typed-row parity table and move the new on…
thomhurst Nov 29, 2026
9a8c54f
build: link the shared inference files into the Roslyn analyzer variants
thomhurst Nov 29, 2026
1fc3a37
fix(generic): address review findings on constructor selection and wi…
thomhurst Nov 29, 2026
05bcba2
fix(generic): second round of review findings on constructor selection
thomhurst Nov 29, 2026
7ba6ffc
test(core): check CastHelper.MayCast against the conversions Cast per…
thomhurst Nov 29, 2026
6526759
fix(generic): third round of review findings on constructor selection
thomhurst Nov 29, 2026
eb6aeca
fix(generic): keep each class row's constructor when rows of one clas…
thomhurst Nov 29, 2026
ed42fbc
refactor(generic): select class-row constructors by value count only
thomhurst Nov 29, 2026
50e11bd
fix(generic): match the generated constructor count check to the shar…
thomhurst Nov 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/internal/generic-method-inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,37 @@ A generic method on a generic class needs a complete pair of class and method ty
The common `GenericMethodInferencePolicyTests` matrix covers class literal/typed/member sources, repeated class types, method-derived class types, inferred and uninferred rows, type-free and empty constructor rows, explicit class and method types, missing constructor rows, and combined class/method source selection. `ClassAndMethodParameterInference` also checks that parameter inference beside duplicate explicit method types preserves the cross product of constructor and matrix rows.

This refactoring preserves the existing mixed-source type-free-row limitation tracked by #7390: reflection only separates type-free literal class rows when all class sources are argument rows. Runtime-value inference can also discover types unavailable to the generator. The shared policy does not make those extraction capabilities identical.

## Class rows and row shape

Generic type unification and constructor selection are shared code, not mirrored adapters. `src/TUnit.Core.SourceGenerator/Helpers/Inference/` is compiled into the generator (all Roslyn variants), the analyzers and `TUnit.Core`. It sees types only through `ITypeModel<TType>`. The generator and analyzers use `RoslynTypeModel` (`ITypeSymbol`), and the engine and generated factories use `ReflectionTypeModel` (`System.Type`). Both adapters classify implicit conversions the same way: no user-defined or tuple conversions.

`GenericTypeUnifier` follows C# method type inference:

- A bare value, or a value bound to `T?`, gives a lower bound. A type argument of a constructed generic, or an array element, gives an exact bound.
- A type parameter takes its exact bound when it has one. All exact bounds must agree, and every lower bound must convert to that type.
- Otherwise a type parameter takes the lower bound every other lower bound converts to: `(T a, T b)` with `(1, 2L)` infers `long` in both modes, whatever the order. Loose `params T[]` values are lower bounds too. An explicitly supplied `T[]` fixes `T` exactly.
- Without such a type, the row does not unify.

`ConstructorRowInference` selects the constructor a class row binds to:

- The public constructor marked `[TestConstructor]` always wins. A row that does not fit it is reported.
- Otherwise it is the first public constructor, in declaration order, whose parameters suit the row's value count. Optional parameters may be omitted, and a trailing single-dimensional array or `params` collection collects loose trailing values.
- The values' types never change the choice, so the generator, reflection mode and both argument binders always pick the same constructor. Constructors with the same arity therefore resolve to the first declared, as before rows chose a constructor. Choosing among them by value type would need every mode to predict three different binders' conversions identically, and review of #7883 kept finding cases where they differed.
- The selected constructor alone infers the class type arguments from the values.

The same rule picks the constructor everywhere:

- **Reflection.** Discovery uses `ReflectionGenericTypeResolver.FindRowConstructor`, and instance creation uses `CreateInstanceFactory`.
- **Generated instance factories.** A class with several public constructors and no `[TestConstructor]` dispatches on `args.Length` in declaration order.
- **TUnit0001.** A class row is checked against each candidate constructor and is reported only when it fits none of them.
- A row no constructor takes fails with a message naming its value count, instead of losing values.

Declared rows (own-class `[MethodDataSource]` members and typed class sources) bind the same way, with two differences:

- A declared `object` element, or one naming the class's own type parameters, is unknown. It neither infers nor rules out a type.
- A tuple row fills a single tuple parameter whole. Otherwise it spreads one element per parameter.

An own-class member whose declared rows infer a complete class instantiation closes the class in both modes. Its rows are not re-partitioned on that instantiation. A member that joins an instantiation an `[Arguments]` row already closed stays partitioned at run time.

Class rows are unwrapped against the constructor's parameters, as method rows are against the method's. A tuple row therefore stays whole for a sole tuple constructor parameter, including one that generated metadata erases to `object` on an open generic class.
3 changes: 3 additions & 0 deletions eng/Roslyn.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<Compile Include="..\TUnit.Core.SourceGenerator\Extensions\ParsableTypeExtensions.cs" />
<Compile Include="..\TUnit.Core.SourceGenerator\Extensions\ExactTypeExtensions.cs" />
<Compile Include="..\TUnit.Core.SourceGenerator\Extensions\TypedDataSourceInference.cs" />
<!-- Shared generic inference, as the base TUnit.Analyzers.csproj links it -->
<Compile Include="..\TUnit.Core.SourceGenerator\Extensions\RoslynTypeModel.cs" />
<Compile Include="..\TUnit.Core.SourceGenerator\Helpers\Inference\*.cs" />
</ItemGroup>

<!-- AspNetCore.Analyzers: include analyzer release tracking files -->
Expand Down
2 changes: 1 addition & 1 deletion src/TUnit.Analyzers/Extensions/CompilationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private static bool HasGenerateGenericTestAttribute(ITypeParameterSymbol typePar
// Keep in step with TestMetadataGenerator.ValidateTypeParameterConstraints in TUnit.Core.SourceGenerator,
// which validates the same type arguments at generation time. This check fails open: any shape it cannot
// decide for certain is accepted, so a gap here is a missed diagnostic rather than a false TUnit0001.
private static bool CanSatisfyConstraints(this Compilation compilation, ITypeSymbol argumentType,
internal static bool CanSatisfyConstraints(this Compilation compilation, ITypeSymbol argumentType,
ITypeParameterSymbol typeParameter, IReadOnlyDictionary<ITypeParameterSymbol, ITypeSymbol>? inferredTypeArguments)
{
// Only report what is certain. Open or unresolved argument types are accepted.
Expand Down
Loading
Loading