about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorOli Scherer <github333195615777966@oli-obk.de>2025-02-06 13:48:12 +0000
committerOli Scherer <github333195615777966@oli-obk.de>2025-02-11 08:51:05 +0000
commit6d7ce4e893003cc652428ec02eb752bba63645e2 (patch)
treecaf8b8ce21c274ebb92c49fa29b92c7ccd0a6f49 /src
parentc182ce9cbc8c29ebc1b4559d027df545e6cdd287 (diff)
downloadrust-6d7ce4e893003cc652428ec02eb752bba63645e2.tar.gz
rust-6d7ce4e893003cc652428ec02eb752bba63645e2.zip
Add a TyPat in the AST to reuse the generic arg lowering logic
Diffstat (limited to 'src')
-rw-r--r--src/tools/rustfmt/src/patterns.rs87
-rw-r--r--src/tools/rustfmt/src/spanned.rs2
-rw-r--r--src/tools/rustfmt/src/types.rs16
3 files changed, 66 insertions, 39 deletions
diff --git a/src/tools/rustfmt/src/patterns.rs b/src/tools/rustfmt/src/patterns.rs
index 1d88726d945..bafed41e39f 100644
--- a/src/tools/rustfmt/src/patterns.rs
+++ b/src/tools/rustfmt/src/patterns.rs
@@ -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,
diff --git a/src/tools/rustfmt/src/spanned.rs b/src/tools/rustfmt/src/spanned.rs
index 6b3e40b9115..e93eb53cd87 100644
--- a/src/tools/rustfmt/src/spanned.rs
+++ b/src/tools/rustfmt/src/spanned.rs
@@ -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
     }
diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs
index f8b713117f4..0009490e86f 100644
--- a/src/tools/rustfmt/src/types.rs
+++ b/src/tools/rustfmt/src/types.rs
@@ -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,