summary refs log tree commit diff stats
path: root/lib/system
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2022-10-03 07:07:55 +0300
committerGitHub <noreply@github.com>2022-10-03 06:07:55 +0200
commit2cca38d33c50650d1606c6ec09c73af6b8b0c3c8 (patch)
treeb4c3053dc221b84fa576efff2f04d41f48bbf148 /lib/system
parent81e7811e01b22518cb5e533cf75ed5dda0873415 (diff)
downloadNim-2cca38d33c50650d1606c6ec09c73af6b8b0c3c8.tar.gz
pragma for sfCallsite instead of name check + better semantics, test (#20464)
* pragma for sfCallsite instead of name check at every template definition

Not documented because it seems to be for internal use?

Should also make it possible to make comparisons and setops imports, but this doesn't have to be done.

I can reuse a name like `cursor` for the pragma as well, added a new name just to be safe.

* make sfCallsite recursive, add tests
Diffstat (limited to 'lib/system')
-rw-r--r--lib/system/comparisons.nim9
-rw-r--r--lib/system/setops.nim7
2 files changed, 11 insertions, 5 deletions
diff --git a/lib/system/comparisons.nim b/lib/system/comparisons.nim
index eedac9d84..36d4d06a8 100644
--- a/lib/system/comparisons.nim
+++ b/lib/system/comparisons.nim
@@ -125,15 +125,18 @@ proc `<`*[T](x, y: ref T): bool {.magic: "LtPtr", noSideEffect.}
 proc `<`*[T](x, y: ptr T): bool {.magic: "LtPtr", noSideEffect.}
 proc `<`*(x, y: pointer): bool {.magic: "LtPtr", noSideEffect.}
 
-template `!=`*(x, y: untyped): untyped =
+when not defined(nimHasCallsitePragma):
+  {.pragma: callsite.}
+
+template `!=`*(x, y: untyped): untyped {.callsite.} =
   ## Unequals operator. This is a shorthand for `not (x == y)`.
   not (x == y)
 
-template `>=`*(x, y: untyped): untyped =
+template `>=`*(x, y: untyped): untyped {.callsite.} =
   ## "is greater or equals" operator. This is the same as `y <= x`.
   y <= x
 
-template `>`*(x, y: untyped): untyped =
+template `>`*(x, y: untyped): untyped {.callsite.} =
   ## "is greater" operator. This is the same as `y < x`.
   y < x
 
diff --git a/lib/system/setops.nim b/lib/system/setops.nim
index 755eafdb8..b42c8b4a8 100644
--- a/lib/system/setops.nim
+++ b/lib/system/setops.nim
@@ -9,7 +9,10 @@ func incl*[T](x: var set[T], y: T) {.magic: "Incl".} =
     a.incl(4)
     assert a == {1, 2, 3, 4, 5}
 
-template incl*[T](x: var set[T], y: set[T]) =
+when not defined(nimHasCallsitePragma):
+  {.pragma: callsite.}
+
+template incl*[T](x: var set[T], y: set[T]) {.callsite.} =
   ## Includes the set `y` in the set `x`.
   runnableExamples:
     var a = {1, 3, 5, 7}
@@ -27,7 +30,7 @@ func excl*[T](x: var set[T], y: T) {.magic: "Excl".} =
     b.excl(5)
     assert b == {2, 3, 6, 12, 545}
 
-template excl*[T](x: var set[T], y: set[T]) =
+template excl*[T](x: var set[T], y: set[T]) {.callsite.} =
   ## Excludes the set `y` from the set `x`.
   runnableExamples:
     var a = {1, 3, 5, 7}
olor: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
discard """
  output: "done generic smallobj asgn opt"
"""

# bug #5402

import lists

type
  Container[T] = ref object
    obj: T

  ListOfContainers[T] = ref object
    list: DoublyLinkedList[Container[T]]

proc contains[T](this: ListOfContainers[T], obj: T): bool =
  for item in this.list.items():
    if item.obj == obj: return true
  return false

proc newListOfContainers[T](): ListOfContainers[T] =
  new(result)
  result.list = initDoublyLinkedList[Container[T]]()

let q = newListOfContainers[int64]()
if not q.contains(123):
  echo "done generic smallobj asgn opt"