Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ use expr::rewrite_call;
use config::BlockIndentStyle;
use macros::convert_try_mac;

use std::iter;
use syntax::{ast, ptr};
use syntax::codemap::{mk_sp, Span};

Expand All @@ -99,6 +100,12 @@ pub fn rewrite_chain(expr: &ast::Expr,
let total_span = expr.span;
let (parent, subexpr_list) = make_subexpr_list(expr, context);

// Bail out if the chain is just try sugar, i.e., an expression followed by
// any number of `?`s.
if chain_only_try(&subexpr_list) {
return rewrite_try(&parent, subexpr_list.len(), context, width, offset);
}

// Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`.
let parent_block_indent = chain_base_indent(context, offset);
let parent_context = &RewriteContext { block_indent: parent_block_indent, ..*context };
Expand Down Expand Up @@ -196,6 +203,27 @@ pub fn rewrite_chain(expr: &ast::Expr,
offset)
}

// True if the chain is only `?`s.
fn chain_only_try(exprs: &[ast::Expr]) -> bool {
exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node {
true
} else {
false
})
}

pub fn rewrite_try(expr: &ast::Expr,
try_count: usize,
context: &RewriteContext,
width: usize,
offset: Indent)
-> Option<String> {
let sub_expr = try_opt!(expr.rewrite(context, width - try_count, offset));
Some(format!("{}{}",
sub_expr,
iter::repeat("?").take(try_count).collect::<String>()))
}

fn join_rewrites(rewrites: &[String], subexps: &[ast::Expr], connector: &str) -> String {
let mut rewrite_iter = rewrites.iter();
let mut result = rewrite_iter.next().unwrap().clone();
Expand Down
15 changes: 15 additions & 0 deletions tests/source/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,18 @@ fn try_shorthand() {
|tcx| tcx.lookup_item_type(def_id).generics)?;
fooooooooooooooooooooooooooo()?.bar()?.baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz()?;
}

fn issue_1004() {
match *self {
ty::ImplOrTraitItem::MethodTraitItem(ref i) => write!(f, "{:?}", i),
ty::ImplOrTraitItem::ConstTraitItem(ref i) => write!(f, "{:?}", i),
ty::ImplOrTraitItem::TypeTraitItem(ref i) => write!(f, "{:?}", i),
}
?;

ty::tls::with(|tcx| {
let tap = ty::Binder(TraitAndProjections(principal, projections));
in_binder(f, tcx, &ty::Binder(""), Some(tap))
})
?;
}
13 changes: 13 additions & 0 deletions tests/target/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,16 @@ fn try_shorthand() {
.bar()?
.baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz()?;
}

fn issue_1004() {
match *self {
ty::ImplOrTraitItem::MethodTraitItem(ref i) => write!(f, "{:?}", i),
ty::ImplOrTraitItem::ConstTraitItem(ref i) => write!(f, "{:?}", i),
ty::ImplOrTraitItem::TypeTraitItem(ref i) => write!(f, "{:?}", i),
}?;

ty::tls::with(|tcx| {
let tap = ty::Binder(TraitAndProjections(principal, projections));
in_binder(f, tcx, &ty::Binder(""), Some(tap))
})?;
}