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;
| 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 => {
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(),
}
// 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)
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));
}
label: Option<ast::Label>,
shape: Shape,
) -> Option<String> {
- 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);
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;
}
}
-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
// 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
+ block.stmts.len() == 1
&& stmt_is_expr(&block.stmts[0])
- && !block_contains_comment(block, source_map)
- && attrs.map_or(true, |a| a.is_empty()))
+ && !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())
}
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)
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)
fn rewrite_expr_addrof(
context: &RewriteContext<'_>,
- _borrow_kind: ast::BorrowKind,
+ borrow_kind: ast::BorrowKind,
mutability: ast::Mutability,
expr: &ast::Expr,
shape: Shape,
) -> Option<String> {
- 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)
}