Repository navigation
fix(sourcegen): close generic methods of generic classes on every class source - #7152
Conversation
…ach class source of a generic class For a generic method of a generic class, the generator treated type arguments from a method-only source ([GenerateGenericTest] on the method, or a method [Arguments] row) as the class's type arguments and left the method open. The generated invoker then named the method type parameter (CS0246) or could not infer it (CS0411). The generator now pairs each class source with each method source, as reflection mode does when it closes the method on each concrete class: class rows with the method's [GenerateGenericTest] types, and a class-level [GenerateGenericTest] with method rows. Type arguments that cover only the class or only the method are no longer emitted. Fixes #7117
…generic class Inferred method type arguments from typed data sources, [MethodDataSource] and IInfersType<T> on a generic method of a generic class are now paired with each class row and class-level [GenerateGenericTest], as reflection mode does. The previous commit stopped them being emitted as class type arguments; without this they would no longer be discovered in source-gen mode.
Codex Review SummaryThis 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. |
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 42 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5e9976d275
ℹ️ 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".
[Medium impact] The PR appears safe to merge; both previous findings are addressed. SummaryFixes generic test methods on generic classes by pairing class sources with method sources.
Both previous findings are addressed. No new actionable issues were found.
|
…er-row constraint failures - Substitute class type parameters in method type arguments inferred from a [MethodDataSource] of the open class (IEnumerable<Func<(T, int)>>), so the generated call names the closed type instead of T (CS0246). - A method constraint violation from a [MethodDataSource] paired with a class row now fails once per row it yields, and one from IInfersType<T> once per row of every method data source, as in reflection mode. - Drop the typed data source pairing: InferTypesFromDataSourceAttribute never infers method types on a generic class, matching reflection mode, so the loop was dead code. - Add TestProject shapes and engine/generator tests for these cases.
|
Review dispositions as of b1a7aac:
The CodeRabbit comment was rate-limited and has no findings. The CI failures on 5e9976d were all cancelled runs, not test failures, so the new push re-runs them. |
Review of #7152: generic method on generic class (#7117)I found no blocking problems. I reviewed the diff only and did not build it or run any tests. The What I like
Suggestions (non-blocking)
Overall this is a solid, well-tested fix. 🤖 Generated with Claude Code |
|
Dispositions for the latest claude-review comment (on b1a7aac):
No code changes, so no new commit. |
Updated [TUnit.Core](https://github.com/thomhurst/TUnit) from 1.75.0 to 1.76.28. <details> <summary>Release notes</summary> _Sourced from [TUnit.Core's releases](https://github.com/thomhurst/TUnit/releases)._ ## 1.76.28 <!-- Release notes generated using configuration in .github/release.yml at v1.76.28 --> ## What's Changed ### Other Changes * Remove legacy JSON output implementation by @Saad-elnagar in thomhurst/TUnit#7234 * fix(engine): drop row-scoped data sources from failed test metadata by @kenny-ish in thomhurst/TUnit#7079 * Merge the analyzer release tables under one New Rules section (RS2007) by @bfs2021 in thomhurst/TUnit#7174 * fix: release finished test instances before session end by @thomhurst in thomhurst/TUnit#7262 * fix: never hand out a disposed shared data-source instance by @thomhurst in thomhurst/TUnit#7248 ### Dependencies * chore(deps): update all non-major dependencies to 1.76.0 by @thomhurst in thomhurst/TUnit#7233 * chore(deps): bump follow-redirects from 1.16.0 to 1.16.1 in /docs by @dependabot[bot] in thomhurst/TUnit#7232 * chore(deps): bump launch-editor from 2.14.1 to 2.14.2 in /docs by @dependabot[bot] in thomhurst/TUnit#7231 * chore(deps): update all non-major dependencies to 1.20.1 by @thomhurst in thomhurst/TUnit#7235 * chore(deps): update dependency awssdk.sqs to 4.0.100.20 by @thomhurst in thomhurst/TUnit#7236 * chore(deps): update dependency microsoft.playwright to 1.64.0 by @thomhurst in thomhurst/TUnit#7237 * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7238 * chore(deps): update dependency path-to-regexp to v9 by @thomhurst in thomhurst/TUnit#7241 * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7242 * chore(deps): update dependency serialize-javascript to v7.1.3 by @thomhurst in thomhurst/TUnit#7243 * chore(deps): update dependency imposter to 1.0.4 by @thomhurst in thomhurst/TUnit#7244 * chore(deps): update dependency stackexchange.redis to 3.4.20 by @thomhurst in thomhurst/TUnit#7245 * chore(deps): update dependency serialize-javascript to v7.1.4 by @thomhurst in thomhurst/TUnit#7246 * chore(deps): update dependency imposter to 1.0.5 by @thomhurst in thomhurst/TUnit#7247 * chore(deps): update all non-major dependencies to 13.6.4 by @thomhurst in thomhurst/TUnit#7251 * chore(deps): update dependency imposter to 1.0.6 by @thomhurst in thomhurst/TUnit#7258 * chore(deps): bump fast-uri from 3.1.8 to 3.1.9 in /docs by @dependabot[bot] in thomhurst/TUnit#7253 * chore(deps): bump body-parser from 1.20.6 to 1.20.9 in /docs by @dependabot[bot] in thomhurst/TUnit#7252 ## New Contributors * @Saad-elnagar made their first contribution in thomhurst/TUnit#7234 * @kenny-ish made their first contribution in thomhurst/TUnit#7079 * @bfs2021 made their first contribution in thomhurst/TUnit#7174 **Full Changelog**: thomhurst/TUnit@v1.76.0...v1.76.28 ## 1.76.0 <!-- Release notes generated using configuration in .github/release.yml at v1.76.0 --> ## What's Changed ### Other Changes * chore: add OSS scanner config, threat model and SECURITY.md by @thomhurst in thomhurst/TUnit#7080 * fix(deps): patch MessagePack for Aspire and use per-TFM Microsoft.Extensions.Http by @Quattordici in thomhurst/TUnit#7072 * fix: skip tests marked with an empty skip reason by @thomhurst in thomhurst/TUnit#7083 * fix(engine): report the engine mode actually selected in the banner by @thomhurst in thomhurst/TUnit#7085 * fix(engine): order keyed NotInParallel tests by priority, not hash code by @thomhurst in thomhurst/TUnit#7090 * fix: remove root causes of flaky tests and the broken Apple smoke jobs by @thomhurst in thomhurst/TUnit#7095 * fix(engine): run synchronous keyed NotInParallel tests concurrently by @thomhurst in thomhurst/TUnit#7087 * fix(sourcegen): run every [Arguments] row that infers the same generic type by @thomhurst in thomhurst/TUnit#7100 * fix: same filter path and display name for generic tests in both engine modes by @thomhurst in thomhurst/TUnit#7110 * fix(engine): apply max parallelism cap to keyed NotInParallel tests by @thomhurst in thomhurst/TUnit#7084 * fix(ci): install only Chromium for Playwright and wait on dpkg lock by @thomhurst in thomhurst/TUnit#6994 * fix(core): keep the engine-selected execution mode when late assemblies load by @thomhurst in thomhurst/TUnit#7091 * fix(engine): end fail-fast runs without crashing the test host by @thomhurst in thomhurst/TUnit#7115 * test: stop the AOT [EngineTest=Pass] run hitting its 5 minute budget by @thomhurst in thomhurst/TUnit#7125 * fix(engine): align reflection-mode data sources and hooks with source-gen by @thomhurst in thomhurst/TUnit#7094 * fix(pipeline): stop skipping the NuGet package tester modules by @thomhurst in thomhurst/TUnit#7127 * ci: take AOT publishes and doc snippets off the Ubuntu critical path by @thomhurst in thomhurst/TUnit#7128 * fix(analyzers): TUnit0001 false positive for MethodDataSource on generic type-parameter parameters by @thomhurst in thomhurst/TUnit#7109 * fix: report generic class constraint violations as failed tests in both modes by @thomhurst in thomhurst/TUnit#7112 * ci: cut runner contention and the AOT publish off the critical path by @thomhurst in thomhurst/TUnit#7131 * fix(assertions): make cross-type IsEqualTo work for numeric and nullable arguments on C# 12 by @thomhurst in thomhurst/TUnit#7086 * fix(engine): substitute class type arguments and array element types in build-time constraint checks by @thomhurst in thomhurst/TUnit#7102 * ci: test the NuGet packages on Windows in their own job by @thomhurst in thomhurst/TUnit#7130 * fix(sourcegen): bind params T[] on generic test methods by @thomhurst in thomhurst/TUnit#7101 * fix(engine): run every [Arguments] row on generic test classes in both modes by @thomhurst in thomhurst/TUnit#7111 * fix(engine): use last inferred type for repeated generic parameters by @theluckystrike in thomhurst/TUnit#7134 * Make the test suite pass in every environment and run fully in CI by @Quattordici in thomhurst/TUnit#7129 * fix(assertions): skip using directive for global-namespace assertion types by @thomhurst in thomhurst/TUnit#7139 * fix(core): format numeric display-name arguments with the invariant culture by @thomhurst in thomhurst/TUnit#7138 * fix(engine): dispose data-source objects of rows that end as failed tests by @thomhurst in thomhurst/TUnit#7150 * Fix net472 compile gate failure in assertions source generator tests by @thomhurst in thomhurst/TUnit#7170 * fix(sourcegen): close generic methods of generic classes on every class source by @thomhurst in thomhurst/TUnit#7152 * fix(engine): infer object for null rows on generic test methods in reflection mode by @thomhurst in thomhurst/TUnit#7142 * fix(engine): report every test once on fail-fast runs by @thomhurst in thomhurst/TUnit#7145 * test(engine): stop whole-process budgets killing healthy scheduling engine tests by @thomhurst in thomhurst/TUnit#7177 * feat(analyzers): report MethodDataSource Arguments that fit two overloads equally (TUnit0076) by @thomhurst in thomhurst/TUnit#7181 * fix(sourcegen): render top-level dynamic as object so typeof compiles by @thomhurst in thomhurst/TUnit#7182 * fix(sourcegen): pair each GenerateGenericTest type only with its typed ClassDataSource<T> by @thomhurst in thomhurst/TUnit#7188 * fix(engine): run untyped [Arguments(null)] rows on generic classes instead of dropping them by @thomhurst in thomhurst/TUnit#7203 * fix(engine): report generic test classes by their generic type definition in TRX by @thomhurst in thomhurst/TUnit#7200 * test(sourcegen): parse snapshot fixtures as a real build does by @thomhurst in thomhurst/TUnit#7189 * refactor(engine): dedupe reflection generic discovery by @thomhurst in thomhurst/TUnit#7183 * test: make TimeoutHelperTests deterministic under thread-pool load by @thomhurst in thomhurst/TUnit#7180 * fix(analyzers): accept null MethodDataSource arguments for nullable parameters by @thomhurst in thomhurst/TUnit#7179 * test(assertions): make WaitsFor cancellation tests robust to slow CI runners by @thomhurst in thomhurst/TUnit#7176 * fix(assertions): support [AssertionFrom] with unbound generic types by @thomhurst in thomhurst/TUnit#7144 ... (truncated) Commits viewable in [compare view](thomhurst/TUnit@v1.75.0...v1.76.28). </details> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Fixes #7117
Summary
On a generic class,
[GenerateGenericTest]on a generic test method produced generated code that did not compile (CS0246 for the method type parameter, or CS0411). This PR fixes that. Source-gen mode now produces the same tests as reflection mode for these shapes.Root cause
CollectConcreteInstantiationssplits each set of type arguments into class arguments and method arguments by position. Some sources supply type arguments for the method only: the method's[GenerateGenericTest], a method[Arguments]row, a typed data source,[MethodDataSource], orIInfersType<T>. Their types went through the same split, so they became the class type arguments and the method stayed open. The generated invoker then calledCastHelper.Cast<TMethod>(...)orinstance.Run(...)with no type arguments.The workaround in the issue does not work either. With a class-level
[GenerateGenericTest], a method[Arguments]row still closed the class over the row's type and left the method open.Fix (generator only;
TestMetadataGenerator.CollectConcreteInstantiations)TryAddInstantiationskips a set of type arguments for a generic method of a generic class unless the set covers both the class and the method.[GenerateGenericTest]is paired with each method source that infers the method type arguments ([Arguments]row, typed data source,IInfersType<T>,[MethodDataSource]). The cartesian product with method[GenerateGenericTest]is unchanged.[Arguments]row is paired with each of those method sources, and with each method[GenerateGenericTest]type.[GenerateGenericTest]types, in reflection mode's order. A type that a source infers runs only that source. Any other[GenerateGenericTest]type, andIInfersType<T>, runs every method data source, as on a non-generic class. For example, class row[Arguments(1)]with[GenerateGenericTest(typeof(long))] [Arguments(2)]givesRun<int>(2)andRun<long>(2)in both modes.Reflection mode already behaved this way, so
TUnit.Engineneeds no change.Tests
tests/TUnit.Core.SourceGenerator.Tests/Bugs/7117/GenerateGenericTestOnMethodOfGenericClassTests.cscompiles the generated output for the issue's shape and related shapes, and asserts there are no compiler errors. Before the fix it failed with 8 CS0246/CS0411 errors.tests/TUnit.TestProject/Bugs/_7117classes andtests/TUnit.Engine.Tests/Issue7117Tests.cscheck test names and counts in both modes (6 shapes). Both modes produce the same 8 tests and all pass.TUnit.Core.SourceGenerator.Tests, full run: 193 passed, 1 skipped, 0 failed, and no snapshot changes.TUnit.Engine.TestsfiltersIssue7117Tests,Issue7106Tests,Issue7092Tests,Issue7096Tests,Issue7105Tests,Issue7107Tests,Issue7121Tests,GenericConstraintValidationTestsandGenericMethodWithDataSourceTests.TUnit.TestProjectnamespacesBugs._4431,Bugs._4440,Bugs._7092andBugs._7099, in source-gen and reflection modes. The counts match, and the only failure is the intentionalExpectedResult.Failuretest in 4431.