Skip to content

Commit bab5e30

Browse files
narimiranAraq
authored andcommitted
fixes #10963, disallow implicit mixing of strings and ints/floats (#11292)
1 parent f94ec36 commit bab5e30

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

compiler/astalgo.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ proc value(this: var DebugPrinter; value: string) =
425425
proc value(this: var DebugPrinter; value: BiggestInt) =
426426
if this.useColor:
427427
this.res.add numberStyle
428-
this.res.add value
428+
this.res.addInt value
429429
if this.useColor:
430430
this.res.add resetStyle
431431

@@ -460,7 +460,7 @@ template earlyExit(this: var DebugPrinter; n: PType | PNode | PSym) =
460460
if this.useColor:
461461
this.res.add backrefStyle
462462
this.res.add "<defined "
463-
this.res.add(this.currentLine - index)
463+
this.res.addInt(this.currentLine - index)
464464
this.res.add " lines upwards>"
465465
if this.useColor:
466466
this.res.add resetStyle

compiler/cgen.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ proc cgFormatValue(result: var string; value: string): void =
111111
result.add value
112112

113113
proc cgFormatValue(result: var string; value: BiggestInt): void =
114-
result.add value
114+
result.addInt value
115115

116116
# TODO: please document
117117
macro ropecg(m: BModule, frmt: static[FormatStr], args: untyped): Rope =

compiler/dfa.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ proc codeListing(c: ControlFlowGraph, result: var string, start=0; last = -1) =
8282
result.add renderTree(c[i].n)
8383
of goto, fork, join:
8484
result.add "L"
85-
result.add c[i].dest+i
85+
result.addInt c[i].dest+i
8686
result.add("\t#")
8787
result.add(debugInfo(c[i].n.info))
8888
result.add("\n")

compiler/ndi.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type
2020

2121
proc doWrite(f: var NdiFile; s: PSym; conf: ConfigRef) =
2222
f.buf.setLen 0
23-
f.buf.add s.info.line.int
23+
f.buf.addInt s.info.line.int
2424
f.buf.add "\t"
25-
f.buf.add s.info.col.int
25+
f.buf.addInt s.info.col.int
2626
f.f.write(s.name.s, "\t")
2727
f.f.writeRope(s.loc.r)
2828
f.f.writeLine("\t", toFullPath(conf, s.info), "\t", f.buf)

lib/pure/json.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,12 @@ proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
692692
of JInt:
693693
if lstArr: result.indent(currIndent)
694694
when defined(js): result.add($node.num)
695-
else: result.add(node.num)
695+
else: result.addInt(node.num)
696696
of JFloat:
697697
if lstArr: result.indent(currIndent)
698698
# Fixme: implement new system.add ops for the JS target
699699
when defined(js): result.add($node.fnum)
700-
else: result.add(node.fnum)
700+
else: result.addFloat(node.fnum)
701701
of JBool:
702702
if lstArr: result.indent(currIndent)
703703
result.add(if node.bval: "true" else: "false")
@@ -773,10 +773,10 @@ proc toUgly*(result: var string, node: JsonNode) =
773773
node.str.escapeJson(result)
774774
of JInt:
775775
when defined(js): result.add($node.num)
776-
else: result.add(node.num)
776+
else: result.addInt(node.num)
777777
of JFloat:
778778
when defined(js): result.add($node.fnum)
779-
else: result.add(node.fnum)
779+
else: result.addFloat(node.fnum)
780780
of JBool:
781781
result.add(if node.bval: "true" else: "false")
782782
of JNull:

lib/system/strmantle.nim

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ proc hashString(s: string): int {.compilerproc.} =
4040
h = h +% h shl 15
4141
result = h
4242

43-
proc add*(result: var string; x: int64) =
43+
proc addInt*(result: var string; x: int64) =
4444
## Converts integer to its string representation and appends it to `result`.
4545
##
4646
## .. code-block:: Nim
4747
## var
4848
## a = "123"
4949
## b = 45
50-
## a.add(b) # a <- "12345"
50+
## a.addInt(b) # a <- "12345"
5151
let base = result.len
5252
setLen(result, base + sizeof(x)*4)
5353
var i = 0
@@ -66,18 +66,22 @@ proc add*(result: var string; x: int64) =
6666
for j in 0..i div 2 - 1:
6767
swap(result[base+j], result[base+i-j-1])
6868

69+
proc add*(result: var string; x: int64) {.deprecated:
70+
"Deprecated since v0.20, use 'addInt'".} =
71+
addInt(result, x)
72+
6973
proc nimIntToStr(x: int): string {.compilerRtl.} =
7074
result = newStringOfCap(sizeof(x)*4)
71-
result.add x
75+
result.addInt x
7276

73-
proc add*(result: var string; x: float) =
77+
proc addFloat*(result: var string; x: float) =
7478
## Converts float to its string representation and appends it to `result`.
7579
##
7680
## .. code-block:: Nim
7781
## var
7882
## a = "123"
7983
## b = 45.67
80-
## a.add(b) # a <- "12345.67"
84+
## a.addFloat(b) # a <- "12345.67"
8185
when nimvm:
8286
result.add $x
8387
else:
@@ -113,9 +117,13 @@ proc add*(result: var string; x: float) =
113117
result.add buf[i]
114118
inc i
115119

120+
proc add*(result: var string; x: float) {.deprecated:
121+
"Deprecated since v0.20, use 'addFloat'".} =
122+
addFloat(result, x)
123+
116124
proc nimFloatToStr(f: float): string {.compilerproc.} =
117125
result = newStringOfCap(8)
118-
result.add f
126+
result.addFloat f
119127

120128
proc c_strtod(buf: cstring, endptr: ptr cstring): float64 {.
121129
importc: "strtod", header: "<stdlib.h>", noSideEffect.}
@@ -284,7 +292,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
284292

285293
proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
286294
result = newStringOfCap(sizeof(x)*4)
287-
result.add x
295+
result.addInt x
288296

289297
proc nimBoolToStr(x: bool): string {.compilerRtl.} =
290298
return if x: "true" else: "false"

0 commit comments

Comments
 (0)