Actual behavior A mocked variadic method whose expectation carries a Do or DoAndReturn function panics when the code under test passes nil as a variadic element. Both actions synthesize the zero value for a nil argument from ft.In(i), where i counts the arguments flattened as the mock passes them, but a variadic func type holds the slice at its last position and nothing beyond it. At the last position the zero value is the slice itself and reflect.Call panics with reflect: cannot use []error as type error in Call; past it, ft.In(i) panics with index out of range.
Expected behavior The function receives nil in that variadic position, the same as it does for a nil fixed argument.
To Reproduce Steps to reproduce the behavior
- Build a
Call for func(string, ...error) and attach Do(func(_ string, errs ...error) {}).
- Run the action with the arguments
"msg", nil, nil.
- The test binary panics instead of calling the function with two nil errors.
TestCall_Do_NilVariadicArg and TestCall_DoAndReturn_NilVariadicArg in the branch below do exactly this and fail on main at d8c3a2a.
Additional Information
- gomock mode (reflect or source): both, the failure is in
gomock/call.go
- gomock version or git ref:
main at d8c3a2a
- golang version: go1.26.2
Triage Notes for the Maintainers
I have a fix with the two tests ready at lenamonj@17c9aee: an argTypeAt helper that folds every position from the variadic one onwards onto the element type, used at both zero-value sites. go vet, go build, ci/test.sh, ci/check_panic_handling.sh and the module tests pass on it. CONTRIBUTING asks for an issue before a pull request, so I am opening this first and will open the pull request if you want the change.
Actual behavior A mocked variadic method whose expectation carries a
DoorDoAndReturnfunction panics when the code under test passesnilas a variadic element. Both actions synthesize the zero value for a nil argument fromft.In(i), whereicounts the arguments flattened as the mock passes them, but a variadic func type holds the slice at its last position and nothing beyond it. At the last position the zero value is the slice itself andreflect.Callpanics withreflect: cannot use []error as type error in Call; past it,ft.In(i)panics withindex out of range.Expected behavior The function receives
nilin that variadic position, the same as it does for a nil fixed argument.To Reproduce Steps to reproduce the behavior
Callforfunc(string, ...error)and attachDo(func(_ string, errs ...error) {})."msg", nil, nil.TestCall_Do_NilVariadicArgandTestCall_DoAndReturn_NilVariadicArgin the branch below do exactly this and fail onmainat d8c3a2a.Additional Information
gomock/call.gomainat d8c3a2aTriage Notes for the Maintainers
I have a fix with the two tests ready at lenamonj@17c9aee: an
argTypeAthelper that folds every position from the variadic one onwards onto the element type, used at both zero-value sites.go vet,go build,ci/test.sh,ci/check_panic_handling.shand the module tests pass on it. CONTRIBUTING asks for an issue before a pull request, so I am opening this first and will open the pull request if you want the change.