summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKaushal Modi <kaushal.modi@gmail.com>2021-11-03 01:48:30 -0400
committerGitHub <noreply@github.com>2021-11-03 06:48:30 +0100
commit5fed1c05ce7ea9cbeb2b70bb8583e836c981abd4 (patch)
treeab5220bdefae5b14b6137a57c130e66b3dc08baa
parent539bced70df035ddbdbf4c3d9c7fd6074f0b1073 (diff)
downloadNim-5fed1c05ce7ea9cbeb2b70bb8583e836c981abd4.tar.gz
manual: Document the use of `static` as a proc call (#19084)
* manual: Document the use of `static` as a proc call

Also adds tests.

Fixes https://github.com/nim-lang/Nim/issues/16987 .

* Update doc/manual.rst

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

* Use the "bug #NNNN" comment syntax for consistency

Ref:
https://nim-lang.github.io/Nim/contributing.html#writing-tests-stdlib

> Always refer to a GitHub issue using the following exact syntax: bug
for tooling.

* manual: Undocument usage of foo.static

foo.static and foo.static() are not expected to work.

Ref: https://github.com/nim-lang/Nim/pull/19084/files#r741203578

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
-rw-r--r--doc/manual.rst14
-rw-r--r--tests/system/tstatic_callable.nim12
-rw-r--r--tests/system/tstatic_callable_error.nim14
3 files changed, 40 insertions, 0 deletions
diff --git a/doc/manual.rst b/doc/manual.rst
index 4b212fe19..1371dba17 100644
--- a/doc/manual.rst
+++ b/doc/manual.rst
@@ -2975,6 +2975,20 @@ Even some code that has side effects is permitted in a static block:
   static:
     echo "echo at compile time"
 
+`static` can also be used like a routine.
+
+.. code-block:: nim
+
+  proc getNum(a: int): int = a
+
+  # Below calls "echo getNum(123)" at compile time.
+  static:
+    echo getNum(123)
+
+  # Below call evaluates the "getNum(123)" at compile time, but its
+  # result gets used at run time.
+  echo static(getNum(123))
+
 There are limitations on what Nim code can be executed at compile time;
 see `Restrictions on Compile-Time Execution
 <#restrictions-on-compileminustime-execution>`_ for details.
diff --git a/tests/system/tstatic_callable.nim b/tests/system/tstatic_callable.nim
new file mode 100644
index 000000000..92d1fca8d
--- /dev/null
+++ b/tests/system/tstatic_callable.nim
@@ -0,0 +1,12 @@
+# bug #16987
+
+proc getNum(a: int): int = a
+
+# Below calls "doAssert getNum(123) == 123" at compile time.
+static:
+  doAssert getNum(123) == 123
+
+# Below calls evaluate the "getNum(123)" at compile time, but the
+# results of those calls get used at run time.
+doAssert (static getNum(123)) == 123
+doAssert (static(getNum(123))) == 123
diff --git a/tests/system/tstatic_callable_error.nim b/tests/system/tstatic_callable_error.nim
new file mode 100644
index 000000000..c6f1e3d07
--- /dev/null
+++ b/tests/system/tstatic_callable_error.nim
@@ -0,0 +1,14 @@
+# bug #16987
+
+discard """
+errormsg: "cannot evaluate at compile time: inp"
+nimout: '''
+tstatic_callable_error.nim(14, 21) Error: cannot evaluate at compile time: inp'''
+"""
+
+
+# line 10
+proc getNum(a: int): int = a
+
+let inp = 123
+echo (static getNum(inp))