Skip to content

fix: match matchers in a params array of a generic method - #1104

Open
vbreuss wants to merge 1 commit into
mainfrom
fix/params-type-parameter-object
Open

vbreuss wants to merge 1 commit into
mainfrom
fix/params-type-parameter-object

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 21, 2026

Copy link
Copy Markdown
Member

A matcher for a params T[] parameter of a generic method was captured as a value. The overload that takes values was promoted with [OverloadResolutionPriority(int.MaxValue)], so for Only<T>(params T[] args) and Sum<T>(int level, params T[] args) the call Setup.Only(It.IsAny<string>()) inferred T as the matcher type, and with the type argument object, to which every matcher converts, Verify.Only<object>(It.IsAny<object>()), Only<object>(It.IsAny<object[]>()), Only<object>(Match.AnyParameters()) and Sum<object>(1, It.IsAny<object>()) bound the overload that takes values and matched nothing. No call could set up or verify such a method with a matcher.

A params array of a type parameter now blocks that promotion, as #1098 does for a parameter of type T: whenever a matcher overload is applicable, it outranks the overload that takes values, and it infers the type argument from the matcher.

  • A lone matcher for the array matches the whole array: Only<object>(It.IsAny<object[]>()).
  • Matchers for the elements match element by element: Only(It.IsAny<string>()) sets up Only<string>, and Only<object>(It.IsAny<object>()) matches one element. A null element next to them matches a null element, also for a nullable type argument.
  • Match.AnyParameters() and Match.Parameters(…) for a lone params T[] stand for the parameters.
  • A lone null for a lone params T[] stands for a null array, and a null for the array next to matchers for the other parameters binds the matcher for the whole array, so it does as well: Setup.Sum<object>(It.IsAny<int>(), null). Above four parameters no overload combines values with the matcher for the whole array.
  • A lone null or default for a parameter before such an array forwards to the overload that takes values, as fix: infer the type argument of a generic method from a matcher in setups and verifications #1098 does for a parameter of type T, so Setup.Sum<object>(default) stands for Sum<object>(0) instead of any parameters. The same holds for a non-generic sibling with a value type parameter whose all-values overload is not promoted, such as F(int value) beside F<T>(T value), which fix: infer the type argument of a generic method from a matcher in setups and verifications #1098 stopped promoting, or beside F<T>(params T[] values), which this change stops promoting.
  • A lone params T[] gets the overload without arguments that params object[] has, so Setup.Only<object>().AnyParameters() keeps working, unless a sibling with as many type parameters takes a call without arguments, such as Only<T>(T value = default!), which the call on the mock binds; this also holds without OverloadResolutionPriorityAttribute. A lone non-generic params array beside such a generic sibling, such as Pick(params string[]) beside Pick<T>(params T[]), gets that overload as well, so Setup.Pick().AnyParameters() keeps working, unless another lone params array with as many type parameters, such as Pick(params int[]), would get the same overload.

Matchers that still bind the overload that takes values, which only happens for the type argument object, are recognized at runtime, as #1071 does for a parameter of type T: Only<object>(It.IsAny<object>(), "a") and Sum<object>(1, "a", It.IsAny<object>()) match element by element and compare the plain values next to them by equality. This goes beyond params object[], where a matcher next to plain values still matches nothing. A matcher for the whole array next to other elements matches one element there, as for params object[]. Plain values keep the comparison they had. Mockolate0007 no longer reports a matcher for an element of such an array that is recognized at runtime, as it already did not for a parameter of type T, and still reports a matcher for the whole array next to other elements.

sut.Only<object>(1, "a");
sut.Only("a");
sut.Mock.Verify.Only<object>(It.IsAny<object>(), "a").Once(); // failed before
sut.Mock.Verify.Only<object>(It.IsAny<object[]>()).Once(); // failed before
sut.Mock.Verify.Only(It.IsAny<string>()).Once(); // failed before
sut.Mock.Verify.Only<object>(1, "a").Once(); // as before

One call behaves differently for every type argument of such a method: a null array now matches a null array instead of throwing an ArgumentNullException, for example Setup.Sum<string>(1, null) or Verify.Only<object>(null), as #1031 defined it for params object[]. A params array of another element type, such as params string[], still throws.

This is a narrow source break, as in #1098: .AnyParameters() is no longer available after a matcher or a null for such a params array, or after a lone null or default or one next to such an array for another parameter (e.g. Setup.Sum<object>(1, null).AnyParameters() or Setup.Sum<object>(default).AnyParameters()), and after a default for a non-generic sibling with as many parameters, such as Setup.W(1, default).AnyParameters() for W(int a, int b) beside W<T>(int a, params T[] values), because such a call now binds a matcher or IParameters overload. Use Match.AnyParameters() instead. A lone default that was ambiguous before can bind now: for Ws<T>(int value) beside Ws<T>(params T[] values), Setup.Ws<object>(default) was CS0121 and now sets up a null array, as the call on the mock Ws<object>(default) is ambiguous as well.

Not covered: a matcher for an element next to plain values without the type argument object, such as Sum(1, It.IsAny<string>()), which infers T as the matcher type, and inferring the type argument from a matcher without OverloadResolutionPriorityAttribute.

@vbreuss vbreuss self-assigned this Nov 21, 2026
@vbreuss vbreuss added the bug Something isn't working label Nov 21, 2026
@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

This PR fixes matcher selection for generic methods with params T[] arguments.

  • Generic params T[] methods can match whole arrays and their elements.
  • No-argument and null calls follow the overload used by the mock.
  • The matcher warning now reflects supported generic array matching.

No new actionable issues found. The earlier snapshot concern remains closed: vbreuss explained that GeneratorCoverage uses targeted tests, and Greptile accepted the correction.

Reviews (4) · Last reviewed commit: "fix: match matchers in a `params` array ..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 11 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Test Results

    58 files  ±  0      58 suites  ±0   2h 4m 26s ⏱️ - 7m 3s
 5 483 tests + 25   5 482 ✅ + 24  0 💤 ±0  1 ❌ +1 
54 113 runs  +219  54 112 ✅ +218  0 💤 ±0  1 ❌ +1 

For more details on these failures, see this check.

Results for commit 269c743. ± Comparison against base commit 05dca38.

This pull request removes 24 and adds 43 tests. Note that renamed tests count towards both.
 x-myHeader3: baz, True)
)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenMatcherForObjectIsMixedWithValues_ShouldSuggestMatchersForTheOtherArguments(verify·Gen<object>(1, {|#0:It·IsAny<object>()|});, It·IsAny<object>())
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoSpellingBindsTheMatcherAsAMatcher_ShouldSaySo(verify·GenOnly<object>({|#0:It·IsAny<object>()|});)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
…
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenAParametersMatchIsComparedAsAValue_ShouldBeFlagged(verify·Gen<object>(1, {|#0:Match·AnyParameters()|});)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenMatcherForObjectIsMixedWithValues_ShouldSuggestMatchersForTheOtherArguments(verify·Gen<object>(1, "a", {|#0:It·IsAny<object[]>()|});, It·IsAny<object[]>())
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·Gen<object>(1, "a", It·IsAny<string>());)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·Gen<object>(1, It·IsAny<object>());)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·Gen<object>(1, ["a", It·IsAny<object>()]);)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·Gen<object>(1, new object[] { "a", It·IsAny<object>() });)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·Gen<object>(1, new object[] { It·IsAny<object[]>() });)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·GenOnly<object>(It·IsAny<object>());)
Mockolate.Analyzers.Tests.MatcherAsValueAnalyzerTests ‑ WhenNoMatcherIsComparedAsAValue_ShouldNotBeFlagged(verify·GenOnly<object>(It·IsAny<object>(), 1);)
Mockolate.Internal.Tests.Internals.MatchersTests ‑ AsParamsMatcher_WithElementMatchers_ShouldMatchElementByElement
…

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Unconfigured (N=1000) 273,296 B 273,297 B 🔴 +1 B (±0%)
Indexer (N=1000) 105,720 B 105,721 B 🔴 +1 B (±0%)

Time

Benchmark main PR Δ
Unconfigured (N=1) 484.8 ns 422.9 ns 🟢 -13%
Method (N=1) 146.3 ns 131.4 ns 🟢 -10%
Sequence 1.80 μs 2.08 μs 🔴 +16%
CompleteWorkflowBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1.696 μs 0.0285 μs 0.0253 μs 1.00 5.14 KB 1.00
Imposter 1.993 μs 0.0411 μs 0.0343 μs 1.18 7.21 KB 1.40
TUnitMocks 4.422 μs 0.0426 μs 0.0399 μs 2.61 16.95 KB 3.30
Moq 1,025.363 μs 5.0314 μs 4.7064 μs 604.56 90.08 KB 17.52
NSubstitute 270.703 μs 0.5978 μs 0.4992 μs 159.61 53.11 KB 10.33
FakeItEasy 306.812 μs 2.5042 μs 2.2199 μs 180.90 64.1 KB 12.47
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.67GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 23.17 ns 0.182 ns 0.170 ns 1.00 136 B 1.00
Imposter 119.67 ns 1.177 ns 0.983 ns 5.16 504 B 3.71
TUnitMocks 39.03 ns 0.219 ns 0.205 ns 1.68 216 B 1.59
Moq 1,148.72 ns 6.465 ns 6.047 ns 49.58 2088 B 15.35
NSubstitute 1,647.82 ns 10.704 ns 9.489 ns 71.12 5032 B 37.00
FakeItEasy 1,337.08 ns 6.436 ns 5.705 ns 57.71 2751 B 20.23
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.00GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteUnconfiguredBenchmarks
Unconfigured N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 422.9 ns 4.17 ns 3.48 ns 1.00 1.51 KB 1.00
Imposter 1 253.4 ns 1.58 ns 1.48 ns 0.60 1.06 KB 0.70
TUnitMocks 1 498.5 ns 3.76 ns 3.51 ns 1.18 1.81 KB 1.20
Moq 1 113,279.8 ns 712.06 ns 631.22 ns 267.87 9.79 KB 6.50
NSubstitute 1 7,748.5 ns 62.07 ns 55.02 ns 18.32 10.17 KB 6.75
FakeItEasy 1 7,587.5 ns 49.97 ns 41.72 ns 17.94 11.58 KB 7.68
Mockolate 10 1,535.0 ns 11.10 ns 9.84 ns 1.00 3.92 KB 1.00
Imposter 10 945.7 ns 11.61 ns 10.86 ns 0.62 2.89 KB 0.74
TUnitMocks 10 3,178.2 ns 33.68 ns 29.86 ns 2.07 8.47 KB 2.16
Moq 10 127,227.5 ns 886.40 ns 785.77 ns 82.89 24.53 KB 6.25
NSubstitute 10 25,997.1 ns 86.54 ns 72.27 ns 16.94 25.06 KB 6.39
FakeItEasy 10 27,690.8 ns 160.79 ns 134.27 ns 18.04 53.92 KB 13.75
Mockolate 1000 120,817.4 ns 658.84 ns 584.04 ns 1.00 266.89 KB 1.00
Imposter 1000 78,093.1 ns 760.04 ns 634.67 ns 0.65 203.99 KB 0.76
TUnitMocks 1000 294,754.5 ns 2,124.37 ns 1,987.14 ns 2.44 691.21 KB 2.59
Moq 1000 1,578,754.7 ns 8,144.40 ns 7,219.80 ns 13.07 1653 KB 6.19
NSubstitute 1000 2,168,775.0 ns 19,358.12 ns 18,107.60 ns 17.95 1653.23 KB 6.19
FakeItEasy 1000 2,774,055.0 ns 54,232.07 ns 50,728.71 ns 22.96 4690.81 KB 17.58
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteMethodBenchmarks
Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 131.4 ns 3.70 ns 3.46 ns 1.00 592 B 1.00
Imposter 1 178.0 ns 5.99 ns 5.60 ns 1.36 808 B 1.36
TUnitMocks 1 444.3 ns 10.75 ns 9.53 ns 3.38 2136 B 3.61
Moq 1 4,535.1 ns 36.22 ns 33.88 ns 34.53 6552 B 11.07
NSubstitute 1 5,669.0 ns 74.86 ns 70.02 ns 43.16 9816 B 16.58
FakeItEasy 1 4,631.8 ns 37.21 ns 31.07 ns 35.26 7517 B 12.70
Mockolate 10 356.9 ns 8.07 ns 7.55 ns 1.00 1096 B 1.00
Imposter 10 392.9 ns 14.63 ns 13.69 ns 1.10 1168 B 1.07
TUnitMocks 10 1,366.6 ns 32.11 ns 30.03 ns 3.83 3888 B 3.55
Moq 10 6,426.4 ns 80.80 ns 75.58 ns 18.01 10536 B 9.61
NSubstitute 10 8,897.6 ns 76.50 ns 71.56 ns 24.94 12841 B 11.72
FakeItEasy 10 8,350.2 ns 201.50 ns 188.48 ns 23.41 15754 B 14.37
Mockolate 1000 24,897.9 ns 164.35 ns 153.73 ns 1.00 56536 B 1.00
Imposter 1000 21,443.5 ns 128.33 ns 113.76 ns 0.86 40768 B 0.72
TUnitMocks 1000 98,143.2 ns 1,302.00 ns 1,154.19 ns 3.94 203633 B 3.60
Moq 1000 189,711.4 ns 1,637.26 ns 1,531.50 ns 7.62 423564 B 7.49
NSubstitute 1000 273,433.6 ns 4,303.71 ns 4,025.70 ns 10.98 378537 B 6.70
FakeItEasy 1000 446,630.2 ns 5,216.28 ns 4,624.10 ns 17.94 943763 B 16.69
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.62GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompletePropertyBenchmarks
Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 252.2 ns 1.12 ns 0.99 ns 1.00 920 B 1.00
Imposter 1 169.1 ns 1.73 ns 1.62 ns 0.67 672 B 0.73
TUnitMocks 1 417.1 ns 4.67 ns 4.36 ns 1.65 1672 B 1.82
Moq 1 7,030.5 ns 22.29 ns 18.61 ns 27.88 10568 B 11.49
NSubstitute 1 5,279.2 ns 24.00 ns 21.28 ns 20.93 11664 B 12.68
FakeItEasy 1 4,888.3 ns 23.89 ns 21.18 ns 19.38 11191 B 12.16
Mockolate 10 581.9 ns 2.97 ns 2.78 ns 1.00 1784 B 1.00
Imposter 10 340.3 ns 1.33 ns 1.24 ns 0.58 960 B 0.54
TUnitMocks 10 1,703.5 ns 11.45 ns 10.71 ns 2.93 4024 B 2.26
Moq 10 11,848.2 ns 30.76 ns 24.02 ns 20.36 18504 B 10.37
NSubstitute 10 11,796.7 ns 37.00 ns 32.80 ns 20.27 21528 B 12.07
FakeItEasy 10 12,824.7 ns 42.97 ns 40.19 ns 22.04 30780 B 17.25
Mockolate 1000 39,384.6 ns 102.94 ns 96.29 ns 1.00 96824 B 1.00
Imposter 1000 19,207.0 ns 90.42 ns 80.16 ns 0.49 32640 B 0.34
TUnitMocks 1000 122,252.6 ns 375.04 ns 332.46 ns 3.10 242489 B 2.50
Moq 1000 468,221.7 ns 1,492.06 ns 1,322.68 ns 11.89 836449 B 8.64
NSubstitute 1000 707,234.8 ns 1,986.48 ns 1,760.96 ns 17.96 1139621 B 11.77
FakeItEasy 1000 901,563.2 ns 7,316.99 ns 6,844.32 ns 22.89 2194285 B 22.66
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.74GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteIndexerBenchmarks
Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 433.7 ns 6.55 ns 6.13 ns 1.00 1.78 KB 1.00
Imposter 1 299.3 ns 8.14 ns 7.62 ns 0.69 1.19 KB 0.67
TUnitMocks 1 670.5 ns 10.19 ns 9.53 ns 1.55 2.79 KB 1.57
Moq 1 181,107.1 ns 447.41 ns 396.62 ns 417.71 37.7 KB 21.16
NSubstitute 1 63,624.4 ns 622.29 ns 582.09 ns 146.74 27.44 KB 15.40
FakeItEasy 1 66,409.2 ns 571.85 ns 506.93 ns 153.17 29.54 KB 16.58
Mockolate 10 1,321.0 ns 12.24 ns 11.45 ns 1.00 2.7 KB 1.00
Imposter 10 698.9 ns 4.27 ns 4.00 ns 0.53 2.24 KB 0.83
TUnitMocks 10 2,337.5 ns 31.41 ns 29.38 ns 1.77 5.44 KB 2.02
Moq 10 212,974.0 ns 617.95 ns 516.02 ns 161.24 47.17 KB 17.50
NSubstitute 10 79,358.9 ns 600.87 ns 562.06 ns 60.08 40.24 KB 14.93
FakeItEasy 10 82,130.6 ns 546.51 ns 511.21 ns 62.18 48.75 KB 18.09
Mockolate 1000 89,235.9 ns 146.61 ns 137.14 ns 1.00 103.24 KB 1.00
Imposter 1000 41,593.7 ns 472.21 ns 441.70 ns 0.47 118.26 KB 1.15
TUnitMocks 1000 171,092.6 ns 987.65 ns 923.85 ns 1.92 293.74 KB 2.85
Moq 1000 838,002.6 ns 6,206.32 ns 5,805.39 ns 9.39 1023.85 KB 9.92
NSubstitute 1000 1,268,936.4 ns 10,644.47 ns 9,956.84 ns 14.22 1480.2 KB 14.34
FakeItEasy 1000 1,158,567.4 ns 12,263.54 ns 10,240.61 ns 12.98 2169.29 KB 21.01
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.33GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteEventBenchmarks
Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 80.52 ns 0.446 ns 0.396 ns 1.00 712 B 1.00
Imposter 100.12 ns 0.455 ns 0.425 ns 1.24 512 B 0.72
TUnitMocks 89.65 ns 0.430 ns 0.402 ns 1.11 1392 B 1.96
Moq 6,935.18 ns 95.778 ns 84.905 ns 86.13 12760 B 17.92
NSubstitute 2,595.68 ns 10.075 ns 8.931 ns 32.24 9192 B 12.91
FakeItEasy 108,949.64 ns 1,267.788 ns 1,123.861 ns 1,353.04 15619 B 21.94
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.54GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CallbackBenchmarks
Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 172.6 ns 3.84 ns 3.59 ns 1.00 800 B 1.00
Imposter 180.6 ns 2.64 ns 2.47 ns 1.05 616 B 0.77
TUnitMocks 424.0 ns 4.94 ns 3.86 ns 2.46 2056 B 2.57
Moq 36,182.4 ns 142.43 ns 133.22 ns 209.75 9046 B 11.31
NSubstitute 3,236.5 ns 23.03 ns 21.54 ns 18.76 7864 B 9.83
FakeItEasy 2,856.0 ns 32.77 ns 30.65 ns 16.56 6924 B 8.65
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 2.70GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

AsyncBenchmarks
Async N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 383.1 ns 2.28 ns 2.13 ns 1.00 1 KB 1.00
Imposter 1 486.3 ns 4.72 ns 4.42 ns 1.27 1.58 KB 1.58
TUnitMocks 1 1,233.7 ns 8.25 ns 7.71 ns 3.22 4.36 KB 4.36
Moq 1 150,252.6 ns 1,108.43 ns 1,036.82 ns 392.21 22.76 KB 22.76
NSubstitute 1 13,230.0 ns 97.07 ns 90.80 ns 34.53 19.28 KB 19.28
FakeItEasy 1 8,664.4 ns 52.48 ns 49.09 ns 22.62 13.99 KB 13.99
Mockolate 10 1,574.4 ns 5.23 ns 4.64 ns 1.00 2.97 KB 1.00
Imposter 10 1,551.0 ns 5.46 ns 5.11 ns 0.99 3.41 KB 1.15
TUnitMocks 10 5,088.7 ns 33.96 ns 30.10 ns 3.23 10.29 KB 3.47
Moq 10 186,324.7 ns 863.08 ns 765.10 ns 118.35 40.18 KB 13.54
NSubstitute 10 23,170.8 ns 114.05 ns 106.68 ns 14.72 30.58 KB 10.30
FakeItEasy 10 35,678.2 ns 189.04 ns 167.58 ns 22.66 59.08 KB 19.90
Mockolate 1000 137,372.4 ns 394.71 ns 349.90 ns 1.00 219.53 KB 1.00
Imposter 1000 119,911.8 ns 316.31 ns 280.40 ns 0.87 204.5 KB 0.93
TUnitMocks 1000 434,092.7 ns 1,451.71 ns 1,212.24 ns 3.16 687.49 KB 3.13
Moq 1000 3,129,762.4 ns 15,257.77 ns 14,272.13 ns 22.78 1890.74 KB 8.61
NSubstitute 1000 1,024,521.7 ns 5,797.04 ns 5,138.93 ns 7.46 1200.45 KB 5.47
FakeItEasy 1000 3,305,805.3 ns 54,973.29 ns 51,422.05 ns 24.06 4933.25 KB 22.47
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 2.99GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

SequenceBenchmarks
Sequence Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 2.079 μs 0.0455 μs 0.0425 μs 1.00 2.23 KB 1.00
Imposter 1.967 μs 0.0281 μs 0.0234 μs 0.95 2.23 KB 1.00
TUnitMocks 2.449 μs 0.0210 μs 0.0175 μs 1.18 3.27 KB 1.46
Moq 7.760 μs 0.0978 μs 0.0915 μs 3.73 10.79 KB 4.83
NSubstitute 6.495 μs 0.0515 μs 0.0456 μs 3.12 14.29 KB 6.40
FakeItEasy 6.076 μs 0.0712 μs 0.0666 μs 2.92 14.28 KB 6.39
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.48GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

@vbreuss
vbreuss force-pushed the fix/params-type-parameter-object branch 3 times, most recently from cc961b0 to 269c743 Compare November 22, 2026 06:02
@sonarqubecloud

Copy link
Copy Markdown

A matcher for a `params T[]` parameter of a generic method was captured as a value. The overload that takes values was promoted with `[OverloadResolutionPriority(int.MaxValue)]`, so for `Only<T>(params T[] args)` and `Sum<T>(int level, params T[] args)` the call `Setup.Only(It.IsAny<string>())` inferred `T` as the matcher type, and with the type argument `object`, to which every matcher converts, `Verify.Only<object>(It.IsAny<object>())`, `Only<object>(It.IsAny<object[]>())`, `Only<object>(Match.AnyParameters())` and `Sum<object>(1, It.IsAny<object>())` bound the overload that takes values and matched nothing. No call could set up or verify such a method with a matcher.

A `params` array of a type parameter now blocks that promotion, as #1098 does for a parameter of type `T`: whenever a matcher overload is applicable, it outranks the overload that takes values, and it infers the type argument from the matcher.

- A lone matcher for the array matches the whole array: `Only<object>(It.IsAny<object[]>())`.
- Matchers for the elements match element by element: `Only(It.IsAny<string>())` sets up `Only<string>`, and `Only<object>(It.IsAny<object>())` matches one element. A `null` element next to them matches a `null` element, also for a nullable type argument.
- `Match.AnyParameters()` and `Match.Parameters(…)` for a lone `params T[]` stand for the parameters.
- A lone `null` for a lone `params T[]` stands for a `null` array, and a `null` for the array next to matchers for the other parameters binds the matcher for the whole array, so it does as well: `Setup.Sum<object>(It.IsAny<int>(), null)`. Above four parameters no overload combines values with the matcher for the whole array.
- A lone `null` or `default` for a parameter before such an array forwards to the overload that takes values, as #1098 does for a parameter of type `T`, so `Setup.Sum<object>(default)` stands for `Sum<object>(0)` instead of any parameters. The same holds for a non-generic sibling with a value type parameter whose all-values overload is not promoted, such as `F(int value)` beside `F<T>(T value)`, which #1098 stopped promoting, or beside `F<T>(params T[] values)`, which this change stops promoting.
- A lone `params T[]` gets the overload without arguments that `params object[]` has, so `Setup.Only<object>().AnyParameters()` keeps working, unless a sibling with as many type parameters takes a call without arguments, such as `Only<T>(T value = default!)`, which the call on the mock binds; this also holds without `OverloadResolutionPriorityAttribute`. A lone non-generic `params` array beside such a generic sibling, such as `Pick(params string[])` beside `Pick<T>(params T[])`, gets that overload as well, so `Setup.Pick().AnyParameters()` keeps working, unless a sibling with as many type parameters takes a call without arguments or another lone `params` array with as many type parameters, such as `Pick(params int[])`, would get the same overload. A lone `null` for such a non-generic array binds the `IParameters` overload, as for a lone `params T[]`, which reports CS8625 and Mockolate0006, unless the array is nullable, such as `params string?[]?`, where it forwards to the overload that takes values and throws an `ArgumentNullException`, as before.

Matchers that still bind the overload that takes values, which only happens for the type argument `object`, are recognized at runtime, as #1071 does for a parameter of type `T`: `Only<object>(It.IsAny<object>(), "a")` and `Sum<object>(1, "a", It.IsAny<object>())` match element by element and compare the plain values next to them by equality. This goes beyond `params object[]`, where a matcher next to plain values still matches nothing. A matcher for the whole array next to other elements matches one element there, as for `params object[]`. Plain values keep the comparison they had. Mockolate0007 no longer reports a matcher for an element of such an array that is recognized at runtime, as it already did not for a parameter of type `T`, and still reports a matcher for the whole array next to other elements.

```csharp
sut.Only<object>(1, "a");
sut.Only("a");
sut.Mock.Verify.Only<object>(It.IsAny<object>(), "a").Once(); // failed before
sut.Mock.Verify.Only<object>(It.IsAny<object[]>()).Once(); // failed before
sut.Mock.Verify.Only(It.IsAny<string>()).Once(); // failed before
sut.Mock.Verify.Only<object>(1, "a").Once(); // as before
```

One call behaves differently for every type argument of such a method: a `null` array now matches a `null` array instead of throwing an `ArgumentNullException`, for example `Setup.Sum<string>(1, null)` or `Verify.Only<object>(null)`, as #1031 defined it for `params object[]`. A lone `null` for a lone `params T[]` that matched any parameters, such as `Setup.Ln<object>(null)` beside `Ln<T, U>(T a = default!)`, now matches a `null` array as well. A `params` array of another element type, such as `params string[]`, still throws, unless a generic sibling stops promoting its overload that takes values: `Setup.Lk("s", null)` for `Lk(string s, params string[])` beside `Lk<T>(string s, params T[])` now matches a `null` array, and `Setup.N(null)` for `N(params string[])` beside `N<T>(params T[])` now binds the `IParameters` overload and matches any parameters instead of throwing.

This is a narrow source break, as in #1098: `.AnyParameters()` is no longer available after a matcher or a `null` for such a `params` array, or after a lone `null` or `default` or one next to such an array for another parameter (e.g. `Setup.Sum<object>(1, null).AnyParameters()` or `Setup.Sum<object>(default).AnyParameters()`), and after a `default` for a non-generic sibling with as many parameters, such as `Setup.W(1, default).AnyParameters()` for `W(int a, int b)` beside `W<T>(int a, params T[] values)`, because such a call now binds a matcher or `IParameters` overload. `Setup.K().AnyParameters()` is no longer available for `K(params string[])` beside `K<T>(params T[])` and `K(string a = "")`, because `Setup.K()` now sets up `K(string a = "")`, which the call on the mock `K()` binds, instead of a method that `K()` never calls; the same holds for a generic sibling, such as `Setup.Optional<object>().AnyParameters()` for `Optional<T>(params T[])` beside `Optional<T>(T value = default!)`. Use `Match.AnyParameters()` instead. A lone `default` that was ambiguous before can bind now: for `Ws<T>(int value)` beside `Ws<T>(params T[] values)`, `Setup.Ws<object>(default)` was CS0121 and now sets up a `null` array, as the call on the mock `Ws<object>(default)` is ambiguous as well. A lone `null` for such an array stays or becomes CS0121 beside a generic sibling with as many parameters whose last one takes a `null` as well, such as `Gm<T>(params T[][])`, `Pc<T>(params string[])` or `Pe<T>(IEnumerable<T>)`, where it did not work on main either; a sibling that takes an array of `T[]`, such as `Pn<T>(T[][])`, keeps taking it, as the call on the mock does. A lone `null` can also become ambiguous, as #1098 made it for a parameter of type `T`: for `Bo(bool value)` and `Bo(int? value)` beside `Bo<T>(params T[] values)`, `Setup.Bo(null)` is CS0121 between the matcher overloads of both siblings, whose overloads that take values are no longer promoted.

Not covered: a matcher for an element next to plain values without the type argument `object`, such as `Sum(1, It.IsAny<string>())`, which infers `T` as the matcher type, and inferring the type argument from a matcher without `OverloadResolutionPriorityAttribute`.
@vbreuss
vbreuss force-pushed the fix/params-type-parameter-object branch from 269c743 to f97b139 Compare November 22, 2026 06:54

This branch has not been deployed

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant