Skip to content

fix: infer the type argument of a generic method from a matcher in setups and verifications - #1098

Merged
vbreuss merged 5 commits into
mainfrom
fix/generic-method-matcher-type-inference
Nov 21, 2026
Merged

vbreuss merged 5 commits into
mainfrom
fix/generic-method-matcher-type-inference

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

On a generic method such as ILogger.BeginScope<TState>(TState state), Setup.BeginScope(It.IsAny<int>()) and Verify.BeginScope(It.IsAny<int>()) compiled but never matched (#1081). The overload that takes values was promoted with [OverloadResolutionPriority(int.MaxValue)], and without explicit type arguments the compiler infers TState from the matcher itself, so the call bound BeginScope<IParameterWithCallback<int>>(value) instead of BeginScope<int>(matcher), and the setup was registered for the wrong closed method.

A parameter typed with a method type parameter that a matcher can satisfy (MayHoldMatcher) now blocks that promotion, exactly like a declared object parameter already does: whenever a matcher overload is applicable, it outranks the overload that takes values, and it infers the type argument from the matcher. To keep the other spellings binding as before, the same rules as for object apply: a lone null or default with explicit type arguments (Setup.Plain<string>(null)) forwards from the IParameters? overload to the overload that takes values, so it still stands for that argument and not for any parameters (only same-arity generic siblings can capture that call), and a type parameter sibling keeps the all-values overload of a lone params object[] unpromoted, so Setup.M("a") still prefers M<string> as the call on the mock does. Above four parameters, where classic mode emits no mixed overloads, the generator now also emits one that takes matchers for the type parameter slots and values elsewhere, also for the overloads that leave out an optional parameter. A matcher for an in parameter is now wrapped covariantly like any other value parameter; before, In<object>(It.IsAny<string>()) and Equals(in object?) setups with a derived-type matcher threw InvalidCastException.

An alternative was an analyzer that reports a type argument inferred as a matcher type. It was not chosen because a generator fix exists that makes the natural spelling work at every arity, while explicit type arguments, literals, null/default, Match.AnyParameters() and params keep their meaning.

This is a narrow source break: .AnyParameters() is no longer available after an explicit null/default or a matcher in a type parameter slot (e.g. Setup.Plain<int>(default).AnyParameters()), because such a call now binds the matcher or IParameters overload, as it already did for an object parameter. Use Match.AnyParameters() instead. A nullable matcher for a non-nullable type argument (Verify.Plain<object>(It.IsAny<object?>())) now reports CS8620, as it does for an object parameter.

ILogger logger = ILogger.CreateMock();
logger.Mock.Setup.BeginScope(It.IsAny<int>()).Returns(scope); // sets up BeginScope<int>
logger.BeginScope(42); // returns scope
logger.Mock.Verify.BeginScope(It.IsAny<int>()).Once(); // passes

@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] The latest changes appear safe to merge.

Summary

The PR makes generic setups and verifications infer their type arguments from matchers. The latest update preserves null matching beside a generic overload.

  • Generic setup and verify calls infer method types from matchers.
  • Explicit null and default arguments keep their literal meaning in generic setups.
  • Matchers for derived types work with base-typed in parameters.

The previously numbered finding is fixed. No new actionable issues were found in the latest changes.

Reviews (5) · Last reviewed commit: "fix: keep a lone null next to a generic ..." · 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 31 files

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

Turn on auto-fix | Re-trigger cubic

Comment thread Docs/pages/06-parameter-matching.md Resolved
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Test Results

    58 files  ±  0      58 suites  ±0   1h 59m 19s ⏱️ - 18m 45s
 5 360 tests + 12   5 360 ✅ + 12  0 💤 ±0  0 ❌ ±0 
53 464 runs  +132  53 464 ✅ +132  0 💤 ±0  0 ❌ ±0 

Results for commit f9362c7. ± Comparison against base commit d1b3146.

This pull request removes 23 and adds 29 tests. Note that renamed tests count towards both.
 x-myHeader3: baz, True)
)
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.Tests.GenericMethodClosedOverObjectTests ‑ ParametersMatch_WithAnyParameters_ShouldSetUpTheMethod
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
…
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
)
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
int GetHashCode();, public override bool Equals(object obj), public override int GetHashCode())
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
int GetHashCode();, public override bool Equals(object obj), public override int GetHashCode())
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object obj);, public override string ToString(), public override bool Equals(object obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object? obj);
int GetHashCode();, public override string ToString(), public override int GetHashCode(), public override bool Equals(object? obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
int GetHashCode();, public override string ToString(), public override int GetHashCode())
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object obj);, public override string ToString(), public override bool Equals(object obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object? obj);
int GetHashCode();, public override string ToString(), public override int GetHashCode(), public override bool Equals(object? obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
int GetHashCode();, public override string ToString(), public override int GetHashCode())
…

♻️ 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,576 B 105,577 B 🔴 +1 B (±0%)
CompleteWorkflowBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1.728 μs 0.0102 μs 0.0095 μs 1.00 5.14 KB 1.00
Imposter 1.978 μs 0.0289 μs 0.0270 μs 1.14 7.21 KB 1.40
TUnitMocks 4.743 μs 0.0657 μs 0.0615 μs 2.75 16.95 KB 3.30
Moq 799.531 μs 3.3827 μs 3.1642 μs 462.79 90.66 KB 17.64
NSubstitute 254.199 μs 1.7191 μs 1.5239 μs 147.14 53.08 KB 10.33
FakeItEasy 283.325 μs 1.3597 μs 1.2054 μs 164.00 63.97 KB 12.44
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  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 12.02 ns 0.564 ns 0.528 ns 1.00 136 B 1.00
Imposter 72.38 ns 1.371 ns 1.145 ns 6.03 504 B 3.71
TUnitMocks 21.42 ns 0.514 ns 0.480 ns 1.79 216 B 1.59
Moq 919.54 ns 10.315 ns 9.648 ns 76.65 2088 B 15.35
NSubstitute 1,258.18 ns 35.142 ns 32.872 ns 104.87 5032 B 37.00
FakeItEasy 1,062.25 ns 22.276 ns 18.602 ns 88.54 2755 B 20.26
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 3.89GHz, 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 368.6 ns 6.02 ns 5.63 ns 1.00 1.51 KB 1.00
Imposter 1 218.4 ns 2.28 ns 1.90 ns 0.59 1.06 KB 0.70
TUnitMocks 1 446.3 ns 8.85 ns 8.28 ns 1.21 1.81 KB 1.20
Moq 1 36,255.7 ns 157.55 ns 139.67 ns 98.37 9.77 KB 6.48
NSubstitute 1 5,383.3 ns 53.52 ns 50.06 ns 14.61 10.01 KB 6.64
FakeItEasy 1 4,482.5 ns 111.21 ns 104.02 ns 12.16 11.57 KB 7.67
Mockolate 10 1,398.3 ns 17.82 ns 16.67 ns 1.00 3.92 KB 1.00
Imposter 10 797.4 ns 12.75 ns 11.93 ns 0.57 2.89 KB 0.74
TUnitMocks 10 2,868.9 ns 45.36 ns 42.43 ns 2.05 8.47 KB 2.16
Moq 10 50,773.6 ns 670.19 ns 626.90 ns 36.32 24.52 KB 6.25
NSubstitute 10 15,140.3 ns 155.40 ns 137.76 ns 10.83 22.93 KB 5.85
FakeItEasy 10 18,945.8 ns 372.63 ns 348.55 ns 13.55 53.92 KB 13.75
Mockolate 1000 115,063.4 ns 1,743.24 ns 1,630.62 ns 1.00 266.89 KB 1.00
Imposter 1000 66,261.0 ns 697.28 ns 652.24 ns 0.58 203.99 KB 0.76
TUnitMocks 1000 250,550.6 ns 2,537.60 ns 2,249.52 ns 2.18 691.21 KB 2.59
Moq 1000 984,370.2 ns 8,445.90 ns 7,052.71 ns 8.56 1652.9 KB 6.19
NSubstitute 1000 995,537.7 ns 2,354.40 ns 2,087.12 ns 8.65 1434.38 KB 5.37
FakeItEasy 1000 1,879,562.2 ns 53,425.08 ns 49,973.85 ns 16.34 4690.6 KB 17.57
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  

CompleteMethodBenchmarks
Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 107.8 ns 0.91 ns 0.85 ns 1.00 608 B 1.00
Imposter 1 139.4 ns 0.69 ns 0.61 ns 1.29 728 B 1.20
TUnitMocks 1 355.9 ns 4.78 ns 4.24 ns 3.30 2088 B 3.43
Moq 1 114,357.6 ns 485.88 ns 430.72 ns 1,060.68 14914 B 24.53
NSubstitute 1 4,411.1 ns 45.96 ns 40.75 ns 40.91 9320 B 15.33
FakeItEasy 1 4,044.9 ns 49.57 ns 43.95 ns 37.52 8160 B 13.42
Mockolate 10 288.8 ns 1.69 ns 1.49 ns 1.00 1112 B 1.00
Imposter 10 276.4 ns 2.50 ns 2.33 ns 0.96 1088 B 0.98
TUnitMocks 10 1,060.7 ns 12.02 ns 10.66 ns 3.67 3840 B 3.45
Moq 10 118,321.2 ns 600.23 ns 532.09 ns 409.66 18906 B 17.00
NSubstitute 10 6,598.4 ns 58.22 ns 54.46 ns 22.85 12344 B 11.10
FakeItEasy 10 6,484.6 ns 67.42 ns 63.06 ns 22.45 16394 B 14.74
Mockolate 1000 20,829.6 ns 179.94 ns 168.31 ns 1.00 56552 B 1.00
Imposter 1000 17,063.0 ns 181.90 ns 170.15 ns 0.82 40688 B 0.72
TUnitMocks 1000 78,232.5 ns 589.40 ns 551.33 ns 3.76 203585 B 3.60
Moq 1000 287,177.0 ns 2,076.88 ns 1,942.71 ns 13.79 431938 B 7.64
NSubstitute 1000 231,311.0 ns 2,130.70 ns 1,993.06 ns 11.11 378037 B 6.68
FakeItEasy 1000 274,727.1 ns 2,663.26 ns 2,360.91 ns 13.19 944402 B 16.70
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.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-v4

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

CompletePropertyBenchmarks
Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 189.3 ns 3.56 ns 3.33 ns 1.00 920 B 1.00
Imposter 1 131.9 ns 0.64 ns 0.57 ns 0.70 672 B 0.73
TUnitMocks 1 336.4 ns 2.20 ns 1.84 ns 1.78 1672 B 1.82
Moq 1 7,547.5 ns 30.58 ns 25.53 ns 39.87 10568 B 11.49
NSubstitute 1 5,266.3 ns 34.41 ns 32.19 ns 27.82 11688 B 12.70
FakeItEasy 1 5,174.4 ns 33.12 ns 29.36 ns 27.34 11196 B 12.17
Mockolate 10 461.9 ns 3.42 ns 3.20 ns 1.00 1784 B 1.00
Imposter 10 251.7 ns 1.03 ns 0.96 ns 0.54 960 B 0.54
TUnitMocks 10 1,170.9 ns 2.23 ns 1.86 ns 2.53 4024 B 2.26
Moq 10 12,168.0 ns 42.41 ns 37.60 ns 26.34 18505 B 10.37
NSubstitute 10 12,403.5 ns 141.72 ns 132.56 ns 26.85 21553 B 12.08
FakeItEasy 10 12,261.6 ns 47.41 ns 42.02 ns 26.55 30785 B 17.26
Mockolate 1000 31,975.8 ns 175.35 ns 146.43 ns 1.00 96824 B 1.00
Imposter 1000 12,948.9 ns 68.61 ns 60.82 ns 0.40 32641 B 0.34
TUnitMocks 1000 88,812.2 ns 450.34 ns 421.24 ns 2.78 242489 B 2.50
Moq 1000 516,582.8 ns 4,103.03 ns 3,637.23 ns 16.16 836541 B 8.64
NSubstitute 1000 781,203.3 ns 9,727.38 ns 9,099.00 ns 24.43 1139726 B 11.77
FakeItEasy 1000 885,087.2 ns 3,912.12 ns 3,266.80 ns 27.68 2194325 B 22.66
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.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-v4

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

CompleteIndexerBenchmarks
Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 431.5 ns 5.35 ns 4.46 ns 1.00 1.64 KB 1.00
Imposter 1 283.5 ns 2.51 ns 2.23 ns 0.66 1.07 KB 0.65
TUnitMocks 1 698.5 ns 7.22 ns 6.75 ns 1.62 2.72 KB 1.66
Moq 1 228,288.3 ns 1,493.16 ns 1,396.71 ns 529.11 20.32 KB 12.38
NSubstitute 1 10,104.5 ns 38.96 ns 34.53 ns 23.42 12.73 KB 7.76
FakeItEasy 1 10,515.4 ns 61.51 ns 51.37 ns 24.37 13.27 KB 8.09
Mockolate 10 1,269.7 ns 19.90 ns 18.62 ns 1.00 2.55 KB 1.00
Imposter 10 749.2 ns 18.48 ns 17.28 ns 0.59 2.13 KB 0.83
TUnitMocks 10 2,359.8 ns 19.96 ns 18.67 ns 1.86 5.37 KB 2.10
Moq 10 241,199.8 ns 1,547.68 ns 1,292.39 ns 190.00 29.68 KB 11.62
NSubstitute 10 26,344.5 ns 178.14 ns 166.63 ns 20.75 25.52 KB 9.99
FakeItEasy 10 23,092.8 ns 312.74 ns 292.53 ns 18.19 32.47 KB 12.71
Mockolate 1000 94,003.7 ns 944.96 ns 883.92 ns 1.00 103.1 KB 1.00
Imposter 1000 46,955.4 ns 764.17 ns 714.80 ns 0.50 118.15 KB 1.15
TUnitMocks 1000 178,987.7 ns 2,583.49 ns 2,290.20 ns 1.90 293.67 KB 2.85
Moq 1000 1,016,748.4 ns 16,114.44 ns 14,285.03 ns 10.82 1006.46 KB 9.76
NSubstitute 1000 1,687,463.3 ns 9,124.82 ns 7,124.06 ns 17.95 1465.59 KB 14.21
FakeItEasy 1000 1,386,785.0 ns 33,471.04 ns 31,308.83 ns 14.75 2153 KB 20.88
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  

CompleteEventBenchmarks
Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 174.2 ns 5.30 ns 4.96 ns 1.00 712 B 1.00
Imposter 182.5 ns 1.08 ns 0.96 ns 1.05 512 B 0.72
TUnitMocks 175.7 ns 4.74 ns 4.43 ns 1.01 1392 B 1.96
Moq 14,982.3 ns 29.56 ns 27.65 ns 86.06 12760 B 17.92
NSubstitute 6,067.7 ns 46.55 ns 41.27 ns 34.85 9192 B 12.91
FakeItEasy 215,735.2 ns 1,359.63 ns 1,135.35 ns 1,239.17 15586 B 21.89
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.66GHz, 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  

CallbackBenchmarks
Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 245.9 ns 1.49 ns 1.39 ns 1.00 800 B 1.00
Imposter 246.7 ns 1.16 ns 0.97 ns 1.00 616 B 0.77
TUnitMocks 614.2 ns 1.65 ns 1.54 ns 2.50 2056 B 2.57
Moq 46,263.7 ns 298.04 ns 248.88 ns 188.12 9113 B 11.39
NSubstitute 4,208.1 ns 36.91 ns 34.53 ns 17.11 7864 B 9.83
FakeItEasy 3,735.8 ns 29.55 ns 27.65 ns 15.19 6924 B 8.65
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  

AsyncBenchmarks
Async N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 263.2 ns 4.12 ns 3.44 ns 1.00 1 KB 1.00
Imposter 1 358.4 ns 6.22 ns 5.81 ns 1.36 1.58 KB 1.58
TUnitMocks 1 1,031.3 ns 39.15 ns 36.62 ns 3.92 4.36 KB 4.36
Moq 1 208,456.5 ns 1,257.89 ns 1,176.63 ns 792.18 22.76 KB 22.76
NSubstitute 1 11,718.2 ns 138.51 ns 115.66 ns 44.53 19.39 KB 19.39
FakeItEasy 1 8,463.2 ns 223.11 ns 208.70 ns 32.16 14 KB 14.00
Mockolate 10 1,281.4 ns 21.52 ns 19.08 ns 1.00 2.97 KB 1.00
Imposter 10 1,175.4 ns 45.63 ns 42.68 ns 0.92 3.41 KB 1.15
TUnitMocks 10 3,908.0 ns 135.62 ns 126.86 ns 3.05 10.29 KB 3.47
Moq 10 239,596.2 ns 1,859.65 ns 1,552.89 ns 187.01 40.19 KB 13.54
NSubstitute 10 21,187.4 ns 562.72 ns 526.37 ns 16.54 30.7 KB 10.34
FakeItEasy 10 32,710.4 ns 1,557.09 ns 1,456.51 ns 25.53 59.08 KB 19.90
Mockolate 1000 105,092.7 ns 523.96 ns 437.53 ns 1.00 219.53 KB 1.00
Imposter 1000 87,827.6 ns 1,342.60 ns 1,190.18 ns 0.84 204.5 KB 0.93
TUnitMocks 1000 337,928.5 ns 5,866.97 ns 5,200.91 ns 3.22 687.49 KB 3.13
Moq 1000 3,183,034.2 ns 32,602.57 ns 28,901.33 ns 30.29 1891.03 KB 8.61
NSubstitute 1000 1,020,102.0 ns 14,604.43 ns 13,660.99 ns 9.71 1202.63 KB 5.48
FakeItEasy 1000 3,158,730.5 ns 77,152.10 ns 68,393.33 ns 30.06 4933.41 KB 22.47
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.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-v4

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

SequenceBenchmarks
Sequence Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 3.628 μs 0.0421 μs 0.0394 μs 1.00 2.23 KB 1.00
Imposter 3.100 μs 0.0358 μs 0.0335 μs 0.85 2.23 KB 1.00
TUnitMocks 4.086 μs 0.0497 μs 0.0465 μs 1.13 3.27 KB 1.46
Moq 12.795 μs 0.0843 μs 0.0747 μs 3.53 10.79 KB 4.83
NSubstitute 11.436 μs 0.0456 μs 0.0381 μs 3.15 14.67 KB 6.57
FakeItEasy 11.837 μs 0.0400 μs 0.0334 μs 3.26 14.27 KB 6.39
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.01GHz, 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/generic-method-matcher-type-inference branch from 017a261 to 861bd79 Compare November 21, 2026 08:41

@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 6 files (changes from recent commits).

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

Turn on auto-fix | Re-trigger cubic

…etups and verifications

On a generic method such as `ILogger.BeginScope<TState>(TState state)`, `Setup.BeginScope(It.IsAny<int>())` and `Verify.BeginScope(It.IsAny<int>())` compiled but never matched (#1081). The overload that takes values was promoted with `[OverloadResolutionPriority(int.MaxValue)]`, and without explicit type arguments the compiler infers `TState` from the matcher itself, so the call bound `BeginScope<IParameterWithCallback<int>>(value)` instead of `BeginScope<int>(matcher)`, and the setup was registered for the wrong closed method.

A parameter typed with a method type parameter that a matcher can satisfy (`MayHoldMatcher`) now blocks that promotion, exactly like a declared `object` parameter already does: whenever a matcher overload is applicable, it outranks the overload that takes values, and it infers the type argument from the matcher. To keep the other spellings binding as before, the same rules as for `object` apply: a lone `null` or `default` with explicit type arguments (`Setup.Plain<string>(null)`) forwards from the `IParameters?` overload to the overload that takes values, so it still stands for that argument and not for any parameters (only same-arity generic siblings can capture that call), and a type parameter sibling keeps the all-values overload of a lone `params object[]` unpromoted, so `Setup.M("a")` still prefers `M<string>` as the call on the mock does. Above four parameters, where classic mode emits no mixed overloads, the generator now also emits one that takes matchers for the type parameter slots and values elsewhere, also for the overloads that leave out an optional parameter. A matcher for an `in` parameter is now wrapped covariantly like any other value parameter; before, `In<object>(It.IsAny<string>())` and `Equals(in object?)` setups with a derived-type matcher threw `InvalidCastException`.

Because the overload that takes values is no longer promoted, a lone parameters match such as `Setup.Plain<object>(Match.AnyParameters())` now binds the `IParameters` overload. The runtime detection of a parameters match in the overload that takes values (#1071) is therefore unreachable, so the generated check and the unreleased `ParametersVoidMethodSetup`/`ParametersReturnMethodSetup` types are removed. A parameters match passed typed as `object` is now compared as a value, as for a declared `object` parameter.

An alternative was an analyzer that reports a type argument inferred as a matcher type. It was not chosen because a generator fix exists that makes the natural spelling work at every arity, while explicit type arguments, literals, `null`/`default`, `Match.AnyParameters()` and `params` keep their meaning.

This is a narrow source break: `.AnyParameters()` is no longer available after an explicit `null`/`default` or a matcher in a type parameter slot (e.g. `Setup.Plain<int>(default).AnyParameters()`), because such a call now binds the matcher or `IParameters` overload, as it already did for an `object` parameter. Use `Match.AnyParameters()` instead. A nullable matcher for a non-nullable type argument (`Verify.Plain<object>(It.IsAny<object?>())`) now reports CS8620, as it does for an `object` parameter.

```csharp
ILogger logger = ILogger.CreateMock();
logger.Mock.Setup.BeginScope(It.IsAny<int>()).Returns(scope); // sets up BeginScope<int>
logger.BeginScope(42); // returns scope
logger.Mock.Verify.BeginScope(It.IsAny<int>()).Once(); // passes
```
A matcher for one type parameter next to a value for another still bound the overload that takes values and inferred the matcher type, and the overloads that leave out a type-parameter argument had no such overload to forward to. The combinations are capped at four type-parameter slots so that the generated overloads stay bounded.
A matcher next to an array matcher for `params object[]` still inferred the matcher type, a value next to a non-generic sibling set up the sibling instead of the generic method that the call binds, and a method with five type-parameter slots and a left-out argument did not compile.
The special cases do not depend on their own type, so they join an existing coverage type without a snapshot of their own (#1100). Union mode never applies to a method with a same-named sibling, so the sibling check there had no effect.
@vbreuss
vbreuss force-pushed the fix/generic-method-matcher-type-inference branch from 230fcc6 to 2aded11 Compare November 21, 2026 10:50
@greptile-apps

This comment has been minimized.

Without the promoted all-values overload, a lone null for a non-generic method next to a generic sibling bound the IParameters overload and matched every call.
@vbreuss
vbreuss enabled auto-merge (squash) November 21, 2026 11:00
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit dacdee7 into main Nov 21, 2026
28 checks passed
@vbreuss
vbreuss deleted the fix/generic-method-matcher-type-inference branch November 21, 2026 11:14
github-actions Bot added a commit that referenced this pull request Nov 21, 2026
…ic method from a matcher in setups and verifications (#1098) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Nov 21, 2026
…ic method from a matcher in setups and verifications (#1098) by Valentin Breuß
vbreuss added a commit that referenced this pull request Nov 21, 2026
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. The rules of #1031 for `params object[]` apply:

- 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.
- `Match.AnyParameters()` and `Match.Parameters(…)` for a lone `params T[]` stand for the parameters.
- A lone `null` for a lone `params T[]` forwards to the overload that takes values, as #1098 does for a parameter of type `T`, so it stands for a `null` array.
- A lone `params T[]` gets the overload without arguments that `params object[]` has, so `Setup.Only<object>().AnyParameters()` keeps working.

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. Plain values keep the comparison they had.

```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 `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 lone `null` for such a `params` array, or after a `null` or `default` literal for another parameter next to it (e.g. `Setup.Sum<object>(1, null).AnyParameters()`), because such a call now binds a matcher or `IParameters` overload. Use `Match.AnyParameters()` instead.

Consumers without `OverloadResolutionPriorityAttribute` get an overload that leaves out the `params` array; it keeps matching only an empty array.

Not covered, as for `params object[]`: 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.
vbreuss added a commit that referenced this pull request Nov 21, 2026
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, and a lone `null` for it forwards to the overload that takes values.

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 `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 added a commit that referenced this pull request Nov 21, 2026
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, and a lone `null` for it forwards to the overload that takes values.

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 `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 added a commit that referenced this pull request Nov 21, 2026
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 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.

```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 `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 added a commit that referenced this pull request Nov 21, 2026
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 keeps binding the `IParameters` overload, which reports CS8625 and Mockolate0006, unless the array is nullable, such as `params string?[]?`, where it forwards to the overload that takes values, as the call on the mock takes a lone `null` as well.

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 `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. A lone `null` can 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 added a commit that referenced this pull request Nov 22, 2026
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 keeps binding the `IParameters` overload, 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 `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. `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. 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` can 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 added a commit that referenced this pull request Nov 22, 2026
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` can 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 added a commit that referenced this pull request Nov 22, 2026
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 added a commit that referenced this pull request Nov 22, 2026
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 that takes a `params` array or another reference type in that place, such as `Gm<T>(params T[][])`, `Pc<T>(params string[])` or `Pe<T>(IEnumerable<T>)`, where it did not work on main either or the call on the mock is ambiguous as well; a sibling that takes an array there, such as `Pn<T>(T[][])` or `Za<T>(string[])`, 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 added a commit that referenced this pull request Nov 22, 2026
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)`, unless a sibling with as many parameters may take a `null` in that place. 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 `null` for such an array stays or becomes CS0121 beside a sibling with as many parameters that may take a `null` in that place, instead of binding a method that the call on the mock may not bind. It did not work on main beside `Gm<T>(params T[][])`, `Pc<T>(params string[])` or `Pe<T>(IEnumerable<T>)` either, but on main `Setup.Za<object>(null)` for `Za<T>(string[] a)` beside `Za<T>(params T[] args)` and `Setup.Ya<object>(null, null)` for `Ya<T>(string x, string y)` beside `Ya<T>(int level, params T[] args)` set up the sibling, and `.AnyParameters()` was available after them. Only a sibling that takes an array of `T[]` there, such as `Pn<T>(T[][])`, keeps taking a `null`, 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`.
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.

Setup or verification of a generic method with a matcher silently binds to the wrong type argument

1 participant