- SU_D(PerlIO_printf(Perl_debug_log, "%p: pop end at at %d [%d]\n",
- ud, PL_savestack_ix, depth));
+
+ /* Skip depths corresponding to scopes for which leave_scope() might not be
+ * called. */
+ while (depth > 1 && PL_scopestack_ix >= i) {
+ I32 j = PL_scopestack[PL_scopestack_ix - i];
+
+ if (j < PL_savestack_ix)
+ break;
+
+ SU_D(PerlIO_printf(Perl_debug_log,
+ "%p: skip scope%*cat depth=%2d scope_ix=%2d new_top=%2d >= cur_base=%2d\n",
+ ud, 6, ' ', depth, PL_scopestack_ix - i, j, PL_savestack_ix));
+
+ SU_UD_DEPTH(ud) = --depth;
+
+ ++i;
+ }
+
+ SU_D(PerlIO_printf(Perl_debug_log,
+ "%p: set destructor at depth=%2d scope_ix=%2d save_ix=%2d\n",
+ ud, depth, PL_scopestack_ix, PL_savestack_ix));