Skip to content

Commit

Permalink
fixes #8603
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Oct 29, 2018
1 parent e44df5d commit f7c0e20
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
9 changes: 8 additions & 1 deletion compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,14 @@ proc semGenericStmt(c: PContext, n: PNode,
n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, ctx)
of nkWhenStmt:
for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmt(c, n.sons[i], flags+{withinMixin}, ctx)
# bug #8603: conditions of 'when' statements are not
# in a 'mixin' context:
let it = n[i]
if it.kind in {nkElifExpr, nkElifBranch}:
n.sons[i].sons[0] = semGenericStmt(c, it[0], flags, ctx)
n.sons[i].sons[1] = semGenericStmt(c, it[1], flags+{withinMixin}, ctx)
else:
n.sons[i] = semGenericStmt(c, it, flags+{withinMixin}, ctx)
of nkWhileStmt:
openScope(c)
for i in countup(0, sonsLen(n)-1):
Expand Down
56 changes: 29 additions & 27 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,6 @@ proc `or`*(x, y: bool): bool {.magic: "Or", noSideEffect.}
proc `xor`*(x, y: bool): bool {.magic: "Xor", noSideEffect.}
## Boolean `exclusive or`; returns true iff ``x != y``.

proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``.

proc new*(T: typedesc): auto =
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it as result value.
##
## When ``T`` is a ref type then the resulting type will be ``T``,
## otherwise it will be ``ref T``.
when (T is ref):
var r: T
else:
var r: ref T
new(r)
return r

const ThisIsSystem = true

proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.}
Expand Down Expand Up @@ -1331,6 +1314,23 @@ proc `is`*[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.}
template `isnot`*(x, y: untyped): untyped = not (x is y)
## Negated version of `is`. Equivalent to ``not(x is y)``.

proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``.

proc new*(T: typedesc): auto =
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it as result value.
##
## When ``T`` is a ref type then the resulting type will be ``T``,
## otherwise it will be ``ref T``.
when (T is ref):
var r: T
else:
var r: ref T
new(r)
return r

proc `of`*[T, S](x: typeDesc[T], y: typeDesc[S]): bool {.magic: "Of", noSideEffect.}
proc `of`*[T, S](x: T, y: typeDesc[S]): bool {.magic: "Of", noSideEffect.}
proc `of`*[T, S](x: T, y: S): bool {.magic: "Of", noSideEffect.}
Expand Down Expand Up @@ -2090,6 +2090,8 @@ when not defined(nimscript) and hasAlloc:
## returns the number of bytes on the shared heap that are owned by the
## process. This is only available when threads are enabled.

proc `|`*(a, b: typedesc): typedesc = discard

when sizeof(int) <= 2:
type IntLikeForCount = int|int8|int16|char|bool|uint8|enum
else:
Expand Down Expand Up @@ -2640,6 +2642,16 @@ proc `<`*[T: tuple](x, y: T): bool =
if c > 0: return false
return false

proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} =
## Special compile-time procedure that checks whether `x` can be compiled
## without any semantic error.
## This can be used to check whether a type supports some operation:
##
## .. code-block:: Nim
## when compiles(3 + 4):
## echo "'+' for integers is available"
discard

proc `$`*[T: tuple|object](x: T): string =
## generic ``$`` operator for tuples that is lifted from the components
## of `x`. Example:
Expand Down Expand Up @@ -4051,16 +4063,6 @@ when hasAlloc:
x[j+i] = item[j]
inc(j)

proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} =
## Special compile-time procedure that checks whether `x` can be compiled
## without any semantic error.
## This can be used to check whether a type supports some operation:
##
## .. code-block:: Nim
## when compiles(3 + 4):
## echo "'+' for integers is available"
discard

when declared(initDebugger):
initDebugger()

Expand Down

0 comments on commit f7c0e20

Please sign in to comment.