summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-08-31 19:24:44 +0200
committerGitHub <noreply@github.com>2019-08-31 19:24:44 +0200
commit15213028b75fc485b5fd1f4461e36a04b2f0102a (patch)
tree0e0fb5b9a800f120f44f8c11bd5981893bbc0470
parent2b565aad89587114148052eabdd430b923c21394 (diff)
parenta6428657e90d68c3993de4fa6ad05c6006378fc3 (diff)
downloadNim-15213028b75fc485b5fd1f4461e36a04b2f0102a.tar.gz
Return typeof(nil) (#12100)
* Allow typeof(nil) as a return type

* $typeof(nil) is now "typeof(nil)", not "nil"
-rw-r--r--compiler/ccgtypes.nim1
-rw-r--r--compiler/jsgen.nim2
-rw-r--r--compiler/types.nim4
-rw-r--r--tests/ccgbugs/tnil_type.nim5
4 files changed, 9 insertions, 3 deletions
diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim
index 1e3826780..5c5999e82 100644
--- a/compiler/ccgtypes.nim
+++ b/compiler/ccgtypes.nim
@@ -155,6 +155,7 @@ proc mapType(conf: ConfigRef; typ: PType): TCTypeKind =
   of tyNone, tyTyped: result = ctVoid
   of tyBool: result = ctBool
   of tyChar: result = ctChar
+  of tyNil: result = ctPtr
   of tySet: result = mapSetType(conf, typ)
   of tyOpenArray, tyArray, tyVarargs, tyUncheckedArray: result = ctArray
   of tyObject, tyTuple: result = ctStruct
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 107fa2b28..1d980ab6f 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -1600,6 +1600,8 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
     result = putToSeq("{}", indirect)
   of tyBool:
     result = putToSeq("false", indirect)
+  of tyNil:
+    result = putToSeq("null", indirect)
   of tyArray:
     let length = toInt(lengthOrd(p.config, t))
     let e = elemType(t)
diff --git a/compiler/types.nim b/compiler/types.nim
index 1473be261..29d880ff6 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -423,7 +423,7 @@ proc rangeToStr(n: PNode): string =
 
 const
   typeToStr: array[TTypeKind, string] = ["None", "bool", "char", "empty",
-    "Alias", "nil", "untyped", "typed", "typeDesc",
+    "Alias", "typeof(nil)", "untyped", "typed", "typeDesc",
     "GenericInvocation", "GenericBody", "GenericInst", "GenericParam",
     "distinct $1", "enum", "ordinal[$1]", "array[$1, $2]", "object", "tuple",
     "set[$1]", "range[$1]", "ptr ", "ref ", "var ", "seq[$1]", "proc",
@@ -1310,7 +1310,7 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
      tyNone, tyForward, tyFromExpr:
     result = t
   of tyNil:
-    if kind != skConst and kind != skParam: result = t
+    if kind != skConst and kind != skParam and kind != skResult: result = t
   of tyString, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCString, tyPointer:
     result = nil
   of tyOrdinal:
diff --git a/tests/ccgbugs/tnil_type.nim b/tests/ccgbugs/tnil_type.nim
index b57e64513..12310dae9 100644
--- a/tests/ccgbugs/tnil_type.nim
+++ b/tests/ccgbugs/tnil_type.nim
@@ -12,4 +12,7 @@ proc f3(_: typedesc) = discard
 f3(typeof(nil))
 
 proc f4[T](_: T) = discard
-f4(nil)
\ No newline at end of file
+f4(nil)
+
+proc f5(): typeof(nil) = nil
+discard f5()