Skip to content

Fix simple generator execution for parameterized methods#80

Closed
Copilot wants to merge 3 commits intobugfix/simple_method_with_parameterfrom
copilot/fix-simple-method-generator
Closed

Fix simple generator execution for parameterized methods#80
Copilot wants to merge 3 commits intobugfix/simple_method_with_parameterfrom
copilot/fix-simple-method-generator

Conversation

Copy link
Contributor

Copilot AI commented Mar 7, 2026

Simple-pattern generator methods with parameters were failing at generation time with TargetParameterCountException (MSGH004). The runtime invocation path only handled zero args or a single converted arg, which broke methods like SimpleMethodWithParameter_Generator(int).

  • Runtime invocation: robust parameter binding

    • Updated GeneratesMethodExecutionRuntime.ConvertArguments(...) to bind all method parameters instead of only the first.
    • Added strict over-arity handling (args.Length > parameters.Length).
    • For missing/null inputs, now supplies CLR default values per parameter type (e.g. 0 for int, null for reference/nullable types), allowing simple-pattern generators with parameters to execute.
  • Regression coverage in generator runtime tests

    • Added a focused test in GeneratesMethodExecutionRuntimeTests to assert simple generator methods with parameters execute successfully using defaulted inputs.
  • Issue test expectation alignment

    • Corrected SimpleMethodWithParameter generated-code expectation to match the actual partial signature (int return type + int someIntParameter) rather than a parameterless/string signature.
private static object?[]? ConvertArguments(object?[]? args, MethodInfo methodInfo)
{
    ParameterInfo[] parameters = methodInfo.GetParameters();
    if (parameters.Length == 0) return null;
    if (args != null && args.Length > parameters.Length) throw new TargetParameterCountException();

    object?[] convertedArguments = new object?[parameters.Length];
    for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
    {
        ParameterInfo parameter = parameters[parameterIndex];
        object? argument = args != null && parameterIndex < args.Length ? args[parameterIndex] : null;
        if (argument == null)
        {
            convertedArguments[parameterIndex] = GetDefaultValue(parameter.ParameterType);
            continue;
        }

        Type targetType = Nullable.GetUnderlyingType(parameter.ParameterType) ?? parameter.ParameterType;
        convertedArguments[parameterIndex] = Convert.ChangeType(argument, targetType);
    }

    return convertedArguments;
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Simple methods with parameters generator fails</issue_title>
<issue_description>Take a look at the SimpleMethodWithParameterTests. It does not compile - the generators fails. But it shouln't. Fix it.

MSGH004: Failed to execute generator method 'SimpleMethodWithParameter_Generator': Error executing generator method 'SimpleMethodWithParameter_Generator': System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.MethodBaseInvoker.ThrowTargetParameterCountException()
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at EasySourceGenerators.Generators.GeneratesMethodExecutionRuntime.ExecuteGeneratorMethodWithArgs(IMethodSymbol generatorMethod, IReadOnlyList`1 allPartialMethods, Compilation compilation, Object[] args)</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Mar 7, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 6 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...tors.Generators/GeneratesMethodExecutionRuntime.cs 75.00% 4 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix generator method parameter count mismatch Fix simple generator execution for parameterized methods Mar 7, 2026
Copilot finished work on behalf of dex3r March 7, 2026 13:24
@dex3r dex3r closed this Mar 7, 2026
@dex3r dex3r deleted the copilot/fix-simple-method-generator branch March 7, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants