X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fexpr.rs;h=71446ce1ab0b1ec1d6c19e98045aea85bbdccb0c;hb=b7c38c9d50fcf48e40cf245b48ab9f36054f40d3;hp=84f01f2a1a9f2ca2bfc7cb49ffa258781f0d73f6;hpb=9f53665f91be16c9aa7afd83f7c79357fec9152b;p=rust.git diff --git a/src/expr.rs b/src/expr.rs index 84f01f2a1a9..71446ce1ab0 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2,9 +2,9 @@ use std::cmp::min; use itertools::Itertools; -use rustc_span::{source_map::SourceMap, BytePos, Span}; -use syntax::token::{DelimToken, LitKind}; -use syntax::{ast, ptr}; +use rustc_ast::token::{DelimToken, LitKind}; +use rustc_ast::{ast, ptr}; +use rustc_span::{BytePos, Span}; use crate::chains::rewrite_chain; use crate::closures; @@ -106,11 +106,11 @@ pub(crate) fn format_expr( }) } ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape), - ast::ExprKind::Struct(ref path, ref fields, ref base) => rewrite_struct_lit( + ast::ExprKind::Struct(ref path, ref fields, ref struct_rest) => rewrite_struct_lit( context, path, fields, - base.as_ref().map(|e| &**e), + struct_rest, &expr.attrs, expr.span, shape, @@ -124,6 +124,9 @@ pub(crate) fn format_expr( | ast::ExprKind::Loop(..) | ast::ExprKind::While(..) => to_control_flow(expr, expr_type) .and_then(|control_flow| control_flow.rewrite(context, shape)), + ast::ExprKind::ConstBlock(ref anon_const) => { + Some(format!("const {}", anon_const.rewrite(context, shape)?)) + } ast::ExprKind::Block(ref block, opt_label) => { match expr_type { ExprType::Statement => { @@ -199,7 +202,7 @@ pub(crate) fn format_expr( ast::ExprKind::Try(..) | ast::ExprKind::Field(..) | ast::ExprKind::MethodCall(..) => { rewrite_chain(expr, context, shape) } - ast::ExprKind::Mac(ref mac) => { + ast::ExprKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| { wrap_str( context.snippet(expr.span).to_owned(), @@ -322,7 +325,11 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool { } // We do not format these expressions yet, but they should still // satisfy our width restrictions. - ast::ExprKind::InlineAsm(..) => Some(context.snippet(expr.span).to_owned()), + // Style Guide RFC for InlineAsm variant pending + // https://github.com/rust-dev-tools/fmt-rfcs/issues/152 + ast::ExprKind::LlvmInlineAsm(..) | ast::ExprKind::InlineAsm(..) => { + Some(context.snippet(expr.span).to_owned()) + } ast::ExprKind::TryBlock(ref block) => { if let rw @ Some(_) = rewrite_single_line_block(context, "try ", block, Some(&expr.attrs), None, shape) @@ -377,6 +384,7 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool { } } ast::ExprKind::Await(_) => rewrite_chain(expr, context, shape), + ast::ExprKind::Underscore => Some("_".to_owned()), ast::ExprKind::Err => None, }; @@ -430,7 +438,7 @@ fn rewrite_empty_block( return None; } - if !block_contains_comment(block, context.source_map) && shape.width >= 2 { + if !block_contains_comment(context, block) && shape.width >= 2 { return Some(format!("{}{}{{}}", prefix, label_str)); } @@ -487,7 +495,7 @@ fn rewrite_single_line_block( label: Option, shape: Shape, ) -> Option { - if is_simple_block(block, attrs, context.source_map) { + if is_simple_block(context, block, attrs) { let expr_shape = shape.offset_left(last_line_width(prefix))?; let expr_str = block.stmts[0].rewrite(context, expr_shape)?; let label_str = rewrite_label(label); @@ -750,8 +758,8 @@ fn rewrite_single_line( let fixed_cost = self.keyword.len() + " { } else { }".len(); if let ast::ExprKind::Block(ref else_node, _) = else_block.kind { - if !is_simple_block(self.block, None, context.source_map) - || !is_simple_block(else_node, None, context.source_map) + if !is_simple_block(context, self.block, None) + || !is_simple_block(context, else_node, None) || pat_expr_str.contains('\n') { return None; @@ -1134,9 +1142,8 @@ fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Op } } -pub(crate) fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) -> bool { - let snippet = source_map.span_to_snippet(block.span).unwrap(); - contains_comment(&snippet) +pub(crate) fn block_contains_comment(context: &RewriteContext<'_>, block: &ast::Block) -> bool { + contains_comment(context.snippet(block.span)) } // Checks that a block contains no statements, an expression and no comments or @@ -1144,37 +1151,37 @@ pub(crate) fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) // FIXME: incorrectly returns false when comment is contained completely within // the expression. pub(crate) fn is_simple_block( + context: &RewriteContext<'_>, block: &ast::Block, attrs: Option<&[ast::Attribute]>, - source_map: &SourceMap, ) -> bool { block.stmts.len() == 1 && stmt_is_expr(&block.stmts[0]) - && !block_contains_comment(block, source_map) + && !block_contains_comment(context, block) && attrs.map_or(true, |a| a.is_empty()) } /// Checks whether a block contains at most one statement or expression, and no /// comments or attributes. pub(crate) fn is_simple_block_stmt( + context: &RewriteContext<'_>, block: &ast::Block, attrs: Option<&[ast::Attribute]>, - source_map: &SourceMap, ) -> bool { block.stmts.len() <= 1 - && !block_contains_comment(block, source_map) + && !block_contains_comment(context, block) && attrs.map_or(true, |a| a.is_empty()) } /// Checks whether a block contains no statements, expressions, comments, or /// inner attributes. pub(crate) fn is_empty_block( + context: &RewriteContext<'_>, block: &ast::Block, attrs: Option<&[ast::Attribute]>, - source_map: &SourceMap, ) -> bool { block.stmts.is_empty() - && !block_contains_comment(block, source_map) + && !block_contains_comment(context, block) && attrs.map_or(true, |a| inner_attributes(a).is_empty()) } @@ -1313,9 +1320,9 @@ pub(crate) fn can_be_overflowed_expr( context.config.overflow_delimited_expr() || (context.use_block_indent() && args_len == 1) } - ast::ExprKind::Mac(ref mac) => { + ast::ExprKind::MacCall(ref mac) => { match ( - syntax::ast::MacDelimiter::from_token(mac.args.delim()), + rustc_ast::ast::MacDelimiter::from_token(mac.args.delim()), context.config.overflow_delimited_expr(), ) { (Some(ast::MacDelimiter::Bracket), true) @@ -1341,7 +1348,7 @@ pub(crate) fn can_be_overflowed_expr( pub(crate) fn is_nested_call(expr: &ast::Expr) -> bool { match expr.kind { - ast::ExprKind::Call(..) | ast::ExprKind::Mac(..) => true, + ast::ExprKind::Call(..) | ast::ExprKind::MacCall(..) => true, ast::ExprKind::AddrOf(_, _, ref expr) | ast::ExprKind::Box(ref expr) | ast::ExprKind::Try(ref expr) @@ -1504,19 +1511,15 @@ fn rewrite_index( } } -fn struct_lit_can_be_aligned(fields: &[ast::Field], base: Option<&ast::Expr>) -> bool { - if base.is_some() { - return false; - } - - fields.iter().all(|field| !field.is_shorthand) +fn struct_lit_can_be_aligned(fields: &[ast::Field], has_base: bool) -> bool { + !has_base && fields.iter().all(|field| !field.is_shorthand) } fn rewrite_struct_lit<'a>( context: &RewriteContext<'_>, path: &ast::Path, fields: &'a [ast::Field], - base: Option<&'a ast::Expr>, + struct_rest: &ast::StructRest, attrs: &[ast::Attribute], span: Span, shape: Shape, @@ -1526,22 +1529,28 @@ fn rewrite_struct_lit<'a>( enum StructLitField<'a> { Regular(&'a ast::Field), Base(&'a ast::Expr), + Rest(&'a Span), } // 2 = " {".len() let path_shape = shape.sub_width(2)?; let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?; - if fields.is_empty() && base.is_none() { - return Some(format!("{} {{}}", path_str)); - } + let has_base = match struct_rest { + ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)), + ast::StructRest::Rest(_) if fields.is_empty() => { + return Some(format!("{} {{ .. }}", path_str)); + } + ast::StructRest::Base(_) => true, + _ => false, + }; // Foo { a: Foo } - indent is +3, width is -5. let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)?; let one_line_width = h_shape.map_or(0, |shape| shape.width); let body_lo = context.snippet_provider.span_after(span, "{"); - let fields_str = if struct_lit_can_be_aligned(fields, base) + let fields_str = if struct_lit_can_be_aligned(fields, has_base) && context.config.struct_field_align_threshold() > 0 { rewrite_with_alignment( @@ -1552,10 +1561,14 @@ enum StructLitField<'a> { one_line_width, )? } else { - let field_iter = fields - .iter() - .map(StructLitField::Regular) - .chain(base.into_iter().map(StructLitField::Base)); + let field_iter = fields.iter().map(StructLitField::Regular).chain( + match struct_rest { + ast::StructRest::Base(expr) => Some(StructLitField::Base(&**expr)), + ast::StructRest::Rest(span) => Some(StructLitField::Rest(span)), + ast::StructRest::None => None, + } + .into_iter(), + ); let span_lo = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => field.span().lo(), @@ -1565,10 +1578,12 @@ enum StructLitField<'a> { let pos = snippet.find_uncommented("..").unwrap(); last_field_hi + BytePos(pos as u32) } + StructLitField::Rest(span) => span.lo(), }; let span_hi = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => field.span().hi(), StructLitField::Base(expr) => expr.span.hi(), + StructLitField::Rest(span) => span.hi(), }; let rewrite = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => { @@ -1580,6 +1595,7 @@ enum StructLitField<'a> { expr.rewrite(context, v_shape.offset_left(2)?) .map(|s| format!("..{}", s)) } + StructLitField::Rest(_) => Some("..".to_owned()), }; let items = itemize_list( @@ -1606,7 +1622,7 @@ enum StructLitField<'a> { nested_shape, tactic, context, - force_no_trailing_comma || base.is_some() || !context.use_block_indent(), + force_no_trailing_comma || has_base || !context.use_block_indent(), ); write_list(&item_vec, &fmt)? @@ -1989,14 +2005,16 @@ pub(crate) fn prefer_next_line( fn rewrite_expr_addrof( context: &RewriteContext<'_>, - _borrow_kind: ast::BorrowKind, + borrow_kind: ast::BorrowKind, mutability: ast::Mutability, expr: &ast::Expr, shape: Shape, ) -> Option { - let operator_str = match mutability { - ast::Mutability::Not => "&", - ast::Mutability::Mut => "&mut ", + let operator_str = match (mutability, borrow_kind) { + (ast::Mutability::Not, ast::BorrowKind::Ref) => "&", + (ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ", + (ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ", + (ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ", }; rewrite_unary_prefix(context, operator_str, expr, shape) }