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

Document auto trait inference for async blocks #1093

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

eholk
Copy link
Contributor

@eholk eholk commented Sep 29, 2021

We are in the process of changing this (rust-lang/rust#69663), but it would be good to document the existing rules before changing them. This should also help explain the compilation errors people are getting in the meantime.

We are in the process of changing this (rust-lang/#69663), but it
would be good to document the existing rules before changing them.
This should also help explain the compilation errors people are
getting in the meantime.
# ;
```

Here the result of `bar()` is in scope during the await of `foo()`, so the result of `bar()` will impact the inferred auto traits.
Copy link
Member

@pnkfelix pnkfelix Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The phrase "during the await of foo()" might be ambiguous, depending on what other assumptions the reader is making.

I guess my concern is about some scenario where you have an EXPR.await, and the temporary scope of some baz() is entirely contained within the EXPR, and does not overlap the .await operator itself.

(I will see if I can construct an illustrative example.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed "during" to "for," but I don't think that really addresses your concern...

A couple of examples that might be relevant here could be something like foo().bar().await, and { let bar = foo().bar(); bar }.await. I'll have to experiment some more, but I think what happens here is that foo() is borrowed across the away in the first example, but not the second.

@pnkfelix
Copy link
Member

I think this documentation would be improved if it had examples of both sides of the coin. Think of it as how some theorems are illuminated by showing both examples and counter-examples.

E.g. you have shown a case where bar() overlaps the foo().await, and thus its auto-traits end up applying to the future produced by the async block.

So I think the other thing I would show is some other bit of code, perhaps as close as possible to the provided example, (or even modify the existing example), where some temporary baz() does not overlap the foo().await, and so its auto-traits do not apply to the future.

@eholk
Copy link
Contributor Author

eholk commented Oct 20, 2021

I added a couple more examples, one that's a slightly more complex version of the previous example, and another that shows a small change to remove a borrow from the scope.

I'm not convinced these are the best examples, but they're an example of the cases I've looked closest at and they do highlight some of the trickier issues here.

What do you think?

@ehuss
Copy link
Contributor

ehuss commented Jan 13, 2022

@pnkfelix Did you have any more comments on this?

@eholk
Copy link
Contributor Author

eholk commented Sep 3, 2024

This came up in the Lang Docs call today. It'd be good to get some up to date documentation in this area. The behavior described in this PR has changed now that drop-tracking-mir is on by default, so it needs to be brought up to date.

#1088 might describe the current behavior, but it also should be checked for correctness.

@traviscross
Copy link
Contributor

@rustbot author

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants