From ce8d3e02f5b0cdbb9330b9c0ffafabb44ab1100d Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 7 Feb 2025 06:19:53 +0800 Subject: [PATCH] fixes bugs on the Nim manual (#24669) ref https://en.cppreference.com/w/cpp/error/exception/what > Pointer to a null-terminated string with explanatory information. The pointer is guaranteed to be valid at least until the exception object from which it is obtained is destroyed, or until a non-const member function on the exception object is called. The pointer is only valid before `CStdException as e` is destroyed Old examples are broken on macOS arm64 ``` /Users/blue/Desktop/nimony/test4.nim(38) test4 /Users/blue/Desktop/nimony/test4.nim(26) fn /Users/blue/.choosenim/toolchains/nim-#devel/lib/std/assertions.nim(41) failedAssertImpl /Users/blue/.choosenim/toolchains/nim-#devel/lib/std/assertions.nim(36) raiseAssert /Users/blue/.choosenim/toolchains/nim-#devel/lib/system/fatal.nim(53) sysFatal Error: unhandled exception: /Users/blue/Desktop/nimony/test4.nim(26, 3) `$b == "foo2"` [AssertionDefect] ``` (cherry picked from commit e6f6c369ffd3073896716502af20727bd8feaf50) --- doc/manual.md | 10 +++++----- tests/cpp/tmanual_exception.nim | 12 +++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/doc/manual.md b/doc/manual.md index 52f17369be..e20d33b3ee 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -5186,22 +5186,22 @@ caught by reference. Example: proc fn() = let a = initRuntimeError("foo") doAssert $a.what == "foo" - var b: cstring + var b = "" try: raise initRuntimeError("foo2") except CStdException as e: doAssert e is CStdException - b = e.what() - doAssert $b == "foo2" + b = $e.what() + doAssert b == "foo2" try: raise initStdException() except CStdException: discard try: raise initRuntimeError("foo3") except CRuntimeError as e: - b = e.what() + b = $e.what() except CStdException: doAssert false - doAssert $b == "foo3" + doAssert b == "foo3" fn() ``` diff --git a/tests/cpp/tmanual_exception.nim b/tests/cpp/tmanual_exception.nim index a91ccffe4c..891bccee05 100644 --- a/tests/cpp/tmanual_exception.nim +++ b/tests/cpp/tmanual_exception.nim @@ -1,6 +1,4 @@ discard """ - # doesn't work on macos 13 seemingly due to libc++ linking issue https://stackoverflow.com/a/77375947 - disabled: osx targets: cpp """ @@ -18,21 +16,21 @@ proc initStdException(): CStdException {.importcpp: "std::exception()", construc proc fn() = let a = initRuntimeError("foo") doAssert $a.what == "foo" - var b: cstring + var b = "" try: raise initRuntimeError("foo2") except CStdException as e: doAssert e is CStdException - b = e.what() - doAssert $b == "foo2" + b = $e.what() + doAssert b == "foo2" try: raise initStdException() except CStdException: discard try: raise initRuntimeError("foo3") except CRuntimeError as e: - b = e.what() + b = $e.what() except CStdException: doAssert false - doAssert $b == "foo3" + doAssert b == "foo3" fn()