Added std/effecttraits.nim (#15462)

This commit is contained in:
Andreas Rumpf
2020-10-02 17:22:31 +02:00
committed by GitHub
parent aa1d7fe1e9
commit 1f4b9cebd4
4 changed files with 53 additions and 1 deletions

View File

@@ -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.

View File

@@ -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
View 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])

View 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)