Skip to content

Commit

Permalink
Fix evaluation of const expression w/ converters
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonBoy committed Jan 31, 2019
1 parent ec6e568 commit f6f789b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 7 additions & 6 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 ':'?")
Expand All @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/vm/teval1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f6f789b

Please sign in to comment.