From f6f789bb4db2a367384ba6ad75706edd503de1f8 Mon Sep 17 00:00:00 2001
From: LemonBoy <thatlemon@gmail.com>
Date: Thu, 31 Jan 2019 16:21:52 +0100
Subject: [PATCH] Fix evaluation of const expression w/ converters

Fixes #10514
---
 compiler/semstmts.nim | 13 +++++++------
 tests/vm/teval1.nim   | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index f1778e816176c..4c6910d50917b 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -578,11 +578,9 @@ proc semConst(c: PContext, n: PNode): PNode =
     if a.sons[length-2].kind != nkEmpty:
       typ = semTypeNode(c, a.sons[length-2], nil)
 
-    var def = semConstExpr(c, a.sons[length-1])
-    if def == nil:
-      localError(c.config, a.sons[length-1].info, errConstExprExpected)
-      continue
-
+    # do not evaluate the node here since the type compatibility check below may
+    # add a converter
+    var def = semExprWithType(c, a[^1])
     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 ':'?")
@@ -597,7 +595,10 @@ proc semConst(c: PContext, n: PNode): PNode =
         def = fitRemoveHiddenConv(c, typ, def)
     else:
       typ = def.typ
-    if typ == nil:
+
+    # evaluate the node
+    def = semConstExpr(c, def)
+    if def == nil:
       localError(c.config, a.sons[length-1].info, errConstExprExpected)
       continue
     if typeAllowed(typ, skConst) != nil and def.kind != nkNilLit:
diff --git a/tests/vm/teval1.nim b/tests/vm/teval1.nim
index 5c323f0e70d35..0316ea2387990 100644
--- a/tests/vm/teval1.nim
+++ b/tests/vm/teval1.nim
@@ -25,3 +25,18 @@ doAssert x == ""
 static:
   var i, j: set[int8] = {}
   var k = i + j
+
+type
+  Obj = object
+    x: int
+
+converter toObj(x: int): Obj = Obj(x: x)
+
+# bug #10514
+block:
+  const
+    b: Obj = 42
+    bar = [b]
+
+  let i_runtime = 0
+  doAssert bar[i_runtime] == b