-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Automatic &mut reborrowing doesn't happen when arguments are for impl Trait
#85161
Comments
Seems the cause is that generics suppresses coercions. |
For what it is worth, this is not specific to |
I think the issue here is not specific to reborrowing or to impl Trait but is because when the compiler converts types it always need a 'target' type which the compiler is trying to convert to. The compiler does not take trait impls into account. So if the compiler knows we want type |
I think this is not a bug and changes here should involve T-lang. I'm going to close this for now. |
Can I try to convince you this is still a serious usability bug? Especially in the sense that, if you look at the initial example, we have two forms of function calls that look exactly the same, but behave differently because of (if I understand correctly) an implementation detail. It doesn't behave in a uniform way, which is a UX pain point (nmatsakis has explained this much better than I can!). The error it leads to is confusing, in my opinion, since based on my own experience and observations other developers around me, everyone expected the re-borrow to happen automatically, so the user kept on looking for another programming mistake. |
Sure thing, I don't mind keeping this open if the goal is to improve the error message. |
Just got bit by this while using the sqlx crate using the postgres backend, specifically when using transactions. (probably a backend-independent problem) let mut transaction = connection_pool.begin().await?;
let conn: &mut PgConnection = transaction.acquire().await?;
sqlx::query("...").execute(conn).await?; // Totally fine.
sqlx::query("...").execute(conn).await?; // Totally NOT fine since conn has moved. I am able to get the code to work by manually reborrowing, but I feel like this shouldn't be necessary. Note that you don't have to reborrow when transactions aren't involved, as you can just pass in a &PgPool, which uses interior mutability. No &mut reborrowing there. sqlx::query("...").execute(&connection_pool).await?; // Totally fine.
sqlx::query("...").execute(&connection_pool).await?; // Totally fine. |
Given code like this:
it currently fails to compile with:
While not a super serious issue ergonomically it'd be great if rustc would apply the same automatic reborrowing logic here to
&mut T
arguments regardless of whether the target function takes a&mut T
or it's satisfying animpl Trait
bound of some form.The text was updated successfully, but these errors were encountered: