Skip to content
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

backport new syntax to rustfmt 1.x #4105

Merged
Merged
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
10 changes: 6 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,14 +1988,16 @@ pub(crate) fn prefer_next_line(

fn rewrite_expr_addrof(
context: &RewriteContext<'_>,
_borrow_kind: ast::BorrowKind,
borrow_kind: ast::BorrowKind,
mutability: ast::Mutability,
expr: &ast::Expr,
shape: Shape,
) -> Option<String> {
let operator_str = match mutability {
ast::Mutability::Not => "&",
ast::Mutability::Mut => "&mut ",
let operator_str = match (mutability, borrow_kind) {
(ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
(ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
(ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
(ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
};
rewrite_unary_prefix(context, operator_str, expr, shape)
}
Expand Down
58 changes: 37 additions & 21 deletions src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
}
}

struct RangeOperand<'a>(&'a Option<ptr::P<ast::Expr>>);

impl<'a> Rewrite for RangeOperand<'a> {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match &self.0 {
None => Some("".to_owned()),
Some(ref exp) => exp.rewrite(context, shape),
}
}
}

impl Rewrite for Pat {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match self.kind {
Expand Down Expand Up @@ -179,29 +190,34 @@ impl Rewrite for Pat {
None
}
}
PatKind::Range(ref lhs, ref rhs, ref end_kind) => match (lhs, rhs) {
(Some(lhs), Some(rhs)) => {
let infix = match end_kind.node {
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
RangeEnd::Excluded => "..",
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
let infix = match end_kind.node {
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
RangeEnd::Excluded => "..",
};
let infix = if context.config.spaces_around_ranges() {
let lhs_spacing = match lhs {
None => "",
Some(_) => " ",
};
let infix = if context.config.spaces_around_ranges() {
format!(" {} ", infix)
} else {
infix.to_owned()
let rhs_spacing = match rhs {
None => "",
Some(_) => " ",
};
rewrite_pair(
&**lhs,
&**rhs,
PairParts::infix(&infix),
context,
shape,
SeparatorPlace::Front,
)
}
(_, _) => unimplemented!(),
},
format!("{}{}{}", lhs_spacing, infix, rhs_spacing)
} else {
infix.to_owned()
};
rewrite_pair(
&RangeOperand(lhs),
&RangeOperand(rhs),
PairParts::infix(&infix),
context,
shape,
SeparatorPlace::Front,
)
}
PatKind::Ref(ref pat, mutability) => {
let prefix = format!("&{}", format_mutability(mutability));
rewrite_unary_prefix(context, &prefix, &**pat, shape)
Expand Down
7 changes: 6 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,12 @@ impl Rewrite for ast::GenericBound {
ast::TraitBoundModifier::Maybe => poly_trait_ref
.rewrite(context, shape.offset_left(1)?)
.map(|s| format!("?{}", s)),
_ => unimplemented!(),
ast::TraitBoundModifier::MaybeConst => poly_trait_ref
.rewrite(context, shape.offset_left(7)?)
.map(|s| format!("?const {}", s)),
ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref
.rewrite(context, shape.offset_left(8)?)
.map(|s| format!("?const ?{}", s)),
};
rewrite.map(|s| if has_paren { format!("({})", s) } else { s })
}
Expand Down
12 changes: 12 additions & 0 deletions tests/source/configs/spaces_around_ranges/false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ fn main() {
_ => bar,
}
}

fn half_open() {
match [5 .. 4, 99 .. 105, 43 .. 44] {
[_, 99 .., _] => {}
[_, .. 105, _] => {}
_ => {}
};

if let ..= 5 = 0 {}
if let .. 5 = 0 {}
if let 5 .. = 0 {}
}
12 changes: 12 additions & 0 deletions tests/source/configs/spaces_around_ranges/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ fn main() {
_ => bar,
}
}

fn half_open() {
match [5..4, 99..105, 43..44] {
[_, 99.., _] => {}
[_, ..105, _] => {}
_ => {}
};

if let ..=5 = 0 {}
if let ..5 = 0 {}
if let 5.. = 0 {}
}
4 changes: 4 additions & 0 deletions tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ fn returns() {
fn addrof() {
& mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
& (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

// raw reference operator
& raw const a;
& raw mut b;
}

fn casts() {
Expand Down
27 changes: 27 additions & 0 deletions tests/source/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,30 @@ fn foo(a: SomeLongComplexType, b: SomeOtherLongComplexType) -> Box<Future<Item =
}

type MyFn = fn(a: SomeLongComplexType, b: SomeOtherLongComplexType,) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;

// Const opt-out

trait T: ? const Super {}

const fn maybe_const<S: ? const T>() -> i32 { <S as T>::CONST }

struct S<T:? const ? Sized>(std::marker::PhantomData<T>);

impl ? const T {}

fn trait_object() -> &'static dyn ? const T { &S }

fn i(_: impl IntoIterator<Item = Box<dyn ? const T>>) {}

fn apit(_: impl ?const T) {}

fn rpit() -> impl ? const T { S }

pub struct Foo<T: Trait>(T);
impl<T: ? const Trait> Foo<T> {
fn new(t: T) -> Self {
// not calling methods on `t`, so we opt out of requiring
// `<T as Trait>` to have const methods via `?const`
Self(t)
}
}
12 changes: 12 additions & 0 deletions tests/target/configs/spaces_around_ranges/false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ fn main() {
_ => bar,
}
}

fn half_open() {
match [5..4, 99..105, 43..44] {
[_, 99.., _] => {}
[_, ..105, _] => {}
_ => {}
};

if let ..=5 = 0 {}
if let ..5 = 0 {}
if let 5.. = 0 {}
}
12 changes: 12 additions & 0 deletions tests/target/configs/spaces_around_ranges/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ fn main() {
_ => bar,
}
}

fn half_open() {
match [5 .. 4, 99 .. 105, 43 .. 44] {
[_, 99 .., _] => {}
[_, .. 105, _] => {}
_ => {}
};

if let ..= 5 = 0 {}
if let .. 5 = 0 {}
if let 5 .. = 0 {}
}
4 changes: 4 additions & 0 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ fn addrof() {
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
&(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

// raw reference operator
&raw const a;
&raw mut b;
}

fn casts() {
Expand Down
33 changes: 33 additions & 0 deletions tests/target/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,36 @@ type MyFn = fn(
a: SomeLongComplexType,
b: SomeOtherLongComplexType,
) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;

// Const opt-out

trait T: ?const Super {}

const fn maybe_const<S: ?const T>() -> i32 {
<S as T>::CONST
}

struct S<T: ?const ?Sized>(std::marker::PhantomData<T>);

impl ?const T {}

fn trait_object() -> &'static dyn ?const T {
&S
}

fn i(_: impl IntoIterator<Item = Box<dyn ?const T>>) {}

fn apit(_: impl ?const T) {}

fn rpit() -> impl ?const T {
S
}

pub struct Foo<T: Trait>(T);
impl<T: ?const Trait> Foo<T> {
fn new(t: T) -> Self {
// not calling methods on `t`, so we opt out of requiring
// `<T as Trait>` to have const methods via `?const`
Self(t)
}
}