Skip to content

Commit

Permalink
Avoid panic on macro inside deeply nested block
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Mar 19, 2019
1 parent 1427e4c commit c9479de
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ impl Shape {
}
}

pub fn saturating_sub_width(&self, width: usize) -> Shape {
self.sub_width(width).unwrap_or(Shape { width: 0, ..*self })
}

pub fn sub_width(&self, width: usize) -> Option<Shape> {
Some(Shape {
width: self.width.checked_sub(width)?,
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
skip_out_of_file_lines_range_visitor!(self, mac.span);

// 1 = ;
let shape = self.shape().sub_width(1).unwrap();
let shape = self.shape().saturating_sub_width(1);
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
self.push_rewrite(mac.span, rewrite);
}
Expand Down
53 changes: 53 additions & 0 deletions tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,56 @@ fn issue3226() {
}
}
}

// #3457
fn issue3457() {
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
println!("Test");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
53 changes: 53 additions & 0 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,56 @@ fn issue3226() {
}
}
}

// #3457
fn issue3457() {
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
println!("Test");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

0 comments on commit c9479de

Please sign in to comment.