-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
.await
does not perform autoref or autoderef
#111546
Comments
Is there a reason why you want this behavior? Just for consistency? Or do you have real code that relies on it? |
This came up for me with the |
.await
does not perform autoref or auto-deref.await
does not perform autoref or autoderef
Weird question: was this present before the move to I'm assuming not, because otherwise this could have potentially broken things that worked before. |
@clarfonthey no, afaict it never did autoderef |
We discussed this in @rust-lang/wg-async's triage meeting today. Overall, we'd like to see more motivation for this behavior, especially since on the PR (#111773) it looks like this change could break existing code. As I understand it, this would let There could be an argument for consistency here, if user's would find it surprising that |
Basically, yes. Which doesn't sound huge on its own—but let's compare with the sync version of //! Sync version
use once_cell::sync::Lazy; // or `std::sync::LazyLock`
static USER_IDS: Lazy<HashMap<String, u32>> = Lazy::new(|| /* sync initialization code */);
fn johns_id() -> Option<u32> {
// No `await` and no method call. `Lazy`'s `Deref` impl allows accessing the value directly.
USER_IDS.get("John Doe").copied()
} //! Async version
use async_lazy::Lazy; // or `async_once_cell::Lazy`
static USER_IDS: Lazy<HashMap<String, u32>> = Lazy::const_new(|| Box::pin(async {/* async initialization code */}));
fn johns_id_no_autoref() -> Option<u32> {
// `.await` and extra method call. +14 characters compared to sync version of this code
USER_IDS.force().await.get("John Doe").copied()
}
fn johns_id_with_autoref() -> Option<u32> {
// `.await`, but no extra method call. Much closer to sync version, +6 characters
USER_IDS.await.get("John Doe").copied()
}
Personally, I had always thought that " |
We discussed this in a recent wg-async meeting (notes). The consensus was that we thought the change was well-motivated. At the same time, we want to be cautious about introducing problems (namely backwards compatibility). There should probably be a crater run of this change, and we should also work through any problematic interactions that could be caused by this change. (@rust-lang/types should probably weigh in.) The main motivation for the change is the analogy to Note that there is another analogy that works against this, the analogy to That being said, this change would need lang team signoff. You can consider this comment wg-async's official recommendation to the lang team. |
@rustbot labels -I-types-nominated This was discussed at a recent T-types meeting. The consensus was that this is a question for T-lang. If T-lang has a specific question for T-types, please renominate. |
In the meantime we could change the error to check for the same steps that autoderef would follow and suggest the right combination of operations (suggest |
While I sympathize with the example of async What I would rather see is postfix address of operator, which would let you take the reference without adding parenthesis: (I'm a bit biased here cause I want postfix addr-of anyway, but making |
Perform autoref/autoderef on `.await` This PR adds support for autoref/autoderef of the reciever of an `.await` (before calling `IntoFuture::into_future()`). This PR is not ready to merge yet—the feature works, but diagnostics are regressed and clippy is broken. I would like to get some feedback on my current approach, before investing more effort into it. Fixes rust-lang#111546.
Given that there's been a crater run and we haven't looked through the results yet, unnominating until @rust-lang/wg-async has time to do so. @rustbot label: -I-lang-nominated |
I'd like to use this issue to collect more use cases in the meantime. Right now this seems like a risky change to make, when so far we only know of one instance where it would be useful. |
I definitely think that this should be something that happens, even if it has to happen in a future edition. (I'd imagine it's probably too late to nudge into the 2024 edition, given the amount of work required and the short deadline.) I also believe that
|
I tried this code:
I expected to see this happen: Compiles and runs successfully. The
.await
performs auto-ref, and<&Foo as IntoFuture>::into_future()
is called.Instead, this happened:
Meta
rustc --version --verbose
:@rustbot label A-async-await
The text was updated successfully, but these errors were encountered: