Skip to content

Mock generator predicate locals can collide with interface parameter names #7433

Closed

Description

@thomhurst

Summary

Mocking an interface whose parameter names collide with generated predicate-conversion locals can produce invalid C#. Generated locals need unique names so valid interfaces can be mocked.

Reproduction

Declare public interface IService { void Send(string value, string __fa_value); } and generate a mock with Mock.Of<IService>(). The generated predicate overload for value declares a local __fa_value in a method already containing a parameter with that name, producing CS0136.

Expected and actual

Expected: generated locals avoid all parameter names. Actual: EmitSingleFuncOverload prefixes the raw parameter name with __fa_ without checking the other parameters. This naming scheme predates #7415; its reviewer noticed the gap.

Environment

Generator source at c663d1a, src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs, lines 1305–1309. Confirmed by inspecting emitted source construction; compilation reproduction has not been run.

Please allocate collision-free local names for predicate conversions and add a generator regression test.

Activity

  1. added
    bugSomething isn't working
    P3Low: cleanup, minor enhancement, or rare edge case
    on Nov 3, 2026
  2. thomhurst commented on Nov 3, 2026

    @thomhurst
    OwnerAuthor

    Audited against main d87516cb9c: the predicate conversion still declares __fa_{rawName} without reserving other parameter names (MockMembersBuilder.cs:1308) and uses that name in the matcher expression. The collision remains independent of #7415; no closing PR was found.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low: cleanup, minor enhancement, or rare edge casebugSomething isn't workingin-progressWork is in progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions