dtolnay / async-trait

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

[Bug] Incorrect rust compiler help hint #269

Closed h4-h closed 1 month ago

h4-h commented 6 months ago

I don't know if this issue should be here or in rust issues, but it does occur with this library.

Versions

name version
rust 1.78.0
async-trait 0.1.80
tokio 1.37.0

Description

If we create a trait that has an asynchronous function with immutable reference to itself and try to mutate it we may get an incorrect (as i know) rust code help hint:

help: consider specifying this binding's type
   |
15 |     async fn non_mutable_fn(&self: &mut TestStruct, new: i32) {
   |                                  +++++++++++++++++

Full code on rust-playground.com

Or here ```rust use async_trait::async_trait; // 0.1.80 use tokio; // 1.37.0 #[async_trait] trait First { async fn non_mutable_fn(&self, new: i32); } struct TestStruct { nums: Vec } #[async_trait] impl First for TestStruct { async fn non_mutable_fn(&self, new: i32) { self.nums.push(new); } } #[tokio::main] async fn main() { let test_struct = TestStruct { nums: Vec::new() }; test_struct.non_mutable_fn(3).await; } ```

Expected behavior

Standard help about mutable reference, without the async-trait crate you can achieve that:

help: consider changing this to be a mutable reference
   |
13 |     async fn non_mutable_fn(&mut self, new: i32) {
   |                             ~~~~~~~~~
dtolnay commented 1 month ago

I don't think this is something that can be improved in the macro. Maybe in the compiler.