dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.84k stars 85 forks source link

Self type in trait implementation causes panic in #[async_trait] #53

Closed SillyFreak closed 4 years ago

SillyFreak commented 4 years ago

Example Code:

use async_trait::async_trait;

struct Struct;

#[async_trait]
pub trait Trait {
    async fn foo();
}

#[async_trait]
impl Trait for Struct {
    async fn foo() {
        let _ = Self;
        // let _ = Struct;
    }
}

Error:

error: custom attribute panicked
  --> src/lib.rs:19:1
   |
19 | #[async_trait]
   | ^^^^^^^^^^^^^^
   |
   = help: message: called `Option::unwrap()` on a `None` value

I traced the error to using Self instead of Struct. non-async functions are not affected. Also the error is different if I change it to Struct { x: u8 }, but not if I cange it to Struct(u8);:

error[E0401]: can't use generic parameters from outer function
  --> src/lib.rs:22:17
   |
20 | impl Trait for Struct {
   | ---- `Self` type implicitly declared here, by this `impl`
21 |     async fn foo() {
22 |         let _ = Self { x: 0 };
   |                 ^^^^
   |                 |
   |                 use of generic parameter from outer function
   |                 use a type here instead

This time not a panic, but this would compile with Struct instead of Self or in a non-async function.

using async-trait0.1.19 & Rust 1.39.0

PS. thanks for this crate, it's awesome!

dtolnay commented 4 years ago

Thanks for the perfectly minimized bug report! I published a fix in 0.1.20.

dtolnay commented 4 years ago

Uh-oh, only let _ = Self and let _ = Self(0) are fixed, not let _ = Self { x: 0 }. One second.

dtolnay commented 4 years ago

Fixed in 0.1.21.