From fefdcc1e873c2338dc8aef0e7abd919b82647da0 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Mon, 23 Sep 2024 16:07:31 -0700 Subject: [PATCH] Add basic pin sugar support to rustfmt --- src/tools/rustfmt/src/types.rs | 8 +++++++- src/tools/rustfmt/tests/source/pin_sugar.rs | 10 ++++++++++ src/tools/rustfmt/tests/target/pin_sugar.rs | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/tools/rustfmt/tests/source/pin_sugar.rs create mode 100644 src/tools/rustfmt/tests/target/pin_sugar.rs diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs index 999deb5dd4aa7..f7177c7f85464 100644 --- a/src/tools/rustfmt/src/types.rs +++ b/src/tools/rustfmt/src/types.rs @@ -829,7 +829,6 @@ impl Rewrite for ast::Ty { } ast::TyKind::Ref(ref lifetime, ref mt) | ast::TyKind::PinnedRef(ref lifetime, ref mt) => { - // FIXME(pin_ergonomics): correctly format pinned reference syntax let mut_str = format_mutability(mt.mutbl); let mut_len = mut_str.len(); let mut result = String::with_capacity(128); @@ -863,6 +862,13 @@ impl Rewrite for ast::Ty { cmnt_lo = lifetime.ident.span.hi(); } + if let ast::TyKind::PinnedRef(..) = self.kind { + result.push_str("pin "); + if ast::Mutability::Not == mt.mutbl { + result.push_str("const "); + } + } + if ast::Mutability::Mut == mt.mutbl { let mut_hi = context.snippet_provider.span_after(self.span(), "mut"); let before_mut_span = mk_sp(cmnt_lo, mut_hi - BytePos::from_usize(3)); diff --git a/src/tools/rustfmt/tests/source/pin_sugar.rs b/src/tools/rustfmt/tests/source/pin_sugar.rs new file mode 100644 index 0000000000000..0eb3c0770c482 --- /dev/null +++ b/src/tools/rustfmt/tests/source/pin_sugar.rs @@ -0,0 +1,10 @@ +// See #130494 + +#![feature(pin_ergonomics)] +#![allow(incomplete_features)] + +fn f(x: &pin const i32) {} +fn g<'a>(x: & 'a pin const i32) {} +fn h<'a>(x: & 'a pin +mut i32) {} +fn i(x: &pin mut i32) {} diff --git a/src/tools/rustfmt/tests/target/pin_sugar.rs b/src/tools/rustfmt/tests/target/pin_sugar.rs new file mode 100644 index 0000000000000..c9fa883e238fd --- /dev/null +++ b/src/tools/rustfmt/tests/target/pin_sugar.rs @@ -0,0 +1,9 @@ +// See #130494 + +#![feature(pin_ergonomics)] +#![allow(incomplete_features)] + +fn f(x: &pin const i32) {} +fn g<'a>(x: &'a pin const i32) {} +fn h<'a>(x: &'a pin mut i32) {} +fn i(x: &pin mut i32) {}