Repository navigation
Description
Mockolate emits for a method an overload that takes a value for every parameter and promotes it with [OverloadResolutionPriority(int.MaxValue)]. Priority filtering runs before betterness, so that overload wins whenever it is applicable. ParametersBlockAllValuesPromotion in Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs skips the promotion when a parameter is declared object (span[i].Type.SpecialType == SpecialType.System_Object), because every matcher converts to object.
The bug
A parameter typed with a type parameter never has SpecialType.System_Object, so the promotion stays. When the call closes the type parameter over object, the all-values overload accepts every matcher and wins:
public interface IFoo
{
void Plain<T>(T value);
void AcceptDependent<T, TBase>(T? value, TBase? other) where T : TBase where TBase : class;
}
sut.Plain<object>("x");
sut.Mock.Verify.Plain<object>(It.IsAny<object?>()).Once(); // binds Plain<object>(object value): never matches
sut.Mock.Verify.Plain<object>(It.IsAny<string>()).Once(); // same
sut.Mock.Verify.Plain<object>(Match.AnyParameters()).Once(); // same: message "Plain<T>(Match.AnyParameters())" as a value
sut.AcceptDependent<string, object>("b", 5);
sut.Mock.Verify.AcceptDependent<string, object>("b", It.IsAny<object?>()).Once();
// binds the all-values overload: "...AcceptDependent<T, TBase>(b, It.IsAny<object>()) exactly once, but it never did."Generated overloads for AcceptDependent (the same shape in Setup):
[OverloadResolutionPriority(int.MaxValue - 1)] AcceptDependent<T, TBase>(IParameters parameters)
[OverloadResolutionPriority(2)] AcceptDependent<T, TBase>(IParameter<T?>? value, IParameter<TBase?>? other)
[OverloadResolutionPriority(1)] AcceptDependent<T, TBase>(T? value, IParameter<TBase?>? other)
[OverloadResolutionPriority(1)] AcceptDependent<T, TBase>(IParameter<T?>? value, TBase? other)
[OverloadResolutionPriority(int.MaxValue)] AcceptDependent<T, TBase>(T? value, TBase? other) // IgnoreParameters
For a one-parameter generic method closed over object no matcher spelling reaches a matcher overload; only a literal value or Plain<object>(default(object)!).AnyParameters() work. With several parameters, a matcher for every parameter works (AcceptDependent<string, object>(It.IsAny<string?>(), It.IsAny<object?>())), a matcher next to a literal does not. A non-generic TakeObject(object?) is protected and works.
Not part of this issue: without explicit type arguments, a matcher in a bare T slot makes the compiler infer T as the matcher type (Plain(It.IsAny<object?>()) infers T = IParameterWithCallback<object?>).
Idea for a fix
Extend ParametersBlockAllValuesPromotion (and IsObjectParams for params T[]) so that a value parameter whose type is a type parameter that can be object blocks the promotion too: a method or type type parameter that is not constrained to struct/unmanaged and has no constraint that excludes object (decide precisely: class, class?, notnull and unconstrained can all be object; an interface or base-class constraint cannot; a constraint to another type parameter depends on that one). The matcher overloads then win whenever they apply, as for a declared object.
Before changing anything, establish why the promotion exists and what the change does to calls that compile today, for every generic method, not only those closed over object:
- literal arguments (
Plain<int>(5),Plain<string>("a"),Plain<string>(null)), the lonenullforwarding of fix: treat a lone null literal as the null argument instead of any parameters #987 (ForwardsLoneNull),default, and mixed literal/matcher calls at arity 2 to 4 and 5+; - ambiguities (CS0121) that the promotion currently resolves;
Match.AnyParameters()on one-parameter and multi-parameter generic methods;- the same for generic type parameters of the mocked type (
IRepo<T>closed overobject: there the parameter IS declaredobjectafter substitution, so it should already be protected; confirm), delegates (Func<T, TResult>closed overobject) and indexers.
If removing the promotion breaks ordinary calls, look for a narrower rule (for example reordering the priorities so the matcher overloads rank above all-values only for such parameters) and say why.
Activity
fix: match a matcher on a generic method closed over `object`
fix: match a matcher on a generic method closed over `object`