* Fixes #10514

(cherry picked from commit f6f789bb4db2a367384ba6ad75706edd503de1f8)

* Add comment

* Add changelog entry
This commit is contained in:
Clyybber 2019-09-28 19:50:20 +02:00 • committed by Andreas Rumpf
commit 85db42ad8c
3 changed files with 24 additions and 7 deletions

View file

@ -11,6 +11,8 @@
### Breaking changes in the compiler ### Breaking changes in the compiler
- Implicit conversions for `const` behave correctly now, meaning that code like `const SOMECONST = 0.int; procThatTakesInt32(SOMECONST)` will be illegal now.
Simply write `const SOMECONST = 0` instead.
## Library additions ## Library additions

View file

@ -595,11 +595,8 @@ proc semConst(c: PContext, n: PNode): PNode =
if a.sons[length-2].kind != nkEmpty: if a.sons[length-2].kind != nkEmpty:
typ = semTypeNode(c, a.sons[length-2], nil) typ = semTypeNode(c, a.sons[length-2], nil)
var def = semConstExpr(c, a.sons[length-1]) # don't evaluate here since the type compatibility check below may add a converter
if def == nil: var def = semExprWithType(c, a[^1])
localError(c.config, a.sons[length-1].info, errConstExprExpected)
continue
if def.typ.kind == tyProc and def.kind == nkSym: if def.typ.kind == tyProc and def.kind == nkSym:
if def.sym.kind == skMacro: if def.sym.kind == skMacro:
localError(c.config, def.info, errCannotAssignMacroSymbol % "constant") localError(c.config, def.info, errCannotAssignMacroSymbol % "constant")
@ -621,7 +618,10 @@ proc semConst(c: PContext, n: PNode): PNode =
def = fitRemoveHiddenConv(c, typ, def) def = fitRemoveHiddenConv(c, typ, def)
else: else:
typ = def.typ 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) localError(c.config, a.sons[length-1].info, errConstExprExpected)
continue continue
if typeAllowed(typ, skConst) != nil and def.kind != nkNilLit: if typeAllowed(typ, skConst) != nil and def.kind != nkNilLit:
@ -639,7 +639,7 @@ proc semConst(c: PContext, n: PNode): PNode =
b.sons[length-2] = a.sons[length-2] b.sons[length-2] = a.sons[length-2]
b.sons[length-1] = def b.sons[length-1] = def
for j in 0 .. length-3: for j in 0 ..< length-2:
var v = semIdentDef(c, a.sons[j], skConst) var v = semIdentDef(c, a.sons[j], skConst)
if sfGenSym notin v.flags: addInterfaceDecl(c, v) if sfGenSym notin v.flags: addInterfaceDecl(c, v)
elif v.owner == nil: v.owner = getCurrOwner(c) elif v.owner == nil: v.owner = getCurrOwner(c)

View file

@ -25,3 +25,18 @@ doAssert x == ""
static: static:
var i, j: set[int8] = {} var i, j: set[int8] = {}
var k = i + j 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