summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authoree7 <45465154+ee7@users.noreply.github.com>2022-08-19 21:40:53 +0200
committerGitHub <noreply@github.com>2022-08-19 15:40:53 -0400
commite8657c7107761fae7a8b5559df3b88165c238a12 (patch)
treeba65f39bf4fd3dd14fb9f695d4d6c01aa9c37ae2 /compiler
parent7fe6dedb623b9bc781df9e68b53e354c73034090 (diff)
downloadNim-e8657c7107761fae7a8b5559df3b88165c238a12.tar.gz
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
Diffstat (limited to 'compiler')
-rw-r--r--compiler/evalffi.nim6
-rw-r--r--compiler/tccgen.nim8
2 files changed, 7 insertions, 7 deletions
diff --git a/compiler/evalffi.nim b/compiler/evalffi.nim
index d1d88a1fa..37edef86c 100644
--- a/compiler/evalffi.nim
+++ b/compiler/evalffi.nim
@@ -65,16 +65,16 @@ proc importcSymbol*(conf: ConfigRef, sym: PSym): PNode =
     if (lib.isNil or lib.kind == libHeader) and not gExeHandle.isNil:
       libPathMsg = "current exe: " & getAppFilename() & " nor libc: " & libcDll
       # first try this exe itself:
-      theAddr = gExeHandle.symAddr(name)
+      theAddr = gExeHandle.symAddr(name.cstring)
       # then try libc:
       if theAddr.isNil:
         let dllhandle = getDll(conf, gDllCache, libcDll, sym.info)
-        theAddr = dllhandle.symAddr(name)
+        theAddr = dllhandle.symAddr(name.cstring)
     elif not lib.isNil:
       let dll = if lib.kind == libHeader: libcDll else: lib.path.strVal
       libPathMsg = dll
       let dllhandle = getDll(conf, gDllCache, dll, sym.info)
-      theAddr = dllhandle.symAddr(name)
+      theAddr = dllhandle.symAddr(name.cstring)
     if theAddr.isNil: globalError(conf, sym.info,
       "cannot import symbol: " & name & " from " & libPathMsg)
     result.intVal = cast[ByteAddress](theAddr)
diff --git a/compiler/tccgen.nim b/compiler/tccgen.nim
index ba553906a..83c891ca8 100644
--- a/compiler/tccgen.nim
+++ b/compiler/tccgen.nim
@@ -48,15 +48,15 @@ proc setupEnvironment =
   var tinycRoot = nimDir / tinyPrefix
   let libpath = nimDir / "lib"
 
-  addIncludePath(gTinyC, libpath)
+  addIncludePath(gTinyC, cstring(libpath))
   when defined(windows):
-    addSysincludePath(gTinyC, tinycRoot / "tinyc/win32/include")
-  addSysincludePath(gTinyC, tinycRoot / "tinyc/include")
+    addSysincludePath(gTinyC, cstring(tinycRoot / "tinyc/win32/include"))
+  addSysincludePath(gTinyC, cstring(tinycRoot / "tinyc/include"))
   when defined(windows):
     defineSymbol(gTinyC, "_WIN32", nil)
     # we need Mingw's headers too:
     var gccbin = getConfigVar("gcc.path") % ["nim", tinycRoot]
-    addSysincludePath(gTinyC, gccbin /../ "include")
+    addSysincludePath(gTinyC, cstring(gccbin /../ "include"))
     #addFile(tinycRoot / r"tinyc\win32\wincrt1.o")
     addFile(tinycRoot / r"tinyc\win32\alloca86.o")
     addFile(tinycRoot / r"tinyc\win32\chkstk.o")