Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using any keyword together with nil causes panic in generated code #820

Closed
1 of 5 tasks
Link512 opened this issue Sep 30, 2024 · 1 comment · Fixed by #821
Closed
1 of 5 tasks

Using any keyword together with nil causes panic in generated code #820

Link512 opened this issue Sep 30, 2024 · 1 comment · Fixed by #821
Labels

Comments

@Link512
Copy link
Contributor

Link512 commented Sep 30, 2024

Description

Using any keyword and returning nil does not generate the same code as for interface{}, causing a nil interface panic

Mockery Version

2.46.1 - does NOT reproduce with previous versions

Go Version

1.23.1

Installation Method

  • Binary Distribution
  • Docker
  • brew
  • go install
  • Other: [specify]

Steps to Reproduce

  1. Define an interface with a method returning any
type Foo interface {
	WithAny() any
	WithInt() interface{}
}
  1. Make that method return nil
func TestFoo(t *testing.T) {
	fooMock := mocks.NewFoo(t)

	fooMock.EXPECT().WithInt().Once().Return(nil)
	fooMock.EXPECT().WithAny().Once().Return(nil)

	_ = fooMock.WithInt()
	_ = fooMock.WithAny() // panics
}

Expected Behavior

Code doesn't panic

Actual Behavior

Code panics.

Upon inspecting the generated code, I've noticed that the two mocked methods differ in how they handle the return:

// for interface{}
if ret.Get(0) != nil {
     r0 = ret.Get(0).(interface{})
}

// for any
r0 = ret.Get(0).(any) 
@LandonTClipp
Copy link
Collaborator

Indeed! I'll have to look into this, this should be a simple fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants