From 5c41baf8f7ceb13a9dfddaa342dbac4f7e6fb5b2 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Thu, 14 Jul 2022 01:57:33 -0400 Subject: [PATCH] Updated how nested function calls are overflowed Fixes 5356 --- src/overflow.rs | 50 ++++++++++++++++++++++++++++++++++++++ tests/source/issue_5356.rs | 13 ++++++++++ tests/target/issue_5356.rs | 25 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 tests/source/issue_5356.rs create mode 100644 tests/target/issue_5356.rs diff --git a/src/overflow.rs b/src/overflow.rs index 6bf8cd0c70b..f49e26ba7ee 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -423,6 +423,56 @@ impl<'a> Context<'a> { } } + // A nested call e.g a(b(...)) + // In this case we're trying to figure out how to overflow function `b`. + ast::ExprKind::Call(ref callee, ref args) if self.items.len() == 1 => { + callee.rewrite(self.context, shape).and_then(|callee_str| { + if !callee_str.contains('\n') { + // if the rewritten callee does not contains a newline then return + // the rewritten expression + return expr.rewrite(self.context, shape); + } + + // If the callee string contains a newline (likely because of generics), + // then we'll update how we'll overflow the argument. + + let items = itemize_list( + self.context.snippet_provider, + args.iter(), + self.suffix, + ",", + |item| item.span().lo(), + |item| item.span().hi(), + |item| item.rewrite(self.context, self.nested_shape), + callee.span.hi(), + expr.span.hi(), + true, + ); + let items: Vec<_> = items.collect(); + let tactic = self.default_tactic(&items); + + let shape = if tactic == DefinitiveListTactic::Horizontal { + // if the argument list could be written horizontally, then unindent + // the shape before rewriting. + Shape::legacy( + self.item_max_width, + shape.indent.block_unindent(self.context.config), + ) + } else { + // If the arguments need any other type of list tactic then + // indent the shape before rewriting. + shape.block_indent(self.item_max_width) + }; + + let rewrite = expr.rewrite(self.context, shape)?; + let indentation = shape + .indent + .to_string_with_newline(self.context.config) + .to_string(); + Some(indentation + &rewrite) + }) + } + _ => expr.rewrite(self.context, shape), } } diff --git a/tests/source/issue_5356.rs b/tests/source/issue_5356.rs new file mode 100644 index 00000000000..850e67ad390 --- /dev/null +++ b/tests/source/issue_5356.rs @@ -0,0 +1,13 @@ +fn fail() { + { + { + if true { + return Err(SomeError::OutOfMemory(OutOfMemory::new(mem::size_of::()))); + } else if true { + return Err(SomeError::OutOfMemory(OutOfMemory::new(core::mem::size_of::()))); + } else { + return Err(SomeError::OutOfMemory(OutOfMemory::new(mem::size_of::(aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbb, cccccccccccccc, dddddddddddddddddd, eeeeeeeeeeeeeeeee)))); + } + } + } +} diff --git a/tests/target/issue_5356.rs b/tests/target/issue_5356.rs new file mode 100644 index 00000000000..28c886cf352 --- /dev/null +++ b/tests/target/issue_5356.rs @@ -0,0 +1,25 @@ +fn fail() { + { + { + if true { + return Err(SomeError::OutOfMemory(OutOfMemory::new( + mem::size_of::() + ))); + } else if true { + return Err(SomeError::OutOfMemory(OutOfMemory::new( + core::mem::size_of::() + ))); + } else { + return Err(SomeError::OutOfMemory(OutOfMemory::new( + mem::size_of::( + aaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbb, + cccccccccccccc, + dddddddddddddddddd, + eeeeeeeeeeeeeeeee, + ), + ))); + } + } + } +}