-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #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: - #130494 r? `@compiler-errors`
- Loading branch information
Showing
17 changed files
with
316 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -1453,6 +1453,7 @@ symbols! { | |
pic, | ||
pie, | ||
pin, | ||
pin_ergonomics, | ||
platform_intrinsics, | ||
plugin, | ||
plugin_registrar, | ||
|
This file contains 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 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 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 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 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 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 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 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.