Make it so that every feature can be used in panicoverride files (#25300)

Refer to #25298

(cherry picked from commit e7809364b3)
This commit is contained in:
Gianmarco
2026-01-24 16:01:21 +01:00
committed by narimiran
parent 35429f6252
commit d9ed8f2717
12 changed files with 54 additions and 21 deletions

View File

@@ -3116,3 +3116,27 @@ proc arrayWithDefault*[T](size: static int): array[size, T] {.noinit, nodestroy,
## Creates a new array filled with `default(T)`.
for i in 0..size-1:
result[i] = default(T)
when hostOS == "standalone":
# Include panicoverride.nim late so users can use the full extent of the
# language in their custom panic handlers (e.g. macros).
# Users define `proc panic(msg: string)` and `proc rawoutput(msg: string)`.
include "$projectpath/panicoverride"
when not declared(panic):
{.error:
"a panic proc with the following signature must be provided " &
"when compiling with --os:standalone: " &
"`proc panic(msg: string) {.nimcall.}`".}
when not declared(rawoutput):
{.error:
"a rawoutput proc with the following signature must be provided " &
"when compiling with --os:standalone: " &
"`proc rawoutput(msg: string) {.nimcall.}`".}
# Wrappers with exportc that fatal.nim references via importc.
# This way panicoverride keeps old API and can still be included without
# ssymbols being duplicated.
proc nimPanic(s: string) {.exportc, noreturn.} = panic(s)
proc nimRawoutput(s: string) {.exportc.} = rawoutput(s)

View File

@@ -14,14 +14,23 @@ const
quirkyExceptions = compileOption("exceptions", "quirky")
when hostOS == "standalone":
include "$projectpath/panicoverride"
# These procs are defined in panicoverride.nim, which gets included at end
# of system.nim with exportc.
proc nimPanic(msg: string) {.importc: "nimPanic", noreturn.}
proc nimRawoutput(msg: string) {.importc: "nimRawoutput".}
func sysFatal(exceptn: typedesc[Defect], message: string) {.inline.} =
panic(message)
proc sysFatal(exceptn: typedesc[Defect], message: string) {.inline, noreturn, raises: [], tags: [].} =
{.cast(noSideEffect).}:
{.cast(raises: []).}:
{.cast(tags: []).}:
nimPanic(message)
func sysFatal(exceptn: typedesc[Defect], message, arg: string) {.inline.} =
rawoutput(message)
panic(arg)
proc sysFatal(exceptn: typedesc[Defect], message, arg: string) {.inline, noreturn, raises: [], tags: [].} =
{.cast(noSideEffect).}:
{.cast(raises: []).}:
{.cast(tags: []).}:
nimRawoutput(message)
nimPanic(arg)
elif quirkyExceptions and not defined(nimscript):
import ansi_c

View File

@@ -5,11 +5,11 @@ proc exit(code: cint) {.importc, header:"stdlib.h".}
{.push stack_trace: off, profiler:off.}
proc rawoutput(s: cstring) =
printf("RAW: %s\n", s)
proc panic(s: cstring) {.noreturn.} =
printf("PANIC: %s\n", s)
proc rawoutput(s: string) =
printf("RAW: %s\n", s.cstring)
proc panic(s: string) {.noreturn.} =
printf("PANIC: %s\n", s.cstring)
exit(0)
{.pop.}

View File

@@ -4,9 +4,9 @@ proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}
{.push stack_trace: off, profiler:off.}
proc rawoutput(s: string) =
printf("%s\n", s)
printf("%s\n", s.cstring)
proc panic(s: string) =
proc panic(s: string) {.noreturn.} =
rawoutput(s)
exit(1)

View File

@@ -3,7 +3,7 @@ discard """
exitcode: "1"
output: '''
t14444.nim(13) t14444
fatal.nim(53) sysFatal
fatal.nim(62) sysFatal
Error: unhandled exception: index out of bounds, the container is empty [IndexDefect]
'''
"""

View File

@@ -8,7 +8,7 @@ t23536.nim(22) t23536
t23536.nim(17) foo
assertions.nim(45) failedAssertImpl
assertions.nim(40) raiseAssert
fatal.nim(53) sysFatal
fatal.nim(62) sysFatal
"""

View File

@@ -6,7 +6,7 @@ t24974.nim(19) d
t24974.nim(16) s
assertions.nim(45) failedAssertImpl
assertions.nim(40) raiseAssert
fatal.nim(53) sysFatal
fatal.nim(62) sysFatal
Error: unhandled exception: t24974.nim(16, 26) `false` [AssertionDefect]
'''
"""

View File

@@ -3,7 +3,7 @@ discard """
output: '''
First top-level statement of ModuleB
m22469.nim(3) m22469
fatal.nim(53) sysFatal
fatal.nim(62) sysFatal
Error: unhandled exception: over- or underflow [OverflowDefect]
'''
"""

View File

@@ -5,7 +5,7 @@ proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}
{.push stack_trace: off, profiler:off.}
proc rawoutput(s: string) =
printf("%s\n", s)
printf("%s\n", s.cstring)
proc panic(s: string) {.noreturn.} =
rawoutput(s)

View File

@@ -1,7 +1,7 @@
discard """
ccodecheck: "\\i !@('systemInit')"
ccodecheck: "\\i !@('systemDatInit')"
output: "hello"
output: "hi 4778"
"""
# bug #2041: Macros need to be available for os:standalone!
import macros

View File

@@ -5,7 +5,7 @@ proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}
{.push stack_trace: off, profiler:off.}
proc rawoutput(s: string) =
printf("%s\n", s)
printf("%s\n", s.cstring)
proc panic(s: string) {.noreturn.} =
rawoutput(s)

View File

@@ -5,7 +5,7 @@ proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}
{.push stack_trace: off, profiler:off.}
proc rawoutput(s: string) =
printf("%s\n", s)
printf("%s\n", s.cstring)
proc panic(s: string) {.noreturn.} =
rawoutput(s)