mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-03 10:24:44 +00:00
Added std/effecttraits.nim (#15462)
This commit is contained in:
@@ -292,7 +292,9 @@ proc mydiv(a, b): int {.raises: [].} =
|
||||
- `system.deepcopy` has to be enabled explicitly for `--gc:arc` and `--gc:orc` via
|
||||
`--deepcopy:on`.
|
||||
|
||||
|
||||
- Added a `std/effecttraits` module for introspection of the inferred `.raise` effects.
|
||||
We hope this enables `async` macros that are precise about the possible exceptions that
|
||||
can be raised.
|
||||
- Added `critbits.toCritBitTree`, similar to `tables.toTable`, creates a new `CritBitTree` with given arguments.
|
||||
|
||||
|
||||
|
||||
@@ -261,3 +261,12 @@ proc registerAdditionalOps*(c: PCtx) =
|
||||
a.setResult osproc.execCmdEx(getString(a, 0), options).toLit
|
||||
registerCallback c, "stdlib.times.getTime", proc (a: VmArgs) {.nimcall.} =
|
||||
setResult(a, times.getTime().toLit)
|
||||
|
||||
registerCallback c, "stdlib.effecttraits.getRaisesListImpl", proc (a: VmArgs) =
|
||||
let fn = getNode(a, 0)
|
||||
if fn.typ != nil and fn.typ.n != nil and fn.typ.n[0].len >= effectListLen and
|
||||
fn.typ.n[0][exceptionEffects] != nil:
|
||||
var list = newNodeI(nkBracket, fn.info)
|
||||
for e in fn.typ.n[0][exceptionEffects]:
|
||||
list.add opMapTypeInstToAst(c.cache, e.typ.skipTypes({tyRef}), e.info)
|
||||
setResult(a, list)
|
||||
|
||||
19
lib/std/effecttraits.nim
Normal file
19
lib/std/effecttraits.nim
Normal file
@@ -0,0 +1,19 @@
|
||||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2018 Nim contributors
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module provides access to the inferred .raises effects
|
||||
## for Nim's macro system.
|
||||
|
||||
import macros
|
||||
|
||||
proc getRaisesListImpl(n: NimNode): NimNode = discard "see compiler/vmops.nim"
|
||||
|
||||
proc getRaisesList*(call: NimNode): NimNode =
|
||||
expectKind call, nnkCallKinds
|
||||
result = getRaisesListImpl(call[0])
|
||||
22
tests/macros/tgetraiseslist.nim
Normal file
22
tests/macros/tgetraiseslist.nim
Normal file
@@ -0,0 +1,22 @@
|
||||
discard """
|
||||
nimout: '''##[ValueError, Gen[string]]##'''
|
||||
"""
|
||||
|
||||
import macros
|
||||
import std / effecttraits
|
||||
|
||||
type
|
||||
Gen[T] = object of CatchableError
|
||||
x: T
|
||||
|
||||
macro m(call: typed): untyped =
|
||||
echo "##", repr getRaisesList(call), "##"
|
||||
result = call
|
||||
|
||||
proc r(inp: int) =
|
||||
if inp == 0:
|
||||
raise newException(ValueError, "bah")
|
||||
elif inp == 1:
|
||||
raise newException(Gen[string], "bahB")
|
||||
|
||||
m r(2)
|
||||
Reference in New Issue
Block a user