fehler icon indicating copy to clipboard operation
fehler copied to clipboard

Fehler didn't support returning `Result<Box<dyn Trait>, _>`

Open earthengine opened this issue 5 years ago • 2 comments

Code:

use fehler::throws;

trait FooTrait {}
struct FooStruct;
struct FooError;
impl FooTrait for FooStruct {}
#[throws(FooError)]
fn foo() -> Box<dyn FooTrait> {
    Box::new(FooStruct)
}

Compile result:

Error[E0271]: type mismatch resolving ...
    |
104 | #[throws(FooError)]
    | ^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn mymodule::FooTrait`, found struct `mymodule::FooStruct`
    |
    = note: expected type `std::boxed::Box<dyn mymodule::FooTrait>`
             found struct `std::boxed::Box<mymodule::FooStruct>`
    = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

earthengine avatar May 16 '20 05:05 earthengine

It should work if you add as Box<dyn FooTrait> to the return expression, but its frustrating that the cast doesn't happen automatically here the way it normally would. Can you build against master and see if the problem persists?

withoutboats avatar May 18 '20 15:05 withoutboats

A possible fix for this issue would be to change the expansion of return expressions from wrapping in ok to wrapping in ok and casting to the ret type.

withoutboats avatar Jun 26 '20 12:06 withoutboats