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

Option as_ref compilation error when implementing cause method for Error trait #53716

Open
k-viktor opened this issue Aug 26, 2018 · 2 comments
Labels
A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@k-viktor
Copy link

Hi, I'm new to rust and this looks like a possible bug to me. I implemented my own error type (focus on the cause method implementation in Error trait):

#[derive(Debug)]
pub struct MyError<T: Error>
{
    description: String,
    cause: Option<T>
}

impl <T: Error> fmt::Display for MyError<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.description)
    }
}

impl <T: Error> MyError<T> {
    pub fn new(description: String, cause: Option<T>) -> MyError<T> {
        MyError{description, cause}
    }
}

impl <T: Error> Error for MyError<T> {
    fn description(&self) -> &str {
        self.description.as_str()
    }

    fn cause(&self) -> Option<&Error> {
        // doesn't work
        //self.cause.as_ref()
        //does work
        match self.cause {
            None => None,
            Some(ref e)=> Some(e)
        }
    }
}

This code seems to work correctly but when I use the commented self.cause.as_ref() instead of the match clause I'm getting following compilation error:

error[E0308]: mismatched types
  --> src\lib.rs:72:9
   |
72 |         self.cause.as_ref()
   |         ^^^^^^^^^^^^^^^^^^^ expected trait std::error::Error, found type parameter
   |
   = note: expected type `std::option::Option<&std::error::Error>`
              found type `std::option::Option<&T>`

I would normally expect it to work because the working match I use as workaround is pretty much the same as the underlying implementation of as_ref() in Option.

Meta

λ rustc --version --verbose
rustc 1.28.0 (9634041 2018-07-30)
binary: rustc
commit-hash: 9634041
commit-date: 2018-07-30
host: x86_64-pc-windows-msvc
release: 1.28.0
LLVM version: 6.0

@estebank
Copy link
Contributor

This happens because self.cause is std::option::Option<T>, where calling self.cause.as_ref() is std::option::Option<&T>, but your return type is std::option::Option<&dyn Error>. &T cannot be coerced by the compiler to &dyn Error automatically even though you have a bound T: Error. I believe that using self.cause.as_ref().map(|c| c as &Error)does the right thing, though.

@wfraser
Copy link

wfraser commented Sep 9, 2019

This seems extra weird to me in the workaround, that if you specify the return type of the closure in the map manually, the coercion works fine, and you don't even need the as:

fn cause(&self) -> Option<&dyn Error> {
    self.cause.as_ref().map(|e| -> &dyn Error { e })
}

I don't get why the return type of the closure has to be specified manually here, when it's already in the return type of the function. It has to match the function return type, so shouldn't type inference figure it out?

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 15, 2019
@fmease fmease added A-trait-system Area: Trait system and removed A-trait-system Area: Trait system labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants