summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-11-19 09:14:41 +0100
committerAraq <rumpf_a@web.de>2015-11-26 17:46:05 +0100
commita2480efd68e19a18342fdb01e11aa338dadfe5ab (patch)
tree8af2188f0d37390b37fce23e8fc7b0928f07fd27
parent0dcaf27b641181127b496db4ac5bd366cc7c7e5c (diff)
downloadNim-a2480efd68e19a18342fdb01e11aa338dadfe5ab.tar.gz
allow 'nil ref T' as a first step which does nothing
-rw-r--r--compiler/semtypes.nim22
-rw-r--r--lib/system.nim6
2 files changed, 19 insertions, 9 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 65cb9421b..bb59a9e75 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -135,13 +135,19 @@ proc semAnyRef(c: PContext; n: PNode; kind: TTypeKind; prev: PType): PType =
     checkMinSonsLen(n, 1)
     var base = semTypeNode(c, n.lastSon, nil)
     result = newOrPrevType(kind, prev, c)
+    var isNilable = false
     # check every except the last is an object:
     for i in isCall .. n.len-2:
-      let region = semTypeNode(c, n[i], nil)
-      if region.skipTypes({tyGenericInst}).kind notin {tyError, tyObject}:
-        message n[i].info, errGenerated, "region needs to be an object type"
-      addSonSkipIntLit(result, region)
+      let ni = n[i]
+      if ni.kind == nkNilLit:
+        isNilable = true
+      else:
+        let region = semTypeNode(c, ni, nil)
+        if region.skipTypes({tyGenericInst}).kind notin {tyError, tyObject}:
+          message n[i].info, errGenerated, "region needs to be an object type"
+        addSonSkipIntLit(result, region)
     addSonSkipIntLit(result, base)
+    #if not isNilable: result.flags.incl tfNotNil
 
 proc semVarType(c: PContext, n: PNode, prev: PType): PType =
   if sonsLen(n) == 1:
@@ -1169,6 +1175,14 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
       result = semTypeNode(c, b, prev)
     elif ident != nil and ident.id == ord(wDotDot):
       result = semRangeAux(c, n, prev)
+    elif n[0].kind == nkNilLit and n.len == 2:
+      result = semTypeNode(c, n.sons[1], prev)
+      if result.skipTypes({tyGenericInst}).kind in NilableTypes+GenericTypes:
+        if tfNotNil in result.flags:
+          result = freshType(result, prev)
+          result.flags.excl(tfNotNil)
+      else:
+        localError(n.info, errGenerated, "invalid type")
     elif n[0].kind notin nkIdentKinds:
       result = semTypeExpr(c, n)
     else:
diff --git a/lib/system.nim b/lib/system.nim
index c5dd58c7b..2fb18fb51 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2584,11 +2584,7 @@ when not defined(JS): #and not defined(nimscript):
 
   when hasAlloc:
     var
-      strDesc: TNimType
-
-    strDesc.size = sizeof(string)
-    strDesc.kind = tyString
-    strDesc.flags = {ntfAcyclic}
+      strDesc = TNimType(size: sizeof(string), kind: tyString, flags: {ntfAcyclic})
 
   when not defined(nimscript):
     include "system/ansi_c"