Skip to content

Source-gen infers generic type arguments from a data source's declared type, so broad declared types fail constraints that the runtime value meets #7116

Closed

Description

@thomhurst

Summary

When a generic test method's data source is declared with a broad static type, source-generation mode infers the type argument from the declared type rather than from the runtime value. If that declared type does not satisfy the method's constraint, the test is dropped (or, once #7102 lands, reported as a constraint failure). Reflection mode infers from the runtime value, so the same test runs and passes.

Repro

public class BroadDeclaredTypeTests
{
    public static IEnumerable<Func<object>> Streams() => [() => new MemoryStream()];
    public static IEnumerable<Func<int?>> Numbers() => [() => 1];

    [Test]
    [MethodDataSource(nameof(Streams))]
    public void Disposable<T>(T value) where T : IDisposable { }

    [Test]
    [MethodDataSource(nameof(Numbers))]
    public void Comparable<T>(T value) where T : IComparable { }
}
  • Reflection mode: both tests are discovered and pass (T = MemoryStream, T = int).
  • Source-gen mode: the generator infers T = object / int?, the constraint check fails, and the tests are not run.

Notes

Activity

  1. added a commit that references this issue on Oct 21, 2026
    b032776
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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions