-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Begin experimental support for pin reborrowing #130526
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b7992f
Begin experimental support for pin reborrowing
eholk a73c8b1
Apply code review suggestions
eholk b2b76fb
Allow shortening reborrows
eholk 92a5d21
Add a test case to make sure we don't reborrow twice
eholk a18800f
pin_ergonomics: allow reborrowing as Pin<&T>
eholk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1418,6 +1418,7 @@ symbols! { | |
pic, | ||
pie, | ||
pin, | ||
pin_ergonomics, | ||
platform_intrinsics, | ||
plugin, | ||
plugin_registrar, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//@ check-pass | ||
|
||
#![feature(pin_ergonomics)] | ||
#![allow(dead_code, incomplete_features)] | ||
|
||
use std::pin::Pin; | ||
|
||
struct Foo; | ||
|
||
impl Foo { | ||
fn baz(self: Pin<&mut Self>) { | ||
} | ||
} | ||
|
||
fn foo(_: Pin<&mut Foo>) { | ||
} | ||
|
||
fn foo_const(_: Pin<&Foo>) { | ||
} | ||
|
||
fn bar(x: Pin<&mut Foo>) { | ||
foo(x); | ||
foo(x); // for this to work we need to automatically reborrow, | ||
// as if the user had written `foo(x.as_mut())`. | ||
|
||
Foo::baz(x); | ||
Foo::baz(x); | ||
|
||
foo_const(x); // make sure we can reborrow &mut as &. | ||
|
||
let x: Pin<&Foo> = Pin::new(&Foo); | ||
|
||
foo_const(x); // make sure reborrowing from & to & works. | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![feature(pin_ergonomics)] | ||
#![allow(dead_code, incomplete_features)] | ||
|
||
// make sure we can't accidentally reborrow Pin<&T> as Pin<&mut T> | ||
|
||
use std::pin::Pin; | ||
|
||
struct Foo; | ||
|
||
fn foo(_: Pin<&mut Foo>) { | ||
} | ||
|
||
fn bar(x: Pin<&Foo>) { | ||
foo(x); //~ ERROR mismatched types | ||
//| ERROR types differ in mutability | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/pin-reborrow-const-as-mut.rs:14:9 | ||
| | ||
LL | foo(x); | ||
| --- ^ types differ in mutability | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected struct `Pin<&mut Foo>` | ||
found struct `Pin<&Foo>` | ||
note: function defined here | ||
--> $DIR/pin-reborrow-const-as-mut.rs:10:4 | ||
| | ||
LL | fn foo(_: Pin<&mut Foo>) { | ||
| ^^^ ---------------- | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![feature(pin_ergonomics)] | ||
#![allow(dead_code, incomplete_features)] | ||
|
||
// Make sure with pin reborrowing that we can only get one mutable reborrow of a pinned reference. | ||
|
||
use std::pin::{pin, Pin}; | ||
|
||
fn twice(_: Pin<&mut i32>, _: Pin<&mut i32>) {} | ||
|
||
fn main() { | ||
let x = pin!(42); | ||
twice(x, x); //~ ERROR cannot borrow | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0499]: cannot borrow `*x.__pointer` as mutable more than once at a time | ||
--> $DIR/pin-reborrow-once.rs:12:14 | ||
| | ||
LL | twice(x, x); | ||
| ----- - ^ second mutable borrow occurs here | ||
| | | | ||
| | first mutable borrow occurs here | ||
| first borrow later used by call | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0499`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//@ check-pass | ||
//@ignore-test | ||
|
||
// Currently ignored due to self reborrowing not being implemented for Pin | ||
|
||
#![feature(pin_ergonomics)] | ||
#![allow(incomplete_features)] | ||
|
||
use std::pin::Pin; | ||
|
||
struct Foo; | ||
|
||
impl Foo { | ||
fn foo(self: Pin<&mut Self>) { | ||
} | ||
} | ||
|
||
fn bar(x: Pin<&mut Foo>) { | ||
x.foo(); | ||
x.foo(); // for this to work we need to automatically reborrow, | ||
// as if the user had written `x.as_mut().foo()`. | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//@check-pass | ||
|
||
#![feature(pin_ergonomics)] | ||
#![allow(dead_code, incomplete_features)] | ||
|
||
use std::pin::Pin; | ||
|
||
fn shorter<'b, T: 'b>(_: Pin<&'b mut T>) {} | ||
|
||
fn test<'a: 'b, 'b, T: 'a>(x: Pin<&'a mut T>) { | ||
shorter::<'b>(x); | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![allow(dead_code, incomplete_features)] | ||
|
||
use std::pin::Pin; | ||
|
||
struct Foo; | ||
|
||
fn foo(_: Pin<&mut Foo>) { | ||
} | ||
|
||
fn bar(mut x: Pin<&mut Foo>) { | ||
foo(x); | ||
foo(x); //~ ERROR use of moved value: `x` | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.