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

Can't set simultaneous expectations of different types on generic method whose generic parameters only appear in output #236

Closed
Kormick opened this issue Dec 18, 2020 · 4 comments · Fixed by #238
Labels
bug Something isn't working

Comments

@Kormick
Copy link

Kormick commented Dec 18, 2020

I'm trying to mock a struct with the generic method. And for the case when we use generic parameter only as return type, expectation set panics if we use different types. But it will be ok if we set expectations for the same type, or if we add the generic parameter as a method argument.
I've created a minimal example in this repo: https://github.com/Kormick/mockall-test. Please take a look.

I'm using rustc 1.50.0-nightly (eb4fc71dc 2020-12-17) (from rustc 1.50.0-nightly (6645da366 2020-12-01)) toolchain

Panic message: panicked at 'called `Result::unwrap()` on an `Err` value: TypeMismatch { expected: "[ONLY ON NIGHTLY]", found: "[ONLY ON NIGHTLY]" }'
For some reason, I always have the [ONLY ON NIGHTLY] message, even though I'm using the nightly toolchain.

Backtrace:

stack backtrace:
   0: rust_begin_unwind
             at /rustc/6645da366eed0c61258a04265bea513e94df7ea6/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/6645da366eed0c61258a04265bea513e94df7ea6/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/6645da366eed0c61258a04265bea513e94df7ea6/library/core/src/option.rs:1268:5
   3: core::result::Result<T,E>::unwrap
             at /home/kormick/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:973:23
   4: mockall_test::__mock_MockStorage::__get::GenericExpectations::expect
             at ./src/lib.rs:5:1
   5: mockall_test::MockStorage::expect_get
             at ./src/lib.rs:5:1
   6: mockall_test::test_get_different_types
             at ./src/lib.rs:32:5
   7: mockall_test::test_get_different_types::{{closure}}
             at ./src/lib.rs:29:1
   8: core::ops::function::FnOnce::call_once
             at /home/kormick/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
   9: core::ops::function::FnOnce::call_once
             at /rustc/6645da366eed0c61258a04265bea513e94df7ea6/library/core/src/ops/function.rs:227:5
@asomers
Copy link
Owner

asomers commented Dec 18, 2020

To get a more useful error message, you must not only use the nightly toolchain, but also build mockall with the "nightly" crate feature.

@Kormick
Copy link
Author

Kormick commented Dec 18, 2020

Adding features = ["nightly"] in Cargo.toml for mockall should be enough, right? I've tried this and with the cloned mockall repo, but still get [ONLY ON NIGHTLY]

@asomers
Copy link
Owner

asomers commented Dec 18, 2020

Oh, I see. That "ONLY ON NIGHTLY" message is coming from the downcast crate, which also has a nightly feature. You've found a bug. I'll try to fix it as time allows.

@asomers asomers added the bug Something isn't working label Dec 18, 2020
@asomers asomers changed the title Expectation set panics for generic method when setting expectations for different types Can't set simultaneous expectations of different types on generic method whose generic parameters only appear in output Dec 18, 2020
@Kormick
Copy link
Author

Kormick commented Dec 18, 2020

Ok, thank you! 👍

asomers added a commit that referenced this issue Dec 22, 2020
The return type was not being used to index into the Expectations
object. Probably I didn't do that to allow generic methods returning
references, because older versions of Mockall didn't already determine
the owned form of the return type.

Fixes #236
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants