Add a TyPat in the AST to reuse the generic arg lowering logic
This commit is contained in:
parent
c182ce9cbc
commit
6d7ce4e893
21 changed files with 241 additions and 264 deletions
|
|
@ -75,12 +75,12 @@ fn is_short_pattern_inner(context: &RewriteContext<'_>, pat: &ast::Pat) -> bool
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RangeOperand<'a> {
|
||||
operand: &'a Option<ptr::P<ast::Expr>>,
|
||||
pub(crate) span: Span,
|
||||
pub(crate) struct RangeOperand<'a, T> {
|
||||
pub operand: &'a Option<ptr::P<T>>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl<'a> Rewrite for RangeOperand<'a> {
|
||||
impl<'a, T: Rewrite> Rewrite for RangeOperand<'a, T> {
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
self.rewrite_result(context, shape).ok()
|
||||
}
|
||||
|
|
@ -259,40 +259,7 @@ impl Rewrite for Pat {
|
|||
}
|
||||
PatKind::Never => Err(RewriteError::Unknown),
|
||||
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 rhs_spacing = match rhs {
|
||||
None => "",
|
||||
Some(_) => " ",
|
||||
};
|
||||
format!("{lhs_spacing}{infix}{rhs_spacing}")
|
||||
} else {
|
||||
infix.to_owned()
|
||||
};
|
||||
let lspan = self.span.with_hi(end_kind.span.lo());
|
||||
let rspan = self.span.with_lo(end_kind.span.hi());
|
||||
rewrite_pair(
|
||||
&RangeOperand {
|
||||
operand: lhs,
|
||||
span: lspan,
|
||||
},
|
||||
&RangeOperand {
|
||||
operand: rhs,
|
||||
span: rspan,
|
||||
},
|
||||
PairParts::infix(&infix),
|
||||
context,
|
||||
shape,
|
||||
SeparatorPlace::Front,
|
||||
)
|
||||
rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span)
|
||||
}
|
||||
PatKind::Ref(ref pat, mutability) => {
|
||||
let prefix = format!("&{}", format_mutability(mutability));
|
||||
|
|
@ -359,6 +326,50 @@ impl Rewrite for Pat {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn rewrite_range_pat<T: Rewrite>(
|
||||
context: &RewriteContext<'_>,
|
||||
shape: Shape,
|
||||
lhs: &Option<ptr::P<T>>,
|
||||
rhs: &Option<ptr::P<T>>,
|
||||
end_kind: &rustc_span::source_map::Spanned<RangeEnd>,
|
||||
span: Span,
|
||||
) -> RewriteResult {
|
||||
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 rhs_spacing = match rhs {
|
||||
None => "",
|
||||
Some(_) => " ",
|
||||
};
|
||||
format!("{lhs_spacing}{infix}{rhs_spacing}")
|
||||
} else {
|
||||
infix.to_owned()
|
||||
};
|
||||
let lspan = span.with_hi(end_kind.span.lo());
|
||||
let rspan = span.with_lo(end_kind.span.hi());
|
||||
rewrite_pair(
|
||||
&RangeOperand {
|
||||
operand: lhs,
|
||||
span: lspan,
|
||||
},
|
||||
&RangeOperand {
|
||||
operand: rhs,
|
||||
span: rspan,
|
||||
},
|
||||
PairParts::infix(&infix),
|
||||
context,
|
||||
shape,
|
||||
SeparatorPlace::Front,
|
||||
)
|
||||
}
|
||||
|
||||
fn rewrite_struct_pat(
|
||||
qself: &Option<ptr::P<ast::QSelf>>,
|
||||
path: &ast::Path,
|
||||
|
|
|
|||
|
|
@ -211,7 +211,7 @@ impl Spanned for ast::PreciseCapturingArg {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Spanned for RangeOperand<'a> {
|
||||
impl<'a, T> Spanned for RangeOperand<'a, T> {
|
||||
fn span(&self) -> Span {
|
||||
self.span
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ use crate::lists::{
|
|||
use crate::macros::{MacroPosition, rewrite_macro};
|
||||
use crate::overflow;
|
||||
use crate::pairs::{PairParts, rewrite_pair};
|
||||
use crate::patterns::rewrite_range_pat;
|
||||
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
|
||||
use crate::shape::Shape;
|
||||
use crate::source_map::SpanUtils;
|
||||
|
|
@ -1045,6 +1046,21 @@ impl Rewrite for ast::Ty {
|
|||
}
|
||||
}
|
||||
|
||||
impl Rewrite for ast::TyPat {
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
self.rewrite_result(context, shape).ok()
|
||||
}
|
||||
|
||||
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||
match self.kind {
|
||||
ast::TyPatKind::Range(ref lhs, ref rhs, ref end_kind) => {
|
||||
rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span)
|
||||
}
|
||||
ast::TyPatKind::Err(_) => Err(RewriteError::Unknown),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_bare_fn(
|
||||
bare_fn: &ast::BareFnTy,
|
||||
span: Span,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue