Skip to content

Implement parsing of pinned borrows #135731

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,10 @@ pub enum BorrowKind {
/// The resulting type is either `*const T` or `*mut T`
/// where `T = typeof($expr)`.
Raw,
/// A pinned borrow, `&pin const $expr` or `&pin mut $expr`.
/// The resulting type is either `Pin<&'a T>` or `Pin<&'a mut T>`
/// where `T = typeof($expr)` and `'a` is some lifetime.
Pin,
}

#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ impl<'a> State<'a> {
self.word_nbsp("raw");
self.print_mutability(mutability, true);
}
ast::BorrowKind::Pin => {
self.word_nbsp("pin");
self.print_mutability(mutability, true);
}
}
self.print_expr_cond_paren(
expr,
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,13 @@ impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764),
}),
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::MutableRefEscaping {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764),
}),
hir::BorrowKind::Ref | hir::BorrowKind::Pin => {
ccx.dcx().create_err(errors::MutableRefEscaping {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764),
})
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,10 @@ impl<'a> State<'a> {
self.word_nbsp("raw");
self.print_mutability(mutability, true);
}
hir::BorrowKind::Pin => {
self.word_nbsp("pin");
self.print_mutability(mutability, true);
}
}
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Prefix);
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_named_place_expr(oprnd);
Ty::new_ptr(self.tcx, ty, mutbl)
}
hir::BorrowKind::Ref => {
hir::BorrowKind::Ref | hir::BorrowKind::Pin => {
// Note: at this point, we cannot say what the best lifetime
// is to use for resulting pointer. We want to use the
// shortest lifetime possible so as to avoid spurious borrowck
Expand All @@ -689,7 +689,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// whose address was taken can actually be made to live as long
// as it needs to live.
let region = self.next_region_var(infer::BorrowRegion(expr.span));
Ty::new_ref(self.tcx, region, ty, mutbl)
match kind {
hir::BorrowKind::Ref => Ty::new_ref(self.tcx, region, ty, mutbl),
hir::BorrowKind::Pin => Ty::new_pinned_ref(self.tcx, region, ty, mutbl),
_ => unreachable!(),
}
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,55 @@ impl<'tcx> ThirBuildCx<'tcx> {
ExprKind::RawBorrow { mutability, arg: self.mirror_expr(arg) }
}

// Make `&pin mut $expr` and `&pin const $expr` into
// `Pin { __pointer: &mut { $expr } }` and `Pin { __pointer: &$expr }`.
hir::ExprKind::AddrOf(hir::BorrowKind::Pin, mutbl, arg_expr) => match expr_ty.kind() {
&ty::Adt(adt_def, args) if tcx.is_lang_item(adt_def.did(), hir::LangItem::Pin) => {
let ty = args.type_at(0);
let arg_ty = self.typeck_results.expr_ty(arg_expr);
let mut arg = self.mirror_expr(arg_expr);
// For `&pin mut $place` where `$place` is not `Unpin`, move the place
// `$place` to ensure it will not be used afterwards.
if mutbl.is_mut() && !arg_ty.is_unpin(self.tcx, self.typing_env) {
let block = self.thir.blocks.push(Block {
targeted_by_break: false,
region_scope: region::Scope {
local_id: arg_expr.hir_id.local_id,
data: region::ScopeData::Node,
},
span: arg_expr.span,
stmts: Box::new([]),
expr: Some(arg),
safety_mode: BlockSafety::Safe,
});
let (temp_lifetime, backwards_incompatible) = self
.rvalue_scopes
.temporary_scope(self.region_scope_tree, arg_expr.hir_id.local_id);
arg = self.thir.exprs.push(Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
ty: arg_ty,
span: arg_expr.span,
kind: ExprKind::Block { block },
});
}
let expr = self.thir.exprs.push(Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
ty,
span: expr.span,
kind: ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg },
});
ExprKind::Adt(Box::new(AdtExpr {
adt_def,
variant_index: FIRST_VARIANT,
args,
fields: Box::new([FieldExpr { name: FieldIdx::from(0u32), expr }]),
user_ty: None,
base: AdtExprBase::None,
}))
}
_ => span_bug!(expr.span, "unexpected type for pinned borrow: {:?}", expr_ty),
},

hir::ExprKind::Block(blk, _) => ExprKind::Block { block: self.mirror_block(blk) },

hir::ExprKind::Assign(lhs, rhs, _) => {
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,19 @@ impl<'a> Parser<'a> {
self.dcx().emit_err(errors::LifetimeInBorrowExpression { span, lifetime_span: lt_span });
}

/// Parse `mut?` or `raw [ const | mut ]`.
/// Parse `mut?` or `[ raw | pin ] [ const | mut ]`.
fn parse_borrow_modifiers(&mut self) -> (ast::BorrowKind, ast::Mutability) {
if self.check_keyword(exp!(Raw)) && self.look_ahead(1, Token::is_mutability) {
// `raw [ const | mut ]`.
let found_raw = self.eat_keyword(exp!(Raw));
assert!(found_raw);
let mutability = self.parse_const_or_mut().unwrap();
(ast::BorrowKind::Raw, mutability)
} else if let Some((ast::Pinnedness::Pinned, mutbl)) = self.parse_pin_and_mut() {
// `pin [ const | mut ]`.
// `pin` has been gated in `self.parse_pin_and_mut()` so we don't
// need to gate it here.
(ast::BorrowKind::Pin, mutbl)
} else {
// `mut?`
(ast::BorrowKind::Ref, self.parse_mutability())
Expand Down
2 changes: 2 additions & 0 deletions src/tools/rustfmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2289,8 +2289,10 @@ fn rewrite_expr_addrof(
) -> RewriteResult {
let operator_str = match (mutability, borrow_kind) {
(ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
(ast::Mutability::Not, ast::BorrowKind::Pin) => "&pin const ",
(ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
(ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
(ast::Mutability::Mut, ast::BorrowKind::Pin) => "&pin mut ",
(ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
};
rewrite_unary_prefix(context, operator_str, expr, shape)
Expand Down
10 changes: 10 additions & 0 deletions src/tools/rustfmt/tests/source/pin_sugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ impl Foo {
mut self) {}
fn i(&pin mut self) {}
}

fn borrows() {
let mut foo = 0_i32;
let x: Pin<&mut _> = & pin
mut foo;

let x: Pin<&_> = &
pin const
foo;
}
7 changes: 7 additions & 0 deletions src/tools/rustfmt/tests/target/pin_sugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ impl Foo {
fn h<'a>(&'a pin mut self) {}
fn i(&pin mut self) {}
}

fn borrows() {
let mut foo = 0_i32;
let x: Pin<&mut _> = &pin mut foo;

let x: Pin<&_> = &pin const foo;
}
43 changes: 43 additions & 0 deletions tests/pretty/pin-ergonomics-hir.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//@ pretty-compare-only
//@ pretty-mode:hir
//@ pp-exact:pin-ergonomics-hir.pp

#![feature(pin_ergonomics)]#![allow(dead_code, incomplete_features)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;

use std::pin::Pin;

struct Foo;

impl Foo {
fn baz(&mut self) { }

fn baz_const(&self) { }

fn baz_lt<'a>(&mut self) { }

fn baz_const_lt(&self) { }
}

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

fn foo_const(_: Pin<&'_ Foo>) { }
fn foo_const_lt(_: Pin<&'_ Foo>) { }

fn bar() {
let mut x: Pin<&mut _> = &pin mut Foo;
foo(x.as_mut());
foo(x.as_mut());
foo_const(x);

let x: Pin<&_> = &pin const Foo;

foo_const(x);
foo_const(x);
}

fn main() { }
40 changes: 40 additions & 0 deletions tests/pretty/pin-ergonomics-hir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//@ pretty-compare-only
//@ pretty-mode:hir
//@ pp-exact:pin-ergonomics-hir.pp

#![feature(pin_ergonomics)]
#![allow(dead_code, incomplete_features)]

use std::pin::Pin;

struct Foo;

impl Foo {
fn baz(&mut self) { }

fn baz_const(&self) { }

fn baz_lt<'a>(&mut self) { }

fn baz_const_lt(&self) { }
}

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

fn foo_const(_: Pin<&'_ Foo>) { }
fn foo_const_lt(_: Pin<&'_ Foo>) { }

fn bar() {
let mut x: Pin<&mut _> = &pin mut Foo;
foo(x.as_mut());
foo(x.as_mut());
foo_const(x);

let x: Pin<&_> = &pin const Foo;

foo_const(x);
foo_const(x);
}

fn main() { }
13 changes: 13 additions & 0 deletions tests/pretty/pin-ergonomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![feature(pin_ergonomics)]
#![allow(dead_code, incomplete_features)]

use std::pin::Pin;

struct Foo;

impl Foo {
Expand All @@ -21,4 +23,15 @@ fn foo_lt<'a>(_: &'a pin mut Foo) {}
fn foo_const(_: &pin const Foo) {}
fn foo_const_lt(_: &'_ pin const Foo) {}

fn bar() {
let mut x: Pin<&mut _> = &pin mut Foo;
foo(x.as_mut());
foo(x.as_mut());
foo_const(x);

let x: Pin<&_> = &pin const Foo;
foo_const(x);
foo_const(x);
}

fn main() {}
28 changes: 28 additions & 0 deletions tests/ui/feature-gates/feature-gate-pin_ergonomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ fn foo(mut x: Pin<&mut Foo>) {
let _y: &pin mut Foo = x; //~ ERROR pinned reference syntax is experimental
}

fn foo_const(x: Pin<&Foo>) {
let _y: &pin const Foo = x; //~ ERROR pinned reference syntax is experimental
}

fn foo_sugar(_: &pin mut Foo) {} //~ ERROR pinned reference syntax is experimental

fn bar(x: Pin<&mut Foo>) {
Expand All @@ -31,6 +35,18 @@ fn baz(mut x: Pin<&mut Foo>) {

fn baz_sugar(_: &pin const Foo) {} //~ ERROR pinned reference syntax is experimental

fn borrows() {
let mut x: Pin<&mut _> = &pin mut Foo; //~ ERROR pinned reference syntax is experimental
foo(x.as_mut());
foo(x.as_mut());
foo_const(x.as_ref());

let x: Pin<&_> = &pin const Foo; //~ ERROR pinned reference syntax is experimental

foo_const(x);
foo_const(x);
}

#[cfg(any())]
mod not_compiled {
use std::pin::Pin;
Expand Down Expand Up @@ -63,6 +79,18 @@ mod not_compiled {
}

fn baz_sugar(_: &pin const Foo) {} //~ ERROR pinned reference syntax is experimental

fn borrows() {
let mut x: Pin<&mut _> = &pin mut Foo; //~ ERROR pinned reference syntax is experimental
foo(x.as_mut());
foo(x.as_mut());
foo_const(x.as_ref());

let x: Pin<&_> = &pin const Foo; //~ ERROR pinned reference syntax is experimental

foo_const(x);
foo_const(x);
}
}

fn main() {}
Loading
Loading