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

Added diagnostic/suggestion for core::pin::pin! in addition to Box::pin if Unpin isn't implemented #109988

Closed
wants to merge 3 commits into from

Conversation

ndrewxie
Copy link
Contributor

@ndrewxie ndrewxie commented Apr 6, 2023

Currently, if an operation on Pin<T> is performed that requires T to implement Unpin, the diagnostic suggestion is to use Box::pin ("note: consider using Box::pin").

As others have noted, using pin! avoids a heap allocation, so it should be suggested as well (and probably over Box::pin whenever possible).

This PR adds the diagnostic "consider using Box::pin if you want ownership of the pinned value", and updates tests to match.

Addresses/attempts to address #109964

@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cjgillot (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 6, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@@ -823,7 +823,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
/// [`pin` module]: crate::pin
#[stable(feature = "pin", since = "1.33.0")]
#[rustc_on_unimplemented(
note = "consider using `Box::pin`",
note = "consider using `core::pin::pin!`\nconsider using `Box::pin` if you want ownership of the pinned value",
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the policy about mentioning core in an diagnostic message? Should we mention the pin! macro instead?

I'm not convinced about the "if you want ownership of the pinned value" part. The important difference between the two is the local storage. What about: "if you need to access the pinned value outside of the current scope"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I suppose the std::pin::pin! macro would be better

Also yeah, that message works better, it captures the "local pin" semantics far better. I'll make the changes and commit

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2023
@ndrewxie
Copy link
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 10, 2023
@@ -823,7 +823,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
/// [`pin` module]: crate::pin
#[stable(feature = "pin", since = "1.33.0")]
#[rustc_on_unimplemented(
note = "consider using `Box::pin`",
note = "consider using `std::pin::pin!`\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
note = "consider using `std::pin::pin!`\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",
note = "consider using the `pin!` macro\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",

Works for both std and no-std 😄

r=me with commits squashed

@ndrewxie ndrewxie deleted the branch rust-lang:master April 12, 2023 18:16
@ndrewxie ndrewxie closed this Apr 12, 2023
@ndrewxie ndrewxie deleted the master branch April 12, 2023 18:16
@ndrewxie
Copy link
Contributor Author

ndrewxie commented Apr 12, 2023

Whoops, I was trying to sort out some git stuff (git won't let me switch to the branch for this issue for some reason, git clone literally didn't clone it), and in the process of renaming the branch I accidentally closed it... Is it OK for me to create a new PR and r= you, @cjgillot ? Sorry😅

I re-made the pull request as #110259

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants