Skip to content

Commit 35f9799

Browse files
committed
Add a TyPat in the AST to reuse the generic arg lowering logic
1 parent 58ba360 commit 35f9799

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

src/patterns.rs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ fn is_short_pattern_inner(context: &RewriteContext<'_>, pat: &ast::Pat) -> bool
7575
}
7676
}
7777

78-
pub(crate) struct RangeOperand<'a> {
79-
operand: &'a Option<ptr::P<ast::Expr>>,
80-
pub(crate) span: Span,
78+
pub(crate) struct RangeOperand<'a, T> {
79+
pub operand: &'a Option<ptr::P<T>>,
80+
pub span: Span,
8181
}
8282

83-
impl<'a> Rewrite for RangeOperand<'a> {
83+
impl<'a, T: Rewrite> Rewrite for RangeOperand<'a, T> {
8484
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
8585
self.rewrite_result(context, shape).ok()
8686
}
@@ -259,40 +259,7 @@ impl Rewrite for Pat {
259259
}
260260
PatKind::Never => Err(RewriteError::Unknown),
261261
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
262-
let infix = match end_kind.node {
263-
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
264-
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
265-
RangeEnd::Excluded => "..",
266-
};
267-
let infix = if context.config.spaces_around_ranges() {
268-
let lhs_spacing = match lhs {
269-
None => "",
270-
Some(_) => " ",
271-
};
272-
let rhs_spacing = match rhs {
273-
None => "",
274-
Some(_) => " ",
275-
};
276-
format!("{lhs_spacing}{infix}{rhs_spacing}")
277-
} else {
278-
infix.to_owned()
279-
};
280-
let lspan = self.span.with_hi(end_kind.span.lo());
281-
let rspan = self.span.with_lo(end_kind.span.hi());
282-
rewrite_pair(
283-
&RangeOperand {
284-
operand: lhs,
285-
span: lspan,
286-
},
287-
&RangeOperand {
288-
operand: rhs,
289-
span: rspan,
290-
},
291-
PairParts::infix(&infix),
292-
context,
293-
shape,
294-
SeparatorPlace::Front,
295-
)
262+
rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span)
296263
}
297264
PatKind::Ref(ref pat, mutability) => {
298265
let prefix = format!("&{}", format_mutability(mutability));
@@ -359,6 +326,50 @@ impl Rewrite for Pat {
359326
}
360327
}
361328

329+
pub fn rewrite_range_pat<T: Rewrite>(
330+
context: &RewriteContext<'_>,
331+
shape: Shape,
332+
lhs: &Option<ptr::P<T>>,
333+
rhs: &Option<ptr::P<T>>,
334+
end_kind: &rustc_span::source_map::Spanned<RangeEnd>,
335+
span: Span,
336+
) -> RewriteResult {
337+
let infix = match end_kind.node {
338+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
339+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
340+
RangeEnd::Excluded => "..",
341+
};
342+
let infix = if context.config.spaces_around_ranges() {
343+
let lhs_spacing = match lhs {
344+
None => "",
345+
Some(_) => " ",
346+
};
347+
let rhs_spacing = match rhs {
348+
None => "",
349+
Some(_) => " ",
350+
};
351+
format!("{lhs_spacing}{infix}{rhs_spacing}")
352+
} else {
353+
infix.to_owned()
354+
};
355+
let lspan = span.with_hi(end_kind.span.lo());
356+
let rspan = span.with_lo(end_kind.span.hi());
357+
rewrite_pair(
358+
&RangeOperand {
359+
operand: lhs,
360+
span: lspan,
361+
},
362+
&RangeOperand {
363+
operand: rhs,
364+
span: rspan,
365+
},
366+
PairParts::infix(&infix),
367+
context,
368+
shape,
369+
SeparatorPlace::Front,
370+
)
371+
}
372+
362373
fn rewrite_struct_pat(
363374
qself: &Option<ptr::P<ast::QSelf>>,
364375
path: &ast::Path,

src/spanned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Spanned for ast::PreciseCapturingArg {
211211
}
212212
}
213213

214-
impl<'a> Spanned for RangeOperand<'a> {
214+
impl<'a, T> Spanned for RangeOperand<'a, T> {
215215
fn span(&self) -> Span {
216216
self.span
217217
}

src/types.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::lists::{
1818
use crate::macros::{MacroPosition, rewrite_macro};
1919
use crate::overflow;
2020
use crate::pairs::{PairParts, rewrite_pair};
21+
use crate::patterns::rewrite_range_pat;
2122
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
2223
use crate::shape::Shape;
2324
use crate::source_map::SpanUtils;
@@ -1045,6 +1046,21 @@ impl Rewrite for ast::Ty {
10451046
}
10461047
}
10471048

1049+
impl Rewrite for ast::TyPat {
1050+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1051+
self.rewrite_result(context, shape).ok()
1052+
}
1053+
1054+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1055+
match self.kind {
1056+
ast::TyPatKind::Range(ref lhs, ref rhs, ref end_kind) => {
1057+
rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span)
1058+
}
1059+
ast::TyPatKind::Err(_) => Err(RewriteError::Unknown),
1060+
}
1061+
}
1062+
}
1063+
10481064
fn rewrite_bare_fn(
10491065
bare_fn: &ast::BareFnTy,
10501066
span: Span,

0 commit comments

Comments
 (0)