deps: Update syntex_syntax to 0.29.1

This commit is contained in:
Kamal Marhubi 2016-03-01 17:27:19 -05:00
parent 684596fcdd
commit 589dabda2f
12 changed files with 230 additions and 213 deletions

4
Cargo.lock generated
View File

@ -9,7 +9,7 @@ dependencies = [
"regex 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -108,7 +108,7 @@ dependencies = [
[[package]] [[package]]
name = "syntex_syntax" name = "syntex_syntax"
version = "0.23.0" version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -21,7 +21,7 @@ regex = "0.1.41"
term = "0.2.11" term = "0.2.11"
strings = "0.0.1" strings = "0.0.1"
diff = "0.1.8" diff = "0.1.8"
syntex_syntax = "0.23.0" syntex_syntax = "0.29.1"
log = "0.3.2" log = "0.3.2"
env_logger = "0.3.1" env_logger = "0.3.1"
getopts = "0.2" getopts = "0.2"

View File

@ -94,7 +94,7 @@ pub fn rewrite_chain(mut expr: &ast::Expr,
let fits_single_line = !veto_single_line && let fits_single_line = !veto_single_line &&
match subexpr_list[0].node { match subexpr_list[0].node {
ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) ast::ExprKind::MethodCall(ref method_name, ref types, ref expressions)
if context.config.chains_overflow_last => { if context.config.chains_overflow_last => {
let len = rewrites.len(); let len = rewrites.len();
let (init, last) = rewrites.split_at_mut(len - 1); let (init, last) = rewrites.split_at_mut(len - 1);
@ -150,28 +150,28 @@ pub fn rewrite_chain(mut expr: &ast::Expr,
// parens, braces and brackets in its idiomatic formatting. // parens, braces and brackets in its idiomatic formatting.
fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool { fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool {
match expr.node { match expr.node {
ast::Expr_::ExprStruct(..) | ast::ExprKind::Struct(..) |
ast::Expr_::ExprWhile(..) | ast::ExprKind::While(..) |
ast::Expr_::ExprWhileLet(..) | ast::ExprKind::WhileLet(..) |
ast::Expr_::ExprIf(..) | ast::ExprKind::If(..) |
ast::Expr_::ExprIfLet(..) | ast::ExprKind::IfLet(..) |
ast::Expr_::ExprBlock(..) | ast::ExprKind::Block(..) |
ast::Expr_::ExprLoop(..) | ast::ExprKind::Loop(..) |
ast::Expr_::ExprForLoop(..) | ast::ExprKind::ForLoop(..) |
ast::Expr_::ExprMatch(..) => repr.contains('\n'), ast::ExprKind::Match(..) => repr.contains('\n'),
ast::Expr_::ExprParen(ref expr) | ast::ExprKind::Paren(ref expr) |
ast::Expr_::ExprBinary(_, _, ref expr) | ast::ExprKind::Binary(_, _, ref expr) |
ast::Expr_::ExprIndex(_, ref expr) | ast::ExprKind::Index(_, ref expr) |
ast::Expr_::ExprUnary(_, ref expr) => is_block_expr(expr, repr), ast::ExprKind::Unary(_, ref expr) => is_block_expr(expr, repr),
_ => false, _ => false,
} }
} }
fn pop_expr_chain(expr: &ast::Expr) -> Option<&ast::Expr> { fn pop_expr_chain(expr: &ast::Expr) -> Option<&ast::Expr> {
match expr.node { match expr.node {
ast::Expr_::ExprMethodCall(_, _, ref expressions) => Some(&expressions[0]), ast::ExprKind::MethodCall(_, _, ref expressions) => Some(&expressions[0]),
ast::Expr_::ExprTupField(ref subexpr, _) | ast::ExprKind::TupField(ref subexpr, _) |
ast::Expr_::ExprField(ref subexpr, _) => Some(subexpr), ast::ExprKind::Field(ref subexpr, _) => Some(subexpr),
_ => None, _ => None,
} }
} }
@ -183,7 +183,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
offset: Indent) offset: Indent)
-> Option<String> { -> Option<String> {
match expr.node { match expr.node {
ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) => { ast::ExprKind::MethodCall(ref method_name, ref types, ref expressions) => {
let inner = &RewriteContext { block_indent: offset, ..*context }; let inner = &RewriteContext { block_indent: offset, ..*context };
rewrite_method_call(method_name.node, rewrite_method_call(method_name.node,
types, types,
@ -193,7 +193,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
width, width,
offset) offset)
} }
ast::Expr_::ExprField(_, ref field) => { ast::ExprKind::Field(_, ref field) => {
let s = format!(".{}", field.node); let s = format!(".{}", field.node);
if s.len() <= width { if s.len() <= width {
Some(s) Some(s)
@ -201,7 +201,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
None None
} }
} }
ast::Expr_::ExprTupField(_, ref field) => { ast::ExprKind::TupField(_, ref field) => {
let s = format!(".{}", field.node); let s = format!(".{}", field.node);
if s.len() <= width { if s.len() <= width {
Some(s) Some(s)
@ -216,7 +216,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
// Determines we can continue formatting a given expression on the same line. // Determines we can continue formatting a given expression on the same line.
fn is_continuable(expr: &ast::Expr) -> bool { fn is_continuable(expr: &ast::Expr) -> bool {
match expr.node { match expr.node {
ast::Expr_::ExprPath(..) => true, ast::ExprKind::Path(..) => true,
_ => false, _ => false,
} }
} }

View File

@ -37,16 +37,16 @@ use syntax::visit::Visitor;
impl Rewrite for ast::Expr { impl Rewrite for ast::Expr {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
let result = match self.node { let result = match self.node {
ast::Expr_::ExprVec(ref expr_vec) => { ast::ExprKind::Vec(ref expr_vec) => {
rewrite_array(expr_vec.iter().map(|e| &**e), rewrite_array(expr_vec.iter().map(|e| &**e),
mk_sp(span_after(self.span, "[", context.codemap), self.span.hi), mk_sp(span_after(self.span, "[", context.codemap), self.span.hi),
context, context,
width, width,
offset) offset)
} }
ast::Expr_::ExprLit(ref l) => { ast::ExprKind::Lit(ref l) => {
match l.node { match l.node {
ast::Lit_::LitStr(_, ast::StrStyle::CookedStr) => { ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
rewrite_string_lit(context, l.span, width, offset) rewrite_string_lit(context, l.span, width, offset)
} }
_ => { _ => {
@ -57,18 +57,18 @@ impl Rewrite for ast::Expr {
} }
} }
} }
ast::Expr_::ExprCall(ref callee, ref args) => { ast::ExprKind::Call(ref callee, ref args) => {
let inner_span = mk_sp(callee.span.hi, self.span.hi); let inner_span = mk_sp(callee.span.hi, self.span.hi);
rewrite_call(context, &**callee, args, inner_span, width, offset) rewrite_call(context, &**callee, args, inner_span, width, offset)
} }
ast::Expr_::ExprParen(ref subexpr) => rewrite_paren(context, subexpr, width, offset), ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, width, offset),
ast::Expr_::ExprBinary(ref op, ref lhs, ref rhs) => { ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
rewrite_binary_op(context, op, lhs, rhs, width, offset) rewrite_binary_op(context, op, lhs, rhs, width, offset)
} }
ast::Expr_::ExprUnary(ref op, ref subexpr) => { ast::ExprKind::Unary(ref op, ref subexpr) => {
rewrite_unary_op(context, op, subexpr, width, offset) rewrite_unary_op(context, op, subexpr, width, offset)
} }
ast::Expr_::ExprStruct(ref path, ref fields, ref base) => { ast::ExprKind::Struct(ref path, ref fields, ref base) => {
rewrite_struct_lit(context, rewrite_struct_lit(context,
path, path,
fields, fields,
@ -77,27 +77,27 @@ impl Rewrite for ast::Expr {
width, width,
offset) offset)
} }
ast::Expr_::ExprTup(ref items) => { ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, rewrite_tuple(context,
items.iter().map(|x| &**x), items.iter().map(|x| &**x),
self.span, self.span,
width, width,
offset) offset)
} }
ast::Expr_::ExprWhile(ref cond, ref block, label) => { ast::ExprKind::While(ref cond, ref block, label) => {
Loop::new_while(None, cond, block, label).rewrite(context, width, offset) Loop::new_while(None, cond, block, label).rewrite(context, width, offset)
} }
ast::Expr_::ExprWhileLet(ref pat, ref cond, ref block, label) => { ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => {
Loop::new_while(Some(pat), cond, block, label).rewrite(context, width, offset) Loop::new_while(Some(pat), cond, block, label).rewrite(context, width, offset)
} }
ast::Expr_::ExprForLoop(ref pat, ref cond, ref block, label) => { ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => {
Loop::new_for(pat, cond, block, label).rewrite(context, width, offset) Loop::new_for(pat, cond, block, label).rewrite(context, width, offset)
} }
ast::Expr_::ExprLoop(ref block, label) => { ast::ExprKind::Loop(ref block, label) => {
Loop::new_loop(block, label).rewrite(context, width, offset) Loop::new_loop(block, label).rewrite(context, width, offset)
} }
ast::Expr_::ExprBlock(ref block) => block.rewrite(context, width, offset), ast::ExprKind::Block(ref block) => block.rewrite(context, width, offset),
ast::Expr_::ExprIf(ref cond, ref if_block, ref else_block) => { ast::ExprKind::If(ref cond, ref if_block, ref else_block) => {
rewrite_if_else(context, rewrite_if_else(context,
cond, cond,
if_block, if_block,
@ -108,7 +108,7 @@ impl Rewrite for ast::Expr {
offset, offset,
true) true)
} }
ast::Expr_::ExprIfLet(ref pat, ref cond, ref if_block, ref else_block) => { ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => {
rewrite_if_else(context, rewrite_if_else(context,
cond, cond,
if_block, if_block,
@ -119,39 +119,39 @@ impl Rewrite for ast::Expr {
offset, offset,
true) true)
} }
ast::Expr_::ExprMatch(ref cond, ref arms) => { ast::ExprKind::Match(ref cond, ref arms) => {
rewrite_match(context, cond, arms, width, offset, self.span) rewrite_match(context, cond, arms, width, offset, self.span)
} }
ast::Expr_::ExprPath(ref qself, ref path) => { ast::ExprKind::Path(ref qself, ref path) => {
rewrite_path(context, true, qself.as_ref(), path, width, offset) rewrite_path(context, true, qself.as_ref(), path, width, offset)
} }
ast::Expr_::ExprAssign(ref lhs, ref rhs) => { ast::ExprKind::Assign(ref lhs, ref rhs) => {
rewrite_assignment(context, lhs, rhs, None, width, offset) rewrite_assignment(context, lhs, rhs, None, width, offset)
} }
ast::Expr_::ExprAssignOp(ref op, ref lhs, ref rhs) => { ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
rewrite_assignment(context, lhs, rhs, Some(op), width, offset) rewrite_assignment(context, lhs, rhs, Some(op), width, offset)
} }
ast::Expr_::ExprAgain(ref opt_ident) => { ast::ExprKind::Again(ref opt_ident) => {
let id_str = match *opt_ident { let id_str = match *opt_ident {
Some(ident) => format!(" {}", ident.node), Some(ident) => format!(" {}", ident.node),
None => String::new(), None => String::new(),
}; };
Some(format!("continue{}", id_str)) Some(format!("continue{}", id_str))
} }
ast::Expr_::ExprBreak(ref opt_ident) => { ast::ExprKind::Break(ref opt_ident) => {
let id_str = match *opt_ident { let id_str = match *opt_ident {
Some(ident) => format!(" {}", ident.node), Some(ident) => format!(" {}", ident.node),
None => String::new(), None => String::new(),
}; };
Some(format!("break{}", id_str)) Some(format!("break{}", id_str))
} }
ast::Expr_::ExprClosure(capture, ref fn_decl, ref body) => { ast::ExprKind::Closure(capture, ref fn_decl, ref body) => {
rewrite_closure(capture, fn_decl, body, self.span, context, width, offset) rewrite_closure(capture, fn_decl, body, self.span, context, width, offset)
} }
ast::Expr_::ExprField(..) | ast::ExprKind::Field(..) |
ast::Expr_::ExprTupField(..) | ast::ExprKind::TupField(..) |
ast::Expr_::ExprMethodCall(..) => rewrite_chain(self, context, width, offset), ast::ExprKind::MethodCall(..) => rewrite_chain(self, context, width, offset),
ast::Expr_::ExprMac(ref mac) => { ast::ExprKind::Mac(ref mac) => {
// Failure to rewrite a marco should not imply failure to // Failure to rewrite a marco should not imply failure to
// rewrite the expression. // rewrite the expression.
rewrite_macro(mac, context, width, offset).or_else(|| { rewrite_macro(mac, context, width, offset).or_else(|| {
@ -161,40 +161,43 @@ impl Rewrite for ast::Expr {
offset) offset)
}) })
} }
ast::Expr_::ExprRet(None) => { ast::ExprKind::Ret(None) => {
wrap_str("return".to_owned(), context.config.max_width, width, offset) wrap_str("return".to_owned(), context.config.max_width, width, offset)
} }
ast::Expr_::ExprRet(Some(ref expr)) => { ast::ExprKind::Ret(Some(ref expr)) => {
rewrite_unary_prefix(context, "return ", &**expr, width, offset) rewrite_unary_prefix(context, "return ", &**expr, width, offset)
} }
ast::Expr_::ExprBox(ref expr) => { ast::ExprKind::Box(ref expr) => {
rewrite_unary_prefix(context, "box ", &**expr, width, offset) rewrite_unary_prefix(context, "box ", &**expr, width, offset)
} }
ast::Expr_::ExprAddrOf(mutability, ref expr) => { ast::ExprKind::AddrOf(mutability, ref expr) => {
rewrite_expr_addrof(context, mutability, expr, width, offset) rewrite_expr_addrof(context, mutability, expr, width, offset)
} }
ast::Expr_::ExprCast(ref expr, ref ty) => { ast::ExprKind::Cast(ref expr, ref ty) => {
rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset) rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset)
} }
ast::Expr_::ExprIndex(ref expr, ref index) => { // TODO(#848): Handle type ascription; rust tracking issue
// https://github.com/rust-lang/rust/issues/23416
ast::ExprKind::Type(_, _) => unimplemented!(),
ast::ExprKind::Index(ref expr, ref index) => {
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset) rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
} }
ast::Expr_::ExprRepeat(ref expr, ref repeats) => { ast::ExprKind::Repeat(ref expr, ref repeats) => {
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset) rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
} }
ast::Expr_::ExprRange(Some(ref lhs), Some(ref rhs)) => { ast::ExprKind::Range(Some(ref lhs), Some(ref rhs)) => {
rewrite_pair(&**lhs, &**rhs, "", "..", "", context, width, offset) rewrite_pair(&**lhs, &**rhs, "", "..", "", context, width, offset)
} }
ast::Expr_::ExprRange(None, Some(ref rhs)) => { ast::ExprKind::Range(None, Some(ref rhs)) => {
rewrite_unary_prefix(context, "..", &**rhs, width, offset) rewrite_unary_prefix(context, "..", &**rhs, width, offset)
} }
ast::Expr_::ExprRange(Some(ref lhs), None) => { ast::ExprKind::Range(Some(ref lhs), None) => {
Some(format!("{}..", Some(format!("{}..",
try_opt!(lhs.rewrite(context, try_opt!(lhs.rewrite(context,
try_opt!(width.checked_sub(2)), try_opt!(width.checked_sub(2)),
offset)))) offset))))
} }
ast::Expr_::ExprRange(None, None) => { ast::ExprKind::Range(None, None) => {
if width >= 2 { if width >= 2 {
Some("..".into()) Some("..".into())
} else { } else {
@ -203,8 +206,8 @@ impl Rewrite for ast::Expr {
} }
// We do not format these expressions yet, but they should still // We do not format these expressions yet, but they should still
// satisfy our width restrictions. // satisfy our width restrictions.
ast::Expr_::ExprInPlace(..) | ast::ExprKind::InPlace(..) |
ast::Expr_::ExprInlineAsm(..) => { ast::ExprKind::InlineAsm(..) => {
wrap_str(context.snippet(self.span), wrap_str(context.snippet(self.span),
context.config.max_width, context.config.max_width,
width, width,
@ -302,7 +305,7 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
// This functions is pretty messy because of the wrapping and unwrapping of // This functions is pretty messy because of the wrapping and unwrapping of
// expressions into and from blocks. See rust issue #27872. // expressions into and from blocks. See rust issue #27872.
fn rewrite_closure(capture: ast::CaptureClause, fn rewrite_closure(capture: ast::CaptureBy,
fn_decl: &ast::FnDecl, fn_decl: &ast::FnDecl,
body: &ast::Block, body: &ast::Block,
span: Span, span: Span,
@ -310,7 +313,7 @@ fn rewrite_closure(capture: ast::CaptureClause,
width: usize, width: usize,
offset: Indent) offset: Indent)
-> Option<String> { -> Option<String> {
let mover = if capture == ast::CaptureClause::CaptureByValue { let mover = if capture == ast::CaptureBy::Value {
"move " "move "
} else { } else {
"" ""
@ -374,9 +377,8 @@ fn rewrite_closure(capture: ast::CaptureClause,
// All closure bodies are blocks in the eyes of the AST, but we may not // All closure bodies are blocks in the eyes of the AST, but we may not
// want to unwrap them when they only contain a single expression. // want to unwrap them when they only contain a single expression.
let inner_expr = match expr.node { let inner_expr = match expr.node {
ast::Expr_::ExprBlock(ref inner) if inner.stmts.is_empty() && inner.expr.is_some() && ast::ExprKind::Block(ref inner) if inner.stmts.is_empty() && inner.expr.is_some() &&
inner.rules == inner.rules == ast::BlockCheckMode::Default => {
ast::BlockCheckMode::DefaultBlock => {
inner.expr.as_ref().unwrap() inner.expr.as_ref().unwrap()
} }
_ => expr, _ => expr,
@ -398,7 +400,7 @@ fn rewrite_closure(capture: ast::CaptureClause,
let body_rewrite = body.expr let body_rewrite = body.expr
.as_ref() .as_ref()
.and_then(|body_expr| { .and_then(|body_expr| {
if let ast::Expr_::ExprBlock(ref inner) = body_expr.node { if let ast::ExprKind::Block(ref inner) = body_expr.node {
Some(inner.rewrite(&context, 2, Indent::empty())) Some(inner.rewrite(&context, 2, Indent::empty()))
} else { } else {
None None
@ -431,7 +433,7 @@ impl Rewrite for ast::Block {
visitor.block_indent = context.block_indent; visitor.block_indent = context.block_indent;
let prefix = match self.rules { let prefix = match self.rules {
ast::BlockCheckMode::UnsafeBlock(..) => { ast::BlockCheckMode::Unsafe(..) => {
let snippet = context.snippet(self.span); let snippet = context.snippet(self.span);
let open_pos = try_opt!(snippet.find_uncommented("{")); let open_pos = try_opt!(snippet.find_uncommented("{"));
visitor.last_pos = self.span.lo + BytePos(open_pos as u32); visitor.last_pos = self.span.lo + BytePos(open_pos as u32);
@ -467,7 +469,7 @@ impl Rewrite for ast::Block {
prefix prefix
} }
ast::BlockCheckMode::DefaultBlock => { ast::BlockCheckMode::Default => {
visitor.last_pos = self.span.lo; visitor.last_pos = self.span.lo;
String::new() String::new()
@ -483,14 +485,14 @@ impl Rewrite for ast::Block {
impl Rewrite for ast::Stmt { impl Rewrite for ast::Stmt {
fn rewrite(&self, context: &RewriteContext, _width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, _width: usize, offset: Indent) -> Option<String> {
let result = match self.node { let result = match self.node {
ast::Stmt_::StmtDecl(ref decl, _) => { ast::StmtKind::Decl(ref decl, _) => {
if let ast::Decl_::DeclLocal(ref local) = decl.node { if let ast::DeclKind::Local(ref local) = decl.node {
local.rewrite(context, context.config.max_width, offset) local.rewrite(context, context.config.max_width, offset)
} else { } else {
None None
} }
} }
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => { ast::StmtKind::Expr(ref ex, _) | ast::StmtKind::Semi(ref ex, _) => {
let suffix = if semicolon_for_stmt(self) { let suffix = if semicolon_for_stmt(self) {
";" ";"
} else { } else {
@ -502,7 +504,7 @@ impl Rewrite for ast::Stmt {
offset) offset)
.map(|s| s + suffix) .map(|s| s + suffix)
} }
ast::Stmt_::StmtMac(..) => None, ast::StmtKind::Mac(..) => None,
}; };
result.and_then(|res| recover_comment_removed(res, self.span, context, _width, offset)) result.and_then(|res| recover_comment_removed(res, self.span, context, _width, offset))
} }
@ -681,7 +683,7 @@ fn rewrite_if_else(context: &RewriteContext,
let rewrite = match else_block.node { let rewrite = match else_block.node {
// If the else expression is another if-else expression, prevent it // If the else expression is another if-else expression, prevent it
// from being formatted on a single line. // from being formatted on a single line.
ast::Expr_::ExprIfLet(ref pat, ref cond, ref if_block, ref next_else_block) => { ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref next_else_block) => {
rewrite_if_else(context, rewrite_if_else(context,
cond, cond,
if_block, if_block,
@ -692,7 +694,7 @@ fn rewrite_if_else(context: &RewriteContext,
offset, offset,
false) false)
} }
ast::Expr_::ExprIf(ref cond, ref if_block, ref next_else_block) => { ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
rewrite_if_else(context, rewrite_if_else(context,
cond, cond,
if_block, if_block,
@ -741,7 +743,7 @@ fn single_line_if_else(context: &RewriteContext,
let else_block = try_opt!(else_block_opt); let else_block = try_opt!(else_block_opt);
let fixed_cost = "if { } else { }".len(); let fixed_cost = "if { } else { }".len();
if let ast::ExprBlock(ref else_node) = else_block.node { if let ast::ExprKind::Block(ref else_node) = else_block.node {
if !is_simple_block(if_node, context.codemap) || if !is_simple_block(if_node, context.codemap) ||
!is_simple_block(else_node, context.codemap) || pat_expr_str.contains('\n') { !is_simple_block(else_node, context.codemap) || pat_expr_str.contains('\n') {
return None; return None;
@ -794,7 +796,7 @@ pub fn is_empty_block(block: &ast::Block, codemap: &CodeMap) -> bool {
} }
fn is_unsafe_block(block: &ast::Block) -> bool { fn is_unsafe_block(block: &ast::Block) -> bool {
if let ast::BlockCheckMode::UnsafeBlock(..) = block.rules { if let ast::BlockCheckMode::Unsafe(..) = block.rules {
true true
} else { } else {
false false
@ -925,15 +927,15 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str { fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str {
if !config.match_wildcard_trailing_comma { if !config.match_wildcard_trailing_comma {
if arm.pats.len() == 1 && arm.pats[0].node == ast::PatWild && arm.guard.is_none() { if arm.pats.len() == 1 && arm.pats[0].node == ast::PatKind::Wild && arm.guard.is_none() {
return ""; return "";
} }
} }
if config.match_block_trailing_comma { if config.match_block_trailing_comma {
"," ","
} else if let ast::ExprBlock(ref block) = body.node { } else if let ast::ExprKind::Block(ref block) = body.node {
if let ast::DefaultBlock = block.rules { if let ast::BlockCheckMode::Default = block.rules {
"" ""
} else { } else {
"," ","
@ -1015,12 +1017,9 @@ impl Rewrite for ast::Arm {
} }
let body = match **body { let body = match **body {
ast::Expr { node: ast::ExprBlock(ref block), .. } if !is_unsafe_block(block) && ast::Expr { node: ast::ExprKind::Block(ref block), .. }
is_simple_block(block, if !is_unsafe_block(block) && is_simple_block(block, context.codemap) &&
context.codemap) && context.config.wrap_match_arms => block.expr.as_ref().map(|e| &**e).unwrap(),
context.config.wrap_match_arms => {
block.expr.as_ref().map(|e| &**e).unwrap()
}
ref x => x, ref x => x,
}; };
@ -1032,7 +1031,7 @@ impl Rewrite for ast::Arm {
let budget = context.config.max_width - line_start - comma.len() - 4; let budget = context.config.max_width - line_start - comma.len() - 4;
let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent); let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent);
let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget); let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget);
let is_block = if let ast::ExprBlock(..) = body.node { let is_block = if let ast::ExprKind::Block(..) = body.node {
true true
} else { } else {
false false
@ -1307,8 +1306,8 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
// indentation. If its first line fits on one line with the other arguments, // indentation. If its first line fits on one line with the other arguments,
// we format the function arguments horizontally. // we format the function arguments horizontally.
let overflow_last = match args.last().map(|x| &x.node) { let overflow_last = match args.last().map(|x| &x.node) {
Some(&ast::Expr_::ExprClosure(..)) | Some(&ast::ExprKind::Closure(..)) |
Some(&ast::Expr_::ExprBlock(..)) if arg_count > 1 => true, Some(&ast::ExprKind::Block(..)) if arg_count > 1 => true,
_ => false, _ => false,
} && context.config.chains_overflow_last; } && context.config.chains_overflow_last;
@ -1666,9 +1665,9 @@ fn rewrite_unary_op(context: &RewriteContext,
-> Option<String> { -> Option<String> {
// For some reason, an UnOp is not spanned like BinOp! // For some reason, an UnOp is not spanned like BinOp!
let operator_str = match *op { let operator_str = match *op {
ast::UnOp::UnDeref => "*", ast::UnOp::Deref => "*",
ast::UnOp::UnNot => "!", ast::UnOp::Not => "!",
ast::UnOp::UnNeg => "-", ast::UnOp::Neg => "-",
}; };
rewrite_unary_prefix(context, operator_str, expr, width, offset) rewrite_unary_prefix(context, operator_str, expr, width, offset)
} }
@ -1744,8 +1743,8 @@ fn rewrite_expr_addrof(context: &RewriteContext,
offset: Indent) offset: Indent)
-> Option<String> { -> Option<String> {
let operator_str = match mutability { let operator_str = match mutability {
ast::Mutability::MutImmutable => "&", ast::Mutability::Immutable => "&",
ast::Mutability::MutMutable => "&mut ", ast::Mutability::Mutable => "&mut ",
}; };
rewrite_unary_prefix(context, operator_str, expr, width, offset) rewrite_unary_prefix(context, operator_str, expr, width, offset)
} }

View File

@ -51,7 +51,7 @@ impl Rewrite for ast::ViewPath {
} }
fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String { fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
let path_item_str = if let ast::PathListItem_::PathListIdent { name, .. } = vpi.node { let path_item_str = if let ast::PathListItemKind::Ident { name, .. } = vpi.node {
// A name. // A name.
if path_str.is_empty() { if path_str.is_empty() {
name.to_string() name.to_string()
@ -74,8 +74,8 @@ fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String
fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option<String> { fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option<String> {
let path_item_str = match vpi.node { let path_item_str = match vpi.node {
ast::PathListItem_::PathListIdent { name, .. } => name.to_string(), ast::PathListItemKind::Ident { name, .. } => name.to_string(),
ast::PathListItem_::PathListMod { .. } => "self".to_owned(), ast::PathListItemKind::Mod { .. } => "self".to_owned(),
}; };
Some(append_alias(path_item_str, vpi)) Some(append_alias(path_item_str, vpi))
@ -83,8 +83,8 @@ fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option<String> {
fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String { fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String {
match vpi.node { match vpi.node {
ast::PathListItem_::PathListIdent { rename: Some(rename), .. } | ast::PathListItemKind::Ident { rename: Some(rename), .. } |
ast::PathListItem_::PathListMod { rename: Some(rename), .. } => { ast::PathListItemKind::Mod { rename: Some(rename), .. } => {
format!("{} as {}", path_item_str, rename) format!("{} as {}", path_item_str, rename)
} }
_ => path_item_str, _ => path_item_str,

View File

@ -27,7 +27,6 @@ use syntax::{ast, abi};
use syntax::codemap::{Span, BytePos, mk_sp}; use syntax::codemap::{Span, BytePos, mk_sp};
use syntax::parse::token; use syntax::parse::token;
use syntax::ast::ImplItem; use syntax::ast::ImplItem;
use syntax::ptr::P;
// Statements of the form // Statements of the form
// let pat: ty = init; // let pat: ty = init;
@ -110,7 +109,7 @@ impl<'a> FmtVisitor<'a> {
let span = mk_sp(item.span.lo, item.span.hi - BytePos(1)); let span = mk_sp(item.span.lo, item.span.hi - BytePos(1));
match item.node { match item.node {
ast::ForeignItem_::ForeignItemFn(ref fn_decl, ref generics) => { ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => {
let indent = self.block_indent; let indent = self.block_indent;
let rewrite = rewrite_fn_base(&self.get_context(), let rewrite = rewrite_fn_base(&self.get_context(),
indent, indent,
@ -136,7 +135,7 @@ impl<'a> FmtVisitor<'a> {
None => self.format_missing(item.span.hi), None => self.format_missing(item.span.hi),
} }
} }
ast::ForeignItem_::ForeignItemStatic(ref ty, is_mutable) => { ast::ForeignItemKind::Static(ref ty, is_mutable) => {
// FIXME(#21): we're dropping potential comments in between the // FIXME(#21): we're dropping potential comments in between the
// function keywords here. // function keywords here.
let mut_str = if is_mutable { let mut_str = if is_mutable {
@ -441,12 +440,12 @@ impl<'a> FmtVisitor<'a> {
} }
pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> { pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
if let ast::Item_::ItemImpl(unsafety, if let ast::ItemKind::Impl(unsafety,
polarity, polarity,
ref generics, ref generics,
ref trait_ref, ref trait_ref,
ref self_ty, ref self_ty,
ref items) = item.node { ref items) = item.node {
let mut result = String::new(); let mut result = String::new();
result.push_str(format_visibility(item.vis)); result.push_str(format_visibility(item.vis));
result.push_str(format_unsafety(unsafety)); result.push_str(format_unsafety(unsafety));
@ -562,7 +561,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
} }
fn is_impl_single_line(context: &RewriteContext, fn is_impl_single_line(context: &RewriteContext,
items: &Vec<P<ImplItem>>, items: &Vec<ImplItem>,
result: &str, result: &str,
where_clause_str: &str, where_clause_str: &str,
item: &ast::Item) item: &ast::Item)
@ -919,15 +918,15 @@ pub fn rewrite_static(prefix: &str,
impl Rewrite for ast::FunctionRetTy { impl Rewrite for ast::FunctionRetTy {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
match *self { match *self {
ast::FunctionRetTy::DefaultReturn(_) => Some(String::new()), ast::FunctionRetTy::Default(_) => Some(String::new()),
ast::FunctionRetTy::NoReturn(_) => { ast::FunctionRetTy::None(_) => {
if width >= 4 { if width >= 4 {
Some("-> !".to_owned()) Some("-> !".to_owned())
} else { } else {
None None
} }
} }
ast::FunctionRetTy::Return(ref ty) => { ast::FunctionRetTy::Ty(ref ty) => {
let inner_width = try_opt!(width.checked_sub(3)); let inner_width = try_opt!(width.checked_sub(3));
ty.rewrite(context, inner_width, offset + 3).map(|r| format!("-> {}", r)) ty.rewrite(context, inner_width, offset + 3).map(|r| format!("-> {}", r))
} }
@ -940,7 +939,7 @@ impl Rewrite for ast::Arg {
if is_named_arg(self) { if is_named_arg(self) {
let mut result = try_opt!(self.pat.rewrite(context, width, offset)); let mut result = try_opt!(self.pat.rewrite(context, width, offset));
if self.ty.node != ast::Ty_::TyInfer { if self.ty.node != ast::TyKind::Infer {
result.push_str(": "); result.push_str(": ");
let max_width = try_opt!(width.checked_sub(result.len())); let max_width = try_opt!(width.checked_sub(result.len()));
let ty_str = try_opt!(self.ty.rewrite(context, max_width, offset + result.len())); let ty_str = try_opt!(self.ty.rewrite(context, max_width, offset + result.len()));
@ -959,7 +958,7 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf,
context: &RewriteContext) context: &RewriteContext)
-> Option<String> { -> Option<String> {
match explicit_self.node { match explicit_self.node {
ast::ExplicitSelf_::SelfRegion(lt, m, _) => { ast::SelfKind::Region(lt, m, _) => {
let mut_str = format_mutability(m); let mut_str = format_mutability(m);
match lt { match lt {
Some(ref l) => { Some(ref l) => {
@ -971,7 +970,7 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf,
None => Some(format!("&{}self", mut_str)), None => Some(format!("&{}self", mut_str)),
} }
} }
ast::ExplicitSelf_::SelfExplicit(ref ty, _) => { ast::SelfKind::Explicit(ref ty, _) => {
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty."); assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
let mutability = explicit_self_mutability(&args[0]); let mutability = explicit_self_mutability(&args[0]);
@ -979,7 +978,7 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf,
Some(format!("{}self: {}", format_mutability(mutability), type_str)) Some(format!("{}self: {}", format_mutability(mutability), type_str))
} }
ast::ExplicitSelf_::SelfValue(_) => { ast::SelfKind::Value(_) => {
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty."); assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
let mutability = explicit_self_mutability(&args[0]); let mutability = explicit_self_mutability(&args[0]);
@ -993,7 +992,7 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf,
// Hacky solution caused by absence of `Mutability` in `SelfValue` and // Hacky solution caused by absence of `Mutability` in `SelfValue` and
// `SelfExplicit` variants of `ast::ExplicitSelf_`. // `SelfExplicit` variants of `ast::ExplicitSelf_`.
fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability { fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
if let ast::Pat_::PatIdent(ast::BindingMode::BindByValue(mutability), _, _) = arg.pat.node { if let ast::PatKind::Ident(ast::BindingMode::ByValue(mutability), _, _) = arg.pat.node {
mutability mutability
} else { } else {
unreachable!() unreachable!()
@ -1010,13 +1009,13 @@ pub fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
pub fn span_hi_for_arg(arg: &ast::Arg) -> BytePos { pub fn span_hi_for_arg(arg: &ast::Arg) -> BytePos {
match arg.ty.node { match arg.ty.node {
ast::Ty_::TyInfer if is_named_arg(arg) => arg.pat.span.hi, ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi,
_ => arg.ty.span.hi, _ => arg.ty.span.hi,
} }
} }
pub fn is_named_arg(arg: &ast::Arg) -> bool { pub fn is_named_arg(arg: &ast::Arg) -> bool {
if let ast::Pat_::PatIdent(_, ident, _) = arg.pat.node { if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
ident.node != token::special_idents::invalid ident.node != token::special_idents::invalid
} else { } else {
true true
@ -1025,9 +1024,9 @@ pub fn is_named_arg(arg: &ast::Arg) -> bool {
fn span_for_return(ret: &ast::FunctionRetTy) -> Span { fn span_for_return(ret: &ast::FunctionRetTy) -> Span {
match *ret { match *ret {
ast::FunctionRetTy::NoReturn(ref span) | ast::FunctionRetTy::None(ref span) |
ast::FunctionRetTy::DefaultReturn(ref span) => span.clone(), ast::FunctionRetTy::Default(ref span) => span.clone(),
ast::FunctionRetTy::Return(ref ty) => ty.span, ast::FunctionRetTy::Ty(ref ty) => ty.span,
} }
} }
@ -1085,7 +1084,7 @@ fn rewrite_fn_base(context: &RewriteContext,
result.push_str("const "); result.push_str("const ");
} }
if abi != abi::Rust { if abi != abi::Abi::Rust {
result.push_str(&::utils::format_abi(abi)); result.push_str(&::utils::format_abi(abi));
} }

View File

@ -26,13 +26,15 @@ extern crate diff;
extern crate term; extern crate term;
use syntax::ast; use syntax::ast;
use syntax::codemap::{mk_sp, Span}; use syntax::codemap::{mk_sp, CodeMap, Span};
use syntax::diagnostic::{EmitterWriter, Handler}; use syntax::errors::Handler;
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
use syntax::parse::{self, ParseSess}; use syntax::parse::{self, ParseSess};
use std::io::stdout; use std::io::stdout;
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
use std::path::Path; use std::path::Path;
use std::rc::Rc;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
@ -367,15 +369,23 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
pub fn format_string(input: String, config: &Config) -> FileMap { pub fn format_string(input: String, config: &Config) -> FileMap {
let path = "stdin"; let path = "stdin";
let mut parse_session = ParseSess::new(); let codemap = Rc::new(CodeMap::new());
let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto,
None,
true,
false,
codemap.clone());
let mut parse_session = ParseSess::with_span_handler(tty_handler, codemap.clone());
let krate = parse::parse_crate_from_source_str(path.to_owned(), let krate = parse::parse_crate_from_source_str(path.to_owned(),
input, input,
Vec::new(), Vec::new(),
&parse_session); &parse_session);
// Suppress error output after parsing. // Suppress error output after parsing.
let emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None)); let silent_emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None, codemap.clone()));
parse_session.span_diagnostic.handler = Handler::with_emitter(false, emitter); parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter);
// FIXME: we still use a FileMap even though we only have // FIXME: we still use a FileMap even though we only have
// one file, because fmt_lines requires a FileMap // one file, because fmt_lines requires a FileMap
@ -393,12 +403,20 @@ pub fn format_string(input: String, config: &Config) -> FileMap {
} }
pub fn format(file: &Path, config: &Config) -> FileMap { pub fn format(file: &Path, config: &Config) -> FileMap {
let mut parse_session = ParseSess::new(); let codemap = Rc::new(CodeMap::new());
let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto,
None,
true,
false,
codemap.clone());
let mut parse_session = ParseSess::with_span_handler(tty_handler, codemap.clone());
let krate = parse::parse_crate_from_file(file, Vec::new(), &parse_session); let krate = parse::parse_crate_from_file(file, Vec::new(), &parse_session);
// Suppress error output after parsing. // Suppress error output after parsing.
let emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None)); let silent_emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None, codemap.clone()));
parse_session.span_diagnostic.handler = Handler::with_emitter(false, emitter); parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter);
let mut file_map = fmt_ast(&krate, &parse_session, file, config); let mut file_map = fmt_ast(&krate, &parse_session, file, config);

View File

@ -40,7 +40,7 @@ fn list_submodules<'a>(module: &'a ast::Mod,
result: &mut HashMap<PathBuf, &'a ast::Mod>) { result: &mut HashMap<PathBuf, &'a ast::Mod>) {
debug!("list_submodules: search_dir: {:?}", search_dir); debug!("list_submodules: search_dir: {:?}", search_dir);
for item in &module.items { for item in &module.items {
if let ast::ItemMod(ref sub_mod) = item.node { if let ast::ItemKind::Mod(ref sub_mod) = item.node {
if !utils::contains_skip(&item.attrs) { if !utils::contains_skip(&item.attrs) {
let is_internal = codemap.span_to_filename(item.span) == let is_internal = codemap.span_to_filename(item.span) ==
codemap.span_to_filename(sub_mod.inner); codemap.span_to_filename(sub_mod.inner);

View File

@ -15,16 +15,16 @@ use lists::{format_item_list, itemize_list};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple}; use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use types::rewrite_path; use types::rewrite_path;
use syntax::ast::{BindingMode, Pat, Pat_, FieldPat}; use syntax::ast::{BindingMode, Pat, PatKind, FieldPat};
impl Rewrite for Pat { impl Rewrite for Pat {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
match self.node { match self.node {
Pat_::PatBox(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset), PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset),
Pat_::PatIdent(binding_mode, ident, ref sub_pat) => { PatKind::Ident(binding_mode, ident, ref sub_pat) => {
let (prefix, mutability) = match binding_mode { let (prefix, mutability) = match binding_mode {
BindingMode::BindByRef(mutability) => ("ref ", mutability), BindingMode::ByRef(mutability) => ("ref ", mutability),
BindingMode::BindByValue(mutability) => ("", mutability), BindingMode::ByValue(mutability) => ("", mutability),
}; };
let mut_infix = format_mutability(mutability); let mut_infix = format_mutability(mutability);
let id_str = ident.node.to_string(); let id_str = ident.node.to_string();
@ -43,31 +43,32 @@ impl Rewrite for Pat {
let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat); let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat);
wrap_str(result, context.config.max_width, width, offset) wrap_str(result, context.config.max_width, width, offset)
} }
Pat_::PatWild => { PatKind::Wild => {
if 1 <= width { if 1 <= width {
Some("_".to_owned()) Some("_".to_owned())
} else { } else {
None None
} }
} }
Pat_::PatQPath(ref q_self, ref path) => { PatKind::QPath(ref q_self, ref path) => {
rewrite_path(context, true, Some(q_self), path, width, offset) rewrite_path(context, true, Some(q_self), path, width, offset)
} }
Pat_::PatRange(ref lhs, ref rhs) => { PatKind::Range(ref lhs, ref rhs) => {
rewrite_pair(&**lhs, &**rhs, "", "...", "", context, width, offset) rewrite_pair(&**lhs, &**rhs, "", "...", "", context, width, offset)
} }
Pat_::PatRegion(ref pat, mutability) => { PatKind::Ref(ref pat, mutability) => {
let prefix = format!("&{}", format_mutability(mutability)); let prefix = format!("&{}", format_mutability(mutability));
rewrite_unary_prefix(context, &prefix, &**pat, width, offset) rewrite_unary_prefix(context, &prefix, &**pat, width, offset)
} }
Pat_::PatTup(ref items) => { PatKind::Tup(ref items) => {
rewrite_tuple(context, rewrite_tuple(context,
items.iter().map(|x| &**x), items.iter().map(|x| &**x),
self.span, self.span,
width, width,
offset) offset)
} }
Pat_::PatEnum(ref path, ref pat_vec) => { PatKind::Path(ref path) => rewrite_path(context, true, None, path, width, offset),
PatKind::TupleStruct(ref path, ref pat_vec) => {
let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset)); let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset));
match *pat_vec { match *pat_vec {
@ -97,8 +98,8 @@ impl Rewrite for Pat {
None => Some(format!("{}(..)", path_str)), None => Some(format!("{}(..)", path_str)),
} }
} }
Pat_::PatLit(ref expr) => expr.rewrite(context, width, offset), PatKind::Lit(ref expr) => expr.rewrite(context, width, offset),
Pat_::PatVec(ref prefix, ref slice_pat, ref suffix) => { PatKind::Vec(ref prefix, ref slice_pat, ref suffix) => {
// Rewrite all the sub-patterns. // Rewrite all the sub-patterns.
let prefix = prefix.iter().map(|p| p.rewrite(context, width, offset)); let prefix = prefix.iter().map(|p| p.rewrite(context, width, offset));
let slice_pat = slice_pat.as_ref().map(|p| { let slice_pat = slice_pat.as_ref().map(|p| {
@ -118,7 +119,7 @@ impl Rewrite for Pat {
let result = format!("[{}]", pats.join(", ")); let result = format!("[{}]", pats.join(", "));
wrap_str(result, context.config.max_width, width, offset) wrap_str(result, context.config.max_width, width, offset)
} }
Pat_::PatStruct(ref path, ref fields, elipses) => { PatKind::Struct(ref path, ref fields, elipses) => {
let path = try_opt!(rewrite_path(context, true, None, path, width, offset)); let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
let (elipses_str, terminator) = if elipses { let (elipses_str, terminator) = if elipses {
@ -167,7 +168,7 @@ impl Rewrite for Pat {
} }
} }
// FIXME(#819) format pattern macros. // FIXME(#819) format pattern macros.
Pat_::PatMac(..) => { PatKind::Mac(..) => {
wrap_str(context.snippet(self.span), wrap_str(context.snippet(self.span),
context.config.max_width, context.config.max_width,
width, width,

View File

@ -172,9 +172,9 @@ fn rewrite_segment(expr_context: bool,
let offset = offset + ident_len; let offset = offset + ident_len;
let params = match segment.parameters { let params = match segment.parameters {
ast::PathParameters::AngleBracketedParameters(ref data) if !data.lifetimes.is_empty() || ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
!data.types.is_empty() || !data.types.is_empty() ||
!data.bindings.is_empty() => { !data.bindings.is_empty() => {
let param_list = data.lifetimes let param_list = data.lifetimes
.iter() .iter()
.map(SegmentParam::LifeTime) .map(SegmentParam::LifeTime)
@ -213,10 +213,10 @@ fn rewrite_segment(expr_context: bool,
format!("{}<{}>", separator, list_str) format!("{}<{}>", separator, list_str)
} }
ast::PathParameters::ParenthesizedParameters(ref data) => { ast::PathParameters::Parenthesized(ref data) => {
let output = match data.output { let output = match data.output {
Some(ref ty) => FunctionRetTy::Return(ty.clone()), Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
None => FunctionRetTy::DefaultReturn(codemap::DUMMY_SP), None => FunctionRetTy::Default(codemap::DUMMY_SP),
}; };
try_opt!(format_function_type(data.inputs.iter().map(|x| &**x), try_opt!(format_function_type(data.inputs.iter().map(|x| &**x),
&output, &output,
@ -259,13 +259,13 @@ fn format_function_type<'a, I>(inputs: I,
let list_str = try_opt!(format_fn_args(items, budget, offset, context.config)); let list_str = try_opt!(format_fn_args(items, budget, offset, context.config));
let output = match *output { let output = match *output {
FunctionRetTy::Return(ref ty) => { FunctionRetTy::Ty(ref ty) => {
let budget = try_opt!(width.checked_sub(4)); let budget = try_opt!(width.checked_sub(4));
let type_str = try_opt!(ty.rewrite(context, budget, offset + 4)); let type_str = try_opt!(ty.rewrite(context, budget, offset + 4));
format!(" -> {}", type_str) format!(" -> {}", type_str)
} }
FunctionRetTy::NoReturn(..) => " -> !".to_owned(), FunctionRetTy::None(..) => " -> !".to_owned(),
FunctionRetTy::DefaultReturn(..) => String::new(), FunctionRetTy::Default(..) => String::new(),
}; };
let infix = if output.len() + list_str.len() > width { let infix = if output.len() + list_str.len() > width {
@ -470,7 +470,7 @@ impl Rewrite for ast::TraitRef {
impl Rewrite for ast::Ty { impl Rewrite for ast::Ty {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
match self.node { match self.node {
ast::TyObjectSum(ref ty, ref bounds) => { ast::TyKind::ObjectSum(ref ty, ref bounds) => {
let ty_str = try_opt!(ty.rewrite(context, width, offset)); let ty_str = try_opt!(ty.rewrite(context, width, offset));
let overhead = ty_str.len() + 3; let overhead = ty_str.len() + 3;
let plus_str = match context.config.type_punctuation_density { let plus_str = match context.config.type_punctuation_density {
@ -484,15 +484,15 @@ impl Rewrite for ast::Ty {
try_opt!(width.checked_sub(overhead)), try_opt!(width.checked_sub(overhead)),
offset + overhead)))) offset + overhead))))
} }
ast::TyPtr(ref mt) => { ast::TyKind::Ptr(ref mt) => {
let prefix = match mt.mutbl { let prefix = match mt.mutbl {
Mutability::MutMutable => "*mut ", Mutability::Mutable => "*mut ",
Mutability::MutImmutable => "*const ", Mutability::Immutable => "*const ",
}; };
rewrite_unary_prefix(context, prefix, &*mt.ty, width, offset) rewrite_unary_prefix(context, prefix, &*mt.ty, width, offset)
} }
ast::TyRptr(ref lifetime, ref mt) => { ast::TyKind::Rptr(ref lifetime, ref mt) => {
let mut_str = format_mutability(mt.mutbl); let mut_str = format_mutability(mt.mutbl);
let mut_len = mut_str.len(); let mut_len = mut_str.len();
Some(match *lifetime { Some(match *lifetime {
@ -520,39 +520,39 @@ impl Rewrite for ast::Ty {
} }
// FIXME: we drop any comments here, even though it's a silly place to put // FIXME: we drop any comments here, even though it's a silly place to put
// comments. // comments.
ast::TyParen(ref ty) => { ast::TyKind::Paren(ref ty) => {
let budget = try_opt!(width.checked_sub(2)); let budget = try_opt!(width.checked_sub(2));
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("({})", ty_str)) ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("({})", ty_str))
} }
ast::TyVec(ref ty) => { ast::TyKind::Vec(ref ty) => {
let budget = try_opt!(width.checked_sub(2)); let budget = try_opt!(width.checked_sub(2));
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str)) ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str))
} }
ast::TyTup(ref items) => { ast::TyKind::Tup(ref items) => {
rewrite_tuple(context, rewrite_tuple(context,
items.iter().map(|x| &**x), items.iter().map(|x| &**x),
self.span, self.span,
width, width,
offset) offset)
} }
ast::TyPolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset), ast::TyKind::PolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset),
ast::TyPath(ref q_self, ref path) => { ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, false, q_self.as_ref(), path, width, offset) rewrite_path(context, false, q_self.as_ref(), path, width, offset)
} }
ast::TyFixedLengthVec(ref ty, ref repeats) => { ast::TyKind::FixedLengthVec(ref ty, ref repeats) => {
rewrite_pair(&**ty, &**repeats, "[", "; ", "]", context, width, offset) rewrite_pair(&**ty, &**repeats, "[", "; ", "]", context, width, offset)
} }
ast::TyInfer => { ast::TyKind::Infer => {
if width >= 1 { if width >= 1 {
Some("_".to_owned()) Some("_".to_owned())
} else { } else {
None None
} }
} }
ast::TyBareFn(ref bare_fn) => { ast::TyKind::BareFn(ref bare_fn) => {
rewrite_bare_fn(bare_fn, self.span, context, width, offset) rewrite_bare_fn(bare_fn, self.span, context, width, offset)
} }
ast::TyMac(..) | ast::TyTypeof(..) => unreachable!(), ast::TyKind::Mac(..) | ast::TyKind::Typeof(..) => unreachable!(),
} }
} }
} }
@ -567,7 +567,7 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
result.push_str(&::utils::format_unsafety(bare_fn.unsafety)); result.push_str(&::utils::format_unsafety(bare_fn.unsafety));
if bare_fn.abi != abi::Rust { if bare_fn.abi != abi::Abi::Rust {
result.push_str(&::utils::format_abi(bare_fn.abi)); result.push_str(&::utils::format_abi(bare_fn.abi));
} }

View File

@ -10,7 +10,7 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItem_}; use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind};
use syntax::codemap::{CodeMap, Span, BytePos}; use syntax::codemap::{CodeMap, Span, BytePos};
use syntax::abi; use syntax::abi;
@ -77,8 +77,8 @@ pub fn format_unsafety(unsafety: ast::Unsafety) -> &'static str {
#[inline] #[inline]
pub fn format_mutability(mutability: ast::Mutability) -> &'static str { pub fn format_mutability(mutability: ast::Mutability) -> &'static str {
match mutability { match mutability {
ast::Mutability::MutMutable => "mut ", ast::Mutability::Mutable => "mut ",
ast::Mutability::MutImmutable => "", ast::Mutability::Immutable => "",
} }
} }
@ -109,8 +109,8 @@ pub fn last_line_width(s: &str) -> usize {
#[inline] #[inline]
fn is_skip(meta_item: &MetaItem) -> bool { fn is_skip(meta_item: &MetaItem) -> bool {
match meta_item.node { match meta_item.node {
MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION, MetaItemKind::Word(ref s) => *s == SKIP_ANNOTATION,
MetaItem_::MetaList(ref s, ref l) => *s == "cfg_attr" && l.len() == 2 && is_skip(&l[1]), MetaItemKind::List(ref s, ref l) => *s == "cfg_attr" && l.len() == 2 && is_skip(&l[1]),
_ => false, _ => false,
} }
} }
@ -137,9 +137,9 @@ pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
#[inline] #[inline]
pub fn semicolon_for_expr(expr: &ast::Expr) -> bool { pub fn semicolon_for_expr(expr: &ast::Expr) -> bool {
match expr.node { match expr.node {
ast::Expr_::ExprRet(..) | ast::ExprKind::Ret(..) |
ast::Expr_::ExprAgain(..) | ast::ExprKind::Again(..) |
ast::Expr_::ExprBreak(..) => true, ast::ExprKind::Break(..) => true,
_ => false, _ => false,
} }
} }
@ -147,16 +147,16 @@ pub fn semicolon_for_expr(expr: &ast::Expr) -> bool {
#[inline] #[inline]
pub fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool { pub fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool {
match stmt.node { match stmt.node {
ast::Stmt_::StmtSemi(ref expr, _) => { ast::StmtKind::Semi(ref expr, _) => {
match expr.node { match expr.node {
ast::Expr_::ExprWhile(..) | ast::ExprKind::While(..) |
ast::Expr_::ExprWhileLet(..) | ast::ExprKind::WhileLet(..) |
ast::Expr_::ExprLoop(..) | ast::ExprKind::Loop(..) |
ast::Expr_::ExprForLoop(..) => false, ast::ExprKind::ForLoop(..) => false,
_ => true, _ => true,
} }
} }
ast::Stmt_::StmtExpr(..) => false, ast::StmtKind::Expr(..) => false,
_ => true, _ => true,
} }
} }

View File

@ -36,8 +36,8 @@ pub struct FmtVisitor<'a> {
impl<'a> FmtVisitor<'a> { impl<'a> FmtVisitor<'a> {
fn visit_stmt(&mut self, stmt: &ast::Stmt) { fn visit_stmt(&mut self, stmt: &ast::Stmt) {
match stmt.node { match stmt.node {
ast::Stmt_::StmtDecl(ref decl, _) => { ast::StmtKind::Decl(ref decl, _) => {
if let ast::Decl_::DeclItem(ref item) = decl.node { if let ast::DeclKind::Item(ref item) = decl.node {
self.visit_item(item); self.visit_item(item);
} else { } else {
let rewrite = stmt.rewrite(&self.get_context(), let rewrite = stmt.rewrite(&self.get_context(),
@ -47,14 +47,14 @@ impl<'a> FmtVisitor<'a> {
self.push_rewrite(stmt.span, rewrite); self.push_rewrite(stmt.span, rewrite);
} }
} }
ast::Stmt_::StmtExpr(..) | ast::Stmt_::StmtSemi(..) => { ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
let rewrite = stmt.rewrite(&self.get_context(), let rewrite = stmt.rewrite(&self.get_context(),
self.config.max_width - self.block_indent.width(), self.config.max_width - self.block_indent.width(),
self.block_indent); self.block_indent);
self.push_rewrite(stmt.span, rewrite); self.push_rewrite(stmt.span, rewrite);
} }
ast::Stmt_::StmtMac(ref mac, _macro_style, _) => { ast::StmtKind::Mac(ref mac, _macro_style, _) => {
self.format_missing_with_indent(stmt.span.lo); self.format_missing_with_indent(stmt.span.lo);
self.visit_mac(mac); self.visit_mac(mac);
} }
@ -183,7 +183,7 @@ impl<'a> FmtVisitor<'a> {
// FIXME This is overly conservative and means we miss attributes on // FIXME This is overly conservative and means we miss attributes on
// inline modules. // inline modules.
match item.node { match item.node {
ast::Item_::ItemMod(_) => { ast::ItemKind::Mod(_) => {
if utils::contains_skip(&item.attrs) { if utils::contains_skip(&item.attrs) {
return; return;
} }
@ -197,10 +197,10 @@ impl<'a> FmtVisitor<'a> {
} }
match item.node { match item.node {
ast::Item_::ItemUse(ref vp) => { ast::ItemKind::Use(ref vp) => {
self.format_import(item.vis, vp, item.span); self.format_import(item.vis, vp, item.span);
} }
ast::Item_::ItemImpl(..) => { ast::ItemKind::Impl(..) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
if let Some(impl_str) = format_impl(&self.get_context(), item, self.block_indent) { if let Some(impl_str) = format_impl(&self.get_context(), item, self.block_indent) {
self.buffer.push_str(&impl_str); self.buffer.push_str(&impl_str);
@ -208,7 +208,7 @@ impl<'a> FmtVisitor<'a> {
} }
} }
// FIXME(#78): format traits. // FIXME(#78): format traits.
ast::Item_::ItemTrait(_, _, _, ref trait_items) => { ast::ItemKind::Trait(_, _, _, ref trait_items) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
self.block_indent = self.block_indent.block_indent(self.config); self.block_indent = self.block_indent.block_indent(self.config);
for item in trait_items { for item in trait_items {
@ -216,13 +216,13 @@ impl<'a> FmtVisitor<'a> {
} }
self.block_indent = self.block_indent.block_unindent(self.config); self.block_indent = self.block_indent.block_unindent(self.config);
} }
ast::Item_::ItemExternCrate(_) => { ast::ItemKind::ExternCrate(_) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
let new_str = self.snippet(item.span); let new_str = self.snippet(item.span);
self.buffer.push_str(&new_str); self.buffer.push_str(&new_str);
self.last_pos = item.span.hi; self.last_pos = item.span.hi;
} }
ast::Item_::ItemStruct(ref def, ref generics) => { ast::ItemKind::Struct(ref def, ref generics) => {
let rewrite = { let rewrite = {
let indent = self.block_indent; let indent = self.block_indent;
let context = self.get_context(); let context = self.get_context();
@ -243,24 +243,24 @@ impl<'a> FmtVisitor<'a> {
}; };
self.push_rewrite(item.span, rewrite); self.push_rewrite(item.span, rewrite);
} }
ast::Item_::ItemEnum(ref def, ref generics) => { ast::ItemKind::Enum(ref def, ref generics) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
self.visit_enum(item.ident, item.vis, def, generics, item.span); self.visit_enum(item.ident, item.vis, def, generics, item.span);
self.last_pos = item.span.hi; self.last_pos = item.span.hi;
} }
ast::Item_::ItemMod(ref module) => { ast::ItemKind::Mod(ref module) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
self.format_mod(module, item.vis, item.span, item.ident); self.format_mod(module, item.vis, item.span, item.ident);
} }
ast::Item_::ItemMac(ref mac) => { ast::ItemKind::Mac(ref mac) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
self.visit_mac(mac); self.visit_mac(mac);
} }
ast::Item_::ItemForeignMod(ref foreign_mod) => { ast::ItemKind::ForeignMod(ref foreign_mod) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
self.format_foreign_mod(foreign_mod, item.span); self.format_foreign_mod(foreign_mod, item.span);
} }
ast::Item_::ItemStatic(ref ty, mutability, ref expr) => { ast::ItemKind::Static(ref ty, mutability, ref expr) => {
let rewrite = rewrite_static("static", let rewrite = rewrite_static("static",
item.vis, item.vis,
item.ident, item.ident,
@ -270,32 +270,32 @@ impl<'a> FmtVisitor<'a> {
&self.get_context()); &self.get_context());
self.push_rewrite(item.span, rewrite); self.push_rewrite(item.span, rewrite);
} }
ast::Item_::ItemConst(ref ty, ref expr) => { ast::ItemKind::Const(ref ty, ref expr) => {
let rewrite = rewrite_static("const", let rewrite = rewrite_static("const",
item.vis, item.vis,
item.ident, item.ident,
ty, ty,
ast::Mutability::MutImmutable, ast::Mutability::Immutable,
expr, expr,
&self.get_context()); &self.get_context());
self.push_rewrite(item.span, rewrite); self.push_rewrite(item.span, rewrite);
} }
ast::Item_::ItemDefaultImpl(..) => { ast::ItemKind::DefaultImpl(..) => {
// FIXME(#78): format impl definitions. // FIXME(#78): format impl definitions.
} }
ast::ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => { ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
self.visit_fn(visit::FnKind::ItemFn(item.ident, self.visit_fn(visit::FnKind::ItemFn(item.ident,
generics, generics,
unsafety, unsafety,
constness, constness,
abi, abi,
item.vis), item.vis),
declaration, decl,
body, body,
item.span, item.span,
item.id) item.id)
} }
ast::Item_::ItemTy(ref ty, ref generics) => { ast::ItemKind::Ty(ref ty, ref generics) => {
let rewrite = rewrite_type_alias(&self.get_context(), let rewrite = rewrite_type_alias(&self.get_context(),
self.block_indent, self.block_indent,
item.ident, item.ident,
@ -314,22 +314,22 @@ impl<'a> FmtVisitor<'a> {
} }
match ti.node { match ti.node {
ast::ConstTraitItem(..) => { ast::TraitItemKind::Const(..) => {
// FIXME: Implement // FIXME: Implement
} }
ast::MethodTraitItem(ref sig, None) => { ast::TraitItemKind::Method(ref sig, None) => {
let indent = self.block_indent; let indent = self.block_indent;
let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span); let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
self.push_rewrite(ti.span, rewrite); self.push_rewrite(ti.span, rewrite);
} }
ast::MethodTraitItem(ref sig, Some(ref body)) => { ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
self.visit_fn(visit::FnKind::Method(ti.ident, sig, None), self.visit_fn(visit::FnKind::Method(ti.ident, sig, None),
&sig.decl, &sig.decl,
&body, &body,
ti.span, ti.span,
ti.id); ti.id);
} }
ast::TypeTraitItem(..) => { ast::TraitItemKind::Type(..) => {
// FIXME: Implement // FIXME: Implement
} }
} }