Skip to content

Commit 54262da

Browse files
authored
Merge pull request rust-lang#3458 from topecongiro/issue-3457
Avoid panic on macro inside deeply nested block
2 parents 1427e4c + c9479de commit 54262da

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

src/shape.rs

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ impl Shape {
228228
}
229229
}
230230

231+
pub fn saturating_sub_width(&self, width: usize) -> Shape {
232+
self.sub_width(width).unwrap_or(Shape { width: 0, ..*self })
233+
}
234+
231235
pub fn sub_width(&self, width: usize) -> Option<Shape> {
232236
Some(Shape {
233237
width: self.width.checked_sub(width)?,

src/visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
536536
skip_out_of_file_lines_range_visitor!(self, mac.span);
537537

538538
// 1 = ;
539-
let shape = self.shape().sub_width(1).unwrap();
539+
let shape = self.shape().saturating_sub_width(1);
540540
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
541541
self.push_rewrite(mac.span, rewrite);
542542
}

tests/source/expr.rs

+53
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,56 @@ fn issue3226() {
479479
}
480480
}
481481
}
482+
483+
// #3457
484+
fn issue3457() {
485+
{
486+
{
487+
{
488+
{
489+
{
490+
{
491+
{
492+
{
493+
{
494+
{
495+
{
496+
{
497+
{
498+
{
499+
{
500+
{
501+
{
502+
{
503+
{
504+
{
505+
{
506+
{
507+
{
508+
{
509+
println!("Test");
510+
}
511+
}
512+
}
513+
}
514+
}
515+
}
516+
}
517+
}
518+
}
519+
}
520+
}
521+
}
522+
}
523+
}
524+
}
525+
}
526+
}
527+
}
528+
}
529+
}
530+
}
531+
}
532+
}
533+
}
534+
}

tests/target/expr.rs

+53
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,56 @@ fn issue3226() {
562562
}
563563
}
564564
}
565+
566+
// #3457
567+
fn issue3457() {
568+
{
569+
{
570+
{
571+
{
572+
{
573+
{
574+
{
575+
{
576+
{
577+
{
578+
{
579+
{
580+
{
581+
{
582+
{
583+
{
584+
{
585+
{
586+
{
587+
{
588+
{
589+
{
590+
{
591+
{
592+
println!("Test");
593+
}
594+
}
595+
}
596+
}
597+
}
598+
}
599+
}
600+
}
601+
}
602+
}
603+
}
604+
}
605+
}
606+
}
607+
}
608+
}
609+
}
610+
}
611+
}
612+
}
613+
}
614+
}
615+
}
616+
}
617+
}

0 commit comments

Comments
 (0)