Prevent "wrong" optimization

Hello everybody,

I have the following code which works but my tests are failing. The reason for this seems to be, that the compiler optimizes away the unwrap_or_else call (At least this is what my debugger told me).

        ).map_err(|err| {
            T::Currency::transfer(&who, &from, direct_reward, AllowDeath)
                .unwrap_or_else(|| err)


In both the following cases, the vested_transfer fails. But in one of the cases the returned value from the map_err is gibberish.

Case 1 - transfer fails

The call of transfer fails and an error is returned. The code then correctly returns an Err(ERROR_TYPE) value.

Case 2 - transfer does NOT fail

The call of transfer succeeds and I want to return the previous error value via the unwrap_or_else call. The returned value is an byte-array:


  • Is this expected behavior?
  • How to correctly return the error upon the success of the transfer call?

I mean, the compiler may very well optimize an unwrap_or_else into an ordinary if/else statement of sorts. I don't think that this is answerable with just what you've given us here.

Like, you claim that the error is an incorrect optimization on the compiler's part? I think that's very unlikely. Sure, the compiler may simplify the code to not literally call unwrap_or_else, but the optimization is most likely correct. My guess is that it's a logic bug in your code.

Never mind, I am closing this. The error originates in a macro I am using for asserting during the tests. It is not even an error, just expected behaviour.

Sorry, but thanks for the quick reply. And I did not want to claim that the optimization is incorrect, just was a guess.

[Edited to further explain the reason, why I am closing this]

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.