summary refs log tree commit diff stats
path: root/tests/stdlib
diff options
context:
space:
mode:
authorJacek Sieka <arnetheduck@gmail.com>2024-03-03 15:40:53 +0100
committerGitHub <noreply@github.com>2024-03-03 15:40:53 +0100
commita1e41930f886986fe4153150bf08de9b84d81c6b (patch)
tree20ba09a04a9556c88fa726653517f2b246c93d84 /tests/stdlib
parent24fbacc63fe8c7a36c77a35bede98462607e950e (diff)
downloadNim-a1e41930f886986fe4153150bf08de9b84d81c6b.tar.gz
strformat: detect format string errors at compile-time (#23356)
This also prevents unwanted `raises: [ValueError]` effects from bubbling
up from correct format strings which makes `fmt` broadly unusable with
`raises`.

The old runtime-based `formatValue` overloads are kept for
backwards-compatibility, should anyone be using runtime format strings.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'tests/stdlib')
-rw-r--r--tests/stdlib/tstrformat.nim15
1 files changed, 14 insertions, 1 deletions
diff --git a/tests/stdlib/tstrformat.nim b/tests/stdlib/tstrformat.nim
index 3c0d55c1d..ff406f898 100644
--- a/tests/stdlib/tstrformat.nim
+++ b/tests/stdlib/tstrformat.nim
@@ -562,7 +562,7 @@ proc main() =
     doAssert &"""{(if true: "'" & "'" & ')' else: "")}""" == "'')"
     doAssert &"{(if true: \"\'\" & \"'\" & ')' else: \"\")}" == "'')"
     doAssert fmt"""{(if true: "'" & ')' else: "")}""" == "')"
-  
+
   block: # issue #20381
     var ss: seq[string]
     template myTemplate(s: string) =
@@ -573,5 +573,18 @@ proc main() =
     foo()
     doAssert ss == @["hello", "hello"]
 
+  block:
+    proc noraises() {.raises: [].} =
+      const
+        flt = 0.0
+        str = "str"
+
+      doAssert fmt"{flt} {str}" == "0.0 str"
+
+    noraises()
+
+  block:
+    doAssert not compiles(fmt"{formatting errors detected at compile time")
+
 static: main()
 main()