]> git.lizzy.rs Git - rust.git/blobdiff - src/patterns.rs
add eq constraints on associated constants
[rust.git] / src / patterns.rs
index 782926202327d238d3acb2bf5b62809d55ca6848..9b74b35f31413cc20c8b0cdd3863fad51b1ae5b0 100644 (file)
@@ -1,9 +1,10 @@
-use rustc_ast::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax};
+use rustc_ast::ast::{self, BindingMode, Pat, PatField, PatKind, RangeEnd, RangeSyntax};
 use rustc_ast::ptr;
 use rustc_span::{BytePos, Span};
 
 use crate::comment::{combine_strs_with_missing_comments, FindUncommented};
 use crate::config::lists::*;
+use crate::config::Version;
 use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
 use crate::lists::{
     definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
 use crate::source_map::SpanUtils;
 use crate::spanned::Spanned;
 use crate::types::{rewrite_path, PathContext};
-use crate::utils::{format_mutability, mk_sp, rewrite_ident};
+use crate::utils::{format_mutability, mk_sp, mk_sp_lo_plus_one, rewrite_ident};
 
 /// Returns `true` if the given pattern is "short".
 /// A short pattern is defined by the following grammar:
 ///
-/// [small, ntp]:
+/// `[small, ntp]`:
 ///     - single token
 ///     - `&[single-line, ntp]`
 ///
-/// [small]:
+/// `[small]`:
 ///     - `[small, ntp]`
 ///     - unary tuple constructor `([small, ntp])`
 ///     - `&[small]`
@@ -45,7 +46,7 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
         | ast::PatKind::Path(..)
         | ast::PatKind::Range(..) => false,
         ast::PatKind::Tuple(ref subpats) => subpats.len() <= 1,
-        ast::PatKind::TupleStruct(ref path, ref subpats) => {
+        ast::PatKind::TupleStruct(_, ref path, ref subpats) => {
             path.segments.len() <= 1 && subpats.len() <= 1
         }
         ast::PatKind::Box(ref p) | ast::PatKind::Ref(ref p, _) | ast::PatKind::Paren(ref p) => {
@@ -55,6 +56,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 {
@@ -179,29 +191,34 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
                     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)
@@ -210,26 +227,36 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
             PatKind::Path(ref q_self, ref path) => {
                 rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)
             }
-            PatKind::TupleStruct(ref path, ref pat_vec) => {
-                let path_str = rewrite_path(context, PathContext::Expr, None, path, shape)?;
+            PatKind::TupleStruct(ref q_self, ref path, ref pat_vec) => {
+                let path_str =
+                    rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)?;
                 rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape)
             }
             PatKind::Lit(ref expr) => expr.rewrite(context, shape),
-            PatKind::Slice(ref slice_pat) => {
+            PatKind::Slice(ref slice_pat) if context.config.version() == Version::One => {
                 let rw: Vec<String> = slice_pat
                     .iter()
                     .map(|p| {
                         if let Some(rw) = p.rewrite(context, shape) {
                             rw
                         } else {
-                            format!("{}", context.snippet(p.span))
+                            context.snippet(p.span).to_string()
                         }
                     })
                     .collect();
                 Some(format!("[{}]", rw.join(", ")))
             }
-            PatKind::Struct(ref path, ref fields, ellipsis) => {
-                rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape)
+            PatKind::Slice(ref slice_pat) => overflow::rewrite_with_square_brackets(
+                context,
+                "",
+                slice_pat.iter(),
+                shape,
+                self.span,
+                None,
+                None,
+            ),
+            PatKind::Struct(ref qself, ref path, ref fields, ellipsis) => {
+                rewrite_struct_pat(qself, path, fields, ellipsis, self.span, context, shape)
             }
             PatKind::MacCall(ref mac) => {
                 rewrite_macro(mac, None, context, shape, MacroPosition::Pat)
@@ -242,8 +269,9 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
 }
 
 fn rewrite_struct_pat(
+    qself: &Option<ast::QSelf>,
     path: &ast::Path,
-    fields: &[ast::FieldPat],
+    fields: &[ast::PatField],
     ellipsis: bool,
     span: Span,
     context: &RewriteContext<'_>,
@@ -251,7 +279,7 @@ fn rewrite_struct_pat(
 ) -> Option<String> {
     // 2 =  ` {`
     let path_shape = shape.sub_width(2)?;
-    let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
+    let path_str = rewrite_path(context, PathContext::Expr, qself.as_ref(), path, path_shape)?;
 
     if fields.is_empty() && !ellipsis {
         return Some(format!("{} {{}}", path_str));
@@ -290,27 +318,27 @@ fn rewrite_struct_pat(
     let mut fields_str = write_list(&item_vec, &fmt)?;
     let one_line_width = h_shape.map_or(0, |shape| shape.width);
 
+    let has_trailing_comma = fmt.needs_trailing_separator();
+
     if ellipsis {
         if fields_str.contains('\n') || fields_str.len() > one_line_width {
             // Add a missing trailing comma.
-            if context.config.trailing_comma() == SeparatorTactic::Never {
-                fields_str.push_str(",");
+            if !has_trailing_comma {
+                fields_str.push(',');
             }
-            fields_str.push_str("\n");
+            fields_str.push('\n');
             fields_str.push_str(&nested_shape.indent.to_string(context.config));
-            fields_str.push_str("..");
         } else {
             if !fields_str.is_empty() {
                 // there are preceding struct fields being matched on
-                if tactic == DefinitiveListTactic::Vertical {
-                    // if the tactic is Vertical, write_list already added a trailing ,
-                    fields_str.push_str(" ");
+                if has_trailing_comma {
+                    fields_str.push(' ');
                 } else {
                     fields_str.push_str(", ");
                 }
             }
-            fields_str.push_str("..");
         }
+        fields_str.push_str("..");
     }
 
     // ast::Pat doesn't have attrs so use &[]
@@ -318,7 +346,7 @@ fn rewrite_struct_pat(
     Some(format!("{} {{{}}}", path_str, fields_str))
 }
 
-impl Rewrite for FieldPat {
+impl Rewrite for PatField {
     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
         let hi_pos = if let Some(last) = self.attrs.last() {
             last.span.hi()
@@ -395,10 +423,7 @@ fn span(&self) -> Span {
 impl<'a> TuplePatField<'a> {
     fn is_dotdot(&self) -> bool {
         match self {
-            TuplePatField::Pat(pat) => match pat.kind {
-                ast::PatKind::Rest => true,
-                _ => false,
-            },
+            TuplePatField::Pat(pat) => matches!(pat.kind, ast::PatKind::Rest),
             TuplePatField::Dotdot(_) => true,
         }
     }
@@ -432,11 +457,11 @@ fn rewrite_tuple_pat(
     context: &RewriteContext<'_>,
     shape: Shape,
 ) -> Option<String> {
-    let mut pat_vec: Vec<_> = pats.iter().map(|x| TuplePatField::Pat(x)).collect();
-
-    if pat_vec.is_empty() {
+    if pats.is_empty() {
         return Some(format!("{}()", path_str.unwrap_or_default()));
     }
+    let mut pat_vec: Vec<_> = pats.iter().map(TuplePatField::Pat).collect();
+
     let wildcard_suffix_len = count_wildcard_suffix_len(context, &pat_vec, span, shape);
     let (pat_vec, span) = if context.config.condense_wildcard_suffixes() && wildcard_suffix_len >= 2
     {
@@ -444,7 +469,7 @@ fn rewrite_tuple_pat(
         let sp = pat_vec[new_item_count - 1].span();
         let snippet = context.snippet(sp);
         let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
-        pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
+        pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp_lo_plus_one(lo));
         (
             &pat_vec[..new_item_count],
             mk_sp(span.lo(), lo + BytePos(1)),
@@ -458,7 +483,7 @@ fn rewrite_tuple_pat(
     let path_str = path_str.unwrap_or_default();
 
     overflow::rewrite_with_parens(
-        &context,
+        context,
         &path_str,
         pat_vec.iter(),
         shape,
@@ -494,10 +519,11 @@ fn count_wildcard_suffix_len(
     )
     .collect();
 
-    for item in items.iter().rev().take_while(|i| match i.item {
-        Some(ref internal_string) if internal_string == "_" => true,
-        _ => false,
-    }) {
+    for item in items
+        .iter()
+        .rev()
+        .take_while(|i| matches!(i.item, Some(ref internal_string) if internal_string == "_"))
+    {
         suffix_len += 1;
 
         if item.has_comment() {