mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-27 09:43:58 +00:00
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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user