mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-11 11:49:33 +00:00
make implicit cstring conversions explicit (#19488)
The Nim manual says that an implicit conversion to cstring will
eventually not be allowed [1]:
A Nim `string` is implicitly convertible to `cstring` for convenience.
[...]
Even though the conversion is implicit, it is not *safe*: The garbage collector
does not consider a `cstring` to be a root and may collect the underlying
memory. For this reason, the implicit conversion will be removed in future
releases of the Nim compiler. Certain idioms like conversion of a `const` string
to `cstring` are safe and will remain to be allowed.
And from Nim 1.6.0, such a conversion triggers a warning [2]:
A dangerous implicit conversion to `cstring` now triggers a `[CStringConv]` warning.
This warning will become an error in future versions! Use an explicit conversion
like `cstring(x)` in order to silence the warning.
However, some files in this repo produced such a warning. For example,
before this commit, compiling `parsejson.nim` would produce:
/foo/Nim/lib/pure/parsejson.nim(221, 37) Warning: implicit conversion to 'cstring' from a non-const location: my.buf; this will become a compile time error in the future [CStringConv]
/foo/Nim/lib/pure/parsejson.nim(231, 39) Warning: implicit conversion to 'cstring' from a non-const location: my.buf; this will become a compile time error in the future [CStringConv]
This commit resolves the most visible `CStringConv` warnings, making the
cstring conversions explicit.
[1] https://github.com/nim-lang/Nim/blob/d2318d9ccfe6/doc/manual.md#cstring-type
[2] https://github.com/nim-lang/Nim/blob/d2318d9ccfe6/changelogs/changelog_1_6_0.md#type-system
This commit is contained in:
@@ -235,7 +235,7 @@ proc tryExec*(db: DbConn, query: SqlQuery,
|
||||
assert(not db.isNil, "Database not connected.")
|
||||
var q = dbFormat(query, args)
|
||||
var stmt: sqlite3.PStmt
|
||||
if prepare_v2(db, q, q.len.cint, stmt, nil) == SQLITE_OK:
|
||||
if prepare_v2(db, q.cstring, q.len.cint, stmt, nil) == SQLITE_OK:
|
||||
let x = step(stmt)
|
||||
if x in {SQLITE_DONE, SQLITE_ROW}:
|
||||
result = finalize(stmt) == SQLITE_OK
|
||||
@@ -278,7 +278,7 @@ proc setupQuery(db: DbConn, query: SqlQuery,
|
||||
args: varargs[string]): PStmt =
|
||||
assert(not db.isNil, "Database not connected.")
|
||||
var q = dbFormat(query, args)
|
||||
if prepare_v2(db, q, q.len.cint, result, nil) != SQLITE_OK: dbError(db)
|
||||
if prepare_v2(db, q.cstring, q.len.cint, result, nil) != SQLITE_OK: dbError(db)
|
||||
|
||||
proc setupQuery(db: DbConn, stmtName: SqlPrepared): SqlPrepared {.since: (1, 3).} =
|
||||
assert(not db.isNil, "Database not connected.")
|
||||
@@ -653,7 +653,7 @@ proc tryInsertID*(db: DbConn, query: SqlQuery,
|
||||
var q = dbFormat(query, args)
|
||||
var stmt: sqlite3.PStmt
|
||||
result = -1
|
||||
if prepare_v2(db, q, q.len.cint, stmt, nil) == SQLITE_OK:
|
||||
if prepare_v2(db, q.cstring, q.len.cint, stmt, nil) == SQLITE_OK:
|
||||
if step(stmt) == SQLITE_DONE:
|
||||
result = last_insert_rowid(db)
|
||||
if finalize(stmt) != SQLITE_OK:
|
||||
|
||||
Reference in New Issue
Block a user