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] d2318d9ccf/doc/manual.md (cstring-type)
[2] d2318d9ccf/changelogs/changelog_1_6_0.md (type-system)
This commit is contained in:
ee7 2022-08-19 21:40:53 +02:00 • committed by GitHub
commit e8657c7107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 63 additions and 63 deletions

View file

@ -487,7 +487,7 @@ when defined(createNimHcr):
recursiveDiscovery(modules[curr].imports)
allModulesOrderedByDFS.add(curr)
continue
loadDll(curr)
loadDll(curr.cstring)
# first load all dependencies of the current module and init it after that
recursiveDiscovery(modules[curr].imports)
@ -497,20 +497,20 @@ when defined(createNimHcr):
proc initModules() =
# first init the pointers to hcr functions and also do the registering of typeinfo globals
for curr in modulesToInit:
initHcrData(curr)
initTypeInfoGlobals(curr)
initHcrData(curr.cstring)
initTypeInfoGlobals(curr.cstring)
# for now system always gets fully inited before any other module (including when reloading)
initPointerData(system)
initGlobalScope(system)
initPointerData(system.cstring)
initGlobalScope(system.cstring)
# proceed with the DatInit calls - for all modules - including the main one!
for curr in allModulesOrderedByDFS:
if curr != system:
initPointerData(curr)
initPointerData(curr.cstring)
mainDatInit()
# execute top-level code (in global scope)
for curr in modulesToInit:
if curr != system:
initGlobalScope(curr)
initGlobalScope(curr.cstring)
# cleanup old symbols which are gone now
for curr in modulesToInit:
cleanupSymbols(curr)