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>
(cherry picked from commit a1e41930f8)
This commit is contained in:
Jacek Sieka
2024-03-03 15:40:53 +01:00
committed by narimiran
parent 1ae6deda61
commit d389310bb9
2 changed files with 114 additions and 43 deletions

View File

@@ -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()