vim-patch:9.1.0999: Vim9: leaking finished exception (#31939)

Problem:  leaking finished exception
          (after v9.1.0984)
Solution: use finish_exception to clean up caught exceptions
          (Yee Cheng Chin)

In Vimscript, v:exception/throwpoint/stacktrace are supposed to reflect
the currently caught exception, and be popped after the exception is
finished (via endtry, finally, or a thrown exception inside catch).
Vim9script does not handle this properly, and leaks them instead. This
is clearly visible when launching GVim with menu enabled.  A caught
exception inside the s:BMShow() in menu.vim would show up when querying
`v:stacktrace` even though the exception was already caught and handled.

To fix this, just use the same functionality as Vimscript by calling
`finish_exception` to properly restore the states. Note that this
assumes `current_exception` is always the same as `caught_stack` which
believe should be the case.

Added tests for this. Also fix up test_stacktrace to properly test the
stack restore behavior where we have nested exceptions in catch blocks
and to also test the vim9script functionality properly.

- Also, remove its dependency on explicitly checking a line number in
  runtest.vim which is a very fragile way to write tests as any minor
  change in runtest.vim (shared among all tests) would require changing
  test_stacktrace.vim. We don't actually need such granularity in the
  test.

closes: vim/vim#16413

2051af1642

Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
This commit is contained in:
zeertzjq 2025-01-10 08:03:44 +08:00 committed by GitHub
parent 7c00e0efbb
commit 846a2019c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,7 @@ func Filepath(name)
endfunc endfunc
func AssertStacktrace(expect, actual) func AssertStacktrace(expect, actual)
call assert_equal(#{lnum: 581, filepath: Filepath('runtest.vim')}, a:actual[0]) call assert_equal(Filepath('runtest.vim'), a:actual[0]['filepath'])
call assert_equal(a:expect, a:actual[-len(a:expect):]) call assert_equal(a:expect, a:actual[-len(a:expect):])
endfunc endfunc
@ -97,6 +97,12 @@ func Test_vstacktrace()
call Xfunc1() call Xfunc1()
catch catch
let stacktrace = v:stacktrace let stacktrace = v:stacktrace
try
call Xfunc1()
catch
let stacktrace_inner = v:stacktrace
endtry
let stacktrace_after = v:stacktrace " should be restored by the exception stack to the previous one
endtry endtry
call assert_equal([], v:stacktrace) call assert_equal([], v:stacktrace)
call AssertStacktrace([ call AssertStacktrace([
@ -104,9 +110,15 @@ func Test_vstacktrace()
\ #{funcref: funcref('Xfunc1'), lnum: 5, filepath: Filepath('Xscript1')}, \ #{funcref: funcref('Xfunc1'), lnum: 5, filepath: Filepath('Xscript1')},
\ #{funcref: funcref('Xfunc2'), lnum: 4, filepath: Filepath('Xscript2')}, \ #{funcref: funcref('Xfunc2'), lnum: 4, filepath: Filepath('Xscript2')},
\ ], stacktrace) \ ], stacktrace)
call AssertStacktrace([
\ #{funcref: funcref('Test_vstacktrace'), lnum: 101, filepath: s:thisfile},
\ #{funcref: funcref('Xfunc1'), lnum: 5, filepath: Filepath('Xscript1')},
\ #{funcref: funcref('Xfunc2'), lnum: 4, filepath: Filepath('Xscript2')},
\ ], stacktrace_inner)
call assert_equal(stacktrace, stacktrace_after)
endfunc endfunc
func Test_zzz_stacktrace_vim9() func Test_stacktrace_vim9()
let lines =<< trim [SCRIPT] let lines =<< trim [SCRIPT]
var stacktrace = getstacktrace() var stacktrace = getstacktrace()
assert_notequal([], stacktrace) assert_notequal([], stacktrace)
@ -122,11 +134,9 @@ func Test_zzz_stacktrace_vim9()
assert_true(has_key(d, 'lnum')) assert_true(has_key(d, 'lnum'))
endfor endfor
endtry endtry
call assert_equal([], v:stacktrace)
[SCRIPT] [SCRIPT]
call CheckDefSuccess(lines) call CheckDefSuccess(lines)
" FIXME: v:stacktrace is not cleared after the exception handling, and this
" test has to be run as the last one because of this.
" call assert_equal([], v:stacktrace)
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab