Skip to content

Nesting of block indented expressions #1572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 23, 2017
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
256 changes: 181 additions & 75 deletions src/expr.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use codemap::SpanUtils;
use lists::{format_item_list, itemize_list, format_fn_args};
use rewrite::{Rewrite, RewriteContext};
use utils::{extra_offset, format_mutability, colon_spaces, wrap_str};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple_type};
use config::TypeDensity;
use itertools::Itertools;

Expand Down Expand Up @@ -662,7 +662,7 @@ impl Rewrite for ast::Ty {
})
}
ast::TyKind::Tup(ref items) => {
rewrite_tuple(context, items.iter().map(|x| &**x), self.span, shape)
rewrite_tuple_type(context, items.iter().map(|x| &**x), self.span, shape)
}
ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
Expand Down
43 changes: 23 additions & 20 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,32 @@ pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, shape: Shape) -> Option<S
{
let snippet = s.as_ref();

if !snippet.contains('\n') && snippet.len() > shape.width {
return None;
} else {
let mut lines = snippet.lines();

// The caller of this function has already placed `shape.offset`
// characters on the first line.
let first_line_max_len = try_opt!(max_width.checked_sub(shape.indent.width()));
if lines.next().unwrap().len() > first_line_max_len {
if !snippet.is_empty() {
if !snippet.contains('\n') && snippet.len() > shape.width {
return None;
}
} else {
let mut lines = snippet.lines();

// The caller of this function has already placed `shape.offset`
// characters on the first line.
let first_line_max_len = try_opt!(max_width.checked_sub(shape.indent.width()));
if lines.next().unwrap().len() > first_line_max_len {
return None;
}

// The other lines must fit within the maximum width.
if lines.any(|line| line.len() > max_width) {
return None;
}
// The other lines must fit within the maximum width.
if lines.any(|line| line.len() > max_width) {
return None;
}

// `width` is the maximum length of the last line, excluding
// indentation.
// A special check for the last line, since the caller may
// place trailing characters on this line.
if snippet.lines().rev().next().unwrap().len() > shape.indent.width() + shape.width {
return None;
// `width` is the maximum length of the last line, excluding
// indentation.
// A special check for the last line, since the caller may
// place trailing characters on this line.
if snippet.lines().rev().next().unwrap().len() >
shape.indent.width() + shape.width {
return None;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/source/configs-fn_call_style-block-trailing-comma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
// rustfmt should not add trailing comma when rewriting macro. See #1528.
fn a() {
panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:");
foo(oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt());
}
51 changes: 51 additions & 0 deletions tests/source/configs-fn_call_style-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,54 @@ fn query(conn: &Connection) -> Result<()> {

Ok(())
}

// #1449
fn future_rayon_wait_1_thread() {
// run with only 1 worker thread; this would deadlock if we couldn't make progress
let mut result = None;
ThreadPool::new(Configuration::new().num_threads(1))
.unwrap()
.install(
|| {
scope(
|s| {
use std::sync::mpsc::channel;
let (tx, rx) = channel();
let a = s.spawn_future(lazy(move || Ok::<usize, ()>(rx.recv().unwrap())));
// ^^^^ FIXME: why is this needed?
let b = s.spawn_future(a.map(|v| v + 1));
let c = s.spawn_future(b.map(|v| v + 1));
s.spawn(move |_| tx.send(20).unwrap());
result = Some(c.rayon_wait().unwrap());
},
);
},
);
assert_eq!(result, Some(22));
}

// #1494
impl Cursor {
fn foo() {
self.cur_type()
.num_template_args()
.or_else(|| {
let n: c_int = unsafe { clang_Cursor_getNumTemplateArguments(self.x) };

if n >= 0 {
Some(n as u32)
} else {
debug_assert_eq!(n, -1);
None
}
})
.or_else(|| {
let canonical = self.canonical();
if canonical != *self {
canonical.num_template_args()
} else {
None
}
});
}
}
129 changes: 129 additions & 0 deletions tests/source/expr-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,132 @@ fn foo() {
DefinitiveListTactic::Horizontal
}
}

fn combine_block() {
foo(
Bar {
x: value,
y: value2,
},
);

foo((Bar {
x: value,
y: value2,
},));

foo((1, 2, 3, Bar {
x: value,
y: value2,
}));

foo((1, 2, 3, |x| {
let y = x + 1;
let z = y + 1;
z
}));

let opt = Some(
Struct(
long_argument_one,
long_argument_two,
long_argggggggg,
),
);

do_thing(
|param| {
action();
foo(param)
},
);

do_thing(
x,
|param| {
action();
foo(param)
},
);

do_thing(
x,
(
1,
2,
3,
|param| {
action();
foo(param)
},
),
);

Ok(
some_function(
lllllllllong_argument_one,
lllllllllong_argument_two,
lllllllllllllllllllllllllllllong_argument_three,
),
);

foo(
thing,
bar(
param2,
pparam1param1param1param1param1param1param1param1param1param1aram1,
param3,
),
);

foo.map_or(
|| {
Ok(
SomeStruct {
f1: 0,
f2: 0,
f3: 0,
},
)
},
);

match opt {
Some(x) => somefunc(anotherfunc(
long_argument_one,
long_argument_two,
long_argument_three,
)),
Some(x) => |x| {
let y = x + 1;
let z = y + 1;
z
},
Some(x) => (1, 2, |x| {
let y = x + 1;
let z = y + 1;
z
}),
Some(x) => SomeStruct {
f1: long_argument_one,
f2: long_argument_two,
f3: long_argument_three,
},
None => Ok(SomeStruct {
f1: long_argument_one,
f2: long_argument_two,
f3: long_argument_three,
}),
};

match x {
y => func(
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
),
_ => func(
x,
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
zzz,
),
}
}
18 changes: 8 additions & 10 deletions tests/target/closure-block-inside-macro.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// rustfmt-fn_call_style: Block

// #1547
fuzz_target!(
|data: &[u8]| {
if let Some(first) = data.first() {
let index = *first as usize;
if index >= ENCODINGS.len() {
return;
}
let encoding = ENCODINGS[index];
dispatch_test(encoding, &data[1..]);
fuzz_target!(|data: &[u8]| {
if let Some(first) = data.first() {
let index = *first as usize;
if index >= ENCODINGS.len() {
return;
}
let encoding = ENCODINGS[index];
dispatch_test(encoding, &data[1..]);
}
);
});
3 changes: 3 additions & 0 deletions tests/target/configs-fn_call_style-block-trailing-comma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
// rustfmt should not add trailing comma when rewriting macro. See #1528.
fn a() {
panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:");
foo(
oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt(),
);
}
63 changes: 56 additions & 7 deletions tests/target/configs-fn_call_style-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ fn main() {
"elit",
);
// #1501
let hyper = Arc::new(
Client::with_connector(HttpsConnector::new(TlsClient::new())),
);
let hyper = Arc::new(Client::with_connector(
HttpsConnector::new(TlsClient::new()),
));
}

// #1521
impl Foo {
fn map_pixel_to_coords(&self, point: &Vector2i, view: &View) -> Vector2f {
unsafe {
Vector2f::from_raw(
ffi::sfRenderTexture_mapPixelToCoords(self.render_texture, point.raw(), view.raw()),
)
Vector2f::from_raw(ffi::sfRenderTexture_mapPixelToCoords(
self.render_texture,
point.raw(),
view.raw(),
))
}
}
}
Expand All @@ -34,7 +36,7 @@ fn issue1420() {
r#"
# Getting started
...
"#
"#,
)
.running(waltz)
}
Expand All @@ -58,3 +60,50 @@ fn query(conn: &Connection) -> Result<()> {

Ok(())
}

// #1449
fn future_rayon_wait_1_thread() {
// run with only 1 worker thread; this would deadlock if we couldn't make progress
let mut result = None;
ThreadPool::new(Configuration::new().num_threads(1))
.unwrap()
.install(|| {
scope(|s| {
use std::sync::mpsc::channel;
let (tx, rx) = channel();
let a = s.spawn_future(lazy(move || Ok::<usize, ()>(rx.recv().unwrap())));
// ^^^^ FIXME: why is this needed?
let b = s.spawn_future(a.map(|v| v + 1));
let c = s.spawn_future(b.map(|v| v + 1));
s.spawn(move |_| tx.send(20).unwrap());
result = Some(c.rayon_wait().unwrap());
});
});
assert_eq!(result, Some(22));
}

// #1494
impl Cursor {
fn foo() {
self.cur_type()
.num_template_args()
.or_else(|| {
let n: c_int = unsafe { clang_Cursor_getNumTemplateArguments(self.x) };

if n >= 0 {
Some(n as u32)
} else {
debug_assert_eq!(n, -1);
None
}
})
.or_else(|| {
let canonical = self.canonical();
if canonical != *self {
canonical.num_template_args()
} else {
None
}
});
}
}
Loading