Skip to content

Pass inout arguments with & in template func mocks - #364

Merged
sidepelican merged 1 commit into
uber:masterfrom
sagarpant:fix-inout-template-func
Nov 4, 2026
Merged

sidepelican merged 1 commit into
uber:masterfrom
sagarpant:fix-inout-template-func

Conversation

@sagarpant

Copy link
Copy Markdown
Contributor

Problem

With --use-template-func, the generated mock passes inout arguments to the handler without &, so mocks for protocols with inout parameters don't compile.

protocol Foo {
    func hash(into hasher: inout Hasher)
}

Before:

public func hash(into hasher: inout Hasher) {
    mockFunc(&hashCallCount)("hash", hashHandler?(hasher), .void)   // error: passing value of type 'Hasher' to an inout parameter requires explicit '&'
}

After:

public func hash(into hasher: inout Hasher) {
    mockFunc(&hashCallCount)("hash", hashHandler?(&hasher), .void)
}

The non-template path already handles this (ClosureTemplate), so only --use-template-func was affected.

Fix

In MethodTemplate.renderParamValues, emit & + the argument name when arg.type.isInOut, matching the non-template path.

Tests

The existing testInoutParams runs without useTemplateFunc, and the only useTemplateFunc: true test (testMockFuncs) uses simpleFuncs, which has no inout parameters, so the combination wasn't covered. This adds testInoutParamsMockFuncs, which runs the existing inoutParams fixture with useTemplateFunc: true.

  • Fails without the fix, passes with it.
  • Full swift test passes locally on macOS (158 XCTests + the Swift Testing suite).

🤖 Generated with Claude Code

With --use-template-func, the handler call in the generated mock passed
inout arguments without &, so mocks for protocols with inout parameters
failed to compile. The non-template path already adds &.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@sagarpant
sagarpant marked this pull request as ready for review October 25, 2026 05:14

@sidepelican sidepelican left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm surprised that the --use-template-func feature is being used, but there doesn't seem to be any problem with this change itself.

@sidepelican
sidepelican merged commit 0dd9ae9 into uber:master Nov 4, 2026
11 checks passed
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