Skip to content

Commit

Permalink
fix #13502 a and b now short-circuits semcheck in VM
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Feb 29, 2020
1 parent 1056f9e commit 5cb9dd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,15 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
result[0] = newSymNode(s, n[0].info)
result[1] = semAddrArg(c, n[1], s.name.s == "unsafeAddr")
result.typ = makePtrType(c, result[1].typ)
of mBitandI: # somehow, not mAnd
if c.inStaticContext > 0: # TODO: if c.inStaticContext == c.inStaticContextForConst + 1 ?
result = newTree(nkCall)
result.add newIdentNode(getIdent(c.cache, "nimInternalAndVMM"), n.info)
result.add n[1]
result.add n[2]
result = semExpr(c, result)
else:
result = semDirectOp(c, n, flags)
of mTypeOf:
markUsed(c, n.info, s)
result = semTypeOf(c, n)
Expand Down
7 changes: 7 additions & 0 deletions lib/system/basic_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ proc `and`*(x, y: bool): bool {.magic: "And", noSideEffect.}
## are true).
##
## Evaluation is lazy: if ``x`` is false, ``y`` will not even be evaluated.
## Semantic check is lazy in VM, to allow `when declared(Foo) and T is Foo`

template nimInternalAndVM*(x: bool, y: untyped): bool =
## Internal lowering used by `and` in VM
when x: y
else: false

proc `or`*(x, y: bool): bool {.magic: "Or", noSideEffect.}
## Boolean ``or``; returns true if ``not (not x and not y)`` (if any of
## the arguments is true).
Expand Down

0 comments on commit 5cb9dd1

Please sign in to comment.