Skip to content

Generic type inference rules are implemented twice (source-gen and reflection) and keep drifting #7371

Closed
2 of 2 issues completed

Description

@thomhurst

What I saw

Two hand-written copies decide how a generic test class or method gets its type arguments:

  • source-gen: TestMetadataGenerator (TryAddInstantiation, InferTypesFromDataSourceAttribute, InferTypesFromTypedDataSourceForClass, and others)
  • reflection: ReflectionTestDataCollector (InferClassTypeArgumentsOfMethodRows, BuildTestsForMethodRowsWithoutClassRow, ScopeMethodRowsToClassTypeArguments, and others)

Each copy answers three questions for every data source kind ([Arguments] row, typed data source, [MethodDataSource], parameter attribute such as [Matrix<T>], [GenerateGenericTest]):

  • Which sources infer an instantiation?
  • In which order, and which source keeps a type that two sources infer?
  • Is a source scoped to the instantiation it infers, or does it run on every instantiation?

The answers drift apart, and most generic-inference bugs filed recently are cases of that drift: #7214, #7333, #7335, #7353, #7362, #7369. In #7337, every round of Codex and Greptile findings was another parity gap of this kind. The latest was a typed source scoped to its instantiation in source-gen but not in reflection (fixed in da8c1aa). The Claude review of #7337 raised the same concern.

Expected

Put the rules (which sources infer, their precedence, and their scope) in one documented table. Have both modes follow it, and run one parity test matrix against it. A new source kind should then need one change in the table, not two hand-written changes.

Reproduce

Compare how each mode handles the same fixture. For example, before da8c1aa, TypeFreeClassRowWithTypedSourceAndInferringParameterAttribute in tests/TUnit.TestProject/Bugs/_7209 ran 4 tests in source-gen and 3 with --reflection.

Activity

  1. added
    enhancementNew feature or request
    P2Medium: edge case, mode divergence, flaky test, or workaround exists
    on Nov 2, 2026
  2. thomhurst commented on Nov 3, 2026

    @thomhurst
    OwnerAuthor

    Split into native sub-issues #7450 (generic-method source precedence, duplicate selection, and fallback) and #7451 (generic-class scoping and class/method pairing). These are independently reviewable policy changes; #7451 depends on #7450. Implementing #7450 in this unit. Existing type-free class-row behavior fixes remain in #7390.

  3. thomhurst commented on Nov 3, 2026

    @thomhurst
    OwnerAuthor

    The generic-method policy portion is implemented in draft PR #7455, closing child #7450. The shared policy, documented rule table, and 13-case matrix are included. All 13 cases passed in both source-generated and reflection modes; the generator suite passed (219 passed, 1 skipped). Parent scope remains open in #7451 for class inference and class/method pairing.

  4. thomhurst commented on Nov 4, 2026

    @thomhurst
    OwnerAuthor

    Re-checked the parent acceptance criteria against freshly fetched origin/main at 10a0af7272. Both native children (#7450 and #7451) are closed, and their implementations are ancestors of this main commit:

    Evidence on main:

    Type extraction, extraction switches, and class-source traversal remain adapter-specific, as explicitly documented and reviewed in #7498; adding a source still requires implementing its type extraction in both adapters. The shared policies centralize the common decisions rather than unifying Roslyn and reflection representations. The existing mixed-source type-free-row behavior remains tracked separately in #7390 and was explicitly outside both children's changes.

    No additional parent-only implementation remains. Closing as completed by #7455 and #7498. This unit audited source, ancestry, matrix wiring, and merged CI evidence; it did not rerun the Engine suite locally, in accordance with repository instructions.

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 existsenhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions