summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorLemonBoy <LemonBoy@users.noreply.github.com>2019-01-23 11:10:51 +0100
committerAndreas Rumpf <rumpf_a@web.de>2019-01-23 11:10:51 +0100
commit9c0e5c4c074ac9d4db7b215ba202ad9e09f18254 (patch)
tree79e7b9b562f01396d4ec0d2b4fc8a0524ad4ae97 /compiler
parent7fcf51248bb5c37cf49765bac020fa7bc86dea34 (diff)
downloadNim-9c0e5c4c074ac9d4db7b215ba202ad9e09f18254.tar.gz
Harmonize the var/let and const handling (#10410)
Fixes #10333
Diffstat (limited to 'compiler')
-rw-r--r--compiler/semstmts.nim18
1 files changed, 14 insertions, 4 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index cac24d33e..5e9d5d9c5 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -440,11 +440,11 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
     if a.kind notin {nkIdentDefs, nkVarTuple, nkConstDef}: illFormedAst(a, c.config)
     checkMinSonsLen(a, 3, c.config)
     var length = sonsLen(a)
-    var typ: PType
+
+    var typ: PType = nil
     if a.sons[length-2].kind != nkEmpty:
       typ = semTypeNode(c, a.sons[length-2], nil)
-    else:
-      typ = nil
+
     var def: PNode = c.graph.emptyNode
     if a.sons[length-1].kind != nkEmpty:
       def = semExprWithType(c, a.sons[length-1], {efAllowDestructor})
@@ -582,9 +582,19 @@ proc semConst(c: PContext, n: PNode): PNode =
     if def == nil:
       localError(c.config, a.sons[length-1].info, errConstExprExpected)
       continue
+
+    if def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro:
+      # prevent the all too common 'const x = int' bug:
+      localError(c.config, def.info, "'typedesc' metatype is not valid here; typed '=' instead of ':'?")
+      def.typ = errorType(c)
+
     # check type compatibility between def.typ and typ:
     if typ != nil:
-      def = fitRemoveHiddenConv(c, typ, def)
+      if typ.isMetaType:
+        def = inferWithMetatype(c, typ, def)
+        typ = def.typ
+      else:
+        def = fitRemoveHiddenConv(c, typ, def)
     else:
       typ = def.typ
     if typ == nil: