Skip to content

Commit

Permalink
reserve sysFatal for Defect (nim-lang#22158)
Browse files Browse the repository at this point in the history
Per manual, `panics:on` affects _only_ `Defect`:s - thus `sysFatal`
should not redirect any other exceptions.

Also, when `sysFatal` is used in `nimPanics` mode, it should use regular
exception handling pipeline to ensure exception hooks are called
consistently for all raised defects.
  • Loading branch information
arnetheduck authored Nov 6, 2023
1 parent eb8824d commit 58c4431
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
15 changes: 6 additions & 9 deletions lib/std/syncio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,11 @@ proc c_fprintf(f: File, frmt: cstring): cint {.
proc c_fputc(c: char, f: File): cint {.
importc: "fputc", header: "<stdio.h>".}

template sysFatal(exc, msg) =
raise newException(exc, msg)

proc raiseEIO(msg: string) {.noinline, noreturn.} =
sysFatal(IOError, msg)
raise newException(IOError, msg)

proc raiseEOF() {.noinline, noreturn.} =
sysFatal(EOFError, "EOF reached")
raise newException(EOFError, "EOF reached")

proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".}

Expand Down Expand Up @@ -764,7 +761,7 @@ proc open*(filename: string,
##
## The file handle associated with the resulting `File` is not inheritable.
if not open(result, filename, mode, bufSize):
sysFatal(IOError, "cannot open: " & filename)
raise newException(IOError, "cannot open: " & filename)

proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign.} =
## Sets the position of the file pointer that is used for read/write
Expand Down Expand Up @@ -852,7 +849,7 @@ proc readFile*(filename: string): string {.tags: [ReadIOEffect], benign.} =
finally:
close(f)
else:
sysFatal(IOError, "cannot open: " & filename)
raise newException(IOError, "cannot open: " & filename)

proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} =
## Opens a file named `filename` for writing. Then writes the
Expand All @@ -865,7 +862,7 @@ proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} =
finally:
close(f)
else:
sysFatal(IOError, "cannot open: " & filename)
raise newException(IOError, "cannot open: " & filename)

proc writeFile*(filename: string, content: openArray[byte]) {.since: (1, 1).} =
## Opens a file named `filename` for writing. Then writes the
Expand Down Expand Up @@ -895,7 +892,7 @@ proc readLines*(filename: string, n: Natural): seq[string] =
finally:
close(f)
else:
sysFatal(IOError, "cannot open: " & filename)
raise newException(IOError, "cannot open: " & filename)

template readLines*(filename: string): seq[
string] {.deprecated: "use readLines with two arguments".} =
Expand Down
2 changes: 1 addition & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2832,7 +2832,7 @@ when notJSnotNims:
hostOS != "any"

proc raiseEIO(msg: string) {.noinline, noreturn.} =
sysFatal(IOError, msg)
raise newException(IOError, msg)

proc echoBinSafe(args: openArray[string]) {.compilerproc.} =
when defined(androidNDK):
Expand Down
2 changes: 1 addition & 1 deletion lib/system/channels_builtin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ proc llRecv(q: PRawChannel, res: pointer, typ: PNimType) =
q.ready = false
if typ != q.elemType:
releaseSys(q.lock)
sysFatal(ValueError, "cannot receive message of wrong type")
raise newException(ValueError, "cannot receive message of wrong type")
rawRecv(q, res, typ)
if q.maxItems > 0 and q.count == q.maxItems - 1:
# Parent thread is awaiting in send. Wake it up.
Expand Down
14 changes: 7 additions & 7 deletions lib/system/fatal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const
when hostOS == "standalone":
include "$projectpath/panicoverride"

func sysFatal(exceptn: typedesc, message: string) {.inline.} =
func sysFatal(exceptn: typedesc[Defect], message: string) {.inline.} =
panic(message)

func sysFatal(exceptn: typedesc, message, arg: string) {.inline.} =
func sysFatal(exceptn: typedesc[Defect], message, arg: string) {.inline.} =
rawoutput(message)
panic(arg)

elif (quirkyExceptions or defined(nimPanics)) and not defined(nimscript):
elif quirkyExceptions and not defined(nimscript):
import ansi_c

func name(t: typedesc): string {.magic: "TypeTrait".}

func sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
func sysFatal(exceptn: typedesc[Defect], message, arg: string) {.inline, noreturn.} =
when nimvm:
# TODO when doAssertRaises works in CT, add a test for it
raise (ref exceptn)(msg: message & arg)
Expand All @@ -45,14 +45,14 @@ elif (quirkyExceptions or defined(nimPanics)) and not defined(nimscript):
cstderr.rawWrite buf
rawQuit 1

func sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
func sysFatal(exceptn: typedesc[Defect], message: string) {.inline, noreturn.} =
sysFatal(exceptn, message, "")

else:
func sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
func sysFatal(exceptn: typedesc[Defect], message: string) {.inline, noreturn.} =
raise (ref exceptn)(msg: message)

func sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
func sysFatal(exceptn: typedesc[Defect], message, arg: string) {.inline, noreturn.} =
raise (ref exceptn)(msg: message & arg)

{.pop.}

0 comments on commit 58c4431

Please sign in to comment.