summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-11-24 14:49:23 +0800
committerGitHub <noreply@github.com>2022-11-24 07:49:23 +0100
commit27a38a9fce16b053dc57175716778657ce233bec (patch)
treed41253fd48d29025f38f9a1dc6f2343d4f418390
parentef2998778138fd0b9a102a1c48c1eacd98361105 (diff)
downloadNim-27a38a9fce16b053dc57175716778657ce233bec.tar.gz
fix #13790; ptr char (+friends) should not implicitly convert to cstring (#20761)
* fix =#13790 ptr char (+friends) should not implicitly convert to cstring

* Apply suggestions from code review

* first round; compiles on windows

* nimPreviewSlimSystem

* conversion is unsafe, cast needed

* fixes more tests

* fixes asyncnet

* another try another error

* last one

* true

* one more

* why bugs didn't show at once

* add `nimPreviewCstringConversion` switch

* typo

Co-authored-by: xflywind <43030857+xflywind@users.noreply.github.com>
-rw-r--r--changelog.md2
-rw-r--r--compiler/nim.cfg1
-rw-r--r--compiler/sigmatch.nim22
-rw-r--r--tests/stdlib/config.nims3
-rw-r--r--tests/tools/config.nims3
5 files changed, 19 insertions, 12 deletions
diff --git a/changelog.md b/changelog.md
index fe7f43720..1c49b2e55 100644
--- a/changelog.md
+++ b/changelog.md
@@ -42,6 +42,8 @@
 - Enabling `-d:nimPreviewSlimSystem` removes the import of `channels_builtin` in
   in the `system` module.
 
+- Enabling `-d:nimPreviewCstringConversion`, `ptr char`, `ptr array[N, char]` and `ptr UncheckedArray[N, char]` don't support conversion to cstring anymore.
+
 - The `gc:v2` option is removed.
 
 - The `mainmodule` and `m` options are removed.
diff --git a/compiler/nim.cfg b/compiler/nim.cfg
index 6dd53bdc9..20ce3810f 100644
--- a/compiler/nim.cfg
+++ b/compiler/nim.cfg
@@ -6,6 +6,7 @@ define:booting
 define:nimcore
 define:nimPreviewFloatRoundtrip
 define:nimPreviewSlimSystem
+define:nimPreviewCstringConversion
 define:nimPreviewRangeDefault
 threads:off
 
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 73d742f1a..2c4f5d2ea 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -1391,16 +1391,18 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
     of tyNil: result = f.allowsNil
     of tyString: result = isConvertible
     of tyPtr:
-      # ptr[Tag, char] is not convertible to 'cstring' for now:
-      if a.len == 1:
-        let pointsTo = a[0].skipTypes(abstractInst)
-        if pointsTo.kind == tyChar: result = isConvertible
-        elif pointsTo.kind == tyUncheckedArray and pointsTo[0].kind == tyChar:
-          result = isConvertible
-        elif pointsTo.kind == tyArray and firstOrd(nil, pointsTo[0]) == 0 and
-            skipTypes(pointsTo[0], {tyRange}).kind in {tyInt..tyInt64} and
-            pointsTo[1].kind == tyChar:
-          result = isConvertible
+      if isDefined(c.c.config, "nimPreviewCstringConversion"):
+        result = isNone
+      else:
+        if a.len == 1:
+          let pointsTo = a[0].skipTypes(abstractInst)
+          if pointsTo.kind == tyChar: result = isConvertible
+          elif pointsTo.kind == tyUncheckedArray and pointsTo[0].kind == tyChar:
+            result = isConvertible
+          elif pointsTo.kind == tyArray and firstOrd(nil, pointsTo[0]) == 0 and
+              skipTypes(pointsTo[0], {tyRange}).kind in {tyInt..tyInt64} and
+              pointsTo[1].kind == tyChar:
+            result = isConvertible
     else: discard
 
   of tyEmpty, tyVoid:
diff --git a/tests/stdlib/config.nims b/tests/stdlib/config.nims
index ea5d738e2..cf97152ba 100644
--- a/tests/stdlib/config.nims
+++ b/tests/stdlib/config.nims
@@ -1,3 +1,4 @@
 switch("styleCheck", "usages")
 switch("styleCheck", "error")
-switch("define", "nimPreviewSlimSystem")
\ No newline at end of file
+switch("define", "nimPreviewSlimSystem")
+switch("define", "nimPreviewCstringConversion")
\ No newline at end of file
diff --git a/tests/tools/config.nims b/tests/tools/config.nims
index 20eb34633..b4bb92b30 100644
--- a/tests/tools/config.nims
+++ b/tests/tools/config.nims
@@ -1 +1,2 @@
---d:nimPreviewSlimSystem
\ No newline at end of file
+--d:nimPreviewSlimSystem
+--d:nimPreviewCstringConversion
\ No newline at end of file