summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/ccgtypes.nim4
-rw-r--r--lib/pure/strformat.nim4
-rw-r--r--tests/stdlib/tstrformat.nim13
3 files changed, 18 insertions, 3 deletions
diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim
index 24d3a0dfb..8a60143fb 100644
--- a/compiler/ccgtypes.nim
+++ b/compiler/ccgtypes.nim
@@ -496,7 +496,7 @@ proc genRecordFieldsAux(m: BModule, n: PNode,
               if hasAttribute in CC[cCompiler].props:
                 add(unionBody, "struct __attribute__((__packed__)){" )
               else:
-                addf(unionBody, "#pragma pack(1)$nstruct{", [])
+                addf(unionBody, "#pragma pack(push, 1)$nstruct{", [])
             add(unionBody, a)
             addf(unionBody, "} $1;$n", [sname])
             if tfPacked in rectype.flags and hasAttribute notin CC[cCompiler].props:
@@ -551,7 +551,7 @@ proc getRecordDesc(m: BModule, typ: PType, name: Rope,
     if hasAttribute in CC[cCompiler].props:
       result = structOrUnion(typ) & " __attribute__((__packed__))"
     else:
-      result = "#pragma pack(1)" & tnl & structOrUnion(typ)
+      result = "#pragma pack(push, 1)" & tnl & structOrUnion(typ)
   else:
     result = structOrUnion(typ)
 
diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim
index e04c80794..d1dedb625 100644
--- a/lib/pure/strformat.nim
+++ b/lib/pure/strformat.nim
@@ -221,8 +221,10 @@ template callFormat(res, arg) {.dirty.} =
 template callFormatOption(res, arg, option) {.dirty.} =
   when compiles(format(arg, option, res)):
     format(arg, option, res)
-  else:
+  elif compiles(format(arg, option)):
     res.add format(arg, option)
+  else:
+    format($arg, option, res)
 
 macro fmt*(pattern: string{lit}): untyped =
   ## For a specification of the ``fmt`` macro, see the module level documentation.
diff --git a/tests/stdlib/tstrformat.nim b/tests/stdlib/tstrformat.nim
new file mode 100644
index 000000000..4e5c614a7
--- /dev/null
+++ b/tests/stdlib/tstrformat.nim
@@ -0,0 +1,13 @@
+discard """
+    action: "run"
+"""
+
+import strformat
+
+type Obj = object
+
+proc `$`(o: Obj): string = "foobar"
+
+var o: Obj
+doAssert fmt"{o}" == "foobar"
+doAssert fmt"{o:10}" == "foobar    "
\ No newline at end of file