X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fpatterns.rs;h=9b74b35f31413cc20c8b0cdd3863fad51b1ae5b0;hb=f5ce84e4f25304dfbf9efeb26a85ef3c9ad594ad;hp=0c57f111761bf006975388bab1680ef03e9589db;hpb=9b0ed57af690bb46eb87de334c6cbef5bd27c174;p=rust.git diff --git a/src/patterns.rs b/src/patterns.rs index 0c57f111761..9b74b35f314 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -1,9 +1,10 @@ +use rustc_ast::ast::{self, BindingMode, Pat, PatField, PatKind, RangeEnd, RangeSyntax}; +use rustc_ast::ptr; use rustc_span::{BytePos, Span}; -use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax}; -use syntax::ptr; 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, @@ -17,16 +18,16 @@ 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>); + +impl<'a> Rewrite for RangeOperand<'a> { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + 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 { match self.kind { @@ -179,29 +191,34 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option 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,28 +227,40 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option 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 = 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) } - PatKind::MacCall(ref mac) => rewrite_macro(mac, None, context, shape, MacroPosition::Pat), PatKind::Paren(ref pat) => pat .rewrite(context, shape.offset_left(1)?.sub_width(1)?) .map(|inner_pat| format!("({})", inner_pat)), @@ -240,8 +269,9 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option } fn rewrite_struct_pat( + qself: &Option, path: &ast::Path, - fields: &[ast::FieldPat], + fields: &[ast::PatField], ellipsis: bool, span: Span, context: &RewriteContext<'_>, @@ -249,7 +279,7 @@ fn rewrite_struct_pat( ) -> Option { // 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)); @@ -288,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 &[] @@ -316,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 { let hi_pos = if let Some(last) = self.attrs.last() { last.span.hi() @@ -393,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, } } @@ -430,11 +457,11 @@ fn rewrite_tuple_pat( context: &RewriteContext<'_>, shape: Shape, ) -> Option { - 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 { @@ -442,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)), @@ -456,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, @@ -492,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() {