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

Begin experimental support for pin reborrowing #130526

Merged
merged 5 commits into from
Sep 20, 2024
Merged

Conversation

eholk
Copy link
Contributor

@eholk eholk commented Sep 18, 2024

This commit adds basic support for reborrowing Pin types in argument position. At the moment it only supports reborrowing Pin<&mut T> as Pin<&mut T> by inserting a call to Pin::as_mut(), and only in argument position (not as the receiver in a method call).

This PR makes the following example compile:

#![feature(pin_ergonomics)]

fn foo(_: Pin<&mut Foo>) {
}

fn bar(mut x: Pin<&mut Foo>) {
    foo(x);
    foo(x);
}

Previously, you would have had to write bar as:

fn bar(mut x: Pin<&mut Foo>) {
    foo(x.as_mut());
    foo(x);
}

Tracking:

r? @compiler-errors

This commit adds basic support for reborrowing `Pin` types in argument
position. At the moment it only supports reborrowing `Pin<&mut T>` as
`Pin<&mut T>` by inserting a call to `Pin::as_mut()`, and only in
argument position (not as the receiver in a method call).
@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 Sep 18, 2024
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Left some thoughts. Specifically, aside from the nits, there's a problem with the desugaring that makes it not as general as when the feature gate is disabled due to the specific lowering to a call of Pin::as_mut. I think that should be changed.

tests/ui/async-await/pin-reborrow-arg.rs Outdated Show resolved Hide resolved
tests/ui/async-await/pin-reborrow-self.rs Show resolved Hide resolved
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/coercion.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/expr_use_visitor.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/thir/cx/expr.rs Outdated Show resolved Hide resolved
@rustbot rustbot 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 Sep 18, 2024
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

Thanks for making this pretty bite-sized though. I was somewhat afraid you were going to put up a huge pin ergonomics PR; I'm happy to review this work, but if possible, I'd like to have more PRs that are smaller than fewer PRs that are larger (I know I am a huge hypocrite for doing that exact thing, so if it's not possible, then I understand lol).

@eholk
Copy link
Contributor Author

eholk commented Sep 18, 2024

Thanks for making this pretty bite-sized though. I was somewhat afraid you were going to put up a huge pin ergonomics PR; I'm happy to review this work, but if possible, I'd like to have more PRs that are smaller than fewer PRs that are larger (I know I am a huge hypocrite for doing that exact thing, so if it's not possible, then I understand lol).

Thanks for the comments! I'm a fan of going for the smallest atomic change I can do, but sometimes the smallest atomic change is quite large. The nice thing about the pin ergonomics experiment is that it's around 4-6 independent features and half of them should be pretty small.

@jieyouxu jieyouxu added the F-pin_ergonomics `#![feature(pin_ergonomics)]` label Sep 19, 2024
Generating a call to `as_mut()` let to more restrictive borrows than
what reborrowing usually gives us. Instead, we change the desugaring to
reborrow the pin internals directly which makes things more expressive.
@eholk
Copy link
Contributor Author

eholk commented Sep 19, 2024

It went ahead and added support for reborrowing Pin<&mut T> and Pin<&T> as Pin<&T>, along with adding a test to make sure you can't reborrow Pin<&T> as Pin<&mut T>.

@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 20, 2024

📌 Commit a18800f has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 20, 2024
…llaumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#128209 (Remove macOS 10.10 dynamic linker bug workaround)
 - rust-lang#130526 (Begin experimental support for pin reborrowing)
 - rust-lang#130611 (Address diagnostics regression for `const_char_encode_utf8`.)
 - rust-lang#130614 (Add arm64e-apple-tvos target)
 - rust-lang#130617 (bail if there are too many non-region infer vars in the query response)
 - rust-lang#130619 (Fix scraped examples height)
 - rust-lang#130624 (Add `Vec::as_non_null`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit fe5f734 into rust-lang:master Sep 20, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 20, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 20, 2024
Rollup merge of rust-lang#130526 - eholk:pin-reborrow, r=compiler-errors

Begin experimental support for pin reborrowing

This commit adds basic support for reborrowing `Pin` types in argument position. At the moment it only supports reborrowing `Pin<&mut T>` as `Pin<&mut T>` by inserting a call to `Pin::as_mut()`, and only in argument position (not as the receiver in a method call).

This PR makes the following example compile:

```rust
#![feature(pin_ergonomics)]

fn foo(_: Pin<&mut Foo>) {
}

fn bar(mut x: Pin<&mut Foo>) {
    foo(x);
    foo(x);
}
```

Previously, you would have had to write `bar` as:

```rust
fn bar(mut x: Pin<&mut Foo>) {
    foo(x.as_mut());
    foo(x);
}
```

Tracking:

- rust-lang#130494

r? `@compiler-errors`
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 2, 2024
…errors

Add support for reborrowing pinned method receivers

This builds on rust-lang#130526 to add pinned reborrowing for method receivers. This enables the folllowing examples to work:

```rust
#![feature(pin_ergonomics)]
#![allow(incomplete_features)]

use std::pin::Pin;

pub struct Foo;

impl Foo {
    fn foo(self: Pin<&mut Self>) {
    }

    fn baz(self: Pin<&Self>) {
    }
}

pub fn bar(x: Pin<&mut Foo>) {
    x.foo();
    x.foo();

    x.baz(); // Pin<&mut Foo> is downgraded to Pin<&Foo>
}

pub fn baaz(x: Pin<&Foo>) {
    x.baz();
    x.baz();
}
```

This PR includes the original one, which is currently in the commit queue, but the only code changes are in the latest commit (d3c53aa).

rust-lang#130494

r? `@compiler-errors`
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Oct 5, 2024
…r-errors

Add support for reborrowing pinned method receivers

This builds on rust-lang#130526 to add pinned reborrowing for method receivers. This enables the folllowing examples to work:

```rust
#![feature(pin_ergonomics)]
#![allow(incomplete_features)]

use std::pin::Pin;

pub struct Foo;

impl Foo {
    fn foo(self: Pin<&mut Self>) {
    }

    fn baz(self: Pin<&Self>) {
    }
}

pub fn bar(x: Pin<&mut Foo>) {
    x.foo();
    x.foo();

    x.baz(); // Pin<&mut Foo> is downgraded to Pin<&Foo>
}

pub fn baaz(x: Pin<&Foo>) {
    x.baz();
    x.baz();
}
```

This PR includes the original one, which is currently in the commit queue, but the only code changes are in the latest commit (d3c53aa).

rust-lang#130494

r? `@compiler-errors`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 5, 2024
Rollup merge of rust-lang#130633 - eholk:pin-reborrow-self, r=compiler-errors

Add support for reborrowing pinned method receivers

This builds on rust-lang#130526 to add pinned reborrowing for method receivers. This enables the folllowing examples to work:

```rust
#![feature(pin_ergonomics)]
#![allow(incomplete_features)]

use std::pin::Pin;

pub struct Foo;

impl Foo {
    fn foo(self: Pin<&mut Self>) {
    }

    fn baz(self: Pin<&Self>) {
    }
}

pub fn bar(x: Pin<&mut Foo>) {
    x.foo();
    x.foo();

    x.baz(); // Pin<&mut Foo> is downgraded to Pin<&Foo>
}

pub fn baaz(x: Pin<&Foo>) {
    x.baz();
    x.baz();
}
```

This PR includes the original one, which is currently in the commit queue, but the only code changes are in the latest commit (d3c53aa).

rust-lang#130494

r? `@compiler-errors`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 15, 2024
…er-errors

Add `&pin (mut|const) T` type position sugar

This adds parser support for `&pin mut T` and `&pin const T` references. These are desugared to `Pin<&mut T>` and `Pin<&T>` in the AST lowering phases.

This PR currently includes rust-lang#130526 since that one is in the commit queue. Only the most recent commits (bd45002 and following) are new.

Tracking:

- rust-lang#130494

r? `@compiler-errors`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 15, 2024
Rollup merge of rust-lang#130635 - eholk:pin-reborrow-sugar, r=compiler-errors

Add `&pin (mut|const) T` type position sugar

This adds parser support for `&pin mut T` and `&pin const T` references. These are desugared to `Pin<&mut T>` and `Pin<&T>` in the AST lowering phases.

This PR currently includes rust-lang#130526 since that one is in the commit queue. Only the most recent commits (bd45002 and following) are new.

Tracking:

- rust-lang#130494

r? `@compiler-errors`
flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 18, 2024
…er-errors

Add `&pin (mut|const) T` type position sugar

This adds parser support for `&pin mut T` and `&pin const T` references. These are desugared to `Pin<&mut T>` and `Pin<&T>` in the AST lowering phases.

This PR currently includes rust-lang#130526 since that one is in the commit queue. Only the most recent commits (bd45002 and following) are new.

Tracking:

- rust-lang#130494

r? `@compiler-errors`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-pin_ergonomics `#![feature(pin_ergonomics)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants