Skip to content

Commit fd6ceeb

Browse files
Rollup merge of #135731 - frank-king:feature/pin-borrow, r=eholk,traviscross
Implement parsing of pinned borrows This PR implements part of #130494. EDIT: It introduces `&pin mut $place` and `&pin const $place` as sugars for `std::pin::pin!($place)` and its shared reference equivalent, except that `$place` will not be moved when borrowing. The borrow check will be in charge of enforcing places cannot be moved or mutably borrowed since being pinned till dropped. ### Implementation steps: - [x] parse the `&pin mut $place` and `&pin const $place` syntaxes - [ ] borrowck of `&pin mut|const` - [ ] support autoref of `&pin mut|const` when needed
2 parents 8cf8b25 + d37c63f commit fd6ceeb

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,8 +2289,10 @@ fn rewrite_expr_addrof(
22892289
) -> RewriteResult {
22902290
let operator_str = match (mutability, borrow_kind) {
22912291
(ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
2292+
(ast::Mutability::Not, ast::BorrowKind::Pin) => "&pin const ",
22922293
(ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
22932294
(ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
2295+
(ast::Mutability::Mut, ast::BorrowKind::Pin) => "&pin mut ",
22942296
(ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
22952297
};
22962298
rewrite_unary_prefix(context, operator_str, expr, shape)

tests/source/pin_sugar.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ impl Foo {
1818
mut self) {}
1919
fn i(&pin mut self) {}
2020
}
21+
22+
fn borrows() {
23+
let mut foo = 0_i32;
24+
let x: Pin<&mut _> = & pin
25+
mut foo;
26+
27+
let x: Pin<&_> = &
28+
pin const
29+
foo;
30+
}

tests/target/pin_sugar.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ impl Foo {
1616
fn h<'a>(&'a pin mut self) {}
1717
fn i(&pin mut self) {}
1818
}
19+
20+
fn borrows() {
21+
let mut foo = 0_i32;
22+
let x: Pin<&mut _> = &pin mut foo;
23+
24+
let x: Pin<&_> = &pin const foo;
25+
}

0 commit comments

Comments
 (0)