Skip to content

Commit 00e199c

Browse files
backport new syntax to rustfmt 1.x (#4105)
* feat: support raw reference operator * feat: support const opt-out syntax * feat: support half open range syntax
1 parent 5ca3d02 commit 00e199c

File tree

11 files changed

+165
-26
lines changed

11 files changed

+165
-26
lines changed

src/expr.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1988,14 +1988,16 @@ pub(crate) fn prefer_next_line(
19881988

19891989
fn rewrite_expr_addrof(
19901990
context: &RewriteContext<'_>,
1991-
_borrow_kind: ast::BorrowKind,
1991+
borrow_kind: ast::BorrowKind,
19921992
mutability: ast::Mutability,
19931993
expr: &ast::Expr,
19941994
shape: Shape,
19951995
) -> Option<String> {
1996-
let operator_str = match mutability {
1997-
ast::Mutability::Not => "&",
1998-
ast::Mutability::Mut => "&mut ",
1996+
let operator_str = match (mutability, borrow_kind) {
1997+
(ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
1998+
(ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
1999+
(ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
2000+
(ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
19992001
};
20002002
rewrite_unary_prefix(context, operator_str, expr, shape)
20012003
}

src/patterns.rs

+37-21
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
5555
}
5656
}
5757

58+
struct RangeOperand<'a>(&'a Option<ptr::P<ast::Expr>>);
59+
60+
impl<'a> Rewrite for RangeOperand<'a> {
61+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
62+
match &self.0 {
63+
None => Some("".to_owned()),
64+
Some(ref exp) => exp.rewrite(context, shape),
65+
}
66+
}
67+
}
68+
5869
impl Rewrite for Pat {
5970
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
6071
match self.kind {
@@ -179,29 +190,34 @@ impl Rewrite for Pat {
179190
None
180191
}
181192
}
182-
PatKind::Range(ref lhs, ref rhs, ref end_kind) => match (lhs, rhs) {
183-
(Some(lhs), Some(rhs)) => {
184-
let infix = match end_kind.node {
185-
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
186-
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
187-
RangeEnd::Excluded => "..",
193+
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
194+
let infix = match end_kind.node {
195+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
196+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
197+
RangeEnd::Excluded => "..",
198+
};
199+
let infix = if context.config.spaces_around_ranges() {
200+
let lhs_spacing = match lhs {
201+
None => "",
202+
Some(_) => " ",
188203
};
189-
let infix = if context.config.spaces_around_ranges() {
190-
format!(" {} ", infix)
191-
} else {
192-
infix.to_owned()
204+
let rhs_spacing = match rhs {
205+
None => "",
206+
Some(_) => " ",
193207
};
194-
rewrite_pair(
195-
&**lhs,
196-
&**rhs,
197-
PairParts::infix(&infix),
198-
context,
199-
shape,
200-
SeparatorPlace::Front,
201-
)
202-
}
203-
(_, _) => unimplemented!(),
204-
},
208+
format!("{}{}{}", lhs_spacing, infix, rhs_spacing)
209+
} else {
210+
infix.to_owned()
211+
};
212+
rewrite_pair(
213+
&RangeOperand(lhs),
214+
&RangeOperand(rhs),
215+
PairParts::infix(&infix),
216+
context,
217+
shape,
218+
SeparatorPlace::Front,
219+
)
220+
}
205221
PatKind::Ref(ref pat, mutability) => {
206222
let prefix = format!("&{}", format_mutability(mutability));
207223
rewrite_unary_prefix(context, &prefix, &**pat, shape)

src/types.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,12 @@ impl Rewrite for ast::GenericBound {
527527
ast::TraitBoundModifier::Maybe => poly_trait_ref
528528
.rewrite(context, shape.offset_left(1)?)
529529
.map(|s| format!("?{}", s)),
530-
_ => unimplemented!(),
530+
ast::TraitBoundModifier::MaybeConst => poly_trait_ref
531+
.rewrite(context, shape.offset_left(7)?)
532+
.map(|s| format!("?const {}", s)),
533+
ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref
534+
.rewrite(context, shape.offset_left(8)?)
535+
.map(|s| format!("?const ?{}", s)),
531536
};
532537
rewrite.map(|s| if has_paren { format!("({})", s) } else { s })
533538
}

tests/source/configs/spaces_around_ranges/false.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5 .. 4, 99 .. 105, 43 .. 44] {
26+
[_, 99 .., _] => {}
27+
[_, .. 105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..= 5 = 0 {}
32+
if let .. 5 = 0 {}
33+
if let 5 .. = 0 {}
34+
}

tests/source/configs/spaces_around_ranges/true.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5..4, 99..105, 43..44] {
26+
[_, 99.., _] => {}
27+
[_, ..105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..=5 = 0 {}
32+
if let ..5 = 0 {}
33+
if let 5.. = 0 {}
34+
}

tests/source/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ fn returns() {
218218
fn addrof() {
219219
& mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
220220
& (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
221+
222+
// raw reference operator
223+
& raw const a;
224+
& raw mut b;
221225
}
222226

223227
fn casts() {

tests/source/type.rs

+27
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,30 @@ fn foo(a: SomeLongComplexType, b: SomeOtherLongComplexType) -> Box<Future<Item =
139139
}
140140

141141
type MyFn = fn(a: SomeLongComplexType, b: SomeOtherLongComplexType,) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;
142+
143+
// Const opt-out
144+
145+
trait T: ? const Super {}
146+
147+
const fn maybe_const<S: ? const T>() -> i32 { <S as T>::CONST }
148+
149+
struct S<T:? const ? Sized>(std::marker::PhantomData<T>);
150+
151+
impl ? const T {}
152+
153+
fn trait_object() -> &'static dyn ? const T { &S }
154+
155+
fn i(_: impl IntoIterator<Item = Box<dyn ? const T>>) {}
156+
157+
fn apit(_: impl ?const T) {}
158+
159+
fn rpit() -> impl ? const T { S }
160+
161+
pub struct Foo<T: Trait>(T);
162+
impl<T: ? const Trait> Foo<T> {
163+
fn new(t: T) -> Self {
164+
// not calling methods on `t`, so we opt out of requiring
165+
// `<T as Trait>` to have const methods via `?const`
166+
Self(t)
167+
}
168+
}

tests/target/configs/spaces_around_ranges/false.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5..4, 99..105, 43..44] {
26+
[_, 99.., _] => {}
27+
[_, ..105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..=5 = 0 {}
32+
if let ..5 = 0 {}
33+
if let 5.. = 0 {}
34+
}

tests/target/configs/spaces_around_ranges/true.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn main() {
2020
_ => bar,
2121
}
2222
}
23+
24+
fn half_open() {
25+
match [5 .. 4, 99 .. 105, 43 .. 44] {
26+
[_, 99 .., _] => {}
27+
[_, .. 105, _] => {}
28+
_ => {}
29+
};
30+
31+
if let ..= 5 = 0 {}
32+
if let .. 5 = 0 {}
33+
if let 5 .. = 0 {}
34+
}

tests/target/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ fn addrof() {
253253
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
254254
&(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
255255
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
256+
257+
// raw reference operator
258+
&raw const a;
259+
&raw mut b;
256260
}
257261

258262
fn casts() {

tests/target/type.rs

+33
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,36 @@ type MyFn = fn(
144144
a: SomeLongComplexType,
145145
b: SomeOtherLongComplexType,
146146
) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;
147+
148+
// Const opt-out
149+
150+
trait T: ?const Super {}
151+
152+
const fn maybe_const<S: ?const T>() -> i32 {
153+
<S as T>::CONST
154+
}
155+
156+
struct S<T: ?const ?Sized>(std::marker::PhantomData<T>);
157+
158+
impl ?const T {}
159+
160+
fn trait_object() -> &'static dyn ?const T {
161+
&S
162+
}
163+
164+
fn i(_: impl IntoIterator<Item = Box<dyn ?const T>>) {}
165+
166+
fn apit(_: impl ?const T) {}
167+
168+
fn rpit() -> impl ?const T {
169+
S
170+
}
171+
172+
pub struct Foo<T: Trait>(T);
173+
impl<T: ?const Trait> Foo<T> {
174+
fn new(t: T) -> Self {
175+
// not calling methods on `t`, so we opt out of requiring
176+
// `<T as Trait>` to have const methods via `?const`
177+
Self(t)
178+
}
179+
}

0 commit comments

Comments
 (0)