Skip to content

Source-gen does not infer generic method types from [InstanceMethodDataSource]; drops it on generic classes #7351

Closed
4 of 4 issues completed

Description

@thomhurst

What happens

On a generic test method, source-generated mode does not infer the method's type arguments from an [InstanceMethodDataSource], even when the data method declares a concrete row type. Reflection mode infers them from the row values.

[Arguments(1)]
public class C(int value)
{
    public IEnumerable<string> Rows() { yield return value.ToString(); yield return "b"; }

    [Test]
    [InstanceMethodDataSource(nameof(Rows))]
    public void StructOnly<T>(T item) where T : struct { }
}
  • Reflection (--reflection): 2 failed tests, Type 'String' does not satisfy the 'struct' constraint for generic parameter 'T'.
  • Source-generated mode: 2 failed tests, Could not infer the type arguments of generic test method 'StructOnly' from data source [InstanceMethodDataSource(nameof(Rows))]. Source-generated mode infers type arguments at compile time from the data source's declared type .... The declared type here, IEnumerable<string>, does determine T.

The same source on a generic class ([Arguments<int>(1)] class C<T>(T value) with M<U>(U item) where U : struct) is dropped completely in source-generated mode: 0 tests are discovered and no diagnostic is shown. Reflection reports 2 failures. This also happens on origin/main (f63a1aa).

Where

TestMetadataGenerator only reads the declared row type for MethodDataSourceAttribute. See the attribute.AttributeClass?.Name == "MethodDataSourceAttribute" checks near InferTypesFromMethodDataSourceRowType. InstanceMethodDataSourceAttribute derives from MethodDataSourceAttribute, but these name checks do not match it.

Reproduce

The non-generic case is tests/TUnit.TestProject/Bugs/_7222/InstanceSourceConstraintViolationTests.cs (added in #7344). Run it with --treenode-filter "/*/TUnit.TestProject.Bugs._7222/InstanceSourceConstraintViolationTests/*", with and without --reflection.

Expected

Both modes infer T from the declared row type of an [InstanceMethodDataSource], as they do for [MethodDataSource], and report the same tests and messages. A generic class with such a method is never silently dropped.

Found while handling a review finding on #7344.

Activity

  1. added
    bugSomething isn't working
    P2Medium: edge case, mode divergence, flaky test, or workaround exists
    on Nov 1, 2026
  2. thomhurst commented on Nov 2, 2026

    @thomhurst
    OwnerAuthor

    Triage: linked as blocked by #7222. The bug also happens on origin/main, but its regression test (Bugs/_7222/InstanceSourceConstraintViolationTests.cs) and the [InstanceMethodDataSource] violation path it builds on arrive with PR #7344 (closes #7222), which changes the same TestMetadataGenerator code.

  3. 12 remaining items

  4. thomhurst commented on Nov 5, 2026

    @thomhurst
    OwnerAuthor

    Checked the parent acceptance criteria against freshly fetched origin/main at b9d47dee1f5228a98b997cb36cc70fc8e578544c. No remaining implementation is needed.

    Fresh validation with SDK 10.0.403, net10.0, macOS arm64:

    Separate treenode filter under /*/TUnit.TestProject.Bugs._7222/ Source-generated Reflection
    InstanceSourceConstraintViolationTests/* 2 total, 2 expected failures 2 total, 2 expected failures
    GenericInstanceSourceTests*/* 5 total, 3 passed, 2 expected failures 5 total, 3 passed, 2 expected failures
    NonGenericInstanceSourceTests/* 2 passed 2 passed

    Each failing run reports exactly twice: Type 'String' does not satisfy the 'struct' constraint for generic parameter 'T'. None reports the former type-inference error. The generic class is retained, and its valid sources run. Exit code 2 is expected for these intentional constraint failures.

    Built tests/TUnit.TestProject/TUnit.TestProject.csproj -f net10.0 -m:1 successfully (0 errors; existing analyzer/fixture warnings). Ran the built test DLL with each filter separately, with and without --reflection. Inspected InstanceMethodTypeInferenceTests and Issue7222Tests regression coverage; the engine suite was not run locally per AGENTS.md. The final merged tuple change also has passing Ubuntu/Windows engine and AOT checks: https://github.com/thomhurst/TUnit/actions/runs/42036426217.

    Closing the parent as implemented and verified on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: edge case, mode divergence, flaky test, or workaround existsbugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions