Skip to content

Commit cb7d687

Browse files
committed
Implement &pin const self and &pin mut self sugars
1 parent 4559163 commit cb7d687

File tree

10 files changed

+304
-13
lines changed

10 files changed

+304
-13
lines changed

compiler/rustc_ast/src/ast.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,8 @@ pub enum SelfKind {
26412641
Value(Mutability),
26422642
/// `&'lt self`, `&'lt mut self`
26432643
Region(Option<Lifetime>, Mutability),
2644+
/// `&'lt pin const self`, `&'lt pin mut self`
2645+
Pinned(Option<Lifetime>, Mutability),
26442646
/// `self: TYPE`, `mut self: TYPE`
26452647
Explicit(P<Ty>, Mutability),
26462648
}
@@ -2650,6 +2652,8 @@ impl SelfKind {
26502652
match self {
26512653
SelfKind::Region(None, mutbl) => mutbl.ref_prefix_str().to_string(),
26522654
SelfKind::Region(Some(lt), mutbl) => format!("&{lt} {}", mutbl.prefix_str()),
2655+
SelfKind::Pinned(None, mutbl) => format!("&pin {}", mutbl.ptr_str()),
2656+
SelfKind::Pinned(Some(lt), mutbl) => format!("&{lt} pin {}", mutbl.ptr_str()),
26532657
SelfKind::Value(_) | SelfKind::Explicit(_, _) => {
26542658
unreachable!("if we had an explicit self, we wouldn't be here")
26552659
}
@@ -2666,11 +2670,13 @@ impl Param {
26662670
if ident.name == kw::SelfLower {
26672671
return match self.ty.kind {
26682672
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
2669-
TyKind::Ref(lt, MutTy { ref ty, mutbl })
2670-
| TyKind::PinnedRef(lt, MutTy { ref ty, mutbl })
2673+
TyKind::Ref(lt, MutTy { ref ty, mutbl }) if ty.kind.is_implicit_self() => {
2674+
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
2675+
}
2676+
TyKind::PinnedRef(lt, MutTy { ref ty, mutbl })
26712677
if ty.kind.is_implicit_self() =>
26722678
{
2673-
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
2679+
Some(respan(self.pat.span, SelfKind::Pinned(lt, mutbl)))
26742680
}
26752681
_ => Some(respan(
26762682
self.pat.span.to(self.ty.span),
@@ -2712,6 +2718,15 @@ impl Param {
27122718
tokens: None,
27132719
}),
27142720
),
2721+
SelfKind::Pinned(lt, mutbl) => (
2722+
mutbl,
2723+
P(Ty {
2724+
id: DUMMY_NODE_ID,
2725+
kind: TyKind::PinnedRef(lt, MutTy { ty: infer_ty, mutbl }),
2726+
span,
2727+
tokens: None,
2728+
}),
2729+
),
27152730
};
27162731
Param {
27172732
attrs,

compiler/rustc_ast_pretty/src/pprust/state.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,13 @@ impl<'a> State<'a> {
17831783
self.print_mutability(*m, false);
17841784
self.word("self")
17851785
}
1786+
SelfKind::Pinned(lt, m) => {
1787+
self.word("&");
1788+
self.print_opt_lifetime(lt);
1789+
self.word("pin ");
1790+
self.print_mutability(*m, true);
1791+
self.word("self")
1792+
}
17861793
SelfKind::Explicit(typ, m) => {
17871794
self.print_mutability(*m, false);
17881795
self.word("self");

compiler/rustc_parse/src/parser/item.rs

+45
Original file line numberDiff line numberDiff line change
@@ -2959,9 +2959,20 @@ impl<'a> Parser<'a> {
29592959
this.is_keyword_ahead(n, &[kw::SelfLower])
29602960
&& this.look_ahead(n + 1, |t| t != &token::PathSep)
29612961
};
2962+
// Is `pin const self` `n` tokens ahead?
2963+
let is_isolated_pin_const_self = |this: &Self, n| {
2964+
this.look_ahead(n, |token| token.is_ident_named(sym::pin))
2965+
&& this.is_keyword_ahead(n + 1, &[kw::Const])
2966+
&& is_isolated_self(this, n + 2)
2967+
};
29622968
// Is `mut self` `n` tokens ahead?
29632969
let is_isolated_mut_self =
29642970
|this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
2971+
// Is `pin mut self` `n` tokens ahead?
2972+
let is_isolated_pin_mut_self = |this: &Self, n| {
2973+
this.look_ahead(n, |token| token.is_ident_named(sym::pin))
2974+
&& is_isolated_mut_self(this, n + 1)
2975+
};
29652976
// Parse `self` or `self: TYPE`. We already know the current token is `self`.
29662977
let parse_self_possibly_typed = |this: &mut Self, m| {
29672978
let eself_ident = expect_self_ident(this);
@@ -3021,6 +3032,20 @@ impl<'a> Parser<'a> {
30213032
self.bump();
30223033
self.bump();
30233034
SelfKind::Region(None, Mutability::Mut)
3035+
} else if is_isolated_pin_const_self(self, 1) {
3036+
// `&pin const self`
3037+
self.bump(); // &
3038+
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3039+
self.bump(); // pin
3040+
self.bump(); // const
3041+
SelfKind::Pinned(None, Mutability::Not)
3042+
} else if is_isolated_pin_mut_self(self, 1) {
3043+
// `&pin mut self`
3044+
self.bump(); // &
3045+
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3046+
self.bump(); // pin
3047+
self.bump(); // mut
3048+
SelfKind::Pinned(None, Mutability::Mut)
30243049
} else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
30253050
// `&'lt self`
30263051
self.bump();
@@ -3032,6 +3057,26 @@ impl<'a> Parser<'a> {
30323057
let lt = self.expect_lifetime();
30333058
self.bump();
30343059
SelfKind::Region(Some(lt), Mutability::Mut)
3060+
} else if self.look_ahead(1, |t| t.is_lifetime())
3061+
&& is_isolated_pin_const_self(self, 2)
3062+
{
3063+
// `&'lt pin const self`
3064+
self.bump(); // &
3065+
let lt = self.expect_lifetime();
3066+
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3067+
self.bump(); // pin
3068+
self.bump(); // const
3069+
SelfKind::Pinned(Some(lt), Mutability::Not)
3070+
} else if self.look_ahead(1, |t| t.is_lifetime())
3071+
&& is_isolated_pin_mut_self(self, 2)
3072+
{
3073+
// `&'lt pin mut self`
3074+
self.bump(); // &
3075+
let lt = self.expect_lifetime();
3076+
self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3077+
self.bump(); // pin
3078+
self.bump(); // mut
3079+
SelfKind::Pinned(Some(lt), Mutability::Mut)
30353080
} else {
30363081
// `&not_self`
30373082
return Ok(None);

src/tools/rustfmt/src/items.rs

+27
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,33 @@ fn rewrite_explicit_self(
23952395
)?),
23962396
}
23972397
}
2398+
ast::SelfKind::Pinned(lt, m) => {
2399+
let mut_str = m.ptr_str();
2400+
match lt {
2401+
Some(ref l) => {
2402+
let lifetime_str = l.rewrite_result(
2403+
context,
2404+
Shape::legacy(context.config.max_width(), Indent::empty()),
2405+
)?;
2406+
Ok(combine_strs_with_missing_comments(
2407+
context,
2408+
param_attrs,
2409+
&format!("&{lifetime_str} pin {mut_str} self"),
2410+
span,
2411+
shape,
2412+
!has_multiple_attr_lines,
2413+
)?)
2414+
}
2415+
None => Ok(combine_strs_with_missing_comments(
2416+
context,
2417+
param_attrs,
2418+
&format!("&pin {mut_str} self"),
2419+
span,
2420+
shape,
2421+
!has_multiple_attr_lines,
2422+
)?),
2423+
}
2424+
}
23982425
ast::SelfKind::Explicit(ref ty, mutability) => {
23992426
let type_str = ty.rewrite_result(
24002427
context,

src/tools/rustfmt/tests/source/pin_sugar.rs

+10
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ fn g<'a>(x: & 'a pin const i32) {}
88
fn h<'a>(x: & 'a pin
99
mut i32) {}
1010
fn i(x: &pin mut i32) {}
11+
12+
struct Foo;
13+
14+
impl Foo {
15+
fn f(&pin const self) {}
16+
fn g<'a>(& 'a pin const self) {}
17+
fn h<'a>(& 'a pin
18+
mut self) {}
19+
fn i(&pin mut self) {}
20+
}

src/tools/rustfmt/tests/target/pin_sugar.rs

+9
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ fn f(x: &pin const i32) {}
77
fn g<'a>(x: &'a pin const i32) {}
88
fn h<'a>(x: &'a pin mut i32) {}
99
fn i(x: &pin mut i32) {}
10+
11+
struct Foo;
12+
13+
impl Foo {
14+
fn f(&pin const self) {}
15+
fn g<'a>(&'a pin const self) {}
16+
fn h<'a>(&'a pin mut self) {}
17+
fn i(&pin mut self) {}
18+
}

tests/pretty/pin-ergonomics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ pp-exact
2+
3+
#![feature(pin_ergonomics)]
4+
#![allow(dead_code, incomplete_features)]
5+
6+
struct Foo;
7+
8+
impl Foo {
9+
fn baz(&pin mut self) {}
10+
11+
fn baz_const(&pin const self) {}
12+
13+
fn baz_lt<'a>(&'a pin mut self) {}
14+
15+
fn baz_const_lt(&'_ pin const self) {}
16+
}
17+
18+
fn foo(_: &pin mut Foo) {}
19+
fn foo_lt<'a>(_: &'a pin mut Foo) {}
20+
21+
fn foo_const(_: &pin const Foo) {}
22+
fn foo_const_lt(_: &'_ pin const Foo) {}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//@ check-pass
2+
3+
#![feature(pin_ergonomics)]
4+
#![allow(dead_code, incomplete_features)]
5+
6+
// Makes sure we can handle `&pin mut self` and `&pin const self` as sugar for
7+
// `self: Pin<&mut Self>` and `self: Pin<&Self>`.
8+
9+
use std::pin::Pin;
10+
11+
struct Foo;
12+
13+
impl Foo {
14+
fn baz(&pin mut self) {}
15+
16+
fn baz_const(&pin const self) {}
17+
18+
fn baz_lt<'a>(&'a pin mut self) {}
19+
20+
fn baz_const_lt(&'_ pin const self) {}
21+
}
22+
23+
fn foo(_: &pin mut Foo) {}
24+
25+
fn foo_const(_: &pin const Foo) {}
26+
27+
fn bar(x: &pin mut Foo) {
28+
// For the calls below to work we need to automatically reborrow,
29+
// as if the user had written `foo(x.as_mut())`.
30+
foo(x);
31+
foo(x);
32+
33+
Foo::baz(x);
34+
Foo::baz(x);
35+
36+
// make sure we can reborrow &mut as &.
37+
foo_const(x);
38+
Foo::baz_const(x);
39+
40+
let x: &pin const _ = Pin::new(&Foo);
41+
42+
foo_const(x); // make sure reborrowing from & to & works.
43+
foo_const(x);
44+
}
45+
46+
fn main() {}

tests/ui/feature-gates/feature-gate-pin_ergonomics.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ struct Foo;
77
impl Foo {
88
fn foo(self: Pin<&mut Self>) {
99
}
10+
fn foo_sugar(&pin mut self) {} //~ ERROR pinned reference syntax is experimental
11+
fn foo_sugar_const(&pin const self) {} //~ ERROR pinned reference syntax is experimental
1012
}
1113

12-
fn foo(x: Pin<&mut Foo>) {
14+
fn foo(mut x: Pin<&mut Foo>) {
15+
Foo::foo_sugar(x.as_mut());
16+
Foo::foo_sugar_const(x.as_ref());
1317
let _y: &pin mut Foo = x; //~ ERROR pinned reference syntax is experimental
1418
}
1519

@@ -27,4 +31,38 @@ fn baz(mut x: Pin<&mut Foo>) {
2731

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

34+
#[cfg(any())]
35+
mod not_compiled {
36+
use std::pin::Pin;
37+
38+
struct Foo;
39+
40+
impl Foo {
41+
fn foo(self: Pin<&mut Self>) {
42+
}
43+
fn foo_sugar(&pin mut self) {} //~ ERROR pinned reference syntax is experimental
44+
fn foo_sugar_const(&pin const self) {} //~ ERROR pinned reference syntax is experimental
45+
}
46+
47+
fn foo(mut x: Pin<&mut Foo>) {
48+
Foo::foo_sugar(x.as_mut());
49+
Foo::foo_sugar_const(x.as_ref());
50+
let _y: &pin mut Foo = x; //~ ERROR pinned reference syntax is experimental
51+
}
52+
53+
fn foo_sugar(_: &pin mut Foo) {} //~ ERROR pinned reference syntax is experimental
54+
55+
fn bar(x: Pin<&mut Foo>) {
56+
foo(x);
57+
foo(x);
58+
}
59+
60+
fn baz(mut x: Pin<&mut Foo>) {
61+
x.foo();
62+
x.foo();
63+
}
64+
65+
fn baz_sugar(_: &pin const Foo) {} //~ ERROR pinned reference syntax is experimental
66+
}
67+
3068
fn main() {}

0 commit comments

Comments
 (0)