Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert #20719; relieve std/assertions of the sysFatal dep #20743

Merged
merged 2 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/std/assertions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

## This module implements assertion handling.

import system/fatal
Copy link
Member Author

@ringabout ringabout Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was actually

when not declared(sysFatal):
  include "system/fatal"

before. I think it is better to do what std/syncio did, which was done years ago.


import std/private/miscdollars
# ---------------------------------------------------------------------------
# helpers
Expand All @@ -30,7 +28,7 @@ when not defined(nimHasSinkInference):

proc raiseAssert*(msg: string) {.noinline, noreturn, nosinks.} =
## Raises an `AssertionDefect` with `msg`.
sysFatal(AssertionDefect, msg)
raise newException(AssertionDefect, msg)

proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
## Raises an `AssertionDefect` with `msg`, but this is hidden
Expand Down
21 changes: 8 additions & 13 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1637,13 +1637,8 @@ when not defined(nimscript):
## for debug builds. Since it's usually used for debugging, this
## is proclaimed to have no IO effect!


when defined(nimHasExceptionsQuery):
const gotoBasedExceptions = compileOption("exceptions", "goto")
else:
const gotoBasedExceptions = false

import system/fatal
when not declared(sysFatal):
include "system/fatal"

when not defined(nimscript):
{.push stackTrace: off, profiler: off.}
Expand All @@ -1656,6 +1651,12 @@ when not defined(nimscript):
when defined(nimV2):
include system/arc

template newException*(exceptn: typedesc, message: string;
parentException: ref Exception = nil): untyped =
## Creates an exception object of type `exceptn` and sets its `msg` field
## to `message`. Returns the new exception object.
(ref exceptn)(msg: message, parent: parentException)

when not defined(nimPreviewSlimSystem):
{.deprecated: "assertions is about to move out of system; use `-d:nimPreviewSlimSystem` and import `std/assertions`".}
import std/assertions
Expand Down Expand Up @@ -1863,12 +1864,6 @@ proc debugEcho*(x: varargs[typed, `$`]) {.magic: "Echo", noSideEffect,
## for debugging routines marked as `noSideEffect
## <manual.html#pragmas-nosideeffect-pragma>`_.

template newException*(exceptn: typedesc, message: string;
parentException: ref Exception = nil): untyped =
## Creates an exception object of type `exceptn` and sets its `msg` field
## to `message`. Returns the new exception object.
(ref exceptn)(msg: message, parent: parentException)

when hostOS == "standalone" and defined(nogc):
proc nimToCStringConv(s: NimString): cstring {.compilerproc, inline.} =
if s == nil or s.len == 0: result = cstring""
Expand Down
19 changes: 12 additions & 7 deletions lib/system/fatal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@

{.push profiler: off.}

when defined(nimHasExceptionsQuery):
const gotoBasedExceptions = compileOption("exceptions", "goto")
else:
const gotoBasedExceptions = false

when hostOS == "standalone":
include "$projectpath/panicoverride"

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

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

elif (defined(nimQuirky) or defined(nimPanics)) and not defined(nimscript):
import system/ansi_c
import ansi_c

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

func sysFatal*(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
func sysFatal(exceptn: typedesc, 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 @@ -41,14 +46,14 @@ elif (defined(nimQuirky) or defined(nimPanics)) and not defined(nimscript):
cstderr.rawWrite buf
quit 1

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

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

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

{.pop.}
2 changes: 1 addition & 1 deletion tests/assert/tassert_c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tassert_c.nim(35) tassert_c
tassert_c.nim(34) foo
assertions.nim(*) failedAssertImpl
assertions.nim(*) raiseAssert
fatal.nim(*) sysFatal"""
"""

proc tmatch(x, p: string): bool =
var i = 0
Expand Down
9 changes: 5 additions & 4 deletions tests/errmsgs/t9768.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
discard """
errormsg: "unhandled exception:"
file: "system/fatal.nim"
errormsg: "unhandled exception: t9768.nim(24, 12) `a < 4` [AssertionDefect]"
file: "std/assertions.nim"
nimout: '''
stack trace: (most recent call last)
t9768.nim(28, 33) main
t9768.nim(23, 11) foo1
t9768.nim(29, 33) main
t9768.nim(24, 11) foo1
'''
"""

Expand All @@ -17,6 +17,7 @@ t9768.nim(23, 11) foo1




## line 20

proc foo1(a: int): auto =
Expand Down