Fix evaluation of const expression w/ converters

Fixes #10514
This commit is contained in:
LemonBoy
2019-01-31 16:21:52 +01:00
parent ec6e5681da
commit f6f789bb4d
2 changed files with 22 additions and 6 deletions

View File

@@ -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:

View File

@@ -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